Skip to content

Commit 9a87957

Browse files
fix(newline-after-import): preserve comments between imports
1 parent 1a39fb8 commit 9a87957

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1212
- [`consistent-type-specifier-style`]: add `prefer-top-level-if-only-type-imports` option ([#3210], thanks [@aldeed])
1313

1414
### Fixed
15+
- [`newline-after-import`]: ignore comments between consecutive imports when `considerComments` is enabled ([#2673])
1516
- [`no-duplicates`]: fix `prefer-inline` autofix producing invalid syntax when an identifier named `from` is imported ([#3236], thanks [@DukeDeSouth] [@Quantaly])
1617
- [`no-duplicates`]: avoid false positives for TypeScript namespace and default type imports that cannot be merged ([#3195], thanks [@sjh9714] [@robyoder])
1718
- ExportMap: resolve export * as ns re-exports under modern parsers ([#3250], thanks [@rasmi] [@JounQin] [@butterybread])

src/rules/newline-after-import.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,16 @@ module.exports = {
179179
return;
180180
}
181181

182+
if (
183+
nextNode
184+
&& (nextNode.type === 'ImportDeclaration' || nextNode.type === 'TSImportEqualsDeclaration' && !nextNode.isExport)
185+
) {
186+
return;
187+
}
188+
182189
if (nextComment && typeof nextComment !== 'undefined') {
183190
commentAfterImport(node, nextComment, 'import');
184-
} else if (nextNode && nextNode.type !== 'ImportDeclaration' && (nextNode.type !== 'TSImportEqualsDeclaration' || nextNode.isExport)) {
191+
} else if (nextNode) {
185192
checkForNewLine(node, nextNode, 'import');
186193
}
187194
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
116116
code: `import path from 'path';\nimport foo from 'foo';\n`,
117117
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
118118
},
119+
{
120+
code: `import foo from 'foo';\n// Bar.\n// Baz.\nimport qux from 'qux';\n\nconst quux = 'quux';`,
121+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
122+
options: [{ considerComments: true }],
123+
},
119124
{
120125
code: `import path from 'path';import foo from 'foo';\n`,
121126
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },

0 commit comments

Comments
 (0)