From b1e2ab2a17d36524996f11a2ac5d1da22e7237e8 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Wed, 30 Aug 2023 20:10:00 +0100 Subject: [PATCH] `with` statements WIP 9 --- lib/serialize/parseFunction.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/serialize/parseFunction.js b/lib/serialize/parseFunction.js index 3e90589f..b64904a1 100644 --- a/lib/serialize/parseFunction.js +++ b/lib/serialize/parseFunction.js @@ -838,10 +838,20 @@ function resolveFunctionInfo( // These are class `extends` clauses and computed method keys. // Initializing properties of `internalVars` needs to happen before processing child functions, // as external vars in child functions can become internal vars in this function. - const internalVarTrails = []; + // Store for later any which have no trails, as that indicates that the binding is accessed + // within a `with () {}` block, and mist not be renamed. + // TODO: This is hacky as hell. There must be a better way to do it. + // TODO: Check if internal vars are always created for all functions above where binding is. + // TODO: Can this logic be combined with logic that prevents functions names being changed? + const internalVarTrails = [], + internalVarsWithNoTrails = []; for (const [varName, trails] of Object.entries(fnInfo.internalVars)) { - if (!internalVars[varName]) internalVars[varName] = []; - internalVarTrails.push({varName, trails}); + if (trails.length > 0) { + if (!internalVars[varName]) internalVars[varName] = []; + internalVarTrails.push({varName, trails}); + } else if (!isNestedFunction) { + internalVarsWithNoTrails.push(varName); + } } // Process child functions @@ -883,15 +893,10 @@ function resolveFunctionInfo( externalVars[varName].push(...trailsToNodes(fnNode, trails, varName)); } } else { - // Var which is external to current function, but internal to function being serialized. - // Skip any where `internalVarNodes.length === 0`, as that indicates the var name is frozen. - // TODO: Is testing `internalVarNodes.length` always going to work? - // TODO: Can this logic be combined with logic that prevents functions names being changed? + // Var which is external to current function, but internal to function being serialized for (const [varName, {trails}] of Object.entries(scope.vars)) { const internalVarNodes = internalVars[varName]; - if (internalVarNodes && internalVarNodes.length > 0) { - internalVarNodes.push(...trailsToNodes(fnNode, trails, varName)); - } + if (internalVarNodes) internalVarNodes.push(...trailsToNodes(fnNode, trails, varName)); } } } @@ -901,6 +906,10 @@ function resolveFunctionInfo( internalVars[varName].push(...trailsToNodes(fnNode, trails, varName)); } + for (const varName of internalVarsWithNoTrails) { + internalVars[varName] = []; + } + // Get global var names const thisGlobalVarNames = fnInfo.globalVarNames; if (thisGlobalVarNames) setAddFrom(globalVarNames, thisGlobalVarNames);