Skip to content

Commit fa9845b

Browse files
authored
feat: remove unnecessary fragments (#5)
1 parent 9985d70 commit fa9845b

3 files changed

Lines changed: 33 additions & 30 deletions

File tree

__tests__/babelPluginReactLive.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ it('babelPluginReactLive', async () => {
6767
}
6868
export const MockManyChilds = () => {
6969
return (
70-
<ComponentBox data-test="id">{\`<>
71-
<div>content 1</div>
72-
<div>content 2</div>
73-
<div>content 3</div>
74-
</>
70+
<ComponentBox data-test="id">{\`
71+
<div>content 1</div>
72+
<div>content 2</div>
73+
<div>content 3</div>
74+
7575
\`}</ComponentBox>
7676
)
7777
}
@@ -87,13 +87,13 @@ it('babelPluginReactLive', async () => {
8787
}
8888
export const MockText = () => {
8989
return (
90-
<ComponentBox data-test="id">{\`<>
91-
text
92-
{'text'}
93-
<span>content</span>
94-
text
95-
{'text'}
96-
</>
90+
<ComponentBox data-test="id">{\`
91+
text
92+
{'text'}
93+
<span>content</span>
94+
text
95+
{'text'}
96+
9797
\`}</ComponentBox>
9898
)
9999
}

babelPluginReactLive.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,30 @@ function babelPluginReactLive(babel, options) {
88
? JSON.parse(fs.readFileSync(options.prettierPath, 'utf-8'))
99
: {}
1010

11-
const wrappCodeInside = (code) => {
12-
const formattedCode = prettier.format(code, {
11+
const convertCodeToJSX = (children) => {
12+
// So prettier can do its work,
13+
// we wrap our JSX children in a fragment.
14+
// But we also remvoe it afterwards.
15+
let code =
16+
children.length > 1
17+
? `<>${children.join('\n')}</>`
18+
: children.join('\n')
19+
20+
code = prettier.format(code, {
1321
...prettierrc,
1422
parser: 'babel',
1523
})
1624

25+
// Prettier adds a leading ;
26+
// And we also escape `
27+
code = code.replace(/^;/, '').replace(/`/g, '\\`')
28+
29+
if (children.length > 1) {
30+
code = code.replace(/^<>|<\/>$|^\s{2}/gm, '')
31+
}
32+
1733
return t.jsxExpressionContainer(
18-
t.templateLiteral(
19-
[
20-
t.templateElement({
21-
raw: formattedCode.replace(/^;/, '').replace(/`/g, '\\`'),
22-
}),
23-
],
24-
[]
25-
)
34+
t.templateLiteral([t.templateElement({ raw: code })], [])
2635
)
2736
}
2837

@@ -129,13 +138,7 @@ function babelPluginReactLive(babel, options) {
129138
)
130139
}
131140

132-
path.node.children = [
133-
wrappCodeInside(
134-
children.length > 1
135-
? `<>${children.join('\n')}</>`
136-
: children.join('\n')
137-
),
138-
]
141+
path.node.children = [convertCodeToJSX(children)]
139142

140143
path.replaceWith(t.identifier(path.toString()))
141144
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"type": "git",
1414
"url": "https://github.com/dnbexperience/eufemia.git"
1515
},
16-
"version": "1.2.1",
16+
"version": "1.3.0",
1717
"main": "babelPluginReactLive.js",
1818
"scripts": {
1919
"test": "jest",

0 commit comments

Comments
 (0)