Redo patch prettier
reverts / redos #1055 (accidentally merged changes from another PR in #1055)
mofeiZ committed
Jan 23, 2023 at 16:03 UTC
16169bec079682708e01bbb2bf20af760cdb6b8a
1 file changed
+22
-26
compiler/forget/src/HIR/BuildHIR.ts
+22
-26
@@ -7,7 +7,6 @@
7
8
import { NodePath, Scope } from "@babel/traverse";
9
import * as t from "@babel/types";
10
-import invariant from "invariant";
10
import { CompilerError, ErrorSeverity } from "../CompilerError";
11
import { Err, Ok, Result } from "../lib/Result";
12
import { assertExhaustive } from "../Utils/utils";
@@ -25,7 +24,7 @@ import {
24
Place,
25
ReturnTerminal,
26
SourceLocation,
28
- ThrowTerminal
27
+ ThrowTerminal,
28
} from "./HIR";
29
import HIRBuilder, { Environment } from "./HIRBuilder";
30
@@ -52,11 +51,8 @@ export function lower(
51
const env = new Environment();
52
const builder = new HIRBuilder(env);
53
55
- // Internal babel is on an older version that does not have hasNode (v7.17)
56
- // See https://github.com/babel/babel/pull/13940/files for impl
57
- // TODO: write helper function for NodePath.node != null
54
const id =
59
- func.isFunctionDeclaration() && (func.get("id").node != null)
55
+ func.isFunctionDeclaration() && func.get("id").hasNode()
56
? builder.resolveIdentifier(func.get("id") as NodePath<t.Identifier>)
57
: null;
58
@@ -136,8 +132,8 @@ function lowerStatement(
132
case "ReturnStatement": {
133
const stmt = stmtPath as NodePath<t.ReturnStatement>;
134
const argument = stmt.get("argument");
139
- const value = (argument.node != null)
140
- ? lowerExpressionToPlace(builder, argument as NodePath<t.Expression>)
135
+ const value = argument.hasNode()
136
+ ? lowerExpressionToPlace(builder, argument)
137
: null;
138
const fallthrough = builder.reserve();
139
const terminal: ReturnTerminal = {
@@ -166,9 +162,9 @@ function lowerStatement(
162
// Block for the alternate (if the test is not truthy)
163
let alternateBlock: BlockId;
164
const alternate = stmt.get("alternate");
169
- if (alternate.node != null) {
165
+ if (alternate.hasNode()) {
166
alternateBlock = builder.enter("block", (blockId) => {
171
- lowerStatement(builder, alternate as NodePath<t.Statement>);
167
+ lowerStatement(builder, alternate);
168
return {
169
kind: "goto",
170
block: continuationBlock.id,
@@ -351,7 +347,7 @@ function lowerStatement(
347
348
const updateBlock = builder.enter("value", (blockId) => {
349
const update = stmt.get("update");
354
- if (!(update.node != null)) {
350
+ if (!update.hasNode()) {
351
builder.pushError({
352
reason: "Handle empty for updater",
353
severity: ErrorSeverity.Todo,
@@ -359,7 +355,7 @@ function lowerStatement(
355
});
356
return { kind: "unsupported", id: makeInstructionId(0) };
357
}
362
- lowerExpressionToVoid(builder, update as NodePath<t.Expression>);
358
+ lowerExpressionToVoid(builder, update);
359
return {
360
kind: "goto",
361
block: testBlock.id,
@@ -395,7 +391,7 @@ function lowerStatement(
391
);
392
393
const test = stmt.get("test");
398
- if (!(test.node != null)) {
394
+ if (!test.hasNode()) {
395
builder.pushError({
396
reason: "ForStatement without test",
397
severity: ErrorSeverity.Todo,
@@ -406,7 +402,7 @@ function lowerStatement(
402
"value",
403
{
404
kind: "if",
409
- test: lowerExpressionToPlace(builder, test as NodePath<t.Expression>),
405
+ test: lowerExpressionToPlace(builder, test),
406
consequent: bodyBlock,
407
alternate: continuationBlock.id,
408
fallthrough: continuationBlock.id,
@@ -574,7 +570,7 @@ function lowerStatement(
570
for (let ii = stmt.get("cases").length - 1; ii >= 0; ii--) {
571
const case_: NodePath<t.SwitchCase> = stmt.get("cases")[ii];
572
const test = case_.get("test");
577
- if (!(test.node != null)) {
573
+ if (!test.hasNode()) {
574
if (hasDefault) {
575
builder.pushError({
576
reason:
@@ -604,7 +600,7 @@ function lowerStatement(
600
});
601
});
602
cases.push({
607
- test: (test.node != null) ? lowerExpressionToPlace(builder, test as NodePath<t.Expression>) : null,
603
+ test: test.hasNode() ? lowerExpressionToPlace(builder, test) : null,
604
block,
605
});
606
fallthrough = block;
@@ -646,13 +642,13 @@ function lowerStatement(
642
*/
643
lowerStatement(builder, stmt.get("block"));
644
const handler = stmt.get("handler");
649
- if (handler.node != null) {
645
+ if (handler.hasNode()) {
646
// TODO: consider whether we need to track the param
651
- lowerStatement(builder, handler.get("body") as NodePath<t.Statement>);
647
+ lowerStatement(builder, handler.get("body"));
648
}
649
const finalizer = stmt.get("finalizer");
654
- if (finalizer.node != null) {
655
- lowerStatement(builder, finalizer as NodePath<t.Statement>);
650
+ if (finalizer.hasNode()) {
651
+ lowerStatement(builder, finalizer);
652
}
653
return;
654
}
@@ -674,8 +670,8 @@ function lowerStatement(
670
const id = declaration.get("id");
671
const init = declaration.get("init");
672
let value: InstructionValue;
677
- if (init.node != null) {
678
- value = lowerExpression(builder, init as NodePath<t.Expression>);
673
+ if (init.hasNode()) {
674
+ value = lowerExpression(builder, init);
675
} else {
676
value = {
677
kind: "Primitive",
@@ -860,7 +856,7 @@ function lowerExpression(
856
let hasError = false;
857
let elements: Place[] = [];
858
for (const element of expr.get("elements")) {
863
- if (!(element.node != null) || !element.isExpression()) {
859
+ if (!element.hasNode() || !element.isExpression()) {
860
builder.pushError({
861
reason: "Handle non-expression array elements",
862
severity: ErrorSeverity.Todo,
@@ -869,7 +865,7 @@ function lowerExpression(
865
hasError = true;
866
continue;
867
}
872
- elements.push(lowerExpressionToPlace(builder, element as NodePath<t.Expression>));
868
+ elements.push(lowerExpressionToPlace(builder, element));
869
}
870
return hasError
871
? { kind: "UnsupportedNode", node: exprNode, loc: exprLoc }
@@ -1774,7 +1770,7 @@ function lowerAssignment(
1770
let hasError = false;
1771
for (let i = 0; i < elements.length; i++) {
1772
const element = elements[i];
1777
- if (!(element.node != null)) {
1773
+ if (!element.hasNode()) {
1774
continue;
1775
}
1776
if (element.node.type === "RestElement") {
@@ -1806,7 +1802,7 @@ function lowerAssignment(
1802
object: { ...arrayPlace },
1803
property,
1804
};
1809
- lowerAssignment(builder, loc, kind, element as NodePath<t.LVal>, value);
1805
+ lowerAssignment(builder, loc, kind, element, value);
1806
}
1807
return hasError
1808
? { kind: "UnsupportedNode", node: lvalueNode, loc }