Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newline-after-import with decorators #595

Merged
merged 3 commits into from
Oct 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
},
],
})