From e1c505440432412bc3e3c89db5bd106053775695 Mon Sep 17 00:00:00 2001 From: Sebastian Werner Date: Thu, 6 Jun 2019 15:23:38 +0200 Subject: [PATCH 1/3] Added support for correctly ordering unknown types e.g. custom aliases --- docs/rules/order.md | 4 +- src/rules/order.js | 4 +- tests/src/rules/order.js | 87 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) diff --git a/docs/rules/order.md b/docs/rules/order.md index 45bde6acc..ab25cf6b1 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 @@ -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: diff --git a/src/rules/order.js b/src/rules/order.js index 685671bfc..71787a0bc 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -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 @@ -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..5ef91a66b 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -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: ` @@ -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: ` From 4827e727e3d792a8a72f6ff0013da214e3cbfa7e Mon Sep 17 00:00:00 2001 From: Sebastian Werner Date: Thu, 6 Jun 2019 15:26:10 +0200 Subject: [PATCH 2/3] Added changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) 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 From d81a5c824f19095be1733fe332286d224eea2f4c Mon Sep 17 00:00:00 2001 From: Sebastian Werner Date: Tue, 11 Jun 2019 09:14:13 +0200 Subject: [PATCH 3/3] fix(ordering): changed default groups to not contain unknown --- docs/rules/order.md | 2 +- src/rules/order.js | 2 +- tests/src/rules/order.js | 14 ++++++++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/docs/rules/order.md b/docs/rules/order.md index ab25cf6b1..88ddca46f 100644 --- a/docs/rules/order.md +++ b/docs/rules/order.md @@ -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", "unknown", "parent", "sibling", "index"]`. +The default value is `["builtin", "external", "parent", "sibling", "index"]`. You can set the options like this: diff --git a/src/rules/order.js b/src/rules/order.js index 71787a0bc..3d3e1b96b 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -4,7 +4,7 @@ import importType from '../core/importType' import isStaticRequire from '../core/staticRequire' import docsUrl from '../docsUrl' -const defaultGroups = ['builtin', 'external', 'unknown', 'parent', 'sibling', 'index'] +const defaultGroups = ['builtin', 'external', 'parent', 'sibling', 'index'] // REPORTING AND FIXING diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index 5ef91a66b..b310dd07f 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -164,16 +164,19 @@ ruleTester.run('order', rule, { var index = require('./'); `, }), - // Using unknown import types (e.g. using an resolver alias via babel) + // 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 - // a custom group list. + // an alternative custom group list. test({ code: ` import { Input } from '-/components/Input'; @@ -197,6 +200,7 @@ ruleTester.run('order', rule, { options: [ { 'newlines-between': 'always', + groups: ['builtin', 'external', 'unknown', 'parent', 'sibling', 'index'], }, ], }), @@ -933,6 +937,11 @@ ruleTester.run('order', rule, { import { Button } from '-/components/Button'; import { add } from './helper'; `, + options: [ + { + groups: ['builtin', 'external', 'unknown', 'parent', 'sibling', 'index'], + }, + ], errors: [ { line: 4, @@ -958,6 +967,7 @@ ruleTester.run('order', rule, { `, options: [ { + groups: ['builtin', 'external', 'unknown', 'parent', 'sibling', 'index'], 'newlines-between': 'always', }, ],