Skip to content

Commit

Permalink
[Fix] newline-after-import: respect decorator annotations
Browse files Browse the repository at this point in the history
Fixes #1784.
  • Loading branch information
lilling authored and ljharb committed Feb 2, 2021
1 parent 1c10e79 commit 359b6e5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)) {
Expand Down
30 changes: 27 additions & 3 deletions tests/src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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() {};`,
Expand Down Expand Up @@ -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: 3,
column: 9,
message: IMPORT_ERROR_MESSAGE,
},
],
parserOptions: { sourceType: 'module' },
parser: require.resolve('babel-eslint'),
})) || [],
),
});

0 comments on commit 359b6e5

Please sign in to comment.