2 * @fileoverview Rule that warns when identifier names that are
3 * blacklisted in the configuration are used.
4 * @author Keith Cirkel (http://keithcirkel.co.uk)
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
18 description: "disallow specified identifiers",
19 category: "Stylistic Issues",
21 url: "https://eslint.org/docs/rules/id-blacklist"
32 blacklisted: "Identifier '{{name}}' is blacklisted."
39 //--------------------------------------------------------------------------
41 //--------------------------------------------------------------------------
43 const blacklist = context.options;
47 * Checks if a string matches the provided pattern
48 * @param {string} name The string to check.
49 * @returns {boolean} if the string is a match
52 function isInvalid(name) {
53 return blacklist.indexOf(name) !== -1;
57 * Verifies if we should report an error or not based on the effective
58 * parent node and the identifier name.
59 * @param {ASTNode} effectiveParent The effective parent node of the node to be reported
60 * @param {string} name The identifier name of the identifier node
61 * @returns {boolean} whether an error should be reported or not
63 function shouldReport(effectiveParent, name) {
64 return effectiveParent.type !== "CallExpression" &&
65 effectiveParent.type !== "NewExpression" &&
70 * Reports an AST node as a rule violation.
71 * @param {ASTNode} node The node to report.
75 function report(node) {
78 messageId: "blacklisted",
88 const name = node.name,
89 effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent;
91 // MemberExpressions get special rules
92 if (node.parent.type === "MemberExpression") {
94 // Always check object names
95 if (node.parent.object.type === "Identifier" &&
96 node.parent.object.name === node.name) {
97 if (isInvalid(name)) {
101 // Report AssignmentExpressions only if they are the left side of the assignment
102 } else if (effectiveParent.type === "AssignmentExpression" &&
103 (effectiveParent.right.type !== "MemberExpression" ||
104 effectiveParent.left.type === "MemberExpression" &&
105 effectiveParent.left.property.name === node.name)) {
106 if (isInvalid(name)) {
111 // Properties have their own rules
112 } else if (node.parent.type === "Property") {
114 if (shouldReport(effectiveParent, name)) {
118 // Report anything that is a match and not a CallExpression
119 } else if (shouldReport(effectiveParent, name)) {