Skip to content

Commit 3abbfe0

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 3abbfe0

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,13 +1355,42 @@ 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+
// We can in theory support nested classes, similarly to functions where we assume that all
1375+
builder.errors.push({
1376+
reason: `Support nested class declarations`,
1377+
severity: ErrorSeverity.Todo,
1378+
loc: stmtPath.node.loc ?? null,
1379+
suggestions: null,
1380+
});
1381+
lowerValueToTemporary(builder, {
1382+
kind: 'UnsupportedNode',
1383+
loc: stmtPath.node.loc ?? GeneratedSource,
1384+
node: stmtPath.node,
1385+
});
1386+
return;
1387+
}
13581388
case 'TypeAlias':
13591389
case 'TSInterfaceDeclaration':
13601390
case 'TSTypeAliasDeclaration': {
13611391
// We do not preserve type annotations/syntax through transformation
13621392
return;
13631393
}
1364-
case 'ClassDeclaration':
13651394
case 'DeclareClass':
13661395
case 'DeclareExportAllDeclaration':
13671396
case 'DeclareExportDeclaration':
@@ -1384,8 +1413,7 @@ function lowerStatement(
13841413
case 'TSExportAssignment':
13851414
case 'TSImportEqualsDeclaration':
13861415
case 'TSModuleDeclaration':
1387-
case 'TSNamespaceExportDeclaration':
1388-
case 'WithStatement': {
1416+
case 'TSNamespaceExportDeclaration': {
13891417
builder.errors.push({
13901418
reason: `(BuildHIR::lowerStatement) Handle ${stmtPath.type} statements`,
13911419
severity: ErrorSeverity.Todo,
@@ -3502,6 +3530,16 @@ function lowerIdentifier(
35023530
return place;
35033531
}
35043532
default: {
3533+
if (binding.kind === 'Global' && binding.name === 'eval') {
3534+
builder.errors.push({
3535+
reason: `The 'eval' function is not supported`,
3536+
description:
3537+
'Eval is an anti-pattern in JavaScript, and the code executed cannot be evaluated by React Compiler',
3538+
severity: ErrorSeverity.InvalidJS,
3539+
loc: exprPath.node.loc ?? null,
3540+
suggestions: null,
3541+
});
3542+
}
35053543
return lowerValueToTemporary(builder, {
35063544
kind: 'LoadGlobal',
35073545
binding,
Lines changed: 24 additions & 0 deletions
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+
Lines changed: 4 additions & 0 deletions
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)