Skip to content

Commit 9fc5285

Browse files
author
Sampo Kivistö
committed
Compile React.Fragment to inferno createFragment call #56
1 parent f049b96 commit 9fc5285

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ All of the following syntaxes are **reserved** for createFragment call
7474

7575
```
7676

77+
React.Fragment is also compiled to inferno createFragment call to ease project migration to Inferno https://github.com/infernojs/babel-plugin-inferno/issues/56.
78+
7779
## Special flags
7880

7981
This plugin provides few special compile time flags that can be used to optimize an inferno application.

dist/index.umd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function isNullOrUndefined(obj) {
1818
}
1919

2020
function isFragment(name) {
21-
return name === 'Fragment' || name === 'Inferno.Fragment';
21+
return name === 'Fragment' || name === 'Inferno.Fragment' || name === 'React.Fragment';
2222
}
2323

2424
var NULL = t.identifier('null');
@@ -115,7 +115,7 @@ function getVNodeType(astNode) {
115115
flags = VNodeTypes[astName] || VNodeFlags.HtmlElement;
116116
}
117117
} else if (astType === 'JSXMemberExpression') {
118-
if (astNode.property.name === 'Fragment' && astNode.object.name === 'Inferno') {
118+
if (astNode.property.name === 'Fragment') {
119119
vNodeType = TYPE_FRAGMENT;
120120
} else {
121121
vNodeType = TYPE_COMPONENT;

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
22
"name": "babel-plugin-inferno",
3-
"version": "6.2.0",
3+
"version": "6.3.0",
44
"description": "Turn JSX into Inferno virtual DOM",
55
"repository": "https://github.com/trueadm/babel-plugin-inferno",
66
"license": "MIT",
77
"main": "lib/index.js",
88
"devDependencies": {
99
"@babel/core": "7.3.3",
10-
"@babel/helper-replace-supers": "7.13.12",
11-
"@babel/plugin-proposal-object-rest-spread": "7.13.8",
12-
"@babel/preset-env": "^7.13.15",
10+
"@babel/helper-replace-supers": "7.14.4",
11+
"@babel/plugin-proposal-object-rest-spread": "7.14.4",
12+
"@babel/preset-env": "^7.14.4",
1313
"babel-eslint": "^10.1.0",
1414
"chai": "^4.3.4",
15-
"eslint": "^7.24.0",
16-
"mocha": "^8.3.2",
15+
"eslint": "^7.27.0",
16+
"mocha": "^8.4.0",
1717
"nyc": "^15.1.0",
18-
"rollup": "^2.45.2",
18+
"rollup": "^2.50.5",
1919
"rollup-plugin-babel": "^4.4.0",
2020
"rollup-plugin-commonjs": "^10.1.0",
2121
"rollup-plugin-node-resolve": "^5.2.0",

tests.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ describe('Transforms', function () {
478478
it('Should createFragment non keyed children', function () {
479479
expect(transform('<Inferno.Fragment key="foo"><div>1</div><span>foo</span></Inferno.Fragment>')).to.eql('createFragment([createVNode(1, "div", null, "1", 16), createVNode(1, "span", null, "foo", 16)], 4, "foo");');
480480
});
481-
481+
482482
it('Should ignore all other props', function () {
483483
expect(transform('<Inferno.Fragment abc="foobar" id="test" key="foo"><div>1</div><span>foo</span></Inferno.Fragment>')).to.eql('createFragment([createVNode(1, "div", null, "1", 16), createVNode(1, "span", null, "foo", 16)], 4, "foo");');
484484
});
@@ -492,6 +492,42 @@ describe('Transforms', function () {
492492
expect(transform('<Inferno.Fragment key="foo" $HasNonKeyedChildren>{magic}</Inferno.Fragment>')).to.eql('createFragment(magic, 4, "foo");');
493493
});
494494
});
495+
496+
describe('React.Fragment', function () {
497+
it('Should createFragment', function () {
498+
expect(transform('<React.Fragment>Test</React.Fragment>')).to.eql('createFragment([createTextVNode("Test")], 4);');
499+
});
500+
501+
it('Should createFragment dynamic children', function () {
502+
expect(transform('<React.Fragment>{dynamic}</React.Fragment>')).to.eql('createFragment(dynamic, 0);');
503+
});
504+
505+
it('Should createFragment keyed children', function () {
506+
expect(transform('<React.Fragment><span key="ok">kk</span><div key="ok2">ok</div></React.Fragment>')).to.eql('createFragment([createVNode(1, "span", null, "kk", 16, null, "ok"), createVNode(1, "div", null, "ok", 16, null, "ok2")], 8);');
507+
});
508+
509+
it('Should createFragment non keyed children', function () {
510+
expect(transform('<React.Fragment><div>1</div><span>foo</span></React.Fragment>')).to.eql('createFragment([createVNode(1, "div", null, "1", 16), createVNode(1, "span", null, "foo", 16)], 4);');
511+
});
512+
513+
// Long syntax specials
514+
it('Should createFragment non keyed children', function () {
515+
expect(transform('<React.Fragment key="foo"><div>1</div><span>foo</span></React.Fragment>')).to.eql('createFragment([createVNode(1, "div", null, "1", 16), createVNode(1, "span", null, "foo", 16)], 4, "foo");');
516+
});
517+
518+
it('Should ignore all other props', function () {
519+
expect(transform('<React.Fragment abc="foobar" id="test" key="foo"><div>1</div><span>foo</span></React.Fragment>')).to.eql('createFragment([createVNode(1, "div", null, "1", 16), createVNode(1, "span", null, "foo", 16)], 4, "foo");');
520+
});
521+
522+
// Optimization flags
523+
it('Should createFragment non keyed children', function () {
524+
expect(transform('<React.Fragment key="foo" $HasKeyedChildren>{magic}</React.Fragment>')).to.eql('createFragment(magic, 8, "foo");');
525+
});
526+
527+
it('Should createFragment non keyed children', function () {
528+
expect(transform('<React.Fragment key="foo" $HasNonKeyedChildren>{magic}</React.Fragment>')).to.eql('createFragment(magic, 4, "foo");');
529+
});
530+
});
495531
});
496532
});
497533

0 commit comments

Comments
 (0)