Skip to content

Commit

Permalink
This is just much better.
Browse files Browse the repository at this point in the history
  • Loading branch information
thesoftwarephilosopher committed Sep 6, 2024
1 parent 6a89617 commit 1946fca
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions site/unity/vanillajsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ export const babelPluginVanillaJSX: babel.PluginItem = {
JSXFragment: {
enter: (path) => {
const jsx = t.objectExpression([
t.objectProperty(jsxSymbol, t.booleanLiteral(true), true),
t.objectProperty(t.identifier('tag'), t.stringLiteral('')),
t.objectProperty(jsxSymbol, t.stringLiteral(''), true),
]);
const attrs = t.objectExpression([]);
pushChildren(jsx, path);
if (attrs.properties.length > 0) {
jsx.properties.push(t.objectProperty(t.identifier('attrs'), attrs));
}
path.replaceWith(jsx);
},
},
Expand All @@ -38,15 +33,13 @@ export const babelPluginVanillaJSX: babel.PluginItem = {
else name = t.stringLiteral(v.name);

const jsx = t.objectExpression([
t.objectProperty(jsxSymbol, t.booleanLiteral(true), true),
t.objectProperty(t.identifier('tag'), name),
t.objectProperty(jsxSymbol, name, true),
]);
const attrs = t.objectExpression([]);

if (path.node.openingElement.attributes.length > 0) {
for (const attr of path.node.openingElement.attributes) {
if (attr.type === 'JSXSpreadAttribute') {
attrs.properties.push(t.spreadElement(attr.argument));
jsx.properties.push(t.spreadElement(attr.argument));
continue;
}

Expand All @@ -66,16 +59,10 @@ export const babelPluginVanillaJSX: babel.PluginItem = {
else if (attr.value.expression.type === 'JSXEmptyExpression') throw new Error('impossible?');
else val = attr.value.expression;

attrs.properties.push(t.objectProperty(name, val));
jsx.properties.push(t.objectProperty(name, val));
}
}

pushChildren(jsx, path);

if (attrs.properties.length > 0) {
jsx.properties.push(t.objectProperty(t.identifier('attrs'), attrs));
}

path.replaceWith(jsx);
}
},
Expand Down Expand Up @@ -118,7 +105,7 @@ function pushChildren(parent: babel.types.ObjectExpression, path: babel.NodePath
parent.properties.push(t.objectProperty(t.identifier("children"), child.argument));
}
else {
parent.properties.push(t.objectProperty(t.identifier("child"), child));
parent.properties.push(t.objectProperty(t.identifier("children"), child));
}
}
else if (children.length > 0) {
Expand Down

0 comments on commit 1946fca

Please sign in to comment.