]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for quote-props rule. | |
3 | * @author Mathias Bynens <http://mathiasbynens.be/> | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/quote-props"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | const ruleTester = new RuleTester(); | |
16 | ||
17 | ruleTester.run("quote-props", rule, { | |
18 | valid: [ | |
19 | "({ '0': 0 })", | |
20 | "({ 'a': 0 })", | |
21 | "({ \"a\": 0 })", | |
22 | "({ 'null': 0 })", | |
23 | "({ 'true': 0 })", | |
24 | "({ 'a-b': 0 })", | |
25 | "({ 'if': 0 })", | |
26 | "({ '@': 0 })", | |
27 | ||
28 | { code: "({ 'a': 0, b(){} })", parserOptions: { ecmaVersion: 6 } }, | |
29 | { code: "({ [x]: 0 });", env: { es6: true } }, | |
30 | { code: "({ x });", env: { es6: true } }, | |
31 | { code: "({ a: 0, b(){} })", options: ["as-needed"], parserOptions: { ecmaVersion: 6 } }, | |
32 | { code: "({ a: 0, [x]: 1 })", options: ["as-needed"], env: { es6: true } }, | |
33 | { code: "({ a: 0, x })", options: ["as-needed"], env: { es6: true } }, | |
34 | { code: "({ '@': 0, [x]: 1 })", options: ["as-needed"], env: { es6: true } }, | |
35 | { code: "({ '@': 0, x })", options: ["as-needed"], env: { es6: true } }, | |
36 | { code: "({ a: 0, b: 0 })", options: ["as-needed"] }, | |
37 | { code: "({ a: 0, 0: 0 })", options: ["as-needed"] }, | |
38 | { code: "({ a: 0, true: 0 })", options: ["as-needed"] }, | |
39 | { code: "({ a: 0, null: 0 })", options: ["as-needed"] }, | |
40 | { code: "({ a: 0, if: 0 })", options: ["as-needed"] }, | |
41 | { code: "({ a: 0, while: 0 })", options: ["as-needed"] }, | |
42 | { code: "({ a: 0, volatile: 0 })", options: ["as-needed"] }, | |
43 | { code: "({ a: 0, '-b': 0 })", options: ["as-needed"] }, | |
44 | { code: "({ a: 0, '@': 0 })", options: ["as-needed"] }, | |
45 | { code: "({ a: 0, '0x0': 0 })", options: ["as-needed"] }, | |
46 | { code: "({ ' 0': 0, '0x0': 0 })", options: ["as-needed"] }, | |
47 | { code: "({ '0 ': 0 })", options: ["as-needed"] }, | |
48 | { code: "({ 'hey//meh': 0 })", options: ["as-needed"] }, | |
49 | { code: "({ 'hey/*meh': 0 })", options: ["as-needed"] }, | |
50 | { code: "({ 'hey/*meh*/': 0 })", options: ["as-needed"] }, | |
51 | { code: "({ 'a': 0, '-b': 0 })", options: ["consistent"] }, | |
52 | { code: "({ 'true': 0, 'b': 0 })", options: ["consistent"] }, | |
53 | { code: "({ null: 0, a: 0 })", options: ["consistent"] }, | |
54 | { code: "({ a: 0, b: 0 })", options: ["consistent"] }, | |
55 | { code: "({ 'a': 1, [x]: 0 });", options: ["consistent"], env: { es6: true } }, | |
56 | { code: "({ 'a': 1, x });", options: ["consistent"], env: { es6: true } }, | |
57 | { code: "({ a: 0, b: 0 })", options: ["consistent-as-needed"] }, | |
58 | { code: "({ a: 0, null: 0 })", options: ["consistent-as-needed"] }, | |
59 | { code: "({ 'a': 0, '-b': 0 })", options: ["consistent-as-needed"] }, | |
60 | { code: "({ '@': 0, 'B': 0 })", options: ["consistent-as-needed"] }, | |
61 | { code: "({ 'while': 0, 'B': 0 })", options: ["consistent-as-needed", { keywords: true }] }, | |
62 | { code: "({ '@': 0, 'B': 0 })", options: ["consistent-as-needed", { keywords: true }] }, | |
63 | { code: "({ '@': 1, [x]: 0 });", options: ["consistent-as-needed"], env: { es6: true } }, | |
64 | { code: "({ '@': 1, x });", options: ["consistent-as-needed"], env: { es6: true } }, | |
65 | { code: "({ a: 1, [x]: 0 });", options: ["consistent-as-needed"], env: { es6: true } }, | |
66 | { code: "({ a: 1, x });", options: ["consistent-as-needed"], env: { es6: true } }, | |
67 | { code: "({ a: 0, 'if': 0 })", options: ["as-needed", { keywords: true }] }, | |
68 | { code: "({ a: 0, 'while': 0 })", options: ["as-needed", { keywords: true }] }, | |
69 | { code: "({ a: 0, 'volatile': 0 })", options: ["as-needed", { keywords: true }] }, | |
70 | { code: "({'unnecessary': 1, 'if': 0})", options: ["as-needed", { keywords: true, unnecessary: false }] }, | |
71 | { code: "({'1': 1})", options: ["as-needed", { numbers: true }] }, | |
72 | { code: "({1: 1, x: 2})", options: ["consistent", { numbers: true }] }, | |
73 | { code: "({1: 1, x: 2})", options: ["consistent-as-needed", { numbers: true }] }, | |
74 | { code: "({ ...x })", options: ["as-needed"], parserOptions: { ecmaVersion: 2018 } }, | |
75 | { code: "({ ...x })", options: ["consistent"], parserOptions: { ecmaVersion: 2018 } }, | |
76 | { code: "({ ...x })", options: ["consistent-as-needed"], parserOptions: { ecmaVersion: 2018 } }, | |
77 | { code: "({ 1n: 1 })", options: ["as-needed"], parserOptions: { ecmaVersion: 2020 } }, | |
78 | { code: "({ 1n: 1 })", options: ["as-needed", { numbers: false }], parserOptions: { ecmaVersion: 2020 } }, | |
79 | { code: "({ 1n: 1 })", options: ["consistent"], parserOptions: { ecmaVersion: 2020 } }, | |
80 | { code: "({ 1n: 1 })", options: ["consistent-as-needed"], parserOptions: { ecmaVersion: 2020 } }, | |
81 | { code: "({ '99999999999999999': 1 })", options: ["as-needed"], parserOptions: { ecmaVersion: 2020 } }, | |
6f036462 TL |
82 | { code: "({ '1n': 1 })", options: ["as-needed"], parserOptions: { ecmaVersion: 2020 } }, |
83 | { code: "({ 1_0: 1 })", options: ["as-needed"], parserOptions: { ecmaVersion: 2021 } }, | |
84 | { code: "({ 1_0: 1 })", options: ["as-needed", { numbers: false }], parserOptions: { ecmaVersion: 2021 } }, | |
85 | { code: "({ '1_0': 1 })", options: ["as-needed"], parserOptions: { ecmaVersion: 2021 } }, | |
86 | { code: "({ '1_0': 1 })", options: ["as-needed", { numbers: false }], parserOptions: { ecmaVersion: 2021 } }, | |
87 | { code: "({ '1_0': 1 })", options: ["as-needed", { numbers: true }], parserOptions: { ecmaVersion: 2021 } }, | |
88 | { code: "({ 1_0: 1, 1: 1 })", options: ["consistent-as-needed"], parserOptions: { ecmaVersion: 2021 } } | |
eb39fafa DC |
89 | ], |
90 | invalid: [{ | |
91 | code: "({ a: 0 })", | |
92 | output: "({ \"a\": 0 })", | |
93 | errors: [{ | |
94 | messageId: "unquotedPropertyFound", | |
95 | data: { property: "a" }, | |
96 | type: "Property" | |
97 | }] | |
98 | }, { | |
99 | code: "({ 0: '0' })", | |
100 | output: "({ \"0\": '0' })", | |
101 | errors: [{ | |
102 | messageId: "unquotedPropertyFound", | |
103 | data: { property: "0" }, | |
104 | type: "Property" | |
105 | }] | |
106 | }, { | |
107 | code: "({ 'a': 0 })", | |
108 | output: "({ a: 0 })", | |
109 | options: ["as-needed"], | |
110 | errors: [{ | |
111 | messageId: "unnecessarilyQuotedProperty", | |
112 | data: { property: "a" }, | |
113 | type: "Property" | |
114 | }] | |
115 | }, { | |
116 | code: "({ 'null': 0 })", | |
117 | output: "({ null: 0 })", | |
118 | options: ["as-needed"], | |
119 | errors: [{ | |
120 | messageId: "unnecessarilyQuotedProperty", | |
121 | data: { property: "null" }, | |
122 | type: "Property" | |
123 | }] | |
124 | }, { | |
125 | code: "({ 'true': 0 })", | |
126 | output: "({ true: 0 })", | |
127 | options: ["as-needed"], | |
128 | errors: [{ | |
129 | messageId: "unnecessarilyQuotedProperty", | |
130 | data: { property: "true" }, | |
131 | type: "Property" | |
132 | }] | |
133 | }, { | |
134 | code: "({ '0': 0 })", | |
135 | output: "({ 0: 0 })", | |
136 | options: ["as-needed"], | |
137 | errors: [{ | |
138 | messageId: "unnecessarilyQuotedProperty", | |
139 | data: { property: "0" }, | |
140 | type: "Property" | |
141 | }] | |
142 | }, { | |
143 | code: "({ '-a': 0, b: 0 })", | |
144 | output: "({ '-a': 0, \"b\": 0 })", | |
145 | options: ["consistent"], | |
146 | errors: [{ | |
147 | messageId: "inconsistentlyQuotedProperty", | |
148 | data: { key: "b" }, | |
149 | type: "Property" | |
150 | }] | |
151 | }, { | |
152 | code: "({ a: 0, 'b': 0 })", | |
153 | output: "({ \"a\": 0, 'b': 0 })", | |
154 | options: ["consistent"], | |
155 | errors: [{ | |
156 | messageId: "inconsistentlyQuotedProperty", | |
157 | data: { key: "a" }, | |
158 | type: "Property" | |
159 | }] | |
160 | }, { | |
161 | code: "({ '-a': 0, b: 0 })", | |
162 | output: "({ '-a': 0, \"b\": 0 })", | |
163 | options: ["consistent-as-needed"], | |
164 | errors: [{ | |
165 | messageId: "inconsistentlyQuotedProperty", | |
166 | data: { key: "b" }, | |
167 | type: "Property" | |
168 | }] | |
169 | }, { | |
170 | code: "({ 'a': 0, 'b': 0 })", | |
171 | output: "({ a: 0, b: 0 })", | |
172 | options: ["consistent-as-needed"], | |
173 | errors: [ | |
174 | { messageId: "redundantQuoting", type: "Property" }, | |
175 | { messageId: "redundantQuoting", type: "Property" } | |
176 | ] | |
177 | }, { | |
178 | code: "({ 'a': 0, [x]: 0 })", | |
179 | output: "({ a: 0, [x]: 0 })", | |
180 | options: ["consistent-as-needed"], | |
181 | env: { es6: true }, | |
182 | errors: [ | |
183 | { messageId: "redundantQuoting", type: "Property" } | |
184 | ] | |
185 | }, { | |
186 | code: "({ 'a': 0, x })", | |
187 | output: "({ a: 0, x })", | |
188 | options: ["consistent-as-needed"], | |
189 | env: { es6: true }, | |
190 | errors: [{ | |
191 | messageId: "redundantQuoting", type: "Property" | |
192 | }] | |
193 | }, { | |
194 | code: "({ 'true': 0, 'null': 0 })", | |
195 | output: "({ true: 0, null: 0 })", | |
196 | options: ["consistent-as-needed"], | |
197 | errors: [ | |
198 | { messageId: "redundantQuoting", type: "Property" }, | |
199 | { messageId: "redundantQuoting", type: "Property" } | |
200 | ] | |
201 | }, { | |
202 | code: "({ true: 0, 'null': 0 })", | |
203 | output: "({ \"true\": 0, 'null': 0 })", | |
204 | options: ["consistent"], | |
205 | errors: [{ | |
206 | messageId: "inconsistentlyQuotedProperty", | |
207 | data: { key: "true" }, | |
208 | type: "Property" | |
209 | }] | |
210 | }, { | |
211 | code: "({ 'a': 0, 'b': 0 })", | |
212 | output: "({ a: 0, b: 0 })", | |
213 | options: ["consistent-as-needed", { keywords: true }], | |
214 | errors: [ | |
215 | { messageId: "redundantQuoting", type: "Property" }, | |
216 | { messageId: "redundantQuoting", type: "Property" } | |
217 | ] | |
218 | }, { | |
219 | code: "({ while: 0, b: 0 })", | |
220 | output: "({ \"while\": 0, \"b\": 0 })", | |
221 | options: ["consistent-as-needed", { keywords: true }], | |
222 | errors: [ | |
223 | { | |
224 | messageId: "requireQuotesDueToReservedWord", | |
225 | data: { property: "while" }, | |
226 | type: "Property" | |
227 | }, | |
228 | { | |
229 | messageId: "requireQuotesDueToReservedWord", | |
230 | data: { property: "while" }, | |
231 | type: "Property" | |
232 | } | |
233 | ] | |
234 | }, { | |
235 | code: "({ while: 0, 'b': 0 })", | |
236 | output: "({ \"while\": 0, 'b': 0 })", | |
237 | options: ["consistent-as-needed", { keywords: true }], | |
238 | errors: [{ | |
239 | messageId: "requireQuotesDueToReservedWord", | |
240 | data: { property: "while" }, | |
241 | type: "Property" | |
242 | ||
243 | }] | |
244 | }, { | |
245 | code: "({ foo: 0, 'bar': 0 })", | |
246 | output: "({ foo: 0, bar: 0 })", | |
247 | options: ["consistent-as-needed", { keywords: true }], | |
248 | errors: [ | |
249 | { messageId: "redundantQuoting", type: "Property" } | |
250 | ] | |
251 | }, { | |
252 | code: | |
253 | "({\n" + | |
254 | " /* a */ 'prop1' /* b */ : /* c */ value1 /* d */ ,\n" + | |
255 | " /* e */ prop2 /* f */ : /* g */ value2 /* h */,\n" + | |
256 | " /* i */ \"prop3\" /* j */ : /* k */ value3 /* l */\n" + | |
257 | "})", | |
258 | output: | |
259 | "({\n" + | |
260 | " /* a */ 'prop1' /* b */ : /* c */ value1 /* d */ ,\n" + | |
261 | " /* e */ \"prop2\" /* f */ : /* g */ value2 /* h */,\n" + | |
262 | " /* i */ \"prop3\" /* j */ : /* k */ value3 /* l */\n" + | |
263 | "})", | |
264 | options: ["consistent"], | |
265 | errors: [{ | |
266 | messageId: "inconsistentlyQuotedProperty", | |
267 | data: { key: "prop2" }, | |
268 | type: "Property" | |
269 | }] | |
270 | }, { | |
271 | code: | |
272 | "({\n" + | |
273 | " /* a */ \"foo\" /* b */ : /* c */ value1 /* d */ ,\n" + | |
274 | " /* e */ \"bar\" /* f */ : /* g */ value2 /* h */,\n" + | |
275 | " /* i */ \"baz\" /* j */ : /* k */ value3 /* l */\n" + | |
276 | "})", | |
277 | output: | |
278 | "({\n" + | |
279 | " /* a */ foo /* b */ : /* c */ value1 /* d */ ,\n" + | |
280 | " /* e */ bar /* f */ : /* g */ value2 /* h */,\n" + | |
281 | " /* i */ baz /* j */ : /* k */ value3 /* l */\n" + | |
282 | "})", | |
283 | options: ["consistent-as-needed"], | |
284 | errors: [ | |
285 | { messageId: "redundantQuoting", type: "Property" }, | |
286 | { messageId: "redundantQuoting", type: "Property" }, | |
287 | { messageId: "redundantQuoting", type: "Property" } | |
288 | ] | |
289 | }, { | |
290 | code: "({'if': 0})", | |
291 | output: "({if: 0})", | |
292 | options: ["as-needed"], | |
293 | errors: [{ | |
294 | messageId: "unnecessarilyQuotedProperty", | |
295 | data: { property: "if" }, | |
296 | type: "Property" | |
297 | }] | |
298 | }, { | |
299 | code: "({'synchronized': 0})", | |
300 | output: "({synchronized: 0})", | |
301 | options: ["as-needed"], | |
302 | errors: [{ | |
303 | messageId: "unnecessarilyQuotedProperty", | |
304 | data: { property: "synchronized" }, | |
305 | type: "Property" | |
306 | }] | |
307 | }, { | |
308 | code: "({while: 0})", | |
309 | output: "({\"while\": 0})", | |
310 | options: ["as-needed", { keywords: true }], | |
311 | errors: [{ | |
312 | messageId: "unquotedReservedProperty", | |
313 | data: { property: "while" }, | |
314 | type: "Property" | |
315 | }] | |
316 | }, { | |
317 | code: "({'unnecessary': 1, if: 0})", | |
318 | output: "({'unnecessary': 1, \"if\": 0})", | |
319 | options: ["as-needed", { keywords: true, unnecessary: false }], | |
320 | errors: [{ | |
321 | messageId: "unquotedReservedProperty", | |
322 | data: { property: "if" }, | |
323 | type: "Property" | |
324 | }] | |
325 | }, { | |
326 | code: "({1: 1})", | |
327 | output: "({\"1\": 1})", | |
328 | options: ["as-needed", { numbers: true }], | |
329 | errors: [{ | |
330 | messageId: "unquotedNumericProperty", | |
331 | data: { property: "1" }, | |
332 | type: "Property" | |
333 | }] | |
334 | }, { | |
335 | code: "({1: 1})", | |
336 | output: "({\"1\": 1})", | |
337 | options: ["always", { numbers: false }], | |
338 | errors: [{ | |
339 | messageId: "unquotedPropertyFound", | |
340 | data: { property: "1" }, | |
341 | type: "Property" | |
342 | }] | |
343 | }, { | |
344 | code: "({0x123: 1})", | |
345 | output: "({\"291\": 1})", // 0x123 === 291 | |
346 | options: ["always"], | |
347 | errors: [{ | |
348 | messageId: "unquotedPropertyFound", | |
349 | data: { property: "291" } | |
350 | ||
351 | }] | |
352 | }, { | |
353 | code: "({1e2: 1})", | |
354 | output: "({\"100\": 1})", | |
355 | options: ["always", { numbers: false }], | |
356 | errors: [{ | |
357 | messageId: "unquotedPropertyFound", | |
358 | data: { property: "100" } | |
359 | ||
360 | }] | |
361 | }, { | |
362 | code: "({5.: 1})", | |
363 | output: "({\"5\": 1})", | |
364 | options: ["always", { numbers: false }], | |
365 | errors: [{ | |
366 | messageId: "unquotedPropertyFound", | |
367 | data: { property: "5" } | |
368 | ||
369 | }] | |
370 | }, { | |
371 | code: "({ 1n: 1 })", | |
372 | output: "({ \"1\": 1 })", | |
373 | options: ["always"], | |
374 | parserOptions: { ecmaVersion: 2020 }, | |
375 | errors: [{ | |
376 | messageId: "unquotedPropertyFound", | |
377 | data: { property: "1" } | |
378 | } | |
379 | ] | |
380 | }, { | |
381 | code: "({ 1n: 1 })", | |
382 | output: "({ \"1\": 1 })", | |
383 | options: ["as-needed", { numbers: true }], | |
384 | parserOptions: { ecmaVersion: 2020 }, | |
385 | errors: [{ | |
386 | messageId: "unquotedNumericProperty", | |
387 | data: { property: "1" } | |
388 | }] | |
6f036462 TL |
389 | }, { |
390 | code: "({ 1_0: 1 })", | |
391 | output: "({ \"10\": 1 })", | |
392 | options: ["as-needed", { numbers: true }], | |
393 | parserOptions: { ecmaVersion: 2021 }, | |
394 | errors: [{ | |
395 | messageId: "unquotedNumericProperty", | |
396 | data: { property: "10" } | |
397 | }] | |
398 | }, { | |
399 | code: "({ 1_2.3_4e0_2: 1 })", | |
400 | output: "({ \"1234\": 1 })", | |
401 | options: ["always"], | |
402 | parserOptions: { ecmaVersion: 2021 }, | |
403 | errors: [{ | |
404 | messageId: "unquotedPropertyFound", | |
405 | data: { property: "1234" } | |
406 | }] | |
407 | }, { | |
408 | code: "({ 0b1_000: 1 })", | |
409 | output: "({ \"8\": 1 })", | |
410 | options: ["always"], | |
411 | parserOptions: { ecmaVersion: 2021 }, | |
412 | errors: [{ | |
413 | messageId: "unquotedPropertyFound", | |
414 | data: { property: "8" } | |
415 | }] | |
416 | }, { | |
417 | code: "({ 1_000: a, '1_000': b })", | |
418 | output: "({ \"1000\": a, '1_000': b })", | |
419 | options: ["consistent-as-needed"], | |
420 | parserOptions: { ecmaVersion: 2021 }, | |
421 | errors: [{ | |
422 | messageId: "inconsistentlyQuotedProperty", | |
423 | data: { key: "1000" } | |
424 | }] | |
eb39fafa DC |
425 | }] |
426 | }); |