@atscript/core 0.1.89 → 0.1.90

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
@@ -1491,11 +1491,7 @@ function getRelFilterScope(owner) {
1491
1491
  const unwound = doc.unwindType(typeName);
1492
1492
  if (!unwound?.def) return [];
1493
1493
  let def = doc.mergeIntersection(unwound.def);
1494
- if (isInterface(def)) {
1495
- const resolved = doc.resolveInterfaceExtends(def);
1496
- if (resolved) def = resolved;
1497
- else def = def.getDefinition() || def;
1498
- }
1494
+ if (isInterface(def)) def = doc.resolveInterfaceExtends(def) || def.getDefinition() || def;
1499
1495
  if (isStructure(def) || isInterface(def)) return Array.from(def.props.values());
1500
1496
  return [];
1501
1497
  }
@@ -1961,6 +1957,38 @@ const block$1 = new _prostojs_parser.Node({
1961
1957
  }
1962
1958
  });
1963
1959
 
1960
+ //#endregion
1961
+ //#region packages/core/src/tokenizer/tokens/query-block.token.ts
1962
+ /**
1963
+ * Query block node — parenthesized sub-expressions inside backtick queries.
1964
+ *
1965
+ * Produces exactly the same token shape as `BlockToken` (`type: 'block'`,
1966
+ * `text: '('`) so every consumer keeps working, but it recognizes
1967
+ * `QueryOperatorToken` inside, which makes `!=`, `>=`, `<=`, `>`, `<`
1968
+ * work within parentheses (the generic block only knows `PunctuationToken`,
1969
+ * whose charset has no `<` / `>`).
1970
+ *
1971
+ * Only registered inside `QueryToken` (and itself, for nested parens) —
1972
+ * the generic `BlockToken` is left untouched so tokenization of `(`/`[`/`{`
1973
+ * outside queries does not change.
1974
+ */ const QueryBlockToken = new _prostojs_parser.Node({
1975
+ name: "block",
1976
+ start: {
1977
+ token: RegExp("(?<text>\\()", "u"),
1978
+ omit: true
1979
+ },
1980
+ end: {
1981
+ token: ")",
1982
+ omit: true
1983
+ },
1984
+ skip: /\s/u,
1985
+ eofClose: true,
1986
+ data: {
1987
+ type: "block",
1988
+ text: ""
1989
+ }
1990
+ });
1991
+
1964
1992
  //#endregion
1965
1993
  //#region packages/core/src/tokenizer/tokens/query-operator.token.ts
1966
1994
  /**
@@ -2076,6 +2104,7 @@ const tokens = {
2076
2104
  text: TextToken,
2077
2105
  regexp: RegExpToken,
2078
2106
  query: QueryToken,
2107
+ queryBlock: QueryBlockToken,
2079
2108
  root: void 0
2080
2109
  };
2081
2110
  const root = new _prostojs_parser.Node({
@@ -2095,7 +2124,8 @@ const root = new _prostojs_parser.Node({
2095
2124
  });
2096
2125
  tokens.root = root;
2097
2126
  BlockToken.recognize(RegExpToken, ...commentNodes.all, BlockToken, QueryToken, AIdentifierToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
2098
- QueryToken.recognize(RegExpToken, QueryOperatorToken, BlockToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
2127
+ QueryToken.recognize(RegExpToken, QueryOperatorToken, QueryBlockToken, BlockToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
2128
+ QueryBlockToken.recognize(RegExpToken, QueryOperatorToken, QueryBlockToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
2099
2129
  /**
2100
2130
  * Recursively extract TLexicalToken[] from a ParsedNode tree.
2101
2131
  * Replaces the v0.5 mapContent('children', callback) + global loop.
@@ -2104,11 +2134,28 @@ QueryToken.recognize(RegExpToken, QueryOperatorToken, BlockToken, IdentifierToke
2104
2134
  * line is 1-based, column is 1-based.
2105
2135
  * VSCode expects 0-based line, 0-based character.
2106
2136
  */ function extractTokens(node) {
2107
- return node.content.map((item) => {
2108
- if (typeof item === "string") return {
2109
- type: "unknown",
2110
- text: item
2111
- };
2137
+ const content = node.content;
2138
+ return content.map((item, index) => {
2139
+ if (typeof item === "string") {
2140
+ const prev = content[index - 1];
2141
+ const next = content[index + 1];
2142
+ const start = typeof prev === "object" ? prev.end : node.start;
2143
+ const end = typeof next === "object" ? next.start : node.end;
2144
+ return {
2145
+ type: "unknown",
2146
+ text: item,
2147
+ getRange: () => ({
2148
+ start: {
2149
+ line: start.line - 1,
2150
+ character: start.column - 1
2151
+ },
2152
+ end: {
2153
+ line: end.line - 1,
2154
+ character: end.column - 1
2155
+ }
2156
+ })
2157
+ };
2158
+ }
2112
2159
  const data = item.data;
2113
2160
  data.getRange = () => ({
2114
2161
  start: {
@@ -2406,38 +2453,29 @@ const VALUE_KEYWORDS = new Set([
2406
2453
  * Returns a SemanticQueryNode or undefined if the content is empty.
2407
2454
  */ function parseQueryExpression(children, messages, sourceToken) {
2408
2455
  if (children.length === 0) return;
2456
+ const ctx = {
2457
+ messages,
2458
+ fallback: sourceToken.range
2459
+ };
2409
2460
  const ni = new NodeIterator(children, messages);
2410
2461
  ni.move();
2411
- const expr = parseOrExpr(ni, messages);
2462
+ const expr = parseOrExpr(ni, ctx);
2412
2463
  if (!expr) return;
2413
- if (ni.$) messages.push({
2414
- severity: 1,
2415
- message: `Unexpected token in query expression: "${ni.$.text}"`,
2416
- range: ni.$.getRange?.() ?? {
2417
- start: {
2418
- line: 0,
2419
- character: 0
2420
- },
2421
- end: {
2422
- line: 0,
2423
- character: 0
2424
- }
2425
- }
2426
- });
2464
+ if (ni.$) pushError(ni, ctx, `Unexpected token in query expression: "${ni.$.text}"`);
2427
2465
  const node = new SemanticQueryNode();
2428
2466
  node.expression = expr;
2429
2467
  node.sourceToken = sourceToken;
2430
2468
  return node;
2431
2469
  }
2432
- function parseOrExpr(ni, messages) {
2433
- const left = parseAndExpr(ni, messages);
2470
+ function parseOrExpr(ni, ctx) {
2471
+ const left = parseAndExpr(ni, ctx);
2434
2472
  if (!left) return;
2435
2473
  const operands = [left];
2436
2474
  while (ni.$?.type === "identifier" && ni.$.text === "or") {
2437
2475
  ni.move();
2438
- const right = parseAndExpr(ni, messages);
2476
+ const right = parseAndExpr(ni, ctx);
2439
2477
  if (!right) {
2440
- pushError(ni, messages, "Expected expression after \"or\"");
2478
+ pushError(ni, ctx, "Expected expression after \"or\"");
2441
2479
  break;
2442
2480
  }
2443
2481
  operands.push(right);
@@ -2448,15 +2486,15 @@ function parseOrExpr(ni, messages) {
2448
2486
  node.operands = operands;
2449
2487
  return node;
2450
2488
  }
2451
- function parseAndExpr(ni, messages) {
2452
- const left = parseUnaryExpr(ni, messages);
2489
+ function parseAndExpr(ni, ctx) {
2490
+ const left = parseUnaryExpr(ni, ctx);
2453
2491
  if (!left) return;
2454
2492
  const operands = [left];
2455
2493
  while (ni.$?.type === "identifier" && ni.$.text === "and") {
2456
2494
  ni.move();
2457
- const right = parseUnaryExpr(ni, messages);
2495
+ const right = parseUnaryExpr(ni, ctx);
2458
2496
  if (!right) {
2459
- pushError(ni, messages, "Expected expression after \"and\"");
2497
+ pushError(ni, ctx, "Expected expression after \"and\"");
2460
2498
  break;
2461
2499
  }
2462
2500
  operands.push(right);
@@ -2467,12 +2505,12 @@ function parseAndExpr(ni, messages) {
2467
2505
  node.operands = operands;
2468
2506
  return node;
2469
2507
  }
2470
- function parseUnaryExpr(ni, messages) {
2508
+ function parseUnaryExpr(ni, ctx) {
2471
2509
  if (ni.$?.type === "identifier" && ni.$.text === "not") {
2472
2510
  ni.move();
2473
- const operand = parseUnaryExpr(ni, messages);
2511
+ const operand = parseUnaryExpr(ni, ctx);
2474
2512
  if (!operand) {
2475
- pushError(ni, messages, "Expected expression after \"not\"");
2513
+ pushError(ni, ctx, "Expected expression after \"not\"");
2476
2514
  return;
2477
2515
  }
2478
2516
  const node = new SemanticQueryLogicalNode();
@@ -2481,29 +2519,34 @@ function parseUnaryExpr(ni, messages) {
2481
2519
  return node;
2482
2520
  }
2483
2521
  if (ni.$?.type === "block" && ni.$.text === "(") {
2484
- const blockChildren = ni.$.children || [];
2522
+ const blockToken = ni.$;
2523
+ const blockChildren = blockToken.children || [];
2485
2524
  ni.move();
2525
+ const subCtx = {
2526
+ messages: ctx.messages,
2527
+ fallback: rangeOf(blockToken, ctx)
2528
+ };
2486
2529
  if (blockChildren.length === 0) {
2487
- pushError(ni, messages, "Empty parenthesized expression");
2530
+ pushErrorAt(subCtx.fallback, ctx, "Empty parenthesized expression");
2488
2531
  return;
2489
2532
  }
2490
- const subNi = new NodeIterator(blockChildren, messages);
2533
+ const subNi = new NodeIterator(blockChildren, ctx.messages);
2491
2534
  subNi.move();
2492
- const expr = parseOrExpr(subNi, messages);
2493
- if (subNi.$) pushError(subNi, messages, `Unexpected token in parenthesized expression: "${subNi.$.text}"`);
2535
+ const expr = parseOrExpr(subNi, subCtx);
2536
+ if (subNi.$) pushError(subNi, subCtx, `Unexpected token in parenthesized expression: "${subNi.$.text}"`);
2494
2537
  return expr;
2495
2538
  }
2496
- return parseComparison(ni, messages);
2539
+ return parseComparison(ni, ctx);
2497
2540
  }
2498
- function parseComparison(ni, messages) {
2499
- const left = parseFieldRef(ni, messages);
2541
+ function parseComparison(ni, ctx) {
2542
+ const left = parseFieldRef(ni, ctx);
2500
2543
  if (!left) return;
2501
2544
  if (ni.$?.type === "punctuation" && SYMBOLIC_OPS.has(ni.$.text)) {
2502
2545
  const opText = ni.$.text;
2503
2546
  ni.move();
2504
- const right = parseValueOrFieldRef(ni, messages);
2547
+ const right = parseValueOrFieldRef(ni, ctx);
2505
2548
  if (!right) {
2506
- pushError(ni, messages, `Expected value or field reference after "${opText}"`);
2549
+ pushError(ni, ctx, `Expected value or field reference after "${opText}"`);
2507
2550
  return;
2508
2551
  }
2509
2552
  const node = new SemanticQueryComparisonNode();
@@ -2516,7 +2559,7 @@ function parseComparison(ni, messages) {
2516
2559
  const opText = ni.$.text;
2517
2560
  if (opText === "in") {
2518
2561
  ni.move();
2519
- const right = parseValueList(ni, messages);
2562
+ const right = parseValueList(ni, ctx);
2520
2563
  if (!right) return;
2521
2564
  const node = new SemanticQueryComparisonNode();
2522
2565
  node.left = left;
@@ -2526,9 +2569,9 @@ function parseComparison(ni, messages) {
2526
2569
  }
2527
2570
  if (opText === "matches") {
2528
2571
  ni.move();
2529
- const right = parseValueOrFieldRef(ni, messages);
2572
+ const right = parseValueOrFieldRef(ni, ctx);
2530
2573
  if (!right) {
2531
- pushError(ni, messages, "Expected value after \"matches\"");
2574
+ pushError(ni, ctx, "Expected value after \"matches\"");
2532
2575
  return;
2533
2576
  }
2534
2577
  const node = new SemanticQueryComparisonNode();
@@ -2544,17 +2587,17 @@ function parseComparison(ni, messages) {
2544
2587
  node.operator = "exists";
2545
2588
  return node;
2546
2589
  }
2547
- if (opText === "not") return parseNotComparison(ni, messages, left);
2548
- pushError(ni, messages, `Unknown operator "${opText}"`);
2590
+ if (opText === "not") return parseNotComparison(ni, ctx, left);
2591
+ pushError(ni, ctx, `Unknown operator "${opText}"`);
2549
2592
  return;
2550
2593
  }
2551
- pushError(ni, messages, "Expected operator after field reference");
2594
+ pushError(ni, ctx, "Expected operator after field reference");
2552
2595
  }
2553
- function parseNotComparison(ni, messages, left) {
2596
+ function parseNotComparison(ni, ctx, left) {
2554
2597
  ni.move();
2555
2598
  if (ni.$?.type === "identifier" && ni.$.text === "in") {
2556
2599
  ni.move();
2557
- const right = parseValueList(ni, messages);
2600
+ const right = parseValueList(ni, ctx);
2558
2601
  if (!right) return;
2559
2602
  const node = new SemanticQueryComparisonNode();
2560
2603
  node.left = left;
@@ -2569,11 +2612,11 @@ function parseNotComparison(ni, messages, left) {
2569
2612
  node.operator = "not exists";
2570
2613
  return node;
2571
2614
  }
2572
- pushError(ni, messages, "Expected \"in\" or \"exists\" after \"not\"");
2615
+ pushError(ni, ctx, "Expected \"in\" or \"exists\" after \"not\"");
2573
2616
  }
2574
- function parseFieldRef(ni, messages) {
2617
+ function parseFieldRef(ni, ctx) {
2575
2618
  if (ni.$?.type !== "identifier") {
2576
- pushError(ni, messages, "Expected field reference");
2619
+ pushError(ni, ctx, "Expected field reference");
2577
2620
  return;
2578
2621
  }
2579
2622
  const firstToken = new Token(ni.$);
@@ -2590,17 +2633,16 @@ function parseFieldRef(ni, messages) {
2590
2633
  fieldText += `.${ni.$.text}`;
2591
2634
  ni.move();
2592
2635
  } else {
2593
- pushError(ni, messages, "Expected identifier after \".\"");
2636
+ pushError(ni, ctx, "Expected identifier after \".\"");
2594
2637
  break;
2595
2638
  }
2596
2639
  }
2597
2640
  const node = new SemanticQueryFieldRefNode();
2598
2641
  node.typeRef = firstToken;
2599
- if (fieldText !== secondToken.text) node.fieldRef = secondToken.clone({ text: fieldText });
2600
- else node.fieldRef = secondToken;
2642
+ node.fieldRef = fieldText === secondToken.text ? secondToken : secondToken.clone({ text: fieldText });
2601
2643
  return node;
2602
2644
  }
2603
- pushError(ni, messages, "Expected identifier after \".\"");
2645
+ pushError(ni, ctx, "Expected identifier after \".\"");
2604
2646
  const node = new SemanticQueryFieldRefNode();
2605
2647
  node.fieldRef = firstToken;
2606
2648
  return node;
@@ -2609,18 +2651,18 @@ function parseFieldRef(ni, messages) {
2609
2651
  node.fieldRef = firstToken;
2610
2652
  return node;
2611
2653
  }
2612
- function parseValueOrFieldRef(ni, messages) {
2654
+ function parseValueOrFieldRef(ni, ctx) {
2613
2655
  if (!ni.$) return;
2614
- if (ni.$.type === "text" || ni.$.type === "number" || ni.$.type === "regexp") return parseValue(ni, messages);
2656
+ if (ni.$.type === "text" || ni.$.type === "number" || ni.$.type === "regexp") return parseValue(ni, ctx);
2615
2657
  if (ni.$.type === "identifier") {
2616
- if (VALUE_KEYWORDS.has(ni.$.text)) return parseValue(ni, messages);
2617
- return parseFieldRef(ni, messages);
2658
+ if (VALUE_KEYWORDS.has(ni.$.text)) return parseValue(ni, ctx);
2659
+ return parseFieldRef(ni, ctx);
2618
2660
  }
2619
- pushError(ni, messages, "Expected value or field reference");
2661
+ pushError(ni, ctx, "Expected value or field reference");
2620
2662
  }
2621
- function parseValue(ni, messages) {
2663
+ function parseValue(ni, ctx) {
2622
2664
  if (!ni.$) {
2623
- pushError(ni, messages, "Expected value");
2665
+ pushError(ni, ctx, "Expected value");
2624
2666
  return;
2625
2667
  }
2626
2668
  const token = ni.$;
@@ -2630,45 +2672,49 @@ function parseValue(ni, messages) {
2630
2672
  ni.move();
2631
2673
  return node;
2632
2674
  }
2633
- pushError(ni, messages, `Unexpected token "${token.text}" where value expected`);
2675
+ pushError(ni, ctx, `Unexpected token "${token.text}" where value expected`);
2634
2676
  }
2635
- function parseValueList(ni, messages) {
2677
+ function parseValueList(ni, ctx) {
2636
2678
  if (ni.$?.type === "block" && ni.$.text === "(") {
2637
- const blockChildren = ni.$.children || [];
2679
+ const blockToken = ni.$;
2680
+ const blockChildren = blockToken.children || [];
2638
2681
  ni.move();
2682
+ const subCtx = {
2683
+ messages: ctx.messages,
2684
+ fallback: rangeOf(blockToken, ctx)
2685
+ };
2639
2686
  const values = [];
2640
2687
  if (blockChildren.length === 0) {
2641
- pushError(ni, messages, "Empty value list in \"in\" expression");
2688
+ pushErrorAt(subCtx.fallback, ctx, "Empty value list in \"in\" expression");
2642
2689
  return;
2643
2690
  }
2644
- const subNi = new NodeIterator(blockChildren, messages);
2691
+ const subNi = new NodeIterator(blockChildren, ctx.messages);
2645
2692
  subNi.move();
2646
- const first = parseValue(subNi, messages);
2693
+ const first = parseValue(subNi, subCtx);
2647
2694
  if (first) values.push(first);
2648
2695
  while (subNi.$?.type === "punctuation" && subNi.$.text === ",") {
2649
2696
  subNi.move();
2650
- const v = parseValue(subNi, messages);
2697
+ const v = parseValue(subNi, subCtx);
2651
2698
  if (v) values.push(v);
2652
2699
  }
2653
- if (subNi.$) pushError(subNi, messages, `Unexpected token in value list: "${subNi.$.text}"`);
2700
+ if (subNi.$) pushError(subNi, subCtx, `Unexpected token in value list: "${subNi.$.text}"`);
2654
2701
  const node = new SemanticQueryValueListNode();
2655
2702
  node.values = values;
2656
2703
  return node;
2657
2704
  }
2658
- pushError(ni, messages, "Expected parenthesized value list after \"in\"");
2705
+ pushError(ni, ctx, "Expected parenthesized value list after \"in\"");
2659
2706
  }
2660
- function pushError(ni, messages, message) {
2661
- const range = ni.$?.getRange?.() ?? {
2662
- start: {
2663
- line: 0,
2664
- character: 0
2665
- },
2666
- end: {
2667
- line: 0,
2668
- character: 0
2669
- }
2670
- };
2671
- messages.push({
2707
+ /**
2708
+ * Range of a token, falling back to the enclosing query/paren range when the
2709
+ * token carries no position of its own.
2710
+ */ function rangeOf(token, ctx) {
2711
+ return token?.getRange?.() ?? ctx.fallback;
2712
+ }
2713
+ function pushError(ni, ctx, message) {
2714
+ pushErrorAt(rangeOf(ni.$, ctx), ctx, message);
2715
+ }
2716
+ function pushErrorAt(range, ctx, message) {
2717
+ ctx.messages.push({
2672
2718
  severity: 1,
2673
2719
  message,
2674
2720
  range
@@ -5428,11 +5474,18 @@ var BuildRepo = class {
5428
5474
  }
5429
5475
  return outFiles;
5430
5476
  }
5431
- async write(config, docs = this.docs) {
5477
+ /**
5478
+ * Renders every document and writes the outputs to disk.
5479
+ *
5480
+ * Each write is awaited and atomic (temp file + rename), because a consumer
5481
+ * watching the output — a dev server, a type checker, another build — must
5482
+ * never observe a half-written file, and an un-awaited write can be
5483
+ * truncated when the process exits.
5484
+ */ async write(config, docs = this.docs) {
5432
5485
  const outFiles = await this.generate(config, docs);
5433
5486
  for (const o of outFiles) if (o.target) {
5434
5487
  await (0, fs_promises.mkdir)(path.default.dirname(o.target), { recursive: true });
5435
- (0, fs_promises.writeFile)(o.target, o.content);
5488
+ await writeFileAtomic(o.target, o.content);
5436
5489
  }
5437
5490
  return outFiles;
5438
5491
  }
@@ -5445,6 +5498,23 @@ var BuildRepo = class {
5445
5498
  this.docs = docs;
5446
5499
  }
5447
5500
  };
5501
+ let tmpSeq = 0;
5502
+ /**
5503
+ * Writes `content` to a sibling temp file and renames it over `target`, so the
5504
+ * target is either the old file or the new one — never a partial write.
5505
+ * The temp file is removed if anything goes wrong.
5506
+ */ async function writeFileAtomic(target, content) {
5507
+ const tmp = `${target}.${process.pid}.${tmpSeq++}.tmp`;
5508
+ try {
5509
+ await (0, fs_promises.writeFile)(tmp, content);
5510
+ await (0, fs_promises.rename)(tmp, target);
5511
+ } catch (error) {
5512
+ try {
5513
+ await (0, fs_promises.rm)(tmp, { force: true });
5514
+ } catch {}
5515
+ throw error;
5516
+ }
5517
+ }
5448
5518
 
5449
5519
  //#endregion
5450
5520
  //#region packages/core/src/flatten.ts
package/dist/index.d.ts CHANGED
@@ -175,6 +175,14 @@ declare class BuildRepo {
175
175
  getDoc(id: string): AtscriptDoc | undefined;
176
176
  diagnostics(): Promise<Map<string, TMessages>>;
177
177
  generate(config: TAtscriptConfigOutput, docs?: AtscriptDoc[]): Promise<TOutput[]>;
178
+ /**
179
+ * Renders every document and writes the outputs to disk.
180
+ *
181
+ * Each write is awaited and atomic (temp file + rename), because a consumer
182
+ * watching the output — a dev server, a type checker, another build — must
183
+ * never observe a half-written file, and an un-awaited write can be
184
+ * truncated when the process exits.
185
+ */
178
186
  write(config: TAtscriptConfigOutput, docs?: AtscriptDoc[]): Promise<TOutput[]>;
179
187
  }
180
188
 
@@ -256,7 +264,7 @@ interface TAnnotationTokens {
256
264
  }
257
265
  type TPrimitiveAnnotationArg = string | number | boolean;
258
266
  type TPrimitiveAnnotationArgs = Record<string, TPrimitiveAnnotationArg>;
259
- type TPrimitiveAnnotationValue = boolean | string | number | TPrimitiveAnnotationArgs | (TPrimitiveAnnotationArg | TPrimitiveAnnotationArgs)[];
267
+ type TPrimitiveAnnotationValue = boolean | string | number | TPrimitiveAnnotationArgs | Array<TPrimitiveAnnotationArg | TPrimitiveAnnotationArgs>;
260
268
  interface TPrimitiveBaseConfig {
261
269
  type?: TPrimitiveTypeDef;
262
270
  documentation?: string;
@@ -848,6 +856,20 @@ interface TAtscriptConfigInput {
848
856
  include?: string[];
849
857
  exclude?: string[];
850
858
  db?: TDbConfig;
859
+ /**
860
+ * Extra Atscript models to include in `asc db sync` on top of the compiled
861
+ * `.as` files — typically models shipped by packages.
862
+ *
863
+ * The callback may return an array of annotated types, a module namespace
864
+ * object (`models: () => import('some-package/models')`), or a nested
865
+ * combination of both. Every export that `isAnnotatedType()` accepts and
866
+ * that carries `@db.table` or `@db.view` metadata is included, deduplicated
867
+ * by identity.
868
+ *
869
+ * Declared at the top level so it works with both the declarative and the
870
+ * function form of `db`.
871
+ */
872
+ models?: () => unknown | Promise<unknown>;
851
873
  }
852
874
  interface TAtscriptConfigOutput {
853
875
  format: TAtscriptRenderFormat;
package/dist/index.mjs CHANGED
@@ -7,7 +7,7 @@ import path$1 from "path";
7
7
  import { URL } from "url";
8
8
  import { Node, printTree } from "@prostojs/parser";
9
9
  import { defu } from "defu";
10
- import { mkdir, writeFile } from "fs/promises";
10
+ import { mkdir, rename, rm, writeFile } from "fs/promises";
11
11
  import { glob } from "glob";
12
12
 
13
13
  //#region packages/core/src/parser/nodes/semantic-node.ts
@@ -1460,11 +1460,7 @@ function getRelFilterScope(owner) {
1460
1460
  const unwound = doc.unwindType(typeName);
1461
1461
  if (!unwound?.def) return [];
1462
1462
  let def = doc.mergeIntersection(unwound.def);
1463
- if (isInterface(def)) {
1464
- const resolved = doc.resolveInterfaceExtends(def);
1465
- if (resolved) def = resolved;
1466
- else def = def.getDefinition() || def;
1467
- }
1463
+ if (isInterface(def)) def = doc.resolveInterfaceExtends(def) || def.getDefinition() || def;
1468
1464
  if (isStructure(def) || isInterface(def)) return Array.from(def.props.values());
1469
1465
  return [];
1470
1466
  }
@@ -1930,6 +1926,38 @@ const block$1 = new Node({
1930
1926
  }
1931
1927
  });
1932
1928
 
1929
+ //#endregion
1930
+ //#region packages/core/src/tokenizer/tokens/query-block.token.ts
1931
+ /**
1932
+ * Query block node — parenthesized sub-expressions inside backtick queries.
1933
+ *
1934
+ * Produces exactly the same token shape as `BlockToken` (`type: 'block'`,
1935
+ * `text: '('`) so every consumer keeps working, but it recognizes
1936
+ * `QueryOperatorToken` inside, which makes `!=`, `>=`, `<=`, `>`, `<`
1937
+ * work within parentheses (the generic block only knows `PunctuationToken`,
1938
+ * whose charset has no `<` / `>`).
1939
+ *
1940
+ * Only registered inside `QueryToken` (and itself, for nested parens) —
1941
+ * the generic `BlockToken` is left untouched so tokenization of `(`/`[`/`{`
1942
+ * outside queries does not change.
1943
+ */ const QueryBlockToken = new Node({
1944
+ name: "block",
1945
+ start: {
1946
+ token: RegExp("(?<text>\\()", "u"),
1947
+ omit: true
1948
+ },
1949
+ end: {
1950
+ token: ")",
1951
+ omit: true
1952
+ },
1953
+ skip: /\s/u,
1954
+ eofClose: true,
1955
+ data: {
1956
+ type: "block",
1957
+ text: ""
1958
+ }
1959
+ });
1960
+
1933
1961
  //#endregion
1934
1962
  //#region packages/core/src/tokenizer/tokens/query-operator.token.ts
1935
1963
  /**
@@ -2045,6 +2073,7 @@ const tokens = {
2045
2073
  text: TextToken,
2046
2074
  regexp: RegExpToken,
2047
2075
  query: QueryToken,
2076
+ queryBlock: QueryBlockToken,
2048
2077
  root: void 0
2049
2078
  };
2050
2079
  const root = new Node({
@@ -2064,7 +2093,8 @@ const root = new Node({
2064
2093
  });
2065
2094
  tokens.root = root;
2066
2095
  BlockToken.recognize(RegExpToken, ...commentNodes.all, BlockToken, QueryToken, AIdentifierToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
2067
- QueryToken.recognize(RegExpToken, QueryOperatorToken, BlockToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
2096
+ QueryToken.recognize(RegExpToken, QueryOperatorToken, QueryBlockToken, BlockToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
2097
+ QueryBlockToken.recognize(RegExpToken, QueryOperatorToken, QueryBlockToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
2068
2098
  /**
2069
2099
  * Recursively extract TLexicalToken[] from a ParsedNode tree.
2070
2100
  * Replaces the v0.5 mapContent('children', callback) + global loop.
@@ -2073,11 +2103,28 @@ QueryToken.recognize(RegExpToken, QueryOperatorToken, BlockToken, IdentifierToke
2073
2103
  * line is 1-based, column is 1-based.
2074
2104
  * VSCode expects 0-based line, 0-based character.
2075
2105
  */ function extractTokens(node) {
2076
- return node.content.map((item) => {
2077
- if (typeof item === "string") return {
2078
- type: "unknown",
2079
- text: item
2080
- };
2106
+ const content = node.content;
2107
+ return content.map((item, index) => {
2108
+ if (typeof item === "string") {
2109
+ const prev = content[index - 1];
2110
+ const next = content[index + 1];
2111
+ const start = typeof prev === "object" ? prev.end : node.start;
2112
+ const end = typeof next === "object" ? next.start : node.end;
2113
+ return {
2114
+ type: "unknown",
2115
+ text: item,
2116
+ getRange: () => ({
2117
+ start: {
2118
+ line: start.line - 1,
2119
+ character: start.column - 1
2120
+ },
2121
+ end: {
2122
+ line: end.line - 1,
2123
+ character: end.column - 1
2124
+ }
2125
+ })
2126
+ };
2127
+ }
2081
2128
  const data = item.data;
2082
2129
  data.getRange = () => ({
2083
2130
  start: {
@@ -2375,38 +2422,29 @@ const VALUE_KEYWORDS = new Set([
2375
2422
  * Returns a SemanticQueryNode or undefined if the content is empty.
2376
2423
  */ function parseQueryExpression(children, messages, sourceToken) {
2377
2424
  if (children.length === 0) return;
2425
+ const ctx = {
2426
+ messages,
2427
+ fallback: sourceToken.range
2428
+ };
2378
2429
  const ni = new NodeIterator(children, messages);
2379
2430
  ni.move();
2380
- const expr = parseOrExpr(ni, messages);
2431
+ const expr = parseOrExpr(ni, ctx);
2381
2432
  if (!expr) return;
2382
- if (ni.$) messages.push({
2383
- severity: 1,
2384
- message: `Unexpected token in query expression: "${ni.$.text}"`,
2385
- range: ni.$.getRange?.() ?? {
2386
- start: {
2387
- line: 0,
2388
- character: 0
2389
- },
2390
- end: {
2391
- line: 0,
2392
- character: 0
2393
- }
2394
- }
2395
- });
2433
+ if (ni.$) pushError(ni, ctx, `Unexpected token in query expression: "${ni.$.text}"`);
2396
2434
  const node = new SemanticQueryNode();
2397
2435
  node.expression = expr;
2398
2436
  node.sourceToken = sourceToken;
2399
2437
  return node;
2400
2438
  }
2401
- function parseOrExpr(ni, messages) {
2402
- const left = parseAndExpr(ni, messages);
2439
+ function parseOrExpr(ni, ctx) {
2440
+ const left = parseAndExpr(ni, ctx);
2403
2441
  if (!left) return;
2404
2442
  const operands = [left];
2405
2443
  while (ni.$?.type === "identifier" && ni.$.text === "or") {
2406
2444
  ni.move();
2407
- const right = parseAndExpr(ni, messages);
2445
+ const right = parseAndExpr(ni, ctx);
2408
2446
  if (!right) {
2409
- pushError(ni, messages, "Expected expression after \"or\"");
2447
+ pushError(ni, ctx, "Expected expression after \"or\"");
2410
2448
  break;
2411
2449
  }
2412
2450
  operands.push(right);
@@ -2417,15 +2455,15 @@ function parseOrExpr(ni, messages) {
2417
2455
  node.operands = operands;
2418
2456
  return node;
2419
2457
  }
2420
- function parseAndExpr(ni, messages) {
2421
- const left = parseUnaryExpr(ni, messages);
2458
+ function parseAndExpr(ni, ctx) {
2459
+ const left = parseUnaryExpr(ni, ctx);
2422
2460
  if (!left) return;
2423
2461
  const operands = [left];
2424
2462
  while (ni.$?.type === "identifier" && ni.$.text === "and") {
2425
2463
  ni.move();
2426
- const right = parseUnaryExpr(ni, messages);
2464
+ const right = parseUnaryExpr(ni, ctx);
2427
2465
  if (!right) {
2428
- pushError(ni, messages, "Expected expression after \"and\"");
2466
+ pushError(ni, ctx, "Expected expression after \"and\"");
2429
2467
  break;
2430
2468
  }
2431
2469
  operands.push(right);
@@ -2436,12 +2474,12 @@ function parseAndExpr(ni, messages) {
2436
2474
  node.operands = operands;
2437
2475
  return node;
2438
2476
  }
2439
- function parseUnaryExpr(ni, messages) {
2477
+ function parseUnaryExpr(ni, ctx) {
2440
2478
  if (ni.$?.type === "identifier" && ni.$.text === "not") {
2441
2479
  ni.move();
2442
- const operand = parseUnaryExpr(ni, messages);
2480
+ const operand = parseUnaryExpr(ni, ctx);
2443
2481
  if (!operand) {
2444
- pushError(ni, messages, "Expected expression after \"not\"");
2482
+ pushError(ni, ctx, "Expected expression after \"not\"");
2445
2483
  return;
2446
2484
  }
2447
2485
  const node = new SemanticQueryLogicalNode();
@@ -2450,29 +2488,34 @@ function parseUnaryExpr(ni, messages) {
2450
2488
  return node;
2451
2489
  }
2452
2490
  if (ni.$?.type === "block" && ni.$.text === "(") {
2453
- const blockChildren = ni.$.children || [];
2491
+ const blockToken = ni.$;
2492
+ const blockChildren = blockToken.children || [];
2454
2493
  ni.move();
2494
+ const subCtx = {
2495
+ messages: ctx.messages,
2496
+ fallback: rangeOf(blockToken, ctx)
2497
+ };
2455
2498
  if (blockChildren.length === 0) {
2456
- pushError(ni, messages, "Empty parenthesized expression");
2499
+ pushErrorAt(subCtx.fallback, ctx, "Empty parenthesized expression");
2457
2500
  return;
2458
2501
  }
2459
- const subNi = new NodeIterator(blockChildren, messages);
2502
+ const subNi = new NodeIterator(blockChildren, ctx.messages);
2460
2503
  subNi.move();
2461
- const expr = parseOrExpr(subNi, messages);
2462
- if (subNi.$) pushError(subNi, messages, `Unexpected token in parenthesized expression: "${subNi.$.text}"`);
2504
+ const expr = parseOrExpr(subNi, subCtx);
2505
+ if (subNi.$) pushError(subNi, subCtx, `Unexpected token in parenthesized expression: "${subNi.$.text}"`);
2463
2506
  return expr;
2464
2507
  }
2465
- return parseComparison(ni, messages);
2508
+ return parseComparison(ni, ctx);
2466
2509
  }
2467
- function parseComparison(ni, messages) {
2468
- const left = parseFieldRef(ni, messages);
2510
+ function parseComparison(ni, ctx) {
2511
+ const left = parseFieldRef(ni, ctx);
2469
2512
  if (!left) return;
2470
2513
  if (ni.$?.type === "punctuation" && SYMBOLIC_OPS.has(ni.$.text)) {
2471
2514
  const opText = ni.$.text;
2472
2515
  ni.move();
2473
- const right = parseValueOrFieldRef(ni, messages);
2516
+ const right = parseValueOrFieldRef(ni, ctx);
2474
2517
  if (!right) {
2475
- pushError(ni, messages, `Expected value or field reference after "${opText}"`);
2518
+ pushError(ni, ctx, `Expected value or field reference after "${opText}"`);
2476
2519
  return;
2477
2520
  }
2478
2521
  const node = new SemanticQueryComparisonNode();
@@ -2485,7 +2528,7 @@ function parseComparison(ni, messages) {
2485
2528
  const opText = ni.$.text;
2486
2529
  if (opText === "in") {
2487
2530
  ni.move();
2488
- const right = parseValueList(ni, messages);
2531
+ const right = parseValueList(ni, ctx);
2489
2532
  if (!right) return;
2490
2533
  const node = new SemanticQueryComparisonNode();
2491
2534
  node.left = left;
@@ -2495,9 +2538,9 @@ function parseComparison(ni, messages) {
2495
2538
  }
2496
2539
  if (opText === "matches") {
2497
2540
  ni.move();
2498
- const right = parseValueOrFieldRef(ni, messages);
2541
+ const right = parseValueOrFieldRef(ni, ctx);
2499
2542
  if (!right) {
2500
- pushError(ni, messages, "Expected value after \"matches\"");
2543
+ pushError(ni, ctx, "Expected value after \"matches\"");
2501
2544
  return;
2502
2545
  }
2503
2546
  const node = new SemanticQueryComparisonNode();
@@ -2513,17 +2556,17 @@ function parseComparison(ni, messages) {
2513
2556
  node.operator = "exists";
2514
2557
  return node;
2515
2558
  }
2516
- if (opText === "not") return parseNotComparison(ni, messages, left);
2517
- pushError(ni, messages, `Unknown operator "${opText}"`);
2559
+ if (opText === "not") return parseNotComparison(ni, ctx, left);
2560
+ pushError(ni, ctx, `Unknown operator "${opText}"`);
2518
2561
  return;
2519
2562
  }
2520
- pushError(ni, messages, "Expected operator after field reference");
2563
+ pushError(ni, ctx, "Expected operator after field reference");
2521
2564
  }
2522
- function parseNotComparison(ni, messages, left) {
2565
+ function parseNotComparison(ni, ctx, left) {
2523
2566
  ni.move();
2524
2567
  if (ni.$?.type === "identifier" && ni.$.text === "in") {
2525
2568
  ni.move();
2526
- const right = parseValueList(ni, messages);
2569
+ const right = parseValueList(ni, ctx);
2527
2570
  if (!right) return;
2528
2571
  const node = new SemanticQueryComparisonNode();
2529
2572
  node.left = left;
@@ -2538,11 +2581,11 @@ function parseNotComparison(ni, messages, left) {
2538
2581
  node.operator = "not exists";
2539
2582
  return node;
2540
2583
  }
2541
- pushError(ni, messages, "Expected \"in\" or \"exists\" after \"not\"");
2584
+ pushError(ni, ctx, "Expected \"in\" or \"exists\" after \"not\"");
2542
2585
  }
2543
- function parseFieldRef(ni, messages) {
2586
+ function parseFieldRef(ni, ctx) {
2544
2587
  if (ni.$?.type !== "identifier") {
2545
- pushError(ni, messages, "Expected field reference");
2588
+ pushError(ni, ctx, "Expected field reference");
2546
2589
  return;
2547
2590
  }
2548
2591
  const firstToken = new Token(ni.$);
@@ -2559,17 +2602,16 @@ function parseFieldRef(ni, messages) {
2559
2602
  fieldText += `.${ni.$.text}`;
2560
2603
  ni.move();
2561
2604
  } else {
2562
- pushError(ni, messages, "Expected identifier after \".\"");
2605
+ pushError(ni, ctx, "Expected identifier after \".\"");
2563
2606
  break;
2564
2607
  }
2565
2608
  }
2566
2609
  const node = new SemanticQueryFieldRefNode();
2567
2610
  node.typeRef = firstToken;
2568
- if (fieldText !== secondToken.text) node.fieldRef = secondToken.clone({ text: fieldText });
2569
- else node.fieldRef = secondToken;
2611
+ node.fieldRef = fieldText === secondToken.text ? secondToken : secondToken.clone({ text: fieldText });
2570
2612
  return node;
2571
2613
  }
2572
- pushError(ni, messages, "Expected identifier after \".\"");
2614
+ pushError(ni, ctx, "Expected identifier after \".\"");
2573
2615
  const node = new SemanticQueryFieldRefNode();
2574
2616
  node.fieldRef = firstToken;
2575
2617
  return node;
@@ -2578,18 +2620,18 @@ function parseFieldRef(ni, messages) {
2578
2620
  node.fieldRef = firstToken;
2579
2621
  return node;
2580
2622
  }
2581
- function parseValueOrFieldRef(ni, messages) {
2623
+ function parseValueOrFieldRef(ni, ctx) {
2582
2624
  if (!ni.$) return;
2583
- if (ni.$.type === "text" || ni.$.type === "number" || ni.$.type === "regexp") return parseValue(ni, messages);
2625
+ if (ni.$.type === "text" || ni.$.type === "number" || ni.$.type === "regexp") return parseValue(ni, ctx);
2584
2626
  if (ni.$.type === "identifier") {
2585
- if (VALUE_KEYWORDS.has(ni.$.text)) return parseValue(ni, messages);
2586
- return parseFieldRef(ni, messages);
2627
+ if (VALUE_KEYWORDS.has(ni.$.text)) return parseValue(ni, ctx);
2628
+ return parseFieldRef(ni, ctx);
2587
2629
  }
2588
- pushError(ni, messages, "Expected value or field reference");
2630
+ pushError(ni, ctx, "Expected value or field reference");
2589
2631
  }
2590
- function parseValue(ni, messages) {
2632
+ function parseValue(ni, ctx) {
2591
2633
  if (!ni.$) {
2592
- pushError(ni, messages, "Expected value");
2634
+ pushError(ni, ctx, "Expected value");
2593
2635
  return;
2594
2636
  }
2595
2637
  const token = ni.$;
@@ -2599,45 +2641,49 @@ function parseValue(ni, messages) {
2599
2641
  ni.move();
2600
2642
  return node;
2601
2643
  }
2602
- pushError(ni, messages, `Unexpected token "${token.text}" where value expected`);
2644
+ pushError(ni, ctx, `Unexpected token "${token.text}" where value expected`);
2603
2645
  }
2604
- function parseValueList(ni, messages) {
2646
+ function parseValueList(ni, ctx) {
2605
2647
  if (ni.$?.type === "block" && ni.$.text === "(") {
2606
- const blockChildren = ni.$.children || [];
2648
+ const blockToken = ni.$;
2649
+ const blockChildren = blockToken.children || [];
2607
2650
  ni.move();
2651
+ const subCtx = {
2652
+ messages: ctx.messages,
2653
+ fallback: rangeOf(blockToken, ctx)
2654
+ };
2608
2655
  const values = [];
2609
2656
  if (blockChildren.length === 0) {
2610
- pushError(ni, messages, "Empty value list in \"in\" expression");
2657
+ pushErrorAt(subCtx.fallback, ctx, "Empty value list in \"in\" expression");
2611
2658
  return;
2612
2659
  }
2613
- const subNi = new NodeIterator(blockChildren, messages);
2660
+ const subNi = new NodeIterator(blockChildren, ctx.messages);
2614
2661
  subNi.move();
2615
- const first = parseValue(subNi, messages);
2662
+ const first = parseValue(subNi, subCtx);
2616
2663
  if (first) values.push(first);
2617
2664
  while (subNi.$?.type === "punctuation" && subNi.$.text === ",") {
2618
2665
  subNi.move();
2619
- const v = parseValue(subNi, messages);
2666
+ const v = parseValue(subNi, subCtx);
2620
2667
  if (v) values.push(v);
2621
2668
  }
2622
- if (subNi.$) pushError(subNi, messages, `Unexpected token in value list: "${subNi.$.text}"`);
2669
+ if (subNi.$) pushError(subNi, subCtx, `Unexpected token in value list: "${subNi.$.text}"`);
2623
2670
  const node = new SemanticQueryValueListNode();
2624
2671
  node.values = values;
2625
2672
  return node;
2626
2673
  }
2627
- pushError(ni, messages, "Expected parenthesized value list after \"in\"");
2674
+ pushError(ni, ctx, "Expected parenthesized value list after \"in\"");
2628
2675
  }
2629
- function pushError(ni, messages, message) {
2630
- const range = ni.$?.getRange?.() ?? {
2631
- start: {
2632
- line: 0,
2633
- character: 0
2634
- },
2635
- end: {
2636
- line: 0,
2637
- character: 0
2638
- }
2639
- };
2640
- messages.push({
2676
+ /**
2677
+ * Range of a token, falling back to the enclosing query/paren range when the
2678
+ * token carries no position of its own.
2679
+ */ function rangeOf(token, ctx) {
2680
+ return token?.getRange?.() ?? ctx.fallback;
2681
+ }
2682
+ function pushError(ni, ctx, message) {
2683
+ pushErrorAt(rangeOf(ni.$, ctx), ctx, message);
2684
+ }
2685
+ function pushErrorAt(range, ctx, message) {
2686
+ ctx.messages.push({
2641
2687
  severity: 1,
2642
2688
  message,
2643
2689
  range
@@ -5397,11 +5443,18 @@ var BuildRepo = class {
5397
5443
  }
5398
5444
  return outFiles;
5399
5445
  }
5400
- async write(config, docs = this.docs) {
5446
+ /**
5447
+ * Renders every document and writes the outputs to disk.
5448
+ *
5449
+ * Each write is awaited and atomic (temp file + rename), because a consumer
5450
+ * watching the output — a dev server, a type checker, another build — must
5451
+ * never observe a half-written file, and an un-awaited write can be
5452
+ * truncated when the process exits.
5453
+ */ async write(config, docs = this.docs) {
5401
5454
  const outFiles = await this.generate(config, docs);
5402
5455
  for (const o of outFiles) if (o.target) {
5403
5456
  await mkdir(path$1.dirname(o.target), { recursive: true });
5404
- writeFile(o.target, o.content);
5457
+ await writeFileAtomic(o.target, o.content);
5405
5458
  }
5406
5459
  return outFiles;
5407
5460
  }
@@ -5414,6 +5467,23 @@ var BuildRepo = class {
5414
5467
  this.docs = docs;
5415
5468
  }
5416
5469
  };
5470
+ let tmpSeq = 0;
5471
+ /**
5472
+ * Writes `content` to a sibling temp file and renames it over `target`, so the
5473
+ * target is either the old file or the new one — never a partial write.
5474
+ * The temp file is removed if anything goes wrong.
5475
+ */ async function writeFileAtomic(target, content) {
5476
+ const tmp = `${target}.${process.pid}.${tmpSeq++}.tmp`;
5477
+ try {
5478
+ await writeFile(tmp, content);
5479
+ await rename(tmp, target);
5480
+ } catch (error) {
5481
+ try {
5482
+ await rm(tmp, { force: true });
5483
+ } catch {}
5484
+ throw error;
5485
+ }
5486
+ }
5417
5487
 
5418
5488
  //#endregion
5419
5489
  //#region packages/core/src/flatten.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/core",
3
- "version": "0.1.89",
3
+ "version": "0.1.90",
4
4
  "description": "Core library for Atscript parsing and file generation.",
5
5
  "keywords": [
6
6
  "annotations",