Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

id-match.js 8.0KB

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