From 3c0af8a3f5b0771550bc02563d3828a9d44e53a7 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Mon, 18 Nov 2024 13:42:43 +0900 Subject: [PATCH] fix: crash on for statement in `*-prototype-*` rules (#224) --- .../index.js | 3 +++ lib/util/define-prototype-method-handler/index.js | 6 ++++++ tests/lib/rules/no-array-prototype-at.js | 2 ++ .../lib/rules/no-nonstandard-array-prototype-properties.js | 2 ++ 4 files changed, 13 insertions(+) diff --git a/lib/util/define-nonstandard-prototype-properties-handler/index.js b/lib/util/define-nonstandard-prototype-properties-handler/index.js index 186af9fe..37c23f86 100644 --- a/lib/util/define-nonstandard-prototype-properties-handler/index.js +++ b/lib/util/define-nonstandard-prototype-properties-handler/index.js @@ -93,6 +93,9 @@ function defineNonstandardPrototypePropertiesHandler( assignmentNode.type === "VariableDeclarator" ? assignmentNode.init : assignmentNode.right + if (!objectNode) { + return + } for (const className of classNames) { if (!objectTypeChecker(node, objectNode, className)) { diff --git a/lib/util/define-prototype-method-handler/index.js b/lib/util/define-prototype-method-handler/index.js index 310d7a11..a3c3cedd 100644 --- a/lib/util/define-prototype-method-handler/index.js +++ b/lib/util/define-prototype-method-handler/index.js @@ -104,6 +104,9 @@ function definePrototypeMethodHandler(context, nameMap, options) { assignmentNode.type === "VariableDeclarator" ? assignmentNode.init : assignmentNode.right + if (!objectNode) { + return + } let objectTypeResult = undefined if ( methodNames.includes(propertyName) && @@ -163,6 +166,9 @@ function definePrototypeMethodHandler(context, nameMap, options) { assignmentNode.type === "VariableDeclarator" ? assignmentNode.init : assignmentNode.right + if (!objectNode) { + return + } for (const [className, methodNames] of nameMapEntries) { let objectTypeResult = undefined if ( diff --git a/tests/lib/rules/no-array-prototype-at.js b/tests/lib/rules/no-array-prototype-at.js index fb312b99..0e8b2190 100644 --- a/tests/lib/rules/no-array-prototype-at.js +++ b/tests/lib/rules/no-array-prototype-at.js @@ -21,6 +21,8 @@ new RuleTester().run(ruleId, rule, { options: [{ aggressive: false }], settings: { "es-x": { aggressive: true } }, }, + // Test for https://github.com/eslint-community/eslint-plugin-es-x/issues/223 + "for (const { x } of foo) {}", ], invalid: [ { diff --git a/tests/lib/rules/no-nonstandard-array-prototype-properties.js b/tests/lib/rules/no-nonstandard-array-prototype-properties.js index 9400ef1d..5d463c9a 100644 --- a/tests/lib/rules/no-nonstandard-array-prototype-properties.js +++ b/tests/lib/rules/no-nonstandard-array-prototype-properties.js @@ -17,6 +17,8 @@ new RuleTester().run(ruleId, rule, { "['A'][0]", "['A']['0']", { code: "['A'].unknown()", options: [{ allow: ["unknown"] }] }, + // Test for https://github.com/eslint-community/eslint-plugin-es-x/issues/223 + "for (const { x } of foo) {}", ], invalid: [ {