Skip to content

Commit

Permalink
with statements WIP 9
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Aug 30, 2023
1 parent 3738553 commit b1e2ab2
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions lib/serialize/parseFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}
}
Expand All @@ -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);
Expand Down

0 comments on commit b1e2ab2

Please sign in to comment.