From 9d6fe5a6b65f397bafc5eb0a995e96717cdc9b53 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Tue, 21 Dec 2021 19:57:52 +0530 Subject: [PATCH] feat: false negative with `onlyDeclarations` + `properties` in id-match (#15431) * fix: false negative with `onlyDeclarations` in `id-match` Fixes #15123 * test: add more cases * fix: false positive with computed properties * refactor: code * test: remove invalid test case --- lib/rules/id-match.js | 11 ++++ tests/lib/rules/id-match.js | 121 +++++++++++++++++++++++++++++++++++- 2 files changed, 131 insertions(+), 1 deletion(-) diff --git a/lib/rules/id-match.js b/lib/rules/id-match.js index 15908da8cfc..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: diff --git a/tests/lib/rules/id-match.js b/tests/lib/rules/id-match.js index 2f48a4b2d2c..de66d8b8d55 100644 --- a/tests/lib/rules/id-match.js +++ b/tests/lib/rules/id-match.js @@ -197,6 +197,58 @@ 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 } + }, + { + code: ` + const foo = { + [a]: 1, + }; + `, + options: ["^[^a]", { + properties: true, + onlyDeclarations: true + }], + parserOptions: { ecmaVersion: 2022 } + }, // Class Methods { @@ -774,7 +826,74 @@ 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" + } + ] + }, + { + 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" + } + ] + }, + { + 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" + } + ] + } ] });