Skip to content

Commit

Permalink
Add count option to newline-after-import (#742)
Browse files Browse the repository at this point in the history
* Add `newlines` option to `newline-after-import`

* Add tests for `newline-after-import` option and fix linter errors in test file

* Fix broken tests, add docs

* Update CHANGELOG

* Add schema and further testing for `newlines` option on `newline-after-import`

* Change `newlines` option to `count` for `newline-after-import` rule

* Fix missing plural in docs

* Add links in changelog and thank @ntdb :)

* Add links to rules in changelog
  • Loading branch information
ntdb authored and jfmengels committed Feb 16, 2017
1 parent 0800951 commit b5a962f
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 58 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
## [Unreleased]
### Added
- [`no-anonymous-default-export`] rule: report anonymous default exports ([#712], thanks [@duncanbeevers]).
- Add new value to `order`'s `newlines-between` option to allow newlines inside import groups ([#627], [#628], thanks [@giodamelio])
- Add new value to [`order`]'s `newlines-between` option to allow newlines inside import groups ([#627], [#628], thanks [@giodamelio])
- Add `count` option to the [`newline-after-import`] rule to allow configuration of number of newlines expected ([#742], thanks [@ntdb])

### Changed
- [`no-extraneous-dependencies`]: use `read-pkg-up` to simplify finding + loading `package.json` ([#680], thanks [@wtgtybhertgeghgtwtg])
Expand Down Expand Up @@ -382,6 +383,7 @@ for info on changes for earlier releases.
[`unambiguous`]: ./docs/rules/unambiguous.md
[`no-anonymous-default-export`]: ./docs/rules/no-anonymous-default-export.md

[#742]: https://github.com/benmosher/eslint-plugin-import/pull/742
[#712]: https://github.com/benmosher/eslint-plugin-import/pull/712
[#680]: https://github.com/benmosher/eslint-plugin-import/pull/680
[#654]: https://github.com/benmosher/eslint-plugin-import/pull/654
Expand Down Expand Up @@ -571,3 +573,4 @@ for info on changes for earlier releases.
[@wtgtybhertgeghgtwtg]: https://github.com/wtgtybhertgeghgtwtg
[@duncanbeevers]: https://github.com/duncanbeevers
[@giodamelio]: https://github.com/giodamelio
[@ntdb]: https://github.com/ntdb
40 changes: 38 additions & 2 deletions docs/rules/newline-after-import.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# newline-after-import

Enforces having an empty line after the last top-level import statement or require call.
Enforces having one or more empty lines after the last top-level import statement or require call.

## Rule Details

This rule has one option, `count` which sets the number of newlines that are enforced after the last top-level import statement or require call. This option defaults to `1`.

Valid:

```js
Expand All @@ -26,7 +28,7 @@ const BAR = require('./bar')
const BAZ = 1
```

...whereas here imports will be reported:
Invalid:

```js
import * as foo from 'foo'
Expand All @@ -46,6 +48,40 @@ const BAZ = 1
const BAR = require('./bar')
```

With `count` set to `2` this will be considered valid:

```js
import defaultExport from './foo'


const FOO = 'BAR'
```

With `count` set to `2` these will be considered invalid:

```js
import defaultExport from './foo'
const FOO = 'BAR'
```

```js
import defaultExport from './foo'

const FOO = 'BAR'
```


## Example options usage
```
{
...
"rules": {
"import/newline-after-import": [{ "count": 2 }]
}
}
```


## When Not To Use It

If you like to visually group module imports with its usage, you don't want to use this rule.
15 changes: 14 additions & 1 deletion src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ function isClassWithDecorator(node) {
module.exports = {
meta: {
docs: {},
schema: [
{
'type': 'object',
'properties': {
'count': {
'type': 'integer',
'minimum': 1,
},
},
'additionalProperties': false,
},
],
},
create: function (context) {
let level = 0
Expand All @@ -55,7 +67,8 @@ module.exports = {
nextNode = nextNode.decorators[0]
}

if (getLineDifference(node, nextNode) < 2) {
const options = context.options[0] || { count: 1 }
if (getLineDifference(node, nextNode) < options.count + 1) {
let column = node.loc.start.column

if (node.loc.start.line !== node.loc.end.line) {
Expand Down
Loading

0 comments on commit b5a962f

Please sign in to comment.