File tree Expand file tree Collapse file tree 2 files changed +54
-2
lines changed Expand file tree Collapse file tree 2 files changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ function toEstree(tree, options) {
42
42
handle : zwitch ( 'type' , {
43
43
invalid : invalid ,
44
44
unknown : unknown ,
45
- handlers : handlers
45
+ handlers : Object . assign ( { } , handlers , options && options . handlers )
46
46
} )
47
47
}
48
48
var result = context . handle ( tree , context )
@@ -417,7 +417,9 @@ function all(parent, context) {
417
417
418
418
while ( ++ index < children . length ) {
419
419
result = context . handle ( children [ index ] , context )
420
- if ( result ) {
420
+ if ( Array . isArray ( result ) ) {
421
+ results = results . concat ( result )
422
+ } else if ( result ) {
421
423
results . push ( result )
422
424
}
423
425
}
Original file line number Diff line number Diff line change @@ -443,6 +443,56 @@ test('hast-util-to-estree', function (t) {
443
443
'should support an custom `mdxJsxTextElement` node w/o name, attributes, or children'
444
444
)
445
445
446
+ t . deepEqual (
447
+ toEstree (
448
+ {
449
+ type : 'root' ,
450
+ children : [
451
+ {
452
+ type : 'array' ,
453
+ value : 'comma,seperated,array'
454
+ }
455
+ ]
456
+ } ,
457
+ {
458
+ handlers : {
459
+ array : ( node ) => {
460
+ var elements = node . value . split ( ',' ) . map ( ( v ) => ( {
461
+ type : 'Literal' ,
462
+ value : v
463
+ } ) )
464
+ return elements
465
+ }
466
+ }
467
+ }
468
+ ) ,
469
+ {
470
+ type : 'Program' ,
471
+ body : [
472
+ {
473
+ type : 'ExpressionStatement' ,
474
+ expression : {
475
+ type : 'JSXFragment' ,
476
+ openingFragment : {
477
+ type : 'JSXOpeningFragment' ,
478
+ attributes : [ ] ,
479
+ selfClosing : false
480
+ } ,
481
+ closingFragment : { type : 'JSXClosingFragment' } ,
482
+ children : [
483
+ { type : 'Literal' , value : 'comma' } ,
484
+ { type : 'Literal' , value : 'seperated' } ,
485
+ { type : 'Literal' , value : 'array' }
486
+ ]
487
+ }
488
+ }
489
+ ] ,
490
+ sourceType : 'module' ,
491
+ comments : [ ]
492
+ } ,
493
+ 'should support custom handler that returns an array'
494
+ )
495
+
446
496
t . end ( )
447
497
} )
448
498
You can’t perform that action at this time.
0 commit comments