[babel] Remove prettier from plugin
I think we added this for easier debugging but isn't strictly needed. Remove since this was causing some issues internally.
Lauren Tan committed
Mar 13, 2023 at 14:32 UTC
e90d058bee744708c56cc6b1ffa2ffe4aa17a39d
1 file changed
+41
-55
compiler/forget/src/Babel/BabelPlugin.ts
+41
-55
@@ -8,11 +8,9 @@
8
/// <reference path="./plugin-syntax-jsx.d.ts" />
9
10
import type * as BabelCore from "@babel/core";
11
-import generate from "@babel/generator";
11
import jsx from "@babel/plugin-syntax-jsx";
12
import * as t from "@babel/types";
13
import invariant from "invariant";
15
-import prettier from "prettier";
14
import { compile } from "../CompilerPipeline";
15
import { parsePluginOptions, PluginOptions } from "./PluginOptions";
16
@@ -59,62 +57,50 @@ export default function ReactForgetBabelPlugin(
57
hasForgetCompiledCode = true;
58
const ast = compile(fn, pass.opts.environment);
59
62
- try {
63
- if (pass.opts.gatingModule) {
64
- // Rename existing function
65
- invariant(fn.node.id, "FunctionDeclaration must have a name");
66
- const original = fn.node.id;
67
- fn.node.id = addSuffix(fn.node.id, "_uncompiled");
68
-
69
- // Rename and append compiled function
70
- invariant(ast.id, "FunctionDeclaration must produce a name");
71
- ast.id = addSuffix(ast.id, "_forget");
72
- const compiledFn = fn.insertAfter(ast)[0];
73
- compiledFn.skip();
74
-
75
- // Build gating test
76
- const test = buildTest({
77
- compiled: ast.id,
78
- uncompiled: fn.node.id,
79
- original,
80
- });
81
-
82
- // Re-export new declaration
83
- const parent = fn.parentPath;
84
- if (t.isExportDefaultDeclaration(parent)) {
85
- // Re-add uncompiled function
86
- parent.replaceWith(fn)[0].skip();
87
-
88
- // Add test and synthesize new export
89
- compiledFn.insertAfter([
90
- test,
91
- t.exportDefaultDeclaration(original),
92
- ]);
93
- } else if (t.isExportNamedDeclaration(parent)) {
94
- // Re-add uncompiled function
95
- parent.replaceWith(fn)[0].skip();
96
-
97
- // Add and export test
98
- compiledFn.insertAfter(t.exportNamedDeclaration(test));
99
- } else {
100
- // Just add the test, no need for re-export
101
- compiledFn.insertAfter(test);
102
- }
60
+ if (pass.opts.gatingModule) {
61
+ // Rename existing function
62
+ invariant(fn.node.id, "FunctionDeclaration must have a name");
63
+ const original = fn.node.id;
64
+ fn.node.id = addSuffix(fn.node.id, "_uncompiled");
65
+
66
+ // Rename and append compiled function
67
+ invariant(ast.id, "FunctionDeclaration must produce a name");
68
+ ast.id = addSuffix(ast.id, "_forget");
69
+ const compiledFn = fn.insertAfter(ast)[0];
70
+ compiledFn.skip();
71
+
72
+ // Build gating test
73
+ const test = buildTest({
74
+ compiled: ast.id,
75
+ uncompiled: fn.node.id,
76
+ original,
77
+ });
78
+
79
+ // Re-export new declaration
80
+ const parent = fn.parentPath;
81
+ if (t.isExportDefaultDeclaration(parent)) {
82
+ // Re-add uncompiled function
83
+ parent.replaceWith(fn)[0].skip();
84
+
85
+ // Add test and synthesize new export
86
+ compiledFn.insertAfter([test, t.exportDefaultDeclaration(original)]);
87
+ } else if (t.isExportNamedDeclaration(parent)) {
88
+ // Re-add uncompiled function
89
+ parent.replaceWith(fn)[0].skip();
90
+
91
+ // Add and export test
92
+ compiledFn.insertAfter(t.exportNamedDeclaration(test));
93
} else {
104
- fn.replaceWith(ast);
94
+ // Just add the test, no need for re-export
95
+ compiledFn.insertAfter(test);
96
}
106
-
107
- // We are generating a new FunctionDeclaration node, so we must skip over it or this
108
- // traversal will loop infinitely.
109
- fn.skip();
110
- } catch (err) {
111
- const result = generate(ast);
112
- err.message = `${err.message}\n\n${prettier.format(result.code, {
113
- semi: true,
114
- parser: "babel-ts",
115
- })}`;
116
- throw err;
97
+ } else {
98
+ fn.replaceWith(ast);
99
}
100
+
101
+ // We are generating a new FunctionDeclaration node, so we must skip over it or this
102
+ // traversal will loop infinitely.
103
+ fn.skip();
104
},
105
};
106