Skip to content

Commit e1c5054

Browse files
committed
Added support for correctly ordering unknown types e.g. custom aliases
1 parent 15e5c61 commit e1c5054

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

docs/rules/order.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ This rule supports the following options:
7777

7878
### `groups: [array]`:
7979

80-
How groups are defined, and the order to respect. `groups` must be an array of `string` or [`string`]. The only allowed `string`s are: `"builtin"`, `"external"`, `"internal"`, `"parent"`, `"sibling"`, `"index"`. The enforced order is the same as the order of each element in a group. Omitted types are implicitly grouped together as the last element. Example:
80+
How groups are defined, and the order to respect. `groups` must be an array of `string` or [`string`]. The only allowed `string`s are: `"builtin"`, `"external"`, `"internal"`, `"unknown"`, `"parent"`, `"sibling"`, `"index"`. The enforced order is the same as the order of each element in a group. Omitted types are implicitly grouped together as the last element. Example:
8181
```js
8282
[
8383
'builtin', // Built-in types are first
@@ -86,7 +86,7 @@ How groups are defined, and the order to respect. `groups` must be an array of `
8686
// Then the rest: internal and external type
8787
]
8888
```
89-
The default value is `["builtin", "external", "parent", "sibling", "index"]`.
89+
The default value is `["builtin", "external", "unknown", "parent", "sibling", "index"]`.
9090

9191
You can set the options like this:
9292

src/rules/order.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import importType from '../core/importType'
44
import isStaticRequire from '../core/staticRequire'
55
import docsUrl from '../docsUrl'
66

7-
const defaultGroups = ['builtin', 'external', 'parent', 'sibling', 'index']
7+
const defaultGroups = ['builtin', 'external', 'unknown', 'parent', 'sibling', 'index']
88

99
// REPORTING AND FIXING
1010

@@ -259,7 +259,7 @@ function isInVariableDeclarator(node) {
259259
(node.type === 'VariableDeclarator' || isInVariableDeclarator(node.parent))
260260
}
261261

262-
const types = ['builtin', 'external', 'internal', 'parent', 'sibling', 'index']
262+
const types = ['builtin', 'external', 'internal', 'unknown', 'parent', 'sibling', 'index']
263263

264264
// Creates an object with type-rank pairs.
265265
// Example: { index: 0, sibling: 1, parent: 1, external: 1, builtin: 2, internal: 2 }

tests/src/rules/order.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,42 @@ ruleTester.run('order', rule, {
164164
var index = require('./');
165165
`,
166166
}),
167+
// Using unknown import types (e.g. using an resolver alias via babel)
168+
test({
169+
code: `
170+
import fs from 'fs';
171+
import { Input } from '-/components/Input';
172+
import { Button } from '-/components/Button';
173+
import { add } from './helper';`,
174+
}),
175+
// Using unknown import types (e.g. using an resolver alias via babel) with
176+
// a custom group list.
177+
test({
178+
code: `
179+
import { Input } from '-/components/Input';
180+
import { Button } from '-/components/Button';
181+
import fs from 'fs';
182+
import { add } from './helper';`,
183+
options: [{
184+
groups: [ 'unknown', 'builtin', 'external', 'parent', 'sibling', 'index' ],
185+
}],
186+
}),
187+
// Using unknown import types (e.g. using an resolver alias via babel)
188+
// Option: newlines-between: 'always'
189+
test({
190+
code: `
191+
import fs from 'fs';
192+
193+
import { Input } from '-/components/Input';
194+
import { Button } from '-/components/Button';
195+
196+
import { add } from './helper';`,
197+
options: [
198+
{
199+
'newlines-between': 'always',
200+
},
201+
],
202+
}),
167203
// Option: newlines-between: 'always'
168204
test({
169205
code: `
@@ -885,6 +921,57 @@ ruleTester.run('order', rule, {
885921
message: '`fs` import should occur after import of `../foo/bar`',
886922
}],
887923
}),
924+
// Default order using import with custom import alias
925+
test({
926+
code: `
927+
import { Button } from '-/components/Button';
928+
import { add } from './helper';
929+
import fs from 'fs';
930+
`,
931+
output: `
932+
import fs from 'fs';
933+
import { Button } from '-/components/Button';
934+
import { add } from './helper';
935+
`,
936+
errors: [
937+
{
938+
line: 4,
939+
message: '`fs` import should occur before import of `-/components/Button`',
940+
},
941+
],
942+
}),
943+
// Default order using import with custom import alias
944+
test({
945+
code: `
946+
import fs from 'fs';
947+
import { Button } from '-/components/Button';
948+
import { LinkButton } from '-/components/Link';
949+
import { add } from './helper';
950+
`,
951+
output: `
952+
import fs from 'fs';
953+
954+
import { Button } from '-/components/Button';
955+
import { LinkButton } from '-/components/Link';
956+
957+
import { add } from './helper';
958+
`,
959+
options: [
960+
{
961+
'newlines-between': 'always',
962+
},
963+
],
964+
errors: [
965+
{
966+
line: 2,
967+
message: 'There should be at least one empty line between import groups',
968+
},
969+
{
970+
line: 4,
971+
message: 'There should be at least one empty line between import groups',
972+
},
973+
],
974+
}),
888975
// Option newlines-between: 'never' - should report unnecessary line between groups
889976
test({
890977
code: `

0 commit comments

Comments
 (0)