Skip to content

Commit a7864a3

Browse files
authored
Fix isUnresolvedVariable (#2693)
1 parent 7e86a8a commit a7864a3

File tree

4 files changed

+131
-3
lines changed

4 files changed

+131
-3
lines changed

rules/utils/is-unresolved-variable.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {findVariable} from '@eslint-community/eslint-utils';
2+
13
/**
24
Checks if the given identifier node is shadowed in the given scope.
35
@@ -7,7 +9,6 @@ Checks if the given identifier node is shadowed in the given scope.
79
*/
810
export default function isUnresolvedVariable(node, context) {
911
const scope = context.sourceCode.getScope(node);
10-
const reference = scope.references
11-
.find(reference => reference.identifier === node);
12-
return !reference.resolved;
12+
const variable = findVariable(scope, node);
13+
return !variable;
1314
}

test/no-typeof-undefined.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ test.snapshot({
1818
'foo = 2; typeof foo === "undefined"',
1919
'/* globals foo: readonly */ typeof foo === "undefined"',
2020
'/* globals globalThis: readonly */ typeof globalThis === "undefined"',
21+
outdent`
22+
function parse() {
23+
switch (typeof value === 'undefined') {}
24+
}
25+
`,
26+
outdent`
27+
/* globals value: readonly */
28+
function parse() {
29+
switch (typeof value === 'undefined') {}
30+
}
31+
`,
2132
// Cases we are not checking
2233
'"undefined" === typeof a.b',
2334
'const UNDEFINED = "undefined"; typeof a.b === UNDEFINED',
@@ -76,6 +87,11 @@ test.snapshot({
7687
a.b) === 'undefined';
7788
}
7889
`,
90+
outdent`
91+
function parse(value) {
92+
switch (typeof value === 'undefined') {}
93+
}
94+
`,
7995
],
8096
});
8197

@@ -86,5 +102,16 @@ test.snapshot({
86102
invalid: [
87103
'typeof undefinedVariableIdentifier === "undefined"',
88104
'typeof Array !== "undefined"',
105+
outdent`
106+
function parse() {
107+
switch (typeof value === 'undefined') {}
108+
}
109+
`,
110+
outdent`
111+
/* globals value: readonly */
112+
function parse() {
113+
switch (typeof value === 'undefined') {}
114+
}
115+
`,
89116
].map(code => ({code, options: [{checkGlobalVariables: true}]})),
90117
});

test/snapshots/no-typeof-undefined.js.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,33 @@ Generated by [AVA](https://avajs.dev).
511511
4 | }␊
512512
`
513513

514+
## invalid(23): function parse(value) { switch (typeof value === 'undefined') {} }
515+
516+
> Input
517+
518+
`␊
519+
1 | function parse(value) {␊
520+
2 | switch (typeof value === 'undefined') {}␊
521+
3 | }␊
522+
`
523+
524+
> Output
525+
526+
`␊
527+
1 | function parse(value) {␊
528+
2 | switch (value === undefined) {}␊
529+
3 | }␊
530+
`
531+
532+
> Error 1/1
533+
534+
`␊
535+
1 | function parse(value) {␊
536+
> 2 | switch (typeof value === 'undefined') {}␊
537+
| ^^^^^^ Compare with \`undefined\` directly instead of using \`typeof\`.␊
538+
3 | }␊
539+
`
540+
514541
## invalid(1): typeof undefinedVariableIdentifier === "undefined"
515542

516543
> Input
@@ -568,3 +595,76 @@ Generated by [AVA](https://avajs.dev).
568595
Suggestion 1/1: Switch to \`… !== undefined\`.␊
569596
1 | Array !== undefined␊
570597
`
598+
599+
## invalid(3): function parse() { switch (typeof value === 'undefined') {} }
600+
601+
> Input
602+
603+
`␊
604+
1 | function parse() {␊
605+
2 | switch (typeof value === 'undefined') {}␊
606+
3 | }␊
607+
`
608+
609+
> Options
610+
611+
`␊
612+
[␊
613+
{␊
614+
"checkGlobalVariables": true␊
615+
}␊
616+
]␊
617+
`
618+
619+
> Error 1/1
620+
621+
`␊
622+
1 | function parse() {␊
623+
> 2 | switch (typeof value === 'undefined') {}␊
624+
| ^^^^^^ Compare with \`undefined\` directly instead of using \`typeof\`.␊
625+
3 | }␊
626+
627+
--------------------------------------------------------------------------------␊
628+
Suggestion 1/1: Switch to \`… === undefined\`.␊
629+
1 | function parse() {␊
630+
2 | switch (value === undefined) {}␊
631+
3 | }␊
632+
`
633+
634+
## invalid(4): /* globals value: readonly */ function parse() { switch (typeof value === 'undefined') {} }
635+
636+
> Input
637+
638+
`␊
639+
1 | /* globals value: readonly */␊
640+
2 | function parse() {␊
641+
3 | switch (typeof value === 'undefined') {}␊
642+
4 | }␊
643+
`
644+
645+
> Options
646+
647+
`␊
648+
[␊
649+
{␊
650+
"checkGlobalVariables": true␊
651+
}␊
652+
]␊
653+
`
654+
655+
> Error 1/1
656+
657+
`␊
658+
1 | /* globals value: readonly */␊
659+
2 | function parse() {␊
660+
> 3 | switch (typeof value === 'undefined') {}␊
661+
| ^^^^^^ Compare with \`undefined\` directly instead of using \`typeof\`.␊
662+
4 | }␊
663+
664+
--------------------------------------------------------------------------------␊
665+
Suggestion 1/1: Switch to \`… === undefined\`.␊
666+
1 | /* globals value: readonly */␊
667+
2 | function parse() {␊
668+
3 | switch (value === undefined) {}␊
669+
4 | }␊
670+
`
193 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)