Skip to content

Commit 96ed3c7

Browse files
aladdin-addljharb
authored andcommitted
1 parent ab82094 commit 96ed3c7

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-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])
@@ -765,6 +766,7 @@ for info on changes for earlier releases.
765766

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

769+
[#2026]: https://github.com/benmosher/eslint-plugin-import/pull/2026
768770
[#2022]: https://github.com/benmosher/eslint-plugin-import/pull/2022
769771
[#2021]: https://github.com/benmosher/eslint-plugin-import/pull/2021
770772
[#1997]: https://github.com/benmosher/eslint-plugin-import/pull/1997
@@ -1357,3 +1359,4 @@ for info on changes for earlier releases.
13571359
[@grit96]: https://github.com/grit96
13581360
[@lilling]: https://github.com/lilling
13591361
[@silviogutierrez]: https://github.com/silviogutierrez
1362+
[@aladdin-add]: https://github.com/aladdin-add

tests/src/rules/no-unresolved.js

+18-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,20 @@ 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+
parserOptions: { ecmaVersion: 2021 },
391+
})) || [],
392+
),
393+
invalid: [].concat(
394+
testVersion('>=7', () => ({
395+
code: 'import("./does-not-exist-l0w9ssmcqy9").then(() => {})',
396+
parserOptions: { ecmaVersion: 2021 },
397+
errors: ["Unable to resolve path to module './does-not-exist-l0w9ssmcqy9'."],
398+
})) || [],
399+
),
400+
});

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)