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 all 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
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