Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions rules/utils/is-unresolved-variable.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {findVariable} from '@eslint-community/eslint-utils';

/**
Checks if the given identifier node is shadowed in the given scope.

Expand All @@ -7,7 +9,6 @@ Checks if the given identifier node is shadowed in the given scope.
*/
export default function isUnresolvedVariable(node, context) {
const scope = context.sourceCode.getScope(node);
const reference = scope.references
.find(reference => reference.identifier === node);
return !reference.resolved;
const variable = findVariable(scope, node);
return !variable;
}
27 changes: 27 additions & 0 deletions test/no-typeof-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ test.snapshot({
'foo = 2; typeof foo === "undefined"',
'/* globals foo: readonly */ typeof foo === "undefined"',
'/* globals globalThis: readonly */ typeof globalThis === "undefined"',
outdent`
function parse() {
switch (typeof value === 'undefined') {}
}
`,
outdent`
/* globals value: readonly */
function parse() {
switch (typeof value === 'undefined') {}
}
`,
// Cases we are not checking
'"undefined" === typeof a.b',
'const UNDEFINED = "undefined"; typeof a.b === UNDEFINED',
Expand Down Expand Up @@ -76,6 +87,11 @@ test.snapshot({
a.b) === 'undefined';
}
`,
outdent`
function parse(value) {
switch (typeof value === 'undefined') {}
}
`,
],
});

Expand All @@ -86,5 +102,16 @@ test.snapshot({
invalid: [
'typeof undefinedVariableIdentifier === "undefined"',
'typeof Array !== "undefined"',
outdent`
function parse() {
switch (typeof value === 'undefined') {}
}
`,
outdent`
/* globals value: readonly */
function parse() {
switch (typeof value === 'undefined') {}
}
`,
].map(code => ({code, options: [{checkGlobalVariables: true}]})),
});
100 changes: 100 additions & 0 deletions test/snapshots/no-typeof-undefined.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,33 @@ Generated by [AVA](https://avajs.dev).
4 | }␊
`

## invalid(23): function parse(value) { switch (typeof value === 'undefined') {} }

> Input

`␊
1 | function parse(value) {␊
2 | switch (typeof value === 'undefined') {}␊
3 | }␊
`

> Output

`␊
1 | function parse(value) {␊
2 | switch (value === undefined) {}␊
3 | }␊
`

> Error 1/1

`␊
1 | function parse(value) {␊
> 2 | switch (typeof value === 'undefined') {}␊
| ^^^^^^ Compare with \`undefined\` directly instead of using \`typeof\`.␊
3 | }␊
`

## invalid(1): typeof undefinedVariableIdentifier === "undefined"

> Input
Expand Down Expand Up @@ -568,3 +595,76 @@ Generated by [AVA](https://avajs.dev).
Suggestion 1/1: Switch to \`… !== undefined\`.␊
1 | Array !== undefined␊
`

## invalid(3): function parse() { switch (typeof value === 'undefined') {} }

> Input

`␊
1 | function parse() {␊
2 | switch (typeof value === 'undefined') {}␊
3 | }␊
`

> Options

`␊
[␊
{␊
"checkGlobalVariables": true␊
}␊
]␊
`

> Error 1/1

`␊
1 | function parse() {␊
> 2 | switch (typeof value === 'undefined') {}␊
| ^^^^^^ Compare with \`undefined\` directly instead of using \`typeof\`.␊
3 | }␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Switch to \`… === undefined\`.␊
1 | function parse() {␊
2 | switch (value === undefined) {}␊
3 | }␊
`

## invalid(4): /* globals value: readonly */ function parse() { switch (typeof value === 'undefined') {} }

> Input

`␊
1 | /* globals value: readonly */␊
2 | function parse() {␊
3 | switch (typeof value === 'undefined') {}␊
4 | }␊
`

> Options

`␊
[␊
{␊
"checkGlobalVariables": true␊
}␊
]␊
`

> Error 1/1

`␊
1 | /* globals value: readonly */␊
2 | function parse() {␊
> 3 | switch (typeof value === 'undefined') {}␊
| ^^^^^^ Compare with \`undefined\` directly instead of using \`typeof\`.␊
4 | }␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Switch to \`… === undefined\`.␊
1 | /* globals value: readonly */␊
2 | function parse() {␊
3 | switch (value === undefined) {}␊
4 | }␊
`
Binary file modified test/snapshots/no-typeof-undefined.js.snap
Binary file not shown.
Loading