Skip to content

Commit a7357c4

Browse files
committed
Output self-closing tags
1 parent 02e147a commit a7357c4

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ export default function jsxToTaggedTemplatesBabelPlugin({ types: t }, options =
8181
}
8282
}
8383
}
84-
raw('>');
8584

86-
if (node.children) {
85+
if (node.children && node.children.length !== 0) {
86+
raw('>');
8787
for (let i = 0; i < node.children.length; i++) {
8888
let child = node.children[i];
8989
if (t.isJSXText(child)) {
@@ -102,17 +102,20 @@ export default function jsxToTaggedTemplatesBabelPlugin({ types: t }, options =
102102
}
103103
}
104104
}
105-
}
106105

107-
if (name.match(/^[A-Z]/)) {
108-
raw('</');
109-
expr(t.identifier(name));
110-
raw('>');
106+
if (name.match(/^[A-Z]/)) {
107+
raw('</');
108+
expr(t.identifier(name));
109+
raw('>');
110+
}
111+
else {
112+
raw('</');
113+
raw(name);
114+
raw('>');
115+
}
111116
}
112117
else {
113-
raw('</');
114-
raw(name);
115-
raw('>');
118+
raw('/>');
116119
}
117120

118121
if (isRoot) {

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,31 @@ import transformJsxToTaggedTemplatesPlugin from '../src/index.mjs';
44
describe('babel-plugin-transform-jsx-to-tagged-templates', () => {
55
test('basic transformation', () => {
66
expect(
7-
transform('(<div id="hello">hello</div>);', {
7+
transform('(<div />);', {
88
babelrc: false,
99
plugins: [
1010
transformJsxToTaggedTemplatesPlugin
1111
]
1212
}).code
13-
).toBe('html`<div id="hello">hello</div>`;');
13+
).toBe('html`<div/>`;');
14+
15+
expect(
16+
transform('(<span>a</span>);', {
17+
babelrc: false,
18+
plugins: [
19+
transformJsxToTaggedTemplatesPlugin
20+
]
21+
}).code
22+
).toBe('html`<span>a</span>`;');
23+
24+
expect(
25+
transform('(<div>hello</div>);', {
26+
babelrc: false,
27+
plugins: [
28+
transformJsxToTaggedTemplatesPlugin
29+
]
30+
}).code
31+
).toBe('html`<div>hello</div>`;');
1432
});
1533

1634
test('nested children transformation', () => {

0 commit comments

Comments
 (0)