diff --git a/src/index.ts b/src/index.ts index 167acfe..868e005 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; } @@ -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; @@ -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, ); }