123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- "use strict";
- const astUtils = require("./utils/ast-utils");
- const regexpp = require("regexpp");
- const regExpParser = new regexpp.RegExpParser();
- const DOUBLE_SPACE = / {2}/u;
- function isString(node) {
- return node && node.type === "Literal" && typeof node.value === "string";
- }
- module.exports = {
- meta: {
- type: "suggestion",
- docs: {
- description: "disallow multiple spaces in regular expressions",
- category: "Possible Errors",
- recommended: true,
- url: "https://eslint.org/docs/rules/no-regex-spaces"
- },
- schema: [],
- fixable: "code"
- },
- create(context) {
-
- function checkRegex(nodeToReport, pattern, rawPattern, rawPatternStartRange, flags) {
-
- if (!DOUBLE_SPACE.test(rawPattern)) {
- return;
- }
- const characterClassNodes = [];
- let regExpAST;
- try {
- regExpAST = regExpParser.parsePattern(pattern, 0, pattern.length, flags.includes("u"));
- } catch (e) {
-
- return;
- }
- regexpp.visitRegExpAST(regExpAST, {
- onCharacterClassEnter(ccNode) {
- characterClassNodes.push(ccNode);
- }
- });
- const spacesPattern = /( {2,})(?: [+*{?]|[^+*{?]|$)/gu;
- let match;
- while ((match = spacesPattern.exec(pattern))) {
- const { 1: { length }, index } = match;
-
- if (
- characterClassNodes.every(({ start, end }) => index < start || end <= index)
- ) {
- context.report({
- node: nodeToReport,
- message: "Spaces are hard to count. Use {{{length}}}.",
- data: { length },
- fix(fixer) {
- if (pattern !== rawPattern) {
- return null;
- }
- return fixer.replaceTextRange(
- [rawPatternStartRange + index, rawPatternStartRange + index + length],
- ` {${length}}`
- );
- }
- });
-
- return;
- }
- }
- }
-
- function checkLiteral(node) {
- if (node.regex) {
- const pattern = node.regex.pattern;
- const rawPattern = node.raw.slice(1, node.raw.lastIndexOf("/"));
- const rawPatternStartRange = node.range[0] + 1;
- const flags = node.regex.flags;
- checkRegex(
- node,
- pattern,
- rawPattern,
- rawPatternStartRange,
- flags
- );
- }
- }
-
- function checkFunction(node) {
- const scope = context.getScope();
- const regExpVar = astUtils.getVariableByName(scope, "RegExp");
- const shadowed = regExpVar && regExpVar.defs.length > 0;
- const patternNode = node.arguments[0];
- const flagsNode = node.arguments[1];
- if (node.callee.type === "Identifier" && node.callee.name === "RegExp" && isString(patternNode) && !shadowed) {
- const pattern = patternNode.value;
- const rawPattern = patternNode.raw.slice(1, -1);
- const rawPatternStartRange = patternNode.range[0] + 1;
- const flags = isString(flagsNode) ? flagsNode.value : "";
- checkRegex(
- node,
- pattern,
- rawPattern,
- rawPatternStartRange,
- flags
- );
- }
- }
- return {
- Literal: checkLiteral,
- CallExpression: checkFunction,
- NewExpression: checkFunction
- };
- }
- };
|