@atscript/core 0.1.30 → 0.1.32

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
@@ -601,7 +601,10 @@ var SemanticPropNode = class extends SemanticNode {
601
601
  registerAtDocument(doc) {
602
602
  super.registerAtDocument(doc);
603
603
  const token = this.token("identifier");
604
- if (token && token.type === "text" && token.multiline) doc.registerMessage(token, "Unexpected end of string");
604
+ if (token) {
605
+ doc.tokensIndex.add(token);
606
+ if (token.type === "text" && token.multiline) doc.registerMessage(token, "Unexpected end of string");
607
+ }
605
608
  }
606
609
  get isPattern() {
607
610
  return !!this.token("identifier")?.pattern;
@@ -2581,7 +2584,49 @@ else {
2581
2584
  }
2582
2585
  return refs;
2583
2586
  }
2584
- if (isProp(token.parentNode)) {} else {
2587
+ if (isProp(token.parentNode)) {
2588
+ const propName$1 = token.parentNode.id;
2589
+ if (!propName$1) return undefined;
2590
+ let parentName;
2591
+ for (const node of this.nodes) if ((isInterface(node) || isStructure(node)) && node.props.get(propName$1) === token.parentNode) {
2592
+ parentName = node.id;
2593
+ break;
2594
+ }
2595
+ if (!parentName) return undefined;
2596
+ const refs = [];
2597
+ const searchDoc = (doc, uri) => {
2598
+ for (const t of doc.referred) {
2599
+ if (t.text !== parentName || !isRef(t.parentNode) || !t.parentNode.hasChain) continue;
2600
+ const chainToken = t.parentNode.chain[0];
2601
+ if (chainToken?.text === propName$1) refs.push({
2602
+ uri,
2603
+ range: chainToken.range,
2604
+ token: chainToken
2605
+ });
2606
+ }
2607
+ for (const node of doc.nodes) {
2608
+ if (!isAnnotate(node) || node.targetName !== parentName) continue;
2609
+ for (const entry of node.entries) if (entry.id === propName$1) {
2610
+ const entryToken = entry.token("identifier");
2611
+ if (entryToken) refs.push({
2612
+ uri,
2613
+ range: entryToken.range,
2614
+ token: entryToken
2615
+ });
2616
+ }
2617
+ }
2618
+ };
2619
+ searchDoc(this, this.id);
2620
+ const parentDefToken = this.registry.definitions.get(parentName);
2621
+ if (parentDefToken?.exported) {
2622
+ const targetName = parentName;
2623
+ for (const d of this.dependants) {
2624
+ const imp = d.imports.get(this.id);
2625
+ if (imp?.imports.find((t) => t.text === targetName)) searchDoc(d, d.id);
2626
+ }
2627
+ }
2628
+ return refs;
2629
+ } else {
2585
2630
  const defForToken = this.getDefinitionFor(token);
2586
2631
  if (defForToken?.token?.isDefinition && defForToken.doc) return defForToken.doc.usageListFor(defForToken.token);
2587
2632
  }
@@ -2655,6 +2700,12 @@ else {
2655
2700
  }];
2656
2701
  return undefined;
2657
2702
  }
2703
+ if (isProp(token.parentNode)) return [{
2704
+ targetUri: this.id,
2705
+ targetRange: token.range,
2706
+ targetSelectionRange: token.range,
2707
+ originSelectionRange: token.range
2708
+ }];
2658
2709
  if (token.isChain && isRef(token.parentNode) && typeof token.index === "number") {
2659
2710
  const id = token.parentNode.id;
2660
2711
  const unwound = this.unwindType(id, token.parentNode.chain.slice(0, token.index));
@@ -3171,6 +3222,34 @@ const zeroRange = {
3171
3222
  //#endregion
3172
3223
  //#region packages/core/src/defaults/db-annotations.ts
3173
3224
  const dbAnnotations = {
3225
+ patch: { strategy: new AnnotationSpec({
3226
+ description: "Defines the **patching strategy** for updating nested objects.\n\n- **\"replace\"** → The field or object will be **fully replaced**.\n- **\"merge\"** → The field or object will be **merged recursively** (applies only to objects, not arrays).\n\n**Example:**\n```atscript\n@db.patch.strategy \"merge\"\nsettings: {\n notifications: boolean\n preferences: {\n theme: string\n }\n}\n```\n",
3227
+ nodeType: ["prop"],
3228
+ multiple: false,
3229
+ argument: {
3230
+ name: "strategy",
3231
+ type: "string",
3232
+ description: "The **patch strategy** for this field: `\"replace\"` (default) or `\"merge\"`.",
3233
+ values: ["replace", "merge"]
3234
+ },
3235
+ validate(token, args, doc) {
3236
+ const field = token.parentNode;
3237
+ const errors = [];
3238
+ const definition$1 = field.getDefinition();
3239
+ if (!definition$1) return errors;
3240
+ let wrongType = false;
3241
+ if (isRef(definition$1)) {
3242
+ const def = doc.unwindType(definition$1.id, definition$1.chain)?.def;
3243
+ if (!isStructure(def) && !isInterface(def) && !isArray(def)) wrongType = true;
3244
+ } else if (!isStructure(definition$1) && !isInterface(definition$1) && !isArray(definition$1)) wrongType = true;
3245
+ if (wrongType) errors.push({
3246
+ message: `@db.patch.strategy requires a field of type object or array`,
3247
+ severity: 1,
3248
+ range: token.range
3249
+ });
3250
+ return errors;
3251
+ }
3252
+ }) },
3174
3253
  table: new AnnotationSpec({
3175
3254
  description: "Marks an interface as a database-persisted entity (table in SQL, collection in MongoDB). If the name argument is omitted, the adapter derives the table name from the interface name.\n\n**Example:**\n```atscript\n@db.table \"users\"\nexport interface User { ... }\n```\n",
3176
3255
  nodeType: ["interface"],
@@ -3222,16 +3301,21 @@ const dbAnnotations = {
3222
3301
  }
3223
3302
  }),
3224
3303
  fulltext: new AnnotationSpec({
3225
- description: "Full-text search index. Fields sharing the same index name form a composite full-text index.\n\n**Example:**\n```atscript\n@db.index.fulltext \"ft_content\"\ntitle: string\n```\n",
3304
+ description: "Full-text search index. Fields sharing the same index name form a composite full-text index.\n\n**Example:**\n```atscript\n@db.index.fulltext \"ft_content\"\ntitle: string\n\n@db.index.fulltext \"ft_content\", 5\nbio: string\n```\n",
3226
3305
  nodeType: ["prop"],
3227
3306
  multiple: true,
3228
3307
  mergeStrategy: "append",
3229
- argument: {
3308
+ argument: [{
3230
3309
  optional: true,
3231
3310
  name: "name",
3232
3311
  type: "string",
3233
3312
  description: "Index name / composite group name."
3234
- }
3313
+ }, {
3314
+ optional: true,
3315
+ name: "weight",
3316
+ type: "number",
3317
+ description: "Field importance in search results (higher = more relevant). Defaults to `1`. Supported by databases with weighted fulltext (e.g., MongoDB, PostgreSQL)."
3318
+ }]
3235
3319
  })
3236
3320
  },
3237
3321
  column: new AnnotationSpec({
@@ -3308,6 +3392,24 @@ else if (fnName === "now" && !["number", "string"].includes(baseType)) errors.pu
3308
3392
  }
3309
3393
  })
3310
3394
  },
3395
+ json: new AnnotationSpec({
3396
+ description: "Forces a field to be stored as a single JSON column instead of being flattened into separate columns. Use on nested object fields that should remain as JSON in the database.\n\n**Example:**\n```atscript\n@db.json\nmetadata: { key: string, value: string }\n```\n",
3397
+ nodeType: ["prop"],
3398
+ validate(token, _args, doc) {
3399
+ const errors = [];
3400
+ const field = token.parentNode;
3401
+ const definition$1 = field.getDefinition();
3402
+ if (definition$1 && isRef(definition$1)) {
3403
+ const unwound = doc.unwindType(definition$1.id, definition$1.chain);
3404
+ if (unwound && isPrimitive(unwound.def)) errors.push({
3405
+ message: "@db.json on a primitive field has no effect — primitive fields are already stored as scalar columns",
3406
+ severity: 2,
3407
+ range: token.range
3408
+ });
3409
+ }
3410
+ return errors;
3411
+ }
3412
+ }),
3311
3413
  ignore: new AnnotationSpec({
3312
3414
  description: "Excludes a field from the database schema. The field exists in the Atscript type but has no column in the DB.\n\n**Example:**\n```atscript\n@db.ignore\ndisplayName: string\n```\n",
3313
3415
  nodeType: ["prop"],
@@ -3501,34 +3603,70 @@ const expectAnnotations = {
3501
3603
  return [];
3502
3604
  }
3503
3605
  }),
3504
- array: { key: new AnnotationSpec({
3505
- description: "Marks a **key field** inside an array. This annotation is used to identify unique fields within an array that can be used as **lookup keys**.\n\n\n\n**Example:**\n```atscript\nexport interface User {\n id: string\n profiles: {\n @expect.array.key\n profileId: string\n name: string\n }[]\n}\n```\n",
3506
- nodeType: ["prop", "type"],
3507
- multiple: false,
3508
- validate(token, args, doc) {
3509
- const field = token.parentNode;
3510
- const errors = [];
3511
- const isOptional = !!field.token("optional");
3512
- if (isOptional) errors.push({
3513
- message: `@expect.array.key can't be optional`,
3514
- severity: 1,
3515
- range: field.token("identifier").range
3516
- });
3517
- const definition$1 = field.getDefinition();
3518
- if (!definition$1) return errors;
3519
- let wrongType = false;
3520
- if (isRef(definition$1)) {
3521
- const def = doc.unwindType(definition$1.id, definition$1.chain)?.def;
3522
- if (isPrimitive(def) && !["string", "number"].includes(def.config.type)) wrongType = true;
3523
- } else wrongType = true;
3524
- if (wrongType) errors.push({
3525
- message: `@expect.array.key must be of type string or number`,
3526
- severity: 1,
3527
- range: token.range
3528
- });
3529
- return errors;
3530
- }
3531
- }) }
3606
+ array: {
3607
+ uniqueItems: new AnnotationSpec({
3608
+ description: "Enforces **unique items** in an array field.\n\nWorks with both **primitive arrays** and **object arrays**:\n\n- **Primitive arrays** (`string[]`, `number[]`) — duplicates are detected by deep equality.\n- **Object arrays** — if the element type defines `@expect.array.key` properties, uniqueness is checked by those keys only; otherwise by deep equality of the whole object.\n\nUnlike `@expect.array.key` (which only *identifies* key fields for lookup/patch operations), `@expect.array.uniqueItems` actively *enforces* uniqueness during validation.\n\n**Examples:**\n```atscript\n// Primitive array — no duplicates allowed\n@expect.array.uniqueItems\ntags: string[]\n\n// Object array with keys — unique by key fields\n@expect.array.uniqueItems\nitems: {\n @expect.array.key\n id: string\n value: number\n}[]\n```\n",
3609
+ nodeType: ["prop"],
3610
+ multiple: false,
3611
+ argument: [{
3612
+ name: "message",
3613
+ optional: true,
3614
+ type: "string",
3615
+ description: "Optional custom error message when duplicate items are found."
3616
+ }],
3617
+ validate(token, args, doc) {
3618
+ const field = token.parentNode;
3619
+ const errors = [];
3620
+ const definition$1 = field.getDefinition();
3621
+ if (!definition$1) return errors;
3622
+ let wrongType = false;
3623
+ if (isRef(definition$1)) {
3624
+ const def = doc.unwindType(definition$1.id, definition$1.chain)?.def;
3625
+ if (!isArray(def)) wrongType = true;
3626
+ } else if (!isArray(definition$1)) wrongType = true;
3627
+ if (wrongType) errors.push({
3628
+ message: `@expect.array.uniqueItems requires an array field`,
3629
+ severity: 1,
3630
+ range: token.range
3631
+ });
3632
+ return errors;
3633
+ }
3634
+ }),
3635
+ key: new AnnotationSpec({
3636
+ description: "Marks a **key field** inside an array of objects. Key fields *identify* elements for lookup and patch operations (`$upsert`, `$update`, `$remove`).\n\n`@expect.array.key` does **not** enforce uniqueness by itself — it only declares which fields form the element identity. To enforce that no two elements share the same key, also add `@expect.array.uniqueItems` on the array field.\n\nMultiple key fields form a **composite key** (all must match for elements to be considered equal).\n\n**Example:**\n```atscript\n@expect.array.uniqueItems // enforce uniqueness by key\nprofiles: {\n @expect.array.key\n profileId: string\n name: string\n}[]\n```\n",
3637
+ nodeType: ["prop", "type"],
3638
+ multiple: false,
3639
+ argument: [{
3640
+ name: "message",
3641
+ optional: true,
3642
+ type: "string",
3643
+ description: "Optional custom error message (used by @expect.array.uniqueItems when checking key-based uniqueness)."
3644
+ }],
3645
+ validate(token, args, doc) {
3646
+ const field = token.parentNode;
3647
+ const errors = [];
3648
+ const isOptional = !!field.token("optional");
3649
+ if (isOptional) errors.push({
3650
+ message: `@expect.array.key can't be optional`,
3651
+ severity: 1,
3652
+ range: field.token("identifier").range
3653
+ });
3654
+ const definition$1 = field.getDefinition();
3655
+ if (!definition$1) return errors;
3656
+ let wrongType = false;
3657
+ if (isRef(definition$1)) {
3658
+ const def = doc.unwindType(definition$1.id, definition$1.chain)?.def;
3659
+ if (isPrimitive(def) && !["string", "number"].includes(def.config.type)) wrongType = true;
3660
+ } else wrongType = true;
3661
+ if (wrongType) errors.push({
3662
+ message: `@expect.array.key must be of type string or number`,
3663
+ severity: 1,
3664
+ range: token.range
3665
+ });
3666
+ return errors;
3667
+ }
3668
+ })
3669
+ }
3532
3670
  };
3533
3671
 
3534
3672
  //#endregion
@@ -4328,6 +4466,251 @@ var BuildRepo = class {
4328
4466
  }
4329
4467
  };
4330
4468
 
4469
+ //#endregion
4470
+ //#region packages/core/src/flatten.ts
4471
+ function flattenInterfaceNode(doc, node) {
4472
+ const flatMap = new Map();
4473
+ const stack = new Set();
4474
+ let struct;
4475
+ if (node.hasExtends) struct = doc.resolveInterfaceExtends(node);
4476
+ if (!struct) struct = node.getDefinition();
4477
+ if (!struct || !isStructure(struct)) return flatMap;
4478
+ walkStructure(doc, struct, "", false, flatMap, stack);
4479
+ return flatMap;
4480
+ }
4481
+ /**
4482
+ * Checks whether a property has the `@db.json` annotation.
4483
+ *
4484
+ * NOTE: This is intentionally hardcoded to `db.json` (rather than being
4485
+ * configurable) because the flattening behavior is tightly coupled to DB
4486
+ * semantics — `@db.json` fields are stored as a single JSON column, so their
4487
+ * sub-paths must not appear in the flat map (they are not individually
4488
+ * queryable in filters or selectable in projections).
4489
+ */ function hasDbJsonAnnotation(prop) {
4490
+ if (!prop.annotations) return false;
4491
+ return prop.annotations.some((a) => a.name === "db.json");
4492
+ }
4493
+ function isPhantomNode(doc, def) {
4494
+ if (!def) return false;
4495
+ if (isPrimitive(def) && def.config.type === "phantom") return true;
4496
+ if (isRef(def)) {
4497
+ const unwound = doc.unwindType(def.id, def.chain);
4498
+ return !!(unwound?.def && isPrimitive(unwound.def) && unwound.def.config.type === "phantom");
4499
+ }
4500
+ return false;
4501
+ }
4502
+ function resolveNode(doc, def) {
4503
+ if (isRef(def)) {
4504
+ const ref$1 = def;
4505
+ const unwound = doc.unwindType(ref$1.id, ref$1.chain);
4506
+ if (unwound?.def) return resolveNode(unwound.doc, unwound.def);
4507
+ return undefined;
4508
+ }
4509
+ if (isInterface(def)) {
4510
+ const iface = def;
4511
+ let struct;
4512
+ if (iface.hasExtends) struct = doc.resolveInterfaceExtends(iface);
4513
+ if (!struct) struct = iface.getDefinition();
4514
+ if (struct) return {
4515
+ def: struct,
4516
+ doc
4517
+ };
4518
+ return undefined;
4519
+ }
4520
+ return {
4521
+ def,
4522
+ doc
4523
+ };
4524
+ }
4525
+ function isComplexNode(def) {
4526
+ return isStructure(def) || isArray(def) || isGroup(def) && def.entity !== "tuple";
4527
+ }
4528
+ function addToFlatMap(flatMap, path$5, descriptor) {
4529
+ const existing = flatMap.get(path$5);
4530
+ if (existing) {
4531
+ const group$1 = new SemanticGroup([existing.def, descriptor.def], "|");
4532
+ flatMap.set(path$5, {
4533
+ def: group$1,
4534
+ doc: existing.doc,
4535
+ optional: existing.optional || descriptor.optional,
4536
+ propNode: existing.propNode,
4537
+ intermediate: existing.intermediate && descriptor.intermediate ? true : undefined
4538
+ });
4539
+ } else flatMap.set(path$5, descriptor);
4540
+ }
4541
+ function walkStructure(doc, struct, prefix, parentOptional, flatMap, stack) {
4542
+ for (const [name, prop] of struct.props) {
4543
+ if (prop.token("identifier")?.pattern) continue;
4544
+ const propDef = prop.getDefinition();
4545
+ if (isPhantomNode(doc, propDef)) continue;
4546
+ const path$5 = prefix ? `${prefix}.${name}` : name;
4547
+ const optional = parentOptional || !!prop.token("optional");
4548
+ const dbJson = hasDbJsonAnnotation(prop);
4549
+ if (!propDef) {
4550
+ addToFlatMap(flatMap, path$5, {
4551
+ def: prop,
4552
+ doc,
4553
+ optional,
4554
+ propNode: prop
4555
+ });
4556
+ continue;
4557
+ }
4558
+ const resolved = resolveNode(doc, propDef);
4559
+ if (!resolved) {
4560
+ addToFlatMap(flatMap, path$5, {
4561
+ def: propDef,
4562
+ doc,
4563
+ optional,
4564
+ propNode: prop
4565
+ });
4566
+ continue;
4567
+ }
4568
+ if (dbJson) {
4569
+ addToFlatMap(flatMap, path$5, {
4570
+ def: resolved.def,
4571
+ doc: resolved.doc,
4572
+ optional,
4573
+ propNode: prop,
4574
+ dbJson: true
4575
+ });
4576
+ continue;
4577
+ }
4578
+ walkNode(resolved.doc, resolved.def, path$5, optional, prop, flatMap, stack);
4579
+ }
4580
+ }
4581
+ function walkNode(doc, def, path$5, optional, propNode, flatMap, stack) {
4582
+ if (stack.has(def)) {
4583
+ addToFlatMap(flatMap, path$5, {
4584
+ def,
4585
+ doc,
4586
+ optional,
4587
+ propNode
4588
+ });
4589
+ return;
4590
+ }
4591
+ stack.add(def);
4592
+ try {
4593
+ if (isStructure(def)) {
4594
+ addToFlatMap(flatMap, path$5, {
4595
+ def,
4596
+ doc,
4597
+ optional,
4598
+ propNode,
4599
+ intermediate: true
4600
+ });
4601
+ walkStructure(doc, def, path$5, optional, flatMap, stack);
4602
+ } else if (isArray(def)) {
4603
+ const elementDef = def.getDefinition();
4604
+ const resolvedElement = elementDef ? resolveNode(doc, elementDef) : undefined;
4605
+ const isComplexElement = resolvedElement && (isStructure(resolvedElement.def) || isGroup(resolvedElement.def) && resolvedElement.def.entity !== "tuple" || isArray(resolvedElement.def));
4606
+ if (isComplexElement) {
4607
+ addToFlatMap(flatMap, path$5, {
4608
+ def,
4609
+ doc,
4610
+ optional,
4611
+ propNode,
4612
+ intermediate: true
4613
+ });
4614
+ walkArrayElement(doc, def, path$5, optional, flatMap, stack);
4615
+ } else addToFlatMap(flatMap, path$5, {
4616
+ def,
4617
+ doc,
4618
+ optional,
4619
+ propNode
4620
+ });
4621
+ } else if (isGroup(def) && def.entity !== "structure") {
4622
+ const group$1 = def;
4623
+ if (group$1.op === "&") {
4624
+ const merged = doc.mergeIntersection(def);
4625
+ if (merged !== def && isStructure(merged)) {
4626
+ addToFlatMap(flatMap, path$5, {
4627
+ def: merged,
4628
+ doc,
4629
+ optional,
4630
+ propNode,
4631
+ intermediate: true
4632
+ });
4633
+ walkStructure(doc, merged, path$5, optional, flatMap, stack);
4634
+ } else addToFlatMap(flatMap, path$5, {
4635
+ def,
4636
+ doc,
4637
+ optional,
4638
+ propNode
4639
+ });
4640
+ } else if (group$1.op === "|") {
4641
+ const branches = group$1.unwrap();
4642
+ let hasComplexBranch = false;
4643
+ let hasLeafBranch = false;
4644
+ for (const branch of branches) {
4645
+ const resolved = resolveNode(doc, branch);
4646
+ if (resolved && isComplexNode(resolved.def)) hasComplexBranch = true;
4647
+ else hasLeafBranch = true;
4648
+ }
4649
+ if (hasComplexBranch) {
4650
+ if (hasLeafBranch) addToFlatMap(flatMap, path$5, {
4651
+ def,
4652
+ doc,
4653
+ optional,
4654
+ propNode
4655
+ });
4656
+ else addToFlatMap(flatMap, path$5, {
4657
+ def,
4658
+ doc,
4659
+ optional,
4660
+ propNode,
4661
+ intermediate: true
4662
+ });
4663
+ for (const branch of branches) {
4664
+ const resolved = resolveNode(doc, branch);
4665
+ if (resolved) walkBranch(resolved.doc, resolved.def, path$5, optional, flatMap, stack);
4666
+ }
4667
+ } else addToFlatMap(flatMap, path$5, {
4668
+ def,
4669
+ doc,
4670
+ optional,
4671
+ propNode
4672
+ });
4673
+ } else addToFlatMap(flatMap, path$5, {
4674
+ def,
4675
+ doc,
4676
+ optional,
4677
+ propNode
4678
+ });
4679
+ } else addToFlatMap(flatMap, path$5, {
4680
+ def,
4681
+ doc,
4682
+ optional,
4683
+ propNode
4684
+ });
4685
+ } finally {
4686
+ stack.delete(def);
4687
+ }
4688
+ }
4689
+ function walkArrayElement(doc, arrayNode, prefix, parentOptional, flatMap, stack) {
4690
+ const elementDef = arrayNode.getDefinition();
4691
+ if (!elementDef) return;
4692
+ const resolved = resolveNode(doc, elementDef);
4693
+ if (!resolved) return;
4694
+ walkBranch(resolved.doc, resolved.def, prefix, parentOptional, flatMap, stack);
4695
+ }
4696
+ /**
4697
+ * Walks a branch (union branch or array element) to collect sub-paths.
4698
+ * Does NOT add an entry for the branch itself — the parent already did that.
4699
+ */ function walkBranch(doc, def, prefix, parentOptional, flatMap, stack) {
4700
+ if (isStructure(def)) walkStructure(doc, def, prefix, parentOptional, flatMap, stack);
4701
+ else if (isArray(def)) walkArrayElement(doc, def, prefix, parentOptional, flatMap, stack);
4702
+ else if (isGroup(def) && def.entity !== "structure") {
4703
+ const group$1 = def;
4704
+ if (group$1.op === "&") {
4705
+ const merged = doc.mergeIntersection(def);
4706
+ if (isStructure(merged)) walkStructure(doc, merged, prefix, parentOptional, flatMap, stack);
4707
+ } else if (group$1.op === "|") for (const branch of group$1.unwrap()) {
4708
+ const resolved = resolveNode(doc, branch);
4709
+ if (resolved) walkBranch(resolved.doc, resolved.def, prefix, parentOptional, flatMap, stack);
4710
+ }
4711
+ }
4712
+ }
4713
+
4331
4714
  //#endregion
4332
4715
  exports.$n = $n
4333
4716
  exports.AnnotationSpec = AnnotationSpec
@@ -4353,6 +4736,7 @@ exports.Token = Token
4353
4736
  exports.build = build
4354
4737
  exports.createAtscriptPlugin = createAtscriptPlugin
4355
4738
  exports.defineConfig = defineConfig
4739
+ exports.flattenInterfaceNode = flattenInterfaceNode
4356
4740
  exports.getRelPath = getRelPath
4357
4741
  exports.isAnnotate = isAnnotate
4358
4742
  exports.isAnnotationSpec = isAnnotationSpec
@@ -4361,6 +4745,7 @@ exports.isConst = isConst
4361
4745
  exports.isGroup = isGroup
4362
4746
  exports.isImport = isImport
4363
4747
  exports.isInterface = isInterface
4748
+ exports.isPhantomNode = isPhantomNode
4364
4749
  exports.isPrimitive = isPrimitive
4365
4750
  exports.isProp = isProp
4366
4751
  exports.isRef = isRef
package/dist/index.d.ts CHANGED
@@ -781,5 +781,51 @@ declare function resolveAnnotation(name: string, annotationsTree?: TAnnotationsT
781
781
  declare function resolveAtscriptFromPath(from: string, id: string): string;
782
782
  declare function getRelPath(fromUri: string, toUri: string): string;
783
783
 
784
- export { $n, AnnotationSpec, AtscriptDoc, AtscriptRepo, BuildRepo, DEFAULT_FORMAT, PluginManager, SemanticAnnotateNode, SemanticArrayNode, SemanticConstNode, SemanticGroup, SemanticImportNode, SemanticInterfaceNode, SemanticNode, SemanticPrimitiveNode, SemanticPropNode, SemanticRefNode, SemanticStructureNode, SemanticTupleNode, SemanticTypeNode, Token, build, createAtscriptPlugin, defineConfig, getRelPath, isAnnotate, isAnnotationSpec, isArray, isConst, isGroup, isImport, isInterface, isPrimitive, isProp, isRef, isStructure, isTuple, isType, loadConfig, loadTsConfig, resolveAnnotation, resolveAtscriptFromPath, resolveConfigFile };
785
- export type { TAnnotationArgument, TAnnotationSpecConfig, TAnnotationTokens, TAnnotationsTree, TAtscriptConfig, TAtscriptConfigInput, TAtscriptConfigOutput, TAtscriptDocConfig, TAtscriptPlugin, TAtscriptRenderFormat, TMessages, TNodeEntity, TOutput, TOutputWithSource, TPluginManagers, TPluginOutput, TPrimitiveAnnotationArg, TPrimitiveAnnotationArgs, TPrimitiveAnnotationValue, TPrimitiveBaseConfig, TPrimitiveConfig, TPrimitiveTypeArray, TPrimitiveTypeComplex, TPrimitiveTypeDef, TPrimitiveTypeFinal, TPrimitiveTypeFinalOptional, TPrimitiveTypeObject, TSemanticToken };
784
+ /**
785
+ * Descriptor for a single field in the flattened interface map.
786
+ */
787
+ interface TFlatFieldDescriptor {
788
+ /** The resolved type definition node. */
789
+ def: SemanticNode;
790
+ /** The owning document (may differ from root for cross-file refs). */
791
+ doc: AtscriptDoc;
792
+ /** Whether the field is optional. */
793
+ optional: boolean;
794
+ /** The property node (for annotation access). Undefined for the root entry. */
795
+ propNode?: SemanticPropNode;
796
+ /**
797
+ * Whether this is an intermediate (non-leaf) path — a structure, array of structures,
798
+ * or union of only complex types. Intermediate entries should be typed as `never`
799
+ * in the rendered output to prevent meaningless `$eq` comparisons, while still
800
+ * appearing in autocomplete for `$select` and `$exists` checks.
801
+ */
802
+ intermediate?: boolean;
803
+ /** Whether this field has the `@db.json` annotation (stored as JSON string in DB). */
804
+ dbJson?: boolean;
805
+ }
806
+ /**
807
+ * Flattens an interface node into a map of dot-notation paths to their type descriptors.
808
+ *
809
+ * This utility exists to improve type-safety for filter expressions and `$select`/`$sort`
810
+ * operations in the DB layer. The generated `__flat` static property on `@db.table`
811
+ * interfaces uses this map to provide autocomplete and compile-time checks for
812
+ * dot-notation query paths.
813
+ *
814
+ * Walks the interface structure (including inherited props from `extends`) and collects
815
+ * all reachable field paths. Nested objects are recursed into, arrays are recursed through
816
+ * their element types, and unions/intersections are recursed through each branch (with
817
+ * colliding paths merged into union groups).
818
+ *
819
+ * Fields annotated with `@db.json` are treated as leaves — their sub-paths are NOT included,
820
+ * because `@db.json` fields are stored as a single JSON column in the database and their
821
+ * sub-paths are not individually queryable.
822
+ *
823
+ * @param doc - The document owning the interface node.
824
+ * @param node - The interface node to flatten.
825
+ * @returns A map of dot-separated paths to their field descriptors.
826
+ */
827
+ declare function flattenInterfaceNode(doc: AtscriptDoc, node: SemanticInterfaceNode): Map<string, TFlatFieldDescriptor>;
828
+ declare function isPhantomNode(doc: AtscriptDoc, def?: SemanticNode): boolean;
829
+
830
+ export { $n, AnnotationSpec, AtscriptDoc, AtscriptRepo, BuildRepo, DEFAULT_FORMAT, PluginManager, SemanticAnnotateNode, SemanticArrayNode, SemanticConstNode, SemanticGroup, SemanticImportNode, SemanticInterfaceNode, SemanticNode, SemanticPrimitiveNode, SemanticPropNode, SemanticRefNode, SemanticStructureNode, SemanticTupleNode, SemanticTypeNode, Token, build, createAtscriptPlugin, defineConfig, flattenInterfaceNode, getRelPath, isAnnotate, isAnnotationSpec, isArray, isConst, isGroup, isImport, isInterface, isPhantomNode, isPrimitive, isProp, isRef, isStructure, isTuple, isType, loadConfig, loadTsConfig, resolveAnnotation, resolveAtscriptFromPath, resolveConfigFile };
831
+ export type { TAnnotationArgument, TAnnotationSpecConfig, TAnnotationTokens, TAnnotationsTree, TAtscriptConfig, TAtscriptConfigInput, TAtscriptConfigOutput, TAtscriptDocConfig, TAtscriptPlugin, TAtscriptRenderFormat, TFlatFieldDescriptor, TMessages, TNodeEntity, TOutput, TOutputWithSource, TPluginManagers, TPluginOutput, TPrimitiveAnnotationArg, TPrimitiveAnnotationArgs, TPrimitiveAnnotationValue, TPrimitiveBaseConfig, TPrimitiveConfig, TPrimitiveTypeArray, TPrimitiveTypeComplex, TPrimitiveTypeDef, TPrimitiveTypeFinal, TPrimitiveTypeFinalOptional, TPrimitiveTypeObject, TSemanticToken };
package/dist/index.mjs CHANGED
@@ -577,7 +577,10 @@ var SemanticPropNode = class extends SemanticNode {
577
577
  registerAtDocument(doc) {
578
578
  super.registerAtDocument(doc);
579
579
  const token = this.token("identifier");
580
- if (token && token.type === "text" && token.multiline) doc.registerMessage(token, "Unexpected end of string");
580
+ if (token) {
581
+ doc.tokensIndex.add(token);
582
+ if (token.type === "text" && token.multiline) doc.registerMessage(token, "Unexpected end of string");
583
+ }
581
584
  }
582
585
  get isPattern() {
583
586
  return !!this.token("identifier")?.pattern;
@@ -2557,7 +2560,49 @@ else {
2557
2560
  }
2558
2561
  return refs;
2559
2562
  }
2560
- if (isProp(token.parentNode)) {} else {
2563
+ if (isProp(token.parentNode)) {
2564
+ const propName$1 = token.parentNode.id;
2565
+ if (!propName$1) return undefined;
2566
+ let parentName;
2567
+ for (const node of this.nodes) if ((isInterface(node) || isStructure(node)) && node.props.get(propName$1) === token.parentNode) {
2568
+ parentName = node.id;
2569
+ break;
2570
+ }
2571
+ if (!parentName) return undefined;
2572
+ const refs = [];
2573
+ const searchDoc = (doc, uri) => {
2574
+ for (const t of doc.referred) {
2575
+ if (t.text !== parentName || !isRef(t.parentNode) || !t.parentNode.hasChain) continue;
2576
+ const chainToken = t.parentNode.chain[0];
2577
+ if (chainToken?.text === propName$1) refs.push({
2578
+ uri,
2579
+ range: chainToken.range,
2580
+ token: chainToken
2581
+ });
2582
+ }
2583
+ for (const node of doc.nodes) {
2584
+ if (!isAnnotate(node) || node.targetName !== parentName) continue;
2585
+ for (const entry of node.entries) if (entry.id === propName$1) {
2586
+ const entryToken = entry.token("identifier");
2587
+ if (entryToken) refs.push({
2588
+ uri,
2589
+ range: entryToken.range,
2590
+ token: entryToken
2591
+ });
2592
+ }
2593
+ }
2594
+ };
2595
+ searchDoc(this, this.id);
2596
+ const parentDefToken = this.registry.definitions.get(parentName);
2597
+ if (parentDefToken?.exported) {
2598
+ const targetName = parentName;
2599
+ for (const d of this.dependants) {
2600
+ const imp = d.imports.get(this.id);
2601
+ if (imp?.imports.find((t) => t.text === targetName)) searchDoc(d, d.id);
2602
+ }
2603
+ }
2604
+ return refs;
2605
+ } else {
2561
2606
  const defForToken = this.getDefinitionFor(token);
2562
2607
  if (defForToken?.token?.isDefinition && defForToken.doc) return defForToken.doc.usageListFor(defForToken.token);
2563
2608
  }
@@ -2631,6 +2676,12 @@ else {
2631
2676
  }];
2632
2677
  return undefined;
2633
2678
  }
2679
+ if (isProp(token.parentNode)) return [{
2680
+ targetUri: this.id,
2681
+ targetRange: token.range,
2682
+ targetSelectionRange: token.range,
2683
+ originSelectionRange: token.range
2684
+ }];
2634
2685
  if (token.isChain && isRef(token.parentNode) && typeof token.index === "number") {
2635
2686
  const id = token.parentNode.id;
2636
2687
  const unwound = this.unwindType(id, token.parentNode.chain.slice(0, token.index));
@@ -3147,6 +3198,34 @@ const zeroRange = {
3147
3198
  //#endregion
3148
3199
  //#region packages/core/src/defaults/db-annotations.ts
3149
3200
  const dbAnnotations = {
3201
+ patch: { strategy: new AnnotationSpec({
3202
+ description: "Defines the **patching strategy** for updating nested objects.\n\n- **\"replace\"** → The field or object will be **fully replaced**.\n- **\"merge\"** → The field or object will be **merged recursively** (applies only to objects, not arrays).\n\n**Example:**\n```atscript\n@db.patch.strategy \"merge\"\nsettings: {\n notifications: boolean\n preferences: {\n theme: string\n }\n}\n```\n",
3203
+ nodeType: ["prop"],
3204
+ multiple: false,
3205
+ argument: {
3206
+ name: "strategy",
3207
+ type: "string",
3208
+ description: "The **patch strategy** for this field: `\"replace\"` (default) or `\"merge\"`.",
3209
+ values: ["replace", "merge"]
3210
+ },
3211
+ validate(token, args, doc) {
3212
+ const field = token.parentNode;
3213
+ const errors = [];
3214
+ const definition$1 = field.getDefinition();
3215
+ if (!definition$1) return errors;
3216
+ let wrongType = false;
3217
+ if (isRef(definition$1)) {
3218
+ const def = doc.unwindType(definition$1.id, definition$1.chain)?.def;
3219
+ if (!isStructure(def) && !isInterface(def) && !isArray(def)) wrongType = true;
3220
+ } else if (!isStructure(definition$1) && !isInterface(definition$1) && !isArray(definition$1)) wrongType = true;
3221
+ if (wrongType) errors.push({
3222
+ message: `@db.patch.strategy requires a field of type object or array`,
3223
+ severity: 1,
3224
+ range: token.range
3225
+ });
3226
+ return errors;
3227
+ }
3228
+ }) },
3150
3229
  table: new AnnotationSpec({
3151
3230
  description: "Marks an interface as a database-persisted entity (table in SQL, collection in MongoDB). If the name argument is omitted, the adapter derives the table name from the interface name.\n\n**Example:**\n```atscript\n@db.table \"users\"\nexport interface User { ... }\n```\n",
3152
3231
  nodeType: ["interface"],
@@ -3198,16 +3277,21 @@ const dbAnnotations = {
3198
3277
  }
3199
3278
  }),
3200
3279
  fulltext: new AnnotationSpec({
3201
- description: "Full-text search index. Fields sharing the same index name form a composite full-text index.\n\n**Example:**\n```atscript\n@db.index.fulltext \"ft_content\"\ntitle: string\n```\n",
3280
+ description: "Full-text search index. Fields sharing the same index name form a composite full-text index.\n\n**Example:**\n```atscript\n@db.index.fulltext \"ft_content\"\ntitle: string\n\n@db.index.fulltext \"ft_content\", 5\nbio: string\n```\n",
3202
3281
  nodeType: ["prop"],
3203
3282
  multiple: true,
3204
3283
  mergeStrategy: "append",
3205
- argument: {
3284
+ argument: [{
3206
3285
  optional: true,
3207
3286
  name: "name",
3208
3287
  type: "string",
3209
3288
  description: "Index name / composite group name."
3210
- }
3289
+ }, {
3290
+ optional: true,
3291
+ name: "weight",
3292
+ type: "number",
3293
+ description: "Field importance in search results (higher = more relevant). Defaults to `1`. Supported by databases with weighted fulltext (e.g., MongoDB, PostgreSQL)."
3294
+ }]
3211
3295
  })
3212
3296
  },
3213
3297
  column: new AnnotationSpec({
@@ -3284,6 +3368,24 @@ else if (fnName === "now" && !["number", "string"].includes(baseType)) errors.pu
3284
3368
  }
3285
3369
  })
3286
3370
  },
3371
+ json: new AnnotationSpec({
3372
+ description: "Forces a field to be stored as a single JSON column instead of being flattened into separate columns. Use on nested object fields that should remain as JSON in the database.\n\n**Example:**\n```atscript\n@db.json\nmetadata: { key: string, value: string }\n```\n",
3373
+ nodeType: ["prop"],
3374
+ validate(token, _args, doc) {
3375
+ const errors = [];
3376
+ const field = token.parentNode;
3377
+ const definition$1 = field.getDefinition();
3378
+ if (definition$1 && isRef(definition$1)) {
3379
+ const unwound = doc.unwindType(definition$1.id, definition$1.chain);
3380
+ if (unwound && isPrimitive(unwound.def)) errors.push({
3381
+ message: "@db.json on a primitive field has no effect — primitive fields are already stored as scalar columns",
3382
+ severity: 2,
3383
+ range: token.range
3384
+ });
3385
+ }
3386
+ return errors;
3387
+ }
3388
+ }),
3287
3389
  ignore: new AnnotationSpec({
3288
3390
  description: "Excludes a field from the database schema. The field exists in the Atscript type but has no column in the DB.\n\n**Example:**\n```atscript\n@db.ignore\ndisplayName: string\n```\n",
3289
3391
  nodeType: ["prop"],
@@ -3477,34 +3579,70 @@ const expectAnnotations = {
3477
3579
  return [];
3478
3580
  }
3479
3581
  }),
3480
- array: { key: new AnnotationSpec({
3481
- description: "Marks a **key field** inside an array. This annotation is used to identify unique fields within an array that can be used as **lookup keys**.\n\n\n\n**Example:**\n```atscript\nexport interface User {\n id: string\n profiles: {\n @expect.array.key\n profileId: string\n name: string\n }[]\n}\n```\n",
3482
- nodeType: ["prop", "type"],
3483
- multiple: false,
3484
- validate(token, args, doc) {
3485
- const field = token.parentNode;
3486
- const errors = [];
3487
- const isOptional = !!field.token("optional");
3488
- if (isOptional) errors.push({
3489
- message: `@expect.array.key can't be optional`,
3490
- severity: 1,
3491
- range: field.token("identifier").range
3492
- });
3493
- const definition$1 = field.getDefinition();
3494
- if (!definition$1) return errors;
3495
- let wrongType = false;
3496
- if (isRef(definition$1)) {
3497
- const def = doc.unwindType(definition$1.id, definition$1.chain)?.def;
3498
- if (isPrimitive(def) && !["string", "number"].includes(def.config.type)) wrongType = true;
3499
- } else wrongType = true;
3500
- if (wrongType) errors.push({
3501
- message: `@expect.array.key must be of type string or number`,
3502
- severity: 1,
3503
- range: token.range
3504
- });
3505
- return errors;
3506
- }
3507
- }) }
3582
+ array: {
3583
+ uniqueItems: new AnnotationSpec({
3584
+ description: "Enforces **unique items** in an array field.\n\nWorks with both **primitive arrays** and **object arrays**:\n\n- **Primitive arrays** (`string[]`, `number[]`) — duplicates are detected by deep equality.\n- **Object arrays** — if the element type defines `@expect.array.key` properties, uniqueness is checked by those keys only; otherwise by deep equality of the whole object.\n\nUnlike `@expect.array.key` (which only *identifies* key fields for lookup/patch operations), `@expect.array.uniqueItems` actively *enforces* uniqueness during validation.\n\n**Examples:**\n```atscript\n// Primitive array — no duplicates allowed\n@expect.array.uniqueItems\ntags: string[]\n\n// Object array with keys — unique by key fields\n@expect.array.uniqueItems\nitems: {\n @expect.array.key\n id: string\n value: number\n}[]\n```\n",
3585
+ nodeType: ["prop"],
3586
+ multiple: false,
3587
+ argument: [{
3588
+ name: "message",
3589
+ optional: true,
3590
+ type: "string",
3591
+ description: "Optional custom error message when duplicate items are found."
3592
+ }],
3593
+ validate(token, args, doc) {
3594
+ const field = token.parentNode;
3595
+ const errors = [];
3596
+ const definition$1 = field.getDefinition();
3597
+ if (!definition$1) return errors;
3598
+ let wrongType = false;
3599
+ if (isRef(definition$1)) {
3600
+ const def = doc.unwindType(definition$1.id, definition$1.chain)?.def;
3601
+ if (!isArray(def)) wrongType = true;
3602
+ } else if (!isArray(definition$1)) wrongType = true;
3603
+ if (wrongType) errors.push({
3604
+ message: `@expect.array.uniqueItems requires an array field`,
3605
+ severity: 1,
3606
+ range: token.range
3607
+ });
3608
+ return errors;
3609
+ }
3610
+ }),
3611
+ key: new AnnotationSpec({
3612
+ description: "Marks a **key field** inside an array of objects. Key fields *identify* elements for lookup and patch operations (`$upsert`, `$update`, `$remove`).\n\n`@expect.array.key` does **not** enforce uniqueness by itself — it only declares which fields form the element identity. To enforce that no two elements share the same key, also add `@expect.array.uniqueItems` on the array field.\n\nMultiple key fields form a **composite key** (all must match for elements to be considered equal).\n\n**Example:**\n```atscript\n@expect.array.uniqueItems // enforce uniqueness by key\nprofiles: {\n @expect.array.key\n profileId: string\n name: string\n}[]\n```\n",
3613
+ nodeType: ["prop", "type"],
3614
+ multiple: false,
3615
+ argument: [{
3616
+ name: "message",
3617
+ optional: true,
3618
+ type: "string",
3619
+ description: "Optional custom error message (used by @expect.array.uniqueItems when checking key-based uniqueness)."
3620
+ }],
3621
+ validate(token, args, doc) {
3622
+ const field = token.parentNode;
3623
+ const errors = [];
3624
+ const isOptional = !!field.token("optional");
3625
+ if (isOptional) errors.push({
3626
+ message: `@expect.array.key can't be optional`,
3627
+ severity: 1,
3628
+ range: field.token("identifier").range
3629
+ });
3630
+ const definition$1 = field.getDefinition();
3631
+ if (!definition$1) return errors;
3632
+ let wrongType = false;
3633
+ if (isRef(definition$1)) {
3634
+ const def = doc.unwindType(definition$1.id, definition$1.chain)?.def;
3635
+ if (isPrimitive(def) && !["string", "number"].includes(def.config.type)) wrongType = true;
3636
+ } else wrongType = true;
3637
+ if (wrongType) errors.push({
3638
+ message: `@expect.array.key must be of type string or number`,
3639
+ severity: 1,
3640
+ range: token.range
3641
+ });
3642
+ return errors;
3643
+ }
3644
+ })
3645
+ }
3508
3646
  };
3509
3647
 
3510
3648
  //#endregion
@@ -4305,4 +4443,249 @@ var BuildRepo = class {
4305
4443
  };
4306
4444
 
4307
4445
  //#endregion
4308
- export { $n, AnnotationSpec, AtscriptDoc, AtscriptRepo, BuildRepo, DEFAULT_FORMAT, PluginManager, SemanticAnnotateNode, SemanticArrayNode, SemanticConstNode, SemanticGroup, SemanticImportNode, SemanticInterfaceNode, SemanticNode, SemanticPrimitiveNode, SemanticPropNode, SemanticRefNode, SemanticStructureNode, SemanticTupleNode, SemanticTypeNode, Token, build, createAtscriptPlugin, defineConfig, getRelPath, isAnnotate, isAnnotationSpec, isArray, isConst, isGroup, isImport, isInterface, isPrimitive, isProp, isRef, isStructure, isTuple, isType, loadConfig, loadTsConfig, resolveAnnotation, resolveAtscriptFromPath, resolveConfigFile };
4446
+ //#region packages/core/src/flatten.ts
4447
+ function flattenInterfaceNode(doc, node) {
4448
+ const flatMap = new Map();
4449
+ const stack = new Set();
4450
+ let struct;
4451
+ if (node.hasExtends) struct = doc.resolveInterfaceExtends(node);
4452
+ if (!struct) struct = node.getDefinition();
4453
+ if (!struct || !isStructure(struct)) return flatMap;
4454
+ walkStructure(doc, struct, "", false, flatMap, stack);
4455
+ return flatMap;
4456
+ }
4457
+ /**
4458
+ * Checks whether a property has the `@db.json` annotation.
4459
+ *
4460
+ * NOTE: This is intentionally hardcoded to `db.json` (rather than being
4461
+ * configurable) because the flattening behavior is tightly coupled to DB
4462
+ * semantics — `@db.json` fields are stored as a single JSON column, so their
4463
+ * sub-paths must not appear in the flat map (they are not individually
4464
+ * queryable in filters or selectable in projections).
4465
+ */ function hasDbJsonAnnotation(prop) {
4466
+ if (!prop.annotations) return false;
4467
+ return prop.annotations.some((a) => a.name === "db.json");
4468
+ }
4469
+ function isPhantomNode(doc, def) {
4470
+ if (!def) return false;
4471
+ if (isPrimitive(def) && def.config.type === "phantom") return true;
4472
+ if (isRef(def)) {
4473
+ const unwound = doc.unwindType(def.id, def.chain);
4474
+ return !!(unwound?.def && isPrimitive(unwound.def) && unwound.def.config.type === "phantom");
4475
+ }
4476
+ return false;
4477
+ }
4478
+ function resolveNode(doc, def) {
4479
+ if (isRef(def)) {
4480
+ const ref$1 = def;
4481
+ const unwound = doc.unwindType(ref$1.id, ref$1.chain);
4482
+ if (unwound?.def) return resolveNode(unwound.doc, unwound.def);
4483
+ return undefined;
4484
+ }
4485
+ if (isInterface(def)) {
4486
+ const iface = def;
4487
+ let struct;
4488
+ if (iface.hasExtends) struct = doc.resolveInterfaceExtends(iface);
4489
+ if (!struct) struct = iface.getDefinition();
4490
+ if (struct) return {
4491
+ def: struct,
4492
+ doc
4493
+ };
4494
+ return undefined;
4495
+ }
4496
+ return {
4497
+ def,
4498
+ doc
4499
+ };
4500
+ }
4501
+ function isComplexNode(def) {
4502
+ return isStructure(def) || isArray(def) || isGroup(def) && def.entity !== "tuple";
4503
+ }
4504
+ function addToFlatMap(flatMap, path$2, descriptor) {
4505
+ const existing = flatMap.get(path$2);
4506
+ if (existing) {
4507
+ const group$1 = new SemanticGroup([existing.def, descriptor.def], "|");
4508
+ flatMap.set(path$2, {
4509
+ def: group$1,
4510
+ doc: existing.doc,
4511
+ optional: existing.optional || descriptor.optional,
4512
+ propNode: existing.propNode,
4513
+ intermediate: existing.intermediate && descriptor.intermediate ? true : undefined
4514
+ });
4515
+ } else flatMap.set(path$2, descriptor);
4516
+ }
4517
+ function walkStructure(doc, struct, prefix, parentOptional, flatMap, stack) {
4518
+ for (const [name, prop] of struct.props) {
4519
+ if (prop.token("identifier")?.pattern) continue;
4520
+ const propDef = prop.getDefinition();
4521
+ if (isPhantomNode(doc, propDef)) continue;
4522
+ const path$2 = prefix ? `${prefix}.${name}` : name;
4523
+ const optional = parentOptional || !!prop.token("optional");
4524
+ const dbJson = hasDbJsonAnnotation(prop);
4525
+ if (!propDef) {
4526
+ addToFlatMap(flatMap, path$2, {
4527
+ def: prop,
4528
+ doc,
4529
+ optional,
4530
+ propNode: prop
4531
+ });
4532
+ continue;
4533
+ }
4534
+ const resolved = resolveNode(doc, propDef);
4535
+ if (!resolved) {
4536
+ addToFlatMap(flatMap, path$2, {
4537
+ def: propDef,
4538
+ doc,
4539
+ optional,
4540
+ propNode: prop
4541
+ });
4542
+ continue;
4543
+ }
4544
+ if (dbJson) {
4545
+ addToFlatMap(flatMap, path$2, {
4546
+ def: resolved.def,
4547
+ doc: resolved.doc,
4548
+ optional,
4549
+ propNode: prop,
4550
+ dbJson: true
4551
+ });
4552
+ continue;
4553
+ }
4554
+ walkNode(resolved.doc, resolved.def, path$2, optional, prop, flatMap, stack);
4555
+ }
4556
+ }
4557
+ function walkNode(doc, def, path$2, optional, propNode, flatMap, stack) {
4558
+ if (stack.has(def)) {
4559
+ addToFlatMap(flatMap, path$2, {
4560
+ def,
4561
+ doc,
4562
+ optional,
4563
+ propNode
4564
+ });
4565
+ return;
4566
+ }
4567
+ stack.add(def);
4568
+ try {
4569
+ if (isStructure(def)) {
4570
+ addToFlatMap(flatMap, path$2, {
4571
+ def,
4572
+ doc,
4573
+ optional,
4574
+ propNode,
4575
+ intermediate: true
4576
+ });
4577
+ walkStructure(doc, def, path$2, optional, flatMap, stack);
4578
+ } else if (isArray(def)) {
4579
+ const elementDef = def.getDefinition();
4580
+ const resolvedElement = elementDef ? resolveNode(doc, elementDef) : undefined;
4581
+ const isComplexElement = resolvedElement && (isStructure(resolvedElement.def) || isGroup(resolvedElement.def) && resolvedElement.def.entity !== "tuple" || isArray(resolvedElement.def));
4582
+ if (isComplexElement) {
4583
+ addToFlatMap(flatMap, path$2, {
4584
+ def,
4585
+ doc,
4586
+ optional,
4587
+ propNode,
4588
+ intermediate: true
4589
+ });
4590
+ walkArrayElement(doc, def, path$2, optional, flatMap, stack);
4591
+ } else addToFlatMap(flatMap, path$2, {
4592
+ def,
4593
+ doc,
4594
+ optional,
4595
+ propNode
4596
+ });
4597
+ } else if (isGroup(def) && def.entity !== "structure") {
4598
+ const group$1 = def;
4599
+ if (group$1.op === "&") {
4600
+ const merged = doc.mergeIntersection(def);
4601
+ if (merged !== def && isStructure(merged)) {
4602
+ addToFlatMap(flatMap, path$2, {
4603
+ def: merged,
4604
+ doc,
4605
+ optional,
4606
+ propNode,
4607
+ intermediate: true
4608
+ });
4609
+ walkStructure(doc, merged, path$2, optional, flatMap, stack);
4610
+ } else addToFlatMap(flatMap, path$2, {
4611
+ def,
4612
+ doc,
4613
+ optional,
4614
+ propNode
4615
+ });
4616
+ } else if (group$1.op === "|") {
4617
+ const branches = group$1.unwrap();
4618
+ let hasComplexBranch = false;
4619
+ let hasLeafBranch = false;
4620
+ for (const branch of branches) {
4621
+ const resolved = resolveNode(doc, branch);
4622
+ if (resolved && isComplexNode(resolved.def)) hasComplexBranch = true;
4623
+ else hasLeafBranch = true;
4624
+ }
4625
+ if (hasComplexBranch) {
4626
+ if (hasLeafBranch) addToFlatMap(flatMap, path$2, {
4627
+ def,
4628
+ doc,
4629
+ optional,
4630
+ propNode
4631
+ });
4632
+ else addToFlatMap(flatMap, path$2, {
4633
+ def,
4634
+ doc,
4635
+ optional,
4636
+ propNode,
4637
+ intermediate: true
4638
+ });
4639
+ for (const branch of branches) {
4640
+ const resolved = resolveNode(doc, branch);
4641
+ if (resolved) walkBranch(resolved.doc, resolved.def, path$2, optional, flatMap, stack);
4642
+ }
4643
+ } else addToFlatMap(flatMap, path$2, {
4644
+ def,
4645
+ doc,
4646
+ optional,
4647
+ propNode
4648
+ });
4649
+ } else addToFlatMap(flatMap, path$2, {
4650
+ def,
4651
+ doc,
4652
+ optional,
4653
+ propNode
4654
+ });
4655
+ } else addToFlatMap(flatMap, path$2, {
4656
+ def,
4657
+ doc,
4658
+ optional,
4659
+ propNode
4660
+ });
4661
+ } finally {
4662
+ stack.delete(def);
4663
+ }
4664
+ }
4665
+ function walkArrayElement(doc, arrayNode, prefix, parentOptional, flatMap, stack) {
4666
+ const elementDef = arrayNode.getDefinition();
4667
+ if (!elementDef) return;
4668
+ const resolved = resolveNode(doc, elementDef);
4669
+ if (!resolved) return;
4670
+ walkBranch(resolved.doc, resolved.def, prefix, parentOptional, flatMap, stack);
4671
+ }
4672
+ /**
4673
+ * Walks a branch (union branch or array element) to collect sub-paths.
4674
+ * Does NOT add an entry for the branch itself — the parent already did that.
4675
+ */ function walkBranch(doc, def, prefix, parentOptional, flatMap, stack) {
4676
+ if (isStructure(def)) walkStructure(doc, def, prefix, parentOptional, flatMap, stack);
4677
+ else if (isArray(def)) walkArrayElement(doc, def, prefix, parentOptional, flatMap, stack);
4678
+ else if (isGroup(def) && def.entity !== "structure") {
4679
+ const group$1 = def;
4680
+ if (group$1.op === "&") {
4681
+ const merged = doc.mergeIntersection(def);
4682
+ if (isStructure(merged)) walkStructure(doc, merged, prefix, parentOptional, flatMap, stack);
4683
+ } else if (group$1.op === "|") for (const branch of group$1.unwrap()) {
4684
+ const resolved = resolveNode(doc, branch);
4685
+ if (resolved) walkBranch(resolved.doc, resolved.def, prefix, parentOptional, flatMap, stack);
4686
+ }
4687
+ }
4688
+ }
4689
+
4690
+ //#endregion
4691
+ export { $n, AnnotationSpec, AtscriptDoc, AtscriptRepo, BuildRepo, DEFAULT_FORMAT, PluginManager, SemanticAnnotateNode, SemanticArrayNode, SemanticConstNode, SemanticGroup, SemanticImportNode, SemanticInterfaceNode, SemanticNode, SemanticPrimitiveNode, SemanticPropNode, SemanticRefNode, SemanticStructureNode, SemanticTupleNode, SemanticTypeNode, Token, build, createAtscriptPlugin, defineConfig, flattenInterfaceNode, getRelPath, isAnnotate, isAnnotationSpec, isArray, isConst, isGroup, isImport, isInterface, isPhantomNode, isPrimitive, isProp, isRef, isStructure, isTuple, isType, loadConfig, loadTsConfig, resolveAnnotation, resolveAtscriptFromPath, resolveConfigFile };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/core",
3
- "version": "0.1.30",
3
+ "version": "0.1.32",
4
4
  "description": "Core library for Atscript parsing and file generation.",
5
5
  "keywords": [
6
6
  "annotations",
@@ -28,7 +28,8 @@
28
28
  | `@expect.max` | `maxValue: number`, `message?: string` | no | number | Maximum value |
29
29
  | `@expect.int` | _(none)_ | no | number | Must be integer |
30
30
  | `@expect.pattern` | `pattern: string`, `flags?: string`, `message?: string` | **yes** (append) | string | Regex validation |
31
- | `@expect.array.key` | _(none)_ | no | string, number | Key field in arrays |
31
+ | `@expect.array.key` | `message?: string` | no | string, number | Key field in arrays (non-optional, string/number only). Multiple = composite key |
32
+ | `@expect.array.uniqueItems` | `message?: string` | no | array | No duplicate items (by key fields or deep equality) |
32
33
 
33
34
  ### `@ui.*` — UI / Presentation Hints
34
35