no-regex-spaces.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * @fileoverview Rule to count multiple spaces in regular expressions
  3. * @author Matt DuVall <http://www.mattduvall.com/>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. const regexpp = require("@eslint-community/regexpp");
  11. //------------------------------------------------------------------------------
  12. // Helpers
  13. //------------------------------------------------------------------------------
  14. const regExpParser = new regexpp.RegExpParser();
  15. const DOUBLE_SPACE = / {2}/u;
  16. /**
  17. * Check if node is a string
  18. * @param {ASTNode} node node to evaluate
  19. * @returns {boolean} True if its a string
  20. * @private
  21. */
  22. function isString(node) {
  23. return node && node.type === "Literal" && typeof node.value === "string";
  24. }
  25. //------------------------------------------------------------------------------
  26. // Rule Definition
  27. //------------------------------------------------------------------------------
  28. /** @type {import('../types').Rule.RuleModule} */
  29. module.exports = {
  30. meta: {
  31. type: "suggestion",
  32. docs: {
  33. description: "Disallow multiple spaces in regular expressions",
  34. recommended: true,
  35. url: "https://eslint.org/docs/latest/rules/no-regex-spaces",
  36. },
  37. schema: [],
  38. fixable: "code",
  39. messages: {
  40. multipleSpaces: "Spaces are hard to count. Use {{{length}}}.",
  41. },
  42. },
  43. create(context) {
  44. const sourceCode = context.sourceCode;
  45. /**
  46. * Validate regular expression
  47. * @param {ASTNode} nodeToReport Node to report.
  48. * @param {string} pattern Regular expression pattern to validate.
  49. * @param {string} rawPattern Raw representation of the pattern in the source code.
  50. * @param {number} rawPatternStartRange Start range of the pattern in the source code.
  51. * @param {string} flags Regular expression flags.
  52. * @returns {void}
  53. * @private
  54. */
  55. function checkRegex(
  56. nodeToReport,
  57. pattern,
  58. rawPattern,
  59. rawPatternStartRange,
  60. flags,
  61. ) {
  62. // Skip if there are no consecutive spaces in the source code, to avoid reporting e.g., RegExp(' \ ').
  63. if (!DOUBLE_SPACE.test(rawPattern)) {
  64. return;
  65. }
  66. const characterClassNodes = [];
  67. let regExpAST;
  68. try {
  69. regExpAST = regExpParser.parsePattern(
  70. pattern,
  71. 0,
  72. pattern.length,
  73. {
  74. unicode: flags.includes("u"),
  75. unicodeSets: flags.includes("v"),
  76. },
  77. );
  78. } catch {
  79. // Ignore regular expressions with syntax errors
  80. return;
  81. }
  82. regexpp.visitRegExpAST(regExpAST, {
  83. onCharacterClassEnter(ccNode) {
  84. characterClassNodes.push(ccNode);
  85. },
  86. });
  87. const spacesPattern = /( {2,})(?: [+*{?]|[^+*{?]|$)/gu;
  88. let match;
  89. while ((match = spacesPattern.exec(pattern))) {
  90. const {
  91. 1: { length },
  92. index,
  93. } = match;
  94. // Report only consecutive spaces that are not in character classes.
  95. if (
  96. characterClassNodes.every(
  97. ({ start, end }) => index < start || end <= index,
  98. )
  99. ) {
  100. context.report({
  101. node: nodeToReport,
  102. messageId: "multipleSpaces",
  103. data: { length },
  104. fix(fixer) {
  105. if (pattern !== rawPattern) {
  106. return null;
  107. }
  108. return fixer.replaceTextRange(
  109. [
  110. rawPatternStartRange + index,
  111. rawPatternStartRange + index + length,
  112. ],
  113. ` {${length}}`,
  114. );
  115. },
  116. });
  117. // Report only the first occurrence of consecutive spaces
  118. return;
  119. }
  120. }
  121. }
  122. /**
  123. * Validate regular expression literals
  124. * @param {ASTNode} node node to validate
  125. * @returns {void}
  126. * @private
  127. */
  128. function checkLiteral(node) {
  129. if (node.regex) {
  130. const pattern = node.regex.pattern;
  131. const rawPattern = node.raw.slice(1, node.raw.lastIndexOf("/"));
  132. const rawPatternStartRange = node.range[0] + 1;
  133. const flags = node.regex.flags;
  134. checkRegex(
  135. node,
  136. pattern,
  137. rawPattern,
  138. rawPatternStartRange,
  139. flags,
  140. );
  141. }
  142. }
  143. /**
  144. * Validate strings passed to the RegExp constructor
  145. * @param {ASTNode} node node to validate
  146. * @returns {void}
  147. * @private
  148. */
  149. function checkFunction(node) {
  150. const scope = sourceCode.getScope(node);
  151. const regExpVar = astUtils.getVariableByName(scope, "RegExp");
  152. const shadowed = regExpVar && regExpVar.defs.length > 0;
  153. const patternNode = node.arguments[0];
  154. if (
  155. node.callee.type === "Identifier" &&
  156. node.callee.name === "RegExp" &&
  157. isString(patternNode) &&
  158. !shadowed
  159. ) {
  160. const pattern = patternNode.value;
  161. const rawPattern = patternNode.raw.slice(1, -1);
  162. const rawPatternStartRange = patternNode.range[0] + 1;
  163. let flags;
  164. if (node.arguments.length < 2) {
  165. // It has no flags.
  166. flags = "";
  167. } else {
  168. const flagsNode = node.arguments[1];
  169. if (isString(flagsNode)) {
  170. flags = flagsNode.value;
  171. } else {
  172. // The flags cannot be determined.
  173. return;
  174. }
  175. }
  176. checkRegex(
  177. node,
  178. pattern,
  179. rawPattern,
  180. rawPatternStartRange,
  181. flags,
  182. );
  183. }
  184. }
  185. return {
  186. Literal: checkLiteral,
  187. CallExpression: checkFunction,
  188. NewExpression: checkFunction,
  189. };
  190. },
  191. };