vsn 1.0.1 → 1.0.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/index.cjs CHANGED
@@ -586,27 +586,32 @@ var AssignmentNode = class extends BaseNode {
586
586
  }
587
587
  const value = await this.value.evaluate(context);
588
588
  if (this.operator !== "=") {
589
- return this.applyCompoundAssignment(context, value);
589
+ return await this.applyCompoundAssignment(context, value);
590
590
  }
591
591
  if (this.target instanceof IdentifierExpression && this.target.name.startsWith("root.") && context.rootScope) {
592
592
  const path = this.target.name.slice("root.".length);
593
- context.rootScope.setPath?.(`self.${path}`, value);
593
+ context.rootScope.setPath?.(path, value);
594
594
  return value;
595
595
  }
596
+ if (this.target instanceof MemberExpression || this.target instanceof IndexExpression) {
597
+ const resolved = await this.resolveAssignmentTarget(context);
598
+ if (resolved?.scope?.setPath) {
599
+ resolved.scope.setPath(resolved.path, value);
600
+ return value;
601
+ }
602
+ }
596
603
  this.assignTarget(context, this.target, value);
597
604
  return value;
598
605
  }
599
- applyCompoundAssignment(context, value) {
606
+ async applyCompoundAssignment(context, value) {
600
607
  if (!context.scope || !context.scope.setPath) {
601
608
  return void 0;
602
609
  }
603
- if (!(this.target instanceof IdentifierExpression)) {
604
- throw new Error("Compound assignment requires a simple identifier");
610
+ const resolved = await this.resolveAssignmentTarget(context);
611
+ if (!resolved) {
612
+ throw new Error("Compound assignment requires a simple identifier or member path");
605
613
  }
606
- const isRoot = this.target.name.startsWith("root.");
607
- const scope = isRoot && context.rootScope ? context.rootScope : context.scope;
608
- const rawPath = isRoot ? this.target.name.slice("root.".length) : this.target.name;
609
- const path = isRoot ? `self.${rawPath}` : rawPath;
614
+ const { scope, path } = resolved;
610
615
  const current = scope?.getPath ? scope.getPath(path) : void 0;
611
616
  let result;
612
617
  if (this.operator === "+=") {
@@ -621,6 +626,65 @@ var AssignmentNode = class extends BaseNode {
621
626
  scope?.setPath?.(path, result);
622
627
  return result;
623
628
  }
629
+ async resolveAssignmentTarget(context) {
630
+ if (this.target instanceof IdentifierExpression) {
631
+ const isRoot = this.target.name.startsWith("root.");
632
+ const rawPath = isRoot ? this.target.name.slice("root.".length) : this.target.name;
633
+ if (isRoot) {
634
+ return { scope: context.scope, path: `root.${rawPath}` };
635
+ }
636
+ return { scope: context.scope, path: rawPath };
637
+ }
638
+ if (this.target instanceof MemberExpression) {
639
+ const resolvedPath = this.target.getIdentifierPath();
640
+ if (!resolvedPath) {
641
+ return null;
642
+ }
643
+ const path = resolvedPath.path;
644
+ const isRoot = path.startsWith("root.");
645
+ const rawPath = isRoot ? path.slice("root.".length) : path;
646
+ if (isRoot) {
647
+ return { scope: context.scope, path: `root.${rawPath}` };
648
+ }
649
+ return { scope: context.scope, path: rawPath };
650
+ }
651
+ if (this.target instanceof IndexExpression) {
652
+ const path = await this.resolveIndexPath(context, this.target);
653
+ if (!path) {
654
+ return null;
655
+ }
656
+ const isRoot = path.startsWith("root.");
657
+ const rawPath = isRoot ? path.slice("root.".length) : path;
658
+ if (isRoot) {
659
+ return { scope: context.scope, path: `root.${rawPath}` };
660
+ }
661
+ return { scope: context.scope, path: rawPath };
662
+ }
663
+ return null;
664
+ }
665
+ async resolveIndexPath(context, expr) {
666
+ const base = await this.resolveTargetPath(context, expr.target);
667
+ if (!base) {
668
+ return null;
669
+ }
670
+ const indexValue = await expr.index.evaluate(context);
671
+ if (indexValue == null) {
672
+ return null;
673
+ }
674
+ return `${base}.${indexValue}`;
675
+ }
676
+ async resolveTargetPath(context, target) {
677
+ if (target instanceof IdentifierExpression) {
678
+ return target.name;
679
+ }
680
+ if (target instanceof MemberExpression) {
681
+ return target.getIdentifierPath()?.path ?? null;
682
+ }
683
+ if (target instanceof IndexExpression) {
684
+ return this.resolveIndexPath(context, target);
685
+ }
686
+ return null;
687
+ }
624
688
  assignTarget(context, target, value) {
625
689
  if (!context.scope || !context.scope.setPath) {
626
690
  return;
@@ -2347,7 +2411,14 @@ ${caret}`;
2347
2411
  return this.parseObjectPattern();
2348
2412
  }
2349
2413
  if (token.type === "Identifier" /* Identifier */) {
2350
- return new IdentifierExpression(this.parseIdentifierPath());
2414
+ const expr = this.parseCallExpression();
2415
+ if (expr instanceof CallExpression) {
2416
+ throw new Error("Invalid assignment target CallExpression");
2417
+ }
2418
+ if (expr instanceof IdentifierExpression || expr instanceof MemberExpression || expr instanceof IndexExpression) {
2419
+ return expr;
2420
+ }
2421
+ throw new Error("Invalid assignment target");
2351
2422
  }
2352
2423
  throw new Error(`Invalid assignment target ${token.type}`);
2353
2424
  }
@@ -2626,6 +2697,25 @@ ${caret}`;
2626
2697
  while (this.stream.peekNonWhitespace(index)?.type === "Dot" /* Dot */ && this.stream.peekNonWhitespace(index + 1)?.type === "Identifier" /* Identifier */) {
2627
2698
  index += 2;
2628
2699
  }
2700
+ while (this.stream.peekNonWhitespace(index)?.type === "LBracket" /* LBracket */) {
2701
+ let depth = 0;
2702
+ while (true) {
2703
+ const token = this.stream.peekNonWhitespace(index);
2704
+ if (!token) {
2705
+ return false;
2706
+ }
2707
+ if (token.type === "LBracket" /* LBracket */) {
2708
+ depth += 1;
2709
+ } else if (token.type === "RBracket" /* RBracket */) {
2710
+ depth -= 1;
2711
+ if (depth === 0) {
2712
+ index += 1;
2713
+ break;
2714
+ }
2715
+ }
2716
+ index += 1;
2717
+ }
2718
+ }
2629
2719
  return this.isAssignmentOperatorStart(index);
2630
2720
  }
2631
2721
  if (first.type === "At" /* At */ || first.type === "Dollar" /* Dollar */) {