Skip to content

Commit

Permalink
[Fix] newline-after-import: consider TypeScript import = syntax
Browse files Browse the repository at this point in the history
Fixes #1811.
  • Loading branch information
ljharb committed Jun 8, 2020
1 parent cc604c1 commit 903e8fb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions src/rules/newline-after-import.js
Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions 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) => {
Expand Down Expand Up @@ -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: [
Expand Down

0 comments on commit 903e8fb

Please sign in to comment.