Skip to content

Commit 8789753

Browse files
aladdin-addljharb
authored andcommitted
1 parent 2057c05 commit 8789753

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

CHANGELOG.md

+3
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 import() ([#2026], thanks [@aladdin-add])
3233

3334
### Changed
3435
- [Generic Import Callback] Make callback for all imports once in rules ([#1237], thanks [@ljqx])
@@ -764,6 +765,7 @@ for info on changes for earlier releases.
764765

765766
[`memo-parser`]: ./memo-parser/README.md
766767

768+
[#2026]: https://github.com/benmosher/eslint-plugin-import/pull/2026
767769
[#2022]: https://github.com/benmosher/eslint-plugin-import/pull/2022
768770
[#2021]: https://github.com/benmosher/eslint-plugin-import/pull/2021
769771
[#1997]: https://github.com/benmosher/eslint-plugin-import/pull/1997
@@ -1355,3 +1357,4 @@ for info on changes for earlier releases.
13551357
[@s-h-a-d-o-w]: https://github.com/s-h-a-d-o-w
13561358
[@grit96]: https://github.com/grit96
13571359
[@lilling]: https://github.com/lilling
1360+
[@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: [
388+
testVersion('>=7', () => ({
389+
code: "import('fs');",
390+
parser: require.resolve('espree'),
391+
parserOptions: { ecmaVersion: 2021 },
392+
})),
393+
],
394+
invalid: [
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)