MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / cleanUp

Function cleanUp

tools/acorn-optimizer.mjs:231–290  ·  view source on GitHub ↗
(ast, names)

Source from the content-addressed store, hash-verified

229 return scope[name];
230 }
231 function cleanUp(ast, names) {
232 recursiveWalk(ast, {
233 ForStatement(node, c) {
234 visitChildren(node, c);
235 // If we had `for (var x = ...; ...)` and we removed `x`, we need to change to `for (; ...)`.
236 if (node.init?.type === 'EmptyStatement') {
237 node.init = null;
238 }
239 },
240 ForInStatement(node, c) {
241 // We can't remove the var in a for-in, as that would result in an invalid syntax. Skip the LHS.
242 c(node.right);
243 c(node.body);
244 },
245 ForOfStatement(node, c) {
246 // We can't remove the var in a for-of, as that would result in an invalid syntax. Skip the LHS.
247 c(node.right);
248 c(node.body);
249 },
250 VariableDeclaration(node, _c) {
251 let removedHere = 0;
252 node.declarations = node.declarations.filter((node) => {
253 assert(node.type === 'VariableDeclarator');
254 let keep = node.init && hasSideEffects(node.init);
255 walkPattern(
256 node.id,
257 (value) => {
258 keep ||= hasSideEffects(value);
259 },
260 (boundName) => {
261 keep ||= !names.has(boundName);
262 },
263 );
264 if (!keep) removedHere = 1;
265 return keep;
266 });
267 removed += removedHere;
268 if (node.declarations.length === 0) {
269 emptyOut(node);
270 }
271 },
272 ExpressionStatement(node, _c) {
273 if (aggressive && !hasSideEffects(node)) {
274 emptyOut(node);
275 removed++;
276 }
277 },
278 FunctionDeclaration(node, _c) {
279 if (names.has(node.id.name)) {
280 removed++;
281 emptyOut(node);
282 return;
283 }
284 // do not recurse into other scopes
285 },
286 // do not recurse into other scopes
287 FunctionExpression() {},
288 ArrowFunctionExpression() {},

Callers 2

handleFunctionFunction · 0.85
iterationFunction · 0.85

Calls 1

recursiveWalkFunction · 0.85

Tested by

no test coverage detected