@atscript/core 0.1.71 → 0.1.73

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
@@ -1030,7 +1030,16 @@ var AnnotationSpec = class {
1030
1030
  range: token.range
1031
1031
  });
1032
1032
  if (this.config.defType?.length) {
1033
- let def = mainToken.parentNode.getDefinition();
1033
+ const parentNode = mainToken.parentNode;
1034
+ let def = parentNode.getDefinition();
1035
+ if (!def && isRef(parentNode)) {
1036
+ const idToken = parentNode.token("identifier");
1037
+ const annotateBlock = idToken ? doc.annotateBlockAt(idToken.range.start.line, idToken.range.start.character) : undefined;
1038
+ if (annotateBlock) {
1039
+ const chain = [parentNode.id, ...parentNode.chain.map((c) => c.text)];
1040
+ def = doc.unwindType(annotateBlock.targetName, chain)?.def || def;
1041
+ }
1042
+ }
1034
1043
  if (isRef(def)) def = doc.unwindType(def.id, def.chain)?.def || def;
1035
1044
  let defEntity = def?.entity || "unknown";
1036
1045
  if (isInterface(def) || isStructure(def)) defEntity = "object";
@@ -3299,6 +3308,7 @@ var AtscriptDoc = class {
3299
3308
  this.imports.clear();
3300
3309
  this.resolvedImports.clear();
3301
3310
  this._allMessages = undefined;
3311
+ this._annotateNodesByTarget = undefined;
3302
3312
  this.tokensIndex = new TokensIndex();
3303
3313
  this.blocksIndex = new BlocksIndex();
3304
3314
  this.resolvedAnnotations = [];
@@ -3461,13 +3471,28 @@ else if (isStructure(def) || isInterface(def) || isPrimitive(def)) def = def.pro
3461
3471
  return right;
3462
3472
  }
3463
3473
  /**
3474
+ * If the position falls inside an `annotate` block, returns that block's node
3475
+ * (whose `targetName` is the annotated interface). Entry refs within the block
3476
+ * resolve through the target interface rather than as top-level definitions.
3477
+ */ annotateBlockAt(line, character) {
3478
+ const block$2 = this.blocksIndex.at(line, character);
3479
+ return block$2?.blockType === "annotate" && isAnnotate(block$2.parentNode) ? block$2.parentNode : undefined;
3480
+ }
3481
+ /**
3464
3482
  * Collects ad-hoc annotations from all `annotate` blocks targeting a given type/interface.
3465
3483
  * Returns a map of property path (dot-joined) → annotations array.
3466
3484
  * For entries without a chain (root-level), the key is the entry identifier.
3467
3485
  */ getAnnotateNodesFor(targetName) {
3468
- const result = [];
3469
- for (const node of this.nodes) if (isAnnotate(node) && node.targetName === targetName) result.push(node);
3470
- return result;
3486
+ if (!this._annotateNodesByTarget) {
3487
+ const index = new Map();
3488
+ for (const node of this.nodes) if (isAnnotate(node)) {
3489
+ const existing = index.get(node.targetName);
3490
+ if (existing) existing.push(node);
3491
+ else index.set(node.targetName, [node]);
3492
+ }
3493
+ this._annotateNodesByTarget = index;
3494
+ }
3495
+ return this._annotateNodesByTarget.get(targetName) ?? [];
3471
3496
  }
3472
3497
  /**
3473
3498
  * Resolves an interface with `extends` by merging parent structures with own props.
@@ -3522,45 +3547,81 @@ else {
3522
3547
  }
3523
3548
  /**
3524
3549
  * Returns a structure equivalent to `struct` with annotate-block contributions
3525
- * folded into matching props' annotation arrays. Skips chained entries
3526
- * (e.g. `annotate Parent { user.address.city }`) — only root-level entries apply.
3527
- * Original prop nodes are reused by reference when no extra annotations apply.
3550
+ * folded into matching props' annotation arrays. Chained entries
3551
+ * (e.g. `annotate Parent { account.active }`) are folded recursively into the
3552
+ * matching inline-object prop's nested structure. Only props/structures along a
3553
+ * touched path are cloned — siblings are reused by reference, so the base's own
3554
+ * (shared) nodes are never mutated.
3528
3555
  */ applyAnnotateBlocksToStructure(struct, annotateNodes) {
3529
3556
  if (!isStructure(struct)) return struct;
3530
- const annotationsByProp = new Map();
3557
+ const entries = [];
3531
3558
  for (const annotateNode of annotateNodes) for (const entry of annotateNode.entries) {
3532
- if (entry.hasChain) continue;
3533
- const propName$1 = entry.id;
3534
3559
  const anns = entry.annotations;
3535
- if (!propName$1 || !anns || anns.length === 0) continue;
3536
- const existing = annotationsByProp.get(propName$1);
3537
- annotationsByProp.set(propName$1, existing ? [...existing, ...anns] : [...anns]);
3560
+ if (!entry.id || !anns || anns.length === 0) continue;
3561
+ const parts = entry.hasChain ? [entry.id, ...entry.chain.map((c) => c.text)] : [entry.id];
3562
+ entries.push({
3563
+ parts,
3564
+ anns
3565
+ });
3566
+ }
3567
+ if (entries.length === 0) return struct;
3568
+ return this.foldAnnotationsIntoStructure(struct, entries) ?? struct;
3569
+ }
3570
+ /**
3571
+ * Recursively folds annotate-block entries into a structure's props. Each entry's
3572
+ * `parts[0]` names a prop in `struct`; remaining parts target a nested inline-object
3573
+ * prop. Returns a new structure with cloned props/structures along touched paths, or
3574
+ * `undefined` when nothing matched (so callers can reuse the original by reference).
3575
+ */ foldAnnotationsIntoStructure(struct, entries) {
3576
+ const byHead = new Map();
3577
+ for (const entry of entries) {
3578
+ const head = entry.parts[0];
3579
+ let group$1 = byHead.get(head);
3580
+ if (!group$1) {
3581
+ group$1 = {
3582
+ own: [],
3583
+ nested: []
3584
+ };
3585
+ byHead.set(head, group$1);
3586
+ }
3587
+ if (entry.parts.length === 1) group$1.own.push(...entry.anns);
3588
+ else group$1.nested.push({
3589
+ parts: entry.parts.slice(1),
3590
+ anns: entry.anns
3591
+ });
3538
3592
  }
3539
- if (annotationsByProp.size === 0) return struct;
3540
- const structNode = struct;
3541
3593
  const newProps = [];
3542
3594
  let changed = false;
3543
- for (const [name, prop] of structNode.props) {
3544
- const extras = annotationsByProp.get(name);
3545
- if (!extras) {
3595
+ for (const [name, prop] of struct.props) {
3596
+ const group$1 = byHead.get(name);
3597
+ if (!group$1) {
3598
+ newProps.push(prop);
3599
+ continue;
3600
+ }
3601
+ const propDef = prop.getDefinition();
3602
+ let newDef = propDef;
3603
+ if (group$1.nested.length > 0 && propDef && isStructure(propDef)) {
3604
+ const foldedNested = this.foldAnnotationsIntoStructure(propDef, group$1.nested);
3605
+ if (foldedNested) newDef = foldedNested;
3606
+ }
3607
+ if (group$1.own.length === 0 && newDef === propDef) {
3546
3608
  newProps.push(prop);
3547
3609
  continue;
3548
3610
  }
3549
3611
  const cloned = new SemanticPropNode();
3550
3612
  const idToken = prop.token("identifier");
3551
3613
  if (idToken) cloned.saveToken(idToken, "identifier");
3552
- const propDef = prop.getDefinition();
3553
- if (propDef) cloned.define(propDef);
3554
- cloned.annotations = this.mergeNodesAnnotations(prop.annotations, extras);
3614
+ if (newDef) cloned.define(newDef);
3615
+ cloned.annotations = group$1.own.length > 0 ? this.mergeNodesAnnotations(prop.annotations, group$1.own) : prop.annotations;
3555
3616
  const optionalToken = prop.token("optional");
3556
3617
  if (optionalToken) cloned.saveToken(optionalToken, "optional");
3557
3618
  newProps.push(cloned);
3558
3619
  changed = true;
3559
3620
  }
3560
- if (!changed) return struct;
3621
+ if (!changed) return undefined;
3561
3622
  const newStruct = new SemanticStructureNode();
3562
3623
  newStruct.setProps(newProps);
3563
- newStruct.annotations = structNode.annotations;
3624
+ newStruct.annotations = struct.annotations;
3564
3625
  return newStruct;
3565
3626
  }
3566
3627
  usageListFor(token) {
@@ -3709,9 +3770,9 @@ else {
3709
3770
  getToDefinitionAt(line, character) {
3710
3771
  const token = this.tokensIndex.at(line, character);
3711
3772
  if (token) {
3712
- const block$2 = this.blocksIndex.at(line, character);
3713
- if (block$2?.blockType === "annotate" && isAnnotate(block$2.parentNode) && isRef(token.parentNode)) {
3714
- const targetName = block$2.parentNode.targetName;
3773
+ const annotateBlock = this.annotateBlockAt(line, character);
3774
+ if (annotateBlock && isRef(token.parentNode)) {
3775
+ const targetName = annotateBlock.targetName;
3715
3776
  const entryRef = token.parentNode;
3716
3777
  const chain = token.isChain && typeof token.index === "number" ? [entryRef.id, ...entryRef.chain.slice(0, token.index).map((c) => c.text)] : [token.text];
3717
3778
  const unwound = this.unwindType(targetName, chain);
@@ -3977,9 +4038,9 @@ else {
3977
4038
  }
3978
4039
  }
3979
4040
  for (const t of this.referred) {
3980
- const block$2 = this.blocksIndex.at(t.range.start.line, t.range.start.character);
3981
- if (block$2?.blockType === "annotate" && isAnnotate(block$2.parentNode)) {
3982
- const targetName = block$2.parentNode.targetName;
4041
+ const annotateBlock = this.annotateBlockAt(t.range.start.line, t.range.start.character);
4042
+ if (annotateBlock) {
4043
+ const targetName = annotateBlock.targetName;
3983
4044
  if (!this.registry.isDefined(targetName)) continue;
3984
4045
  const chain = isRef(t.parentNode) && t.parentNode.hasChain ? [t.text, ...t.parentNode.chain.map((c) => c.text)] : [t.text];
3985
4046
  const unwound = this.unwindType(targetName, chain);
@@ -4221,6 +4282,7 @@ else {
4221
4282
  */ _define_property$3(this, "dependenciesMap", void 0);
4222
4283
  _define_property$3(this, "nodes", void 0);
4223
4284
  _define_property$3(this, "_text", void 0);
4285
+ _define_property$3(this, "_annotateNodesByTarget", void 0);
4224
4286
  _define_property$3(this, "resolvedAnnotations", void 0);
4225
4287
  _define_property$3(this, "annotations", void 0);
4226
4288
  _define_property$3(this, "_nextTypeId", void 0);
package/dist/index.d.ts CHANGED
@@ -485,6 +485,7 @@ declare class AtscriptDoc {
485
485
  updateDependencies(docs: AtscriptDoc[]): void;
486
486
  nodes: SemanticNode[];
487
487
  private _text;
488
+ private _annotateNodesByTarget;
488
489
  get text(): string;
489
490
  update(text: string, debug?: boolean): void;
490
491
  cleanup(): void;
@@ -524,6 +525,12 @@ declare class AtscriptDoc {
524
525
  token: Token;
525
526
  }[] | undefined;
526
527
  evalAnnotationsForNode(givenNode: SemanticNode): TAnnotationTokens[] | undefined;
528
+ /**
529
+ * If the position falls inside an `annotate` block, returns that block's node
530
+ * (whose `targetName` is the annotated interface). Entry refs within the block
531
+ * resolve through the target interface rather than as top-level definitions.
532
+ */
533
+ annotateBlockAt(line: number, character: number): SemanticAnnotateNode | undefined;
527
534
  /**
528
535
  * Collects ad-hoc annotations from all `annotate` blocks targeting a given type/interface.
529
536
  * Returns a map of property path (dot-joined) → annotations array.
@@ -546,11 +553,20 @@ declare class AtscriptDoc {
546
553
  resolveInterfaceExtends(node: SemanticInterfaceNode, _visited?: Set<string>, _originDoc?: AtscriptDoc): SemanticNode | undefined;
547
554
  /**
548
555
  * Returns a structure equivalent to `struct` with annotate-block contributions
549
- * folded into matching props' annotation arrays. Skips chained entries
550
- * (e.g. `annotate Parent { user.address.city }`) — only root-level entries apply.
551
- * Original prop nodes are reused by reference when no extra annotations apply.
556
+ * folded into matching props' annotation arrays. Chained entries
557
+ * (e.g. `annotate Parent { account.active }`) are folded recursively into the
558
+ * matching inline-object prop's nested structure. Only props/structures along a
559
+ * touched path are cloned — siblings are reused by reference, so the base's own
560
+ * (shared) nodes are never mutated.
552
561
  */
553
562
  private applyAnnotateBlocksToStructure;
563
+ /**
564
+ * Recursively folds annotate-block entries into a structure's props. Each entry's
565
+ * `parts[0]` names a prop in `struct`; remaining parts target a nested inline-object
566
+ * prop. Returns a new structure with cloned props/structures along touched paths, or
567
+ * `undefined` when nothing matched (so callers can reuse the original by reference).
568
+ */
569
+ private foldAnnotationsIntoStructure;
554
570
  usageListFor(token: Token): Array<{
555
571
  uri: string;
556
572
  range: Token['range'];
package/dist/index.mjs CHANGED
@@ -1006,7 +1006,16 @@ var AnnotationSpec = class {
1006
1006
  range: token.range
1007
1007
  });
1008
1008
  if (this.config.defType?.length) {
1009
- let def = mainToken.parentNode.getDefinition();
1009
+ const parentNode = mainToken.parentNode;
1010
+ let def = parentNode.getDefinition();
1011
+ if (!def && isRef(parentNode)) {
1012
+ const idToken = parentNode.token("identifier");
1013
+ const annotateBlock = idToken ? doc.annotateBlockAt(idToken.range.start.line, idToken.range.start.character) : undefined;
1014
+ if (annotateBlock) {
1015
+ const chain = [parentNode.id, ...parentNode.chain.map((c) => c.text)];
1016
+ def = doc.unwindType(annotateBlock.targetName, chain)?.def || def;
1017
+ }
1018
+ }
1010
1019
  if (isRef(def)) def = doc.unwindType(def.id, def.chain)?.def || def;
1011
1020
  let defEntity = def?.entity || "unknown";
1012
1021
  if (isInterface(def) || isStructure(def)) defEntity = "object";
@@ -3275,6 +3284,7 @@ var AtscriptDoc = class {
3275
3284
  this.imports.clear();
3276
3285
  this.resolvedImports.clear();
3277
3286
  this._allMessages = undefined;
3287
+ this._annotateNodesByTarget = undefined;
3278
3288
  this.tokensIndex = new TokensIndex();
3279
3289
  this.blocksIndex = new BlocksIndex();
3280
3290
  this.resolvedAnnotations = [];
@@ -3437,13 +3447,28 @@ else if (isStructure(def) || isInterface(def) || isPrimitive(def)) def = def.pro
3437
3447
  return right;
3438
3448
  }
3439
3449
  /**
3450
+ * If the position falls inside an `annotate` block, returns that block's node
3451
+ * (whose `targetName` is the annotated interface). Entry refs within the block
3452
+ * resolve through the target interface rather than as top-level definitions.
3453
+ */ annotateBlockAt(line, character) {
3454
+ const block$2 = this.blocksIndex.at(line, character);
3455
+ return block$2?.blockType === "annotate" && isAnnotate(block$2.parentNode) ? block$2.parentNode : undefined;
3456
+ }
3457
+ /**
3440
3458
  * Collects ad-hoc annotations from all `annotate` blocks targeting a given type/interface.
3441
3459
  * Returns a map of property path (dot-joined) → annotations array.
3442
3460
  * For entries without a chain (root-level), the key is the entry identifier.
3443
3461
  */ getAnnotateNodesFor(targetName) {
3444
- const result = [];
3445
- for (const node of this.nodes) if (isAnnotate(node) && node.targetName === targetName) result.push(node);
3446
- return result;
3462
+ if (!this._annotateNodesByTarget) {
3463
+ const index = new Map();
3464
+ for (const node of this.nodes) if (isAnnotate(node)) {
3465
+ const existing = index.get(node.targetName);
3466
+ if (existing) existing.push(node);
3467
+ else index.set(node.targetName, [node]);
3468
+ }
3469
+ this._annotateNodesByTarget = index;
3470
+ }
3471
+ return this._annotateNodesByTarget.get(targetName) ?? [];
3447
3472
  }
3448
3473
  /**
3449
3474
  * Resolves an interface with `extends` by merging parent structures with own props.
@@ -3498,45 +3523,81 @@ else {
3498
3523
  }
3499
3524
  /**
3500
3525
  * Returns a structure equivalent to `struct` with annotate-block contributions
3501
- * folded into matching props' annotation arrays. Skips chained entries
3502
- * (e.g. `annotate Parent { user.address.city }`) — only root-level entries apply.
3503
- * Original prop nodes are reused by reference when no extra annotations apply.
3526
+ * folded into matching props' annotation arrays. Chained entries
3527
+ * (e.g. `annotate Parent { account.active }`) are folded recursively into the
3528
+ * matching inline-object prop's nested structure. Only props/structures along a
3529
+ * touched path are cloned — siblings are reused by reference, so the base's own
3530
+ * (shared) nodes are never mutated.
3504
3531
  */ applyAnnotateBlocksToStructure(struct, annotateNodes) {
3505
3532
  if (!isStructure(struct)) return struct;
3506
- const annotationsByProp = new Map();
3533
+ const entries = [];
3507
3534
  for (const annotateNode of annotateNodes) for (const entry of annotateNode.entries) {
3508
- if (entry.hasChain) continue;
3509
- const propName$1 = entry.id;
3510
3535
  const anns = entry.annotations;
3511
- if (!propName$1 || !anns || anns.length === 0) continue;
3512
- const existing = annotationsByProp.get(propName$1);
3513
- annotationsByProp.set(propName$1, existing ? [...existing, ...anns] : [...anns]);
3536
+ if (!entry.id || !anns || anns.length === 0) continue;
3537
+ const parts = entry.hasChain ? [entry.id, ...entry.chain.map((c) => c.text)] : [entry.id];
3538
+ entries.push({
3539
+ parts,
3540
+ anns
3541
+ });
3542
+ }
3543
+ if (entries.length === 0) return struct;
3544
+ return this.foldAnnotationsIntoStructure(struct, entries) ?? struct;
3545
+ }
3546
+ /**
3547
+ * Recursively folds annotate-block entries into a structure's props. Each entry's
3548
+ * `parts[0]` names a prop in `struct`; remaining parts target a nested inline-object
3549
+ * prop. Returns a new structure with cloned props/structures along touched paths, or
3550
+ * `undefined` when nothing matched (so callers can reuse the original by reference).
3551
+ */ foldAnnotationsIntoStructure(struct, entries) {
3552
+ const byHead = new Map();
3553
+ for (const entry of entries) {
3554
+ const head = entry.parts[0];
3555
+ let group$1 = byHead.get(head);
3556
+ if (!group$1) {
3557
+ group$1 = {
3558
+ own: [],
3559
+ nested: []
3560
+ };
3561
+ byHead.set(head, group$1);
3562
+ }
3563
+ if (entry.parts.length === 1) group$1.own.push(...entry.anns);
3564
+ else group$1.nested.push({
3565
+ parts: entry.parts.slice(1),
3566
+ anns: entry.anns
3567
+ });
3514
3568
  }
3515
- if (annotationsByProp.size === 0) return struct;
3516
- const structNode = struct;
3517
3569
  const newProps = [];
3518
3570
  let changed = false;
3519
- for (const [name, prop] of structNode.props) {
3520
- const extras = annotationsByProp.get(name);
3521
- if (!extras) {
3571
+ for (const [name, prop] of struct.props) {
3572
+ const group$1 = byHead.get(name);
3573
+ if (!group$1) {
3574
+ newProps.push(prop);
3575
+ continue;
3576
+ }
3577
+ const propDef = prop.getDefinition();
3578
+ let newDef = propDef;
3579
+ if (group$1.nested.length > 0 && propDef && isStructure(propDef)) {
3580
+ const foldedNested = this.foldAnnotationsIntoStructure(propDef, group$1.nested);
3581
+ if (foldedNested) newDef = foldedNested;
3582
+ }
3583
+ if (group$1.own.length === 0 && newDef === propDef) {
3522
3584
  newProps.push(prop);
3523
3585
  continue;
3524
3586
  }
3525
3587
  const cloned = new SemanticPropNode();
3526
3588
  const idToken = prop.token("identifier");
3527
3589
  if (idToken) cloned.saveToken(idToken, "identifier");
3528
- const propDef = prop.getDefinition();
3529
- if (propDef) cloned.define(propDef);
3530
- cloned.annotations = this.mergeNodesAnnotations(prop.annotations, extras);
3590
+ if (newDef) cloned.define(newDef);
3591
+ cloned.annotations = group$1.own.length > 0 ? this.mergeNodesAnnotations(prop.annotations, group$1.own) : prop.annotations;
3531
3592
  const optionalToken = prop.token("optional");
3532
3593
  if (optionalToken) cloned.saveToken(optionalToken, "optional");
3533
3594
  newProps.push(cloned);
3534
3595
  changed = true;
3535
3596
  }
3536
- if (!changed) return struct;
3597
+ if (!changed) return undefined;
3537
3598
  const newStruct = new SemanticStructureNode();
3538
3599
  newStruct.setProps(newProps);
3539
- newStruct.annotations = structNode.annotations;
3600
+ newStruct.annotations = struct.annotations;
3540
3601
  return newStruct;
3541
3602
  }
3542
3603
  usageListFor(token) {
@@ -3685,9 +3746,9 @@ else {
3685
3746
  getToDefinitionAt(line, character) {
3686
3747
  const token = this.tokensIndex.at(line, character);
3687
3748
  if (token) {
3688
- const block$2 = this.blocksIndex.at(line, character);
3689
- if (block$2?.blockType === "annotate" && isAnnotate(block$2.parentNode) && isRef(token.parentNode)) {
3690
- const targetName = block$2.parentNode.targetName;
3749
+ const annotateBlock = this.annotateBlockAt(line, character);
3750
+ if (annotateBlock && isRef(token.parentNode)) {
3751
+ const targetName = annotateBlock.targetName;
3691
3752
  const entryRef = token.parentNode;
3692
3753
  const chain = token.isChain && typeof token.index === "number" ? [entryRef.id, ...entryRef.chain.slice(0, token.index).map((c) => c.text)] : [token.text];
3693
3754
  const unwound = this.unwindType(targetName, chain);
@@ -3953,9 +4014,9 @@ else {
3953
4014
  }
3954
4015
  }
3955
4016
  for (const t of this.referred) {
3956
- const block$2 = this.blocksIndex.at(t.range.start.line, t.range.start.character);
3957
- if (block$2?.blockType === "annotate" && isAnnotate(block$2.parentNode)) {
3958
- const targetName = block$2.parentNode.targetName;
4017
+ const annotateBlock = this.annotateBlockAt(t.range.start.line, t.range.start.character);
4018
+ if (annotateBlock) {
4019
+ const targetName = annotateBlock.targetName;
3959
4020
  if (!this.registry.isDefined(targetName)) continue;
3960
4021
  const chain = isRef(t.parentNode) && t.parentNode.hasChain ? [t.text, ...t.parentNode.chain.map((c) => c.text)] : [t.text];
3961
4022
  const unwound = this.unwindType(targetName, chain);
@@ -4197,6 +4258,7 @@ else {
4197
4258
  */ _define_property$3(this, "dependenciesMap", void 0);
4198
4259
  _define_property$3(this, "nodes", void 0);
4199
4260
  _define_property$3(this, "_text", void 0);
4261
+ _define_property$3(this, "_annotateNodesByTarget", void 0);
4200
4262
  _define_property$3(this, "resolvedAnnotations", void 0);
4201
4263
  _define_property$3(this, "annotations", void 0);
4202
4264
  _define_property$3(this, "_nextTypeId", void 0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/core",
3
- "version": "0.1.71",
3
+ "version": "0.1.73",
4
4
  "description": "Core library for Atscript parsing and file generation.",
5
5
  "keywords": [
6
6
  "annotations",