Skip to content

Commit

Permalink
Update: handle computed properties in camelcase (fixes #11084) (#11113)
Browse files Browse the repository at this point in the history
  • Loading branch information
madbence authored and platinumazure committed Dec 22, 2018
1 parent 1009304 commit 4242314
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/rules/camelcase.js
Expand Up @@ -100,14 +100,20 @@ module.exports = {
* @private
*/
function isInsideObjectPattern(node) {
let { parent } = node;
let current = node;

while (parent) {
if (parent.type === "ObjectPattern") {
while (current) {
const parent = current.parent;

if (parent && parent.type === "Property" && parent.computed && parent.key === current) {
return false;
}

if (current.type === "ObjectPattern") {
return true;
}

parent = parent.parent;
current = parent;
}

return false;
Expand Down Expand Up @@ -169,12 +175,15 @@ module.exports = {

if (node.parent.parent && node.parent.parent.type === "ObjectPattern") {
if (node.parent.shorthand && node.parent.value.left && nameIsUnderscored) {

report(node);
}

const assignmentKeyEqualsValue = node.parent.key.name === node.parent.value.name;

if (isUnderscored(name) && node.parent.computed) {
report(node);
}

// prevent checking righthand side of destructured object
if (node.parent.key === node && node.parent.value !== node) {
return;
Expand Down
28 changes: 28 additions & 0 deletions tests/lib/rules/camelcase.js
Expand Up @@ -106,6 +106,11 @@ ruleTester.run("camelcase", rule, {
options: [{ ignoreDestructuring: true }],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var { [{category_id} = query]: categoryId } = query;",
options: [{ ignoreDestructuring: true }],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var { category_id: category } = query;",
parserOptions: { ecmaVersion: 6 }
Expand Down Expand Up @@ -345,6 +350,29 @@ ruleTester.run("camelcase", rule, {
}
]
},
{
code: "var { [category_id]: categoryId } = query;",
options: [{ ignoreDestructuring: true }],
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "category_id" },
type: "Identifier"
}
]
},
{
code: "var { [category_id]: categoryId } = query;",
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "category_id" },
type: "Identifier"
}
]
},
{
code: "var { category_id: categoryId, ...other_props } = query;",
options: [{ ignoreDestructuring: true }],
Expand Down

0 comments on commit 4242314

Please sign in to comment.