From 74f5e26e00e0104cd8ddf08896508d497868b08b Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Tue, 11 Apr 2023 13:41:33 +0100 Subject: [PATCH] [be][test] Remove hir-test --- compiler/forget/src/__tests__/hir-test.ts | 145 ---------------------- 1 file changed, 145 deletions(-) delete mode 100644 compiler/forget/src/__tests__/hir-test.ts diff --git a/compiler/forget/src/__tests__/hir-test.ts b/compiler/forget/src/__tests__/hir-test.ts deleted file mode 100644 index 7a391fd604..0000000000 --- a/compiler/forget/src/__tests__/hir-test.ts +++ /dev/null @@ -1,145 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -"use strict"; - -import { parse } from "@babel/parser"; -import traverse, { NodePath } from "@babel/traverse"; -import * as t from "@babel/types"; -import path from "path"; - -import invariant from "invariant"; -import * as CompilerPipeline from "../CompilerPipeline"; -import { Effect, ValueKind } from "../HIR"; -import { EnvironmentConfig } from "../HIR/Environment"; -import { printFunction } from "../HIR/PrintHIR"; -import { toggleLogging } from "../Utils/logger"; -import { Err, Ok, Result } from "../Utils/Result"; -import generateTestsFromFixtures from "./test-utils/generateTestsFromFixtures"; - -// TODO: make pipeline names an enum -// Currently, this is the last pass that operates on hir -const LAST_HIR_PASS = "LeaveSSA"; - -describe("React Forget (HIR version)", () => { - generateTestsFromFixtures( - path.join(__dirname, "fixtures", "hir"), - (input, file, options) => { - if (options.debug) { - toggleLogging(options.debug); - } - - const compileResult = compile(input, options.language, { - customHooks: new Map([ - [ - "useFreeze", - { - name: "useFreeze", - kind: "Custom", - valueKind: ValueKind.Frozen, - effectKind: Effect.Freeze, - }, - ], - ]), - inlineUseMemo: options.environment?.inlineUseMemo ?? false, - }); - - if (compileResult.isErr()) { - const error = compileResult.unwrapErr(); - error.message = `Expected fixture '${file}' to succeed but it failed with error:\n\n${error.message}`; - throw error; - } - const transformedSources = compileResult.unwrap().map(({ hir }) => { - return ` -## HIR - -${wrapWithTripleBackticks(hir, "javascript")} - - `.trim(); - }); - if (transformedSources === null || transformedSources.length === 0) { - throw new Error(`Expected at least one output for file '${file}'.`); - } - return ` -## Input - -${wrapWithTripleBackticks(input, "javascript")} - -${transformedSources.join("\n")} - `; - } - ); -}); - -type CompileResult = { - hir: string; -}; - -function compile( - source: string, - language: "flow" | "typescript", - compilerEnv: Partial -): Result, Error> { - const transformedFns = new Array(); - const babelAsts = parseFunctions(source, language); - if (babelAsts.isErr()) { - return babelAsts; - } - - try { - for (const ast of babelAsts.unwrap()) { - let hirString: string | null = null; - for (const result of CompilerPipeline.run(ast, compilerEnv)) { - switch (result.kind) { - case "hir": { - if (result.name === LAST_HIR_PASS) { - hirString = printFunction(result.value); - break; - } - } - } - } - invariant( - hirString !== null, - `Expected to find pass with name ${LAST_HIR_PASS}` - ); - transformedFns.push({ hir: hirString }); - } - return Ok(transformedFns); - } catch (e) { - return Err(e); - } -} - -function parseFunctions( - source: string, - language: "flow" | "typescript" -): Result>, Error> { - const items: Array> = []; - try { - const ast = parse(source, { - plugins: [language, "jsx"], - sourceType: "module", - }); - traverse(ast, { - // Only collect top-level functions - FunctionDeclaration(nodePath) { - items.push(nodePath); - nodePath.skip(); - }, - }); - } catch (e) { - return Err(e); - } - return Ok(items); -} - -function wrapWithTripleBackticks(s: string, ext?: string) { - return `\`\`\`${ext ?? ""} -${s} -\`\`\``; -}