From 1951ef517ab50b6d664a97bd3d799e347914e186 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 8 Jun 2020 11:18:27 -0700 Subject: [PATCH 1/6] [Tests] `order`: group TS tests together --- tests/src/rules/order.js | 89 ++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index f6e2dddbaa..bad48bf38d 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -3,6 +3,7 @@ import { test, getTSParsers, getNonDefaultParsers } from '../utils' import { RuleTester } from 'eslint' import eslintPkg from 'eslint/package.json' import semver from 'semver' +import flatMap from 'array.prototype.flatmap' const ruleTester = new RuleTester() , rule = require('rules/order') @@ -167,8 +168,8 @@ ruleTester.run('order', rule, { var index = require('./'); `, }), - // Export equals expressions should be on top alongside with ordinary import-statements. - ...getTSParsers().map(parser => ( + ...flatMap(getTSParsers(), parser => [ + // Export equals expressions should be on top alongside with ordinary import-statements. test({ code: ` import async, {foo1} from 'async'; @@ -181,8 +182,8 @@ ruleTester.run('order', rule, { var index = require('./'); `, parser, - }) - )), + }), + ]), // Adding unknown import types (e.g. using a resolver alias via babel) to the groups. test({ code: ` @@ -1158,7 +1159,7 @@ ruleTester.run('order', rule, { message: '`fs` import should occur after import of `../foo/bar`', }], }), - ...getTSParsers().map(parser => ( + ...flatMap(getTSParsers(), parser => [ test({ code: ` var fs = require('fs'); @@ -1174,8 +1175,44 @@ ruleTester.run('order', rule, { errors: [{ message: '`fs` import should occur after import of `../foo/bar`', }], - }) - )), + }), + { + code: ` + var async = require('async'); + var fs = require('fs'); + `, + output: ` + var fs = require('fs'); + var async = require('async'); + `, + parser, + errors: [{ + message: '`fs` import should occur before import of `async`', + }], + }, + test({ + code: ` + import sync = require('sync'); + import async, {foo1} from 'async'; + + import index from './'; + `, + output: ` + import async, {foo1} from 'async'; + import sync = require('sync'); + + import index from './'; + `, + options: [{ + groups: ['external', 'index'], + alphabetize: {order: 'asc'}, + }], + parser, + errors: [{ + message: '`async` import should occur before import of `sync`', + }], + }), + ]), // Default order using import with custom import alias test({ code: ` @@ -1909,20 +1946,6 @@ ruleTester.run('order', rule, { message: '`fs` import should occur before import of `async`', }], })), - ...getTSParsers().map(parser => ({ - code: ` - var async = require('async'); - var fs = require('fs'); - `, - output: ` - var fs = require('fs'); - var async = require('async'); - `, - parser, - errors: [{ - message: '`fs` import should occur before import of `async`', - }], - })), // Option alphabetize: {order: 'asc'} test({ code: ` @@ -1947,30 +1970,6 @@ ruleTester.run('order', rule, { message: '`Bar` import should occur before import of `bar`', }], }), - ...getTSParsers().map(parser => ( - test({ - code: ` - import sync = require('sync'); - import async, {foo1} from 'async'; - - import index from './'; - `, - output: ` - import async, {foo1} from 'async'; - import sync = require('sync'); - - import index from './'; - `, - options: [{ - groups: ['external', 'index'], - alphabetize: {order: 'asc'}, - }], - parser, - errors: [{ - message: '`async` import should occur before import of `sync`', - }], - }) - )), // Option alphabetize: {order: 'desc'} test({ code: ` From cc604c1ed9267427c50dd8e598f6a1e136867173 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 8 Jun 2020 11:24:51 -0700 Subject: [PATCH 2/6] =?UTF-8?q?[Fix]=20`order`:=20avoid=20a=20crash=20on?= =?UTF-8?q?=20TypeScript=E2=80=99s=20`export=20import`=20syntax?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #1808. --- CHANGELOG.md | 5 +++++ src/core/importType.js | 2 +- src/rules/order.js | 2 ++ tests/src/rules/order.js | 7 +++++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca63eb5f8d..cb89f8723d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ## [Unreleased] +### Fixed +- [`order`]: avoid a crash on TypeScript’s `export import` syntax ([#1808], thanks [@ljharb]) + ## [2.21.1] - 2020-06-07 ### Fixed - TypeScript: [`import/named`]: avoid requiring `typescript` when not using TS ([#1805], thanks [@ljharb]) @@ -897,6 +900,8 @@ 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 +[#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 [#1366]: https://github.com/benmosher/eslint-plugin-import/issues/1366 [#1334]: https://github.com/benmosher/eslint-plugin-import/issues/1334 diff --git a/src/core/importType.js b/src/core/importType.js index 4d56b86d4b..ff2d10b60f 100644 --- a/src/core/importType.js +++ b/src/core/importType.js @@ -12,7 +12,7 @@ function baseModule(name) { } export function isAbsolute(name) { - return name.indexOf('/') === 0 + return name && name.startsWith('/') } // path is defined only when a resolver resolves to a non-standard path diff --git a/src/rules/order.js b/src/rules/order.js index 9edac3af91..b407145405 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -611,6 +611,8 @@ module.exports = { let name if (node.moduleReference.type === 'TSExternalModuleReference') { name = node.moduleReference.expression.value + } else if (node.isExport) { + name = node.moduleReference.name } else { name = null } diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index bad48bf38d..e8ee82ec6c 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -183,6 +183,13 @@ ruleTester.run('order', rule, { `, parser, }), + + test({ + code: ` + export import CreateSomething = _CreateSomething; + `, + parser, + }), ]), // Adding unknown import types (e.g. using a resolver alias via babel) to the groups. test({ From 903e8fbcaf345544338077e23a83e669fd54bc14 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 8 Jun 2020 13:05:48 -0700 Subject: [PATCH 3/6] [Fix] `newline-after-import`: consider TypeScript `import =` syntax Fixes #1811. --- CHANGELOG.md | 2 ++ src/rules/newline-after-import.js | 11 ++++--- tests/src/rules/newline-after-import.js | 39 +++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 4 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..8255b189cc 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 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: [ From 4ce280a0e1527380c25c7ca7d83767326d972442 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 9 Jun 2020 11:49:58 -0700 Subject: [PATCH 4/6] [Fix] `no-internal-modules`: avoid a crash on a named export declaration Fixes #1814. --- CHANGELOG.md | 2 ++ src/rules/no-internal-modules.js | 4 +++- tests/src/rules/no-internal-modules.js | 24 +++++++++++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c58beba1a6..0d5251dfcf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,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]) +- [`no-internal-modules`]: avoid a crash on a named export declaration ([#1814], thanks [@ljharb]) ## [2.21.1] - 2020-06-07 ### Fixed @@ -901,6 +902,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 +[#1814]: https://github.com/benmosher/eslint-plugin-import/issues/1814 [#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 diff --git a/src/rules/no-internal-modules.js b/src/rules/no-internal-modules.js index b5d7496a2a..bd13ab07d0 100644 --- a/src/rules/no-internal-modules.js +++ b/src/rules/no-internal-modules.js @@ -95,7 +95,9 @@ module.exports = { checkImportForReaching(node.source.value, node.source) }, ExportNamedDeclaration(node) { - checkImportForReaching(node.source.value, node.source) + if (node.source) { + checkImportForReaching(node.source.value, node.source) + } }, CallExpression(node) { if (isStaticRequire(node)) { diff --git a/tests/src/rules/no-internal-modules.js b/tests/src/rules/no-internal-modules.js index 5058fcb349..da9a4ca1a0 100644 --- a/tests/src/rules/no-internal-modules.js +++ b/tests/src/rules/no-internal-modules.js @@ -1,7 +1,8 @@ import { RuleTester } from 'eslint' +import flatMap from 'array.prototype.flatmap' import rule from 'rules/no-internal-modules' -import { test, testFilePath } from '../utils' +import { test, testFilePath, getTSParsers } from '../utils' const ruleTester = new RuleTester() @@ -92,6 +93,27 @@ ruleTester.run('no-internal-modules', rule, { allow: [ '**/index{.js,}' ], } ], }), + test({ + code: ` + export class AuthHelper { + + static checkAuth(auth) { + } + } + `, + }), + ...flatMap(getTSParsers(), (parser) => [ + test({ + code: ` + export class AuthHelper { + + public static checkAuth(auth?: string): boolean { + } + } + `, + parser: parser, + }), + ]), ], invalid: [ From ffd540fc7a32c154eb870d05b865de0db64fa2cb Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 9 Jun 2020 16:04:00 -0700 Subject: [PATCH 5/6] [Dev Deps] update `in-publish`, `typescript` --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 4d6de7644c..73578badd1 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "eslint-plugin-json": "^2.1.1", "fs-copy-file-sync": "^1.1.1", "glob": "^7.1.6", - "in-publish": "^2.0.0", + "in-publish": "^2.0.1", "linklocal": "^2.8.2", "lodash.isarray": "^4.0.0", "mocha": "^3.5.3", @@ -90,7 +90,7 @@ "rimraf": "^2.7.1", "semver": "^6.3.0", "sinon": "^2.4.1", - "typescript": "~3.8.3", + "typescript": "~3.9.5", "typescript-eslint-parser": "^22.0.0" }, "peerDependencies": { From f53e872540dfc13b129f456844373b451fa540f3 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 9 Jun 2020 16:05:17 -0700 Subject: [PATCH 6/6] Bump to v2.21.2 --- CHANGELOG.md | 4 +++- package.json | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d5251dfcf..1d583358ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ## [Unreleased] +## [2.21.2] - 2020-06-09 ### Fixed - [`order`]: avoid a crash on TypeScript’s `export import` syntax ([#1808], thanks [@ljharb]) - [`newline-after-import`]: consider TypeScript `import =` syntax' ([#1811], thanks [@ljharb]) @@ -991,7 +992,8 @@ for info on changes for earlier releases. [#119]: https://github.com/benmosher/eslint-plugin-import/issues/119 [#89]: https://github.com/benmosher/eslint-plugin-import/issues/89 -[Unreleased]: https://github.com/benmosher/eslint-plugin-import/compare/v2.21.1...HEAD +[Unreleased]: https://github.com/benmosher/eslint-plugin-import/compare/v2.21.2...HEAD +[2.21.2]: https://github.com/benmosher/eslint-plugin-import/compare/v2.21.1...v2.21.2 [2.21.1]: https://github.com/benmosher/eslint-plugin-import/compare/v2.21.0...v2.21.1 [2.21.0]: https://github.com/benmosher/eslint-plugin-import/compare/v2.20.2...v2.21.0 [2.20.1]: https://github.com/benmosher/eslint-plugin-import/compare/v2.20.1...v2.20.2 diff --git a/package.json b/package.json index 73578badd1..9b42324f66 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-import", - "version": "2.21.1", + "version": "2.21.2", "description": "Import with sanity.", "engines": { "node": ">=4"