]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for prefer-template rule. | |
3 | * @author Toru Nagashima | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/prefer-template"); | |
13 | const { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | const errors = [{ | |
20 | messageId: "unexpectedStringConcatenation", | |
21 | type: "BinaryExpression" | |
22 | }]; | |
23 | ||
24 | const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } }); | |
25 | ||
26 | ruleTester.run("prefer-template", rule, { | |
27 | valid: [ | |
28 | "'use strict';", | |
29 | "var foo = 'foo' + '\\0';", | |
30 | "var foo = 'bar';", | |
31 | "var foo = 'bar' + 'baz';", | |
32 | "var foo = foo + +'100';", | |
33 | "var foo = `bar`;", | |
34 | "var foo = `hello, ${name}!`;", | |
35 | ||
36 | // https://github.com/eslint/eslint/issues/3507 | |
37 | "var foo = `foo` + `bar` + \"hoge\";", | |
38 | "var foo = `foo` +\n `bar` +\n \"hoge\";" | |
39 | ], | |
40 | invalid: [ | |
41 | { | |
42 | code: "var foo = 'hello, ' + name + '!';", | |
43 | output: "var foo = `hello, ${ name }!`;", | |
44 | errors | |
45 | }, | |
46 | { | |
47 | code: "var foo = bar + 'baz';", | |
48 | output: "var foo = `${bar }baz`;", | |
49 | errors | |
50 | }, | |
51 | { | |
52 | code: "var foo = bar + `baz`;", | |
53 | output: "var foo = `${bar }baz`;", | |
54 | errors | |
55 | }, | |
56 | { | |
57 | code: "var foo = +100 + 'yen';", | |
58 | output: "var foo = `${+100 }yen`;", | |
59 | errors | |
60 | }, | |
61 | { | |
62 | code: "var foo = 'bar' + baz;", | |
63 | output: "var foo = `bar${ baz}`;", | |
64 | errors | |
65 | }, | |
66 | { | |
67 | code: "var foo = '¥' + (n * 1000) + '-'", | |
68 | output: "var foo = `¥${ n * 1000 }-`", | |
69 | errors | |
70 | }, | |
71 | { | |
72 | code: "var foo = 'aaa' + aaa; var bar = 'bbb' + bbb;", | |
73 | output: "var foo = `aaa${ aaa}`; var bar = `bbb${ bbb}`;", | |
74 | errors: [errors[0], errors[0]] | |
75 | }, | |
76 | { | |
77 | code: "var string = (number + 1) + 'px';", | |
78 | output: "var string = `${number + 1 }px`;", | |
79 | errors | |
80 | }, | |
81 | { | |
82 | code: "var foo = 'bar' + baz + 'qux';", | |
83 | output: "var foo = `bar${ baz }qux`;", | |
84 | errors | |
85 | }, | |
86 | { | |
87 | code: "var foo = '0 backslashes: ${bar}' + baz;", | |
88 | output: "var foo = `0 backslashes: \\${bar}${ baz}`;", | |
89 | errors | |
90 | }, | |
91 | { | |
92 | code: "var foo = '1 backslash: \\${bar}' + baz;", | |
93 | output: "var foo = `1 backslash: \\${bar}${ baz}`;", | |
94 | errors | |
95 | }, | |
96 | { | |
97 | code: "var foo = '2 backslashes: \\\\${bar}' + baz;", | |
98 | output: "var foo = `2 backslashes: \\\\\\${bar}${ baz}`;", | |
99 | errors | |
100 | }, | |
101 | { | |
102 | code: "var foo = '3 backslashes: \\\\\\${bar}' + baz;", | |
103 | output: "var foo = `3 backslashes: \\\\\\${bar}${ baz}`;", | |
104 | errors | |
105 | }, | |
106 | { | |
107 | code: "var foo = bar + 'this is a backtick: `' + baz;", | |
108 | output: "var foo = `${bar }this is a backtick: \\`${ baz}`;", | |
109 | errors | |
110 | }, | |
111 | { | |
112 | code: "var foo = bar + 'this is a backtick preceded by a backslash: \\`' + baz;", | |
113 | output: "var foo = `${bar }this is a backtick preceded by a backslash: \\`${ baz}`;", | |
114 | errors | |
115 | }, | |
116 | { | |
117 | code: "var foo = bar + 'this is a backtick preceded by two backslashes: \\\\`' + baz;", | |
118 | output: "var foo = `${bar }this is a backtick preceded by two backslashes: \\\\\\`${ baz}`;", | |
119 | errors | |
120 | }, | |
121 | { | |
122 | code: "var foo = bar + `${baz}foo`;", | |
123 | output: "var foo = `${bar }${baz}foo`;", | |
124 | errors | |
125 | }, | |
126 | { | |
127 | code: | |
128 | "var foo = 'favorites: ' + favorites.map(f => {\n" + | |
129 | " return f.name;\n" + | |
130 | "}) + ';';", | |
131 | output: | |
132 | "var foo = `favorites: ${ favorites.map(f => {\n" + | |
133 | " return f.name;\n" + | |
134 | "}) };`;", | |
135 | errors | |
136 | }, | |
137 | { | |
138 | code: "var foo = bar + baz + 'qux';", | |
139 | output: "var foo = `${bar + baz }qux`;", | |
140 | errors | |
141 | }, | |
142 | { | |
143 | code: | |
144 | "var foo = 'favorites: ' +\n" + | |
145 | " favorites.map(f => {\n" + | |
146 | " return f.name;\n" + | |
147 | " }) +\n" + | |
148 | "';';", | |
149 | output: | |
150 | "var foo = `favorites: ${ \n" + | |
151 | " favorites.map(f => {\n" + | |
152 | " return f.name;\n" + | |
153 | " }) \n" + | |
154 | "};`;", | |
155 | errors | |
156 | }, | |
157 | { | |
158 | code: "var foo = /* a */ 'bar' /* b */ + /* c */ baz /* d */ + 'qux' /* e */ ;", | |
159 | output: "var foo = /* a */ `bar${ /* b */ /* c */ baz /* d */ }qux` /* e */ ;", | |
160 | errors | |
161 | }, | |
162 | { | |
163 | code: "var foo = bar + ('baz') + 'qux' + (boop);", | |
164 | output: "var foo = `${bar }baz` + `qux${ boop}`;", | |
165 | errors | |
166 | }, | |
167 | { | |
168 | code: "foo + 'unescapes an escaped single quote in a single-quoted string: \\''", | |
169 | output: "`${foo }unescapes an escaped single quote in a single-quoted string: '`", | |
170 | errors | |
171 | }, | |
172 | { | |
173 | code: "foo + \"unescapes an escaped double quote in a double-quoted string: \\\"\"", | |
174 | output: "`${foo }unescapes an escaped double quote in a double-quoted string: \"`", | |
175 | errors | |
176 | }, | |
177 | { | |
178 | code: "foo + 'does not unescape an escaped double quote in a single-quoted string: \\\"'", | |
179 | output: "`${foo }does not unescape an escaped double quote in a single-quoted string: \\\"`", | |
180 | errors | |
181 | }, | |
182 | { | |
183 | code: "foo + \"does not unescape an escaped single quote in a double-quoted string: \\'\"", | |
184 | output: "`${foo }does not unescape an escaped single quote in a double-quoted string: \\'`", | |
185 | errors | |
186 | }, | |
187 | { | |
188 | code: "foo + 'handles unicode escapes correctly: \\x27'", // "\x27" === "'" | |
189 | output: "`${foo }handles unicode escapes correctly: \\x27`", | |
190 | errors | |
191 | }, | |
192 | { | |
193 | code: "foo + 'does not autofix octal escape sequence' + '\\033'", | |
194 | output: null, | |
195 | errors | |
196 | }, | |
6f036462 TL |
197 | { |
198 | code: "foo + 'does not autofix non-octal decimal escape sequence' + '\\8'", | |
199 | output: null, | |
200 | errors | |
201 | }, | |
eb39fafa DC |
202 | { |
203 | code: "foo + '\\n other text \\033'", | |
204 | output: null, | |
205 | errors | |
206 | }, | |
207 | { | |
208 | code: "foo + '\\0\\1'", | |
209 | output: null, | |
210 | errors | |
211 | }, | |
212 | { | |
213 | code: "foo + '\\08'", | |
214 | output: null, | |
215 | errors | |
216 | }, | |
217 | { | |
218 | code: "foo + '\\\\033'", | |
219 | output: "`${foo }\\\\033`", | |
220 | errors | |
221 | }, | |
222 | { | |
223 | code: "foo + '\\0'", | |
224 | output: "`${foo }\\0`", | |
225 | errors | |
8f9d1d4d DC |
226 | }, |
227 | ||
228 | // https://github.com/eslint/eslint/issues/15083 | |
229 | { | |
230 | code: `"default-src 'self' https://*.google.com;" | |
231 | + "frame-ancestors 'none';" | |
232 | + "report-to " + foo + ";"`, | |
233 | output: `\`default-src 'self' https://*.google.com;\` | |
234 | + \`frame-ancestors 'none';\` | |
235 | + \`report-to \${ foo };\``, | |
236 | errors | |
237 | }, | |
238 | { | |
239 | code: "'a' + 'b' + foo", | |
240 | output: "`a` + `b${ foo}`", | |
241 | errors | |
242 | }, | |
243 | { | |
244 | code: "'a' + 'b' + foo + 'c' + 'd'", | |
245 | output: "`a` + `b${ foo }c` + `d`", | |
246 | errors | |
247 | }, | |
248 | { | |
249 | code: "'a' + 'b + c' + foo + 'd' + 'e'", | |
250 | output: "`a` + `b + c${ foo }d` + `e`", | |
251 | errors | |
252 | }, | |
253 | { | |
254 | code: "'a' + 'b' + foo + ('c' + 'd')", | |
255 | output: "`a` + `b${ foo }c` + `d`", | |
256 | errors | |
257 | }, | |
258 | { | |
259 | code: "'a' + 'b' + foo + ('a' + 'b')", | |
260 | output: "`a` + `b${ foo }a` + `b`", | |
261 | errors | |
262 | }, | |
263 | { | |
264 | code: "'a' + 'b' + foo + ('c' + 'd') + ('e' + 'f')", | |
265 | output: "`a` + `b${ foo }c` + `d` + `e` + `f`", | |
266 | errors | |
267 | }, | |
268 | { | |
269 | code: "foo + ('a' + 'b') + ('c' + 'd')", | |
270 | output: "`${foo }a` + `b` + `c` + `d`", | |
271 | errors | |
272 | }, | |
273 | { | |
274 | code: "'a' + foo + ('b' + 'c') + ('d' + bar + 'e')", | |
275 | output: "`a${ foo }b` + `c` + `d${ bar }e`", | |
276 | errors | |
277 | }, | |
278 | { | |
279 | code: "foo + ('b' + 'c') + ('d' + bar + 'e')", | |
280 | output: "`${foo }b` + `c` + `d${ bar }e`", | |
281 | errors | |
282 | }, | |
283 | { | |
284 | code: "'a' + 'b' + foo + ('c' + 'd' + 'e')", | |
285 | output: "`a` + `b${ foo }c` + `d` + `e`", | |
286 | errors | |
287 | }, | |
288 | { | |
289 | code: "'a' + 'b' + foo + ('c' + bar + 'd')", | |
290 | output: "`a` + `b${ foo }c${ bar }d`", | |
291 | errors | |
292 | }, | |
293 | { | |
294 | code: "'a' + 'b' + foo + ('c' + bar + ('d' + 'e') + 'f')", | |
295 | output: "`a` + `b${ foo }c${ bar }d` + `e` + `f`", | |
296 | errors | |
297 | }, | |
298 | { | |
299 | code: "'a' + 'b' + foo + ('c' + bar + 'e') + 'f' + test", | |
300 | output: "`a` + `b${ foo }c${ bar }e` + `f${ test}`", | |
301 | errors | |
302 | }, | |
303 | { | |
304 | code: "'a' + foo + ('b' + bar + 'c') + ('d' + test)", | |
305 | output: "`a${ foo }b${ bar }c` + `d${ test}`", | |
306 | errors | |
307 | }, | |
308 | { | |
309 | code: "'a' + foo + ('b' + 'c') + ('d' + bar)", | |
310 | output: "`a${ foo }b` + `c` + `d${ bar}`", | |
311 | errors | |
312 | }, | |
313 | { | |
314 | code: "foo + ('a' + bar + 'b') + 'c' + test", | |
315 | output: "`${foo }a${ bar }b` + `c${ test}`", | |
316 | errors | |
317 | }, | |
318 | { | |
319 | code: "'a' + '`b`' + c", | |
320 | output: "`a` + `\\`b\\`${ c}`", | |
321 | errors | |
322 | }, | |
323 | { | |
324 | code: "'a' + '`b` + `c`' + d", | |
325 | output: "`a` + `\\`b\\` + \\`c\\`${ d}`", | |
326 | errors | |
327 | }, | |
328 | { | |
329 | code: "'a' + b + ('`c`' + '`d`')", | |
330 | output: "`a${ b }\\`c\\`` + `\\`d\\``", | |
331 | errors | |
332 | }, | |
333 | { | |
334 | code: "'`a`' + b + ('`c`' + '`d`')", | |
335 | output: "`\\`a\\`${ b }\\`c\\`` + `\\`d\\``", | |
336 | errors | |
337 | }, | |
338 | { | |
339 | code: "foo + ('`a`' + bar + '`b`') + '`c`' + test", | |
340 | output: "`${foo }\\`a\\`${ bar }\\`b\\`` + `\\`c\\`${ test}`", | |
341 | errors | |
342 | }, | |
343 | { | |
344 | code: "'a' + ('b' + 'c') + d", | |
345 | output: "`a` + `b` + `c${ d}`", | |
346 | errors | |
347 | }, | |
348 | { | |
349 | code: "'a' + ('`b`' + '`c`') + d", | |
350 | output: "`a` + `\\`b\\`` + `\\`c\\`${ d}`", | |
351 | errors | |
352 | }, | |
353 | { | |
354 | code: "a + ('b' + 'c') + d", | |
355 | output: "`${a }b` + `c${ d}`", | |
356 | errors | |
357 | }, | |
358 | { | |
359 | code: "a + ('b' + 'c') + (d + 'e')", | |
360 | output: "`${a }b` + `c${ d }e`", | |
361 | errors | |
362 | }, | |
363 | { | |
364 | code: "a + ('`b`' + '`c`') + d", | |
365 | output: "`${a }\\`b\\`` + `\\`c\\`${ d}`", | |
366 | errors | |
367 | }, | |
368 | { | |
369 | code: "a + ('`b` + `c`' + '`d`') + e", | |
370 | output: "`${a }\\`b\\` + \\`c\\`` + `\\`d\\`${ e}`", | |
371 | errors | |
372 | }, | |
373 | { | |
374 | code: "'a' + ('b' + 'c' + 'd') + e", | |
375 | output: "`a` + `b` + `c` + `d${ e}`", | |
376 | errors | |
377 | }, | |
378 | { | |
379 | code: "'a' + ('b' + 'c' + 'd' + (e + 'f') + 'g' +'h' + 'i') + j", | |
380 | output: "`a` + `b` + `c` + `d${ e }fg` +`h` + `i${ j}`", | |
381 | errors | |
382 | }, | |
383 | { | |
384 | code: "a + (('b' + 'c') + 'd')", | |
385 | output: "`${a }b` + `c` + `d`", | |
386 | errors | |
387 | }, | |
388 | { | |
389 | code: "(a + 'b') + ('c' + 'd') + e", | |
390 | output: "`${a }b` + `c` + `d${ e}`", | |
391 | errors | |
392 | }, | |
393 | { | |
394 | code: "var foo = \"Hello \" + \"world \" + \"another \" + test", | |
395 | output: "var foo = `Hello ` + `world ` + `another ${ test}`", | |
396 | errors | |
397 | }, | |
398 | { | |
399 | code: "'Hello ' + '\"world\" ' + test", | |
400 | output: "`Hello ` + `\"world\" ${ test}`", | |
401 | errors | |
402 | }, | |
403 | { | |
404 | code: "\"Hello \" + \"'world' \" + test", | |
405 | output: "`Hello ` + `'world' ${ test}`", | |
406 | errors | |
eb39fafa DC |
407 | } |
408 | ] | |
409 | }); |