@atscript/core 0.1.72 → 0.1.74

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,21 +1030,38 @@ var AnnotationSpec = class {
1030
1030
  range: token.range
1031
1031
  });
1032
1032
  if (this.config.defType?.length) {
1033
- let def = mainToken.parentNode.getDefinition();
1034
- if (isRef(def)) def = doc.unwindType(def.id, def.chain)?.def || def;
1035
- let defEntity = def?.entity || "unknown";
1036
- if (isInterface(def) || isStructure(def)) defEntity = "object";
1037
- else if (isGroup(def) && def.entity !== "tuple") defEntity = def.op === "&" ? "intersection" : "union";
1038
- if (!isPrimitive(def) && !this.config.defType.includes(defEntity) || isPrimitive(def) && !this.config.defType.includes(def.type)) messages.push({
1039
- message: `Expected type is (${this.config.defType.join(" | ")}), got "${isPrimitive(def) ? def.type : def?.entity || "unknown"}"`,
1040
- severity: 1,
1041
- range: mainToken.range
1042
- });
1033
+ const parentNode = mainToken.parentNode;
1034
+ const idToken = isRef(parentNode) ? parentNode.token("identifier") : undefined;
1035
+ const isAnnotateEntry = !!idToken && !!doc.annotateBlockAt(idToken.range.start.line, idToken.range.start.character);
1036
+ if (!isAnnotateEntry) {
1037
+ let def = parentNode.getDefinition();
1038
+ if (isRef(def)) def = doc.unwindType(def.id, def.chain)?.def || def;
1039
+ messages.push(...this.validateTargetType(def, mainToken.range) || []);
1040
+ }
1043
1041
  }
1044
1042
  }
1045
1043
  if (this.config.validate) messages.push(...this.config.validate(mainToken, args, doc) || []);
1046
1044
  return messages.length > 0 ? messages : undefined;
1047
1045
  }
1046
+ /**
1047
+ * Checks an already-resolved target node against this annotation's `defType`
1048
+ * guard (e.g. `@expect.minLength` requires `string | array`). Returns a
1049
+ * diagnostic when the type doesn't match, or `undefined` when it matches or no
1050
+ * guard is configured. Shared by the parse-time check (for inline props) and
1051
+ * the deferred annotate-entry check in `AtscriptDoc.getDiagMessages`, where
1052
+ * imported target types finally resolve.
1053
+ */ validateTargetType(def, range) {
1054
+ if (!this.config.defType?.length) return undefined;
1055
+ let defEntity = def?.entity || "unknown";
1056
+ if (isInterface(def) || isStructure(def)) defEntity = "object";
1057
+ else if (isGroup(def) && def.entity !== "tuple") defEntity = def.op === "&" ? "intersection" : "union";
1058
+ if (!isPrimitive(def) && !this.config.defType.includes(defEntity) || isPrimitive(def) && !this.config.defType.includes(def.type)) return [{
1059
+ message: `Expected type is (${this.config.defType.join(" | ")}), got "${isPrimitive(def) ? def.type : def?.entity || "unknown"}"`,
1060
+ severity: 1,
1061
+ range
1062
+ }];
1063
+ return undefined;
1064
+ }
1048
1065
  renderDocs(index) {
1049
1066
  if (typeof index === "number") {
1050
1067
  const a = this.arguments[index];
@@ -3462,6 +3479,14 @@ else if (isStructure(def) || isInterface(def) || isPrimitive(def)) def = def.pro
3462
3479
  return right;
3463
3480
  }
3464
3481
  /**
3482
+ * If the position falls inside an `annotate` block, returns that block's node
3483
+ * (whose `targetName` is the annotated interface). Entry refs within the block
3484
+ * resolve through the target interface rather than as top-level definitions.
3485
+ */ annotateBlockAt(line, character) {
3486
+ const block$2 = this.blocksIndex.at(line, character);
3487
+ return block$2?.blockType === "annotate" && isAnnotate(block$2.parentNode) ? block$2.parentNode : undefined;
3488
+ }
3489
+ /**
3465
3490
  * Collects ad-hoc annotations from all `annotate` blocks targeting a given type/interface.
3466
3491
  * Returns a map of property path (dot-joined) → annotations array.
3467
3492
  * For entries without a chain (root-level), the key is the entry identifier.
@@ -3753,9 +3778,9 @@ else group$1.nested.push({
3753
3778
  getToDefinitionAt(line, character) {
3754
3779
  const token = this.tokensIndex.at(line, character);
3755
3780
  if (token) {
3756
- const block$2 = this.blocksIndex.at(line, character);
3757
- if (block$2?.blockType === "annotate" && isAnnotate(block$2.parentNode) && isRef(token.parentNode)) {
3758
- const targetName = block$2.parentNode.targetName;
3781
+ const annotateBlock = this.annotateBlockAt(line, character);
3782
+ if (annotateBlock && isRef(token.parentNode)) {
3783
+ const targetName = annotateBlock.targetName;
3759
3784
  const entryRef = token.parentNode;
3760
3785
  const chain = token.isChain && typeof token.index === "number" ? [entryRef.id, ...entryRef.chain.slice(0, token.index).map((c) => c.text)] : [token.text];
3761
3786
  const unwound = this.unwindType(targetName, chain);
@@ -4021,14 +4046,15 @@ else group$1.nested.push({
4021
4046
  }
4022
4047
  }
4023
4048
  for (const t of this.referred) {
4024
- const block$2 = this.blocksIndex.at(t.range.start.line, t.range.start.character);
4025
- if (block$2?.blockType === "annotate" && isAnnotate(block$2.parentNode)) {
4026
- const targetName = block$2.parentNode.targetName;
4049
+ const annotateBlock = this.annotateBlockAt(t.range.start.line, t.range.start.character);
4050
+ if (annotateBlock) {
4051
+ const targetName = annotateBlock.targetName;
4027
4052
  if (!this.registry.isDefined(targetName)) continue;
4028
4053
  const chain = isRef(t.parentNode) && t.parentNode.hasChain ? [t.text, ...t.parentNode.chain.map((c) => c.text)] : [t.text];
4029
4054
  const unwound = this.unwindType(targetName, chain);
4030
4055
  const chainResolved = unwound?.def && !isGroup(unwound.def);
4031
- if (!chainResolved && !this.resolveChainWithMerge(targetName, chain)) {
4056
+ const resolvedType = chainResolved ? unwound.def : this.resolveChainWithMerge(targetName, chain);
4057
+ if (!resolvedType) {
4032
4058
  const lastToken = chain.length > 1 && isRef(t.parentNode) ? t.parentNode.chain[t.parentNode.chain.length - 1] || t : t;
4033
4059
  this._allMessages.push({
4034
4060
  severity: 1,
@@ -4049,6 +4075,11 @@ else group$1.nested.push({
4049
4075
  });
4050
4076
  }
4051
4077
  }
4078
+ if (resolvedType && isRef(t.parentNode) && t.parentNode.annotations) for (const a of t.parentNode.annotations) {
4079
+ const spec = this.resolveAnnotation(a.token.text.slice(1));
4080
+ const msgs = spec?.validateTargetType(resolvedType, a.token.range);
4081
+ if (msgs) this._allMessages.push(...msgs);
4082
+ }
4052
4083
  continue;
4053
4084
  }
4054
4085
  if (!this.registry.isDefined(t)) {
@@ -4095,9 +4126,13 @@ else group$1.nested.push({
4095
4126
  * Walks a property chain step-by-step, applying mergeIntersection at each
4096
4127
  * level. Mirrors the completion logic in the LSP server so that intersection
4097
4128
  * types like `{ a: string } & { b: number }` are handled correctly.
4129
+ *
4130
+ * Returns the resolved leaf definition (unwound through refs, parity with
4131
+ * `unwindType`), or `undefined` when the chain can't be resolved. Callers that
4132
+ * only need a yes/no resolution check use the truthiness of the result.
4098
4133
  */ resolveChainWithMerge(targetName, chain) {
4099
4134
  let def = this.unwindType(targetName)?.def;
4100
- if (!def) return false;
4135
+ if (!def) return undefined;
4101
4136
  for (const prop of chain) {
4102
4137
  def = this.mergeIntersection(def);
4103
4138
  if (isProp(def)) {
@@ -4106,15 +4141,16 @@ else group$1.nested.push({
4106
4141
  }
4107
4142
  if (isStructure(def) || isInterface(def)) {
4108
4143
  const next = def.props.get(prop);
4109
- if (!next) return false;
4144
+ if (!next) return undefined;
4110
4145
  def = next.getDefinition() || next;
4111
4146
  } else if (isGroup(def)) {
4112
4147
  const found = this.findPropInGroup(def, prop);
4113
- if (!found) return false;
4148
+ if (!found) return undefined;
4114
4149
  def = found;
4115
- } else return false;
4150
+ } else return undefined;
4116
4151
  }
4117
- return true;
4152
+ if (isRef(def)) def = this.unwindType(def.id, def.chain)?.def || def;
4153
+ return def;
4118
4154
  }
4119
4155
  findPropInGroup(group$1, propName$1) {
4120
4156
  for (const item of group$1.unwrap()) {
package/dist/index.d.ts CHANGED
@@ -525,6 +525,12 @@ declare class AtscriptDoc {
525
525
  token: Token;
526
526
  }[] | undefined;
527
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;
528
534
  /**
529
535
  * Collects ad-hoc annotations from all `annotate` blocks targeting a given type/interface.
530
536
  * Returns a map of property path (dot-joined) → annotations array.
@@ -664,6 +670,10 @@ declare class AtscriptDoc {
664
670
  * Walks a property chain step-by-step, applying mergeIntersection at each
665
671
  * level. Mirrors the completion logic in the LSP server so that intersection
666
672
  * types like `{ a: string } & { b: number }` are handled correctly.
673
+ *
674
+ * Returns the resolved leaf definition (unwound through refs, parity with
675
+ * `unwindType`), or `undefined` when the chain can't be resolved. Callers that
676
+ * only need a yes/no resolution check use the truthiness of the result.
667
677
  */
668
678
  private resolveChainWithMerge;
669
679
  private findPropInGroup;
@@ -879,6 +889,15 @@ declare class AnnotationSpec {
879
889
  private validateType;
880
890
  modify(mainToken: Token, args: Token[], doc: AtscriptDoc): void;
881
891
  validate(mainToken: Token, args: Token[], doc: AtscriptDoc): TMessages | undefined;
892
+ /**
893
+ * Checks an already-resolved target node against this annotation's `defType`
894
+ * guard (e.g. `@expect.minLength` requires `string | array`). Returns a
895
+ * diagnostic when the type doesn't match, or `undefined` when it matches or no
896
+ * guard is configured. Shared by the parse-time check (for inline props) and
897
+ * the deferred annotate-entry check in `AtscriptDoc.getDiagMessages`, where
898
+ * imported target types finally resolve.
899
+ */
900
+ validateTargetType(def: SemanticNode | undefined, range: Token['range']): TMessages | undefined;
882
901
  renderDocs(index: number | string): string | undefined;
883
902
  protected getDefaultValueForType(name: string, type: TAnnotationArgument['type']): string;
884
903
  }
package/dist/index.mjs CHANGED
@@ -1006,21 +1006,38 @@ var AnnotationSpec = class {
1006
1006
  range: token.range
1007
1007
  });
1008
1008
  if (this.config.defType?.length) {
1009
- let def = mainToken.parentNode.getDefinition();
1010
- if (isRef(def)) def = doc.unwindType(def.id, def.chain)?.def || def;
1011
- let defEntity = def?.entity || "unknown";
1012
- if (isInterface(def) || isStructure(def)) defEntity = "object";
1013
- else if (isGroup(def) && def.entity !== "tuple") defEntity = def.op === "&" ? "intersection" : "union";
1014
- if (!isPrimitive(def) && !this.config.defType.includes(defEntity) || isPrimitive(def) && !this.config.defType.includes(def.type)) messages.push({
1015
- message: `Expected type is (${this.config.defType.join(" | ")}), got "${isPrimitive(def) ? def.type : def?.entity || "unknown"}"`,
1016
- severity: 1,
1017
- range: mainToken.range
1018
- });
1009
+ const parentNode = mainToken.parentNode;
1010
+ const idToken = isRef(parentNode) ? parentNode.token("identifier") : undefined;
1011
+ const isAnnotateEntry = !!idToken && !!doc.annotateBlockAt(idToken.range.start.line, idToken.range.start.character);
1012
+ if (!isAnnotateEntry) {
1013
+ let def = parentNode.getDefinition();
1014
+ if (isRef(def)) def = doc.unwindType(def.id, def.chain)?.def || def;
1015
+ messages.push(...this.validateTargetType(def, mainToken.range) || []);
1016
+ }
1019
1017
  }
1020
1018
  }
1021
1019
  if (this.config.validate) messages.push(...this.config.validate(mainToken, args, doc) || []);
1022
1020
  return messages.length > 0 ? messages : undefined;
1023
1021
  }
1022
+ /**
1023
+ * Checks an already-resolved target node against this annotation's `defType`
1024
+ * guard (e.g. `@expect.minLength` requires `string | array`). Returns a
1025
+ * diagnostic when the type doesn't match, or `undefined` when it matches or no
1026
+ * guard is configured. Shared by the parse-time check (for inline props) and
1027
+ * the deferred annotate-entry check in `AtscriptDoc.getDiagMessages`, where
1028
+ * imported target types finally resolve.
1029
+ */ validateTargetType(def, range) {
1030
+ if (!this.config.defType?.length) return undefined;
1031
+ let defEntity = def?.entity || "unknown";
1032
+ if (isInterface(def) || isStructure(def)) defEntity = "object";
1033
+ else if (isGroup(def) && def.entity !== "tuple") defEntity = def.op === "&" ? "intersection" : "union";
1034
+ if (!isPrimitive(def) && !this.config.defType.includes(defEntity) || isPrimitive(def) && !this.config.defType.includes(def.type)) return [{
1035
+ message: `Expected type is (${this.config.defType.join(" | ")}), got "${isPrimitive(def) ? def.type : def?.entity || "unknown"}"`,
1036
+ severity: 1,
1037
+ range
1038
+ }];
1039
+ return undefined;
1040
+ }
1024
1041
  renderDocs(index) {
1025
1042
  if (typeof index === "number") {
1026
1043
  const a = this.arguments[index];
@@ -3438,6 +3455,14 @@ else if (isStructure(def) || isInterface(def) || isPrimitive(def)) def = def.pro
3438
3455
  return right;
3439
3456
  }
3440
3457
  /**
3458
+ * If the position falls inside an `annotate` block, returns that block's node
3459
+ * (whose `targetName` is the annotated interface). Entry refs within the block
3460
+ * resolve through the target interface rather than as top-level definitions.
3461
+ */ annotateBlockAt(line, character) {
3462
+ const block$2 = this.blocksIndex.at(line, character);
3463
+ return block$2?.blockType === "annotate" && isAnnotate(block$2.parentNode) ? block$2.parentNode : undefined;
3464
+ }
3465
+ /**
3441
3466
  * Collects ad-hoc annotations from all `annotate` blocks targeting a given type/interface.
3442
3467
  * Returns a map of property path (dot-joined) → annotations array.
3443
3468
  * For entries without a chain (root-level), the key is the entry identifier.
@@ -3729,9 +3754,9 @@ else group$1.nested.push({
3729
3754
  getToDefinitionAt(line, character) {
3730
3755
  const token = this.tokensIndex.at(line, character);
3731
3756
  if (token) {
3732
- const block$2 = this.blocksIndex.at(line, character);
3733
- if (block$2?.blockType === "annotate" && isAnnotate(block$2.parentNode) && isRef(token.parentNode)) {
3734
- const targetName = block$2.parentNode.targetName;
3757
+ const annotateBlock = this.annotateBlockAt(line, character);
3758
+ if (annotateBlock && isRef(token.parentNode)) {
3759
+ const targetName = annotateBlock.targetName;
3735
3760
  const entryRef = token.parentNode;
3736
3761
  const chain = token.isChain && typeof token.index === "number" ? [entryRef.id, ...entryRef.chain.slice(0, token.index).map((c) => c.text)] : [token.text];
3737
3762
  const unwound = this.unwindType(targetName, chain);
@@ -3997,14 +4022,15 @@ else group$1.nested.push({
3997
4022
  }
3998
4023
  }
3999
4024
  for (const t of this.referred) {
4000
- const block$2 = this.blocksIndex.at(t.range.start.line, t.range.start.character);
4001
- if (block$2?.blockType === "annotate" && isAnnotate(block$2.parentNode)) {
4002
- const targetName = block$2.parentNode.targetName;
4025
+ const annotateBlock = this.annotateBlockAt(t.range.start.line, t.range.start.character);
4026
+ if (annotateBlock) {
4027
+ const targetName = annotateBlock.targetName;
4003
4028
  if (!this.registry.isDefined(targetName)) continue;
4004
4029
  const chain = isRef(t.parentNode) && t.parentNode.hasChain ? [t.text, ...t.parentNode.chain.map((c) => c.text)] : [t.text];
4005
4030
  const unwound = this.unwindType(targetName, chain);
4006
4031
  const chainResolved = unwound?.def && !isGroup(unwound.def);
4007
- if (!chainResolved && !this.resolveChainWithMerge(targetName, chain)) {
4032
+ const resolvedType = chainResolved ? unwound.def : this.resolveChainWithMerge(targetName, chain);
4033
+ if (!resolvedType) {
4008
4034
  const lastToken = chain.length > 1 && isRef(t.parentNode) ? t.parentNode.chain[t.parentNode.chain.length - 1] || t : t;
4009
4035
  this._allMessages.push({
4010
4036
  severity: 1,
@@ -4025,6 +4051,11 @@ else group$1.nested.push({
4025
4051
  });
4026
4052
  }
4027
4053
  }
4054
+ if (resolvedType && isRef(t.parentNode) && t.parentNode.annotations) for (const a of t.parentNode.annotations) {
4055
+ const spec = this.resolveAnnotation(a.token.text.slice(1));
4056
+ const msgs = spec?.validateTargetType(resolvedType, a.token.range);
4057
+ if (msgs) this._allMessages.push(...msgs);
4058
+ }
4028
4059
  continue;
4029
4060
  }
4030
4061
  if (!this.registry.isDefined(t)) {
@@ -4071,9 +4102,13 @@ else group$1.nested.push({
4071
4102
  * Walks a property chain step-by-step, applying mergeIntersection at each
4072
4103
  * level. Mirrors the completion logic in the LSP server so that intersection
4073
4104
  * types like `{ a: string } & { b: number }` are handled correctly.
4105
+ *
4106
+ * Returns the resolved leaf definition (unwound through refs, parity with
4107
+ * `unwindType`), or `undefined` when the chain can't be resolved. Callers that
4108
+ * only need a yes/no resolution check use the truthiness of the result.
4074
4109
  */ resolveChainWithMerge(targetName, chain) {
4075
4110
  let def = this.unwindType(targetName)?.def;
4076
- if (!def) return false;
4111
+ if (!def) return undefined;
4077
4112
  for (const prop of chain) {
4078
4113
  def = this.mergeIntersection(def);
4079
4114
  if (isProp(def)) {
@@ -4082,15 +4117,16 @@ else group$1.nested.push({
4082
4117
  }
4083
4118
  if (isStructure(def) || isInterface(def)) {
4084
4119
  const next = def.props.get(prop);
4085
- if (!next) return false;
4120
+ if (!next) return undefined;
4086
4121
  def = next.getDefinition() || next;
4087
4122
  } else if (isGroup(def)) {
4088
4123
  const found = this.findPropInGroup(def, prop);
4089
- if (!found) return false;
4124
+ if (!found) return undefined;
4090
4125
  def = found;
4091
- } else return false;
4126
+ } else return undefined;
4092
4127
  }
4093
- return true;
4128
+ if (isRef(def)) def = this.unwindType(def.id, def.chain)?.def || def;
4129
+ return def;
4094
4130
  }
4095
4131
  findPropInGroup(group$1, propName$1) {
4096
4132
  for (const item of group$1.unwrap()) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/core",
3
- "version": "0.1.72",
3
+ "version": "0.1.74",
4
4
  "description": "Core library for Atscript parsing and file generation.",
5
5
  "keywords": [
6
6
  "annotations",