@rohal12/spindle 0.59.26 → 0.59.28
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 +312 -73
- package/dist/pkg/story-variables.js +514 -71
- package/dist/pkg/tooling.js +1 -0
- package/package.json +1 -1
- package/src/execute-mutation.ts +17 -1
- package/src/index.tsx +24 -6
- package/src/macro-discovery.ts +299 -0
- package/src/markup/render.tsx +40 -15
- package/src/markup/validate.ts +27 -6
- package/src/static-literal.ts +222 -50
- package/src/story-variables.ts +133 -20
- 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/tooling.d.ts +52 -4
package/src/static-literal.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
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 =
|
|
@@ -20,20 +20,42 @@ export interface LiteralShape {
|
|
|
20
20
|
fields?: Map<string, LiteralShape>;
|
|
21
21
|
}
|
|
22
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
|
+
|
|
23
29
|
/** What a literal reads as. */
|
|
24
30
|
export type StaticLiteral =
|
|
25
31
|
/** A value of this shape. */
|
|
26
32
|
| { shape: LiteralShape }
|
|
27
|
-
/** A value no variable can hold
|
|
28
|
-
|
|
|
33
|
+
/** A value no variable can hold. */
|
|
34
|
+
| UnsupportedLiteral
|
|
35
|
+
/** Evaluating it throws an error with this message. */
|
|
36
|
+
| { throws: string }
|
|
29
37
|
/** Not known without running it. */
|
|
30
38
|
| undefined;
|
|
31
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
|
+
|
|
32
47
|
const NUMBER_RE =
|
|
33
48
|
/(?:0[xX][\da-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][+-]?\d+)?)(n?)/y;
|
|
34
49
|
const IDENT_RE = /[A-Za-z_$][\w$]*/y;
|
|
50
|
+
const KEY_RE = /[\w$]+/y;
|
|
35
51
|
const ARROW_RE =
|
|
36
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
|
+
};
|
|
37
59
|
|
|
38
60
|
/** Read `src`, one literal and nothing else. */
|
|
39
61
|
export function staticLiteral(src: string): StaticLiteral {
|
|
@@ -43,9 +65,35 @@ export function staticLiteral(src: string): StaticLiteral {
|
|
|
43
65
|
return reader.i === src.length ? value : undefined;
|
|
44
66
|
}
|
|
45
67
|
|
|
46
|
-
|
|
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 {
|
|
47
95
|
i = 0;
|
|
48
|
-
constructor(
|
|
96
|
+
constructor(protected readonly src: string) {}
|
|
49
97
|
|
|
50
98
|
/** Skip whitespace and comments. */
|
|
51
99
|
space(): void {
|
|
@@ -80,15 +128,27 @@ class Reader {
|
|
|
80
128
|
}
|
|
81
129
|
|
|
82
130
|
/** Skip the string literal at the cursor: its body, and whether it is closed. */
|
|
83
|
-
|
|
131
|
+
protected string(): { body: string; closed: boolean } {
|
|
84
132
|
const { end, closed } = scanStringLiteral(this.src, this.i);
|
|
85
133
|
const body = this.src.slice(this.i + 1, closed ? end - 1 : end);
|
|
86
134
|
this.i = end;
|
|
87
135
|
return { body, closed };
|
|
88
136
|
}
|
|
89
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
|
+
|
|
90
150
|
/** A template literal: a string, but for `${}`. */
|
|
91
|
-
|
|
151
|
+
protected template(): StaticLiteral {
|
|
92
152
|
const { src } = this;
|
|
93
153
|
for (let i = this.i + 1; i < src.length; i++) {
|
|
94
154
|
const c = src.charAt(i);
|
|
@@ -102,10 +162,11 @@ class Reader {
|
|
|
102
162
|
}
|
|
103
163
|
|
|
104
164
|
/** A number, or a sign and one. */
|
|
105
|
-
|
|
165
|
+
protected number(): StaticLiteral {
|
|
106
166
|
const { src } = this;
|
|
107
167
|
let i = this.i;
|
|
108
|
-
|
|
168
|
+
const sign = src.charAt(i);
|
|
169
|
+
if (sign === '+' || sign === '-') {
|
|
109
170
|
i++;
|
|
110
171
|
while (/\s/.test(src.charAt(i))) i++;
|
|
111
172
|
}
|
|
@@ -113,9 +174,18 @@ class Reader {
|
|
|
113
174
|
const match = NUMBER_RE.exec(src);
|
|
114
175
|
if (match) {
|
|
115
176
|
this.i = NUMBER_RE.lastIndex;
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
+
};
|
|
119
189
|
}
|
|
120
190
|
IDENT_RE.lastIndex = i;
|
|
121
191
|
const word = IDENT_RE.exec(src)?.[0];
|
|
@@ -127,10 +197,12 @@ class Reader {
|
|
|
127
197
|
}
|
|
128
198
|
|
|
129
199
|
/** A keyword, a function, or a name (which is not static). */
|
|
130
|
-
|
|
200
|
+
protected word(): StaticLiteral {
|
|
131
201
|
const { src } = this;
|
|
132
|
-
|
|
133
|
-
|
|
202
|
+
const start = this.i;
|
|
203
|
+
const arrow = this.arrow();
|
|
204
|
+
if (arrow !== undefined)
|
|
205
|
+
return arrow ? this.functionFrom(start) : undefined;
|
|
134
206
|
IDENT_RE.lastIndex = this.i;
|
|
135
207
|
const word = IDENT_RE.exec(src)?.[0];
|
|
136
208
|
if (word === undefined) return undefined;
|
|
@@ -148,84 +220,184 @@ class Reader {
|
|
|
148
220
|
return { shape: { type: 'number' } };
|
|
149
221
|
case 'undefined':
|
|
150
222
|
this.i = end;
|
|
151
|
-
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
|
+
}
|
|
152
232
|
case 'function':
|
|
153
233
|
case 'class':
|
|
154
234
|
case 'async':
|
|
155
|
-
return this.
|
|
235
|
+
return this.skipDeclaration(word)
|
|
236
|
+
? this.functionFrom(start)
|
|
237
|
+
: undefined;
|
|
156
238
|
}
|
|
157
239
|
return undefined;
|
|
158
240
|
}
|
|
159
241
|
|
|
160
|
-
/**
|
|
161
|
-
|
|
162
|
-
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
|
+
};
|
|
163
248
|
}
|
|
164
249
|
|
|
165
|
-
/**
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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();
|
|
171
296
|
}
|
|
172
297
|
|
|
173
298
|
/** An object literal: the members that are static. */
|
|
174
|
-
|
|
299
|
+
protected object(): StaticLiteral {
|
|
175
300
|
const { src } = this;
|
|
176
|
-
const
|
|
301
|
+
const members: Members = new Map();
|
|
302
|
+
let thrown: string | undefined;
|
|
177
303
|
this.i++;
|
|
178
304
|
for (;;) {
|
|
179
305
|
this.space();
|
|
180
306
|
const c = src.charAt(this.i);
|
|
181
307
|
if (c === '}') {
|
|
182
308
|
this.i++;
|
|
183
|
-
return
|
|
309
|
+
return thrown === undefined
|
|
310
|
+
? this.objectOf(members)
|
|
311
|
+
: { throws: thrown };
|
|
184
312
|
}
|
|
185
313
|
if (c === ',') {
|
|
186
314
|
this.i++;
|
|
187
315
|
continue;
|
|
188
316
|
}
|
|
189
317
|
if (this.i >= src.length) return undefined;
|
|
318
|
+
const start = this.i;
|
|
190
319
|
const key = this.key();
|
|
320
|
+
if (key === '__proto__') return undefined; // sets the prototype
|
|
191
321
|
this.space();
|
|
192
|
-
|
|
322
|
+
const next = src.charAt(this.i);
|
|
323
|
+
if (key !== undefined && next === ':') {
|
|
193
324
|
this.i++;
|
|
194
325
|
const at = this.i;
|
|
195
326
|
const value = this.value();
|
|
196
327
|
this.space();
|
|
197
|
-
if (value &&
|
|
198
|
-
|
|
199
|
-
|
|
328
|
+
if (value && this.atMemberEnd()) {
|
|
329
|
+
if ('throws' in value) thrown ??= value.throws;
|
|
330
|
+
else members.set(key, 'shape' in value ? value.shape : value);
|
|
200
331
|
continue;
|
|
201
332
|
}
|
|
202
|
-
// Not static:
|
|
333
|
+
// Not static: it replaces any earlier member of the same name
|
|
334
|
+
members.set(key, null);
|
|
203
335
|
this.i = at;
|
|
204
|
-
} else if (key !== undefined &&
|
|
205
|
-
|
|
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();
|
|
206
354
|
}
|
|
207
355
|
if (!this.skipMember()) return undefined;
|
|
208
356
|
}
|
|
209
357
|
}
|
|
210
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
|
+
|
|
211
379
|
/** The key of a member (a name, a string or a number), if it has one. */
|
|
212
|
-
|
|
380
|
+
protected key(): string | undefined {
|
|
213
381
|
const { src } = this;
|
|
214
382
|
const c = src.charAt(this.i);
|
|
215
|
-
if (c === '"' || c === "'")
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
const word = /[\w$]+/y;
|
|
220
|
-
word.lastIndex = this.i;
|
|
221
|
-
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];
|
|
222
386
|
if (found === undefined) return undefined;
|
|
223
|
-
this.i =
|
|
224
|
-
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);
|
|
225
397
|
}
|
|
226
398
|
|
|
227
399
|
/** Skip to the `,` or `}` that ends a member, balancing what is inside. */
|
|
228
|
-
|
|
400
|
+
protected skipMember(): boolean {
|
|
229
401
|
const { src } = this;
|
|
230
402
|
while (this.i < src.length) {
|
|
231
403
|
const c = src.charAt(this.i);
|
|
@@ -236,7 +408,7 @@ class Reader {
|
|
|
236
408
|
}
|
|
237
409
|
|
|
238
410
|
/** Skip an array literal (or any bracketed group) at the cursor. */
|
|
239
|
-
|
|
411
|
+
protected skipBalanced(): boolean {
|
|
240
412
|
const { src } = this;
|
|
241
413
|
const close = { '[': ']', '{': '}', '(': ')' }[src.charAt(this.i)];
|
|
242
414
|
if (!close) return false;
|
|
@@ -252,7 +424,7 @@ class Reader {
|
|
|
252
424
|
}
|
|
253
425
|
|
|
254
426
|
/** Skip one character, or one string, template, comment or group. */
|
|
255
|
-
|
|
427
|
+
protected skipOne(): boolean {
|
|
256
428
|
const { src } = this;
|
|
257
429
|
const c = src.charAt(this.i);
|
|
258
430
|
if (c === '"' || c === "'") return this.string().closed;
|
|
@@ -268,7 +440,7 @@ class Reader {
|
|
|
268
440
|
}
|
|
269
441
|
|
|
270
442
|
/** Skip a template literal, with the expressions in it. */
|
|
271
|
-
|
|
443
|
+
protected skipTemplate(): boolean {
|
|
272
444
|
const { src } = this;
|
|
273
445
|
this.i++;
|
|
274
446
|
while (this.i < src.length) {
|
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
|
|
@@ -115,7 +121,8 @@ export type DeclarationErrorCode =
|
|
|
115
121
|
| 'invalid-declaration'
|
|
116
122
|
| 'invalid-name'
|
|
117
123
|
| 'duplicate-declaration'
|
|
118
|
-
| 'unsupported-value'
|
|
124
|
+
| 'unsupported-value'
|
|
125
|
+
| 'syntax';
|
|
119
126
|
|
|
120
127
|
/**
|
|
121
128
|
* A declaration of a StoryVariables or StoryTransients passage: the name
|
|
@@ -154,6 +161,31 @@ function shapeSchema(shape: LiteralShape): FieldSchema {
|
|
|
154
161
|
return { type: shape.type, fields };
|
|
155
162
|
}
|
|
156
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
|
+
|
|
157
189
|
/**
|
|
158
190
|
* Read the declarations of a StoryVariables (`$name = value`) or
|
|
159
191
|
* StoryTransients (`%name = value`) passage, without evaluating them and
|
|
@@ -162,9 +194,10 @@ function shapeSchema(shape: LiteralShape): FieldSchema {
|
|
|
162
194
|
*
|
|
163
195
|
* A declaration is a line of the form `$name = expression`; blank lines are
|
|
164
196
|
* skipped. A line that is not, and a name that cannot be a variable, are
|
|
165
|
-
* errors. So is an initializer
|
|
166
|
-
* `undefined`)
|
|
167
|
-
*
|
|
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
|
|
168
201
|
* declarations are returned (the later wins when evaluated).
|
|
169
202
|
*/
|
|
170
203
|
export function parseDeclarations(
|
|
@@ -225,18 +258,39 @@ export function parseDeclarations(
|
|
|
225
258
|
);
|
|
226
259
|
}
|
|
227
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
|
+
}
|
|
228
275
|
const literal = staticLiteral(expr);
|
|
229
|
-
if (literal
|
|
276
|
+
if (!literal) continue;
|
|
277
|
+
if ('shape' in literal) {
|
|
230
278
|
declaration.schema = shapeSchema(literal.shape);
|
|
231
|
-
} else if (literal) {
|
|
279
|
+
} else if ('throws' in literal) {
|
|
232
280
|
error(
|
|
233
281
|
'unsupported-value',
|
|
234
282
|
declaration.valueStart,
|
|
235
283
|
valueEnd,
|
|
236
|
-
|
|
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}`,
|
|
237
292
|
);
|
|
238
293
|
}
|
|
239
|
-
declarations.push(declaration);
|
|
240
294
|
}
|
|
241
295
|
return { declarations, errors };
|
|
242
296
|
}
|
|
@@ -425,6 +479,13 @@ interface ReferenceOptions {
|
|
|
425
479
|
parametersOf: ParametersOf;
|
|
426
480
|
/** Read half-typed markup: skip its malformed tags (default: none read). */
|
|
427
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;
|
|
428
489
|
}
|
|
429
490
|
|
|
430
491
|
/** Where index `i` of a text is in the markup being read. */
|
|
@@ -544,6 +605,9 @@ function scanArgs(
|
|
|
544
605
|
): void {
|
|
545
606
|
if (!token.rawArgs) return;
|
|
546
607
|
if (params) {
|
|
608
|
+
if (options.all && !options.storeVarMacros.has(token.name.toLowerCase())) {
|
|
609
|
+
scanVariableParameters(token, params, at, emit);
|
|
610
|
+
}
|
|
547
611
|
const pieces = [
|
|
548
612
|
...argPieces(token.rawArgs, token.argsStart, params, token.name, ''),
|
|
549
613
|
];
|
|
@@ -565,6 +629,58 @@ function scanArgs(
|
|
|
565
629
|
scanCode(token.rawArgs, (i) => at(token.argsStart + i), emit);
|
|
566
630
|
}
|
|
567
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
|
+
|
|
568
684
|
/** Report the `$var` references in the tokens of `content`. */
|
|
569
685
|
function collectTokenRefs(
|
|
570
686
|
content: string,
|
|
@@ -576,6 +692,14 @@ function collectTokenRefs(
|
|
|
576
692
|
const { storeVarMacros, parametersOf } = options;
|
|
577
693
|
for (let t = 0; t < tokens.length; t++) {
|
|
578
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
|
+
}
|
|
579
703
|
if (token.type === 'variable') {
|
|
580
704
|
if (
|
|
581
705
|
(token.scope === 'variable' || token.scope === 'transient') &&
|
|
@@ -611,18 +735,7 @@ function collectTokenRefs(
|
|
|
611
735
|
} else if (token.type === 'macro' && !token.isClose) {
|
|
612
736
|
scanArgs(token, parametersOf(token.name), at, emit, options);
|
|
613
737
|
// Selectors (`{.{$cls} button}`) are interpolated when rendered
|
|
614
|
-
|
|
615
|
-
token.selectorsStart !== undefined &&
|
|
616
|
-
token.selectorsEnd !== undefined
|
|
617
|
-
) {
|
|
618
|
-
const from = token.selectorsStart;
|
|
619
|
-
scanInterpolations(
|
|
620
|
-
content.slice(from, token.selectorsEnd),
|
|
621
|
-
(i) => at(from + i),
|
|
622
|
-
emit,
|
|
623
|
-
{ ...options, storeVarMacros: NO_STORE_VAR_MACROS },
|
|
624
|
-
);
|
|
625
|
-
}
|
|
738
|
+
scanSelectors(content, token, at, emit, options);
|
|
626
739
|
|
|
627
740
|
if (storeVarMacros.has(token.name.toLowerCase())) {
|
|
628
741
|
const lead = token.rawArgs.length - token.rawArgs.trimStart().length;
|