Skip to content

Commit 5a6c20d

Browse files
committed
[compiler] Patch error reporting for blocklisted imports
ghstack-source-id: 614c1e9 Pull Request resolved: #30652
1 parent 0e6d8c3 commit 5a6c20d

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,39 @@
77

88
import {NodePath} from '@babel/core';
99
import * as t from '@babel/types';
10-
import {CompilerError} from '../CompilerError';
10+
import {CompilerError, ErrorSeverity} from '../CompilerError';
1111
import {EnvironmentConfig, ExternalFunction, GeneratedSource} from '../HIR';
1212
import {getOrInsertDefault} from '../Utils/utils';
1313

1414
export function validateRestrictedImports(
1515
path: NodePath<t.Program>,
1616
{validateBlocklistedImports}: EnvironmentConfig,
17-
): void {
17+
): CompilerError | null {
1818
if (
1919
validateBlocklistedImports == null ||
2020
validateBlocklistedImports.length === 0
2121
) {
22-
return;
22+
return null;
2323
}
24+
const error = new CompilerError();
2425
const restrictedImports = new Set(validateBlocklistedImports);
2526
path.traverse({
2627
ImportDeclaration(importDeclPath) {
2728
if (restrictedImports.has(importDeclPath.node.source.value)) {
28-
CompilerError.throwTodo({
29+
error.push({
30+
severity: ErrorSeverity.Todo,
2931
reason: 'Bailing out due to blocklisted import',
3032
description: `Import from module ${importDeclPath.node.source.value}`,
3133
loc: importDeclPath.node.loc ?? null,
3234
});
3335
}
3436
},
3537
});
38+
if (error.hasErrors()) {
39+
return error;
40+
} else {
41+
return null;
42+
}
3643
}
3744

3845
export function addImportsToProgram(

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,17 @@ function isFilePartOfSources(
277277
return false;
278278
}
279279

280+
/**
281+
* `compileProgram` is directly invoked by the react-compiler babel plugin, so
282+
* exceptions thrown by this function will fail the babel build.
283+
* - call `handleError` if your error is recoverable.
284+
* Unless the error is a warning / info diagnostic, compilation of a function
285+
* / entire file should also be skipped.
286+
* - throw an exception if the error is fatal / not recoverable.
287+
* Examples of this are invalid compiler configs or failure to codegen outlined
288+
* functions *after* already emitting optimized components / hooks that invoke
289+
* the outlined functions.
290+
*/
280291
export function compileProgram(
281292
program: NodePath<t.Program>,
282293
pass: CompilerPass,
@@ -300,7 +311,11 @@ export function compileProgram(
300311
});
301312
}
302313
const environment = environmentResult.unwrap();
303-
validateRestrictedImports(program, environment);
314+
const restrictedImportsErr = validateRestrictedImports(program, environment);
315+
if (restrictedImportsErr) {
316+
handleError(restrictedImportsErr, pass, null);
317+
return;
318+
}
304319
const useMemoCacheIdentifier = program.scope.generateUidIdentifier('c');
305320
const moduleName = pass.opts.runtimeModule ?? 'react/compiler-runtime';
306321

0 commit comments

Comments
 (0)