Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for correctly ordering unknown types e.g. custom aliases #1375

Merged
merged 3 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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"]`.
ljharb marked this conversation as resolved.
Show resolved Hide resolved

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