Skip to content

Commit b307c7c

Browse files
Averin Antonljharb
authored andcommitted
[Fix] newline-after-import: recognize decorators
Fixes #1004.
1 parent a8888b0 commit b307c7c

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1515
- [`order`]: Recognize pathGroup config for first group ([#1719], [#1724], thanks [@forivall], [@xpl])
1616
- [`no-unused-modules`]: Fix re-export not counting as usage when used in combination with import ([#1722], thanks [@Ephem])
1717
- [`no-duplicates`]: Handle TS import type ([#1676], thanks [@kmui2])
18+
- [``newline-after-import`: recognize decorators ([#1139], thanks [@atos1990])
1819

1920
### Changed
2021
- TypeScript config: Disable [`named`][] ([#1726], thanks [@astorije])
@@ -770,6 +771,7 @@ for info on changes for earlier releases.
770771
[#1157]: https://github.com/benmosher/eslint-plugin-import/pull/1157
771772
[#1151]: https://github.com/benmosher/eslint-plugin-import/pull/1151
772773
[#1142]: https://github.com/benmosher/eslint-plugin-import/pull/1142
774+
[#1139]: https://github.com/benmosher/eslint-plugin-import/pull/1139
773775
[#1137]: https://github.com/benmosher/eslint-plugin-import/pull/1137
774776
[#1135]: https://github.com/benmosher/eslint-plugin-import/pull/1135
775777
[#1128]: https://github.com/benmosher/eslint-plugin-import/pull/1128
@@ -1153,3 +1155,4 @@ for info on changes for earlier releases.
11531155
[@Ephem]: https://github.com/Ephem
11541156
[@kmui2]: https://github.com/kmui2
11551157
[@arvigeus]: https://github.com/arvigeus
1158+
[@atos1990]: https://github.com/atos1990

src/rules/newline-after-import.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ function isClassWithDecorator(node) {
4343
return node.type === 'ClassDeclaration' && node.decorators && node.decorators.length
4444
}
4545

46+
function isExportDefaultClass(node) {
47+
return node.type === 'ExportDefaultDeclaration' && node.declaration.type === 'ClassDeclaration'
48+
}
49+
4650
module.exports = {
4751
meta: {
4852
type: 'layout',
@@ -68,7 +72,13 @@ module.exports = {
6872
const requireCalls = []
6973

7074
function checkForNewLine(node, nextNode, type) {
71-
if (isClassWithDecorator(nextNode)) {
75+
if (isExportDefaultClass(nextNode)) {
76+
let classNode = nextNode.declaration
77+
78+
if (isClassWithDecorator(classNode)) {
79+
nextNode = classNode.decorators[0]
80+
}
81+
} else if (isClassWithDecorator(nextNode)) {
7282
nextNode = nextNode.decorators[0]
7383
}
7484

tests/src/rules/newline-after-import.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
165165
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
166166
parser: require.resolve('babel-eslint'),
167167
},
168+
{
169+
code : `// issue 1004\nimport foo from 'foo';\n\n@SomeDecorator(foo)\nexport default class Test {}`,
170+
parserOptions: { sourceType: 'module' },
171+
parser: require.resolve('babel-eslint'),
172+
},
173+
{
174+
code : `// issue 1004\nconst foo = require('foo');\n\n@SomeDecorator(foo)\nexport default class Test {}`,
175+
parserOptions: { sourceType: 'module' },
176+
parser: require.resolve('babel-eslint'),
177+
},
168178
],
169179

170180
invalid: [
@@ -340,5 +350,27 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
340350
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
341351
parser: require.resolve('babel-eslint'),
342352
},
353+
{
354+
code: `// issue 10042\nimport foo from 'foo';\n@SomeDecorator(foo)\nexport default class Test {}`,
355+
output: `// issue 10042\nimport foo from 'foo';\n\n@SomeDecorator(foo)\nexport default class Test {}`,
356+
errors: [ {
357+
line: 2,
358+
column: 1,
359+
message: IMPORT_ERROR_MESSAGE,
360+
} ],
361+
parserOptions: { sourceType: 'module' },
362+
parser: require.resolve('babel-eslint'),
363+
},
364+
{
365+
code: `// issue 1004\nconst foo = require('foo');\n@SomeDecorator(foo)\nexport default class Test {}`,
366+
output: `// issue 1004\nconst foo = require('foo');\n\n@SomeDecorator(foo)\nexport default class Test {}`,
367+
errors: [ {
368+
line: 2,
369+
column: 1,
370+
message: REQUIRE_ERROR_MESSAGE,
371+
} ],
372+
parserOptions: { sourceType: 'module' },
373+
parser: require.resolve('babel-eslint'),
374+
},
343375
],
344376
})

0 commit comments

Comments
 (0)