Skip to content

Commit

Permalink
Merge pull request #1375 from swernerx/fix/order-unknown-types
Browse files Browse the repository at this point in the history
[New] Added support for correctly ordering `unknown` types e.g. custom aliases
  • Loading branch information
ljharb committed Jun 11, 2019
2 parents 15e5c61 + d81a5c8 commit 45bfe47
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## [Unreleased]

- [`order`]: Adds support for correctly sorting unknown types into a single group (thanks [@swernerx])

## [2.17.3] - 2019-05-23

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion 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 Down
2 changes: 1 addition & 1 deletion src/rules/order.js
Original file line number Diff line number Diff line change
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
97 changes: 97 additions & 0 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,46 @@ ruleTester.run('order', rule, {
var index = require('./');
`,
}),
// Addijg unknown import types (e.g. using an resolver alias via babel) to the groups.
test({
code: `
import fs from 'fs';
import { Input } from '-/components/Input';
import { Button } from '-/components/Button';
import { add } from './helper';`,
options: [{
groups: ['builtin', 'external', 'unknown', 'parent', 'sibling', 'index'],
}],
}),
// Using unknown import types (e.g. using an resolver alias via babel) with
// an alternative 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',
groups: ['builtin', 'external', 'unknown', 'parent', 'sibling', 'index'],
},
],
}),
// Option: newlines-between: 'always'
test({
code: `
Expand Down Expand Up @@ -885,6 +925,63 @@ 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';
`,
options: [
{
groups: ['builtin', 'external', 'unknown', 'parent', 'sibling', 'index'],
},
],
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: [
{
groups: ['builtin', 'external', 'unknown', 'parent', 'sibling', 'index'],
'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 45bfe47

Please sign in to comment.