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 Closes import-js#627
  • Loading branch information
giodamelio committed Nov 15, 2016
1 parent bfdc2bb commit 5c1a3d1
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- Properly report [`newline-after-import`] when next line is a decorator
- Fixed documentation for the default values for the [`order`] rule ([#601])

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

## [2.0.1] - 2016-10-06
### Fixed
- Fixed code that relied on removed dependencies. ([#604])
Expand Down Expand Up @@ -428,6 +431,8 @@ for info on changes for earlier releases.

[#660]: https://github.com/benmosher/eslint-plugin-import/issues/660
[#653]: https://github.com/benmosher/eslint-plugin-import/issues/653
[#628]: https://github.com/benmosher/eslint-plugin-import/issues/628
[#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
[#602]: https://github.com/benmosher/eslint-plugin-import/issues/602
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 5c1a3d1

Please sign in to comment.