Skip to content

Commit fba16ba

Browse files
authored
Wrap static blocks by fragment (#84)
* Request desired behavior in tests * Remove own keys from generating code
1 parent 048568c commit fba16ba

File tree

10 files changed

+36
-37
lines changed

10 files changed

+36
-37
lines changed

src/__tests__/__snapshots__/each-else.test.js.snap

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ exports[`JavaScript output: transformed source code 1`] = `
1111
return <span key={item}>{item}</span>;
1212
})
1313
: \\"No items in this list\\"}
14-
{Array.isArray(list) && list.length
15-
? list.map(item => {
16-
return <span key={item}>{item}</span>;
17-
})
18-
: [
19-
\\"This is \\",
20-
<strong key=\\"pug:1:0\\">literally</strong>,
21-
\\" the same list!\\"
22-
]}
14+
{Array.isArray(list) && list.length ? (
15+
list.map(item => {
16+
return <span key={item}>{item}</span>;
17+
})
18+
) : (
19+
<React.Fragment>
20+
This is <strong>literally</strong> the same list!
21+
</React.Fragment>
22+
)}
2323
</div>
2424
);
2525
})}

src/__tests__/__snapshots__/if-else.test.js.snap

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ exports[`JavaScript output: transformed source code 1`] = `
77
return (
88
<div key={i}>
99
{v === 1 ? \\"One\\" : v === 2 ? \\"Two\\" : \\"Many\\"}
10-
{v === 3
11-
? [<strong key=\\"pug:2:0\\">Three</strong>, \\"is the magic number!\\"]
12-
: null}
13-
{v == 2 ? null : [<strong key=\\"pug:5:0\\">Definitely</strong>, \\"not 2\\"]}
10+
{v === 3 ? (
11+
<React.Fragment>
12+
<strong>Three</strong>is the magic number!
13+
</React.Fragment>
14+
) : null}
15+
{v == 2 ? null : (
16+
<React.Fragment>
17+
<strong>Definitely</strong>not 2
18+
</React.Fragment>
19+
)}
1420
</div>
1521
);
1622
})}

src/__tests__/__snapshots__/interpolation.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = new function() {
2020
Here is a list of people that you might want to follow,{\\"\\\\n\\"}
2121
{[\\"one being \\", this.names[1], \\"!\\"].join(\\"\\")}
2222
</p>
23-
{showFirstPerson ? <Person name={this.names[0]} key=\\"pug:0:0\\" /> : null}
23+
{showFirstPerson ? <Person name={this.names[0]} /> : null}
2424
{this.names.map((name, key) => (
2525
<p key={key}>{[\\"Hi \\", name, \\", bye \\", name].join(\\"\\")}</p>
2626
))}

src/__tests__/__snapshots__/plain-js.test.js.snap

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
exports[`case JavaScript output: transformed source code 1`] = `
44
"const n = 0;
5-
module.exports =
6-
n === 1 ? (
7-
<p key=\\"pug:0:0\\">One</p>
8-
) : n === 2 ? (
9-
<p key=\\"pug:1:0\\">Two</p>
10-
) : null;
5+
module.exports = n === 1 ? <p>One</p> : n === 2 ? <p>Two</p> : null;
116
"
127
`;
138

@@ -107,7 +102,7 @@ Array [
107102
exports[`each-multiple static html output: static html 1`] = `"<div>1</div><div>2</div><div>3</div><div>1</div><div>2</div><div>3</div>"`;
108103

109104
exports[`if JavaScript output: transformed source code 1`] = `
110-
"module.exports = false ? <p key=\\"pug:0:0\\">Truthy</p> : null;
105+
"module.exports = false ? <p>Truthy</p> : null;
111106
"
112107
`;
113108

src/__tests__/__snapshots__/react-fragment.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ exports[`with each static html output: static html 1`] = `"<div>1</div><div>2</d
5353
exports[`with if JavaScript output: transformed source code 1`] = `
5454
"module.exports = (
5555
<React.Fragment>
56-
{true ? <div key=\\"pug:0:0\\">Truthy</div> : null}
56+
{true ? <div>Truthy</div> : null}
5757
<div>Next item</div>
5858
</React.Fragment>
5959
);

src/block-key.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,7 @@ export class StaticBlock implements Key {
6969
}
7070
this._update();
7171
}
72-
handleAttributes(attrs: Array<JSXAttribute | JSXSpreadAttribute>) {
73-
for (const _attr of attrs) {
74-
const attr = t.asJSXAttribute(_attr);
75-
if (attr && t.isJSXIdentifier(attr.name, {name: 'key'})) {
76-
return;
77-
}
78-
}
79-
this.getKey(key => {
80-
attrs.push(t.jSXAttribute(t.jSXIdentifier('key'), toJsxValue(key)));
81-
});
82-
}
72+
handleAttributes(attrs: Array<JSXAttribute | JSXSpreadAttribute>) {}
8373
end() {
8474
this._ended = true;
8575
this._update();

src/utils/jsx.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ export function buildJSXFragment(children: Array<any>): JSXElement {
3838

3939
const jSXChildren = children.map(item => {
4040
if (!isAllowedChild(item)) {
41+
if (item.type === 'StringLiteral') {
42+
return t.jSXText(item.value);
43+
}
44+
4145
return t.jSXExpressionContainer(item);
4246
}
47+
4348
return item;
4449
});
4550

src/visitors/Case.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22

33
import parseExpression from '../utils/parse-expression';
4+
import {buildJSXFragment} from '../utils/jsx';
45
import type Context from '../context';
56
import t from '../lib/babel-types';
67
import {visitExpressions} from '../visitors';
@@ -23,7 +24,7 @@ function convertCases(
2324
if (children.length === 0) {
2425
return t.identifier('null');
2526
}
26-
return t.arrayExpression(children);
27+
return buildJSXFragment(children);
2728
},
2829
);
2930
if (node.expr === 'default') {

src/visitors/Conditional.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22

33
import parseExpression from '../utils/parse-expression';
4+
import {buildJSXFragment} from '../utils/jsx';
45
import type Context from '../context';
56
import t from '../lib/babel-types';
67
import {visitExpressions} from '../visitors';
@@ -23,7 +24,7 @@ const ConditionalVisitor = {
2324
if (children.length === 0) {
2425
return t.identifier('null');
2526
}
26-
return t.arrayExpression(children);
27+
return buildJSXFragment(children);
2728
},
2829
);
2930
const alternate = context.staticBlock(
@@ -42,7 +43,7 @@ const ConditionalVisitor = {
4243
if (children.length === 0) {
4344
return t.identifier('null');
4445
}
45-
return t.arrayExpression(children);
46+
return buildJSXFragment(children);
4647
},
4748
);
4849

src/visitors/Each.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22

33
import parseExpression from '../utils/parse-expression';
4+
import {buildJSXFragment} from '../utils/jsx';
45
import type Context from '../context';
56
import t from '../lib/babel-types';
67
import {visitExpressions} from '../visitors';
@@ -18,7 +19,7 @@ function getAlternate(node: Object, context: Context): Expression {
1819
if (children.length === 1) {
1920
return children[0];
2021
}
21-
return t.arrayExpression(children);
22+
return buildJSXFragment(children);
2223
},
2324
);
2425
}

0 commit comments

Comments
 (0)