Skip to content

Commit

Permalink
[Fix] no-unresolved: Check dynamic imports when using the built-in Es…
Browse files Browse the repository at this point in the history
…lint 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`.
  • Loading branch information
davidbonnet authored and ljharb committed Mar 24, 2021
1 parent d31d615 commit 14acb5b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
34 changes: 26 additions & 8 deletions tests/src/rules/no-unresolved.js
@@ -1,6 +1,6 @@
import * as path from 'path';

import { test, SYNTAX_CASES } from '../utils';
import { test, SYNTAX_CASES, testVersion } from '../utils';

import { CASE_SENSITIVE_FS } from 'eslint-module-utils/resolve';

Expand All @@ -22,7 +22,7 @@ function runResolverTests(resolver) {
}

ruleTester.run(`no-unresolved (${resolver})`, rule, {
valid: [
valid: [].concat(
test({ code: 'import "./malformed.js"' }),

rest({ code: 'import foo from "./bar";' }),
Expand All @@ -32,6 +32,13 @@ function runResolverTests(resolver) {
rest({ code: "import('fs');",
parser: require.resolve('babel-eslint') }),

// check with eslint parser
testVersion('>= 6', () => rest({
code: "import('fs');",
parser: require.resolve('espree'),
parserOptions: { ecmaVersion: 2021 },
})) || [],

rest({ code: 'import * as foo from "a"' }),

rest({ code: 'export { foo } from "./bar"' }),
Expand Down Expand Up @@ -83,9 +90,9 @@ function runResolverTests(resolver) {
options: [{ commonjs: true }] }),
rest({ code: 'require(foo)',
options: [{ commonjs: true }] }),
],
),

invalid: [
invalid: [].concat(
rest({
code: 'import reallyfake from "./reallyfake/module"',
settings: { 'import/ignore': ['^\\./fake/'] },
Expand Down Expand Up @@ -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') }),

Expand All @@ -131,6 +138,17 @@ function runResolverTests(resolver) {
errors: ["Unable to resolve path to module './does-not-exist'."],
}),

// check with eslint parser
testVersion('>= 6', () => 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: 2021 },
})) || [],

// export symmetry proposal
rest({ code: 'export * as bar from "./does-not-exist"',
parser: require.resolve('babel-eslint'),
Expand Down Expand Up @@ -187,7 +205,7 @@ function runResolverTests(resolver) {
type: 'Literal',
}],
}),
],
),
});

ruleTester.run(`issue #333 (${resolver})`, rule, {
Expand Down
1 change: 1 addition & 0 deletions utils/moduleVisitor.js
Expand Up @@ -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,
Expand Down

0 comments on commit 14acb5b

Please sign in to comment.