From 3c5e2c8b8d5af0db009848149e7d650a49d37995 Mon Sep 17 00:00:00 2001 From: David Bonnet Date: Wed, 24 Mar 2021 13:44:38 +0100 Subject: [PATCH] [Fix] no-unresolved: Check dynamic imports when using the built-in Eslint parser Dynamic imports landed in EcmaScript 2020, and Eslint now parses it out of the box. However, the node containing the dynamic import is an `ImportExpression`. --- CHANGELOG.md | 3 +++ tests/src/rules/no-unresolved.js | 24 +++++++++++++++++++++--- utils/moduleVisitor.js | 1 + 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b9edced61..556db161fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel - [`extensions`]/[`no-cycle`]/[`no-extraneous-dependencies`]: Correct module real path resolution ([#1696], thanks [@paztis]) - [`no-named-default`]: ignore Flow import type and typeof ([#1983], thanks [@christianvuerings]) - [`no-extraneous-dependencies`]: Exclude flow `typeof` imports ([#1534], thanks [@devongovett]) +- [`no-unresolved`]: Check dynamic imports when using the built-in Eslint parser ([#2012], thanks [@davidbonnet]) ### Changed - [Generic Import Callback] Make callback for all imports once in rules ([#1237], thanks [@ljqx]) @@ -760,6 +761,7 @@ for info on changes for earlier releases. [`memo-parser`]: ./memo-parser/README.md +[#2012]: https://github.com/benmosher/eslint-plugin-import/pull/2012 [#1993]: https://github.com/benmosher/eslint-plugin-import/pull/1993 [#1983]: https://github.com/benmosher/eslint-plugin-import/pull/1983 [#1974]: https://github.com/benmosher/eslint-plugin-import/pull/1974 @@ -1344,3 +1346,4 @@ for info on changes for earlier releases. [@christianvuerings]: https://github.com/christianvuerings [@devongovett]: https://github.com/devongovett [@dwardu]: https://github.com/dwardu +[@davidbonnet]: https://github.com/davidbonnet diff --git a/tests/src/rules/no-unresolved.js b/tests/src/rules/no-unresolved.js index da7d4dc5ae..9530c9fd10 100644 --- a/tests/src/rules/no-unresolved.js +++ b/tests/src/rules/no-unresolved.js @@ -32,6 +32,13 @@ function runResolverTests(resolver) { rest({ code: "import('fs');", parser: require.resolve('babel-eslint') }), + // check with eslint parser + rest({ + code: "import('fs');", + parser: require.resolve('espree'), + parserOptions: { ecmaVersion: 12 }, + }), + rest({ code: 'import * as foo from "a"' }), rest({ code: 'export { foo } from "./bar"' }), @@ -118,9 +125,9 @@ function runResolverTests(resolver) { }] }), rest({ code: "import('in-alternate-root').then(function({DEEP}){});", - errors: [{ message: 'Unable to resolve path to ' + - "module 'in-alternate-root'.", - type: 'Literal', + errors: [{ + message: 'Unable to resolve path to module \'in-alternate-root\'.', + type: 'Literal', }], parser: require.resolve('babel-eslint') }), @@ -131,6 +138,17 @@ function runResolverTests(resolver) { errors: ["Unable to resolve path to module './does-not-exist'."], }), + // check with eslint parser + rest({ + code: "import('in-alternate-root').then(function({DEEP}){});", + errors: [{ + message: 'Unable to resolve path to module \'in-alternate-root\'.', + type: 'Literal', + }], + parser: require.resolve('espree'), + parserOptions: { ecmaVersion: 12 }, + }), + // export symmetry proposal rest({ code: 'export * as bar from "./does-not-exist"', parser: require.resolve('babel-eslint'), diff --git a/utils/moduleVisitor.js b/utils/moduleVisitor.js index d801515bce..1ff7991ab6 100644 --- a/utils/moduleVisitor.js +++ b/utils/moduleVisitor.js @@ -84,6 +84,7 @@ exports.default = function visitModules(visitor, options) { if (options.esmodule) { Object.assign(visitors, { 'ImportDeclaration': checkSource, + 'ImportExpression': checkSource, 'ExportNamedDeclaration': checkSource, 'ExportAllDeclaration': checkSource, 'CallExpression': checkImportCall,