]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/no-regex-spaces.js
import 8.3.0 source
[pve-eslint.git] / eslint / tests / lib / rules / no-regex-spaces.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Tests for regex-spaces rule.
3 * @author Matt DuVall <http://www.mattduvall.com/>
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/no-regex-spaces"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
609c276f
TL
15//------------------------------------------------------------------------------
16// Tests
17//------------------------------------------------------------------------------
18
eb39fafa
DC
19const ruleTester = new RuleTester();
20
21ruleTester.run("no-regex-spaces", rule, {
22 valid: [
23 "var foo = /foo/;",
24 "var foo = RegExp('foo')",
25 "var foo = / /;",
26 "var foo = RegExp(' ')",
27 "var foo = / a b c d /;",
28 "var foo = /bar {3}baz/g;",
29 "var foo = RegExp('bar {3}baz', 'g')",
30 "var foo = new RegExp('bar {3}baz')",
31 "var foo = /bar\t\t\tbaz/;",
32 "var foo = RegExp('bar\t\t\tbaz');",
33 "var foo = new RegExp('bar\t\t\tbaz');",
34 "var RegExp = function() {}; var foo = new RegExp('bar baz');",
35 "var RegExp = function() {}; var foo = RegExp('bar baz');",
36 "var foo = / +/;",
37 "var foo = / ?/;",
38 "var foo = / */;",
39 "var foo = / {2}/;",
40
41 // don't report if there are no consecutive spaces in the source code
42 "var foo = /bar \\ baz/;",
43 "var foo = /bar\\ \\ baz/;",
44 "var foo = /bar \\u0020 baz/;",
45 "var foo = /bar\\u0020\\u0020baz/;",
46 "var foo = new RegExp('bar \\ baz')",
47 "var foo = new RegExp('bar\\ \\ baz')",
48 "var foo = new RegExp('bar \\\\ baz')",
49 "var foo = new RegExp('bar \\u0020 baz')",
50 "var foo = new RegExp('bar\\u0020\\u0020baz')",
51 "var foo = new RegExp('bar \\\\u0020 baz')",
52
53 // don't report spaces in character classes
54 "var foo = /[ ]/;",
55 "var foo = /[ ]/;",
56 "var foo = / [ ] /;",
57 "var foo = / [ ] [ ] /;",
58 "var foo = new RegExp('[ ]');",
59 "var foo = new RegExp('[ ]');",
60 "var foo = new RegExp(' [ ] ');",
61 "var foo = RegExp(' [ ] [ ] ');",
62 "var foo = new RegExp(' \\[ ');",
63 "var foo = new RegExp(' \\[ \\] ');",
64
65 // don't report invalid regex
66 "var foo = new RegExp('[ ');",
67 "var foo = new RegExp('{ ', 'u');"
68 ],
69
70 invalid: [
71 {
72 code: "var foo = /bar baz/;",
73 output: "var foo = /bar {2}baz/;",
74 errors: [
75 {
76 messageId: "multipleSpaces",
77 data: { length: "2" },
78 type: "Literal"
79 }
80 ]
81 },
82 {
83 code: "var foo = /bar baz/;",
84 output: "var foo = /bar {4}baz/;",
85 errors: [
86 {
87 messageId: "multipleSpaces",
88 data: { length: "4" },
89 type: "Literal"
90 }
91 ]
92 },
93 {
94 code: "var foo = / a b c d /;",
95 output: "var foo = / a b {2}c d /;",
96 errors: [
97 {
98 messageId: "multipleSpaces",
99 data: { length: "2" },
100 type: "Literal"
101 }
102 ]
103 },
104 {
105 code: "var foo = RegExp(' a b c d ');",
106 output: "var foo = RegExp(' a b c d {2}');",
107 errors: [
108 {
109 messageId: "multipleSpaces",
110 data: { length: "2" },
111 type: "CallExpression"
112 }
113 ]
114 },
115 {
116 code: "var foo = RegExp('bar baz');",
117 output: "var foo = RegExp('bar {4}baz');",
118 errors: [
119 {
120 messageId: "multipleSpaces",
121 data: { length: "4" },
122 type: "CallExpression"
123 }
124 ]
125 },
126 {
127 code: "var foo = new RegExp('bar baz');",
128 output: "var foo = new RegExp('bar {4}baz');",
129 errors: [
130 {
131 messageId: "multipleSpaces",
132 data: { length: "4" },
133 type: "NewExpression"
134 }
135 ]
136 },
137 {
138
139 // `RegExp` is not shadowed in the scope where it's called
140 code: "{ let RegExp = function() {}; } var foo = RegExp('bar baz');",
141 output: "{ let RegExp = function() {}; } var foo = RegExp('bar {4}baz');",
142 parserOptions: { ecmaVersion: 6 },
143 errors: [
144 {
145 messageId: "multipleSpaces",
146 data: { length: "4" },
147 type: "CallExpression"
148 }
149 ]
150 },
151 {
152 code: "var foo = /bar {3}baz/;",
153 output: "var foo = /bar {2} {3}baz/;",
154 errors: [
155 {
156 messageId: "multipleSpaces",
157 data: { length: "2" },
158 type: "Literal"
159 }
160 ]
161 },
162 {
163 code: "var foo = /bar ?baz/;",
164 output: "var foo = /bar {3} ?baz/;",
165 errors: [
166 {
167 messageId: "multipleSpaces",
168 data: { length: "3" },
169 type: "Literal"
170 }
171 ]
172 },
173 {
174 code: "var foo = new RegExp('bar *baz')",
175 output: "var foo = new RegExp('bar {2} *baz')",
176 errors: [
177 {
178 messageId: "multipleSpaces",
179 data: { length: "2" },
180 type: "NewExpression"
181 }
182 ]
183 },
184 {
185 code: "var foo = RegExp('bar +baz')",
186 output: "var foo = RegExp('bar {2} +baz')",
187 errors: [
188 {
189 messageId: "multipleSpaces",
190 data: { length: "2" },
191 type: "CallExpression"
192 }
193 ]
194 },
195 {
196 code: "var foo = new RegExp('bar ');",
197 output: "var foo = new RegExp('bar {4}');",
198 errors: [
199 {
200 messageId: "multipleSpaces",
201 data: { length: "4" },
202 type: "NewExpression"
203 }
204 ]
205 },
206 {
207 code: "var foo = /bar\\ baz/;",
208 output: "var foo = /bar\\ {2}baz/;",
209 errors: [
210 {
211 messageId: "multipleSpaces",
212 data: { length: "2" },
213 type: "Literal"
214 }
215 ]
216 },
217 {
218 code: "var foo = /[ ] /;",
219 output: "var foo = /[ ] {2}/;",
220 errors: [
221 {
222 messageId: "multipleSpaces",
223 data: { length: "2" },
224 type: "Literal"
225 }
226 ]
227 },
228 {
229 code: "var foo = / [ ] /;",
230 output: "var foo = / {2}[ ] /;",
231 errors: [
232 {
233 messageId: "multipleSpaces",
234 data: { length: "2" },
235 type: "Literal"
236 }
237 ]
238 },
239 {
240 code: "var foo = new RegExp('[ ] ');",
241 output: "var foo = new RegExp('[ ] {2}');",
242 errors: [
243 {
244 messageId: "multipleSpaces",
245 data: { length: "2" },
246 type: "NewExpression"
247 }
248 ]
249 },
250 {
251 code: "var foo = RegExp(' [ ]');",
252 output: "var foo = RegExp(' {2}[ ]');",
253 errors: [
254 {
255 messageId: "multipleSpaces",
256 data: { length: "2" },
257 type: "CallExpression"
258 }
259 ]
260 },
261 {
262 code: "var foo = /\\[ /;",
263 output: "var foo = /\\[ {2}/;",
264 errors: [
265 {
266 messageId: "multipleSpaces",
267 data: { length: "2" },
268 type: "Literal"
269 }
270 ]
271 },
272 {
273 code: "var foo = /\\[ \\]/;",
274 output: "var foo = /\\[ {2}\\]/;",
275 errors: [
276 {
277 messageId: "multipleSpaces",
278 data: { length: "2" },
279 type: "Literal"
280 }
281 ]
282 },
283 {
284 code: "var foo = /(?: )/;",
285 output: "var foo = /(?: {2})/;",
286 errors: [
287 {
288 messageId: "multipleSpaces",
289 data: { length: "2" },
290 type: "Literal"
291 }
292 ]
293 },
294 {
295 code: "var foo = RegExp('^foo(?= )');",
296 output: "var foo = RegExp('^foo(?= {3})');",
297 errors: [
298 {
299 messageId: "multipleSpaces",
300 data: { length: "3" },
301 type: "CallExpression"
302 }
303 ]
304 },
305 {
306 code: "var foo = /\\ /",
307 output: "var foo = /\\ {2}/",
308 errors: [
309 {
310 messageId: "multipleSpaces",
311 data: { length: "2" },
312 type: "Literal"
313 }
314 ]
315 },
316 {
317 code: "var foo = / \\ /",
318 output: "var foo = / \\ {2}/",
319 errors: [
320 {
321 messageId: "multipleSpaces",
322 data: { length: "2" },
323 type: "Literal"
324 }
325 ]
326 },
327
328 // report only the first occurrence of consecutive spaces
329 {
330 code: "var foo = / foo /;",
331 output: "var foo = / {2}foo /;",
332 errors: [
333 {
334 messageId: "multipleSpaces",
335 data: { length: "2" },
336 type: "Literal"
337 }
338 ]
339 },
340
341 // don't fix strings with escape sequences
342 {
343 code: "var foo = new RegExp('\\\\d ')",
344 output: null,
345 errors: [
346 {
347 messageId: "multipleSpaces",
348 data: { length: "2" },
349 type: "NewExpression"
350 }
351 ]
352 },
353 {
354 code: "var foo = RegExp('\\u0041 ')",
355 output: null,
356 errors: [
357 {
358 messageId: "multipleSpaces",
359 data: { length: "3" },
360 type: "CallExpression"
361 }
362 ]
363 },
364 {
365 code: "var foo = new RegExp('\\\\[ \\\\]');",
366 output: null,
367 errors: [
368 {
369 messageId: "multipleSpaces",
370 data: { length: "2" },
371 type: "NewExpression"
372 }
373 ]
374 }
375 ]
376});