You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* rename new-lines-between to newLinesBetween (closes#1)
* remove lodash.cond
* add docs url
* clean up comments
* change build from babel to typescript. convert import-type to typescript. Remove "external", "internal", and "builtin" types. Just use "module".
* update static-require to ts.
* rename resolveImportType to determineImportType
* convert order-imports into typescript as possible. (ignoring eslint source specific types)
* improve test output. update all valid tests to match updated API. remove dependencies.
* update readme to remove instructions on no longe relevant things
* fix 200 lines of tests
* fix more tests. add trailing commas to prettier settings to stop going crazy
* fix all tests
* 1.0.0-0
* update documenation and include absolute in the default groups
* improved migration docs
# Enforce a _configurable_convention in module import order
2
2
3
-
Enforce a convention in the order of `require()` / `import` statements. The order is as shown in the following example:
3
+
Enforce a convention in the order of `require()` / `import` statements. The default order is as shown in the following example:
4
4
5
5
```js
6
-
// 1. node "builtins"
6
+
// 1. "absolute" path modules
7
+
importabsfrom'/absolute-module'; // uncommon
8
+
// 2. all non-relative and non-absolute "modules"
7
9
importfsfrom'fs';
8
10
importpathfrom'path';
9
-
// 2. "external" modules
10
11
import_from'lodash';
11
12
importchalkfrom'chalk';
12
-
// 3. "internal" modules
13
-
// (non-relative modules that aren't found in your `node_modules` folder (or specified external-modules folder)
14
13
importfoofrom'src/foo';
15
-
//4. modules from a "parent" directory
14
+
//3. modules from a "parent" directory
16
15
importfoofrom'../foo';
17
16
importquxfrom'../../foo/qux';
18
-
//5. "sibling" modules from the same or a sibling's directory
17
+
//4. "sibling" modules from the same or a sibling's directory
19
18
importbarfrom'./bar';
20
19
importbazfrom'./bar/baz';
21
-
//6. "index" of the current directory
20
+
//5. "index" of the current directory
22
21
importmainfrom'./';
23
22
```
24
23
25
-
Unassigned imports are ignored, as the order they are imported in may be important.
24
+
Notes:
26
25
27
-
Statements using the ES6 `import` syntax must appear before any `require()` statements.
26
+
- Unassigned imports are ignored (ex: `import 'polyfill'`), as the order they are imported in may be important.
27
+
- Statements using the ES6 `import` syntax must appear before any `require()` statements.
28
28
29
-
## Fail
29
+
## Usage
30
30
31
-
```js
32
-
import_from'lodash';
33
-
importpathfrom'path'; // `path` import should occur before import of `lodash`
34
-
35
-
// -----
36
-
37
-
var _ =require('lodash');
38
-
var path =require('path'); // `path` import should occur before import of `lodash`
39
-
40
-
// -----
41
-
42
-
var path =require('path');
43
-
importfoofrom'./foo'; // `import` statements must be before `require` statement
44
-
```
45
-
46
-
## Pass
31
+
To use the rule, update your `eslint` config.
47
32
48
33
```js
49
-
importpathfrom'path';
50
-
import_from'lodash';
51
-
52
-
// -----
53
-
54
-
var path =require('path');
55
-
var _ =require('lodash');
56
-
57
-
// -----
58
-
59
-
// Allowed as ̀`babel-register` is not assigned.
60
-
require('babel-register');
61
-
var path =require('path');
62
-
63
-
// -----
64
-
65
-
// Allowed as `import` must be before `require`
66
-
importfoofrom'./foo';
67
-
var path =require('path');
34
+
{
35
+
// .eslintrc.js
36
+
plugins: ['eslint-plugin-import-helpers'],
37
+
rules: {
38
+
'import-helpers/order-imports': [
39
+
'warn',
40
+
{ // example configuration
41
+
newlinesBetween:'always',
42
+
groups: [
43
+
'module',
44
+
'/^@shared/',
45
+
['parent', 'sibling', 'index'],
46
+
],
47
+
alphabetize: { order:'asc', ignoreCase:true },
48
+
},
49
+
],
50
+
}
51
+
}
68
52
```
69
53
70
54
## Options
71
55
72
56
This rule supports the following options:
73
57
74
-
### `groups: [array]`:
58
+
### `groups: Array<string | Array<string>>`:
59
+
60
+
> The default value is `["absolute", "module", "parent", "sibling", "index"]`.
75
61
76
-
How groups are defined, and the order to respect. `groups`must be an array of `string` or [`string`]. The `string` must either be one of:
62
+
Groups dictates how the imports should be grouped and it what order. `groups`is an array. Each value in the array must be a valid string or an array of valid strings. The valid strings are:
Enforces or forbids new lines between import groups:
108
94
109
95
- If set to `ignore`, no errors related to new lines between import groups will be reported (default).
110
96
- If set to `always`, at least one new line between each group will be enforced, and new lines inside a group will be forbidden. To prevent multiple lines between imports, core `no-multiple-empty-lines` rule can be used.
97
+
- If set to `always-and-inside-groups`, at least one new line between each import statement will be enforced.
111
98
- If set to `never`, no new lines are allowed in the entire import section.
112
99
113
-
With the default group setting, the following will be invalid:
100
+
With the default group setting, the following will be valid:
In v1, `builtin`, `external`, `internal` have all been combined into one group, `module`. This simplifies the logic for this rule and makes it so it ONLY looks at the import strings and doesn't attempt any module resolution itself. The same functionality can be accomplished using regular expression groups.
157
+
158
+
If you want to keep the same `builtin` functionality, create a custom regular expression group to replace it, like so.
0 commit comments