Skip to content

Commit

Permalink
Fix: check assignment property target in camelcase (fixes #13025)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan committed Mar 11, 2020
1 parent 0243549 commit b9a6776
Show file tree
Hide file tree
Showing 2 changed files with 256 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/rules/camelcase.js
Expand Up @@ -125,6 +125,38 @@ module.exports = {
return false;
}

/**
* Checks whether the given node represents assignment target property in destructuring.
*
* For examples:
* ({a: b.foo} = c); // => true for `foo`
* ({a: b[foo]} = c); // => true for `foo`
* ([a.foo] = b); // => true for `foo`
* ([a[foo]] = b); // => true for `foo`
* ({...a.foo} = b); // => true for `foo`
* ({...a[foo]} = b); // => true for `foo`
* @param {ASTNode} node An Identifier node to check
* @returns {boolean} True if the node is an assignment target property in destructuring.
*/
function isAssignmentTargetPropertyInDestructuring(node) {
if (
node.parent.type === "MemberExpression" &&
node.parent.property === node
) {
const effectiveParent = node.parent.parent;

return (
effectiveParent &&
effectiveParent.type === "Property" &&
effectiveParent.value === node.parent &&
effectiveParent.parent.type === "ObjectPattern" ||
effectiveParent.type === "ArrayPattern" ||
effectiveParent.type === "RestElement"
);
}
return false;
}

/**
* Reports an AST node as a rule violation.
* @param {ASTNode} node The node to report.
Expand Down Expand Up @@ -170,6 +202,9 @@ module.exports = {
// Report AssignmentExpressions only if they are the left side of the assignment
} else if (effectiveParent.type === "AssignmentExpression" && nameIsUnderscored && (effectiveParent.right.type !== "MemberExpression" || effectiveParent.left.type === "MemberExpression" && effectiveParent.left.property.name === node.name)) {
report(node);

} else if (isAssignmentTargetPropertyInDestructuring(node)) {
report(node);
}

/*
Expand Down
221 changes: 221 additions & 0 deletions tests/lib/rules/camelcase.js
Expand Up @@ -225,6 +225,49 @@ ruleTester.run("camelcase", rule, {
code: "foo = { [computedBar]: 0 };",
options: [{ ignoreDestructuring: true }],
parserOptions: { ecmaVersion: 6 }
},
{
code: "({ a: obj.fo_o } = bar);",
options: [{ allow: ["fo_o"] }],
parserOptions: { ecmaVersion: 6 }
},
{
code: "({ a: obj.fo_o } = bar);",
options: [{ properties: "never" }],
parserOptions: { ecmaVersion: 6 }
},
{
code: "({ a: obj.fo_o.b_ar } = bar);",
options: [{ properties: "never" }],
parserOptions: { ecmaVersion: 6 }
},
{
code: "({ a: { b: obj.fo_o } } = bar);",
options: [{ properties: "never" }],
parserOptions: { ecmaVersion: 6 }
},
{
code: "([obj.fo_o] = bar);",
options: [{ properties: "never" }],
parserOptions: { ecmaVersion: 6 }
},
{
code: "({ c: [ob.fo_o]} = bar);",
options: [{ properties: "never" }],
parserOptions: { ecmaVersion: 6 }
},
{
code: "([obj.fo_o.b_ar] = bar);",
options: [{ properties: "never" }],
parserOptions: { ecmaVersion: 6 }
},
{
code: "({obj} = baz.fo_o);",
parserOptions: { ecmaVersion: 6 }
},
{
code: "([obj] = baz.fo_o);",
parserOptions: { ecmaVersion: 6 }
}
],
invalid: [
Expand Down Expand Up @@ -736,6 +779,184 @@ ruleTester.run("camelcase", rule, {
type: "Identifier"
}
]
},
{
code: "({ a: obj.fo_o } = bar);",
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "fo_o" },
type: "Identifier"
}
]
},
{
code: "({ a: obj[fo_o] } = bar);",
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "fo_o" },
type: "Identifier"
}
]
},
{
code: "({ a: obj.fo_o } = bar);",
options: [{ ignoreDestructuring: true }],
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "fo_o" },
type: "Identifier"
}
]
},
{
code: "({ a: obj.fo_o.b_ar } = baz);",
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "b_ar" },
type: "Identifier"
}
]
},
{
code: "({ a: { b: { c: obj.fo_o } } } = bar);",
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "fo_o" },
type: "Identifier"
}
]
},
{
code: "({ a: { b: { c: obj.fo_o.b_ar } } } = baz);",
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "b_ar" },
type: "Identifier"
}
]
},
{
code: "([obj.fo_o] = bar);",
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "fo_o" },
type: "Identifier"
}
]
},
{
code: "([obj[fo_o]] = bar);",
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "fo_o" },
type: "Identifier"
}
]
},
{
code: "([obj.fo_o] = bar);",
options: [{ ignoreDestructuring: true }],
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "fo_o" },
type: "Identifier"
}
]
},
{
code: "({ a: [obj.fo_o] } = bar);",
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "fo_o" },
type: "Identifier"
}
]
},
{
code: "({ a: { b: [obj.fo_o] } } = bar);",
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "fo_o" },
type: "Identifier"
}
]
},
{
code: "([obj.fo_o.ba_r] = baz);",
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "ba_r" },
type: "Identifier"
}
]
},
{
code: "({...obj.fo_o} = baz);",
parserOptions: { ecmaVersion: 9 },
errors: [
{
messageId: "notCamelCase",
data: { name: "fo_o" },
type: "Identifier"
}
]
},
{
code: "({...obj.fo_o.ba_r} = baz);",
parserOptions: { ecmaVersion: 9 },
errors: [
{
messageId: "notCamelCase",
data: { name: "ba_r" },
type: "Identifier"
}
]
},
{
code: "({c: {...obj.fo_o }} = baz);",
parserOptions: { ecmaVersion: 9 },
errors: [
{
messageId: "notCamelCase",
data: { name: "fo_o" },
type: "Identifier"
}
]
},
{
code: "({c: {...obj[fo_o] }} = baz);",
parserOptions: { ecmaVersion: 9 },
errors: [
{
messageId: "notCamelCase",
data: { name: "fo_o" },
type: "Identifier"
}
]
}
]
});

0 comments on commit b9a6776

Please sign in to comment.