@samitouri / QOS-React-2 / commits / 74f5e26e00

[be][test] Remove hir-test

Sathya Gunasekaran committed Apr 11, 2023 at 13:41 UTC 74f5e26e00e0104cd8ddf08896508d497868b08b
1 file changed -145
compiler/forget/src/__tests__/hir-test.ts deleted
-145
@@ -1,145 +0,0 @@
1 -/**
2 - * Copyright (c) Meta Platforms, Inc. and affiliates.
3 - *
4 - * This source code is licensed under the MIT license found in the
5 - * LICENSE file in the root directory of this source tree.
6 - */
7 -
8 -"use strict";
9 -
10 -import { parse } from "@babel/parser";
11 -import traverse, { NodePath } from "@babel/traverse";
12 -import * as t from "@babel/types";
13 -import path from "path";
14 -
15 -import invariant from "invariant";
16 -import * as CompilerPipeline from "../CompilerPipeline";
17 -import { Effect, ValueKind } from "../HIR";
18 -import { EnvironmentConfig } from "../HIR/Environment";
19 -import { printFunction } from "../HIR/PrintHIR";
20 -import { toggleLogging } from "../Utils/logger";
21 -import { Err, Ok, Result } from "../Utils/Result";
22 -import generateTestsFromFixtures from "./test-utils/generateTestsFromFixtures";
23 -
24 -// TODO: make pipeline names an enum
25 -// Currently, this is the last pass that operates on hir
26 -const LAST_HIR_PASS = "LeaveSSA";
27 -
28 -describe("React Forget (HIR version)", () => {
29 - generateTestsFromFixtures(
30 - path.join(__dirname, "fixtures", "hir"),
31 - (input, file, options) => {
32 - if (options.debug) {
33 - toggleLogging(options.debug);
34 - }
35 -
36 - const compileResult = compile(input, options.language, {
37 - customHooks: new Map([
38 - [
39 - "useFreeze",
40 - {
41 - name: "useFreeze",
42 - kind: "Custom",
43 - valueKind: ValueKind.Frozen,
44 - effectKind: Effect.Freeze,
45 - },
46 - ],
47 - ]),
48 - inlineUseMemo: options.environment?.inlineUseMemo ?? false,
49 - });
50 -
51 - if (compileResult.isErr()) {
52 - const error = compileResult.unwrapErr();
53 - error.message = `Expected fixture '${file}' to succeed but it failed with error:\n\n${error.message}`;
54 - throw error;
55 - }
56 - const transformedSources = compileResult.unwrap().map(({ hir }) => {
57 - return `
58 -## HIR
59 -
60 -${wrapWithTripleBackticks(hir, "javascript")}
61 -
62 - `.trim();
63 - });
64 - if (transformedSources === null || transformedSources.length === 0) {
65 - throw new Error(`Expected at least one output for file '${file}'.`);
66 - }
67 - return `
68 -## Input
69 -
70 -${wrapWithTripleBackticks(input, "javascript")}
71 -
72 -${transformedSources.join("\n")}
73 - `;
74 - }
75 - );
76 -});
77 -
78 -type CompileResult = {
79 - hir: string;
80 -};
81 -
82 -function compile(
83 - source: string,
84 - language: "flow" | "typescript",
85 - compilerEnv: Partial<EnvironmentConfig>
86 -): Result<Array<CompileResult>, Error> {
87 - const transformedFns = new Array<CompileResult>();
88 - const babelAsts = parseFunctions(source, language);
89 - if (babelAsts.isErr()) {
90 - return babelAsts;
91 - }
92 -
93 - try {
94 - for (const ast of babelAsts.unwrap()) {
95 - let hirString: string | null = null;
96 - for (const result of CompilerPipeline.run(ast, compilerEnv)) {
97 - switch (result.kind) {
98 - case "hir": {
99 - if (result.name === LAST_HIR_PASS) {
100 - hirString = printFunction(result.value);
101 - break;
102 - }
103 - }
104 - }
105 - }
106 - invariant(
107 - hirString !== null,
108 - `Expected to find pass with name ${LAST_HIR_PASS}`
109 - );
110 - transformedFns.push({ hir: hirString });
111 - }
112 - return Ok(transformedFns);
113 - } catch (e) {
114 - return Err(e);
115 - }
116 -}
117 -
118 -function parseFunctions(
119 - source: string,
120 - language: "flow" | "typescript"
121 -): Result<Array<NodePath<t.FunctionDeclaration>>, Error> {
122 - const items: Array<NodePath<t.FunctionDeclaration>> = [];
123 - try {
124 - const ast = parse(source, {
125 - plugins: [language, "jsx"],
126 - sourceType: "module",
127 - });
128 - traverse(ast, {
129 - // Only collect top-level functions
130 - FunctionDeclaration(nodePath) {
131 - items.push(nodePath);
132 - nodePath.skip();
133 - },
134 - });
135 - } catch (e) {
136 - return Err(e);
137 - }
138 - return Ok(items);
139 -}
140 -
141 -function wrapWithTripleBackticks(s: string, ext?: string) {
142 - return `\`\`\`${ext ?? ""}
143 -${s}
144 -\`\`\``;
145 -}