]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for semi-spacing. | |
3 | * @author Mathias Schreck | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/semi-spacing"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | const ruleTester = new RuleTester(); | |
20 | ||
21 | ruleTester.run("semi-spacing", rule, { | |
22 | valid: [ | |
23 | "var a = 'b';", | |
24 | "var a = 'b ; c';", | |
25 | "var a = 'b',\nc = 'd';", | |
26 | "var a = function() {};", | |
27 | ";(function(){}());", | |
28 | "var a = 'b'\n;(function(){}())", | |
29 | "debugger\n;(function(){}())", | |
30 | "while (true) { break; }", | |
31 | "while (true) { continue; }", | |
32 | "debugger;", | |
33 | "function foo() { return; }", | |
34 | "throw new Error('foo');", | |
35 | "for (var i = 0; i < 10; i++) {}", | |
36 | "for (;;) {}", | |
37 | { | |
38 | code: "var a = 'b' ;", | |
39 | options: [{ before: true, after: true }] | |
40 | }, | |
41 | { | |
42 | code: "var a = 'b';c = 'd';", | |
43 | options: [{ before: false, after: false }] | |
44 | }, | |
45 | { | |
46 | code: "for (var i = 0 ;i < 10 ;i++) {}", | |
47 | options: [{ before: true, after: false }] | |
48 | }, | |
49 | { | |
50 | code: "for (var i = 0 ; i < 10 ; i++) {}", | |
51 | options: [{ before: true, after: true }] | |
52 | }, | |
53 | ||
54 | // https://github.com/eslint/eslint/issues/3721 | |
55 | "function foo(){return 2;}", | |
56 | "for(var i = 0; i < results.length;) {}", | |
57 | { code: "function foo() { return 2; }", options: [{ after: false }] }, | |
6f036462 TL |
58 | { code: "for ( var i = 0;i < results.length; ) {}", options: [{ after: false }] }, |
59 | ||
60 | "do {} while (true); foo" | |
eb39fafa DC |
61 | ], |
62 | invalid: [ | |
ebb53d86 TL |
63 | { |
64 | code: "var a = 'b' ;", | |
65 | output: "var a = 'b';", | |
66 | errors: [ | |
67 | { | |
68 | messageId: "unexpectedWhitespaceBefore", | |
69 | type: "VariableDeclaration", | |
70 | line: 1, | |
71 | column: 12, | |
72 | endLine: 1, | |
73 | endColumn: 14 | |
74 | } | |
75 | ] | |
76 | }, | |
eb39fafa DC |
77 | { |
78 | code: "var a = 'b' ;", | |
79 | output: "var a = 'b';", | |
ebb53d86 TL |
80 | errors: [ |
81 | { | |
82 | messageId: "unexpectedWhitespaceBefore", | |
83 | type: "VariableDeclaration", | |
84 | line: 1, | |
85 | column: 12, | |
86 | endLine: 1, | |
87 | endColumn: 13 | |
88 | } | |
89 | ] | |
eb39fafa DC |
90 | }, |
91 | { | |
92 | code: "var a = 'b',\nc = 'd' ;", | |
93 | output: "var a = 'b',\nc = 'd';", | |
ebb53d86 | 94 | errors: [{ messageId: "unexpectedWhitespaceBefore", type: "VariableDeclaration", line: 2, column: 8 }] |
eb39fafa DC |
95 | }, |
96 | { | |
97 | code: "var a = function() {} ;", | |
98 | output: "var a = function() {};", | |
ebb53d86 | 99 | errors: [{ messageId: "unexpectedWhitespaceBefore", type: "VariableDeclaration", line: 1, column: 22 }] |
eb39fafa DC |
100 | }, |
101 | { | |
102 | code: "var a = function() {\n} ;", | |
103 | output: "var a = function() {\n};", | |
ebb53d86 | 104 | errors: [{ messageId: "unexpectedWhitespaceBefore", type: "VariableDeclaration", line: 2, column: 2 }] |
eb39fafa DC |
105 | }, |
106 | { | |
107 | code: "/^a$/.test('b') ;", | |
108 | output: "/^a$/.test('b');", | |
ebb53d86 | 109 | errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ExpressionStatement", line: 1, column: 16 }] |
eb39fafa DC |
110 | }, |
111 | { | |
112 | code: ";(function(){}()) ;", | |
113 | output: ";(function(){}());", | |
ebb53d86 | 114 | errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ExpressionStatement", line: 1, column: 18 }] |
eb39fafa DC |
115 | }, |
116 | { | |
117 | code: "while (true) { break ; }", | |
118 | output: "while (true) { break; }", | |
ebb53d86 | 119 | errors: [{ messageId: "unexpectedWhitespaceBefore", type: "BreakStatement", line: 1, column: 21 }] |
eb39fafa DC |
120 | }, |
121 | { | |
122 | code: "while (true) { continue ; }", | |
123 | output: "while (true) { continue; }", | |
ebb53d86 | 124 | errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ContinueStatement", line: 1, column: 24 }] |
eb39fafa DC |
125 | }, |
126 | { | |
127 | code: "debugger ;", | |
128 | output: "debugger;", | |
ebb53d86 | 129 | errors: [{ messageId: "unexpectedWhitespaceBefore", type: "DebuggerStatement", line: 1, column: 9 }] |
eb39fafa DC |
130 | }, |
131 | { | |
132 | code: "function foo() { return ; }", | |
133 | output: "function foo() { return; }", | |
ebb53d86 | 134 | errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ReturnStatement", line: 1, column: 24 }] |
eb39fafa DC |
135 | }, |
136 | { | |
137 | code: "throw new Error('foo') ;", | |
138 | output: "throw new Error('foo');", | |
ebb53d86 | 139 | errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ThrowStatement", line: 1, column: 23 }] |
eb39fafa DC |
140 | }, |
141 | { | |
142 | code: "for (var i = 0 ; i < 10 ; i++) {}", | |
143 | output: "for (var i = 0; i < 10; i++) {}", | |
144 | errors: [ | |
ebb53d86 TL |
145 | { messageId: "unexpectedWhitespaceBefore", type: "ForStatement", line: 1, column: 15 }, |
146 | { messageId: "unexpectedWhitespaceBefore", type: "ForStatement", line: 1, column: 24 } | |
eb39fafa DC |
147 | ] |
148 | }, | |
149 | { | |
150 | code: "var a = 'b';c = 'd';", | |
151 | output: "var a = 'b'; c = 'd';", | |
ebb53d86 TL |
152 | errors: [ |
153 | { | |
154 | messageId: "missingWhitespaceAfter", | |
155 | type: "VariableDeclaration", | |
156 | line: 1, | |
157 | column: 12, | |
158 | endLine: 1, | |
159 | endColumn: 13 | |
160 | } | |
161 | ] | |
eb39fafa DC |
162 | }, |
163 | { | |
164 | code: "var a = 'b';", | |
165 | output: "var a = 'b' ;", | |
166 | options: [{ before: true, after: true }], | |
ebb53d86 TL |
167 | errors: [ |
168 | { | |
169 | messageId: "missingWhitespaceBefore", | |
170 | type: "VariableDeclaration", | |
171 | line: 1, | |
172 | column: 12, | |
173 | endLine: 1, | |
174 | endColumn: 13 | |
175 | } | |
176 | ] | |
eb39fafa DC |
177 | }, |
178 | { | |
179 | code: "var a = 'b'; c = 'd';", | |
180 | output: "var a = 'b';c = 'd';", | |
181 | options: [{ before: false, after: false }], | |
ebb53d86 TL |
182 | errors: [ |
183 | { | |
184 | messageId: "unexpectedWhitespaceAfter", | |
185 | type: "VariableDeclaration", | |
186 | line: 1, | |
187 | column: 13, | |
188 | endLine: 1, | |
189 | endColumn: 14 | |
190 | } | |
191 | ] | |
192 | }, | |
193 | { | |
194 | code: "var a = 'b'; c = 'd';", | |
195 | output: "var a = 'b';c = 'd';", | |
196 | options: [{ before: false, after: false }], | |
197 | errors: [ | |
198 | { | |
199 | messageId: "unexpectedWhitespaceAfter", | |
200 | type: "VariableDeclaration", | |
201 | line: 1, | |
202 | column: 13, | |
203 | endLine: 1, | |
204 | endColumn: 16 | |
205 | } | |
206 | ] | |
eb39fafa DC |
207 | }, |
208 | { | |
209 | code: "for (var i = 0;i < 10;i++) {}", | |
210 | output: "for (var i = 0; i < 10; i++) {}", | |
211 | errors: [ | |
ebb53d86 TL |
212 | { |
213 | messageId: "missingWhitespaceAfter", | |
214 | type: "ForStatement", | |
215 | line: 1, | |
216 | column: 15, | |
217 | endLine: 1, | |
218 | endColumn: 16 | |
219 | }, | |
220 | { | |
221 | messageId: "missingWhitespaceAfter", | |
222 | type: "ForStatement", | |
223 | line: 1, | |
224 | column: 22, | |
225 | endLine: 1, | |
226 | endColumn: 23 | |
227 | } | |
eb39fafa DC |
228 | ] |
229 | }, | |
230 | { | |
231 | code: "for (var i = 0; i < 10; i++) {}", | |
232 | output: "for (var i = 0 ; i < 10 ; i++) {}", | |
233 | options: [{ before: true, after: true }], | |
234 | errors: [ | |
ebb53d86 TL |
235 | { |
236 | messageId: "missingWhitespaceBefore", | |
237 | type: "ForStatement", | |
238 | line: 1, | |
239 | column: 15, | |
240 | endLine: 1, | |
241 | endColumn: 16 | |
242 | }, | |
243 | { | |
244 | messageId: "missingWhitespaceBefore", | |
245 | type: "ForStatement", | |
246 | line: 1, | |
247 | column: 23, | |
248 | endLine: 1, | |
249 | endColumn: 24 | |
250 | } | |
eb39fafa DC |
251 | ] |
252 | }, | |
253 | { | |
254 | code: "for (var i = 0; i < 10; i++) {}", | |
255 | output: "for (var i = 0;i < 10;i++) {}", | |
256 | options: [{ before: false, after: false }], | |
257 | errors: [ | |
ebb53d86 TL |
258 | { messageId: "unexpectedWhitespaceAfter", type: "ForStatement", line: 1, column: 16 }, |
259 | { messageId: "unexpectedWhitespaceAfter", type: "ForStatement", line: 1, column: 24 } | |
eb39fafa DC |
260 | ] |
261 | }, | |
262 | { | |
263 | code: "import Foo from 'bar' ;", | |
264 | output: "import Foo from 'bar';", | |
265 | options: [{ before: false, after: true }], | |
266 | parserOptions: { ecmaVersion: 6, sourceType: "module" }, | |
267 | errors: [ | |
ebb53d86 | 268 | { messageId: "unexpectedWhitespaceBefore", type: "ImportDeclaration", line: 1, column: 22 } |
eb39fafa DC |
269 | ] |
270 | }, | |
271 | { | |
272 | code: "import * as foo from 'bar' ;", | |
273 | output: "import * as foo from 'bar';", | |
274 | options: [{ before: false, after: true }], | |
275 | parserOptions: { ecmaVersion: 6, sourceType: "module" }, | |
276 | errors: [ | |
ebb53d86 | 277 | { messageId: "unexpectedWhitespaceBefore", type: "ImportDeclaration", line: 1, column: 27 } |
eb39fafa DC |
278 | ] |
279 | }, | |
280 | { | |
281 | code: "var foo = 0; export {foo} ;", | |
282 | output: "var foo = 0; export {foo};", | |
283 | options: [{ before: false, after: true }], | |
284 | parserOptions: { ecmaVersion: 6, sourceType: "module" }, | |
285 | errors: [ | |
ebb53d86 | 286 | { messageId: "unexpectedWhitespaceBefore", type: "ExportNamedDeclaration", line: 1, column: 26 } |
eb39fafa DC |
287 | ] |
288 | }, | |
289 | { | |
290 | code: "export * from 'foo' ;", | |
291 | output: "export * from 'foo';", | |
292 | options: [{ before: false, after: true }], | |
293 | parserOptions: { ecmaVersion: 6, sourceType: "module" }, | |
294 | errors: [ | |
ebb53d86 | 295 | { messageId: "unexpectedWhitespaceBefore", type: "ExportAllDeclaration", line: 1, column: 20 } |
eb39fafa DC |
296 | ] |
297 | }, | |
298 | { | |
299 | code: "export default foo ;", | |
300 | output: "export default foo;", | |
301 | options: [{ before: false, after: true }], | |
302 | parserOptions: { ecmaVersion: 6, sourceType: "module" }, | |
303 | errors: [ | |
ebb53d86 TL |
304 | { messageId: "unexpectedWhitespaceBefore", type: "ExportDefaultDeclaration", line: 1, column: 19 } |
305 | ] | |
306 | }, | |
307 | { | |
308 | code: "while(foo) {continue ;}", | |
309 | output: "while(foo) {continue;}", | |
310 | options: [{ before: false, after: true }], | |
311 | parserOptions: { ecmaVersion: 6 }, | |
312 | errors: [ | |
313 | { | |
314 | messageId: "unexpectedWhitespaceBefore", | |
315 | type: "ContinueStatement", | |
316 | line: 1, | |
317 | column: 21, | |
318 | endLine: 1, | |
319 | endColumn: 24 | |
320 | } | |
321 | ] | |
322 | }, | |
323 | { | |
324 | code: "if(foo) {throw new Error() ; }", | |
325 | output: "if(foo) {throw new Error(); }", | |
326 | options: [{ before: false, after: false }], | |
327 | parserOptions: { ecmaVersion: 6 }, | |
328 | errors: [ | |
329 | { | |
330 | messageId: "unexpectedWhitespaceBefore", | |
331 | type: "ThrowStatement", | |
332 | line: 1, | |
333 | column: 27, | |
334 | endLine: 1, | |
335 | endColumn: 30 | |
336 | } | |
337 | ] | |
338 | }, | |
339 | { | |
340 | code: "for(a ; ; );", | |
341 | output: "for(a;; );", | |
342 | options: [{ before: false, after: false }], | |
343 | parserOptions: { ecmaVersion: 6 }, | |
344 | errors: [{ | |
345 | type: "ForStatement", | |
346 | messageId: "unexpectedWhitespaceBefore", | |
347 | line: 1, | |
348 | column: 6, | |
349 | endLine: 1, | |
350 | endColumn: 7 | |
351 | }, | |
352 | { | |
353 | type: "ForStatement", | |
354 | messageId: "unexpectedWhitespaceAfter", | |
355 | line: 1, | |
356 | column: 8, | |
357 | endLine: 1, | |
358 | endColumn: 9 | |
359 | }] | |
360 | }, | |
361 | { | |
362 | code: "for(a ; \n ; );", | |
363 | output: "for(a; \n ; );", | |
364 | options: [{ before: false, after: false }], | |
365 | parserOptions: { ecmaVersion: 6 }, | |
366 | errors: [{ | |
367 | type: "ForStatement", | |
368 | messageId: "unexpectedWhitespaceBefore", | |
369 | line: 1, | |
370 | column: 6, | |
371 | endLine: 1, | |
372 | endColumn: 7 | |
373 | } | |
eb39fafa | 374 | ] |
6f036462 TL |
375 | }, |
376 | { | |
377 | code: "do {} while (true) ;", | |
378 | output: "do {} while (true);", | |
379 | errors: [{ | |
380 | messageId: "unexpectedWhitespaceBefore", | |
381 | type: "DoWhileStatement", | |
382 | line: 1, | |
383 | column: 19, | |
384 | endLine: 1, | |
385 | endColumn: 20 | |
386 | }] | |
387 | }, | |
388 | { | |
389 | code: "do {} while (true);foo", | |
390 | output: "do {} while (true); foo", | |
391 | errors: [{ | |
392 | messageId: "missingWhitespaceAfter", | |
393 | type: "DoWhileStatement", | |
394 | line: 1, | |
395 | column: 19, | |
396 | endLine: 1, | |
397 | endColumn: 20 | |
398 | }] | |
399 | }, | |
400 | { | |
401 | code: "do {} while (true); foo", | |
402 | output: "do {} while (true) ;foo", | |
403 | options: [{ before: true, after: false }], | |
404 | errors: [{ | |
405 | messageId: "missingWhitespaceBefore", | |
406 | type: "DoWhileStatement", | |
407 | line: 1, | |
408 | column: 19, | |
409 | endLine: 1, | |
410 | endColumn: 20 | |
411 | }, | |
412 | { | |
413 | messageId: "unexpectedWhitespaceAfter", | |
414 | type: "DoWhileStatement", | |
415 | line: 1, | |
416 | column: 20, | |
417 | endLine: 1, | |
418 | endColumn: 22 | |
419 | }] | |
eb39fafa DC |
420 | } |
421 | ] | |
422 | }); |