@atscript/core 0.1.31 → 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({
@@ -3519,34 +3603,70 @@ const expectAnnotations = {
3519
3603
  return [];
3520
3604
  }
3521
3605
  }),
3522
- array: { key: new AnnotationSpec({
3523
- 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",
3524
- nodeType: ["prop", "type"],
3525
- multiple: false,
3526
- validate(token, args, doc) {
3527
- const field = token.parentNode;
3528
- const errors = [];
3529
- const isOptional = !!field.token("optional");
3530
- if (isOptional) errors.push({
3531
- message: `@expect.array.key can't be optional`,
3532
- severity: 1,
3533
- range: field.token("identifier").range
3534
- });
3535
- const definition$1 = field.getDefinition();
3536
- if (!definition$1) return errors;
3537
- let wrongType = false;
3538
- if (isRef(definition$1)) {
3539
- const def = doc.unwindType(definition$1.id, definition$1.chain)?.def;
3540
- if (isPrimitive(def) && !["string", "number"].includes(def.config.type)) wrongType = true;
3541
- } else wrongType = true;
3542
- if (wrongType) errors.push({
3543
- message: `@expect.array.key must be of type string or number`,
3544
- severity: 1,
3545
- range: token.range
3546
- });
3547
- return errors;
3548
- }
3549
- }) }
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
+ }
3550
3670
  };
3551
3671
 
3552
3672
  //#endregion
@@ -4625,6 +4745,7 @@ exports.isConst = isConst
4625
4745
  exports.isGroup = isGroup
4626
4746
  exports.isImport = isImport
4627
4747
  exports.isInterface = isInterface
4748
+ exports.isPhantomNode = isPhantomNode
4628
4749
  exports.isPrimitive = isPrimitive
4629
4750
  exports.isProp = isProp
4630
4751
  exports.isRef = isRef
package/dist/index.d.ts CHANGED
@@ -825,6 +825,7 @@ interface TFlatFieldDescriptor {
825
825
  * @returns A map of dot-separated paths to their field descriptors.
826
826
  */
827
827
  declare function flattenInterfaceNode(doc: AtscriptDoc, node: SemanticInterfaceNode): Map<string, TFlatFieldDescriptor>;
828
+ declare function isPhantomNode(doc: AtscriptDoc, def?: SemanticNode): boolean;
828
829
 
829
- 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, isPrimitive, isProp, isRef, isStructure, isTuple, isType, loadConfig, loadTsConfig, resolveAnnotation, resolveAtscriptFromPath, resolveConfigFile };
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 };
830
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({
@@ -3495,34 +3579,70 @@ const expectAnnotations = {
3495
3579
  return [];
3496
3580
  }
3497
3581
  }),
3498
- array: { key: new AnnotationSpec({
3499
- 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",
3500
- nodeType: ["prop", "type"],
3501
- multiple: false,
3502
- validate(token, args, doc) {
3503
- const field = token.parentNode;
3504
- const errors = [];
3505
- const isOptional = !!field.token("optional");
3506
- if (isOptional) errors.push({
3507
- message: `@expect.array.key can't be optional`,
3508
- severity: 1,
3509
- range: field.token("identifier").range
3510
- });
3511
- const definition$1 = field.getDefinition();
3512
- if (!definition$1) return errors;
3513
- let wrongType = false;
3514
- if (isRef(definition$1)) {
3515
- const def = doc.unwindType(definition$1.id, definition$1.chain)?.def;
3516
- if (isPrimitive(def) && !["string", "number"].includes(def.config.type)) wrongType = true;
3517
- } else wrongType = true;
3518
- if (wrongType) errors.push({
3519
- message: `@expect.array.key must be of type string or number`,
3520
- severity: 1,
3521
- range: token.range
3522
- });
3523
- return errors;
3524
- }
3525
- }) }
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
+ }
3526
3646
  };
3527
3647
 
3528
3648
  //#endregion
@@ -4568,4 +4688,4 @@ else if (isGroup(def) && def.entity !== "structure") {
4568
4688
  }
4569
4689
 
4570
4690
  //#endregion
4571
- 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, isPrimitive, isProp, isRef, isStructure, isTuple, isType, loadConfig, loadTsConfig, resolveAnnotation, resolveAtscriptFromPath, resolveConfigFile };
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.31",
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