@@ -55,19 +55,22 @@ function detective(src, options: detective.Options = { url: false }) {
5555 if ( file ) references . push ( file ) ;
5656 } ) ;
5757
58- if ( options . url ) {
59- root . walkDecls ( ( decl ) => {
60- const { nodes } = parseValue ( decl . value ) ;
61- const files = nodes . filter ( isUrlNode ) . map ( getValueOrUrl ) ;
62- if ( files ) {
63- for ( const file of files ) {
64- debug ( 'found %s of %s' , 'url() with import' , file ) ;
65- }
58+ if ( ! options . url ) return references ;
59+
60+ root . walkDecls ( ( decl ) => {
61+ const { nodes } = parseValue ( decl . value ) ;
62+ const files = nodes
63+ . filter ( ( node ) => isUrlNode ( node ) )
64+ . map ( ( node ) => getValueOrUrl ( node ) ) ;
6665
67- references = references . concat ( files ) ;
66+ if ( files ) {
67+ for ( const file of files ) {
68+ debug ( 'found %s of %s' , 'url() with import' , file ) ;
6869 }
69- } ) ;
70- }
70+
71+ references = references . concat ( files ) ;
72+ }
73+ } ) ;
7174
7275 return references ;
7376}
@@ -77,14 +80,8 @@ function parseValue(value: string) {
7780}
7881
7982function getValueOrUrl ( node : ChildNode ) {
80- let ret ;
81- if ( isUrlNode ( node ) ) {
82- // ['file']
83- const innerNode = node . nodes [ 0 ] ;
84- ret = getValue ( innerNode ) ;
85- } else {
86- ret = getValue ( node ) ;
87- }
83+ // ['file']
84+ const ret = isUrlNode ( node ) ? getValue ( node . nodes [ 0 ] ) : getValue ( node ) ;
8885
8986 // is-url sometimes gets data: URLs wrong
9087 return ! isUrl ( ret ) && ! ret . startsWith ( 'data:' ) && ret ;
@@ -95,22 +92,14 @@ function getValue(node: ChildNode) {
9592 throw new Error ( 'Unexpectedly found a node without a value' ) ;
9693 }
9794
98- if ( node . type === 'quoted' ) {
99- return node . contents ;
100- }
101-
102- return node . value ;
95+ return node . type === 'quoted' ? node . contents : node . value ;
10396}
10497
10598function isNodeWithValue (
10699 node : ChildNode ,
107100) : node is Word | Numeric | Operator | Punctuation | Quoted {
108- return (
109- node . type === 'word' ||
110- node . type === 'numeric' ||
111- node . type === 'operator' ||
112- node . type === 'punctuation' ||
113- node . type === 'quoted'
101+ return [ 'word' , 'numeric' , 'operator' , 'punctuation' , 'quoted' ] . includes (
102+ node . type ,
114103 ) ;
115104}
116105
0 commit comments