Skip to content

Commit

Permalink
transform: Fix various JSX transform bugs
Browse files Browse the repository at this point in the history
Put it more inline with the better tested html() template literal.
  • Loading branch information
Nefsen402 committed Mar 27, 2024
1 parent 22c58ae commit 0e5b304
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions transform/htmlLiteral.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;

0 comments on commit 0e5b304

Please sign in to comment.