UNPKG

15.3 kBJavaScriptView Raw
1var rule = require('../../rules/quotes'),
2 RuleTester = require('../RuleTester');
3
4var ruleTester = new RuleTester();
5ruleTester.run('babel/quotes', rule, {
6 valid: [
7 "var foo = \"bar\";",
8 { code: "var foo = 'bar';", options: ["single"] },
9 { code: "var foo = \"bar\";", options: ["double"] },
10 { code: "var foo = 1;", options: ["single"] },
11 { code: "var foo = 1;", options: ["double"] },
12 { code: "var foo = \"'\";", options: ["single", { avoidEscape: true }] },
13 { code: "var foo = '\"';", options: ["double", { avoidEscape: true }] },
14 { code: "var foo = <div>Hello world</div>;", options: ["single"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
15 { code: "var foo = <div id=\"foo\"></div>;", options: ["single"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
16 { code: "var foo = <div>Hello world</div>;", options: ["double"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
17 { code: "var foo = <div>Hello world</div>;", options: ["double", { avoidEscape: true }], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
18 { code: "var foo = `bar`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
19 { code: "var foo = `bar 'baz'`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
20 { code: "var foo = `bar \"baz\"`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
21 { code: "var foo = 1;", options: ["backtick"] },
22 { code: "var foo = \"a string containing `backtick` quotes\";", options: ["backtick", { avoidEscape: true }] },
23 { code: "var foo = <div id=\"foo\"></div>;", options: ["backtick"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
24 { code: "var foo = <div>Hello world</div>;", options: ["backtick"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
25
26 // Backticks are only okay if they have substitutions, contain a line break, or are tagged
27 { code: "var foo = `back\ntick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
28 { code: "var foo = `back\rtick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
29 { code: "var foo = `back\u2028tick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
30 { code: "var foo = `back\u2029tick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
31 {
32 code: "var foo = `back\\\\\ntick`;", // 2 backslashes followed by a newline
33 options: ["single"],
34 parserOptions: { ecmaVersion: 6 }
35 },
36 { code: "var foo = `back\\\\\\\\\ntick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
37 { code: "var foo = `\n`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
38 { code: "var foo = `back${x}tick`;", options: ["double"], parserOptions: { ecmaVersion: 6 } },
39 { code: "var foo = tag`backtick`;", options: ["double"], parserOptions: { ecmaVersion: 6 } },
40
41 // Backticks are also okay if allowTemplateLiterals
42 { code: "var foo = `bar 'foo' baz` + 'bar';", options: ["single", { allowTemplateLiterals: true }], parserOptions: { ecmaVersion: 6 } },
43 { code: "var foo = `bar 'foo' baz` + \"bar\";", options: ["double", { allowTemplateLiterals: true }], parserOptions: { ecmaVersion: 6 } },
44 { code: "var foo = `bar 'foo' baz` + `bar`;", options: ["backtick", { allowTemplateLiterals: true }], parserOptions: { ecmaVersion: 6 } },
45
46 // `backtick` should not warn the directive prologues.
47 { code: "\"use strict\"; var foo = `backtick`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
48 { code: "\"use strict\"; 'use strong'; \"use asm\"; var foo = `backtick`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
49 { code: "function foo() { \"use strict\"; \"use strong\"; \"use asm\"; var foo = `backtick`; }", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
50 { code: "(function() { 'use strict'; 'use strong'; 'use asm'; var foo = `backtick`; })();", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
51 { code: "(() => { \"use strict\"; \"use strong\"; \"use asm\"; var foo = `backtick`; })();", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
52
53 // `backtick` should not warn import/export sources.
54 { code: "import \"a\"; import 'b';", options: ["backtick"], parserOptions: { sourceType: "module" } },
55 { code: "import a from \"a\"; import b from 'b';", options: ["backtick"], parserOptions: { sourceType: "module" } },
56 { code: "export * from \"a\"; export * from 'b';", options: ["backtick"], parserOptions: { sourceType: "module" } },
57
58 // `backtick` should not warn property/method names (not computed).
59 { code: "var obj = {\"key0\": 0, 'key1': 1};", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
60 { code: "class Foo { 'bar'(){} }", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
61 { code: "class Foo { static ''(){} }", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
62
63 // Babel
64 '<>foo</>;',
65 { code: '<>foo</>;', options: ['single'] },
66 { code: '<>foo</>;', options: ['double'] },
67 '<><div /><div /></>;',
68 { code: '<><div /><div /></>;', options: ['single'] },
69 { code: '<><div /><div /></>;', options: ['double'] },
70 ],
71 invalid: [
72 {
73 code: "var foo = 'bar';",
74 output: "var foo = \"bar\";",
75 errors: [{ message: "Strings must use doublequote.", type: "Literal" }]
76 },
77 {
78 code: "var foo = \"bar\";",
79 output: "var foo = 'bar';",
80 options: ["single"],
81 errors: [{ message: "Strings must use singlequote.", type: "Literal" }]
82 },
83 {
84 code: "var foo = `bar`;",
85 output: "var foo = 'bar';",
86 options: ["single"],
87 parserOptions: {
88 ecmaVersion: 6
89 },
90 errors: [{ message: "Strings must use singlequote.", type: "TemplateLiteral" }]
91 },
92 {
93 code: "var foo = 'don\\'t';",
94 output: "var foo = \"don't\";",
95 errors: [{ message: "Strings must use doublequote.", type: "Literal" }]
96 },
97 {
98 code: "var msg = \"Plugin '\" + name + \"' not found\"",
99 output: "var msg = 'Plugin \\'' + name + '\\' not found'",
100 options: ["single"],
101 errors: [
102 { message: "Strings must use singlequote.", type: "Literal", column: 11 },
103 { message: "Strings must use singlequote.", type: "Literal", column: 31 }
104 ]
105 },
106 {
107 code: "var foo = 'bar';",
108 output: "var foo = \"bar\";",
109 options: ["double"],
110 errors: [{ message: "Strings must use doublequote.", type: "Literal" }]
111 },
112 {
113 code: "var foo = `bar`;",
114 output: "var foo = \"bar\";",
115 options: ["double"],
116 parserOptions: {
117 ecmaVersion: 6
118 },
119 errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
120 },
121 {
122 code: "var foo = \"bar\";",
123 output: "var foo = 'bar';",
124 options: ["single", { avoidEscape: true }],
125 errors: [{ message: "Strings must use singlequote.", type: "Literal" }]
126 },
127 {
128 code: "var foo = 'bar';",
129 output: "var foo = \"bar\";",
130 options: ["double", { avoidEscape: true }],
131 errors: [{ message: "Strings must use doublequote.", type: "Literal" }]
132 },
133 {
134 code: "var foo = '\\\\';",
135 output: "var foo = \"\\\\\";",
136 options: ["double", { avoidEscape: true }],
137 errors: [{ message: "Strings must use doublequote.", type: "Literal" }]
138 },
139 {
140 code: "var foo = \"bar\";",
141 output: "var foo = 'bar';",
142 options: ["single", { allowTemplateLiterals: true }],
143 errors: [{ message: "Strings must use singlequote.", type: "Literal" }]
144 },
145 {
146 code: "var foo = 'bar';",
147 output: "var foo = \"bar\";",
148 options: ["double", { allowTemplateLiterals: true }],
149 errors: [{ message: "Strings must use doublequote.", type: "Literal" }]
150 },
151 {
152 code: "var foo = 'bar';",
153 output: "var foo = `bar`;",
154 options: ["backtick"],
155 errors: [{ message: "Strings must use backtick.", type: "Literal" }]
156 },
157 {
158 code: "var foo = 'b${x}a$r';",
159 output: "var foo = `b\\${x}a$r`;",
160 options: ["backtick"],
161 errors: [{ message: "Strings must use backtick.", type: "Literal" }]
162 },
163 {
164 code: "var foo = \"bar\";",
165 output: "var foo = `bar`;",
166 options: ["backtick"],
167 errors: [{ message: "Strings must use backtick.", type: "Literal" }]
168 },
169 {
170 code: "var foo = \"bar\";",
171 output: "var foo = `bar`;",
172 options: ["backtick", { avoidEscape: true }],
173 errors: [{ message: "Strings must use backtick.", type: "Literal" }]
174 },
175 {
176 code: "var foo = 'bar';",
177 output: "var foo = `bar`;",
178 options: ["backtick", { avoidEscape: true }],
179 errors: [{ message: "Strings must use backtick.", type: "Literal" }]
180 },
181
182 // "use strict" is *not* a directive prologue in these statements so is subject to the rule
183 {
184 code: "var foo = `backtick`; \"use strict\";",
185 output: "var foo = `backtick`; `use strict`;",
186 options: ["backtick"],
187 parserOptions: { ecmaVersion: 6 },
188 errors: [{ message: "Strings must use backtick.", type: "Literal" }]
189 },
190 {
191 code: "{ \"use strict\"; var foo = `backtick`; }",
192 output: "{ `use strict`; var foo = `backtick`; }",
193 options: ["backtick"],
194 parserOptions: { ecmaVersion: 6 },
195 errors: [{ message: "Strings must use backtick.", type: "Literal" }]
196 },
197 {
198 code: "if (1) { \"use strict\"; var foo = `backtick`; }",
199 output: "if (1) { `use strict`; var foo = `backtick`; }",
200 options: ["backtick"],
201 parserOptions: { ecmaVersion: 6 },
202 errors: [{ message: "Strings must use backtick.", type: "Literal" }]
203 },
204
205 // `backtick` should warn computed property names.
206 {
207 code: "var obj = {[\"key0\"]: 0, ['key1']: 1};",
208 output: "var obj = {[`key0`]: 0, [`key1`]: 1};",
209 options: ["backtick"],
210 parserOptions: { ecmaVersion: 6 },
211 errors: [
212 { message: "Strings must use backtick.", type: "Literal" },
213 { message: "Strings must use backtick.", type: "Literal" }
214 ]
215 },
216 {
217 code: "class Foo { ['a'](){} static ['b'](){} }",
218 output: "class Foo { [`a`](){} static [`b`](){} }",
219 options: ["backtick"],
220 parserOptions: { ecmaVersion: 6 },
221 errors: [
222 { message: "Strings must use backtick.", type: "Literal" },
223 { message: "Strings must use backtick.", type: "Literal" }
224 ]
225 },
226
227 // https://github.com/eslint/eslint/issues/7084
228 {
229 code: "<div blah={\"blah\"} />",
230 output: "<div blah={'blah'} />",
231 options: ["single"],
232 parserOptions: { ecmaFeatures: { jsx: true } },
233 errors: [
234 { message: "Strings must use singlequote.", type: "Literal" }
235 ]
236 },
237 {
238 code: "<div blah={'blah'} />",
239 output: "<div blah={\"blah\"} />",
240 options: ["double"],
241 parserOptions: { ecmaFeatures: { jsx: true } },
242 errors: [
243 { message: "Strings must use doublequote.", type: "Literal" }
244 ]
245 },
246 {
247 code: "<div blah={'blah'} />",
248 output: "<div blah={`blah`} />",
249 options: ["backtick"],
250 parserOptions: { ecmaFeatures: { jsx: true } },
251 errors: [
252 { message: "Strings must use backtick.", type: "Literal" }
253 ]
254 },
255
256 // https://github.com/eslint/eslint/issues/7610
257 {
258 code: "`use strict`;",
259 output: null,
260 parserOptions: { ecmaVersion: 6 },
261 errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
262 },
263 {
264 code: "function foo() { `use strict`; foo(); }",
265 output: null,
266 parserOptions: { ecmaVersion: 6 },
267 errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
268 },
269 {
270 code: "foo = function() { `use strict`; foo(); }",
271 output: null,
272 parserOptions: { ecmaVersion: 6 },
273 errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
274 },
275 {
276 code: "() => { `use strict`; foo(); }",
277 output: null,
278 parserOptions: { ecmaVersion: 6 },
279 errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
280 },
281 {
282 code: "() => { foo(); `use strict`; }",
283 output: "() => { foo(); \"use strict\"; }",
284 parserOptions: { ecmaVersion: 6 },
285 errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
286 },
287 {
288 code: "foo(); `use strict`;",
289 output: "foo(); \"use strict\";",
290 parserOptions: { ecmaVersion: 6 },
291 errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
292 },
293
294 // https://github.com/eslint/eslint/issues/7646
295 {
296 code: "var foo = `foo\\nbar`;",
297 output: "var foo = \"foo\\nbar\";",
298 parserOptions: { ecmaVersion: 6 },
299 errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
300 },
301 {
302 code: "var foo = `foo\\\nbar`;", // 1 backslash followed by a newline
303 output: "var foo = \"foo\\\nbar\";",
304 parserOptions: { ecmaVersion: 6 },
305 errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
306 },
307 {
308 code: "var foo = `foo\\\\\\\nbar`;", // 3 backslashes followed by a newline
309 output: "var foo = \"foo\\\\\\\nbar\";",
310 parserOptions: { ecmaVersion: 6 },
311 errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
312 },
313 {
314 code: "````",
315 output: "\"\"``",
316 parserOptions: { ecmaVersion: 6 },
317 errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral", line: 1, column: 1 }]
318 }
319 ],
320});