@rohal12/spindle 0.61.7 → 0.62.1
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 +3 -1
- package/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +336 -105
- package/dist/pkg/play.js +3 -49
- package/dist/pkg/story-variables.js +119 -71
- package/dist/pkg/tooling.js +3 -0
- package/package.json +5 -5
- package/src/code-check.ts +11 -9
- package/src/components/macros/PassageDisplay.tsx +372 -36
- package/src/components/macros/arg-utils.ts +7 -2
- package/src/components/macros/macro-args.ts +113 -19
- package/src/js-lexer.ts +47 -12
- package/src/play/resolve-story.ts +5 -63
- package/src/story-variables.ts +10 -4
- package/src/tooling.ts +19 -4
- package/src/types-drift-check.ts +17 -23
- package/types/tooling.d.ts +56 -1
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* empty).
|
|
16
16
|
*/
|
|
17
17
|
import { parseDelay } from '../../utils/parse-delay';
|
|
18
|
+
import type { Span as SourceSpan } from '../../markup/tokens';
|
|
18
19
|
import type { MacroArgs, ParameterDef } from '../../registry';
|
|
19
20
|
import type { StoryState } from '../../store';
|
|
20
21
|
import { noPassageError } from '../../runtime-errors';
|
|
@@ -42,6 +43,8 @@ interface Place {
|
|
|
42
43
|
/** The index in the raw arguments of the text being read. */
|
|
43
44
|
at: number;
|
|
44
45
|
spans: ArgSpans | undefined;
|
|
46
|
+
/** Full parameter spans for tolerant editor input, including keywords. */
|
|
47
|
+
toolingSpans?: ArgSpans;
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
/** The terms of `src`: the runs of text between its depth-0 whitespace. */
|
|
@@ -80,11 +83,18 @@ export class MacroArgumentError extends Error {
|
|
|
80
83
|
|
|
81
84
|
/** Read `src`, at `place`, as `param` says. */
|
|
82
85
|
function readValue(param: ParameterDef, src: string, place: Place): unknown {
|
|
83
|
-
|
|
86
|
+
const span: Span = [place.at, place.at + src.length];
|
|
87
|
+
place.spans?.set(param, span);
|
|
88
|
+
place.toolingSpans?.set(param, span);
|
|
84
89
|
switch (param.type) {
|
|
85
90
|
case 'string': {
|
|
86
91
|
const value = readWholeQuoted(src);
|
|
87
|
-
if (value === null)
|
|
92
|
+
if (value === null) {
|
|
93
|
+
if (place.toolingSpans && (src[0] === '"' || src[0] === "'")) {
|
|
94
|
+
return readText(src);
|
|
95
|
+
}
|
|
96
|
+
throw new MacroArgumentError(param, src);
|
|
97
|
+
}
|
|
88
98
|
return value;
|
|
89
99
|
}
|
|
90
100
|
case 'text':
|
|
@@ -128,7 +138,7 @@ function matchAt(re: RegExp, src: string, pos: number): string | null {
|
|
|
128
138
|
function readOptions(
|
|
129
139
|
params: readonly ParameterDef[],
|
|
130
140
|
src: string,
|
|
131
|
-
{ at, spans }: Place,
|
|
141
|
+
{ at, spans, toolingSpans }: Place,
|
|
132
142
|
): Record<string, unknown> {
|
|
133
143
|
const options: Record<string, unknown> = {};
|
|
134
144
|
let i = 0;
|
|
@@ -136,9 +146,10 @@ function readOptions(
|
|
|
136
146
|
const key = matchAt(WORD_RE, src, i);
|
|
137
147
|
if (!key) {
|
|
138
148
|
// Skip whitespace and stray characters, or a stray quoted string.
|
|
139
|
-
i = readQuoted(src, i)?.end ?? i + 1;
|
|
149
|
+
i = readQuoted(src, i, !!toolingSpans)?.end ?? i + 1;
|
|
140
150
|
continue;
|
|
141
151
|
}
|
|
152
|
+
const keyAt = i;
|
|
142
153
|
i += key.length;
|
|
143
154
|
|
|
144
155
|
// A value is a quoted string, which may follow the keyword directly
|
|
@@ -147,7 +158,7 @@ function readOptions(
|
|
|
147
158
|
let val: string | undefined;
|
|
148
159
|
let j = i;
|
|
149
160
|
while (j < src.length && isWhitespace(src[j]!)) j++;
|
|
150
|
-
const quoted = readQuoted(src, j);
|
|
161
|
+
const quoted = readQuoted(src, j, !!toolingSpans);
|
|
151
162
|
const digits = quoted || j === i ? null : matchAt(NUMBER_RE, src, j);
|
|
152
163
|
if (quoted) {
|
|
153
164
|
val = quoted.value;
|
|
@@ -161,8 +172,9 @@ function readOptions(
|
|
|
161
172
|
if (param && param.type !== 'flag' && val !== undefined) {
|
|
162
173
|
spans?.set(param, [at + j, at + i]);
|
|
163
174
|
}
|
|
175
|
+
if (param) toolingSpans?.set(param, [at + keyAt, at + i]);
|
|
164
176
|
// A keyword that takes a string was given anything else, or nothing
|
|
165
|
-
if (param?.type === 'string' && !quoted) {
|
|
177
|
+
if (param?.type === 'string' && !quoted && !toolingSpans) {
|
|
166
178
|
throw new MacroArgumentError(
|
|
167
179
|
param,
|
|
168
180
|
matchAt(WORD_OR_PUNCT_RE, src, j) ?? '',
|
|
@@ -210,7 +222,11 @@ function takeFlag(src: string, word: string, alone: boolean): Span | null {
|
|
|
210
222
|
* term with terms on both sides; `=` is an assignment at depth 0, not part
|
|
211
223
|
* of `==` or `!=`.
|
|
212
224
|
*/
|
|
213
|
-
function findSeparator(
|
|
225
|
+
function findSeparator(
|
|
226
|
+
src: string,
|
|
227
|
+
word: string,
|
|
228
|
+
tolerant = false,
|
|
229
|
+
): Span | null {
|
|
214
230
|
if (word === '=') {
|
|
215
231
|
const indices = topLevelIndices(src, (ch) => ch === '=');
|
|
216
232
|
for (let k = 0; k < indices.length; k++) {
|
|
@@ -224,9 +240,9 @@ function findSeparator(src: string, word: string): Span | null {
|
|
|
224
240
|
return null;
|
|
225
241
|
}
|
|
226
242
|
const spans = terms(src);
|
|
227
|
-
for (let k = 1; k < spans.length - 1; k++) {
|
|
243
|
+
for (let k = 1; k < spans.length - (tolerant ? 0 : 1); k++) {
|
|
228
244
|
if (slice(src, spans[k]!) === word) {
|
|
229
|
-
return [spans[k - 1]![1], spans[k + 1]
|
|
245
|
+
return [spans[k - 1]![1], spans[k + 1]?.[0] ?? src.length];
|
|
230
246
|
}
|
|
231
247
|
}
|
|
232
248
|
return null;
|
|
@@ -252,7 +268,7 @@ function readGroup(
|
|
|
252
268
|
params: readonly ParameterDef[],
|
|
253
269
|
src: string | null,
|
|
254
270
|
args: Record<string, unknown>,
|
|
255
|
-
{ at: srcAt, spans }: Place,
|
|
271
|
+
{ at: srcAt, spans, toolingSpans }: Place,
|
|
256
272
|
): void {
|
|
257
273
|
let [text, at] = trimmed(src ?? '', srcAt);
|
|
258
274
|
// A lone word is the passage of {include inline}, not its flag
|
|
@@ -260,7 +276,14 @@ function readGroup(
|
|
|
260
276
|
for (const flag of params.filter((p) => p.type === 'flag')) {
|
|
261
277
|
const rest = src === null ? null : takeFlag(text, flag.name, alone);
|
|
262
278
|
args[flag.name] = rest !== null;
|
|
263
|
-
if (rest)
|
|
279
|
+
if (rest) {
|
|
280
|
+
const flagSpan: Span =
|
|
281
|
+
rest[0] > 0
|
|
282
|
+
? [0, text.indexOf(flag.name) + flag.name.length]
|
|
283
|
+
: [text.lastIndexOf(flag.name), text.length];
|
|
284
|
+
toolingSpans?.set(flag, [at + flagSpan[0], at + flagSpan[1]]);
|
|
285
|
+
[text, at] = trimmed(slice(text, rest), at + rest[0]);
|
|
286
|
+
}
|
|
264
287
|
}
|
|
265
288
|
const positional = params.filter((p) => p.type !== 'flag');
|
|
266
289
|
if (positional.length === 0) return;
|
|
@@ -271,10 +294,21 @@ function readGroup(
|
|
|
271
294
|
);
|
|
272
295
|
const restIndex = lastText < 0 ? positional.length - 1 : lastText;
|
|
273
296
|
/** Read `param` from the text from `start` to `end`. */
|
|
274
|
-
const read = (param: ParameterDef, [start, end]: Span) =>
|
|
275
|
-
start === end
|
|
276
|
-
|
|
277
|
-
|
|
297
|
+
const read = (param: ParameterDef, [start, end]: Span, tolerant = true) => {
|
|
298
|
+
if (start === end) return unset(param);
|
|
299
|
+
try {
|
|
300
|
+
return readValue(param, text.slice(start, end), {
|
|
301
|
+
at: at + start,
|
|
302
|
+
spans,
|
|
303
|
+
toolingSpans,
|
|
304
|
+
});
|
|
305
|
+
} catch (error) {
|
|
306
|
+
if (toolingSpans && tolerant && error instanceof MacroArgumentError) {
|
|
307
|
+
return undefined;
|
|
308
|
+
}
|
|
309
|
+
throw error;
|
|
310
|
+
}
|
|
311
|
+
};
|
|
278
312
|
|
|
279
313
|
// One parameter reads the whole text: no need to split it.
|
|
280
314
|
if (positional.length === 1) {
|
|
@@ -292,7 +326,7 @@ function readGroup(
|
|
|
292
326
|
// `string` that isn't quoted) belongs to the rest, as in {meter $hp 100}
|
|
293
327
|
const readTrailing = (param: ParameterDef, span: Span) => {
|
|
294
328
|
try {
|
|
295
|
-
return read(param, span);
|
|
329
|
+
return read(param, span, false);
|
|
296
330
|
} catch (error) {
|
|
297
331
|
if (error instanceof MacroArgumentError) return undefined;
|
|
298
332
|
throw error;
|
|
@@ -306,7 +340,10 @@ function readGroup(
|
|
|
306
340
|
args[param.name] = value ?? unset(param);
|
|
307
341
|
if (value !== undefined) to--;
|
|
308
342
|
// A term it didn't take
|
|
309
|
-
else
|
|
343
|
+
else {
|
|
344
|
+
spans?.delete(param);
|
|
345
|
+
toolingSpans?.delete(param);
|
|
346
|
+
}
|
|
310
347
|
}
|
|
311
348
|
const rest = positional[restIndex]!;
|
|
312
349
|
args[rest.name] =
|
|
@@ -328,6 +365,7 @@ export function parseMacroArgs<const P extends readonly ParameterDef[]>(
|
|
|
328
365
|
rawArgs: string,
|
|
329
366
|
parameters: P,
|
|
330
367
|
spans?: ArgSpans,
|
|
368
|
+
toolingSpans?: ArgSpans,
|
|
331
369
|
): MacroArgs<P> {
|
|
332
370
|
const args: Record<string, unknown> = {};
|
|
333
371
|
// The text after the last separator read, null once one is missing
|
|
@@ -335,18 +373,31 @@ export function parseMacroArgs<const P extends readonly ParameterDef[]>(
|
|
|
335
373
|
let groupStart = 0;
|
|
336
374
|
parameters.forEach((separator, i) => {
|
|
337
375
|
if (separator.type !== 'separator') return;
|
|
338
|
-
const at =
|
|
376
|
+
const at =
|
|
377
|
+
rest === null
|
|
378
|
+
? null
|
|
379
|
+
: findSeparator(rest, separator.name, !!toolingSpans);
|
|
339
380
|
args[separator.name] = at !== null;
|
|
381
|
+
if (at && toolingSpans) {
|
|
382
|
+
const start =
|
|
383
|
+
restAt + at[0] + rest!.slice(at[0], at[1]).indexOf(separator.name);
|
|
384
|
+
toolingSpans.set(separator, [start, start + separator.name.length]);
|
|
385
|
+
}
|
|
340
386
|
const before = at ? rest!.slice(0, at[0]) : rest;
|
|
341
387
|
readGroup(parameters.slice(groupStart, i), before, args, {
|
|
342
388
|
at: restAt,
|
|
343
389
|
spans,
|
|
390
|
+
toolingSpans,
|
|
344
391
|
});
|
|
345
392
|
rest = at ? rest!.slice(at[1]) : null;
|
|
346
393
|
if (at) restAt += at[1];
|
|
347
394
|
groupStart = i + 1;
|
|
348
395
|
});
|
|
349
|
-
readGroup(parameters.slice(groupStart), rest, args, {
|
|
396
|
+
readGroup(parameters.slice(groupStart), rest, args, {
|
|
397
|
+
at: restAt,
|
|
398
|
+
spans,
|
|
399
|
+
toolingSpans,
|
|
400
|
+
});
|
|
350
401
|
return args as MacroArgs<P>;
|
|
351
402
|
}
|
|
352
403
|
|
|
@@ -399,3 +450,46 @@ export function readBoundVariable(rawArgs: string): string {
|
|
|
399
450
|
const first = terms(src)[0];
|
|
400
451
|
return first ? slice(src, first) : '';
|
|
401
452
|
}
|
|
453
|
+
|
|
454
|
+
/** A consumed argument and its declared parameter, with UTF-16 source offsets. */
|
|
455
|
+
export interface MacroArgumentSpan extends SourceSpan {
|
|
456
|
+
/** The original parameter object, including for nested keyword options. */
|
|
457
|
+
parameter: ParameterDef;
|
|
458
|
+
/** Index of the top-level parameter, for signature help. */
|
|
459
|
+
parameterIndex: number;
|
|
460
|
+
/** Indices through nested options, starting at parameterIndex. */
|
|
461
|
+
parameterPath: number[];
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* The runtime's argument grouping, tolerant of missing or unclosed strings.
|
|
466
|
+
* Includes quotes, flags, separators and option keywords; offsets are into
|
|
467
|
+
* rawArgs. Absent arguments have no span. Options also have their enclosing
|
|
468
|
+
* parameter span, so consumers can choose the most specific overlapping span.
|
|
469
|
+
* No expressions are evaluated. Parameter identities are preserved.
|
|
470
|
+
*/
|
|
471
|
+
export function macroArgumentSpans(
|
|
472
|
+
rawArgs: string,
|
|
473
|
+
parameters: readonly ParameterDef[],
|
|
474
|
+
): MacroArgumentSpan[] {
|
|
475
|
+
const spans: ArgSpans = new Map();
|
|
476
|
+
parseMacroArgs(rawArgs, parameters, undefined, spans);
|
|
477
|
+
const result: MacroArgumentSpan[] = [];
|
|
478
|
+
const add = (params: readonly ParameterDef[], path: number[]) => {
|
|
479
|
+
params.forEach((parameter, index) => {
|
|
480
|
+
const parameterPath = [...path, index];
|
|
481
|
+
const span = spans.get(parameter);
|
|
482
|
+
if (span)
|
|
483
|
+
result.push({
|
|
484
|
+
parameter,
|
|
485
|
+
parameterIndex: parameterPath[0]!,
|
|
486
|
+
parameterPath,
|
|
487
|
+
start: span[0],
|
|
488
|
+
end: span[1],
|
|
489
|
+
});
|
|
490
|
+
if (parameter.parameters) add(parameter.parameters, parameterPath);
|
|
491
|
+
});
|
|
492
|
+
};
|
|
493
|
+
add(parameters, []);
|
|
494
|
+
return result.sort((a, b) => a.start - b.start || b.end - a.end);
|
|
495
|
+
}
|
package/src/js-lexer.ts
CHANGED
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
* be declared (`let _x`, `(_a) => …`) or be property names (`a.@x`), and
|
|
27
27
|
* code must be valid modern JavaScript (a non-strict script).
|
|
28
28
|
*/
|
|
29
|
+
import type { Span } from './markup/tokens';
|
|
29
30
|
import {
|
|
30
31
|
Parser,
|
|
31
32
|
tokTypes as tt,
|
|
@@ -496,15 +497,16 @@ function bracketsBefore(
|
|
|
496
497
|
// Parsing: references, string text, errors
|
|
497
498
|
// ---------------------------------------------------------------------------
|
|
498
499
|
|
|
499
|
-
export interface VariableRef {
|
|
500
|
+
export interface VariableRef extends Span {
|
|
500
501
|
sigil: Sigil;
|
|
501
502
|
/** The name after the sigil. */
|
|
502
503
|
name: string;
|
|
503
|
-
/** Range of the sigil and name in the source. */
|
|
504
|
-
start: number;
|
|
505
|
-
end: number;
|
|
506
504
|
/** A shorthand property (`{ $gold }`): its key is to be written out. */
|
|
507
505
|
shorthand: boolean;
|
|
506
|
+
/** Whether evaluating this occurrence reads the binding. */
|
|
507
|
+
read: boolean;
|
|
508
|
+
/** Whether this occurrence assigns or deletes the binding itself (not a member). */
|
|
509
|
+
write: boolean;
|
|
508
510
|
}
|
|
509
511
|
|
|
510
512
|
export interface ParsedCode {
|
|
@@ -693,14 +695,19 @@ function walk(
|
|
|
693
695
|
out: ParsedCode,
|
|
694
696
|
binding: boolean,
|
|
695
697
|
shorthand: boolean,
|
|
698
|
+
access: 'read' | 'write' | 'readwrite' = 'read',
|
|
696
699
|
): void {
|
|
697
700
|
if (Array.isArray(node)) {
|
|
698
|
-
for (const n of node) walk(n, src, out, binding, false);
|
|
701
|
+
for (const n of node) walk(n, src, out, binding, false, access);
|
|
699
702
|
return;
|
|
700
703
|
}
|
|
701
704
|
if (!isNode(node)) return;
|
|
702
|
-
const sub = (
|
|
703
|
-
|
|
705
|
+
const sub = (
|
|
706
|
+
child: unknown,
|
|
707
|
+
bind = false,
|
|
708
|
+
short = false,
|
|
709
|
+
role: typeof access = 'read',
|
|
710
|
+
) => walk(child, src, out, bind, short, role);
|
|
704
711
|
switch (node.type) {
|
|
705
712
|
case 'Identifier': {
|
|
706
713
|
const sigil = sigilOf(node, src);
|
|
@@ -718,6 +725,8 @@ function walk(
|
|
|
718
725
|
start: node.start,
|
|
719
726
|
end: node.end,
|
|
720
727
|
shorthand,
|
|
728
|
+
read: access !== 'write',
|
|
729
|
+
write: access !== 'read',
|
|
721
730
|
});
|
|
722
731
|
return;
|
|
723
732
|
}
|
|
@@ -740,13 +749,13 @@ function walk(
|
|
|
740
749
|
if (node.shorthand) {
|
|
741
750
|
const value = node.value as AnyNode;
|
|
742
751
|
if (value.type === 'AssignmentPattern') {
|
|
743
|
-
sub(value.left, binding, true);
|
|
752
|
+
sub(value.left, binding, true, access);
|
|
744
753
|
sub(value.right);
|
|
745
754
|
} else {
|
|
746
|
-
sub(value, binding, true);
|
|
755
|
+
sub(value, binding, true, access);
|
|
747
756
|
}
|
|
748
757
|
} else {
|
|
749
|
-
sub(node.value, binding);
|
|
758
|
+
sub(node.value, binding, false, access);
|
|
750
759
|
}
|
|
751
760
|
return;
|
|
752
761
|
case 'MethodDefinition':
|
|
@@ -784,15 +793,41 @@ function walk(
|
|
|
784
793
|
sub(node.param, true);
|
|
785
794
|
sub(node.body);
|
|
786
795
|
return;
|
|
796
|
+
case 'AssignmentExpression':
|
|
797
|
+
sub(
|
|
798
|
+
node.left,
|
|
799
|
+
false,
|
|
800
|
+
false,
|
|
801
|
+
node.operator === '=' ? 'write' : 'readwrite',
|
|
802
|
+
);
|
|
803
|
+
sub(node.right);
|
|
804
|
+
return;
|
|
805
|
+
case 'UnaryExpression':
|
|
806
|
+
sub(
|
|
807
|
+
node.argument,
|
|
808
|
+
false,
|
|
809
|
+
false,
|
|
810
|
+
node.operator === 'delete' ? 'write' : 'read',
|
|
811
|
+
);
|
|
812
|
+
return;
|
|
813
|
+
case 'UpdateExpression':
|
|
814
|
+
sub(node.argument, false, false, 'readwrite');
|
|
815
|
+
return;
|
|
816
|
+
case 'ForInStatement':
|
|
817
|
+
case 'ForOfStatement':
|
|
818
|
+
sub(node.left, false, false, 'write');
|
|
819
|
+
sub(node.right);
|
|
820
|
+
sub(node.body);
|
|
821
|
+
return;
|
|
787
822
|
case 'AssignmentPattern':
|
|
788
|
-
sub(node.left, binding);
|
|
823
|
+
sub(node.left, binding, false, access);
|
|
789
824
|
sub(node.right);
|
|
790
825
|
return;
|
|
791
826
|
case 'ObjectPattern':
|
|
792
827
|
case 'ArrayPattern':
|
|
793
828
|
case 'RestElement':
|
|
794
829
|
for (const key of ['properties', 'elements', 'argument']) {
|
|
795
|
-
sub(node[key], binding);
|
|
830
|
+
sub(node[key], binding, false, access);
|
|
796
831
|
}
|
|
797
832
|
return;
|
|
798
833
|
default:
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { glob, readFile, stat } from 'node:fs/promises';
|
|
2
|
-
import {
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
4
|
import type * as TweeTs from '@rohal12/twee-ts';
|
|
5
5
|
import { errorMessage } from '../utils/error-message';
|
|
@@ -61,57 +61,6 @@ async function expandGlob(pattern: string): Promise<string[]> {
|
|
|
61
61
|
return matches.sort();
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
/** A path segment with glob syntax in it (as `path.matchesGlob` reads it). */
|
|
65
|
-
const GLOB_SEGMENT = /[*?[\]{}()!+@]/;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* An `exclude` glob of the project config as twee-ts matches it: against
|
|
69
|
-
* paths relative to the working directory. A relative glob is relative to the
|
|
70
|
-
* project directory; an absolute one has its literal leading folders made
|
|
71
|
-
* relative. Either way the folder part added or rewritten is escaped.
|
|
72
|
-
*/
|
|
73
|
-
function rebaseExclude(dir: string, pattern: string): string {
|
|
74
|
-
let base = dir;
|
|
75
|
-
let rest = pattern.replace(/^\.\//, '');
|
|
76
|
-
if (isAbsolute(pattern)) {
|
|
77
|
-
const parts = pattern.split('/');
|
|
78
|
-
let literal = parts.findIndex((part) => GLOB_SEGMENT.test(part));
|
|
79
|
-
if (literal === -1) literal = parts.length;
|
|
80
|
-
base = parts.slice(0, literal).join('/') || '/';
|
|
81
|
-
rest = parts.slice(literal).join('/');
|
|
82
|
-
}
|
|
83
|
-
const fromCwd = globLiteral(
|
|
84
|
-
relative(process.cwd(), base).split(sep).join('/'),
|
|
85
|
-
);
|
|
86
|
-
if (fromCwd === '') return rest;
|
|
87
|
-
return rest === '' ? fromCwd : `${fromCwd}/${rest}`;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* The project config with its paths made independent of the working
|
|
92
|
-
* directory. twee-ts reads `sources`, `modules` and `headFile` relative to
|
|
93
|
-
* the working directory, and matches `exclude` globs against paths relative
|
|
94
|
-
* to it, so each is rebased from the project directory.
|
|
95
|
-
*/
|
|
96
|
-
function rebaseConfig(
|
|
97
|
-
dir: string,
|
|
98
|
-
config: TweeTs.TweeTsConfig,
|
|
99
|
-
): TweeTs.TweeTsConfig {
|
|
100
|
-
const rebased: TweeTs.TweeTsConfig = {
|
|
101
|
-
...config,
|
|
102
|
-
sources: (config.sources ?? ['.']).map((s) => resolve(dir, s)),
|
|
103
|
-
};
|
|
104
|
-
if (config.modules)
|
|
105
|
-
rebased.modules = config.modules.map((m) => resolve(dir, m));
|
|
106
|
-
if (config.headFile !== undefined) {
|
|
107
|
-
rebased.headFile = resolve(dir, config.headFile);
|
|
108
|
-
}
|
|
109
|
-
if (config.exclude) {
|
|
110
|
-
rebased.exclude = config.exclude.map((e) => rebaseExclude(dir, e));
|
|
111
|
-
}
|
|
112
|
-
return rebased;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
64
|
/**
|
|
116
65
|
* The project config less its output and format options. Those are ours: the
|
|
117
66
|
* project's own would write a file, compile for another format, or fetch one.
|
|
@@ -130,15 +79,6 @@ function withoutFormatOptions(
|
|
|
130
79
|
);
|
|
131
80
|
}
|
|
132
81
|
|
|
133
|
-
/**
|
|
134
|
-
* `text` as a glob matching only itself, for Node's `path.matchesGlob` (which
|
|
135
|
-
* twee-ts uses): a metacharacter goes in brackets. Braces and `!` cannot be
|
|
136
|
-
* bracketed there, so each becomes `?` (any one character).
|
|
137
|
-
*/
|
|
138
|
-
function globLiteral(text: string): string {
|
|
139
|
-
return text.replace(/[*?[\]()+@]/g, (c) => `[${c}]`).replace(/[{}!]/g, '?');
|
|
140
|
-
}
|
|
141
|
-
|
|
142
82
|
/** The compile sources and project config for a path (or glob). */
|
|
143
83
|
async function sourcesFor(
|
|
144
84
|
tweeTs: TweeTsModule,
|
|
@@ -163,11 +103,13 @@ async function sourcesFor(
|
|
|
163
103
|
if (!isDirectory) return { sources: [path], config: {} };
|
|
164
104
|
const found = tweeTs.loadConfig(path);
|
|
165
105
|
if (!found) return { sources: [path], config: {} };
|
|
166
|
-
|
|
106
|
+
// twee-ts resolves config paths and structured excludes against the project
|
|
107
|
+
// directory itself. Rebasing them again would duplicate relative prefixes.
|
|
108
|
+
const config = found;
|
|
167
109
|
if (config.sources?.length === 0) {
|
|
168
110
|
throw new Error(`spindle: ${path} has a twee-ts config with no sources`);
|
|
169
111
|
}
|
|
170
|
-
return { sources: config.sources ?? [], config };
|
|
112
|
+
return { sources: [...(config.sources ?? [resolve(path)])], config };
|
|
171
113
|
}
|
|
172
114
|
|
|
173
115
|
/** The diagnostics a thrown twee-ts error carries, when it has any. */
|
package/src/story-variables.ts
CHANGED
|
@@ -177,7 +177,7 @@ function initializerSyntaxError(
|
|
|
177
177
|
): { message: string; pos: number } | undefined {
|
|
178
178
|
const prefix = 'return (';
|
|
179
179
|
try {
|
|
180
|
-
parse(prefix + expr + ')', {
|
|
180
|
+
parse(prefix + expr + '\n)', {
|
|
181
181
|
ecmaVersion: 'latest',
|
|
182
182
|
allowReturnOutsideFunction: true,
|
|
183
183
|
});
|
|
@@ -335,7 +335,7 @@ export function parseStoryVariables(
|
|
|
335
335
|
const expr = content.slice(valueStart, valueEnd);
|
|
336
336
|
let value: unknown;
|
|
337
337
|
try {
|
|
338
|
-
value = new Function('return (' + expr + ')')();
|
|
338
|
+
value = new Function('return (' + expr + '\n)')();
|
|
339
339
|
} catch (err) {
|
|
340
340
|
throw new Error(
|
|
341
341
|
`${passageName}: Failed to evaluate "${sigil}${name} = ${expr}": ${errorMessage(err)}`,
|
|
@@ -797,7 +797,10 @@ function collectTokenRefs(
|
|
|
797
797
|
// Selectors (`{.{$cls} button}`) are interpolated when rendered
|
|
798
798
|
scanSelectors(content, token, at, emit, options);
|
|
799
799
|
|
|
800
|
-
if (
|
|
800
|
+
if (
|
|
801
|
+
storeVarMacros.has(token.name.toLowerCase()) &&
|
|
802
|
+
!parametersOf.widget?.(token.name)
|
|
803
|
+
) {
|
|
801
804
|
const lead = token.rawArgs.length - token.rawArgs.trimStart().length;
|
|
802
805
|
const first = token.rawArgs.trim().split(/\s+/)[0] ?? '';
|
|
803
806
|
const quoted = QUOTED_VAR_ARG_RE.exec(first);
|
|
@@ -818,7 +821,10 @@ function collectTokenRefs(
|
|
|
818
821
|
}
|
|
819
822
|
}
|
|
820
823
|
|
|
821
|
-
if (
|
|
824
|
+
if (
|
|
825
|
+
token.name.toLowerCase() === 'do' &&
|
|
826
|
+
!parametersOf.widget?.(token.name)
|
|
827
|
+
) {
|
|
822
828
|
// A {do} body is JavaScript: scan its source text as code, however
|
|
823
829
|
// the markup tokens split it up.
|
|
824
830
|
let close = t + 1;
|
package/src/tooling.ts
CHANGED
|
@@ -55,13 +55,24 @@ export type {
|
|
|
55
55
|
} from './story-variables';
|
|
56
56
|
export { checkParameterTypes } from './registry';
|
|
57
57
|
export { formatDiagnostic } from './markup/validate';
|
|
58
|
-
export {
|
|
58
|
+
export {
|
|
59
|
+
effectiveWidgetDefinitions,
|
|
60
|
+
parseWidgetDef,
|
|
61
|
+
} from './widgets/widget-def';
|
|
59
62
|
export type { WidgetDef, WidgetDefinition } from './widgets/widget-def';
|
|
60
63
|
|
|
61
64
|
// The parsing rules (see "Tooling API" in docs/tooling.md): the leaf rules
|
|
62
65
|
// the runtime parses with, so editor tooling needn't mirror them.
|
|
63
|
-
export {
|
|
66
|
+
export {
|
|
67
|
+
findCodeEnd,
|
|
68
|
+
lexJs,
|
|
69
|
+
lexTemplate,
|
|
70
|
+
parseCode,
|
|
71
|
+
scanStringLiteral,
|
|
72
|
+
} from './js-lexer';
|
|
64
73
|
export type {
|
|
74
|
+
ParsedCode,
|
|
75
|
+
VariableRef,
|
|
65
76
|
FindCodeEndOptions,
|
|
66
77
|
JsGoal,
|
|
67
78
|
JsLexHandlers,
|
|
@@ -116,10 +127,14 @@ export {
|
|
|
116
127
|
} from './components/macros/arg-utils';
|
|
117
128
|
export { splitIncludeFlag } from './components/macros/include-args';
|
|
118
129
|
export {
|
|
130
|
+
macroArgumentSpans,
|
|
119
131
|
evaluatePassageName,
|
|
120
132
|
passageTarget,
|
|
121
133
|
} from './components/macros/macro-args';
|
|
122
|
-
export type {
|
|
134
|
+
export type {
|
|
135
|
+
MacroArgumentSpan,
|
|
136
|
+
PassageTarget,
|
|
137
|
+
} from './components/macros/macro-args';
|
|
123
138
|
export { transform } from './transform';
|
|
124
139
|
export type {
|
|
125
140
|
ArgumentErrorPiece,
|
|
@@ -164,13 +179,13 @@ export interface ToolingMacro {
|
|
|
164
179
|
* Its declared parameters: their types tell which arguments are code, and
|
|
165
180
|
* what a `string` or `text` argument holds (see ParameterDef.holds).
|
|
166
181
|
*/
|
|
182
|
+
parameters?: readonly ParameterDef[];
|
|
167
183
|
/**
|
|
168
184
|
* For a widget: its `@` parameters and what the ones that declare it hold
|
|
169
185
|
* (`@target:passage`, see WidgetDef), so that a call is checked like the
|
|
170
186
|
* call of a macro whose `string` parameter has that `holds`.
|
|
171
187
|
*/
|
|
172
188
|
widget?: Pick<WidgetDef, 'params' | 'holds'>;
|
|
173
|
-
parameters?: readonly ParameterDef[];
|
|
174
189
|
/** Whether it resolves markup: its strings hold markup by default. */
|
|
175
190
|
interpolate?: boolean;
|
|
176
191
|
/** Whether its first argument names the story variable it binds. */
|
package/src/types-drift-check.ts
CHANGED
|
@@ -86,12 +86,26 @@ const _publishedToSource: Source = {} as Published;
|
|
|
86
86
|
/** `true` if `A` and `B` are assignable to each other, else `false`. */
|
|
87
87
|
type Same<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false;
|
|
88
88
|
|
|
89
|
+
/** Also catch missing optional fields, which mutual assignability allows. */
|
|
90
|
+
type SameShape<A, B> = Same<A, B> extends true ? Same<keyof A, keyof B> : false;
|
|
91
|
+
|
|
89
92
|
// The rest of the tooling entry point: what it parses and returns (tokens,
|
|
90
93
|
// pieces, pairing, declarations) and the stateless checks. Each must be the
|
|
91
94
|
// same type in the source and in types/tooling.d.ts, or `false` here is no
|
|
92
95
|
// `true`.
|
|
93
96
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
94
|
-
|
|
97
|
+
type AssertTrue<T extends readonly true[]> = T;
|
|
98
|
+
type ToolingChecks = [
|
|
99
|
+
Same<typeof SourceTooling.parseCode, typeof PublishedTooling.parseCode>,
|
|
100
|
+
Same<
|
|
101
|
+
typeof SourceTooling.macroArgumentSpans,
|
|
102
|
+
typeof PublishedTooling.macroArgumentSpans
|
|
103
|
+
>,
|
|
104
|
+
Same<
|
|
105
|
+
typeof SourceTooling.effectiveWidgetDefinitions,
|
|
106
|
+
typeof PublishedTooling.effectiveWidgetDefinitions
|
|
107
|
+
>,
|
|
108
|
+
SameShape<SourceTooling.WidgetDefinition, PublishedTooling.WidgetDefinition>,
|
|
95
109
|
Same<SourceTooling.MarkupError, PublishedTooling.MarkupError>,
|
|
96
110
|
Same<SourceTooling.Token, PublishedTooling.Token>,
|
|
97
111
|
Same<SourceTooling.Piece, PublishedTooling.Piece>,
|
|
@@ -99,7 +113,7 @@ const _toolingTypes: [
|
|
|
99
113
|
Same<SourceTooling.PairingError, PublishedTooling.PairingError>,
|
|
100
114
|
Same<SourceTooling.Declaration, PublishedTooling.Declaration>,
|
|
101
115
|
Same<SourceTooling.DeclarationError, PublishedTooling.DeclarationError>,
|
|
102
|
-
|
|
116
|
+
SameShape<SourceTooling.ToolingMacro, PublishedTooling.ToolingMacro>,
|
|
103
117
|
Same<typeof SourceTooling.pairMarkup, typeof PublishedTooling.pairMarkup>,
|
|
104
118
|
Same<
|
|
105
119
|
typeof SourceTooling.passagePieces,
|
|
@@ -139,28 +153,8 @@ const _toolingTypes: [
|
|
|
139
153
|
typeof SourceTooling.formatPassage,
|
|
140
154
|
typeof PublishedTooling.formatPassage
|
|
141
155
|
>,
|
|
142
|
-
] = [
|
|
143
|
-
true,
|
|
144
|
-
true,
|
|
145
|
-
true,
|
|
146
|
-
true,
|
|
147
|
-
true,
|
|
148
|
-
true,
|
|
149
|
-
true,
|
|
150
|
-
true,
|
|
151
|
-
true,
|
|
152
|
-
true,
|
|
153
|
-
true,
|
|
154
|
-
true,
|
|
155
|
-
true,
|
|
156
|
-
true,
|
|
157
|
-
true,
|
|
158
|
-
true,
|
|
159
|
-
true,
|
|
160
|
-
true,
|
|
161
|
-
true,
|
|
162
|
-
true,
|
|
163
156
|
];
|
|
157
|
+
type _toolingTypes = AssertTrue<ToolingChecks>;
|
|
164
158
|
|
|
165
159
|
// A typical custom macro written against the published types must type-check,
|
|
166
160
|
// and misuse of the hooks must not (no `any` leaking through).
|