id-match.js 11 KB

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