@atscript/core 0.1.90 → 0.1.92

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
@@ -1829,29 +1829,35 @@ var NodeIterator = class NodeIterator {
1829
1829
 
1830
1830
  //#endregion
1831
1831
  //#region packages/core/src/tokenizer/tokens/block.token.ts
1832
- const pairs = {
1832
+ /**
1833
+ * Builds a block node that opens on any key of `pairs` and closes on the
1834
+ * matching value; the opening character is kept as the token's `text`.
1835
+ */ function createBlockToken(pairs) {
1836
+ const opening = Object.keys(pairs).map((c) => c.replace(/[\\^$.*+?()[\]{}|/-]/gu, "\\$&")).join("");
1837
+ return new _prostojs_parser.Node({
1838
+ name: "block",
1839
+ start: {
1840
+ token: new RegExp(`(?<text>[${opening}])`, "u"),
1841
+ omit: true
1842
+ },
1843
+ end: {
1844
+ token: (ctx) => pairs[ctx.node.data.text] || "",
1845
+ omit: true
1846
+ },
1847
+ skip: /\s/u,
1848
+ eofClose: true,
1849
+ data: {
1850
+ type: "block",
1851
+ text: ""
1852
+ }
1853
+ });
1854
+ }
1855
+ /**
1856
+ * Block node
1857
+ */ const BlockToken = createBlockToken({
1833
1858
  "{": "}",
1834
1859
  "(": ")",
1835
1860
  "[": "]"
1836
- };
1837
- /**
1838
- * Block node
1839
- */ const BlockToken = new _prostojs_parser.Node({
1840
- name: "block",
1841
- start: {
1842
- token: RegExp("(?<text>[([{])", "u"),
1843
- omit: true
1844
- },
1845
- end: {
1846
- token: (ctx) => pairs[ctx.node.data.text] || "",
1847
- omit: true
1848
- },
1849
- skip: /\s/u,
1850
- eofClose: true,
1851
- data: {
1852
- type: "block",
1853
- text: ""
1854
- }
1855
1861
  });
1856
1862
 
1857
1863
  //#endregion
@@ -1962,32 +1968,16 @@ const block$1 = new _prostojs_parser.Node({
1962
1968
  /**
1963
1969
  * Query block node — parenthesized sub-expressions inside backtick queries.
1964
1970
  *
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 `<` / `>`).
1971
+ * Same definition as `BlockToken` narrowed to `(`, so it produces the same
1972
+ * token shape (`type: 'block'`, `text: '('`) and every consumer keeps working,
1973
+ * but it recognizes `QueryOperatorToken` inside (see `tokens/index.ts`), which
1974
+ * makes `!=`, `>=`, `<=`, `>`, `<` work within parentheses — the generic block
1975
+ * only knows `PunctuationToken`, whose charset has no `<` / `>`.
1970
1976
  *
1971
1977
  * Only registered inside `QueryToken` (and itself, for nested parens) —
1972
1978
  * the generic `BlockToken` is left untouched so tokenization of `(`/`[`/`{`
1973
1979
  * 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
- });
1980
+ */ const QueryBlockToken = createBlockToken({ "(": ")" });
1991
1981
 
1992
1982
  //#endregion
1993
1983
  //#region packages/core/src/tokenizer/tokens/query-operator.token.ts
@@ -2126,6 +2116,12 @@ tokens.root = root;
2126
2116
  BlockToken.recognize(RegExpToken, ...commentNodes.all, BlockToken, QueryToken, AIdentifierToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
2127
2117
  QueryToken.recognize(RegExpToken, QueryOperatorToken, QueryBlockToken, BlockToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
2128
2118
  QueryBlockToken.recognize(RegExpToken, QueryOperatorToken, QueryBlockToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
2119
+ /** 1-based parser position → 0-based VSCode position, shifted by `offset` characters. */ function toPos(p, offset = 0) {
2120
+ return {
2121
+ line: p.line - 1,
2122
+ character: p.column - 1 + offset
2123
+ };
2124
+ }
2129
2125
  /**
2130
2126
  * Recursively extract TLexicalToken[] from a ParsedNode tree.
2131
2127
  * Replaces the v0.5 mapContent('children', callback) + global loop.
@@ -2145,27 +2141,15 @@ QueryBlockToken.recognize(RegExpToken, QueryOperatorToken, QueryBlockToken, Iden
2145
2141
  type: "unknown",
2146
2142
  text: item,
2147
2143
  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
- }
2144
+ start: toPos(start),
2145
+ end: toPos(end)
2156
2146
  })
2157
2147
  };
2158
2148
  }
2159
2149
  const data = item.data;
2160
2150
  data.getRange = () => ({
2161
- start: {
2162
- line: item.start.line - 1,
2163
- character: item.start.column - 1 + (data.startOffset ?? 0)
2164
- },
2165
- end: {
2166
- line: item.end.line - 1,
2167
- character: item.end.column - 1 + (data.endOffset ?? 0)
2168
- }
2151
+ start: toPos(item.start, data.startOffset),
2152
+ end: toPos(item.end, data.endOffset)
2169
2153
  });
2170
2154
  data.children = extractTokens(item);
2171
2155
  return data;
@@ -2448,34 +2432,42 @@ const VALUE_KEYWORDS = new Set([
2448
2432
  "null",
2449
2433
  "undefined"
2450
2434
  ]);
2435
+ const ZERO_RANGE = {
2436
+ start: {
2437
+ line: 0,
2438
+ character: 0
2439
+ },
2440
+ end: {
2441
+ line: 0,
2442
+ character: 0
2443
+ }
2444
+ };
2451
2445
  /**
2452
- * Parse a backtick-delimited query expression from its child tokens.
2446
+ * Parse a backtick-delimited query expression.
2447
+ *
2448
+ * `ni` iterates the query token's children and carries that token as its
2449
+ * `parent` — the range to blame when a diagnostic has no token of its own —
2450
+ * so fork it from the annotation iterator while the query token is current.
2453
2451
  * Returns a SemanticQueryNode or undefined if the content is empty.
2454
- */ function parseQueryExpression(children, messages, sourceToken) {
2455
- if (children.length === 0) return;
2456
- const ctx = {
2457
- messages,
2458
- fallback: sourceToken.range
2459
- };
2460
- const ni = new NodeIterator(children, messages);
2461
- ni.move();
2462
- const expr = parseOrExpr(ni, ctx);
2452
+ */ function parseQueryExpression(ni, sourceToken) {
2453
+ if (!ni.$) return;
2454
+ const expr = parseOrExpr(ni);
2463
2455
  if (!expr) return;
2464
- if (ni.$) pushError(ni, ctx, `Unexpected token in query expression: "${ni.$.text}"`);
2456
+ if (ni.$) pushError(ni, `Unexpected token in query expression: "${ni.$.text}"`);
2465
2457
  const node = new SemanticQueryNode();
2466
2458
  node.expression = expr;
2467
2459
  node.sourceToken = sourceToken;
2468
2460
  return node;
2469
2461
  }
2470
- function parseOrExpr(ni, ctx) {
2471
- const left = parseAndExpr(ni, ctx);
2462
+ function parseOrExpr(ni) {
2463
+ const left = parseAndExpr(ni);
2472
2464
  if (!left) return;
2473
2465
  const operands = [left];
2474
2466
  while (ni.$?.type === "identifier" && ni.$.text === "or") {
2475
2467
  ni.move();
2476
- const right = parseAndExpr(ni, ctx);
2468
+ const right = parseAndExpr(ni);
2477
2469
  if (!right) {
2478
- pushError(ni, ctx, "Expected expression after \"or\"");
2470
+ pushError(ni, "Expected expression after \"or\"");
2479
2471
  break;
2480
2472
  }
2481
2473
  operands.push(right);
@@ -2486,15 +2478,15 @@ function parseOrExpr(ni, ctx) {
2486
2478
  node.operands = operands;
2487
2479
  return node;
2488
2480
  }
2489
- function parseAndExpr(ni, ctx) {
2490
- const left = parseUnaryExpr(ni, ctx);
2481
+ function parseAndExpr(ni) {
2482
+ const left = parseUnaryExpr(ni);
2491
2483
  if (!left) return;
2492
2484
  const operands = [left];
2493
2485
  while (ni.$?.type === "identifier" && ni.$.text === "and") {
2494
2486
  ni.move();
2495
- const right = parseUnaryExpr(ni, ctx);
2487
+ const right = parseUnaryExpr(ni);
2496
2488
  if (!right) {
2497
- pushError(ni, ctx, "Expected expression after \"and\"");
2489
+ pushError(ni, "Expected expression after \"and\"");
2498
2490
  break;
2499
2491
  }
2500
2492
  operands.push(right);
@@ -2505,12 +2497,12 @@ function parseAndExpr(ni, ctx) {
2505
2497
  node.operands = operands;
2506
2498
  return node;
2507
2499
  }
2508
- function parseUnaryExpr(ni, ctx) {
2500
+ function parseUnaryExpr(ni) {
2509
2501
  if (ni.$?.type === "identifier" && ni.$.text === "not") {
2510
2502
  ni.move();
2511
- const operand = parseUnaryExpr(ni, ctx);
2503
+ const operand = parseUnaryExpr(ni);
2512
2504
  if (!operand) {
2513
- pushError(ni, ctx, "Expected expression after \"not\"");
2505
+ pushError(ni, "Expected expression after \"not\"");
2514
2506
  return;
2515
2507
  }
2516
2508
  const node = new SemanticQueryLogicalNode();
@@ -2519,34 +2511,27 @@ function parseUnaryExpr(ni, ctx) {
2519
2511
  return node;
2520
2512
  }
2521
2513
  if (ni.$?.type === "block" && ni.$.text === "(") {
2522
- const blockToken = ni.$;
2523
- const blockChildren = blockToken.children || [];
2514
+ const subNi = ni.fork(ni.$.children || []);
2524
2515
  ni.move();
2525
- const subCtx = {
2526
- messages: ctx.messages,
2527
- fallback: rangeOf(blockToken, ctx)
2528
- };
2529
- if (blockChildren.length === 0) {
2530
- pushErrorAt(subCtx.fallback, ctx, "Empty parenthesized expression");
2516
+ if (!subNi.$) {
2517
+ pushError(subNi, "Empty parenthesized expression");
2531
2518
  return;
2532
2519
  }
2533
- const subNi = new NodeIterator(blockChildren, ctx.messages);
2534
- subNi.move();
2535
- const expr = parseOrExpr(subNi, subCtx);
2536
- if (subNi.$) pushError(subNi, subCtx, `Unexpected token in parenthesized expression: "${subNi.$.text}"`);
2520
+ const expr = parseOrExpr(subNi);
2521
+ if (subNi.$) pushError(subNi, `Unexpected token in parenthesized expression: "${subNi.$.text}"`);
2537
2522
  return expr;
2538
2523
  }
2539
- return parseComparison(ni, ctx);
2524
+ return parseComparison(ni);
2540
2525
  }
2541
- function parseComparison(ni, ctx) {
2542
- const left = parseFieldRef(ni, ctx);
2526
+ function parseComparison(ni) {
2527
+ const left = parseFieldRef(ni);
2543
2528
  if (!left) return;
2544
2529
  if (ni.$?.type === "punctuation" && SYMBOLIC_OPS.has(ni.$.text)) {
2545
2530
  const opText = ni.$.text;
2546
2531
  ni.move();
2547
- const right = parseValueOrFieldRef(ni, ctx);
2532
+ const right = parseValueOrFieldRef(ni);
2548
2533
  if (!right) {
2549
- pushError(ni, ctx, `Expected value or field reference after "${opText}"`);
2534
+ pushError(ni, `Expected value or field reference after "${opText}"`);
2550
2535
  return;
2551
2536
  }
2552
2537
  const node = new SemanticQueryComparisonNode();
@@ -2559,7 +2544,7 @@ function parseComparison(ni, ctx) {
2559
2544
  const opText = ni.$.text;
2560
2545
  if (opText === "in") {
2561
2546
  ni.move();
2562
- const right = parseValueList(ni, ctx);
2547
+ const right = parseValueList(ni);
2563
2548
  if (!right) return;
2564
2549
  const node = new SemanticQueryComparisonNode();
2565
2550
  node.left = left;
@@ -2569,9 +2554,9 @@ function parseComparison(ni, ctx) {
2569
2554
  }
2570
2555
  if (opText === "matches") {
2571
2556
  ni.move();
2572
- const right = parseValueOrFieldRef(ni, ctx);
2557
+ const right = parseValueOrFieldRef(ni);
2573
2558
  if (!right) {
2574
- pushError(ni, ctx, "Expected value after \"matches\"");
2559
+ pushError(ni, "Expected value after \"matches\"");
2575
2560
  return;
2576
2561
  }
2577
2562
  const node = new SemanticQueryComparisonNode();
@@ -2587,17 +2572,17 @@ function parseComparison(ni, ctx) {
2587
2572
  node.operator = "exists";
2588
2573
  return node;
2589
2574
  }
2590
- if (opText === "not") return parseNotComparison(ni, ctx, left);
2591
- pushError(ni, ctx, `Unknown operator "${opText}"`);
2575
+ if (opText === "not") return parseNotComparison(ni, left);
2576
+ pushError(ni, `Unknown operator "${opText}"`);
2592
2577
  return;
2593
2578
  }
2594
- pushError(ni, ctx, "Expected operator after field reference");
2579
+ pushError(ni, "Expected operator after field reference");
2595
2580
  }
2596
- function parseNotComparison(ni, ctx, left) {
2581
+ function parseNotComparison(ni, left) {
2597
2582
  ni.move();
2598
2583
  if (ni.$?.type === "identifier" && ni.$.text === "in") {
2599
2584
  ni.move();
2600
- const right = parseValueList(ni, ctx);
2585
+ const right = parseValueList(ni);
2601
2586
  if (!right) return;
2602
2587
  const node = new SemanticQueryComparisonNode();
2603
2588
  node.left = left;
@@ -2612,11 +2597,11 @@ function parseNotComparison(ni, ctx, left) {
2612
2597
  node.operator = "not exists";
2613
2598
  return node;
2614
2599
  }
2615
- pushError(ni, ctx, "Expected \"in\" or \"exists\" after \"not\"");
2600
+ pushError(ni, "Expected \"in\" or \"exists\" after \"not\"");
2616
2601
  }
2617
- function parseFieldRef(ni, ctx) {
2602
+ function parseFieldRef(ni) {
2618
2603
  if (ni.$?.type !== "identifier") {
2619
- pushError(ni, ctx, "Expected field reference");
2604
+ pushError(ni, "Expected field reference");
2620
2605
  return;
2621
2606
  }
2622
2607
  const firstToken = new Token(ni.$);
@@ -2633,7 +2618,7 @@ function parseFieldRef(ni, ctx) {
2633
2618
  fieldText += `.${ni.$.text}`;
2634
2619
  ni.move();
2635
2620
  } else {
2636
- pushError(ni, ctx, "Expected identifier after \".\"");
2621
+ pushError(ni, "Expected identifier after \".\"");
2637
2622
  break;
2638
2623
  }
2639
2624
  }
@@ -2642,7 +2627,7 @@ function parseFieldRef(ni, ctx) {
2642
2627
  node.fieldRef = fieldText === secondToken.text ? secondToken : secondToken.clone({ text: fieldText });
2643
2628
  return node;
2644
2629
  }
2645
- pushError(ni, ctx, "Expected identifier after \".\"");
2630
+ pushError(ni, "Expected identifier after \".\"");
2646
2631
  const node = new SemanticQueryFieldRefNode();
2647
2632
  node.fieldRef = firstToken;
2648
2633
  return node;
@@ -2651,18 +2636,18 @@ function parseFieldRef(ni, ctx) {
2651
2636
  node.fieldRef = firstToken;
2652
2637
  return node;
2653
2638
  }
2654
- function parseValueOrFieldRef(ni, ctx) {
2639
+ function parseValueOrFieldRef(ni) {
2655
2640
  if (!ni.$) return;
2656
- if (ni.$.type === "text" || ni.$.type === "number" || ni.$.type === "regexp") return parseValue(ni, ctx);
2641
+ if (ni.$.type === "text" || ni.$.type === "number" || ni.$.type === "regexp") return parseValue(ni);
2657
2642
  if (ni.$.type === "identifier") {
2658
- if (VALUE_KEYWORDS.has(ni.$.text)) return parseValue(ni, ctx);
2659
- return parseFieldRef(ni, ctx);
2643
+ if (VALUE_KEYWORDS.has(ni.$.text)) return parseValue(ni);
2644
+ return parseFieldRef(ni);
2660
2645
  }
2661
- pushError(ni, ctx, "Expected value or field reference");
2646
+ pushError(ni, "Expected value or field reference");
2662
2647
  }
2663
- function parseValue(ni, ctx) {
2648
+ function parseValue(ni) {
2664
2649
  if (!ni.$) {
2665
- pushError(ni, ctx, "Expected value");
2650
+ pushError(ni, "Expected value");
2666
2651
  return;
2667
2652
  }
2668
2653
  const token = ni.$;
@@ -2672,52 +2657,42 @@ function parseValue(ni, ctx) {
2672
2657
  ni.move();
2673
2658
  return node;
2674
2659
  }
2675
- pushError(ni, ctx, `Unexpected token "${token.text}" where value expected`);
2660
+ pushError(ni, `Unexpected token "${token.text}" where value expected`);
2676
2661
  }
2677
- function parseValueList(ni, ctx) {
2662
+ function parseValueList(ni) {
2678
2663
  if (ni.$?.type === "block" && ni.$.text === "(") {
2679
- const blockToken = ni.$;
2680
- const blockChildren = blockToken.children || [];
2664
+ const subNi = ni.fork(ni.$.children || []);
2681
2665
  ni.move();
2682
- const subCtx = {
2683
- messages: ctx.messages,
2684
- fallback: rangeOf(blockToken, ctx)
2685
- };
2686
- const values = [];
2687
- if (blockChildren.length === 0) {
2688
- pushErrorAt(subCtx.fallback, ctx, "Empty value list in \"in\" expression");
2666
+ if (!subNi.$) {
2667
+ pushError(subNi, "Empty value list in \"in\" expression");
2689
2668
  return;
2690
2669
  }
2691
- const subNi = new NodeIterator(blockChildren, ctx.messages);
2692
- subNi.move();
2693
- const first = parseValue(subNi, subCtx);
2670
+ const values = [];
2671
+ const first = parseValue(subNi);
2694
2672
  if (first) values.push(first);
2695
2673
  while (subNi.$?.type === "punctuation" && subNi.$.text === ",") {
2696
2674
  subNi.move();
2697
- const v = parseValue(subNi, subCtx);
2675
+ const v = parseValue(subNi);
2698
2676
  if (v) values.push(v);
2699
2677
  }
2700
- if (subNi.$) pushError(subNi, subCtx, `Unexpected token in value list: "${subNi.$.text}"`);
2678
+ if (subNi.$) pushError(subNi, `Unexpected token in value list: "${subNi.$.text}"`);
2701
2679
  const node = new SemanticQueryValueListNode();
2702
2680
  node.values = values;
2703
2681
  return node;
2704
2682
  }
2705
- pushError(ni, ctx, "Expected parenthesized value list after \"in\"");
2683
+ pushError(ni, "Expected parenthesized value list after \"in\"");
2706
2684
  }
2707
2685
  /**
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;
2686
+ * Range to blame for a diagnostic: the current token, else the enclosing
2687
+ * query token / "(...)" block the iterator was forked from.
2688
+ */ function rangeOf(ni) {
2689
+ return ni.$?.getRange?.() ?? ni.parent?.getRange?.() ?? ZERO_RANGE;
2712
2690
  }
2713
- function pushError(ni, ctx, message) {
2714
- pushErrorAt(rangeOf(ni.$, ctx), ctx, message);
2715
- }
2716
- function pushErrorAt(range, ctx, message) {
2717
- ctx.messages.push({
2691
+ function pushError(ni, message) {
2692
+ ni.messages.push({
2718
2693
  severity: 1,
2719
2694
  message,
2720
- range
2695
+ range: rangeOf(ni)
2721
2696
  });
2722
2697
  }
2723
2698
 
@@ -2840,6 +2815,10 @@ function annotations() {
2840
2815
  }
2841
2816
  while (ni.satisfies(...opts.argument)) {
2842
2817
  let argToken = new Token(ni.$);
2818
+ if (argToken.type === "query") {
2819
+ const queryNode = parseQueryExpression(ni.fork(argToken.children), argToken);
2820
+ if (queryNode) argToken.queryNode = queryNode;
2821
+ }
2843
2822
  ni.accepted();
2844
2823
  ni.move();
2845
2824
  if (argToken.type === "identifier" && ![
@@ -2858,10 +2837,6 @@ function annotations() {
2858
2837
  }
2859
2838
  if (chainText !== argToken.text) argToken = argToken.clone({ text: chainText });
2860
2839
  }
2861
- if (argToken.type === "query") {
2862
- const queryNode = parseQueryExpression(argToken.children, ni.messages, argToken);
2863
- if (queryNode) argToken.queryNode = queryNode;
2864
- }
2865
2840
  addArgument(argToken);
2866
2841
  if (ni.satisfies(opts.comma)) {
2867
2842
  ni.accepted();
@@ -5299,6 +5274,28 @@ var AtscriptRepo = class {
5299
5274
  }
5300
5275
  return atscript;
5301
5276
  }
5277
+ /**
5278
+ * Drops the cached document for `id` and detaches it from its dependencies
5279
+ * (which also removes it from each dependency's `dependants`).
5280
+ *
5281
+ * Documents are cached forever and an imported document is read from disk
5282
+ * only once, so a file that changed outside the repo (a watcher, a dev
5283
+ * server, an editor) keeps resolving to its stale parse until it is closed.
5284
+ * After closing, the next `openDocument(id)` without text re-reads the file.
5285
+ *
5286
+ * Dependants are left untouched on purpose: their next `checkDoc` re-runs
5287
+ * `checkImport` -> `openDocument(id)`, which re-reads the file and rewires
5288
+ * their `dependenciesMap` to the fresh document.
5289
+ *
5290
+ * @returns `true` when a cached document was dropped, `false` when nothing
5291
+ * was cached under `id`.
5292
+ */ closeDocument(id) {
5293
+ const cached = this.atscripts.get(id);
5294
+ if (!cached) return false;
5295
+ this.atscripts.delete(id);
5296
+ cached.then((d) => d.updateDependencies([]), () => {});
5297
+ return true;
5298
+ }
5302
5299
  async _openDocument(id, text) {
5303
5300
  const { manager } = await this.resolveConfig(id);
5304
5301
  const newId = await manager.resolve(id);
@@ -5425,6 +5422,7 @@ function _define_property(obj, key, value) {
5425
5422
  else obj[key] = value;
5426
5423
  return obj;
5427
5424
  }
5425
+ /** Upper bound on concurrent file operations — macOS defaults `ulimit -n` to 256. */ const WRITE_CONCURRENCY = 32;
5428
5426
  async function build(config) {
5429
5427
  const rootDir = config.rootDir ? config.rootDir.startsWith("/") ? config.rootDir : path.default.join(process.cwd(), config.rootDir) : process.cwd();
5430
5428
  config.rootDir = rootDir;
@@ -5480,13 +5478,14 @@ var BuildRepo = class {
5480
5478
  * Each write is awaited and atomic (temp file + rename), because a consumer
5481
5479
  * watching the output — a dev server, a type checker, another build — must
5482
5480
  * never observe a half-written file, and an un-awaited write can be
5483
- * truncated when the process exits.
5481
+ * truncated when the process exits. Outputs whose content did not change
5482
+ * are not rewritten, so those watchers see no event for them.
5484
5483
  */ async write(config, docs = this.docs) {
5485
5484
  const outFiles = await this.generate(config, docs);
5486
- for (const o of outFiles) if (o.target) {
5487
- await (0, fs_promises.mkdir)(path.default.dirname(o.target), { recursive: true });
5488
- await writeFileAtomic(o.target, o.content);
5489
- }
5485
+ const targeted = outFiles.filter((o) => o.target);
5486
+ const dirs = new Set(targeted.map((o) => path.default.dirname(o.target)));
5487
+ await Promise.all(Array.from(dirs, (dir) => (0, fs_promises.mkdir)(dir, { recursive: true })));
5488
+ for (let i = 0; i < targeted.length; i += WRITE_CONCURRENCY) await Promise.all(targeted.slice(i, i + WRITE_CONCURRENCY).map((o) => writeIfChanged(o.target, o.content)));
5490
5489
  return outFiles;
5491
5490
  }
5492
5491
  constructor(rootDir, repo, docs) {
@@ -5498,6 +5497,12 @@ var BuildRepo = class {
5498
5497
  this.docs = docs;
5499
5498
  }
5500
5499
  };
5500
+ /**
5501
+ * Writes `content` to `target` unless the file already holds exactly that
5502
+ * content, so an unchanged output produces no filesystem event.
5503
+ */ async function writeIfChanged(target, content) {
5504
+ if (await (0, fs_promises.readFile)(target, "utf8").catch(() => void 0) !== content) await writeFileAtomic(target, content);
5505
+ }
5501
5506
  let tmpSeq = 0;
5502
5507
  /**
5503
5508
  * Writes `content` to a sibling temp file and renames it over `target`, so the
package/dist/index.d.ts CHANGED
@@ -157,6 +157,23 @@ declare class AtscriptRepo {
157
157
  loadPluginManagerFor(id: string): Promise<TPluginManagers>;
158
158
  resolveConfig(id: string): Promise<TPluginManagers>;
159
159
  openDocument(id: string, text?: string): Promise<AtscriptDoc>;
160
+ /**
161
+ * Drops the cached document for `id` and detaches it from its dependencies
162
+ * (which also removes it from each dependency's `dependants`).
163
+ *
164
+ * Documents are cached forever and an imported document is read from disk
165
+ * only once, so a file that changed outside the repo (a watcher, a dev
166
+ * server, an editor) keeps resolving to its stale parse until it is closed.
167
+ * After closing, the next `openDocument(id)` without text re-reads the file.
168
+ *
169
+ * Dependants are left untouched on purpose: their next `checkDoc` re-runs
170
+ * `checkImport` -> `openDocument(id)`, which re-reads the file and rewires
171
+ * their `dependenciesMap` to the fresh document.
172
+ *
173
+ * @returns `true` when a cached document was dropped, `false` when nothing
174
+ * was cached under `id`.
175
+ */
176
+ closeDocument(id: string): boolean;
160
177
  protected _openDocument(id: string, text?: string): Promise<AtscriptDoc>;
161
178
  checkDoc(atscript: AtscriptDoc): Promise<void>;
162
179
  checkImports(atscript: AtscriptDoc, checked?: Set<string>): Promise<void>;
@@ -181,7 +198,8 @@ declare class BuildRepo {
181
198
  * Each write is awaited and atomic (temp file + rename), because a consumer
182
199
  * watching the output — a dev server, a type checker, another build — must
183
200
  * never observe a half-written file, and an un-awaited write can be
184
- * truncated when the process exits.
201
+ * truncated when the process exits. Outputs whose content did not change
202
+ * are not rewritten, so those watchers see no event for them.
185
203
  */
186
204
  write(config: TAtscriptConfigOutput, docs?: AtscriptDoc[]): Promise<TOutput[]>;
187
205
  }
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, rename, rm, writeFile } from "fs/promises";
10
+ import { mkdir, readFile as readFile$1, 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
@@ -1798,29 +1798,35 @@ var NodeIterator = class NodeIterator {
1798
1798
 
1799
1799
  //#endregion
1800
1800
  //#region packages/core/src/tokenizer/tokens/block.token.ts
1801
- const pairs = {
1801
+ /**
1802
+ * Builds a block node that opens on any key of `pairs` and closes on the
1803
+ * matching value; the opening character is kept as the token's `text`.
1804
+ */ function createBlockToken(pairs) {
1805
+ const opening = Object.keys(pairs).map((c) => c.replace(/[\\^$.*+?()[\]{}|/-]/gu, "\\$&")).join("");
1806
+ return new Node({
1807
+ name: "block",
1808
+ start: {
1809
+ token: new RegExp(`(?<text>[${opening}])`, "u"),
1810
+ omit: true
1811
+ },
1812
+ end: {
1813
+ token: (ctx) => pairs[ctx.node.data.text] || "",
1814
+ omit: true
1815
+ },
1816
+ skip: /\s/u,
1817
+ eofClose: true,
1818
+ data: {
1819
+ type: "block",
1820
+ text: ""
1821
+ }
1822
+ });
1823
+ }
1824
+ /**
1825
+ * Block node
1826
+ */ const BlockToken = createBlockToken({
1802
1827
  "{": "}",
1803
1828
  "(": ")",
1804
1829
  "[": "]"
1805
- };
1806
- /**
1807
- * Block node
1808
- */ const BlockToken = new Node({
1809
- name: "block",
1810
- start: {
1811
- token: RegExp("(?<text>[([{])", "u"),
1812
- omit: true
1813
- },
1814
- end: {
1815
- token: (ctx) => pairs[ctx.node.data.text] || "",
1816
- omit: true
1817
- },
1818
- skip: /\s/u,
1819
- eofClose: true,
1820
- data: {
1821
- type: "block",
1822
- text: ""
1823
- }
1824
1830
  });
1825
1831
 
1826
1832
  //#endregion
@@ -1931,32 +1937,16 @@ const block$1 = new Node({
1931
1937
  /**
1932
1938
  * Query block node — parenthesized sub-expressions inside backtick queries.
1933
1939
  *
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 `<` / `>`).
1940
+ * Same definition as `BlockToken` narrowed to `(`, so it produces the same
1941
+ * token shape (`type: 'block'`, `text: '('`) and every consumer keeps working,
1942
+ * but it recognizes `QueryOperatorToken` inside (see `tokens/index.ts`), which
1943
+ * makes `!=`, `>=`, `<=`, `>`, `<` work within parentheses — the generic block
1944
+ * only knows `PunctuationToken`, whose charset has no `<` / `>`.
1939
1945
  *
1940
1946
  * Only registered inside `QueryToken` (and itself, for nested parens) —
1941
1947
  * the generic `BlockToken` is left untouched so tokenization of `(`/`[`/`{`
1942
1948
  * 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
- });
1949
+ */ const QueryBlockToken = createBlockToken({ "(": ")" });
1960
1950
 
1961
1951
  //#endregion
1962
1952
  //#region packages/core/src/tokenizer/tokens/query-operator.token.ts
@@ -2095,6 +2085,12 @@ tokens.root = root;
2095
2085
  BlockToken.recognize(RegExpToken, ...commentNodes.all, BlockToken, QueryToken, AIdentifierToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
2096
2086
  QueryToken.recognize(RegExpToken, QueryOperatorToken, QueryBlockToken, BlockToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
2097
2087
  QueryBlockToken.recognize(RegExpToken, QueryOperatorToken, QueryBlockToken, IdentifierToken, TextToken, NumberToken, PunctuationToken);
2088
+ /** 1-based parser position → 0-based VSCode position, shifted by `offset` characters. */ function toPos(p, offset = 0) {
2089
+ return {
2090
+ line: p.line - 1,
2091
+ character: p.column - 1 + offset
2092
+ };
2093
+ }
2098
2094
  /**
2099
2095
  * Recursively extract TLexicalToken[] from a ParsedNode tree.
2100
2096
  * Replaces the v0.5 mapContent('children', callback) + global loop.
@@ -2114,27 +2110,15 @@ QueryBlockToken.recognize(RegExpToken, QueryOperatorToken, QueryBlockToken, Iden
2114
2110
  type: "unknown",
2115
2111
  text: item,
2116
2112
  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
- }
2113
+ start: toPos(start),
2114
+ end: toPos(end)
2125
2115
  })
2126
2116
  };
2127
2117
  }
2128
2118
  const data = item.data;
2129
2119
  data.getRange = () => ({
2130
- start: {
2131
- line: item.start.line - 1,
2132
- character: item.start.column - 1 + (data.startOffset ?? 0)
2133
- },
2134
- end: {
2135
- line: item.end.line - 1,
2136
- character: item.end.column - 1 + (data.endOffset ?? 0)
2137
- }
2120
+ start: toPos(item.start, data.startOffset),
2121
+ end: toPos(item.end, data.endOffset)
2138
2122
  });
2139
2123
  data.children = extractTokens(item);
2140
2124
  return data;
@@ -2417,34 +2401,42 @@ const VALUE_KEYWORDS = new Set([
2417
2401
  "null",
2418
2402
  "undefined"
2419
2403
  ]);
2404
+ const ZERO_RANGE = {
2405
+ start: {
2406
+ line: 0,
2407
+ character: 0
2408
+ },
2409
+ end: {
2410
+ line: 0,
2411
+ character: 0
2412
+ }
2413
+ };
2420
2414
  /**
2421
- * Parse a backtick-delimited query expression from its child tokens.
2415
+ * Parse a backtick-delimited query expression.
2416
+ *
2417
+ * `ni` iterates the query token's children and carries that token as its
2418
+ * `parent` — the range to blame when a diagnostic has no token of its own —
2419
+ * so fork it from the annotation iterator while the query token is current.
2422
2420
  * Returns a SemanticQueryNode or undefined if the content is empty.
2423
- */ function parseQueryExpression(children, messages, sourceToken) {
2424
- if (children.length === 0) return;
2425
- const ctx = {
2426
- messages,
2427
- fallback: sourceToken.range
2428
- };
2429
- const ni = new NodeIterator(children, messages);
2430
- ni.move();
2431
- const expr = parseOrExpr(ni, ctx);
2421
+ */ function parseQueryExpression(ni, sourceToken) {
2422
+ if (!ni.$) return;
2423
+ const expr = parseOrExpr(ni);
2432
2424
  if (!expr) return;
2433
- if (ni.$) pushError(ni, ctx, `Unexpected token in query expression: "${ni.$.text}"`);
2425
+ if (ni.$) pushError(ni, `Unexpected token in query expression: "${ni.$.text}"`);
2434
2426
  const node = new SemanticQueryNode();
2435
2427
  node.expression = expr;
2436
2428
  node.sourceToken = sourceToken;
2437
2429
  return node;
2438
2430
  }
2439
- function parseOrExpr(ni, ctx) {
2440
- const left = parseAndExpr(ni, ctx);
2431
+ function parseOrExpr(ni) {
2432
+ const left = parseAndExpr(ni);
2441
2433
  if (!left) return;
2442
2434
  const operands = [left];
2443
2435
  while (ni.$?.type === "identifier" && ni.$.text === "or") {
2444
2436
  ni.move();
2445
- const right = parseAndExpr(ni, ctx);
2437
+ const right = parseAndExpr(ni);
2446
2438
  if (!right) {
2447
- pushError(ni, ctx, "Expected expression after \"or\"");
2439
+ pushError(ni, "Expected expression after \"or\"");
2448
2440
  break;
2449
2441
  }
2450
2442
  operands.push(right);
@@ -2455,15 +2447,15 @@ function parseOrExpr(ni, ctx) {
2455
2447
  node.operands = operands;
2456
2448
  return node;
2457
2449
  }
2458
- function parseAndExpr(ni, ctx) {
2459
- const left = parseUnaryExpr(ni, ctx);
2450
+ function parseAndExpr(ni) {
2451
+ const left = parseUnaryExpr(ni);
2460
2452
  if (!left) return;
2461
2453
  const operands = [left];
2462
2454
  while (ni.$?.type === "identifier" && ni.$.text === "and") {
2463
2455
  ni.move();
2464
- const right = parseUnaryExpr(ni, ctx);
2456
+ const right = parseUnaryExpr(ni);
2465
2457
  if (!right) {
2466
- pushError(ni, ctx, "Expected expression after \"and\"");
2458
+ pushError(ni, "Expected expression after \"and\"");
2467
2459
  break;
2468
2460
  }
2469
2461
  operands.push(right);
@@ -2474,12 +2466,12 @@ function parseAndExpr(ni, ctx) {
2474
2466
  node.operands = operands;
2475
2467
  return node;
2476
2468
  }
2477
- function parseUnaryExpr(ni, ctx) {
2469
+ function parseUnaryExpr(ni) {
2478
2470
  if (ni.$?.type === "identifier" && ni.$.text === "not") {
2479
2471
  ni.move();
2480
- const operand = parseUnaryExpr(ni, ctx);
2472
+ const operand = parseUnaryExpr(ni);
2481
2473
  if (!operand) {
2482
- pushError(ni, ctx, "Expected expression after \"not\"");
2474
+ pushError(ni, "Expected expression after \"not\"");
2483
2475
  return;
2484
2476
  }
2485
2477
  const node = new SemanticQueryLogicalNode();
@@ -2488,34 +2480,27 @@ function parseUnaryExpr(ni, ctx) {
2488
2480
  return node;
2489
2481
  }
2490
2482
  if (ni.$?.type === "block" && ni.$.text === "(") {
2491
- const blockToken = ni.$;
2492
- const blockChildren = blockToken.children || [];
2483
+ const subNi = ni.fork(ni.$.children || []);
2493
2484
  ni.move();
2494
- const subCtx = {
2495
- messages: ctx.messages,
2496
- fallback: rangeOf(blockToken, ctx)
2497
- };
2498
- if (blockChildren.length === 0) {
2499
- pushErrorAt(subCtx.fallback, ctx, "Empty parenthesized expression");
2485
+ if (!subNi.$) {
2486
+ pushError(subNi, "Empty parenthesized expression");
2500
2487
  return;
2501
2488
  }
2502
- const subNi = new NodeIterator(blockChildren, ctx.messages);
2503
- subNi.move();
2504
- const expr = parseOrExpr(subNi, subCtx);
2505
- if (subNi.$) pushError(subNi, subCtx, `Unexpected token in parenthesized expression: "${subNi.$.text}"`);
2489
+ const expr = parseOrExpr(subNi);
2490
+ if (subNi.$) pushError(subNi, `Unexpected token in parenthesized expression: "${subNi.$.text}"`);
2506
2491
  return expr;
2507
2492
  }
2508
- return parseComparison(ni, ctx);
2493
+ return parseComparison(ni);
2509
2494
  }
2510
- function parseComparison(ni, ctx) {
2511
- const left = parseFieldRef(ni, ctx);
2495
+ function parseComparison(ni) {
2496
+ const left = parseFieldRef(ni);
2512
2497
  if (!left) return;
2513
2498
  if (ni.$?.type === "punctuation" && SYMBOLIC_OPS.has(ni.$.text)) {
2514
2499
  const opText = ni.$.text;
2515
2500
  ni.move();
2516
- const right = parseValueOrFieldRef(ni, ctx);
2501
+ const right = parseValueOrFieldRef(ni);
2517
2502
  if (!right) {
2518
- pushError(ni, ctx, `Expected value or field reference after "${opText}"`);
2503
+ pushError(ni, `Expected value or field reference after "${opText}"`);
2519
2504
  return;
2520
2505
  }
2521
2506
  const node = new SemanticQueryComparisonNode();
@@ -2528,7 +2513,7 @@ function parseComparison(ni, ctx) {
2528
2513
  const opText = ni.$.text;
2529
2514
  if (opText === "in") {
2530
2515
  ni.move();
2531
- const right = parseValueList(ni, ctx);
2516
+ const right = parseValueList(ni);
2532
2517
  if (!right) return;
2533
2518
  const node = new SemanticQueryComparisonNode();
2534
2519
  node.left = left;
@@ -2538,9 +2523,9 @@ function parseComparison(ni, ctx) {
2538
2523
  }
2539
2524
  if (opText === "matches") {
2540
2525
  ni.move();
2541
- const right = parseValueOrFieldRef(ni, ctx);
2526
+ const right = parseValueOrFieldRef(ni);
2542
2527
  if (!right) {
2543
- pushError(ni, ctx, "Expected value after \"matches\"");
2528
+ pushError(ni, "Expected value after \"matches\"");
2544
2529
  return;
2545
2530
  }
2546
2531
  const node = new SemanticQueryComparisonNode();
@@ -2556,17 +2541,17 @@ function parseComparison(ni, ctx) {
2556
2541
  node.operator = "exists";
2557
2542
  return node;
2558
2543
  }
2559
- if (opText === "not") return parseNotComparison(ni, ctx, left);
2560
- pushError(ni, ctx, `Unknown operator "${opText}"`);
2544
+ if (opText === "not") return parseNotComparison(ni, left);
2545
+ pushError(ni, `Unknown operator "${opText}"`);
2561
2546
  return;
2562
2547
  }
2563
- pushError(ni, ctx, "Expected operator after field reference");
2548
+ pushError(ni, "Expected operator after field reference");
2564
2549
  }
2565
- function parseNotComparison(ni, ctx, left) {
2550
+ function parseNotComparison(ni, left) {
2566
2551
  ni.move();
2567
2552
  if (ni.$?.type === "identifier" && ni.$.text === "in") {
2568
2553
  ni.move();
2569
- const right = parseValueList(ni, ctx);
2554
+ const right = parseValueList(ni);
2570
2555
  if (!right) return;
2571
2556
  const node = new SemanticQueryComparisonNode();
2572
2557
  node.left = left;
@@ -2581,11 +2566,11 @@ function parseNotComparison(ni, ctx, left) {
2581
2566
  node.operator = "not exists";
2582
2567
  return node;
2583
2568
  }
2584
- pushError(ni, ctx, "Expected \"in\" or \"exists\" after \"not\"");
2569
+ pushError(ni, "Expected \"in\" or \"exists\" after \"not\"");
2585
2570
  }
2586
- function parseFieldRef(ni, ctx) {
2571
+ function parseFieldRef(ni) {
2587
2572
  if (ni.$?.type !== "identifier") {
2588
- pushError(ni, ctx, "Expected field reference");
2573
+ pushError(ni, "Expected field reference");
2589
2574
  return;
2590
2575
  }
2591
2576
  const firstToken = new Token(ni.$);
@@ -2602,7 +2587,7 @@ function parseFieldRef(ni, ctx) {
2602
2587
  fieldText += `.${ni.$.text}`;
2603
2588
  ni.move();
2604
2589
  } else {
2605
- pushError(ni, ctx, "Expected identifier after \".\"");
2590
+ pushError(ni, "Expected identifier after \".\"");
2606
2591
  break;
2607
2592
  }
2608
2593
  }
@@ -2611,7 +2596,7 @@ function parseFieldRef(ni, ctx) {
2611
2596
  node.fieldRef = fieldText === secondToken.text ? secondToken : secondToken.clone({ text: fieldText });
2612
2597
  return node;
2613
2598
  }
2614
- pushError(ni, ctx, "Expected identifier after \".\"");
2599
+ pushError(ni, "Expected identifier after \".\"");
2615
2600
  const node = new SemanticQueryFieldRefNode();
2616
2601
  node.fieldRef = firstToken;
2617
2602
  return node;
@@ -2620,18 +2605,18 @@ function parseFieldRef(ni, ctx) {
2620
2605
  node.fieldRef = firstToken;
2621
2606
  return node;
2622
2607
  }
2623
- function parseValueOrFieldRef(ni, ctx) {
2608
+ function parseValueOrFieldRef(ni) {
2624
2609
  if (!ni.$) return;
2625
- if (ni.$.type === "text" || ni.$.type === "number" || ni.$.type === "regexp") return parseValue(ni, ctx);
2610
+ if (ni.$.type === "text" || ni.$.type === "number" || ni.$.type === "regexp") return parseValue(ni);
2626
2611
  if (ni.$.type === "identifier") {
2627
- if (VALUE_KEYWORDS.has(ni.$.text)) return parseValue(ni, ctx);
2628
- return parseFieldRef(ni, ctx);
2612
+ if (VALUE_KEYWORDS.has(ni.$.text)) return parseValue(ni);
2613
+ return parseFieldRef(ni);
2629
2614
  }
2630
- pushError(ni, ctx, "Expected value or field reference");
2615
+ pushError(ni, "Expected value or field reference");
2631
2616
  }
2632
- function parseValue(ni, ctx) {
2617
+ function parseValue(ni) {
2633
2618
  if (!ni.$) {
2634
- pushError(ni, ctx, "Expected value");
2619
+ pushError(ni, "Expected value");
2635
2620
  return;
2636
2621
  }
2637
2622
  const token = ni.$;
@@ -2641,52 +2626,42 @@ function parseValue(ni, ctx) {
2641
2626
  ni.move();
2642
2627
  return node;
2643
2628
  }
2644
- pushError(ni, ctx, `Unexpected token "${token.text}" where value expected`);
2629
+ pushError(ni, `Unexpected token "${token.text}" where value expected`);
2645
2630
  }
2646
- function parseValueList(ni, ctx) {
2631
+ function parseValueList(ni) {
2647
2632
  if (ni.$?.type === "block" && ni.$.text === "(") {
2648
- const blockToken = ni.$;
2649
- const blockChildren = blockToken.children || [];
2633
+ const subNi = ni.fork(ni.$.children || []);
2650
2634
  ni.move();
2651
- const subCtx = {
2652
- messages: ctx.messages,
2653
- fallback: rangeOf(blockToken, ctx)
2654
- };
2655
- const values = [];
2656
- if (blockChildren.length === 0) {
2657
- pushErrorAt(subCtx.fallback, ctx, "Empty value list in \"in\" expression");
2635
+ if (!subNi.$) {
2636
+ pushError(subNi, "Empty value list in \"in\" expression");
2658
2637
  return;
2659
2638
  }
2660
- const subNi = new NodeIterator(blockChildren, ctx.messages);
2661
- subNi.move();
2662
- const first = parseValue(subNi, subCtx);
2639
+ const values = [];
2640
+ const first = parseValue(subNi);
2663
2641
  if (first) values.push(first);
2664
2642
  while (subNi.$?.type === "punctuation" && subNi.$.text === ",") {
2665
2643
  subNi.move();
2666
- const v = parseValue(subNi, subCtx);
2644
+ const v = parseValue(subNi);
2667
2645
  if (v) values.push(v);
2668
2646
  }
2669
- if (subNi.$) pushError(subNi, subCtx, `Unexpected token in value list: "${subNi.$.text}"`);
2647
+ if (subNi.$) pushError(subNi, `Unexpected token in value list: "${subNi.$.text}"`);
2670
2648
  const node = new SemanticQueryValueListNode();
2671
2649
  node.values = values;
2672
2650
  return node;
2673
2651
  }
2674
- pushError(ni, ctx, "Expected parenthesized value list after \"in\"");
2652
+ pushError(ni, "Expected parenthesized value list after \"in\"");
2675
2653
  }
2676
2654
  /**
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;
2655
+ * Range to blame for a diagnostic: the current token, else the enclosing
2656
+ * query token / "(...)" block the iterator was forked from.
2657
+ */ function rangeOf(ni) {
2658
+ return ni.$?.getRange?.() ?? ni.parent?.getRange?.() ?? ZERO_RANGE;
2681
2659
  }
2682
- function pushError(ni, ctx, message) {
2683
- pushErrorAt(rangeOf(ni.$, ctx), ctx, message);
2684
- }
2685
- function pushErrorAt(range, ctx, message) {
2686
- ctx.messages.push({
2660
+ function pushError(ni, message) {
2661
+ ni.messages.push({
2687
2662
  severity: 1,
2688
2663
  message,
2689
- range
2664
+ range: rangeOf(ni)
2690
2665
  });
2691
2666
  }
2692
2667
 
@@ -2809,6 +2784,10 @@ function annotations() {
2809
2784
  }
2810
2785
  while (ni.satisfies(...opts.argument)) {
2811
2786
  let argToken = new Token(ni.$);
2787
+ if (argToken.type === "query") {
2788
+ const queryNode = parseQueryExpression(ni.fork(argToken.children), argToken);
2789
+ if (queryNode) argToken.queryNode = queryNode;
2790
+ }
2812
2791
  ni.accepted();
2813
2792
  ni.move();
2814
2793
  if (argToken.type === "identifier" && ![
@@ -2827,10 +2806,6 @@ function annotations() {
2827
2806
  }
2828
2807
  if (chainText !== argToken.text) argToken = argToken.clone({ text: chainText });
2829
2808
  }
2830
- if (argToken.type === "query") {
2831
- const queryNode = parseQueryExpression(argToken.children, ni.messages, argToken);
2832
- if (queryNode) argToken.queryNode = queryNode;
2833
- }
2834
2809
  addArgument(argToken);
2835
2810
  if (ni.satisfies(opts.comma)) {
2836
2811
  ni.accepted();
@@ -5268,6 +5243,28 @@ var AtscriptRepo = class {
5268
5243
  }
5269
5244
  return atscript;
5270
5245
  }
5246
+ /**
5247
+ * Drops the cached document for `id` and detaches it from its dependencies
5248
+ * (which also removes it from each dependency's `dependants`).
5249
+ *
5250
+ * Documents are cached forever and an imported document is read from disk
5251
+ * only once, so a file that changed outside the repo (a watcher, a dev
5252
+ * server, an editor) keeps resolving to its stale parse until it is closed.
5253
+ * After closing, the next `openDocument(id)` without text re-reads the file.
5254
+ *
5255
+ * Dependants are left untouched on purpose: their next `checkDoc` re-runs
5256
+ * `checkImport` -> `openDocument(id)`, which re-reads the file and rewires
5257
+ * their `dependenciesMap` to the fresh document.
5258
+ *
5259
+ * @returns `true` when a cached document was dropped, `false` when nothing
5260
+ * was cached under `id`.
5261
+ */ closeDocument(id) {
5262
+ const cached = this.atscripts.get(id);
5263
+ if (!cached) return false;
5264
+ this.atscripts.delete(id);
5265
+ cached.then((d) => d.updateDependencies([]), () => {});
5266
+ return true;
5267
+ }
5271
5268
  async _openDocument(id, text) {
5272
5269
  const { manager } = await this.resolveConfig(id);
5273
5270
  const newId = await manager.resolve(id);
@@ -5394,6 +5391,7 @@ function _define_property(obj, key, value) {
5394
5391
  else obj[key] = value;
5395
5392
  return obj;
5396
5393
  }
5394
+ /** Upper bound on concurrent file operations — macOS defaults `ulimit -n` to 256. */ const WRITE_CONCURRENCY = 32;
5397
5395
  async function build(config) {
5398
5396
  const rootDir = config.rootDir ? config.rootDir.startsWith("/") ? config.rootDir : path$1.join(process.cwd(), config.rootDir) : process.cwd();
5399
5397
  config.rootDir = rootDir;
@@ -5449,13 +5447,14 @@ var BuildRepo = class {
5449
5447
  * Each write is awaited and atomic (temp file + rename), because a consumer
5450
5448
  * watching the output — a dev server, a type checker, another build — must
5451
5449
  * never observe a half-written file, and an un-awaited write can be
5452
- * truncated when the process exits.
5450
+ * truncated when the process exits. Outputs whose content did not change
5451
+ * are not rewritten, so those watchers see no event for them.
5453
5452
  */ async write(config, docs = this.docs) {
5454
5453
  const outFiles = await this.generate(config, docs);
5455
- for (const o of outFiles) if (o.target) {
5456
- await mkdir(path$1.dirname(o.target), { recursive: true });
5457
- await writeFileAtomic(o.target, o.content);
5458
- }
5454
+ const targeted = outFiles.filter((o) => o.target);
5455
+ const dirs = new Set(targeted.map((o) => path$1.dirname(o.target)));
5456
+ await Promise.all(Array.from(dirs, (dir) => mkdir(dir, { recursive: true })));
5457
+ for (let i = 0; i < targeted.length; i += WRITE_CONCURRENCY) await Promise.all(targeted.slice(i, i + WRITE_CONCURRENCY).map((o) => writeIfChanged(o.target, o.content)));
5459
5458
  return outFiles;
5460
5459
  }
5461
5460
  constructor(rootDir, repo, docs) {
@@ -5467,6 +5466,12 @@ var BuildRepo = class {
5467
5466
  this.docs = docs;
5468
5467
  }
5469
5468
  };
5469
+ /**
5470
+ * Writes `content` to `target` unless the file already holds exactly that
5471
+ * content, so an unchanged output produces no filesystem event.
5472
+ */ async function writeIfChanged(target, content) {
5473
+ if (await readFile$1(target, "utf8").catch(() => void 0) !== content) await writeFileAtomic(target, content);
5474
+ }
5470
5475
  let tmpSeq = 0;
5471
5476
  /**
5472
5477
  * Writes `content` to a sibling temp file and renames it over `target`, so the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/core",
3
- "version": "0.1.90",
3
+ "version": "0.1.92",
4
4
  "description": "Core library for Atscript parsing and file generation.",
5
5
  "keywords": [
6
6
  "annotations",