@hyperframes/parsers 0.8.205 → 0.8.207

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.
@@ -687,43 +687,152 @@ function parseProgram(script) {
687
687
  }
688
688
  function literalString(node) {
689
689
  if (node?.type === "Literal" && typeof node.value === "string") return node.value;
690
- if (node?.type === "TemplateLiteral" && node.expressions?.length === 0) {
691
- return node.quasis?.[0]?.value?.cooked;
690
+ if (node?.type === "TemplateLiteral" && node.expressions.length === 0) {
691
+ return node.quasis[0]?.value.cooked ?? void 0;
692
692
  }
693
693
  return void 0;
694
694
  }
695
695
  function memberName(node) {
696
696
  if (node?.type !== "MemberExpression") return void 0;
697
- if (!node.computed && node.property?.type === "Identifier") return node.property.name;
697
+ if (!node.computed && node.property.type === "Identifier") return node.property.name;
698
698
  return literalString(node.property);
699
699
  }
700
- function lookupSelector(node, bindings) {
701
- if (node?.type === "Identifier") return bindings.get(node.name);
702
- if (node?.type !== "CallExpression" || node.callee?.type !== "MemberExpression") {
700
+ function isFunctionScope(node) {
701
+ return node.type === "Program" || node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression" || node.type === "StaticBlock";
702
+ }
703
+ function isScope(node) {
704
+ return isFunctionScope(node) || node.type === "BlockStatement" || node.type === "CatchClause" || node.type === "ForStatement" || node.type === "ForInStatement" || node.type === "ForOfStatement" || node.type === "SwitchStatement" || node.type === "ClassDeclaration" || node.type === "ClassExpression";
705
+ }
706
+ function declarePattern(pattern, initializer, scope, initializerScope = scope) {
707
+ switch (pattern.type) {
708
+ case "Identifier":
709
+ if (initializer || !scope.bindings.has(pattern.name)) {
710
+ scope.bindings.set(pattern.name, { initializer, scope: initializerScope });
711
+ }
712
+ break;
713
+ case "RestElement":
714
+ declarePattern(pattern.argument, null, scope);
715
+ break;
716
+ case "AssignmentPattern":
717
+ declarePattern(pattern.left, null, scope);
718
+ break;
719
+ case "ArrayPattern":
720
+ case "ObjectPattern":
721
+ declarePatterns(destructuredPatterns(pattern), scope);
722
+ break;
723
+ }
724
+ }
725
+ function declarePatterns(patterns, scope) {
726
+ for (const pattern of patterns) declarePattern(pattern, null, scope);
727
+ }
728
+ function destructuredPatterns(pattern) {
729
+ if (pattern.type === "ObjectPattern") {
730
+ return pattern.properties.map(
731
+ (property) => property.type === "RestElement" ? property.argument : property.value
732
+ );
733
+ }
734
+ return pattern.elements.filter((element) => element !== null);
735
+ }
736
+ function declareOptionalPattern(pattern, scope) {
737
+ if (pattern) declarePattern(pattern, null, scope);
738
+ }
739
+ function declareScopedName(node, scope) {
740
+ switch (node.type) {
741
+ case "FunctionExpression":
742
+ case "FunctionDeclaration":
743
+ declareOptionalPattern(node.id, scope);
744
+ declarePatterns(node.params, scope);
745
+ break;
746
+ case "ArrowFunctionExpression":
747
+ declarePatterns(node.params, scope);
748
+ break;
749
+ case "ClassDeclaration":
750
+ case "ClassExpression":
751
+ declareOptionalPattern(node.id, scope);
752
+ break;
753
+ case "CatchClause":
754
+ declareOptionalPattern(node.param, scope);
755
+ break;
756
+ case "ImportDeclaration":
757
+ declarePatterns(
758
+ node.specifiers.map((specifier) => specifier.local),
759
+ scope
760
+ );
761
+ break;
762
+ }
763
+ }
764
+ function lookupBinding(name, scope) {
765
+ for (let current = scope; current; current = current.parent) {
766
+ const binding = current.bindings.get(name);
767
+ if (binding) return binding;
768
+ }
769
+ return void 0;
770
+ }
771
+ function collectBindings(ast) {
772
+ const root = { bindings: /* @__PURE__ */ new Map() };
773
+ const scopes = new WeakMap([[ast, root]]);
774
+ const scopeAt = (ancestors, functionOnly = false) => {
775
+ let parent = root;
776
+ let selected = root;
777
+ for (const node of ancestors) {
778
+ if (!isScope(node)) continue;
779
+ let scope = scopes.get(node);
780
+ if (!scope) {
781
+ scope = { parent, bindings: /* @__PURE__ */ new Map() };
782
+ scopes.set(node, scope);
783
+ }
784
+ parent = scope;
785
+ if (!functionOnly || isFunctionScope(node)) selected = scope;
786
+ }
787
+ return selected;
788
+ };
789
+ acornWalk2.fullAncestor(ast, (node, _state, ancestors) => {
790
+ const scope = scopeAt(ancestors);
791
+ if (node.type === "VariableDeclarator") {
792
+ const declaration = ancestors.at(-2);
793
+ const bindingScope = scopeAt(
794
+ ancestors,
795
+ declaration?.type === "VariableDeclaration" && declaration.kind === "var"
796
+ );
797
+ declarePattern(node.id, node.init ?? null, bindingScope, scope);
798
+ return;
799
+ }
800
+ if ((node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") && node.id) {
801
+ declarePattern(node.id, null, scopeAt(ancestors.slice(0, -1)));
802
+ }
803
+ declareScopedName(node, scope);
804
+ });
805
+ return scopeAt;
806
+ }
807
+ function lookupSelector(node, scope, seen = /* @__PURE__ */ new Set()) {
808
+ if (node?.type === "Identifier") {
809
+ const binding = lookupBinding(node.name, scope);
810
+ if (!binding || seen.has(binding)) return void 0;
811
+ seen.add(binding);
812
+ return lookupSelector(binding.initializer, binding.scope, seen);
813
+ }
814
+ return selectorFromDomLookup(node);
815
+ }
816
+ function selectorFromDomLookup(node) {
817
+ if (node?.type !== "CallExpression" || node.callee.type !== "MemberExpression") {
703
818
  return void 0;
704
819
  }
705
820
  const method = memberName(node.callee);
706
821
  if (method !== "getElementById" && method !== "querySelector") return void 0;
707
- const value = literalString(node.arguments?.[0]);
822
+ const value = literalString(node.arguments[0]);
708
823
  if (!value) return void 0;
709
824
  return method === "getElementById" ? `#${value}` : value;
710
825
  }
711
826
  function extractMediaSrcMutations(script) {
712
827
  try {
713
828
  const ast = parseProgram(script);
714
- const bindings = /* @__PURE__ */ new Map();
715
- acornWalk2.simple(ast, {
716
- VariableDeclarator(node) {
717
- if (node.id?.type !== "Identifier") return;
718
- const selector = lookupSelector(node.init, bindings);
719
- if (selector) bindings.set(node.id.name, selector);
720
- }
721
- });
829
+ const scopeAt = collectBindings(ast);
722
830
  const mutations = [];
723
- acornWalk2.simple(ast, {
724
- AssignmentExpression(node) {
725
- if (node.operator !== "=" || memberName(node.left) !== "src") return;
726
- const selector = lookupSelector(node.left.object, bindings);
831
+ acornWalk2.ancestor(ast, {
832
+ AssignmentExpression(node, _state, ancestors) {
833
+ if (node.operator !== "=" || node.left.type !== "MemberExpression" || memberName(node.left) !== "src")
834
+ return;
835
+ const selector = lookupSelector(node.left.object, scopeAt(ancestors));
727
836
  if (!selector) return;
728
837
  mutations.push({
729
838
  selector,
@@ -731,10 +840,11 @@ function extractMediaSrcMutations(script) {
731
840
  raw: script.slice(node.start, node.end)
732
841
  });
733
842
  },
734
- CallExpression(node) {
735
- if (memberName(node.callee) !== "setAttribute") return;
736
- if (literalString(node.arguments?.[0])?.toLowerCase() !== "src") return;
737
- const selector = lookupSelector(node.callee.object, bindings);
843
+ CallExpression(node, _state, ancestors) {
844
+ if (node.callee.type !== "MemberExpression" || memberName(node.callee) !== "setAttribute")
845
+ return;
846
+ if (literalString(node.arguments[0])?.toLowerCase() !== "src") return;
847
+ const selector = lookupSelector(node.callee.object, scopeAt(ancestors));
738
848
  if (!selector) return;
739
849
  mutations.push({
740
850
  selector,
@@ -1,2 +1,2 @@
1
- export { B as BUILTIN_VAR_KEYS, D as DROPPED_VAR_KEYS, E as EXTRAS_KEYS, u as GSAP_DEFAULT_DURATION, H as HoldScope, f as PROPERTY_GROUPS, g as PropertyGroupName, S as SUPPORTED_EASES, h as SUPPORTED_PROPS, i as classifyPropertyGroup, j as classifyTweenPropertyGroup, x as holdScope, y as isTweenConfigKey, z as isXYPositionWrite, C as keyframeHoldForAnimation } from './gsapSerialize-DTDYI-MO.js';
1
+ export { B as BUILTIN_VAR_KEYS, D as DROPPED_VAR_KEYS, E as EXTRAS_KEYS, u as GSAP_DEFAULT_DURATION, H as HoldScope, f as PROPERTY_GROUPS, g as PropertyGroupName, S as SUPPORTED_EASES, h as SUPPORTED_PROPS, i as classifyPropertyGroup, j as classifyTweenPropertyGroup, x as holdScope, y as isTweenConfigKey, z as isXYPositionWrite, C as keyframeHoldForAnimation } from './gsapSerialize-CvtYLHBX.js';
2
2
  import './types-CdT7qqke.js';
@@ -1,5 +1,5 @@
1
- import { G as GsapAnimation, P as ParsedGsap, A as ArcPathConfig, a as ArcPathSegment } from './gsapSerialize-DTDYI-MO.js';
2
- export { b as GsapKeyframeFormat, c as GsapKeyframesData, d as GsapMethod, e as GsapPercentageKeyframe, f as PROPERTY_GROUPS, g as PropertyGroupName, S as SUPPORTED_EASES, h as SUPPORTED_PROPS, i as classifyPropertyGroup, j as classifyTweenPropertyGroup, k as getAnimationsForElementId, l as gsapAnimationsToKeyframes, m as keyframesToGsapAnimations, s as serializeGsapAnimations, v as validateCompositionGsap } from './gsapSerialize-DTDYI-MO.js';
1
+ import { G as GsapAnimation, P as ParsedGsap, A as ArcPathConfig, a as ArcPathSegment } from './gsapSerialize-CvtYLHBX.js';
2
+ export { b as GsapKeyframeFormat, c as GsapKeyframesData, d as GsapMethod, e as GsapPercentageKeyframe, f as PROPERTY_GROUPS, g as PropertyGroupName, S as SUPPORTED_EASES, h as SUPPORTED_PROPS, i as classifyPropertyGroup, j as classifyTweenPropertyGroup, k as getAnimationsForElementId, l as gsapAnimationsToKeyframes, m as keyframesToGsapAnimations, s as serializeGsapAnimations, v as validateCompositionGsap } from './gsapSerialize-CvtYLHBX.js';
3
3
  export { SPRING_PRESETS, SpringPreset, generateSpringEaseData } from './springEase.js';
4
4
  import './types-CdT7qqke.js';
5
5
 
@@ -251,7 +251,7 @@ function serializeGsapAnimations(animations, timelineVar = "tl", options) {
251
251
  const mediaTime = time - start;
252
252
  if (time >= start && time < end) {
253
253
  if (Math.abs(media.currentTime - mediaTime) > 0.1) {
254
- media.currentTime = mediaTime;
254
+ media.currentTime = mediaTime + ${Number(options.mediaElementLeadSeconds ?? 0)};
255
255
  }
256
256
  if (media.paused && !${timelineVar}.paused()) {
257
257
  media.play().catch(function() {});
@@ -1,5 +1,5 @@
1
- import { P as ParsedGsap, d as GsapMethod, G as GsapAnimation } from './gsapSerialize-DTDYI-MO.js';
2
- export { A as ArcPathConfig, a as ArcPathSegment, n as GsapProvenance, o as GsapProvenanceKind, K as KeyframeEditability, M as MotionPathShape, p as buildArcPath, q as editabilityForProvenance } from './gsapSerialize-DTDYI-MO.js';
1
+ import { P as ParsedGsap, d as GsapMethod, G as GsapAnimation } from './gsapSerialize-CvtYLHBX.js';
2
+ export { A as ArcPathConfig, a as ArcPathSegment, n as GsapProvenance, o as GsapProvenanceKind, K as KeyframeEditability, M as MotionPathShape, p as buildArcPath, q as editabilityForProvenance } from './gsapSerialize-CvtYLHBX.js';
3
3
  import './types-CdT7qqke.js';
4
4
 
5
5
  /**
@@ -1,4 +1,4 @@
1
- export { A as ArcPathConfig, a as ArcPathSegment, u as GSAP_DEFAULT_DURATION, G as GsapAnimation, c as GsapKeyframesData, d as GsapMethod, e as GsapPercentageKeyframe, n as GsapProvenance, o as GsapProvenanceKind, K as KeyframeEditability, f as PROPERTY_GROUPS, P as ParsedGsap, g as PropertyGroupName, S as SUPPORTED_EASES, h as SUPPORTED_PROPS, w as SourcedGsapPercentageKeyframe, r as SplitAnimationsOptions, t as SplitAnimationsResult, i as classifyPropertyGroup, j as classifyTweenPropertyGroup, q as editabilityForProvenance, k as getAnimationsForElementId, l as gsapAnimationsToKeyframes, m as keyframesToGsapAnimations, s as serializeGsapAnimations, v as validateCompositionGsap } from './gsapSerialize-DTDYI-MO.js';
1
+ export { A as ArcPathConfig, a as ArcPathSegment, u as GSAP_DEFAULT_DURATION, G as GsapAnimation, c as GsapKeyframesData, d as GsapMethod, e as GsapPercentageKeyframe, n as GsapProvenance, o as GsapProvenanceKind, K as KeyframeEditability, f as PROPERTY_GROUPS, P as ParsedGsap, g as PropertyGroupName, S as SUPPORTED_EASES, h as SUPPORTED_PROPS, w as SourcedGsapPercentageKeyframe, r as SplitAnimationsOptions, t as SplitAnimationsResult, i as classifyPropertyGroup, j as classifyTweenPropertyGroup, q as editabilityForProvenance, k as getAnimationsForElementId, l as gsapAnimationsToKeyframes, m as keyframesToGsapAnimations, s as serializeGsapAnimations, v as validateCompositionGsap } from './gsapSerialize-CvtYLHBX.js';
2
2
  export { isStudioHoldSet } from './gsapParser.js';
3
3
  export { SPRING_PRESETS, SpringPreset, generateSpringEaseData } from './springEase.js';
4
4
  export { parseGsapScriptAcorn as parseGsapScript } from './gsapParserAcorn.js';
@@ -218,7 +218,7 @@ function serializeGsapAnimations(animations, timelineVar = "tl", options) {
218
218
  const mediaTime = time - start;
219
219
  if (time >= start && time < end) {
220
220
  if (Math.abs(media.currentTime - mediaTime) > 0.1) {
221
- media.currentTime = mediaTime;
221
+ media.currentTime = mediaTime + ${Number(options.mediaElementLeadSeconds ?? 0)};
222
222
  }
223
223
  if (media.paused && !${timelineVar}.paused()) {
224
224
  media.play().catch(function() {});
@@ -209,6 +209,7 @@ interface SplitAnimationsResult {
209
209
  */
210
210
  declare function serializeGsapAnimations(animations: GsapAnimation[], timelineVar?: string, options?: {
211
211
  includeMediaSync?: boolean;
212
+ mediaElementLeadSeconds?: number;
212
213
  preamble?: string;
213
214
  postamble?: string;
214
215
  }): string;
@@ -1,4 +1,4 @@
1
- import { G as GsapAnimation, A as ArcPathConfig, r as SplitAnimationsOptions, t as SplitAnimationsResult, a as ArcPathSegment } from './gsapSerialize-DTDYI-MO.js';
1
+ import { G as GsapAnimation, A as ArcPathConfig, r as SplitAnimationsOptions, t as SplitAnimationsResult, a as ArcPathSegment } from './gsapSerialize-CvtYLHBX.js';
2
2
  import './types-CdT7qqke.js';
3
3
 
4
4
  type TweenTarget = Pick<GsapAnimation, "targetSelector" | "hasPartialSelector">;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { C as CompositionVariable, T as TimelineElement, a as CanvasResolution, K as Keyframe, S as StageZoomKeyframe, V as ValidationResult } from './types-CdT7qqke.js';
2
2
  export { A as AddElementData, b as Asset, B as BooleanVariable, c as CANVAS_DIMENSIONS, d as COMPOSITION_VARIABLE_TYPES, e as ColorVariable, f as CompositionAPI, g as CompositionAsset, h as CompositionSpec, i as CompositionVariableBase, j as CompositionVariableType, D as DEFAULT_DURATIONS, E as ElementKeyframes, k as EnumVariable, F as FontValue, l as FontVariable, I as ImageValue, m as ImageVariable, n as KeyframeProperties, M as MediaElementType, o as MediaFile, N as NumberVariable, P as PlayerAPI, R as ResolvedResolutionFlag, p as StageZoom, q as StringVariable, r as TIMELINE_COLORS, s as TimelineCompositionElement, t as TimelineElementBase, u as TimelineElementType, v as TimelineMediaElement, w as TimelineTextElement, x as VALID_CANVAS_RESOLUTIONS, W as WaveformData, y as getDefaultStageZoom, z as isAspectAgnosticResolutionAlias, G as isCompositionElement, H as isMediaElement, J as isTextElement, L as normalizeResolutionFlag, O as resolveResolutionFlagPair } from './types-CdT7qqke.js';
3
- export { A as ArcPathConfig, a as ArcPathSegment, u as GSAP_DEFAULT_DURATION, G as GsapAnimation, c as GsapKeyframesData, d as GsapMethod, e as GsapPercentageKeyframe, n as GsapProvenance, o as GsapProvenanceKind, K as KeyframeEditability, f as PROPERTY_GROUPS, P as ParsedGsap, g as PropertyGroupName, S as SUPPORTED_EASES, h as SUPPORTED_PROPS, w as SourcedGsapPercentageKeyframe, r as SplitAnimationsOptions, t as SplitAnimationsResult, i as classifyPropertyGroup, j as classifyTweenPropertyGroup, q as editabilityForProvenance, k as getAnimationsForElementId, l as gsapAnimationsToKeyframes, m as keyframesToGsapAnimations, s as serializeGsapAnimations, v as validateCompositionGsap } from './gsapSerialize-DTDYI-MO.js';
3
+ export { A as ArcPathConfig, a as ArcPathSegment, u as GSAP_DEFAULT_DURATION, G as GsapAnimation, c as GsapKeyframesData, d as GsapMethod, e as GsapPercentageKeyframe, n as GsapProvenance, o as GsapProvenanceKind, K as KeyframeEditability, f as PROPERTY_GROUPS, P as ParsedGsap, g as PropertyGroupName, S as SUPPORTED_EASES, h as SUPPORTED_PROPS, w as SourcedGsapPercentageKeyframe, r as SplitAnimationsOptions, t as SplitAnimationsResult, i as classifyPropertyGroup, j as classifyTweenPropertyGroup, q as editabilityForProvenance, k as getAnimationsForElementId, l as gsapAnimationsToKeyframes, m as keyframesToGsapAnimations, s as serializeGsapAnimations, v as validateCompositionGsap } from './gsapSerialize-CvtYLHBX.js';
4
4
  export { isStudioHoldSet } from './gsapParser.js';
5
5
  export { getObjectArrayKeyframeTiming } from './gsapParserExports.js';
6
6
  export { SPRING_PRESETS, SpringPreset, generateSpringEaseData } from './springEase.js';
package/dist/index.js CHANGED
@@ -322,7 +322,7 @@ function serializeGsapAnimations(animations, timelineVar = "tl", options) {
322
322
  const mediaTime = time - start;
323
323
  if (time >= start && time < end) {
324
324
  if (Math.abs(media.currentTime - mediaTime) > 0.1) {
325
- media.currentTime = mediaTime;
325
+ media.currentTime = mediaTime + ${Number(options.mediaElementLeadSeconds ?? 0)};
326
326
  }
327
327
  if (media.paused && !${timelineVar}.paused()) {
328
328
  media.play().catch(function() {});
@@ -4348,43 +4348,152 @@ function parseProgram2(script) {
4348
4348
  }
4349
4349
  function literalString(node) {
4350
4350
  if (node?.type === "Literal" && typeof node.value === "string") return node.value;
4351
- if (node?.type === "TemplateLiteral" && node.expressions?.length === 0) {
4352
- return node.quasis?.[0]?.value?.cooked;
4351
+ if (node?.type === "TemplateLiteral" && node.expressions.length === 0) {
4352
+ return node.quasis[0]?.value.cooked ?? void 0;
4353
4353
  }
4354
4354
  return void 0;
4355
4355
  }
4356
4356
  function memberName(node) {
4357
4357
  if (node?.type !== "MemberExpression") return void 0;
4358
- if (!node.computed && node.property?.type === "Identifier") return node.property.name;
4358
+ if (!node.computed && node.property.type === "Identifier") return node.property.name;
4359
4359
  return literalString(node.property);
4360
4360
  }
4361
- function lookupSelector(node, bindings) {
4362
- if (node?.type === "Identifier") return bindings.get(node.name);
4363
- if (node?.type !== "CallExpression" || node.callee?.type !== "MemberExpression") {
4361
+ function isFunctionScope(node) {
4362
+ return node.type === "Program" || node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression" || node.type === "StaticBlock";
4363
+ }
4364
+ function isScope(node) {
4365
+ return isFunctionScope(node) || node.type === "BlockStatement" || node.type === "CatchClause" || node.type === "ForStatement" || node.type === "ForInStatement" || node.type === "ForOfStatement" || node.type === "SwitchStatement" || node.type === "ClassDeclaration" || node.type === "ClassExpression";
4366
+ }
4367
+ function declarePattern(pattern, initializer, scope, initializerScope = scope) {
4368
+ switch (pattern.type) {
4369
+ case "Identifier":
4370
+ if (initializer || !scope.bindings.has(pattern.name)) {
4371
+ scope.bindings.set(pattern.name, { initializer, scope: initializerScope });
4372
+ }
4373
+ break;
4374
+ case "RestElement":
4375
+ declarePattern(pattern.argument, null, scope);
4376
+ break;
4377
+ case "AssignmentPattern":
4378
+ declarePattern(pattern.left, null, scope);
4379
+ break;
4380
+ case "ArrayPattern":
4381
+ case "ObjectPattern":
4382
+ declarePatterns(destructuredPatterns(pattern), scope);
4383
+ break;
4384
+ }
4385
+ }
4386
+ function declarePatterns(patterns, scope) {
4387
+ for (const pattern of patterns) declarePattern(pattern, null, scope);
4388
+ }
4389
+ function destructuredPatterns(pattern) {
4390
+ if (pattern.type === "ObjectPattern") {
4391
+ return pattern.properties.map(
4392
+ (property) => property.type === "RestElement" ? property.argument : property.value
4393
+ );
4394
+ }
4395
+ return pattern.elements.filter((element) => element !== null);
4396
+ }
4397
+ function declareOptionalPattern(pattern, scope) {
4398
+ if (pattern) declarePattern(pattern, null, scope);
4399
+ }
4400
+ function declareScopedName(node, scope) {
4401
+ switch (node.type) {
4402
+ case "FunctionExpression":
4403
+ case "FunctionDeclaration":
4404
+ declareOptionalPattern(node.id, scope);
4405
+ declarePatterns(node.params, scope);
4406
+ break;
4407
+ case "ArrowFunctionExpression":
4408
+ declarePatterns(node.params, scope);
4409
+ break;
4410
+ case "ClassDeclaration":
4411
+ case "ClassExpression":
4412
+ declareOptionalPattern(node.id, scope);
4413
+ break;
4414
+ case "CatchClause":
4415
+ declareOptionalPattern(node.param, scope);
4416
+ break;
4417
+ case "ImportDeclaration":
4418
+ declarePatterns(
4419
+ node.specifiers.map((specifier) => specifier.local),
4420
+ scope
4421
+ );
4422
+ break;
4423
+ }
4424
+ }
4425
+ function lookupBinding(name, scope) {
4426
+ for (let current = scope; current; current = current.parent) {
4427
+ const binding = current.bindings.get(name);
4428
+ if (binding) return binding;
4429
+ }
4430
+ return void 0;
4431
+ }
4432
+ function collectBindings(ast) {
4433
+ const root = { bindings: /* @__PURE__ */ new Map() };
4434
+ const scopes = new WeakMap([[ast, root]]);
4435
+ const scopeAt = (ancestors, functionOnly = false) => {
4436
+ let parent = root;
4437
+ let selected = root;
4438
+ for (const node of ancestors) {
4439
+ if (!isScope(node)) continue;
4440
+ let scope = scopes.get(node);
4441
+ if (!scope) {
4442
+ scope = { parent, bindings: /* @__PURE__ */ new Map() };
4443
+ scopes.set(node, scope);
4444
+ }
4445
+ parent = scope;
4446
+ if (!functionOnly || isFunctionScope(node)) selected = scope;
4447
+ }
4448
+ return selected;
4449
+ };
4450
+ acornWalk5.fullAncestor(ast, (node, _state, ancestors) => {
4451
+ const scope = scopeAt(ancestors);
4452
+ if (node.type === "VariableDeclarator") {
4453
+ const declaration = ancestors.at(-2);
4454
+ const bindingScope = scopeAt(
4455
+ ancestors,
4456
+ declaration?.type === "VariableDeclaration" && declaration.kind === "var"
4457
+ );
4458
+ declarePattern(node.id, node.init ?? null, bindingScope, scope);
4459
+ return;
4460
+ }
4461
+ if ((node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") && node.id) {
4462
+ declarePattern(node.id, null, scopeAt(ancestors.slice(0, -1)));
4463
+ }
4464
+ declareScopedName(node, scope);
4465
+ });
4466
+ return scopeAt;
4467
+ }
4468
+ function lookupSelector(node, scope, seen = /* @__PURE__ */ new Set()) {
4469
+ if (node?.type === "Identifier") {
4470
+ const binding = lookupBinding(node.name, scope);
4471
+ if (!binding || seen.has(binding)) return void 0;
4472
+ seen.add(binding);
4473
+ return lookupSelector(binding.initializer, binding.scope, seen);
4474
+ }
4475
+ return selectorFromDomLookup(node);
4476
+ }
4477
+ function selectorFromDomLookup(node) {
4478
+ if (node?.type !== "CallExpression" || node.callee.type !== "MemberExpression") {
4364
4479
  return void 0;
4365
4480
  }
4366
4481
  const method = memberName(node.callee);
4367
4482
  if (method !== "getElementById" && method !== "querySelector") return void 0;
4368
- const value = literalString(node.arguments?.[0]);
4483
+ const value = literalString(node.arguments[0]);
4369
4484
  if (!value) return void 0;
4370
4485
  return method === "getElementById" ? `#${value}` : value;
4371
4486
  }
4372
4487
  function extractMediaSrcMutations(script) {
4373
4488
  try {
4374
4489
  const ast = parseProgram2(script);
4375
- const bindings = /* @__PURE__ */ new Map();
4376
- acornWalk5.simple(ast, {
4377
- VariableDeclarator(node) {
4378
- if (node.id?.type !== "Identifier") return;
4379
- const selector = lookupSelector(node.init, bindings);
4380
- if (selector) bindings.set(node.id.name, selector);
4381
- }
4382
- });
4490
+ const scopeAt = collectBindings(ast);
4383
4491
  const mutations = [];
4384
- acornWalk5.simple(ast, {
4385
- AssignmentExpression(node) {
4386
- if (node.operator !== "=" || memberName(node.left) !== "src") return;
4387
- const selector = lookupSelector(node.left.object, bindings);
4492
+ acornWalk5.ancestor(ast, {
4493
+ AssignmentExpression(node, _state, ancestors) {
4494
+ if (node.operator !== "=" || node.left.type !== "MemberExpression" || memberName(node.left) !== "src")
4495
+ return;
4496
+ const selector = lookupSelector(node.left.object, scopeAt(ancestors));
4388
4497
  if (!selector) return;
4389
4498
  mutations.push({
4390
4499
  selector,
@@ -4392,10 +4501,11 @@ function extractMediaSrcMutations(script) {
4392
4501
  raw: script.slice(node.start, node.end)
4393
4502
  });
4394
4503
  },
4395
- CallExpression(node) {
4396
- if (memberName(node.callee) !== "setAttribute") return;
4397
- if (literalString(node.arguments?.[0])?.toLowerCase() !== "src") return;
4398
- const selector = lookupSelector(node.callee.object, bindings);
4504
+ CallExpression(node, _state, ancestors) {
4505
+ if (node.callee.type !== "MemberExpression" || memberName(node.callee) !== "setAttribute")
4506
+ return;
4507
+ if (literalString(node.arguments[0])?.toLowerCase() !== "src") return;
4508
+ const selector = lookupSelector(node.callee.object, scopeAt(ancestors));
4399
4509
  if (!selector) return;
4400
4510
  mutations.push({
4401
4511
  selector,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperframes/parsers",
3
- "version": "0.8.205",
3
+ "version": "0.8.207",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/heygen-com/hyperframes",
@@ -135,7 +135,7 @@
135
135
  "tsx": "^4.21.0",
136
136
  "typescript": "^5.0.0",
137
137
  "vitest": "^4.1.11",
138
- "@hyperframes/core": "0.8.205"
138
+ "@hyperframes/core": "0.8.207"
139
139
  },
140
140
  "scripts": {
141
141
  "build": "tsup",