From 10215e6c775a0b9c0e9ddd30fb401c3bd243f6c7 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Thu, 16 Dec 2021 16:08:31 +0530 Subject: [PATCH] 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" + } + ] + } ] });