Skip to content

Commit

Permalink
Update: report rename id destructuring in id-blacklist (fixes #12807)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan committed Feb 16, 2020
1 parent 41de9df commit 5807c19
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 5 deletions.
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
)
);
}

/**
* 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 &&
effectiveParent.type === "Property" && effectiveParent.value === node.parent &&
effectiveParent.parent.type === "ObjectPattern" && effectiveParent.parent.parent.left === effectiveParent.parent
) {
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
}
]
}
]
});

0 comments on commit 5807c19

Please sign in to comment.