id-match.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**
  2. * @fileoverview Rule to flag non-matching identifiers
  3. * @author Matthieu Larcher
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../shared/types').Rule} */
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "Require identifiers to match a specified regular expression",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/id-match"
  17. },
  18. schema: [
  19. {
  20. type: "string"
  21. },
  22. {
  23. type: "object",
  24. properties: {
  25. properties: {
  26. type: "boolean",
  27. default: false
  28. },
  29. classFields: {
  30. type: "boolean",
  31. default: false
  32. },
  33. onlyDeclarations: {
  34. type: "boolean",
  35. default: false
  36. },
  37. ignoreDestructuring: {
  38. type: "boolean",
  39. default: false
  40. }
  41. },
  42. additionalProperties: false
  43. }
  44. ],
  45. messages: {
  46. notMatch: "Identifier '{{name}}' does not match the pattern '{{pattern}}'.",
  47. notMatchPrivate: "Identifier '#{{name}}' does not match the pattern '{{pattern}}'."
  48. }
  49. },
  50. create(context) {
  51. //--------------------------------------------------------------------------
  52. // Options
  53. //--------------------------------------------------------------------------
  54. const pattern = context.options[0] || "^.+$",
  55. regexp = new RegExp(pattern, "u");
  56. const options = context.options[1] || {},
  57. checkProperties = !!options.properties,
  58. checkClassFields = !!options.classFields,
  59. onlyDeclarations = !!options.onlyDeclarations,
  60. ignoreDestructuring = !!options.ignoreDestructuring;
  61. let globalScope;
  62. //--------------------------------------------------------------------------
  63. // Helpers
  64. //--------------------------------------------------------------------------
  65. // contains reported nodes to avoid reporting twice on destructuring with shorthand notation
  66. const reportedNodes = new Set();
  67. const ALLOWED_PARENT_TYPES = new Set(["CallExpression", "NewExpression"]);
  68. const DECLARATION_TYPES = new Set(["FunctionDeclaration", "VariableDeclarator"]);
  69. const IMPORT_TYPES = new Set(["ImportSpecifier", "ImportNamespaceSpecifier", "ImportDefaultSpecifier"]);
  70. /**
  71. * Checks whether the given node represents a reference to a global variable that is not declared in the source code.
  72. * These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables.
  73. * @param {ASTNode} node `Identifier` node to check.
  74. * @returns {boolean} `true` if the node is a reference to a global variable.
  75. */
  76. function isReferenceToGlobalVariable(node) {
  77. const variable = globalScope.set.get(node.name);
  78. return variable && variable.defs.length === 0 &&
  79. variable.references.some(ref => ref.identifier === node);
  80. }
  81. /**
  82. * Checks if a string matches the provided pattern
  83. * @param {string} name The string to check.
  84. * @returns {boolean} if the string is a match
  85. * @private
  86. */
  87. function isInvalid(name) {
  88. return !regexp.test(name);
  89. }
  90. /**
  91. * Checks if a parent of a node is an ObjectPattern.
  92. * @param {ASTNode} node The node to check.
  93. * @returns {boolean} if the node is inside an ObjectPattern
  94. * @private
  95. */
  96. function isInsideObjectPattern(node) {
  97. let { parent } = node;
  98. while (parent) {
  99. if (parent.type === "ObjectPattern") {
  100. return true;
  101. }
  102. parent = parent.parent;
  103. }
  104. return false;
  105. }
  106. /**
  107. * Verifies if we should report an error or not based on the effective
  108. * parent node and the identifier name.
  109. * @param {ASTNode} effectiveParent The effective parent node of the node to be reported
  110. * @param {string} name The identifier name of the identifier node
  111. * @returns {boolean} whether an error should be reported or not
  112. */
  113. function shouldReport(effectiveParent, name) {
  114. return (!onlyDeclarations || DECLARATION_TYPES.has(effectiveParent.type)) &&
  115. !ALLOWED_PARENT_TYPES.has(effectiveParent.type) && isInvalid(name);
  116. }
  117. /**
  118. * Reports an AST node as a rule violation.
  119. * @param {ASTNode} node The node to report.
  120. * @returns {void}
  121. * @private
  122. */
  123. function report(node) {
  124. /*
  125. * We used the range instead of the node because it's possible
  126. * for the same identifier to be represented by two different
  127. * nodes, with the most clear example being shorthand properties:
  128. * { foo }
  129. * In this case, "foo" is represented by one node for the name
  130. * and one for the value. The only way to know they are the same
  131. * is to look at the range.
  132. */
  133. if (!reportedNodes.has(node.range.toString())) {
  134. const messageId = (node.type === "PrivateIdentifier")
  135. ? "notMatchPrivate" : "notMatch";
  136. context.report({
  137. node,
  138. messageId,
  139. data: {
  140. name: node.name,
  141. pattern
  142. }
  143. });
  144. reportedNodes.add(node.range.toString());
  145. }
  146. }
  147. return {
  148. Program() {
  149. globalScope = context.getScope();
  150. },
  151. Identifier(node) {
  152. const name = node.name,
  153. parent = node.parent,
  154. effectiveParent = (parent.type === "MemberExpression") ? parent.parent : parent;
  155. if (isReferenceToGlobalVariable(node)) {
  156. return;
  157. }
  158. if (parent.type === "MemberExpression") {
  159. if (!checkProperties) {
  160. return;
  161. }
  162. // Always check object names
  163. if (parent.object.type === "Identifier" &&
  164. parent.object.name === name) {
  165. if (isInvalid(name)) {
  166. report(node);
  167. }
  168. // Report AssignmentExpressions left side's assigned variable id
  169. } else if (effectiveParent.type === "AssignmentExpression" &&
  170. effectiveParent.left.type === "MemberExpression" &&
  171. effectiveParent.left.property.name === node.name) {
  172. if (isInvalid(name)) {
  173. report(node);
  174. }
  175. // Report AssignmentExpressions only if they are the left side of the assignment
  176. } else if (effectiveParent.type === "AssignmentExpression" && effectiveParent.right.type !== "MemberExpression") {
  177. if (isInvalid(name)) {
  178. report(node);
  179. }
  180. }
  181. // For https://github.com/eslint/eslint/issues/15123
  182. } else if (
  183. parent.type === "Property" &&
  184. parent.parent.type === "ObjectExpression" &&
  185. parent.key === node &&
  186. !parent.computed
  187. ) {
  188. if (checkProperties && isInvalid(name)) {
  189. report(node);
  190. }
  191. /*
  192. * Properties have their own rules, and
  193. * AssignmentPattern nodes can be treated like Properties:
  194. * e.g.: const { no_camelcased = false } = bar;
  195. */
  196. } else if (parent.type === "Property" || parent.type === "AssignmentPattern") {
  197. if (parent.parent && parent.parent.type === "ObjectPattern") {
  198. if (!ignoreDestructuring && parent.shorthand && parent.value.left && isInvalid(name)) {
  199. report(node);
  200. }
  201. const assignmentKeyEqualsValue = parent.key.name === parent.value.name;
  202. // prevent checking righthand side of destructured object
  203. if (!assignmentKeyEqualsValue && parent.key === node) {
  204. return;
  205. }
  206. const valueIsInvalid = parent.value.name && isInvalid(name);
  207. // ignore destructuring if the option is set, unless a new identifier is created
  208. if (valueIsInvalid && !(assignmentKeyEqualsValue && ignoreDestructuring)) {
  209. report(node);
  210. }
  211. }
  212. // never check properties or always ignore destructuring
  213. if ((!checkProperties && !parent.computed) || (ignoreDestructuring && isInsideObjectPattern(node))) {
  214. return;
  215. }
  216. // don't check right hand side of AssignmentExpression to prevent duplicate warnings
  217. if (parent.right !== node && shouldReport(effectiveParent, name)) {
  218. report(node);
  219. }
  220. // Check if it's an import specifier
  221. } else if (IMPORT_TYPES.has(parent.type)) {
  222. // Report only if the local imported identifier is invalid
  223. if (parent.local && parent.local.name === node.name && isInvalid(name)) {
  224. report(node);
  225. }
  226. } else if (parent.type === "PropertyDefinition") {
  227. if (checkClassFields && isInvalid(name)) {
  228. report(node);
  229. }
  230. // Report anything that is invalid that isn't a CallExpression
  231. } else if (shouldReport(effectiveParent, name)) {
  232. report(node);
  233. }
  234. },
  235. "PrivateIdentifier"(node) {
  236. const isClassField = node.parent.type === "PropertyDefinition";
  237. if (isClassField && !checkClassFields) {
  238. return;
  239. }
  240. if (isInvalid(node.name)) {
  241. report(node);
  242. }
  243. }
  244. };
  245. }
  246. };