Skip to content

Commit

Permalink
newline-after-import with decorators (#595)
Browse files Browse the repository at this point in the history
* Fix #592: newline-after-import crash with decorator

* properly report newline-after-import when decorator is next line

* update changelog for #592
  • Loading branch information
lukekarrys authored and benmosher committed Oct 22, 2016
1 parent 34bf9bd commit de0dbe5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
## [Unreleased]
### Fixed
- [`prefer-default-export`] handles re-exported default exports ([#609])
- Fix crash when using `newline-after-import` with decorators ([#592])
- Properly report `newline-after-import` when next line is a decorator

## [2.0.1] - 2016-10-06
### Fixed
Expand Down Expand Up @@ -402,6 +404,7 @@ for info on changes for earlier releases.

[#609]: https://github.com/benmosher/eslint-plugin-import/issues/609
[#604]: https://github.com/benmosher/eslint-plugin-import/issues/604
[#592]: https://github.com/benmosher/eslint-plugin-import/issues/592
[#577]: https://github.com/benmosher/eslint-plugin-import/issues/577
[#570]: https://github.com/benmosher/eslint-plugin-import/issues/570
[#567]: https://github.com/benmosher/eslint-plugin-import/issues/567
Expand Down
9 changes: 9 additions & 0 deletions src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ function getLineDifference(node, nextNode) {
return nextNode.loc.start.line - node.loc.end.line
}

function isClassWithDecorator(node) {
return node.type === 'ClassDeclaration' && node.decorators && node.decorators.length
}

module.exports = {
meta: {
Expand All @@ -48,6 +51,10 @@ module.exports = {
const requireCalls = []

function checkForNewLine(node, nextNode, type) {
if (isClassWithDecorator(nextNode)) {
nextNode = nextNode.decorators[0]
}

if (getLineDifference(node, nextNode) < 2) {
let column = node.loc.start.column

Expand Down Expand Up @@ -116,11 +123,13 @@ module.exports = {
ArrowFunctionExpression: incrementLevel,
BlockStatement: incrementLevel,
ObjectExpression: incrementLevel,
Decorator: incrementLevel,
'FunctionDeclaration:exit': decrementLevel,
'FunctionExpression:exit': decrementLevel,
'ArrowFunctionExpression:exit': decrementLevel,
'BlockStatement:exit': decrementLevel,
'ObjectExpression:exit': decrementLevel,
'Decorator:exit': decrementLevel,
}
},
}
33 changes: 33 additions & 0 deletions tests/src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
`,
parserOptions: { sourceType: 'module' },
},
{
code: `//issue 592
@SomeDecorator(require('./some-file'))
export default class App {}
`,
parserOptions: { sourceType: 'module' },
parser: 'babel-eslint',
},
{
code: "var foo = require('foo');\n\n@SomeDecorator(foo)\nclass Foo {}",
parserOptions: { sourceType: 'module' },
parser: 'babel-eslint',
},
],

invalid: [
Expand Down Expand Up @@ -248,5 +261,25 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
} ],
parserOptions: { sourceType: 'module' },
},
{
code: "import foo from 'foo';\n@SomeDecorator(foo)\nclass Foo {}",
errors: [ {
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE,
} ],
parserOptions: { sourceType: 'module' },
parser: 'babel-eslint',
},
{
code: "var foo = require('foo');\n@SomeDecorator(foo)\nclass Foo {}",
errors: [ {
line: 1,
column: 1,
message: REQUIRE_ERROR_MESSAGE,
} ],
parserOptions: { sourceType: 'module' },
parser: 'babel-eslint',
},
],
})

0 comments on commit de0dbe5

Please sign in to comment.