id-match.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. docs: {
  12. description: "require identifiers to match a specified regular expression",
  13. category: "Stylistic Issues",
  14. recommended: false,
  15. url: "https://eslint.org/docs/rules/id-match"
  16. },
  17. schema: [
  18. {
  19. type: "string"
  20. },
  21. {
  22. type: "object",
  23. properties: {
  24. properties: {
  25. type: "boolean"
  26. }
  27. }
  28. }
  29. ]
  30. },
  31. create(context) {
  32. //--------------------------------------------------------------------------
  33. // Helpers
  34. //--------------------------------------------------------------------------
  35. const pattern = context.options[0] || "^.+$",
  36. regexp = new RegExp(pattern);
  37. const options = context.options[1] || {},
  38. properties = !!options.properties,
  39. onlyDeclarations = !!options.onlyDeclarations;
  40. /**
  41. * Checks if a string matches the provided pattern
  42. * @param {string} name The string to check.
  43. * @returns {boolean} if the string is a match
  44. * @private
  45. */
  46. function isInvalid(name) {
  47. return !regexp.test(name);
  48. }
  49. /**
  50. * Verifies if we should report an error or not based on the effective
  51. * parent node and the identifier name.
  52. * @param {ASTNode} effectiveParent The effective parent node of the node to be reported
  53. * @param {string} name The identifier name of the identifier node
  54. * @returns {boolean} whether an error should be reported or not
  55. */
  56. function shouldReport(effectiveParent, name) {
  57. return effectiveParent.type !== "CallExpression" &&
  58. effectiveParent.type !== "NewExpression" &&
  59. isInvalid(name);
  60. }
  61. /**
  62. * Reports an AST node as a rule violation.
  63. * @param {ASTNode} node The node to report.
  64. * @returns {void}
  65. * @private
  66. */
  67. function report(node) {
  68. context.report({
  69. node,
  70. message: "Identifier '{{name}}' does not match the pattern '{{pattern}}'.",
  71. data: {
  72. name: node.name,
  73. pattern
  74. }
  75. });
  76. }
  77. return {
  78. Identifier(node) {
  79. const name = node.name,
  80. parent = node.parent,
  81. effectiveParent = (parent.type === "MemberExpression") ? parent.parent : parent;
  82. if (parent.type === "MemberExpression") {
  83. if (!properties) {
  84. return;
  85. }
  86. // Always check object names
  87. if (parent.object.type === "Identifier" &&
  88. parent.object.name === name) {
  89. if (isInvalid(name)) {
  90. report(node);
  91. }
  92. // Report AssignmentExpressions only if they are the left side of the assignment
  93. } else if (effectiveParent.type === "AssignmentExpression" &&
  94. (effectiveParent.right.type !== "MemberExpression" ||
  95. effectiveParent.left.type === "MemberExpression" &&
  96. effectiveParent.left.property.name === name)) {
  97. if (isInvalid(name)) {
  98. report(node);
  99. }
  100. }
  101. } else if (parent.type === "Property") {
  102. if (!properties || parent.key.name !== name) {
  103. return;
  104. }
  105. if (shouldReport(effectiveParent, name)) {
  106. report(node);
  107. }
  108. } else {
  109. const isDeclaration = effectiveParent.type === "FunctionDeclaration" || effectiveParent.type === "VariableDeclarator";
  110. if (onlyDeclarations && !isDeclaration) {
  111. return;
  112. }
  113. if (shouldReport(effectiveParent, name)) {
  114. report(node);
  115. }
  116. }
  117. }
  118. };
  119. }
  120. };