dillon1000/react

Commit

fix[describeClassComponentFrame]: invoke constructor with new keyword (#36455)

For JavaScript runtimes that do not have
[`Reflect`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect)
supported, we had a fall back that was calling the constructor with
overridden `this` context via
```
fn.apply(Fake.prototype);
```

In ES6, it is required to call constructor only with the `new` keyword,
otherwise the runtime is expected to throw a corresponding TypeError:
```
TypeError: Class constructor <> cannot be invoked without 'new'
```

We've observed this error in Hermes runtime, but this is applicable to
V8 or any other runtime. The only reason why V8 wasn't affected is
because it implemented Reflect APIs.

Instead of the incorrect call, we will fall back to calling `new fn()`,
but with a temporary patched prototype of the class, which would make a
trap out of the setter for `props` object. We use the same approach when
`Reflect` APIs are available, but instead of modifying the prototype, we
pass the fake context:

https://github.com/facebook/react/blob/d5736f098edee62c44f27b053e6e48f5fa443803/packages/shared/ReactComponentStackFrame.js#L129-L148


---

See tests implemented. Without the changes, the test would fail with the
`TypeError` mentioned above.
Browse files
Changed paths4 files
First-parent comparison
M packages/react-devtools-shared/src/backend/shared/DevToolsComponentStackFrame.js ModifiedM packages/react-reconciler/src/__tests__/ReactErrorStacks-test.js ModifiedM packages/shared/ReactComponentStackFrame.js ModifiedM scripts/jest/setupTests.js Modified
Patch

Files changed

Rendering syntax-highlighted changes…