@atscript/core 0.1.73 → 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
@@ -1031,29 +1031,37 @@ var AnnotationSpec = class {
1031
1031
  });
1032
1032
  if (this.config.defType?.length) {
1033
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
- }
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) || []);
1042
1040
  }
1043
- if (isRef(def)) def = doc.unwindType(def.id, def.chain)?.def || def;
1044
- let defEntity = def?.entity || "unknown";
1045
- if (isInterface(def) || isStructure(def)) defEntity = "object";
1046
- else if (isGroup(def) && def.entity !== "tuple") defEntity = def.op === "&" ? "intersection" : "union";
1047
- if (!isPrimitive(def) && !this.config.defType.includes(defEntity) || isPrimitive(def) && !this.config.defType.includes(def.type)) messages.push({
1048
- message: `Expected type is (${this.config.defType.join(" | ")}), got "${isPrimitive(def) ? def.type : def?.entity || "unknown"}"`,
1049
- severity: 1,
1050
- range: mainToken.range
1051
- });
1052
1041
  }
1053
1042
  }
1054
1043
  if (this.config.validate) messages.push(...this.config.validate(mainToken, args, doc) || []);
1055
1044
  return messages.length > 0 ? messages : undefined;
1056
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
+ }
1057
1065
  renderDocs(index) {
1058
1066
  if (typeof index === "number") {
1059
1067
  const a = this.arguments[index];
@@ -4045,7 +4053,8 @@ else group$1.nested.push({
4045
4053
  const chain = isRef(t.parentNode) && t.parentNode.hasChain ? [t.text, ...t.parentNode.chain.map((c) => c.text)] : [t.text];
4046
4054
  const unwound = this.unwindType(targetName, chain);
4047
4055
  const chainResolved = unwound?.def && !isGroup(unwound.def);
4048
- if (!chainResolved && !this.resolveChainWithMerge(targetName, chain)) {
4056
+ const resolvedType = chainResolved ? unwound.def : this.resolveChainWithMerge(targetName, chain);
4057
+ if (!resolvedType) {
4049
4058
  const lastToken = chain.length > 1 && isRef(t.parentNode) ? t.parentNode.chain[t.parentNode.chain.length - 1] || t : t;
4050
4059
  this._allMessages.push({
4051
4060
  severity: 1,
@@ -4066,6 +4075,11 @@ else group$1.nested.push({
4066
4075
  });
4067
4076
  }
4068
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
+ }
4069
4083
  continue;
4070
4084
  }
4071
4085
  if (!this.registry.isDefined(t)) {
@@ -4112,9 +4126,13 @@ else group$1.nested.push({
4112
4126
  * Walks a property chain step-by-step, applying mergeIntersection at each
4113
4127
  * level. Mirrors the completion logic in the LSP server so that intersection
4114
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.
4115
4133
  */ resolveChainWithMerge(targetName, chain) {
4116
4134
  let def = this.unwindType(targetName)?.def;
4117
- if (!def) return false;
4135
+ if (!def) return undefined;
4118
4136
  for (const prop of chain) {
4119
4137
  def = this.mergeIntersection(def);
4120
4138
  if (isProp(def)) {
@@ -4123,15 +4141,16 @@ else group$1.nested.push({
4123
4141
  }
4124
4142
  if (isStructure(def) || isInterface(def)) {
4125
4143
  const next = def.props.get(prop);
4126
- if (!next) return false;
4144
+ if (!next) return undefined;
4127
4145
  def = next.getDefinition() || next;
4128
4146
  } else if (isGroup(def)) {
4129
4147
  const found = this.findPropInGroup(def, prop);
4130
- if (!found) return false;
4148
+ if (!found) return undefined;
4131
4149
  def = found;
4132
- } else return false;
4150
+ } else return undefined;
4133
4151
  }
4134
- return true;
4152
+ if (isRef(def)) def = this.unwindType(def.id, def.chain)?.def || def;
4153
+ return def;
4135
4154
  }
4136
4155
  findPropInGroup(group$1, propName$1) {
4137
4156
  for (const item of group$1.unwrap()) {
package/dist/index.d.ts CHANGED
@@ -670,6 +670,10 @@ declare class AtscriptDoc {
670
670
  * Walks a property chain step-by-step, applying mergeIntersection at each
671
671
  * level. Mirrors the completion logic in the LSP server so that intersection
672
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.
673
677
  */
674
678
  private resolveChainWithMerge;
675
679
  private findPropInGroup;
@@ -885,6 +889,15 @@ declare class AnnotationSpec {
885
889
  private validateType;
886
890
  modify(mainToken: Token, args: Token[], doc: AtscriptDoc): void;
887
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;
888
901
  renderDocs(index: number | string): string | undefined;
889
902
  protected getDefaultValueForType(name: string, type: TAnnotationArgument['type']): string;
890
903
  }
package/dist/index.mjs CHANGED
@@ -1007,29 +1007,37 @@ var AnnotationSpec = class {
1007
1007
  });
1008
1008
  if (this.config.defType?.length) {
1009
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
- }
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) || []);
1018
1016
  }
1019
- if (isRef(def)) def = doc.unwindType(def.id, def.chain)?.def || def;
1020
- let defEntity = def?.entity || "unknown";
1021
- if (isInterface(def) || isStructure(def)) defEntity = "object";
1022
- else if (isGroup(def) && def.entity !== "tuple") defEntity = def.op === "&" ? "intersection" : "union";
1023
- if (!isPrimitive(def) && !this.config.defType.includes(defEntity) || isPrimitive(def) && !this.config.defType.includes(def.type)) messages.push({
1024
- message: `Expected type is (${this.config.defType.join(" | ")}), got "${isPrimitive(def) ? def.type : def?.entity || "unknown"}"`,
1025
- severity: 1,
1026
- range: mainToken.range
1027
- });
1028
1017
  }
1029
1018
  }
1030
1019
  if (this.config.validate) messages.push(...this.config.validate(mainToken, args, doc) || []);
1031
1020
  return messages.length > 0 ? messages : undefined;
1032
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
+ }
1033
1041
  renderDocs(index) {
1034
1042
  if (typeof index === "number") {
1035
1043
  const a = this.arguments[index];
@@ -4021,7 +4029,8 @@ else group$1.nested.push({
4021
4029
  const chain = isRef(t.parentNode) && t.parentNode.hasChain ? [t.text, ...t.parentNode.chain.map((c) => c.text)] : [t.text];
4022
4030
  const unwound = this.unwindType(targetName, chain);
4023
4031
  const chainResolved = unwound?.def && !isGroup(unwound.def);
4024
- if (!chainResolved && !this.resolveChainWithMerge(targetName, chain)) {
4032
+ const resolvedType = chainResolved ? unwound.def : this.resolveChainWithMerge(targetName, chain);
4033
+ if (!resolvedType) {
4025
4034
  const lastToken = chain.length > 1 && isRef(t.parentNode) ? t.parentNode.chain[t.parentNode.chain.length - 1] || t : t;
4026
4035
  this._allMessages.push({
4027
4036
  severity: 1,
@@ -4042,6 +4051,11 @@ else group$1.nested.push({
4042
4051
  });
4043
4052
  }
4044
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
+ }
4045
4059
  continue;
4046
4060
  }
4047
4061
  if (!this.registry.isDefined(t)) {
@@ -4088,9 +4102,13 @@ else group$1.nested.push({
4088
4102
  * Walks a property chain step-by-step, applying mergeIntersection at each
4089
4103
  * level. Mirrors the completion logic in the LSP server so that intersection
4090
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.
4091
4109
  */ resolveChainWithMerge(targetName, chain) {
4092
4110
  let def = this.unwindType(targetName)?.def;
4093
- if (!def) return false;
4111
+ if (!def) return undefined;
4094
4112
  for (const prop of chain) {
4095
4113
  def = this.mergeIntersection(def);
4096
4114
  if (isProp(def)) {
@@ -4099,15 +4117,16 @@ else group$1.nested.push({
4099
4117
  }
4100
4118
  if (isStructure(def) || isInterface(def)) {
4101
4119
  const next = def.props.get(prop);
4102
- if (!next) return false;
4120
+ if (!next) return undefined;
4103
4121
  def = next.getDefinition() || next;
4104
4122
  } else if (isGroup(def)) {
4105
4123
  const found = this.findPropInGroup(def, prop);
4106
- if (!found) return false;
4124
+ if (!found) return undefined;
4107
4125
  def = found;
4108
- } else return false;
4126
+ } else return undefined;
4109
4127
  }
4110
- return true;
4128
+ if (isRef(def)) def = this.unwindType(def.id, def.chain)?.def || def;
4129
+ return def;
4111
4130
  }
4112
4131
  findPropInGroup(group$1, propName$1) {
4113
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.73",
3
+ "version": "0.1.74",
4
4
  "description": "Core library for Atscript parsing and file generation.",
5
5
  "keywords": [
6
6
  "annotations",