Skip to content

Commit 7de39c3

Browse files
authored
Wrap non-JSX children by jSXContainer (#74)
So now we wrap all non-jsx statements by React.Fragment. Now the following code works well. ```jsx pug` - console.log('Hello') p I'm sibling ` ```
1 parent e59283d commit 7de39c3

File tree

6 files changed

+140
-6
lines changed

6 files changed

+140
-6
lines changed
Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`JavaScript output: transformed source code 1`] = `
3+
exports[`basic JavaScript output: transformed source code 1`] = `
44
"module.exports = (
55
<React.Fragment>
66
<div />
@@ -10,11 +10,98 @@ exports[`JavaScript output: transformed source code 1`] = `
1010
"
1111
`;
1212

13-
exports[`html output: generated html 1`] = `
13+
exports[`basic html output: generated html 1`] = `
1414
Array [
1515
<div />,
1616
<div />,
1717
]
1818
`;
1919

20-
exports[`static html output: static html 1`] = `"<div></div><div></div>"`;
20+
exports[`basic static html output: static html 1`] = `"<div></div><div></div>"`;
21+
22+
exports[`with each JavaScript output: transformed source code 1`] = `
23+
"module.exports = (
24+
<React.Fragment>
25+
{((_pug_nodes, _pug_arr) => {
26+
if (!(_pug_arr == null || Array.isArray(_pug_arr)))
27+
throw new Error(
28+
'Expected \\"[1, 2, 3]\\" to be an array because it is passed to each.'
29+
);
30+
if (_pug_arr == null || _pug_arr.length === 0) return undefined;
31+
32+
for (let _pug_index = 0; _pug_index < _pug_arr.length; _pug_index++) {
33+
const item = _pug_arr[_pug_index];
34+
_pug_nodes[_pug_nodes.length] = (
35+
<div key={\\"pug\\" + item + \\":0\\"}>{item}</div>
36+
);
37+
}
38+
39+
return _pug_nodes;
40+
})([], [1, 2, 3])}
41+
<div>Next item</div>
42+
</React.Fragment>
43+
);
44+
"
45+
`;
46+
47+
exports[`with each html output: generated html 1`] = `
48+
Array [
49+
<div>
50+
1
51+
</div>,
52+
<div>
53+
2
54+
</div>,
55+
<div>
56+
3
57+
</div>,
58+
<div>
59+
Next item
60+
</div>,
61+
]
62+
`;
63+
64+
exports[`with each static html output: static html 1`] = `"<div>1</div><div>2</div><div>3</div><div>Next item</div>"`;
65+
66+
exports[`with if JavaScript output: transformed source code 1`] = `
67+
"module.exports = (
68+
<React.Fragment>
69+
{true ? <div key=\\"pug:0:0\\">Truthy</div> : undefined}
70+
<div>Next item</div>
71+
</React.Fragment>
72+
);
73+
"
74+
`;
75+
76+
exports[`with if html output: generated html 1`] = `
77+
Array [
78+
<div>
79+
Truthy
80+
</div>,
81+
<div>
82+
Next item
83+
</div>,
84+
]
85+
`;
86+
87+
exports[`with if static html output: static html 1`] = `"<div>Truthy</div><div>Next item</div>"`;
88+
89+
exports[`with inline javascript JavaScript output: transformed source code 1`] = `
90+
"let _i;
91+
92+
module.exports = (
93+
<React.Fragment>
94+
{((_i = 100), undefined)}
95+
<div>{_i}</div>
96+
</React.Fragment>
97+
);
98+
"
99+
`;
100+
101+
exports[`with inline javascript html output: generated html 1`] = `
102+
<div>
103+
100
104+
</div>
105+
`;
106+
107+
exports[`with inline javascript static html output: static html 1`] = `"<div>100</div>"`;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = pug`
2+
each item in [1, 2, 3]
3+
div(key=item)
4+
= item
5+
6+
div Next item
7+
`;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = pug`
2+
if true
3+
div Truthy
4+
5+
div Next item
6+
`;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = pug`
2+
- const i = 100
3+
div= i
4+
`;
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
import testHelper from './test-helper';
22

3-
testHelper(__dirname + '/react-fragment.input.js');
3+
describe('with each', () => {
4+
testHelper(__dirname + '/react-fragment-each.input.js');
5+
});
6+
7+
describe('with if', () => {
8+
testHelper(__dirname + '/react-fragment-if.input.js');
9+
});
10+
11+
describe('with inline javascript', () => {
12+
testHelper(__dirname + '/react-fragment-inline-js.input.js');
13+
});
14+
15+
describe('basic', () => {
16+
testHelper(__dirname + '/react-fragment.input.js');
17+
});

src/utils/jsx.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,28 @@ export function buildJSXElement(
2020
return t.jSXElement(open, close, children, noChildren);
2121
}
2222

23+
const isAllowedChild = item =>
24+
[
25+
'JSXText',
26+
'JSXExpressionContainer',
27+
'JSXSpreadChild',
28+
'JSXElement',
29+
].includes(item.type);
30+
2331
// TODO: This can be replaced when migrating to Babel 7 as JSXFragment
2432
// has been added in v7.0.0-beta.30.
25-
export function buildJSXFragment(children: JSXChildren): JSXElement {
33+
export function buildJSXFragment(children: Array<any>): JSXElement {
2634
const fragmentExpression = t.jSXMemberExpression(
2735
t.jSXIdentifier('React'),
2836
t.jSXIdentifier('Fragment'),
2937
);
30-
return buildJSXElement(fragmentExpression, [], children);
38+
39+
const jSXChildren = children.map(item => {
40+
if (!isAllowedChild(item)) {
41+
return t.jSXExpressionContainer(item);
42+
}
43+
return item;
44+
});
45+
46+
return buildJSXElement(fragmentExpression, [], jSXChildren);
3147
}

0 commit comments

Comments
 (0)