Skip to content

Commit 3a1ff39

Browse files
committed
[compiler] Derive ErrorSeverity from ErrorCategory
With facebook#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 c4e2508 commit 3a1ff39

File tree

63 files changed

+379
-395
lines changed

Some content is hidden

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

63 files changed

+379
-395
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: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {NodePath} from '@babel/core';
99
import * as t from '@babel/types';
1010
import {Scope as BabelScope} from '@babel/traverse';
1111

12-
import {CompilerError, ErrorCategory, ErrorSeverity} from '../CompilerError';
12+
import {CompilerError, ErrorCategory} from '../CompilerError';
1313
import {
1414
EnvironmentConfig,
1515
GeneratedSource,
@@ -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 & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
CompilerError,
1212
CompilerErrorDetail,
1313
ErrorCategory,
14-
ErrorSeverity,
1514
} from '../CompilerError';
1615
import {ExternalFunction, ReactFunctionType} from '../HIR/Environment';
1716
import {CodegenFunction} from '../ReactiveScopes';
@@ -105,7 +104,6 @@ function findDirectivesDynamicGating(
105104
errors.push({
106105
reason: `Dynamic gating directive is not a valid JavaScript identifier`,
107106
description: `Found '${directive.value.value}'`,
108-
severity: ErrorSeverity.InvalidReact,
109107
category: ErrorCategory.Gating,
110108
loc: directive.loc ?? null,
111109
suggestions: null,
@@ -122,7 +120,6 @@ function findDirectivesDynamicGating(
122120
description: `Expected a single directive but found [${result
123121
.map(r => r.directive.value.value)
124122
.join(', ')}]`,
125-
severity: ErrorSeverity.InvalidReact,
126123
category: ErrorCategory.Gating,
127124
loc: result[0].directive.loc ?? null,
128125
suggestions: null,
@@ -141,15 +138,13 @@ function findDirectivesDynamicGating(
141138
}
142139
}
143140

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

148145
function isConfigError(err: unknown): boolean {
149146
if (err instanceof CompilerError) {
150-
return err.details.some(
151-
detail => detail.severity === ErrorSeverity.InvalidConfig,
152-
);
147+
return err.details.some(detail => detail.category === ErrorCategory.Config);
153148
}
154149
return false;
155150
}
@@ -214,8 +209,7 @@ function handleError(
214209
logError(err, context, fnLoc);
215210
if (
216211
context.opts.panicThreshold === 'all_errors' ||
217-
(context.opts.panicThreshold === 'critical_errors' &&
218-
isCriticalError(err)) ||
212+
(context.opts.panicThreshold === 'critical_errors' && isError(err)) ||
219213
isConfigError(err) // Always throws regardless of panic threshold
220214
) {
221215
throw err;
@@ -458,7 +452,6 @@ export function compileProgram(
458452
new CompilerErrorDetail({
459453
reason:
460454
'Unexpected compiled functions when module scope opt-out is present',
461-
severity: ErrorSeverity.Invariant,
462455
category: ErrorCategory.Invariant,
463456
loc: null,
464457
}),
@@ -827,7 +820,6 @@ function shouldSkipCompilation(
827820
reason: `Expected a filename but found none.`,
828821
description:
829822
"When the 'sources' config options is specified, the React compiler will only compile files with a name",
830-
severity: ErrorSeverity.InvalidConfig,
831823
category: ErrorCategory.Config,
832824
loc: null,
833825
}),
@@ -890,7 +882,6 @@ function validateNoDynamicallyCreatedComponentsOrHooks(
890882
if (nestedFnType === 'Component' || nestedFnType === 'Hook') {
891883
CompilerError.throwDiagnostic({
892884
category: ErrorCategory.Factories,
893-
severity: ErrorSeverity.InvalidReact,
894885
reason: `Components and hooks cannot be created dynamically`,
895886
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`,
896887
details: [

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
CompilerError,
1313
CompilerSuggestionOperation,
1414
ErrorCategory,
15-
ErrorSeverity,
1615
} from '../CompilerError';
1716
import {assertExhaustive} from '../Utils/utils';
1817
import {GeneratedSource} from '../HIR';
@@ -186,7 +185,6 @@ export function suppressionsToCompilerError(
186185
CompilerDiagnostic.create({
187186
reason: reason,
188187
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,
190188
category: ErrorCategory.Suppression,
191189
suggestions: [
192190
{

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import {NodePath} from '@babel/core';
99
import * as t from '@babel/types';
1010

11-
import {CompilerError, EnvironmentConfig, ErrorSeverity, Logger} from '..';
11+
import {CompilerError, EnvironmentConfig, Logger} from '..';
1212
import {getOrInsertWith} from '../Utils/utils';
1313
import {Environment, GeneratedSource} from '../HIR';
1414
import {DEFAULT_EXPORT} from '../HIR/Environment';
@@ -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)