@rohal12/spindle 0.59.30 → 0.59.31
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 +397 -89
- package/dist/pkg/story-variables.js +321 -52
- package/package.json +1 -1
- package/src/automation/runner.ts +4 -0
- package/src/components/macros/PassageDisplay.tsx +106 -22
- package/src/components/macros/Repeat.tsx +4 -2
- package/src/components/macros/SaveManager.tsx +11 -4
- package/src/components/macros/Unset.tsx +26 -13
- package/src/hooks/use-modal-focus.ts +62 -9
- package/src/index.tsx +10 -9
- package/src/js-bindings.ts +299 -37
- package/src/js-lexer.ts +64 -11
- package/src/macro-discovery.ts +60 -12
- package/src/markup/ast.ts +22 -1
- package/src/markup/render.tsx +44 -15
- package/src/parser.ts +41 -10
- package/src/prng.ts +24 -1
- package/src/saves/save-manager.ts +36 -9
- package/src/store.ts +79 -5
- package/src/story-api.ts +10 -2
- package/src/structural.ts +7 -0
- package/src/tooling.ts +3 -2
- package/src/utils/macro-names.ts +4 -0
- package/src/utils/namespace.ts +11 -2
- package/src/widgets/ast-scanner.ts +4 -1
- package/src/widgets/register-widget-def.ts +2 -3
- package/src/widgets/widget-def.ts +36 -6
- package/src/widgets/widget-registry.ts +6 -2
package/src/js-bindings.ts
CHANGED
|
@@ -34,6 +34,11 @@ interface Binding {
|
|
|
34
34
|
ordered: boolean;
|
|
35
35
|
/** The `{` of the object literal it is declared as, if it is. */
|
|
36
36
|
object?: number;
|
|
37
|
+
/**
|
|
38
|
+
* Not a declaration but a write to the value of one (an assignment, a
|
|
39
|
+
* property of it set): `scope` is then that of the declaration it writes to.
|
|
40
|
+
*/
|
|
41
|
+
write?: true;
|
|
37
42
|
}
|
|
38
43
|
|
|
39
44
|
const CLOSERS: Record<string, Kind> = { '}': '{', ')': '(', ']': '[' };
|
|
@@ -41,6 +46,23 @@ const CLOSERS: Record<string, Kind> = { '}': '{', ')': '(', ']': '[' };
|
|
|
41
46
|
const BLOCK_WORDS = new Set(['if', 'for', 'while', 'switch', 'with', 'catch']);
|
|
42
47
|
const NOT_PARAMETERS = new Set(['if', 'for', 'while', 'switch', 'with']);
|
|
43
48
|
const DECLARATIONS = new Set(['const', 'let', 'var']);
|
|
49
|
+
/** After a name: `=`, `+=`, `&&=`, `++` and the other ways to give it a value. */
|
|
50
|
+
const WRITE_RE =
|
|
51
|
+
/(?:(?:\*\*|<<|>>>?|&&|\|\||\?\?|[-+*/%&|^])=(?!=)|=(?![=>])|\+\+|--)/y;
|
|
52
|
+
/** A call of these on `x.list` changes `x`; as does one of these on `x` itself. */
|
|
53
|
+
const MUTATORS = new Set([
|
|
54
|
+
'push',
|
|
55
|
+
'pop',
|
|
56
|
+
'shift',
|
|
57
|
+
'unshift',
|
|
58
|
+
'splice',
|
|
59
|
+
'sort',
|
|
60
|
+
'reverse',
|
|
61
|
+
'fill',
|
|
62
|
+
'copyWithin',
|
|
63
|
+
]);
|
|
64
|
+
const MUTATING_CALL_RE =
|
|
65
|
+
/(?:Object|Reflect)\s*\.\s*(?:assign|defineProperty|defineProperties|setPrototypeOf|set|deleteProperty)\s*$/;
|
|
44
66
|
/** Before a name in a pattern or parameter list, by the group around it. */
|
|
45
67
|
const BEFORE_BINDING: Record<Kind, string> = {
|
|
46
68
|
'{': '{,:',
|
|
@@ -67,18 +89,41 @@ export class Bindings {
|
|
|
67
89
|
* (`const`, `let` or `var`), when that is the declaration it names: the
|
|
68
90
|
* innermost one in the scope of the use, that comes before it unless the
|
|
69
91
|
* use is in a function of its own. `undefined` if it names another value,
|
|
70
|
-
*
|
|
92
|
+
* more than one declaration is the innermost, or the value is written to
|
|
93
|
+
* (given another, or one of its `properties` set) where the use can follow.
|
|
71
94
|
*/
|
|
72
|
-
objectOf(
|
|
95
|
+
objectOf(
|
|
96
|
+
name: string,
|
|
97
|
+
at: number,
|
|
98
|
+
properties: ReadonlySet<string>,
|
|
99
|
+
): number | undefined {
|
|
73
100
|
const { code } = this;
|
|
74
101
|
const escaped = name.replace(/\$/g, '\\$');
|
|
75
102
|
const bindings: Binding[] = [];
|
|
76
103
|
for (const m of code.matchAll(
|
|
77
104
|
new RegExp(`(?<![\\w$])${escaped}(?![\\w$])`, 'g'),
|
|
78
105
|
)) {
|
|
79
|
-
const binding = this.bindingAt(m.index, name.length);
|
|
106
|
+
const binding = this.bindingAt(m.index, name.length, properties);
|
|
80
107
|
if (binding) bindings.push(binding);
|
|
81
108
|
}
|
|
109
|
+
// A write is to the declaration it sees: in the scope of that one
|
|
110
|
+
const declarations = bindings.filter((b) => !b.write);
|
|
111
|
+
for (const b of bindings) {
|
|
112
|
+
if (b.write) {
|
|
113
|
+
b.scope =
|
|
114
|
+
this.innermostVisible(declarations, b.at)[0]?.scope ?? this.top;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const found = this.innermostVisible(bindings, at);
|
|
118
|
+
return found.length === 1 ? found[0]!.object : undefined;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Of the `bindings` the use at `at` can see, those in the innermost scope:
|
|
123
|
+
* the scope is the use's, and a binding comes before it, unless the use is
|
|
124
|
+
* in a function of its own.
|
|
125
|
+
*/
|
|
126
|
+
private innermostVisible(bindings: Binding[], at: number): Binding[] {
|
|
82
127
|
const useFunction = this.functionStart(at);
|
|
83
128
|
const visible = bindings.filter(
|
|
84
129
|
(b) =>
|
|
@@ -87,8 +132,7 @@ export class Bindings {
|
|
|
87
132
|
(!b.ordered || b.at < at || this.functionStart(b.at) !== useFunction),
|
|
88
133
|
);
|
|
89
134
|
const innermost = Math.max(...visible.map((b) => b.scope.start));
|
|
90
|
-
|
|
91
|
-
return found.length === 1 ? found[0]!.object : undefined;
|
|
135
|
+
return visible.filter((b) => b.scope.start === innermost);
|
|
92
136
|
}
|
|
93
137
|
|
|
94
138
|
// -- the brackets ---------------------------------------------------------
|
|
@@ -133,7 +177,21 @@ export class Bindings {
|
|
|
133
177
|
return undefined;
|
|
134
178
|
}
|
|
135
179
|
|
|
136
|
-
/**
|
|
180
|
+
/**
|
|
181
|
+
* The word before a `(` group (`if`, `for`, `catch`, a function's name…);
|
|
182
|
+
* `for await` is `for`.
|
|
183
|
+
*/
|
|
184
|
+
private ownerWord(paren: Group): string {
|
|
185
|
+
const word = this.wordBefore(this.backFrom(paren.open) + 1);
|
|
186
|
+
if (word.text !== 'await') return word.text;
|
|
187
|
+
const before = this.wordBefore(this.backFrom(word.start) + 1).text;
|
|
188
|
+
return before === 'for' ? before : word.text;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Whether the `{` group is the body of a function (or method), or of a
|
|
193
|
+
* class static block, which has the scope of a `var` of its own.
|
|
194
|
+
*/
|
|
137
195
|
private isFunctionBody(group: Group): boolean {
|
|
138
196
|
if (group.kind !== '{') return false;
|
|
139
197
|
let known = this.functions.get(group);
|
|
@@ -142,9 +200,12 @@ export class Bindings {
|
|
|
142
200
|
const { code } = this;
|
|
143
201
|
if (code.charAt(before) === ')') {
|
|
144
202
|
const params = this.byClose.get(before);
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
203
|
+
known = !!params && !BLOCK_WORDS.has(this.ownerWord(params));
|
|
204
|
+
} else {
|
|
205
|
+
known =
|
|
206
|
+
code.slice(before - 1, before + 1) === '=>' ||
|
|
207
|
+
this.wordBefore(before + 1).text === 'static';
|
|
208
|
+
}
|
|
148
209
|
this.functions.set(group, known);
|
|
149
210
|
}
|
|
150
211
|
return known;
|
|
@@ -175,11 +236,65 @@ export class Bindings {
|
|
|
175
236
|
return start < 0 ? this.top : this.scopeOf(this.byOpen.get(start));
|
|
176
237
|
}
|
|
177
238
|
|
|
178
|
-
/**
|
|
239
|
+
/**
|
|
240
|
+
* What the declaration keyword `word` (`var`, `let`, `const`) declares for:
|
|
241
|
+
* the function for `var`, the block for the others, or the `for` statement,
|
|
242
|
+
* header and body, when the declaration is in its header.
|
|
243
|
+
*/
|
|
179
244
|
private declaredIn(word: { text: string; start: number }): Scope {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
245
|
+
if (word.text === 'var') return this.functionAt(word.start);
|
|
246
|
+
const group = this.innermost(word.start);
|
|
247
|
+
if (group?.kind === '(' && this.ownerWord(group) === 'for') {
|
|
248
|
+
return { start: group.open, end: this.statementEnd(group.close + 1) };
|
|
249
|
+
}
|
|
250
|
+
return this.blockAt(word.start);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Where the statement that starts at `from` ends: a block, the body of an
|
|
255
|
+
* `if`, `for`, `while` or `with` (and the `else` of an `if`), or else up to
|
|
256
|
+
* a `;`, the closer of the group it is in, or a line break that is followed
|
|
257
|
+
* by a statement of its own.
|
|
258
|
+
*/
|
|
259
|
+
private statementEnd(from: number): number {
|
|
260
|
+
const { code } = this;
|
|
261
|
+
const start = this.forward(from);
|
|
262
|
+
const block = this.byOpen.get(start);
|
|
263
|
+
if (block?.kind === '{') return Math.min(block.close + 1, code.length);
|
|
264
|
+
const word = this.wordAt(start);
|
|
265
|
+
if (/^(?:if|for|while|with)$/.test(word)) {
|
|
266
|
+
let paren = this.forward(start + word.length);
|
|
267
|
+
if (word === 'for' && this.wordAt(paren) === 'await') {
|
|
268
|
+
paren = this.forward(paren + 5);
|
|
269
|
+
}
|
|
270
|
+
const header = this.byOpen.get(paren);
|
|
271
|
+
if (header) {
|
|
272
|
+
const end = this.statementEnd(header.close + 1);
|
|
273
|
+
const next = this.forward(end);
|
|
274
|
+
return word === 'if' && this.wordAt(next) === 'else'
|
|
275
|
+
? this.statementEnd(next + 4)
|
|
276
|
+
: end;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
for (let end = start; end < code.length; end++) {
|
|
280
|
+
const c = code.charAt(end);
|
|
281
|
+
if (c === ';') return end + 1;
|
|
282
|
+
if (CLOSERS[c]) return end;
|
|
283
|
+
const group = this.byOpen.get(end);
|
|
284
|
+
if (group) end = group.close;
|
|
285
|
+
else if (c === '\n' && this.startsStatement(end)) return end;
|
|
286
|
+
}
|
|
287
|
+
return code.length;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Whether the line break at `index` ends a statement, and one follows. */
|
|
291
|
+
private startsStatement(index: number): boolean {
|
|
292
|
+
const next = this.forward(index);
|
|
293
|
+
return (
|
|
294
|
+
/[\w$)\]}]/.test(this.code.charAt(this.backFrom(index))) &&
|
|
295
|
+
isWordChar(this.code.charAt(next)) &&
|
|
296
|
+
!/^(?:in|of|instanceof|else)$/.test(this.wordAt(next))
|
|
297
|
+
);
|
|
183
298
|
}
|
|
184
299
|
|
|
185
300
|
/** What a parameter list, whose `)` is `close`, is the scope of: the body. */
|
|
@@ -221,6 +336,11 @@ export class Bindings {
|
|
|
221
336
|
return i;
|
|
222
337
|
}
|
|
223
338
|
|
|
339
|
+
/** The word that starts at `index` (empty if there is none). */
|
|
340
|
+
private wordAt(index: number): string {
|
|
341
|
+
return this.code.slice(index, this.nameEnd(index));
|
|
342
|
+
}
|
|
343
|
+
|
|
224
344
|
/** The word that ends before `end`, and where it starts. */
|
|
225
345
|
private wordBefore(end: number): { text: string; start: number } {
|
|
226
346
|
let start = end;
|
|
@@ -230,8 +350,15 @@ export class Bindings {
|
|
|
230
350
|
|
|
231
351
|
// -- the declarations -----------------------------------------------------
|
|
232
352
|
|
|
233
|
-
/**
|
|
234
|
-
|
|
353
|
+
/**
|
|
354
|
+
* What the name of `length` characters at `at` declares, or writes to, if it
|
|
355
|
+
* does. A write to a `properties` of it counts, one to another does not.
|
|
356
|
+
*/
|
|
357
|
+
private bindingAt(
|
|
358
|
+
at: number,
|
|
359
|
+
length: number,
|
|
360
|
+
properties: ReadonlySet<string>,
|
|
361
|
+
): Binding | undefined {
|
|
235
362
|
const { code } = this;
|
|
236
363
|
let before = this.backFrom(at);
|
|
237
364
|
const spread = code.slice(before - 2, before + 1) === '...';
|
|
@@ -242,17 +369,21 @@ export class Bindings {
|
|
|
242
369
|
this.declarationAt(at, before, spread) ??
|
|
243
370
|
this.patternAt(at, before, after) ??
|
|
244
371
|
this.arrowParameterAt(at, after) ??
|
|
245
|
-
this.
|
|
372
|
+
(spread ? undefined : this.writeAt(at, before, after, properties))
|
|
246
373
|
);
|
|
247
374
|
}
|
|
248
375
|
|
|
249
|
-
/** `const x`, `let x`, `var x`, `function x`, `class x`. */
|
|
376
|
+
/** `const x`, `let x`, `var x`, a later `y` of `let x, y`, `function x`, `class x`. */
|
|
250
377
|
private declarationAt(
|
|
251
378
|
at: number,
|
|
252
379
|
before: number,
|
|
253
380
|
spread: boolean,
|
|
254
381
|
): Binding | undefined {
|
|
255
|
-
|
|
382
|
+
if (spread) return undefined;
|
|
383
|
+
const word =
|
|
384
|
+
this.code.charAt(before) === ','
|
|
385
|
+
? this.listKeyword(before)
|
|
386
|
+
: this.wordBefore(before + 1);
|
|
256
387
|
if (!word || this.code.charAt(word.start - 1) === '.') return undefined;
|
|
257
388
|
if (DECLARATIONS.has(word.text)) {
|
|
258
389
|
const scope = this.declaredIn(word);
|
|
@@ -271,41 +402,99 @@ export class Bindings {
|
|
|
271
402
|
return undefined;
|
|
272
403
|
}
|
|
273
404
|
|
|
405
|
+
/**
|
|
406
|
+
* The `const`, `let` or `var` of the declaration list that the `,` at
|
|
407
|
+
* `comma` is in (`let a = 1, b`, also in a `for` header): the nearest one
|
|
408
|
+
* before it in the same brackets, that no `;` is between.
|
|
409
|
+
*/
|
|
410
|
+
private listKeyword(
|
|
411
|
+
comma: number,
|
|
412
|
+
): { text: string; start: number } | undefined {
|
|
413
|
+
const { code } = this;
|
|
414
|
+
const floor = this.innermost(comma)?.open ?? -1;
|
|
415
|
+
for (let i = comma - 1; i > floor; i--) {
|
|
416
|
+
const c = code.charAt(i);
|
|
417
|
+
if (c === ';') return undefined;
|
|
418
|
+
const group = CLOSERS[c] ? this.byClose.get(i) : undefined;
|
|
419
|
+
if (group) i = group.open;
|
|
420
|
+
else if (isWordChar(c)) {
|
|
421
|
+
const word = this.wordBefore(i + 1);
|
|
422
|
+
if (
|
|
423
|
+
DECLARATIONS.has(word.text) &&
|
|
424
|
+
code.charAt(word.start - 1) !== '.'
|
|
425
|
+
) {
|
|
426
|
+
return word;
|
|
427
|
+
}
|
|
428
|
+
i = word.start;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
return undefined;
|
|
432
|
+
}
|
|
433
|
+
|
|
274
434
|
private nameEnd(at: number): number {
|
|
275
435
|
let end = at;
|
|
276
436
|
while (isWordChar(this.code.charAt(end))) end++;
|
|
277
437
|
return end;
|
|
278
438
|
}
|
|
279
439
|
|
|
280
|
-
/**
|
|
440
|
+
/**
|
|
441
|
+
* A name in a pattern (`const { x } = …`) or a parameter list; or one a
|
|
442
|
+
* pattern assigns to (`({ x } = …)`).
|
|
443
|
+
*/
|
|
281
444
|
private patternAt(
|
|
282
445
|
at: number,
|
|
283
446
|
before: number,
|
|
284
447
|
after: number,
|
|
285
448
|
): Binding | undefined {
|
|
286
449
|
const { code } = this;
|
|
287
|
-
|
|
288
|
-
if (!
|
|
450
|
+
const inner = this.innermost(at);
|
|
451
|
+
if (!inner || !BEFORE_BINDING[inner.kind].includes(code.charAt(before))) {
|
|
289
452
|
return undefined;
|
|
290
453
|
}
|
|
291
|
-
if (
|
|
292
|
-
|
|
454
|
+
if (inner.kind === '{' && code.charAt(after) === ':') return undefined; // a key
|
|
455
|
+
// The outermost pattern, to see whether it is assigned to
|
|
456
|
+
let pattern: Group | undefined;
|
|
457
|
+
for (let group: Group | undefined = inner; group; group = group.parent) {
|
|
293
458
|
if (group.kind === '(') {
|
|
294
|
-
const
|
|
295
|
-
const scope = NOT_PARAMETERS.has(word)
|
|
459
|
+
const scope = NOT_PARAMETERS.has(this.ownerWord(group))
|
|
296
460
|
? undefined
|
|
297
461
|
: this.bodyAfter(group.close);
|
|
298
|
-
return scope
|
|
462
|
+
return scope
|
|
463
|
+
? { scope, at, ordered: false }
|
|
464
|
+
: this.patternWriteAt(at, pattern);
|
|
299
465
|
}
|
|
466
|
+
pattern = group;
|
|
300
467
|
const open = this.backFrom(group.open);
|
|
301
|
-
const word =
|
|
302
|
-
|
|
468
|
+
const word =
|
|
469
|
+
code.charAt(open) === ','
|
|
470
|
+
? this.listKeyword(open)
|
|
471
|
+
: this.wordBefore(open + 1);
|
|
472
|
+
if (word && DECLARATIONS.has(word.text)) {
|
|
303
473
|
return { scope: this.declaredIn(word), at, ordered: true };
|
|
304
474
|
}
|
|
305
475
|
// Inside another pattern or list
|
|
306
|
-
if (!'({[,:'.includes(code.charAt(open)))
|
|
476
|
+
if (!'({[,:'.includes(code.charAt(open))) break;
|
|
307
477
|
}
|
|
308
|
-
return
|
|
478
|
+
return this.patternWriteAt(at, pattern);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/** `[x] = …`, `({ x } = …)`, `for ({ x } of …)`: a name that is assigned to. */
|
|
482
|
+
private patternWriteAt(
|
|
483
|
+
at: number,
|
|
484
|
+
pattern: Group | undefined,
|
|
485
|
+
): Binding | undefined {
|
|
486
|
+
if (!pattern) return undefined;
|
|
487
|
+
const { code } = this;
|
|
488
|
+
const open = this.backFrom(pattern.open);
|
|
489
|
+
if (/[\w$)\]]/.test(code.charAt(open))) return undefined; // `a[x] = …`
|
|
490
|
+
const next = this.forward(pattern.close + 1);
|
|
491
|
+
const inForHead =
|
|
492
|
+
pattern.parent?.kind === '(' &&
|
|
493
|
+
this.ownerWord(pattern.parent) === 'for' &&
|
|
494
|
+
/^(?:of|in)(?![\w$])/.test(code.slice(next));
|
|
495
|
+
return inForHead || this.isWriteOperator(next)
|
|
496
|
+
? { scope: this.top, at, ordered: true, write: true }
|
|
497
|
+
: undefined;
|
|
309
498
|
}
|
|
310
499
|
|
|
311
500
|
/** `x => …`. */
|
|
@@ -315,18 +504,91 @@ export class Bindings {
|
|
|
315
504
|
: undefined;
|
|
316
505
|
}
|
|
317
506
|
|
|
318
|
-
/**
|
|
319
|
-
private
|
|
507
|
+
/** Whether an assignment, `++` or `--` operator is at `index`. */
|
|
508
|
+
private isWriteOperator(index: number): boolean {
|
|
509
|
+
WRITE_RE.lastIndex = index;
|
|
510
|
+
return WRITE_RE.test(this.code);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* `x = …`, `x += …`, `x++`, `for (x of …)`, and the writes to what it
|
|
515
|
+
* holds that change the config it is (`x.name = …`, `delete x.name`,
|
|
516
|
+
* `x.parameters.push(…)`, `Object.assign(x, …)`): a value not known.
|
|
517
|
+
*/
|
|
518
|
+
private writeAt(
|
|
320
519
|
at: number,
|
|
321
520
|
before: number,
|
|
322
521
|
after: number,
|
|
522
|
+
properties: ReadonlySet<string>,
|
|
323
523
|
): Binding | undefined {
|
|
324
524
|
const { code } = this;
|
|
325
|
-
if (
|
|
326
|
-
|
|
525
|
+
if (this.isClassBody(this.innermost(at))) return undefined; // a field
|
|
526
|
+
const { keys, end } = this.memberChain(after);
|
|
527
|
+
const prefix = ['++', '--'].includes(code.slice(before - 1, before + 1));
|
|
528
|
+
const written =
|
|
529
|
+
prefix ||
|
|
530
|
+
this.isWriteOperator(end) ||
|
|
531
|
+
(keys.length > 0 &&
|
|
532
|
+
(this.wordBefore(before + 1).text === 'delete' ||
|
|
533
|
+
(keys.length > 1 &&
|
|
534
|
+
code.charAt(end) === '(' &&
|
|
535
|
+
MUTATORS.has(keys[keys.length - 1]!)))) ||
|
|
536
|
+
(keys.length === 0 && this.isForTarget(before, end)) ||
|
|
537
|
+
(keys.length === 0 && this.isMutatedArgument(before, end));
|
|
538
|
+
// A property that is not a part of the macro's config does not matter
|
|
539
|
+
const first = keys[0];
|
|
540
|
+
if (!written || (first && !properties.has(first))) return undefined;
|
|
541
|
+
return { scope: this.top, at, ordered: true, write: true };
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/** Whether the group is the body of a class: its members are its own. */
|
|
545
|
+
private isClassBody(group: Group | undefined): boolean {
|
|
546
|
+
if (group?.kind !== '{') return false;
|
|
547
|
+
let start = group.open;
|
|
548
|
+
while (start > 0 && !'{};'.includes(this.code.charAt(start - 1))) start--;
|
|
549
|
+
return /(?<![\w$.])class(?![\w$])/.test(this.code.slice(start, group.open));
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* The properties read from a name at `from` (`.a.b[c]`, a key is null if
|
|
554
|
+
* computed), and where the chain ends.
|
|
555
|
+
*/
|
|
556
|
+
private memberChain(from: number): { keys: (string | null)[]; end: number } {
|
|
557
|
+
const { code } = this;
|
|
558
|
+
const keys: (string | null)[] = [];
|
|
559
|
+
let end = from;
|
|
560
|
+
for (;;) {
|
|
561
|
+
let i = end;
|
|
562
|
+
if (code.startsWith('?.', i)) i += 2;
|
|
563
|
+
else if (code.charAt(i) === '.' && code.charAt(i + 1) !== '.') i++;
|
|
564
|
+
else if (code.charAt(i) !== '[') break;
|
|
565
|
+
i = this.forward(i);
|
|
566
|
+
const bracket = code.charAt(i) === '[' ? this.byOpen.get(i) : undefined;
|
|
567
|
+
const key = bracket ? '' : this.wordAt(i);
|
|
568
|
+
if (!bracket && !key) break;
|
|
569
|
+
keys.push(bracket ? null : key);
|
|
570
|
+
end = this.forward(bracket ? bracket.close + 1 : i + key.length);
|
|
327
571
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
572
|
+
return { keys, end };
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/** `for (x of …)`: the name is assigned each item. */
|
|
576
|
+
private isForTarget(before: number, end: number): boolean {
|
|
577
|
+
const group = this.byOpen.get(before);
|
|
578
|
+
return (
|
|
579
|
+
group?.kind === '(' &&
|
|
580
|
+
this.ownerWord(group) === 'for' &&
|
|
581
|
+
/^(?:of|in)(?![\w$])/.test(this.code.slice(end))
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/** `Object.assign(x, …)` and the like: the name is the first argument. */
|
|
586
|
+
private isMutatedArgument(before: number, end: number): boolean {
|
|
587
|
+
const group = this.byOpen.get(before);
|
|
588
|
+
return (
|
|
589
|
+
group?.kind === '(' &&
|
|
590
|
+
',)'.includes(this.code.charAt(end)) &&
|
|
591
|
+
MUTATING_CALL_RE.test(this.code.slice(0, group.open))
|
|
592
|
+
);
|
|
331
593
|
}
|
|
332
594
|
}
|
package/src/js-lexer.ts
CHANGED
|
@@ -152,7 +152,10 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
152
152
|
colonEndsTernary = false;
|
|
153
153
|
/** The last `}` closed a block. */
|
|
154
154
|
closedBlock = false;
|
|
155
|
-
/**
|
|
155
|
+
/**
|
|
156
|
+
* The value of the token before the one being finished, but for a member
|
|
157
|
+
* name (`a.static`).
|
|
158
|
+
*/
|
|
156
159
|
prevValue: unknown;
|
|
157
160
|
/** The type of the token before the last one. */
|
|
158
161
|
typeBeforeLast: TokenType = tt.eof;
|
|
@@ -226,10 +229,11 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
226
229
|
// or an object key (`class D { function; }`, `{ class: 1 }`)
|
|
227
230
|
const self = this as unknown as ParserState;
|
|
228
231
|
const prevType = self.type;
|
|
229
|
-
|
|
232
|
+
// The word before, unless it is a member name (`a.static`, `a?.let`),
|
|
233
|
+
// which no contextual keyword is
|
|
234
|
+
this.prevValue = isMemberDot(this.typeBeforeLast) ? undefined : self.value;
|
|
230
235
|
const property =
|
|
231
|
-
(type.keyword !== undefined &&
|
|
232
|
-
(prevType === tt.dot || prevType === tt.questionDot)) ||
|
|
236
|
+
(type.keyword !== undefined && isMemberDot(prevType)) ||
|
|
233
237
|
((type === tt._function || type === tt._class) &&
|
|
234
238
|
notABody(self.input, self.pos));
|
|
235
239
|
// acorn takes a `function` or `class` for a statement's (whose `}` a
|
|
@@ -249,14 +253,17 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
249
253
|
this.typeBeforeLast = prevType;
|
|
250
254
|
// @ts-expect-error acorn internals
|
|
251
255
|
super.finishToken(property ? tt.name : type, value);
|
|
252
|
-
//
|
|
253
|
-
//
|
|
256
|
+
// acorn takes a name for a contextual keyword wherever it reads (`of`
|
|
257
|
+
// of a for-of loop, `yield` in a generator), after which an operand
|
|
258
|
+
// comes. A member name (`a?.of`, which acorn checks only after `.`) and
|
|
259
|
+
// a variable named `of` (`const of of list`) are operands.
|
|
254
260
|
if (
|
|
255
261
|
type === tt.name &&
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
262
|
+
(isMemberDot(prevType) ||
|
|
263
|
+
(value === 'of' &&
|
|
264
|
+
(prevType === tt._const ||
|
|
265
|
+
prevType === tt._var ||
|
|
266
|
+
this.prevValue === 'let')))
|
|
260
267
|
) {
|
|
261
268
|
self.exprAllowed = false;
|
|
262
269
|
}
|
|
@@ -313,6 +320,11 @@ function transientAssignment(p: ParserState, end: number): boolean {
|
|
|
313
320
|
}
|
|
314
321
|
}
|
|
315
322
|
|
|
323
|
+
/** Is `type` the `.` or `?.` of a member access? */
|
|
324
|
+
function isMemberDot(type: TokenType): boolean {
|
|
325
|
+
return type === tt.dot || type === tt.questionDot;
|
|
326
|
+
}
|
|
327
|
+
|
|
316
328
|
const OPENERS = new Set([tt.parenL, tt.bracketL, tt.braceL, tt.dollarBraceL]);
|
|
317
329
|
const CLOSERS = new Set([tt.parenR, tt.bracketR, tt.braceR]);
|
|
318
330
|
|
|
@@ -509,6 +521,14 @@ export function parseCode(
|
|
|
509
521
|
src: string,
|
|
510
522
|
goal: JsGoal = 'expression',
|
|
511
523
|
): ParsedCode {
|
|
524
|
+
return parseWithAst(src, goal).out;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** What parseCode finds in `src`, and the syntax tree it is in. */
|
|
528
|
+
function parseWithAst(
|
|
529
|
+
src: string,
|
|
530
|
+
goal: JsGoal,
|
|
531
|
+
): { out: ParsedCode; ast: AnyNode } {
|
|
512
532
|
// Whether `%name` is a transient or `%` modulo is guessed from the tokens
|
|
513
533
|
// before it, as acorn guesses whether `/` opens a regex. Where the guess
|
|
514
534
|
// makes the parser fail at a `%`, the other reading is tried, and the
|
|
@@ -521,7 +541,7 @@ export function parseCode(
|
|
|
521
541
|
const out: ParsedCode = { refs: [], strings: [] };
|
|
522
542
|
walk(ast, src, out, false, false);
|
|
523
543
|
out.refs.sort((a, b) => a.start - b.start);
|
|
524
|
-
return out;
|
|
544
|
+
return { out, ast };
|
|
525
545
|
} catch (error) {
|
|
526
546
|
const pos = (error as { pos?: number }).pos ?? -1;
|
|
527
547
|
if (!(error instanceof SyntaxError) || percentAt.has(pos)) throw error;
|
|
@@ -532,6 +552,39 @@ export function parseCode(
|
|
|
532
552
|
}
|
|
533
553
|
}
|
|
534
554
|
|
|
555
|
+
/** The variable at the root of a reference path, and whether it has members. */
|
|
556
|
+
export interface ReferencePath {
|
|
557
|
+
sigil: Sigil;
|
|
558
|
+
name: string;
|
|
559
|
+
/** The path goes on past the variable (`$a.b`, `$a["b"]`, `$a[i]`). */
|
|
560
|
+
nested: boolean;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* Read `src` as a reference path: a variable with any number of `.name` and
|
|
565
|
+
* `[expression]` members after it (`$data["field"]`, `@list[_i].x`), and
|
|
566
|
+
* nothing else. Returns null for any other code (`$a + 1`, `$a.b()`, `$a,
|
|
567
|
+
* $b`, `$a = 1`), and throws a `CodeSyntaxError` for code that is not
|
|
568
|
+
* well-formed.
|
|
569
|
+
*/
|
|
570
|
+
export function parseReferencePath(src: string): ReferencePath | null {
|
|
571
|
+
const { ast } = parseWithAst(src, 'expression');
|
|
572
|
+
// `$a?.b` is a chain expression around the members
|
|
573
|
+
let node: AnyNode =
|
|
574
|
+
ast.type === 'ChainExpression' ? (ast.expression as AnyNode) : ast;
|
|
575
|
+
let nested = false;
|
|
576
|
+
while (node.type === 'MemberExpression') {
|
|
577
|
+
node = node.object as AnyNode;
|
|
578
|
+
nested = true;
|
|
579
|
+
}
|
|
580
|
+
if (node.type !== 'Identifier') return null;
|
|
581
|
+
const sigil = sigilOf(node, src);
|
|
582
|
+
// Parentheses around the variable or a member (`($a).b`) are not part of
|
|
583
|
+
// a path as written
|
|
584
|
+
if (!sigil || node.start !== 0) return null;
|
|
585
|
+
return { sigil: sigil[0], name: sigil[1], nested };
|
|
586
|
+
}
|
|
587
|
+
|
|
535
588
|
/**
|
|
536
589
|
* Parse `src` as `goal`, reading `%` as `percentAt` says where it says, and
|
|
537
590
|
* noting where it read a transient in `transientsRead`.
|