Skip to content

Commit ee7c5d1

Browse files
authored
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
1 parent d992382 commit ee7c5d1

2 files changed

Lines changed: 103 additions & 2 deletions

File tree

lib/rules/camelcase.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ module.exports = {
146146

147147
/**
148148
* Checks if a given binding identifier uses the original name as-is.
149-
* - If it's in object destructuring, the original name is its property name.
149+
* - If it's in object destructuring or object expression, the original name is its property name.
150150
* - If it's in import declaration, the original name is its exported name.
151151
* @param {ASTNode} node The `Identifier` node to check.
152152
* @returns {boolean} `true` if the identifier uses the original name as-is.
@@ -161,7 +161,7 @@ module.exports = {
161161
switch (parent.type) {
162162
case "Property":
163163
return (
164-
parent.parent.type === "ObjectPattern" &&
164+
(parent.parent.type === "ObjectPattern" || parent.parent.type === "ObjectExpression") &&
165165
parent.value === valueNode &&
166166
!parent.computed &&
167167
parent.key.type === "Identifier" &&

tests/lib/rules/camelcase.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,35 @@ ruleTester.run("camelcase", rule, {
407407
code: "class C { snake_case; #snake_case; #snake_case2() {} }",
408408
options: [{ properties: "never" }],
409409
parserOptions: { ecmaVersion: 2022 }
410+
},
411+
412+
// Combinations of `properties` and `ignoreDestructring`
413+
{
414+
code: `
415+
const { some_property } = obj;
416+
417+
const bar = { some_property };
418+
419+
obj.some_property = 10;
420+
421+
const xyz = { some_property: obj.some_property };
422+
423+
const foo = ({ some_property }) => {
424+
console.log(some_property)
425+
};
426+
`,
427+
options: [{ properties: "never", ignoreDestructuring: true }],
428+
parserOptions: { ecmaVersion: 2022 }
429+
},
430+
431+
// https://github.com/eslint/eslint/issues/15572
432+
{
433+
code: `
434+
const { some_property } = obj;
435+
doSomething({ some_property });
436+
`,
437+
options: [{ properties: "never", ignoreDestructuring: true }],
438+
parserOptions: { ecmaVersion: 2022 }
410439
}
411440
],
412441
invalid: [
@@ -1416,6 +1445,78 @@ ruleTester.run("camelcase", rule, {
14161445
options: [{ properties: "always" }],
14171446
parserOptions: { ecmaVersion: 2022 },
14181447
errors: [{ messageId: "notCamelCasePrivate", data: { name: "snake_case" } }]
1448+
},
1449+
1450+
// Combinations of `properties` and `ignoreDestructring`
1451+
{
1452+
code: `
1453+
const { some_property } = obj;
1454+
doSomething({ some_property });
1455+
`,
1456+
options: [{ properties: "always", ignoreDestructuring: true }],
1457+
parserOptions: { ecmaVersion: 2022 },
1458+
errors: [
1459+
{
1460+
messageId: "notCamelCase",
1461+
data: { name: "some_property" },
1462+
line: 3,
1463+
column: 27
1464+
}
1465+
]
1466+
},
1467+
{
1468+
code: `
1469+
const { some_property } = obj;
1470+
doSomething({ some_property });
1471+
doSomething({ [some_property]: "bar" });
1472+
`,
1473+
options: [{ properties: "never", ignoreDestructuring: true }],
1474+
parserOptions: { ecmaVersion: 2022 },
1475+
errors: [
1476+
{
1477+
messageId: "notCamelCase",
1478+
data: { name: "some_property" },
1479+
line: 4,
1480+
column: 28
1481+
}
1482+
]
1483+
},
1484+
{
1485+
code: `
1486+
const { some_property } = obj;
1487+
1488+
const bar = { some_property };
1489+
1490+
obj.some_property = 10;
1491+
1492+
const xyz = { some_property: obj.some_property };
1493+
1494+
const foo = ({ some_property }) => {
1495+
console.log(some_property)
1496+
};
1497+
`,
1498+
options: [{ properties: "always", ignoreDestructuring: true }],
1499+
parserOptions: { ecmaVersion: 2022 },
1500+
errors: [
1501+
{
1502+
messageId: "notCamelCase",
1503+
data: { name: "some_property" },
1504+
line: 4,
1505+
column: 27
1506+
},
1507+
{
1508+
messageId: "notCamelCase",
1509+
data: { name: "some_property" },
1510+
line: 6,
1511+
column: 17
1512+
},
1513+
{
1514+
messageId: "notCamelCase",
1515+
data: { name: "some_property" },
1516+
line: 8,
1517+
column: 27
1518+
}
1519+
]
14191520
}
14201521
]
14211522
});

0 commit comments

Comments
 (0)