Skip to content

Commit 9d4e901

Browse files
authored
Add support for handlers
Closes GH-3.
1 parent f144d87 commit 9d4e901

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function toEstree(tree, options) {
4242
handle: zwitch('type', {
4343
invalid: invalid,
4444
unknown: unknown,
45-
handlers: handlers
45+
handlers: Object.assign({}, handlers, options && options.handlers)
4646
})
4747
}
4848
var result = context.handle(tree, context)
@@ -417,7 +417,9 @@ function all(parent, context) {
417417

418418
while (++index < children.length) {
419419
result = context.handle(children[index], context)
420-
if (result) {
420+
if (Array.isArray(result)) {
421+
results = results.concat(result)
422+
} else if (result) {
421423
results.push(result)
422424
}
423425
}

test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,56 @@ test('hast-util-to-estree', function (t) {
443443
'should support an custom `mdxJsxTextElement` node w/o name, attributes, or children'
444444
)
445445

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+
446496
t.end()
447497
})
448498

0 commit comments

Comments
 (0)