[Fresh] Fix a crash with implicit arrow return (#16687)
Dan Abramov committed
Sep 6, 2019 at 19:58 UTC
9044bb0fa3c60ffe723034486b02168bdf63f786
3 files changed
+81
packages/react-refresh/src/ReactFreshBabelPlugin.js
+5
@@ -571,6 +571,11 @@ export default function(babel) {
571
572
// The signature call is split in two parts. One part is called inside the function.
573
// This is used to signal when first render happens.
574
+ if (path.node.body.type !== 'BlockStatement') {
575
+ path.node.body = t.blockStatement([
576
+ t.returnStatement(path.node.body),
577
+ ]);
578
+ }
579
path
580
.get('body')
581
.unshiftContainer(
packages/react-refresh/src/__tests__/ReactFreshBabelPlugin-test.js
+13
@@ -460,4 +460,17 @@ describe('ReactFreshBabelPlugin', () => {
460
`),
461
).toMatchSnapshot();
462
});
463
+
464
+ it('can handle implicit arrow returns', () => {
465
+ expect(
466
+ transform(`
467
+ export default () => useContext(X);
468
+ export const Foo = () => useContext(X);
469
+ module.exports = () => useContext(X);
470
+ const Bar = () => useContext(X);
471
+ const Baz = memo(() => useContext(X));
472
+ const Qux = () => (0, useContext(X));
473
+ `),
474
+ ).toMatchSnapshot();
475
+ });
476
});
packages/react-refresh/src/__tests__/__snapshots__/ReactFreshBabelPlugin-test.js.snap
+63
@@ -1,5 +1,68 @@
1
// Jest Snapshot v1, https://goo.gl/fbAQLP
2
3
+exports[`ReactFreshBabelPlugin can handle implicit arrow returns 1`] = `
4
+var _s = $RefreshSig$(),
5
+ _s2 = $RefreshSig$(),
6
+ _s3 = $RefreshSig$(),
7
+ _s4 = $RefreshSig$(),
8
+ _s5 = $RefreshSig$(),
9
+ _s6 = $RefreshSig$();
10
+
11
+export default _s(() => {
12
+ _s();
13
+
14
+ return useContext(X);
15
+}, "useContext{}");
16
+export const Foo = () => {
17
+ _s2();
18
+
19
+ return useContext(X);
20
+};
21
+
22
+_s2(Foo, "useContext{}");
23
+
24
+_c = Foo;
25
+module.exports = _s3(() => {
26
+ _s3();
27
+
28
+ return useContext(X);
29
+}, "useContext{}");
30
+
31
+const Bar = () => {
32
+ _s4();
33
+
34
+ return useContext(X);
35
+};
36
+
37
+_s4(Bar, "useContext{}");
38
+
39
+_c2 = Bar;
40
+
41
+const Baz = _c4 = memo(_c3 = _s5(() => {
42
+ _s5();
43
+
44
+ return useContext(X);
45
+}, "useContext{}"));
46
+
47
+const Qux = () => {
48
+ _s6();
49
+
50
+ return 0, useContext(X);
51
+};
52
+
53
+_s6(Qux, "useContext{}");
54
+
55
+_c5 = Qux;
56
+
57
+var _c, _c2, _c3, _c4, _c5;
58
+
59
+$RefreshReg$(_c, "Foo");
60
+$RefreshReg$(_c2, "Bar");
61
+$RefreshReg$(_c3, "Baz$memo");
62
+$RefreshReg$(_c4, "Baz");
63
+$RefreshReg$(_c5, "Qux");
64
+`;
65
+
66
exports[`ReactFreshBabelPlugin does not consider require-like methods to be HOCs 1`] = `
67
const A = require('A');
68