From 0e2a52a42284db1f45bb45303296e7089f266482 Mon Sep 17 00:00:00 2001 From: Lauren Tan Date: Thu, 6 Mar 2025 12:45:38 -0500 Subject: [PATCH] [compiler] Make CompilerError compatible with reflection In #32416, @michaelfaith discovered that https://github.com/facebook/react/blob/029e8bd618af23fbdd9efdac565ad81f7d4640d8/scripts/jest/setupTests.js#L82 would fail on the merged compiler eslint rule. Upon further investigation I discovered that our CompilerError implementation doesn't play nicely with Reflect, so `Reflect.construct` would fail as `.toString` would error: ``` TypeError: Cannot read properties of undefined (reading 'map') at _CompilerError.toString (/Users/.../code/react/node_modules/babel-plugin-react-compiler/src/CompilerError.ts:200:25) at _CompilerError.get message [as message] (/Users/.../code/react/node_modules/babel-plugin-react-compiler/src/CompilerError.ts:194:17) at Object.construct (/Users/.../code/react/scripts/jest/setupTests.js:153:52) ``` I tested this fix using my local copy of the built babel-plugin-react-compiler to verify that this fixes those tests. --- .../babel-plugin-react-compiler/src/CompilerError.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts b/compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts index 5ea6f986281..3a3010dd2f6 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts @@ -188,6 +188,7 @@ export class CompilerError extends Error { constructor(...args: Array) { super(...args); this.name = 'ReactCompilerError'; + this.details = []; } override get message(): string { @@ -197,7 +198,10 @@ export class CompilerError extends Error { override set message(_message: string) {} override toString(): string { - return this.details.map(detail => detail.toString()).join('\n\n'); + if (Array.isArray(this.details)) { + return this.details.map(detail => detail.toString()).join('\n\n'); + } + return this.name; } push(options: CompilerErrorDetailOptions): CompilerErrorDetail {