unpeek 1.6.1 → 1.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/ast/index.js CHANGED
@@ -92,15 +92,100 @@ function analyzeAstHotPaths(ast) {
92
92
  const coldFunctionNodes = /* @__PURE__ */ new WeakSet();
93
93
  const hotExemptNodes = /* @__PURE__ */ new WeakSet();
94
94
  const directHotNodes = /* @__PURE__ */ new Set();
95
+ const serializedFunctionNodes = /* @__PURE__ */ new Set();
96
+ const serializerHelperNodes = /* @__PURE__ */ new Set();
95
97
  const allFunctions = [];
96
98
  const fnByName = /* @__PURE__ */ new Map();
97
99
  const scopeFunctions = /* @__PURE__ */ new Map();
98
100
  const fnScopeChain = /* @__PURE__ */ new Map();
101
+ const fnParams = /* @__PURE__ */ new Map();
102
+ const fnVarAliases = /* @__PURE__ */ new Map();
99
103
  const callerToCallees = /* @__PURE__ */ new Map();
104
+ const pendingSerializedRefs = [];
105
+ const allCallSites = [];
106
+ function ensureFnParams(fnNode) {
107
+ let paramSet = fnParams.get(fnNode);
108
+ if (!paramSet) {
109
+ paramSet = /* @__PURE__ */ new Set();
110
+ for (const p of fnNode.params || []) {
111
+ if (p?.type === "Identifier") {
112
+ paramSet.add(p.name);
113
+ } else if (p?.type === "AssignmentPattern" && p.left?.type === "Identifier") {
114
+ paramSet.add(p.left.name);
115
+ }
116
+ }
117
+ fnParams.set(fnNode, paramSet);
118
+ }
119
+ return paramSet;
120
+ }
121
+ function recordVarAlias(scopeNode, varName, rhsName) {
122
+ let map = fnVarAliases.get(scopeNode);
123
+ if (!map) {
124
+ map = /* @__PURE__ */ new Map();
125
+ fnVarAliases.set(scopeNode, map);
126
+ }
127
+ map.set(varName, rhsName);
128
+ }
129
+ function resolveAliasTarget(scopeNode, identName) {
130
+ const map = fnVarAliases.get(scopeNode);
131
+ let cur = identName;
132
+ const seen = /* @__PURE__ */ new Set();
133
+ while (map && map.has(cur) && !seen.has(cur)) {
134
+ seen.add(cur);
135
+ cur = map.get(cur);
136
+ }
137
+ return cur;
138
+ }
139
+ function markSerializedFunction(node) {
140
+ serializedFunctionNodes.add(node);
141
+ hotFunctionNodes.add(node);
142
+ directHotNodes.add(node);
143
+ hotExemptNodes.add(node);
144
+ }
145
+ function findEnclosingFunction(ancestors, selfNode) {
146
+ for (let i = ancestors.length - 1; i >= 0; i--) {
147
+ if (ancestors[i] !== selfNode && FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
148
+ return ancestors[i];
149
+ }
150
+ }
151
+ return null;
152
+ }
153
+ function findOutermostModuleFunction(ancestors) {
154
+ const fnList = [];
155
+ for (let i = 0; i < ancestors.length; i++) {
156
+ if (FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
157
+ fnList.push(ancestors[i]);
158
+ }
159
+ }
160
+ if (fnList.length === 0) return null;
161
+ if (fnList.length === 1) return fnList[0];
162
+ return fnList[1];
163
+ }
164
+ function isZeroArgIifeFactoryFunction(node) {
165
+ if (!node || node.params && node.params.length > 0) return false;
166
+ if (!node.body || node.body.type !== "BlockStatement") return false;
167
+ const stmts = (node.body.body || []).filter(
168
+ (s) => !(s.type === "ExpressionStatement" && s.directive)
169
+ );
170
+ if (stmts.length !== 1 || stmts[0].type !== "ReturnStatement") return false;
171
+ const retArg = stmts[0].argument;
172
+ if (!retArg || retArg.type !== "CallExpression") return false;
173
+ const callee = retArg.callee;
174
+ return callee && (callee.type === "FunctionExpression" || callee.type === "ArrowFunctionExpression") && callee.body?.type === "BlockStatement";
175
+ }
100
176
  function registerFunctionNode(node, ancestors) {
101
177
  const name = getFunctionLabel(node, ancestors);
102
178
  allFunctions.push({ node, name });
103
179
  fnByName.set(name, node);
180
+ const paramSet = /* @__PURE__ */ new Set();
181
+ for (const p of node.params || []) {
182
+ if (p?.type === "Identifier") {
183
+ paramSet.add(p.name);
184
+ } else if (p?.type === "AssignmentPattern" && p.left?.type === "Identifier") {
185
+ paramSet.add(p.left.name);
186
+ }
187
+ }
188
+ fnParams.set(node, paramSet);
104
189
  const enclosingScope = findEnclosingScopeNode(ancestors, node);
105
190
  let localMap = scopeFunctions.get(enclosingScope);
106
191
  if (!localMap) {
@@ -109,6 +194,22 @@ function analyzeAstHotPaths(ast) {
109
194
  }
110
195
  localMap.set(name, node);
111
196
  const parent = ancestors[ancestors.length - 2];
197
+ if (parent?.type === "VariableDeclarator" && parent.id?.type === "Identifier") {
198
+ fnByName.set(parent.id.name, node);
199
+ localMap.set(parent.id.name, node);
200
+ } else if (parent?.type === "AssignmentExpression" && parent.left?.type === "Identifier") {
201
+ fnByName.set(parent.left.name, node);
202
+ localMap.set(parent.left.name, node);
203
+ } else if (parent?.type === "AssignmentExpression" && parent.left?.type === "MemberExpression" && !parent.left.computed && parent.left.property?.type === "Identifier") {
204
+ const propName = parent.left.property.name;
205
+ fnByName.set(propName, node);
206
+ localMap.set(propName, node);
207
+ if (parent.left.object?.type === "Identifier") {
208
+ const qual = `${parent.left.object.name}.${propName}`;
209
+ fnByName.set(qual, node);
210
+ localMap.set(qual, node);
211
+ }
212
+ }
112
213
  const grandParent = ancestors[ancestors.length - 3];
113
214
  const greatGrandParent = ancestors[ancestors.length - 4];
114
215
  if (parent?.type === "Property" && parent.key?.type === "Identifier" && grandParent?.type === "ObjectExpression" && greatGrandParent?.type === "VariableDeclarator" && greatGrandParent.id?.type === "Identifier") {
@@ -118,7 +219,7 @@ function analyzeAstHotPaths(ast) {
118
219
  }
119
220
  const chain = [];
120
221
  for (let i = ancestors.length - 1; i >= 0; i--) {
121
- if (FUNCTION_NODE_TYPES.has(ancestors[i].type) || ancestors[i].type === "Program") {
222
+ if (ancestors[i] !== node && (FUNCTION_NODE_TYPES.has(ancestors[i].type) || ancestors[i].type === "Program")) {
122
223
  chain.push(ancestors[i]);
123
224
  }
124
225
  }
@@ -127,8 +228,13 @@ function analyzeAstHotPaths(ast) {
127
228
  hotFunctionNodes.add(node);
128
229
  directHotNodes.add(node);
129
230
  }
231
+ if (isZeroArgIifeFactoryFunction(node)) {
232
+ markSerializedFunction(node);
233
+ }
130
234
  }
131
235
  function resolveCalleeNode(callerNode, calleeKey) {
236
+ const directLocal = scopeFunctions.get(callerNode)?.get(calleeKey);
237
+ if (directLocal) return directLocal;
132
238
  const chain = fnScopeChain.get(callerNode);
133
239
  if (chain) {
134
240
  for (const scopeNode of chain) {
@@ -139,6 +245,25 @@ function analyzeAstHotPaths(ast) {
139
245
  }
140
246
  return fnByName.get(calleeKey);
141
247
  }
248
+ function queueSerializedTarget(target, enclosingScope) {
249
+ if (!target) return;
250
+ if (FUNCTION_NODE_TYPES.has(target.type)) {
251
+ markSerializedFunction(target);
252
+ } else if (target.type === "Identifier") {
253
+ pendingSerializedRefs.push({ scopeNode: enclosingScope, name: target.name });
254
+ } else if (target.type === "MemberExpression" && !target.computed && target.property?.type === "Identifier") {
255
+ pendingSerializedRefs.push({
256
+ scopeNode: enclosingScope,
257
+ name: target.property.name
258
+ });
259
+ if (target.object?.type === "Identifier") {
260
+ pendingSerializedRefs.push({
261
+ scopeNode: enclosingScope,
262
+ name: `${target.object.name}.${target.property.name}`
263
+ });
264
+ }
265
+ }
266
+ }
142
267
  walk.ancestor(ast, {
143
268
  FunctionDeclaration(node, _state, ancestors) {
144
269
  registerFunctionNode(node, ancestors);
@@ -149,6 +274,59 @@ function analyzeAstHotPaths(ast) {
149
274
  ArrowFunctionExpression(node, _state, ancestors) {
150
275
  registerFunctionNode(node, ancestors);
151
276
  },
277
+ VariableDeclarator(node, _state, ancestors) {
278
+ if (node.id?.type === "Identifier" && node.init?.type === "Identifier") {
279
+ const scopeNode = findEnclosingFunction(ancestors) ?? ast;
280
+ recordVarAlias(scopeNode, node.id.name, node.init.name);
281
+ }
282
+ },
283
+ AssignmentExpression(node, _state, ancestors) {
284
+ const left = node.left;
285
+ if (left?.type === "Identifier" && node.right?.type === "Identifier") {
286
+ const scopeNode = findEnclosingFunction(ancestors) ?? ast;
287
+ recordVarAlias(scopeNode, left.name, node.right.name);
288
+ }
289
+ const isBareOnMessage = left?.type === "Identifier" && left.name === "onmessage";
290
+ const isSelfOnMessage = left?.type === "MemberExpression" && !left.computed && left.object?.type === "Identifier" && left.object.name === "self" && left.property?.type === "Identifier" && left.property.name === "onmessage";
291
+ if (isBareOnMessage || isSelfOnMessage) {
292
+ const enclosingFn = findEnclosingFunction(ancestors);
293
+ if (enclosingFn) {
294
+ markSerializedFunction(enclosingFn);
295
+ }
296
+ const outerFn = findOutermostModuleFunction(ancestors);
297
+ if (outerFn) {
298
+ markSerializedFunction(outerFn);
299
+ }
300
+ const scopeNode = enclosingFn ?? ast;
301
+ queueSerializedTarget(node.right, scopeNode);
302
+ }
303
+ },
304
+ ObjectExpression(node, _state, ancestors) {
305
+ const props = node.properties || [];
306
+ const propMap = /* @__PURE__ */ new Map();
307
+ for (const p of props) {
308
+ if (p?.type === "Property" && !p.computed) {
309
+ const keyName = p.key?.type === "Identifier" ? p.key.name : p.key?.type === "Literal" ? String(p.key.value) : "";
310
+ if (keyName) {
311
+ propMap.set(keyName, p.value);
312
+ }
313
+ }
314
+ }
315
+ const hasWorkerDescriptorShape = propMap.has("init") && propMap.has("dependencies") || propMap.has("isWorkerModule") || propMap.has("getTransferables") && (propMap.has("init") || propMap.has("dependencies"));
316
+ if (hasWorkerDescriptorShape) {
317
+ const scopeNode = findEnclosingFunction(ancestors) ?? ast;
318
+ for (const key of ["init", "getTransferables", "worker"]) {
319
+ const val = propMap.get(key);
320
+ if (val) queueSerializedTarget(val, scopeNode);
321
+ }
322
+ const deps = propMap.get("dependencies");
323
+ if (deps?.type === "ArrayExpression") {
324
+ for (const el of deps.elements || []) {
325
+ if (el) queueSerializedTarget(el, scopeNode);
326
+ }
327
+ }
328
+ }
329
+ },
152
330
  CallExpression(node, _state, ancestors) {
153
331
  const callee = node.callee;
154
332
  let calleeName = "";
@@ -165,38 +343,76 @@ function analyzeAstHotPaths(ast) {
165
343
  qualifiedCall = memberProp;
166
344
  }
167
345
  }
346
+ const enclosingFn = findEnclosingFunction(ancestors);
347
+ const currentScope = enclosingFn ?? ast;
168
348
  if (qualifiedCall || memberProp) {
169
- for (let i = ancestors.length - 1; i >= 0; i--) {
170
- if (FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
171
- let set = callerToCallees.get(ancestors[i]);
172
- if (!set) {
173
- set = /* @__PURE__ */ new Set();
174
- callerToCallees.set(ancestors[i], set);
175
- }
176
- if (qualifiedCall) set.add(qualifiedCall);
177
- if (memberProp && !ARRAY_HOF_METHODS.has(memberProp)) {
178
- set.add(memberProp);
179
- }
180
- break;
349
+ allCallSites.push({
350
+ scopeNode: currentScope,
351
+ calleeKey: qualifiedCall || memberProp,
352
+ args: node.arguments || []
353
+ });
354
+ if (enclosingFn) {
355
+ let set = callerToCallees.get(enclosingFn);
356
+ if (!set) {
357
+ set = /* @__PURE__ */ new Set();
358
+ callerToCallees.set(enclosingFn, set);
359
+ }
360
+ if (qualifiedCall) set.add(qualifiedCall);
361
+ if (memberProp && !ARRAY_HOF_METHODS.has(memberProp)) {
362
+ set.add(memberProp);
181
363
  }
182
364
  }
183
365
  }
366
+ if (memberProp === "toString" && (!node.arguments || node.arguments.length === 0) && callee?.type === "MemberExpression") {
367
+ const targetObj = callee.object;
368
+ queueSerializedTarget(targetObj, currentScope);
369
+ if (targetObj?.type === "Identifier" && enclosingFn) {
370
+ const resolvedName = resolveAliasTarget(enclosingFn, targetObj.name);
371
+ if (ensureFnParams(enclosingFn).has(resolvedName)) {
372
+ serializerHelperNodes.add(enclosingFn);
373
+ }
374
+ if (resolvedName !== targetObj.name) {
375
+ queueSerializedTarget({ type: "Identifier", name: resolvedName }, currentScope);
376
+ }
377
+ }
378
+ }
379
+ if ((memberProp === "call" || memberProp === "apply") && callee?.type === "MemberExpression" && callee.object?.type === "MemberExpression" && callee.object.property?.type === "Identifier" && callee.object.property.name === "toString" && node.arguments?.length > 0) {
380
+ const firstArg = node.arguments[0];
381
+ queueSerializedTarget(firstArg, currentScope);
382
+ if (firstArg?.type === "Identifier" && enclosingFn) {
383
+ const resolvedName = resolveAliasTarget(enclosingFn, firstArg.name);
384
+ if (ensureFnParams(enclosingFn).has(resolvedName)) {
385
+ serializerHelperNodes.add(enclosingFn);
386
+ }
387
+ if (resolvedName !== firstArg.name) {
388
+ queueSerializedTarget({ type: "Identifier", name: resolvedName }, currentScope);
389
+ }
390
+ }
391
+ }
392
+ if (calleeName === "importScripts" || memberProp === "importScripts" && callee?.object?.type === "Identifier" && callee.object.name === "self") {
393
+ if (enclosingFn) markSerializedFunction(enclosingFn);
394
+ const outerFn = findOutermostModuleFunction(ancestors);
395
+ if (outerFn) markSerializedFunction(outerFn);
396
+ }
184
397
  if (ARRAY_HOF_METHODS.has(memberProp) || calleeName === "requestAnimationFrame" || memberProp === "requestAnimationFrame" || calleeName === "cancelAnimationFrame" || memberProp === "cancelAnimationFrame") {
185
398
  for (const arg of node.arguments || []) {
186
399
  if (FUNCTION_NODE_TYPES.has(arg.type)) {
187
400
  hotFunctionNodes.add(arg);
188
401
  directHotNodes.add(arg);
189
402
  hotExemptNodes.add(arg);
403
+ } else if (arg?.type === "Identifier") {
404
+ allCallSites.push({
405
+ scopeNode: currentScope,
406
+ calleeKey: arg.name,
407
+ args: [arg]
408
+ });
190
409
  }
191
410
  }
192
411
  }
193
412
  if (UI_HOOK_OR_RENDER_CALLS.has(calleeName) || UI_HOOK_OR_RENDER_CALLS.has(memberProp)) {
194
- for (let i = ancestors.length - 1; i >= 0; i--) {
195
- if (FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
196
- hotFunctionNodes.add(ancestors[i]);
197
- directHotNodes.add(ancestors[i]);
198
- break;
199
- }
413
+ if (enclosingFn) {
414
+ hotFunctionNodes.add(enclosingFn);
415
+ directHotNodes.add(enclosingFn);
200
416
  }
201
417
  }
202
418
  },
@@ -221,10 +437,41 @@ function analyzeAstHotPaths(ast) {
221
437
  markEnclosingFunctionHot(ancestors, hotFunctionNodes, directHotNodes);
222
438
  }
223
439
  });
440
+ for (const callSite of allCallSites) {
441
+ const resolvedCallee = resolveCalleeNode(callSite.scopeNode, callSite.calleeKey);
442
+ if (resolvedCallee && serializerHelperNodes.has(resolvedCallee)) {
443
+ for (const arg of callSite.args) {
444
+ queueSerializedTarget(arg, callSite.scopeNode);
445
+ }
446
+ }
447
+ }
448
+ for (const ref of pendingSerializedRefs) {
449
+ const targetNode = resolveCalleeNode(ref.scopeNode, ref.name);
450
+ if (targetNode) {
451
+ markSerializedFunction(targetNode);
452
+ }
453
+ }
224
454
  const forwardHotSet = new Set(directHotNodes);
225
455
  let forwardChanged = true;
226
456
  while (forwardChanged) {
227
457
  forwardChanged = false;
458
+ for (const fn of allFunctions) {
459
+ if (forwardHotSet.has(fn.node)) continue;
460
+ const chain = fnScopeChain.get(fn.node);
461
+ if (!chain) continue;
462
+ for (const anc of chain) {
463
+ if (anc !== fn.node && (serializedFunctionNodes.has(anc) || forwardHotSet.has(anc))) {
464
+ forwardHotSet.add(fn.node);
465
+ hotFunctionNodes.add(fn.node);
466
+ hotExemptNodes.add(fn.node);
467
+ if (serializedFunctionNodes.has(anc)) {
468
+ serializedFunctionNodes.add(fn.node);
469
+ }
470
+ forwardChanged = true;
471
+ break;
472
+ }
473
+ }
474
+ }
228
475
  for (const callerNode of Array.from(forwardHotSet)) {
229
476
  const calleeNames = callerToCallees.get(callerNode);
230
477
  if (!calleeNames) continue;
@@ -234,6 +481,9 @@ function analyzeAstHotPaths(ast) {
234
481
  forwardHotSet.add(targetNode);
235
482
  hotFunctionNodes.add(targetNode);
236
483
  hotExemptNodes.add(targetNode);
484
+ if (serializedFunctionNodes.has(callerNode)) {
485
+ serializedFunctionNodes.add(targetNode);
486
+ }
237
487
  forwardChanged = true;
238
488
  }
239
489
  }
@@ -912,8 +1162,13 @@ function createOpaqueGuardStatement(seed, fnIndex) {
912
1162
  right: { type: "Literal", value: wrongVal, raw: String(wrongVal) }
913
1163
  },
914
1164
  consequent: {
915
- type: "ReturnStatement",
916
- argument: null
1165
+ type: "ExpressionStatement",
1166
+ expression: {
1167
+ type: "UnaryExpression",
1168
+ operator: "void",
1169
+ prefix: true,
1170
+ argument: { type: "Literal", value: 0, raw: "0" }
1171
+ }
917
1172
  },
918
1173
  alternate: null
919
1174
  };