From ea7fe29bd8c43353ff0d7f0d8db469b5434164bf Mon Sep 17 00:00:00 2001 From: Lauren Tan Date: Sat, 6 Sep 2025 12:59:23 -0400 Subject: [PATCH] [compiler] Filter out disabled errors from being reported This PR stops error details of severity `ErrorSeverity.Off` from being reported. --- .../src/CompilerError.ts | 39 ++++++++++--------- .../src/Entrypoint/Imports.ts | 2 +- .../src/Entrypoint/Program.ts | 4 +- .../src/HIR/BuildHIR.ts | 4 +- .../Inference/InferMutationAliasingRanges.ts | 2 +- .../ReactiveScopes/CodegenReactiveFunction.ts | 2 +- .../src/Transform/TransformFire.ts | 2 +- .../ValidateNoDerivedComputationsInEffects.ts | 2 +- .../Validation/ValidateNoRefAccessInRender.ts | 2 +- 9 files changed, 30 insertions(+), 29 deletions(-) diff --git a/compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts b/compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts index c4076a7fbb..2e7acac254 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts @@ -279,6 +279,7 @@ export class CompilerErrorDetail { */ export class CompilerError extends Error { details: Array = []; + disabledDetails: Array = []; printedMessage: string | null = null; static invariant( @@ -359,6 +360,7 @@ export class CompilerError extends Error { super(...args); this.name = 'ReactCompilerError'; this.details = []; + this.disabledDetails = []; } override get message(): string { @@ -399,10 +401,15 @@ export class CompilerError extends Error { merge(other: CompilerError): void { this.details.push(...other.details); + this.disabledDetails.push(...other.disabledDetails); } pushDiagnostic(diagnostic: CompilerDiagnostic): void { - this.details.push(diagnostic); + if (diagnostic.severity === ErrorSeverity.Off) { + this.disabledDetails.push(diagnostic); + } else { + this.details.push(diagnostic); + } } /** @@ -423,43 +430,40 @@ export class CompilerError extends Error { * @deprecated use {@link pushDiagnostic} instead */ pushErrorDetail(detail: CompilerErrorDetail): CompilerErrorDetail { - this.details.push(detail); + if (detail.severity === ErrorSeverity.Off) { + this.disabledDetails.push(detail); + } else { + this.details.push(detail); + } return detail; } - hasErrors(): boolean { + hasAnyErrors(): boolean { return this.details.length > 0; } asResult(): Result { - return this.hasErrors() ? Err(this) : Ok(undefined); + return this.hasAnyErrors() ? Err(this) : Ok(undefined); } /** * Returns true if any of the error details are of severity Error. */ - isError(): boolean { - let res = false; + hasErrors(): boolean { for (const detail of this.details) { - if (detail.severity === ErrorSeverity.Off) { - return false; - } if (detail.severity === ErrorSeverity.Error) { - res = true; + return true; } } - return res; + return false; } /** * Returns true if there are no Errors and there is at least one Warning. */ - isWarning(): boolean { + hasWarning(): boolean { let res = false; for (const detail of this.details) { - if (detail.severity === ErrorSeverity.Off) { - return false; - } if (detail.severity === ErrorSeverity.Error) { return false; } @@ -470,12 +474,9 @@ export class CompilerError extends Error { return res; } - isHint(): boolean { + hasHints(): boolean { let res = false; for (const detail of this.details) { - if (detail.severity === ErrorSeverity.Off) { - return false; - } if (detail.severity === ErrorSeverity.Error) { return false; } diff --git a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts index 5d7ffc5976..9b0db75912 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts @@ -46,7 +46,7 @@ export function validateRestrictedImports( } }, }); - if (error.hasErrors()) { + if (error.hasAnyErrors()) { return error; } else { return null; diff --git a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts index dd4cfd819a..0d7f2f4a4c 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts @@ -111,7 +111,7 @@ function findDirectivesDynamicGating( } } } - if (errors.hasErrors()) { + if (errors.hasAnyErrors()) { return Err(errors); } else if (result.length > 1) { const error = new CompilerError(); @@ -139,7 +139,7 @@ function findDirectivesDynamicGating( } function isError(err: unknown): boolean { - return !(err instanceof CompilerError) || err.isError(); + return !(err instanceof CompilerError) || err.hasErrors(); } function isConfigError(err: unknown): boolean { diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts index 26d241c67b..02607f27b4 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts @@ -213,7 +213,7 @@ export function lower( ); } - if (builder.errors.hasErrors()) { + if (builder.errors.hasAnyErrors()) { return Err(builder.errors); } @@ -2667,7 +2667,7 @@ function lowerExpression( * lowerIdentifierForAssignment should have already reported an error if it returned null, * we check here just in case */ - if (!builder.errors.hasErrors()) { + if (!builder.errors.hasAnyErrors()) { builder.errors.push({ reason: `(BuildHIR::lowerExpression) Found an invalid UpdateExpression without a previously reported error`, category: ErrorCategory.Invariant, diff --git a/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingRanges.ts b/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingRanges.ts index 29f68a77ae..32f84e1e28 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingRanges.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingRanges.ts @@ -568,7 +568,7 @@ export function inferMutationAliasingRanges( } } - if (errors.hasErrors() && !isFunctionExpression) { + if (errors.hasAnyErrors() && !isFunctionExpression) { return Err(errors); } return Ok(functionEffects); diff --git a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts index 894f6ce209..fa6392b276 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -372,7 +372,7 @@ function codegenReactiveFunction( } } - if (cx.errors.hasErrors()) { + if (cx.errors.hasAnyErrors()) { return Err(cx.errors); } diff --git a/compiler/packages/babel-plugin-react-compiler/src/Transform/TransformFire.ts b/compiler/packages/babel-plugin-react-compiler/src/Transform/TransformFire.ts index c7c3a21cf9..6793df5710 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Transform/TransformFire.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Transform/TransformFire.ts @@ -698,7 +698,7 @@ class Context { } hasErrors(): boolean { - return this.#errors.hasErrors(); + return this.#errors.hasAnyErrors(); } throwIfErrorsFound(): void { diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects.ts index 0e30794f0b..8ec7542a9d 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects.ts @@ -104,7 +104,7 @@ export function validateNoDerivedComputationsInEffects(fn: HIRFunction): void { } } } - if (errors.hasErrors()) { + if (errors.hasAnyErrors()) { throw errors; } } diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoRefAccessInRender.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoRefAccessInRender.ts index de415881d6..eb053ac419 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoRefAccessInRender.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoRefAccessInRender.ts @@ -735,7 +735,7 @@ function validateNoRefAccessInRenderImpl( } } - if (errors.hasErrors()) { + if (errors.hasAnyErrors()) { return Err(errors); } }