Skip to content

Commit a1dc769

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 a1dc769

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1624,12 +1624,41 @@ 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+
// eslint-disable-next-line no-eval
1639+
return (0, eval)(
1640+
'({' + JSON.stringify(name) + ':async function(){}})',
1641+
)[name];
1642+
}
1643+
} else if (code.startsWith('(function')) {
1644+
const idx = code.indexOf('(', 9);
1645+
if (idx !== -1) {
1646+
const name = code.slice(9, idx).trim();
1647+
// eslint-disable-next-line no-eval
1648+
return (0, eval)(
1649+
'({' + JSON.stringify(name) + ':function(){}})',
1650+
)[name];
1651+
}
1652+
} else if (code.startsWith('(class')) {
1653+
const idx = code.indexOf('{', 6);
1654+
if (idx !== -1) {
1655+
const name = code.slice(6, idx).trim();
1656+
// eslint-disable-next-line no-eval
1657+
return (0, eval)('({' + JSON.stringify(name) + ':class{}})')[
1658+
name
1659+
];
1660+
}
1661+
}
16331662
return function () {};
16341663
}
16351664
}

0 commit comments

Comments
 (0)