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