id-blacklist.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  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)
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Helpers
  9. //------------------------------------------------------------------------------
  10. /**
  11. * Checks whether the given node represents assignment target in a normal assignment or destructuring.
  12. * @param {ASTNode} node The node to check.
  13. * @returns {boolean} `true` if the node is assignment target.
  14. */
  15. function isAssignmentTarget(node) {
  16. const parent = node.parent;
  17. return (
  18. // normal assignment
  19. (
  20. parent.type === "AssignmentExpression" &&
  21. parent.left === node
  22. ) ||
  23. // destructuring
  24. parent.type === "ArrayPattern" ||
  25. parent.type === "RestElement" ||
  26. (
  27. parent.type === "Property" &&
  28. parent.value === node &&
  29. parent.parent.type === "ObjectPattern"
  30. ) ||
  31. (
  32. parent.type === "AssignmentPattern" &&
  33. parent.left === node
  34. )
  35. );
  36. }
  37. /**
  38. * Checks whether the given node represents an imported name that is renamed in the same import/export specifier.
  39. *
  40. * Examples:
  41. * import { a as b } from 'mod'; // node `a` is renamed import
  42. * export { a as b } from 'mod'; // node `a` is renamed import
  43. * @param {ASTNode} node `Identifier` node to check.
  44. * @returns {boolean} `true` if the node is a renamed import.
  45. */
  46. function isRenamedImport(node) {
  47. const parent = node.parent;
  48. return (
  49. (
  50. parent.type === "ImportSpecifier" &&
  51. parent.imported !== parent.local &&
  52. parent.imported === node
  53. ) ||
  54. (
  55. parent.type === "ExportSpecifier" &&
  56. parent.parent.source && // re-export
  57. parent.local !== parent.exported &&
  58. parent.local === node
  59. )
  60. );
  61. }
  62. /**
  63. * Checks whether the given node is a renamed identifier node in an ObjectPattern destructuring.
  64. *
  65. * Examples:
  66. * const { a : b } = foo; // node `a` is renamed node.
  67. * @param {ASTNode} node `Identifier` node to check.
  68. * @returns {boolean} `true` if the node is a renamed node in an ObjectPattern destructuring.
  69. */
  70. function isRenamedInDestructuring(node) {
  71. const parent = node.parent;
  72. return (
  73. (
  74. !parent.computed &&
  75. parent.type === "Property" &&
  76. parent.parent.type === "ObjectPattern" &&
  77. parent.value !== node &&
  78. parent.key === node
  79. )
  80. );
  81. }
  82. /**
  83. * Checks whether the given node represents shorthand definition of a property in an object literal.
  84. * @param {ASTNode} node `Identifier` node to check.
  85. * @returns {boolean} `true` if the node is a shorthand property definition.
  86. */
  87. function isShorthandPropertyDefinition(node) {
  88. const parent = node.parent;
  89. return (
  90. parent.type === "Property" &&
  91. parent.parent.type === "ObjectExpression" &&
  92. parent.shorthand
  93. );
  94. }
  95. //------------------------------------------------------------------------------
  96. // Rule Definition
  97. //------------------------------------------------------------------------------
  98. module.exports = {
  99. meta: {
  100. deprecated: true,
  101. replacedBy: ["id-denylist"],
  102. type: "suggestion",
  103. docs: {
  104. description: "disallow specified identifiers",
  105. category: "Stylistic Issues",
  106. recommended: false,
  107. url: "https://eslint.org/docs/rules/id-blacklist"
  108. },
  109. schema: {
  110. type: "array",
  111. items: {
  112. type: "string"
  113. },
  114. uniqueItems: true
  115. },
  116. messages: {
  117. restricted: "Identifier '{{name}}' is restricted."
  118. }
  119. },
  120. create(context) {
  121. const denyList = new Set(context.options);
  122. const reportedNodes = new Set();
  123. let globalScope;
  124. /**
  125. * Checks whether the given name is restricted.
  126. * @param {string} name The name to check.
  127. * @returns {boolean} `true` if the name is restricted.
  128. * @private
  129. */
  130. function isRestricted(name) {
  131. return denyList.has(name);
  132. }
  133. /**
  134. * Checks whether the given node represents a reference to a global variable that is not declared in the source code.
  135. * These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables.
  136. * @param {ASTNode} node `Identifier` node to check.
  137. * @returns {boolean} `true` if the node is a reference to a global variable.
  138. */
  139. function isReferenceToGlobalVariable(node) {
  140. const variable = globalScope.set.get(node.name);
  141. return variable && variable.defs.length === 0 &&
  142. variable.references.some(ref => ref.identifier === node);
  143. }
  144. /**
  145. * Determines whether the given node should be checked.
  146. * @param {ASTNode} node `Identifier` node.
  147. * @returns {boolean} `true` if the node should be checked.
  148. */
  149. function shouldCheck(node) {
  150. const parent = node.parent;
  151. /*
  152. * Member access has special rules for checking property names.
  153. * Read access to a property with a restricted name is allowed, because it can be on an object that user has no control over.
  154. * Write access isn't allowed, because it potentially creates a new property with a restricted name.
  155. */
  156. if (
  157. parent.type === "MemberExpression" &&
  158. parent.property === node &&
  159. !parent.computed
  160. ) {
  161. return isAssignmentTarget(parent);
  162. }
  163. return (
  164. parent.type !== "CallExpression" &&
  165. parent.type !== "NewExpression" &&
  166. !isRenamedImport(node) &&
  167. !isRenamedInDestructuring(node) &&
  168. !(
  169. isReferenceToGlobalVariable(node) &&
  170. !isShorthandPropertyDefinition(node)
  171. )
  172. );
  173. }
  174. /**
  175. * Reports an AST node as a rule violation.
  176. * @param {ASTNode} node The node to report.
  177. * @returns {void}
  178. * @private
  179. */
  180. function report(node) {
  181. if (!reportedNodes.has(node)) {
  182. context.report({
  183. node,
  184. messageId: "restricted",
  185. data: {
  186. name: node.name
  187. }
  188. });
  189. reportedNodes.add(node);
  190. }
  191. }
  192. return {
  193. Program() {
  194. globalScope = context.getScope();
  195. },
  196. Identifier(node) {
  197. if (isRestricted(node.name) && shouldCheck(node)) {
  198. report(node);
  199. }
  200. }
  201. };
  202. }
  203. };