mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Bailout on assigning to globals
This commit is contained in:
@@ -1790,7 +1790,36 @@ function lowerAssignment(
|
||||
switch (lvalueNode.type) {
|
||||
case "Identifier": {
|
||||
const lvalue = lvaluePath as NodePath<t.Identifier>;
|
||||
const place = lowerIdentifier(builder, lvalue);
|
||||
const identifier = builder.resolveIdentifier(lvalue);
|
||||
if (identifier == null) {
|
||||
if (kind === InstructionKind.Reassign) {
|
||||
// Trying to reassign a global is not allowed
|
||||
builder.errors.push({
|
||||
reason: `(BuildHIR::lowerAssignment) Assigning to an identifier defined outside the function scope is not supported.`,
|
||||
severity: ErrorSeverity.InvalidInput,
|
||||
nodePath: lvalue,
|
||||
});
|
||||
} else {
|
||||
// Else its an internal error bc we couldn't find the binding
|
||||
builder.errors.push({
|
||||
reason: `(BuildHIR::lowerAssignment) Could not find binding for declaration.`,
|
||||
severity: ErrorSeverity.Invariant,
|
||||
nodePath: lvalue,
|
||||
});
|
||||
}
|
||||
return {
|
||||
kind: "UnsupportedNode",
|
||||
loc: lvalue.node.loc ?? GeneratedSource,
|
||||
node: lvalue.node,
|
||||
};
|
||||
}
|
||||
|
||||
const place: Place = {
|
||||
kind: "Identifier",
|
||||
identifier: identifier,
|
||||
effect: Effect.Unknown,
|
||||
loc: lvalue.node.loc ?? GeneratedSource,
|
||||
};
|
||||
builder.push({
|
||||
id: makeInstructionId(0),
|
||||
lvalue: { place: { ...place }, kind },
|
||||
|
||||
@@ -68,6 +68,9 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) {
|
||||
default: {
|
||||
}
|
||||
}
|
||||
|
||||
// Cannot assign to globals
|
||||
someUnknownGlobal = true;
|
||||
}
|
||||
|
||||
```
|
||||
@@ -400,6 +403,14 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) {
|
||||
59 | }
|
||||
60 | case foo(): {
|
||||
61 | }
|
||||
|
||||
[ReactForget] InvalidInputError: (BuildHIR::lowerAssignment) Assigning to an identifier defined outside the function scope is not supported.
|
||||
67 |
|
||||
68 | // Cannot assign to globals
|
||||
> 69 | someUnknownGlobal = true;
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
70 | }
|
||||
71 |
|
||||
```
|
||||
|
||||
|
||||
@@ -64,4 +64,7 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) {
|
||||
default: {
|
||||
}
|
||||
}
|
||||
|
||||
// Cannot assign to globals
|
||||
someUnknownGlobal = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user