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
@@ -26,10 +26,41 @@ const readWhole = (tail) => {
26
26
  // colon before the selection, and ends at the last bar before which it reads
27
27
  // whole, or at the end of the line. A configuration that does not
28
28
  // read whole leaves the line unread.
29
+ // whether a bar stands outside the brackets of a predicate: a bar inside one
30
+ // belongs to an operand, not to the selector
31
+ const hasTopLevelBar = (line) => {
32
+ let depth = 0;
33
+ let angle = 0;
34
+ let quote = '';
35
+ for (const c of line) {
36
+ // a value in quotes or angles is read whole, whatever brackets it holds
37
+ if (quote) {
38
+ if (c === quote)
39
+ quote = '';
40
+ }
41
+ else if (angle) {
42
+ if (c === '<' || c === '«')
43
+ angle++;
44
+ else if (c === '>' || c === '»')
45
+ angle--;
46
+ }
47
+ else if (c === "'" || c === '"')
48
+ quote = c;
49
+ else if (c === '<' || c === '«')
50
+ angle++;
51
+ else if (c === '[' || c === '(')
52
+ depth++;
53
+ else if ((c === ']' || c === ')') && depth > 0)
54
+ depth--;
55
+ else if (c === '|' && depth === 0)
56
+ return true;
57
+ }
58
+ return false;
59
+ };
29
60
  const splitDirective = (line) => {
30
61
  // without the address of a source there is no place for a configuration: a
31
62
  // source is written with its scheme, or without one when a selection follows
32
- if (!/^[a-z][\w+.-]*:/i.test(line) && !line.includes('|'))
63
+ if (!/^[a-z][\w+.-]*:/i.test(line) && !hasTopLevelBar(line))
33
64
  return { selector: line };
34
65
  let start = -1;
35
66
  for (let i = 0; i < line.length; i++) {
@@ -111,6 +142,49 @@ export const silently = (work) => {
111
142
  }
112
143
  };
113
144
  const isIncludeBlock = (node) => node && typeof node === 'object' && node.type === 'block' && node.name === 'include';
145
+ /*
146
+ =begin pod :kind<export>
147
+
148
+ =head2 hasSameDocumentInclude
149
+
150
+ Whether a tree holds an C<=include> with no source, one that selects from the
151
+ document itself. Such an include needs no file to resolve, so a host that has no
152
+ files to give can still assemble the tree with C<onlySameDocument>.
153
+
154
+ =end pod
155
+ */
156
+ export const hasSameDocumentInclude = (tree) => {
157
+ const work = [tree];
158
+ while (work.length) {
159
+ const node = work.pop();
160
+ if (!node || typeof node !== 'object')
161
+ continue;
162
+ if (Array.isArray(node)) {
163
+ for (const item of node)
164
+ work.push(item);
165
+ continue;
166
+ }
167
+ if (isIncludeBlock(node)) {
168
+ const line = getTextContentFromNode(node.content)?.toString().trim() ?? '';
169
+ const { selector, unread } = splitDirective(line);
170
+ if (!selector || unread)
171
+ continue;
172
+ try {
173
+ const parsed = readSelector(selector);
174
+ if (!parsed.scheme && !parsed.document && !parsed.anchor && parsed.patterns.length > 0)
175
+ return true;
176
+ }
177
+ catch (e) {
178
+ if (!(e instanceof SelectorError))
179
+ throw e;
180
+ }
181
+ continue;
182
+ }
183
+ if (node.content)
184
+ work.push(node.content);
185
+ }
186
+ return false;
187
+ };
114
188
  const keepBlocks = (items) => items.filter(item => item && typeof item === 'object' && !('file' in item));
115
189
  // A parser may hand back a list of nodes rather than a document; a selection
116
190
  // needs one node to walk, and it must hold the parsed nodes themselves.
@@ -179,6 +253,8 @@ export const assembleIncludes = (tree, opts) => {
179
253
  opts.onError(problem);
180
254
  };
181
255
  const held = [];
256
+ // the slot of the include with no source the second walk is resolving, outermost first
257
+ const slots = [];
182
258
  // the problem an error thrown with no handler was made from
183
259
  const thrownFrom = new WeakMap();
184
260
  // For each include being resolved, whether a block of its content was met
@@ -205,13 +281,18 @@ export const assembleIncludes = (tree, opts) => {
205
281
  throw error;
206
282
  }
207
283
  }
208
- held.push(...entries);
284
+ const slot = slots[0];
285
+ const at = slot ? held.indexOf(slot) : -1;
286
+ if (at >= 0)
287
+ held.splice(at, 0, ...entries);
288
+ else
289
+ held.push(...entries);
209
290
  };
210
291
  const report = (problem) => hold([{ problem, shown: true }]);
211
292
  const flush = () => {
212
293
  held
213
294
  .splice(0)
214
- .filter(entry => entry.shown)
295
+ .filter(entry => entry.shown && !entry.slot)
215
296
  .forEach(entry => emit(entry.problem));
216
297
  };
217
298
  // how many includes brought their content so far
@@ -228,6 +309,34 @@ export const assembleIncludes = (tree, opts) => {
228
309
  // copy of it, wherever it is made, waits as well. The mark is this call's own:
229
310
  // a later pass does not read it.
230
311
  const WAITING = Symbol('waiting include');
312
+ // An include with no source selects from the document in one state: its includes
313
+ // of other sources in, its own includes with no source as written (Specification,
314
+ // Include directive). The first walk leaves each such include in place with this
315
+ // mark, which carries the place it stands in; when the walk is over, a second walk
316
+ // over its result resolves the marked includes from that result, which they are not
317
+ // in, so none of them sees what another one brought. Its copies are walked by that
318
+ // second walk too, and a block is not placed inside itself: the blocks the walk is
319
+ // inside, read back to the blocks of that result they are copies of, are the path a
320
+ // block found must not be on.
321
+ const PLACEMENT = Symbol('include with no source');
322
+ let placements = 0;
323
+ let secondWalk = false;
324
+ // the result of the first walk, which the marked includes select from
325
+ let state;
326
+ // something in the first walk waited for a source: the state is not known in full
327
+ let stateWaits = false;
328
+ // the block of the state a block of the second walk is, or is a copy of
329
+ const placedFrom = new WeakMap();
330
+ const inside = [];
331
+ // In the second walk an include with no source inside a wrapper may bring nothing:
332
+ // the assignments it held go on past the wrapper, to the next block of the list
333
+ // that holds it, as they would have had it been resolved in the first walk. They
334
+ // pass only out of the wrappers the tree adds (root, _folded_section), never out of
335
+ // a written block, and not out of the root of the document, where they are reported.
336
+ let escapeNext = false;
337
+ let escaped;
338
+ // a cycle of an include with no source is reported whether or not it loses =set
339
+ const alwaysShown = new WeakSet();
231
340
  const FAILED = 'include-failed';
232
341
  const isMark = (node) => node && typeof node === 'object' && node.type === FAILED;
233
342
  const firstStop = (nodes, holder) => {
@@ -361,15 +470,33 @@ export const assembleIncludes = (tree, opts) => {
361
470
  const out = [];
362
471
  let pending = [];
363
472
  let last;
473
+ const mayEscape = escapeNext;
474
+ escapeNext = false;
475
+ // what an include with no source says in the second walk goes to its slot
364
476
  const visit = (n) => {
477
+ const slot = secondWalk && isIncludeBlock(n) && !slots.length ? n[PLACEMENT]?.slot : undefined;
478
+ if (slot)
479
+ slots.push(slot);
480
+ try {
481
+ visitEntry(n);
482
+ }
483
+ finally {
484
+ if (slot)
485
+ slots.pop();
486
+ }
487
+ };
488
+ const visitEntry = (n) => {
365
489
  if (n && n.type === 'config' && typeof n.name === 'string' && Array.isArray(n.config)) {
366
490
  config[n.name] = mergeConfigSettings(n.config, config[n.name]);
367
491
  }
368
- if (isIncludeBlock(n)) {
492
+ if (isIncludeBlock(n) && (!secondWalk || n[PLACEMENT])) {
369
493
  // assignments carried from an earlier include are older than its own
370
494
  const set = mergeSet(pending, n.set);
371
495
  pending = [];
372
- const here = [...chain, { file, location: n.location }];
496
+ // an include with no source names the place it was written in
497
+ const placed = secondWalk ? n[PLACEMENT] : undefined;
498
+ const here = placed ? placed.here : [...chain, { file, location: n.location }];
499
+ const where = placed ? placed.file : file;
373
500
  let resolved;
374
501
  const at = reached.length;
375
502
  try {
@@ -394,6 +521,32 @@ export const assembleIncludes = (tree, opts) => {
394
521
  finally {
395
522
  reached.length = at;
396
523
  }
524
+ if (resolved.kept) {
525
+ // as written, with the assignments it would have taken
526
+ const kept = set.length ? { ...n, set } : n;
527
+ if (kept !== n) {
528
+ const known = origin.get(n);
529
+ if (known)
530
+ origin.set(kept, known);
531
+ opts.onCopy?.(n, kept);
532
+ }
533
+ out.push(kept);
534
+ return;
535
+ }
536
+ if (resolved.placement) {
537
+ // left for the second walk with its assignments, as the place it stands in
538
+ placements++;
539
+ const slot = { problem: undefined, shown: false, slot: true };
540
+ if (!quiet)
541
+ held.push(slot);
542
+ const kept = { ...n, ...(set.length ? { set } : {}), [PLACEMENT]: { ...resolved.placement, slot } };
543
+ const known = origin.get(n);
544
+ if (known)
545
+ origin.set(kept, known);
546
+ opts.onCopy?.(n, kept);
547
+ out.push(kept);
548
+ return;
549
+ }
397
550
  if (resolved.waiting) {
398
551
  const kept = { ...n, ...(set.length ? { set } : {}), [WAITING]: true };
399
552
  const known = origin.get(n);
@@ -407,16 +560,24 @@ export const assembleIncludes = (tree, opts) => {
407
560
  if (!failure)
408
561
  brought++;
409
562
  if (!failure)
410
- markVia(nodes, `${file}@${n.location?.start?.offset ?? ''}`, { file, location: n.location });
563
+ markVia(nodes, `${where}@${n.location?.start?.offset ?? ''}`, { file: where, location: n.location });
564
+ // a copy an include with no source placed came first through the way that include came
565
+ if (!failure && placed) {
566
+ const way = origin.get(n);
567
+ if (way?.via)
568
+ prefixVia(nodes, way);
569
+ }
411
570
  if (!failure && resolved.outside.length) {
412
- const step = stepOf(file, n.location);
571
+ const step = stepOf(where, n.location);
413
572
  const problem = { kind: 'external', target: selector, message: '', chain: here };
414
573
  hold([{ problem, shown: false, outside: { step, written: resolved.written, sources: resolved.outside } }]);
415
574
  }
416
575
  if (failure) {
417
576
  const entries = failure.map(problem => ({
418
577
  problem,
419
- shown: problem.kind !== 'cycle' || (opts.reportCycles === true && !maskCycles.has(problem)),
578
+ shown: problem.kind !== 'cycle' ||
579
+ alwaysShown.has(problem) ||
580
+ (opts.reportCycles === true && !maskCycles.has(problem)),
420
581
  }));
421
582
  if (set.length)
422
583
  lose(entries, set);
@@ -438,7 +599,7 @@ export const assembleIncludes = (tree, opts) => {
438
599
  out.push(...nodes);
439
600
  return;
440
601
  }
441
- const applied = applySetToFirst(nodes, set, { mode: 'include', origin, onCopy: opts.onCopy, holder });
602
+ const applied = applySetToFirst(nodes, set, { mode: 'include', origin, onCopy: copied, holder });
442
603
  out.push(...applied.nodes);
443
604
  if (applied.outcome !== 'none')
444
605
  return;
@@ -453,37 +614,50 @@ export const assembleIncludes = (tree, opts) => {
453
614
  return;
454
615
  }
455
616
  const walked = walkNode(n, context, stack, chain, file, home, config, holder);
617
+ const leaving = escaped;
618
+ escaped = undefined;
456
619
  const items = Array.isArray(walked) ? walked : [walked];
620
+ const goOn = () => {
621
+ if (!leaving)
622
+ return;
623
+ pending = mergeSet(pending, leaving.pending);
624
+ last = leaving.last ?? last;
625
+ };
457
626
  if (!pending.length) {
458
627
  out.push(...items);
459
- return;
628
+ return goOn();
460
629
  }
461
630
  const stopped = firstStop(items, holder);
462
631
  if (stopped) {
463
632
  lose(stopped, pending);
464
633
  pending = [];
465
634
  out.push(...items);
466
- return;
635
+ return goOn();
467
636
  }
468
- const applied = applySetToFirst(items, pending, { mode: 'carry', origin, onCopy: opts.onCopy, holder });
637
+ const applied = applySetToFirst(items, pending, { mode: 'carry', origin, onCopy: copied, holder });
469
638
  if (applied.outcome !== 'none')
470
639
  pending = [];
471
640
  out.push(...applied.nodes);
641
+ goOn();
472
642
  };
473
643
  level++;
474
644
  try {
475
645
  for (const n of list) {
476
646
  const from = out.length;
477
647
  visit(n);
478
- if (level === 1) {
648
+ // the problems wait for the end of the call: the second walk may yet name
649
+ // assignments a failure of the first one lost
650
+ if (level === 1)
479
651
  confirmOutside(out.slice(from));
480
- flush();
481
- }
482
652
  }
483
653
  }
484
654
  finally {
485
655
  level--;
486
656
  }
657
+ if (pending.length && mayEscape) {
658
+ escaped = { pending, last };
659
+ return out;
660
+ }
487
661
  if (pending.length && last) {
488
662
  report({
489
663
  kind: 'set-target',
@@ -512,6 +686,22 @@ export const assembleIncludes = (tree, opts) => {
512
686
  };
513
687
  visit(nodes);
514
688
  };
689
+ // The way a placement came, put before the way of each node it brought.
690
+ const prefixVia = (nodes, way) => {
691
+ const visit = (node) => {
692
+ if (!node || typeof node !== 'object')
693
+ return;
694
+ if (Array.isArray(node))
695
+ return node.forEach(visit);
696
+ const known = origin.get(node);
697
+ if (known) {
698
+ const via = known.via ? `${way.via}>${known.via}` : way.via;
699
+ origin.set(node, { ...known, via, steps: [...(way.steps ?? []), ...(known.steps ?? [])] });
700
+ }
701
+ visit(node.content);
702
+ };
703
+ visit(nodes);
704
+ };
515
705
  // Which directive of which entry: the file it is written in, as the walk names
516
706
  // it, and its offset there.
517
707
  const stepOf = (file, location) => `${file}\u0000${location?.start?.offset ?? ''}`;
@@ -606,6 +796,57 @@ export const assembleIncludes = (tree, opts) => {
606
796
  copy[key] = key === 'location' ? node[key] : copyDeep(node[key]);
607
797
  if (node[WAITING])
608
798
  copy[WAITING] = true;
799
+ if (node[PLACEMENT])
800
+ copy[PLACEMENT] = node[PLACEMENT];
801
+ const known = origin.get(node);
802
+ if (known)
803
+ origin.set(copy, known);
804
+ opts.onCopy?.(node, copy);
805
+ return copy;
806
+ };
807
+ // What a copy made inside this call keeps of the node it copies: where an include
808
+ // failed, a reading on its own, the block of the state it stands for, the marks.
809
+ const carry = (from, to) => {
810
+ const failed = failedAt.get(from);
811
+ if (failed)
812
+ failedAt.set(to, failed);
813
+ const alone = readAlone.get(from);
814
+ if (alone)
815
+ readAlone.set(to, alone);
816
+ const placed = placedFrom.get(from);
817
+ if (placed)
818
+ placedFrom.set(to, placed);
819
+ if (from[WAITING])
820
+ to[WAITING] = true;
821
+ if (from[PLACEMENT])
822
+ to[PLACEMENT] = from[PLACEMENT];
823
+ };
824
+ const copied = (from, to) => {
825
+ carry(from, to);
826
+ opts.onCopy?.(from, to);
827
+ };
828
+ // A block found in the state, copied for one place: each node remembers the node
829
+ // of the state it copies, and a failure, a reading on its own and the marks it
830
+ // carries come with it, so a copy is read as the block it copies.
831
+ const copyPlaced = (node) => {
832
+ if (Array.isArray(node))
833
+ return node.map(copyPlaced);
834
+ if (!node || typeof node !== 'object')
835
+ return node;
836
+ const copy = {};
837
+ for (const key of Object.keys(node))
838
+ copy[key] = key === 'location' ? node[key] : copyPlaced(node[key]);
839
+ if (node[WAITING])
840
+ copy[WAITING] = true;
841
+ if (node[PLACEMENT])
842
+ copy[PLACEMENT] = node[PLACEMENT];
843
+ placedFrom.set(copy, placedFrom.get(node) ?? node);
844
+ const failed = failedAt.get(node);
845
+ if (failed)
846
+ failedAt.set(copy, failed);
847
+ const alone = readAlone.get(node);
848
+ if (alone)
849
+ readAlone.set(copy, alone);
609
850
  const known = origin.get(node);
610
851
  if (known)
611
852
  origin.set(copy, known);
@@ -693,6 +934,106 @@ export const assembleIncludes = (tree, opts) => {
693
934
  failures.length = failed;
694
935
  }
695
936
  };
937
+ // A file an operand names is read the way an included file is, from the directory
938
+ // of the directive; one already on the way does not resolve.
939
+ const operandReader = (context, stack, here) => {
940
+ let waits = false;
941
+ const read = (scheme, document) => {
942
+ if (!schemes.includes(scheme))
943
+ return undefined;
944
+ const found = provider.locate(document, context, true, undefined, scheme);
945
+ const refused = found && refusal(found);
946
+ if (refused !== undefined) {
947
+ throw new SelectorError('resolution', `the source does not resolve: ${scheme}:${document}: ${refused}`);
948
+ }
949
+ const source = found?.sources[0];
950
+ const target = source?.id ?? '';
951
+ const text = !source || stack.includes(target) ? null : textOf(source);
952
+ if (!found || text === undefined)
953
+ waits = true;
954
+ if (!source || text === null || text === undefined)
955
+ return undefined;
956
+ let own;
957
+ try {
958
+ own = opts.parse(text, target, undefined, { format: source.format ?? formatOfFile(target) });
959
+ }
960
+ catch (e) {
961
+ // a file that fails to parse is one the operand cannot be read from
962
+ if (!opts.tolerant)
963
+ throw e;
964
+ return undefined;
965
+ }
966
+ recordOrigin(own, { file: target, text }, origin);
967
+ // the file is a document of its own: it is walked as the first walk walks
968
+ const second = secondWalk;
969
+ secondWalk = false;
970
+ try {
971
+ return [
972
+ {
973
+ file: document,
974
+ node: asDocument(walkNode(own, source.context, [...stack, target], here, target, own, {})),
975
+ },
976
+ ];
977
+ }
978
+ finally {
979
+ secondWalk = second;
980
+ }
981
+ };
982
+ return { read, waits: () => waits };
983
+ };
984
+ // An include with no source, in the second walk: its blocks are found in the state,
985
+ // copied, and walked where it stands, unless one of them holds it.
986
+ const resolvePlacement = (node, selector, stack, config) => {
987
+ const at = node[PLACEMENT];
988
+ const mark = failures.length;
989
+ const done = (nodes, failure) => ({
990
+ nodes,
991
+ failure,
992
+ inner: failures.slice(mark),
993
+ selector,
994
+ outside: [],
995
+ written: selector,
996
+ });
997
+ if (stateWaits)
998
+ return { ...done([node]), waiting: true };
999
+ const operands = operandReader(at.context, stack, at.here);
1000
+ const doc = { file: mainFile, node: asDocument(state) };
1001
+ let found;
1002
+ try {
1003
+ found = outermost(keepBlocks(runSelector(selector, [doc], { home: [doc], readSource: operands.read })));
1004
+ }
1005
+ catch (e) {
1006
+ if (!(e instanceof SelectorError))
1007
+ throw e;
1008
+ if (operands.waits())
1009
+ return { ...done([node]), waiting: true };
1010
+ return done([], [
1011
+ {
1012
+ kind: 'operand',
1013
+ target: selector,
1014
+ message: `include selector cannot be read: ${e.message}`,
1015
+ chain: at.here,
1016
+ },
1017
+ ]);
1018
+ }
1019
+ if (operands.waits())
1020
+ return { ...done([node]), waiting: true };
1021
+ if (found.some(block => inside.includes(block))) {
1022
+ const problem = {
1023
+ kind: 'cycle',
1024
+ target: selector,
1025
+ message: `include brings nothing: a block it would bring holds it: ${selector}`,
1026
+ chain: at.here,
1027
+ };
1028
+ alwaysShown.add(problem);
1029
+ return done([], [problem]);
1030
+ }
1031
+ const nodes = found.flatMap(block => {
1032
+ const walked = walkNode(copyPlaced(block), at.context, stack, at.here, at.file, state, { ...config });
1033
+ return Array.isArray(walked) ? walked : [walked];
1034
+ });
1035
+ return done(nodes);
1036
+ };
696
1037
  // What an include comes to: the nodes that take its place, or the problems
697
1038
  // that left it with nothing, not yet reported
698
1039
  const resolveInclude = (node, context, stack, here, file, home, config) => {
@@ -731,6 +1072,15 @@ export const assembleIncludes = (tree, opts) => {
731
1072
  // what it would have brought in is missing, and a reader of the tree cannot
732
1073
  // tell that on its own.
733
1074
  const fail = (problem, keep = false) => done(keep ? [withoutSet(node)] : [], [problem]);
1075
+ // a selection with patterns and no source selects from the document itself
1076
+ const sameDocument = !!selector && !!parsed && !parsed.scheme && !parsed.document && !parsed.anchor && parsed.patterns.length > 0;
1077
+ if (sameDocument && !secondWalk)
1078
+ return { ...done([node]), placement: { file, here, context } };
1079
+ if (sameDocument)
1080
+ return resolvePlacement(node, selector, stack, config);
1081
+ // a host with no files to give leaves the other includes as they are written
1082
+ if (opts.onlySameDocument)
1083
+ return { ...done([node]), kept: true };
734
1084
  if (!selector || !parsed || !parsed.scheme || !parsed.document) {
735
1085
  return fail({
736
1086
  kind: 'unparsed-selector',
@@ -758,7 +1108,11 @@ export const assembleIncludes = (tree, opts) => {
758
1108
  chain: here,
759
1109
  }, true);
760
1110
  }
761
- const wait = () => ({ ...done([node]), roots: undefined, waiting: true });
1111
+ const wait = () => {
1112
+ if (!secondWalk)
1113
+ stateWaits = true;
1114
+ return { ...done([node]), roots: undefined, waiting: true };
1115
+ };
762
1116
  const located = provider.locate(parsed.document, context, false, here[here.length - 1], parsed.scheme);
763
1117
  if (!located)
764
1118
  return wait();
@@ -962,42 +1316,8 @@ export const assembleIncludes = (tree, opts) => {
962
1316
  });
963
1317
  }
964
1318
  roots = placedDocs;
965
- // a file an operand names is read the way an included file is, from the
966
- // directory of the directive; one already on the way does not resolve
967
- let operandWaits = false;
968
- const readSource = (scheme, document) => {
969
- if (!schemes.includes(scheme))
970
- return undefined;
971
- const found = provider.locate(document, context, true, undefined, scheme);
972
- const refused = found && refusal(found);
973
- if (refused !== undefined) {
974
- throw new SelectorError('resolution', `the source does not resolve: ${scheme}:${document}: ${refused}`);
975
- }
976
- const source = found?.sources[0];
977
- const target = source?.id ?? '';
978
- const text = !source || stack.includes(target) ? null : textOf(source);
979
- if (!found || text === undefined)
980
- operandWaits = true;
981
- if (!source || text === null || text === undefined)
982
- return undefined;
983
- let own;
984
- try {
985
- own = opts.parse(text, target, undefined, { format: source.format ?? formatOfFile(target) });
986
- }
987
- catch (e) {
988
- // a file that fails to parse is one the operand cannot be read from
989
- if (!opts.tolerant)
990
- throw e;
991
- return undefined;
992
- }
993
- recordOrigin(own, { file: target, text }, origin);
994
- return [
995
- {
996
- file: document,
997
- node: asDocument(walkNode(own, source.context, [...stack, target], here, target, own, {})),
998
- },
999
- ];
1000
- };
1319
+ const operands = operandReader(context, stack, here);
1320
+ const readSource = operands.read;
1001
1321
  // An include that waits for a later pass stands for blocks not yet known. The
1002
1322
  // universal pattern without a predicate would find every one of them, so the
1003
1323
  // include stays where it is written, among the found blocks; under other
@@ -1039,14 +1359,14 @@ export const assembleIncludes = (tree, opts) => {
1039
1359
  };
1040
1360
  try {
1041
1361
  const found = outermost(keepBlocks(runSelector(selector, docs, { home: [{ file, node: asDocument(home) }], readSource })));
1042
- if (operandWaits)
1362
+ if (operands.waits())
1043
1363
  return wait();
1044
1364
  return done(withoutLooseDirectives(placedFor(withWaiting(found))));
1045
1365
  }
1046
1366
  catch (e) {
1047
1367
  if (!(e instanceof SelectorError))
1048
1368
  throw e;
1049
- if (operandWaits)
1369
+ if (operands.waits())
1050
1370
  return wait();
1051
1371
  return fail({
1052
1372
  kind: 'operand',
@@ -1061,8 +1381,12 @@ export const assembleIncludes = (tree, opts) => {
1061
1381
  return node;
1062
1382
  if (Array.isArray(node))
1063
1383
  return walkList(node, context, stack, chain, file, home, config, holder);
1064
- if (isIncludeBlock(node))
1384
+ // the second walk resolves only the includes with no source; the others stay
1385
+ if (isIncludeBlock(node)) {
1386
+ if (secondWalk && !node[PLACEMENT])
1387
+ return node;
1065
1388
  return walkList([node], context, stack, chain, file, home, config, holder);
1389
+ }
1066
1390
  const wrapper = node.type === 'block' && (node.name === 'root' || node.name === '_folded_section');
1067
1391
  if (isSetTarget(node, holder))
1068
1392
  markReached();
@@ -1070,17 +1394,40 @@ export const assembleIncludes = (tree, opts) => {
1070
1394
  // a block is a lexical scope for the settings declared inside it
1071
1395
  const scope = node.type === 'block' && !wrapper ? { ...config } : config;
1072
1396
  const inner = holderInside(node, holder);
1073
- const copy = { ...node, content: walkList(node.content, context, stack, chain, file, home, scope, inner) };
1397
+ // a block the second walk is inside, as the block of the state it stands for
1398
+ const counted = secondWalk && node.type === 'block' && !wrapper;
1399
+ if (counted)
1400
+ inside.push(placedFrom.get(node) ?? node);
1401
+ let content;
1402
+ try {
1403
+ // the root of the document has no list around it to pass assignments on to
1404
+ escapeNext = secondWalk && wrapper && node !== state;
1405
+ content = walkList(node.content, context, stack, chain, file, home, scope, inner);
1406
+ }
1407
+ finally {
1408
+ if (counted)
1409
+ inside.pop();
1410
+ }
1411
+ const copy = { ...node, content };
1074
1412
  const known = origin.get(node);
1075
1413
  if (known)
1076
1414
  origin.set(copy, known);
1415
+ const from = placedFrom.get(node);
1416
+ if (from)
1417
+ placedFrom.set(copy, from);
1077
1418
  opts.onCopy?.(node, copy);
1078
1419
  return copy;
1079
1420
  }
1080
1421
  return node;
1081
1422
  };
1082
1423
  try {
1083
- return unmark(walkNode(tree, opts.context, opts.self ? [opts.self] : [], [], mainFile, tree, {}));
1424
+ const self = opts.self ? [opts.self] : [];
1425
+ const first = walkNode(tree, opts.context, self, [], mainFile, tree, {});
1426
+ if (!placements)
1427
+ return unmark(first);
1428
+ state = first;
1429
+ secondWalk = true;
1430
+ return unmark(walkNode(first, opts.context, self, [], mainFile, tree, {}));
1084
1431
  }
1085
1432
  finally {
1086
1433
  flush();