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

Update: report rename id destructuring in id-blacklist (fixes #12807) #12923

Merged
merged 5 commits into from Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 32 additions & 1 deletion lib/rules/id-blacklist.js
Expand Up @@ -81,6 +81,27 @@ module.exports = {
);
}

/**
* Checks whether the given node is a renamed identifier node in an ObjectPattern destructuring.
*
* Examples:
* const { a : b } = foo; // node `a` is renamed node.
* @param {ASTNode} node `Identifier` node to check.
* @returns {boolean} `true` if the node is a renamed node in an ObjectPattern destructuring.
*/
function isRenamedInDestructuring(node) {
const parent = node.parent;

return (
(
parent.type === "Property" &&
parent.parent.type === "ObjectPattern" &&
parent.value !== node &&
parent.key === node
)
);
yeonjuan marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Verifies if we should report an error or not.
* @param {ASTNode} node The node to check
Expand All @@ -92,8 +113,8 @@ module.exports = {
return (
parent.type !== "CallExpression" &&
parent.type !== "NewExpression" &&
parent.parent.type !== "ObjectPattern" &&
!isRenamedImport(node) &&
!isRenamedInDestructuring(node) &&
isInvalid(node.name)
);
}
Expand Down Expand Up @@ -141,6 +162,16 @@ module.exports = {
if (isInvalid(name)) {
report(node);
}

// Report the last identifier in an ObjectPattern destructuring.
} else if (
!effectiveParent.computed &&
yeonjuan marked this conversation as resolved.
Show resolved Hide resolved
effectiveParent.type === "Property" && effectiveParent.value === node.parent &&
effectiveParent.parent.type === "ObjectPattern" && effectiveParent.parent.parent.left === effectiveParent.parent
yeonjuan marked this conversation as resolved.
Show resolved Hide resolved
) {
if (isInvalid(name)) {
report(node);
}
}

} else if (shouldReport(node)) {
Expand Down
104 changes: 100 additions & 4 deletions tests/lib/rules/id-blacklist.js
Expand Up @@ -77,22 +77,22 @@ ruleTester.run("id-blacklist", rule, {
},
{
code: "const {foo: bar} = baz",
options: ["foo", "bar"],
options: ["foo"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "const {foo: {bar: baz}} = qux",
options: ["foo", "bar", "baz"],
options: ["foo", "bar"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "function foo({ bar: baz }) {}",
options: ["bar", "baz"],
options: ["bar"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "function foo({ bar: {baz: qux} }) {}",
options: ["bar", "baz", "qux"],
options: ["bar", "baz"],
parserOptions: { ecmaVersion: 6 }
},
{
Expand Down Expand Up @@ -126,6 +126,11 @@ ruleTester.run("id-blacklist", rule, {
{
code: "foo.bar",
options: ["bar"]
},
{
code: "({foo: obj.bar.bar.bar.baz} = {});",
options: ["foo", "bar"],
parserOptions: { ecmaVersion: 6 }
}
],
invalid: [
Expand Down Expand Up @@ -522,6 +527,97 @@ ruleTester.run("id-blacklist", rule, {
errors: [
error
]
},
{
code: "const {foo} = baz",
options: ["foo"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "blacklisted",
data: { name: "foo" },
type: "Identifier",
column: 8
}
]
},
{
code: "const {foo: bar} = baz",
options: ["foo", "bar"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "blacklisted",
data: { name: "bar" },
type: "Identifier",
column: 13
}
]
},
{
code: "const {foo: {bar: baz}} = qux",
options: ["foo", "bar", "baz"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "blacklisted",
data: { name: "baz" },
type: "Identifier",
column: 19
}
]
},
{
code: "function foo({ bar: baz }) {}",
options: ["bar", "baz"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "blacklisted",
data: { name: "baz" },
type: "Identifier",
column: 21
}
]
},
{
code: "function foo({ bar: {baz: qux} }) {}",
options: ["bar", "baz", "qux"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "blacklisted",
data: { name: "qux" },
type: "Identifier",
column: 27
}
]
},
{
code: "({foo: obj.bar} = baz);",
options: ["bar"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "blacklisted",
data: { name: "bar" },
type: "Identifier",
column: 12
}
]
},
{
code: "({foo: obj.bar.bar.bar.baz} = {});",
options: ["bar", "baz"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "blacklisted",
data: { name: "baz" },
type: "Identifier",
column: 24
}
]
}
]
});