podlite 0.0.90 → 0.0.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.
Files changed (50) hide show
  1. package/CHANGELOG.podlite +24 -0
  2. package/esm/assemble/index.d.ts +2 -0
  3. package/esm/assemble/index.js +404 -57
  4. package/esm/assemble/index.js.map +1 -1
  5. package/esm/cli.js +5 -1
  6. package/esm/cli.js.map +1 -1
  7. package/esm/lint/grammar/lint.js +1181 -183
  8. package/esm/lint/grammar/lint.js.map +1 -1
  9. package/esm/lint/rules/include-resolves.js +30 -17
  10. package/esm/lint/rules/include-resolves.js.map +1 -1
  11. package/esm/lint/rules/index.js +6 -0
  12. package/esm/lint/rules/index.js.map +1 -1
  13. package/esm/lint/rules/item-hierarchy.d.ts +3 -0
  14. package/esm/lint/rules/item-hierarchy.js +65 -0
  15. package/esm/lint/rules/item-hierarchy.js.map +1 -0
  16. package/esm/lint/rules/markup-code-too-deep.d.ts +3 -0
  17. package/esm/lint/rules/markup-code-too-deep.js +16 -0
  18. package/esm/lint/rules/markup-code-too-deep.js.map +1 -0
  19. package/esm/lint/rules/table-column-width.js +14 -6
  20. package/esm/lint/rules/table-column-width.js.map +1 -1
  21. package/esm/lint/rules/test-under-rule.d.ts +3 -0
  22. package/esm/lint/rules/test-under-rule.js +135 -0
  23. package/esm/lint/rules/test-under-rule.js.map +1 -0
  24. package/esm/version.d.ts +1 -1
  25. package/esm/version.js +1 -1
  26. package/lib/assemble/index.d.ts +2 -0
  27. package/lib/assemble/index.js +406 -58
  28. package/lib/assemble/index.js.map +1 -1
  29. package/lib/cli.js +5 -1
  30. package/lib/cli.js.map +1 -1
  31. package/lib/lint/grammar/lint.js +1181 -183
  32. package/lib/lint/grammar/lint.js.map +1 -1
  33. package/lib/lint/rules/include-resolves.js +29 -16
  34. package/lib/lint/rules/include-resolves.js.map +1 -1
  35. package/lib/lint/rules/index.js +6 -0
  36. package/lib/lint/rules/index.js.map +1 -1
  37. package/lib/lint/rules/item-hierarchy.d.ts +3 -0
  38. package/lib/lint/rules/item-hierarchy.js +68 -0
  39. package/lib/lint/rules/item-hierarchy.js.map +1 -0
  40. package/lib/lint/rules/markup-code-too-deep.d.ts +3 -0
  41. package/lib/lint/rules/markup-code-too-deep.js +19 -0
  42. package/lib/lint/rules/markup-code-too-deep.js.map +1 -0
  43. package/lib/lint/rules/table-column-width.js +14 -6
  44. package/lib/lint/rules/table-column-width.js.map +1 -1
  45. package/lib/lint/rules/test-under-rule.d.ts +3 -0
  46. package/lib/lint/rules/test-under-rule.js +138 -0
  47. package/lib/lint/rules/test-under-rule.js.map +1 -0
  48. package/lib/version.d.ts +1 -1
  49. package/lib/version.js +1 -1
  50. package/package.json +8 -8
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createSourceStore = exports.assembleAsync = exports.sourcesFromFiles = exports.assembleIncludes = exports.silently = exports.externalMessage = exports.isWarning = void 0;
3
+ exports.createSourceStore = exports.assembleAsync = exports.sourcesFromFiles = exports.assembleIncludes = exports.hasSameDocumentInclude = exports.silently = exports.externalMessage = exports.isWarning = void 0;
4
4
  const schema_1 = require("@podlite/schema");
5
5
  const toc_1 = require("@podlite/toc");
6
6
  const file_format_1 = require("../file-format");
@@ -30,10 +30,41 @@ const readWhole = (tail) => {
30
30
  // colon before the selection, and ends at the last bar before which it reads
31
31
  // whole, or at the end of the line. A configuration that does not
32
32
  // read whole leaves the line unread.
33
+ // whether a bar stands outside the brackets of a predicate: a bar inside one
34
+ // belongs to an operand, not to the selector
35
+ const hasTopLevelBar = (line) => {
36
+ let depth = 0;
37
+ let angle = 0;
38
+ let quote = '';
39
+ for (const c of line) {
40
+ // a value in quotes or angles is read whole, whatever brackets it holds
41
+ if (quote) {
42
+ if (c === quote)
43
+ quote = '';
44
+ }
45
+ else if (angle) {
46
+ if (c === '<' || c === '«')
47
+ angle++;
48
+ else if (c === '>' || c === '»')
49
+ angle--;
50
+ }
51
+ else if (c === "'" || c === '"')
52
+ quote = c;
53
+ else if (c === '<' || c === '«')
54
+ angle++;
55
+ else if (c === '[' || c === '(')
56
+ depth++;
57
+ else if ((c === ']' || c === ')') && depth > 0)
58
+ depth--;
59
+ else if (c === '|' && depth === 0)
60
+ return true;
61
+ }
62
+ return false;
63
+ };
33
64
  const splitDirective = (line) => {
34
65
  // without the address of a source there is no place for a configuration: a
35
66
  // source is written with its scheme, or without one when a selection follows
36
- if (!/^[a-z][\w+.-]*:/i.test(line) && !line.includes('|'))
67
+ if (!/^[a-z][\w+.-]*:/i.test(line) && !hasTopLevelBar(line))
37
68
  return { selector: line };
38
69
  let start = -1;
39
70
  for (let i = 0; i < line.length; i++) {
@@ -117,6 +148,50 @@ const silently = (work) => {
117
148
  };
118
149
  exports.silently = silently;
119
150
  const isIncludeBlock = (node) => node && typeof node === 'object' && node.type === 'block' && node.name === 'include';
151
+ /*
152
+ =begin pod :kind<export>
153
+
154
+ =head2 hasSameDocumentInclude
155
+
156
+ Whether a tree holds an C<=include> with no source, one that selects from the
157
+ document itself. Such an include needs no file to resolve, so a host that has no
158
+ files to give can still assemble the tree with C<onlySameDocument>.
159
+
160
+ =end pod
161
+ */
162
+ const hasSameDocumentInclude = (tree) => {
163
+ const work = [tree];
164
+ while (work.length) {
165
+ const node = work.pop();
166
+ if (!node || typeof node !== 'object')
167
+ continue;
168
+ if (Array.isArray(node)) {
169
+ for (const item of node)
170
+ work.push(item);
171
+ continue;
172
+ }
173
+ if (isIncludeBlock(node)) {
174
+ const line = (0, schema_1.getTextContentFromNode)(node.content)?.toString().trim() ?? '';
175
+ const { selector, unread } = splitDirective(line);
176
+ if (!selector || unread)
177
+ continue;
178
+ try {
179
+ const parsed = (0, schema_1.readSelector)(selector);
180
+ if (!parsed.scheme && !parsed.document && !parsed.anchor && parsed.patterns.length > 0)
181
+ return true;
182
+ }
183
+ catch (e) {
184
+ if (!(e instanceof schema_1.SelectorError))
185
+ throw e;
186
+ }
187
+ continue;
188
+ }
189
+ if (node.content)
190
+ work.push(node.content);
191
+ }
192
+ return false;
193
+ };
194
+ exports.hasSameDocumentInclude = hasSameDocumentInclude;
120
195
  const keepBlocks = (items) => items.filter(item => item && typeof item === 'object' && !('file' in item));
121
196
  // A parser may hand back a list of nodes rather than a document; a selection
122
197
  // needs one node to walk, and it must hold the parsed nodes themselves.
@@ -185,6 +260,8 @@ const assembleIncludes = (tree, opts) => {
185
260
  opts.onError(problem);
186
261
  };
187
262
  const held = [];
263
+ // the slot of the include with no source the second walk is resolving, outermost first
264
+ const slots = [];
188
265
  // the problem an error thrown with no handler was made from
189
266
  const thrownFrom = new WeakMap();
190
267
  // For each include being resolved, whether a block of its content was met
@@ -211,13 +288,18 @@ const assembleIncludes = (tree, opts) => {
211
288
  throw error;
212
289
  }
213
290
  }
214
- held.push(...entries);
291
+ const slot = slots[0];
292
+ const at = slot ? held.indexOf(slot) : -1;
293
+ if (at >= 0)
294
+ held.splice(at, 0, ...entries);
295
+ else
296
+ held.push(...entries);
215
297
  };
216
298
  const report = (problem) => hold([{ problem, shown: true }]);
217
299
  const flush = () => {
218
300
  held
219
301
  .splice(0)
220
- .filter(entry => entry.shown)
302
+ .filter(entry => entry.shown && !entry.slot)
221
303
  .forEach(entry => emit(entry.problem));
222
304
  };
223
305
  // how many includes brought their content so far
@@ -234,6 +316,34 @@ const assembleIncludes = (tree, opts) => {
234
316
  // copy of it, wherever it is made, waits as well. The mark is this call's own:
235
317
  // a later pass does not read it.
236
318
  const WAITING = Symbol('waiting include');
319
+ // An include with no source selects from the document in one state: its includes
320
+ // of other sources in, its own includes with no source as written (Specification,
321
+ // Include directive). The first walk leaves each such include in place with this
322
+ // mark, which carries the place it stands in; when the walk is over, a second walk
323
+ // over its result resolves the marked includes from that result, which they are not
324
+ // in, so none of them sees what another one brought. Its copies are walked by that
325
+ // second walk too, and a block is not placed inside itself: the blocks the walk is
326
+ // inside, read back to the blocks of that result they are copies of, are the path a
327
+ // block found must not be on.
328
+ const PLACEMENT = Symbol('include with no source');
329
+ let placements = 0;
330
+ let secondWalk = false;
331
+ // the result of the first walk, which the marked includes select from
332
+ let state;
333
+ // something in the first walk waited for a source: the state is not known in full
334
+ let stateWaits = false;
335
+ // the block of the state a block of the second walk is, or is a copy of
336
+ const placedFrom = new WeakMap();
337
+ const inside = [];
338
+ // In the second walk an include with no source inside a wrapper may bring nothing:
339
+ // the assignments it held go on past the wrapper, to the next block of the list
340
+ // that holds it, as they would have had it been resolved in the first walk. They
341
+ // pass only out of the wrappers the tree adds (root, _folded_section), never out of
342
+ // a written block, and not out of the root of the document, where they are reported.
343
+ let escapeNext = false;
344
+ let escaped;
345
+ // a cycle of an include with no source is reported whether or not it loses =set
346
+ const alwaysShown = new WeakSet();
237
347
  const FAILED = 'include-failed';
238
348
  const isMark = (node) => node && typeof node === 'object' && node.type === FAILED;
239
349
  const firstStop = (nodes, holder) => {
@@ -367,15 +477,33 @@ const assembleIncludes = (tree, opts) => {
367
477
  const out = [];
368
478
  let pending = [];
369
479
  let last;
480
+ const mayEscape = escapeNext;
481
+ escapeNext = false;
482
+ // what an include with no source says in the second walk goes to its slot
370
483
  const visit = (n) => {
484
+ const slot = secondWalk && isIncludeBlock(n) && !slots.length ? n[PLACEMENT]?.slot : undefined;
485
+ if (slot)
486
+ slots.push(slot);
487
+ try {
488
+ visitEntry(n);
489
+ }
490
+ finally {
491
+ if (slot)
492
+ slots.pop();
493
+ }
494
+ };
495
+ const visitEntry = (n) => {
371
496
  if (n && n.type === 'config' && typeof n.name === 'string' && Array.isArray(n.config)) {
372
497
  config[n.name] = (0, schema_1.mergeConfigSettings)(n.config, config[n.name]);
373
498
  }
374
- if (isIncludeBlock(n)) {
499
+ if (isIncludeBlock(n) && (!secondWalk || n[PLACEMENT])) {
375
500
  // assignments carried from an earlier include are older than its own
376
501
  const set = (0, schema_1.mergeSet)(pending, n.set);
377
502
  pending = [];
378
- const here = [...chain, { file, location: n.location }];
503
+ // an include with no source names the place it was written in
504
+ const placed = secondWalk ? n[PLACEMENT] : undefined;
505
+ const here = placed ? placed.here : [...chain, { file, location: n.location }];
506
+ const where = placed ? placed.file : file;
379
507
  let resolved;
380
508
  const at = reached.length;
381
509
  try {
@@ -400,6 +528,32 @@ const assembleIncludes = (tree, opts) => {
400
528
  finally {
401
529
  reached.length = at;
402
530
  }
531
+ if (resolved.kept) {
532
+ // as written, with the assignments it would have taken
533
+ const kept = set.length ? { ...n, set } : n;
534
+ if (kept !== n) {
535
+ const known = origin.get(n);
536
+ if (known)
537
+ origin.set(kept, known);
538
+ opts.onCopy?.(n, kept);
539
+ }
540
+ out.push(kept);
541
+ return;
542
+ }
543
+ if (resolved.placement) {
544
+ // left for the second walk with its assignments, as the place it stands in
545
+ placements++;
546
+ const slot = { problem: undefined, shown: false, slot: true };
547
+ if (!quiet)
548
+ held.push(slot);
549
+ const kept = { ...n, ...(set.length ? { set } : {}), [PLACEMENT]: { ...resolved.placement, slot } };
550
+ const known = origin.get(n);
551
+ if (known)
552
+ origin.set(kept, known);
553
+ opts.onCopy?.(n, kept);
554
+ out.push(kept);
555
+ return;
556
+ }
403
557
  if (resolved.waiting) {
404
558
  const kept = { ...n, ...(set.length ? { set } : {}), [WAITING]: true };
405
559
  const known = origin.get(n);
@@ -413,16 +567,24 @@ const assembleIncludes = (tree, opts) => {
413
567
  if (!failure)
414
568
  brought++;
415
569
  if (!failure)
416
- markVia(nodes, `${file}@${n.location?.start?.offset ?? ''}`, { file, location: n.location });
570
+ markVia(nodes, `${where}@${n.location?.start?.offset ?? ''}`, { file: where, location: n.location });
571
+ // a copy an include with no source placed came first through the way that include came
572
+ if (!failure && placed) {
573
+ const way = origin.get(n);
574
+ if (way?.via)
575
+ prefixVia(nodes, way);
576
+ }
417
577
  if (!failure && resolved.outside.length) {
418
- const step = stepOf(file, n.location);
578
+ const step = stepOf(where, n.location);
419
579
  const problem = { kind: 'external', target: selector, message: '', chain: here };
420
580
  hold([{ problem, shown: false, outside: { step, written: resolved.written, sources: resolved.outside } }]);
421
581
  }
422
582
  if (failure) {
423
583
  const entries = failure.map(problem => ({
424
584
  problem,
425
- shown: problem.kind !== 'cycle' || (opts.reportCycles === true && !maskCycles.has(problem)),
585
+ shown: problem.kind !== 'cycle' ||
586
+ alwaysShown.has(problem) ||
587
+ (opts.reportCycles === true && !maskCycles.has(problem)),
426
588
  }));
427
589
  if (set.length)
428
590
  lose(entries, set);
@@ -444,7 +606,7 @@ const assembleIncludes = (tree, opts) => {
444
606
  out.push(...nodes);
445
607
  return;
446
608
  }
447
- const applied = (0, schema_1.applySetToFirst)(nodes, set, { mode: 'include', origin, onCopy: opts.onCopy, holder });
609
+ const applied = (0, schema_1.applySetToFirst)(nodes, set, { mode: 'include', origin, onCopy: copied, holder });
448
610
  out.push(...applied.nodes);
449
611
  if (applied.outcome !== 'none')
450
612
  return;
@@ -459,37 +621,50 @@ const assembleIncludes = (tree, opts) => {
459
621
  return;
460
622
  }
461
623
  const walked = walkNode(n, context, stack, chain, file, home, config, holder);
624
+ const leaving = escaped;
625
+ escaped = undefined;
462
626
  const items = Array.isArray(walked) ? walked : [walked];
627
+ const goOn = () => {
628
+ if (!leaving)
629
+ return;
630
+ pending = (0, schema_1.mergeSet)(pending, leaving.pending);
631
+ last = leaving.last ?? last;
632
+ };
463
633
  if (!pending.length) {
464
634
  out.push(...items);
465
- return;
635
+ return goOn();
466
636
  }
467
637
  const stopped = firstStop(items, holder);
468
638
  if (stopped) {
469
639
  lose(stopped, pending);
470
640
  pending = [];
471
641
  out.push(...items);
472
- return;
642
+ return goOn();
473
643
  }
474
- const applied = (0, schema_1.applySetToFirst)(items, pending, { mode: 'carry', origin, onCopy: opts.onCopy, holder });
644
+ const applied = (0, schema_1.applySetToFirst)(items, pending, { mode: 'carry', origin, onCopy: copied, holder });
475
645
  if (applied.outcome !== 'none')
476
646
  pending = [];
477
647
  out.push(...applied.nodes);
648
+ goOn();
478
649
  };
479
650
  level++;
480
651
  try {
481
652
  for (const n of list) {
482
653
  const from = out.length;
483
654
  visit(n);
484
- if (level === 1) {
655
+ // the problems wait for the end of the call: the second walk may yet name
656
+ // assignments a failure of the first one lost
657
+ if (level === 1)
485
658
  confirmOutside(out.slice(from));
486
- flush();
487
- }
488
659
  }
489
660
  }
490
661
  finally {
491
662
  level--;
492
663
  }
664
+ if (pending.length && mayEscape) {
665
+ escaped = { pending, last };
666
+ return out;
667
+ }
493
668
  if (pending.length && last) {
494
669
  report({
495
670
  kind: 'set-target',
@@ -518,6 +693,22 @@ const assembleIncludes = (tree, opts) => {
518
693
  };
519
694
  visit(nodes);
520
695
  };
696
+ // The way a placement came, put before the way of each node it brought.
697
+ const prefixVia = (nodes, way) => {
698
+ const visit = (node) => {
699
+ if (!node || typeof node !== 'object')
700
+ return;
701
+ if (Array.isArray(node))
702
+ return node.forEach(visit);
703
+ const known = origin.get(node);
704
+ if (known) {
705
+ const via = known.via ? `${way.via}>${known.via}` : way.via;
706
+ origin.set(node, { ...known, via, steps: [...(way.steps ?? []), ...(known.steps ?? [])] });
707
+ }
708
+ visit(node.content);
709
+ };
710
+ visit(nodes);
711
+ };
521
712
  // Which directive of which entry: the file it is written in, as the walk names
522
713
  // it, and its offset there.
523
714
  const stepOf = (file, location) => `${file}\u0000${location?.start?.offset ?? ''}`;
@@ -612,6 +803,57 @@ const assembleIncludes = (tree, opts) => {
612
803
  copy[key] = key === 'location' ? node[key] : copyDeep(node[key]);
613
804
  if (node[WAITING])
614
805
  copy[WAITING] = true;
806
+ if (node[PLACEMENT])
807
+ copy[PLACEMENT] = node[PLACEMENT];
808
+ const known = origin.get(node);
809
+ if (known)
810
+ origin.set(copy, known);
811
+ opts.onCopy?.(node, copy);
812
+ return copy;
813
+ };
814
+ // What a copy made inside this call keeps of the node it copies: where an include
815
+ // failed, a reading on its own, the block of the state it stands for, the marks.
816
+ const carry = (from, to) => {
817
+ const failed = failedAt.get(from);
818
+ if (failed)
819
+ failedAt.set(to, failed);
820
+ const alone = readAlone.get(from);
821
+ if (alone)
822
+ readAlone.set(to, alone);
823
+ const placed = placedFrom.get(from);
824
+ if (placed)
825
+ placedFrom.set(to, placed);
826
+ if (from[WAITING])
827
+ to[WAITING] = true;
828
+ if (from[PLACEMENT])
829
+ to[PLACEMENT] = from[PLACEMENT];
830
+ };
831
+ const copied = (from, to) => {
832
+ carry(from, to);
833
+ opts.onCopy?.(from, to);
834
+ };
835
+ // A block found in the state, copied for one place: each node remembers the node
836
+ // of the state it copies, and a failure, a reading on its own and the marks it
837
+ // carries come with it, so a copy is read as the block it copies.
838
+ const copyPlaced = (node) => {
839
+ if (Array.isArray(node))
840
+ return node.map(copyPlaced);
841
+ if (!node || typeof node !== 'object')
842
+ return node;
843
+ const copy = {};
844
+ for (const key of Object.keys(node))
845
+ copy[key] = key === 'location' ? node[key] : copyPlaced(node[key]);
846
+ if (node[WAITING])
847
+ copy[WAITING] = true;
848
+ if (node[PLACEMENT])
849
+ copy[PLACEMENT] = node[PLACEMENT];
850
+ placedFrom.set(copy, placedFrom.get(node) ?? node);
851
+ const failed = failedAt.get(node);
852
+ if (failed)
853
+ failedAt.set(copy, failed);
854
+ const alone = readAlone.get(node);
855
+ if (alone)
856
+ readAlone.set(copy, alone);
615
857
  const known = origin.get(node);
616
858
  if (known)
617
859
  origin.set(copy, known);
@@ -699,6 +941,106 @@ const assembleIncludes = (tree, opts) => {
699
941
  failures.length = failed;
700
942
  }
701
943
  };
944
+ // A file an operand names is read the way an included file is, from the directory
945
+ // of the directive; one already on the way does not resolve.
946
+ const operandReader = (context, stack, here) => {
947
+ let waits = false;
948
+ const read = (scheme, document) => {
949
+ if (!schemes.includes(scheme))
950
+ return undefined;
951
+ const found = provider.locate(document, context, true, undefined, scheme);
952
+ const refused = found && refusal(found);
953
+ if (refused !== undefined) {
954
+ throw new schema_1.SelectorError('resolution', `the source does not resolve: ${scheme}:${document}: ${refused}`);
955
+ }
956
+ const source = found?.sources[0];
957
+ const target = source?.id ?? '';
958
+ const text = !source || stack.includes(target) ? null : textOf(source);
959
+ if (!found || text === undefined)
960
+ waits = true;
961
+ if (!source || text === null || text === undefined)
962
+ return undefined;
963
+ let own;
964
+ try {
965
+ own = opts.parse(text, target, undefined, { format: source.format ?? (0, file_format_1.formatOfFile)(target) });
966
+ }
967
+ catch (e) {
968
+ // a file that fails to parse is one the operand cannot be read from
969
+ if (!opts.tolerant)
970
+ throw e;
971
+ return undefined;
972
+ }
973
+ recordOrigin(own, { file: target, text }, origin);
974
+ // the file is a document of its own: it is walked as the first walk walks
975
+ const second = secondWalk;
976
+ secondWalk = false;
977
+ try {
978
+ return [
979
+ {
980
+ file: document,
981
+ node: asDocument(walkNode(own, source.context, [...stack, target], here, target, own, {})),
982
+ },
983
+ ];
984
+ }
985
+ finally {
986
+ secondWalk = second;
987
+ }
988
+ };
989
+ return { read, waits: () => waits };
990
+ };
991
+ // An include with no source, in the second walk: its blocks are found in the state,
992
+ // copied, and walked where it stands, unless one of them holds it.
993
+ const resolvePlacement = (node, selector, stack, config) => {
994
+ const at = node[PLACEMENT];
995
+ const mark = failures.length;
996
+ const done = (nodes, failure) => ({
997
+ nodes,
998
+ failure,
999
+ inner: failures.slice(mark),
1000
+ selector,
1001
+ outside: [],
1002
+ written: selector,
1003
+ });
1004
+ if (stateWaits)
1005
+ return { ...done([node]), waiting: true };
1006
+ const operands = operandReader(at.context, stack, at.here);
1007
+ const doc = { file: mainFile, node: asDocument(state) };
1008
+ let found;
1009
+ try {
1010
+ found = (0, schema_1.outermost)(keepBlocks((0, schema_1.runSelector)(selector, [doc], { home: [doc], readSource: operands.read })));
1011
+ }
1012
+ catch (e) {
1013
+ if (!(e instanceof schema_1.SelectorError))
1014
+ throw e;
1015
+ if (operands.waits())
1016
+ return { ...done([node]), waiting: true };
1017
+ return done([], [
1018
+ {
1019
+ kind: 'operand',
1020
+ target: selector,
1021
+ message: `include selector cannot be read: ${e.message}`,
1022
+ chain: at.here,
1023
+ },
1024
+ ]);
1025
+ }
1026
+ if (operands.waits())
1027
+ return { ...done([node]), waiting: true };
1028
+ if (found.some(block => inside.includes(block))) {
1029
+ const problem = {
1030
+ kind: 'cycle',
1031
+ target: selector,
1032
+ message: `include brings nothing: a block it would bring holds it: ${selector}`,
1033
+ chain: at.here,
1034
+ };
1035
+ alwaysShown.add(problem);
1036
+ return done([], [problem]);
1037
+ }
1038
+ const nodes = found.flatMap(block => {
1039
+ const walked = walkNode(copyPlaced(block), at.context, stack, at.here, at.file, state, { ...config });
1040
+ return Array.isArray(walked) ? walked : [walked];
1041
+ });
1042
+ return done(nodes);
1043
+ };
702
1044
  // What an include comes to: the nodes that take its place, or the problems
703
1045
  // that left it with nothing, not yet reported
704
1046
  const resolveInclude = (node, context, stack, here, file, home, config) => {
@@ -737,6 +1079,15 @@ const assembleIncludes = (tree, opts) => {
737
1079
  // what it would have brought in is missing, and a reader of the tree cannot
738
1080
  // tell that on its own.
739
1081
  const fail = (problem, keep = false) => done(keep ? [withoutSet(node)] : [], [problem]);
1082
+ // a selection with patterns and no source selects from the document itself
1083
+ const sameDocument = !!selector && !!parsed && !parsed.scheme && !parsed.document && !parsed.anchor && parsed.patterns.length > 0;
1084
+ if (sameDocument && !secondWalk)
1085
+ return { ...done([node]), placement: { file, here, context } };
1086
+ if (sameDocument)
1087
+ return resolvePlacement(node, selector, stack, config);
1088
+ // a host with no files to give leaves the other includes as they are written
1089
+ if (opts.onlySameDocument)
1090
+ return { ...done([node]), kept: true };
740
1091
  if (!selector || !parsed || !parsed.scheme || !parsed.document) {
741
1092
  return fail({
742
1093
  kind: 'unparsed-selector',
@@ -764,7 +1115,11 @@ const assembleIncludes = (tree, opts) => {
764
1115
  chain: here,
765
1116
  }, true);
766
1117
  }
767
- const wait = () => ({ ...done([node]), roots: undefined, waiting: true });
1118
+ const wait = () => {
1119
+ if (!secondWalk)
1120
+ stateWaits = true;
1121
+ return { ...done([node]), roots: undefined, waiting: true };
1122
+ };
768
1123
  const located = provider.locate(parsed.document, context, false, here[here.length - 1], parsed.scheme);
769
1124
  if (!located)
770
1125
  return wait();
@@ -968,42 +1323,8 @@ const assembleIncludes = (tree, opts) => {
968
1323
  });
969
1324
  }
970
1325
  roots = placedDocs;
971
- // a file an operand names is read the way an included file is, from the
972
- // directory of the directive; one already on the way does not resolve
973
- let operandWaits = false;
974
- const readSource = (scheme, document) => {
975
- if (!schemes.includes(scheme))
976
- return undefined;
977
- const found = provider.locate(document, context, true, undefined, scheme);
978
- const refused = found && refusal(found);
979
- if (refused !== undefined) {
980
- throw new schema_1.SelectorError('resolution', `the source does not resolve: ${scheme}:${document}: ${refused}`);
981
- }
982
- const source = found?.sources[0];
983
- const target = source?.id ?? '';
984
- const text = !source || stack.includes(target) ? null : textOf(source);
985
- if (!found || text === undefined)
986
- operandWaits = true;
987
- if (!source || text === null || text === undefined)
988
- return undefined;
989
- let own;
990
- try {
991
- own = opts.parse(text, target, undefined, { format: source.format ?? (0, file_format_1.formatOfFile)(target) });
992
- }
993
- catch (e) {
994
- // a file that fails to parse is one the operand cannot be read from
995
- if (!opts.tolerant)
996
- throw e;
997
- return undefined;
998
- }
999
- recordOrigin(own, { file: target, text }, origin);
1000
- return [
1001
- {
1002
- file: document,
1003
- node: asDocument(walkNode(own, source.context, [...stack, target], here, target, own, {})),
1004
- },
1005
- ];
1006
- };
1326
+ const operands = operandReader(context, stack, here);
1327
+ const readSource = operands.read;
1007
1328
  // An include that waits for a later pass stands for blocks not yet known. The
1008
1329
  // universal pattern without a predicate would find every one of them, so the
1009
1330
  // include stays where it is written, among the found blocks; under other
@@ -1045,14 +1366,14 @@ const assembleIncludes = (tree, opts) => {
1045
1366
  };
1046
1367
  try {
1047
1368
  const found = (0, schema_1.outermost)(keepBlocks((0, schema_1.runSelector)(selector, docs, { home: [{ file, node: asDocument(home) }], readSource })));
1048
- if (operandWaits)
1369
+ if (operands.waits())
1049
1370
  return wait();
1050
1371
  return done(withoutLooseDirectives(placedFor(withWaiting(found))));
1051
1372
  }
1052
1373
  catch (e) {
1053
1374
  if (!(e instanceof schema_1.SelectorError))
1054
1375
  throw e;
1055
- if (operandWaits)
1376
+ if (operands.waits())
1056
1377
  return wait();
1057
1378
  return fail({
1058
1379
  kind: 'operand',
@@ -1067,8 +1388,12 @@ const assembleIncludes = (tree, opts) => {
1067
1388
  return node;
1068
1389
  if (Array.isArray(node))
1069
1390
  return walkList(node, context, stack, chain, file, home, config, holder);
1070
- if (isIncludeBlock(node))
1391
+ // the second walk resolves only the includes with no source; the others stay
1392
+ if (isIncludeBlock(node)) {
1393
+ if (secondWalk && !node[PLACEMENT])
1394
+ return node;
1071
1395
  return walkList([node], context, stack, chain, file, home, config, holder);
1396
+ }
1072
1397
  const wrapper = node.type === 'block' && (node.name === 'root' || node.name === '_folded_section');
1073
1398
  if ((0, schema_1.isSetTarget)(node, holder))
1074
1399
  markReached();
@@ -1076,17 +1401,40 @@ const assembleIncludes = (tree, opts) => {
1076
1401
  // a block is a lexical scope for the settings declared inside it
1077
1402
  const scope = node.type === 'block' && !wrapper ? { ...config } : config;
1078
1403
  const inner = (0, schema_1.holderInside)(node, holder);
1079
- const copy = { ...node, content: walkList(node.content, context, stack, chain, file, home, scope, inner) };
1404
+ // a block the second walk is inside, as the block of the state it stands for
1405
+ const counted = secondWalk && node.type === 'block' && !wrapper;
1406
+ if (counted)
1407
+ inside.push(placedFrom.get(node) ?? node);
1408
+ let content;
1409
+ try {
1410
+ // the root of the document has no list around it to pass assignments on to
1411
+ escapeNext = secondWalk && wrapper && node !== state;
1412
+ content = walkList(node.content, context, stack, chain, file, home, scope, inner);
1413
+ }
1414
+ finally {
1415
+ if (counted)
1416
+ inside.pop();
1417
+ }
1418
+ const copy = { ...node, content };
1080
1419
  const known = origin.get(node);
1081
1420
  if (known)
1082
1421
  origin.set(copy, known);
1422
+ const from = placedFrom.get(node);
1423
+ if (from)
1424
+ placedFrom.set(copy, from);
1083
1425
  opts.onCopy?.(node, copy);
1084
1426
  return copy;
1085
1427
  }
1086
1428
  return node;
1087
1429
  };
1088
1430
  try {
1089
- return unmark(walkNode(tree, opts.context, opts.self ? [opts.self] : [], [], mainFile, tree, {}));
1431
+ const self = opts.self ? [opts.self] : [];
1432
+ const first = walkNode(tree, opts.context, self, [], mainFile, tree, {});
1433
+ if (!placements)
1434
+ return unmark(first);
1435
+ state = first;
1436
+ secondWalk = true;
1437
+ return unmark(walkNode(first, opts.context, self, [], mainFile, tree, {}));
1090
1438
  }
1091
1439
  finally {
1092
1440
  flush();