Skip to content

Commit 6d4dd70

Browse files
committed
Assign custom name to inferred name if available
1 parent 5c6a7a3 commit 6d4dd70

File tree

1 file changed

+52
-27
lines changed

1 file changed

+52
-27
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,6 +1969,42 @@ function createModel(response: Response, model: any): any {
19691969
return model;
19701970
}
19711971

1972+
function getInferredFunctionApproximate(code: string): () => void {
1973+
let slicedCode;
1974+
if (code.startsWith('Object.defineProperty(')) {
1975+
slicedCode = code.slice('Object.defineProperty('.length);
1976+
} else if (code.startsWith('(')) {
1977+
slicedCode = code.slice(1);
1978+
} else {
1979+
slicedCode = code;
1980+
}
1981+
if (slicedCode.startsWith('async function')) {
1982+
const idx = slicedCode.indexOf('(', 14);
1983+
if (idx !== -1) {
1984+
const name = slicedCode.slice(14, idx).trim();
1985+
// eslint-disable-next-line no-eval
1986+
return (0, eval)('({' + JSON.stringify(name) + ':async function(){}})')[
1987+
name
1988+
];
1989+
}
1990+
} else if (slicedCode.startsWith('function')) {
1991+
const idx = slicedCode.indexOf('(', 8);
1992+
if (idx !== -1) {
1993+
const name = slicedCode.slice(8, idx).trim();
1994+
// eslint-disable-next-line no-eval
1995+
return (0, eval)('({' + JSON.stringify(name) + ':function(){}})')[name];
1996+
}
1997+
} else if (slicedCode.startsWith('class')) {
1998+
const idx = slicedCode.indexOf('{', 5);
1999+
if (idx !== -1) {
2000+
const name = slicedCode.slice(5, idx).trim();
2001+
// eslint-disable-next-line no-eval
2002+
return (0, eval)('({' + JSON.stringify(name) + ':class{}})')[name];
2003+
}
2004+
}
2005+
return function () {};
2006+
}
2007+
19722008
function parseModelString(
19732009
response: Response,
19742010
parentObject: Object,
@@ -2163,35 +2199,24 @@ function parseModelString(
21632199
} catch (x) {
21642200
// We currently use this to express functions so we fail parsing it,
21652201
// let's just return a blank function as a place holder.
2166-
if (code.startsWith('(async function')) {
2167-
const idx = code.indexOf('(', 15);
2168-
if (idx !== -1) {
2169-
const name = code.slice(15, idx).trim();
2170-
// eslint-disable-next-line no-eval
2171-
return (0, eval)(
2172-
'({' + JSON.stringify(name) + ':async function(){}})',
2173-
)[name];
2174-
}
2175-
} else if (code.startsWith('(function')) {
2176-
const idx = code.indexOf('(', 9);
2177-
if (idx !== -1) {
2178-
const name = code.slice(9, idx).trim();
2179-
// eslint-disable-next-line no-eval
2180-
return (0, eval)(
2181-
'({' + JSON.stringify(name) + ':function(){}})',
2182-
)[name];
2183-
}
2184-
} else if (code.startsWith('(class')) {
2185-
const idx = code.indexOf('{', 6);
2186-
if (idx !== -1) {
2187-
const name = code.slice(6, idx).trim();
2188-
// eslint-disable-next-line no-eval
2189-
return (0, eval)('({' + JSON.stringify(name) + ':class{}})')[
2190-
name
2191-
];
2202+
let fn;
2203+
try {
2204+
fn = getInferredFunctionApproximate(code);
2205+
if (code.startsWith('Object.defineProperty(')) {
2206+
const DESCRIPTOR = ',"name",{value:"';
2207+
const idx = code.lastIndexOf(DESCRIPTOR);
2208+
if (idx !== -1) {
2209+
const name = JSON.parse(
2210+
code.slice(idx + DESCRIPTOR.length - 1, code.length - 2),
2211+
);
2212+
// $FlowFixMe[cannot-write]
2213+
Object.defineProperty(fn, 'name', {value: name});
2214+
}
21922215
}
2216+
} catch (_) {
2217+
fn = function () {};
21932218
}
2194-
return function () {};
2219+
return fn;
21952220
}
21962221
}
21972222
// Fallthrough

0 commit comments

Comments
 (0)