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 dynamic import with a template literal #779 #810

Merged
merged 1 commit into from
Jul 17, 2023
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ test/fake_modules/node_modules/eslint-config-foo-bar/
test/fake_modules/eslint_config_js/
test/fake_modules/import_function/
test/fake_modules/import_function_missing/
test/fake_modules/import_function_template_literal/
test/fake_modules/import_function_webpack/
21 changes: 16 additions & 5 deletions src/detector/importCallExpression.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import lodash from 'lodash';

export default function importCallExpression(node) {
return node.type === 'CallExpression' &&
if (
node.type === 'CallExpression' &&
node.callee &&
((node.callee.type === 'Identifier' && node.callee.name === 'import') ||
node.callee.type === 'Import' ||
Expand All @@ -10,8 +11,18 @@ export default function importCallExpression(node) {
node.callee.object.name === 'System' &&
node.callee.property &&
node.callee.property.name === 'import')) &&
node.arguments[0] &&
lodash.isString(node.arguments[0].value)
? [node.arguments[0].value]
: [];
node.arguments[0]
) {
if (lodash.isString(node.arguments[0].value)) {
return [node.arguments[0].value];
}
if (
node.arguments[0].type === 'TemplateLiteral' &&
node.arguments[0].quasis.length === 1 &&
lodash.isString(node.arguments[0].quasis[0].value.raw)
) {
return [node.arguments[0].quasis[0].value.raw];
}
}
return [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import(`optimist`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"optimist": "~0.6.0"
}
}
14 changes: 14 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ export default [
},
expectedErrorCode: 0,
},
{
name: 'find module for dynamic import(`template-literal`) when present',
module: 'import_function_template_literal',
options: {},
expected: {
dependencies: [],
devDependencies: [],
missing: {},
using: {
optimist: ['index.js'],
},
},
expectedErrorCode: 0,
},
{
name: 'find module for dynamic import() with magic Webpack comment',
module: 'import_function_webpack',
Expand Down