[valueblocks] Support sequence expressions (comma operator)
Joe Savona committed
Jan 31, 2023 at 13:39 UTC
149353badb29b904ba0257b6679917d2d40a5b3c
3 files changed
+43
-28
compiler/forget/src/HIR/BuildHIR.ts
+18
@@ -889,6 +889,24 @@ function lowerExpression(
889
loc: exprLoc,
890
};
891
}
892
+ case "SequenceExpression": {
893
+ const expr = exprPath as NodePath<t.SequenceExpression>;
894
+ const exprLoc = expr.node.loc ?? GeneratedSource;
895
+
896
+ let last: Place | null = null;
897
+ for (const item of expr.get("expressions")) {
898
+ last = lowerExpressionToPlace(builder, item);
899
+ }
900
+ if (last === null) {
901
+ builder.errors.push({
902
+ reason: `(BuildHIR::lowerExpression) Expected SequenceExpression to have at least one expression`,
903
+ severity: ErrorSeverity.InvalidInput,
904
+ nodePath: expr,
905
+ });
906
+ return { kind: "UnsupportedNode", node: expr.node, loc: exprLoc };
907
+ }
908
+ return last;
909
+ }
910
case "ConditionalExpression": {
911
const expr = exprPath as NodePath<t.ConditionalExpression>;
912
const exprLoc = expr.node.loc ?? GeneratedSource;
compiler/forget/src/__tests__/fixtures/hir/sequence-expression.expect.md
+20
-20
@@ -2,50 +2,50 @@
2
## Input
3
4
```javascript
5
-// @only
5
function sequence(props) {
7
- let x = (null, Math.max(1, 2), sequence({}));
8
- if (((x = x + 1), x < 10)) {
9
- x = 10;
6
+ let x = (null, Math.max(1, 2), foo());
7
+ while ((foo(), true)) {
8
+ x = (foo(), 2);
9
}
11
- // while (((x = x * 2), x < 20)) {
12
- // x = ((x = x + 1), x + 1);
13
- // }
10
return x;
11
}
12
13
+function foo() {}
14
+
15
```
16
17
## Code
18
19
```javascript
22
-// @only
20
function sequence(props) {
21
const $ = React.useMemoCache();
22
null;
23
Math.max(1, 2);
24
let t0;
25
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
29
- t0 = sequence({});
26
+ t0 = foo();
27
$[0] = t0;
28
} else {
29
t0 = $[0];
30
}
34
- const x = t0;
35
- const x$0 = x + 1;
36
- let x$1;
37
- if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
38
- x$1 = x$0;
39
- if (x$0 < 10) {
40
- const x$2 = 10;
41
- x$1 = x$2;
31
+ const c_1 = $[1] !== t0;
32
+ let x;
33
+ if (c_1) {
34
+ x = t0;
35
+ while ((foo(), true)) {
36
+ foo();
37
+ 2;
38
+ x = 2;
39
}
43
- $[1] = x$1;
40
+ $[1] = t0;
41
+ $[2] = x;
42
} else {
45
- x$1 = $[1];
43
+ x = $[2];
44
}
47
- return x$1;
45
+ return x;
46
}
47
48
+function foo() {}
49
+
50
```
51
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/sequence-expression.js
+5
-8
@@ -1,12 +1,9 @@
1
-// @skip
1
function sequence(props) {
3
- let x = (null, Math.max(1, 2), sequence({}));
4
- if (((x = x + 1), x < 10)) {
5
- x = 10;
6
- }
7
- x = ((x = x + 1), x > 15) && x < 20 ? ((x = x + 1), x) : 42;
8
- while (((x = x * 2), x < 20)) {
9
- x = ((x = x + 1), x + 1);
2
+ let x = (null, Math.max(1, 2), foo());
3
+ while ((foo(), true)) {
4
+ x = (foo(), 2);
5
}
6
return x;
7
}
8
+
9
+function foo() {}