Skip to content

Commit 31b70be

Browse files
authored
feat: enable fragments and text as valid children (#1)
1 parent 022e9a6 commit 31b70be

6 files changed

Lines changed: 97 additions & 39 deletions

File tree

__mocks__/Examples.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,25 @@ export const MockManyChilds = () => {
4343
</ComponentBox>
4444
)
4545
}
46+
47+
export const MockFragment = () => {
48+
return (
49+
<ComponentBox data-test="id">
50+
<>
51+
<span>content 1</span>
52+
<span>content 2</span>
53+
<span>content 3</span>
54+
</>
55+
</ComponentBox>
56+
)
57+
}
58+
59+
export const MockText = () => {
60+
return (
61+
<ComponentBox data-test="id">
62+
text
63+
<span>content</span>
64+
text
65+
</ComponentBox>
66+
)
67+
}

__tests__/babelPluginReactLive.test.ts

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,42 +39,62 @@ it('babelPluginReactLive', async () => {
3939
})
4040

4141
expect(formattedCode).toMatchInlineSnapshot(`
42-
"const ComponentBox = ({ children }) => children
43-
export const MockNoInlineWithComponent = () => {
44-
return (
45-
<ComponentBox data-test="id" noInline>{\`const DemoComponent = () => {
46-
return <>content</>
47-
}
48-
render(<DemoComponent />)
49-
\`}</ComponentBox>
50-
)
51-
}
52-
export const MockNoInlineWithText = () => {
53-
return (
54-
<ComponentBox data-test="id" noInline>{\`render(<>content</>)
55-
\`}</ComponentBox>
56-
)
57-
}
58-
export const MockOneChilds = () => {
59-
return (
60-
<ComponentBox data-test="id">{\`<div>content</div>
61-
\`}</ComponentBox>
62-
)
63-
}
64-
export const MockManyChilds = () => {
65-
return (
66-
<ComponentBox data-test="id">{\`<>
67-
<div>content 1</div>
68-
<div>content 2</div>
69-
<div>content 3</div>
70-
</>
71-
\`}</ComponentBox>
72-
)
73-
}
74-
"
75-
`)
42+
"const ComponentBox = ({ children }) => children
43+
export const MockNoInlineWithComponent = () => {
44+
return (
45+
<ComponentBox data-test="id" noInline>{\`const DemoComponent = () => {
46+
return <>content</>
47+
}
48+
render(<DemoComponent />)
49+
\`}</ComponentBox>
50+
)
51+
}
52+
export const MockNoInlineWithText = () => {
53+
return (
54+
<ComponentBox data-test="id" noInline>{\`render(<>content</>)
55+
\`}</ComponentBox>
56+
)
57+
}
58+
export const MockOneChilds = () => {
59+
return (
60+
<ComponentBox data-test="id">{\`<div>content</div>
61+
\`}</ComponentBox>
62+
)
63+
}
64+
export const MockManyChilds = () => {
65+
return (
66+
<ComponentBox data-test="id">{\`<>
67+
<div>content 1</div>
68+
<div>content 2</div>
69+
<div>content 3</div>
70+
</>
71+
\`}</ComponentBox>
72+
)
73+
}
74+
export const MockFragment = () => {
75+
return (
76+
<ComponentBox data-test="id">{\`<>
77+
<span>content 1</span>
78+
<span>content 2</span>
79+
<span>content 3</span>
80+
</>
81+
\`}</ComponentBox>
82+
)
83+
}
84+
export const MockText = () => {
85+
return (
86+
<ComponentBox data-test="id">{\`<>
87+
text
88+
<span>content</span>
89+
text
90+
</>
91+
\`}</ComponentBox>
92+
)
93+
}
94+
"
95+
`)
7696

7797
expect(formattedCode.match(/noInline/g)).toHaveLength(2)
78-
expect(formattedCode.match(/\{`/g)).toHaveLength(4)
79-
expect(formattedCode.match(/`\}/g)).toHaveLength(4)
98+
expect(formattedCode.match(/\{`/g)).toHaveLength(6)
99+
expect(formattedCode.match(/`\}/g)).toHaveLength(6)
80100
})

babelPluginReactLive.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ function babelPluginReactLive(babel, options) {
9191
children.push(code)
9292
}
9393
},
94+
JSXFragment(path) {
95+
if (rootPath === path.parentPath) {
96+
const code = path.getSource()
97+
98+
children.push(code)
99+
}
100+
},
101+
JSXText(path) {
102+
if (rootPath === path.parentPath) {
103+
const code = path.getSource()
104+
105+
if (code.trim().length) {
106+
children.push(code)
107+
}
108+
}
109+
},
94110
})
95111

96112
if (children.length > 0) {

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ module.exports = {
22
transform: {
33
'\\.[jt]sx?$': 'babel-jest',
44
},
5+
testPathIgnorePatterns: ['<rootDir>/.history'],
56
}

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.0.0",
16+
"version": "1.1.0",
1717
"main": "babelPluginReactLive.js",
1818
"scripts": {
1919
"test": "jest",

yarn.lock

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,7 +1520,7 @@ __metadata:
15201520
languageName: node
15211521
linkType: hard
15221522

1523-
"@jest/globals@npm:29.3.1, @jest/globals@npm:^29.3.1":
1523+
"@jest/globals@npm:^29.3.1":
15241524
version: 29.3.1
15251525
resolution: "@jest/globals@npm:29.3.1"
15261526
dependencies:
@@ -2076,7 +2076,6 @@ __metadata:
20762076
"@babel/preset-env": 7.20.2
20772077
"@babel/preset-react": 7.18.6
20782078
"@babel/preset-typescript": 7.18.6
2079-
"@jest/globals": 29.3.1
20802079
"@types/jest": 29.2.3
20812080
babel-jest: 29.3.1
20822081
jest: 29.3.1

0 commit comments

Comments
 (0)