]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/dot-location.js
import 7.12.1 upstream release
[pve-eslint.git] / eslint / tests / lib / rules / dot-location.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Tests for dot-location.
3 * @author Greg Cochard
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/dot-location"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
16// Tests
17//------------------------------------------------------------------------------
18
19const ruleTester = new RuleTester();
20
21ruleTester.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 }
eb39fafa
DC
201 }
202 ],
203 invalid: [
204 {
205 code: "obj\n.property",
206 output: "obj.\nproperty",
207 options: ["object"],
208 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1, endLine: 2, endColumn: 2 }]
209 },
210 {
211 code: "obj.\nproperty",
212 output: "obj\n.property",
213 options: ["property"],
214 errors: [{ messageId: "expectedDotBeforeProperty", type: "MemberExpression", line: 1, column: 4, endLine: 1, endColumn: 5 }]
215 },
216 {
217 code: "(obj).\nproperty",
218 output: "(obj)\n.property",
219 options: ["property"],
220 errors: [{ messageId: "expectedDotBeforeProperty", type: "MemberExpression", line: 1, column: 6 }]
221 },
222 {
223 code: "5\n.toExponential()",
224 output: "5 .\ntoExponential()",
225 options: ["object"],
226 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }]
227 },
228 {
229 code: "-5\n.toExponential()",
230 output: "-5 .\ntoExponential()",
231 options: ["object"],
232 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }]
233 },
6f036462
TL
234 {
235 code: "01\n.toExponential()",
236 output: "01.\ntoExponential()",
237 options: ["object"],
238 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }]
239 },
240 {
241 code: "08\n.toExponential()",
242 output: "08 .\ntoExponential()",
243 options: ["object"],
244 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }]
245 },
246 {
247 code: "0190\n.toExponential()",
248 output: "0190 .\ntoExponential()",
249 options: ["object"],
250 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }]
251 },
252 {
253 code: "5_000\n.toExponential()",
254 output: "5_000 .\ntoExponential()",
255 options: ["object"],
256 parserOptions: { ecmaVersion: 2021 },
257 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }]
258 },
259 {
260 code: "5_000_00\n.toExponential()",
261 output: "5_000_00 .\ntoExponential()",
262 options: ["object"],
263 parserOptions: { ecmaVersion: 2021 },
264 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }]
265 },
266 {
267 code: "5.000_000\n.toExponential()",
268 output: "5.000_000.\ntoExponential()",
269 options: ["object"],
270 parserOptions: { ecmaVersion: 2021 },
271 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }]
272 },
273 {
274 code: "0b1010_1010\n.toExponential()",
275 output: "0b1010_1010.\ntoExponential()",
276 options: ["object"],
277 parserOptions: { ecmaVersion: 2021 },
278 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }]
279 },
eb39fafa
DC
280 {
281 code: "foo /* a */ . /* b */ \n /* c */ bar",
282 output: "foo /* a */ /* b */ \n /* c */ .bar",
283 options: ["property"],
284 errors: [{ messageId: "expectedDotBeforeProperty", type: "MemberExpression", line: 1, column: 13 }]
285 },
286 {
287 code: "foo /* a */ \n /* b */ . /* c */ bar",
288 output: "foo. /* a */ \n /* b */ /* c */ bar",
289 options: ["object"],
290 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 10 }]
291 },
292 {
293 code: "f(a\n)\n.prop",
294 output: "f(a\n).\nprop",
295 options: ["object"],
296 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 1 }]
297 },
298 {
299 code: "`\n`\n.prop",
300 output: "`\n`.\nprop",
301 options: ["object"],
302 parserOptions: { ecmaVersion: 6 },
303 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 1 }]
304 },
305
306 // https://github.com/eslint/eslint/issues/11868 (also in valid)
307 {
308 code: "(a\n)\n.prop",
309 output: "(a\n).\nprop",
310 options: ["object"],
311 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 1 }]
312 },
313 {
314 code: "(a\n)\n.\nprop",
315 output: "(a\n).\n\nprop",
316 options: ["object"],
317 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 1 }]
318 },
319 {
320 code: "(f(a)\n)\n.prop",
321 output: "(f(a)\n).\nprop",
322 options: ["object"],
323 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 1 }]
324 },
325 {
326 code: "(f(a\n)\n)\n.prop",
327 output: "(f(a\n)\n).\nprop",
328 options: ["object"],
329 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 4, column: 1 }]
330 },
331 {
332 code: "((obj\n))\n.prop",
333 output: "((obj\n)).\nprop",
334 options: ["object"],
335 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 1 }]
336 },
337 {
338 code: "((obj\n)\n)\n.prop",
339 output: "((obj\n)\n).\nprop",
340 options: ["object"],
341 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 4, column: 1 }]
342 },
343 {
344 code: "(a\n) /* a */ \n.prop",
345 output: "(a\n). /* a */ \nprop",
346 options: ["object"],
347 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 1 }]
348 },
349 {
350 code: "(a\n)\n/* a */\n.prop",
351 output: "(a\n).\n/* a */\nprop",
352 options: ["object"],
353 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 4, column: 1 }]
354 },
355 {
356 code: "(a\n)\n/* a */.prop",
357 output: "(a\n).\n/* a */prop",
358 options: ["object"],
359 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 3, column: 8 }]
360 },
361 {
362 code: "(5)\n.toExponential()",
363 output: "(5).\ntoExponential()",
364 options: ["object"],
365 errors: [{ messageId: "expectedDotAfterObject", type: "MemberExpression", line: 2, column: 1 }]
6f036462
TL
366 },
367
368 // Optional chaining
369 {
370 code: "obj\n?.prop",
371 output: "obj?.\nprop",
372 options: ["object"],
373 parserOptions: { ecmaVersion: 2020 },
374 errors: [{ messageId: "expectedDotAfterObject" }]
375 },
376 {
377 code: "10\n?.prop",
378 output: "10?.\nprop",
379 options: ["object"],
380 parserOptions: { ecmaVersion: 2020 },
381 errors: [{ messageId: "expectedDotAfterObject" }]
382 },
383 {
384 code: "obj?.\nprop",
385 output: "obj\n?.prop",
386 options: ["property"],
387 parserOptions: { ecmaVersion: 2020 },
388 errors: [{ messageId: "expectedDotBeforeProperty" }]
eb39fafa
DC
389 }
390 ]
391});