@samitouri / QOS-React-1 / commits / 52875a72fe

Invariant if codegen tries to emit a temporary as an identifier

For T181507827 — adds an invariant in codegen when emitting identifiers to ensure that we only create babel Identifier nodes for nodes that the compiler has explicitly promoted to valid, named identifiers. This means that we'll fail for unnamed temporaries (previously caught), as well as promoted temporaries that somehow didn't get renamed by RenameVariables (newly caught).

Joe Savona committed Mar 6, 2024 at 21:50 UTC 52875a72fef2722347e4aa37ed5da30d7bd4f31e
3 files changed +44 -6
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts
+9 -6
@@ -2060,11 +2060,14 @@ function codegenPlace(cx: Context, place: Place): t.Expression | t.JSXText {
2060 }
2061
2062 function convertIdentifier(identifier: Identifier): t.Identifier {
2063 - CompilerError.invariant(identifier.name !== null, {
2064 - reason: `Expected temporaries to be promoted to named identifiers in an earlier pass`,
2065 - loc: GeneratedSource,
2066 - description: `identifier ${identifier.id} is unnamed`,
2067 - suggestions: null,
2068 - });
2063 + CompilerError.invariant(
2064 + identifier.name !== null && identifier.name.kind === "named",
2065 + {
2066 + reason: `Expected temporaries to be promoted to named identifiers in an earlier pass`,
2067 + loc: GeneratedSource,
2068 + description: `identifier ${identifier.id} is unnamed`,
2069 + suggestions: null,
2070 + }
2071 + );
2072 return t.identifier(identifier.name.value);
2073 }
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-try-catch-within-function-expression.expect.md new
+25
@@ -0,0 +1,25 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Component(props) {
6 + const callback = () => {
7 + try {
8 + return [];
9 + } catch (e) {
10 + return;
11 + }
12 + };
13 + return callback();
14 +}
15 +
16 +```
17 +
18 +
19 +## Error
20 +
21 +```
22 +[ReactForget] Invariant: Expected temporaries to be promoted to named identifiers in an earlier pass. identifier 16 is unnamed
23 +```
24 +
25 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-try-catch-within-function-expression.js new
+10
@@ -0,0 +1,10 @@
1 +function Component(props) {
2 + const callback = () => {
3 + try {
4 + return [];
5 + } catch (e) {
6 + return;
7 + }
8 + };
9 + return callback();
10 +}