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
This commit is contained in:
Lauren Tan
2023-02-06 19:03:54 -05:00
parent 0d2653dbd4
commit e159844b0d
+13 -2
View File
@@ -8,8 +8,10 @@
/// <reference path="./plugin-syntax-jsx.d.ts" />
import type * as BabelCore from "@babel/core";
import generate from "@babel/generator";
import jsx from "@babel/plugin-syntax-jsx";
import { parseCompilerFlags } from "../CompilerFlags";
import prettier from "prettier";
import { compile } from "../CompilerPipeline";
/**
@@ -46,8 +48,17 @@ export default function ReactForgetBabelPlugin(
// We are generating a new FunctionDeclaration node, so we must skip over it or this
// traversal will loop infinitely.
fn.replaceWith(ast);
fn.skip();
try {
fn.replaceWith(ast);
fn.skip();
} catch (err) {
const result = generate(ast);
err.message = `${err.message}\n\n${prettier.format(result.code, {
semi: true,
parser: "babel-ts",
})}`;
throw err;
}
},
},
},