Skip to content

Commit

Permalink
fix: dynamic import & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Apr 12, 2021
1 parent 8424548 commit 2420b04
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 7 additions & 0 deletions tests/src/rules/no-unresolved.js
Expand Up @@ -141,6 +141,13 @@ function runResolverTests(resolver) {
errors: ["Unable to resolve path to module './does-not-exist'."],
}),

// refs: https://github.com/benmosher/eslint-plugin-import/issues/2024
rest({
code: 'import("./does-not-exist").then(() => {})',
parserOptions: { ecmaVersion: 2020 },
errors: ["Unable to resolve path to module './does-not-exist'."],
}),

// commonjs setting
rest({
code: 'var bar = require("./baz")',
Expand Down
13 changes: 12 additions & 1 deletion utils/moduleVisitor.js
Expand Up @@ -36,7 +36,17 @@ exports.default = function visitModules(visitor, options) {

// for esmodule dynamic `import()` calls
function checkImportCall(node) {
const modulePath = node.source;
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];
}

if (modulePath.type !== 'Literal') return;
if (typeof modulePath.value !== 'string') return;

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

0 comments on commit 2420b04

Please sign in to comment.