Skip to content

Commit

Permalink
Minor refactoring (#272)
Browse files Browse the repository at this point in the history
* return early
* use a ternary
* be explicit when calling function
* use array includes
  • Loading branch information
XhmikosR authored Apr 8, 2024
1 parent a7cb86d commit a200b51
Showing 1 changed file with 19 additions and 30 deletions.
49 changes: 19 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,22 @@ function detective(src, options: detective.Options = { url: false }) {
if (file) references.push(file);
});

if (options.url) {
root.walkDecls((decl) => {
const { nodes } = parseValue(decl.value);
const files = nodes.filter(isUrlNode).map(getValueOrUrl);
if (files) {
for (const file of files) {
debug('found %s of %s', 'url() with import', file);
}
if (!options.url) return references;

root.walkDecls((decl) => {
const { nodes } = parseValue(decl.value);
const files = nodes
.filter((node) => isUrlNode(node))
.map((node) => getValueOrUrl(node));

references = references.concat(files);
if (files) {
for (const file of files) {
debug('found %s of %s', 'url() with import', file);
}
});
}

references = references.concat(files);
}
});

return references;
}
Expand All @@ -77,14 +80,8 @@ function parseValue(value: string) {
}

function getValueOrUrl(node: ChildNode) {
let ret;
if (isUrlNode(node)) {
// ['file']
const innerNode = node.nodes[0];
ret = getValue(innerNode);
} else {
ret = getValue(node);
}
// ['file']
const ret = isUrlNode(node) ? getValue(node.nodes[0]) : getValue(node);

// is-url sometimes gets data: URLs wrong
return !isUrl(ret) && !ret.startsWith('data:') && ret;
Expand All @@ -95,22 +92,14 @@ function getValue(node: ChildNode) {
throw new Error('Unexpectedly found a node without a value');
}

if (node.type === 'quoted') {
return node.contents;
}

return node.value;
return node.type === 'quoted' ? node.contents : node.value;
}

function isNodeWithValue(
node: ChildNode,
): node is Word | Numeric | Operator | Punctuation | Quoted {
return (
node.type === 'word' ||
node.type === 'numeric' ||
node.type === 'operator' ||
node.type === 'punctuation' ||
node.type === 'quoted'
return ['word', 'numeric', 'operator', 'punctuation', 'quoted'].includes(
node.type,
);
}

Expand Down

0 comments on commit a200b51

Please sign in to comment.