Skip to content

Commit

Permalink
Add 'always-with-newlines' value to order's newlines-between option t…
Browse files Browse the repository at this point in the history
…o allow newlines inside import groups
  • Loading branch information
giodamelio committed Oct 16, 2016
1 parent d9605a0 commit bfd6222
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Fixed
- [`prefer-default-export`] handles re-exported default exports ([#609])

### Added
- Add new value to `order`'s `newlines-between` option to allow newlines inside import groups ([#627])

## [2.0.1] - 2016-10-06
### Fixed
- Fixed code that relied on removed dependencies. ([#604])
Expand Down Expand Up @@ -400,6 +403,7 @@ for info on changes for earlier releases.
[#157]: https://github.com/benmosher/eslint-plugin-import/pull/157
[#314]: https://github.com/benmosher/eslint-plugin-import/pull/314

[#627]: https://github.com/benmosher/eslint-plugin-import/issues/627
[#609]: https://github.com/benmosher/eslint-plugin-import/issues/609
[#604]: https://github.com/benmosher/eslint-plugin-import/issues/604
[#577]: https://github.com/benmosher/eslint-plugin-import/issues/577
Expand Down
23 changes: 22 additions & 1 deletion docs/rules/order.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ You can set the options like this:
"import/order": ["error", {"groups": ["index", "sibling", "parent", "internal", "external", "builtin"]}]
```

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


Enforces or forbids new lines between import groups:

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

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

```js
/* eslint import/order: ["error", {"newlines-between": "always-with-newlines"}] */
import fs from 'fs';

import path from 'path';
import index from './';
import sibling from './foo';
```

```js
/* eslint import/order: ["error", {"newlines-between": "never"}] */
import fs from 'fs';
Expand All @@ -133,6 +143,17 @@ import index from './';
import sibling from './foo';
```

```js
/* eslint import/order: ["error", {"newlines-between": "always-with-newlines"}] */
import fs from 'fs';

import path from 'path';

import index from './';

import sibling from './foo';
```

```js
/* eslint import/order: ["error", {"newlines-between": "never"}] */
import fs from 'fs';
Expand Down
19 changes: 13 additions & 6 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,24 @@ function makeNewlinesBetweenReport (context, imported, newlinesBetweenImports) {
let previousImport = imported[0]

imported.slice(1).forEach(function(currentImport) {
if (newlinesBetweenImports === 'always') {
if (currentImport.rank !== previousImport.rank
&& getNumberOfEmptyLinesBetween(currentImport, previousImport) === 0)
const emptyLinesBetween = getNumberOfEmptyLinesBetween(currentImport, previousImport)

if (newlinesBetweenImports === 'always' || newlinesBetweenImports === 'always-with-newlines') {
if (currentImport.rank !== previousImport.rank && emptyLinesBetween === 0)
{
context.report(
previousImport.node, 'There should be at least one empty line between import groups'
)
} else if (currentImport.rank === previousImport.rank
&& getNumberOfEmptyLinesBetween(currentImport, previousImport) > 0)
&& emptyLinesBetween > 0
&& newlinesBetweenImports !== 'always-with-newlines')
{
context.report(
previousImport.node, 'There should be no empty line within import group'
)
}
} else {
if (getNumberOfEmptyLinesBetween(currentImport, previousImport) > 0) {
if (emptyLinesBetween > 0) {
context.report(previousImport.node, 'There should be no empty line between import groups')
}
}
Expand All @@ -156,7 +158,12 @@ module.exports = {
type: 'array',
},
'newlines-between': {
enum: [ 'ignore', 'always', 'never' ],
enum: [
'ignore',
'always',
'always-with-newlines',
'never',
],
},
},
additionalProperties: false,
Expand Down
26 changes: 26 additions & 0 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,32 @@ ruleTester.run('order', rule, {
`,
options: [{ 'newlines-between': 'always' }]
}),
// Option: newlines-between: 'always-with-newlines'
test({
code: `
var fs = require('fs');
var path = require('path');
var util = require('util');
var async = require('async');
var relParent1 = require('../foo');
var relParent2 = require('../');
var relParent3 = require('../bar');
var sibling = require('./foo');
var sibling2 = require('./bar');
var sibling3 = require('./foobar');
`,
options: [
{
'newlines-between': 'always-with-newlines',
},
],
}),
],
invalid: [
// builtin before external module (require)
Expand Down

0 comments on commit bfd6222

Please sign in to comment.