diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ce7ac773..e96ef5f3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/src/rules/newline-after-import.js b/src/rules/newline-after-import.js index 3eb692198..3198a9579 100644 --- a/src/rules/newline-after-import.js +++ b/src/rules/newline-after-import.js @@ -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: { @@ -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 @@ -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, } }, } diff --git a/tests/src/rules/newline-after-import.js b/tests/src/rules/newline-after-import.js index ed9d6de9b..afd942ef0 100644 --- a/tests/src/rules/newline-after-import.js +++ b/tests/src/rules/newline-after-import.js @@ -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: [ @@ -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', + }, ], })