Skip to content

Commit

Permalink
fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Dec 20, 2020
1 parent 009ed21 commit d7a1473
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/mdx/estree-to-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ module.exports = estreeToJs
const customGenerator = Object.assign({}, astring.baseGenerator, {
JSXAttribute: JSXAttribute,
JSXClosingElement: JSXClosingElement,
JSXClosingFragment: JSXClosingFragment,
JSXElement: JSXElement,
JSXEmptyExpression: JSXEmptyExpression,
JSXExpressionContainer: JSXExpressionContainer,
JSXFragment: JSXFragment,
JSXIdentifier: JSXIdentifier,
JSXMemberExpression: JSXMemberExpression,
JSXNamespacedName: JSXNamespacedName,
JSXOpeningElement: JSXOpeningElement,
JSXOpeningFragment: JSXOpeningFragment,
JSXSpreadAttribute: JSXSpreadAttribute,
JSXText: JSXText
})
Expand All @@ -36,6 +39,11 @@ function JSXClosingElement(node, state) {
this[node.name.type](node.name, state)
}

// `</>`
function JSXClosingFragment(node, state) {
state.write('</>')
}

// `<div></div>`
function JSXElement(node, state) {
state.write('<')
Expand All @@ -56,6 +64,24 @@ function JSXElement(node, state) {
}
}

// `<></>`
function JSXFragment(node, state) {
this[node.openingFragment.type](node.openingElement, state)

let index = -1

while (++index < node.children.length) {
this[node.children[index].type](node.children[index], state)
}

/* istanbul ignore if - incorrect tree. */
if (!node.closingFragment) {
throw new Error('Cannot handle fragment w/o closing tag')
}

this[node.closingFragment.type](node.closingElement, state)
}

// `{}`
function JSXEmptyExpression() {}

Expand All @@ -77,6 +103,11 @@ function JSXOpeningElement(node, state) {
}
}

// `<>`
function JSXOpeningFragment(node, state) {
state.write('<>')
}

// `div`
function JSXIdentifier(node, state) {
state.write(node.name)
Expand Down

0 comments on commit d7a1473

Please sign in to comment.