Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] newline-after-import: recognize decorators #1139

Merged
merged 2 commits into from Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`order`]: Recognize pathGroup config for first group ([#1719], [#1724], thanks [@forivall], [@xpl])
- [`no-unused-modules`]: Fix re-export not counting as usage when used in combination with import ([#1722], thanks [@Ephem])
- [`no-duplicates`]: Handle TS import type ([#1676], thanks [@kmui2])
- [``newline-after-import`: recognize decorators ([#1139], thanks [@atos1990])

### Changed
- TypeScript config: Disable [`named`][] ([#1726], thanks [@astorije])
Expand Down Expand Up @@ -770,6 +771,7 @@ for info on changes for earlier releases.
[#1157]: https://github.com/benmosher/eslint-plugin-import/pull/1157
[#1151]: https://github.com/benmosher/eslint-plugin-import/pull/1151
[#1142]: https://github.com/benmosher/eslint-plugin-import/pull/1142
[#1139]: https://github.com/benmosher/eslint-plugin-import/pull/1139
[#1137]: https://github.com/benmosher/eslint-plugin-import/pull/1137
[#1135]: https://github.com/benmosher/eslint-plugin-import/pull/1135
[#1128]: https://github.com/benmosher/eslint-plugin-import/pull/1128
Expand Down Expand Up @@ -1153,3 +1155,4 @@ for info on changes for earlier releases.
[@Ephem]: https://github.com/Ephem
[@kmui2]: https://github.com/kmui2
[@arvigeus]: https://github.com/arvigeus
[@atos1990]: https://github.com/atos1990
2 changes: 1 addition & 1 deletion appveyor.yml
Expand Up @@ -48,7 +48,7 @@ test_script:
- npm --version

# core tests
- npm test
- npm run tests-only

# resolver tests
- cd .\resolvers\webpack && npm test && cd ..\..
Expand Down
12 changes: 11 additions & 1 deletion src/rules/newline-after-import.js
Expand Up @@ -43,6 +43,10 @@ function isClassWithDecorator(node) {
return node.type === 'ClassDeclaration' && node.decorators && node.decorators.length
}

function isExportDefaultClass(node) {
return node.type === 'ExportDefaultDeclaration' && node.declaration.type === 'ClassDeclaration'
}

module.exports = {
meta: {
type: 'layout',
Expand All @@ -68,7 +72,13 @@ module.exports = {
const requireCalls = []

function checkForNewLine(node, nextNode, type) {
if (isClassWithDecorator(nextNode)) {
if (isExportDefaultClass(nextNode)) {
let classNode = nextNode.declaration

if (isClassWithDecorator(classNode)) {
nextNode = classNode.decorators[0]
}
} else if (isClassWithDecorator(nextNode)) {
nextNode = nextNode.decorators[0]
}

Expand Down
32 changes: 32 additions & 0 deletions tests/src/rules/newline-after-import.js
Expand Up @@ -165,6 +165,16 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
parser: require.resolve('babel-eslint'),
},
{
code : `// issue 1004\nimport foo from 'foo';\n\n@SomeDecorator(foo)\nexport default class Test {}`,
parserOptions: { sourceType: 'module' },
parser: require.resolve('babel-eslint'),
},
{
code : `// issue 1004\nconst foo = require('foo');\n\n@SomeDecorator(foo)\nexport default class Test {}`,
parserOptions: { sourceType: 'module' },
parser: require.resolve('babel-eslint'),
},
],

invalid: [
Expand Down Expand Up @@ -340,5 +350,27 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
parser: require.resolve('babel-eslint'),
},
{
code: `// issue 10042\nimport foo from 'foo';\n@SomeDecorator(foo)\nexport default class Test {}`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can these be reverted to use actual newlines instead of \n? it's much more readable.

output: `// issue 10042\nimport foo from 'foo';\n\n@SomeDecorator(foo)\nexport default class Test {}`,
errors: [ {
line: 2,
column: 1,
message: IMPORT_ERROR_MESSAGE,
} ],
parserOptions: { sourceType: 'module' },
parser: require.resolve('babel-eslint'),
},
{
code: `// issue 1004\nconst foo = require('foo');\n@SomeDecorator(foo)\nexport default class Test {}`,
output: `// issue 1004\nconst foo = require('foo');\n\n@SomeDecorator(foo)\nexport default class Test {}`,
errors: [ {
line: 2,
column: 1,
message: REQUIRE_ERROR_MESSAGE,
} ],
parserOptions: { sourceType: 'module' },
parser: require.resolve('babel-eslint'),
},
],
})