Skip to content

Commit ec4374c

Browse files
authored
[compiler] Show logged errors in playground (facebook#33740)
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 60b5271 commit ec4374c

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
PrintedCompilerPipelineValue,
4545
} from './Output';
4646
import {transformFromAstSync} from '@babel/core';
47+
import {LoggerEvent} from 'babel-plugin-react-compiler/dist/Entrypoint';
4748

4849
function parseInput(
4950
input: string,
@@ -143,6 +144,7 @@ const COMMON_HOOKS: Array<[string, Hook]> = [
143144
function compile(source: string): [CompilerOutput, 'flow' | 'typescript'] {
144145
const results = new Map<string, Array<PrintedCompilerPipelineValue>>();
145146
const error = new CompilerError();
147+
const otherErrors: Array<CompilerErrorDetail> = [];
146148
const upsert: (result: PrintedCompilerPipelineValue) => void = result => {
147149
const entry = results.get(result.name);
148150
if (Array.isArray(entry)) {
@@ -210,7 +212,11 @@ function compile(source: string): [CompilerOutput, 'flow' | 'typescript'] {
210212
},
211213
logger: {
212214
debugLogIRs: logIR,
213-
logEvent: () => {},
215+
logEvent: (_filename: string | null, event: LoggerEvent) => {
216+
if (event.kind === 'CompileError') {
217+
otherErrors.push(new CompilerErrorDetail(event.detail));
218+
}
219+
},
214220
},
215221
});
216222
transformOutput = invokeCompiler(source, language, opts);
@@ -237,6 +243,10 @@ function compile(source: string): [CompilerOutput, 'flow' | 'typescript'] {
237243
);
238244
}
239245
}
246+
// Only include logger errors if there weren't other errors
247+
if (!error.hasErrors() && otherErrors.length !== 0) {
248+
otherErrors.forEach(e => error.push(e));
249+
}
240250
if (error.hasErrors()) {
241251
return [{kind: 'err', results, error: error}, language];
242252
}

0 commit comments

Comments
 (0)