Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: report rename id destructuring in id-blacklist (fixes #12807) (
…#12923)

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

* check RestElement, ArrayPattern, computed

* check assignment pattern, computed property

* add option

* remove dupe condition
  • Loading branch information
yeonjuan committed Feb 19, 2020
1 parent 6423e11 commit 7747177
Show file tree
Hide file tree
Showing 2 changed files with 378 additions and 5 deletions.
42 changes: 41 additions & 1 deletion lib/rules/id-blacklist.js
Expand Up @@ -81,6 +81,28 @@ 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.computed &&
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 +114,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 +163,24 @@ module.exports = {
if (isInvalid(name)) {
report(node);
}

// Report the last identifier in an ObjectPattern destructuring.
} else if (
(
effectiveParent.type === "Property" &&
effectiveParent.value === node.parent &&
effectiveParent.parent.type === "ObjectPattern"
) ||
effectiveParent.type === "RestElement" ||
effectiveParent.type === "ArrayPattern" ||
(
effectiveParent.type === "AssignmentPattern" &&
effectiveParent.left === node.parent
)
) {
if (isInvalid(name)) {
report(node);
}
}

} else if (shouldReport(node)) {
Expand Down

0 comments on commit 7747177

Please sign in to comment.