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: check assignment property target in camelcase (fixes #13025) #13027

Merged
merged 4 commits into from Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
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`
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
* ([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.
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
*/
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"
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
);
}
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) && nameIsUnderscored) {
report(node);
}

/*
Expand Down
226 changes: 226 additions & 0 deletions tests/lib/rules/camelcase.js
Expand Up @@ -225,6 +225,54 @@ 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 }
}
],
invalid: [
Expand Down Expand Up @@ -736,6 +784,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"
}
]
}
]
});