Skip to content

Commit eabec08

Browse files
committed
Add 'always-with-newlines' value to order's newlines-between option to allow newlines inside import groups Closes #627
1 parent d9605a0 commit eabec08

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

Diff for: CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
77
### Fixed
88
- [`prefer-default-export`] handles re-exported default exports ([#609])
99

10+
### Added
11+
- Add new value to `order`'s `newlines-between` option to allow newlines inside import groups ([#627], [#628])
12+
1013
## [2.0.1] - 2016-10-06
1114
### Fixed
1215
- Fixed code that relied on removed dependencies. ([#604])
@@ -400,6 +403,8 @@ for info on changes for earlier releases.
400403
[#157]: https://github.com/benmosher/eslint-plugin-import/pull/157
401404
[#314]: https://github.com/benmosher/eslint-plugin-import/pull/314
402405

406+
[#628]: https://github.com/benmosher/eslint-plugin-import/issues/628
407+
[#627]: https://github.com/benmosher/eslint-plugin-import/issues/627
403408
[#609]: https://github.com/benmosher/eslint-plugin-import/issues/609
404409
[#604]: https://github.com/benmosher/eslint-plugin-import/issues/604
405410
[#577]: https://github.com/benmosher/eslint-plugin-import/issues/577

Diff for: docs/rules/order.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,14 @@ You can set the options like this:
9292
"import/order": ["error", {"groups": ["index", "sibling", "parent", "internal", "external", "builtin"]}]
9393
```
9494

95-
### `newlines-between: [ignore|always|never]`:
95+
### `newlines-between: [ignore|always|always-with-newlines|never]`:
9696

9797

9898
Enforces or forbids new lines between import groups:
9999

100100
- If set to `ignore`, no errors related to new lines between import groups will be reported (default).
101101
- 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.
102+
- If set to `always-with-newlines`, it will act like `always` except newlines are allowed inside import groups.
102103
- If set to `never`, no new lines are allowed in the entire import section.
103104

104105
With the default group setting, the following will be invalid:
@@ -111,6 +112,15 @@ import index from './';
111112
import sibling from './foo';
112113
```
113114

115+
```js
116+
/* eslint import/order: ["error", {"newlines-between": "always-with-newlines"}] */
117+
import fs from 'fs';
118+
119+
import path from 'path';
120+
import index from './';
121+
import sibling from './foo';
122+
```
123+
114124
```js
115125
/* eslint import/order: ["error", {"newlines-between": "never"}] */
116126
import fs from 'fs';
@@ -133,6 +143,17 @@ import index from './';
133143
import sibling from './foo';
134144
```
135145

146+
```js
147+
/* eslint import/order: ["error", {"newlines-between": "always-with-newlines"}] */
148+
import fs from 'fs';
149+
150+
import path from 'path';
151+
152+
import index from './';
153+
154+
import sibling from './foo';
155+
```
156+
136157
```js
137158
/* eslint import/order: ["error", {"newlines-between": "never"}] */
138159
import fs from 'fs';

Diff for: src/rules/order.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,24 @@ function makeNewlinesBetweenReport (context, imported, newlinesBetweenImports) {
120120
let previousImport = imported[0]
121121

122122
imported.slice(1).forEach(function(currentImport) {
123-
if (newlinesBetweenImports === 'always') {
124-
if (currentImport.rank !== previousImport.rank
125-
&& getNumberOfEmptyLinesBetween(currentImport, previousImport) === 0)
123+
const emptyLinesBetween = getNumberOfEmptyLinesBetween(currentImport, previousImport)
124+
125+
if (newlinesBetweenImports === 'always' || newlinesBetweenImports === 'always-with-newlines') {
126+
if (currentImport.rank !== previousImport.rank && emptyLinesBetween === 0)
126127
{
127128
context.report(
128129
previousImport.node, 'There should be at least one empty line between import groups'
129130
)
130131
} else if (currentImport.rank === previousImport.rank
131-
&& getNumberOfEmptyLinesBetween(currentImport, previousImport) > 0)
132+
&& emptyLinesBetween > 0
133+
&& newlinesBetweenImports !== 'always-with-newlines')
132134
{
133135
context.report(
134136
previousImport.node, 'There should be no empty line within import group'
135137
)
136138
}
137139
} else {
138-
if (getNumberOfEmptyLinesBetween(currentImport, previousImport) > 0) {
140+
if (emptyLinesBetween > 0) {
139141
context.report(previousImport.node, 'There should be no empty line between import groups')
140142
}
141143
}
@@ -156,7 +158,12 @@ module.exports = {
156158
type: 'array',
157159
},
158160
'newlines-between': {
159-
enum: [ 'ignore', 'always', 'never' ],
161+
enum: [
162+
'ignore',
163+
'always',
164+
'always-with-newlines',
165+
'never',
166+
],
160167
},
161168
},
162169
additionalProperties: false,

Diff for: tests/src/rules/order.js

+26
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,32 @@ ruleTester.run('order', rule, {
376376
`,
377377
options: [{ 'newlines-between': 'always' }]
378378
}),
379+
// Option: newlines-between: 'always-with-newlines'
380+
test({
381+
code: `
382+
var fs = require('fs');
383+
var path = require('path');
384+
385+
var util = require('util');
386+
387+
var async = require('async');
388+
389+
var relParent1 = require('../foo');
390+
var relParent2 = require('../');
391+
392+
var relParent3 = require('../bar');
393+
394+
var sibling = require('./foo');
395+
var sibling2 = require('./bar');
396+
397+
var sibling3 = require('./foobar');
398+
`,
399+
options: [
400+
{
401+
'newlines-between': 'always-with-newlines',
402+
},
403+
],
404+
}),
379405
],
380406
invalid: [
381407
// builtin before external module (require)

0 commit comments

Comments
 (0)