@atscript/core 0.1.29 → 0.1.31

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
@@ -35,7 +35,7 @@ const fs_promises = __toESM(require("fs/promises"));
35
35
  const glob = __toESM(require("glob"));
36
36
 
37
37
  //#region packages/core/src/parser/nodes/semantic-node.ts
38
- function _define_property$14(obj, key, value) {
38
+ function _define_property$15(obj, key, value) {
39
39
  if (key in obj) Object.defineProperty(obj, key, {
40
40
  value,
41
41
  enumerable: true,
@@ -145,14 +145,14 @@ else this.annotationsCounter.set(name, 1);
145
145
  return this.definition;
146
146
  }
147
147
  constructor(entity) {
148
- _define_property$14(this, "entity", void 0);
149
- _define_property$14(this, "tokens", void 0);
150
- _define_property$14(this, "isGroup", void 0);
151
- _define_property$14(this, "definition", void 0);
152
- _define_property$14(this, "annotations", void 0);
153
- _define_property$14(this, "annotationsCounter", void 0);
154
- _define_property$14(this, "__typeId", void 0);
155
- _define_property$14(this, "_documentation", void 0);
148
+ _define_property$15(this, "entity", void 0);
149
+ _define_property$15(this, "tokens", void 0);
150
+ _define_property$15(this, "isGroup", void 0);
151
+ _define_property$15(this, "definition", void 0);
152
+ _define_property$15(this, "annotations", void 0);
153
+ _define_property$15(this, "annotationsCounter", void 0);
154
+ _define_property$15(this, "__typeId", void 0);
155
+ _define_property$15(this, "_documentation", void 0);
156
156
  this.entity = entity;
157
157
  this.isGroup = false;
158
158
  }
@@ -249,7 +249,7 @@ var SemanticConstNode = class extends SemanticNode {
249
249
 
250
250
  //#endregion
251
251
  //#region packages/core/src/parser/nodes/group-node.ts
252
- function _define_property$13(obj, key, value) {
252
+ function _define_property$14(obj, key, value) {
253
253
  if (key in obj) Object.defineProperty(obj, key, {
254
254
  value,
255
255
  enumerable: true,
@@ -294,7 +294,7 @@ var SemanticGroup = class extends SemanticNode {
294
294
  return s;
295
295
  }
296
296
  constructor(nodes = [], operator) {
297
- super("group"), _define_property$13(this, "nodes", void 0), _define_property$13(this, "operator", void 0), _define_property$13(this, "isGroup", void 0), this.nodes = nodes, this.operator = operator, this.isGroup = true;
297
+ super("group"), _define_property$14(this, "nodes", void 0), _define_property$14(this, "operator", void 0), _define_property$14(this, "isGroup", void 0), this.nodes = nodes, this.operator = operator, this.isGroup = true;
298
298
  }
299
299
  };
300
300
 
@@ -316,7 +316,26 @@ var SemanticImportNode = class extends SemanticNode {
316
316
 
317
317
  //#endregion
318
318
  //#region packages/core/src/parser/nodes/interface-node.ts
319
+ function _define_property$13(obj, key, value) {
320
+ if (key in obj) Object.defineProperty(obj, key, {
321
+ value,
322
+ enumerable: true,
323
+ configurable: true,
324
+ writable: true
325
+ });
326
+ else obj[key] = value;
327
+ return obj;
328
+ }
319
329
  var SemanticInterfaceNode = class extends SemanticNode {
330
+ addExtendsToken(token) {
331
+ this._extendsTokens.push(token);
332
+ }
333
+ get extendsTokens() {
334
+ return this._extendsTokens;
335
+ }
336
+ get hasExtends() {
337
+ return this._extendsTokens.length > 0;
338
+ }
320
339
  registerAtDocument(doc) {
321
340
  super.registerAtDocument(doc);
322
341
  this.__typeId = doc.nextTypeId();
@@ -326,13 +345,26 @@ var SemanticInterfaceNode = class extends SemanticNode {
326
345
  token.exported = true;
327
346
  doc.registerExport(this);
328
347
  }
348
+ for (const t of this._extendsTokens) {
349
+ t.isReference = true;
350
+ doc.referred.push(t);
351
+ doc.tokensIndex.add(t);
352
+ }
329
353
  }
330
354
  get props() {
331
355
  if (this.definition) return isStructure(this.definition) ? this.definition.props : new Map();
332
356
  return new Map();
333
357
  }
358
+ toString(level = 0, prefix = "●") {
359
+ let s = super.toString(level, prefix);
360
+ if (this._extendsTokens.length > 0) {
361
+ const extendsStr = this._extendsTokens.map((t) => t.text).join(", ");
362
+ s = s.replace("[interface]", `[interface extends ${extendsStr}]`);
363
+ }
364
+ return s;
365
+ }
334
366
  constructor() {
335
- super("interface");
367
+ super("interface"), _define_property$13(this, "_extendsTokens", []);
336
368
  }
337
369
  };
338
370
 
@@ -1980,6 +2012,38 @@ function parseRegExpLiteral(literal) {
1980
2012
  const [, pattern, flags] = match;
1981
2013
  return new RegExp(pattern.replace(/\\\//g, "/"), flags);
1982
2014
  }
2015
+ function extendsClause() {
2016
+ const s = {
2017
+ extends: {
2018
+ node: "identifier",
2019
+ text: "extends"
2020
+ },
2021
+ comma: {
2022
+ node: "punctuation",
2023
+ text: ","
2024
+ },
2025
+ id: { node: "identifier" }
2026
+ };
2027
+ return { handler(ni, target) {
2028
+ if (!ni.$ || !ni.satisfies(s.extends)) return true;
2029
+ ni.accepted();
2030
+ ni.move();
2031
+ ni.skip(["\n"]);
2032
+ const node = target.node;
2033
+ while (ni.$ && ni.satisfies(s.id)) {
2034
+ node.addExtendsToken(new Token(ni.$));
2035
+ ni.accepted();
2036
+ ni.move();
2037
+ ni.skip(["\n"]);
2038
+ if (ni.$ && ni.satisfies(s.comma)) {
2039
+ ni.accepted();
2040
+ ni.move();
2041
+ ni.skip(["\n"]);
2042
+ } else break;
2043
+ }
2044
+ return true;
2045
+ } };
2046
+ }
1983
2047
 
1984
2048
  //#endregion
1985
2049
  //#region packages/core/src/parser/pipes/pipes.ts
@@ -2035,6 +2099,7 @@ const interfaceType = $pipe("interface", [
2035
2099
  identifier("export").saveAs("export").optional().skip("\n"),
2036
2100
  identifier("interface").saveAs("type").skip("\n").suppressEobError(),
2037
2101
  identifier().saveAs("identifier").global().skip("\n"),
2102
+ extendsClause(),
2038
2103
  definition([structureBlock()])
2039
2104
  ]).skip("\n", ";");
2040
2105
  function defineValuePipe(entity, name, supportArray = false) {
@@ -2467,6 +2532,38 @@ else if (isStructure(def) || isInterface(def) || isPrimitive(def)) def = def.pro
2467
2532
  for (const node of this.nodes) if (isAnnotate(node) && node.targetName === targetName) result.push(node);
2468
2533
  return result;
2469
2534
  }
2535
+ /**
2536
+ * Resolves an interface with `extends` by merging parent structures with own props.
2537
+ * Parents are merged left-to-right, then own props overlay on top (own takes priority).
2538
+ * Only merges props and their annotations; interface-level annotations are NOT inherited.
2539
+ */ resolveInterfaceExtends(node, _visited) {
2540
+ if (!node.hasExtends) return undefined;
2541
+ const visited = _visited || new Set();
2542
+ if (node.id && visited.has(node.id)) return undefined;
2543
+ if (node.id) visited.add(node.id);
2544
+ let mergedStruct;
2545
+ for (const extendsToken of node.extendsTokens) {
2546
+ const unwound = this.unwindType(extendsToken.text);
2547
+ if (!unwound?.def) continue;
2548
+ let parentDef = unwound.def;
2549
+ if (isInterface(parentDef) && parentDef.hasExtends) {
2550
+ const resolved = unwound.doc.resolveInterfaceExtends(parentDef, visited);
2551
+ parentDef = resolved || parentDef.getDefinition() || parentDef;
2552
+ } else if (isInterface(parentDef)) parentDef = parentDef.getDefinition() || parentDef;
2553
+ parentDef = this.mergeIntersection(parentDef);
2554
+ if (!mergedStruct) mergedStruct = parentDef;
2555
+ else {
2556
+ const merged = this.mergeDefs(mergedStruct, parentDef);
2557
+ mergedStruct = merged[0];
2558
+ }
2559
+ }
2560
+ const ownDef = node.getDefinition();
2561
+ if (ownDef && mergedStruct) {
2562
+ const merged = this.mergeDefs(mergedStruct, ownDef);
2563
+ return merged[0];
2564
+ }
2565
+ return mergedStruct || ownDef;
2566
+ }
2470
2567
  usageListFor(token) {
2471
2568
  if (token.isDefinition) {
2472
2569
  const refs = this.referred.filter((t) => t.text === token.text).map((r) => ({
@@ -2734,6 +2831,46 @@ else if (isStructure(def) || isInterface(def) || isPrimitive(def)) def = def.pro
2734
2831
  ...this.semanticMessages,
2735
2832
  ...this.messages
2736
2833
  ];
2834
+ for (const node of this.nodes) if (isInterface(node) && node.hasExtends) {
2835
+ const iface = node;
2836
+ for (const t of iface.extendsTokens) if (t.text === iface.id) this._allMessages.push({
2837
+ severity: TSeverity.Error,
2838
+ message: `Interface "${iface.id}" cannot extend itself`,
2839
+ range: t.range
2840
+ });
2841
+ const ownDef = iface.getDefinition();
2842
+ if (ownDef && isStructure(ownDef)) {
2843
+ const parentProps = new Map();
2844
+ for (const t of iface.extendsTokens) {
2845
+ if (t.text === iface.id) continue;
2846
+ const unwound = this.unwindType(t.text);
2847
+ if (!unwound?.def) continue;
2848
+ let parentDef = unwound.def;
2849
+ if (isInterface(parentDef)) {
2850
+ if (parentDef.hasExtends) {
2851
+ const resolved = unwound.doc.resolveInterfaceExtends(parentDef);
2852
+ if (resolved && isStructure(resolved)) {
2853
+ for (const [name] of resolved.props) parentProps.set(name, t.text);
2854
+ continue;
2855
+ }
2856
+ }
2857
+ parentDef = parentDef.getDefinition() || parentDef;
2858
+ }
2859
+ if (isStructure(parentDef)) for (const [name] of parentDef.props) parentProps.set(name, t.text);
2860
+ }
2861
+ for (const [name, prop] of ownDef.props) {
2862
+ const parentName = parentProps.get(name);
2863
+ if (parentName) {
2864
+ const propToken = prop.token("identifier");
2865
+ if (propToken) this._allMessages.push({
2866
+ severity: TSeverity.Error,
2867
+ message: `Property "${name}" already exists in parent "${parentName}" — override in extends is not allowed`,
2868
+ range: propToken.range
2869
+ });
2870
+ }
2871
+ }
2872
+ }
2873
+ }
2737
2874
  for (const t of this.referred) {
2738
2875
  const block$2 = this.blocksIndex.at(t.range.start.line, t.range.start.character);
2739
2876
  if (block$2?.blockType === "annotate" && isAnnotate(block$2.parentNode)) {
@@ -3171,6 +3308,24 @@ else if (fnName === "now" && !["number", "string"].includes(baseType)) errors.pu
3171
3308
  }
3172
3309
  })
3173
3310
  },
3311
+ json: new AnnotationSpec({
3312
+ 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",
3313
+ nodeType: ["prop"],
3314
+ validate(token, _args, doc) {
3315
+ const errors = [];
3316
+ const field = token.parentNode;
3317
+ const definition$1 = field.getDefinition();
3318
+ if (definition$1 && isRef(definition$1)) {
3319
+ const unwound = doc.unwindType(definition$1.id, definition$1.chain);
3320
+ if (unwound && isPrimitive(unwound.def)) errors.push({
3321
+ message: "@db.json on a primitive field has no effect — primitive fields are already stored as scalar columns",
3322
+ severity: 2,
3323
+ range: token.range
3324
+ });
3325
+ }
3326
+ return errors;
3327
+ }
3328
+ }),
3174
3329
  ignore: new AnnotationSpec({
3175
3330
  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",
3176
3331
  nodeType: ["prop"],
@@ -4191,6 +4346,251 @@ var BuildRepo = class {
4191
4346
  }
4192
4347
  };
4193
4348
 
4349
+ //#endregion
4350
+ //#region packages/core/src/flatten.ts
4351
+ function flattenInterfaceNode(doc, node) {
4352
+ const flatMap = new Map();
4353
+ const stack = new Set();
4354
+ let struct;
4355
+ if (node.hasExtends) struct = doc.resolveInterfaceExtends(node);
4356
+ if (!struct) struct = node.getDefinition();
4357
+ if (!struct || !isStructure(struct)) return flatMap;
4358
+ walkStructure(doc, struct, "", false, flatMap, stack);
4359
+ return flatMap;
4360
+ }
4361
+ /**
4362
+ * Checks whether a property has the `@db.json` annotation.
4363
+ *
4364
+ * NOTE: This is intentionally hardcoded to `db.json` (rather than being
4365
+ * configurable) because the flattening behavior is tightly coupled to DB
4366
+ * semantics — `@db.json` fields are stored as a single JSON column, so their
4367
+ * sub-paths must not appear in the flat map (they are not individually
4368
+ * queryable in filters or selectable in projections).
4369
+ */ function hasDbJsonAnnotation(prop) {
4370
+ if (!prop.annotations) return false;
4371
+ return prop.annotations.some((a) => a.name === "db.json");
4372
+ }
4373
+ function isPhantomNode(doc, def) {
4374
+ if (!def) return false;
4375
+ if (isPrimitive(def) && def.config.type === "phantom") return true;
4376
+ if (isRef(def)) {
4377
+ const unwound = doc.unwindType(def.id, def.chain);
4378
+ return !!(unwound?.def && isPrimitive(unwound.def) && unwound.def.config.type === "phantom");
4379
+ }
4380
+ return false;
4381
+ }
4382
+ function resolveNode(doc, def) {
4383
+ if (isRef(def)) {
4384
+ const ref$1 = def;
4385
+ const unwound = doc.unwindType(ref$1.id, ref$1.chain);
4386
+ if (unwound?.def) return resolveNode(unwound.doc, unwound.def);
4387
+ return undefined;
4388
+ }
4389
+ if (isInterface(def)) {
4390
+ const iface = def;
4391
+ let struct;
4392
+ if (iface.hasExtends) struct = doc.resolveInterfaceExtends(iface);
4393
+ if (!struct) struct = iface.getDefinition();
4394
+ if (struct) return {
4395
+ def: struct,
4396
+ doc
4397
+ };
4398
+ return undefined;
4399
+ }
4400
+ return {
4401
+ def,
4402
+ doc
4403
+ };
4404
+ }
4405
+ function isComplexNode(def) {
4406
+ return isStructure(def) || isArray(def) || isGroup(def) && def.entity !== "tuple";
4407
+ }
4408
+ function addToFlatMap(flatMap, path$5, descriptor) {
4409
+ const existing = flatMap.get(path$5);
4410
+ if (existing) {
4411
+ const group$1 = new SemanticGroup([existing.def, descriptor.def], "|");
4412
+ flatMap.set(path$5, {
4413
+ def: group$1,
4414
+ doc: existing.doc,
4415
+ optional: existing.optional || descriptor.optional,
4416
+ propNode: existing.propNode,
4417
+ intermediate: existing.intermediate && descriptor.intermediate ? true : undefined
4418
+ });
4419
+ } else flatMap.set(path$5, descriptor);
4420
+ }
4421
+ function walkStructure(doc, struct, prefix, parentOptional, flatMap, stack) {
4422
+ for (const [name, prop] of struct.props) {
4423
+ if (prop.token("identifier")?.pattern) continue;
4424
+ const propDef = prop.getDefinition();
4425
+ if (isPhantomNode(doc, propDef)) continue;
4426
+ const path$5 = prefix ? `${prefix}.${name}` : name;
4427
+ const optional = parentOptional || !!prop.token("optional");
4428
+ const dbJson = hasDbJsonAnnotation(prop);
4429
+ if (!propDef) {
4430
+ addToFlatMap(flatMap, path$5, {
4431
+ def: prop,
4432
+ doc,
4433
+ optional,
4434
+ propNode: prop
4435
+ });
4436
+ continue;
4437
+ }
4438
+ const resolved = resolveNode(doc, propDef);
4439
+ if (!resolved) {
4440
+ addToFlatMap(flatMap, path$5, {
4441
+ def: propDef,
4442
+ doc,
4443
+ optional,
4444
+ propNode: prop
4445
+ });
4446
+ continue;
4447
+ }
4448
+ if (dbJson) {
4449
+ addToFlatMap(flatMap, path$5, {
4450
+ def: resolved.def,
4451
+ doc: resolved.doc,
4452
+ optional,
4453
+ propNode: prop,
4454
+ dbJson: true
4455
+ });
4456
+ continue;
4457
+ }
4458
+ walkNode(resolved.doc, resolved.def, path$5, optional, prop, flatMap, stack);
4459
+ }
4460
+ }
4461
+ function walkNode(doc, def, path$5, optional, propNode, flatMap, stack) {
4462
+ if (stack.has(def)) {
4463
+ addToFlatMap(flatMap, path$5, {
4464
+ def,
4465
+ doc,
4466
+ optional,
4467
+ propNode
4468
+ });
4469
+ return;
4470
+ }
4471
+ stack.add(def);
4472
+ try {
4473
+ if (isStructure(def)) {
4474
+ addToFlatMap(flatMap, path$5, {
4475
+ def,
4476
+ doc,
4477
+ optional,
4478
+ propNode,
4479
+ intermediate: true
4480
+ });
4481
+ walkStructure(doc, def, path$5, optional, flatMap, stack);
4482
+ } else if (isArray(def)) {
4483
+ const elementDef = def.getDefinition();
4484
+ const resolvedElement = elementDef ? resolveNode(doc, elementDef) : undefined;
4485
+ const isComplexElement = resolvedElement && (isStructure(resolvedElement.def) || isGroup(resolvedElement.def) && resolvedElement.def.entity !== "tuple" || isArray(resolvedElement.def));
4486
+ if (isComplexElement) {
4487
+ addToFlatMap(flatMap, path$5, {
4488
+ def,
4489
+ doc,
4490
+ optional,
4491
+ propNode,
4492
+ intermediate: true
4493
+ });
4494
+ walkArrayElement(doc, def, path$5, optional, flatMap, stack);
4495
+ } else addToFlatMap(flatMap, path$5, {
4496
+ def,
4497
+ doc,
4498
+ optional,
4499
+ propNode
4500
+ });
4501
+ } else if (isGroup(def) && def.entity !== "structure") {
4502
+ const group$1 = def;
4503
+ if (group$1.op === "&") {
4504
+ const merged = doc.mergeIntersection(def);
4505
+ if (merged !== def && isStructure(merged)) {
4506
+ addToFlatMap(flatMap, path$5, {
4507
+ def: merged,
4508
+ doc,
4509
+ optional,
4510
+ propNode,
4511
+ intermediate: true
4512
+ });
4513
+ walkStructure(doc, merged, path$5, optional, flatMap, stack);
4514
+ } else addToFlatMap(flatMap, path$5, {
4515
+ def,
4516
+ doc,
4517
+ optional,
4518
+ propNode
4519
+ });
4520
+ } else if (group$1.op === "|") {
4521
+ const branches = group$1.unwrap();
4522
+ let hasComplexBranch = false;
4523
+ let hasLeafBranch = false;
4524
+ for (const branch of branches) {
4525
+ const resolved = resolveNode(doc, branch);
4526
+ if (resolved && isComplexNode(resolved.def)) hasComplexBranch = true;
4527
+ else hasLeafBranch = true;
4528
+ }
4529
+ if (hasComplexBranch) {
4530
+ if (hasLeafBranch) addToFlatMap(flatMap, path$5, {
4531
+ def,
4532
+ doc,
4533
+ optional,
4534
+ propNode
4535
+ });
4536
+ else addToFlatMap(flatMap, path$5, {
4537
+ def,
4538
+ doc,
4539
+ optional,
4540
+ propNode,
4541
+ intermediate: true
4542
+ });
4543
+ for (const branch of branches) {
4544
+ const resolved = resolveNode(doc, branch);
4545
+ if (resolved) walkBranch(resolved.doc, resolved.def, path$5, optional, flatMap, stack);
4546
+ }
4547
+ } else addToFlatMap(flatMap, path$5, {
4548
+ def,
4549
+ doc,
4550
+ optional,
4551
+ propNode
4552
+ });
4553
+ } else addToFlatMap(flatMap, path$5, {
4554
+ def,
4555
+ doc,
4556
+ optional,
4557
+ propNode
4558
+ });
4559
+ } else addToFlatMap(flatMap, path$5, {
4560
+ def,
4561
+ doc,
4562
+ optional,
4563
+ propNode
4564
+ });
4565
+ } finally {
4566
+ stack.delete(def);
4567
+ }
4568
+ }
4569
+ function walkArrayElement(doc, arrayNode, prefix, parentOptional, flatMap, stack) {
4570
+ const elementDef = arrayNode.getDefinition();
4571
+ if (!elementDef) return;
4572
+ const resolved = resolveNode(doc, elementDef);
4573
+ if (!resolved) return;
4574
+ walkBranch(resolved.doc, resolved.def, prefix, parentOptional, flatMap, stack);
4575
+ }
4576
+ /**
4577
+ * Walks a branch (union branch or array element) to collect sub-paths.
4578
+ * Does NOT add an entry for the branch itself — the parent already did that.
4579
+ */ function walkBranch(doc, def, prefix, parentOptional, flatMap, stack) {
4580
+ if (isStructure(def)) walkStructure(doc, def, prefix, parentOptional, flatMap, stack);
4581
+ else if (isArray(def)) walkArrayElement(doc, def, prefix, parentOptional, flatMap, stack);
4582
+ else if (isGroup(def) && def.entity !== "structure") {
4583
+ const group$1 = def;
4584
+ if (group$1.op === "&") {
4585
+ const merged = doc.mergeIntersection(def);
4586
+ if (isStructure(merged)) walkStructure(doc, merged, prefix, parentOptional, flatMap, stack);
4587
+ } else if (group$1.op === "|") for (const branch of group$1.unwrap()) {
4588
+ const resolved = resolveNode(doc, branch);
4589
+ if (resolved) walkBranch(resolved.doc, resolved.def, prefix, parentOptional, flatMap, stack);
4590
+ }
4591
+ }
4592
+ }
4593
+
4194
4594
  //#endregion
4195
4595
  exports.$n = $n
4196
4596
  exports.AnnotationSpec = AnnotationSpec
@@ -4216,6 +4616,7 @@ exports.Token = Token
4216
4616
  exports.build = build
4217
4617
  exports.createAtscriptPlugin = createAtscriptPlugin
4218
4618
  exports.defineConfig = defineConfig
4619
+ exports.flattenInterfaceNode = flattenInterfaceNode
4219
4620
  exports.getRelPath = getRelPath
4220
4621
  exports.isAnnotate = isAnnotate
4221
4622
  exports.isAnnotationSpec = isAnnotationSpec
package/dist/index.d.ts CHANGED
@@ -315,6 +315,38 @@ declare class SemanticNode {
315
315
  getDefinition(): SemanticNode | undefined;
316
316
  }
317
317
 
318
+ declare class SemanticRefNode extends SemanticNode {
319
+ constructor();
320
+ get referredIdentifiers(): Token[];
321
+ protected _chain: Token[];
322
+ protected _dots: Token[];
323
+ registerAtDocument(doc: AtscriptDoc): void;
324
+ addChain(token: Token): void;
325
+ addDot(token: Token): void;
326
+ get chain(): Token[];
327
+ get hasChain(): boolean;
328
+ toString(level?: number, prefix?: string): string;
329
+ }
330
+
331
+ declare class SemanticPropNode extends SemanticNode {
332
+ constructor();
333
+ registerAtDocument(doc: AtscriptDoc): void;
334
+ get isPattern(): boolean;
335
+ get nestedProps(): Map<string, SemanticPropNode> | undefined;
336
+ get nestedType(): SemanticRefNode | undefined;
337
+ }
338
+
339
+ declare class SemanticInterfaceNode extends SemanticNode {
340
+ private _extendsTokens;
341
+ constructor();
342
+ addExtendsToken(token: Token): void;
343
+ get extendsTokens(): Token[];
344
+ get hasExtends(): boolean;
345
+ registerAtDocument(doc: AtscriptDoc): void;
346
+ get props(): Map<string, SemanticPropNode>;
347
+ toString(level?: number, prefix?: string): string;
348
+ }
349
+
318
350
  declare class SemanticPrimitiveNode extends SemanticNode {
319
351
  private readonly _id;
320
352
  readonly config: TPrimitiveConfig;
@@ -462,6 +494,12 @@ declare class AtscriptDoc {
462
494
  * For entries without a chain (root-level), the key is the entry identifier.
463
495
  */
464
496
  getAnnotateNodesFor(targetName: string): SemanticAnnotateNode[];
497
+ /**
498
+ * Resolves an interface with `extends` by merging parent structures with own props.
499
+ * Parents are merged left-to-right, then own props overlay on top (own takes priority).
500
+ * Only merges props and their annotations; interface-level annotations are NOT inherited.
501
+ */
502
+ resolveInterfaceExtends(node: SemanticInterfaceNode, _visited?: Set<string>): SemanticNode | undefined;
465
503
  usageListFor(token: Token): Array<{
466
504
  uri: string;
467
505
  range: Token['range'];
@@ -585,19 +623,6 @@ declare class AtscriptDoc {
585
623
  mergeNodesAnnotations(left?: TAnnotationTokens[], right?: TAnnotationTokens[]): TAnnotationTokens[];
586
624
  }
587
625
 
588
- declare class SemanticRefNode extends SemanticNode {
589
- constructor();
590
- get referredIdentifiers(): Token[];
591
- protected _chain: Token[];
592
- protected _dots: Token[];
593
- registerAtDocument(doc: AtscriptDoc): void;
594
- addChain(token: Token): void;
595
- addDot(token: Token): void;
596
- get chain(): Token[];
597
- get hasChain(): boolean;
598
- toString(level?: number, prefix?: string): string;
599
- }
600
-
601
626
  declare class SemanticAnnotateNode extends SemanticNode {
602
627
  constructor();
603
628
  get isMutating(): boolean;
@@ -637,20 +662,6 @@ declare class SemanticImportNode extends SemanticNode {
637
662
  registerAtDocument(doc: AtscriptDoc): void;
638
663
  }
639
664
 
640
- declare class SemanticPropNode extends SemanticNode {
641
- constructor();
642
- registerAtDocument(doc: AtscriptDoc): void;
643
- get isPattern(): boolean;
644
- get nestedProps(): Map<string, SemanticPropNode> | undefined;
645
- get nestedType(): SemanticRefNode | undefined;
646
- }
647
-
648
- declare class SemanticInterfaceNode extends SemanticNode {
649
- constructor();
650
- registerAtDocument(doc: AtscriptDoc): void;
651
- get props(): Map<string, SemanticPropNode>;
652
- }
653
-
654
665
  declare class SemanticStructureNode extends SemanticGroup {
655
666
  constructor();
656
667
  readonly props: Map<string, SemanticPropNode>;
@@ -770,5 +781,50 @@ declare function resolveAnnotation(name: string, annotationsTree?: TAnnotationsT
770
781
  declare function resolveAtscriptFromPath(from: string, id: string): string;
771
782
  declare function getRelPath(fromUri: string, toUri: string): string;
772
783
 
773
- 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 };
774
- 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
+
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 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
@@ -11,7 +11,7 @@ import { mkdir, writeFile } from "fs/promises";
11
11
  import { glob } from "glob";
12
12
 
13
13
  //#region packages/core/src/parser/nodes/semantic-node.ts
14
- function _define_property$14(obj, key, value) {
14
+ function _define_property$15(obj, key, value) {
15
15
  if (key in obj) Object.defineProperty(obj, key, {
16
16
  value,
17
17
  enumerable: true,
@@ -121,14 +121,14 @@ else this.annotationsCounter.set(name, 1);
121
121
  return this.definition;
122
122
  }
123
123
  constructor(entity) {
124
- _define_property$14(this, "entity", void 0);
125
- _define_property$14(this, "tokens", void 0);
126
- _define_property$14(this, "isGroup", void 0);
127
- _define_property$14(this, "definition", void 0);
128
- _define_property$14(this, "annotations", void 0);
129
- _define_property$14(this, "annotationsCounter", void 0);
130
- _define_property$14(this, "__typeId", void 0);
131
- _define_property$14(this, "_documentation", void 0);
124
+ _define_property$15(this, "entity", void 0);
125
+ _define_property$15(this, "tokens", void 0);
126
+ _define_property$15(this, "isGroup", void 0);
127
+ _define_property$15(this, "definition", void 0);
128
+ _define_property$15(this, "annotations", void 0);
129
+ _define_property$15(this, "annotationsCounter", void 0);
130
+ _define_property$15(this, "__typeId", void 0);
131
+ _define_property$15(this, "_documentation", void 0);
132
132
  this.entity = entity;
133
133
  this.isGroup = false;
134
134
  }
@@ -225,7 +225,7 @@ var SemanticConstNode = class extends SemanticNode {
225
225
 
226
226
  //#endregion
227
227
  //#region packages/core/src/parser/nodes/group-node.ts
228
- function _define_property$13(obj, key, value) {
228
+ function _define_property$14(obj, key, value) {
229
229
  if (key in obj) Object.defineProperty(obj, key, {
230
230
  value,
231
231
  enumerable: true,
@@ -270,7 +270,7 @@ var SemanticGroup = class extends SemanticNode {
270
270
  return s;
271
271
  }
272
272
  constructor(nodes = [], operator) {
273
- super("group"), _define_property$13(this, "nodes", void 0), _define_property$13(this, "operator", void 0), _define_property$13(this, "isGroup", void 0), this.nodes = nodes, this.operator = operator, this.isGroup = true;
273
+ super("group"), _define_property$14(this, "nodes", void 0), _define_property$14(this, "operator", void 0), _define_property$14(this, "isGroup", void 0), this.nodes = nodes, this.operator = operator, this.isGroup = true;
274
274
  }
275
275
  };
276
276
 
@@ -292,7 +292,26 @@ var SemanticImportNode = class extends SemanticNode {
292
292
 
293
293
  //#endregion
294
294
  //#region packages/core/src/parser/nodes/interface-node.ts
295
+ function _define_property$13(obj, key, value) {
296
+ if (key in obj) Object.defineProperty(obj, key, {
297
+ value,
298
+ enumerable: true,
299
+ configurable: true,
300
+ writable: true
301
+ });
302
+ else obj[key] = value;
303
+ return obj;
304
+ }
295
305
  var SemanticInterfaceNode = class extends SemanticNode {
306
+ addExtendsToken(token) {
307
+ this._extendsTokens.push(token);
308
+ }
309
+ get extendsTokens() {
310
+ return this._extendsTokens;
311
+ }
312
+ get hasExtends() {
313
+ return this._extendsTokens.length > 0;
314
+ }
296
315
  registerAtDocument(doc) {
297
316
  super.registerAtDocument(doc);
298
317
  this.__typeId = doc.nextTypeId();
@@ -302,13 +321,26 @@ var SemanticInterfaceNode = class extends SemanticNode {
302
321
  token.exported = true;
303
322
  doc.registerExport(this);
304
323
  }
324
+ for (const t of this._extendsTokens) {
325
+ t.isReference = true;
326
+ doc.referred.push(t);
327
+ doc.tokensIndex.add(t);
328
+ }
305
329
  }
306
330
  get props() {
307
331
  if (this.definition) return isStructure(this.definition) ? this.definition.props : new Map();
308
332
  return new Map();
309
333
  }
334
+ toString(level = 0, prefix = "●") {
335
+ let s = super.toString(level, prefix);
336
+ if (this._extendsTokens.length > 0) {
337
+ const extendsStr = this._extendsTokens.map((t) => t.text).join(", ");
338
+ s = s.replace("[interface]", `[interface extends ${extendsStr}]`);
339
+ }
340
+ return s;
341
+ }
310
342
  constructor() {
311
- super("interface");
343
+ super("interface"), _define_property$13(this, "_extendsTokens", []);
312
344
  }
313
345
  };
314
346
 
@@ -1956,6 +1988,38 @@ function parseRegExpLiteral(literal) {
1956
1988
  const [, pattern, flags] = match;
1957
1989
  return new RegExp(pattern.replace(/\\\//g, "/"), flags);
1958
1990
  }
1991
+ function extendsClause() {
1992
+ const s = {
1993
+ extends: {
1994
+ node: "identifier",
1995
+ text: "extends"
1996
+ },
1997
+ comma: {
1998
+ node: "punctuation",
1999
+ text: ","
2000
+ },
2001
+ id: { node: "identifier" }
2002
+ };
2003
+ return { handler(ni, target) {
2004
+ if (!ni.$ || !ni.satisfies(s.extends)) return true;
2005
+ ni.accepted();
2006
+ ni.move();
2007
+ ni.skip(["\n"]);
2008
+ const node = target.node;
2009
+ while (ni.$ && ni.satisfies(s.id)) {
2010
+ node.addExtendsToken(new Token(ni.$));
2011
+ ni.accepted();
2012
+ ni.move();
2013
+ ni.skip(["\n"]);
2014
+ if (ni.$ && ni.satisfies(s.comma)) {
2015
+ ni.accepted();
2016
+ ni.move();
2017
+ ni.skip(["\n"]);
2018
+ } else break;
2019
+ }
2020
+ return true;
2021
+ } };
2022
+ }
1959
2023
 
1960
2024
  //#endregion
1961
2025
  //#region packages/core/src/parser/pipes/pipes.ts
@@ -2011,6 +2075,7 @@ const interfaceType = $pipe("interface", [
2011
2075
  identifier("export").saveAs("export").optional().skip("\n"),
2012
2076
  identifier("interface").saveAs("type").skip("\n").suppressEobError(),
2013
2077
  identifier().saveAs("identifier").global().skip("\n"),
2078
+ extendsClause(),
2014
2079
  definition([structureBlock()])
2015
2080
  ]).skip("\n", ";");
2016
2081
  function defineValuePipe(entity, name, supportArray = false) {
@@ -2443,6 +2508,38 @@ else if (isStructure(def) || isInterface(def) || isPrimitive(def)) def = def.pro
2443
2508
  for (const node of this.nodes) if (isAnnotate(node) && node.targetName === targetName) result.push(node);
2444
2509
  return result;
2445
2510
  }
2511
+ /**
2512
+ * Resolves an interface with `extends` by merging parent structures with own props.
2513
+ * Parents are merged left-to-right, then own props overlay on top (own takes priority).
2514
+ * Only merges props and their annotations; interface-level annotations are NOT inherited.
2515
+ */ resolveInterfaceExtends(node, _visited) {
2516
+ if (!node.hasExtends) return undefined;
2517
+ const visited = _visited || new Set();
2518
+ if (node.id && visited.has(node.id)) return undefined;
2519
+ if (node.id) visited.add(node.id);
2520
+ let mergedStruct;
2521
+ for (const extendsToken of node.extendsTokens) {
2522
+ const unwound = this.unwindType(extendsToken.text);
2523
+ if (!unwound?.def) continue;
2524
+ let parentDef = unwound.def;
2525
+ if (isInterface(parentDef) && parentDef.hasExtends) {
2526
+ const resolved = unwound.doc.resolveInterfaceExtends(parentDef, visited);
2527
+ parentDef = resolved || parentDef.getDefinition() || parentDef;
2528
+ } else if (isInterface(parentDef)) parentDef = parentDef.getDefinition() || parentDef;
2529
+ parentDef = this.mergeIntersection(parentDef);
2530
+ if (!mergedStruct) mergedStruct = parentDef;
2531
+ else {
2532
+ const merged = this.mergeDefs(mergedStruct, parentDef);
2533
+ mergedStruct = merged[0];
2534
+ }
2535
+ }
2536
+ const ownDef = node.getDefinition();
2537
+ if (ownDef && mergedStruct) {
2538
+ const merged = this.mergeDefs(mergedStruct, ownDef);
2539
+ return merged[0];
2540
+ }
2541
+ return mergedStruct || ownDef;
2542
+ }
2446
2543
  usageListFor(token) {
2447
2544
  if (token.isDefinition) {
2448
2545
  const refs = this.referred.filter((t) => t.text === token.text).map((r) => ({
@@ -2710,6 +2807,46 @@ else if (isStructure(def) || isInterface(def) || isPrimitive(def)) def = def.pro
2710
2807
  ...this.semanticMessages,
2711
2808
  ...this.messages
2712
2809
  ];
2810
+ for (const node of this.nodes) if (isInterface(node) && node.hasExtends) {
2811
+ const iface = node;
2812
+ for (const t of iface.extendsTokens) if (t.text === iface.id) this._allMessages.push({
2813
+ severity: TSeverity.Error,
2814
+ message: `Interface "${iface.id}" cannot extend itself`,
2815
+ range: t.range
2816
+ });
2817
+ const ownDef = iface.getDefinition();
2818
+ if (ownDef && isStructure(ownDef)) {
2819
+ const parentProps = new Map();
2820
+ for (const t of iface.extendsTokens) {
2821
+ if (t.text === iface.id) continue;
2822
+ const unwound = this.unwindType(t.text);
2823
+ if (!unwound?.def) continue;
2824
+ let parentDef = unwound.def;
2825
+ if (isInterface(parentDef)) {
2826
+ if (parentDef.hasExtends) {
2827
+ const resolved = unwound.doc.resolveInterfaceExtends(parentDef);
2828
+ if (resolved && isStructure(resolved)) {
2829
+ for (const [name] of resolved.props) parentProps.set(name, t.text);
2830
+ continue;
2831
+ }
2832
+ }
2833
+ parentDef = parentDef.getDefinition() || parentDef;
2834
+ }
2835
+ if (isStructure(parentDef)) for (const [name] of parentDef.props) parentProps.set(name, t.text);
2836
+ }
2837
+ for (const [name, prop] of ownDef.props) {
2838
+ const parentName = parentProps.get(name);
2839
+ if (parentName) {
2840
+ const propToken = prop.token("identifier");
2841
+ if (propToken) this._allMessages.push({
2842
+ severity: TSeverity.Error,
2843
+ message: `Property "${name}" already exists in parent "${parentName}" — override in extends is not allowed`,
2844
+ range: propToken.range
2845
+ });
2846
+ }
2847
+ }
2848
+ }
2849
+ }
2713
2850
  for (const t of this.referred) {
2714
2851
  const block$2 = this.blocksIndex.at(t.range.start.line, t.range.start.character);
2715
2852
  if (block$2?.blockType === "annotate" && isAnnotate(block$2.parentNode)) {
@@ -3147,6 +3284,24 @@ else if (fnName === "now" && !["number", "string"].includes(baseType)) errors.pu
3147
3284
  }
3148
3285
  })
3149
3286
  },
3287
+ json: new AnnotationSpec({
3288
+ 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",
3289
+ nodeType: ["prop"],
3290
+ validate(token, _args, doc) {
3291
+ const errors = [];
3292
+ const field = token.parentNode;
3293
+ const definition$1 = field.getDefinition();
3294
+ if (definition$1 && isRef(definition$1)) {
3295
+ const unwound = doc.unwindType(definition$1.id, definition$1.chain);
3296
+ if (unwound && isPrimitive(unwound.def)) errors.push({
3297
+ message: "@db.json on a primitive field has no effect — primitive fields are already stored as scalar columns",
3298
+ severity: 2,
3299
+ range: token.range
3300
+ });
3301
+ }
3302
+ return errors;
3303
+ }
3304
+ }),
3150
3305
  ignore: new AnnotationSpec({
3151
3306
  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",
3152
3307
  nodeType: ["prop"],
@@ -4168,4 +4323,249 @@ var BuildRepo = class {
4168
4323
  };
4169
4324
 
4170
4325
  //#endregion
4171
- 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 };
4326
+ //#region packages/core/src/flatten.ts
4327
+ function flattenInterfaceNode(doc, node) {
4328
+ const flatMap = new Map();
4329
+ const stack = new Set();
4330
+ let struct;
4331
+ if (node.hasExtends) struct = doc.resolveInterfaceExtends(node);
4332
+ if (!struct) struct = node.getDefinition();
4333
+ if (!struct || !isStructure(struct)) return flatMap;
4334
+ walkStructure(doc, struct, "", false, flatMap, stack);
4335
+ return flatMap;
4336
+ }
4337
+ /**
4338
+ * Checks whether a property has the `@db.json` annotation.
4339
+ *
4340
+ * NOTE: This is intentionally hardcoded to `db.json` (rather than being
4341
+ * configurable) because the flattening behavior is tightly coupled to DB
4342
+ * semantics — `@db.json` fields are stored as a single JSON column, so their
4343
+ * sub-paths must not appear in the flat map (they are not individually
4344
+ * queryable in filters or selectable in projections).
4345
+ */ function hasDbJsonAnnotation(prop) {
4346
+ if (!prop.annotations) return false;
4347
+ return prop.annotations.some((a) => a.name === "db.json");
4348
+ }
4349
+ function isPhantomNode(doc, def) {
4350
+ if (!def) return false;
4351
+ if (isPrimitive(def) && def.config.type === "phantom") return true;
4352
+ if (isRef(def)) {
4353
+ const unwound = doc.unwindType(def.id, def.chain);
4354
+ return !!(unwound?.def && isPrimitive(unwound.def) && unwound.def.config.type === "phantom");
4355
+ }
4356
+ return false;
4357
+ }
4358
+ function resolveNode(doc, def) {
4359
+ if (isRef(def)) {
4360
+ const ref$1 = def;
4361
+ const unwound = doc.unwindType(ref$1.id, ref$1.chain);
4362
+ if (unwound?.def) return resolveNode(unwound.doc, unwound.def);
4363
+ return undefined;
4364
+ }
4365
+ if (isInterface(def)) {
4366
+ const iface = def;
4367
+ let struct;
4368
+ if (iface.hasExtends) struct = doc.resolveInterfaceExtends(iface);
4369
+ if (!struct) struct = iface.getDefinition();
4370
+ if (struct) return {
4371
+ def: struct,
4372
+ doc
4373
+ };
4374
+ return undefined;
4375
+ }
4376
+ return {
4377
+ def,
4378
+ doc
4379
+ };
4380
+ }
4381
+ function isComplexNode(def) {
4382
+ return isStructure(def) || isArray(def) || isGroup(def) && def.entity !== "tuple";
4383
+ }
4384
+ function addToFlatMap(flatMap, path$2, descriptor) {
4385
+ const existing = flatMap.get(path$2);
4386
+ if (existing) {
4387
+ const group$1 = new SemanticGroup([existing.def, descriptor.def], "|");
4388
+ flatMap.set(path$2, {
4389
+ def: group$1,
4390
+ doc: existing.doc,
4391
+ optional: existing.optional || descriptor.optional,
4392
+ propNode: existing.propNode,
4393
+ intermediate: existing.intermediate && descriptor.intermediate ? true : undefined
4394
+ });
4395
+ } else flatMap.set(path$2, descriptor);
4396
+ }
4397
+ function walkStructure(doc, struct, prefix, parentOptional, flatMap, stack) {
4398
+ for (const [name, prop] of struct.props) {
4399
+ if (prop.token("identifier")?.pattern) continue;
4400
+ const propDef = prop.getDefinition();
4401
+ if (isPhantomNode(doc, propDef)) continue;
4402
+ const path$2 = prefix ? `${prefix}.${name}` : name;
4403
+ const optional = parentOptional || !!prop.token("optional");
4404
+ const dbJson = hasDbJsonAnnotation(prop);
4405
+ if (!propDef) {
4406
+ addToFlatMap(flatMap, path$2, {
4407
+ def: prop,
4408
+ doc,
4409
+ optional,
4410
+ propNode: prop
4411
+ });
4412
+ continue;
4413
+ }
4414
+ const resolved = resolveNode(doc, propDef);
4415
+ if (!resolved) {
4416
+ addToFlatMap(flatMap, path$2, {
4417
+ def: propDef,
4418
+ doc,
4419
+ optional,
4420
+ propNode: prop
4421
+ });
4422
+ continue;
4423
+ }
4424
+ if (dbJson) {
4425
+ addToFlatMap(flatMap, path$2, {
4426
+ def: resolved.def,
4427
+ doc: resolved.doc,
4428
+ optional,
4429
+ propNode: prop,
4430
+ dbJson: true
4431
+ });
4432
+ continue;
4433
+ }
4434
+ walkNode(resolved.doc, resolved.def, path$2, optional, prop, flatMap, stack);
4435
+ }
4436
+ }
4437
+ function walkNode(doc, def, path$2, optional, propNode, flatMap, stack) {
4438
+ if (stack.has(def)) {
4439
+ addToFlatMap(flatMap, path$2, {
4440
+ def,
4441
+ doc,
4442
+ optional,
4443
+ propNode
4444
+ });
4445
+ return;
4446
+ }
4447
+ stack.add(def);
4448
+ try {
4449
+ if (isStructure(def)) {
4450
+ addToFlatMap(flatMap, path$2, {
4451
+ def,
4452
+ doc,
4453
+ optional,
4454
+ propNode,
4455
+ intermediate: true
4456
+ });
4457
+ walkStructure(doc, def, path$2, optional, flatMap, stack);
4458
+ } else if (isArray(def)) {
4459
+ const elementDef = def.getDefinition();
4460
+ const resolvedElement = elementDef ? resolveNode(doc, elementDef) : undefined;
4461
+ const isComplexElement = resolvedElement && (isStructure(resolvedElement.def) || isGroup(resolvedElement.def) && resolvedElement.def.entity !== "tuple" || isArray(resolvedElement.def));
4462
+ if (isComplexElement) {
4463
+ addToFlatMap(flatMap, path$2, {
4464
+ def,
4465
+ doc,
4466
+ optional,
4467
+ propNode,
4468
+ intermediate: true
4469
+ });
4470
+ walkArrayElement(doc, def, path$2, optional, flatMap, stack);
4471
+ } else addToFlatMap(flatMap, path$2, {
4472
+ def,
4473
+ doc,
4474
+ optional,
4475
+ propNode
4476
+ });
4477
+ } else if (isGroup(def) && def.entity !== "structure") {
4478
+ const group$1 = def;
4479
+ if (group$1.op === "&") {
4480
+ const merged = doc.mergeIntersection(def);
4481
+ if (merged !== def && isStructure(merged)) {
4482
+ addToFlatMap(flatMap, path$2, {
4483
+ def: merged,
4484
+ doc,
4485
+ optional,
4486
+ propNode,
4487
+ intermediate: true
4488
+ });
4489
+ walkStructure(doc, merged, path$2, optional, flatMap, stack);
4490
+ } else addToFlatMap(flatMap, path$2, {
4491
+ def,
4492
+ doc,
4493
+ optional,
4494
+ propNode
4495
+ });
4496
+ } else if (group$1.op === "|") {
4497
+ const branches = group$1.unwrap();
4498
+ let hasComplexBranch = false;
4499
+ let hasLeafBranch = false;
4500
+ for (const branch of branches) {
4501
+ const resolved = resolveNode(doc, branch);
4502
+ if (resolved && isComplexNode(resolved.def)) hasComplexBranch = true;
4503
+ else hasLeafBranch = true;
4504
+ }
4505
+ if (hasComplexBranch) {
4506
+ if (hasLeafBranch) addToFlatMap(flatMap, path$2, {
4507
+ def,
4508
+ doc,
4509
+ optional,
4510
+ propNode
4511
+ });
4512
+ else addToFlatMap(flatMap, path$2, {
4513
+ def,
4514
+ doc,
4515
+ optional,
4516
+ propNode,
4517
+ intermediate: true
4518
+ });
4519
+ for (const branch of branches) {
4520
+ const resolved = resolveNode(doc, branch);
4521
+ if (resolved) walkBranch(resolved.doc, resolved.def, path$2, optional, flatMap, stack);
4522
+ }
4523
+ } else addToFlatMap(flatMap, path$2, {
4524
+ def,
4525
+ doc,
4526
+ optional,
4527
+ propNode
4528
+ });
4529
+ } else addToFlatMap(flatMap, path$2, {
4530
+ def,
4531
+ doc,
4532
+ optional,
4533
+ propNode
4534
+ });
4535
+ } else addToFlatMap(flatMap, path$2, {
4536
+ def,
4537
+ doc,
4538
+ optional,
4539
+ propNode
4540
+ });
4541
+ } finally {
4542
+ stack.delete(def);
4543
+ }
4544
+ }
4545
+ function walkArrayElement(doc, arrayNode, prefix, parentOptional, flatMap, stack) {
4546
+ const elementDef = arrayNode.getDefinition();
4547
+ if (!elementDef) return;
4548
+ const resolved = resolveNode(doc, elementDef);
4549
+ if (!resolved) return;
4550
+ walkBranch(resolved.doc, resolved.def, prefix, parentOptional, flatMap, stack);
4551
+ }
4552
+ /**
4553
+ * Walks a branch (union branch or array element) to collect sub-paths.
4554
+ * Does NOT add an entry for the branch itself — the parent already did that.
4555
+ */ function walkBranch(doc, def, prefix, parentOptional, flatMap, stack) {
4556
+ if (isStructure(def)) walkStructure(doc, def, prefix, parentOptional, flatMap, stack);
4557
+ else if (isArray(def)) walkArrayElement(doc, def, prefix, parentOptional, flatMap, stack);
4558
+ else if (isGroup(def) && def.entity !== "structure") {
4559
+ const group$1 = def;
4560
+ if (group$1.op === "&") {
4561
+ const merged = doc.mergeIntersection(def);
4562
+ if (isStructure(merged)) walkStructure(doc, merged, prefix, parentOptional, flatMap, stack);
4563
+ } else if (group$1.op === "|") for (const branch of group$1.unwrap()) {
4564
+ const resolved = resolveNode(doc, branch);
4565
+ if (resolved) walkBranch(resolved.doc, resolved.def, prefix, parentOptional, flatMap, stack);
4566
+ }
4567
+ }
4568
+ }
4569
+
4570
+ //#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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/core",
3
- "version": "0.1.29",
3
+ "version": "0.1.31",
4
4
  "description": "Core library for Atscript parsing and file generation.",
5
5
  "keywords": [
6
6
  "annotations",