Skip to content
This repository was archived by the owner on Aug 4, 2020. It is now read-only.

Commit c38ab4c

Browse files
Add valid-typeof rule with support for BigInt (#161)
1 parent 59d271d commit c38ab4c

File tree

3 files changed

+208
-1
lines changed

3 files changed

+208
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ original ones as well!).
3333
"babel/object-curly-spacing": 1,
3434
"babel/quotes": 1,
3535
"babel/semi": 1,
36-
"babel/no-unused-expressions": 1
36+
"babel/no-unused-expressions": 1,
37+
"babel/valid-typeof": 1
3738
}
3839
}
3940
```
@@ -49,6 +50,7 @@ Each rule corresponds to a core `eslint` rule, and has the same options.
4950
- `babel/quotes`: doesn't complain about JSX fragment shorthand syntax (`<>foo</>;`)
5051
- `babel/semi`: doesn't fail when using `for await (let something of {})`. Includes class properties (🛠)
5152
- `babel/no-unused-expressions`: doesn't fail when using `do` expressions or [optional chaining](https://github.com/tc39/proposal-optional-chaining) (`a?.b()`).
53+
- `babel/valid-typeof`: doesn't complain when used with [BigInt](https://github.com/tc39/proposal-bigint) (`typeof BigInt(9007199254740991) === 'bigint'`).
5254

5355
#### Deprecated
5456

rules/valid-typeof.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
const ruleComposer = require('eslint-rule-composer');
4+
const eslint = require('eslint');
5+
const validTypeOf = new eslint.Linter().getRules().get('valid-typeof');
6+
7+
module.exports = ruleComposer.filterReports(
8+
validTypeOf,
9+
(problem, metadata) => {
10+
return problem.node.value !== 'bigint';
11+
}
12+
)

tests/rules/valid-typeof.js

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/**
2+
* @fileoverview Ensures that the results of typeof are compared against a valid string
3+
* @author Ian Christian Myers
4+
*/
5+
6+
"use strict";
7+
8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
const rule = require("../../rules/valid-typeof"),
13+
RuleTester = require("../RuleTester");
14+
15+
//------------------------------------------------------------------------------
16+
// Tests
17+
//------------------------------------------------------------------------------
18+
19+
const ruleTester = new RuleTester();
20+
21+
ruleTester.run("valid-typeof", rule, {
22+
valid: [
23+
// Original test cases.
24+
"typeof foo === 'string'",
25+
"typeof foo === 'object'",
26+
"typeof foo === 'function'",
27+
"typeof foo === 'undefined'",
28+
"typeof foo === 'boolean'",
29+
"typeof foo === 'number'",
30+
"'string' === typeof foo",
31+
"'object' === typeof foo",
32+
"'function' === typeof foo",
33+
"'undefined' === typeof foo",
34+
"'boolean' === typeof foo",
35+
"'number' === typeof foo",
36+
"typeof foo === typeof bar",
37+
"typeof foo === baz",
38+
"typeof foo !== someType",
39+
"typeof bar != someType",
40+
"someType === typeof bar",
41+
"someType == typeof bar",
42+
"typeof foo == 'string'",
43+
"typeof(foo) === 'string'",
44+
"typeof(foo) !== 'string'",
45+
"typeof(foo) == 'string'",
46+
"typeof(foo) != 'string'",
47+
"var oddUse = typeof foo + 'thing'",
48+
{
49+
code: "typeof foo === 'number'",
50+
options: [{ requireStringLiterals: true }]
51+
},
52+
{
53+
code: "typeof foo === \"number\"",
54+
options: [{ requireStringLiterals: true }]
55+
},
56+
{
57+
code: "var baz = typeof foo + 'thing'",
58+
options: [{ requireStringLiterals: true }]
59+
},
60+
{
61+
code: "typeof foo === typeof bar",
62+
options: [{ requireStringLiterals: true }]
63+
},
64+
{
65+
code: "typeof foo === `string`",
66+
options: [{ requireStringLiterals: true }],
67+
parserOptions: { ecmaVersion: 6 }
68+
},
69+
{
70+
code: "`object` === typeof foo",
71+
options: [{ requireStringLiterals: true }],
72+
parserOptions: { ecmaVersion: 6 }
73+
},
74+
{
75+
code: "typeof foo === `str${somethingElse}`",
76+
parserOptions: { ecmaVersion: 6 }
77+
},
78+
79+
// Babel-specific test cases.
80+
{
81+
code: "typeof BigInt(Number.MAX_SAFE_INTEGER) === 'bigint'"
82+
},
83+
{
84+
code: "'bigint' === typeof BigInt(Number.MAX_SAFE_INTEGER)"
85+
},
86+
{
87+
code: "typeof BigInt(Number.MAX_SAFE_INTEGER) === 'bigint'",
88+
options: [{ requireStringLiterals: true }]
89+
},
90+
],
91+
invalid: [
92+
{
93+
code: "typeof foo === 'strnig'",
94+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
95+
},
96+
{
97+
code: "'strnig' === typeof foo",
98+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
99+
},
100+
{
101+
code: "if (typeof bar === 'umdefined') {}",
102+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
103+
},
104+
{
105+
code: "typeof foo !== 'strnig'",
106+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
107+
},
108+
{
109+
code: "'strnig' !== typeof foo",
110+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
111+
},
112+
{
113+
code: "if (typeof bar !== 'umdefined') {}",
114+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
115+
},
116+
{
117+
code: "typeof foo != 'strnig'",
118+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
119+
},
120+
{
121+
code: "'strnig' != typeof foo",
122+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
123+
},
124+
{
125+
code: "if (typeof bar != 'umdefined') {}",
126+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
127+
},
128+
{
129+
code: "typeof foo == 'strnig'",
130+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
131+
},
132+
{
133+
code: "'strnig' == typeof foo",
134+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
135+
},
136+
{
137+
code: "if (typeof bar == 'umdefined') {}",
138+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
139+
},
140+
{
141+
code: "if (typeof bar === `umdefined`) {}",
142+
parserOptions: { ecmaVersion: 6 },
143+
errors: [{ message: "Invalid typeof comparison value.", type: "TemplateLiteral" }]
144+
},
145+
{
146+
code: "typeof foo == 'invalid string'",
147+
options: [{ requireStringLiterals: true }],
148+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
149+
},
150+
{
151+
code: "typeof foo == Object",
152+
options: [{ requireStringLiterals: true }],
153+
errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }]
154+
},
155+
{
156+
code: "typeof foo === undefined",
157+
options: [{ requireStringLiterals: true }],
158+
errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }]
159+
},
160+
{
161+
code: "undefined === typeof foo",
162+
options: [{ requireStringLiterals: true }],
163+
errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }]
164+
},
165+
{
166+
code: "undefined == typeof foo",
167+
options: [{ requireStringLiterals: true }],
168+
errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }]
169+
},
170+
{
171+
code: "typeof foo === `undefined${foo}`",
172+
options: [{ requireStringLiterals: true }],
173+
parserOptions: { ecmaVersion: 6 },
174+
errors: [{ message: "Typeof comparisons should be to string literals.", type: "TemplateLiteral" }]
175+
},
176+
{
177+
code: "typeof foo === `${string}`",
178+
options: [{ requireStringLiterals: true }],
179+
parserOptions: { ecmaVersion: 6 },
180+
errors: [{ message: "Typeof comparisons should be to string literals.", type: "TemplateLiteral" }]
181+
},
182+
183+
// Babel-specific test cases.
184+
{
185+
code: "typeof foo === 'bgiint'",
186+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
187+
},
188+
{
189+
code: "'bignit' === typeof foo",
190+
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
191+
},
192+
]
193+
});

0 commit comments

Comments
 (0)