[prettier] Patch prettier to write modified TS files (#1055)
mofeiZ committed
Jan 23, 2023 at 15:57 UTC
07cda62430b103d9e1580fbb915b6c603ad1d2a2
2 files changed
+27
-23
compiler/forget/scripts/prettier.js
+1
-1
@@ -25,7 +25,7 @@ let didWarn = false;
25
let didError = false;
26
27
const files = glob
28
- .sync("**/*.js", {
28
+ .sync("**/*.{js,ts}", {
29
ignore: [
30
"**/node_modules/**",
31
"packages/demo-2021Q3/**",
compiler/forget/src/HIR/BuildHIR.ts
+26
-22
@@ -7,6 +7,7 @@
7
8
import { NodePath, Scope } from "@babel/traverse";
9
import * as t from "@babel/types";
10
+import invariant from "invariant";
11
import { CompilerError, ErrorSeverity } from "../CompilerError";
12
import { Err, Ok, Result } from "../lib/Result";
13
import { assertExhaustive } from "../Utils/utils";
@@ -24,7 +25,7 @@ import {
25
Place,
26
ReturnTerminal,
27
SourceLocation,
27
- ThrowTerminal,
28
+ ThrowTerminal
29
} from "./HIR";
30
import HIRBuilder, { Environment } from "./HIRBuilder";
31
@@ -51,8 +52,11 @@ export function lower(
52
const env = new Environment();
53
const builder = new HIRBuilder(env);
54
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
58
const id =
55
- func.isFunctionDeclaration() && func.get("id").hasNode()
59
+ func.isFunctionDeclaration() && (func.get("id").node != null)
60
? builder.resolveIdentifier(func.get("id") as NodePath<t.Identifier>)
61
: null;
62
@@ -132,8 +136,8 @@ function lowerStatement(
136
case "ReturnStatement": {
137
const stmt = stmtPath as NodePath<t.ReturnStatement>;
138
const argument = stmt.get("argument");
135
- const value = argument.hasNode()
136
- ? lowerExpressionToPlace(builder, argument)
139
+ const value = (argument.node != null)
140
+ ? lowerExpressionToPlace(builder, argument as NodePath<t.Expression>)
141
: null;
142
const fallthrough = builder.reserve();
143
const terminal: ReturnTerminal = {
@@ -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,7 @@ function lowerStatement(
406
"value",
407
{
408
kind: "if",
405
- test: lowerExpressionToPlace(builder, test),
409
+ test: lowerExpressionToPlace(builder, test as NodePath<t.Expression>),
410
consequent: bodyBlock,
411
alternate: continuationBlock.id,
412
fallthrough: continuationBlock.id,
@@ -570,7 +574,7 @@ function lowerStatement(
574
for (let ii = stmt.get("cases").length - 1; ii >= 0; ii--) {
575
const case_: NodePath<t.SwitchCase> = stmt.get("cases")[ii];
576
const test = case_.get("test");
573
- if (!test.hasNode()) {
577
+ if (!(test.node != null)) {
578
if (hasDefault) {
579
builder.pushError({
580
reason:
@@ -600,7 +604,7 @@ function lowerStatement(
604
});
605
});
606
cases.push({
603
- test: test.hasNode() ? lowerExpressionToPlace(builder, test) : null,
607
+ test: (test.node != null) ? lowerExpressionToPlace(builder, test as NodePath<t.Expression>) : null,
608
block,
609
});
610
fallthrough = block;
@@ -642,13 +646,13 @@ function lowerStatement(
646
*/
647
lowerStatement(builder, stmt.get("block"));
648
const handler = stmt.get("handler");
645
- if (handler.hasNode()) {
649
+ if (handler.node != null) {
650
// TODO: consider whether we need to track the param
647
- lowerStatement(builder, handler.get("body"));
651
+ lowerStatement(builder, handler.get("body") as NodePath<t.Statement>);
652
}
653
const finalizer = stmt.get("finalizer");
650
- if (finalizer.hasNode()) {
651
- lowerStatement(builder, finalizer);
654
+ if (finalizer.node != null) {
655
+ lowerStatement(builder, finalizer as NodePath<t.Statement>);
656
}
657
return;
658
}
@@ -670,8 +674,8 @@ function lowerStatement(
674
const id = declaration.get("id");
675
const init = declaration.get("init");
676
let value: InstructionValue;
673
- if (init.hasNode()) {
674
- value = lowerExpression(builder, init);
677
+ if (init.node != null) {
678
+ value = lowerExpression(builder, init as NodePath<t.Expression>);
679
} else {
680
value = {
681
kind: "Primitive",
@@ -856,7 +860,7 @@ function lowerExpression(
860
let hasError = false;
861
let elements: Place[] = [];
862
for (const element of expr.get("elements")) {
859
- if (!element.hasNode() || !element.isExpression()) {
863
+ if (!(element.node != null) || !element.isExpression()) {
864
builder.pushError({
865
reason: "Handle non-expression array elements",
866
severity: ErrorSeverity.Todo,
@@ -865,7 +869,7 @@ function lowerExpression(
869
hasError = true;
870
continue;
871
}
868
- elements.push(lowerExpressionToPlace(builder, element));
872
+ elements.push(lowerExpressionToPlace(builder, element as NodePath<t.Expression>));
873
}
874
return hasError
875
? { kind: "UnsupportedNode", node: exprNode, loc: exprLoc }
@@ -1770,7 +1774,7 @@ function lowerAssignment(
1774
let hasError = false;
1775
for (let i = 0; i < elements.length; i++) {
1776
const element = elements[i];
1773
- if (!element.hasNode()) {
1777
+ if (!(element.node != null)) {
1778
continue;
1779
}
1780
if (element.node.type === "RestElement") {
@@ -1802,7 +1806,7 @@ function lowerAssignment(
1806
object: { ...arrayPlace },
1807
property,
1808
};
1805
- lowerAssignment(builder, loc, kind, element, value);
1809
+ lowerAssignment(builder, loc, kind, element as NodePath<t.LVal>, value);
1810
}
1811
return hasError
1812
? { kind: "UnsupportedNode", node: lvalueNode, loc }