Skip to content

Commit 9d90dc2

Browse files
committed
[compiler] Derive ErrorSeverity from ErrorCategory
With #34176 we now have granular lint rules created for each compiler ErrorCategory. However, we had remnants of our old error severities still in use which makes reporting errors quite clunky. Previously you would need to specify both a category and severity which often ended up being the same. This PR moves severity definition into our rules which are generated from our categories. For now I decided to defer "upgrading" categories from a simple string to a sum type since we are only using severities to map errors to eslint severity.
1 parent de5a1b2 commit 9d90dc2

File tree

53 files changed

+299
-333
lines changed

Some content is hidden

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

53 files changed

+299
-333
lines changed

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

Lines changed: 210 additions & 144 deletions
Large diffs are not rendered by default.

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export function validateRestrictedImports(
3939
if (restrictedImports.has(importDeclPath.node.source.value)) {
4040
error.push({
4141
category: ErrorCategory.Todo,
42-
severity: ErrorSeverity.Todo,
4342
reason: 'Bailing out due to blocklisted import',
4443
description: `Import from module ${importDeclPath.node.source.value}`,
4544
loc: importDeclPath.node.loc ?? null,
@@ -207,7 +206,6 @@ export class ProgramContext {
207206
const error = new CompilerError();
208207
error.push({
209208
category: ErrorCategory.Todo,
210-
severity: ErrorSeverity.Todo,
211209
reason: 'Encountered conflicting global in generated program',
212210
description: `Conflict from local binding ${name}`,
213211
loc: scope.getBinding(name)?.path.node.loc ?? null,

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ function findDirectivesDynamicGating(
105105
errors.push({
106106
reason: `Dynamic gating directive is not a valid JavaScript identifier`,
107107
description: `Found '${directive.value.value}'`,
108-
severity: ErrorSeverity.InvalidReact,
109108
category: ErrorCategory.Gating,
110109
loc: directive.loc ?? null,
111110
suggestions: null,
@@ -122,7 +121,6 @@ function findDirectivesDynamicGating(
122121
description: `Expected a single directive but found [${result
123122
.map(r => r.directive.value.value)
124123
.join(', ')}]`,
125-
severity: ErrorSeverity.InvalidReact,
126124
category: ErrorCategory.Gating,
127125
loc: result[0].directive.loc ?? null,
128126
suggestions: null,
@@ -141,15 +139,13 @@ function findDirectivesDynamicGating(
141139
}
142140
}
143141

144-
function isCriticalError(err: unknown): boolean {
145-
return !(err instanceof CompilerError) || err.isCritical();
142+
function isError(err: unknown): boolean {
143+
return !(err instanceof CompilerError) || err.isError();
146144
}
147145

148146
function isConfigError(err: unknown): boolean {
149147
if (err instanceof CompilerError) {
150-
return err.details.some(
151-
detail => detail.severity === ErrorSeverity.InvalidConfig,
152-
);
148+
return err.details.some(detail => detail.category === ErrorCategory.Config);
153149
}
154150
return false;
155151
}
@@ -214,8 +210,7 @@ function handleError(
214210
logError(err, context, fnLoc);
215211
if (
216212
context.opts.panicThreshold === 'all_errors' ||
217-
(context.opts.panicThreshold === 'critical_errors' &&
218-
isCriticalError(err)) ||
213+
(context.opts.panicThreshold === 'critical_errors' && isError(err)) ||
219214
isConfigError(err) // Always throws regardless of panic threshold
220215
) {
221216
throw err;
@@ -458,7 +453,6 @@ export function compileProgram(
458453
new CompilerErrorDetail({
459454
reason:
460455
'Unexpected compiled functions when module scope opt-out is present',
461-
severity: ErrorSeverity.Invariant,
462456
category: ErrorCategory.Invariant,
463457
loc: null,
464458
}),
@@ -827,7 +821,6 @@ function shouldSkipCompilation(
827821
reason: `Expected a filename but found none.`,
828822
description:
829823
"When the 'sources' config options is specified, the React compiler will only compile files with a name",
830-
severity: ErrorSeverity.InvalidConfig,
831824
category: ErrorCategory.Config,
832825
loc: null,
833826
}),
@@ -890,7 +883,6 @@ function validateNoDynamicallyCreatedComponentsOrHooks(
890883
if (nestedFnType === 'Component' || nestedFnType === 'Hook') {
891884
CompilerError.throwDiagnostic({
892885
category: ErrorCategory.Factories,
893-
severity: ErrorSeverity.InvalidReact,
894886
reason: `Components and hooks cannot be created dynamically`,
895887
description: `The function \`${nestedName}\` appears to be a React ${nestedFnType.toLowerCase()}, but it's defined inside \`${parentName}\`. Components and Hooks should always be declared at module scope`,
896888
details: [

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ export function suppressionsToCompilerError(
186186
CompilerDiagnostic.create({
187187
reason: reason,
188188
description: `React Compiler only works when your components follow all the rules of React, disabling them may result in unexpected or incorrect behavior. Found suppression \`${suppressionRange.disableComment.value.trim()}\``,
189-
severity: ErrorSeverity.InvalidReact,
190189
category: ErrorCategory.Suppression,
191190
suggestions: [
192191
{

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,15 @@ import {
2020
} from '../CompilerError';
2121

2222
function throwInvalidReact(
23-
options: Omit<CompilerDiagnosticOptions, 'severity'>,
23+
options: CompilerDiagnosticOptions,
2424
{logger, filename}: TraversalState,
2525
): never {
26-
const detail: CompilerDiagnosticOptions = {
27-
severity: ErrorSeverity.InvalidReact,
28-
...options,
29-
};
3026
logger?.logEvent(filename, {
3127
kind: 'CompileError',
3228
fnLoc: null,
33-
detail: new CompilerDiagnostic(detail),
29+
detail: new CompilerDiagnostic(options),
3430
});
35-
CompilerError.throwDiagnostic(detail);
31+
CompilerError.throwDiagnostic(options);
3632
}
3733

3834
function isAutodepsSigil(

0 commit comments

Comments
 (0)