Support string literal keys for object expressions

For ObjectExpression, we now support computed keys where the key is a string 
literal.
This commit is contained in:
Joe Savona
2023-03-06 14:51:18 -08:00
parent a9d6d2d95a
commit f3c662845b
3 changed files with 42 additions and 2 deletions
+7 -2
View File
@@ -809,7 +809,12 @@ function lowerExpression(
continue;
}
const key = propertyPath.node.key;
if (key.type !== "Identifier") {
let keyName: string;
if (key.type === "Identifier") {
keyName = key.name;
} else if (key.type === "StringLiteral") {
keyName = key.value;
} else {
builder.errors.push({
reason: `(BuildHIR::lowerExpression) Expected Identifier, got ${key.type} key in ObjectExpression`,
severity: ErrorSeverity.InvalidInput,
@@ -827,7 +832,7 @@ function lowerExpression(
continue;
}
const value = lowerExpressionToTemporary(builder, valuePath);
properties.set(key.name, value);
properties.set(keyName, value);
}
return {
kind: "ObjectExpression",
@@ -0,0 +1,31 @@
## Input
```javascript
function Component(props) {
const x = { ["foo"]: props.foo };
return x;
}
```
## Code
```javascript
function Component(props) {
const $ = React.unstable_useMemoCache(2);
const c_0 = $[0] !== props.foo;
let t0;
if (c_0) {
t0 = { foo: props.foo };
$[0] = props.foo;
$[1] = t0;
} else {
t0 = $[1];
}
const x = t0;
return x;
}
```
@@ -0,0 +1,4 @@
function Component(props) {
const x = { ["foo"]: props.foo };
return x;
}