]>
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 | |
226 | } | |
227 | ] | |
228 | }); |