Navigation Menu

Skip to content

Commit

Permalink
Fix: id-blacklist false positives on renamed imports (#12831)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Feb 5, 2020
1 parent b7f0d20 commit 95e0586
Show file tree
Hide file tree
Showing 2 changed files with 317 additions and 29 deletions.
82 changes: 53 additions & 29 deletions lib/rules/id-blacklist.js
Expand Up @@ -41,6 +41,7 @@ module.exports = {
//--------------------------------------------------------------------------

const blacklist = context.options;
const reportedNodes = new Set();


/**
Expand All @@ -54,18 +55,46 @@ module.exports = {
}

/**
* Verifies if we should report an error or not based on the effective
* parent node and the identifier name.
* @param {ASTNode} effectiveParent The effective parent node of the node to be reported
* @param {string} name The identifier name of the identifier node
* Checks whether the given node represents an imported name that is renamed in the same import/export specifier.
*
* Examples:
* import { a as b } from 'mod'; // node `a` is renamed import
* export { a as b } from 'mod'; // node `a` is renamed import
* @param {ASTNode} node `Identifier` node to check.
* @returns {boolean} `true` if the node is a renamed import.
*/
function isRenamedImport(node) {
const parent = node.parent;

return (
(
parent.type === "ImportSpecifier" &&
parent.imported !== parent.local &&
parent.imported === node
) ||
(
parent.type === "ExportSpecifier" &&
parent.parent.source && // re-export
parent.local !== parent.exported &&
parent.local === node
)
);
}

/**
* Verifies if we should report an error or not.
* @param {ASTNode} node The node to check
* @returns {boolean} whether an error should be reported or not
*/
function shouldReport(effectiveParent, name) {
function shouldReport(node) {
const parent = node.parent;

return (
effectiveParent.type !== "CallExpression" &&
effectiveParent.type !== "NewExpression" &&
effectiveParent.parent.type !== "ObjectPattern" &&
isInvalid(name)
parent.type !== "CallExpression" &&
parent.type !== "NewExpression" &&
parent.parent.type !== "ObjectPattern" &&
!isRenamedImport(node) &&
isInvalid(node.name)
);
}

Expand All @@ -76,27 +105,30 @@ module.exports = {
* @private
*/
function report(node) {
context.report({
node,
messageId: "blacklisted",
data: {
name: node.name
}
});
if (!reportedNodes.has(node)) {
context.report({
node,
messageId: "blacklisted",
data: {
name: node.name
}
});
reportedNodes.add(node);
}
}

return {

Identifier(node) {
const name = node.name,
effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent;

// MemberExpressions get special rules
if (node.parent.type === "MemberExpression") {
const name = node.name,
effectiveParent = node.parent.parent;

// Always check object names
if (node.parent.object.type === "Identifier" &&
node.parent.object.name === node.name) {
node.parent.object.name === name) {
if (isInvalid(name)) {
report(node);
}
Expand All @@ -105,21 +137,13 @@ module.exports = {
} else if (effectiveParent.type === "AssignmentExpression" &&
(effectiveParent.right.type !== "MemberExpression" ||
effectiveParent.left.type === "MemberExpression" &&
effectiveParent.left.property.name === node.name)) {
effectiveParent.left.property.name === name)) {
if (isInvalid(name)) {
report(node);
}
}

// Properties have their own rules
} else if (node.parent.type === "Property") {

if (shouldReport(effectiveParent, name)) {
report(node);
}

// Report anything that is a match and not a CallExpression
} else if (shouldReport(effectiveParent, name)) {
} else if (shouldReport(node)) {
report(node);
}
}
Expand Down

0 comments on commit 95e0586

Please sign in to comment.