Skip to content

Commit 2caa9c2

Browse files
authored
prefer-number-isnan: treat ESM import bindings as local shadows (#43500)
1 parent cba5a29 commit 2caa9c2

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

eslint-factory/src/rules/prefer-number-isnan.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ describe("prefer-number-isnan", () => {
3434
valid: [
3535
`function isNaN(value) { return false; } isNaN(value);`,
3636
`const isNaN = Number.isNaN; isNaN(value);`,
37+
`import { isNaN } from "lodash"; isNaN(value);`,
3738
`const globalThis = { isNaN(value) { return value; } }; globalThis.isNaN(value);`,
3839
`const window = { isNaN(value) { return value; } }; window["isNaN"](value);`,
3940
`const global = { isNaN(value) { return value; } }; global.isNaN(value);`,
41+
`import { globalThis } from "./global-shim"; globalThis.isNaN(value);`,
42+
`import { window } from "./browser-shim"; window.isNaN(value);`,
43+
`import { global } from "./server-shim"; global["isNaN"](value);`,
4044
// Dynamic computed access — identifier property reference, not string literal "isNaN"
4145
`globalThis[isNaN](value);`,
4246
],
@@ -99,4 +103,20 @@ describe("prefer-number-isnan", () => {
99103
],
100104
});
101105
});
106+
107+
it("invalid: global isNaN() is still flagged in ESM mode without a shadow", () => {
108+
esmRuleTester.run("prefer-number-isnan", preferNumberIsNanRule, {
109+
valid: [],
110+
invalid: [
111+
{
112+
code: `isNaN(value);`,
113+
errors: [{ messageId: "preferNumberIsNaN", suggestions: [{ messageId: "replaceWithNumberIsNaN", output: `Number.isNaN(value);` }] }],
114+
},
115+
{
116+
code: `window.isNaN(value);`,
117+
errors: [{ messageId: "preferNumberIsNaN", suggestions: [{ messageId: "replaceWithNumberIsNaN", output: `Number.isNaN(value);` }] }],
118+
},
119+
],
120+
});
121+
});
102122
});

eslint-factory/src/rules/prefer-number-isnan.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export const preferNumberIsNanRule = createRule({
3434
while (scope) {
3535
const variable = scope.set.get(name);
3636

37-
if (variable?.defs.some(d => d.type !== "ImportBinding")) {
37+
// Any local definition shadows the global (including ESM ImportBinding).
38+
if (variable && variable.defs.length > 0) {
3839
return true;
3940
}
4041

0 commit comments

Comments
 (0)