diff --git a/lib/rules/camelcase.js b/lib/rules/camelcase.js index 6bb1838a72e..d25e3dac999 100644 --- a/lib/rules/camelcase.js +++ b/lib/rules/camelcase.js @@ -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" && @@ -311,6 +311,7 @@ module.exports = { * It looks unnecessary because declarations are reported. */ for (const reference of variable.references) { + if (reference.init) { continue; // Skip the write references of initializers. } diff --git a/tests/lib/rules/camelcase.js b/tests/lib/rules/camelcase.js index 3a47a1b4b2d..9710f065cf6 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 foo = ({ some_property }) => { + console.log(some_property) + }; + + function bar({ some_property }) { + console.log(some_property) + }; + `, + options: [{ properties: "never", ignoreDestructuring: true }], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ messageId: "notCamelCase", data: { name: "some_property" } }] + }, + + // https://github.com/eslint/eslint/issues/15572 + { + code: ` + const { some_property } = obj; + doSomething({ some_property }); + `, + options: [{ properties: "never", ignoreDestructuring: true }], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ messageId: "notCamelCase", data: { name: "some_property" } }] } ], invalid: [ @@ -1416,6 +1445,26 @@ 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" } }] + }, + { + code: ` + const { some_property } = obj; + doSomething({ [some_property]: "bar" }); + `, + options: [{ properties: "never", ignoreDestructuring: true }], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ messageId: "notCamelCase", data: { name: "some_property" } }] } ] });