id-denylist.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. type: "suggestion",
  101. docs: {
  102. description: "disallow specified identifiers",
  103. category: "Stylistic Issues",
  104. recommended: false,
  105. url: "https://eslint.org/docs/rules/id-denylist"
  106. },
  107. schema: {
  108. type: "array",
  109. items: {
  110. type: "string"
  111. },
  112. uniqueItems: true
  113. },
  114. messages: {
  115. restricted: "Identifier '{{name}}' is restricted."
  116. }
  117. },
  118. create(context) {
  119. const denyList = new Set(context.options);
  120. const reportedNodes = new Set();
  121. let globalScope;
  122. /**
  123. * Checks whether the given name is restricted.
  124. * @param {string} name The name to check.
  125. * @returns {boolean} `true` if the name is restricted.
  126. * @private
  127. */
  128. function isRestricted(name) {
  129. return denyList.has(name);
  130. }
  131. /**
  132. * Checks whether the given node represents a reference to a global variable that is not declared in the source code.
  133. * These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables.
  134. * @param {ASTNode} node `Identifier` node to check.
  135. * @returns {boolean} `true` if the node is a reference to a global variable.
  136. */
  137. function isReferenceToGlobalVariable(node) {
  138. const variable = globalScope.set.get(node.name);
  139. return variable && variable.defs.length === 0 &&
  140. variable.references.some(ref => ref.identifier === node);
  141. }
  142. /**
  143. * Determines whether the given node should be checked.
  144. * @param {ASTNode} node `Identifier` node.
  145. * @returns {boolean} `true` if the node should be checked.
  146. */
  147. function shouldCheck(node) {
  148. const parent = node.parent;
  149. /*
  150. * Member access has special rules for checking property names.
  151. * Read access to a property with a restricted name is allowed, because it can be on an object that user has no control over.
  152. * Write access isn't allowed, because it potentially creates a new property with a restricted name.
  153. */
  154. if (
  155. parent.type === "MemberExpression" &&
  156. parent.property === node &&
  157. !parent.computed
  158. ) {
  159. return isAssignmentTarget(parent);
  160. }
  161. return (
  162. parent.type !== "CallExpression" &&
  163. parent.type !== "NewExpression" &&
  164. !isRenamedImport(node) &&
  165. !isRenamedInDestructuring(node) &&
  166. !(
  167. isReferenceToGlobalVariable(node) &&
  168. !isShorthandPropertyDefinition(node)
  169. )
  170. );
  171. }
  172. /**
  173. * Reports an AST node as a rule violation.
  174. * @param {ASTNode} node The node to report.
  175. * @returns {void}
  176. * @private
  177. */
  178. function report(node) {
  179. if (!reportedNodes.has(node)) {
  180. context.report({
  181. node,
  182. messageId: "restricted",
  183. data: {
  184. name: node.name
  185. }
  186. });
  187. reportedNodes.add(node);
  188. }
  189. }
  190. return {
  191. Program() {
  192. globalScope = context.getScope();
  193. },
  194. Identifier(node) {
  195. if (isRestricted(node.name) && shouldCheck(node)) {
  196. report(node);
  197. }
  198. }
  199. };
  200. }
  201. };