id-blacklist.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. * @deprecated in ESLint v7.5.0
  6. */
  7. "use strict";
  8. //------------------------------------------------------------------------------
  9. // Helpers
  10. //------------------------------------------------------------------------------
  11. /**
  12. * Checks whether the given node represents assignment target in a normal assignment or destructuring.
  13. * @param {ASTNode} node The node to check.
  14. * @returns {boolean} `true` if the node is assignment target.
  15. */
  16. function isAssignmentTarget(node) {
  17. const parent = node.parent;
  18. return (
  19. // normal assignment
  20. (parent.type === "AssignmentExpression" && parent.left === node) ||
  21. // destructuring
  22. parent.type === "ArrayPattern" ||
  23. parent.type === "RestElement" ||
  24. (parent.type === "Property" &&
  25. parent.value === node &&
  26. parent.parent.type === "ObjectPattern") ||
  27. (parent.type === "AssignmentPattern" && parent.left === node)
  28. );
  29. }
  30. /**
  31. * Checks whether the given node represents an imported name that is renamed in the same import/export specifier.
  32. *
  33. * Examples:
  34. * import { a as b } from 'mod'; // node `a` is renamed import
  35. * export { a as b } from 'mod'; // node `a` is renamed import
  36. * @param {ASTNode} node `Identifier` node to check.
  37. * @returns {boolean} `true` if the node is a renamed import.
  38. */
  39. function isRenamedImport(node) {
  40. const parent = node.parent;
  41. return (
  42. (parent.type === "ImportSpecifier" &&
  43. parent.imported !== parent.local &&
  44. parent.imported === node) ||
  45. (parent.type === "ExportSpecifier" &&
  46. parent.parent.source && // re-export
  47. parent.local !== parent.exported &&
  48. parent.local === node)
  49. );
  50. }
  51. /**
  52. * Checks whether the given node is a renamed identifier node in an ObjectPattern destructuring.
  53. *
  54. * Examples:
  55. * const { a : b } = foo; // node `a` is renamed node.
  56. * @param {ASTNode} node `Identifier` node to check.
  57. * @returns {boolean} `true` if the node is a renamed node in an ObjectPattern destructuring.
  58. */
  59. function isRenamedInDestructuring(node) {
  60. const parent = node.parent;
  61. return (
  62. !parent.computed &&
  63. parent.type === "Property" &&
  64. parent.parent.type === "ObjectPattern" &&
  65. parent.value !== node &&
  66. parent.key === node
  67. );
  68. }
  69. /**
  70. * Checks whether the given node represents shorthand definition of a property in an object literal.
  71. * @param {ASTNode} node `Identifier` node to check.
  72. * @returns {boolean} `true` if the node is a shorthand property definition.
  73. */
  74. function isShorthandPropertyDefinition(node) {
  75. const parent = node.parent;
  76. return (
  77. parent.type === "Property" &&
  78. parent.parent.type === "ObjectExpression" &&
  79. parent.shorthand
  80. );
  81. }
  82. //------------------------------------------------------------------------------
  83. // Rule Definition
  84. //------------------------------------------------------------------------------
  85. /** @type {import('../types').Rule.RuleModule} */
  86. module.exports = {
  87. meta: {
  88. deprecated: {
  89. message: "The rule was renamed.",
  90. url: "https://eslint.org/blog/2020/07/eslint-v7.5.0-released/#deprecating-id-blacklist",
  91. deprecatedSince: "7.5.0",
  92. availableUntil: "11.0.0",
  93. replacedBy: [
  94. {
  95. rule: {
  96. name: "id-denylist",
  97. url: "https://eslint.org/docs/rules/id-denylist",
  98. },
  99. },
  100. ],
  101. },
  102. type: "suggestion",
  103. docs: {
  104. description: "Disallow specified identifiers",
  105. recommended: false,
  106. url: "https://eslint.org/docs/latest/rules/id-blacklist",
  107. },
  108. schema: {
  109. type: "array",
  110. items: {
  111. type: "string",
  112. },
  113. uniqueItems: true,
  114. },
  115. messages: {
  116. restricted: "Identifier '{{name}}' is restricted.",
  117. },
  118. },
  119. create(context) {
  120. const denyList = new Set(context.options);
  121. const reportedNodes = new Set();
  122. const sourceCode = context.sourceCode;
  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 (
  142. variable &&
  143. variable.defs.length === 0 &&
  144. variable.references.some(ref => ref.identifier === node)
  145. );
  146. }
  147. /**
  148. * Determines whether the given node should be checked.
  149. * @param {ASTNode} node `Identifier` node.
  150. * @returns {boolean} `true` if the node should be checked.
  151. */
  152. function shouldCheck(node) {
  153. const parent = node.parent;
  154. /*
  155. * Member access has special rules for checking property names.
  156. * Read access to a property with a restricted name is allowed, because it can be on an object that user has no control over.
  157. * Write access isn't allowed, because it potentially creates a new property with a restricted name.
  158. */
  159. if (
  160. parent.type === "MemberExpression" &&
  161. parent.property === node &&
  162. !parent.computed
  163. ) {
  164. return isAssignmentTarget(parent);
  165. }
  166. return (
  167. parent.type !== "CallExpression" &&
  168. parent.type !== "NewExpression" &&
  169. !isRenamedImport(node) &&
  170. !isRenamedInDestructuring(node) &&
  171. !(
  172. isReferenceToGlobalVariable(node) &&
  173. !isShorthandPropertyDefinition(node)
  174. )
  175. );
  176. }
  177. /**
  178. * Reports an AST node as a rule violation.
  179. * @param {ASTNode} node The node to report.
  180. * @returns {void}
  181. * @private
  182. */
  183. function report(node) {
  184. /*
  185. * We used the range instead of the node because it's possible
  186. * for the same identifier to be represented by two different
  187. * nodes, with the most clear example being shorthand properties:
  188. * { foo }
  189. * In this case, "foo" is represented by one node for the name
  190. * and one for the value. The only way to know they are the same
  191. * is to look at the range.
  192. */
  193. if (!reportedNodes.has(node.range.toString())) {
  194. context.report({
  195. node,
  196. messageId: "restricted",
  197. data: {
  198. name: node.name,
  199. },
  200. });
  201. reportedNodes.add(node.range.toString());
  202. }
  203. }
  204. return {
  205. Program(node) {
  206. globalScope = sourceCode.getScope(node);
  207. },
  208. Identifier(node) {
  209. if (isRestricted(node.name) && shouldCheck(node)) {
  210. report(node);
  211. }
  212. },
  213. };
  214. },
  215. };