Skip to content

Commit d9e38e6

Browse files
committed
[compiler] Migrate CompilerError.invariant to new CompilerDiagnostic infra
Mechanical PR to migrate existing invariants to use the new CompilerDiagnostic infra @josephsavona added. Will tackle the others at a later time.
1 parent 064609a commit d9e38e6

File tree

84 files changed

+1974
-338
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1974
-338
lines changed

compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export enum ErrorSeverity {
3737
export type CompilerDiagnosticOptions = {
3838
category: ErrorCategory;
3939
reason: string;
40-
description: string;
40+
description: string | null;
4141
details: Array<CompilerDiagnosticDetail>;
4242
suggestions?: Array<CompilerSuggestion> | null | undefined;
4343
};
@@ -49,7 +49,7 @@ export type CompilerDiagnosticDetail =
4949
| {
5050
kind: 'error';
5151
loc: SourceLocation | null;
52-
message: string;
52+
message: string | null;
5353
}
5454
| {
5555
kind: 'hint';
@@ -126,8 +126,8 @@ export class CompilerDiagnostic {
126126
return this.options.category;
127127
}
128128

129-
withDetail(detail: CompilerDiagnosticDetail): CompilerDiagnostic {
130-
this.options.details.push(detail);
129+
withDetails(...details: Array<CompilerDiagnosticDetail>): CompilerDiagnostic {
130+
this.options.details.push(...details);
131131
return this;
132132
}
133133

@@ -155,9 +155,9 @@ export class CompilerDiagnostic {
155155
}
156156
let codeFrame: string;
157157
try {
158-
codeFrame = printCodeFrame(source, loc, detail.message);
158+
codeFrame = printCodeFrame(source, loc, detail.message ?? '');
159159
} catch (e) {
160-
codeFrame = detail.message;
160+
codeFrame = detail.message ?? '';
161161
}
162162
buffer.push('\n\n');
163163
if (loc.filename != null) {
@@ -284,15 +284,16 @@ export class CompilerError extends Error {
284284

285285
static invariant(
286286
condition: unknown,
287-
options: Omit<CompilerErrorDetailOptions, 'category'>,
287+
options: Omit<CompilerDiagnosticOptions, 'category'>,
288288
): asserts condition {
289289
if (!condition) {
290290
const errors = new CompilerError();
291-
errors.pushErrorDetail(
292-
new CompilerErrorDetail({
293-
...options,
291+
errors.pushDiagnostic(
292+
CompilerDiagnostic.create({
293+
reason: options.reason,
294+
description: options.description,
294295
category: ErrorCategory.Invariant,
295-
}),
296+
}).withDetails(...options.details),
296297
);
297298
throw errors;
298299
}

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Gating.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,26 @@ function insertAdditionalFunctionDeclaration(
5151
CompilerError.invariant(originalFnName != null && compiled.id != null, {
5252
reason:
5353
'Expected function declarations that are referenced elsewhere to have a named identifier',
54-
loc: fnPath.node.loc ?? null,
54+
description: null,
55+
details: [
56+
{
57+
kind: 'error',
58+
loc: fnPath.node.loc ?? null,
59+
message: null,
60+
},
61+
],
5562
});
5663
CompilerError.invariant(originalFnParams.length === compiledParams.length, {
5764
reason:
5865
'Expected React Compiler optimized function declarations to have the same number of parameters as source',
59-
loc: fnPath.node.loc ?? null,
66+
description: null,
67+
details: [
68+
{
69+
kind: 'error',
70+
loc: fnPath.node.loc ?? null,
71+
message: null,
72+
},
73+
],
6074
});
6175

6276
const gatingCondition = t.identifier(
@@ -140,7 +154,13 @@ export function insertGatedFunctionDeclaration(
140154
CompilerError.invariant(compiled.type === 'FunctionDeclaration', {
141155
reason: 'Expected compiled node type to match input type',
142156
description: `Got ${compiled.type} but expected FunctionDeclaration`,
143-
loc: fnPath.node.loc ?? null,
157+
details: [
158+
{
159+
kind: 'error',
160+
loc: fnPath.node.loc ?? null,
161+
message: null,
162+
},
163+
],
144164
});
145165
insertAdditionalFunctionDeclaration(
146166
fnPath,

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,13 @@ export function addImportsToProgram(
257257
reason:
258258
'Encountered conflicting import specifiers in generated program',
259259
description: `Conflict from import ${loweredImport.module}:(${loweredImport.imported} as ${loweredImport.name}).`,
260-
loc: GeneratedSource,
260+
details: [
261+
{
262+
kind: 'error',
263+
loc: GeneratedSource,
264+
message: null,
265+
},
266+
],
261267
suggestions: null,
262268
},
263269
);
@@ -268,7 +274,13 @@ export function addImportsToProgram(
268274
reason:
269275
'Found inconsistent import specifier. This is an internal bug.',
270276
description: `Expected import ${moduleName}:${specifierName} but found ${loweredImport.module}:${loweredImport.imported}`,
271-
loc: GeneratedSource,
277+
details: [
278+
{
279+
kind: 'error',
280+
loc: GeneratedSource,
281+
message: null,
282+
},
283+
],
272284
},
273285
);
274286
}

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,13 @@ function insertNewOutlinedFunctionNode(
310310
CompilerError.invariant(insertedFuncDecl.isFunctionDeclaration(), {
311311
reason: 'Expected inserted function declaration',
312312
description: `Got: ${insertedFuncDecl}`,
313-
loc: insertedFuncDecl.node?.loc ?? null,
313+
details: [
314+
{
315+
kind: 'error',
316+
loc: insertedFuncDecl.node?.loc ?? null,
317+
message: null,
318+
},
319+
],
314320
});
315321
return insertedFuncDecl;
316322
}
@@ -419,7 +425,14 @@ export function compileProgram(
419425
for (const outlined of compiled.outlined) {
420426
CompilerError.invariant(outlined.fn.outlined.length === 0, {
421427
reason: 'Unexpected nested outlined functions',
422-
loc: outlined.fn.loc,
428+
description: null,
429+
details: [
430+
{
431+
kind: 'error',
432+
loc: outlined.fn.loc,
433+
message: null,
434+
},
435+
],
423436
});
424437
const fn = insertNewOutlinedFunctionNode(
425438
program,
@@ -1407,7 +1420,13 @@ export function getReactCompilerRuntimeModule(
14071420
{
14081421
reason: 'Expected target to already be validated',
14091422
description: null,
1410-
loc: null,
1423+
details: [
1424+
{
1425+
kind: 'error',
1426+
loc: null,
1427+
message: null,
1428+
},
1429+
],
14111430
suggestions: null,
14121431
},
14131432
);

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Suppression.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,14 @@ export function suppressionsToCompilerError(
152152
): CompilerError {
153153
CompilerError.invariant(suppressionRanges.length !== 0, {
154154
reason: `Expected at least suppression comment source range`,
155-
loc: GeneratedSource,
155+
description: null,
156+
details: [
157+
{
158+
kind: 'error',
159+
loc: GeneratedSource,
160+
message: null,
161+
},
162+
],
156163
});
157164
const error = new CompilerError();
158165
for (const suppressionRange of suppressionRanges) {
@@ -196,7 +203,7 @@ export function suppressionsToCompilerError(
196203
op: CompilerSuggestionOperation.Remove,
197204
},
198205
],
199-
}).withDetail({
206+
}).withDetails({
200207
kind: 'error',
201208
loc: suppressionRange.disableComment.loc ?? null,
202209
message: 'Found React rule suppression',

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/ValidateNoUntransformedReferences.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,14 @@ function validateImportSpecifier(
217217
const binding = local.scope.getBinding(local.node.name);
218218
CompilerError.invariant(binding != null, {
219219
reason: 'Expected binding to be found for import specifier',
220-
loc: local.node.loc ?? null,
220+
description: null,
221+
details: [
222+
{
223+
kind: 'error',
224+
loc: local.node.loc ?? null,
225+
message: null,
226+
},
227+
],
221228
});
222229
checkFn(binding.referencePaths, state);
223230
}
@@ -237,7 +244,14 @@ function validateNamespacedImport(
237244

238245
CompilerError.invariant(binding != null, {
239246
reason: 'Expected binding to be found for import specifier',
240-
loc: local.node.loc ?? null,
247+
description: null,
248+
details: [
249+
{
250+
kind: 'error',
251+
loc: local.node.loc ?? null,
252+
message: null,
253+
},
254+
],
241255
});
242256
const filteredReferences = new Map<
243257
CheckInvalidReferenceFn,

compiler/packages/babel-plugin-react-compiler/src/Flood/TypeErrors.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ export function raiseUnificationErrors(
4646
if (errs.length === 0) {
4747
CompilerError.invariant(false, {
4848
reason: 'Should not have array of zero errors',
49-
loc,
49+
description: null,
50+
details: [
51+
{
52+
kind: 'error',
53+
loc,
54+
message: null,
55+
},
56+
],
5057
});
5158
} else if (errs.length === 1) {
5259
CompilerError.throwInvalidJS({

compiler/packages/babel-plugin-react-compiler/src/Flood/Types.ts

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,13 @@ export function makeLinearId(id: number): LinearId {
152152
CompilerError.invariant(id >= 0 && Number.isInteger(id), {
153153
reason: 'Expected LinearId id to be a non-negative integer',
154154
description: null,
155-
loc: null,
155+
details: [
156+
{
157+
kind: 'error',
158+
loc: null,
159+
message: null,
160+
},
161+
],
156162
suggestions: null,
157163
});
158164
return id as LinearId;
@@ -167,7 +173,13 @@ export function makeTypeParameterId(id: number): TypeParameterId {
167173
CompilerError.invariant(id >= 0 && Number.isInteger(id), {
168174
reason: 'Expected TypeParameterId to be a non-negative integer',
169175
description: null,
170-
loc: null,
176+
details: [
177+
{
178+
kind: 'error',
179+
loc: null,
180+
message: null,
181+
},
182+
],
171183
suggestions: null,
172184
});
173185
return id as TypeParameterId;
@@ -191,7 +203,13 @@ export function makeVariableId(id: number): VariableId {
191203
CompilerError.invariant(id >= 0 && Number.isInteger(id), {
192204
reason: 'Expected VariableId id to be a non-negative integer',
193205
description: null,
194-
loc: null,
206+
details: [
207+
{
208+
kind: 'error',
209+
loc: null,
210+
message: null,
211+
},
212+
],
195213
suggestions: null,
196214
});
197215
return id as VariableId;
@@ -399,7 +417,14 @@ function convertFlowType(flowType: FlowType, loc: string): ResolvedType {
399417
} else {
400418
CompilerError.invariant(false, {
401419
reason: `Unsupported property kind ${prop.kind}`,
402-
loc: GeneratedSource,
420+
description: null,
421+
details: [
422+
{
423+
kind: 'error',
424+
loc: GeneratedSource,
425+
message: null,
426+
},
427+
],
403428
});
404429
}
405430
}
@@ -468,7 +493,14 @@ function convertFlowType(flowType: FlowType, loc: string): ResolvedType {
468493
} else {
469494
CompilerError.invariant(false, {
470495
reason: `Unsupported property kind ${prop.kind}`,
471-
loc: GeneratedSource,
496+
description: null,
497+
details: [
498+
{
499+
kind: 'error',
500+
loc: GeneratedSource,
501+
message: null,
502+
},
503+
],
472504
});
473505
}
474506
}
@@ -487,7 +519,14 @@ function convertFlowType(flowType: FlowType, loc: string): ResolvedType {
487519
} else {
488520
CompilerError.invariant(false, {
489521
reason: `Unsupported property kind ${prop.kind}`,
490-
loc: GeneratedSource,
522+
description: null,
523+
details: [
524+
{
525+
kind: 'error',
526+
loc: GeneratedSource,
527+
message: null,
528+
},
529+
],
491530
});
492531
}
493532
}
@@ -500,7 +539,14 @@ function convertFlowType(flowType: FlowType, loc: string): ResolvedType {
500539
}
501540
CompilerError.invariant(false, {
502541
reason: `Unsupported class instance type ${flowType.def.type.kind}`,
503-
loc: GeneratedSource,
542+
description: null,
543+
details: [
544+
{
545+
kind: 'error',
546+
loc: GeneratedSource,
547+
message: null,
548+
},
549+
],
504550
});
505551
}
506552
case 'Fun':
@@ -559,7 +605,14 @@ function convertFlowType(flowType: FlowType, loc: string): ResolvedType {
559605
} else {
560606
CompilerError.invariant(false, {
561607
reason: `Unsupported component props type ${propsType.type.kind}`,
562-
loc: GeneratedSource,
608+
description: null,
609+
details: [
610+
{
611+
kind: 'error',
612+
loc: GeneratedSource,
613+
message: null,
614+
},
615+
],
563616
});
564617
}
565618

@@ -712,7 +765,14 @@ export class FlowTypeEnv implements ITypeEnv {
712765
// TODO: use flow-js only for web environments (e.g. playground)
713766
CompilerError.invariant(env.config.flowTypeProvider != null, {
714767
reason: 'Expected flowDumpTypes to be defined in environment config',
715-
loc: GeneratedSource,
768+
description: null,
769+
details: [
770+
{
771+
kind: 'error',
772+
loc: GeneratedSource,
773+
message: null,
774+
},
775+
],
716776
});
717777
let stdout: any;
718778
if (source === lastFlowSource) {

0 commit comments

Comments
 (0)