From 9710853baf9649fed556dc0e9d39765649675b7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Markb=C3=A5ge?= Date: Thu, 30 May 2024 15:00:55 -0400 Subject: [PATCH] [Flight] Try/Catch Eval (#29671) Follow up to https://github.com/facebook/react/pull/29632. It's possible for `eval` to throw such as if we're in a CSP environment. This is non-essential debug information. We can still proceed to create a fake stack entry. It'll still have the right name. It just won't have the right line/col number nor source url/source map. It might also be ignored listed since it's inside Flight. --- packages/react-client/src/ReactFlightClient.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/react-client/src/ReactFlightClient.js b/packages/react-client/src/ReactFlightClient.js index 7795b3025215d..3cd07de4b1f14 100644 --- a/packages/react-client/src/ReactFlightClient.js +++ b/packages/react-client/src/ReactFlightClient.js @@ -1648,7 +1648,7 @@ const taskCache: null | WeakMap< ConsoleTask, > = supportsCreateTask ? new WeakMap() : null; -type FakeFunction = (FakeFunction) => T; +type FakeFunction = (() => T) => T; const fakeFunctionCache: Map> = __DEV__ ? new Map() : (null: any); @@ -1684,8 +1684,18 @@ function createFakeFunction( code += '//# sourceURL=' + filename; } - // eslint-disable-next-line no-eval - const fn: FakeFunction = (0, eval)(code); + let fn: FakeFunction; + try { + // eslint-disable-next-line no-eval + fn = (0, eval)(code); + } catch (x) { + // If eval fails, such as if in an environment that doesn't support it, + // we fallback to creating a function here. It'll still have the right + // name but it'll lose line/column number and file name. + fn = function (_) { + return _(); + }; + } // $FlowFixMe[cannot-write] Object.defineProperty(fn, 'name', {value: name || '(anonymous)'}); // $FlowFixMe[prop-missing]