Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: check assignment property target in camelcase (fixes #13025) (#1…
…3027)

* Fix: check assignment property target in camelcase (fixes #13025)

* check camelcase

* remove useless check

* fix is isAssignmentTargetPropertyInDestructuring
  • Loading branch information
yeonjuan committed Mar 26, 2020
1 parent 8d50a7d commit 0c20bc0
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/rules/camelcase.js
Expand Up @@ -125,6 +125,40 @@ 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.foo] = b); // => true for `foo`
* ([a.foo = 1] = 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 &&
!node.parent.computed
) {
const effectiveParent = node.parent.parent;

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

/**
* Reports an AST node as a rule violation.
* @param {ASTNode} node The node to report.
Expand Down Expand Up @@ -170,6 +204,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) && nameIsUnderscored) {
report(node);
}

/*
Expand Down
210 changes: 210 additions & 0 deletions tests/lib/rules/camelcase.js
Expand Up @@ -225,6 +225,59 @@ 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.foo } = 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 }
},
{
code: "([obj.foo = obj.fo_o] = bar);",
options: [{ properties: "always" }],
parserOptions: { ecmaVersion: 6 }
}
],
invalid: [
Expand Down Expand Up @@ -736,6 +789,163 @@ 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);",
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);",
options: [{ ignoreDestructuring: true }],
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "notCamelCase",
data: { name: "fo_o" },
type: "Identifier"
}
]
},
{
code: "([obj.fo_o = 1] = bar);",
options: [{ properties: "always" }],
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"
}
]
}
]
});

0 comments on commit 0c20bc0

Please sign in to comment.