Skip to content

Commit 8065847

Browse files
authored
v1 into master! (#9)
* 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
1 parent dd59bb9 commit 8065847

19 files changed

+2181
-1498
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
lib
2+
lib
3+
test-results

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
test
22
src
33
docs
4-
node_modules
4+
node_modules
5+
test-results

.prettierrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
2-
"printWidth": 100,
2+
"printWidth": 120,
33
"arrowParens": "always",
44
"semi": true,
55
"singleQuote": true,
66
"useTabs": true,
77
"tabWidth": 4,
8-
"endOfLine": "lf"
8+
"endOfLine": "lf",
9+
"trailingComma": "es5"
910
}

docs/rules/order-imports.md

Lines changed: 77 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,120 @@
1-
# Enforce a convention in module import order
1+
# Enforce a _configurable_ convention in module import order
22

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:
44

55
```js
6-
// 1. node "builtins"
6+
// 1. "absolute" path modules
7+
import abs from '/absolute-module'; // uncommon
8+
// 2. all non-relative and non-absolute "modules"
79
import fs from 'fs';
810
import path from 'path';
9-
// 2. "external" modules
1011
import _ from 'lodash';
1112
import chalk from 'chalk';
12-
// 3. "internal" modules
13-
// (non-relative modules that aren't found in your `node_modules` folder (or specified external-modules folder)
1413
import foo from 'src/foo';
15-
// 4. modules from a "parent" directory
14+
// 3. modules from a "parent" directory
1615
import foo from '../foo';
1716
import qux from '../../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
1918
import bar from './bar';
2019
import baz from './bar/baz';
21-
// 6. "index" of the current directory
20+
// 5. "index" of the current directory
2221
import main from './';
2322
```
2423

25-
Unassigned imports are ignored, as the order they are imported in may be important.
24+
Notes:
2625

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.
2828

29-
## Fail
29+
## Usage
3030

31-
```js
32-
import _ from 'lodash';
33-
import path from '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-
import foo from './foo'; // `import` statements must be before `require` statement
44-
```
45-
46-
## Pass
31+
To use the rule, update your `eslint` config.
4732

4833
```js
49-
import path from '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-
import foo from './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+
}
6852
```
6953

7054
## Options
7155

7256
This rule supports the following options:
7357

74-
### `groups: [array]`:
58+
### `groups: Array<string | Array<string>>`:
59+
60+
> The default value is `["absolute", "module", "parent", "sibling", "index"]`.
7561
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:
7763

78-
- `"builtin"`, `"external"`, `"internal"`, `"parent"`, `"sibling"`, `"index"`
79-
- or a regular expression like string: `/^shared/` (wrapped in `/`).
64+
- `'module'` | `'absolute'` | `'parent'` | `'sibling'` | `'index'`
65+
- or a regular expression like string, ex: `/^shared/`
66+
- the wrapping `/` is essential
8067
- in this example, it would match any import paths starting with `'shared'`
68+
- note: files are first categorized as matching a regular expression group before going into another group
8169

8270
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:
8371

8472
```js
8573
[
86-
'builtin', // Built-in types are first
74+
'absolute', // any absolute path modules are first (ex: `/path/to/code.ts`)
75+
'module', // then normal modules (ex: `lodash/pull`)
8776
['sibling', 'parent'], // Then sibling and parent types. They can be mingled together
8877
'/^shared/', // any import paths starting with 'shared'
89-
'index' // Then the index file
90-
// Then the rest: internal and external type
78+
'index', // Then the index file
9179
];
9280
```
9381

94-
The default value is `["builtin", "external", "parent", "sibling", "index"]`.
95-
9682
You can set the options like this:
9783

9884
```js
9985
"import-helpers/order-imports": [
10086
"error",
101-
{"groups": ["index", "sibling", "parent", "/core/", "internal", "external", "builtin"]}
87+
{"groups": [ 'module', '/^@shared/', ['parent', 'sibling', 'index'] ]}
10288
]
10389
```
10490

105-
### `newlines-between: [ignore|always|never]`:
91+
### `newlinesBetween: [ignore|always|always-and-inside-groups|never]`:
10692

10793
Enforces or forbids new lines between import groups:
10894

10995
- If set to `ignore`, no errors related to new lines between import groups will be reported (default).
11096
- 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.
11198
- If set to `never`, no new lines are allowed in the entire import section.
11299

113-
With the default group setting, the following will be invalid:
100+
With the default group setting, the following will be valid:
114101

115102
```js
116-
/* eslint import-helpers/order-imports: ["error", {"newlines-between": "always"}] */
117-
import fs from 'fs';
118-
import path from 'path';
119-
import index from './';
120-
import sibling from './foo';
121-
```
122-
123-
```js
124-
/* eslint import-helpers/order-imports: ["error", {"newlines-between": "never"}] */
103+
/* eslint import-helpers/order-imports: ["error", {"newlinesBetween": "always"}] */
125104
import fs from 'fs';
126105
import path from 'path';
127106

128-
import index from './';
129-
130107
import sibling from './foo';
131-
```
132-
133-
while those will be valid:
134-
135-
```js
136-
/* eslint import-helpers/order-imports: ["error", {"newlines-between": "always"}] */
137-
import fs from 'fs';
138-
import path from 'path';
139108

140109
import index from './';
141-
142-
import sibling from './foo';
143110
```
144111

145112
```js
146-
/* eslint import-helpers/order-imports: ["error", {"newlines-between": "never"}] */
113+
/* eslint import-helpers/order-imports: ["error", {"newlinesBetween": "never"}] */
147114
import fs from 'fs';
148115
import path from 'path';
149-
import index from './';
150116
import sibling from './foo';
117+
import index from './';
151118
```
152119

153120
### `alphabetize: object`:
@@ -161,11 +128,19 @@ Example setting:
161128

162129
```js
163130
alphabetize: {
164-
order: 'asc', /* sort in ascending order. Options: ['ignore', 'asc', 'desc'] */
165-
ignoreCase: false, /* case-sensitive. This property does not have any effect if 'order' is set to 'ignore' */
131+
order: 'asc', /* sort in ascending order. Options: ['ignore', 'asc', 'desc'] */
132+
ignoreCase: false, /* case-sensitive. This property does not have any effect if 'order' is set to 'ignore' */
166133
}
167134
```
168135

136+
This will pass:
137+
138+
```js
139+
import Baz from 'Baz';
140+
import bar from 'bar';
141+
import foo from 'foo';
142+
```
143+
169144
This will fail the rule check:
170145

171146
```js
@@ -174,16 +149,26 @@ import bar from 'bar';
174149
import Baz from 'Baz';
175150
```
176151

177-
While this will pass:
152+
## Upgrading from v0.14 to v1
178153

179-
```js
180-
import Baz from 'Baz';
181-
import bar from 'bar';
182-
import foo from 'foo';
154+
### `builtin` | `external` | `internal``module`
155+
156+
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.
159+
160+
```javascript
161+
// v0.14
162+
groups: ['builtin', 'sibling'];
163+
// v1
164+
groups: [
165+
'/^(assert|async_hooks|buffer|child_process|cluster|console|constants|crypto|dgram|dns|domain|events|fs|http|http2|https|inspector|module|net|os|path|perf_hooks|process|punycode|querystring|readline|repl|stream|string_decoder|timers|tls|trace_events|tty|url|util|v8|vm|zli)/',
166+
'sibling',
167+
];
183168
```
184169

185-
## Related
170+
If you want to keep the same `internal`/`external` functionality, create a custom regular expression group with your modules names.
186171

187-
- [`import/external-module-folders`] setting
172+
### `'newlines-between' → 'newlinesBetween'`
188173

189-
[`import/external-module-folders`]: ../../README.md#importexternal-module-folders
174+
In v1, the `newLinesBetween` configuration option is now in camel case.

gulpfile.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const gulp = require('gulp');
2-
const babel = require('gulp-babel');
2+
const ts = require('gulp-typescript');
33
const rimraf = require('rimraf');
44

5-
const SRC = 'src/**/*.js';
5+
const SRC = 'src/**/?(*.js|*.ts)';
66
const DEST = 'lib';
7+
const tsProject = ts.createProject('tsconfig.json');
78

89
gulp.task('clean', function(done) {
910
rimraf(DEST, done);
@@ -12,7 +13,7 @@ gulp.task('clean', function(done) {
1213
gulp.task('src', ['clean'], function() {
1314
return gulp
1415
.src(SRC)
15-
.pipe(babel())
16+
.pipe(tsProject())
1617
.pipe(gulp.dest(DEST));
1718
});
1819

package.json

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"name": "eslint-plugin-import-helpers",
3-
"version": "0.1.4",
3+
"version": "1.0.0-0",
44
"description": "ESLint Rules to Aid with Imports",
55
"main": "lib/index.js",
66
"scripts": {
7-
"prepublish": "gulp prepublish",
8-
"test": "node ./test/run-all"
7+
"build": "gulp src",
8+
"prepublish": "npm run build",
9+
"test": "npm run build && npm run test-quick",
10+
"test-quick": "NODE_PATH=./lib nyc -s mocha -R dot --recursive test -t test-results"
911
},
1012
"keywords": [
1113
"eslint",
@@ -22,18 +24,15 @@
2224
"eslint": "2.x - 5.x"
2325
},
2426
"devDependencies": {
25-
"@babel/core": "^7.4.0",
27+
"@types/node": "^11.12.2",
2628
"@typescript-eslint/parser": "^1.5.0",
2729
"eslint": "^5.15.3",
2830
"gulp": "^3.9.0",
29-
"gulp-babel": "6.1.2",
31+
"gulp-typescript": "^5.0.1",
32+
"mocha": "^6.1.2",
33+
"nyc": "^13.3.0",
3034
"rimraf": "2.5.2",
31-
"typescript": "^3.3.4000"
35+
"typescript": "^3.4.1"
3236
},
33-
"dependencies": {
34-
"builtin-modules": "^1.1.1",
35-
"eslint-import-resolver-node": "^0.3.2",
36-
"eslint-module-utils": "^2.3.0",
37-
"lodash.cond": "^4.5.2"
38-
}
37+
"dependencies": {}
3938
}

0 commit comments

Comments
 (0)