Skip to content

Commit

Permalink
index: use one function for extracting dependencies (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR authored Jul 26, 2024
1 parent 0667ba5 commit ddf303f
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ module.exports = function detective(content, options = {}) {

walker.walk(ast, node => {
if (isImportStatement(node)) {
dependencies = [...dependencies, ...extractDependencies(node)];
dependencies = [...dependencies, ...extractDependencies(node, ['string', 'ident'])];
return;
}

if (options?.url && node.type === 'uri') {
dependencies = [...dependencies, ...extractUriDependencies(node)];
dependencies = [...dependencies, ...extractDependencies(node, ['string', 'ident', 'raw'])];
}
});

Expand All @@ -57,14 +57,8 @@ function isImportStatement(node) {
return ['ident', 'import'].includes(atKeyword.content[0].type);
}

function extractDependencies(importStatementNode) {
function extractDependencies(importStatementNode, innerNodeTypes) {
return importStatementNode.content
.filter(innerNode => ['string', 'ident'].includes(innerNode.type))
.map(identifierNode => identifierNode.content.replaceAll(/["']/g, ''));
}

function extractUriDependencies(importStatementNode) {
return importStatementNode.content
.filter(innerNode => ['string', 'ident', 'raw'].includes(innerNode.type))
.filter(innerNode => innerNodeTypes.includes(innerNode.type))
.map(identifierNode => identifierNode.content.replaceAll(/["']/g, ''));
}

0 comments on commit ddf303f

Please sign in to comment.