From 1d6eebfb7ff44b24627ef404112bb151f683efe7 Mon Sep 17 00:00:00 2001 From: mofeiZ <34200447+mofeiZ@users.noreply.github.com> Date: Fri, 17 May 2024 11:58:15 -0700 Subject: [PATCH] [Compiler][script] Dedupe error report counts before reporting in healthcheck (#29085) Certain compiler passes currently may collect a few error events before reporting (see https://github.com/facebook/react/blob/main/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts#L101-L107) --- .../src/checks/reactCompiler.ts | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/compiler/packages/react-compiler-healthcheck/src/checks/reactCompiler.ts b/compiler/packages/react-compiler-healthcheck/src/checks/reactCompiler.ts index b122a89a19ee..1cf668eae1cb 100644 --- a/compiler/packages/react-compiler-healthcheck/src/checks/reactCompiler.ts +++ b/compiler/packages/react-compiler-healthcheck/src/checks/reactCompiler.ts @@ -13,15 +13,18 @@ import BabelPluginReactCompiler, { type CompilerErrorDetailOptions, type PluginOptions, } from "babel-plugin-react-compiler/src"; -import { LoggerEvent } from "babel-plugin-react-compiler/src/Entrypoint"; +import { LoggerEvent as RawLoggerEvent } from "babel-plugin-react-compiler/src/Entrypoint"; import chalk from "chalk"; +type LoggerEvent = RawLoggerEvent & {filename: string | null}; + const SucessfulCompilation: Array = []; const ActionableFailures: Array = []; const OtherFailures: Array = []; const logger = { - logEvent(_: string | null, event: LoggerEvent) { + logEvent(filename: string | null, rawEvent: RawLoggerEvent) { + const event = {...rawEvent, filename}; switch (event.kind) { case "CompileSuccess": { SucessfulCompilation.push(event); @@ -104,6 +107,29 @@ function compile(sourceCode: string, filename: string) { const JsFileExtensionRE = /(js|ts|jsx|tsx)$/; +/** + * Counts unique source locations (filename + function definition location) + * in source. + * The compiler currently occasionally emits multiple error events for a + * single file (e.g. to report multiple rules of react violations in the + * same pass). + * TODO: enable non-destructive `CompilerDiagnostic` logging in dev mode, + * and log a "CompilationStart" event for every function we begin processing. + */ +function countUniqueLocInEvents(events: Array): number { + const seenLocs = new Set(); + let count = 0; + for (const e of events) { + if (e.filename != null && e.fnLoc != null) { + seenLocs.add(`${e.filename}:${e.fnLoc.start}:${e.fnLoc.end}`); + } else { + // failed to dedup due to lack of source locations + count++; + } + } + return count + seenLocs.size; +} + export default { run(source: string, path: string): void { if (JsFileExtensionRE.exec(path) !== null) { @@ -114,8 +140,8 @@ export default { report(): void { const totalComponents = SucessfulCompilation.length + - OtherFailures.length + - ActionableFailures.length; + countUniqueLocInEvents(OtherFailures) + + countUniqueLocInEvents(ActionableFailures) console.log( chalk.green( `Successfully compiled ${SucessfulCompilation.length} out of ${totalComponents} components.`