[ez][be] Make early return explicit in compileAndInsert
Mofei Zhang committed
Sep 14, 2023 at 19:40 UTC
b3730eb496425f273683ccfcc18498676c1f16d9
1 file changed
+58
-61
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts
+58
-61
@@ -69,8 +69,7 @@ function compileAndInsertNewFunctionDeclaration(
69
return false;
70
}
71
72
- let compiledFn: CodegenFunction | null = null;
73
- let hasForgetMutatedOriginalSource = false;
72
+ let compiledFn: CodegenFunction;
73
try {
74
compiledFn = compileFn(fnPath, pass.opts.environment);
75
} catch (err) {
@@ -92,75 +91,73 @@ function compileAndInsertNewFunctionDeclaration(
91
log(err, pass.filename ?? null);
92
}
93
}
94
+ return false;
95
}
96
97
+ // Successfully compiled
98
if (pass.opts.noEmit === true) {
99
return false;
100
}
101
101
- // Sucessfully compiled
102
- if (compiledFn != null) {
103
- // We are generating a new FunctionDeclaration node, so we must skip over it or this
104
- // traversal will loop infinitely.
105
- fnPath.skip();
106
-
107
- let transformedFunction:
108
- | t.FunctionDeclaration
109
- | t.ArrowFunctionExpression
110
- | t.FunctionExpression;
111
- switch (fnPath.node.type) {
112
- case "FunctionDeclaration": {
113
- const fn: t.FunctionDeclaration = {
114
- type: "FunctionDeclaration",
115
- id: compiledFn.id,
116
- loc: fnPath.node.loc ?? null,
117
- async: compiledFn.async,
118
- generator: compiledFn.generator,
119
- params: compiledFn.params,
120
- body: compiledFn.body,
121
- };
122
- transformedFunction = fn;
123
- break;
124
- }
125
- case "ArrowFunctionExpression": {
126
- const fn: t.ArrowFunctionExpression = {
127
- type: "ArrowFunctionExpression",
128
- loc: fnPath.node.loc ?? null,
129
- async: compiledFn.async,
130
- generator: compiledFn.generator,
131
- params: compiledFn.params,
132
- expression: fnPath.node.expression,
133
- body: compiledFn.body,
134
- };
135
- transformedFunction = fn;
136
- break;
137
- }
138
- case "FunctionExpression": {
139
- const fn: t.FunctionExpression = {
140
- type: "FunctionExpression",
141
- id: compiledFn.id,
142
- loc: fnPath.node.loc ?? null,
143
- async: compiledFn.async,
144
- generator: compiledFn.generator,
145
- params: compiledFn.params,
146
- body: compiledFn.body,
147
- };
148
- transformedFunction = fn;
149
- break;
150
- }
102
+ // We are generating a new FunctionDeclaration node, so we must skip over it or this
103
+ // traversal will loop infinitely.
104
+ fnPath.skip();
105
+
106
+ let transformedFunction:
107
+ | t.FunctionDeclaration
108
+ | t.ArrowFunctionExpression
109
+ | t.FunctionExpression;
110
+ switch (fnPath.node.type) {
111
+ case "FunctionDeclaration": {
112
+ const fn: t.FunctionDeclaration = {
113
+ type: "FunctionDeclaration",
114
+ id: compiledFn.id,
115
+ loc: fnPath.node.loc ?? null,
116
+ async: compiledFn.async,
117
+ generator: compiledFn.generator,
118
+ params: compiledFn.params,
119
+ body: compiledFn.body,
120
+ };
121
+ transformedFunction = fn;
122
+ break;
123
+ }
124
+ case "ArrowFunctionExpression": {
125
+ const fn: t.ArrowFunctionExpression = {
126
+ type: "ArrowFunctionExpression",
127
+ loc: fnPath.node.loc ?? null,
128
+ async: compiledFn.async,
129
+ generator: compiledFn.generator,
130
+ params: compiledFn.params,
131
+ expression: fnPath.node.expression,
132
+ body: compiledFn.body,
133
+ };
134
+ transformedFunction = fn;
135
+ break;
136
+ }
137
+ case "FunctionExpression": {
138
+ const fn: t.FunctionExpression = {
139
+ type: "FunctionExpression",
140
+ id: compiledFn.id,
141
+ loc: fnPath.node.loc ?? null,
142
+ async: compiledFn.async,
143
+ generator: compiledFn.generator,
144
+ params: compiledFn.params,
145
+ body: compiledFn.body,
146
+ };
147
+ transformedFunction = fn;
148
+ break;
149
}
150
+ }
151
153
- // Ensure we avoid visiting the original function again (since we move it
154
- // within the AST in gating mode)
155
- ALREADY_COMPILED.add(fnPath);
156
- // And avoid visiting the new version as well
157
- ALREADY_COMPILED.add(transformedFunction);
152
+ // Ensure we avoid visiting the original function again (since we move it
153
+ // within the AST in gating mode)
154
+ ALREADY_COMPILED.add(fnPath);
155
+ // And avoid visiting the new version as well
156
+ ALREADY_COMPILED.add(transformedFunction);
157
159
- insertNewFunctionDeclaration(fnPath, transformedFunction, pass);
160
- hasForgetMutatedOriginalSource = true;
161
- }
158
+ insertNewFunctionDeclaration(fnPath, transformedFunction, pass);
159
163
- return hasForgetMutatedOriginalSource;
160
+ return true;
161
}
162
163
function insertNewFunctionDeclaration(