Skip to content

Commit

Permalink
Added support for correctly ordering unknown types e.g. custom aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
swernerx committed Jun 6, 2019
1 parent 15e5c61 commit e1c5054
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/rules/order.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ This rule supports the following options:

### `groups: [array]`:

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:
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:
```js
[
'builtin', // Built-in types are first
Expand All @@ -86,7 +86,7 @@ How groups are defined, and the order to respect. `groups` must be an array of `
// Then the rest: internal and external type
]
```
The default value is `["builtin", "external", "parent", "sibling", "index"]`.
The default value is `["builtin", "external", "unknown", "parent", "sibling", "index"]`.

You can set the options like this:

Expand Down
4 changes: 2 additions & 2 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import importType from '../core/importType'
import isStaticRequire from '../core/staticRequire'
import docsUrl from '../docsUrl'

const defaultGroups = ['builtin', 'external', 'parent', 'sibling', 'index']
const defaultGroups = ['builtin', 'external', 'unknown', 'parent', 'sibling', 'index']

// REPORTING AND FIXING

Expand Down Expand Up @@ -259,7 +259,7 @@ function isInVariableDeclarator(node) {
(node.type === 'VariableDeclarator' || isInVariableDeclarator(node.parent))
}

const types = ['builtin', 'external', 'internal', 'parent', 'sibling', 'index']
const types = ['builtin', 'external', 'internal', 'unknown', 'parent', 'sibling', 'index']

// Creates an object with type-rank pairs.
// Example: { index: 0, sibling: 1, parent: 1, external: 1, builtin: 2, internal: 2 }
Expand Down
87 changes: 87 additions & 0 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,42 @@ ruleTester.run('order', rule, {
var index = require('./');
`,
}),
// Using unknown import types (e.g. using an resolver alias via babel)
test({
code: `
import fs from 'fs';
import { Input } from '-/components/Input';
import { Button } from '-/components/Button';
import { add } from './helper';`,
}),
// Using unknown import types (e.g. using an resolver alias via babel) with
// a custom group list.
test({
code: `
import { Input } from '-/components/Input';
import { Button } from '-/components/Button';
import fs from 'fs';
import { add } from './helper';`,
options: [{
groups: [ 'unknown', 'builtin', 'external', 'parent', 'sibling', 'index' ],
}],
}),
// Using unknown import types (e.g. using an resolver alias via babel)
// Option: newlines-between: 'always'
test({
code: `
import fs from 'fs';
import { Input } from '-/components/Input';
import { Button } from '-/components/Button';
import { add } from './helper';`,
options: [
{
'newlines-between': 'always',
},
],
}),
// Option: newlines-between: 'always'
test({
code: `
Expand Down Expand Up @@ -885,6 +921,57 @@ ruleTester.run('order', rule, {
message: '`fs` import should occur after import of `../foo/bar`',
}],
}),
// Default order using import with custom import alias
test({
code: `
import { Button } from '-/components/Button';
import { add } from './helper';
import fs from 'fs';
`,
output: `
import fs from 'fs';
import { Button } from '-/components/Button';
import { add } from './helper';
`,
errors: [
{
line: 4,
message: '`fs` import should occur before import of `-/components/Button`',
},
],
}),
// Default order using import with custom import alias
test({
code: `
import fs from 'fs';
import { Button } from '-/components/Button';
import { LinkButton } from '-/components/Link';
import { add } from './helper';
`,
output: `
import fs from 'fs';
import { Button } from '-/components/Button';
import { LinkButton } from '-/components/Link';
import { add } from './helper';
`,
options: [
{
'newlines-between': 'always',
},
],
errors: [
{
line: 2,
message: 'There should be at least one empty line between import groups',
},
{
line: 4,
message: 'There should be at least one empty line between import groups',
},
],
}),
// Option newlines-between: 'never' - should report unnecessary line between groups
test({
code: `
Expand Down

0 comments on commit e1c5054

Please sign in to comment.