Skip to content

Commit

Permalink
Merge pull request #810 from ArtskydJ/fix-dynamic-import-template-lit…
Browse files Browse the repository at this point in the history
…eral
  • Loading branch information
rumpl committed Jul 17, 2023
2 parents c91c14c + 9a702dc commit 83e9f54
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
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

0 comments on commit 83e9f54

Please sign in to comment.