Skip to content

Commit

Permalink
Merge branch 'master' into feature-no-self-import
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Feb 16, 2017
2 parents d25cdd9 + b5a962f commit e695506
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 60 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
- Add [`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])
- Add [`no-self-import`] rule: forbids a module from importing itself. ([#727], [#449], [#447], thanks [@giodamelio]).

### Changed
Expand Down Expand Up @@ -383,6 +384,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
[#727]: https://github.com/benmosher/eslint-plugin-import/pull/727
[#712]: https://github.com/benmosher/eslint-plugin-import/pull/712
[#680]: https://github.com/benmosher/eslint-plugin-import/pull/680
Expand Down Expand Up @@ -575,3 +577,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
2 changes: 1 addition & 1 deletion docs/rules/named.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Redux's npm module includes this key, and thereby is lintable, for example.

A module path that is [ignored] or not [unambiguously an ES module] will not be reported when imported.

[ignored]: ../README.md#importignore
[ignored]: ../../README.md#importignore
[unambiguously an ES module]: https://github.com/bmeck/UnambiguousJavaScriptGrammar


Expand Down
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.
2 changes: 1 addition & 1 deletion docs/rules/no-named-as-default.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import foo from './foo.js';
import bar from './foo.js';
```

For post-ES2015 `export` extensions, this also prevents exporting the default from a referenced module as a name within than module, for the same reasons:
For post-ES2015 `export` extensions, this also prevents exporting the default from a referenced module as a name within that module, for the same reasons:

```js
// valid:
Expand Down
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 e695506

Please sign in to comment.