Skip to content

Commit 124dad8

Browse files
authored
Merge branch 'master' into comment-support
2 parents 315705f + 4d50d16 commit 124dad8

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

packages/babel-plugin-htm/index.mjs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,14 @@ export default function htmBabelPlugin({ types: t }, options = {}) {
134134
}
135135

136136
function transform(node, state) {
137-
if (node === undefined) return t.identifier('undefined');
138-
if (node === null) return t.nullLiteral();
137+
if (t.isNode(node)) return node;
138+
if (typeof node === 'string') return stringValue(node);
139+
if (typeof node === 'undefined') return t.identifier('undefined');
139140

140141
const { tag, props, children } = node;
141-
function childMapper(child) {
142-
if (typeof child==='string') {
143-
return stringValue(child);
144-
}
145-
return t.isNode(child) ? child : transform(child, state);
146-
}
147142
const newTag = typeof tag === 'string' ? t.stringLiteral(tag) : tag;
148143
const newProps = spreadNode(props, state);
149-
const newChildren = t.arrayExpression(children.map(childMapper));
144+
const newChildren = t.arrayExpression(children.map(child => transform(child, state)));
150145
return createVNode(newTag, newProps, newChildren);
151146
}
152147

test/index.test.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ describe('htm', () => {
119119
expect(html`<a><//>`).toEqual({ tag: 'a', props: null, children: [] });
120120
});
121121

122+
test('non-element roots', () => {
123+
expect(html`foo`).toEqual('foo');
124+
expect(html`${1}`).toEqual(1);
125+
expect(html`foo${1}`).toEqual(['foo', 1]);
126+
expect(html`foo${1}bar`).toEqual(['foo', 1, 'bar']);
127+
});
128+
122129
test('text child', () => {
123130
expect(html`<a>foo</a>`).toEqual({ tag: 'a', props: null, children: ['foo'] });
124131
expect(html`<a>foo bar</a>`).toEqual({ tag: 'a', props: null, children: ['foo bar'] });
@@ -182,7 +189,7 @@ describe('htm', () => {
182189
expect(html`<a>${''}9aaaaaaaaa${''}</a>`).not.toEqual(html`<a>${''}0${''}aaaaaaaaa${''}</a>`);
183190
expect(html`<a>${''}0${''}aaaaaaaa${''}</a>`).not.toEqual(html`<a>${''}.8aaaaaaaa${''}</a>`);
184191
});
185-
192+
186193
test('do not mutate spread variables', () => {
187194
const obj = {};
188195
html`<a ...${obj} b="1" />`;

0 commit comments

Comments
 (0)