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: respect decorator annotations #1985

Merged
merged 1 commit into from May 12, 2021
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 @@ -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
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
@@ -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'),
})) || [],
),
});