]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/quotes.js
first commit
[pve-eslint.git] / eslint / tests / lib / rules / quotes.js
1 /**
2 * @fileoverview Tests for quotes rule.
3 * @author Matt DuVall <http://www.mattduvall.com/>, Michael Paulukonis
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/quotes"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 const ruleTester = new RuleTester();
16
17 ruleTester.run("quotes", rule, {
18 valid: [
19 "var foo = \"bar\";",
20 { code: "var foo = 'bar';", options: ["single"] },
21 { code: "var foo = \"bar\";", options: ["double"] },
22 { code: "var foo = 1;", options: ["single"] },
23 { code: "var foo = 1;", options: ["double"] },
24 { code: "var foo = \"'\";", options: ["single", { avoidEscape: true }] },
25 { code: "var foo = '\"';", options: ["double", { avoidEscape: true }] },
26 { code: "var foo = <>Hello world</>;", options: ["single"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
27 { code: "var foo = <>Hello world</>;", options: ["double"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
28 { code: "var foo = <>Hello world</>;", options: ["double", { avoidEscape: true }], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
29 { code: "var foo = <>Hello world</>;", options: ["backtick"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
30 { code: "var foo = <div>Hello world</div>;", options: ["single"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
31 { code: "var foo = <div id=\"foo\"></div>;", options: ["single"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
32 { code: "var foo = <div>Hello world</div>;", options: ["double"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
33 { code: "var foo = <div>Hello world</div>;", options: ["double", { avoidEscape: true }], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
34 { code: "var foo = `bar`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
35 { code: "var foo = `bar 'baz'`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
36 { code: "var foo = `bar \"baz\"`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
37 { code: "var foo = 1;", options: ["backtick"] },
38 { code: "var foo = \"a string containing `backtick` quotes\";", options: ["backtick", { avoidEscape: true }] },
39 { code: "var foo = <div id=\"foo\"></div>;", options: ["backtick"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
40 { code: "var foo = <div>Hello world</div>;", options: ["backtick"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
41
42 // Backticks are only okay if they have substitutions, contain a line break, or are tagged
43 { code: "var foo = `back\ntick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
44 { code: "var foo = `back\rtick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
45 { code: "var foo = `back\u2028tick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
46 { code: "var foo = `back\u2029tick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
47 {
48 code: "var foo = `back\\\\\ntick`;", // 2 backslashes followed by a newline
49 options: ["single"],
50 parserOptions: { ecmaVersion: 6 }
51 },
52 { code: "var foo = `back\\\\\\\\\ntick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
53 { code: "var foo = `\n`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
54 { code: "var foo = `back${x}tick`;", options: ["double"], parserOptions: { ecmaVersion: 6 } },
55 { code: "var foo = tag`backtick`;", options: ["double"], parserOptions: { ecmaVersion: 6 } },
56
57 // Backticks are also okay if allowTemplateLiterals
58 { code: "var foo = `bar 'foo' baz` + 'bar';", options: ["single", { allowTemplateLiterals: true }], parserOptions: { ecmaVersion: 6 } },
59 { code: "var foo = `bar 'foo' baz` + \"bar\";", options: ["double", { allowTemplateLiterals: true }], parserOptions: { ecmaVersion: 6 } },
60 { code: "var foo = `bar 'foo' baz` + `bar`;", options: ["backtick", { allowTemplateLiterals: true }], parserOptions: { ecmaVersion: 6 } },
61
62 // `backtick` should not warn the directive prologues.
63 { code: "\"use strict\"; var foo = `backtick`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
64 { code: "\"use strict\"; 'use strong'; \"use asm\"; var foo = `backtick`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
65 { code: "function foo() { \"use strict\"; \"use strong\"; \"use asm\"; var foo = `backtick`; }", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
66 { code: "(function() { 'use strict'; 'use strong'; 'use asm'; var foo = `backtick`; })();", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
67 { code: "(() => { \"use strict\"; \"use strong\"; \"use asm\"; var foo = `backtick`; })();", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
68
69 // `backtick` should not warn import/export sources.
70 { code: "import \"a\"; import 'b';", options: ["backtick"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
71 { code: "import a from \"a\"; import b from 'b';", options: ["backtick"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
72 { code: "export * from \"a\"; export * from 'b';", options: ["backtick"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
73
74 // `backtick` should not warn property/method names (not computed).
75 { code: "var obj = {\"key0\": 0, 'key1': 1};", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
76 { code: "class Foo { 'bar'(){} }", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
77 { code: "class Foo { static ''(){} }", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }
78 ],
79 invalid: [
80 {
81 code: "var foo = 'bar';",
82 output: "var foo = \"bar\";",
83 errors: [{
84 messageId: "wrongQuotes",
85 data: { description: "doublequote" },
86 type: "Literal"
87 }]
88 },
89 {
90 code: "var foo = \"bar\";",
91 output: "var foo = 'bar';",
92 options: ["single"],
93 errors: [{
94 messageId: "wrongQuotes",
95 data: { description: "singlequote" },
96 type: "Literal"
97 }]
98 },
99 {
100 code: "var foo = `bar`;",
101 output: "var foo = 'bar';",
102 options: ["single"],
103 parserOptions: {
104 ecmaVersion: 6
105 },
106 errors: [{
107 messageId: "wrongQuotes",
108 data: { description: "singlequote" },
109 type: "TemplateLiteral"
110 }]
111 },
112 {
113 code: "var foo = 'don\\'t';",
114 output: "var foo = \"don't\";",
115 errors: [{
116 messageId: "wrongQuotes",
117 data: { description: "doublequote" },
118 type: "Literal"
119 }]
120 },
121 {
122 code: "var msg = \"Plugin '\" + name + \"' not found\"",
123 output: "var msg = 'Plugin \\'' + name + '\\' not found'",
124 options: ["single"],
125 errors: [
126 {
127 messageId: "wrongQuotes",
128 data: { description: "singlequote" },
129 type: "Literal",
130 column: 11
131 },
132 {
133 messageId: "wrongQuotes",
134 data: { description: "singlequote" },
135 type: "Literal",
136 column: 31
137 }
138 ]
139 },
140 {
141 code: "var foo = 'bar';",
142 output: "var foo = \"bar\";",
143 options: ["double"],
144 errors: [{
145 messageId: "wrongQuotes",
146 data: { description: "doublequote" },
147 type: "Literal"
148 }]
149 },
150 {
151 code: "var foo = `bar`;",
152 output: "var foo = \"bar\";",
153 options: ["double"],
154 parserOptions: {
155 ecmaVersion: 6
156 },
157 errors: [{
158 messageId: "wrongQuotes",
159 data: { description: "doublequote" },
160 type: "TemplateLiteral"
161 }]
162 },
163 {
164 code: "var foo = \"bar\";",
165 output: "var foo = 'bar';",
166 options: ["single", { avoidEscape: true }],
167 errors: [{
168 messageId: "wrongQuotes",
169 data: { description: "singlequote" },
170 type: "Literal"
171 }]
172 },
173 {
174 code: "var foo = 'bar';",
175 output: "var foo = \"bar\";",
176 options: ["double", { avoidEscape: true }],
177 errors: [{
178 messageId: "wrongQuotes",
179 data: { description: "doublequote" },
180 type: "Literal"
181 }]
182 },
183 {
184 code: "var foo = '\\\\';",
185 output: "var foo = \"\\\\\";",
186 options: ["double", { avoidEscape: true }],
187 errors: [{
188 messageId: "wrongQuotes",
189 data: { description: "doublequote" },
190 type: "Literal"
191 }]
192 },
193 {
194 code: "var foo = \"bar\";",
195 output: "var foo = 'bar';",
196 options: ["single", { allowTemplateLiterals: true }],
197 errors: [{
198 messageId: "wrongQuotes",
199 data: { description: "singlequote" },
200 type: "Literal"
201 }]
202 },
203 {
204 code: "var foo = 'bar';",
205 output: "var foo = \"bar\";",
206 options: ["double", { allowTemplateLiterals: true }],
207 errors: [{
208 messageId: "wrongQuotes",
209 data: { description: "doublequote" },
210 type: "Literal"
211 }]
212 },
213 {
214 code: "var foo = 'bar';",
215 output: "var foo = `bar`;",
216 options: ["backtick"],
217 parserOptions: { ecmaVersion: 2015 },
218 errors: [{
219 messageId: "wrongQuotes",
220 data: { description: "backtick" },
221 type: "Literal"
222 }]
223 },
224 {
225 code: "var foo = 'b${x}a$r';",
226 output: "var foo = `b\\${x}a$r`;",
227 options: ["backtick"],
228 parserOptions: { ecmaVersion: 2015 },
229 errors: [{
230 messageId: "wrongQuotes",
231 data: { description: "backtick" },
232 type: "Literal"
233 }]
234 },
235 {
236 code: "var foo = \"bar\";",
237 output: "var foo = `bar`;",
238 options: ["backtick"],
239 parserOptions: { ecmaVersion: 2015 },
240 errors: [{
241 messageId: "wrongQuotes",
242 data: { description: "backtick" },
243 type: "Literal"
244 }]
245 },
246 {
247 code: "var foo = \"bar\";",
248 output: "var foo = `bar`;",
249 options: ["backtick", { avoidEscape: true }],
250 parserOptions: { ecmaVersion: 2015 },
251 errors: [{
252 messageId: "wrongQuotes",
253 data: { description: "backtick" },
254 type: "Literal"
255 }]
256 },
257 {
258 code: "var foo = 'bar';",
259 output: "var foo = `bar`;",
260 options: ["backtick", { avoidEscape: true }],
261 parserOptions: { ecmaVersion: 2015 },
262 errors: [{
263 messageId: "wrongQuotes",
264 data: { description: "backtick" },
265 type: "Literal"
266 }]
267 },
268
269 // "use strict" is *not* a directive prologue in these statements so is subject to the rule
270 {
271 code: "var foo = `backtick`; \"use strict\";",
272 output: "var foo = `backtick`; `use strict`;",
273 options: ["backtick"],
274 parserOptions: { ecmaVersion: 6 },
275 errors: [{
276 messageId: "wrongQuotes",
277 data: { description: "backtick" },
278 type: "Literal"
279 }]
280 },
281 {
282 code: "{ \"use strict\"; var foo = `backtick`; }",
283 output: "{ `use strict`; var foo = `backtick`; }",
284 options: ["backtick"],
285 parserOptions: { ecmaVersion: 6 },
286 errors: [{
287 messageId: "wrongQuotes",
288 data: { description: "backtick" },
289 type: "Literal"
290 }]
291 },
292 {
293 code: "if (1) { \"use strict\"; var foo = `backtick`; }",
294 output: "if (1) { `use strict`; var foo = `backtick`; }",
295 options: ["backtick"],
296 parserOptions: { ecmaVersion: 6 },
297 errors: [{
298 messageId: "wrongQuotes",
299 data: { description: "backtick" },
300 type: "Literal"
301 }]
302 },
303
304 // `backtick` should warn computed property names.
305 {
306 code: "var obj = {[\"key0\"]: 0, ['key1']: 1};",
307 output: "var obj = {[`key0`]: 0, [`key1`]: 1};",
308 options: ["backtick"],
309 parserOptions: { ecmaVersion: 6 },
310 errors: [
311 {
312 messageId: "wrongQuotes",
313 data: { description: "backtick" },
314 type: "Literal"
315 },
316 {
317 messageId: "wrongQuotes",
318 data: { description: "backtick" },
319 type: "Literal"
320 }
321 ]
322 },
323 {
324 code: "class Foo { ['a'](){} static ['b'](){} }",
325 output: "class Foo { [`a`](){} static [`b`](){} }",
326 options: ["backtick"],
327 parserOptions: { ecmaVersion: 6 },
328 errors: [
329 {
330 messageId: "wrongQuotes",
331 data: { description: "backtick" },
332 type: "Literal"
333 },
334 {
335 messageId: "wrongQuotes",
336 data: { description: "backtick" },
337 type: "Literal"
338 }
339 ]
340 },
341
342 // https://github.com/eslint/eslint/issues/7084
343 {
344 code: "<div blah={\"blah\"} />",
345 output: "<div blah={'blah'} />",
346 options: ["single"],
347 parserOptions: { ecmaFeatures: { jsx: true } },
348 errors: [
349 {
350 messageId: "wrongQuotes",
351 data: { description: "singlequote" },
352 type: "Literal"
353 }
354 ]
355 },
356 {
357 code: "<div blah={'blah'} />",
358 output: "<div blah={\"blah\"} />",
359 options: ["double"],
360 parserOptions: { ecmaFeatures: { jsx: true } },
361 errors: [
362 {
363 messageId: "wrongQuotes",
364 data: { description: "doublequote" },
365 type: "Literal"
366 }
367 ]
368 },
369 {
370 code: "<div blah={'blah'} />",
371 output: "<div blah={`blah`} />",
372 options: ["backtick"],
373 parserOptions: { ecmaFeatures: { jsx: true }, ecmaVersion: 2015 },
374 errors: [
375 {
376 messageId: "wrongQuotes",
377 data: { description: "backtick" },
378 type: "Literal"
379 }
380 ]
381 },
382
383 // https://github.com/eslint/eslint/issues/7610
384 {
385 code: "`use strict`;",
386 output: null,
387 parserOptions: { ecmaVersion: 6 },
388 errors: [{
389 messageId: "wrongQuotes",
390 data: { description: "doublequote" },
391 type: "TemplateLiteral"
392 }]
393 },
394 {
395 code: "function foo() { `use strict`; foo(); }",
396 output: null,
397 parserOptions: { ecmaVersion: 6 },
398 errors: [{
399 messageId: "wrongQuotes",
400 data: { description: "doublequote" },
401 type: "TemplateLiteral"
402 }]
403 },
404 {
405 code: "foo = function() { `use strict`; foo(); }",
406 output: null,
407 parserOptions: { ecmaVersion: 6 },
408 errors: [{
409 messageId: "wrongQuotes",
410 data: { description: "doublequote" },
411 type: "TemplateLiteral"
412 }]
413 },
414 {
415 code: "() => { `use strict`; foo(); }",
416 output: null,
417 parserOptions: { ecmaVersion: 6 },
418 errors: [{
419 messageId: "wrongQuotes",
420 data: { description: "doublequote" },
421 type: "TemplateLiteral"
422 }]
423 },
424 {
425 code: "() => { foo(); `use strict`; }",
426 output: "() => { foo(); \"use strict\"; }",
427 parserOptions: { ecmaVersion: 6 },
428 errors: [{
429 messageId: "wrongQuotes",
430 data: { description: "doublequote" },
431 type: "TemplateLiteral"
432 }]
433 },
434 {
435 code: "foo(); `use strict`;",
436 output: "foo(); \"use strict\";",
437 parserOptions: { ecmaVersion: 6 },
438 errors: [{
439 messageId: "wrongQuotes",
440 data: { description: "doublequote" },
441 type: "TemplateLiteral"
442 }]
443 },
444
445 // https://github.com/eslint/eslint/issues/7646
446 {
447 code: "var foo = `foo\\nbar`;",
448 output: "var foo = \"foo\\nbar\";",
449 parserOptions: { ecmaVersion: 6 },
450 errors: [{
451 messageId: "wrongQuotes",
452 data: { description: "doublequote" },
453 type: "TemplateLiteral"
454 }]
455 },
456 {
457 code: "var foo = `foo\\\nbar`;", // 1 backslash followed by a newline
458 output: "var foo = \"foo\\\nbar\";",
459 parserOptions: { ecmaVersion: 6 },
460 errors: [{
461 messageId: "wrongQuotes",
462 data: { description: "doublequote" },
463 type: "TemplateLiteral"
464 }]
465 },
466 {
467 code: "var foo = `foo\\\\\\\nbar`;", // 3 backslashes followed by a newline
468 output: "var foo = \"foo\\\\\\\nbar\";",
469 parserOptions: { ecmaVersion: 6 },
470 errors: [{
471 messageId: "wrongQuotes",
472 data: { description: "doublequote" },
473 type: "TemplateLiteral"
474 }]
475 },
476 {
477 code: "````",
478 output: "\"\"``",
479 parserOptions: { ecmaVersion: 6 },
480 errors: [{
481 messageId: "wrongQuotes",
482 data: { description: "doublequote" },
483 type: "TemplateLiteral",
484 line: 1,
485 column: 1
486 }]
487 },
488
489 // Strings containing octal escape sequences. Don't autofix to backticks.
490 {
491 code: "var foo = \"\\1\"",
492 output: "var foo = '\\1'",
493 options: ["single"],
494 errors: [
495 {
496 messageId: "wrongQuotes",
497 data: { description: "singlequote" },
498 type: "Literal"
499 }
500 ]
501 },
502 {
503 code: "var foo = '\\1'",
504 output: "var foo = \"\\1\"",
505 options: ["double"],
506 errors: [
507 {
508 messageId: "wrongQuotes",
509 data: { description: "doublequote" },
510 type: "Literal"
511 }
512 ]
513 },
514 {
515 code: "var notoctal = '\\0'",
516 output: "var notoctal = `\\0`",
517 options: ["backtick"],
518 parserOptions: { ecmaVersion: 6 },
519 errors: [
520 {
521 messageId: "wrongQuotes",
522 data: { description: "backtick" },
523 type: "Literal"
524 }
525 ]
526 },
527 {
528 code: "var foo = '\\1'",
529 output: null,
530 options: ["backtick"],
531 parserOptions: { ecmaVersion: 6 },
532 errors: [
533 {
534 messageId: "wrongQuotes",
535 data: { description: "backtick" },
536 type: "Literal"
537 }
538 ]
539 },
540 {
541 code: "var foo = \"\\1\"",
542 output: null,
543 options: ["backtick"],
544 parserOptions: { ecmaVersion: 6 },
545 errors: [
546 {
547 messageId: "wrongQuotes",
548 data: { description: "backtick" },
549 type: "Literal"
550 }
551 ]
552 },
553 {
554 code: "var foo = '\\01'",
555 output: null,
556 options: ["backtick"],
557 parserOptions: { ecmaVersion: 6 },
558 errors: [
559 {
560 messageId: "wrongQuotes",
561 data: { description: "backtick" },
562 type: "Literal"
563 }
564 ]
565 },
566 {
567 code: "var foo = '\\0\\1'",
568 output: null,
569 options: ["backtick"],
570 parserOptions: { ecmaVersion: 6 },
571 errors: [
572 {
573 messageId: "wrongQuotes",
574 data: { description: "backtick" },
575 type: "Literal"
576 }
577 ]
578 },
579 {
580 code: "var foo = '\\08'",
581 output: null,
582 options: ["backtick"],
583 parserOptions: { ecmaVersion: 6 },
584 errors: [
585 {
586 messageId: "wrongQuotes",
587 data: { description: "backtick" },
588 type: "Literal"
589 }
590 ]
591 },
592 {
593 code: "var foo = 'prefix \\33'",
594 output: null,
595 options: ["backtick"],
596 parserOptions: { ecmaVersion: 6 },
597 errors: [
598 {
599 messageId: "wrongQuotes",
600 data: { description: "backtick" },
601 type: "Literal"
602 }
603 ]
604 },
605 {
606 code: "var foo = 'prefix \\75 suffix'",
607 output: null,
608 options: ["backtick"],
609 parserOptions: { ecmaVersion: 6 },
610 errors: [
611 {
612 messageId: "wrongQuotes",
613 data: { description: "backtick" },
614 type: "Literal"
615 }
616 ]
617 }
618 ]
619 });