Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-regex-spaces.js
1 /**
2  * @fileoverview Rule to count multiple spaces in regular expressions
3  * @author Matt DuVall <http://www.mattduvall.com/>
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const astUtils = require("./utils/ast-utils");
13 const regexpp = require("regexpp");
14
15 //------------------------------------------------------------------------------
16 // Helpers
17 //------------------------------------------------------------------------------
18
19 const regExpParser = new regexpp.RegExpParser();
20 const DOUBLE_SPACE = / {2}/u;
21
22 /**
23  * Check if node is a string
24  * @param {ASTNode} node node to evaluate
25  * @returns {boolean} True if its a string
26  * @private
27  */
28 function isString(node) {
29     return node && node.type === "Literal" && typeof node.value === "string";
30 }
31
32 //------------------------------------------------------------------------------
33 // Rule Definition
34 //------------------------------------------------------------------------------
35
36 module.exports = {
37     meta: {
38         type: "suggestion",
39
40         docs: {
41             description: "disallow multiple spaces in regular expressions",
42             category: "Possible Errors",
43             recommended: true,
44             url: "https://eslint.org/docs/rules/no-regex-spaces"
45         },
46
47         schema: [],
48         fixable: "code"
49     },
50
51     create(context) {
52
53         /**
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.
60          * @returns {void}
61          * @private
62          */
63         function checkRegex(nodeToReport, pattern, rawPattern, rawPatternStartRange, flags) {
64
65             // Skip if there are no consecutive spaces in the source code, to avoid reporting e.g., RegExp(' \ ').
66             if (!DOUBLE_SPACE.test(rawPattern)) {
67                 return;
68             }
69
70             const characterClassNodes = [];
71             let regExpAST;
72
73             try {
74                 regExpAST = regExpParser.parsePattern(pattern, 0, pattern.length, flags.includes("u"));
75             } catch (e) {
76
77                 // Ignore regular expressions with syntax errors
78                 return;
79             }
80
81             regexpp.visitRegExpAST(regExpAST, {
82                 onCharacterClassEnter(ccNode) {
83                     characterClassNodes.push(ccNode);
84                 }
85             });
86
87             const spacesPattern = /( {2,})(?: [+*{?]|[^+*{?]|$)/gu;
88             let match;
89
90             while ((match = spacesPattern.exec(pattern))) {
91                 const { 1: { length }, index } = match;
92
93                 // Report only consecutive spaces that are not in character classes.
94                 if (
95                     characterClassNodes.every(({ start, end }) => index < start || end <= index)
96                 ) {
97                     context.report({
98                         node: nodeToReport,
99                         message: "Spaces are hard to count. Use {{{length}}}.",
100                         data: { length },
101                         fix(fixer) {
102                             if (pattern !== rawPattern) {
103                                 return null;
104                             }
105                             return fixer.replaceTextRange(
106                                 [rawPatternStartRange + index, rawPatternStartRange + index + length],
107                                 ` {${length}}`
108                             );
109                         }
110                     });
111
112                     // Report only the first occurence of consecutive spaces
113                     return;
114                 }
115             }
116         }
117
118         /**
119          * Validate regular expression literals
120          * @param {ASTNode} node node to validate
121          * @returns {void}
122          * @private
123          */
124         function checkLiteral(node) {
125             if (node.regex) {
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;
130
131                 checkRegex(
132                     node,
133                     pattern,
134                     rawPattern,
135                     rawPatternStartRange,
136                     flags
137                 );
138             }
139         }
140
141         /**
142          * Validate strings passed to the RegExp constructor
143          * @param {ASTNode} node node to validate
144          * @returns {void}
145          * @private
146          */
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];
153
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 : "";
159
160                 checkRegex(
161                     node,
162                     pattern,
163                     rawPattern,
164                     rawPatternStartRange,
165                     flags
166                 );
167             }
168         }
169
170         return {
171             Literal: checkLiteral,
172             CallExpression: checkFunction,
173             NewExpression: checkFunction
174         };
175     }
176 };