]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/sort-imports.js
import 7.12.1 upstream release
[pve-eslint.git] / eslint / tests / lib / rules / sort-imports.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Tests for sort-imports rule.
3 * @author Christian Schuller
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/sort-imports"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
16// Tests
17//------------------------------------------------------------------------------
18
19const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6, sourceType: "module" } }),
20 expectedError = {
21 messageId: "sortImportsAlphabetically",
22 type: "ImportDeclaration"
23 },
24 ignoreCaseArgs = [{ ignoreCase: true }];
25
26ruleTester.run("sort-imports", rule, {
27 valid: [
28 "import a from 'foo.js';\n" +
29 "import b from 'bar.js';\n" +
30 "import c from 'baz.js';\n",
31 "import * as B from 'foo.js';\n" +
32 "import A from 'bar.js';",
33 "import * as B from 'foo.js';\n" +
34 "import {a, b} from 'bar.js';",
35 "import {b, c} from 'bar.js';\n" +
36 "import A from 'foo.js';",
37 {
38 code:
39 "import A from 'bar.js';\n" +
40 "import {b, c} from 'foo.js';",
41 options: [{
42 memberSyntaxSortOrder: ["single", "multiple", "none", "all"]
43 }]
44 },
45 "import {a, b} from 'bar.js';\n" +
46 "import {c, d} from 'foo.js';",
47 "import A from 'foo.js';\n" +
48 "import B from 'bar.js';",
49 "import A from 'foo.js';\n" +
50 "import a from 'bar.js';",
51 "import a, * as b from 'foo.js';\n" +
52 "import c from 'bar.js';",
53 "import 'foo.js';\n" +
54 " import a from 'bar.js';",
55 "import B from 'foo.js';\n" +
56 "import a from 'bar.js';",
57 {
58 code:
59 "import a from 'foo.js';\n" +
60 "import B from 'bar.js';",
61 options: ignoreCaseArgs
62 },
63 "import {a, b, c, d} from 'foo.js';",
64 {
65 code:
66 "import a from 'foo.js';\n" +
67 "import B from 'bar.js';",
68 options: [{
69 ignoreDeclarationSort: true
70 }]
71 },
72 {
73 code: "import {b, A, C, d} from 'foo.js';",
74 options: [{
75 ignoreMemberSort: true
76 }]
77 },
78 {
79 code: "import {B, a, C, d} from 'foo.js';",
80 options: [{
81 ignoreMemberSort: true
82 }]
83 },
84 {
85 code: "import {a, B, c, D} from 'foo.js';",
86 options: ignoreCaseArgs
87 },
88 "import a, * as b from 'foo.js';",
89 "import * as a from 'foo.js';\n" +
90 "\n" +
91 "import b from 'bar.js';",
92 "import * as bar from 'bar.js';\n" +
93 "import * as foo from 'foo.js';",
94
95 // https://github.com/eslint/eslint/issues/5130
96 {
97 code:
98 "import 'foo';\n" +
99 "import bar from 'bar';",
100 options: ignoreCaseArgs
101 },
102
103 // https://github.com/eslint/eslint/issues/5305
6f036462
TL
104 "import React, {Component} from 'react';",
105
106 // allowSeparatedGroups
107 {
108 code: "import b from 'b';\n\nimport a from 'a';",
109 options: [{ allowSeparatedGroups: true }]
110 },
111 {
112 code: "import a from 'a';\n\nimport 'b';",
113 options: [{ allowSeparatedGroups: true }]
114 },
115 {
116 code: "import { b } from 'b';\n\n\nimport { a } from 'a';",
117 options: [{ allowSeparatedGroups: true }]
118 },
119 {
120 code: "import b from 'b';\n// comment\nimport a from 'a';",
121 options: [{ allowSeparatedGroups: true }]
122 },
123 {
124 code: "import b from 'b';\nfoo();\nimport a from 'a';",
125 options: [{ allowSeparatedGroups: true }]
126 },
127 {
128 code: "import { b } from 'b';/*\n comment \n*/import { a } from 'a';",
129 options: [{ allowSeparatedGroups: true }]
130 },
131 {
132 code: "import b from\n'b';\n\nimport\n a from 'a';",
133 options: [{ allowSeparatedGroups: true }]
134 },
135 {
136 code: "import c from 'c';\n\nimport a from 'a';\nimport b from 'b';",
137 options: [{ allowSeparatedGroups: true }]
138 },
139 {
140 code: "import c from 'c';\n\nimport b from 'b';\n\nimport a from 'a';",
141 options: [{ allowSeparatedGroups: true }]
142 }
eb39fafa
DC
143 ],
144 invalid: [
145 {
146 code:
147 "import a from 'foo.js';\n" +
148 "import A from 'bar.js';",
149 output: null,
150 errors: [expectedError]
151 },
152 {
153 code:
154 "import b from 'foo.js';\n" +
155 "import a from 'bar.js';",
156 output: null,
157 errors: [expectedError]
158 },
159 {
160 code:
161 "import {b, c} from 'foo.js';\n" +
162 "import {a, d} from 'bar.js';",
163 output: null,
164 errors: [expectedError]
165 },
166 {
167 code:
168 "import * as foo from 'foo.js';\n" +
169 "import * as bar from 'bar.js';",
170 output: null,
171 errors: [expectedError]
172 },
173 {
174 code:
175 "import a from 'foo.js';\n" +
176 "import {b, c} from 'bar.js';",
177 output: null,
178 errors: [{
179 messageId: "unexpectedSyntaxOrder",
180 data: {
181 syntaxA: "multiple",
182 syntaxB: "single"
183 },
184 type: "ImportDeclaration"
185 }]
186 },
187 {
188 code:
189 "import a from 'foo.js';\n" +
190 "import * as b from 'bar.js';",
191 output: null,
192 errors: [{
193 messageId: "unexpectedSyntaxOrder",
194 data: {
195 syntaxA: "all",
196 syntaxB: "single"
197 },
198 type: "ImportDeclaration"
199 }]
200 },
201 {
202 code:
203 "import a from 'foo.js';\n" +
204 "import 'bar.js';",
205 output: null,
206 errors: [{
207 messageId: "unexpectedSyntaxOrder",
208 data: {
209 syntaxA: "none",
210 syntaxB: "single"
211 },
212 type: "ImportDeclaration"
213 }]
214 },
215 {
216 code:
217 "import b from 'bar.js';\n" +
218 "import * as a from 'foo.js';",
219 output: null,
220 options: [{
221 memberSyntaxSortOrder: ["all", "single", "multiple", "none"]
222 }],
223 errors: [{
224 messageId: "unexpectedSyntaxOrder",
225 data: {
226 syntaxA: "all",
227 syntaxB: "single"
228 },
229 type: "ImportDeclaration"
230 }]
231 },
232 {
233 code: "import {b, a, d, c} from 'foo.js';",
234 output: "import {a, b, c, d} from 'foo.js';",
235 errors: [{
236 messageId: "sortMembersAlphabetically",
237 data: { memberName: "a" },
238 type: "ImportSpecifier"
239 }]
240 },
241 {
242 code:
243 "import {b, a, d, c} from 'foo.js';\n" +
244 "import {e, f, g, h} from 'bar.js';",
245 output:
246 "import {a, b, c, d} from 'foo.js';\n" +
247 "import {e, f, g, h} from 'bar.js';",
248 options: [{
249 ignoreDeclarationSort: true
250 }],
251 errors: [{
252 messageId: "sortMembersAlphabetically",
253 data: { memberName: "a" },
254 type: "ImportSpecifier"
255 }]
256 },
257 {
258 code: "import {a, B, c, D} from 'foo.js';",
259 output: "import {B, D, a, c} from 'foo.js';",
260 errors: [{
261 messageId: "sortMembersAlphabetically",
262 data: { memberName: "B" },
263 type: "ImportSpecifier"
264 }]
265 },
266 {
267 code: "import {zzzzz, /* comment */ aaaaa} from 'foo.js';",
268 output: null, // not fixed due to comment
269 errors: [{
270 messageId: "sortMembersAlphabetically",
271 data: { memberName: "aaaaa" },
272 type: "ImportSpecifier"
273 }]
274 },
275 {
276 code: "import {zzzzz /* comment */, aaaaa} from 'foo.js';",
277 output: null, // not fixed due to comment
278 errors: [{
279 messageId: "sortMembersAlphabetically",
280 data: { memberName: "aaaaa" },
281 type: "ImportSpecifier"
282 }]
283 },
284 {
285 code: "import {/* comment */ zzzzz, aaaaa} from 'foo.js';",
286 output: null, // not fixed due to comment
287 errors: [{
288 messageId: "sortMembersAlphabetically",
289 data: { memberName: "aaaaa" },
290 type: "ImportSpecifier"
291 }]
292 },
293 {
294 code: "import {zzzzz, aaaaa /* comment */} from 'foo.js';",
295 output: null, // not fixed due to comment
296 errors: [{
297 messageId: "sortMembersAlphabetically",
298 data: { memberName: "aaaaa" },
299 type: "ImportSpecifier"
300 }]
301 },
302 {
303 code: `
304 import {
305 boop,
306 foo,
307 zoo,
308 baz as qux,
309 bar,
310 beep
311 } from 'foo.js';
312 `,
313 output: `
314 import {
315 bar,
316 beep,
317 boop,
318 foo,
319 baz as qux,
320 zoo
321 } from 'foo.js';
322 `,
323 errors: [{
324 messageId: "sortMembersAlphabetically",
325 data: { memberName: "qux" },
326 type: "ImportSpecifier"
327 }]
6f036462
TL
328 },
329
330 // allowSeparatedGroups
331 {
332 code: "import b from 'b';\nimport a from 'a';",
333 output: null,
334 errors: [{
335 messageId: "sortImportsAlphabetically",
336 type: "ImportDeclaration"
337 }]
338 },
339 {
340 code: "import b from 'b';\nimport a from 'a';",
341 output: null,
342 options: [{}],
343 errors: [{
344 messageId: "sortImportsAlphabetically",
345 type: "ImportDeclaration"
346 }]
347 },
348 {
349 code: "import b from 'b';\nimport a from 'a';",
350 output: null,
351 options: [{ allowSeparatedGroups: false }],
352 errors: [{
353 messageId: "sortImportsAlphabetically",
354 type: "ImportDeclaration"
355 }]
356 },
357 {
358 code: "import b from 'b';import a from 'a';",
359 output: null,
360 options: [{ allowSeparatedGroups: false }],
361 errors: [{
362 messageId: "sortImportsAlphabetically",
363 type: "ImportDeclaration"
364 }]
365 },
366 {
367 code: "import b from 'b'; /* comment */ import a from 'a';",
368 output: null,
369 options: [{ allowSeparatedGroups: false }],
370 errors: [{
371 messageId: "sortImportsAlphabetically",
372 type: "ImportDeclaration"
373 }]
374 },
375 {
376 code: "import b from 'b'; // comment\nimport a from 'a';",
377 output: null,
378 options: [{ allowSeparatedGroups: false }],
379 errors: [{
380 messageId: "sortImportsAlphabetically",
381 type: "ImportDeclaration"
382 }]
383 },
384 {
385 code: "import b from 'b'; // comment 1\n/* comment 2 */import a from 'a';",
386 output: null,
387 options: [{ allowSeparatedGroups: false }],
388 errors: [{
389 messageId: "sortImportsAlphabetically",
390 type: "ImportDeclaration"
391 }]
392 },
393 {
394 code: "import { b } from 'b'; /* comment line 1 \n comment line 2 */ import { a } from 'a';",
395 output: null,
396 options: [{ allowSeparatedGroups: false }],
397 errors: [{
398 messageId: "sortImportsAlphabetically",
399 type: "ImportDeclaration"
400 }]
401 },
402 {
403 code: "import b\nfrom 'b'; import a\nfrom 'a';",
404 output: null,
405 options: [{ allowSeparatedGroups: false }],
406 errors: [{
407 messageId: "sortImportsAlphabetically",
408 type: "ImportDeclaration"
409 }]
410 },
411 {
412 code: "import { b } from \n'b'; /* comment */ import\n { a } from 'a';",
413 output: null,
414 options: [{ allowSeparatedGroups: false }],
415 errors: [{
416 messageId: "sortImportsAlphabetically",
417 type: "ImportDeclaration"
418 }]
419 },
420 {
421 code: "import { b } from \n'b';\nimport\n { a } from 'a';",
422 output: null,
423 options: [{ allowSeparatedGroups: false }],
424 errors: [{
425 messageId: "sortImportsAlphabetically",
426 type: "ImportDeclaration"
427 }]
428 },
429 {
430 code: "import c from 'c';\n\nimport b from 'b';\nimport a from 'a';",
431 output: null,
432 options: [{ allowSeparatedGroups: true }],
433 errors: [{
434 messageId: "sortImportsAlphabetically",
435 type: "ImportDeclaration",
436 line: 4
437 }]
438 },
439 {
440 code: "import b from 'b';\n\nimport { c, a } from 'c';",
441 output: "import b from 'b';\n\nimport { a, c } from 'c';",
442 options: [{ allowSeparatedGroups: true }],
443 errors: [{
444 messageId: "sortMembersAlphabetically",
445 type: "ImportSpecifier"
446 }]
eb39fafa
DC
447 }
448 ]
449});