Skip to content

Commit 3acc1fd

Browse files
committed
[compiler] Show logged errors in playground
In playground it's helpful to show all errors, even those that don't completely abort compilation. For example, to help demonstrate that the compiler catches things like setState in effects. This detects these errors and ensures we show them.
1 parent 956d770 commit 3acc1fd

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

compiler/apps/playground/components/Editor/EditorImpl.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import BabelPluginReactCompiler, {
2121
parsePluginOptions,
2222
printReactiveFunctionWithOutlined,
2323
printFunctionWithOutlined,
24+
CompilerErrorDetailOptions,
2425
} from 'babel-plugin-react-compiler';
2526
import clsx from 'clsx';
2627
import invariant from 'invariant';
@@ -44,6 +45,7 @@ import {
4445
PrintedCompilerPipelineValue,
4546
} from './Output';
4647
import {transformFromAstSync} from '@babel/core';
48+
import {LoggerEvent} from 'babel-plugin-react-compiler/dist/Entrypoint';
4749

4850
function parseInput(
4951
input: string,
@@ -143,6 +145,7 @@ const COMMON_HOOKS: Array<[string, Hook]> = [
143145
function compile(source: string): [CompilerOutput, 'flow' | 'typescript'] {
144146
const results = new Map<string, Array<PrintedCompilerPipelineValue>>();
145147
const error = new CompilerError();
148+
const otherErrors: Array<CompilerErrorDetail> = [];
146149
const upsert: (result: PrintedCompilerPipelineValue) => void = result => {
147150
const entry = results.get(result.name);
148151
if (Array.isArray(entry)) {
@@ -210,7 +213,11 @@ function compile(source: string): [CompilerOutput, 'flow' | 'typescript'] {
210213
},
211214
logger: {
212215
debugLogIRs: logIR,
213-
logEvent: () => {},
216+
logEvent: (_filename: string | null, event: LoggerEvent) => {
217+
if (event.kind === 'CompileError') {
218+
otherErrors.push(new CompilerErrorDetail(event.detail));
219+
}
220+
},
214221
},
215222
});
216223
transformOutput = invokeCompiler(source, language, opts);
@@ -237,6 +244,10 @@ function compile(source: string): [CompilerOutput, 'flow' | 'typescript'] {
237244
);
238245
}
239246
}
247+
// Only include logger errors if there weren't other errors
248+
if (!error.hasErrors() && otherErrors.length !== 0) {
249+
otherErrors.forEach(e => error.push(e));
250+
}
240251
if (error.hasErrors()) {
241252
return [{kind: 'err', results, error: error}, language];
242253
}

0 commit comments

Comments
 (0)