@@ -25509,6 +25509,9 @@ const installImprove = (modules) => {
2550925509 const localFunctions = new Map();
2551025510 /** @type {Set<Node>} */
2551125511 const writtenNames = new Set();
25512+ // The name a name declared once was set to, by declaring symbol.
25513+ /** @type {Map<Node, Node>} */
25514+ const functionAliases = new Map();
2551225515 // An `eval`, direct or not, may write any binding it can see, unseen.
2551325516 let programReadsEval = false;
2551425517 /**
@@ -25533,6 +25536,36 @@ const installImprove = (modules) => {
2553325536 ? undefined
2553425537 : localFunctions.get(declaration);
2553525538 };
25539+ /**
25540+ * The name a callee stands for, following names declared once, never
25541+ * written again, and set to another name; a cycle stops where it closes.
25542+ * @param {Node} callee the call's callee
25543+ * @returns {Node} the last name followed, or the callee itself
25544+ */
25545+ const aliasedFunction = (callee) => {
25546+ /** @type {Set<Node>} */
25547+ const seen = new Set();
25548+ let current = callee;
25549+ if (programReadsEval) return current;
25550+ while (
25551+ A.isSymbolRefNode(current) &&
25552+ current.definition &&
25553+ current.definition.identifiers.length === 1
25554+ ) {
25555+ const [declaration] = current.definition.identifiers;
25556+ const target = functionAliases.get(declaration);
25557+ if (
25558+ target === undefined ||
25559+ writtenNames.has(declaration) ||
25560+ seen.has(declaration)
25561+ ) {
25562+ break;
25563+ }
25564+ seen.add(declaration);
25565+ current = target;
25566+ }
25567+ return current;
25568+ };
2553625569 /**
2553725570 * Records what a function the program defines but cannot be read may do
2553825571 * with the global object or a built-in handed to it.
@@ -25569,13 +25602,15 @@ const installImprove = (modules) => {
2556925602 parameterAliases.clear();
2557025603 localFunctions.clear();
2557125604 writtenNames.clear();
25605+ functionAliases.clear();
2557225606 }
2557325607 };
2557425608 // Runs once scope is known, so a local named `self` is told from the global.
2557525609 collapseHooks.probeProgram = (toplevel) => {
2557625610 parameterAliases.clear();
2557725611 localFunctions.clear();
2557825612 writtenNames.clear();
25613+ functionAliases.clear();
2557925614 programReadsEval = false;
2558025615 if (improvements.enabled) {
2558125616 let depth = 0;
@@ -25650,6 +25685,13 @@ const installImprove = (modules) => {
2565025685 A.isLambdaNode(node.init)
2565125686 ) {
2565225687 localFunctions.set(node.id.definition.identifiers[0], node.init);
25688+ } else if (
25689+ A.isVarDefNode(node) &&
25690+ A.isSymbolDeclarationNode(node.id) &&
25691+ node.init &&
25692+ A.isSymbolRefNode(node.init)
25693+ ) {
25694+ functionAliases.set(node.id.definition.identifiers[0], node.init);
2565325695 } else if (
2565425696 A.isSymbolRefNode(node) &&
2565525697 (isWrittenName(node, walker) ||
@@ -25680,7 +25722,7 @@ const installImprove = (modules) => {
2568025722 // it, until no call hands one on.
2568125723 for (let i = 0; i < handOffs.length; i++) {
2568225724 const { call, argument, name } = handOffs[i];
25683- const lambda = calledFunction(call.callee);
25725+ const lambda = calledFunction(aliasedFunction( call.callee) );
2568425726 if (lambda === undefined || !A.isLambdaNode(lambda)) continue;
2568525727 const index = call.arguments.indexOf(argument);
2568625728 const parameter = lambda.params[index];
0 commit comments