[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.
This commit is contained in:
Sathya Gunasekaran
2023-09-15 16:08:41 +01:00
parent 00e9888fb8
commit 365a88a8aa
5 changed files with 26 additions and 22 deletions
@@ -1246,7 +1246,7 @@ function lowerExpression(
properties.push({
kind: "ObjectProperty",
place: value,
...loweredKey,
key: loweredKey,
});
} else if (propertyPath.isSpreadElement()) {
const place = lowerExpressionToTemporary(
@@ -3125,7 +3125,7 @@ function lowerAssignment(
properties.push({
kind: "ObjectProperty",
place: identifier,
...loweredKey,
key: loweredKey,
});
} else {
const temp = buildTemporaryPlace(
@@ -3135,7 +3135,7 @@ function lowerAssignment(
properties.push({
kind: "ObjectProperty",
place: { ...temp },
...loweredKey,
key: loweredKey,
});
followups.push({ place: temp, path: element as NodePath<t.LVal> }); // TODO remove type cast
}
@@ -546,10 +546,14 @@ export type ObjectPattern = {
properties: Array<ObjectProperty | SpreadPattern>;
};
export type ObjectProperty = {
kind: "ObjectProperty";
export type ObjectPropertyKey = {
type: "string" | "identifier";
name: string; // TODO: make a Place
};
export type ObjectProperty = {
kind: "ObjectProperty";
key: ObjectPropertyKey;
place: Place;
};
@@ -19,7 +19,7 @@ import {
InstructionValue,
LValue,
MutableRange,
ObjectProperty,
ObjectPropertyKey,
Pattern,
Phi,
Place,
@@ -263,12 +263,12 @@ function printHole(): string {
return "<hole>";
}
function printObjectPropertyKey(property: ObjectProperty): string {
switch (property.type) {
function printObjectPropertyKey(key: ObjectPropertyKey): string {
switch (key.type) {
case "identifier":
return property.name;
return key.name;
case "string":
return `'${property.name}'`;
return `'${key.name}'`;
}
}
@@ -295,7 +295,7 @@ export function printInstructionValue(instrValue: ReactiveValue): string {
for (const property of instrValue.properties) {
if (property.kind === "ObjectProperty") {
properties.push(
`${printObjectPropertyKey(property)}: ${printPlace(
`${printObjectPropertyKey(property.key)}: ${printPlace(
property.place
)}`
);
@@ -631,7 +631,7 @@ export function printPattern(pattern: Pattern | Place | SpreadPattern): string {
.map((item) => {
switch (item.kind) {
case "ObjectProperty": {
return `${printObjectPropertyKey(item)}: ${printPattern(
return `${printObjectPropertyKey(item.key)}: ${printPattern(
item.place
)}`;
}
@@ -16,7 +16,7 @@ import {
IdentifierId,
InstructionKind,
JsxAttribute,
ObjectProperty,
ObjectPropertyKey,
Pattern,
Place,
ReactiveBlock,
@@ -937,14 +937,14 @@ function codegenInstructionValue(
const properties = [];
for (const property of instrValue.properties) {
if (property.kind === "ObjectProperty") {
const key = codegenObjectPropertyKey(property);
const key = codegenObjectPropertyKey(property.key);
const value = codegenPlace(cx, property.place);
properties.push(
t.objectProperty(
key,
value,
false,
value.type === "Identifier" && value.name === property.name
value.type === "Identifier" && value.name === property.key.name
)
);
} else {
@@ -1356,13 +1356,13 @@ function convertMemberExpressionToJsx(
}
function codegenObjectPropertyKey(
property: ObjectProperty
key: ObjectPropertyKey
): t.StringLiteral | t.Identifier {
switch (property.type) {
switch (key.type) {
case "identifier":
return t.identifier(property.name);
return t.identifier(key.name);
case "string":
return t.stringLiteral(property.name);
return t.stringLiteral(key.name);
}
}
@@ -1384,13 +1384,13 @@ function codegenLValue(
return t.objectPattern(
pattern.properties.map((property) => {
if (property.kind === "ObjectProperty") {
const key = codegenObjectPropertyKey(property);
const key = codegenObjectPropertyKey(property.key);
const value = codegenLValue(property.place);
return t.objectProperty(
key,
value,
false,
value.type === "Identifier" && value.name === property.name
value.type === "Identifier" && value.name === property.key.name
);
} else {
return t.restElement(codegenLValue(property.place));
@@ -236,7 +236,7 @@ function* generateInstructionTypes(
yield equation(property.place.identifier.type, {
kind: "Property",
object: value.value.identifier.type,
propertyName: property.name,
propertyName: property.key.name,
});
}
}