Skip to content

Commit 312d921

Browse files
committed
Babel: fix child iteration erroring out on nested arrays of children
1 parent bdff308 commit 312d921

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/babel.mjs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export default function htmBabelPlugin({ types: t }, options = {}) {
8080
};
8181
}
8282

83-
function mapChildren(child, index, children) {
83+
function childMapper(child, index, children) {
8484
// JSX-style whitespace: (@TODO: remove? doesn't match the browser version)
8585
if (typeof child==='string' && child.trim().length===0 || child==null) {
8686
if (index===0 || index===children.length-1) return null;
@@ -94,7 +94,7 @@ export default function htmBabelPlugin({ types: t }, options = {}) {
9494
return child;
9595
}
9696

97-
function h(tag, props, ...children) {
97+
function h(tag, props) {
9898
if (typeof tag==='string') {
9999
const matches = tag.match(/\$\$\$_h_\[(\d+)\]/);
100100
if (matches) tag = currentExpressions[matches[1]];
@@ -129,10 +129,26 @@ export default function htmBabelPlugin({ types: t }, options = {}) {
129129
return t.objectProperty(propertyName(key), value);
130130
})
131131
);
132-
133-
if (Array.isArray(children)) {
134-
children = t.arrayExpression(children.map(mapChildren).filter(Boolean));
132+
133+
// recursive iteration of possibly nested arrays of children.
134+
let children = [];
135+
if (arguments.length>2) {
136+
const stack = [];
137+
// eslint-disable-next-line prefer-rest-params
138+
for (let i=arguments.length; i-->2; ) stack.push(arguments[i]);
139+
while (stack.length) {
140+
const child = stack.pop();
141+
if (Array.isArray(child)) {
142+
for (let i=child.length; i--; ) stack.push(child[i]);
143+
}
144+
else if (child!=null) {
145+
children.push(child);
146+
}
147+
}
148+
children = children.map(childMapper).filter(Boolean);
135149
}
150+
children = t.arrayExpression(children);
151+
136152
return createVNode(tag, propsNode, children);
137153
}
138154

0 commit comments

Comments
 (0)