File tree Expand file tree Collapse file tree 2 files changed +44
-5
lines changed Expand file tree Collapse file tree 2 files changed +44
-5
lines changed Original file line number Diff line number Diff line change @@ -2729,10 +2729,13 @@ type ParseConfig = {
2729
2729
} & Pick < SyntaxConfig , 'features' | 'scope' > ;
2730
2730
2731
2731
// https://github.com/csstree/csstree/blob/9de5189fadd6fb4e3a149eec0e80d6ed0d0541e5/lib/parser/create.js#L90
2732
- type ParserContext = TokenStream
2732
+ type ParserContext < AvailableNodes extends CssNodeCommon = CssNode > = TokenStream
2733
2733
& ParseConfig
2734
2734
& { config : ParseConfig }
2735
2735
& Parser
2736
+ & {
2737
+ [ K in AvailableNodes [ "type" ] ] : ( this : ParserContext , ...args : unknown [ ] ) => Extract < AvailableNodes , { type : K } > ;
2738
+ }
2736
2739
& {
2737
2740
// Anything else
2738
2741
[ key : string ] : unknown ;
Original file line number Diff line number Diff line change @@ -276,10 +276,13 @@ const customSyntax = csstree.fork({
276
276
structure : {
277
277
children : [ [ ] ]
278
278
} ,
279
- parse : ( ) => {
279
+ parse ( ) {
280
+
281
+ const dec = this . Declaration ( ) ;
282
+
280
283
return {
281
284
type : 'CustomNode3' ,
282
- value : 'hello'
285
+ value : dec . property
283
286
} ;
284
287
} ,
285
288
generate ( node : csstree . CssNode ) {
@@ -291,10 +294,13 @@ const customSyntax = csstree.fork({
291
294
}
292
295
} ,
293
296
CustomNode4 : {
294
- parse : ( ) => {
297
+ parse ( ) {
298
+
299
+ const id = this . Identifier ( ) ;
300
+
295
301
return {
296
302
type : 'CustomNode3' ,
297
- value : 'hello'
303
+ value : id . name
298
304
} ;
299
305
} ,
300
306
} ,
@@ -308,6 +314,36 @@ const customSyntax = csstree.fork({
308
314
}
309
315
} ) ;
310
316
317
+ // Parsing with custom node types
318
+
319
+ interface CustomNode extends csstree . CssNodeCommon {
320
+ type : 'CustomNode' ;
321
+ value : string ;
322
+ }
323
+
324
+ type CustomNodes = csstree . CssNode | CustomNode ;
325
+
326
+ function customParseFunction ( this : csstree . ParserContext < CustomNodes > , value : string ) {
327
+
328
+ const node = this . CustomNode ( ) ;
329
+ const id = this . Identifier ( ) ;
330
+
331
+ return {
332
+ type : 'CustomNode2' ,
333
+ value : node . value
334
+ } ;
335
+ }
336
+
337
+ const partialNodeConfig : Partial < csstree . NodeSyntaxConfig > = {
338
+ parse ( this : csstree . ParserContext < CustomNodes > , value : string ) {
339
+ const node = this . CustomNode ( ) ;
340
+ return {
341
+ type : 'CustomNode3' ,
342
+ value : node . value
343
+ } ;
344
+ } ,
345
+ }
346
+
311
347
const customAst = customSyntax . parse ( '.example { custom: value }' ) ;
312
348
customSyntax . walk ( customAst ,
313
349
( node ) => {
You can’t perform that action at this time.
0 commit comments