Skip to content

Commit 6f46b6f

Browse files
committed
fix: no-unresolved check import() (fixes #2024)
1 parent 2057c05 commit 6f46b6f

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
2929
- [`no-extraneous-dependencies`]: Exclude flow `typeof` imports ([#1534], thanks [@devongovett])
3030
- [`newline-after-import`]: respect decorator annotations ([#1985], thanks [@lilling])
3131
- [`no-restricted-paths`]: enhance performance for zones with `except` paths ([#2022], thanks [@malykhinvi])
32+
- [`no-unresolved`]: Check dynamic imports when using the built-in Eslint parser ([#2026], thanks [@aladdin-add])
3233

3334
### Changed
3435
- [Generic Import Callback] Make callback for all imports once in rules ([#1237], thanks [@ljqx])
@@ -1355,3 +1356,4 @@ for info on changes for earlier releases.
13551356
[@s-h-a-d-o-w]: https://github.com/s-h-a-d-o-w
13561357
[@grit96]: https://github.com/grit96
13571358
[@lilling]: https://github.com/lilling
1359+
[@aladdin-add]: https://github.com/aladdin-add

tests/src/rules/no-unresolved.js

+20-2
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

@@ -93,7 +93,6 @@ function runResolverTests(resolver) {
9393
'\'./reallyfake/module\'.' }],
9494
}),
9595

96-
9796
rest({
9897
code: "import bar from './baz';",
9998
errors: [{ message: "Unable to resolve path to module './baz'.",
@@ -382,3 +381,22 @@ ruleTester.run('no-unresolved syntax verification', rule, {
382381
valid: SYNTAX_CASES,
383382
invalid:[],
384383
});
384+
385+
// https://github.com/benmosher/eslint-plugin-import/issues/2024
386+
ruleTester.run('import() with built-in parser', rule, {
387+
valid: [].concat(
388+
testVersion('>=7', () => ({
389+
code: "import('fs');",
390+
parser: require.resolve('espree'),
391+
parserOptions: { ecmaVersion: 2021 },
392+
})) || [],
393+
),
394+
invalid: [].concat(
395+
testVersion('>=7', () => ({
396+
code: 'import("./does-not-exist").then(() => {})',
397+
parser: require.resolve('espree'),
398+
parserOptions: { ecmaVersion: 2021 },
399+
errors: ["Unable to resolve path to module './does-not-exist'."],
400+
})) || [],
401+
),
402+
});

utils/moduleVisitor.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,17 @@ exports.default = function visitModules(visitor, options) {
3636

3737
// for esmodule dynamic `import()` calls
3838
function checkImportCall(node) {
39-
if (node.callee.type !== 'Import') return;
40-
if (node.arguments.length !== 1) return;
39+
let modulePath;
40+
// refs https://github.com/estree/estree/blob/master/es2020.md#importexpression
41+
if (node.type === 'ImportExpression') {
42+
modulePath = node.source;
43+
} else if (node.type === 'CallExpression') {
44+
if (node.callee.type !== 'Import') return;
45+
if (node.arguments.length !== 1) return;
46+
47+
modulePath = node.arguments[0];
48+
}
4149

42-
const modulePath = node.arguments[0];
4350
if (modulePath.type !== 'Literal') return;
4451
if (typeof modulePath.value !== 'string') return;
4552

@@ -87,6 +94,7 @@ exports.default = function visitModules(visitor, options) {
8794
'ExportNamedDeclaration': checkSource,
8895
'ExportAllDeclaration': checkSource,
8996
'CallExpression': checkImportCall,
97+
'ImportExpression': checkImportCall,
9098
});
9199
}
92100

0 commit comments

Comments
 (0)