Skip to content

Commit

Permalink
fix: false positive in camelcase with combined properties (#15581)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
snitin315 committed Feb 15, 2022
1 parent d992382 commit ee7c5d1
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rules/camelcase.js
Expand Up @@ -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.
Expand All @@ -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" &&
Expand Down
101 changes: 101 additions & 0 deletions tests/lib/rules/camelcase.js
Expand Up @@ -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: [
Expand Down Expand Up @@ -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
}
]
}
]
});

0 comments on commit ee7c5d1

Please sign in to comment.