Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] no-unresolved: check import() #2026

Merged
merged 1 commit into from May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -29,6 +29,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`no-extraneous-dependencies`]: Exclude flow `typeof` imports ([#1534], thanks [@devongovett])
- [`newline-after-import`]: respect decorator annotations ([#1985], thanks [@lilling])
- [`no-restricted-paths`]: enhance performance for zones with `except` paths ([#2022], thanks [@malykhinvi])
- [`no-unresolved`]: check import() ([#2026], thanks [@aladdin-add])

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

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

[#2026]: https://github.com/benmosher/eslint-plugin-import/pull/2026
[#2022]: https://github.com/benmosher/eslint-plugin-import/pull/2022
[#2021]: https://github.com/benmosher/eslint-plugin-import/pull/2021
[#1997]: https://github.com/benmosher/eslint-plugin-import/pull/1997
Expand Down Expand Up @@ -1357,3 +1359,4 @@ for info on changes for earlier releases.
[@grit96]: https://github.com/grit96
[@lilling]: https://github.com/lilling
[@silviogutierrez]: https://github.com/silviogutierrez
[@aladdin-add]: https://github.com/aladdin-add
20 changes: 18 additions & 2 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 Down Expand Up @@ -93,7 +93,6 @@ function runResolverTests(resolver) {
'\'./reallyfake/module\'.' }],
}),


rest({
code: "import bar from './baz';",
errors: [{ message: "Unable to resolve path to module './baz'.",
Expand Down Expand Up @@ -382,3 +381,20 @@ ruleTester.run('no-unresolved syntax verification', rule, {
valid: SYNTAX_CASES,
invalid:[],
});

// https://github.com/benmosher/eslint-plugin-import/issues/2024
ruleTester.run('import() with built-in parser', rule, {
valid: [].concat(
testVersion('>=7', () => ({
code: "import('fs');",
parserOptions: { ecmaVersion: 2021 },
})) || [],
),
invalid: [].concat(
testVersion('>=7', () => ({
code: 'import("./does-not-exist-l0w9ssmcqy9").then(() => {})',
parserOptions: { ecmaVersion: 2021 },
errors: ["Unable to resolve path to module './does-not-exist-l0w9ssmcqy9'."],
})) || [],
),
});
14 changes: 11 additions & 3 deletions utils/moduleVisitor.js
Expand Up @@ -36,10 +36,17 @@ exports.default = function visitModules(visitor, options) {

// for esmodule dynamic `import()` calls
function checkImportCall(node) {
if (node.callee.type !== 'Import') return;
if (node.arguments.length !== 1) return;
let modulePath;
// refs https://github.com/estree/estree/blob/master/es2020.md#importexpression
if (node.type === 'ImportExpression') {
modulePath = node.source;
} else if (node.type === 'CallExpression') {
if (node.callee.type !== 'Import') return;
if (node.arguments.length !== 1) return;

modulePath = node.arguments[0];
}

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

Expand Down Expand Up @@ -87,6 +94,7 @@ exports.default = function visitModules(visitor, options) {
'ExportNamedDeclaration': checkSource,
'ExportAllDeclaration': checkSource,
'CallExpression': checkImportCall,
'ImportExpression': checkImportCall,
});
}

Expand Down