/
no-typeof-undefined.mjs
90 lines (87 loc) · 2.21 KB
/
no-typeof-undefined.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import outdent from 'outdent';
import {getTester} from './utils/test.mjs';
const {test} = getTester(import.meta);
test.snapshot({
valid: [
'typeof a.b',
'typeof a.b > "undefined"',
'a.b === "undefined"',
'void a.b === "undefined"',
'+a.b === "undefined"',
'++a.b === "undefined"',
'a.b++ === "undefined"',
'foo === undefined',
'typeof a.b === "string"',
'typeof foo === "undefined"',
'foo = 2; typeof foo === "undefined"',
'/* globals foo: readonly */ typeof foo === "undefined"',
'/* globals globalThis: readonly */ typeof globalThis === "undefined"',
// Cases we are not checking
'"undefined" === typeof a.b',
'const UNDEFINED = "undefined"; typeof a.b === UNDEFINED',
'typeof a.b === `undefined`',
],
invalid: [
'typeof a.b === "undefined"',
'typeof a.b !== "undefined"',
'typeof a.b == "undefined"',
'typeof a.b != "undefined"',
'typeof a.b == \'undefined\'',
'let foo; typeof foo === "undefined"',
'const foo = 1; typeof foo === "undefined"',
'var foo; typeof foo === "undefined"',
'var foo; var foo; typeof foo === "undefined"',
'for (const foo of bar) typeof foo === "undefined";',
outdent`
let foo;
function bar() {
typeof foo === "undefined";
}
`,
'function foo() {typeof foo === "undefined"}',
'function foo(bar) {typeof bar === "undefined"}',
'function foo({bar}) {typeof bar === "undefined"}',
'function foo([bar]) {typeof bar === "undefined"}',
'typeof foo.bar === "undefined"',
outdent`
import foo from 'foo';
typeof foo.bar === "undefined"
`,
// ASI
outdent`
foo
typeof [] === "undefined";
`,
outdent`
foo
typeof (a ? b : c) === "undefined";
`,
outdent`
function a() {
return typeof // comment
a.b === 'undefined';
}
`,
outdent`
function a() {
return (typeof // ReturnStatement argument is parenthesized
a.b === 'undefined');
}
`,
outdent`
function a() {
return (typeof // UnaryExpression is parenthesized
a.b) === 'undefined';
}
`,
],
});
// `checkGlobalVariables: true`
test.snapshot({
valid: [
],
invalid: [
'typeof undefinedVariableIdentifier === "undefined"',
'typeof Array !== "undefined"',
].map(code => ({code, options: [{checkGlobalVariables: true}]})),
});