@rohal12/spindle 0.64.32 → 0.64.34
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/pkg/format.js +1 -1
- package/dist/pkg/headless.js +182 -45
- package/dist/pkg/story-variables.js +210 -51
- package/dist/pkg/tooling.js +1 -0
- package/package.json +1 -1
- package/src/code-check.ts +1 -0
- package/src/components/macros/SaveManager.tsx +7 -3
- package/src/components/macros/input-macro.ts +4 -1
- package/src/define-macro.ts +5 -21
- package/src/format/format-passage.ts +9 -0
- package/src/hooks/use-modal-focus.ts +23 -10
- package/src/js-bindings.ts +64 -27
- package/src/js-lexer.ts +155 -15
- package/src/markup/ast.ts +6 -1
- package/src/markup/pair.ts +10 -2
- package/src/markup/parse.ts +40 -1
- package/src/markup/spindle.d.peggy.ts +2 -0
- package/src/markup/spindle.peggy +5 -6
- package/src/markup/validate.ts +3 -0
- package/src/tooling.ts +5 -0
- package/src/types-drift-check.ts +10 -0
- package/src/utils/control-bindings.ts +52 -0
- package/src/utils/control-edits.ts +12 -1
- package/types/tooling.d.ts +40 -0
|
@@ -22,6 +22,17 @@ const FOCUSABLE = [
|
|
|
22
22
|
'[tabindex]',
|
|
23
23
|
].join(',');
|
|
24
24
|
|
|
25
|
+
/** Only the first summary child of a details element is its native toggle. */
|
|
26
|
+
function isDetailsToggle(el: HTMLElement): boolean {
|
|
27
|
+
return (
|
|
28
|
+
el.localName === 'summary' &&
|
|
29
|
+
el.parentElement?.localName === 'details' &&
|
|
30
|
+
Array.from(el.parentElement.children).find(
|
|
31
|
+
(c) => c.localName === 'summary',
|
|
32
|
+
) === el
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
25
36
|
/**
|
|
26
37
|
* Whether `el` can be focused at all: shown, rendered, enabled (also by its
|
|
27
38
|
* fieldset) and not inert.
|
|
@@ -38,7 +49,16 @@ function isAvailable(el: HTMLElement): boolean {
|
|
|
38
49
|
if (at.hasAttribute('inert')) return false;
|
|
39
50
|
}
|
|
40
51
|
// Also covers controls disabled by a <fieldset disabled> ancestor
|
|
41
|
-
|
|
52
|
+
if (el.matches(':disabled')) return false;
|
|
53
|
+
// [tabindex] also selects empty/invalid attributes on plain elements.
|
|
54
|
+
// Those do not make the element focusable. Editing hosts can still
|
|
55
|
+
// report tabIndex -1; negative valid tabindexes allow scripted focus.
|
|
56
|
+
return (
|
|
57
|
+
tabIndexAttr(el) !== null ||
|
|
58
|
+
el.tabIndex >= 0 ||
|
|
59
|
+
(el.isContentEditable && !el.parentElement?.isContentEditable) ||
|
|
60
|
+
isDetailsToggle(el)
|
|
61
|
+
);
|
|
42
62
|
}
|
|
43
63
|
|
|
44
64
|
/** The `tabindex` of `el` as an integer, or null when absent or invalid. */
|
|
@@ -92,15 +112,8 @@ function isTabStop(el: HTMLElement, radioStops: RadioStops): boolean {
|
|
|
92
112
|
return false;
|
|
93
113
|
}
|
|
94
114
|
// Only a details element's first summary child is its toggle
|
|
95
|
-
if (el.localName === 'summary')
|
|
96
|
-
|
|
97
|
-
if (
|
|
98
|
-
details?.localName !== 'details' ||
|
|
99
|
-
Array.from(details.children).find((c) => c.localName === 'summary') !== el
|
|
100
|
-
) {
|
|
101
|
-
return tabIndex !== null;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
115
|
+
if (el.localName === 'summary' && !isDetailsToggle(el))
|
|
116
|
+
return tabIndex !== null;
|
|
104
117
|
// localName, not instanceof: elements of an embedded document are of
|
|
105
118
|
// another realm's classes
|
|
106
119
|
const input = el as HTMLInputElement;
|
package/src/js-bindings.ts
CHANGED
|
@@ -119,6 +119,10 @@ export class Bindings {
|
|
|
119
119
|
private readonly byClose = new Map<number, Group>();
|
|
120
120
|
private readonly functions = new Map<Group, boolean>();
|
|
121
121
|
private readonly functionStarts = new Map<number, number>();
|
|
122
|
+
private readonly listKeywords = new Map<
|
|
123
|
+
number,
|
|
124
|
+
{ floor: number; word: { text: string; start: number } | undefined }
|
|
125
|
+
>();
|
|
122
126
|
private readonly facts = new Map<
|
|
123
127
|
ReadonlySet<string>,
|
|
124
128
|
Map<string, Binding[]>
|
|
@@ -336,33 +340,51 @@ export class Bindings {
|
|
|
336
340
|
*/
|
|
337
341
|
private statementEnd(from: number): number {
|
|
338
342
|
const { code } = this;
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
343
|
+
// Suspend enclosing statements on the heap: distributed scripts can
|
|
344
|
+
// contain arbitrarily deep unbraced bodies, even if JS cannot run them.
|
|
345
|
+
const pendingIf: boolean[] = [];
|
|
346
|
+
statements: while (true) {
|
|
347
|
+
const start = this.forward(from);
|
|
348
|
+
const block = this.byOpen.get(start);
|
|
349
|
+
let end: number;
|
|
350
|
+
if (block?.kind === '{') {
|
|
351
|
+
end = Math.min(block.close + 1, code.length);
|
|
352
|
+
} else {
|
|
353
|
+
const word = this.wordAt(start);
|
|
354
|
+
if (/^(?:if|for|while|with)$/.test(word)) {
|
|
355
|
+
let paren = this.forward(start + word.length);
|
|
356
|
+
if (word === 'for' && this.wordAt(paren) === 'await') {
|
|
357
|
+
paren = this.forward(paren + 5);
|
|
358
|
+
}
|
|
359
|
+
const header = this.byOpen.get(paren);
|
|
360
|
+
if (header) {
|
|
361
|
+
pendingIf.push(word === 'if');
|
|
362
|
+
from = header.close + 1;
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
for (end = start; end < code.length; end++) {
|
|
367
|
+
const c = code.charAt(end);
|
|
368
|
+
if (c === ';') {
|
|
369
|
+
end++;
|
|
370
|
+
break;
|
|
371
|
+
}
|
|
372
|
+
if (CLOSERS[c]) break;
|
|
373
|
+
const group = this.byOpen.get(end);
|
|
374
|
+
if (group) end = group.close;
|
|
375
|
+
else if (c === '\n' && this.startsStatement(end)) break;
|
|
376
|
+
}
|
|
347
377
|
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
const end = this.statementEnd(header.close + 1);
|
|
378
|
+
while (pendingIf.length) {
|
|
379
|
+
const isIf = pendingIf.pop();
|
|
351
380
|
const next = this.forward(end);
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
381
|
+
if (isIf && this.wordAt(next) === 'else') {
|
|
382
|
+
from = next + 4;
|
|
383
|
+
continue statements;
|
|
384
|
+
}
|
|
355
385
|
}
|
|
386
|
+
return end;
|
|
356
387
|
}
|
|
357
|
-
for (let end = start; end < code.length; end++) {
|
|
358
|
-
const c = code.charAt(end);
|
|
359
|
-
if (c === ';') return end + 1;
|
|
360
|
-
if (CLOSERS[c]) return end;
|
|
361
|
-
const group = this.byOpen.get(end);
|
|
362
|
-
if (group) end = group.close;
|
|
363
|
-
else if (c === '\n' && this.startsStatement(end)) return end;
|
|
364
|
-
}
|
|
365
|
-
return code.length;
|
|
366
388
|
}
|
|
367
389
|
|
|
368
390
|
/** Whether the line break at `index` ends a statement, and one follows. */
|
|
@@ -490,9 +512,19 @@ export class Bindings {
|
|
|
490
512
|
): { text: string; start: number } | undefined {
|
|
491
513
|
const { code } = this;
|
|
492
514
|
const floor = this.innermost(comma)?.open ?? -1;
|
|
493
|
-
|
|
515
|
+
const visited: number[] = [];
|
|
516
|
+
let result: { text: string; start: number } | undefined;
|
|
517
|
+
for (let i = comma; i > floor; i--) {
|
|
494
518
|
const c = code.charAt(i);
|
|
495
|
-
if (c === ';')
|
|
519
|
+
if (c === ';') break;
|
|
520
|
+
if (c === ',') {
|
|
521
|
+
const cached = this.listKeywords.get(i);
|
|
522
|
+
if (cached?.floor === floor) {
|
|
523
|
+
result = cached.word;
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
visited.push(i);
|
|
527
|
+
}
|
|
496
528
|
const group = CLOSERS[c] ? this.byClose.get(i) : undefined;
|
|
497
529
|
if (group) i = group.open;
|
|
498
530
|
else if (isWordChar(c)) {
|
|
@@ -501,12 +533,17 @@ export class Bindings {
|
|
|
501
533
|
DECLARATIONS.has(word.text) &&
|
|
502
534
|
code.charAt(word.start - 1) !== '.'
|
|
503
535
|
) {
|
|
504
|
-
|
|
536
|
+
result = word;
|
|
537
|
+
break;
|
|
505
538
|
}
|
|
506
539
|
i = word.start;
|
|
507
540
|
}
|
|
508
541
|
}
|
|
509
|
-
|
|
542
|
+
// Cache every traversed prefix, including misses, so lookup order does
|
|
543
|
+
// not matter. Retain the bracket floor for tolerant recovery contexts.
|
|
544
|
+
for (const at of visited)
|
|
545
|
+
this.listKeywords.set(at, { floor, word: result });
|
|
546
|
+
return result;
|
|
510
547
|
}
|
|
511
548
|
|
|
512
549
|
private nameEnd(at: number): number {
|
package/src/js-lexer.ts
CHANGED
|
@@ -552,6 +552,46 @@ export interface ParsedCode {
|
|
|
552
552
|
strings: string[];
|
|
553
553
|
}
|
|
554
554
|
|
|
555
|
+
/** A binding write (empty path), or a member write, with the entire target span. */
|
|
556
|
+
export interface CodeMutation extends Span {
|
|
557
|
+
/** Null for an indirect target; null path segments are computed keys. */
|
|
558
|
+
target: { sigil: Sigil; name: string; path: (string | null)[] } | null;
|
|
559
|
+
/** Compound assignments and updates read the target as well as writing it. */
|
|
560
|
+
read: boolean;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/** A reason declaration-time shapes alone cannot justify a missing-member advisory. */
|
|
564
|
+
export interface CodeShapeUncertainty extends Span {
|
|
565
|
+
reason:
|
|
566
|
+
| 'computed-write'
|
|
567
|
+
| 'indirect-write'
|
|
568
|
+
| 'prototype-write'
|
|
569
|
+
| 'call'
|
|
570
|
+
| 'escape'
|
|
571
|
+
| 'dynamic-scope';
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
export interface CodeEffects {
|
|
575
|
+
mutations: CodeMutation[];
|
|
576
|
+
uncertain: CodeShapeUncertainty[];
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* Conservative shape effects, using the same grammar and target roles as
|
|
581
|
+
* parseCode. Includes nested functions and unreachable branches; does not
|
|
582
|
+
* attempt alias, call, control-flow or execution-order analysis.
|
|
583
|
+
*/
|
|
584
|
+
export function analyzeCodeEffects(
|
|
585
|
+
src: string,
|
|
586
|
+
goal: JsGoal = 'expression',
|
|
587
|
+
): CodeEffects {
|
|
588
|
+
const effects: CodeEffects = { mutations: [], uncertain: [] };
|
|
589
|
+
parseWithAst(src, goal, effects);
|
|
590
|
+
effects.mutations.sort((a, b) => a.start - b.start);
|
|
591
|
+
effects.uncertain.sort((a, b) => a.start - b.start);
|
|
592
|
+
return effects;
|
|
593
|
+
}
|
|
594
|
+
|
|
555
595
|
/**
|
|
556
596
|
* Parse `src` as `goal`, and find its variable references and string text.
|
|
557
597
|
* Throws a `CodeSyntaxError` for code that is not well-formed, and for a
|
|
@@ -568,10 +608,11 @@ export function parseCode(
|
|
|
568
608
|
function parseWithAst(
|
|
569
609
|
src: string,
|
|
570
610
|
goal: JsGoal,
|
|
611
|
+
effects?: CodeEffects,
|
|
571
612
|
): { out: ParsedCode; ast: AnyNode } {
|
|
572
613
|
const ast = parseAst(src, goal);
|
|
573
614
|
const out: ParsedCode = { refs: [], strings: [] };
|
|
574
|
-
walk(ast, src, out, false, false);
|
|
615
|
+
walk(ast, src, out, false, false, 'read', effects);
|
|
575
616
|
out.refs.sort((a, b) => a.start - b.start);
|
|
576
617
|
return { out, ast };
|
|
577
618
|
}
|
|
@@ -718,6 +759,28 @@ function propertyName(node: unknown, src: string): void {
|
|
|
718
759
|
}
|
|
719
760
|
}
|
|
720
761
|
|
|
762
|
+
/** Keep static member keys decoded; an arbitrary computed key stays unknown. */
|
|
763
|
+
function mutationTarget(
|
|
764
|
+
node: AnyNode,
|
|
765
|
+
src: string,
|
|
766
|
+
): { root: [Sigil, string] | null; path: (string | null)[] } {
|
|
767
|
+
const path: (string | null)[] = [];
|
|
768
|
+
while (node.type === 'MemberExpression') {
|
|
769
|
+
const property = node.property as AnyNode;
|
|
770
|
+
path.push(
|
|
771
|
+
!node.computed && property.type === 'Identifier'
|
|
772
|
+
? (property.name as string)
|
|
773
|
+
: property.type === 'Literal' &&
|
|
774
|
+
['string', 'number'].includes(typeof property.value)
|
|
775
|
+
? String(property.value)
|
|
776
|
+
: null,
|
|
777
|
+
);
|
|
778
|
+
node = node.object as AnyNode;
|
|
779
|
+
}
|
|
780
|
+
const sigil = node.type === 'Identifier' ? sigilOf(node, src) : null;
|
|
781
|
+
return { root: sigil, path: path.reverse() };
|
|
782
|
+
}
|
|
783
|
+
|
|
721
784
|
/**
|
|
722
785
|
* Collect the references and string text under `node`. `binding`: the
|
|
723
786
|
* identifiers here are declared (a `let`, a parameter), where a sigil
|
|
@@ -730,13 +793,14 @@ function walk(
|
|
|
730
793
|
binding: boolean,
|
|
731
794
|
shorthand: boolean,
|
|
732
795
|
access: 'read' | 'write' | 'readwrite' = 'read',
|
|
796
|
+
effects?: CodeEffects,
|
|
733
797
|
): void {
|
|
734
|
-
type Visit = [unknown, boolean, boolean, typeof access];
|
|
735
|
-
const pending: Visit[] = [[root, binding, shorthand, access]];
|
|
798
|
+
type Visit = [unknown, boolean, boolean, typeof access, boolean, boolean];
|
|
799
|
+
const pending: Visit[] = [[root, binding, shorthand, access, false, false]];
|
|
736
800
|
while (pending.length) {
|
|
737
|
-
const [node, bind, short, role] = pending.pop()!;
|
|
801
|
+
const [node, bind, short, role, memberObject, escapes] = pending.pop()!;
|
|
738
802
|
const children: Visit[] = [];
|
|
739
|
-
visit(node, bind, short, role, children);
|
|
803
|
+
visit(node, bind, short, role, memberObject, escapes, children);
|
|
740
804
|
// Reverse only the work stack, retaining reference/string and error order.
|
|
741
805
|
for (let i = children.length - 1; i >= 0; i--) pending.push(children[i]!);
|
|
742
806
|
}
|
|
@@ -746,10 +810,13 @@ function walk(
|
|
|
746
810
|
binding: boolean,
|
|
747
811
|
shorthand: boolean,
|
|
748
812
|
access: Visit[3],
|
|
813
|
+
memberObject: boolean,
|
|
814
|
+
escapes: boolean,
|
|
749
815
|
children: Visit[],
|
|
750
816
|
): void {
|
|
751
817
|
if (Array.isArray(node)) {
|
|
752
|
-
for (const n of node)
|
|
818
|
+
for (const n of node)
|
|
819
|
+
children.push([n, binding, false, access, false, escapes]);
|
|
753
820
|
return;
|
|
754
821
|
}
|
|
755
822
|
if (!isNode(node)) return;
|
|
@@ -758,13 +825,39 @@ function walk(
|
|
|
758
825
|
bind = false,
|
|
759
826
|
short = false,
|
|
760
827
|
role: typeof access = 'read',
|
|
828
|
+
member = false,
|
|
829
|
+
escape = escapes,
|
|
761
830
|
) => {
|
|
762
|
-
children.push([child, bind, short, role]);
|
|
831
|
+
children.push([child, bind, short, role, member, escape]);
|
|
763
832
|
};
|
|
833
|
+
const uncertain = (reason: CodeShapeUncertainty['reason']) =>
|
|
834
|
+
effects?.uncertain.push({ start: node.start, end: node.end, reason });
|
|
835
|
+
if (effects) {
|
|
836
|
+
if (
|
|
837
|
+
[
|
|
838
|
+
'CallExpression',
|
|
839
|
+
'NewExpression',
|
|
840
|
+
'TaggedTemplateExpression',
|
|
841
|
+
].includes(node.type)
|
|
842
|
+
)
|
|
843
|
+
uncertain('call');
|
|
844
|
+
if (node.type === 'WithStatement') uncertain('dynamic-scope');
|
|
845
|
+
if (escapes && node.type === 'ThisExpression') uncertain('escape');
|
|
846
|
+
}
|
|
764
847
|
switch (node.type) {
|
|
765
848
|
case 'Identifier': {
|
|
766
849
|
const sigil = sigilOf(node, src);
|
|
767
|
-
if (!sigil)
|
|
850
|
+
if (!sigil) {
|
|
851
|
+
if (
|
|
852
|
+
effects &&
|
|
853
|
+
escapes &&
|
|
854
|
+
!binding &&
|
|
855
|
+
!memberObject &&
|
|
856
|
+
access === 'read'
|
|
857
|
+
)
|
|
858
|
+
uncertain('escape');
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
768
861
|
if (binding) {
|
|
769
862
|
throw new CodeSyntaxError(
|
|
770
863
|
`"${node.name as string}" is a ${NAMESPACE_NAME[sigil[0]]} variable and can't be declared`,
|
|
@@ -781,6 +874,20 @@ function walk(
|
|
|
781
874
|
read: access !== 'write',
|
|
782
875
|
write: access !== 'read',
|
|
783
876
|
});
|
|
877
|
+
if (effects) {
|
|
878
|
+
if (access !== 'read') {
|
|
879
|
+
effects.mutations.push({
|
|
880
|
+
start: node.start,
|
|
881
|
+
end: node.end,
|
|
882
|
+
target: { sigil: sigil[0], name: sigil[1], path: [] },
|
|
883
|
+
read: access !== 'write',
|
|
884
|
+
});
|
|
885
|
+
} else if (!memberObject) {
|
|
886
|
+
// A bare value may escape into an alias or foreign code. Without
|
|
887
|
+
// data flow, do not claim its declaration fixes its later shape.
|
|
888
|
+
uncertain('escape');
|
|
889
|
+
}
|
|
890
|
+
}
|
|
784
891
|
return;
|
|
785
892
|
}
|
|
786
893
|
case 'Literal':
|
|
@@ -791,8 +898,29 @@ function walk(
|
|
|
791
898
|
case 'TemplateElement':
|
|
792
899
|
out.strings.push((node.value as { raw: string }).raw);
|
|
793
900
|
return;
|
|
901
|
+
case 'ChainExpression':
|
|
902
|
+
// Optional chaining can wrap a delete target. Keep its write role
|
|
903
|
+
// so the member records the mutation and any shape uncertainty.
|
|
904
|
+
sub(node.expression, binding, shorthand, access, memberObject);
|
|
905
|
+
return;
|
|
794
906
|
case 'MemberExpression':
|
|
795
|
-
|
|
907
|
+
if (effects && escapes && !memberObject && access === 'read')
|
|
908
|
+
uncertain('escape');
|
|
909
|
+
if (effects && !binding && access !== 'read') {
|
|
910
|
+
const { root, path } = mutationTarget(node, src);
|
|
911
|
+
const target = root ? { sigil: root[0], name: root[1], path } : null;
|
|
912
|
+
effects.mutations.push({
|
|
913
|
+
start: node.start,
|
|
914
|
+
end: node.end,
|
|
915
|
+
target,
|
|
916
|
+
read: access !== 'write',
|
|
917
|
+
});
|
|
918
|
+
if (!target) uncertain('indirect-write');
|
|
919
|
+
if (path.includes(null)) uncertain('computed-write');
|
|
920
|
+
if (path.some((key) => key === 'prototype' || key === '__proto__'))
|
|
921
|
+
uncertain('prototype-write');
|
|
922
|
+
}
|
|
923
|
+
sub(node.object, false, false, 'read', true);
|
|
796
924
|
if (node.computed) sub(node.property);
|
|
797
925
|
else propertyName(node.property, src);
|
|
798
926
|
return;
|
|
@@ -803,7 +931,7 @@ function walk(
|
|
|
803
931
|
const value = node.value as AnyNode;
|
|
804
932
|
if (value.type === 'AssignmentPattern') {
|
|
805
933
|
sub(value.left, binding, true, access);
|
|
806
|
-
sub(value.right);
|
|
934
|
+
sub(value.right, false, false, 'read', false, true);
|
|
807
935
|
} else {
|
|
808
936
|
sub(value, binding, true, access);
|
|
809
937
|
}
|
|
@@ -815,7 +943,14 @@ function walk(
|
|
|
815
943
|
case 'PropertyDefinition':
|
|
816
944
|
if (node.computed) sub(node.key);
|
|
817
945
|
else propertyName(node.key, src);
|
|
818
|
-
sub(
|
|
946
|
+
sub(
|
|
947
|
+
node.value,
|
|
948
|
+
false,
|
|
949
|
+
false,
|
|
950
|
+
'read',
|
|
951
|
+
false,
|
|
952
|
+
node.type === 'PropertyDefinition' || escapes,
|
|
953
|
+
);
|
|
819
954
|
return;
|
|
820
955
|
case 'LabeledStatement':
|
|
821
956
|
sub(node.body);
|
|
@@ -827,7 +962,7 @@ function walk(
|
|
|
827
962
|
return;
|
|
828
963
|
case 'VariableDeclarator':
|
|
829
964
|
sub(node.id, true);
|
|
830
|
-
sub(node.init);
|
|
965
|
+
sub(node.init, false, false, 'read', false, true);
|
|
831
966
|
return;
|
|
832
967
|
case 'FunctionDeclaration':
|
|
833
968
|
case 'FunctionExpression':
|
|
@@ -853,7 +988,12 @@ function walk(
|
|
|
853
988
|
false,
|
|
854
989
|
node.operator === '=' ? 'write' : 'readwrite',
|
|
855
990
|
);
|
|
856
|
-
sub(node.right);
|
|
991
|
+
sub(node.right, false, false, 'read', false, true);
|
|
992
|
+
return;
|
|
993
|
+
case 'ReturnStatement':
|
|
994
|
+
case 'ThrowStatement':
|
|
995
|
+
case 'YieldExpression':
|
|
996
|
+
sub(node.argument, false, false, 'read', false, true);
|
|
857
997
|
return;
|
|
858
998
|
case 'UnaryExpression':
|
|
859
999
|
sub(
|
|
@@ -869,12 +1009,12 @@ function walk(
|
|
|
869
1009
|
case 'ForInStatement':
|
|
870
1010
|
case 'ForOfStatement':
|
|
871
1011
|
sub(node.left, false, false, 'write');
|
|
872
|
-
sub(node.right);
|
|
1012
|
+
sub(node.right, false, false, 'read', false, true);
|
|
873
1013
|
sub(node.body);
|
|
874
1014
|
return;
|
|
875
1015
|
case 'AssignmentPattern':
|
|
876
1016
|
sub(node.left, binding, false, access);
|
|
877
|
-
sub(node.right);
|
|
1017
|
+
sub(node.right, false, false, 'read', false, true);
|
|
878
1018
|
return;
|
|
879
1019
|
case 'ObjectPattern':
|
|
880
1020
|
case 'ArrayPattern':
|
package/src/markup/ast.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { withSelectors, type Selectors, type VariableScope } from './tokens';
|
|
6
6
|
import type { PairedBranch, PairedNode } from './pair';
|
|
7
7
|
import { NameSet } from '../utils/macro-names';
|
|
8
|
-
import { getMacroParameters } from '../registry';
|
|
8
|
+
import { getMacro, getMacroParameters } from '../registry';
|
|
9
9
|
|
|
10
10
|
export interface TextNode {
|
|
11
11
|
type: 'text';
|
|
@@ -108,6 +108,11 @@ export function isBlockMacro(name: string): boolean {
|
|
|
108
108
|
return isBuiltinBlockMacro(name);
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
/** Explicit implementations can also be invoked outside a companion owner. */
|
|
112
|
+
export function isStandaloneMacro(name: string): boolean {
|
|
113
|
+
return WIDGETS.has(name) || getMacro(name) !== undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
111
116
|
/** Built-in defaults without any process-global registrations or widgets. */
|
|
112
117
|
export function isBuiltinBlockMacro(name: string): boolean {
|
|
113
118
|
return BLOCK_MACROS.has(name);
|
package/src/markup/pair.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import { branchParents, getMacroParameters } from '../registry';
|
|
14
14
|
import { sourcePositions } from './line-column';
|
|
15
|
-
import { isBlockMacro, isRawMacro } from './ast';
|
|
15
|
+
import { isBlockMacro, isRawMacro, isStandaloneMacro } from './ast';
|
|
16
16
|
import type { HtmlToken, MacroToken, Span, Token } from './tokens';
|
|
17
17
|
|
|
18
18
|
/** What is wrong with the pairing, by kind. */
|
|
@@ -82,6 +82,8 @@ export interface PairMarkupOptions {
|
|
|
82
82
|
isRaw?(name: string): boolean;
|
|
83
83
|
/** Parents declaring a branching sub-macro (default: registered metadata). */
|
|
84
84
|
branchParents?(name: string): readonly string[];
|
|
85
|
+
/** Whether a companion name also has a standalone macro/widget implementation. */
|
|
86
|
+
isStandalone?(name: string): boolean;
|
|
85
87
|
/**
|
|
86
88
|
* The markup the tokens are of: lets a message say at which line and column
|
|
87
89
|
* an element was opened (else it says at which offset).
|
|
@@ -152,6 +154,7 @@ export function pairMarkup(
|
|
|
152
154
|
): PairedMarkup {
|
|
153
155
|
const isBlock = options.isBlock ?? isBlockMacro;
|
|
154
156
|
const isRaw = options.isRaw ?? isRawMacro;
|
|
157
|
+
const isStandalone = options.isStandalone ?? isStandaloneMacro;
|
|
155
158
|
const parentsOf = (name: string): readonly string[] => {
|
|
156
159
|
if (options.branchParents) return options.branchParents(name);
|
|
157
160
|
const registered = branchParents(name);
|
|
@@ -291,7 +294,12 @@ export function pairMarkup(
|
|
|
291
294
|
if (
|
|
292
295
|
token.type === 'macro' &&
|
|
293
296
|
!token.isClose &&
|
|
294
|
-
parentsOf(token.name).length > 0
|
|
297
|
+
parentsOf(token.name).length > 0 &&
|
|
298
|
+
(!isStandalone(token.name) ||
|
|
299
|
+
(last(stack)?.node.token.type === 'macro' &&
|
|
300
|
+
parentsOf(token.name).includes(
|
|
301
|
+
lowerName(last(stack)!.node.token as MacroToken),
|
|
302
|
+
)))
|
|
295
303
|
) {
|
|
296
304
|
const tag = token as MacroToken;
|
|
297
305
|
const frame = last(stack);
|
package/src/markup/parse.ts
CHANGED
|
@@ -24,6 +24,7 @@ export interface MarkupHooks extends CodeEnd {
|
|
|
24
24
|
/** Whether a macro takes a body closed by `{/name}`. */
|
|
25
25
|
isBlock(name: string): boolean;
|
|
26
26
|
branchParents?(name: string): readonly string[];
|
|
27
|
+
isStandalone?(name: string): boolean;
|
|
27
28
|
/** Whether a macro's body is JavaScript, kept verbatim (`{do}`). */
|
|
28
29
|
isRaw(name: string): boolean;
|
|
29
30
|
/** Whether an attribute's value is code (`onclick`), not markup. */
|
|
@@ -93,6 +94,7 @@ function run(
|
|
|
93
94
|
startRule: 'Tokens' | 'SelectorsPrefix',
|
|
94
95
|
options: ParseMarkupOptions,
|
|
95
96
|
completion = false,
|
|
97
|
+
findDelimiter = indexDelimiters(source),
|
|
96
98
|
): unknown {
|
|
97
99
|
const hooks = { ...defaultHooks, ...options.hooks };
|
|
98
100
|
if (!options.hooks?.closeBrace) {
|
|
@@ -106,6 +108,7 @@ function run(
|
|
|
106
108
|
text: options.text === true,
|
|
107
109
|
hooks,
|
|
108
110
|
completion,
|
|
111
|
+
findDelimiter,
|
|
109
112
|
});
|
|
110
113
|
} catch (err) {
|
|
111
114
|
const { location, code } = err as {
|
|
@@ -128,6 +131,27 @@ function run(
|
|
|
128
131
|
}
|
|
129
132
|
}
|
|
130
133
|
|
|
134
|
+
/** Literal delimiter searches shared by suffix and prefix recovery attempts. */
|
|
135
|
+
function indexDelimiters(source: string) {
|
|
136
|
+
const positions = new Map<string, number[]>();
|
|
137
|
+
return (delimiter: string, from: number, end = source.length): number => {
|
|
138
|
+
let offsets = positions.get(delimiter);
|
|
139
|
+
if (!offsets) {
|
|
140
|
+
offsets = [];
|
|
141
|
+
for (
|
|
142
|
+
let at = source.indexOf(delimiter);
|
|
143
|
+
at >= 0;
|
|
144
|
+
at = source.indexOf(delimiter, at + 1)
|
|
145
|
+
) {
|
|
146
|
+
offsets.push(at);
|
|
147
|
+
}
|
|
148
|
+
positions.set(delimiter, offsets);
|
|
149
|
+
}
|
|
150
|
+
const at = offsets[upperBound(offsets, from - 1)];
|
|
151
|
+
return at !== undefined && at + delimiter.length <= end ? at : -1;
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
131
155
|
/** What tolerant parsing makes of markup (see analyzeMarkup). */
|
|
132
156
|
export interface MarkupAnalysis {
|
|
133
157
|
tokens: Token[];
|
|
@@ -156,6 +180,7 @@ export function analyzeMarkup(
|
|
|
156
180
|
isBlock: hooks.isBlock,
|
|
157
181
|
isRaw: hooks.isRaw,
|
|
158
182
|
branchParents: hooks.branchParents,
|
|
183
|
+
isStandalone: hooks.isStandalone,
|
|
159
184
|
source,
|
|
160
185
|
});
|
|
161
186
|
if (errors.length === 0 && paired.errors.length === 0) {
|
|
@@ -289,6 +314,7 @@ export function scanMarkupTolerant(
|
|
|
289
314
|
// Share the index across recovery attempts, whose inputs are suffixes or
|
|
290
315
|
// truncated prefixes. Re-indexing each attempt would itself be quadratic.
|
|
291
316
|
const codeEnd = indexCodeEnds(source);
|
|
317
|
+
const findDelimiter = indexDelimiters(source);
|
|
292
318
|
const customCloseBrace = options.hooks?.closeBrace;
|
|
293
319
|
const lastBrace = source.lastIndexOf('}');
|
|
294
320
|
/** `text` shifted by `base`, tokenized: the tokens or the error. */
|
|
@@ -309,7 +335,20 @@ export function scanMarkupTolerant(
|
|
|
309
335
|
};
|
|
310
336
|
try {
|
|
311
337
|
return (
|
|
312
|
-
run(
|
|
338
|
+
run(
|
|
339
|
+
text,
|
|
340
|
+
'Tokens',
|
|
341
|
+
{ ...options, hooks },
|
|
342
|
+
completion,
|
|
343
|
+
(delimiter, from) => {
|
|
344
|
+
const at = findDelimiter(
|
|
345
|
+
delimiter,
|
|
346
|
+
base + from,
|
|
347
|
+
base + text.length,
|
|
348
|
+
);
|
|
349
|
+
return at < 0 ? -1 : at - base;
|
|
350
|
+
},
|
|
351
|
+
) as Token[]
|
|
313
352
|
).map((token) => shiftOffsets(token, base));
|
|
314
353
|
} catch (error) {
|
|
315
354
|
if (!(error instanceof MarkupError)) throw error;
|
|
@@ -20,6 +20,8 @@ export interface ParseOptions {
|
|
|
20
20
|
text: boolean;
|
|
21
21
|
/** What the grammar leaves to code (see parse.ts MarkupHooks). */
|
|
22
22
|
hooks: unknown;
|
|
23
|
+
/** Indexed literal search, relative to this parse attempt. */
|
|
24
|
+
findDelimiter: (delimiter: string, from: number) => number;
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
/**
|
package/src/markup/spindle.peggy
CHANGED
|
@@ -133,9 +133,8 @@ const textMode = options.text === true;
|
|
|
133
133
|
// Only cursor-context parsing accepts EOF in place of an unfinished closer.
|
|
134
134
|
const completionMode = options.completion === true;
|
|
135
135
|
const hooks = options.hooks;
|
|
136
|
-
//
|
|
137
|
-
|
|
138
|
-
const lastCommentClose = input.lastIndexOf('-->');
|
|
136
|
+
// Indexed by parse.ts across recovery suffixes and truncated prefixes.
|
|
137
|
+
const findDelimiter = options.findDelimiter;
|
|
139
138
|
/** End of the stretch a `.|n|` repetition consumes (see skipTo). */
|
|
140
139
|
let skipEnd = -1;
|
|
141
140
|
/** Whether the attribute whose value is being read holds code. */
|
|
@@ -252,7 +251,7 @@ TextRun
|
|
|
252
251
|
* rendering drops it. (An unclosed `<!--` is ordinary text.)
|
|
253
252
|
*/
|
|
254
253
|
Comment
|
|
255
|
-
= value:$("<!--" (">" / "->" / &{ return completionMode || offset()
|
|
254
|
+
= value:$("<!--" (">" / "->" / &{ return completionMode || findDelimiter('-->', offset()) >= 0; } (!"-->" .)* ("-->" / OpenEnd))) {
|
|
256
255
|
const node = lexeme('text', location(), { value });
|
|
257
256
|
node.comment = true;
|
|
258
257
|
return node;
|
|
@@ -303,8 +302,8 @@ LinkLexeme
|
|
|
303
302
|
*/
|
|
304
303
|
LinkText
|
|
305
304
|
= start:Here &{
|
|
306
|
-
const close =
|
|
307
|
-
const nested =
|
|
305
|
+
const close = findDelimiter(']]', start);
|
|
306
|
+
const nested = findDelimiter('[[', start);
|
|
308
307
|
linkTextEnd = Math.min(close < 0 ? input.length : close, nested < 0 ? input.length : nested);
|
|
309
308
|
return true;
|
|
310
309
|
}
|
package/src/markup/validate.ts
CHANGED
|
@@ -220,6 +220,9 @@ export function validateMarkup(
|
|
|
220
220
|
ReturnType<typeof sourcePositions>
|
|
221
221
|
>();
|
|
222
222
|
const hooks = {
|
|
223
|
+
...(options.parametersOf?.pairing?.isStandalone && {
|
|
224
|
+
isStandalone: options.parametersOf.pairing.isStandalone,
|
|
225
|
+
}),
|
|
223
226
|
...(options.isBlockMacro && { isBlock: options.isBlockMacro }),
|
|
224
227
|
...(options.branchParents && { branchParents: options.branchParents }),
|
|
225
228
|
isRaw: (name: string) =>
|
package/src/tooling.ts
CHANGED
|
@@ -93,11 +93,15 @@ export {
|
|
|
93
93
|
lexJs,
|
|
94
94
|
lexTemplate,
|
|
95
95
|
parseCode,
|
|
96
|
+
analyzeCodeEffects,
|
|
96
97
|
scanStringLiteral,
|
|
97
98
|
} from './js-lexer';
|
|
98
99
|
export { indexStrictCodeEnds } from './markup/code-end';
|
|
99
100
|
export type {
|
|
100
101
|
ParsedCode,
|
|
102
|
+
CodeEffects,
|
|
103
|
+
CodeMutation,
|
|
104
|
+
CodeShapeUncertainty,
|
|
101
105
|
VariableRef,
|
|
102
106
|
FindCodeEndOptions,
|
|
103
107
|
JsGoal,
|
|
@@ -249,6 +253,7 @@ function tokenizationOptions(
|
|
|
249
253
|
isRawMacro(name, parametersOf.widget?.(name) !== undefined),
|
|
250
254
|
isBlock: blockLookup(macros),
|
|
251
255
|
branchParents: branchParentLookup(macros),
|
|
256
|
+
isStandalone: parametersOf.pairing!.isStandalone,
|
|
252
257
|
...options.hooks,
|
|
253
258
|
},
|
|
254
259
|
};
|