Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: false positive in camelcase with combined properties #15581

Merged
merged 5 commits into from Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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") &&
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
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
}
]
}
]
});