id-match.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * @fileoverview Rule to flag non-matching identifiers
  3. * @author Matthieu Larcher
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "require identifiers to match a specified regular expression",
  14. category: "Stylistic Issues",
  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. onlyDeclarations: {
  30. type: "boolean",
  31. default: false
  32. },
  33. ignoreDestructuring: {
  34. type: "boolean",
  35. default: false
  36. }
  37. },
  38. additionalProperties: false
  39. }
  40. ],
  41. messages: {
  42. notMatch: "Identifier '{{name}}' does not match the pattern '{{pattern}}'."
  43. }
  44. },
  45. create(context) {
  46. //--------------------------------------------------------------------------
  47. // Options
  48. //--------------------------------------------------------------------------
  49. const pattern = context.options[0] || "^.+$",
  50. regexp = new RegExp(pattern, "u");
  51. const options = context.options[1] || {},
  52. properties = !!options.properties,
  53. onlyDeclarations = !!options.onlyDeclarations,
  54. ignoreDestructuring = !!options.ignoreDestructuring;
  55. //--------------------------------------------------------------------------
  56. // Helpers
  57. //--------------------------------------------------------------------------
  58. // contains reported nodes to avoid reporting twice on destructuring with shorthand notation
  59. const reported = new Map();
  60. const ALLOWED_PARENT_TYPES = new Set(["CallExpression", "NewExpression"]);
  61. const DECLARATION_TYPES = new Set(["FunctionDeclaration", "VariableDeclarator"]);
  62. const IMPORT_TYPES = new Set(["ImportSpecifier", "ImportNamespaceSpecifier", "ImportDefaultSpecifier"]);
  63. /**
  64. * Checks if a string matches the provided pattern
  65. * @param {string} name The string to check.
  66. * @returns {boolean} if the string is a match
  67. * @private
  68. */
  69. function isInvalid(name) {
  70. return !regexp.test(name);
  71. }
  72. /**
  73. * Checks if a parent of a node is an ObjectPattern.
  74. * @param {ASTNode} node The node to check.
  75. * @returns {boolean} if the node is inside an ObjectPattern
  76. * @private
  77. */
  78. function isInsideObjectPattern(node) {
  79. let { parent } = node;
  80. while (parent) {
  81. if (parent.type === "ObjectPattern") {
  82. return true;
  83. }
  84. parent = parent.parent;
  85. }
  86. return false;
  87. }
  88. /**
  89. * Verifies if we should report an error or not based on the effective
  90. * parent node and the identifier name.
  91. * @param {ASTNode} effectiveParent The effective parent node of the node to be reported
  92. * @param {string} name The identifier name of the identifier node
  93. * @returns {boolean} whether an error should be reported or not
  94. */
  95. function shouldReport(effectiveParent, name) {
  96. return (!onlyDeclarations || DECLARATION_TYPES.has(effectiveParent.type)) &&
  97. !ALLOWED_PARENT_TYPES.has(effectiveParent.type) && isInvalid(name);
  98. }
  99. /**
  100. * Reports an AST node as a rule violation.
  101. * @param {ASTNode} node The node to report.
  102. * @returns {void}
  103. * @private
  104. */
  105. function report(node) {
  106. if (!reported.has(node)) {
  107. context.report({
  108. node,
  109. messageId: "notMatch",
  110. data: {
  111. name: node.name,
  112. pattern
  113. }
  114. });
  115. reported.set(node, true);
  116. }
  117. }
  118. return {
  119. Identifier(node) {
  120. const name = node.name,
  121. parent = node.parent,
  122. effectiveParent = (parent.type === "MemberExpression") ? parent.parent : parent;
  123. if (parent.type === "MemberExpression") {
  124. if (!properties) {
  125. return;
  126. }
  127. // Always check object names
  128. if (parent.object.type === "Identifier" &&
  129. parent.object.name === name) {
  130. if (isInvalid(name)) {
  131. report(node);
  132. }
  133. // Report AssignmentExpressions left side's assigned variable id
  134. } else if (effectiveParent.type === "AssignmentExpression" &&
  135. effectiveParent.left.type === "MemberExpression" &&
  136. effectiveParent.left.property.name === node.name) {
  137. if (isInvalid(name)) {
  138. report(node);
  139. }
  140. // Report AssignmentExpressions only if they are the left side of the assignment
  141. } else if (effectiveParent.type === "AssignmentExpression" && effectiveParent.right.type !== "MemberExpression") {
  142. if (isInvalid(name)) {
  143. report(node);
  144. }
  145. }
  146. /*
  147. * Properties have their own rules, and
  148. * AssignmentPattern nodes can be treated like Properties:
  149. * e.g.: const { no_camelcased = false } = bar;
  150. */
  151. } else if (parent.type === "Property" || parent.type === "AssignmentPattern") {
  152. if (parent.parent && parent.parent.type === "ObjectPattern") {
  153. if (parent.shorthand && parent.value.left && isInvalid(name)) {
  154. report(node);
  155. }
  156. const assignmentKeyEqualsValue = parent.key.name === parent.value.name;
  157. // prevent checking righthand side of destructured object
  158. if (!assignmentKeyEqualsValue && parent.key === node) {
  159. return;
  160. }
  161. const valueIsInvalid = parent.value.name && isInvalid(name);
  162. // ignore destructuring if the option is set, unless a new identifier is created
  163. if (valueIsInvalid && !(assignmentKeyEqualsValue && ignoreDestructuring)) {
  164. report(node);
  165. }
  166. }
  167. // never check properties or always ignore destructuring
  168. if (!properties || (ignoreDestructuring && isInsideObjectPattern(node))) {
  169. return;
  170. }
  171. // don't check right hand side of AssignmentExpression to prevent duplicate warnings
  172. if (parent.right !== node && shouldReport(effectiveParent, name)) {
  173. report(node);
  174. }
  175. // Check if it's an import specifier
  176. } else if (IMPORT_TYPES.has(parent.type)) {
  177. // Report only if the local imported identifier is invalid
  178. if (parent.local && parent.local.name === node.name && isInvalid(name)) {
  179. report(node);
  180. }
  181. // Report anything that is invalid that isn't a CallExpression
  182. } else if (shouldReport(effectiveParent, name)) {
  183. report(node);
  184. }
  185. }
  186. };
  187. }
  188. };