diff --git a/CHANGELOG.md b/CHANGELOG.md index e9a08b114..1f3c66cf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/rules/order.md b/docs/rules/order.md index 45bde6acc..88ddca46f 100644 --- a/docs/rules/order.md +++ b/docs/rules/order.md @@ -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 diff --git a/src/rules/order.js b/src/rules/order.js index 685671bfc..3d3e1b96b 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -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 } diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index 477ed8a7a..b310dd07f 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -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: ` @@ -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: `