Skip to content

Commit d490084

Browse files
committed
Encode the name of a function as an object property
1 parent ff89ba7 commit d490084

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,19 +1891,33 @@ function createFakeFunction<T>(
18911891
const comment =
18921892
'/* This module was rendered by a Server Component. Turn on Source Maps to see the server source. */';
18931893

1894+
if (!name) {
1895+
// An eval:ed function with no name gets the name "eval". We give it something more descriptive.
1896+
name = '(anonymous)';
1897+
}
1898+
const encodedName = JSON.stringify(name);
18941899
// We generate code where the call is at the line and column of the server executed code.
18951900
// This allows us to use the original source map as the source map of this fake file to
18961901
// point to the original source.
18971902
let code;
18981903
if (line <= 1) {
1899-
code = '_=>' + ' '.repeat(col < 4 ? 0 : col - 4) + '_()\n' + comment;
1904+
const minSize = encodedName.length + 8;
1905+
code =
1906+
'({' +
1907+
encodedName +
1908+
'}:_=>' +
1909+
' '.repeat(col < minSize ? 0 : col - minSize) +
1910+
'_()})\n' +
1911+
comment;
19001912
} else {
19011913
code =
19021914
comment +
19031915
'\n'.repeat(line - 2) +
1904-
'_=>\n' +
1916+
'({' +
1917+
encodedName +
1918+
'}:_=>\n' +
19051919
' '.repeat(col < 1 ? 0 : col - 1) +
1906-
'_()';
1920+
'_()})';
19071921
}
19081922

19091923
if (filename.startsWith('/')) {
@@ -1931,7 +1945,7 @@ function createFakeFunction<T>(
19311945
let fn: FakeFunction<T>;
19321946
try {
19331947
// eslint-disable-next-line no-eval
1934-
fn = (0, eval)(code);
1948+
fn = (0, eval)(code)[name];
19351949
} catch (x) {
19361950
// If eval fails, such as if in an environment that doesn't support it,
19371951
// we fallback to creating a function here. It'll still have the right
@@ -1940,10 +1954,6 @@ function createFakeFunction<T>(
19401954
return _();
19411955
};
19421956
}
1943-
// $FlowFixMe[cannot-write]
1944-
Object.defineProperty(fn, 'name', {value: name || '(anonymous)'});
1945-
// $FlowFixMe[prop-missing]
1946-
fn.displayName = name;
19471957
return fn;
19481958
}
19491959

0 commit comments

Comments
 (0)