Skip to content

Commit

Permalink
Implement JSXFragment (#200)
Browse files Browse the repository at this point in the history
* Implement JSXFragment

* Add `Fragment` to generated header

* Only add JSX Fragment when it was encountered
  • Loading branch information
kungfooman committed Jul 22, 2024
1 parent c1b1816 commit 25e787d
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src-transpiler/Stringifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,17 @@ class Stringifier {
}
return out;
}
encounteredFragment = false;
/**
* @returns {string} The header.
*/
getHeader() {
if (!this.addReactImport) {
return '';
}
if (this.encounteredFragment) {
return "import {createElement, Fragment} from 'react';\n";
}
return "import {createElement} from 'react';\n";
}
get parent() {
Expand Down Expand Up @@ -1133,6 +1137,42 @@ class Stringifier {
out += ')';
return out;
}
/**
* @param {import("@babel/types").JSXFragment} node - The Babel AST node.
* @returns {string} Stringification of the node.
*/
JSXFragment(node) {
const {/*openingFragment, closingFragment,*/ children} = node;
this.encounteredFragment = true;
// console.log('JSXFragment', {openingFragment, closingFragment, children});
let {spaces} = this;
let out = 'createElement(\n';
this.numSpaces++;
spaces = this.spaces;
out += spaces;
out += 'Fragment,\n';
out += spaces;
out += 'null';
// Same code for children as in JSXElement
if (children.length) {
out += ',\n';
for (const child of children) {
const source = this.toSource(child);
if (!source.length) {
continue;
}
out += spaces;
out += source;
out += ',\n';
}
} else {
out += '\n';
}
this.numSpaces--;
out += this.spaces;
out += ')';
return out;
}
/**
* @param {import("@babel/types").JSXIdentifier} node - The Babel AST node.
* @returns {string} Stringification of the node.
Expand Down

0 comments on commit 25e787d

Please sign in to comment.