Skip to content

Commit 477c211

Browse files
committed
Normalize children with @babel/types react.buildChildren
1 parent 6420110 commit 477c211

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

packages/babel-plugin-transform-jsx-to-tagged-templates/src/index.mjs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default function jsxToTaggedTemplatesBabelPlugin({ types: t }, options =
5454
const { name } = open.name;
5555

5656
const toProcess = [];
57-
57+
5858
if (name.match(/^[A-Z]/)) {
5959
raw('<');
6060
expr(t.identifier(name));
@@ -90,25 +90,21 @@ export default function jsxToTaggedTemplatesBabelPlugin({ types: t }, options =
9090
}
9191
}
9292

93-
if (htmlOutput || node.children && node.children.length !== 0) {
93+
const children = t.react.buildChildren(node);
94+
if (htmlOutput || children && children.length !== 0) {
9495
raw('>');
95-
for (let i = 0; i < node.children.length; i++) {
96-
let child = node.children[i];
97-
if (t.isJSXText(child)) {
96+
for (let i = 0; i < children.length; i++) {
97+
let child = children[i];
98+
if (t.isStringLiteral(child)) {
9899
// @todo - expose `whitespace: true` option?
99-
raw(child.value.trim());
100+
raw(child.value);
101+
}
102+
else if (t.isJSXElement(child)) {
103+
processNode(child);
100104
}
101105
else {
102-
if (t.isJSXExpressionContainer(child)) {
103-
child = child.expression;
104-
}
105-
if (t.isJSXElement(child)) {
106-
processNode(child);
107-
}
108-
else {
109-
expr(child);
110-
toProcess.push(child);
111-
}
106+
expr(child);
107+
toProcess.push(child);
112108
}
113109
}
114110

packages/babel-plugin-transform-jsx-to-tagged-templates/test/index.test.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,23 @@ describe('babel-plugin-transform-jsx-to-tagged-templates', () => {
8585
).toBe('const Foo = () => html`<div class="foo" draggable><h1>Hello</h1><p>world.</p></div>`;');
8686
});
8787

88-
test('inter-element whitespace is collapsed', () => {
88+
test('inter-element whitespace is collapsed similarly to the JSX plugin', () => {
8989
expect(
90-
compile('const Foo = props => <div a b> a <em> b </em> c <strong> d </strong> e </div>;')
91-
).toBe('const Foo = props => html`<div a b>a<em>b</em>c<strong>d</strong>e</div>`;');
90+
compile('const Foo = props => <div a b> a \n <em> b \n B </em> c <strong> d </strong> e </div>;')
91+
).toBe('const Foo = props => html`<div a b> a<em> b B </em> c <strong> d </strong> e </div>`;');
9292
});
93-
93+
9494
test('nested JSX Expressions produce nested templates', () => {
9595
expect(
9696
compile('const Foo = props => <ul>{props.items.map(item =>\n <li>\n {item}\n </li>\n)}</ul>;')
9797
).toBe('const Foo = props => html`<ul>${props.items.map(item => html`<li>${item}</li>`)}</ul>`;');
9898
});
99+
100+
test('empty expressions are ignored', () => {
101+
expect(
102+
compile(`(<div>{/* a comment */}</div>);`)
103+
).toBe('html`<div/>`;');
104+
});
99105
});
100106

101107
describe('integration with babel-plugin-jsx-pragmatic', () => {

0 commit comments

Comments
 (0)