]>
Commit | Line | Data |
---|---|---|
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 | ||
12 | const rule = require("../../../lib/rules/sort-imports"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6, sourceType: "module" } }), | |
20 | expectedError = { | |
21 | messageId: "sortImportsAlphabetically", | |
22 | type: "ImportDeclaration" | |
23 | }, | |
24 | ignoreCaseArgs = [{ ignoreCase: true }]; | |
25 | ||
26 | ruleTester.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 | |
104 | "import React, {Component} from 'react';" | |
105 | ], | |
106 | invalid: [ | |
107 | { | |
108 | code: | |
109 | "import a from 'foo.js';\n" + | |
110 | "import A from 'bar.js';", | |
111 | output: null, | |
112 | errors: [expectedError] | |
113 | }, | |
114 | { | |
115 | code: | |
116 | "import b from 'foo.js';\n" + | |
117 | "import a from 'bar.js';", | |
118 | output: null, | |
119 | errors: [expectedError] | |
120 | }, | |
121 | { | |
122 | code: | |
123 | "import {b, c} from 'foo.js';\n" + | |
124 | "import {a, d} from 'bar.js';", | |
125 | output: null, | |
126 | errors: [expectedError] | |
127 | }, | |
128 | { | |
129 | code: | |
130 | "import * as foo from 'foo.js';\n" + | |
131 | "import * as bar from 'bar.js';", | |
132 | output: null, | |
133 | errors: [expectedError] | |
134 | }, | |
135 | { | |
136 | code: | |
137 | "import a from 'foo.js';\n" + | |
138 | "import {b, c} from 'bar.js';", | |
139 | output: null, | |
140 | errors: [{ | |
141 | messageId: "unexpectedSyntaxOrder", | |
142 | data: { | |
143 | syntaxA: "multiple", | |
144 | syntaxB: "single" | |
145 | }, | |
146 | type: "ImportDeclaration" | |
147 | }] | |
148 | }, | |
149 | { | |
150 | code: | |
151 | "import a from 'foo.js';\n" + | |
152 | "import * as b from 'bar.js';", | |
153 | output: null, | |
154 | errors: [{ | |
155 | messageId: "unexpectedSyntaxOrder", | |
156 | data: { | |
157 | syntaxA: "all", | |
158 | syntaxB: "single" | |
159 | }, | |
160 | type: "ImportDeclaration" | |
161 | }] | |
162 | }, | |
163 | { | |
164 | code: | |
165 | "import a from 'foo.js';\n" + | |
166 | "import 'bar.js';", | |
167 | output: null, | |
168 | errors: [{ | |
169 | messageId: "unexpectedSyntaxOrder", | |
170 | data: { | |
171 | syntaxA: "none", | |
172 | syntaxB: "single" | |
173 | }, | |
174 | type: "ImportDeclaration" | |
175 | }] | |
176 | }, | |
177 | { | |
178 | code: | |
179 | "import b from 'bar.js';\n" + | |
180 | "import * as a from 'foo.js';", | |
181 | output: null, | |
182 | options: [{ | |
183 | memberSyntaxSortOrder: ["all", "single", "multiple", "none"] | |
184 | }], | |
185 | errors: [{ | |
186 | messageId: "unexpectedSyntaxOrder", | |
187 | data: { | |
188 | syntaxA: "all", | |
189 | syntaxB: "single" | |
190 | }, | |
191 | type: "ImportDeclaration" | |
192 | }] | |
193 | }, | |
194 | { | |
195 | code: "import {b, a, d, c} from 'foo.js';", | |
196 | output: "import {a, b, c, d} from 'foo.js';", | |
197 | errors: [{ | |
198 | messageId: "sortMembersAlphabetically", | |
199 | data: { memberName: "a" }, | |
200 | type: "ImportSpecifier" | |
201 | }] | |
202 | }, | |
203 | { | |
204 | code: | |
205 | "import {b, a, d, c} from 'foo.js';\n" + | |
206 | "import {e, f, g, h} from 'bar.js';", | |
207 | output: | |
208 | "import {a, b, c, d} from 'foo.js';\n" + | |
209 | "import {e, f, g, h} from 'bar.js';", | |
210 | options: [{ | |
211 | ignoreDeclarationSort: true | |
212 | }], | |
213 | errors: [{ | |
214 | messageId: "sortMembersAlphabetically", | |
215 | data: { memberName: "a" }, | |
216 | type: "ImportSpecifier" | |
217 | }] | |
218 | }, | |
219 | { | |
220 | code: "import {a, B, c, D} from 'foo.js';", | |
221 | output: "import {B, D, a, c} from 'foo.js';", | |
222 | errors: [{ | |
223 | messageId: "sortMembersAlphabetically", | |
224 | data: { memberName: "B" }, | |
225 | type: "ImportSpecifier" | |
226 | }] | |
227 | }, | |
228 | { | |
229 | code: "import {zzzzz, /* comment */ aaaaa} from 'foo.js';", | |
230 | output: null, // not fixed due to comment | |
231 | errors: [{ | |
232 | messageId: "sortMembersAlphabetically", | |
233 | data: { memberName: "aaaaa" }, | |
234 | type: "ImportSpecifier" | |
235 | }] | |
236 | }, | |
237 | { | |
238 | code: "import {zzzzz /* comment */, aaaaa} from 'foo.js';", | |
239 | output: null, // not fixed due to comment | |
240 | errors: [{ | |
241 | messageId: "sortMembersAlphabetically", | |
242 | data: { memberName: "aaaaa" }, | |
243 | type: "ImportSpecifier" | |
244 | }] | |
245 | }, | |
246 | { | |
247 | code: "import {/* comment */ zzzzz, aaaaa} from 'foo.js';", | |
248 | output: null, // not fixed due to comment | |
249 | errors: [{ | |
250 | messageId: "sortMembersAlphabetically", | |
251 | data: { memberName: "aaaaa" }, | |
252 | type: "ImportSpecifier" | |
253 | }] | |
254 | }, | |
255 | { | |
256 | code: "import {zzzzz, aaaaa /* comment */} from 'foo.js';", | |
257 | output: null, // not fixed due to comment | |
258 | errors: [{ | |
259 | messageId: "sortMembersAlphabetically", | |
260 | data: { memberName: "aaaaa" }, | |
261 | type: "ImportSpecifier" | |
262 | }] | |
263 | }, | |
264 | { | |
265 | code: ` | |
266 | import { | |
267 | boop, | |
268 | foo, | |
269 | zoo, | |
270 | baz as qux, | |
271 | bar, | |
272 | beep | |
273 | } from 'foo.js'; | |
274 | `, | |
275 | output: ` | |
276 | import { | |
277 | bar, | |
278 | beep, | |
279 | boop, | |
280 | foo, | |
281 | baz as qux, | |
282 | zoo | |
283 | } from 'foo.js'; | |
284 | `, | |
285 | errors: [{ | |
286 | messageId: "sortMembersAlphabetically", | |
287 | data: { memberName: "qux" }, | |
288 | type: "ImportSpecifier" | |
289 | }] | |
290 | } | |
291 | ] | |
292 | }); |