diff --git a/rules/utils/is-unresolved-variable.js b/rules/utils/is-unresolved-variable.js index 531bad129b..b89b9c4e93 100644 --- a/rules/utils/is-unresolved-variable.js +++ b/rules/utils/is-unresolved-variable.js @@ -1,3 +1,5 @@ +import {findVariable} from '@eslint-community/eslint-utils'; + /** Checks if the given identifier node is shadowed in the given scope. @@ -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; } diff --git a/test/no-typeof-undefined.js b/test/no-typeof-undefined.js index 15ff01504e..6095813667 100644 --- a/test/no-typeof-undefined.js +++ b/test/no-typeof-undefined.js @@ -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', @@ -76,6 +87,11 @@ test.snapshot({ a.b) === 'undefined'; } `, + outdent` + function parse(value) { + switch (typeof value === 'undefined') {} + } + `, ], }); @@ -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}]})), }); diff --git a/test/snapshots/no-typeof-undefined.js.md b/test/snapshots/no-typeof-undefined.js.md index 3312d4fc3c..97140b4e59 100644 --- a/test/snapshots/no-typeof-undefined.js.md +++ b/test/snapshots/no-typeof-undefined.js.md @@ -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 @@ -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 | }␊ + ` diff --git a/test/snapshots/no-typeof-undefined.js.snap b/test/snapshots/no-typeof-undefined.js.snap index 8dad76954c..d9f56fdbe1 100644 Binary files a/test/snapshots/no-typeof-undefined.js.snap and b/test/snapshots/no-typeof-undefined.js.snap differ