@samitouri / QOS-React / commits / e159844b0d

Add generated code to errors thrown by BabelPlugin

If we've generated an erroneous AST (eg duplicate declarations), this lets us observe what the code looked like to aid debugging

Lauren Tan committed Feb 6, 2023 at 19:03 UTC e159844b0d95cc65f3001c003d95faac6cf35b17
1 file changed +13 -2
compiler/forget/src/Babel/BabelPlugin.ts
+13 -2
@@ -8,8 +8,10 @@
8 /// <reference path="./plugin-syntax-jsx.d.ts" />
9
10 import type * as BabelCore from "@babel/core";
11 +import generate from "@babel/generator";
12 import jsx from "@babel/plugin-syntax-jsx";
13 import { parseCompilerFlags } from "../CompilerFlags";
14 +import prettier from "prettier";
15 import { compile } from "../CompilerPipeline";
16
17 /**
@@ -46,8 +48,17 @@ export default function ReactForgetBabelPlugin(
48
49 // We are generating a new FunctionDeclaration node, so we must skip over it or this
50 // traversal will loop infinitely.
49 - fn.replaceWith(ast);
50 - fn.skip();
51 + try {
52 + fn.replaceWith(ast);
53 + fn.skip();
54 + } catch (err) {
55 + const result = generate(ast);
56 + err.message = `${err.message}\n\n${prettier.format(result.code, {
57 + semi: true,
58 + parser: "babel-ts",
59 + })}`;
60 + throw err;
61 + }
62 },
63 },
64 },