@samitouri / QOS-React / commits / 365a88a8aa

[hir] Refactor out ObjectPropertyKey

The type of ObjectProperty is specific to the key, not the ObjectProperty. In the future, we want to add a type to represent the value of the ObjectProperty. This PR moves out the fields related to the key of the ObjectProperty to a separate type.

Sathya Gunasekaran committed Sep 15, 2023 at 16:08 UTC 365a88a8aae2a801a4dc21a2910e02b03f4d2ca8
5 files changed +26 -22
compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts
+3 -3
@@ -1246,7 +1246,7 @@ function lowerExpression(
1246 properties.push({
1247 kind: "ObjectProperty",
1248 place: value,
1249 - ...loweredKey,
1249 + key: loweredKey,
1250 });
1251 } else if (propertyPath.isSpreadElement()) {
1252 const place = lowerExpressionToTemporary(
@@ -3125,7 +3125,7 @@ function lowerAssignment(
3125 properties.push({
3126 kind: "ObjectProperty",
3127 place: identifier,
3128 - ...loweredKey,
3128 + key: loweredKey,
3129 });
3130 } else {
3131 const temp = buildTemporaryPlace(
@@ -3135,7 +3135,7 @@ function lowerAssignment(
3135 properties.push({
3136 kind: "ObjectProperty",
3137 place: { ...temp },
3138 - ...loweredKey,
3138 + key: loweredKey,
3139 });
3140 followups.push({ place: temp, path: element as NodePath<t.LVal> }); // TODO remove type cast
3141 }
compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts
+6 -2
@@ -546,10 +546,14 @@ export type ObjectPattern = {
546 properties: Array<ObjectProperty | SpreadPattern>;
547 };
548
549 -export type ObjectProperty = {
550 - kind: "ObjectProperty";
549 +export type ObjectPropertyKey = {
550 type: "string" | "identifier";
551 name: string; // TODO: make a Place
552 +};
553 +
554 +export type ObjectProperty = {
555 + kind: "ObjectProperty";
556 + key: ObjectPropertyKey;
557 place: Place;
558 };
559
compiler/packages/babel-plugin-react-forget/src/HIR/PrintHIR.ts
+7 -7
@@ -19,7 +19,7 @@ import {
19 InstructionValue,
20 LValue,
21 MutableRange,
22 - ObjectProperty,
22 + ObjectPropertyKey,
23 Pattern,
24 Phi,
25 Place,
@@ -263,12 +263,12 @@ function printHole(): string {
263 return "<hole>";
264 }
265
266 -function printObjectPropertyKey(property: ObjectProperty): string {
267 - switch (property.type) {
266 +function printObjectPropertyKey(key: ObjectPropertyKey): string {
267 + switch (key.type) {
268 case "identifier":
269 - return property.name;
269 + return key.name;
270 case "string":
271 - return `'${property.name}'`;
271 + return `'${key.name}'`;
272 }
273 }
274
@@ -295,7 +295,7 @@ export function printInstructionValue(instrValue: ReactiveValue): string {
295 for (const property of instrValue.properties) {
296 if (property.kind === "ObjectProperty") {
297 properties.push(
298 - `${printObjectPropertyKey(property)}: ${printPlace(
298 + `${printObjectPropertyKey(property.key)}: ${printPlace(
299 property.place
300 )}`
301 );
@@ -631,7 +631,7 @@ export function printPattern(pattern: Pattern | Place | SpreadPattern): string {
631 .map((item) => {
632 switch (item.kind) {
633 case "ObjectProperty": {
634 - return `${printObjectPropertyKey(item)}: ${printPattern(
634 + return `${printObjectPropertyKey(item.key)}: ${printPattern(
635 item.place
636 )}`;
637 }
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts
+9 -9
@@ -16,7 +16,7 @@ import {
16 IdentifierId,
17 InstructionKind,
18 JsxAttribute,
19 - ObjectProperty,
19 + ObjectPropertyKey,
20 Pattern,
21 Place,
22 ReactiveBlock,
@@ -937,14 +937,14 @@ function codegenInstructionValue(
937 const properties = [];
938 for (const property of instrValue.properties) {
939 if (property.kind === "ObjectProperty") {
940 - const key = codegenObjectPropertyKey(property);
940 + const key = codegenObjectPropertyKey(property.key);
941 const value = codegenPlace(cx, property.place);
942 properties.push(
943 t.objectProperty(
944 key,
945 value,
946 false,
947 - value.type === "Identifier" && value.name === property.name
947 + value.type === "Identifier" && value.name === property.key.name
948 )
949 );
950 } else {
@@ -1356,13 +1356,13 @@ function convertMemberExpressionToJsx(
1356 }
1357
1358 function codegenObjectPropertyKey(
1359 - property: ObjectProperty
1359 + key: ObjectPropertyKey
1360 ): t.StringLiteral | t.Identifier {
1361 - switch (property.type) {
1361 + switch (key.type) {
1362 case "identifier":
1363 - return t.identifier(property.name);
1363 + return t.identifier(key.name);
1364 case "string":
1365 - return t.stringLiteral(property.name);
1365 + return t.stringLiteral(key.name);
1366 }
1367 }
1368
@@ -1384,13 +1384,13 @@ function codegenLValue(
1384 return t.objectPattern(
1385 pattern.properties.map((property) => {
1386 if (property.kind === "ObjectProperty") {
1387 - const key = codegenObjectPropertyKey(property);
1387 + const key = codegenObjectPropertyKey(property.key);
1388 const value = codegenLValue(property.place);
1389 return t.objectProperty(
1390 key,
1391 value,
1392 false,
1393 - value.type === "Identifier" && value.name === property.name
1393 + value.type === "Identifier" && value.name === property.key.name
1394 );
1395 } else {
1396 return t.restElement(codegenLValue(property.place));
compiler/packages/babel-plugin-react-forget/src/TypeInference/InferTypes.ts
+1 -1
@@ -236,7 +236,7 @@ function* generateInstructionTypes(
236 yield equation(property.place.identifier.type, {
237 kind: "Property",
238 object: value.value.identifier.type,
239 - propertyName: property.name,
239 + propertyName: property.key.name,
240 });
241 }
242 }