[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)
mofeiZ committed
May 17, 2024 at 11:58 UTC
1d6eebfb7ff44b24627ef404112bb151f683efe7
1 file changed
+30
-4
compiler/packages/react-compiler-healthcheck/src/checks/reactCompiler.ts
+30
-4
@@ -13,15 +13,18 @@ import BabelPluginReactCompiler, {
13
type CompilerErrorDetailOptions,
14
type PluginOptions,
15
} from "babel-plugin-react-compiler/src";
16
-import { LoggerEvent } from "babel-plugin-react-compiler/src/Entrypoint";
16
+import { LoggerEvent as RawLoggerEvent } from "babel-plugin-react-compiler/src/Entrypoint";
17
import chalk from "chalk";
18
19
+type LoggerEvent = RawLoggerEvent & {filename: string | null};
20
+
21
const SucessfulCompilation: Array<LoggerEvent> = [];
22
const ActionableFailures: Array<LoggerEvent> = [];
23
const OtherFailures: Array<LoggerEvent> = [];
24
25
const logger = {
24
- logEvent(_: string | null, event: LoggerEvent) {
26
+ logEvent(filename: string | null, rawEvent: RawLoggerEvent) {
27
+ const event = {...rawEvent, filename};
28
switch (event.kind) {
29
case "CompileSuccess": {
30
SucessfulCompilation.push(event);
@@ -104,6 +107,29 @@ function compile(sourceCode: string, filename: string) {
107
108
const JsFileExtensionRE = /(js|ts|jsx|tsx)$/;
109
110
+/**
111
+ * Counts unique source locations (filename + function definition location)
112
+ * in source.
113
+ * The compiler currently occasionally emits multiple error events for a
114
+ * single file (e.g. to report multiple rules of react violations in the
115
+ * same pass).
116
+ * TODO: enable non-destructive `CompilerDiagnostic` logging in dev mode,
117
+ * and log a "CompilationStart" event for every function we begin processing.
118
+ */
119
+function countUniqueLocInEvents(events: Array<LoggerEvent>): number {
120
+ const seenLocs = new Set<string>();
121
+ let count = 0;
122
+ for (const e of events) {
123
+ if (e.filename != null && e.fnLoc != null) {
124
+ seenLocs.add(`${e.filename}:${e.fnLoc.start}:${e.fnLoc.end}`);
125
+ } else {
126
+ // failed to dedup due to lack of source locations
127
+ count++;
128
+ }
129
+ }
130
+ return count + seenLocs.size;
131
+}
132
+
133
export default {
134
run(source: string, path: string): void {
135
if (JsFileExtensionRE.exec(path) !== null) {
@@ -114,8 +140,8 @@ export default {
140
report(): void {
141
const totalComponents =
142
SucessfulCompilation.length +
117
- OtherFailures.length +
118
- ActionableFailures.length;
143
+ countUniqueLocInEvents(OtherFailures) +
144
+ countUniqueLocInEvents(ActionableFailures)
145
console.log(
146
chalk.green(
147
`Successfully compiled ${SucessfulCompilation.length} out of ${totalComponents} components.`