From 37c66e3b0a462da6be828a5bcfab8c84fb5a03b6 Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Fri, 30 Sep 2016 13:37:29 -0700 Subject: [PATCH 1/3] Fix #592: newline-after-import crash with decorator --- src/rules/newline-after-import.js | 2 ++ tests/src/rules/newline-after-import.js | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/src/rules/newline-after-import.js b/src/rules/newline-after-import.js index 3eb692198..4e6981a6d 100644 --- a/src/rules/newline-after-import.js +++ b/src/rules/newline-after-import.js @@ -116,11 +116,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..442b8ff74 100644 --- a/tests/src/rules/newline-after-import.js +++ b/tests/src/rules/newline-after-import.js @@ -128,6 +128,14 @@ 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', + }, ], invalid: [ From 3603907d9850522b19fc0d96443479f89569eeae Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Fri, 30 Sep 2016 13:39:01 -0700 Subject: [PATCH 2/3] properly report newline-after-import when decorator is next line --- src/rules/newline-after-import.js | 7 +++++++ tests/src/rules/newline-after-import.js | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/rules/newline-after-import.js b/src/rules/newline-after-import.js index 4e6981a6d..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 diff --git a/tests/src/rules/newline-after-import.js b/tests/src/rules/newline-after-import.js index 442b8ff74..afd942ef0 100644 --- a/tests/src/rules/newline-after-import.js +++ b/tests/src/rules/newline-after-import.js @@ -136,6 +136,11 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), { parserOptions: { sourceType: 'module' }, parser: 'babel-eslint', }, + { + code: "var foo = require('foo');\n\n@SomeDecorator(foo)\nclass Foo {}", + parserOptions: { sourceType: 'module' }, + parser: 'babel-eslint', + }, ], invalid: [ @@ -256,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', + }, ], }) From 4e11e80de2bd796213938c27196b26ecc0f5cc65 Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Fri, 30 Sep 2016 13:42:08 -0700 Subject: [PATCH 3/3] update changelog for #592 --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) 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