Represent ObjectExpression properties as Array<ObjectProperty>
Refactors the representation of ObjectExpression properties from a Map to an `Array<ObjectProperty>` to prepare for the next diff which adds spread element support.
Joe Savona committed
Mar 6, 2023 at 15:19 UTC
7893a6c40382969107434d05563c60e1ad5d0750
6 files changed
+22
-19
compiler/forget/src/HIR/BuildHIR.ts
+7
-2
@@ -29,6 +29,7 @@ import {
29
JsxAttribute,
30
makeInstructionId,
31
ObjectPattern,
32
+ ObjectProperty,
33
Place,
34
ReturnTerminal,
35
SourceLocation,
@@ -798,7 +799,7 @@ function lowerExpression(
799
case "ObjectExpression": {
800
const expr = exprPath as NodePath<t.ObjectExpression>;
801
const propertyPaths = expr.get("properties");
801
- const properties: Map<string, Place> = new Map();
802
+ const properties: Array<ObjectProperty> = [];
803
for (const propertyPath of propertyPaths) {
804
if (!propertyPath.isObjectProperty()) {
805
builder.errors.push({
@@ -832,7 +833,11 @@ function lowerExpression(
833
continue;
834
}
835
const value = lowerExpressionToTemporary(builder, valuePath);
835
- properties.set(keyName, value);
836
+ properties.push({
837
+ kind: "ObjectProperty",
838
+ name: keyName,
839
+ place: value,
840
+ });
841
}
842
return {
843
kind: "ObjectExpression",
compiler/forget/src/HIR/HIR.ts
+1
-1
@@ -518,7 +518,7 @@ export type InstructionValue =
518
}
519
| {
520
kind: "ObjectExpression";
521
- properties: Map<string, Place> | null; // null === empty object
521
+ properties: Array<ObjectProperty>; // null === empty object
522
loc: SourceLocation;
523
}
524
| { kind: "ArrayExpression"; elements: Array<Place>; loc: SourceLocation }
compiler/forget/src/HIR/PrintHIR.ts
+2
-2
@@ -229,8 +229,8 @@ export function printInstructionValue(instrValue: ReactiveValue): string {
229
case "ObjectExpression": {
230
const properties = [];
231
if (instrValue.properties !== null) {
232
- for (const [key, value] of instrValue.properties) {
233
- properties.push(`${key}: ${printPlace(value)}`);
232
+ for (const property of instrValue.properties) {
233
+ properties.push(`${property.name}: ${printPlace(property.place)}`);
234
}
235
}
236
value = `Object { ${properties.join(", ")} }`;
compiler/forget/src/HIR/visitors.ts
+4
-7
@@ -130,8 +130,8 @@ export function* eachInstructionValueOperand(
130
break;
131
}
132
case "ObjectExpression": {
133
- if (instrValue.properties !== null) {
134
- yield* instrValue.properties.values();
133
+ for (const property of instrValue.properties) {
134
+ yield property.place;
135
}
136
break;
137
}
@@ -325,11 +325,8 @@ export function mapInstructionOperands(
325
break;
326
}
327
case "ObjectExpression": {
328
- if (instrValue.properties !== null) {
329
- const props = instrValue.properties;
330
- for (const [prop, place] of props) {
331
- props.set(prop, fn(place));
332
- }
328
+ for (const property of instrValue.properties) {
329
+ property.place = fn(property.place);
330
}
331
break;
332
}
compiler/forget/src/Inference/InferReferenceEffects.ts
+1
-1
@@ -100,7 +100,7 @@ export default function inferReferenceEffects(fn: HIRFunction) {
100
// TODO(gsn): This is a hack.
101
const value: InstructionValue = {
102
kind: "ObjectExpression",
103
- properties: null,
103
+ properties: [],
104
loc: ref.loc,
105
};
106
initialState.initialize(value, ValueKind.Context);
compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts
+7
-6
@@ -585,12 +585,13 @@ function codegenInstructionValue(
585
}
586
case "ObjectExpression": {
587
const properties = [];
588
- if (instrValue.properties !== null) {
589
- for (const [property, value] of instrValue.properties) {
590
- properties.push(
591
- t.objectProperty(t.stringLiteral(property), codegenPlace(cx, value))
592
- );
593
- }
588
+ for (const property of instrValue.properties) {
589
+ properties.push(
590
+ t.objectProperty(
591
+ t.stringLiteral(property.name),
592
+ codegenPlace(cx, property.place)
593
+ )
594
+ );
595
}
596
value = t.objectExpression(properties);
597
break;