Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,10 @@ export class CompilerDiagnostic {
}

printErrorMessage(source: string, options: PrintErrorMessageOptions): string {
const buffer = [
printErrorSummary(this.category, this.reason),
'\n\n',
this.description,
];
const buffer = [printErrorSummary(this.category, this.reason)];
if (this.description != null) {
buffer.push('\n\n', `${this.description}.`);
}
for (const detail of this.options.details) {
switch (detail.kind) {
case 'error': {
Expand Down Expand Up @@ -280,6 +279,7 @@ export class CompilerErrorDetail {
*/
export class CompilerError extends Error {
details: Array<CompilerErrorDetail | CompilerDiagnostic> = [];
disabledDetails: Array<CompilerErrorDetail | CompilerDiagnostic> = [];
printedMessage: string | null = null;

static invariant(
Expand Down Expand Up @@ -360,6 +360,7 @@ export class CompilerError extends Error {
super(...args);
this.name = 'ReactCompilerError';
this.details = [];
this.disabledDetails = [];
}

override get message(): string {
Expand Down Expand Up @@ -400,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);
}
}

/**
Expand All @@ -424,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<void, CompilerError> {
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;
}
Expand All @@ -471,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function validateRestrictedImports(
}
},
});
if (error.hasErrors()) {
if (error.hasAnyErrors()) {
return error;
} else {
return null;
Expand Down Expand Up @@ -256,7 +256,7 @@ export function addImportsToProgram(
{
reason:
'Encountered conflicting import specifiers in generated program',
description: `Conflict from import ${loweredImport.module}:(${loweredImport.imported} as ${loweredImport.name}).`,
description: `Conflict from import ${loweredImport.module}:(${loweredImport.imported} as ${loweredImport.name})`,
details: [
{
kind: 'error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function findDirectivesDynamicGating(
}
}
}
if (errors.hasErrors()) {
if (errors.hasAnyErrors()) {
return Err(errors);
} else if (result.length > 1) {
const error = new CompilerError();
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function assertValidEffectImportReference(
reason:
'Cannot infer dependencies of this effect. This will break your build!',
description:
'To resolve, either pass a dependency array or fix reported compiler bailout diagnostics.' +
'To resolve, either pass a dependency array or fix reported compiler bailout diagnostics' +
(maybeErrorDiagnostic ? ` ${maybeErrorDiagnostic}` : ''),
details: [
{
Expand Down Expand Up @@ -128,9 +128,7 @@ function assertValidFireImportReference(
reason: '[Fire] Untransformed reference to compiler-required feature.',
description:
'Either remove this `fire` call or ensure it is successfully transformed by the compiler' +
maybeErrorDiagnostic
? ` ${maybeErrorDiagnostic}`
: '',
(maybeErrorDiagnostic != null ? ` ${maybeErrorDiagnostic}` : ''),
details: [
{
kind: 'error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function lower(
CompilerDiagnostic.create({
category: ErrorCategory.Invariant,
reason: 'Could not find binding',
description: `[BuildHIR] Could not find binding for param \`${param.node.name}\`.`,
description: `[BuildHIR] Could not find binding for param \`${param.node.name}\``,
}).withDetails({
kind: 'error',
loc: param.node.loc ?? null,
Expand Down Expand Up @@ -173,7 +173,7 @@ export function lower(
CompilerDiagnostic.create({
category: ErrorCategory.Todo,
reason: `Handle ${param.node.type} parameters`,
description: `[BuildHIR] Add support for ${param.node.type} parameters.`,
description: `[BuildHIR] Add support for ${param.node.type} parameters`,
}).withDetails({
kind: 'error',
loc: param.node.loc ?? null,
Expand Down Expand Up @@ -204,7 +204,7 @@ export function lower(
CompilerDiagnostic.create({
category: ErrorCategory.Syntax,
reason: `Unexpected function body kind`,
description: `Expected function body to be an expression or a block statement, got \`${body.type}\`.`,
description: `Expected function body to be an expression or a block statement, got \`${body.type}\``,
}).withDetails({
kind: 'error',
loc: body.node.loc ?? null,
Expand All @@ -213,7 +213,7 @@ export function lower(
);
}

if (builder.errors.hasErrors()) {
if (builder.errors.hasAnyErrors()) {
return Err(builder.errors);
}

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ export function dropManualMemoization(
manualMemo.loadInstr.value.kind === 'PropertyLoad'
? 'React.useMemo'
: 'useMemo'
} callback doesn't return a value. useMemo is for computing and caching values, not for arbitrary side effects.`,
} callback doesn't return a value. useMemo is for computing and caching values, not for arbitrary side effects`,
suggestions: null,
}).withDetails({
kind: 'error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ function applySignature(
const diagnostic = CompilerDiagnostic.create({
category: ErrorCategory.Immutability,
reason: 'This value cannot be modified',
description: `${reason}.`,
description: reason,
}).withDetails({
kind: 'error',
loc: effect.value.loc,
Expand Down Expand Up @@ -1094,7 +1094,7 @@ function applyEffect(
const diagnostic = CompilerDiagnostic.create({
category: ErrorCategory.Immutability,
reason: 'Cannot access variable before it is declared',
description: `${variable ?? 'This variable'} is accessed before it is declared, which prevents the earlier access from updating when this value changes over time.`,
description: `${variable ?? 'This variable'} is accessed before it is declared, which prevents the earlier access from updating when this value changes over time`,
});
if (hoistedAccess != null && hoistedAccess.loc != effect.value.loc) {
diagnostic.withDetails({
Expand Down Expand Up @@ -1133,7 +1133,7 @@ function applyEffect(
const diagnostic = CompilerDiagnostic.create({
category: ErrorCategory.Immutability,
reason: 'This value cannot be modified',
description: `${reason}.`,
description: reason,
}).withDetails({
kind: 'error',
loc: effect.value.loc,
Expand Down Expand Up @@ -2269,7 +2269,7 @@ function computeEffectsForLegacySignature(
'This API returns functions which cannot be memoized without leading to stale UI. ' +
'To prevent this, by default React Compiler will skip memoizing this component/hook. ' +
'However, you may see issues if values from this API are passed to other components/hooks that are ' +
'memoized.',
'memoized',
].join(''),
}).withDetails({
kind: 'error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ export function inferMutationAliasingRanges(
}
}

if (errors.hasErrors() && !isFunctionExpression) {
if (errors.hasAnyErrors() && !isFunctionExpression) {
return Err(errors);
}
return Ok(functionEffects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class CheckInstructionsAgainstScopesVisitor extends ReactiveFunctionVisitor<
CompilerError.invariant(false, {
reason:
'Encountered an instruction that should be part of a scope, but where that scope has already completed',
description: `Instruction [${id}] is part of scope @${scope.id}, but that scope has already completed.`,
description: `Instruction [${id}] is part of scope @${scope.id}, but that scope has already completed`,
details: [
{
kind: 'error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ function codegenReactiveFunction(
}
}

if (cx.errors.hasErrors()) {
if (cx.errors.hasAnyErrors()) {
return Err(cx.errors);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ class Context {
}

hasErrors(): boolean {
return this.#errors.hasErrors();
return this.#errors.hasAnyErrors();
}

throwIfErrorsFound(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function validateLocalsNotReassignedAfterRender(fn: HIRFunction): void {
CompilerDiagnostic.create({
category: ErrorCategory.Immutability,
reason: 'Cannot reassign variable after render completes',
description: `Reassigning ${variable} after render has completed can cause inconsistent behavior on subsequent renders. Consider using state instead.`,
description: `Reassigning ${variable} after render has completed can cause inconsistent behavior on subsequent renders. Consider using state instead`,
}).withDetails({
kind: 'error',
loc: reassignment.loc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function validateNoCapitalizedCalls(
CompilerError.throwInvalidReact({
category: ErrorCategory.CapitalizedCalls,
reason,
description: `${calleeName} may be a component.`,
description: `${calleeName} may be a component`,
loc: value.loc,
suggestions: null,
});
Expand All @@ -83,7 +83,7 @@ export function validateNoCapitalizedCalls(
errors.push({
category: ErrorCategory.CapitalizedCalls,
reason,
description: `${propertyName} may be a component.`,
description: `${propertyName} may be a component`,
loc: value.loc,
suggestions: null,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function validateNoDerivedComputationsInEffects(fn: HIRFunction): void {
}
}
}
if (errors.hasErrors()) {
if (errors.hasAnyErrors()) {
throw errors;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function validateNoFreezingKnownMutableFunctions(
CompilerDiagnostic.create({
category: ErrorCategory.Immutability,
reason: 'Cannot modify local variables after render completes',
description: `This argument is a function which may reassign or mutate ${variable} after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead.`,
description: `This argument is a function which may reassign or mutate ${variable} after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead`,
})
.withDetails({
kind: 'error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ function validateNoRefAccessInRenderImpl(
}
}

if (errors.hasErrors()) {
if (errors.hasAnyErrors()) {
return Err(errors);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ function validateInferredDep(
errorDiagnostic
? getCompareDependencyResultDescription(errorDiagnostic)
: 'Inferred dependency not present in source'
}.`
}`
: '',
]
.join('')
Expand Down Expand Up @@ -551,7 +551,7 @@ class Visitor extends ReactiveFunctionVisitor<VisitorState> {
reason: 'Existing memoization could not be preserved',
description: [
'React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. ',
'This dependency may be mutated later, which could cause the value to change unexpectedly.',
'This dependency may be mutated later, which could cause the value to change unexpectedly',
].join(''),
}).withDetails({
kind: 'error',
Expand Down Expand Up @@ -603,7 +603,7 @@ class Visitor extends ReactiveFunctionVisitor<VisitorState> {
category: ErrorCategory.PreserveManualMemo,
reason: 'Existing memoization could not be preserved',
description: [
'React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. This value was memoized in source but not in compilation output. ',
'React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. This value was memoized in source but not in compilation output',
DEBUG
? `${printIdentifier(identifier)} was not memoized.`
: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function validateStaticComponents(
CompilerDiagnostic.create({
category: ErrorCategory.StaticComponents,
reason: 'Cannot create components during render',
description: `Components created during render will reset their state each time they are created. Declare components outside of render. `,
description: `Components created during render will reset their state each time they are created. Declare components outside of render`,
})
.withDetails({
kind: 'error',
Expand Down
Loading
Loading