From ee7c5d14a2cb5ce352d1851cec858b942572d2cc Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Tue, 15 Feb 2022 17:01:20 +0530 Subject: [PATCH] fix: false positive in `camelcase` with combined properties (#15581) * fix: false positive in `camelcase` with combined options Fixes #15572 * test: add mores cases for camelcase rule * chore: update JSDoc comment * test: add location * chore: remove unwanted change --- lib/rules/camelcase.js | 4 +- tests/lib/rules/camelcase.js | 101 +++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/lib/rules/camelcase.js b/lib/rules/camelcase.js index 6bb1838a72e..e4761466902 100644 --- a/lib/rules/camelcase.js +++ b/lib/rules/camelcase.js @@ -146,7 +146,7 @@ module.exports = { /** * Checks if a given binding identifier uses the original name as-is. - * - If it's in object destructuring, the original name is its property name. + * - If it's in object destructuring or object expression, the original name is its property name. * - If it's in import declaration, the original name is its exported name. * @param {ASTNode} node The `Identifier` node to check. * @returns {boolean} `true` if the identifier uses the original name as-is. @@ -161,7 +161,7 @@ module.exports = { switch (parent.type) { case "Property": return ( - parent.parent.type === "ObjectPattern" && + (parent.parent.type === "ObjectPattern" || parent.parent.type === "ObjectExpression") && parent.value === valueNode && !parent.computed && parent.key.type === "Identifier" && diff --git a/tests/lib/rules/camelcase.js b/tests/lib/rules/camelcase.js index 3a47a1b4b2d..79c237ce202 100644 --- a/tests/lib/rules/camelcase.js +++ b/tests/lib/rules/camelcase.js @@ -407,6 +407,35 @@ ruleTester.run("camelcase", rule, { code: "class C { snake_case; #snake_case; #snake_case2() {} }", options: [{ properties: "never" }], parserOptions: { ecmaVersion: 2022 } + }, + + // Combinations of `properties` and `ignoreDestructring` + { + code: ` + const { some_property } = obj; + + const bar = { some_property }; + + obj.some_property = 10; + + const xyz = { some_property: obj.some_property }; + + const foo = ({ some_property }) => { + console.log(some_property) + }; + `, + options: [{ properties: "never", ignoreDestructuring: true }], + parserOptions: { ecmaVersion: 2022 } + }, + + // https://github.com/eslint/eslint/issues/15572 + { + code: ` + const { some_property } = obj; + doSomething({ some_property }); + `, + options: [{ properties: "never", ignoreDestructuring: true }], + parserOptions: { ecmaVersion: 2022 } } ], invalid: [ @@ -1416,6 +1445,78 @@ ruleTester.run("camelcase", rule, { options: [{ properties: "always" }], parserOptions: { ecmaVersion: 2022 }, errors: [{ messageId: "notCamelCasePrivate", data: { name: "snake_case" } }] + }, + + // Combinations of `properties` and `ignoreDestructring` + { + code: ` + const { some_property } = obj; + doSomething({ some_property }); + `, + options: [{ properties: "always", ignoreDestructuring: true }], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + messageId: "notCamelCase", + data: { name: "some_property" }, + line: 3, + column: 27 + } + ] + }, + { + code: ` + const { some_property } = obj; + doSomething({ some_property }); + doSomething({ [some_property]: "bar" }); + `, + options: [{ properties: "never", ignoreDestructuring: true }], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + messageId: "notCamelCase", + data: { name: "some_property" }, + line: 4, + column: 28 + } + ] + }, + { + code: ` + const { some_property } = obj; + + const bar = { some_property }; + + obj.some_property = 10; + + const xyz = { some_property: obj.some_property }; + + const foo = ({ some_property }) => { + console.log(some_property) + }; + `, + options: [{ properties: "always", ignoreDestructuring: true }], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + messageId: "notCamelCase", + data: { name: "some_property" }, + line: 4, + column: 27 + }, + { + messageId: "notCamelCase", + data: { name: "some_property" }, + line: 6, + column: 17 + }, + { + messageId: "notCamelCase", + data: { name: "some_property" }, + line: 8, + column: 27 + } + ] } ] });