@rohal12/spindle 0.62.8 → 0.63.0
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 +321 -208
- package/dist/pkg/macro-registry.js +3 -1
- package/dist/pkg/macro-registry.json +3 -1
- package/dist/pkg/story-variables.js +357 -197
- package/dist/pkg/tooling.js +1 -0
- package/package.json +1 -1
- package/src/code-check.ts +83 -36
- package/src/components/macros/Computed.tsx +5 -3
- package/src/components/macros/Cycle.tsx +1 -0
- package/src/format/format-passage.ts +37 -1
- package/src/js-lexer.ts +180 -134
- package/src/markup/ast.ts +17 -9
- package/src/markup/expansion-budget.ts +23 -0
- package/src/markup/pair.ts +20 -1
- package/src/markup/parse.ts +2 -2
- package/src/markup/render.tsx +4 -4
- package/src/markup/validate.ts +13 -0
- package/src/observe/observe.ts +8 -3
- package/src/parser.ts +2 -2
- package/src/passage-roles.ts +19 -0
- package/src/registry.ts +3 -2
- package/src/store.ts +6 -23
- package/src/story-api.ts +1 -5
- package/src/story-variables.ts +49 -44
- package/src/tooling.ts +18 -11
- package/src/widgets/ast-scanner.ts +17 -13
- package/src/widgets/widget-def.ts +3 -0
- package/types/tooling.d.ts +12 -1
package/src/markup/ast.ts
CHANGED
|
@@ -68,8 +68,7 @@ const BLOCK_MACROS = new NameSet([
|
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
70
|
* The block widgets (those whose body renders `{@children}`), kept apart from
|
|
71
|
-
*
|
|
72
|
-
* definition is, whereas the macros in BLOCK_MACROS stay blocks.
|
|
71
|
+
* built-in defaults: a widget is a block only while its latest definition is.
|
|
73
72
|
*/
|
|
74
73
|
const BLOCK_WIDGETS = new NameSet();
|
|
75
74
|
/** All widgets, including inline ones, override built-in parsing semantics. */
|
|
@@ -77,8 +76,12 @@ const WIDGETS = new NameSet();
|
|
|
77
76
|
|
|
78
77
|
/** Classification owned by defineMacro, independent of explicit registrations. */
|
|
79
78
|
const DEFINED_BLOCK_MACROS = new NameSet();
|
|
79
|
+
const DEFINED_MACROS = new NameSet();
|
|
80
|
+
/** Explicit registrations remain independent of macro/widget replacements. */
|
|
81
|
+
const REGISTERED_BLOCK_MACROS = new NameSet();
|
|
80
82
|
|
|
81
83
|
export function setDefinedBlockMacro(name: string, isBlock: boolean): void {
|
|
84
|
+
DEFINED_MACROS.add(name);
|
|
82
85
|
setClassification(DEFINED_BLOCK_MACROS, name, isBlock);
|
|
83
86
|
}
|
|
84
87
|
|
|
@@ -98,16 +101,20 @@ export function isRawMacro(name: string, widget = WIDGETS.has(name)): boolean {
|
|
|
98
101
|
|
|
99
102
|
/** Whether a macro takes a body closed by `{/name}`. */
|
|
100
103
|
export function isBlockMacro(name: string): boolean {
|
|
101
|
-
return
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
if (REGISTERED_BLOCK_MACROS.has(name)) return true;
|
|
105
|
+
if (WIDGETS.has(name)) return BLOCK_WIDGETS.has(name);
|
|
106
|
+
if (DEFINED_MACROS.has(name)) return DEFINED_BLOCK_MACROS.has(name);
|
|
107
|
+
return BLOCK_MACROS.has(name);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Explicit parser registrations, unaffected by effective tooling metadata. */
|
|
111
|
+
export function isRegisteredBlockMacro(name: string): boolean {
|
|
112
|
+
return REGISTERED_BLOCK_MACROS.has(name);
|
|
106
113
|
}
|
|
107
114
|
|
|
108
115
|
/** Register a custom macro as a block macro so the parser nests children. */
|
|
109
116
|
export function registerBlockMacro(name: string): void {
|
|
110
|
-
|
|
117
|
+
REGISTERED_BLOCK_MACROS.add(name);
|
|
111
118
|
}
|
|
112
119
|
|
|
113
120
|
/**
|
|
@@ -127,8 +134,9 @@ export function clearBlockWidgets(): void {
|
|
|
127
134
|
|
|
128
135
|
/** Unregister a custom block macro (for test cleanup). */
|
|
129
136
|
export function unregisterBlockMacro(name: string): void {
|
|
130
|
-
|
|
137
|
+
REGISTERED_BLOCK_MACROS.delete(name);
|
|
131
138
|
DEFINED_BLOCK_MACROS.delete(name);
|
|
139
|
+
DEFINED_MACROS.delete(name);
|
|
132
140
|
}
|
|
133
141
|
|
|
134
142
|
/** Macros whose branches render as one: the node holds one per branch. */
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Shared bounds for reparsing markup inside labels, selectors and attributes. */
|
|
2
|
+
export class MarkupExpansionBudget {
|
|
3
|
+
static readonly maxDepth = 64;
|
|
4
|
+
private depth = 0;
|
|
5
|
+
private remaining: number;
|
|
6
|
+
|
|
7
|
+
constructor(sourceLength: number) {
|
|
8
|
+
// Bound total rescanning as well as call depth, including large labels.
|
|
9
|
+
this.remaining = Math.max(65_536, sourceLength * 8);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
enter(length: number): boolean {
|
|
13
|
+
if (this.depth >= MarkupExpansionBudget.maxDepth || length > this.remaining)
|
|
14
|
+
return false;
|
|
15
|
+
this.remaining -= length;
|
|
16
|
+
this.depth++;
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
leave(): void {
|
|
21
|
+
this.depth--;
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/markup/pair.ts
CHANGED
|
@@ -17,7 +17,14 @@ import type { HtmlToken, MacroToken, Span, Token } from './tokens';
|
|
|
17
17
|
|
|
18
18
|
/** What is wrong with the pairing, by kind. */
|
|
19
19
|
export type PairingErrorCode =
|
|
20
|
-
'unclosed-block'
|
|
20
|
+
| 'unclosed-block'
|
|
21
|
+
| 'mismatched-closer'
|
|
22
|
+
| 'stray-closer'
|
|
23
|
+
| 'misplaced-branch'
|
|
24
|
+
| 'nesting-limit';
|
|
25
|
+
|
|
26
|
+
/** Keep AST construction and recursive rendering within a safe call depth. */
|
|
27
|
+
const MAX_MARKUP_DEPTH = 256;
|
|
21
28
|
|
|
22
29
|
/**
|
|
23
30
|
* A problem in the pairing of the tokens, at the token from `start` up to
|
|
@@ -322,6 +329,18 @@ export function pairMarkup(
|
|
|
322
329
|
add(node);
|
|
323
330
|
continue;
|
|
324
331
|
}
|
|
332
|
+
if (stack.length === MAX_MARKUP_DEPTH) {
|
|
333
|
+
report(
|
|
334
|
+
'nesting-limit',
|
|
335
|
+
token,
|
|
336
|
+
`Markup nesting exceeds the limit of ${MAX_MARKUP_DEPTH} elements`,
|
|
337
|
+
token.start,
|
|
338
|
+
{
|
|
339
|
+
name: nameOf(token as MacroToken | HtmlToken),
|
|
340
|
+
limit: String(MAX_MARKUP_DEPTH),
|
|
341
|
+
},
|
|
342
|
+
);
|
|
343
|
+
}
|
|
325
344
|
const body: PairedBody = { children: [], branches: [] };
|
|
326
345
|
node.body = body;
|
|
327
346
|
const key = tagKey(token as MacroToken | HtmlToken);
|
package/src/markup/parse.ts
CHANGED
|
@@ -321,7 +321,7 @@ export function scanMarkupTolerant(
|
|
|
321
321
|
const rest = source.slice(base);
|
|
322
322
|
const result = attempt(rest, base);
|
|
323
323
|
if (Array.isArray(result)) {
|
|
324
|
-
tokens.push(
|
|
324
|
+
for (const token of result) tokens.push(token);
|
|
325
325
|
break;
|
|
326
326
|
}
|
|
327
327
|
const at = base + result.offset;
|
|
@@ -345,7 +345,7 @@ export function scanMarkupTolerant(
|
|
|
345
345
|
cut = prefix.offset;
|
|
346
346
|
prefix = attempt(rest.slice(0, cut), base);
|
|
347
347
|
}
|
|
348
|
-
tokens.push(
|
|
348
|
+
for (const token of prefix) tokens.push(token);
|
|
349
349
|
damage.push(base + cut);
|
|
350
350
|
// The character that started the damage is text
|
|
351
351
|
if (cut < rest.length) {
|
package/src/markup/render.tsx
CHANGED
|
@@ -62,7 +62,7 @@ export const StructuralContext = createContext(false);
|
|
|
62
62
|
/**
|
|
63
63
|
* True while rendering inside an element whose content is not markdown: SVG
|
|
64
64
|
* (whose namespace `<p>` wrappers would break) and the preformatted `<pre>`
|
|
65
|
-
* `<textarea>` and `<
|
|
65
|
+
* `<textarea>`, `<style>` and `<script>`, whose text is literal.
|
|
66
66
|
* Macro and widget bodies read it so their content stays literal too.
|
|
67
67
|
*/
|
|
68
68
|
export const RawTextContext = createContext(false);
|
|
@@ -113,7 +113,7 @@ const STRUCTURAL_ELEMENTS = new Set([
|
|
|
113
113
|
]);
|
|
114
114
|
|
|
115
115
|
/** Elements whose content is literal text, not markdown. */
|
|
116
|
-
const PREFORMATTED_ELEMENTS = new Set(['pre', 'textarea', 'style']);
|
|
116
|
+
const PREFORMATTED_ELEMENTS = new Set(['pre', 'textarea', 'style', 'script']);
|
|
117
117
|
export const WidgetChildrenContext = createContext<WidgetChildren | null>(null);
|
|
118
118
|
|
|
119
119
|
/**
|
|
@@ -774,8 +774,8 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
774
774
|
let children: preact.ComponentChildren = undefined;
|
|
775
775
|
let structural = false;
|
|
776
776
|
if (node.children.length > 0) {
|
|
777
|
-
if (tag === 'style') {
|
|
778
|
-
//
|
|
777
|
+
if (tag === 'style' || tag === 'script') {
|
|
778
|
+
// Raw-text elements keep script text and character-reference lookalikes
|
|
779
779
|
// (`?a=1¬ch=2`) as written (#453)
|
|
780
780
|
children = node.children.map((n) =>
|
|
781
781
|
n.type === 'text' ? n.value : renderSingleNode(n),
|
package/src/markup/validate.ts
CHANGED
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
import { CodeSyntaxError } from '../js-lexer';
|
|
26
26
|
import { sourcePositions } from './line-column';
|
|
27
27
|
import { isRawMacro } from './ast';
|
|
28
|
+
import { isSpecialPassage } from '../passage-roles';
|
|
28
29
|
|
|
29
30
|
/** A passage to validate. */
|
|
30
31
|
export interface MarkupPassage {
|
|
@@ -48,6 +49,7 @@ export type MarkupDiagnosticCode =
|
|
|
48
49
|
| 'argument-error'
|
|
49
50
|
| 'code-syntax'
|
|
50
51
|
| 'unknown-passage'
|
|
52
|
+
| 'special-passage'
|
|
51
53
|
| 'unquoted-passage-name';
|
|
52
54
|
|
|
53
55
|
/** What a diagnostic names, by code (see docs/tooling.md). */
|
|
@@ -371,6 +373,17 @@ export function validateMarkup(
|
|
|
371
373
|
);
|
|
372
374
|
break;
|
|
373
375
|
case 'passage':
|
|
376
|
+
if (piece.navigation && isSpecialPassage(piece.name)) {
|
|
377
|
+
report(
|
|
378
|
+
passage,
|
|
379
|
+
at,
|
|
380
|
+
at + piece.length,
|
|
381
|
+
'special-passage',
|
|
382
|
+
`${where}Cannot navigate to special passage ${JSON.stringify(piece.name)} in ${piece.label}.`,
|
|
383
|
+
{ name: piece.name, macro: piece.macro },
|
|
384
|
+
);
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
374
387
|
if (
|
|
375
388
|
options.checkPassageNames !== false &&
|
|
376
389
|
!passageNames.has(piece.name)
|
package/src/observe/observe.ts
CHANGED
|
@@ -168,9 +168,14 @@ export function observe(): Observation {
|
|
|
168
168
|
passage,
|
|
169
169
|
text: body ? observableText(body, DIALOG) : '',
|
|
170
170
|
}));
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
171
|
+
// The persistent interface can show errors outside the passage. Scan its
|
|
172
|
+
// shared root once, then read dialogs and the hidden StoryContext separately.
|
|
173
|
+
const errors = [
|
|
174
|
+
document.getElementById('root') ?? passageEl,
|
|
175
|
+
...bodies.map((d) => d.body),
|
|
176
|
+
contextEl,
|
|
177
|
+
].flatMap((el) =>
|
|
178
|
+
el ? observableErrors(el, DIALOG, el === contextEl ? el : undefined) : [],
|
|
174
179
|
);
|
|
175
180
|
// The runtime errors shown (as banners) until dismissed, with what failed
|
|
176
181
|
for (const { context, message } of getRuntimeErrors()) {
|
package/src/parser.ts
CHANGED
|
@@ -136,12 +136,12 @@ const escapeTextReferences = (text: string) =>
|
|
|
136
136
|
|
|
137
137
|
/**
|
|
138
138
|
* Elements whose text the renderer shows as written, with no character
|
|
139
|
-
* references decoded:
|
|
139
|
+
* references decoded: those it treats as raw text (`style`, `script`, see
|
|
140
140
|
* HtmlNodeRenderer), for which the browser did not decode any either.
|
|
141
141
|
*/
|
|
142
142
|
function isLiteralTextElement(parent: ParentNode): boolean {
|
|
143
143
|
return (
|
|
144
|
-
(parent as Element).localName
|
|
144
|
+
['style', 'script'].includes((parent as Element).localName) &&
|
|
145
145
|
(parent as Element).namespaceURI === HTML_NAMESPACE
|
|
146
146
|
);
|
|
147
147
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Runtime-owned passage roles, shared with stateless authoring tools. */
|
|
2
|
+
const SPECIAL_PASSAGES = new Set([
|
|
3
|
+
'StoryInit',
|
|
4
|
+
'StoryInterface',
|
|
5
|
+
'StoryVariables',
|
|
6
|
+
'StoryTransients',
|
|
7
|
+
'StoryLoading',
|
|
8
|
+
'SaveTitle',
|
|
9
|
+
'PassageReady',
|
|
10
|
+
'PassageHeader',
|
|
11
|
+
'PassageFooter',
|
|
12
|
+
'PassageDone',
|
|
13
|
+
'StoryContext',
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
/** Whether `name` is a special passage, which navigation never goes to. */
|
|
17
|
+
export function isSpecialPassage(name: string): boolean {
|
|
18
|
+
return SPECIAL_PASSAGES.has(name);
|
|
19
|
+
}
|
package/src/registry.ts
CHANGED
|
@@ -254,20 +254,21 @@ export function registerMacroMetadata(
|
|
|
254
254
|
}
|
|
255
255
|
}
|
|
256
256
|
|
|
257
|
-
/** The parents declaring a branching companion (
|
|
257
|
+
/** The parents declaring a branching companion (input options are values). */
|
|
258
258
|
export function branchParents(
|
|
259
259
|
name: string,
|
|
260
260
|
macros?: readonly Pick<MacroMetadata, 'name' | 'subMacros'>[],
|
|
261
261
|
): string[] {
|
|
262
262
|
if (!macros) {
|
|
263
263
|
return [...(companionParents.get(name) ?? [])].filter(
|
|
264
|
-
(parent) => parent !== 'listbox',
|
|
264
|
+
(parent) => parent !== 'listbox' && parent !== 'cycle',
|
|
265
265
|
);
|
|
266
266
|
}
|
|
267
267
|
return effectiveNamedDefinitions(macros)
|
|
268
268
|
.filter(
|
|
269
269
|
(macro) =>
|
|
270
270
|
macro.name.toLowerCase() !== 'listbox' &&
|
|
271
|
+
macro.name.toLowerCase() !== 'cycle' &&
|
|
271
272
|
macro.subMacros.some((sub) => sub.toLowerCase() === name.toLowerCase()),
|
|
272
273
|
)
|
|
273
274
|
.map((macro) => macro.name.toLowerCase());
|
package/src/store.ts
CHANGED
|
@@ -64,7 +64,7 @@ import {
|
|
|
64
64
|
underChange,
|
|
65
65
|
type PathChange,
|
|
66
66
|
} from './structural';
|
|
67
|
-
import { getByPath } from './utils/object-path';
|
|
67
|
+
import { getByPath, setByPath } from './utils/object-path';
|
|
68
68
|
import { isIndexKey } from './utils/value-kinds';
|
|
69
69
|
import {
|
|
70
70
|
noPassageError,
|
|
@@ -95,31 +95,14 @@ import {
|
|
|
95
95
|
type Counts,
|
|
96
96
|
type Namespace,
|
|
97
97
|
} from './utils/namespace';
|
|
98
|
+
import { isSpecialPassage } from './passage-roles';
|
|
99
|
+
export { isSpecialPassage } from './passage-roles';
|
|
98
100
|
|
|
99
101
|
enablePatches();
|
|
100
102
|
// Story state holds Map and Set values: Immer must be able to draft them
|
|
101
103
|
// when a write (a dot path, a macro binding) reaches one
|
|
102
104
|
enableMapSet();
|
|
103
105
|
|
|
104
|
-
const SPECIAL_PASSAGES = new Set([
|
|
105
|
-
'StoryInit',
|
|
106
|
-
'StoryInterface',
|
|
107
|
-
'StoryVariables',
|
|
108
|
-
'StoryTransients',
|
|
109
|
-
'StoryLoading',
|
|
110
|
-
'SaveTitle',
|
|
111
|
-
'PassageReady',
|
|
112
|
-
'PassageHeader',
|
|
113
|
-
'PassageFooter',
|
|
114
|
-
'PassageDone',
|
|
115
|
-
'StoryContext',
|
|
116
|
-
]);
|
|
117
|
-
|
|
118
|
-
/** Whether `name` is a special passage, which navigation never goes to. */
|
|
119
|
-
export function isSpecialPassage(name: string): boolean {
|
|
120
|
-
return SPECIAL_PASSAGES.has(name);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
106
|
// ---------------------------------------------------------------------------
|
|
124
107
|
// Patch-based variable history (module-level, outside Zustand)
|
|
125
108
|
// ---------------------------------------------------------------------------
|
|
@@ -1515,14 +1498,14 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1515
1498
|
setVariable: (name: string, value: unknown) => {
|
|
1516
1499
|
set((state) => {
|
|
1517
1500
|
checkVariableName(name, `$${name}`);
|
|
1518
|
-
state.variables[name]
|
|
1501
|
+
setByPath(state.variables, [name], value);
|
|
1519
1502
|
});
|
|
1520
1503
|
},
|
|
1521
1504
|
|
|
1522
1505
|
setTemporary: (name: string, value: unknown) => {
|
|
1523
1506
|
set((state) => {
|
|
1524
1507
|
checkVariableName(name, `_${name}`);
|
|
1525
|
-
state.temporary[name]
|
|
1508
|
+
setByPath(state.temporary, [name], value);
|
|
1526
1509
|
});
|
|
1527
1510
|
},
|
|
1528
1511
|
|
|
@@ -1543,7 +1526,7 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1543
1526
|
setTransient: (name: string, value: unknown) => {
|
|
1544
1527
|
set((state) => {
|
|
1545
1528
|
checkVariableName(name, `%${name}`);
|
|
1546
|
-
state.transient[name]
|
|
1529
|
+
setByPath(state.transient, [name], value);
|
|
1547
1530
|
});
|
|
1548
1531
|
},
|
|
1549
1532
|
|
package/src/story-api.ts
CHANGED
|
@@ -275,11 +275,7 @@ function setOne(draft: VariableNamespaces, name: string, value: unknown): void {
|
|
|
275
275
|
const { isTransient, key } = parseName(name);
|
|
276
276
|
const namespace = isTransient ? draft.transient : draft.variables;
|
|
277
277
|
|
|
278
|
-
|
|
279
|
-
setByPath(namespace, key.split('.'), value);
|
|
280
|
-
} else {
|
|
281
|
-
namespace[key] = value;
|
|
282
|
-
}
|
|
278
|
+
setByPath(namespace, key.split('.'), value);
|
|
283
279
|
}
|
|
284
280
|
|
|
285
281
|
/** A method calling the store action `name`, as the store holds it then. */
|
package/src/story-variables.ts
CHANGED
|
@@ -11,8 +11,10 @@ import { errorMessage } from './utils/error-message';
|
|
|
11
11
|
import {
|
|
12
12
|
CodeSyntaxError,
|
|
13
13
|
lexJs,
|
|
14
|
+
readMemberPath,
|
|
14
15
|
scanStringLiteral,
|
|
15
16
|
type JsGoal,
|
|
17
|
+
type VariableRef,
|
|
16
18
|
} from './js-lexer';
|
|
17
19
|
import {
|
|
18
20
|
argPieces,
|
|
@@ -42,6 +44,7 @@ import {
|
|
|
42
44
|
} from './utils/namespace';
|
|
43
45
|
import { staticLiteral, type LiteralShape } from './static-literal';
|
|
44
46
|
import { parse } from 'acorn';
|
|
47
|
+
import { MarkupExpansionBudget } from './markup/expansion-budget';
|
|
45
48
|
|
|
46
49
|
/**
|
|
47
50
|
* The tokens of markup, or none if it is malformed (validateMarkup reports
|
|
@@ -88,9 +91,6 @@ function declarationRegex(sigil: string): RegExp {
|
|
|
88
91
|
const escaped = sigil.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
89
92
|
return new RegExp(`^${escaped}(\\w+)\\s*=\\s*(.+)$`);
|
|
90
93
|
}
|
|
91
|
-
/** A `$name` or `%name` reference with its dot path, at a sigil the lexer found. */
|
|
92
|
-
const VAR_PATH_RE = /([$%])(\w+(?:\.\w+)*)/y;
|
|
93
|
-
|
|
94
94
|
const VALID_VAR_TYPES = new Set<string>(['number', 'string', 'boolean']);
|
|
95
95
|
|
|
96
96
|
/** Boxed sample values whose members a primitive of each type can access. */
|
|
@@ -503,6 +503,7 @@ const BUILTIN_STORE_VAR_MACROS: readonly string[] = [
|
|
|
503
503
|
|
|
504
504
|
/** What reading the variable references of markup depends on. */
|
|
505
505
|
interface ReferenceOptions {
|
|
506
|
+
expansionBudget?: MarkupExpansionBudget;
|
|
506
507
|
/** The (lowercase) names of the input macros that bind a variable. */
|
|
507
508
|
storeVarMacros: ReadonlySet<string>;
|
|
508
509
|
parametersOf: ParametersOf;
|
|
@@ -524,9 +525,6 @@ type Emit = (ref: VariableReference) => void;
|
|
|
524
525
|
|
|
525
526
|
const NO_STORE_VAR_MACROS: ReadonlySet<string> = new Set();
|
|
526
527
|
|
|
527
|
-
/** A `.field` path written right after a reference, which may be empty. */
|
|
528
|
-
const FIELD_PATH_RE = /(?:\.\w+)*/y;
|
|
529
|
-
|
|
530
528
|
/**
|
|
531
529
|
* The reference to variable `name`, which JavaScript reads at `[start, end)`
|
|
532
530
|
* of `code` (the sigil and the name as written, which may be spelled with
|
|
@@ -534,39 +532,21 @@ const FIELD_PATH_RE = /(?:\.\w+)*/y;
|
|
|
534
532
|
*/
|
|
535
533
|
function referenceAt(
|
|
536
534
|
code: string,
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
535
|
+
{
|
|
536
|
+
sigil,
|
|
537
|
+
name,
|
|
538
|
+
start,
|
|
539
|
+
end,
|
|
540
|
+
}: Pick<VariableRef, 'sigil' | 'name' | 'start' | 'end'>,
|
|
541
541
|
at: At,
|
|
542
542
|
): VariableReference {
|
|
543
|
-
|
|
544
|
-
const fields = FIELD_PATH_RE.exec(code)![0];
|
|
543
|
+
const fields = readMemberPath(code, end);
|
|
545
544
|
return {
|
|
546
545
|
sigil: sigil as '$' | '%',
|
|
547
546
|
name,
|
|
548
|
-
path: fields
|
|
547
|
+
path: fields.path,
|
|
549
548
|
start: at(start),
|
|
550
|
-
end: at(
|
|
551
|
-
};
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
/** A reference written at `index` of `term`, a `variable` parameter as written. */
|
|
555
|
-
function termReferenceAt(
|
|
556
|
-
term: string,
|
|
557
|
-
index: number,
|
|
558
|
-
at: At,
|
|
559
|
-
): VariableReference | undefined {
|
|
560
|
-
VAR_PATH_RE.lastIndex = index;
|
|
561
|
-
const match = VAR_PATH_RE.exec(term);
|
|
562
|
-
if (!match) return undefined;
|
|
563
|
-
const [name, ...path] = match[2]!.split('.');
|
|
564
|
-
return {
|
|
565
|
-
sigil: match[1] as '$' | '%',
|
|
566
|
-
name: name!,
|
|
567
|
-
path,
|
|
568
|
-
start: at(index),
|
|
569
|
-
end: at(index + match[0].length),
|
|
549
|
+
end: at(fields.end),
|
|
570
550
|
};
|
|
571
551
|
}
|
|
572
552
|
|
|
@@ -594,7 +574,24 @@ function scanInterpolations(
|
|
|
594
574
|
options: ReferenceOptions,
|
|
595
575
|
): void {
|
|
596
576
|
if (!text.includes('{')) return;
|
|
597
|
-
|
|
577
|
+
const budget = options.expansionBudget!;
|
|
578
|
+
if (!budget.enter(text.length)) return;
|
|
579
|
+
try {
|
|
580
|
+
const offset = at(0);
|
|
581
|
+
const sourceOffsets =
|
|
582
|
+
at(text.length) - offset === text.length
|
|
583
|
+
? undefined
|
|
584
|
+
: Array.from({ length: text.length + 1 }, (_, i) => at(i));
|
|
585
|
+
collectTokenRefs(
|
|
586
|
+
text,
|
|
587
|
+
tokensOf(text, true, options),
|
|
588
|
+
(i) => sourceOffsets?.[i] ?? offset + i,
|
|
589
|
+
emit,
|
|
590
|
+
options,
|
|
591
|
+
);
|
|
592
|
+
} finally {
|
|
593
|
+
budget.leave();
|
|
594
|
+
}
|
|
598
595
|
}
|
|
599
596
|
|
|
600
597
|
/**
|
|
@@ -618,7 +615,7 @@ function scanCode(
|
|
|
618
615
|
}
|
|
619
616
|
for (const ref of parsed.refs) {
|
|
620
617
|
if (ref.sigil !== '$' && ref.sigil !== '%') continue;
|
|
621
|
-
emit(referenceAt(code, ref
|
|
618
|
+
emit(referenceAt(code, ref, at));
|
|
622
619
|
}
|
|
623
620
|
}
|
|
624
621
|
|
|
@@ -634,7 +631,7 @@ function scanCodeLeniently(
|
|
|
634
631
|
{
|
|
635
632
|
variable(sigil, name, index, _nesting, end) {
|
|
636
633
|
if (sigil !== '$' && sigil !== '%') return;
|
|
637
|
-
emit(referenceAt(code, sigil, name, index, end, at));
|
|
634
|
+
emit(referenceAt(code, { sigil, name, start: index, end }, at));
|
|
638
635
|
},
|
|
639
636
|
// Strings, regex literals and comments hold no references
|
|
640
637
|
literal() {},
|
|
@@ -682,8 +679,8 @@ function scanArgs(
|
|
|
682
679
|
const params = parametersOf(token.name);
|
|
683
680
|
let byParameters = false;
|
|
684
681
|
if (params) {
|
|
685
|
-
if (
|
|
686
|
-
scanVariableParameters(token, params, at, emit);
|
|
682
|
+
if (!options.storeVarMacros.has(token.name.toLowerCase())) {
|
|
683
|
+
scanVariableParameters(token, params, at, emit, options.all);
|
|
687
684
|
}
|
|
688
685
|
const pieces = [
|
|
689
686
|
...argPieces(token.rawArgs, token.argsStart, params, token.name, ''),
|
|
@@ -709,14 +706,15 @@ function scanArgs(
|
|
|
709
706
|
|
|
710
707
|
/**
|
|
711
708
|
* Report the variables the `variable` parameters of a macro name
|
|
712
|
-
* (`{unset $x}`, `{computed $x = …}`)
|
|
713
|
-
*
|
|
709
|
+
* (`{unset $x}`, `{computed $x = …}`). Computed members read variables even
|
|
710
|
+
* when the receiver itself is only included in the complete editor list.
|
|
714
711
|
*/
|
|
715
712
|
function scanVariableParameters(
|
|
716
713
|
token: Extract<Token, { type: 'macro' }>,
|
|
717
714
|
params: readonly ParameterDef[],
|
|
718
715
|
at: At,
|
|
719
716
|
emit: Emit,
|
|
717
|
+
all: boolean | undefined,
|
|
720
718
|
): void {
|
|
721
719
|
const spans: ArgSpans = new Map();
|
|
722
720
|
try {
|
|
@@ -728,10 +726,14 @@ function scanVariableParameters(
|
|
|
728
726
|
for (const param of params) {
|
|
729
727
|
const span = spans.get(param);
|
|
730
728
|
if (param.type !== 'variable' || !span) continue;
|
|
731
|
-
const
|
|
732
|
-
|
|
729
|
+
const start = token.argsStart + span[0];
|
|
730
|
+
scanCode(
|
|
731
|
+
token.rawArgs.slice(span[0], span[1]),
|
|
732
|
+
(i) => at(start + i),
|
|
733
|
+
(ref) => {
|
|
734
|
+
if (all || ref.start !== at(start)) emit(ref);
|
|
735
|
+
},
|
|
733
736
|
);
|
|
734
|
-
if (ref) emit(ref);
|
|
735
737
|
}
|
|
736
738
|
}
|
|
737
739
|
|
|
@@ -908,7 +910,10 @@ export function collectVariableReferences(
|
|
|
908
910
|
tokensOf(content, false, options),
|
|
909
911
|
(i) => i,
|
|
910
912
|
(ref) => found.push(ref),
|
|
911
|
-
|
|
913
|
+
{
|
|
914
|
+
...options,
|
|
915
|
+
expansionBudget: new MarkupExpansionBudget(content.length),
|
|
916
|
+
},
|
|
912
917
|
),
|
|
913
918
|
);
|
|
914
919
|
// The scan visits a macro's arguments before its selectors, and bindings
|
package/src/tooling.ts
CHANGED
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
type MarkupDiagnostic,
|
|
17
17
|
type MarkupPassage,
|
|
18
18
|
} from './markup/validate';
|
|
19
|
-
import { isBlockMacro, isRawMacro } from './markup/ast';
|
|
19
|
+
import { isBlockMacro, isRawMacro, isRegisteredBlockMacro } from './markup/ast';
|
|
20
20
|
import {
|
|
21
21
|
tokenizeMarkup as tokenizeRawMarkup,
|
|
22
22
|
tokenizeMarkupTolerant as tokenizeRawMarkupTolerant,
|
|
@@ -122,6 +122,7 @@ export type {
|
|
|
122
122
|
PairMarkupOptions,
|
|
123
123
|
} from './markup/pair';
|
|
124
124
|
export { isBlockMacro } from './markup/ast';
|
|
125
|
+
export { isSpecialPassage } from './passage-roles';
|
|
125
126
|
export type { MarkupErrorCode } from './markup/parse';
|
|
126
127
|
export {
|
|
127
128
|
endsWithOperator,
|
|
@@ -271,9 +272,6 @@ export function validateStoryMarkup(
|
|
|
271
272
|
const widgets = effectiveWidgetDefinitions(
|
|
272
273
|
readWidgetDefinitions(list, parameterLookup(all)),
|
|
273
274
|
);
|
|
274
|
-
const blocks = new Set(
|
|
275
|
-
widgets.filter((def) => def.block).map((def) => def.name.toLowerCase()),
|
|
276
|
-
);
|
|
277
275
|
const parametersOf = parameterLookup([
|
|
278
276
|
...all,
|
|
279
277
|
...definedWidgetMacros(widgets),
|
|
@@ -282,15 +280,21 @@ export function validateStoryMarkup(
|
|
|
282
280
|
const name = macro.name.toLowerCase();
|
|
283
281
|
known.add(name);
|
|
284
282
|
for (const sub of macro.subMacros) known.add(sub.toLowerCase());
|
|
285
|
-
if (macro.block) blocks.add(name);
|
|
286
283
|
}
|
|
287
284
|
return validateMarkup(list, {
|
|
288
285
|
branchParents: (name) => branchParents(name, all),
|
|
289
286
|
isKnownMacro: (name) => known.has(name),
|
|
290
287
|
macroNames: known,
|
|
291
288
|
parametersOf,
|
|
292
|
-
isBlockMacro: (
|
|
293
|
-
|
|
289
|
+
isBlockMacro: blockLookup([
|
|
290
|
+
...all,
|
|
291
|
+
...widgets.map((w) => ({
|
|
292
|
+
name: w.name,
|
|
293
|
+
block: w.block,
|
|
294
|
+
subMacros: [],
|
|
295
|
+
widget: w,
|
|
296
|
+
})),
|
|
297
|
+
]),
|
|
294
298
|
checkPassageNames: options.checkPassageNames,
|
|
295
299
|
tolerant: options.tolerant,
|
|
296
300
|
});
|
|
@@ -303,10 +307,13 @@ function definedWidgetMacros(widgets: readonly WidgetDefinition[]) {
|
|
|
303
307
|
|
|
304
308
|
/** Whether a macro takes a body, among `macros` and the built-in ones. */
|
|
305
309
|
function blockLookup(macros: readonly ToolingMacro[]) {
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
+
const definitions = new NameMap<boolean>();
|
|
311
|
+
for (const macro of effectiveToolingMacros(macros)) {
|
|
312
|
+
definitions.set(macro.name, macro.block);
|
|
313
|
+
}
|
|
314
|
+
return (name: string) =>
|
|
315
|
+
isRegisteredBlockMacro(name) ||
|
|
316
|
+
(definitions.get(name) ?? isBlockMacro(name));
|
|
310
317
|
}
|
|
311
318
|
|
|
312
319
|
/**
|