Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flight] Encode the name of a function as an object property #30325

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -1891,19 +1891,33 @@ function createFakeFunction<T>(
const comment =
'/* This module was rendered by a Server Component. Turn on Source Maps to see the server source. */';

if (!name) {
// An eval:ed function with no name gets the name "eval". We give it something more descriptive.
name = '(anonymous)';
}
const encodedName = JSON.stringify(name);
// We generate code where the call is at the line and column of the server executed code.
// This allows us to use the original source map as the source map of this fake file to
// point to the original source.
let code;
if (line <= 1) {
code = '_=>' + ' '.repeat(col < 4 ? 0 : col - 4) + '_()\n' + comment;
const minSize = encodedName.length + 7;
code =
'({' +
encodedName +
':_=>' +
' '.repeat(col < minSize ? 0 : col - minSize) +
'_()})\n' +
comment;
} else {
code =
comment +
'\n'.repeat(line - 2) +
'_=>\n' +
'({' +
encodedName +
':_=>\n' +
' '.repeat(col < 1 ? 0 : col - 1) +
'_()';
'_()})';
}

if (filename.startsWith('/')) {
Expand Down Expand Up @@ -1931,7 +1945,7 @@ function createFakeFunction<T>(
let fn: FakeFunction<T>;
try {
// eslint-disable-next-line no-eval
fn = (0, eval)(code);
fn = (0, eval)(code)[name];
} 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no longer true?

Expand All @@ -1940,10 +1954,6 @@ function createFakeFunction<T>(
return _();
};
}
// $FlowFixMe[cannot-write]
Object.defineProperty(fn, 'name', {value: name || '(anonymous)'});
// $FlowFixMe[prop-missing]
fn.displayName = name;
return fn;
}

Expand Down