Skip to content

Commit e57882c

Browse files
committed
Parse the name if we fail to eval a function
Such as if the body contains native code.
1 parent f741c81 commit e57882c

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1624,12 +1624,38 @@ function parseModelString(
16241624
if (__DEV__) {
16251625
// In DEV mode we allow indirect eval to produce functions for logging.
16261626
// This should not compile to eval() because then it has local scope access.
1627+
const code = value.slice(2);
16271628
try {
16281629
// eslint-disable-next-line no-eval
1629-
return (0, eval)(value.slice(2));
1630+
return (0, eval)(code);
16301631
} catch (x) {
16311632
// We currently use this to express functions so we fail parsing it,
16321633
// let's just return a blank function as a place holder.
1634+
if (code.startsWith('(async function')) {
1635+
const idx = code.indexOf('(', 15);
1636+
if (idx !== -1) {
1637+
const name = code.slice(15, idx).trim();
1638+
return (0, eval)(
1639+
'({' + JSON.stringify(name) + ':async function(){}})',
1640+
)[name];
1641+
}
1642+
} else if (code.startsWith('(function')) {
1643+
const idx = code.indexOf('(', 9);
1644+
if (idx !== -1) {
1645+
const name = code.slice(9, idx).trim();
1646+
return (0, eval)(
1647+
'({' + JSON.stringify(name) + ':function(){}})',
1648+
)[name];
1649+
}
1650+
} else if (code.startsWith('(class')) {
1651+
const idx = code.indexOf('{', 6);
1652+
if (idx !== -1) {
1653+
const name = code.slice(6, idx).trim();
1654+
return (0, eval)('({' + JSON.stringify(name) + ':class{}})')[
1655+
name
1656+
];
1657+
}
1658+
}
16331659
return function () {};
16341660
}
16351661
}

0 commit comments

Comments
 (0)