@@ -31,6 +31,37 @@ function shouldHandleRule(rule, node, result) {
3131 return true ;
3232}
3333
34+ function getWebpackIgnoreCommentValue ( index , nodes , inBetween ) {
35+ if ( index === 0 && typeof inBetween !== "undefined" ) {
36+ return inBetween ;
37+ }
38+
39+ let prevValueNode = nodes [ index - 1 ] ;
40+
41+ if ( ! prevValueNode ) {
42+ // eslint-disable-next-line consistent-return
43+ return ;
44+ }
45+
46+ if ( prevValueNode . type === "space" ) {
47+ if ( ! nodes [ index - 2 ] ) {
48+ // eslint-disable-next-line consistent-return
49+ return ;
50+ }
51+
52+ prevValueNode = nodes [ index - 2 ] ;
53+ }
54+
55+ if ( prevValueNode . type !== "comment" ) {
56+ // eslint-disable-next-line consistent-return
57+ return ;
58+ }
59+
60+ const matched = prevValueNode . value . match ( webpackIgnoreCommentRegexp ) ;
61+
62+ return matched && matched [ 2 ] === "true" ;
63+ }
64+
3465function visitor ( result , parsedResults , node , key ) {
3566 if ( ! needParseDeclaration . test ( node [ key ] ) ) {
3667 return ;
@@ -40,7 +71,7 @@ function visitor(result, parsedResults, node, key) {
4071 typeof node . raws . value === "undefined" ? node [ key ] : node . raws . value . raw
4172 ) ;
4273
43- let needIgnore ;
74+ let inBetween ;
4475
4576 if ( typeof node . raws . between !== "undefined" ) {
4677 const lastCommentIndex = node . raws . between . lastIndexOf ( "/*" ) ;
@@ -50,7 +81,7 @@ function visitor(result, parsedResults, node, key) {
5081 . match ( webpackIgnoreCommentRegexp ) ;
5182
5283 if ( matched ) {
53- needIgnore = matched [ 2 ] === "true" ;
84+ inBetween = matched [ 2 ] === "true" ;
5485 }
5586 }
5687
@@ -66,30 +97,26 @@ function visitor(result, parsedResults, node, key) {
6697 }
6798 }
6899
69- parsed . walk ( ( valueNode ) => {
70- if ( valueNode . type === "comment" ) {
71- const matched = valueNode . value . match ( webpackIgnoreCommentRegexp ) ;
72-
73- needIgnore = matched && matched [ 2 ] === "true" ;
74-
75- return ;
76- }
100+ let needIgnore ;
77101
102+ parsed . walk ( ( valueNode , index , valueNodes ) => {
78103 if ( valueNode . type !== "function" ) {
79104 return ;
80105 }
81106
82- if ( isIgnoreOnDeclaration && needIgnore !== false ) {
83- return ;
84- }
107+ if ( isUrlFunc . test ( valueNode . value ) ) {
108+ needIgnore = getWebpackIgnoreCommentValue ( index , valueNodes , inBetween ) ;
85109
86- if ( needIgnore ) {
87- // eslint-disable-next-line no-undefined
88- needIgnore = undefined ;
89- return ;
90- }
110+ if ( isIgnoreOnDeclaration && needIgnore !== false ) {
111+ return ;
112+ }
113+
114+ if ( needIgnore ) {
115+ // eslint-disable-next-line no-undefined
116+ needIgnore = undefined ;
117+ return ;
118+ }
91119
92- if ( isUrlFunc . test ( valueNode . value ) ) {
93120 const { nodes } = valueNode ;
94121 const isStringValue = nodes . length !== 0 && nodes [ 0 ] . type === "string" ;
95122 const url = isStringValue ? nodes [ 0 ] . value : valueParser . stringify ( nodes ) ;
@@ -109,25 +136,23 @@ function visitor(result, parsedResults, node, key) {
109136 // eslint-disable-next-line consistent-return
110137 return false ;
111138 } else if ( isImageSetFunc . test ( valueNode . value ) ) {
112- let imageSetWebpackIgnore = false ;
113-
114- for ( const nNode of valueNode . nodes ) {
139+ for ( const [ innerIndex , nNode ] of valueNode . nodes . entries ( ) ) {
115140 const { type, value } = nNode ;
116141
117- if ( type === "comment" ) {
118- const matched = value . match ( webpackIgnoreCommentRegexp ) ;
142+ if ( type === "function" && isUrlFunc . test ( value ) ) {
143+ needIgnore = getWebpackIgnoreCommentValue (
144+ innerIndex ,
145+ valueNode . nodes
146+ ) ;
119147
120- if ( matched ) {
121- imageSetWebpackIgnore = matched [ 2 ] === "true" ;
148+ if ( isIgnoreOnDeclaration && needIgnore !== false ) {
149+ // eslint-disable-next-line no-continue
150+ continue ;
122151 }
123152
124- // eslint-disable-next-line no-continue
125- continue ;
126- }
127-
128- if ( type === "function" && isUrlFunc . test ( value ) ) {
129- if ( imageSetWebpackIgnore ) {
130- imageSetWebpackIgnore = false ;
153+ if ( needIgnore ) {
154+ // eslint-disable-next-line no-undefined
155+ needIgnore = undefined ;
131156 // eslint-disable-next-line no-continue
132157 continue ;
133158 }
@@ -154,8 +179,19 @@ function visitor(result, parsedResults, node, key) {
154179 } ) ;
155180 }
156181 } else if ( type === "string" ) {
157- if ( imageSetWebpackIgnore ) {
158- imageSetWebpackIgnore = false ;
182+ needIgnore = getWebpackIgnoreCommentValue (
183+ innerIndex ,
184+ valueNode . nodes
185+ ) ;
186+
187+ if ( isIgnoreOnDeclaration && needIgnore !== false ) {
188+ // eslint-disable-next-line no-continue
189+ continue ;
190+ }
191+
192+ if ( needIgnore ) {
193+ // eslint-disable-next-line no-undefined
194+ needIgnore = undefined ;
159195 // eslint-disable-next-line no-continue
160196 continue ;
161197 }
0 commit comments