]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for dot-location. | |
3 | * @author Greg Cochard | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/dot-location"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | const ruleTester = new RuleTester(); | |
20 | ||
21 | ruleTester.run("dot-location", rule, { | |
22 | valid: [ | |
23 | "obj.\nprop", | |
24 | "obj. \nprop", | |
25 | "obj.\n prop", | |
26 | "(obj).\nprop", | |
27 | "obj\n['prop']", | |
28 | "obj['prop']", | |
29 | { | |
30 | code: "obj.\nprop", | |
31 | options: ["object"] | |
32 | }, | |
33 | { | |
34 | code: "obj\n.prop", | |
35 | options: ["property"] | |
36 | }, | |
37 | { | |
38 | code: "(obj)\n.prop", | |
39 | options: ["property"] | |
40 | }, | |
41 | { | |
42 | code: "obj . prop", | |
43 | options: ["object"] | |
44 | }, | |
45 | { | |
46 | code: "obj /* a */ . prop", | |
47 | options: ["object"] | |
48 | }, | |
49 | { | |
50 | code: "obj . \nprop", | |
51 | options: ["object"] | |
52 | }, | |
53 | { | |
54 | code: "obj . prop", | |
55 | options: ["property"] | |
56 | }, | |
57 | { | |
58 | code: "obj . /* a */ prop", | |
59 | options: ["property"] | |
60 | }, | |
61 | { | |
62 | code: "obj\n. prop", | |
63 | options: ["property"] | |
64 | }, | |
65 | { | |
66 | code: "f(a\n).prop", | |
67 | options: ["object"] | |
68 | }, | |
69 | { | |
70 | code: "`\n`.prop", | |
71 | options: ["object"], | |
72 | parserOptions: { ecmaVersion: 6 } | |
73 | }, | |
74 | { | |
75 | code: "obj[prop]", | |
76 | options: ["object"] | |
77 | }, | |
78 | { | |
79 | code: "obj\n[prop]", | |
80 | options: ["object"] | |
81 | }, | |
82 | { | |
83 | code: "obj[\nprop]", | |
84 | options: ["object"] | |
85 | }, | |
86 | { | |
87 | code: "obj\n[\nprop\n]", | |
88 | options: ["object"] | |
89 | }, | |
90 | { | |
91 | code: "obj[prop]", | |
92 | options: ["property"] | |
93 | }, | |
94 | { | |
95 | code: "obj\n[prop]", | |
96 | options: ["property"] | |
97 | }, | |
98 | { | |
99 | code: "obj[\nprop]", | |
100 | options: ["property"] | |
101 | }, | |
102 | { | |
103 | code: "obj\n[\nprop\n]", | |
104 | options: ["property"] | |
105 | }, | |
106 | ||
107 | // https://github.com/eslint/eslint/issues/11868 (also in invalid) | |
108 | { | |
109 | code: "(obj).prop", | |
110 | options: ["object"] | |
111 | }, | |
112 | { | |
113 | code: "(obj).\nprop", | |
114 | options: ["object"] | |
115 | }, | |
116 | { | |
117 | code: "(obj\n).\nprop", | |
118 | options: ["object"] | |
119 | }, | |
120 | { | |
121 | code: "(\nobj\n).\nprop", | |
122 | options: ["object"] | |
123 | }, | |
124 | { | |
125 | code: "((obj\n)).\nprop", | |
126 | options: ["object"] | |
127 | }, | |
128 | { | |
129 | code: "(f(a)\n).\nprop", | |
130 | options: ["object"] | |
131 | }, | |
132 | { | |
133 | code: "((obj\n)\n).\nprop", | |
134 | options: ["object"] | |
135 | }, | |
136 | { | |
137 | code: "(\na &&\nb()\n).toString()", | |
138 | options: ["object"] | |
6f036462 TL |
139 | }, |
140 | ||
141 | // Optional chaining | |
142 | { | |
143 | code: "obj?.prop", | |
144 | options: ["object"], | |
145 | parserOptions: { ecmaVersion: 2020 } | |
146 | }, | |
147 | { | |
148 | code: "obj?.[key]", | |
149 | options: ["object"], | |
150 | parserOptions: { ecmaVersion: 2020 } | |
151 | }, | |
152 | { | |
153 | code: "obj?.\nprop", | |
154 | options: ["object"], | |
155 | parserOptions: { ecmaVersion: 2020 } | |
156 | }, | |
157 | { | |
158 | code: "obj\n?.[key]", | |
159 | options: ["object"], | |
160 | parserOptions: { ecmaVersion: 2020 } | |
161 | }, | |
162 | { | |
163 | code: "obj?.\n[key]", | |
164 | options: ["object"], | |
165 | parserOptions: { ecmaVersion: 2020 } | |
166 | }, | |
167 | { | |
168 | code: "obj?.[\nkey]", | |
169 | options: ["object"], | |
170 | parserOptions: { ecmaVersion: 2020 } | |
171 | }, | |
172 | { | |
173 | code: "obj?.prop", | |
174 | options: ["property"], | |
175 | parserOptions: { ecmaVersion: 2020 } | |
176 | }, | |
177 | { | |
178 | code: "obj?.[key]", | |
179 | options: ["property"], | |
180 | parserOptions: { ecmaVersion: 2020 } | |
181 | }, | |
182 | { | |
183 | code: "obj\n?.prop", | |
184 | options: ["property"], | |
185 | parserOptions: { ecmaVersion: 2020 } | |
186 | }, | |
187 | { | |
188 | code: "obj\n?.[key]", | |
189 | options: ["property"], | |
190 | parserOptions: { ecmaVersion: 2020 } | |
191 | }, | |
192 | { | |
193 | code: "obj?.\n[key]", | |
194 | options: ["property"], | |
195 | parserOptions: { ecmaVersion: 2020 } | |
196 | }, | |
197 | { | |
198 | code: "obj?.[\nkey]", | |
199 | options: ["property"], | |
200 | parserOptions: { ecmaVersion: 2020 } | |
609c276f TL |
201 | }, |
202 | ||
203 | // Private properties | |
204 | { | |
205 | code: "class C { #a; foo() { this.\n#a; } }", | |
206 | options: ["object"], | |
207 | parserOptions: { ecmaVersion: 2022 } | |
208 | }, | |
209 | { | |
210 | code: "class C { #a; foo() { this\n.#a; } }", | |
211 | options: ["property"], | |
212 | parserOptions: { ecmaVersion: 2022 } | |
eb39fafa DC |
213 | } |
214 | ], | |
215 | invalid: [ | |
216 | { | |
217 | code: "obj\n.property", | |
218 | output: "obj.\nproperty", | |
219 | options: ["object"], | |
220 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1, endLine: 2, endColumn: 2 }] | |
221 | }, | |
222 | { | |
223 | code: "obj.\nproperty", | |
224 | output: "obj\n.property", | |
225 | options: ["property"], | |
226 | errors: [{ messageId: "expectedDotBeforeProperty", type: "MemberExpression", line: 1, column: 4, endLine: 1, endColumn: 5 }] | |
227 | }, | |
228 | { | |
229 | code: "(obj).\nproperty", | |
230 | output: "(obj)\n.property", | |
231 | options: ["property"], | |
232 | errors: [{ messageId: "expectedDotBeforeProperty", type: "MemberExpression", line: 1, column: 6 }] | |
233 | }, | |
234 | { | |
235 | code: "5\n.toExponential()", | |
236 | output: "5 .\ntoExponential()", | |
237 | options: ["object"], | |
238 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }] | |
239 | }, | |
240 | { | |
241 | code: "-5\n.toExponential()", | |
242 | output: "-5 .\ntoExponential()", | |
243 | options: ["object"], | |
244 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }] | |
245 | }, | |
6f036462 TL |
246 | { |
247 | code: "01\n.toExponential()", | |
248 | output: "01.\ntoExponential()", | |
249 | options: ["object"], | |
250 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }] | |
251 | }, | |
252 | { | |
253 | code: "08\n.toExponential()", | |
254 | output: "08 .\ntoExponential()", | |
255 | options: ["object"], | |
256 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }] | |
257 | }, | |
258 | { | |
259 | code: "0190\n.toExponential()", | |
260 | output: "0190 .\ntoExponential()", | |
261 | options: ["object"], | |
262 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }] | |
263 | }, | |
264 | { | |
265 | code: "5_000\n.toExponential()", | |
266 | output: "5_000 .\ntoExponential()", | |
267 | options: ["object"], | |
268 | parserOptions: { ecmaVersion: 2021 }, | |
269 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }] | |
270 | }, | |
271 | { | |
272 | code: "5_000_00\n.toExponential()", | |
273 | output: "5_000_00 .\ntoExponential()", | |
274 | options: ["object"], | |
275 | parserOptions: { ecmaVersion: 2021 }, | |
276 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }] | |
277 | }, | |
278 | { | |
279 | code: "5.000_000\n.toExponential()", | |
280 | output: "5.000_000.\ntoExponential()", | |
281 | options: ["object"], | |
282 | parserOptions: { ecmaVersion: 2021 }, | |
283 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }] | |
284 | }, | |
285 | { | |
286 | code: "0b1010_1010\n.toExponential()", | |
287 | output: "0b1010_1010.\ntoExponential()", | |
288 | options: ["object"], | |
289 | parserOptions: { ecmaVersion: 2021 }, | |
290 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }] | |
291 | }, | |
eb39fafa DC |
292 | { |
293 | code: "foo /* a */ . /* b */ \n /* c */ bar", | |
294 | output: "foo /* a */ /* b */ \n /* c */ .bar", | |
295 | options: ["property"], | |
296 | errors: [{ messageId: "expectedDotBeforeProperty", type: "MemberExpression", line: 1, column: 13 }] | |
297 | }, | |
298 | { | |
299 | code: "foo /* a */ \n /* b */ . /* c */ bar", | |
300 | output: "foo. /* a */ \n /* b */ /* c */ bar", | |
301 | options: ["object"], | |
302 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 10 }] | |
303 | }, | |
304 | { | |
305 | code: "f(a\n)\n.prop", | |
306 | output: "f(a\n).\nprop", | |
307 | options: ["object"], | |
308 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 1 }] | |
309 | }, | |
310 | { | |
311 | code: "`\n`\n.prop", | |
312 | output: "`\n`.\nprop", | |
313 | options: ["object"], | |
314 | parserOptions: { ecmaVersion: 6 }, | |
315 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 1 }] | |
316 | }, | |
317 | ||
318 | // https://github.com/eslint/eslint/issues/11868 (also in valid) | |
319 | { | |
320 | code: "(a\n)\n.prop", | |
321 | output: "(a\n).\nprop", | |
322 | options: ["object"], | |
323 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 1 }] | |
324 | }, | |
325 | { | |
326 | code: "(a\n)\n.\nprop", | |
327 | output: "(a\n).\n\nprop", | |
328 | options: ["object"], | |
329 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 1 }] | |
330 | }, | |
331 | { | |
332 | code: "(f(a)\n)\n.prop", | |
333 | output: "(f(a)\n).\nprop", | |
334 | options: ["object"], | |
335 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 1 }] | |
336 | }, | |
337 | { | |
338 | code: "(f(a\n)\n)\n.prop", | |
339 | output: "(f(a\n)\n).\nprop", | |
340 | options: ["object"], | |
341 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 4, column: 1 }] | |
342 | }, | |
343 | { | |
344 | code: "((obj\n))\n.prop", | |
345 | output: "((obj\n)).\nprop", | |
346 | options: ["object"], | |
347 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 1 }] | |
348 | }, | |
349 | { | |
350 | code: "((obj\n)\n)\n.prop", | |
351 | output: "((obj\n)\n).\nprop", | |
352 | options: ["object"], | |
353 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 4, column: 1 }] | |
354 | }, | |
355 | { | |
356 | code: "(a\n) /* a */ \n.prop", | |
357 | output: "(a\n). /* a */ \nprop", | |
358 | options: ["object"], | |
359 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 1 }] | |
360 | }, | |
361 | { | |
362 | code: "(a\n)\n/* a */\n.prop", | |
363 | output: "(a\n).\n/* a */\nprop", | |
364 | options: ["object"], | |
365 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 4, column: 1 }] | |
366 | }, | |
367 | { | |
368 | code: "(a\n)\n/* a */.prop", | |
369 | output: "(a\n).\n/* a */prop", | |
370 | options: ["object"], | |
371 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 8 }] | |
372 | }, | |
373 | { | |
374 | code: "(5)\n.toExponential()", | |
375 | output: "(5).\ntoExponential()", | |
376 | options: ["object"], | |
377 | errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }] | |
6f036462 TL |
378 | }, |
379 | ||
380 | // Optional chaining | |
381 | { | |
382 | code: "obj\n?.prop", | |
383 | output: "obj?.\nprop", | |
384 | options: ["object"], | |
385 | parserOptions: { ecmaVersion: 2020 }, | |
386 | errors: [{ messageId: "expectedDotAfterObject" }] | |
387 | }, | |
388 | { | |
389 | code: "10\n?.prop", | |
390 | output: "10?.\nprop", | |
391 | options: ["object"], | |
392 | parserOptions: { ecmaVersion: 2020 }, | |
393 | errors: [{ messageId: "expectedDotAfterObject" }] | |
394 | }, | |
395 | { | |
396 | code: "obj?.\nprop", | |
397 | output: "obj\n?.prop", | |
398 | options: ["property"], | |
399 | parserOptions: { ecmaVersion: 2020 }, | |
400 | errors: [{ messageId: "expectedDotBeforeProperty" }] | |
609c276f TL |
401 | }, |
402 | ||
403 | // Private properties | |
404 | { | |
405 | code: "class C { #a; foo() { this\n.#a; } }", | |
406 | output: "class C { #a; foo() { this.\n#a; } }", | |
407 | options: ["object"], | |
408 | parserOptions: { ecmaVersion: 2022 }, | |
409 | errors: [{ messageId: "expectedDotAfterObject" }] | |
410 | }, | |
411 | { | |
412 | code: "class C { #a; foo() { this.\n#a; } }", | |
413 | output: "class C { #a; foo() { this\n.#a; } }", | |
414 | options: ["property"], | |
415 | parserOptions: { ecmaVersion: 2022 }, | |
416 | errors: [{ messageId: "expectedDotBeforeProperty" }] | |
eb39fafa DC |
417 | } |
418 | ] | |
419 | }); |