@samitouri / QOS-React / commits / 00e9888fb8

[ez][patch] Remove calls to NodePath.hasNode

--- Recent commits added calls to `NodePath.hasNode`, which does not exist in babel v7.1.6 (internal). Forget is failing with this error. ```js TypeError: handlerPath.hasNode is not a function at lowerStatement (.../babel-plugin-react-forget/HIR/BuildHIR.js:924:58) ... ```

Mofei Zhang committed Sep 15, 2023 at 13:55 UTC 00e9888fb80719150363b3d011c53b890a4647af
2 files changed +30 -26
compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts
+19 -26
@@ -15,7 +15,7 @@ import {
15 ErrorSeverity,
16 } from "../CompilerError";
17 import { Err, Ok, Result } from "../Utils/Result";
18 -import { assertExhaustive } from "../Utils/utils";
18 +import { assertExhaustive, hasNode } from "../Utils/utils";
19 import { Environment } from "./Environment";
20 import {
21 ArrayExpression,
@@ -78,14 +78,14 @@ export function lower(
78 });
79 }
80
81 - // Internal babel is on an older version that does not have hasNode (v7.17)
82 - // See https://github.com/babel/babel/pull/13940/files for impl
83 - // TODO: write helper function for NodePath.node != null
81 let id: string | null = null;
85 - if (func.isFunctionDeclaration() && func.get("id").node != null) {
86 - id = (func.get("id") as NodePath<t.Identifier>).node.name;
87 - } else if (func.isFunctionExpression() && func.get("id").node != null) {
88 - id = (func.get("id") as NodePath<t.Identifier>).node.name;
82 + if (func.isFunctionDeclaration() || func.isFunctionExpression()) {
83 + const idNode = (
84 + func as NodePath<t.FunctionDeclaration | t.FunctionExpression>
85 + ).get("id");
86 + if (hasNode(idNode)) {
87 + id = idNode.node.name;
88 + }
89 }
90 const params: Array<Place> = [];
91 func.get("params").forEach((param) => {
@@ -266,9 +266,9 @@ function lowerStatement(
266 // Block for the alternate (if the test is not truthy)
267 let alternateBlock: BlockId;
268 const alternate = stmt.get("alternate");
269 - if (alternate.node != null) {
269 + if (hasNode(alternate)) {
270 alternateBlock = builder.enter("block", (_blockId) => {
271 - lowerStatement(builder, alternate as NodePath<t.Statement>);
271 + lowerStatement(builder, alternate);
272 return {
273 kind: "goto",
274 block: continuationBlock.id,
@@ -364,9 +364,9 @@ function lowerStatement(
364
365 let updateBlock: BlockId | null = null;
366 const update = stmt.get("update");
367 - if (update.node != null) {
367 + if (hasNode(update)) {
368 updateBlock = builder.enter("loop", (_blockId) => {
369 - lowerExpressionToTemporary(builder, update as NodePath<t.Expression>);
369 + lowerExpressionToTemporary(builder, update);
370 return {
371 kind: "goto",
372 block: testBlock.id,
@@ -590,11 +590,8 @@ function lowerStatement(
590 });
591 });
592 let test: Place | null = null;
593 - if (testExpr.node != null) {
594 - test = lowerReorderableExpression(
595 - builder,
596 - testExpr as NodePath<t.Expression>
597 - );
593 + if (hasNode(testExpr)) {
594 + test = lowerReorderableExpression(builder, testExpr);
595 }
596 cases.push({
597 test,
@@ -649,11 +646,8 @@ function lowerStatement(
646 for (const declaration of stmt.get("declarations")) {
647 const id = declaration.get("id");
648 const init = declaration.get("init");
652 - if (init.node != null) {
653 - const value = lowerExpressionToTemporary(
654 - builder,
655 - init as NodePath<t.Expression>
656 - );
649 + if (hasNode(init)) {
650 + const value = lowerExpressionToTemporary(builder, init);
651 lowerAssignment(
652 builder,
653 stmt.node.loc ?? GeneratedSource,
@@ -1015,8 +1009,7 @@ function lowerStatement(
1009 const continuationBlock = builder.reserve("block");
1010
1011 const handlerPath = stmt.get("handler");
1018 - // NOTE: null check is for older babel versions, the hasNode() check is for TS to refine the type
1019 - if (handlerPath.node == null || !handlerPath.hasNode()) {
1012 + if (!hasNode(handlerPath)) {
1013 builder.errors.push({
1014 reason: `(BuildHIR::lowerStatement) Handle TryStatement without a catch clause`,
1015 severity: ErrorSeverity.Todo,
@@ -1025,7 +1018,7 @@ function lowerStatement(
1018 });
1019 return;
1020 }
1028 - if (stmt.get("finalizer").node != null) {
1021 + if (hasNode(stmt.get("finalizer"))) {
1022 builder.errors.push({
1023 reason: `(BuildHIR::lowerStatement) Handle TryStatement with a finalizer ('finally') clause`,
1024 severity: ErrorSeverity.Todo,
@@ -1039,7 +1032,7 @@ function lowerStatement(
1032 place: Place;
1033 path: NodePath<t.Identifier | t.ArrayPattern | t.ObjectPattern>;
1034 } | null = null;
1042 - if (handlerBindingPath.node != null && handlerBindingPath.hasNode()) {
1035 + if (hasNode(handlerBindingPath)) {
1036 const place: Place = {
1037 kind: "Identifier",
1038 identifier: builder.makeTemporary(),
compiler/packages/babel-plugin-react-forget/src/Utils/utils.ts
+11
@@ -5,6 +5,8 @@
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 +import { NodePath } from "@babel/traverse";
9 +
10 /**
11 * Trigger an exhaustivess check in TypeScript and throw at runtime.
12 *
@@ -67,3 +69,12 @@ export function Set_union<T>(a: Set<T>, b: Set<T>): Set<T> {
69 }
70 return union;
71 }
72 +
73 +export function hasNode<T>(
74 + input: NodePath<T | null | undefined>
75 +): input is NodePath<NonNullable<T>> {
76 + // Internal babel is on an older version that does not have hasNode (v7.17)
77 + // See https://github.com/babel/babel/pull/13940/files for impl
78 + // https://github.com/babel/babel/blob/5ebab544af2f1c6fc6abdaae6f4e5426975c9a16/packages/babel-traverse/src/path/index.ts#L128-L130
79 + return input.node != null;
80 +}