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 //------------------------------------------------------------------------------
117 description: "disallow specified identifiers",
118 category: "Stylistic Issues",
120 url: "https://eslint.org/docs/rules/id-denylist"
131 restricted: "Identifier '{{name}}' is restricted."
137 const denyList = new Set(context.options);
138 const reportedNodes = new Set();
143 * Checks whether the given name is restricted.
144 * @param {string} name The name to check.
145 * @returns {boolean} `true` if the name is restricted.
148 function isRestricted(name) {
149 return denyList.has(name);
153 * Checks whether the given node represents a reference to a global variable that is not declared in the source code.
154 * These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables.
155 * @param {ASTNode} node `Identifier` node to check.
156 * @returns {boolean} `true` if the node is a reference to a global variable.
158 function isReferenceToGlobalVariable(node) {
159 const variable = globalScope.set.get(node.name);
161 return variable && variable.defs.length === 0 &&
162 variable.references.some(ref => ref.identifier === node);
166 * Determines whether the given node should be checked.
167 * @param {ASTNode} node `Identifier` node.
168 * @returns {boolean} `true` if the node should be checked.
170 function shouldCheck(node) {
171 const parent = node.parent;
174 * Member access has special rules for checking property names.
175 * Read access to a property with a restricted name is allowed, because it can be on an object that user has no control over.
176 * Write access isn't allowed, because it potentially creates a new property with a restricted name.
179 parent.type === "MemberExpression" &&
180 parent.property === node &&
183 return isAssignmentTarget(parent);
187 parent.type !== "CallExpression" &&
188 parent.type !== "NewExpression" &&
189 !isRenamedImport(node) &&
190 !isRenamedInDestructuring(node) &&
192 isReferenceToGlobalVariable(node) &&
193 !isShorthandPropertyDefinition(node)
199 * Reports an AST node as a rule violation.
200 * @param {ASTNode} node The node to report.
204 function report(node) {
205 if (!reportedNodes.has(node)) {
208 messageId: "restricted",
213 reportedNodes.add(node);
220 globalScope = context.getScope();
224 if (isRestricted(node.name) && shouldCheck(node)) {