From 10215e6c775a0b9c0e9ddd30fb401c3bd243f6c7 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Thu, 16 Dec 2021 16:08:31 +0530 Subject: [PATCH 1/5] fix: false negative with `onlyDeclarations` in `id-match` Fixes #15123 --- lib/rules/id-match.js | 7 +++++++ tests/lib/rules/id-match.js | 27 ++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/rules/id-match.js b/lib/rules/id-match.js index 15908da8cfc..b09155f0f5b 100644 --- a/lib/rules/id-match.js +++ b/lib/rules/id-match.js @@ -238,6 +238,13 @@ module.exports = { } } + // For https://github.com/eslint/eslint/issues/15123 + if (parent.parent && parent.parent.type === "ObjectExpression") { + if (checkProperties && parent.key === node && isInvalid(name)) { + report(node); + } + } + // never check properties or always ignore destructuring if (!checkProperties || (ignoreDestructuring && isInsideObjectPattern(node))) { return; diff --git a/tests/lib/rules/id-match.js b/tests/lib/rules/id-match.js index 2f48a4b2d2c..cb21e5d8f0b 100644 --- a/tests/lib/rules/id-match.js +++ b/tests/lib/rules/id-match.js @@ -774,7 +774,32 @@ ruleTester.run("id-match", rule, { type: "PrivateIdentifier" } ] - } + }, + // https://github.com/eslint/eslint/issues/15123 + { + code: ` + const foo = { + foo_one: 1, + bar_one: 2, + fooBar: 3 + }; + `, + options: ["^[^_]+$", { + properties: true, + onlyDeclarations: true + }], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + message: "Identifier 'foo_one' does not match the pattern '^[^_]+$'.", + type: "Identifier" + }, + { + message: "Identifier 'bar_one' does not match the pattern '^[^_]+$'.", + type: "Identifier" + } + ] + } ] }); From 8dad653a81a5a0e4faf93bd1fd01d77790858166 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Thu, 16 Dec 2021 16:27:18 +0530 Subject: [PATCH 2/5] test: add more cases --- tests/lib/rules/id-match.js | 64 +++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/lib/rules/id-match.js b/tests/lib/rules/id-match.js index cb21e5d8f0b..5a1f75812f4 100644 --- a/tests/lib/rules/id-match.js +++ b/tests/lib/rules/id-match.js @@ -197,6 +197,46 @@ ruleTester.run("id-match", rule, { }], parserOptions: { ecmaVersion: 2022 } }, + { + code: ` + const foo = { + foo_one: 1, + bar_one: 2, + fooBar: 3 + }; + `, + options: ["^[^_]+$", { + properties: false + }], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + const foo = { + foo_one: 1, + bar_one: 2, + fooBar: 3 + }; + `, + options: ["^[^_]+$", { + onlyDeclarations: true + }], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + const foo = { + foo_one: 1, + bar_one: 2, + fooBar: 3 + }; + `, + options: ["^[^_]+$", { + properties: false, + onlyDeclarations: false + }], + parserOptions: { ecmaVersion: 2022 } + }, // Class Methods { @@ -800,6 +840,30 @@ ruleTester.run("id-match", rule, { type: "Identifier" } ] + }, + { + code: ` + const foo = { + foo_one: 1, + bar_one: 2, + fooBar: 3 + }; + `, + options: ["^[^_]+$", { + properties: true, + onlyDeclarations: false + }], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + message: "Identifier 'foo_one' does not match the pattern '^[^_]+$'.", + type: "Identifier" + }, + { + message: "Identifier 'bar_one' does not match the pattern '^[^_]+$'.", + type: "Identifier" + } + ] } ] }); From bbe507e5e8e75edf67073974568a71ce3103004a Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sat, 18 Dec 2021 07:50:59 +0530 Subject: [PATCH 3/5] fix: false positive with computed properties --- lib/rules/id-match.js | 6 +++++- tests/lib/rules/id-match.js | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/rules/id-match.js b/lib/rules/id-match.js index b09155f0f5b..938e719cb05 100644 --- a/lib/rules/id-match.js +++ b/lib/rules/id-match.js @@ -240,7 +240,11 @@ module.exports = { // For https://github.com/eslint/eslint/issues/15123 if (parent.parent && parent.parent.type === "ObjectExpression") { - if (checkProperties && parent.key === node && isInvalid(name)) { + if ( + (checkProperties && onlyDeclarations && !parent.computed) && + parent.key === node && + isInvalid(name) + ) { report(node); } } diff --git a/tests/lib/rules/id-match.js b/tests/lib/rules/id-match.js index 5a1f75812f4..1bcac19b9b8 100644 --- a/tests/lib/rules/id-match.js +++ b/tests/lib/rules/id-match.js @@ -237,6 +237,30 @@ ruleTester.run("id-match", rule, { }], parserOptions: { ecmaVersion: 2022 } }, + { + code: ` + const foo = { + [a]: 1, + }; + `, + options: ["^[^a]", { + properties: false, + onlyDeclarations: false + }], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + const foo = { + [a]: 1, + }; + `, + options: ["^[^a]", { + properties: true, + onlyDeclarations: true + }], + parserOptions: { ecmaVersion: 2022 } + }, // Class Methods { @@ -864,6 +888,24 @@ ruleTester.run("id-match", rule, { type: "Identifier" } ] + }, + { + code: ` + const foo = { + [a]: 1, + }; + `, + options: ["^[^a]", { + properties: true, + onlyDeclarations: false + }], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + message: "Identifier 'a' does not match the pattern '^[^a]'.", + type: "Identifier" + } + ] } ] }); From c5a813def1941805786c78e5fe046a76576e77da Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Tue, 21 Dec 2021 14:59:19 +0530 Subject: [PATCH 4/5] refactor: code --- lib/rules/id-match.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/rules/id-match.js b/lib/rules/id-match.js index 938e719cb05..977e66a26ad 100644 --- a/lib/rules/id-match.js +++ b/lib/rules/id-match.js @@ -211,6 +211,17 @@ module.exports = { } } + // For https://github.com/eslint/eslint/issues/15123 + } else if ( + parent.type === "Property" && + parent.parent.type === "ObjectExpression" && + parent.key === node && + !parent.computed + ) { + if (checkProperties && isInvalid(name)) { + report(node); + } + /* * Properties have their own rules, and * AssignmentPattern nodes can be treated like Properties: @@ -238,17 +249,6 @@ module.exports = { } } - // For https://github.com/eslint/eslint/issues/15123 - if (parent.parent && parent.parent.type === "ObjectExpression") { - if ( - (checkProperties && onlyDeclarations && !parent.computed) && - parent.key === node && - isInvalid(name) - ) { - report(node); - } - } - // never check properties or always ignore destructuring if (!checkProperties || (ignoreDestructuring && isInsideObjectPattern(node))) { return; From 88f192c87555a7a953c397c1359c2950dbf1c19d Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Tue, 21 Dec 2021 17:44:46 +0530 Subject: [PATCH 5/5] test: remove invalid test case --- tests/lib/rules/id-match.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/lib/rules/id-match.js b/tests/lib/rules/id-match.js index 1bcac19b9b8..de66d8b8d55 100644 --- a/tests/lib/rules/id-match.js +++ b/tests/lib/rules/id-match.js @@ -237,18 +237,6 @@ ruleTester.run("id-match", rule, { }], parserOptions: { ecmaVersion: 2022 } }, - { - code: ` - const foo = { - [a]: 1, - }; - `, - options: ["^[^a]", { - properties: false, - onlyDeclarations: false - }], - parserOptions: { ecmaVersion: 2022 } - }, { code: ` const foo = {