Skip to content

Commit f9de628

Browse files
committed
[compiler] Errors for eval(), with statments, class declarations
* Error for `eval()` * More specific error message for `with (expr) { ... }` syntax * More specific error message for class declarations
1 parent ec4374c commit f9de628

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,13 +1355,45 @@ function lowerStatement(
13551355

13561356
return;
13571357
}
1358+
case 'WithStatement': {
1359+
builder.errors.push({
1360+
reason: `JavaScript 'with' syntax is not supported`,
1361+
description: `'with' syntax is considered deprecated and removed from JavaScript standards, consider alternatives`,
1362+
severity: ErrorSeverity.InvalidJS,
1363+
loc: stmtPath.node.loc ?? null,
1364+
suggestions: null,
1365+
});
1366+
lowerValueToTemporary(builder, {
1367+
kind: 'UnsupportedNode',
1368+
loc: stmtPath.node.loc ?? GeneratedSource,
1369+
node: stmtPath.node,
1370+
});
1371+
return;
1372+
}
1373+
case 'ClassDeclaration': {
1374+
/*
1375+
* We can in theory support nested classes, similarly to functions where we track values
1376+
* captured by the class and consider mutations of the instances to mutate the class itself
1377+
*/
1378+
builder.errors.push({
1379+
reason: `Support nested class declarations`,
1380+
severity: ErrorSeverity.Todo,
1381+
loc: stmtPath.node.loc ?? null,
1382+
suggestions: null,
1383+
});
1384+
lowerValueToTemporary(builder, {
1385+
kind: 'UnsupportedNode',
1386+
loc: stmtPath.node.loc ?? GeneratedSource,
1387+
node: stmtPath.node,
1388+
});
1389+
return;
1390+
}
13581391
case 'TypeAlias':
13591392
case 'TSInterfaceDeclaration':
13601393
case 'TSTypeAliasDeclaration': {
13611394
// We do not preserve type annotations/syntax through transformation
13621395
return;
13631396
}
1364-
case 'ClassDeclaration':
13651397
case 'DeclareClass':
13661398
case 'DeclareExportAllDeclaration':
13671399
case 'DeclareExportDeclaration':
@@ -1384,8 +1416,7 @@ function lowerStatement(
13841416
case 'TSExportAssignment':
13851417
case 'TSImportEqualsDeclaration':
13861418
case 'TSModuleDeclaration':
1387-
case 'TSNamespaceExportDeclaration':
1388-
case 'WithStatement': {
1419+
case 'TSNamespaceExportDeclaration': {
13891420
builder.errors.push({
13901421
reason: `(BuildHIR::lowerStatement) Handle ${stmtPath.type} statements`,
13911422
severity: ErrorSeverity.Todo,
@@ -3502,6 +3533,16 @@ function lowerIdentifier(
35023533
return place;
35033534
}
35043535
default: {
3536+
if (binding.kind === 'Global' && binding.name === 'eval') {
3537+
builder.errors.push({
3538+
reason: `The 'eval' function is not supported`,
3539+
description:
3540+
'Eval is an anti-pattern in JavaScript, and the code executed cannot be evaluated by React Compiler',
3541+
severity: ErrorSeverity.InvalidJS,
3542+
loc: exprPath.node.loc ?? null,
3543+
suggestions: null,
3544+
});
3545+
}
35053546
return lowerValueToTemporary(builder, {
35063547
kind: 'LoadGlobal',
35073548
binding,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
## Input
3+
4+
```javascript
5+
function Component(props) {
6+
eval('props.x = true');
7+
return <div />;
8+
}
9+
10+
```
11+
12+
13+
## Error
14+
15+
```
16+
1 | function Component(props) {
17+
> 2 | eval('props.x = true');
18+
| ^^^^ InvalidJS: The 'eval' function is not supported. Eval is an anti-pattern in JavaScript, and the code executed cannot be evaluated by React Compiler (2:2)
19+
3 | return <div />;
20+
4 | }
21+
5 |
22+
```
23+
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function Component(props) {
2+
eval('props.x = true');
3+
return <div />;
4+
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-kitchensink.expect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ let moduleLocal = false;
8484
> 3 | var x = [];
8585
| ^^^^^^^^^^^ Todo: (BuildHIR::lowerStatement) Handle var kinds in VariableDeclaration (3:3)
8686
87-
Todo: (BuildHIR::lowerStatement) Handle ClassDeclaration statements (5:10)
87+
Todo: Support nested class declarations (5:10)
8888
8989
Todo: (BuildHIR::lowerStatement) Handle non-variable initialization in ForStatement (20:22)
9090

0 commit comments

Comments
 (0)