[babel] Outline insertNewFunctionDeclaration
This lets us use the parsed and validated `gating` and `instrumentForget` options.
Sathya Gunasekaran committed
Nov 7, 2023 at 11:04 UTC
58a10c0ac1ce429aad943d7ee8da6b608e9e3cde
1 file changed
+24
-32
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts
+24
-32
@@ -106,14 +106,10 @@ function handleError(
106
}
107
}
108
109
-/**
110
- * Mutates the source AST to include a newly Forget-compiled function.
111
- */
112
-function insertNewFunctionDeclaration(
109
+function createNewFunctionNode(
110
originalFn: BabelFn,
114
- compiledFn: CodegenFunction,
115
- pass: CompilerPass
116
-): void {
111
+ compiledFn: CodegenFunction
112
+): t.FunctionDeclaration | t.ArrowFunctionExpression | t.FunctionExpression {
113
let transformedFn:
114
| t.FunctionDeclaration
115
| t.ArrowFunctionExpression
@@ -162,25 +158,7 @@ function insertNewFunctionDeclaration(
158
159
// Avoid visiting the new transformed version
160
ALREADY_COMPILED.add(transformedFn);
165
-
166
- if (pass.opts.instrumentForget != null) {
167
- const instrumentFnName = pass.opts.instrumentForget.importSpecifierName;
168
- addInstrumentForget(transformedFn, instrumentFnName);
169
- }
170
- if (pass.opts)
171
- if (pass.opts.gating != null) {
172
- if (pass.opts.instrumentForget != null) {
173
- const instrumentFnName = pass.opts.instrumentForget.importSpecifierName;
174
- addInstrumentForget(originalFn.node, instrumentFnName);
175
- }
176
- insertGatedFunctionDeclaration(
177
- originalFn,
178
- transformedFn,
179
- pass.opts.gating
180
- );
181
- } else {
182
- originalFn.replaceWith(transformedFn);
183
- }
161
+ return transformedFn;
162
}
163
164
function findEslintSuppressions(
@@ -329,17 +307,17 @@ export function compileProgram(
307
}
308
309
const externalFunctions: ExternalFunction[] = [];
310
+ let instrumentForget: null | ExternalFunction = null;
311
+ let gating: null | ExternalFunction = null;
312
try {
313
// TODO: check for duplicate import specifiers
314
if (options.gating != null) {
335
- const gating = tryParseExternalFunction(options.gating);
315
+ gating = tryParseExternalFunction(options.gating);
316
externalFunctions.push(gating);
317
}
318
319
if (options.instrumentForget != null) {
340
- const instrumentForget = tryParseExternalFunction(
341
- options.instrumentForget
342
- );
320
+ instrumentForget = tryParseExternalFunction(options.instrumentForget);
321
externalFunctions.push(instrumentForget);
322
}
323
@@ -356,8 +334,22 @@ export function compileProgram(
334
335
// Only insert Forget-ified functions if we have not encountered a critical
336
// error elsewhere in the file, regardless of bailout mode.
359
- for (const { originalFn: fn, compiledFn } of compiledFns) {
360
- insertNewFunctionDeclaration(fn, compiledFn, pass);
337
+ for (const { originalFn, compiledFn } of compiledFns) {
338
+ const transformedFn = createNewFunctionNode(originalFn, compiledFn);
339
+ if (instrumentForget != null) {
340
+ const instrumentFnName = instrumentForget.importSpecifierName;
341
+ addInstrumentForget(transformedFn, instrumentFnName);
342
+ }
343
+
344
+ if (gating != null) {
345
+ if (instrumentForget != null) {
346
+ const instrumentFnName = instrumentForget.importSpecifierName;
347
+ addInstrumentForget(originalFn.node, instrumentFnName);
348
+ }
349
+ insertGatedFunctionDeclaration(originalFn, transformedFn, gating);
350
+ } else {
351
+ originalFn.replaceWith(transformedFn);
352
+ }
353
}
354
355
// Forget compiled the component, we need to update existing imports of unstable_useMemoCache