2 * @fileoverview Rule to count multiple spaces in regular expressions
3 * @author Matt DuVall <http://www.mattduvall.com/>
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
13 const regexpp = require("regexpp");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const regExpParser = new regexpp.RegExpParser();
20 const DOUBLE_SPACE = / {2}/u;
23 * Check if node is a string
24 * @param {ASTNode} node node to evaluate
25 * @returns {boolean} True if its a string
28 function isString(node) {
29 return node && node.type === "Literal" && typeof node.value === "string";
32 //------------------------------------------------------------------------------
34 //------------------------------------------------------------------------------
41 description: "disallow multiple spaces in regular expressions",
42 category: "Possible Errors",
44 url: "https://eslint.org/docs/rules/no-regex-spaces"
54 * Validate regular expression
55 * @param {ASTNode} nodeToReport Node to report.
56 * @param {string} pattern Regular expression pattern to validate.
57 * @param {string} rawPattern Raw representation of the pattern in the source code.
58 * @param {number} rawPatternStartRange Start range of the pattern in the source code.
59 * @param {string} flags Regular expression flags.
63 function checkRegex(nodeToReport, pattern, rawPattern, rawPatternStartRange, flags) {
65 // Skip if there are no consecutive spaces in the source code, to avoid reporting e.g., RegExp(' \ ').
66 if (!DOUBLE_SPACE.test(rawPattern)) {
70 const characterClassNodes = [];
74 regExpAST = regExpParser.parsePattern(pattern, 0, pattern.length, flags.includes("u"));
77 // Ignore regular expressions with syntax errors
81 regexpp.visitRegExpAST(regExpAST, {
82 onCharacterClassEnter(ccNode) {
83 characterClassNodes.push(ccNode);
87 const spacesPattern = /( {2,})(?: [+*{?]|[^+*{?]|$)/gu;
90 while ((match = spacesPattern.exec(pattern))) {
91 const { 1: { length }, index } = match;
93 // Report only consecutive spaces that are not in character classes.
95 characterClassNodes.every(({ start, end }) => index < start || end <= index)
99 message: "Spaces are hard to count. Use {{{length}}}.",
102 if (pattern !== rawPattern) {
105 return fixer.replaceTextRange(
106 [rawPatternStartRange + index, rawPatternStartRange + index + length],
112 // Report only the first occurence of consecutive spaces
119 * Validate regular expression literals
120 * @param {ASTNode} node node to validate
124 function checkLiteral(node) {
126 const pattern = node.regex.pattern;
127 const rawPattern = node.raw.slice(1, node.raw.lastIndexOf("/"));
128 const rawPatternStartRange = node.range[0] + 1;
129 const flags = node.regex.flags;
135 rawPatternStartRange,
142 * Validate strings passed to the RegExp constructor
143 * @param {ASTNode} node node to validate
147 function checkFunction(node) {
148 const scope = context.getScope();
149 const regExpVar = astUtils.getVariableByName(scope, "RegExp");
150 const shadowed = regExpVar && regExpVar.defs.length > 0;
151 const patternNode = node.arguments[0];
152 const flagsNode = node.arguments[1];
154 if (node.callee.type === "Identifier" && node.callee.name === "RegExp" && isString(patternNode) && !shadowed) {
155 const pattern = patternNode.value;
156 const rawPattern = patternNode.raw.slice(1, -1);
157 const rawPatternStartRange = patternNode.range[0] + 1;
158 const flags = isString(flagsNode) ? flagsNode.value : "";
164 rawPatternStartRange,
171 Literal: checkLiteral,
172 CallExpression: checkFunction,
173 NewExpression: checkFunction