From ba3368d5feddeeba922b2250a1bf19cc1931a6d0 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 8 Jun 2020 13:05:48 -0700 Subject: [PATCH] [Fix] `newline-after-import`: consider TypeScript `import =` syntax Fixes #1811. --- CHANGELOG.md | 2 ++ src/rules/newline-after-import.js | 4 +-- tests/src/rules/newline-after-import.js | 39 +++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb89f8723d..c58beba1a6 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 7807dfcdab..da05377119 100644 --- a/src/rules/newline-after-import.js +++ b/src/rules/newline-after-import.js @@ -116,12 +116,12 @@ after ${type} statement not followed by another ${type}.`, } return { - ImportDeclaration: function (node) { + 'ImportDeclaration, TSImportEqualsDeclaration': function (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') } }, diff --git a/tests/src/rules/newline-after-import.js b/tests/src/rules/newline-after-import.js index bb94b56dad..626e6e0261 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: [