]>
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 const ruleTester
= new RuleTester();
17 ruleTester
.run("quotes", rule
, {
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 } } },
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 } },
48 code
: "var foo = `back\\\\\ntick`;", // 2 backslashes followed by a newline
50 parserOptions
: { ecmaVersion
: 6 }
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 } },
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 } },
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 } },
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" } },
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 } }
81 code
: "var foo = 'bar';",
82 output
: "var foo = \"bar\";",
84 messageId
: "wrongQuotes",
85 data
: { description
: "doublequote" },
90 code
: "var foo = \"bar\";",
91 output
: "var foo = 'bar';",
94 messageId
: "wrongQuotes",
95 data
: { description
: "singlequote" },
100 code
: "var foo = `bar`;",
101 output
: "var foo = 'bar';",
107 messageId
: "wrongQuotes",
108 data
: { description
: "singlequote" },
109 type
: "TemplateLiteral"
113 code
: "var foo = 'don\\'t';",
114 output
: "var foo = \"don't\";",
116 messageId
: "wrongQuotes",
117 data
: { description
: "doublequote" },
122 code
: "var msg = \"Plugin '\" + name + \"' not found\"",
123 output
: "var msg = 'Plugin \\'' + name + '\\' not found'",
127 messageId
: "wrongQuotes",
128 data
: { description
: "singlequote" },
133 messageId
: "wrongQuotes",
134 data
: { description
: "singlequote" },
141 code
: "var foo = 'bar';",
142 output
: "var foo = \"bar\";",
145 messageId
: "wrongQuotes",
146 data
: { description
: "doublequote" },
151 code
: "var foo = `bar`;",
152 output
: "var foo = \"bar\";",
158 messageId
: "wrongQuotes",
159 data
: { description
: "doublequote" },
160 type
: "TemplateLiteral"
164 code
: "var foo = \"bar\";",
165 output
: "var foo = 'bar';",
166 options
: ["single", { avoidEscape
: true }],
168 messageId
: "wrongQuotes",
169 data
: { description
: "singlequote" },
174 code
: "var foo = 'bar';",
175 output
: "var foo = \"bar\";",
176 options
: ["double", { avoidEscape
: true }],
178 messageId
: "wrongQuotes",
179 data
: { description
: "doublequote" },
184 code
: "var foo = '\\\\';",
185 output
: "var foo = \"\\\\\";",
186 options
: ["double", { avoidEscape
: true }],
188 messageId
: "wrongQuotes",
189 data
: { description
: "doublequote" },
194 code
: "var foo = \"bar\";",
195 output
: "var foo = 'bar';",
196 options
: ["single", { allowTemplateLiterals
: true }],
198 messageId
: "wrongQuotes",
199 data
: { description
: "singlequote" },
204 code
: "var foo = 'bar';",
205 output
: "var foo = \"bar\";",
206 options
: ["double", { allowTemplateLiterals
: true }],
208 messageId
: "wrongQuotes",
209 data
: { description
: "doublequote" },
214 code
: "var foo = 'bar';",
215 output
: "var foo = `bar`;",
216 options
: ["backtick"],
217 parserOptions
: { ecmaVersion
: 2015 },
219 messageId
: "wrongQuotes",
220 data
: { description
: "backtick" },
225 code
: "var foo = 'b${x}a$r';",
226 output
: "var foo = `b\\${x}a$r`;",
227 options
: ["backtick"],
228 parserOptions
: { ecmaVersion
: 2015 },
230 messageId
: "wrongQuotes",
231 data
: { description
: "backtick" },
236 code
: "var foo = \"bar\";",
237 output
: "var foo = `bar`;",
238 options
: ["backtick"],
239 parserOptions
: { ecmaVersion
: 2015 },
241 messageId
: "wrongQuotes",
242 data
: { description
: "backtick" },
247 code
: "var foo = \"bar\";",
248 output
: "var foo = `bar`;",
249 options
: ["backtick", { avoidEscape
: true }],
250 parserOptions
: { ecmaVersion
: 2015 },
252 messageId
: "wrongQuotes",
253 data
: { description
: "backtick" },
258 code
: "var foo = 'bar';",
259 output
: "var foo = `bar`;",
260 options
: ["backtick", { avoidEscape
: true }],
261 parserOptions
: { ecmaVersion
: 2015 },
263 messageId
: "wrongQuotes",
264 data
: { description
: "backtick" },
269 // "use strict" is *not* a directive prologue in these statements so is subject to the rule
271 code
: "var foo = `backtick`; \"use strict\";",
272 output
: "var foo = `backtick`; `use strict`;",
273 options
: ["backtick"],
274 parserOptions
: { ecmaVersion
: 6 },
276 messageId
: "wrongQuotes",
277 data
: { description
: "backtick" },
282 code
: "{ \"use strict\"; var foo = `backtick`; }",
283 output
: "{ `use strict`; var foo = `backtick`; }",
284 options
: ["backtick"],
285 parserOptions
: { ecmaVersion
: 6 },
287 messageId
: "wrongQuotes",
288 data
: { description
: "backtick" },
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 },
298 messageId
: "wrongQuotes",
299 data
: { description
: "backtick" },
304 // `backtick` should warn computed property names.
306 code
: "var obj = {[\"key0\"]: 0, ['key1']: 1};",
307 output
: "var obj = {[`key0`]: 0, [`key1`]: 1};",
308 options
: ["backtick"],
309 parserOptions
: { ecmaVersion
: 6 },
312 messageId
: "wrongQuotes",
313 data
: { description
: "backtick" },
317 messageId
: "wrongQuotes",
318 data
: { description
: "backtick" },
324 code
: "class Foo { ['a'](){} static ['b'](){} }",
325 output
: "class Foo { [`a`](){} static [`b`](){} }",
326 options
: ["backtick"],
327 parserOptions
: { ecmaVersion
: 6 },
330 messageId
: "wrongQuotes",
331 data
: { description
: "backtick" },
335 messageId
: "wrongQuotes",
336 data
: { description
: "backtick" },
342 // https://github.com/eslint/eslint/issues/7084
344 code
: "<div blah={\"blah\"} />",
345 output
: "<div blah={'blah'} />",
347 parserOptions
: { ecmaFeatures
: { jsx
: true } },
350 messageId
: "wrongQuotes",
351 data
: { description
: "singlequote" },
357 code
: "<div blah={'blah'} />",
358 output
: "<div blah={\"blah\"} />",
360 parserOptions
: { ecmaFeatures
: { jsx
: true } },
363 messageId
: "wrongQuotes",
364 data
: { description
: "doublequote" },
370 code
: "<div blah={'blah'} />",
371 output
: "<div blah={`blah`} />",
372 options
: ["backtick"],
373 parserOptions
: { ecmaFeatures
: { jsx
: true }, ecmaVersion
: 2015 },
376 messageId
: "wrongQuotes",
377 data
: { description
: "backtick" },
383 // https://github.com/eslint/eslint/issues/7610
385 code
: "`use strict`;",
387 parserOptions
: { ecmaVersion
: 6 },
389 messageId
: "wrongQuotes",
390 data
: { description
: "doublequote" },
391 type
: "TemplateLiteral"
395 code
: "function foo() { `use strict`; foo(); }",
397 parserOptions
: { ecmaVersion
: 6 },
399 messageId
: "wrongQuotes",
400 data
: { description
: "doublequote" },
401 type
: "TemplateLiteral"
405 code
: "foo = function() { `use strict`; foo(); }",
407 parserOptions
: { ecmaVersion
: 6 },
409 messageId
: "wrongQuotes",
410 data
: { description
: "doublequote" },
411 type
: "TemplateLiteral"
415 code
: "() => { `use strict`; foo(); }",
417 parserOptions
: { ecmaVersion
: 6 },
419 messageId
: "wrongQuotes",
420 data
: { description
: "doublequote" },
421 type
: "TemplateLiteral"
425 code
: "() => { foo(); `use strict`; }",
426 output
: "() => { foo(); \"use strict\"; }",
427 parserOptions
: { ecmaVersion
: 6 },
429 messageId
: "wrongQuotes",
430 data
: { description
: "doublequote" },
431 type
: "TemplateLiteral"
435 code
: "foo(); `use strict`;",
436 output
: "foo(); \"use strict\";",
437 parserOptions
: { ecmaVersion
: 6 },
439 messageId
: "wrongQuotes",
440 data
: { description
: "doublequote" },
441 type
: "TemplateLiteral"
445 // https://github.com/eslint/eslint/issues/7646
447 code
: "var foo = `foo\\nbar`;",
448 output
: "var foo = \"foo\\nbar\";",
449 parserOptions
: { ecmaVersion
: 6 },
451 messageId
: "wrongQuotes",
452 data
: { description
: "doublequote" },
453 type
: "TemplateLiteral"
457 code
: "var foo = `foo\\\nbar`;", // 1 backslash followed by a newline
458 output
: "var foo = \"foo\\\nbar\";",
459 parserOptions
: { ecmaVersion
: 6 },
461 messageId
: "wrongQuotes",
462 data
: { description
: "doublequote" },
463 type
: "TemplateLiteral"
467 code
: "var foo = `foo\\\\\\\nbar`;", // 3 backslashes followed by a newline
468 output
: "var foo = \"foo\\\\\\\nbar\";",
469 parserOptions
: { ecmaVersion
: 6 },
471 messageId
: "wrongQuotes",
472 data
: { description
: "doublequote" },
473 type
: "TemplateLiteral"
479 parserOptions
: { ecmaVersion
: 6 },
481 messageId
: "wrongQuotes",
482 data
: { description
: "doublequote" },
483 type
: "TemplateLiteral",
489 // Strings containing octal escape sequences. Don't autofix to backticks.
491 code
: "var foo = \"\\1\"",
492 output
: "var foo = '\\1'",
496 messageId
: "wrongQuotes",
497 data
: { description
: "singlequote" },
503 code
: "var foo = '\\1'",
504 output
: "var foo = \"\\1\"",
508 messageId
: "wrongQuotes",
509 data
: { description
: "doublequote" },
515 code
: "var notoctal = '\\0'",
516 output
: "var notoctal = `\\0`",
517 options
: ["backtick"],
518 parserOptions
: { ecmaVersion
: 6 },
521 messageId
: "wrongQuotes",
522 data
: { description
: "backtick" },
528 code
: "var foo = '\\1'",
530 options
: ["backtick"],
531 parserOptions
: { ecmaVersion
: 6 },
534 messageId
: "wrongQuotes",
535 data
: { description
: "backtick" },
541 code
: "var foo = \"\\1\"",
543 options
: ["backtick"],
544 parserOptions
: { ecmaVersion
: 6 },
547 messageId
: "wrongQuotes",
548 data
: { description
: "backtick" },
554 code
: "var foo = '\\01'",
556 options
: ["backtick"],
557 parserOptions
: { ecmaVersion
: 6 },
560 messageId
: "wrongQuotes",
561 data
: { description
: "backtick" },
567 code
: "var foo = '\\0\\1'",
569 options
: ["backtick"],
570 parserOptions
: { ecmaVersion
: 6 },
573 messageId
: "wrongQuotes",
574 data
: { description
: "backtick" },
580 code
: "var foo = '\\08'",
582 options
: ["backtick"],
583 parserOptions
: { ecmaVersion
: 6 },
586 messageId
: "wrongQuotes",
587 data
: { description
: "backtick" },
593 code
: "var foo = 'prefix \\33'",
595 options
: ["backtick"],
596 parserOptions
: { ecmaVersion
: 6 },
599 messageId
: "wrongQuotes",
600 data
: { description
: "backtick" },
606 code
: "var foo = 'prefix \\75 suffix'",
608 options
: ["backtick"],
609 parserOptions
: { ecmaVersion
: 6 },
612 messageId
: "wrongQuotes",
613 data
: { description
: "backtick" },