diff --git a/CHANGELOG.md b/CHANGELOG.md index cb89f8723..c58beba1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ### Fixed - [`order`]: avoid a crash on TypeScript’s `export import` syntax ([#1808], thanks [@ljharb]) +- [`newline-after-import`]: consider TypeScript `import =` syntax' ([#1811], thanks [@ljharb]) ## [2.21.1] - 2020-06-07 ### Fixed @@ -900,6 +901,7 @@ for info on changes for earlier releases. [#211]: https://github.com/benmosher/eslint-plugin-import/pull/211 [#164]: https://github.com/benmosher/eslint-plugin-import/pull/164 [#157]: https://github.com/benmosher/eslint-plugin-import/pull/157 +[#1811]: https://github.com/benmosher/eslint-plugin-import/issues/1811 [#1808]: https://github.com/benmosher/eslint-plugin-import/issues/1808 [#1805]: https://github.com/benmosher/eslint-plugin-import/issues/1805 [#1565]: https://github.com/benmosher/eslint-plugin-import/issues/1565 diff --git a/src/rules/newline-after-import.js b/src/rules/newline-after-import.js index 7807dfcda..8255b189c 100644 --- a/src/rules/newline-after-import.js +++ b/src/rules/newline-after-import.js @@ -115,16 +115,19 @@ after ${type} statement not followed by another ${type}.`, level-- } - return { - ImportDeclaration: function (node) { + function checkImport(node) { const { parent } = node const nodePosition = parent.body.indexOf(node) const nextNode = parent.body[nodePosition + 1] - if (nextNode && nextNode.type !== 'ImportDeclaration') { + if (nextNode && nextNode.type !== 'ImportDeclaration' && nextNode.type !== 'TSImportEqualsDeclaration') { checkForNewLine(node, nextNode, 'import') } - }, + } + + return { + ImportDeclaration: checkImport, + TSImportEqualsDeclaration: checkImport, CallExpression: function(node) { if (isStaticRequire(node) && level === 0) { requireCalls.push(node) diff --git a/tests/src/rules/newline-after-import.js b/tests/src/rules/newline-after-import.js index bb94b56da..626e6e026 100644 --- a/tests/src/rules/newline-after-import.js +++ b/tests/src/rules/newline-after-import.js @@ -1,4 +1,7 @@ import { RuleTester } from 'eslint' +import flatMap from 'array.prototype.flatmap' + +import { getTSParsers } from '../utils' const IMPORT_ERROR_MESSAGE = 'Expected 1 empty line after import statement not followed by another import.' const IMPORT_ERROR_MESSAGE_MULTIPLE = (count) => { @@ -175,6 +178,42 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), { parserOptions: { sourceType: 'module' }, parser: require.resolve('babel-eslint'), }, + ...flatMap(getTSParsers(), (parser) => [ + { + code: ` + import { ExecaReturnValue } from 'execa'; + import execa = require('execa'); + `, + parser: parser, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + }, + { + code: ` + import execa = require('execa'); + import { ExecaReturnValue } from 'execa'; + `, + parser: parser, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + }, + { + code: ` + import { ExecaReturnValue } from 'execa'; + import execa = require('execa'); + import { ExecbReturnValue } from 'execb'; + `, + parser: parser, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + }, + { + code: ` + import execa = require('execa'); + import { ExecaReturnValue } from 'execa'; + import execb = require('execb'); + `, + parser: parser, + parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, + }, + ]), ], invalid: [