GeezerVison is showing github.com — [Fresh] Fix a crash with implicit arrow return (#16687) · react/react@9044bb0
File treeExpand file treeCollapse file tree Open diff view settings packages/react-refresh/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-refresh/src/ReactFreshBabelPlugin.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -571,6 +571,11 @@ export default function(babel) { | 571 | 571 |
| 572 | 572 | // The signature call is split in two parts. One part is called inside the function. | 573 | 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 | + } | 574 | 579 | path | 575 | 580 | .get('body') | 576 | 581 | .unshiftContainer( | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-refresh/src/__tests__/ReactFreshBabelPlugin-test.js +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -460,4 +460,17 @@ describe('ReactFreshBabelPlugin', () => { | 460 | 460 | `), | 461 | 461 | ).toMatchSnapshot(); | 462 | 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 | + }); | 463 | 476 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-refresh/src/__tests__/__snapshots__/ReactFreshBabelPlugin-test.js.snap +63Lines changed: 63 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP | 2 | 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 | + | 3 | 66 | exports[`ReactFreshBabelPlugin does not consider require-like methods to be HOCs 1`] = ` | 4 | 67 | const A = require('A'); | 5 | 68 |
| |
|