From dff05237c582d379c730bf1132c0fc8e7e0b627a Mon Sep 17 00:00:00 2001 From: Sathya Gunsasekaran Date: Tue, 23 Apr 2024 12:28:42 +0100 Subject: [PATCH] [healthcheck] Compile sources Run the compiler on the globbed soruces. The logger is used to capture the success and failure compilation cases at the component level. (If we were to compile the entire file directly, we wouldn't get this granularity) For now, we just log the number of success and failures. In the future, we can provide a better report building on this. ghstack-source-id: 6d2d918190b6ed5d42b795491bbce29a950b9741 Pull Request resolved: https://github.com/facebook/react-forget/pull/2888 --- .../babel-plugin-react-forget/src/index.ts | 1 + compiler/packages/health-check/src/index.ts | 51 ++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/compiler/packages/babel-plugin-react-forget/src/index.ts b/compiler/packages/babel-plugin-react-forget/src/index.ts index 4882d6ffdd..b314f6925c 100644 --- a/compiler/packages/babel-plugin-react-forget/src/index.ts +++ b/compiler/packages/babel-plugin-react-forget/src/index.ts @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +export { runReactForgetBabelPlugin } from "./Babel/RunReactForgetBabelPlugin"; export { CompilerError, CompilerErrorDetail, diff --git a/compiler/packages/health-check/src/index.ts b/compiler/packages/health-check/src/index.ts index a23e2c0502..65b22af1bd 100644 --- a/compiler/packages/health-check/src/index.ts +++ b/compiler/packages/health-check/src/index.ts @@ -5,9 +5,54 @@ * LICENSE file in the root directory of this source tree. */ +import { + runReactForgetBabelPlugin, + type PluginOptions, +} from "babel-plugin-react-forget/src"; +import { LoggerEvent } from "babel-plugin-react-forget/src/Entrypoint"; import { glob } from "fast-glob"; +import * as fs from "fs/promises"; import yargs from "yargs/yargs"; +const SUCCESS: Array = []; +const FAILURES: Array = []; + +const logger = { + logEvent(_: string | null, event: LoggerEvent) { + switch (event.kind) { + case "CompileSuccess": { + SUCCESS.push(event); + return; + } + case "CompileError": { + FAILURES.push(event); + return; + } + case "CompileDiagnostic": + case "PipelineError": + // TODO(gsn): Silenty fail? + } + }, +}; + +const COMPILER_OPTIONS: Partial = { + noEmit: true, + compilationMode: "infer", + panicThreshold: "critical_errors", + logger, +}; + +function compile(sourceCode: string, filename: string) { + try { + runReactForgetBabelPlugin( + sourceCode, + filename, + "typescript", + COMPILER_OPTIONS + ); + } catch {} +} + async function main() { const argv = yargs(process.argv.slice(2)) .scriptName("healthcheck") @@ -41,8 +86,12 @@ async function main() { }; for (const path of await glob(src, globOptions)) { - console.log(path); + const source = await fs.readFile(path, "utf-8"); + compile(source, path); } + + console.log(`Successful compilation: ${SUCCESS.length}`); + console.log(`Failed compilation: ${FAILURES.length}`); } main();