@samitouri / QOS-React / commits / f18a01bf59

Add todo for destructuring to context variables

We don't have `DestructureContext` yet, this adds a todo for the cases where we should use one.

Joe Savona committed Mar 21, 2024 at 17:12 UTC f18a01bf59be6107daa817432f20b9e912e84d35
2 files changed +34 -18
compiler/packages/babel-plugin-react-forget/src/Validation/ValidateContextVariableLValues.ts
+30 -14
@@ -56,7 +56,7 @@ function validateContextVariableLValuesImpl(
56 }
57 case "Destructure": {
58 for (const lvalue of eachPatternOperand(value.lvalue.pattern)) {
59 - visit(identifierKinds, lvalue, "local");
59 + visit(identifierKinds, lvalue, "destructure");
60 }
61 break;
62 }
@@ -84,23 +84,39 @@ function validateContextVariableLValuesImpl(
84 }
85 }
86
87 -type IdentifierKinds = Map<IdentifierId, "local" | "context">;
87 +type IdentifierKinds = Map<
88 + IdentifierId,
89 + { place: Place; kind: "local" | "context" | "destructure" }
90 +>;
91
92 function visit(
93 identifiers: IdentifierKinds,
94 place: Place,
92 - kind: "local" | "context"
95 + kind: "local" | "context" | "destructure"
96 ): void {
94 - const prevKind = identifiers.get(place.identifier.id);
95 - if (prevKind !== undefined && prevKind !== kind) {
96 - CompilerError.invariant(false, {
97 - reason: `Expected all references to a variable to be consistently local or context references`,
98 - loc: place.loc,
99 - description: `Identifier ${printPlace(
100 - place
101 - )} is referenced as a ${kind} variable, but was previously referenced as a ${prevKind} variable`,
102 - suggestions: null,
103 - });
97 + const prev = identifiers.get(place.identifier.id);
98 + if (prev !== undefined) {
99 + const wasContext = prev.kind === "context";
100 + const isContext = kind === "context";
101 + if (wasContext !== isContext) {
102 + if (prev.kind === "destructure" || kind === "destructure") {
103 + CompilerError.throwTodo({
104 + reason: `Support destructuring of context variables`,
105 + loc: kind === "destructure" ? place.loc : prev.place.loc,
106 + description: null,
107 + suggestions: null,
108 + });
109 + }
110 +
111 + CompilerError.invariant(false, {
112 + reason: `Expected all references to a variable to be consistently local or context references`,
113 + loc: place.loc,
114 + description: `Identifier ${printPlace(
115 + place
116 + )} is referenced as a ${kind} variable, but was previously referenced as a ${prev} variable`,
117 + suggestions: null,
118 + });
119 + }
120 }
105 - identifiers.set(place.identifier.id, kind);
121 + identifiers.set(place.identifier.id, { place, kind });
122 }
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-reassign-const.expect.md
+4 -4
@@ -21,13 +21,13 @@ function Component({ foo }) {
21 ## Error
22
23 ```
24 + 1 | import { Stringify } from "shared-runtime";
25 2 |
25 - 3 | function Component({ foo }) {
26 -> 4 | let bar = foo.bar;
27 - | ^^^ [ReactForget] Invariant: Expected all references to a variable to be consistently local or context references. Identifier <unknown> foo$1 is referenced as a context variable, but was previously referenced as a local variable (4:4)
26 +> 3 | function Component({ foo }) {
27 + | ^^^ [ReactForget] Todo: Support destructuring of context variables (3:3)
28 + 4 | let bar = foo.bar;
29 5 | return (
30 6 | <Stringify
30 - 7 | handler={() => {
31 ```
32
33
\ No newline at end of file