Skip to content

Commit 359b6e5

Browse files
lillingljharb
authored andcommitted
[Fix] newline-after-import: respect decorator annotations
Fixes #1784.
1 parent 1c10e79 commit 359b6e5

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
2727
- [`extensions`]/[`no-cycle`]/[`no-extraneous-dependencies`]: Correct module real path resolution ([#1696], thanks [@paztis])
2828
- [`no-named-default`]: ignore Flow import type and typeof ([#1983], thanks [@christianvuerings])
2929
- [`no-extraneous-dependencies`]: Exclude flow `typeof` imports ([#1534], thanks [@devongovett])
30+
- [`newline-after-import`]: respect decorator annotations ([#1985], thanks [@lilling])
3031

3132
### Changed
3233
- [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.
765766
[#2021]: https://github.com/benmosher/eslint-plugin-import/pull/2021
766767
[#1997]: https://github.com/benmosher/eslint-plugin-import/pull/1997
767768
[#1993]: https://github.com/benmosher/eslint-plugin-import/pull/1993
769+
[#1985]: https://github.com/benmosher/eslint-plugin-import/pull/1985
768770
[#1983]: https://github.com/benmosher/eslint-plugin-import/pull/1983
769771
[#1974]: https://github.com/benmosher/eslint-plugin-import/pull/1974
770772
[#1958]: https://github.com/benmosher/eslint-plugin-import/pull/1958
@@ -1350,3 +1352,4 @@ for info on changes for earlier releases.
13501352
[@dwardu]: https://github.com/dwardu
13511353
[@s-h-a-d-o-w]: https://github.com/s-h-a-d-o-w
13521354
[@grit96]: https://github.com/grit96
1355+
[@lilling]: https://github.com/lilling

Diff for: src/rules/newline-after-import.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ function isExportDefaultClass(node) {
4747
return node.type === 'ExportDefaultDeclaration' && node.declaration.type === 'ClassDeclaration';
4848
}
4949

50+
function isExportNameClass(node) {
51+
return node.type === 'ExportNamedDeclaration' && node.declaration.type === 'ClassDeclaration';
52+
}
53+
5054
module.exports = {
5155
meta: {
5256
type: 'layout',
@@ -72,7 +76,7 @@ module.exports = {
7276
const requireCalls = [];
7377

7478
function checkForNewLine(node, nextNode, type) {
75-
if (isExportDefaultClass(nextNode)) {
79+
if (isExportDefaultClass(nextNode) || isExportNameClass(nextNode)) {
7680
const classNode = nextNode.declaration;
7781

7882
if (isClassWithDecorator(classNode)) {

Diff for: tests/src/rules/newline-after-import.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { RuleTester } from 'eslint';
22
import flatMap from 'array.prototype.flatmap';
33

4-
import { getTSParsers } from '../utils';
4+
import { getTSParsers, testVersion } from '../utils';
55

66
const IMPORT_ERROR_MESSAGE = 'Expected 1 empty line after import statement not followed by another import.';
77
const IMPORT_ERROR_MESSAGE_MULTIPLE = (count) => {
@@ -234,7 +234,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
234234
]),
235235
],
236236

237-
invalid: [
237+
invalid: [].concat(
238238
{
239239
code: `import foo from 'foo';\nexport default function() {};`,
240240
output: `import foo from 'foo';\n\nexport default function() {};`,
@@ -429,5 +429,29 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
429429
parserOptions: { sourceType: 'module' },
430430
parser: require.resolve('babel-eslint'),
431431
},
432-
],
432+
testVersion('>= 6', () => ({
433+
code: `
434+
// issue 1784
435+
import { map } from 'rxjs/operators';
436+
@Component({})
437+
export class Test {}
438+
`,
439+
output: `
440+
// issue 1784
441+
import { map } from 'rxjs/operators';
442+
443+
@Component({})
444+
export class Test {}
445+
`,
446+
errors: [
447+
{
448+
line: 3,
449+
column: 9,
450+
message: IMPORT_ERROR_MESSAGE,
451+
},
452+
],
453+
parserOptions: { sourceType: 'module' },
454+
parser: require.resolve('babel-eslint'),
455+
})) || [],
456+
),
433457
});

0 commit comments

Comments
 (0)