From 0e5b304ffe9e7abebfe13299118385f22c889373 Mon Sep 17 00:00:00 2001 From: Alexander Orzechowski Date: Wed, 27 Mar 2024 15:49:55 -0400 Subject: [PATCH] transform: Fix various JSX transform bugs Put it more inline with the better tested html() template literal. --- transform/htmlLiteral.js | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/transform/htmlLiteral.js b/transform/htmlLiteral.js index 0f933f1..ebc6dcb 100644 --- a/transform/htmlLiteral.js +++ b/transform/htmlLiteral.js @@ -125,17 +125,21 @@ const parse = node => { ]; const children = transformChildren(node); - - if (children.length) { - args.push(t.arrayExpression(children)); + if (children) { + args.push(createArray(children)); } return t.callExpression(t.identifier('h'), args); }; const transformChildren = node => { + if (node.type !== 'JSXFragment' && !node.closingElement) { + if (node.children.length) throw new Error("Expected no children if there is no closing element"); + return null; + } + const children = []; - let canExclude = false, newline = false, whitespace = false, cur = ''; + let canExclude = true, newline = false, whitespace = false, cur = ''; const flush = () => { if (whitespace) { @@ -178,11 +182,13 @@ const transformChildren = node => { } else if (child.type === 'JSXExpressionContainer') { flush(); - children.push(child.expression); + if (child.expression.type !== 'JSXEmptyExpression') { + children.push(child.expression); + } } else if (child.type === 'JSXFragment') { flush(); - children.push(createArray(transformChildren(child))); + children.push(...transformChildren(child)); } else { throw new Error("Unknown AST type for JSX child: " + child.type); } @@ -298,13 +304,4 @@ const transform = (source, options) => { }, source); }; -/* -console.log(transform(` - import {html} from 'h'; - import Observer from 'dude'; - - console.log(html\`<<\`); -`, {plugins: ['jsx']}).code); -*/ - export default transform;