Skip to content

Commit 45bfe47

Browse files
authored
Merge pull request #1375 from swernerx/fix/order-unknown-types
[New] Added support for correctly ordering `unknown` types e.g. custom aliases
2 parents 15e5c61 + d81a5c8 commit 45bfe47

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
55

66
## [Unreleased]
77

8+
- [`order`]: Adds support for correctly sorting unknown types into a single group (thanks [@swernerx])
9+
810
## [2.17.3] - 2019-05-23
911

1012
### Fixed

docs/rules/order.md

+1-1
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

src/rules/order.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -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

+97
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,46 @@ ruleTester.run('order', rule, {
164164
var index = require('./');
165165
`,
166166
}),
167+
// Addijg unknown import types (e.g. using an resolver alias via babel) to the groups.
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+
options: [{
175+
groups: ['builtin', 'external', 'unknown', 'parent', 'sibling', 'index'],
176+
}],
177+
}),
178+
// Using unknown import types (e.g. using an resolver alias via babel) with
179+
// an alternative custom group list.
180+
test({
181+
code: `
182+
import { Input } from '-/components/Input';
183+
import { Button } from '-/components/Button';
184+
import fs from 'fs';
185+
import { add } from './helper';`,
186+
options: [{
187+
groups: [ 'unknown', 'builtin', 'external', 'parent', 'sibling', 'index' ],
188+
}],
189+
}),
190+
// Using unknown import types (e.g. using an resolver alias via babel)
191+
// Option: newlines-between: 'always'
192+
test({
193+
code: `
194+
import fs from 'fs';
195+
196+
import { Input } from '-/components/Input';
197+
import { Button } from '-/components/Button';
198+
199+
import { add } from './helper';`,
200+
options: [
201+
{
202+
'newlines-between': 'always',
203+
groups: ['builtin', 'external', 'unknown', 'parent', 'sibling', 'index'],
204+
},
205+
],
206+
}),
167207
// Option: newlines-between: 'always'
168208
test({
169209
code: `
@@ -885,6 +925,63 @@ ruleTester.run('order', rule, {
885925
message: '`fs` import should occur after import of `../foo/bar`',
886926
}],
887927
}),
928+
// Default order using import with custom import alias
929+
test({
930+
code: `
931+
import { Button } from '-/components/Button';
932+
import { add } from './helper';
933+
import fs from 'fs';
934+
`,
935+
output: `
936+
import fs from 'fs';
937+
import { Button } from '-/components/Button';
938+
import { add } from './helper';
939+
`,
940+
options: [
941+
{
942+
groups: ['builtin', 'external', 'unknown', 'parent', 'sibling', 'index'],
943+
},
944+
],
945+
errors: [
946+
{
947+
line: 4,
948+
message: '`fs` import should occur before import of `-/components/Button`',
949+
},
950+
],
951+
}),
952+
// Default order using import with custom import alias
953+
test({
954+
code: `
955+
import fs from 'fs';
956+
import { Button } from '-/components/Button';
957+
import { LinkButton } from '-/components/Link';
958+
import { add } from './helper';
959+
`,
960+
output: `
961+
import fs from 'fs';
962+
963+
import { Button } from '-/components/Button';
964+
import { LinkButton } from '-/components/Link';
965+
966+
import { add } from './helper';
967+
`,
968+
options: [
969+
{
970+
groups: ['builtin', 'external', 'unknown', 'parent', 'sibling', 'index'],
971+
'newlines-between': 'always',
972+
},
973+
],
974+
errors: [
975+
{
976+
line: 2,
977+
message: 'There should be at least one empty line between import groups',
978+
},
979+
{
980+
line: 4,
981+
message: 'There should be at least one empty line between import groups',
982+
},
983+
],
984+
}),
888985
// Option newlines-between: 'never' - should report unnecessary line between groups
889986
test({
890987
code: `

0 commit comments

Comments
 (0)