@samitouri / QOS-React / commits / 2b75c7915b

Add @panicOnBailout support to snap

Test plan: tests pass with snap

Lauren Tan committed Apr 21, 2023 at 15:22 UTC 2b75c7915bed25e9114d76b3c0c019dfc7b61f1f
1 file changed +26
compiler/forget/packages/snap/src/compiler-worker.ts
+26
@@ -9,6 +9,8 @@ import fs from "fs/promises";
9 import path from "path";
10 import { exists } from "./utils";
11
12 +const originalConsoleError = console.error;
13 +
14 let version: number | null = null;
15 export function clearRequireCache() {
16 Object.keys(require.cache).forEach(function (key) {
@@ -29,6 +31,10 @@ export async function compile(
31 fixture: string,
32 compilerVersion: number
33 ): Promise<TestResult> {
34 + const seenConsoleErrors: Array<string> = [];
35 + console.error = (...messages: Array<string>) => {
36 + seenConsoleErrors.push(...messages);
37 + };
38 if (version !== null && compilerVersion !== version) {
39 clearRequireCache();
40 }
@@ -76,6 +82,11 @@ export async function compile(
82 };
83 }
84
85 + let panicOnBailout = true;
86 + if (firstLine.indexOf("@panicOnBailout false") !== -1) {
87 + panicOnBailout = false;
88 + }
89 +
90 const language = parseLanguage(firstLine);
91
92 code = runReactForgetBabelPlugin(input, fixture, language, {
@@ -96,11 +107,22 @@ export async function compile(
107 },
108 logger: null,
109 gating,
110 + panicOnBailout,
111 }).code;
112 } catch (e) {
113 error = e;
114 }
115
116 + // Promote console errors so they can be recorded in fixture output
117 + for (const consoleError of seenConsoleErrors) {
118 + if (error != null) {
119 + error.message = `${error.message}\n\n${consoleError}`;
120 + } else {
121 + error = new Error(consoleError);
122 + error.name = "ConsoleError";
123 + }
124 + }
125 +
126 let output: string;
127 const expectError = fixture.startsWith("error.");
128 if (expectError) {
@@ -109,6 +131,8 @@ export async function compile(
131 throw new Error(
132 `Expected an error to be thrown for fixture: '${fixture}', remove the 'error.' prefix if an error is not expected.`
133 );
134 + } else if (code != null) {
135 + output = `${formatOutput(code)}\n${formatErrorOutput(error)}`;
136 } else {
137 output = formatErrorOutput(error);
138 }
@@ -132,6 +156,8 @@ ${wrapWithTripleBackticks(input, "javascript")}
156 ${output}
157 `; // trailing newline + space internional
158
159 + console.error = originalConsoleError;
160 +
161 return { inputPath, outputPath, actual, expected };
162 }
163