Address feedback
Lauren Tan committed
Aug 14, 2023 at 12:05 UTC
298bf55f25ce698c16bcd9fc9cdb60e810b8a6fb
1 file changed
+40
-27
compiler/forget/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts
+40
-27
@@ -96,31 +96,46 @@ function compileAndInsertNewFunctionDeclaration(
96
});
97
const originalIdent = fnPath.node.id;
98
99
- let gatedFn = null;
100
- if (pass.opts.gating != null) {
101
- gatedFn = insertGatedFunctionDeclaration(
102
- fnPath,
103
- compiledFn,
104
- originalIdent,
105
- pass.opts.gating
106
- );
107
- } else {
108
- fnPath.replaceWith(compiledFn);
109
- }
110
-
111
- if (pass.opts.instrumentForget != null) {
112
- const instrumentFnName = pass.opts.instrumentForget.importSpecifierName;
113
- addInstrumentForget(fnPath, originalIdent.name, instrumentFnName);
114
- if (pass.opts.gating != null && gatedFn != null) {
115
- addInstrumentForget(gatedFn, originalIdent.name, instrumentFnName);
116
- }
117
- }
99
+ insertNewFunctionDeclaration(fnPath, originalIdent, compiledFn, pass);
100
hasForgetMutatedOriginalSource = true;
101
}
102
103
return hasForgetMutatedOriginalSource;
104
}
105
106
+function insertNewFunctionDeclaration(
107
+ fnPath: NodePath<t.FunctionDeclaration>,
108
+ originalIdent: t.Identifier,
109
+ compiledFn: t.FunctionDeclaration,
110
+ pass: CompilerPass
111
+): void {
112
+ let gatedFn = null;
113
+ if (pass.opts.gating != null) {
114
+ gatedFn = insertGatedFunctionDeclaration(
115
+ fnPath,
116
+ compiledFn,
117
+ originalIdent,
118
+ pass.opts.gating
119
+ );
120
+ } else {
121
+ fnPath.replaceWith(compiledFn);
122
+ }
123
+
124
+ if (pass.opts.instrumentForget != null) {
125
+ const instrumentFnName = pass.opts.instrumentForget.importSpecifierName;
126
+ addInstrumentForget(fnPath, originalIdent.name, instrumentFnName);
127
+ if (pass.opts.gating != null) {
128
+ CompilerError.invariant(gatedFn != null, {
129
+ reason: "Should have inserted a gated function declaration",
130
+ description: null,
131
+ loc: null,
132
+ suggestions: null,
133
+ });
134
+ addInstrumentForget(gatedFn, originalIdent.name, instrumentFnName);
135
+ }
136
+ }
137
+}
138
+
139
export function compileProgram(
140
program: NodePath<t.Program>,
141
pass: CompilerPass
@@ -205,10 +220,9 @@ export function compileProgram(
220
return;
221
}
222
208
- hasForgetMutatedOriginalSource = compileAndInsertNewFunctionDeclaration(
209
- fn,
210
- pass
211
- );
223
+ if (compileAndInsertNewFunctionDeclaration(fn, pass) === true) {
224
+ hasForgetMutatedOriginalSource = true;
225
+ }
226
},
227
228
ArrowFunctionExpression(
@@ -239,10 +253,9 @@ export function compileProgram(
253
return;
254
}
255
242
- hasForgetMutatedOriginalSource = compileAndInsertNewFunctionDeclaration(
243
- loweredFn,
244
- pass
245
- );
256
+ if (compileAndInsertNewFunctionDeclaration(loweredFn, pass) === true) {
257
+ hasForgetMutatedOriginalSource = true;
258
+ }
259
},
260
},
261
{