]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/quotes.js
2 * @fileoverview Tests for quotes rule.
3 * @author Matt DuVall <http://www.mattduvall.com/>, Michael Paulukonis
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require("../../../lib/rules/quotes"),
13 { RuleTester
} = require("../../../lib/rule-tester");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ruleTester
= new RuleTester();
21 ruleTester
.run("quotes", rule
, {
24 { code
: "var foo = 'bar';", options
: ["single"] },
25 { code
: "var foo = \"bar\";", options
: ["double"] },
26 { code
: "var foo = 1;", options
: ["single"] },
27 { code
: "var foo = 1;", options
: ["double"] },
28 { code
: "var foo = \"'\";", options
: ["single", { avoidEscape
: true }] },
29 { code
: "var foo = '\"';", options
: ["double", { avoidEscape
: true }] },
30 { code
: "var foo = <>Hello world</>;", options
: ["single"], parserOptions
: { ecmaVersion
: 6, ecmaFeatures
: { jsx
: true } } },
31 { code
: "var foo = <>Hello world</>;", options
: ["double"], parserOptions
: { ecmaVersion
: 6, ecmaFeatures
: { jsx
: true } } },
32 { code
: "var foo = <>Hello world</>;", options
: ["double", { avoidEscape
: true }], parserOptions
: { ecmaVersion
: 6, ecmaFeatures
: { jsx
: true } } },
33 { code
: "var foo = <>Hello world</>;", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6, ecmaFeatures
: { jsx
: true } } },
34 { code
: "var foo = <div>Hello world</div>;", options
: ["single"], parserOptions
: { ecmaVersion
: 6, ecmaFeatures
: { jsx
: true } } },
35 { code
: "var foo = <div id=\"foo\"></div>;", options
: ["single"], parserOptions
: { ecmaVersion
: 6, ecmaFeatures
: { jsx
: true } } },
36 { code
: "var foo = <div>Hello world</div>;", options
: ["double"], parserOptions
: { ecmaVersion
: 6, ecmaFeatures
: { jsx
: true } } },
37 { code
: "var foo = <div>Hello world</div>;", options
: ["double", { avoidEscape
: true }], parserOptions
: { ecmaVersion
: 6, ecmaFeatures
: { jsx
: true } } },
38 { code
: "var foo = `bar`;", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6 } },
39 { code
: "var foo = `bar 'baz'`;", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6 } },
40 { code
: "var foo = `bar \"baz\"`;", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6 } },
41 { code
: "var foo = 1;", options
: ["backtick"] },
42 { code
: "var foo = \"a string containing `backtick` quotes\";", options
: ["backtick", { avoidEscape
: true }] },
43 { code
: "var foo = <div id=\"foo\"></div>;", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6, ecmaFeatures
: { jsx
: true } } },
44 { code
: "var foo = <div>Hello world</div>;", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6, ecmaFeatures
: { jsx
: true } } },
45 { code
: "class C { \"f\"; \"m\"() {} }", options
: ["double"], parserOptions
: { ecmaVersion
: 2022 } },
46 { code
: "class C { 'f'; 'm'() {} }", options
: ["single"], parserOptions
: { ecmaVersion
: 2022 } },
48 // Backticks are only okay if they have substitutions, contain a line break, or are tagged
49 { code
: "var foo = `back\ntick`;", options
: ["single"], parserOptions
: { ecmaVersion
: 6 } },
50 { code
: "var foo = `back\rtick`;", options
: ["single"], parserOptions
: { ecmaVersion
: 6 } },
51 { code
: "var foo = `back\u2028tick`;", options
: ["single"], parserOptions
: { ecmaVersion
: 6 } },
52 { code
: "var foo = `back\u2029tick`;", options
: ["single"], parserOptions
: { ecmaVersion
: 6 } },
54 code
: "var foo = `back\\\\\ntick`;", // 2 backslashes followed by a newline
56 parserOptions
: { ecmaVersion
: 6 }
58 { code
: "var foo = `back\\\\\\\\\ntick`;", options
: ["single"], parserOptions
: { ecmaVersion
: 6 } },
59 { code
: "var foo = `\n`;", options
: ["single"], parserOptions
: { ecmaVersion
: 6 } },
60 { code
: "var foo = `back${x}tick`;", options
: ["double"], parserOptions
: { ecmaVersion
: 6 } },
61 { code
: "var foo = tag`backtick`;", options
: ["double"], parserOptions
: { ecmaVersion
: 6 } },
63 // Backticks are also okay if allowTemplateLiterals
64 { code
: "var foo = `bar 'foo' baz` + 'bar';", options
: ["single", { allowTemplateLiterals
: true }], parserOptions
: { ecmaVersion
: 6 } },
65 { code
: "var foo = `bar 'foo' baz` + \"bar\";", options
: ["double", { allowTemplateLiterals
: true }], parserOptions
: { ecmaVersion
: 6 } },
66 { code
: "var foo = `bar 'foo' baz` + `bar`;", options
: ["backtick", { allowTemplateLiterals
: true }], parserOptions
: { ecmaVersion
: 6 } },
68 // `backtick` should not warn the directive prologues.
69 { code
: "\"use strict\"; var foo = `backtick`;", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6 } },
70 { code
: "\"use strict\"; 'use strong'; \"use asm\"; var foo = `backtick`;", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6 } },
71 { code
: "function foo() { \"use strict\"; \"use strong\"; \"use asm\"; var foo = `backtick`; }", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6 } },
72 { code
: "(function() { 'use strict'; 'use strong'; 'use asm'; var foo = `backtick`; })();", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6 } },
73 { code
: "(() => { \"use strict\"; \"use strong\"; \"use asm\"; var foo = `backtick`; })();", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6 } },
75 // `backtick` should not warn import/export sources.
76 { code
: "import \"a\"; import 'b';", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
77 { code
: "import a from \"a\"; import b from 'b';", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
78 { code
: "export * from \"a\"; export * from 'b';", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
80 // `backtick` should not warn module export names.
81 { code
: "import { \"a\" as b, 'c' as d } from 'mod';", options
: ["backtick"], parserOptions
: { ecmaVersion
: 2022, sourceType
: "module" } },
82 { code
: "let a, c; export { a as \"b\", c as 'd' };", options
: ["backtick"], parserOptions
: { ecmaVersion
: 2022, sourceType
: "module" } },
83 { code
: "export { \"a\", 'b' } from 'mod';", options
: ["backtick"], parserOptions
: { ecmaVersion
: 2022, sourceType
: "module" } },
84 { code
: "export { a as \"b\", c as 'd' } from 'mod';", options
: ["backtick"], parserOptions
: { ecmaVersion
: 2022, sourceType
: "module" } },
85 { code
: "export { \"a\" as b, 'c' as d } from 'mod';", options
: ["backtick"], parserOptions
: { ecmaVersion
: 2022, sourceType
: "module" } },
86 { code
: "export { \"a\" as \"b\", 'c' as 'd' } from 'mod';", options
: ["backtick"], parserOptions
: { ecmaVersion
: 2022, sourceType
: "module" } },
87 { code
: "export * as \"a\" from 'mod';", options
: ["backtick"], parserOptions
: { ecmaVersion
: 2022, sourceType
: "module" } },
88 { code
: "export * as 'a' from 'mod';", options
: ["backtick"], parserOptions
: { ecmaVersion
: 2022, sourceType
: "module" } },
90 // `backtick` should not warn property/method names (not computed).
91 { code
: "var obj = {\"key0\": 0, 'key1': 1};", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6 } },
92 { code
: "class Foo { 'bar'(){} }", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6 } },
93 { code
: "class Foo { static ''(){} }", options
: ["backtick"], parserOptions
: { ecmaVersion
: 6 } },
94 { code
: "class C { \"double\"; 'single'; }", options
: ["backtick"], parserOptions
: { ecmaVersion
: 2022 } }
98 code
: "var foo = 'bar';",
99 output
: "var foo = \"bar\";",
101 messageId
: "wrongQuotes",
102 data
: { description
: "doublequote" },
107 code
: "var foo = \"bar\";",
108 output
: "var foo = 'bar';",
111 messageId
: "wrongQuotes",
112 data
: { description
: "singlequote" },
117 code
: "var foo = `bar`;",
118 output
: "var foo = 'bar';",
124 messageId
: "wrongQuotes",
125 data
: { description
: "singlequote" },
126 type
: "TemplateLiteral"
130 code
: "var foo = 'don\\'t';",
131 output
: "var foo = \"don't\";",
133 messageId
: "wrongQuotes",
134 data
: { description
: "doublequote" },
139 code
: "var msg = \"Plugin '\" + name + \"' not found\"",
140 output
: "var msg = 'Plugin \\'' + name + '\\' not found'",
144 messageId
: "wrongQuotes",
145 data
: { description
: "singlequote" },
150 messageId
: "wrongQuotes",
151 data
: { description
: "singlequote" },
158 code
: "var foo = 'bar';",
159 output
: "var foo = \"bar\";",
162 messageId
: "wrongQuotes",
163 data
: { description
: "doublequote" },
168 code
: "var foo = `bar`;",
169 output
: "var foo = \"bar\";",
175 messageId
: "wrongQuotes",
176 data
: { description
: "doublequote" },
177 type
: "TemplateLiteral"
181 code
: "var foo = \"bar\";",
182 output
: "var foo = 'bar';",
183 options
: ["single", { avoidEscape
: true }],
185 messageId
: "wrongQuotes",
186 data
: { description
: "singlequote" },
191 code
: "var foo = 'bar';",
192 output
: "var foo = \"bar\";",
193 options
: ["double", { avoidEscape
: true }],
195 messageId
: "wrongQuotes",
196 data
: { description
: "doublequote" },
201 code
: "var foo = '\\\\';",
202 output
: "var foo = \"\\\\\";",
203 options
: ["double", { avoidEscape
: true }],
205 messageId
: "wrongQuotes",
206 data
: { description
: "doublequote" },
211 code
: "var foo = \"bar\";",
212 output
: "var foo = 'bar';",
213 options
: ["single", { allowTemplateLiterals
: true }],
215 messageId
: "wrongQuotes",
216 data
: { description
: "singlequote" },
221 code
: "var foo = 'bar';",
222 output
: "var foo = \"bar\";",
223 options
: ["double", { allowTemplateLiterals
: true }],
225 messageId
: "wrongQuotes",
226 data
: { description
: "doublequote" },
231 code
: "var foo = 'bar';",
232 output
: "var foo = `bar`;",
233 options
: ["backtick"],
234 parserOptions
: { ecmaVersion
: 2015 },
236 messageId
: "wrongQuotes",
237 data
: { description
: "backtick" },
242 code
: "var foo = 'b${x}a$r';",
243 output
: "var foo = `b\\${x}a$r`;",
244 options
: ["backtick"],
245 parserOptions
: { ecmaVersion
: 2015 },
247 messageId
: "wrongQuotes",
248 data
: { description
: "backtick" },
253 code
: "var foo = \"bar\";",
254 output
: "var foo = `bar`;",
255 options
: ["backtick"],
256 parserOptions
: { ecmaVersion
: 2015 },
258 messageId
: "wrongQuotes",
259 data
: { description
: "backtick" },
264 code
: "var foo = \"bar\";",
265 output
: "var foo = `bar`;",
266 options
: ["backtick", { avoidEscape
: true }],
267 parserOptions
: { ecmaVersion
: 2015 },
269 messageId
: "wrongQuotes",
270 data
: { description
: "backtick" },
275 code
: "var foo = 'bar';",
276 output
: "var foo = `bar`;",
277 options
: ["backtick", { avoidEscape
: true }],
278 parserOptions
: { ecmaVersion
: 2015 },
280 messageId
: "wrongQuotes",
281 data
: { description
: "backtick" },
286 // "use strict" is *not* a directive prologue in these statements so is subject to the rule
288 code
: "var foo = `backtick`; \"use strict\";",
289 output
: "var foo = `backtick`; `use strict`;",
290 options
: ["backtick"],
291 parserOptions
: { ecmaVersion
: 6 },
293 messageId
: "wrongQuotes",
294 data
: { description
: "backtick" },
299 code
: "{ \"use strict\"; var foo = `backtick`; }",
300 output
: "{ `use strict`; var foo = `backtick`; }",
301 options
: ["backtick"],
302 parserOptions
: { ecmaVersion
: 6 },
304 messageId
: "wrongQuotes",
305 data
: { description
: "backtick" },
310 code
: "if (1) { \"use strict\"; var foo = `backtick`; }",
311 output
: "if (1) { `use strict`; var foo = `backtick`; }",
312 options
: ["backtick"],
313 parserOptions
: { ecmaVersion
: 6 },
315 messageId
: "wrongQuotes",
316 data
: { description
: "backtick" },
321 // `backtick` should warn computed property names.
323 code
: "var obj = {[\"key0\"]: 0, ['key1']: 1};",
324 output
: "var obj = {[`key0`]: 0, [`key1`]: 1};",
325 options
: ["backtick"],
326 parserOptions
: { ecmaVersion
: 6 },
329 messageId
: "wrongQuotes",
330 data
: { description
: "backtick" },
334 messageId
: "wrongQuotes",
335 data
: { description
: "backtick" },
341 code
: "class Foo { ['a'](){} static ['b'](){} }",
342 output
: "class Foo { [`a`](){} static [`b`](){} }",
343 options
: ["backtick"],
344 parserOptions
: { ecmaVersion
: 6 },
347 messageId
: "wrongQuotes",
348 data
: { description
: "backtick" },
352 messageId
: "wrongQuotes",
353 data
: { description
: "backtick" },
359 // https://github.com/eslint/eslint/issues/7084
361 code
: "<div blah={\"blah\"} />",
362 output
: "<div blah={'blah'} />",
364 parserOptions
: { ecmaFeatures
: { jsx
: true } },
367 messageId
: "wrongQuotes",
368 data
: { description
: "singlequote" },
374 code
: "<div blah={'blah'} />",
375 output
: "<div blah={\"blah\"} />",
377 parserOptions
: { ecmaFeatures
: { jsx
: true } },
380 messageId
: "wrongQuotes",
381 data
: { description
: "doublequote" },
387 code
: "<div blah={'blah'} />",
388 output
: "<div blah={`blah`} />",
389 options
: ["backtick"],
390 parserOptions
: { ecmaFeatures
: { jsx
: true }, ecmaVersion
: 2015 },
393 messageId
: "wrongQuotes",
394 data
: { description
: "backtick" },
400 // https://github.com/eslint/eslint/issues/7610
402 code
: "`use strict`;",
404 parserOptions
: { ecmaVersion
: 6 },
406 messageId
: "wrongQuotes",
407 data
: { description
: "doublequote" },
408 type
: "TemplateLiteral"
412 code
: "function foo() { `use strict`; foo(); }",
414 parserOptions
: { ecmaVersion
: 6 },
416 messageId
: "wrongQuotes",
417 data
: { description
: "doublequote" },
418 type
: "TemplateLiteral"
422 code
: "foo = function() { `use strict`; foo(); }",
424 parserOptions
: { ecmaVersion
: 6 },
426 messageId
: "wrongQuotes",
427 data
: { description
: "doublequote" },
428 type
: "TemplateLiteral"
432 code
: "() => { `use strict`; foo(); }",
434 parserOptions
: { ecmaVersion
: 6 },
436 messageId
: "wrongQuotes",
437 data
: { description
: "doublequote" },
438 type
: "TemplateLiteral"
442 code
: "() => { foo(); `use strict`; }",
443 output
: "() => { foo(); \"use strict\"; }",
444 parserOptions
: { ecmaVersion
: 6 },
446 messageId
: "wrongQuotes",
447 data
: { description
: "doublequote" },
448 type
: "TemplateLiteral"
452 code
: "foo(); `use strict`;",
453 output
: "foo(); \"use strict\";",
454 parserOptions
: { ecmaVersion
: 6 },
456 messageId
: "wrongQuotes",
457 data
: { description
: "doublequote" },
458 type
: "TemplateLiteral"
462 // https://github.com/eslint/eslint/issues/7646
464 code
: "var foo = `foo\\nbar`;",
465 output
: "var foo = \"foo\\nbar\";",
466 parserOptions
: { ecmaVersion
: 6 },
468 messageId
: "wrongQuotes",
469 data
: { description
: "doublequote" },
470 type
: "TemplateLiteral"
474 code
: "var foo = `foo\\\nbar`;", // 1 backslash followed by a newline
475 output
: "var foo = \"foo\\\nbar\";",
476 parserOptions
: { ecmaVersion
: 6 },
478 messageId
: "wrongQuotes",
479 data
: { description
: "doublequote" },
480 type
: "TemplateLiteral"
484 code
: "var foo = `foo\\\\\\\nbar`;", // 3 backslashes followed by a newline
485 output
: "var foo = \"foo\\\\\\\nbar\";",
486 parserOptions
: { ecmaVersion
: 6 },
488 messageId
: "wrongQuotes",
489 data
: { description
: "doublequote" },
490 type
: "TemplateLiteral"
496 parserOptions
: { ecmaVersion
: 6 },
498 messageId
: "wrongQuotes",
499 data
: { description
: "doublequote" },
500 type
: "TemplateLiteral",
506 // Strings containing octal escape sequences. Don't autofix to backticks.
508 code
: "var foo = \"\\1\"",
509 output
: "var foo = '\\1'",
513 messageId
: "wrongQuotes",
514 data
: { description
: "singlequote" },
520 code
: "var foo = '\\1'",
521 output
: "var foo = \"\\1\"",
525 messageId
: "wrongQuotes",
526 data
: { description
: "doublequote" },
532 code
: "var notoctal = '\\0'",
533 output
: "var notoctal = `\\0`",
534 options
: ["backtick"],
535 parserOptions
: { ecmaVersion
: 6 },
538 messageId
: "wrongQuotes",
539 data
: { description
: "backtick" },
545 code
: "var foo = '\\1'",
547 options
: ["backtick"],
548 parserOptions
: { ecmaVersion
: 6 },
551 messageId
: "wrongQuotes",
552 data
: { description
: "backtick" },
558 code
: "var foo = \"\\1\"",
560 options
: ["backtick"],
561 parserOptions
: { ecmaVersion
: 6 },
564 messageId
: "wrongQuotes",
565 data
: { description
: "backtick" },
571 code
: "var foo = '\\01'",
573 options
: ["backtick"],
574 parserOptions
: { ecmaVersion
: 6 },
577 messageId
: "wrongQuotes",
578 data
: { description
: "backtick" },
584 code
: "var foo = '\\0\\1'",
586 options
: ["backtick"],
587 parserOptions
: { ecmaVersion
: 6 },
590 messageId
: "wrongQuotes",
591 data
: { description
: "backtick" },
597 code
: "var foo = '\\08'",
599 options
: ["backtick"],
600 parserOptions
: { ecmaVersion
: 6 },
603 messageId
: "wrongQuotes",
604 data
: { description
: "backtick" },
610 code
: "var foo = 'prefix \\33'",
612 options
: ["backtick"],
613 parserOptions
: { ecmaVersion
: 6 },
616 messageId
: "wrongQuotes",
617 data
: { description
: "backtick" },
623 code
: "var foo = 'prefix \\75 suffix'",
625 options
: ["backtick"],
626 parserOptions
: { ecmaVersion
: 6 },
629 messageId
: "wrongQuotes",
630 data
: { description
: "backtick" },
636 code
: "var nonOctalDecimalEscape = '\\8'",
638 options
: ["backtick"],
639 parserOptions
: { ecmaVersion
: 6 },
642 messageId
: "wrongQuotes",
643 data
: { description
: "backtick" },
652 code
: "class C { 'foo'; }",
653 output
: "class C { \"foo\"; }",
655 parserOptions
: { ecmaVersion
: 2022 },
658 messageId
: "wrongQuotes",
659 data
: { description
: "doublequote" },
665 code
: "class C { 'foo'() {} }",
666 output
: "class C { \"foo\"() {} }",
668 parserOptions
: { ecmaVersion
: 2022 },
671 messageId
: "wrongQuotes",
672 data
: { description
: "doublequote" },
678 code
: "class C { \"foo\"; }",
679 output
: "class C { 'foo'; }",
681 parserOptions
: { ecmaVersion
: 2022 },
684 messageId
: "wrongQuotes",
685 data
: { description
: "singlequote" },
691 code
: "class C { \"foo\"() {} }",
692 output
: "class C { 'foo'() {} }",
694 parserOptions
: { ecmaVersion
: 2022 },
697 messageId
: "wrongQuotes",
698 data
: { description
: "singlequote" },
704 code
: "class C { [\"foo\"]; }",
705 output
: "class C { [`foo`]; }",
706 options
: ["backtick"],
707 parserOptions
: { ecmaVersion
: 2022 },
710 messageId
: "wrongQuotes",
711 data
: { description
: "backtick" },
717 code
: "class C { foo = \"foo\"; }",
718 output
: "class C { foo = `foo`; }",
719 options
: ["backtick"],
720 parserOptions
: { ecmaVersion
: 2022 },
723 messageId
: "wrongQuotes",
724 data
: { description
: "backtick" },