Support string literal keys for object expressions
For ObjectExpression, we now support computed keys where the key is a string literal.
Joe Savona committed
Mar 6, 2023 at 14:51 UTC
f3c662845bc43f0f4f5d449fdce1081b164e0174
3 files changed
+42
-2
compiler/forget/src/HIR/BuildHIR.ts
+7
-2
@@ -809,7 +809,12 @@ function lowerExpression(
809
continue;
810
}
811
const key = propertyPath.node.key;
812
- if (key.type !== "Identifier") {
812
+ let keyName: string;
813
+ if (key.type === "Identifier") {
814
+ keyName = key.name;
815
+ } else if (key.type === "StringLiteral") {
816
+ keyName = key.value;
817
+ } else {
818
builder.errors.push({
819
reason: `(BuildHIR::lowerExpression) Expected Identifier, got ${key.type} key in ObjectExpression`,
820
severity: ErrorSeverity.InvalidInput,
@@ -827,7 +832,7 @@ function lowerExpression(
832
continue;
833
}
834
const value = lowerExpressionToTemporary(builder, valuePath);
830
- properties.set(key.name, value);
835
+ properties.set(keyName, value);
836
}
837
return {
838
kind: "ObjectExpression",
compiler/forget/src/__tests__/fixtures/hir/object-expression-string-literal-key.expect.md
new
+31
@@ -0,0 +1,31 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ const x = { ["foo"]: props.foo };
7
+ return x;
8
+}
9
+
10
+```
11
+
12
+## Code
13
+
14
+```javascript
15
+function Component(props) {
16
+ const $ = React.unstable_useMemoCache(2);
17
+ const c_0 = $[0] !== props.foo;
18
+ let t0;
19
+ if (c_0) {
20
+ t0 = { foo: props.foo };
21
+ $[0] = props.foo;
22
+ $[1] = t0;
23
+ } else {
24
+ t0 = $[1];
25
+ }
26
+ const x = t0;
27
+ return x;
28
+}
29
+
30
+```
31
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/object-expression-string-literal-key.js
new
+4
@@ -0,0 +1,4 @@
1
+function Component(props) {
2
+ const x = { ["foo"]: props.foo };
3
+ return x;
4
+}