Skip to content

Commit 903e8fb

Browse files
committed
[Fix] newline-after-import: consider TypeScript import = syntax
Fixes #1811.
1 parent cc604c1 commit 903e8fb

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
88

99
### Fixed
1010
- [`order`]: avoid a crash on TypeScript’s `export import` syntax ([#1808], thanks [@ljharb])
11+
- [`newline-after-import`]: consider TypeScript `import =` syntax' ([#1811], thanks [@ljharb])
1112

1213
## [2.21.1] - 2020-06-07
1314
### Fixed
@@ -900,6 +901,7 @@ for info on changes for earlier releases.
900901
[#211]: https://github.com/benmosher/eslint-plugin-import/pull/211
901902
[#164]: https://github.com/benmosher/eslint-plugin-import/pull/164
902903
[#157]: https://github.com/benmosher/eslint-plugin-import/pull/157
904+
[#1811]: https://github.com/benmosher/eslint-plugin-import/issues/1811
903905
[#1808]: https://github.com/benmosher/eslint-plugin-import/issues/1808
904906
[#1805]: https://github.com/benmosher/eslint-plugin-import/issues/1805
905907
[#1565]: https://github.com/benmosher/eslint-plugin-import/issues/1565

src/rules/newline-after-import.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,19 @@ after ${type} statement not followed by another ${type}.`,
115115
level--
116116
}
117117

118-
return {
119-
ImportDeclaration: function (node) {
118+
function checkImport(node) {
120119
const { parent } = node
121120
const nodePosition = parent.body.indexOf(node)
122121
const nextNode = parent.body[nodePosition + 1]
123122

124-
if (nextNode && nextNode.type !== 'ImportDeclaration') {
123+
if (nextNode && nextNode.type !== 'ImportDeclaration' && nextNode.type !== 'TSImportEqualsDeclaration') {
125124
checkForNewLine(node, nextNode, 'import')
126125
}
127-
},
126+
}
127+
128+
return {
129+
ImportDeclaration: checkImport,
130+
TSImportEqualsDeclaration: checkImport,
128131
CallExpression: function(node) {
129132
if (isStaticRequire(node) && level === 0) {
130133
requireCalls.push(node)

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { RuleTester } from 'eslint'
2+
import flatMap from 'array.prototype.flatmap'
3+
4+
import { getTSParsers } from '../utils'
25

36
const IMPORT_ERROR_MESSAGE = 'Expected 1 empty line after import statement not followed by another import.'
47
const IMPORT_ERROR_MESSAGE_MULTIPLE = (count) => {
@@ -175,6 +178,42 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
175178
parserOptions: { sourceType: 'module' },
176179
parser: require.resolve('babel-eslint'),
177180
},
181+
...flatMap(getTSParsers(), (parser) => [
182+
{
183+
code: `
184+
import { ExecaReturnValue } from 'execa';
185+
import execa = require('execa');
186+
`,
187+
parser: parser,
188+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
189+
},
190+
{
191+
code: `
192+
import execa = require('execa');
193+
import { ExecaReturnValue } from 'execa';
194+
`,
195+
parser: parser,
196+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
197+
},
198+
{
199+
code: `
200+
import { ExecaReturnValue } from 'execa';
201+
import execa = require('execa');
202+
import { ExecbReturnValue } from 'execb';
203+
`,
204+
parser: parser,
205+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
206+
},
207+
{
208+
code: `
209+
import execa = require('execa');
210+
import { ExecaReturnValue } from 'execa';
211+
import execb = require('execb');
212+
`,
213+
parser: parser,
214+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
215+
},
216+
]),
178217
],
179218

180219
invalid: [

0 commit comments

Comments
 (0)