2 * @fileoverview Rule that warns when identifier names that are
3 * specified in the configuration are used.
4 * @author Keith Cirkel (http://keithcirkel.co.uk)
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
14 * Checks whether the given node represents assignment target in a normal assignment or destructuring.
15 * @param {ASTNode} node The node to check.
16 * @returns {boolean} `true` if the node is assignment target.
18 function isAssignmentTarget(node) {
19 const parent = node.parent;
25 parent.type === "AssignmentExpression" &&
30 parent.type === "ArrayPattern" ||
31 parent.type === "RestElement" ||
33 parent.type === "Property" &&
34 parent.value === node &&
35 parent.parent.type === "ObjectPattern"
38 parent.type === "AssignmentPattern" &&
45 * Checks whether the given node represents an imported name that is renamed in the same import/export specifier.
48 * import { a as b } from 'mod'; // node `a` is renamed import
49 * export { a as b } from 'mod'; // node `a` is renamed import
50 * @param {ASTNode} node `Identifier` node to check.
51 * @returns {boolean} `true` if the node is a renamed import.
53 function isRenamedImport(node) {
54 const parent = node.parent;
58 parent.type === "ImportSpecifier" &&
59 parent.imported !== parent.local &&
60 parent.imported === node
63 parent.type === "ExportSpecifier" &&
64 parent.parent.source && // re-export
65 parent.local !== parent.exported &&
72 * Checks whether the given node is a renamed identifier node in an ObjectPattern destructuring.
75 * const { a : b } = foo; // node `a` is renamed node.
76 * @param {ASTNode} node `Identifier` node to check.
77 * @returns {boolean} `true` if the node is a renamed node in an ObjectPattern destructuring.
79 function isRenamedInDestructuring(node) {
80 const parent = node.parent;
85 parent.type === "Property" &&
86 parent.parent.type === "ObjectPattern" &&
87 parent.value !== node &&
94 * Checks whether the given node represents shorthand definition of a property in an object literal.
95 * @param {ASTNode} node `Identifier` node to check.
96 * @returns {boolean} `true` if the node is a shorthand property definition.
98 function isShorthandPropertyDefinition(node) {
99 const parent = node.parent;
102 parent.type === "Property" &&
103 parent.parent.type === "ObjectExpression" &&
108 //------------------------------------------------------------------------------
110 //------------------------------------------------------------------------------
115 replacedBy: ["id-denylist"],
120 description: "disallow specified identifiers",
121 category: "Stylistic Issues",
123 url: "https://eslint.org/docs/rules/id-blacklist"
134 restricted: "Identifier '{{name}}' is restricted."
140 const denyList = new Set(context.options);
141 const reportedNodes = new Set();
146 * Checks whether the given name is restricted.
147 * @param {string} name The name to check.
148 * @returns {boolean} `true` if the name is restricted.
151 function isRestricted(name) {
152 return denyList.has(name);
156 * Checks whether the given node represents a reference to a global variable that is not declared in the source code.
157 * These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables.
158 * @param {ASTNode} node `Identifier` node to check.
159 * @returns {boolean} `true` if the node is a reference to a global variable.
161 function isReferenceToGlobalVariable(node) {
162 const variable = globalScope.set.get(node.name);
164 return variable && variable.defs.length === 0 &&
165 variable.references.some(ref => ref.identifier === node);
169 * Determines whether the given node should be checked.
170 * @param {ASTNode} node `Identifier` node.
171 * @returns {boolean} `true` if the node should be checked.
173 function shouldCheck(node) {
174 const parent = node.parent;
177 * Member access has special rules for checking property names.
178 * Read access to a property with a restricted name is allowed, because it can be on an object that user has no control over.
179 * Write access isn't allowed, because it potentially creates a new property with a restricted name.
182 parent.type === "MemberExpression" &&
183 parent.property === node &&
186 return isAssignmentTarget(parent);
190 parent.type !== "CallExpression" &&
191 parent.type !== "NewExpression" &&
192 !isRenamedImport(node) &&
193 !isRenamedInDestructuring(node) &&
195 isReferenceToGlobalVariable(node) &&
196 !isShorthandPropertyDefinition(node)
202 * Reports an AST node as a rule violation.
203 * @param {ASTNode} node The node to report.
207 function report(node) {
208 if (!reportedNodes.has(node)) {
211 messageId: "restricted",
216 reportedNodes.add(node);
223 globalScope = context.getScope();
227 if (isRestricted(node.name) && shouldCheck(node)) {