Skip to content

Commit 1d6eebf

Browse files
authored
[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)
1 parent 6400172 commit 1d6eebf

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

compiler/packages/react-compiler-healthcheck/src/checks/reactCompiler.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ import BabelPluginReactCompiler, {
1313
type CompilerErrorDetailOptions,
1414
type PluginOptions,
1515
} 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";
1717
import chalk from "chalk";
1818

19+
type LoggerEvent = RawLoggerEvent & {filename: string | null};
20+
1921
const SucessfulCompilation: Array<LoggerEvent> = [];
2022
const ActionableFailures: Array<LoggerEvent> = [];
2123
const OtherFailures: Array<LoggerEvent> = [];
2224

2325
const logger = {
24-
logEvent(_: string | null, event: LoggerEvent) {
26+
logEvent(filename: string | null, rawEvent: RawLoggerEvent) {
27+
const event = {...rawEvent, filename};
2528
switch (event.kind) {
2629
case "CompileSuccess": {
2730
SucessfulCompilation.push(event);
@@ -104,6 +107,29 @@ function compile(sourceCode: string, filename: string) {
104107

105108
const JsFileExtensionRE = /(js|ts|jsx|tsx)$/;
106109

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+
107133
export default {
108134
run(source: string, path: string): void {
109135
if (JsFileExtensionRE.exec(path) !== null) {
@@ -114,8 +140,8 @@ export default {
114140
report(): void {
115141
const totalComponents =
116142
SucessfulCompilation.length +
117-
OtherFailures.length +
118-
ActionableFailures.length;
143+
countUniqueLocInEvents(OtherFailures) +
144+
countUniqueLocInEvents(ActionableFailures)
119145
console.log(
120146
chalk.green(
121147
`Successfully compiled ${SucessfulCompilation.length} out of ${totalComponents} components.`

0 commit comments

Comments
 (0)