]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/dot-notation.js
import 7.12.1 upstream release
[pve-eslint.git] / eslint / tests / lib / rules / dot-notation.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Tests for dot-notation rule.
3 * @author Josh Perez
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/dot-notation"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
16// Helpers
17//------------------------------------------------------------------------------
18
19const ruleTester = new RuleTester();
20
21/**
22 * Quote a string in "double quotes" because it’s painful
23 * with a double-quoted string literal
24 * @param {string} str The string to quote
25 * @returns {string} `"${str}"`
26 */
27function q(str) {
28 return `"${str}"`;
29}
30
31ruleTester.run("dot-notation", rule, {
32 valid: [
33 "a.b;",
34 "a.b.c;",
35 "a['12'];",
36 "a[b];",
37 "a[0];",
38 { code: "a.b.c;", options: [{ allowKeywords: false }] },
39 { code: "a.arguments;", options: [{ allowKeywords: false }] },
40 { code: "a.let;", options: [{ allowKeywords: false }] },
41 { code: "a.yield;", options: [{ allowKeywords: false }] },
42 { code: "a.eval;", options: [{ allowKeywords: false }] },
43 { code: "a[0];", options: [{ allowKeywords: false }] },
44 { code: "a['while'];", options: [{ allowKeywords: false }] },
45 { code: "a['true'];", options: [{ allowKeywords: false }] },
46 { code: "a['null'];", options: [{ allowKeywords: false }] },
47 { code: "a[true];", options: [{ allowKeywords: false }] },
48 { code: "a[null];", options: [{ allowKeywords: false }] },
49 { code: "a.true;", options: [{ allowKeywords: true }] },
50 { code: "a.null;", options: [{ allowKeywords: true }] },
51 { code: "a['snake_case'];", options: [{ allowPattern: "^[a-z]+(_[a-z]+)+$" }] },
52 { code: "a['lots_of_snake_case'];", options: [{ allowPattern: "^[a-z]+(_[a-z]+)+$" }] },
53 { code: "a[`time${range}`];", parserOptions: { ecmaVersion: 6 } },
54 { code: "a[`while`];", options: [{ allowKeywords: false }], parserOptions: { ecmaVersion: 6 } },
55 { code: "a[`time range`];", parserOptions: { ecmaVersion: 6 } },
56 "a.true;",
57 "a.null;",
58 "a[undefined];",
59 "a[void 0];",
60 "a[b()];",
61 { code: "a[/(?<zero>0)/];", parserOptions: { ecmaVersion: 2018 } }
62 ],
63 invalid: [
64 {
65 code: "a.true;",
66 output: "a[\"true\"];",
67 options: [{ allowKeywords: false }],
68 errors: [{ messageId: "useBrackets", data: { key: "true" } }]
69 },
70 {
71 code: "a['true'];",
72 output: "a.true;",
73 errors: [{ messageId: "useDot", data: { key: q("true") } }]
74 },
75 {
76 code: "a[`time`];",
77 output: "a.time;",
78 parserOptions: { ecmaVersion: 6 },
79 errors: [{ messageId: "useDot", data: { key: "`time`" } }]
80 },
81 {
82 code: "a[null];",
83 output: "a.null;",
84 errors: [{ messageId: "useDot", data: { key: "null" } }]
85 },
86 {
87 code: "a[true];",
88 output: "a.true;",
89 errors: [{ messageId: "useDot", data: { key: "true" } }]
90 },
91 {
92 code: "a[false];",
93 output: "a.false;",
94 errors: [{ messageId: "useDot", data: { key: "false" } }]
95 },
96 {
97 code: "a['b'];",
98 output: "a.b;",
99 errors: [{ messageId: "useDot", data: { key: q("b") } }]
100 },
101 {
102 code: "a.b['c'];",
103 output: "a.b.c;",
104 errors: [{ messageId: "useDot", data: { key: q("c") } }]
105 },
106 {
107 code: "a['_dangle'];",
108 output: "a._dangle;",
109 options: [{ allowPattern: "^[a-z]+(_[a-z]+)+$" }],
110 errors: [{ messageId: "useDot", data: { key: q("_dangle") } }]
111 },
112 {
113 code: "a['SHOUT_CASE'];",
114 output: "a.SHOUT_CASE;",
115 options: [{ allowPattern: "^[a-z]+(_[a-z]+)+$" }],
116 errors: [{ messageId: "useDot", data: { key: q("SHOUT_CASE") } }]
117 },
118 {
119 code:
120 "a\n" +
121 " ['SHOUT_CASE'];",
122 output:
123 "a\n" +
124 " .SHOUT_CASE;",
125 errors: [{
126 messageId: "useDot",
127 data: { key: q("SHOUT_CASE") },
128 line: 2,
129 column: 4
130 }]
131 },
132 {
133 code:
134 "getResource()\n" +
135 " .then(function(){})\n" +
136 " [\"catch\"](function(){})\n" +
137 " .then(function(){})\n" +
138 " [\"catch\"](function(){});",
139 output:
140 "getResource()\n" +
141 " .then(function(){})\n" +
142 " .catch(function(){})\n" +
143 " .then(function(){})\n" +
144 " .catch(function(){});",
145 errors: [
146 {
147 messageId: "useDot",
148 data: { key: q("catch") },
149 line: 3,
150 column: 6
151 },
152 {
153 messageId: "useDot",
154 data: { key: q("catch") },
155 line: 5,
156 column: 6
157 }
158 ]
159 },
160 {
161 code:
162 "foo\n" +
163 " .while;",
164 output:
165 "foo\n" +
166 " [\"while\"];",
167 options: [{ allowKeywords: false }],
168 errors: [{ messageId: "useBrackets", data: { key: "while" } }]
169 },
170 {
171 code: "foo[ /* comment */ 'bar' ]",
172 output: null, // Not fixed due to comment
173 errors: [{ messageId: "useDot", data: { key: q("bar") } }]
174 },
175 {
176 code: "foo[ 'bar' /* comment */ ]",
177 output: null, // Not fixed due to comment
178 errors: [{ messageId: "useDot", data: { key: q("bar") } }]
179 },
180 {
181 code: "foo[ 'bar' ];",
182 output: "foo.bar;",
183 errors: [{ messageId: "useDot", data: { key: q("bar") } }]
184 },
185 {
186 code: "foo. /* comment */ while",
187 output: null, // Not fixed due to comment
188 options: [{ allowKeywords: false }],
189 errors: [{ messageId: "useBrackets", data: { key: "while" } }]
190 },
191 {
192 code: "foo[('bar')]",
193 output: "foo.bar",
194 errors: [{ messageId: "useDot", data: { key: q("bar") } }]
195 },
196 {
197 code: "foo[(null)]",
198 output: "foo.null",
199 errors: [{ messageId: "useDot", data: { key: "null" } }]
200 },
201 {
202 code: "(foo)['bar']",
203 output: "(foo).bar",
204 errors: [{ messageId: "useDot", data: { key: q("bar") } }]
205 },
206 {
207 code: "1['toString']",
208 output: "1 .toString",
209 errors: [{ messageId: "useDot", data: { key: q("toString") } }]
210 },
211 {
212 code: "foo['bar']instanceof baz",
213 output: "foo.bar instanceof baz",
214 errors: [{ messageId: "useDot", data: { key: q("bar") } }]
215 },
216 {
217 code: "let.if()",
218 output: null, // `let["if"]()` is a syntax error because `let[` indicates a destructuring variable declaration
219 options: [{ allowKeywords: false }],
220 errors: [{ messageId: "useBrackets", data: { key: "if" } }]
6f036462
TL
221 },
222 {
223 code: "5['prop']",
224 output: "5 .prop",
225 errors: [{ messageId: "useDot", data: { key: q("prop") } }]
226 },
227 {
228 code: "-5['prop']",
229 output: "-5 .prop",
230 errors: [{ messageId: "useDot", data: { key: q("prop") } }]
231 },
232 {
233 code: "01['prop']",
234 output: "01.prop",
235 errors: [{ messageId: "useDot", data: { key: q("prop") } }]
236 },
237 {
238 code: "01234567['prop']",
239 output: "01234567.prop",
240 errors: [{ messageId: "useDot", data: { key: q("prop") } }]
241 },
242 {
243 code: "08['prop']",
244 output: "08 .prop",
245 errors: [{ messageId: "useDot", data: { key: q("prop") } }]
246 },
247 {
248 code: "090['prop']",
249 output: "090 .prop",
250 errors: [{ messageId: "useDot", data: { key: q("prop") } }]
251 },
252 {
253 code: "018['prop']",
254 output: "018 .prop",
255 errors: [{ messageId: "useDot", data: { key: q("prop") } }]
256 },
257 {
258 code: "5_000['prop']",
259 output: "5_000 .prop",
260 parserOptions: { ecmaVersion: 2021 },
261 errors: [{ messageId: "useDot", data: { key: q("prop") } }]
262 },
263 {
264 code: "5_000_00['prop']",
265 output: "5_000_00 .prop",
266 parserOptions: { ecmaVersion: 2021 },
267 errors: [{ messageId: "useDot", data: { key: q("prop") } }]
268 },
269 {
270 code: "5.000_000['prop']",
271 output: "5.000_000.prop",
272 parserOptions: { ecmaVersion: 2021 },
273 errors: [{ messageId: "useDot", data: { key: q("prop") } }]
274 },
275 {
276 code: "0b1010_1010['prop']",
277 output: "0b1010_1010.prop",
278 parserOptions: { ecmaVersion: 2021 },
279 errors: [{ messageId: "useDot", data: { key: q("prop") } }]
280 },
281
282 // Optional chaining
283 {
284 code: "obj?.['prop']",
285 output: "obj?.prop",
286 parserOptions: { ecmaVersion: 2020 },
287 errors: [{ messageId: "useDot", data: { key: q("prop") } }]
288 },
289 {
290 code: "0?.['prop']",
291 output: "0?.prop",
292 parserOptions: { ecmaVersion: 2020 },
293 errors: [{ messageId: "useDot", data: { key: q("prop") } }]
294 },
295 {
296 code: "obj?.true",
297 output: "obj?.[\"true\"]",
298 options: [{ allowKeywords: false }],
299 parserOptions: { ecmaVersion: 2020 },
300 errors: [{ messageId: "useBrackets", data: { key: "true" } }]
301 },
302 {
303 code: "let?.true",
304 output: "let?.[\"true\"]",
305 options: [{ allowKeywords: false }],
306 parserOptions: { ecmaVersion: 2020 },
307 errors: [{ messageId: "useBrackets", data: { key: "true" } }]
eb39fafa
DC
308 }
309 ]
310});