Inline hasNode check (hack)
www currently on an older version of babel
mofeiZ committed
Jan 23, 2023 at 16:08 UTC
3ba90a7f9c5ad26e7da84e539b3ee9b58e86717b
1 file changed
+34
-22
compiler/forget/src/HIR/BuildHIR.ts
+34
-22
@@ -51,8 +51,11 @@ export function lower(
51
const env = new Environment();
52
const builder = new HIRBuilder(env);
53
54
+ // Internal babel is on an older version that does not have hasNode (v7.17)
55
+ // See https://github.com/babel/babel/pull/13940/files for impl
56
+ // TODO: write helper function for NodePath.node != null
57
const id =
55
- func.isFunctionDeclaration() && func.get("id").hasNode()
58
+ func.isFunctionDeclaration() && func.get("id").node != null
59
? builder.resolveIdentifier(func.get("id") as NodePath<t.Identifier>)
60
: null;
61
@@ -132,9 +135,10 @@ function lowerStatement(
135
case "ReturnStatement": {
136
const stmt = stmtPath as NodePath<t.ReturnStatement>;
137
const argument = stmt.get("argument");
135
- const value = argument.hasNode()
136
- ? lowerExpressionToPlace(builder, argument)
137
- : null;
138
+ const value =
139
+ argument.node != null
140
+ ? lowerExpressionToPlace(builder, argument as NodePath<t.Expression>)
141
+ : null;
142
const fallthrough = builder.reserve();
143
const terminal: ReturnTerminal = {
144
kind: "return",
@@ -162,9 +166,9 @@ function lowerStatement(
166
// Block for the alternate (if the test is not truthy)
167
let alternateBlock: BlockId;
168
const alternate = stmt.get("alternate");
165
- if (alternate.hasNode()) {
169
+ if (alternate.node != null) {
170
alternateBlock = builder.enter("block", (blockId) => {
167
- lowerStatement(builder, alternate);
171
+ lowerStatement(builder, alternate as NodePath<t.Statement>);
172
return {
173
kind: "goto",
174
block: continuationBlock.id,
@@ -347,7 +351,7 @@ function lowerStatement(
351
352
const updateBlock = builder.enter("value", (blockId) => {
353
const update = stmt.get("update");
350
- if (!update.hasNode()) {
354
+ if (update.node == null) {
355
builder.pushError({
356
reason: "Handle empty for updater",
357
severity: ErrorSeverity.Todo,
@@ -355,7 +359,7 @@ function lowerStatement(
359
});
360
return { kind: "unsupported", id: makeInstructionId(0) };
361
}
358
- lowerExpressionToVoid(builder, update);
362
+ lowerExpressionToVoid(builder, update as NodePath<t.Expression>);
363
return {
364
kind: "goto",
365
block: testBlock.id,
@@ -391,7 +395,7 @@ function lowerStatement(
395
);
396
397
const test = stmt.get("test");
394
- if (!test.hasNode()) {
398
+ if (test.node == null) {
399
builder.pushError({
400
reason: "ForStatement without test",
401
severity: ErrorSeverity.Todo,
@@ -402,7 +406,10 @@ function lowerStatement(
406
"value",
407
{
408
kind: "if",
405
- test: lowerExpressionToPlace(builder, test),
409
+ test: lowerExpressionToPlace(
410
+ builder,
411
+ test as NodePath<t.Expression>
412
+ ),
413
consequent: bodyBlock,
414
alternate: continuationBlock.id,
415
fallthrough: continuationBlock.id,
@@ -570,7 +577,7 @@ function lowerStatement(
577
for (let ii = stmt.get("cases").length - 1; ii >= 0; ii--) {
578
const case_: NodePath<t.SwitchCase> = stmt.get("cases")[ii];
579
const test = case_.get("test");
573
- if (!test.hasNode()) {
580
+ if (test.node == null) {
581
if (hasDefault) {
582
builder.pushError({
583
reason:
@@ -600,7 +607,10 @@ function lowerStatement(
607
});
608
});
609
cases.push({
603
- test: test.hasNode() ? lowerExpressionToPlace(builder, test) : null,
610
+ test:
611
+ test.node != null
612
+ ? lowerExpressionToPlace(builder, test as NodePath<t.Expression>)
613
+ : null,
614
block,
615
});
616
fallthrough = block;
@@ -642,13 +652,13 @@ function lowerStatement(
652
*/
653
lowerStatement(builder, stmt.get("block"));
654
const handler = stmt.get("handler");
645
- if (handler.hasNode()) {
655
+ if (handler.node != null) {
656
// TODO: consider whether we need to track the param
647
- lowerStatement(builder, handler.get("body"));
657
+ lowerStatement(builder, handler.get("body") as NodePath<t.Statement>);
658
}
659
const finalizer = stmt.get("finalizer");
650
- if (finalizer.hasNode()) {
651
- lowerStatement(builder, finalizer);
660
+ if (finalizer.node != null) {
661
+ lowerStatement(builder, finalizer as NodePath<t.Statement>);
662
}
663
return;
664
}
@@ -670,8 +680,8 @@ function lowerStatement(
680
const id = declaration.get("id");
681
const init = declaration.get("init");
682
let value: InstructionValue;
673
- if (init.hasNode()) {
674
- value = lowerExpression(builder, init);
683
+ if (init.node != null) {
684
+ value = lowerExpression(builder, init as NodePath<t.Expression>);
685
} else {
686
value = {
687
kind: "Primitive",
@@ -856,7 +866,7 @@ function lowerExpression(
866
let hasError = false;
867
let elements: Place[] = [];
868
for (const element of expr.get("elements")) {
859
- if (!element.hasNode() || !element.isExpression()) {
869
+ if (element.node == null || !element.isExpression()) {
870
builder.pushError({
871
reason: "Handle non-expression array elements",
872
severity: ErrorSeverity.Todo,
@@ -865,7 +875,9 @@ function lowerExpression(
875
hasError = true;
876
continue;
877
}
868
- elements.push(lowerExpressionToPlace(builder, element));
878
+ elements.push(
879
+ lowerExpressionToPlace(builder, element as NodePath<t.Expression>)
880
+ );
881
}
882
return hasError
883
? { kind: "UnsupportedNode", node: exprNode, loc: exprLoc }
@@ -1770,7 +1782,7 @@ function lowerAssignment(
1782
let hasError = false;
1783
for (let i = 0; i < elements.length; i++) {
1784
const element = elements[i];
1773
- if (!element.hasNode()) {
1785
+ if (element.node == null) {
1786
continue;
1787
}
1788
if (element.node.type === "RestElement") {
@@ -1802,7 +1814,7 @@ function lowerAssignment(
1814
object: { ...arrayPlace },
1815
property,
1816
};
1805
- lowerAssignment(builder, loc, kind, element, value);
1817
+ lowerAssignment(builder, loc, kind, element as NodePath<t.LVal>, value);
1818
}
1819
return hasError
1820
? { kind: "UnsupportedNode", node: lvalueNode, loc }