@rohal12/spindle 0.59.25 → 0.59.27
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 +592 -341
- package/dist/pkg/story-variables.js +654 -170
- package/dist/pkg/tooling.js +1 -0
- package/package.json +23 -24
- package/src/automation/load-yaml.ts +1 -1
- package/src/components/macros/MacroError.tsx +1 -1
- package/src/components/macros/SaveManager.tsx +2 -2
- package/src/components/macros/macro-args.ts +1 -2
- package/src/execute-mutation.ts +4 -3
- package/src/hooks/use-modal-focus.ts +1 -1
- package/src/hooks/use-story-fields.ts +1 -1
- package/src/macro-discovery.ts +299 -0
- package/src/markup/ast.ts +1 -5
- package/src/markup/code-attributes.ts +1 -3
- package/src/markup/code-end.ts +1 -1
- package/src/markup/line-column.ts +1 -1
- package/src/markup/pair.ts +1 -4
- package/src/markup/render.tsx +10 -9
- package/src/markup/validate.ts +27 -6
- package/src/static-literal.ts +223 -56
- package/src/store.ts +1 -1
- package/src/story-variables.ts +134 -26
- package/src/tooling.ts +28 -5
- package/src/types-drift-check.ts +17 -0
- package/src/widgets/ast-scanner.ts +4 -1
- package/src/widgets/widget-def.ts +5 -4
- package/types/index.d.ts +2 -10
- package/types/tooling.d.ts +56 -21
package/src/static-literal.ts
CHANGED
|
@@ -7,16 +7,11 @@
|
|
|
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 { scanStringLiteral } from './js-lexer';
|
|
10
|
+
import { scanStringLiteral, stringLiteralValue } from './js-lexer';
|
|
11
11
|
|
|
12
12
|
/** The type of a variable by its default (see story-variables.ts VarType). */
|
|
13
13
|
type LiteralType =
|
|
14
|
-
| '
|
|
15
|
-
| 'string'
|
|
16
|
-
| 'boolean'
|
|
17
|
-
| 'array'
|
|
18
|
-
| 'object'
|
|
19
|
-
| 'null';
|
|
14
|
+
'number' | 'string' | 'boolean' | 'array' | 'object' | 'null';
|
|
20
15
|
|
|
21
16
|
/** The shape of a literal (see story-variables.ts FieldSchema). */
|
|
22
17
|
export interface LiteralShape {
|
|
@@ -25,20 +20,42 @@ export interface LiteralShape {
|
|
|
25
20
|
fields?: Map<string, LiteralShape>;
|
|
26
21
|
}
|
|
27
22
|
|
|
23
|
+
/** A value no variable can hold: its `typeof`, and `String(value)`. */
|
|
24
|
+
export interface UnsupportedLiteral {
|
|
25
|
+
unsupported: 'function' | 'undefined' | 'bigint';
|
|
26
|
+
value: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
28
29
|
/** What a literal reads as. */
|
|
29
30
|
export type StaticLiteral =
|
|
30
31
|
/** A value of this shape. */
|
|
31
32
|
| { shape: LiteralShape }
|
|
32
|
-
/** A value no variable can hold
|
|
33
|
-
|
|
|
33
|
+
/** A value no variable can hold. */
|
|
34
|
+
| UnsupportedLiteral
|
|
35
|
+
/** Evaluating it throws an error with this message. */
|
|
36
|
+
| { throws: string }
|
|
34
37
|
/** Not known without running it. */
|
|
35
38
|
| undefined;
|
|
36
39
|
|
|
40
|
+
/**
|
|
41
|
+
* The members an object literal gives: shapes, values no variable can hold,
|
|
42
|
+
* or `null` for a value that is not known (it still has its place in the
|
|
43
|
+
* order of the members).
|
|
44
|
+
*/
|
|
45
|
+
type Members = Map<string, LiteralShape | UnsupportedLiteral | null>;
|
|
46
|
+
|
|
37
47
|
const NUMBER_RE =
|
|
38
48
|
/(?:0[xX][\da-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][+-]?\d+)?)(n?)/y;
|
|
39
49
|
const IDENT_RE = /[A-Za-z_$][\w$]*/y;
|
|
50
|
+
const KEY_RE = /[\w$]+/y;
|
|
40
51
|
const ARROW_RE =
|
|
41
52
|
/(?:async\s+)?(?:\((?:[^()]|\([^()]*\))*\)|[A-Za-z_$][\w$]*)\s*=>/y;
|
|
53
|
+
const ARRAY_INDEX_RE = /^(?:0|[1-9]\d{0,8})$/;
|
|
54
|
+
|
|
55
|
+
const UNSUPPORTED_UNDEFINED: UnsupportedLiteral = {
|
|
56
|
+
unsupported: 'undefined',
|
|
57
|
+
value: 'undefined',
|
|
58
|
+
};
|
|
42
59
|
|
|
43
60
|
/** Read `src`, one literal and nothing else. */
|
|
44
61
|
export function staticLiteral(src: string): StaticLiteral {
|
|
@@ -48,9 +65,35 @@ export function staticLiteral(src: string): StaticLiteral {
|
|
|
48
65
|
return reader.i === src.length ? value : undefined;
|
|
49
66
|
}
|
|
50
67
|
|
|
51
|
-
|
|
68
|
+
/**
|
|
69
|
+
* The keys of an object in the order `Object.entries` lists them (array
|
|
70
|
+
* indices first, ascending, then the others as they were added): the order
|
|
71
|
+
* the runtime meets the members in.
|
|
72
|
+
*/
|
|
73
|
+
function entryOrder(keys: Iterable<string>): string[] {
|
|
74
|
+
const all = [...keys];
|
|
75
|
+
const indices = all
|
|
76
|
+
.filter((key) => ARRAY_INDEX_RE.test(key))
|
|
77
|
+
.sort((a, b) => Number(a) - Number(b));
|
|
78
|
+
return [...indices, ...all.filter((key) => !ARRAY_INDEX_RE.test(key))];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** `String(value)` of a BigInt literal's digits, or `undefined` if they are no BigInt. */
|
|
82
|
+
function bigintValue(digits: string): string | undefined {
|
|
83
|
+
try {
|
|
84
|
+
return BigInt(digits.replace(/_/g, '')).toString();
|
|
85
|
+
} catch {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The cursor over a JavaScript text that the literal reader moves: the
|
|
92
|
+
* helpers that skip what is not read are shared (see macro-discovery.ts).
|
|
93
|
+
*/
|
|
94
|
+
export class Reader {
|
|
52
95
|
i = 0;
|
|
53
|
-
constructor(
|
|
96
|
+
constructor(protected readonly src: string) {}
|
|
54
97
|
|
|
55
98
|
/** Skip whitespace and comments. */
|
|
56
99
|
space(): void {
|
|
@@ -85,15 +128,27 @@ class Reader {
|
|
|
85
128
|
}
|
|
86
129
|
|
|
87
130
|
/** Skip the string literal at the cursor: its body, and whether it is closed. */
|
|
88
|
-
|
|
131
|
+
protected string(): { body: string; closed: boolean } {
|
|
89
132
|
const { end, closed } = scanStringLiteral(this.src, this.i);
|
|
90
133
|
const body = this.src.slice(this.i + 1, closed ? end - 1 : end);
|
|
91
134
|
this.i = end;
|
|
92
135
|
return { body, closed };
|
|
93
136
|
}
|
|
94
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Skip the string literal at the cursor and read its value, or `undefined`
|
|
140
|
+
* if it is not closed or not well-formed.
|
|
141
|
+
*/
|
|
142
|
+
protected stringValue(): string | undefined {
|
|
143
|
+
const at = this.i;
|
|
144
|
+
const { closed } = this.string();
|
|
145
|
+
return closed
|
|
146
|
+
? (stringLiteralValue(this.src.slice(at, this.i)) ?? undefined)
|
|
147
|
+
: undefined;
|
|
148
|
+
}
|
|
149
|
+
|
|
95
150
|
/** A template literal: a string, but for `${}`. */
|
|
96
|
-
|
|
151
|
+
protected template(): StaticLiteral {
|
|
97
152
|
const { src } = this;
|
|
98
153
|
for (let i = this.i + 1; i < src.length; i++) {
|
|
99
154
|
const c = src.charAt(i);
|
|
@@ -107,10 +162,11 @@ class Reader {
|
|
|
107
162
|
}
|
|
108
163
|
|
|
109
164
|
/** A number, or a sign and one. */
|
|
110
|
-
|
|
165
|
+
protected number(): StaticLiteral {
|
|
111
166
|
const { src } = this;
|
|
112
167
|
let i = this.i;
|
|
113
|
-
|
|
168
|
+
const sign = src.charAt(i);
|
|
169
|
+
if (sign === '+' || sign === '-') {
|
|
114
170
|
i++;
|
|
115
171
|
while (/\s/.test(src.charAt(i))) i++;
|
|
116
172
|
}
|
|
@@ -118,9 +174,18 @@ class Reader {
|
|
|
118
174
|
const match = NUMBER_RE.exec(src);
|
|
119
175
|
if (match) {
|
|
120
176
|
this.i = NUMBER_RE.lastIndex;
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
177
|
+
if (!match[1]) return { shape: { type: 'number' } };
|
|
178
|
+
// A BigInt: `+1n` throws, `-1n` and `1n` are values no variable can hold
|
|
179
|
+
if (sign === '+') {
|
|
180
|
+
return { throws: 'Cannot convert a BigInt value to a number' };
|
|
181
|
+
}
|
|
182
|
+
const digits = bigintValue(match[0].slice(0, -1));
|
|
183
|
+
return digits === undefined
|
|
184
|
+
? undefined
|
|
185
|
+
: {
|
|
186
|
+
unsupported: 'bigint',
|
|
187
|
+
value: sign === '-' && digits !== '0' ? `-${digits}` : digits,
|
|
188
|
+
};
|
|
124
189
|
}
|
|
125
190
|
IDENT_RE.lastIndex = i;
|
|
126
191
|
const word = IDENT_RE.exec(src)?.[0];
|
|
@@ -132,10 +197,12 @@ class Reader {
|
|
|
132
197
|
}
|
|
133
198
|
|
|
134
199
|
/** A keyword, a function, or a name (which is not static). */
|
|
135
|
-
|
|
200
|
+
protected word(): StaticLiteral {
|
|
136
201
|
const { src } = this;
|
|
137
|
-
|
|
138
|
-
|
|
202
|
+
const start = this.i;
|
|
203
|
+
const arrow = this.arrow();
|
|
204
|
+
if (arrow !== undefined)
|
|
205
|
+
return arrow ? this.functionFrom(start) : undefined;
|
|
139
206
|
IDENT_RE.lastIndex = this.i;
|
|
140
207
|
const word = IDENT_RE.exec(src)?.[0];
|
|
141
208
|
if (word === undefined) return undefined;
|
|
@@ -153,84 +220,184 @@ class Reader {
|
|
|
153
220
|
return { shape: { type: 'number' } };
|
|
154
221
|
case 'undefined':
|
|
155
222
|
this.i = end;
|
|
156
|
-
return
|
|
223
|
+
return UNSUPPORTED_UNDEFINED;
|
|
224
|
+
case 'void': {
|
|
225
|
+
// `void 0` is `undefined`, whatever the operand
|
|
226
|
+
this.i = end;
|
|
227
|
+
const operand = this.value();
|
|
228
|
+
return operand && !('throws' in operand)
|
|
229
|
+
? UNSUPPORTED_UNDEFINED
|
|
230
|
+
: operand;
|
|
231
|
+
}
|
|
157
232
|
case 'function':
|
|
158
233
|
case 'class':
|
|
159
234
|
case 'async':
|
|
160
|
-
return this.
|
|
235
|
+
return this.skipDeclaration(word)
|
|
236
|
+
? this.functionFrom(start)
|
|
237
|
+
: undefined;
|
|
161
238
|
}
|
|
162
239
|
return undefined;
|
|
163
240
|
}
|
|
164
241
|
|
|
165
|
-
/**
|
|
166
|
-
|
|
167
|
-
return
|
|
242
|
+
/** The function (or class) that was skipped since `start`. */
|
|
243
|
+
protected functionFrom(start: number): StaticLiteral {
|
|
244
|
+
return {
|
|
245
|
+
unsupported: 'function',
|
|
246
|
+
value: this.src.slice(start, this.i).trimEnd(),
|
|
247
|
+
};
|
|
168
248
|
}
|
|
169
249
|
|
|
170
|
-
/**
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
250
|
+
/**
|
|
251
|
+
* Skip the parameters of an arrow function and its body, which ends at a
|
|
252
|
+
* `,` or `}` outside brackets, or at the end of the text. `undefined` if the
|
|
253
|
+
* text at the cursor is no arrow function, false if it cannot be skipped.
|
|
254
|
+
*/
|
|
255
|
+
protected arrow(): boolean | undefined {
|
|
256
|
+
const { src } = this;
|
|
257
|
+
ARROW_RE.lastIndex = this.i;
|
|
258
|
+
if (!ARROW_RE.test(src)) return undefined;
|
|
259
|
+
this.i = ARROW_RE.lastIndex;
|
|
260
|
+
this.space();
|
|
261
|
+
if (src.charAt(this.i) === '{') return this.skipBalanced();
|
|
262
|
+
while (this.i < src.length && !',}'.includes(src.charAt(this.i))) {
|
|
263
|
+
if (!this.skipOne()) break;
|
|
264
|
+
}
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Skip a `function`, `async function` or `class` (its keyword `word` is at
|
|
270
|
+
* the cursor), up to the end of its body.
|
|
271
|
+
*/
|
|
272
|
+
protected skipDeclaration(word: string): boolean {
|
|
273
|
+
const { src } = this;
|
|
274
|
+
this.i += word.length;
|
|
275
|
+
this.space();
|
|
276
|
+
if (word === 'async') {
|
|
277
|
+
IDENT_RE.lastIndex = this.i;
|
|
278
|
+
if (IDENT_RE.exec(src)?.[0] !== 'function') return false;
|
|
279
|
+
return this.skipDeclaration('function');
|
|
280
|
+
}
|
|
281
|
+
if (word === 'function') {
|
|
282
|
+
if (src.charAt(this.i) === '*') this.i++;
|
|
283
|
+
this.space();
|
|
284
|
+
IDENT_RE.lastIndex = this.i;
|
|
285
|
+
if (IDENT_RE.test(src)) this.i = IDENT_RE.lastIndex;
|
|
286
|
+
this.space();
|
|
287
|
+
if (src.charAt(this.i) !== '(' || !this.skipBalanced()) return false;
|
|
288
|
+
this.space();
|
|
289
|
+
} else {
|
|
290
|
+
// A class: up to its body, past what it extends
|
|
291
|
+
while (this.i < src.length && src.charAt(this.i) !== '{') {
|
|
292
|
+
if (!this.skipOne()) return false;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return src.charAt(this.i) === '{' && this.skipBalanced();
|
|
176
296
|
}
|
|
177
297
|
|
|
178
298
|
/** An object literal: the members that are static. */
|
|
179
|
-
|
|
299
|
+
protected object(): StaticLiteral {
|
|
180
300
|
const { src } = this;
|
|
181
|
-
const
|
|
301
|
+
const members: Members = new Map();
|
|
302
|
+
let thrown: string | undefined;
|
|
182
303
|
this.i++;
|
|
183
304
|
for (;;) {
|
|
184
305
|
this.space();
|
|
185
306
|
const c = src.charAt(this.i);
|
|
186
307
|
if (c === '}') {
|
|
187
308
|
this.i++;
|
|
188
|
-
return
|
|
309
|
+
return thrown === undefined
|
|
310
|
+
? this.objectOf(members)
|
|
311
|
+
: { throws: thrown };
|
|
189
312
|
}
|
|
190
313
|
if (c === ',') {
|
|
191
314
|
this.i++;
|
|
192
315
|
continue;
|
|
193
316
|
}
|
|
194
317
|
if (this.i >= src.length) return undefined;
|
|
318
|
+
const start = this.i;
|
|
195
319
|
const key = this.key();
|
|
320
|
+
if (key === '__proto__') return undefined; // sets the prototype
|
|
196
321
|
this.space();
|
|
197
|
-
|
|
322
|
+
const next = src.charAt(this.i);
|
|
323
|
+
if (key !== undefined && next === ':') {
|
|
198
324
|
this.i++;
|
|
199
325
|
const at = this.i;
|
|
200
326
|
const value = this.value();
|
|
201
327
|
this.space();
|
|
202
|
-
if (value &&
|
|
203
|
-
|
|
204
|
-
|
|
328
|
+
if (value && this.atMemberEnd()) {
|
|
329
|
+
if ('throws' in value) thrown ??= value.throws;
|
|
330
|
+
else members.set(key, 'shape' in value ? value.shape : value);
|
|
205
331
|
continue;
|
|
206
332
|
}
|
|
207
|
-
// Not static:
|
|
333
|
+
// Not static: it replaces any earlier member of the same name
|
|
334
|
+
members.set(key, null);
|
|
208
335
|
this.i = at;
|
|
209
|
-
} else if (key !== undefined &&
|
|
210
|
-
|
|
336
|
+
} else if (key !== undefined && next === '(') {
|
|
337
|
+
// A method
|
|
338
|
+
if (!this.skipBalanced()) return undefined;
|
|
339
|
+
this.space();
|
|
340
|
+
if (src.charAt(this.i) === '{' && this.skipBalanced()) {
|
|
341
|
+
members.set(key, {
|
|
342
|
+
unsupported: 'function',
|
|
343
|
+
value: src.slice(start, this.i),
|
|
344
|
+
});
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
return undefined;
|
|
348
|
+
} else if (key !== undefined && (next === ',' || next === '}')) {
|
|
349
|
+
members.set(key, null); // shorthand: the value of a variable
|
|
350
|
+
continue;
|
|
351
|
+
} else {
|
|
352
|
+
// A spread, a computed key, an accessor: it can replace any member
|
|
353
|
+
members.clear();
|
|
211
354
|
}
|
|
212
355
|
if (!this.skipMember()) return undefined;
|
|
213
356
|
}
|
|
214
357
|
}
|
|
215
358
|
|
|
359
|
+
/** Whether a member ends at the cursor. */
|
|
360
|
+
protected atMemberEnd(): boolean {
|
|
361
|
+
return this.i < this.src.length && ',}'.includes(this.src.charAt(this.i));
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* The object literal with these members: a value no variable can hold
|
|
366
|
+
* anywhere in it (the first the runtime meets) is what it reads as.
|
|
367
|
+
*/
|
|
368
|
+
protected objectOf(members: Members): StaticLiteral {
|
|
369
|
+
const fields = new Map<string, LiteralShape>();
|
|
370
|
+
for (const key of entryOrder(members.keys())) {
|
|
371
|
+
const member = members.get(key);
|
|
372
|
+
if (!member) continue;
|
|
373
|
+
if ('unsupported' in member) return member;
|
|
374
|
+
fields.set(key, member);
|
|
375
|
+
}
|
|
376
|
+
return { shape: { type: 'object', fields } };
|
|
377
|
+
}
|
|
378
|
+
|
|
216
379
|
/** The key of a member (a name, a string or a number), if it has one. */
|
|
217
|
-
|
|
380
|
+
protected key(): string | undefined {
|
|
218
381
|
const { src } = this;
|
|
219
382
|
const c = src.charAt(this.i);
|
|
220
|
-
if (c === '"' || c === "'")
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
const word = /[\w$]+/y;
|
|
225
|
-
word.lastIndex = this.i;
|
|
226
|
-
const found = word.exec(src)?.[0];
|
|
383
|
+
if (c === '"' || c === "'") return this.stringValue();
|
|
384
|
+
KEY_RE.lastIndex = this.i;
|
|
385
|
+
const found = KEY_RE.exec(src)?.[0];
|
|
227
386
|
if (found === undefined) return undefined;
|
|
228
|
-
this.i =
|
|
229
|
-
return found;
|
|
387
|
+
this.i = KEY_RE.lastIndex;
|
|
388
|
+
if (!/^\d/.test(found)) return found;
|
|
389
|
+
// A number: its key is its value as a string
|
|
390
|
+
const digits = found.replace(/_/g, '');
|
|
391
|
+
if (/^0\d/.test(digits)) return undefined;
|
|
392
|
+
if (digits.endsWith('n') && !/^0[xX]/.test(digits)) {
|
|
393
|
+
return bigintValue(digits.slice(0, -1));
|
|
394
|
+
}
|
|
395
|
+
const value = Number(digits);
|
|
396
|
+
return Number.isNaN(value) ? undefined : String(value);
|
|
230
397
|
}
|
|
231
398
|
|
|
232
399
|
/** Skip to the `,` or `}` that ends a member, balancing what is inside. */
|
|
233
|
-
|
|
400
|
+
protected skipMember(): boolean {
|
|
234
401
|
const { src } = this;
|
|
235
402
|
while (this.i < src.length) {
|
|
236
403
|
const c = src.charAt(this.i);
|
|
@@ -241,7 +408,7 @@ class Reader {
|
|
|
241
408
|
}
|
|
242
409
|
|
|
243
410
|
/** Skip an array literal (or any bracketed group) at the cursor. */
|
|
244
|
-
|
|
411
|
+
protected skipBalanced(): boolean {
|
|
245
412
|
const { src } = this;
|
|
246
413
|
const close = { '[': ']', '{': '}', '(': ')' }[src.charAt(this.i)];
|
|
247
414
|
if (!close) return false;
|
|
@@ -257,7 +424,7 @@ class Reader {
|
|
|
257
424
|
}
|
|
258
425
|
|
|
259
426
|
/** Skip one character, or one string, template, comment or group. */
|
|
260
|
-
|
|
427
|
+
protected skipOne(): boolean {
|
|
261
428
|
const { src } = this;
|
|
262
429
|
const c = src.charAt(this.i);
|
|
263
430
|
if (c === '"' || c === "'") return this.string().closed;
|
|
@@ -273,7 +440,7 @@ class Reader {
|
|
|
273
440
|
}
|
|
274
441
|
|
|
275
442
|
/** Skip a template literal, with the expressions in it. */
|
|
276
|
-
|
|
443
|
+
protected skipTemplate(): boolean {
|
|
277
444
|
const { src } = this;
|
|
278
445
|
this.i++;
|
|
279
446
|
while (this.i < src.length) {
|
package/src/store.ts
CHANGED
|
@@ -165,7 +165,7 @@ function computeVarPatches(
|
|
|
165
165
|
}
|
|
166
166
|
// Probe an unchanged variable with a copy of its own: it shares with a
|
|
167
167
|
// recorded one if the copy meets an object already copied
|
|
168
|
-
for (let found = seen.size > 0; found;
|
|
168
|
+
for (let found = seen.size > 0; found;) {
|
|
169
169
|
found = false;
|
|
170
170
|
for (const key of unchanged) {
|
|
171
171
|
const probe = new Map<object, object>();
|
package/src/story-variables.ts
CHANGED
|
@@ -23,9 +23,15 @@ import {
|
|
|
23
23
|
type ParametersOf,
|
|
24
24
|
} from './code-check';
|
|
25
25
|
import { getMacroParameters, type ParameterDef } from './registry';
|
|
26
|
+
import {
|
|
27
|
+
MacroArgumentError,
|
|
28
|
+
parseMacroArgs,
|
|
29
|
+
type ArgSpans,
|
|
30
|
+
} from './components/macros/macro-args';
|
|
26
31
|
import { NOT_MARKUP } from './markup/validate';
|
|
27
32
|
import { createNamespace, variableNameError } from './utils/namespace';
|
|
28
33
|
import { staticLiteral, type LiteralShape } from './static-literal';
|
|
34
|
+
import { parse } from 'acorn';
|
|
29
35
|
|
|
30
36
|
/**
|
|
31
37
|
* The tokens of markup, or none if it is malformed (validateMarkup reports
|
|
@@ -46,12 +52,7 @@ function tokensOf(
|
|
|
46
52
|
}
|
|
47
53
|
|
|
48
54
|
export type VarType =
|
|
49
|
-
| '
|
|
50
|
-
| 'string'
|
|
51
|
-
| 'boolean'
|
|
52
|
-
| 'array'
|
|
53
|
-
| 'object'
|
|
54
|
-
| 'null';
|
|
55
|
+
'number' | 'string' | 'boolean' | 'array' | 'object' | 'null';
|
|
55
56
|
|
|
56
57
|
export interface FieldSchema {
|
|
57
58
|
type: VarType;
|
|
@@ -120,7 +121,8 @@ export type DeclarationErrorCode =
|
|
|
120
121
|
| 'invalid-declaration'
|
|
121
122
|
| 'invalid-name'
|
|
122
123
|
| 'duplicate-declaration'
|
|
123
|
-
| 'unsupported-value'
|
|
124
|
+
| 'unsupported-value'
|
|
125
|
+
| 'syntax';
|
|
124
126
|
|
|
125
127
|
/**
|
|
126
128
|
* A declaration of a StoryVariables or StoryTransients passage: the name
|
|
@@ -159,6 +161,31 @@ function shapeSchema(shape: LiteralShape): FieldSchema {
|
|
|
159
161
|
return { type: shape.type, fields };
|
|
160
162
|
}
|
|
161
163
|
|
|
164
|
+
/**
|
|
165
|
+
* What is wrong with the syntax of an initializer, as it is evaluated
|
|
166
|
+
* (`return (expr)` in a function body), or `undefined`. `pos` is where, as an
|
|
167
|
+
* index into `expr` (it may be past its end, for an unexpected end).
|
|
168
|
+
*/
|
|
169
|
+
function initializerSyntaxError(
|
|
170
|
+
expr: string,
|
|
171
|
+
): { message: string; pos: number } | undefined {
|
|
172
|
+
const prefix = 'return (';
|
|
173
|
+
try {
|
|
174
|
+
parse(prefix + expr + ')', {
|
|
175
|
+
ecmaVersion: 'latest',
|
|
176
|
+
allowReturnOutsideFunction: true,
|
|
177
|
+
});
|
|
178
|
+
} catch (err) {
|
|
179
|
+
if (!(err instanceof SyntaxError)) throw err;
|
|
180
|
+
const { pos } = err as SyntaxError & { pos: number };
|
|
181
|
+
return {
|
|
182
|
+
message: err.message.replace(/ \(\d+:\d+\)$/, ''),
|
|
183
|
+
pos: Math.max(0, pos - prefix.length),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
162
189
|
/**
|
|
163
190
|
* Read the declarations of a StoryVariables (`$name = value`) or
|
|
164
191
|
* StoryTransients (`%name = value`) passage, without evaluating them and
|
|
@@ -167,9 +194,10 @@ function shapeSchema(shape: LiteralShape): FieldSchema {
|
|
|
167
194
|
*
|
|
168
195
|
* A declaration is a line of the form `$name = expression`; blank lines are
|
|
169
196
|
* skipped. A line that is not, and a name that cannot be a variable, are
|
|
170
|
-
* errors. So is an initializer
|
|
171
|
-
* `undefined`)
|
|
172
|
-
*
|
|
197
|
+
* errors. So is an initializer with a syntax error, and one that no variable
|
|
198
|
+
* can hold (a function, `undefined`) when it is a literal, or one that throws
|
|
199
|
+
* when it is a literal (`+1n`); whether any other initializer can only be
|
|
200
|
+
* known by running it. A name declared again is an error too, but both
|
|
173
201
|
* declarations are returned (the later wins when evaluated).
|
|
174
202
|
*/
|
|
175
203
|
export function parseDeclarations(
|
|
@@ -230,18 +258,39 @@ export function parseDeclarations(
|
|
|
230
258
|
);
|
|
231
259
|
}
|
|
232
260
|
seen.add(name);
|
|
261
|
+
declarations.push(declaration);
|
|
262
|
+
const failed = `Failed to evaluate "${sigil}${name} = ${expr}"`;
|
|
263
|
+
const syntax = initializerSyntaxError(expr);
|
|
264
|
+
if (syntax) {
|
|
265
|
+
// At the problem; at the last character for an unexpected end
|
|
266
|
+
const at = Math.min(syntax.pos, expr.length - 1);
|
|
267
|
+
error(
|
|
268
|
+
'syntax',
|
|
269
|
+
declaration.valueStart + at,
|
|
270
|
+
declaration.valueStart + at + 1,
|
|
271
|
+
`${failed}: ${syntax.message}`,
|
|
272
|
+
);
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
233
275
|
const literal = staticLiteral(expr);
|
|
234
|
-
if (literal
|
|
276
|
+
if (!literal) continue;
|
|
277
|
+
if ('shape' in literal) {
|
|
235
278
|
declaration.schema = shapeSchema(literal.shape);
|
|
236
|
-
} else if (literal) {
|
|
279
|
+
} else if ('throws' in literal) {
|
|
237
280
|
error(
|
|
238
281
|
'unsupported-value',
|
|
239
282
|
declaration.valueStart,
|
|
240
283
|
valueEnd,
|
|
241
|
-
|
|
284
|
+
`${failed}: ${literal.throws}`,
|
|
285
|
+
);
|
|
286
|
+
} else {
|
|
287
|
+
error(
|
|
288
|
+
'unsupported-value',
|
|
289
|
+
declaration.valueStart,
|
|
290
|
+
valueEnd,
|
|
291
|
+
`Unsupported type "${literal.unsupported}" for value ${literal.value}. ${UNSUPPORTED_TYPE}`,
|
|
242
292
|
);
|
|
243
293
|
}
|
|
244
|
-
declarations.push(declaration);
|
|
245
294
|
}
|
|
246
295
|
return { declarations, errors };
|
|
247
296
|
}
|
|
@@ -430,6 +479,13 @@ interface ReferenceOptions {
|
|
|
430
479
|
parametersOf: ParametersOf;
|
|
431
480
|
/** Read half-typed markup: skip its malformed tags (default: none read). */
|
|
432
481
|
tolerant?: boolean;
|
|
482
|
+
/**
|
|
483
|
+
* Also report the references the story start does not check: the variable
|
|
484
|
+
* a macro writes to, such as the target of `{unset}` and `{computed}`
|
|
485
|
+
* (a `variable` parameter), and those in the selectors of a link,
|
|
486
|
+
* `{$var}` display or expression.
|
|
487
|
+
*/
|
|
488
|
+
all?: boolean;
|
|
433
489
|
}
|
|
434
490
|
|
|
435
491
|
/** Where index `i` of a text is in the markup being read. */
|
|
@@ -549,6 +605,9 @@ function scanArgs(
|
|
|
549
605
|
): void {
|
|
550
606
|
if (!token.rawArgs) return;
|
|
551
607
|
if (params) {
|
|
608
|
+
if (options.all && !options.storeVarMacros.has(token.name.toLowerCase())) {
|
|
609
|
+
scanVariableParameters(token, params, at, emit);
|
|
610
|
+
}
|
|
552
611
|
const pieces = [
|
|
553
612
|
...argPieces(token.rawArgs, token.argsStart, params, token.name, ''),
|
|
554
613
|
];
|
|
@@ -570,6 +629,58 @@ function scanArgs(
|
|
|
570
629
|
scanCode(token.rawArgs, (i) => at(token.argsStart + i), emit);
|
|
571
630
|
}
|
|
572
631
|
|
|
632
|
+
/**
|
|
633
|
+
* Report the variables the `variable` parameters of a macro name
|
|
634
|
+
* (`{unset $x}`, `{computed $x = …}`), when they are `$` or `%` variables.
|
|
635
|
+
* Arguments that do not parse hold none.
|
|
636
|
+
*/
|
|
637
|
+
function scanVariableParameters(
|
|
638
|
+
token: Extract<Token, { type: 'macro' }>,
|
|
639
|
+
params: readonly ParameterDef[],
|
|
640
|
+
at: At,
|
|
641
|
+
emit: Emit,
|
|
642
|
+
): void {
|
|
643
|
+
const spans: ArgSpans = new Map();
|
|
644
|
+
try {
|
|
645
|
+
parseMacroArgs(token.rawArgs, params, spans);
|
|
646
|
+
} catch (err) {
|
|
647
|
+
if (err instanceof MacroArgumentError) return;
|
|
648
|
+
throw err;
|
|
649
|
+
}
|
|
650
|
+
for (const param of params) {
|
|
651
|
+
const span = spans.get(param);
|
|
652
|
+
if (param.type !== 'variable' || !span) continue;
|
|
653
|
+
const ref = referenceAt(token.rawArgs.slice(span[0], span[1]), 0, (i) =>
|
|
654
|
+
at(token.argsStart + span[0] + i),
|
|
655
|
+
);
|
|
656
|
+
if (ref) emit(ref);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/** Report the references in the selectors of a token, which are interpolated. */
|
|
661
|
+
function scanSelectors(
|
|
662
|
+
content: string,
|
|
663
|
+
token: Token,
|
|
664
|
+
at: At,
|
|
665
|
+
emit: Emit,
|
|
666
|
+
options: ReferenceOptions,
|
|
667
|
+
): void {
|
|
668
|
+
if (
|
|
669
|
+
!('selectorsStart' in token) ||
|
|
670
|
+
token.selectorsStart === undefined ||
|
|
671
|
+
token.selectorsEnd === undefined
|
|
672
|
+
) {
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
const from = token.selectorsStart;
|
|
676
|
+
scanInterpolations(
|
|
677
|
+
content.slice(from, token.selectorsEnd),
|
|
678
|
+
(i) => at(from + i),
|
|
679
|
+
emit,
|
|
680
|
+
{ ...options, storeVarMacros: NO_STORE_VAR_MACROS },
|
|
681
|
+
);
|
|
682
|
+
}
|
|
683
|
+
|
|
573
684
|
/** Report the `$var` references in the tokens of `content`. */
|
|
574
685
|
function collectTokenRefs(
|
|
575
686
|
content: string,
|
|
@@ -581,6 +692,14 @@ function collectTokenRefs(
|
|
|
581
692
|
const { storeVarMacros, parametersOf } = options;
|
|
582
693
|
for (let t = 0; t < tokens.length; t++) {
|
|
583
694
|
const token = tokens[t]!;
|
|
695
|
+
if (
|
|
696
|
+
options.all &&
|
|
697
|
+
token.type !== 'macro' &&
|
|
698
|
+
token.type !== 'html' &&
|
|
699
|
+
token.type !== 'text'
|
|
700
|
+
) {
|
|
701
|
+
scanSelectors(content, token, at, emit, options);
|
|
702
|
+
}
|
|
584
703
|
if (token.type === 'variable') {
|
|
585
704
|
if (
|
|
586
705
|
(token.scope === 'variable' || token.scope === 'transient') &&
|
|
@@ -616,18 +735,7 @@ function collectTokenRefs(
|
|
|
616
735
|
} else if (token.type === 'macro' && !token.isClose) {
|
|
617
736
|
scanArgs(token, parametersOf(token.name), at, emit, options);
|
|
618
737
|
// Selectors (`{.{$cls} button}`) are interpolated when rendered
|
|
619
|
-
|
|
620
|
-
token.selectorsStart !== undefined &&
|
|
621
|
-
token.selectorsEnd !== undefined
|
|
622
|
-
) {
|
|
623
|
-
const from = token.selectorsStart;
|
|
624
|
-
scanInterpolations(
|
|
625
|
-
content.slice(from, token.selectorsEnd),
|
|
626
|
-
(i) => at(from + i),
|
|
627
|
-
emit,
|
|
628
|
-
{ ...options, storeVarMacros: NO_STORE_VAR_MACROS },
|
|
629
|
-
);
|
|
630
|
-
}
|
|
738
|
+
scanSelectors(content, token, at, emit, options);
|
|
631
739
|
|
|
632
740
|
if (storeVarMacros.has(token.name.toLowerCase())) {
|
|
633
741
|
const lead = token.rawArgs.length - token.rawArgs.trimStart().length;
|