diff --git a/packages/babel-plugin-htm/index.mjs b/packages/babel-plugin-htm/index.mjs index 3452736..3b32406 100644 --- a/packages/babel-plugin-htm/index.mjs +++ b/packages/babel-plugin-htm/index.mjs @@ -134,19 +134,14 @@ export default function htmBabelPlugin({ types: t }, options = {}) { } function transform(node, state) { - if (node === undefined) return t.identifier('undefined'); - if (node === null) return t.nullLiteral(); + if (t.isNode(node)) return node; + if (typeof node === 'string') return stringValue(node); + if (typeof node === 'undefined') return t.identifier('undefined'); const { tag, props, children } = node; - function childMapper(child) { - if (typeof child==='string') { - return stringValue(child); - } - return t.isNode(child) ? child : transform(child, state); - } const newTag = typeof tag === 'string' ? t.stringLiteral(tag) : tag; const newProps = spreadNode(props, state); - const newChildren = t.arrayExpression(children.map(childMapper)); + const newChildren = t.arrayExpression(children.map(child => transform(child, state))); return createVNode(newTag, newProps, newChildren); } diff --git a/test/index.test.mjs b/test/index.test.mjs index 55a613b..65b2f67 100644 --- a/test/index.test.mjs +++ b/test/index.test.mjs @@ -119,6 +119,13 @@ describe('htm', () => { expect(html``).toEqual({ tag: 'a', props: null, children: [] }); }); + test('non-element roots', () => { + expect(html`foo`).toEqual('foo'); + expect(html`${1}`).toEqual(1); + expect(html`foo${1}`).toEqual(['foo', 1]); + expect(html`foo${1}bar`).toEqual(['foo', 1, 'bar']); + }); + test('text child', () => { expect(html`foo`).toEqual({ tag: 'a', props: null, children: ['foo'] }); expect(html`foo bar`).toEqual({ tag: 'a', props: null, children: ['foo bar'] }); @@ -182,7 +189,7 @@ describe('htm', () => { expect(html`${''}9aaaaaaaaa${''}`).not.toEqual(html`${''}0${''}aaaaaaaaa${''}`); expect(html`${''}0${''}aaaaaaaa${''}`).not.toEqual(html`${''}.8aaaaaaaa${''}`); }); - + test('do not mutate spread variables', () => { const obj = {}; html``;