@@ -19,6 +19,9 @@ import detectIndent from 'detect-indent'
19
19
import isObject from '../../../util/isObject'
20
20
import { cssObjToAst } from '../../util/cssObjToAst'
21
21
import dset from 'dset'
22
+ import selectorParser from 'postcss-selector-parser'
23
+ import { logFull } from '../../util/logFull'
24
+ import { flatten } from '../../../util/array'
22
25
23
26
export async function provideInvalidApplyCodeActions (
24
27
state : State ,
@@ -119,7 +122,8 @@ export async function provideInvalidApplyCodeActions(
119
122
new RegExp ( outputIndent , 'g' ) ,
120
123
documentIndent . indent
121
124
)
122
- } ) ,
125
+ } )
126
+ . replace ( / ^ ( \s + ) ( .* ?[ ^ { } ] \n ) ( \S ) / gm, '$1$2$1$3' ) ,
123
127
} )
124
128
125
129
return false
@@ -205,9 +209,12 @@ function classNameToAst(
205
209
for ( let i = 1 ; i <= path . length ; i ++ ) {
206
210
dset ( obj , path . slice ( 0 , i ) , { } )
207
211
}
212
+
213
+ selector = appendPseudosToSelector ( selector , pseudo )
214
+ if ( selector === null ) return null
215
+
208
216
let rule = {
209
- // TODO: use proper selector parser
210
- [ selector + pseudo . join ( '' ) ] : {
217
+ [ selector ] : {
211
218
[ `@apply ${ classNameParts [ classNameParts . length - 1 ] } ${
212
219
important ? ' !important' : ''
213
220
} `] : '' ,
@@ -221,3 +228,38 @@ function classNameToAst(
221
228
222
229
return cssObjToAst ( obj , state . modules . postcss )
223
230
}
231
+
232
+ function appendPseudosToSelector (
233
+ selector : string ,
234
+ pseudos : string [ ]
235
+ ) : string | null {
236
+ if ( pseudos . length === 0 ) return selector
237
+
238
+ let canTransform = true
239
+
240
+ let transformedSelector = selectorParser ( ( selectors ) => {
241
+ flatten ( selectors . split ( ( _ ) => true ) ) . forEach ( ( sel ) => {
242
+ // @ts -ignore
243
+ for ( let i = sel . nodes . length - 1 ; i >= 0 ; i -- ) {
244
+ // @ts -ignore
245
+ if ( sel . nodes [ i ] . type !== 'pseudo' ) {
246
+ break
247
+ // @ts -ignore
248
+ } else if ( pseudos . includes ( sel . nodes [ i ] . value ) ) {
249
+ canTransform = false
250
+ break
251
+ }
252
+ }
253
+ if ( canTransform ) {
254
+ pseudos . forEach ( ( p ) => {
255
+ // @ts -ignore
256
+ sel . append ( selectorParser . pseudo ( { value : p } ) )
257
+ } )
258
+ }
259
+ } )
260
+ } ) . processSync ( selector )
261
+
262
+ if ( ! canTransform ) return null
263
+
264
+ return transformedSelector
265
+ }
0 commit comments