Skip to content

Commit

Permalink
fix: no-unresolved check import() (fixes #2024)
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed May 11, 2021
1 parent 2057c05 commit 2b05677
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
22 changes: 20 additions & 2 deletions tests/src/rules/no-unresolved.js
Original file line number Diff line number Diff line change
@@ -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,22 @@ 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');",
parser: require.resolve('espree'),
parserOptions: { ecmaVersion: 2021 },
})) || [],
),
invalid: [].concat(
testVersion('>=7', () => ({
code: 'import("./does-not-exist").then(() => {})',
parser: require.resolve('espree'),
parserOptions: { ecmaVersion: 2021 },
errors: ["Unable to resolve path to module './does-not-exist'."],
})) || [],
),
});
14 changes: 11 additions & 3 deletions utils/moduleVisitor.js
Original file line number Diff line number Diff line change
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

0 comments on commit 2b05677

Please sign in to comment.