id-match.js 8.1 KB

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