@rohal12/spindle 0.59.29 → 0.59.30
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/README.md +1 -0
- package/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +560 -272
- package/dist/pkg/macro-registry.js +19 -0
- package/dist/pkg/macro-registry.json +19 -0
- package/dist/pkg/story-variables.js +494 -90
- package/package.json +1 -1
- package/src/action-registry.ts +59 -5
- package/src/automation/load-yaml.ts +111 -20
- package/src/automation/runner.ts +15 -13
- package/src/automation/types.ts +7 -5
- package/src/class-registry.ts +8 -5
- package/src/code-check.ts +87 -20
- package/src/components/macros/Checkbox.tsx +5 -2
- package/src/components/macros/Cycle.tsx +1 -0
- package/src/components/macros/For.tsx +1 -7
- package/src/components/macros/Listbox.tsx +1 -0
- package/src/components/macros/MenubarButtons.tsx +6 -1
- package/src/components/macros/Nobr.tsx +1 -0
- package/src/components/macros/PassageDisplay.tsx +42 -0
- package/src/components/macros/Radiobutton.tsx +1 -0
- package/src/components/macros/SaveManager.tsx +19 -4
- package/src/components/macros/SettingsControls.tsx +19 -4
- package/src/components/macros/Span.tsx +1 -0
- package/src/components/macros/StoryTitle.tsx +1 -0
- package/src/components/macros/VarDisplay.tsx +2 -3
- package/src/components/macros/WidgetInvocation.tsx +5 -6
- package/src/components/macros/input-macro.ts +1 -0
- package/src/components/macros/locals-scope.tsx +19 -8
- package/src/components/macros/macro-args.ts +11 -3
- package/src/expression.ts +34 -18
- package/src/hooks/use-merged-locals.ts +17 -2
- package/src/hooks/use-modal-focus.ts +190 -129
- package/src/index.tsx +22 -12
- package/src/js-bindings.ts +332 -0
- package/src/js-lexer.ts +12 -3
- package/src/macro-discovery.ts +25 -22
- package/src/static-literal.ts +87 -16
- package/src/store.ts +4 -1
- package/src/story-api.ts +13 -4
- package/src/story-variables.ts +91 -28
- package/src/structural.ts +19 -15
- package/src/tooling.ts +21 -8
- package/src/transform.ts +22 -7
- package/src/utils/namespace.ts +4 -0
- package/src/utils/object-path.ts +2 -2
- package/src/utils/stable-key.ts +17 -6
- package/src/utils/value-kinds.ts +38 -1
- package/types/index.d.ts +12 -5
- package/types/tooling.d.ts +12 -2
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which declaration a variable names where it is used in JavaScript, read
|
|
3
|
+
* from the text without running it (see discoverMacros): the scope the use is
|
|
4
|
+
* in, the declarations of the name that scope can see, and the innermost one.
|
|
5
|
+
*
|
|
6
|
+
* It reads the shape of the code (brackets, and the words before and after a
|
|
7
|
+
* name) in text whose strings, comments and regexes are blanked, so it needs
|
|
8
|
+
* no syntax tree and goes on through code that is not complete. What it
|
|
9
|
+
* cannot place, it counts as a declaration of unknown value: a parameter, a
|
|
10
|
+
* pattern, an assignment. Better no answer than another declaration's.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
type Kind = '{' | '(' | '[';
|
|
14
|
+
|
|
15
|
+
interface Group {
|
|
16
|
+
kind: Kind;
|
|
17
|
+
open: number;
|
|
18
|
+
/** The closing bracket, or the end of the text if there is none. */
|
|
19
|
+
close: number;
|
|
20
|
+
parent: Group | undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The text a name is visible in: `[start, end)`. */
|
|
24
|
+
interface Scope {
|
|
25
|
+
start: number;
|
|
26
|
+
end: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface Binding {
|
|
30
|
+
scope: Scope;
|
|
31
|
+
/** Where the name is declared. */
|
|
32
|
+
at: number;
|
|
33
|
+
/** Whether it must come before a use in the same function. */
|
|
34
|
+
ordered: boolean;
|
|
35
|
+
/** The `{` of the object literal it is declared as, if it is. */
|
|
36
|
+
object?: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const CLOSERS: Record<string, Kind> = { '}': '{', ')': '(', ']': '[' };
|
|
40
|
+
/** What `(…) {` is when a word like this is before the `(`: a block. */
|
|
41
|
+
const BLOCK_WORDS = new Set(['if', 'for', 'while', 'switch', 'with', 'catch']);
|
|
42
|
+
const NOT_PARAMETERS = new Set(['if', 'for', 'while', 'switch', 'with']);
|
|
43
|
+
const DECLARATIONS = new Set(['const', 'let', 'var']);
|
|
44
|
+
/** Before a name in a pattern or parameter list, by the group around it. */
|
|
45
|
+
const BEFORE_BINDING: Record<Kind, string> = {
|
|
46
|
+
'{': '{,:',
|
|
47
|
+
'[': '[,',
|
|
48
|
+
'(': '(,',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const isWordChar = (c: string | undefined) => !!c && /[\w$]/.test(c);
|
|
52
|
+
|
|
53
|
+
/** Resolve the declaration of variables in `code` (see blankedCode in macro-discovery). */
|
|
54
|
+
export class Bindings {
|
|
55
|
+
private readonly groups: Group[] = [];
|
|
56
|
+
private readonly byOpen = new Map<number, Group>();
|
|
57
|
+
private readonly byClose = new Map<number, Group>();
|
|
58
|
+
private readonly functions = new Map<Group, boolean>();
|
|
59
|
+
private readonly top: Scope = { start: -1, end: Infinity };
|
|
60
|
+
|
|
61
|
+
constructor(private readonly code: string) {
|
|
62
|
+
this.readGroups();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The `{` of the object literal that `name`, used at `at`, is declared as
|
|
67
|
+
* (`const`, `let` or `var`), when that is the declaration it names: the
|
|
68
|
+
* innermost one in the scope of the use, that comes before it unless the
|
|
69
|
+
* use is in a function of its own. `undefined` if it names another value,
|
|
70
|
+
* or more than one declaration is the innermost.
|
|
71
|
+
*/
|
|
72
|
+
objectOf(name: string, at: number): number | undefined {
|
|
73
|
+
const { code } = this;
|
|
74
|
+
const escaped = name.replace(/\$/g, '\\$');
|
|
75
|
+
const bindings: Binding[] = [];
|
|
76
|
+
for (const m of code.matchAll(
|
|
77
|
+
new RegExp(`(?<![\\w$])${escaped}(?![\\w$])`, 'g'),
|
|
78
|
+
)) {
|
|
79
|
+
const binding = this.bindingAt(m.index, name.length);
|
|
80
|
+
if (binding) bindings.push(binding);
|
|
81
|
+
}
|
|
82
|
+
const useFunction = this.functionStart(at);
|
|
83
|
+
const visible = bindings.filter(
|
|
84
|
+
(b) =>
|
|
85
|
+
b.scope.start <= at &&
|
|
86
|
+
at < b.scope.end &&
|
|
87
|
+
(!b.ordered || b.at < at || this.functionStart(b.at) !== useFunction),
|
|
88
|
+
);
|
|
89
|
+
const innermost = Math.max(...visible.map((b) => b.scope.start));
|
|
90
|
+
const found = visible.filter((b) => b.scope.start === innermost);
|
|
91
|
+
return found.length === 1 ? found[0]!.object : undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// -- the brackets ---------------------------------------------------------
|
|
95
|
+
|
|
96
|
+
private readGroups(): void {
|
|
97
|
+
const { code, groups } = this;
|
|
98
|
+
const stack: Group[] = [];
|
|
99
|
+
for (let i = 0; i < code.length; i++) {
|
|
100
|
+
const c = code.charAt(i);
|
|
101
|
+
if (c === '{' || c === '(' || c === '[') {
|
|
102
|
+
const group: Group = {
|
|
103
|
+
kind: c,
|
|
104
|
+
open: i,
|
|
105
|
+
close: code.length,
|
|
106
|
+
parent: stack[stack.length - 1],
|
|
107
|
+
};
|
|
108
|
+
groups.push(group);
|
|
109
|
+
stack.push(group);
|
|
110
|
+
this.byOpen.set(i, group);
|
|
111
|
+
} else if (CLOSERS[c]) {
|
|
112
|
+
// Tolerate a closer with no opener: the innermost of its kind closes
|
|
113
|
+
let k = stack.length - 1;
|
|
114
|
+
while (k >= 0 && stack[k]!.kind !== CLOSERS[c]) k--;
|
|
115
|
+
if (k >= 0) {
|
|
116
|
+
stack[k]!.close = i;
|
|
117
|
+
this.byClose.set(i, stack[k]!);
|
|
118
|
+
stack.length = k;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The innermost group that has `index` inside it, of this kind if given. */
|
|
125
|
+
private innermost(index: number, kind?: Kind): Group | undefined {
|
|
126
|
+
const { groups } = this;
|
|
127
|
+
for (let k = groups.length - 1; k >= 0; k--) {
|
|
128
|
+
const g = groups[k]!;
|
|
129
|
+
if (g.open < index && index < g.close && (!kind || g.kind === kind)) {
|
|
130
|
+
return g;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Whether the `{` group is the body of a function (or method). */
|
|
137
|
+
private isFunctionBody(group: Group): boolean {
|
|
138
|
+
if (group.kind !== '{') return false;
|
|
139
|
+
let known = this.functions.get(group);
|
|
140
|
+
if (known === undefined) {
|
|
141
|
+
const before = this.backFrom(group.open);
|
|
142
|
+
const { code } = this;
|
|
143
|
+
if (code.charAt(before) === ')') {
|
|
144
|
+
const params = this.byClose.get(before);
|
|
145
|
+
const word = params && this.wordBefore(this.backFrom(params.open) + 1);
|
|
146
|
+
known = !!params && !BLOCK_WORDS.has(word!.text);
|
|
147
|
+
} else known = code.slice(before - 1, before + 1) === '=>';
|
|
148
|
+
this.functions.set(group, known);
|
|
149
|
+
}
|
|
150
|
+
return known;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The start of the function that has `index` in its body, or -1. */
|
|
154
|
+
private functionStart(index: number): number {
|
|
155
|
+
for (let g = this.innermost(index, '{'); g; g = g.parent) {
|
|
156
|
+
if (g.kind === '{' && this.isFunctionBody(g)) return g.open;
|
|
157
|
+
}
|
|
158
|
+
return -1;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// -- the scopes -----------------------------------------------------------
|
|
162
|
+
|
|
163
|
+
private scopeOf(group: Group | undefined): Scope {
|
|
164
|
+
return group ? { start: group.open, end: group.close } : this.top;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** The block `index` is in: what `let` and `const` declare for. */
|
|
168
|
+
private blockAt(index: number): Scope {
|
|
169
|
+
return this.scopeOf(this.innermost(index, '{'));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** The function (or the whole text) `index` is in: what `var` declares for. */
|
|
173
|
+
private functionAt(index: number): Scope {
|
|
174
|
+
const start = this.functionStart(index);
|
|
175
|
+
return start < 0 ? this.top : this.scopeOf(this.byOpen.get(start));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** What the declaration keyword `word` (`var`, `let`, `const`) declares for. */
|
|
179
|
+
private declaredIn(word: { text: string; start: number }): Scope {
|
|
180
|
+
return word.text === 'var'
|
|
181
|
+
? this.functionAt(word.start)
|
|
182
|
+
: this.blockAt(word.start);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** What a parameter list, whose `)` is `close`, is the scope of: the body. */
|
|
186
|
+
private bodyAfter(close: number): Scope | undefined {
|
|
187
|
+
const next = this.forward(close + 1);
|
|
188
|
+
if (this.code.charAt(next) === '{')
|
|
189
|
+
return this.scopeOf(this.byOpen.get(next));
|
|
190
|
+
return this.code.startsWith('=>', next)
|
|
191
|
+
? this.arrowBody(next + 2)
|
|
192
|
+
: undefined;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** The body of an arrow function, whose `=>` ends before `from`. */
|
|
196
|
+
private arrowBody(from: number): Scope {
|
|
197
|
+
const { code } = this;
|
|
198
|
+
const start = this.forward(from);
|
|
199
|
+
if (code.charAt(start) === '{') return this.scopeOf(this.byOpen.get(start));
|
|
200
|
+
let end = start;
|
|
201
|
+
while (end < code.length && !',;)]}'.includes(code.charAt(end))) {
|
|
202
|
+
end = this.byOpen.get(end)?.close ?? end;
|
|
203
|
+
end++;
|
|
204
|
+
}
|
|
205
|
+
return { start, end };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// -- the text -------------------------------------------------------------
|
|
209
|
+
|
|
210
|
+
/** The index of the last character that is no space before `index`, or -1. */
|
|
211
|
+
private backFrom(index: number): number {
|
|
212
|
+
let i = index - 1;
|
|
213
|
+
while (i >= 0 && /\s/.test(this.code.charAt(i))) i--;
|
|
214
|
+
return i;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The index of the first character that is no space from `index` on. */
|
|
218
|
+
private forward(index: number): number {
|
|
219
|
+
let i = index;
|
|
220
|
+
while (i < this.code.length && /\s/.test(this.code.charAt(i))) i++;
|
|
221
|
+
return i;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** The word that ends before `end`, and where it starts. */
|
|
225
|
+
private wordBefore(end: number): { text: string; start: number } {
|
|
226
|
+
let start = end;
|
|
227
|
+
while (start > 0 && isWordChar(this.code.charAt(start - 1))) start--;
|
|
228
|
+
return { text: this.code.slice(start, end), start };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// -- the declarations -----------------------------------------------------
|
|
232
|
+
|
|
233
|
+
/** What the name of `length` characters at `at` declares, if it does. */
|
|
234
|
+
private bindingAt(at: number, length: number): Binding | undefined {
|
|
235
|
+
const { code } = this;
|
|
236
|
+
let before = this.backFrom(at);
|
|
237
|
+
const spread = code.slice(before - 2, before + 1) === '...';
|
|
238
|
+
if (code.charAt(before) === '.' && !spread) return undefined; // a property
|
|
239
|
+
if (spread) before = this.backFrom(before - 2);
|
|
240
|
+
const after = this.forward(at + length);
|
|
241
|
+
return (
|
|
242
|
+
this.declarationAt(at, before, spread) ??
|
|
243
|
+
this.patternAt(at, before, after) ??
|
|
244
|
+
this.arrowParameterAt(at, after) ??
|
|
245
|
+
this.assignmentAt(at, before, after)
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** `const x`, `let x`, `var x`, `function x`, `class x`. */
|
|
250
|
+
private declarationAt(
|
|
251
|
+
at: number,
|
|
252
|
+
before: number,
|
|
253
|
+
spread: boolean,
|
|
254
|
+
): Binding | undefined {
|
|
255
|
+
const word = spread ? undefined : this.wordBefore(before + 1);
|
|
256
|
+
if (!word || this.code.charAt(word.start - 1) === '.') return undefined;
|
|
257
|
+
if (DECLARATIONS.has(word.text)) {
|
|
258
|
+
const scope = this.declaredIn(word);
|
|
259
|
+
const init = /\s*=\s*(?=\{)/y;
|
|
260
|
+
init.lastIndex = this.nameEnd(at);
|
|
261
|
+
const object = init.test(this.code) ? init.lastIndex : undefined;
|
|
262
|
+
return { scope, at, ordered: true, object };
|
|
263
|
+
}
|
|
264
|
+
if (word.text === 'function' || word.text === 'class') {
|
|
265
|
+
return {
|
|
266
|
+
scope: this.blockAt(word.start),
|
|
267
|
+
at,
|
|
268
|
+
ordered: word.text === 'class',
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private nameEnd(at: number): number {
|
|
275
|
+
let end = at;
|
|
276
|
+
while (isWordChar(this.code.charAt(end))) end++;
|
|
277
|
+
return end;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** A name in a pattern (`const { x } = …`) or a parameter list. */
|
|
281
|
+
private patternAt(
|
|
282
|
+
at: number,
|
|
283
|
+
before: number,
|
|
284
|
+
after: number,
|
|
285
|
+
): Binding | undefined {
|
|
286
|
+
const { code } = this;
|
|
287
|
+
let group = this.innermost(at);
|
|
288
|
+
if (!group || !BEFORE_BINDING[group.kind].includes(code.charAt(before))) {
|
|
289
|
+
return undefined;
|
|
290
|
+
}
|
|
291
|
+
if (group.kind === '{' && code.charAt(after) === ':') return undefined; // a key
|
|
292
|
+
for (; group; group = group.parent) {
|
|
293
|
+
if (group.kind === '(') {
|
|
294
|
+
const word = this.wordBefore(this.backFrom(group.open) + 1).text;
|
|
295
|
+
const scope = NOT_PARAMETERS.has(word)
|
|
296
|
+
? undefined
|
|
297
|
+
: this.bodyAfter(group.close);
|
|
298
|
+
return scope && { scope, at, ordered: false };
|
|
299
|
+
}
|
|
300
|
+
const open = this.backFrom(group.open);
|
|
301
|
+
const word = this.wordBefore(open + 1);
|
|
302
|
+
if (DECLARATIONS.has(word.text)) {
|
|
303
|
+
return { scope: this.declaredIn(word), at, ordered: true };
|
|
304
|
+
}
|
|
305
|
+
// Inside another pattern or list
|
|
306
|
+
if (!'({[,:'.includes(code.charAt(open))) return undefined;
|
|
307
|
+
}
|
|
308
|
+
return undefined;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** `x => …`. */
|
|
312
|
+
private arrowParameterAt(at: number, after: number): Binding | undefined {
|
|
313
|
+
return this.code.startsWith('=>', after)
|
|
314
|
+
? { scope: this.arrowBody(after + 2), at, ordered: false }
|
|
315
|
+
: undefined;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** `x = …`: a declaration in a list, or an assignment: a value not known. */
|
|
319
|
+
private assignmentAt(
|
|
320
|
+
at: number,
|
|
321
|
+
before: number,
|
|
322
|
+
after: number,
|
|
323
|
+
): Binding | undefined {
|
|
324
|
+
const { code } = this;
|
|
325
|
+
if (code.charAt(after) !== '=' || '=>'.includes(code.charAt(after + 1))) {
|
|
326
|
+
return undefined;
|
|
327
|
+
}
|
|
328
|
+
const scope =
|
|
329
|
+
code.charAt(before) === ',' ? this.functionAt(at) : this.blockAt(at);
|
|
330
|
+
return { scope, at, ordered: true };
|
|
331
|
+
}
|
|
332
|
+
}
|
package/src/js-lexer.ts
CHANGED
|
@@ -53,9 +53,17 @@ export interface JsLexHandlers {
|
|
|
53
53
|
literal?(text: string, index: number, nesting: number): void;
|
|
54
54
|
/**
|
|
55
55
|
* A sigil variable reference — `$name`, `_name`, `@name` or `%name` —
|
|
56
|
-
* covering the sigil and `name
|
|
56
|
+
* covering the sigil and `name`, which is the name JavaScript reads (one
|
|
57
|
+
* written with an escape, `$fo\u006f`, is `foo`). It is written from
|
|
58
|
+
* `index` up to `end` (the sigil and the name as written).
|
|
57
59
|
*/
|
|
58
|
-
variable?(
|
|
60
|
+
variable?(
|
|
61
|
+
sigil: Sigil,
|
|
62
|
+
name: string,
|
|
63
|
+
index: number,
|
|
64
|
+
nesting: number,
|
|
65
|
+
end: number,
|
|
66
|
+
): void;
|
|
59
67
|
}
|
|
60
68
|
|
|
61
69
|
/**
|
|
@@ -1055,9 +1063,10 @@ function walkTokens(
|
|
|
1055
1063
|
if (sigil && !property) {
|
|
1056
1064
|
handlers.variable?.(
|
|
1057
1065
|
sigil,
|
|
1058
|
-
|
|
1066
|
+
(tok.value as string).slice(1),
|
|
1059
1067
|
tok.start,
|
|
1060
1068
|
nesting,
|
|
1069
|
+
tok.end,
|
|
1061
1070
|
);
|
|
1062
1071
|
} else code(tok.start, tok.end);
|
|
1063
1072
|
} else if (
|
package/src/macro-discovery.ts
CHANGED
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
* without running it (see discoverMacros): the literal parts of the config
|
|
4
4
|
* object, and nothing else.
|
|
5
5
|
*/
|
|
6
|
-
import { lexJs } from './js-lexer';
|
|
7
6
|
import {
|
|
8
7
|
checkParameterTypes,
|
|
9
8
|
type ParameterDef,
|
|
10
9
|
type ParameterType,
|
|
11
10
|
type StringHolds,
|
|
12
11
|
} from './registry';
|
|
13
|
-
import {
|
|
12
|
+
import { Bindings } from './js-bindings';
|
|
13
|
+
import { Reader, regexLiterals } from './static-literal';
|
|
14
14
|
import type { ToolingMacro } from './tooling';
|
|
15
15
|
|
|
16
16
|
/** A macro that a `defineMacro` call declares, and where its name is written. */
|
|
@@ -158,19 +158,21 @@ class ConfigReader extends Reader {
|
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
-
/**
|
|
162
|
-
|
|
161
|
+
/**
|
|
162
|
+
* The text with everything but code (strings, comments, regexes) blanked, and
|
|
163
|
+
* where its regex literals are.
|
|
164
|
+
*/
|
|
165
|
+
function codeOnly(source: string): {
|
|
166
|
+
code: string;
|
|
167
|
+
regexes: Map<number, number>;
|
|
168
|
+
} {
|
|
163
169
|
const chars = Array.from({ length: source.length }, () => ' ');
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
code(ch, index) {
|
|
168
|
-
for (let k = 0; k < ch.length; k++) chars[index + k] = ch[k]!;
|
|
169
|
-
},
|
|
170
|
+
const regexes = regexLiterals(source, 'statements', {
|
|
171
|
+
code(ch, index) {
|
|
172
|
+
for (let k = 0; k < ch.length; k++) chars[index + k] = ch[k]!;
|
|
170
173
|
},
|
|
171
|
-
|
|
172
|
-
);
|
|
173
|
-
return chars.join('');
|
|
174
|
+
});
|
|
175
|
+
return { code: chars.join(''), regexes };
|
|
174
176
|
}
|
|
175
177
|
|
|
176
178
|
const stringOf = (d: Datum | undefined) =>
|
|
@@ -265,8 +267,9 @@ function macroOf(config: Datum): DiscoveredMacro | undefined {
|
|
|
265
267
|
* `source` (JavaScript: a script passage, the body of a `{do}`, a project
|
|
266
268
|
* file) declare, in source order, read from the text without running it. A
|
|
267
269
|
* call whose config is an object literal (or a variable declared as one with
|
|
268
|
-
* `const`, `let` or `var`
|
|
269
|
-
*
|
|
270
|
+
* `const`, `let` or `var` that the call sees, see Bindings: not a parameter, a
|
|
271
|
+
* pattern or an assignment of the name) and whose `name` is a string is found,
|
|
272
|
+
* wherever it is, even inside a function. Only what is written out is read: a `block`,
|
|
270
273
|
* `interpolate`, `storeVar` or `merged` that is a boolean literal, the
|
|
271
274
|
* strings of `subMacros`, and `parameters` when every one is an object
|
|
272
275
|
* literal with a string `name` and `type` (and `holds`, `required`,
|
|
@@ -276,20 +279,20 @@ function macroOf(config: Datum): DiscoveredMacro | undefined {
|
|
|
276
279
|
* throws for none.
|
|
277
280
|
*/
|
|
278
281
|
export function discoverMacros(source: string): DiscoveredMacro[] {
|
|
279
|
-
const code = codeOnly(source);
|
|
282
|
+
const { code, regexes } = codeOnly(source);
|
|
280
283
|
const found: DiscoveredMacro[] = [];
|
|
284
|
+
let bindings: Bindings | undefined;
|
|
281
285
|
for (const call of code.matchAll(CALL_RE)) {
|
|
282
286
|
let at = call.index + call[0].length;
|
|
283
287
|
if (code.charAt(at) !== '{') {
|
|
284
288
|
const variable = IDENTIFIER_ARGUMENT_RE.exec(code.slice(at))?.[1];
|
|
285
289
|
if (!variable) continue;
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
at = declaration.index + declaration[0].length;
|
|
290
|
+
bindings ??= new Bindings(code);
|
|
291
|
+
const object = bindings.objectOf(variable, call.index);
|
|
292
|
+
if (object === undefined) continue;
|
|
293
|
+
at = object;
|
|
291
294
|
}
|
|
292
|
-
const reader = new ConfigReader(source);
|
|
295
|
+
const reader = new ConfigReader(source, { goal: 'statements', regexes });
|
|
293
296
|
reader.i = at;
|
|
294
297
|
const config = reader.datum();
|
|
295
298
|
const macro = config && macroOf(config);
|
package/src/static-literal.ts
CHANGED
|
@@ -7,7 +7,13 @@
|
|
|
7
7
|
* an operator, a reference, a spread) is "not static", which is no error: the
|
|
8
8
|
* value is simply not known without running the code.
|
|
9
9
|
*/
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
lexJs,
|
|
12
|
+
scanStringLiteral,
|
|
13
|
+
stringLiteralValue,
|
|
14
|
+
type JsGoal,
|
|
15
|
+
type JsLexHandlers,
|
|
16
|
+
} from './js-lexer';
|
|
11
17
|
|
|
12
18
|
/** The type of a variable by its default (see story-variables.ts VarType). */
|
|
13
19
|
type LiteralType =
|
|
@@ -45,11 +51,13 @@ export type StaticLiteral =
|
|
|
45
51
|
type Members = Map<string, LiteralShape | UnsupportedLiteral | null>;
|
|
46
52
|
|
|
47
53
|
const NUMBER_RE =
|
|
48
|
-
/(?:0[xX][\da-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][+-]?\d
|
|
54
|
+
/(?:0[xX][\da-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][+-]?\d[\d_]*)?)(n?)/y;
|
|
49
55
|
const IDENT_RE = /[A-Za-z_$][\w$]*/y;
|
|
50
56
|
const KEY_RE = /[\w$]+/y;
|
|
51
57
|
const ARROW_RE =
|
|
52
58
|
/(?:async\s+)?(?:\((?:[^()]|\([^()]*\))*\)|[A-Za-z_$][\w$]*)\s*=>/y;
|
|
59
|
+
/** The end of a line: the four line terminators of JavaScript. */
|
|
60
|
+
const LINE_END_RE = /[\n\r\u2028\u2029]/g;
|
|
53
61
|
const ARRAY_INDEX_RE = /^(?:0|[1-9]\d{0,8})$/;
|
|
54
62
|
|
|
55
63
|
const UNSUPPORTED_UNDEFINED: UnsupportedLiteral = {
|
|
@@ -78,6 +86,34 @@ function entryOrder(keys: Iterable<string>): string[] {
|
|
|
78
86
|
return [...indices, ...all.filter((key) => !ARRAY_INDEX_RE.test(key))];
|
|
79
87
|
}
|
|
80
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Where the regex literals in `src` are, by their start: JavaScript text
|
|
91
|
+
* cannot tell a regex from a division without the code before it, which the
|
|
92
|
+
* lexer reads. `handlers` see the same walk.
|
|
93
|
+
*/
|
|
94
|
+
export function regexLiterals(
|
|
95
|
+
src: string,
|
|
96
|
+
goal: JsGoal = 'expression',
|
|
97
|
+
handlers: JsLexHandlers = {},
|
|
98
|
+
): Map<number, number> {
|
|
99
|
+
const ends = new Map<number, number>();
|
|
100
|
+
lexJs(
|
|
101
|
+
src,
|
|
102
|
+
{
|
|
103
|
+
...handlers,
|
|
104
|
+
literal(text, index, nesting) {
|
|
105
|
+
// A comment is `//` or `/*`, which no regex starts with
|
|
106
|
+
if (text.charAt(0) === '/' && !/^\/[/*]/.test(text)) {
|
|
107
|
+
ends.set(index, index + text.length);
|
|
108
|
+
}
|
|
109
|
+
handlers.literal?.(text, index, nesting);
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
goal,
|
|
113
|
+
);
|
|
114
|
+
return ends;
|
|
115
|
+
}
|
|
116
|
+
|
|
81
117
|
/** `String(value)` of a BigInt literal's digits, or `undefined` if they are no BigInt. */
|
|
82
118
|
function bigintValue(digits: string): string | undefined {
|
|
83
119
|
try {
|
|
@@ -93,7 +129,20 @@ function bigintValue(digits: string): string | undefined {
|
|
|
93
129
|
*/
|
|
94
130
|
export class Reader {
|
|
95
131
|
i = 0;
|
|
96
|
-
|
|
132
|
+
private regexes: Map<number, number> | undefined;
|
|
133
|
+
private readonly goal: JsGoal;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* `regexes` (see regexLiterals) are read from `src` when they are first
|
|
137
|
+
* needed, unless the caller has them already.
|
|
138
|
+
*/
|
|
139
|
+
constructor(
|
|
140
|
+
protected readonly src: string,
|
|
141
|
+
options: { goal?: JsGoal; regexes?: Map<number, number> } = {},
|
|
142
|
+
) {
|
|
143
|
+
this.goal = options.goal ?? 'expression';
|
|
144
|
+
this.regexes = options.regexes;
|
|
145
|
+
}
|
|
97
146
|
|
|
98
147
|
/** Skip whitespace and comments. */
|
|
99
148
|
space(): void {
|
|
@@ -101,8 +150,8 @@ export class Reader {
|
|
|
101
150
|
for (;;) {
|
|
102
151
|
while (/\s/.test(src.charAt(this.i))) this.i++;
|
|
103
152
|
if (src.startsWith('//', this.i)) {
|
|
104
|
-
|
|
105
|
-
this.i =
|
|
153
|
+
LINE_END_RE.lastIndex = this.i;
|
|
154
|
+
this.i = LINE_END_RE.test(src) ? LINE_END_RE.lastIndex - 1 : src.length;
|
|
106
155
|
} else if (src.startsWith('/*', this.i)) {
|
|
107
156
|
const end = src.indexOf('*/', this.i + 2);
|
|
108
157
|
this.i = end < 0 ? src.length : end + 2;
|
|
@@ -170,10 +219,9 @@ export class Reader {
|
|
|
170
219
|
i++;
|
|
171
220
|
while (/\s/.test(src.charAt(i))) i++;
|
|
172
221
|
}
|
|
173
|
-
|
|
174
|
-
const match = NUMBER_RE.exec(src);
|
|
222
|
+
const match = this.numberAt(i);
|
|
175
223
|
if (match) {
|
|
176
|
-
this.i =
|
|
224
|
+
this.i = i + match[0].length;
|
|
177
225
|
if (!match[1]) return { shape: { type: 'number' } };
|
|
178
226
|
// A BigInt: `+1n` throws, `-1n` and `1n` are values no variable can hold
|
|
179
227
|
if (sign === '+') {
|
|
@@ -189,13 +237,19 @@ export class Reader {
|
|
|
189
237
|
}
|
|
190
238
|
IDENT_RE.lastIndex = i;
|
|
191
239
|
const word = IDENT_RE.exec(src)?.[0];
|
|
192
|
-
if (word === 'Infinity') {
|
|
240
|
+
if (word === 'Infinity' || word === 'NaN') {
|
|
193
241
|
this.i = IDENT_RE.lastIndex;
|
|
194
242
|
return { shape: { type: 'number' } };
|
|
195
243
|
}
|
|
196
244
|
return undefined;
|
|
197
245
|
}
|
|
198
246
|
|
|
247
|
+
/** The numeric literal (a BigInt's `n` is `match[1]`) at `at`, if there is one. */
|
|
248
|
+
private numberAt(at: number): RegExpExecArray | null {
|
|
249
|
+
NUMBER_RE.lastIndex = at;
|
|
250
|
+
return NUMBER_RE.exec(this.src);
|
|
251
|
+
}
|
|
252
|
+
|
|
199
253
|
/** A keyword, a function, or a name (which is not static). */
|
|
200
254
|
protected word(): StaticLiteral {
|
|
201
255
|
const { src } = this;
|
|
@@ -216,6 +270,7 @@ export class Reader {
|
|
|
216
270
|
this.i = end;
|
|
217
271
|
return { shape: { type: 'null' } };
|
|
218
272
|
case 'NaN':
|
|
273
|
+
case 'Infinity':
|
|
219
274
|
this.i = end;
|
|
220
275
|
return { shape: { type: 'number' } };
|
|
221
276
|
case 'undefined':
|
|
@@ -381,17 +436,24 @@ export class Reader {
|
|
|
381
436
|
const { src } = this;
|
|
382
437
|
const c = src.charAt(this.i);
|
|
383
438
|
if (c === '"' || c === "'") return this.stringValue();
|
|
439
|
+
if (/[\d.]/.test(c)) return this.numberKey();
|
|
384
440
|
KEY_RE.lastIndex = this.i;
|
|
385
441
|
const found = KEY_RE.exec(src)?.[0];
|
|
386
442
|
if (found === undefined) return undefined;
|
|
387
443
|
this.i = KEY_RE.lastIndex;
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
444
|
+
return found;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** A number as a key: the whole literal, named as the runtime names it. */
|
|
448
|
+
private numberKey(): string | undefined {
|
|
449
|
+
const match = this.numberAt(this.i);
|
|
450
|
+
if (!match) return undefined;
|
|
451
|
+
this.i += match[0].length;
|
|
452
|
+
// `1a` is no number
|
|
453
|
+
if (/[\w$]/.test(this.src.charAt(this.i))) return undefined;
|
|
454
|
+
const digits = match[0].replace(/_/g, '');
|
|
455
|
+
if (/^0\d/.test(digits)) return undefined; // legacy octal
|
|
456
|
+
if (match[1]) return bigintValue(digits.slice(0, -1));
|
|
395
457
|
const value = Number(digits);
|
|
396
458
|
return Number.isNaN(value) ? undefined : String(value);
|
|
397
459
|
}
|
|
@@ -433,6 +495,15 @@ export class Reader {
|
|
|
433
495
|
this.space();
|
|
434
496
|
return true;
|
|
435
497
|
}
|
|
498
|
+
if (c === '/') {
|
|
499
|
+
// A regex literal, whose brackets and quotes are no JavaScript structure
|
|
500
|
+
this.regexes ??= regexLiterals(src, this.goal);
|
|
501
|
+
const end = this.regexes.get(this.i);
|
|
502
|
+
if (end !== undefined) {
|
|
503
|
+
this.i = end;
|
|
504
|
+
return true;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
436
507
|
if (c === '[' || c === '{' || c === '(') return this.skipBalanced();
|
|
437
508
|
if (c === ']' || c === '}' || c === ')') return false;
|
|
438
509
|
this.i++;
|
package/src/store.ts
CHANGED
|
@@ -61,6 +61,7 @@ import {
|
|
|
61
61
|
type PathChange,
|
|
62
62
|
} from './structural';
|
|
63
63
|
import { getByPath } from './utils/object-path';
|
|
64
|
+
import { isIndexKey } from './utils/value-kinds';
|
|
64
65
|
import { noPassageError, showRuntimeError } from './runtime-errors';
|
|
65
66
|
import {
|
|
66
67
|
snapshotPRNG,
|
|
@@ -865,7 +866,9 @@ function writeKey(
|
|
|
865
866
|
change?: PathChange,
|
|
866
867
|
): void {
|
|
867
868
|
const deleted = change?.deleted ?? false;
|
|
868
|
-
|
|
869
|
+
// The elements of an array and its length; its other keys are the fields of
|
|
870
|
+
// a subclass instance (#490)
|
|
871
|
+
if (Array.isArray(holder) && (key === 'length' || isIndexKey(key))) {
|
|
869
872
|
const index = Number(key);
|
|
870
873
|
if (deleted) {
|
|
871
874
|
// A hole stays where it was; anything else was cut off
|