Skip to content

Commit 5837636

Browse files
davidbonnetljharb
authored andcommitted
[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`.
1 parent d31d615 commit 5837636

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
2525
- [`extensions`]/[`no-cycle`]/[`no-extraneous-dependencies`]: Correct module real path resolution ([#1696], thanks [@paztis])
2626
- [`no-named-default`]: ignore Flow import type and typeof ([#1983], thanks [@christianvuerings])
2727
- [`no-extraneous-dependencies`]: Exclude flow `typeof` imports ([#1534], thanks [@devongovett])
28+
- [`no-unresolved`]: Check dynamic imports when using the built-in Eslint parser ([#2012], thanks [@davidbonnet])
2829

2930
### Changed
3031
- [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.
760761

761762
[`memo-parser`]: ./memo-parser/README.md
762763

764+
[#2012]: https://github.com/benmosher/eslint-plugin-import/pull/2012
763765
[#1993]: https://github.com/benmosher/eslint-plugin-import/pull/1993
764766
[#1983]: https://github.com/benmosher/eslint-plugin-import/pull/1983
765767
[#1974]: https://github.com/benmosher/eslint-plugin-import/pull/1974
@@ -1344,3 +1346,4 @@ for info on changes for earlier releases.
13441346
[@christianvuerings]: https://github.com/christianvuerings
13451347
[@devongovett]: https://github.com/devongovett
13461348
[@dwardu]: https://github.com/dwardu
1349+
[@davidbonnet]: https://github.com/davidbonnet

tests/src/rules/no-unresolved.js

+25-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'path';
22

3-
import { test, SYNTAX_CASES } from '../utils';
3+
import { test, SYNTAX_CASES, testVersion } from '../utils';
44

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

@@ -22,7 +22,7 @@ function runResolverTests(resolver) {
2222
}
2323

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

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

35+
// check with eslint parser
36+
testVersion('>= 6', () => rest({
37+
code: "import('fs');",
38+
parser: require.resolve('espree'),
39+
parserOptions: { ecmaVersion: 2021 },
40+
})) || [],
41+
3542
rest({ code: 'import * as foo from "a"' }),
3643

3744
rest({ code: 'export { foo } from "./bar"' }),
@@ -85,7 +92,7 @@ function runResolverTests(resolver) {
8592
options: [{ commonjs: true }] }),
8693
],
8794

88-
invalid: [
95+
invalid: [].concat(
8996
rest({
9097
code: 'import reallyfake from "./reallyfake/module"',
9198
settings: { 'import/ignore': ['^\\./fake/'] },
@@ -118,9 +125,9 @@ function runResolverTests(resolver) {
118125
}] }),
119126
rest({
120127
code: "import('in-alternate-root').then(function({DEEP}){});",
121-
errors: [{ message: 'Unable to resolve path to ' +
122-
"module 'in-alternate-root'.",
123-
type: 'Literal',
128+
errors: [{
129+
message: 'Unable to resolve path to module \'in-alternate-root\'.',
130+
type: 'Literal',
124131
}],
125132
parser: require.resolve('babel-eslint') }),
126133

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

141+
// check with eslint parser
142+
testVersion('>= 6', () => rest({
143+
code: "import('in-alternate-root').then(function({DEEP}){});",
144+
errors: [{
145+
message: 'Unable to resolve path to module \'in-alternate-root\'.',
146+
type: 'Literal',
147+
}],
148+
parser: require.resolve('espree'),
149+
parserOptions: { ecmaVersion: 2021 },
150+
})) || [],
151+
134152
// export symmetry proposal
135153
rest({ code: 'export * as bar from "./does-not-exist"',
136154
parser: require.resolve('babel-eslint'),
@@ -187,7 +205,7 @@ function runResolverTests(resolver) {
187205
type: 'Literal',
188206
}],
189207
}),
190-
],
208+
),
191209
});
192210

193211
ruleTester.run(`issue #333 (${resolver})`, rule, {

utils/moduleVisitor.js

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ exports.default = function visitModules(visitor, options) {
8484
if (options.esmodule) {
8585
Object.assign(visitors, {
8686
'ImportDeclaration': checkSource,
87+
'ImportExpression': checkSource,
8788
'ExportNamedDeclaration': checkSource,
8889
'ExportAllDeclaration': checkSource,
8990
'CallExpression': checkImportCall,

0 commit comments

Comments
 (0)