From a0851c310ffc685855718fa3bd52af642cc42618 Mon Sep 17 00:00:00 2001 From: lilling Date: Tue, 2 Feb 2021 22:01:16 +0200 Subject: [PATCH] [Fix] `newline-after-import`: respect decorator annotations Fixes #1784. --- CHANGELOG.md | 3 +++ src/rules/newline-after-import.js | 6 ++++- tests/src/rules/newline-after-import.js | 30 ++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb347894fd..f54e6c07ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel - [`extensions`]/[`no-cycle`]/[`no-extraneous-dependencies`]: Correct module real path resolution ([#1696], thanks [@paztis]) - [`no-named-default`]: ignore Flow import type and typeof ([#1983], thanks [@christianvuerings]) - [`no-extraneous-dependencies`]: Exclude flow `typeof` imports ([#1534], thanks [@devongovett]) +- [`newline-after-import`]: respect decorator annotations ([#1985], thanks [@lilling]) ### Changed - [Generic Import Callback] Make callback for all imports once in rules ([#1237], thanks [@ljqx]) @@ -765,6 +766,7 @@ for info on changes for earlier releases. [#2021]: https://github.com/benmosher/eslint-plugin-import/pull/2021 [#1997]: https://github.com/benmosher/eslint-plugin-import/pull/1997 [#1993]: https://github.com/benmosher/eslint-plugin-import/pull/1993 +[#1985]: https://github.com/benmosher/eslint-plugin-import/pull/1985 [#1983]: https://github.com/benmosher/eslint-plugin-import/pull/1983 [#1974]: https://github.com/benmosher/eslint-plugin-import/pull/1974 [#1958]: https://github.com/benmosher/eslint-plugin-import/pull/1958 @@ -1350,3 +1352,4 @@ for info on changes for earlier releases. [@dwardu]: https://github.com/dwardu [@s-h-a-d-o-w]: https://github.com/s-h-a-d-o-w [@grit96]: https://github.com/grit96 +[@lilling]: https://github.com/lilling diff --git a/src/rules/newline-after-import.js b/src/rules/newline-after-import.js index 1e698f13e5..935572aa47 100644 --- a/src/rules/newline-after-import.js +++ b/src/rules/newline-after-import.js @@ -47,6 +47,10 @@ function isExportDefaultClass(node) { return node.type === 'ExportDefaultDeclaration' && node.declaration.type === 'ClassDeclaration'; } +function isExportNameClass(node) { + return node.type === 'ExportNamedDeclaration' && node.declaration.type === 'ClassDeclaration'; +} + module.exports = { meta: { type: 'layout', @@ -72,7 +76,7 @@ module.exports = { const requireCalls = []; function checkForNewLine(node, nextNode, type) { - if (isExportDefaultClass(nextNode)) { + if (isExportDefaultClass(nextNode) || isExportNameClass(nextNode)) { const classNode = nextNode.declaration; if (isClassWithDecorator(classNode)) { diff --git a/tests/src/rules/newline-after-import.js b/tests/src/rules/newline-after-import.js index 58f5ef1dd8..fbee0f6f22 100644 --- a/tests/src/rules/newline-after-import.js +++ b/tests/src/rules/newline-after-import.js @@ -1,7 +1,7 @@ import { RuleTester } from 'eslint'; import flatMap from 'array.prototype.flatmap'; -import { getTSParsers } from '../utils'; +import { getTSParsers, testVersion } from '../utils'; const IMPORT_ERROR_MESSAGE = 'Expected 1 empty line after import statement not followed by another import.'; const IMPORT_ERROR_MESSAGE_MULTIPLE = (count) => { @@ -234,7 +234,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), { ]), ], - invalid: [ + invalid: [].concat( { code: `import foo from 'foo';\nexport default function() {};`, output: `import foo from 'foo';\n\nexport default function() {};`, @@ -429,5 +429,29 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), { parserOptions: { sourceType: 'module' }, parser: require.resolve('babel-eslint'), }, - ], + testVersion('>= 6', { + code: ` + // issue 1784 + import { map } from 'rxjs/operators'; + @Component({}) + export class Test {} + `, + output: ` + // issue 1784 + import { map } from 'rxjs/operators'; + + @Component({}) + export class Test {} + `, + errors: [ + { + line: 2, + column: 1, + message: IMPORT_ERROR_MESSAGE, + }, + ], + parserOptions: { sourceType: 'module' }, + parser: require.resolve('babel-eslint'), + }), + ), });