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).
This commit is contained in:
Joe Savona
2024-03-06 21:50:03 -08:00
parent ebbada309d
commit 52875a72fe
3 changed files with 44 additions and 6 deletions
@@ -2060,11 +2060,14 @@ function codegenPlace(cx: Context, place: Place): t.Expression | t.JSXText {
}
function convertIdentifier(identifier: Identifier): t.Identifier {
CompilerError.invariant(identifier.name !== null, {
reason: `Expected temporaries to be promoted to named identifiers in an earlier pass`,
loc: GeneratedSource,
description: `identifier ${identifier.id} is unnamed`,
suggestions: null,
});
CompilerError.invariant(
identifier.name !== null && identifier.name.kind === "named",
{
reason: `Expected temporaries to be promoted to named identifiers in an earlier pass`,
loc: GeneratedSource,
description: `identifier ${identifier.id} is unnamed`,
suggestions: null,
}
);
return t.identifier(identifier.name.value);
}
@@ -0,0 +1,25 @@
## Input
```javascript
function Component(props) {
const callback = () => {
try {
return [];
} catch (e) {
return;
}
};
return callback();
}
```
## Error
```
[ReactForget] Invariant: Expected temporaries to be promoted to named identifiers in an earlier pass. identifier 16 is unnamed
```
@@ -0,0 +1,10 @@
function Component(props) {
const callback = () => {
try {
return [];
} catch (e) {
return;
}
};
return callback();
}