@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
package/dist/pkg/play.js
CHANGED
|
@@ -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 { Worker } from "node:worker_threads";
|
|
5
5
|
//#region src/utils/error-message.ts
|
|
@@ -62,44 +62,6 @@ async function expandGlob(pattern) {
|
|
|
62
62
|
for await (const file of glob(pattern)) matches.push(file);
|
|
63
63
|
return matches.sort();
|
|
64
64
|
}
|
|
65
|
-
/** A path segment with glob syntax in it (as `path.matchesGlob` reads it). */
|
|
66
|
-
var GLOB_SEGMENT = /[*?[\]{}()!+@]/;
|
|
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, pattern) {
|
|
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(relative(process.cwd(), base).split(sep).join("/"));
|
|
84
|
-
if (fromCwd === "") return rest;
|
|
85
|
-
return rest === "" ? fromCwd : `${fromCwd}/${rest}`;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* The project config with its paths made independent of the working
|
|
89
|
-
* directory. twee-ts reads `sources`, `modules` and `headFile` relative to
|
|
90
|
-
* the working directory, and matches `exclude` globs against paths relative
|
|
91
|
-
* to it, so each is rebased from the project directory.
|
|
92
|
-
*/
|
|
93
|
-
function rebaseConfig(dir, config) {
|
|
94
|
-
const rebased = {
|
|
95
|
-
...config,
|
|
96
|
-
sources: (config.sources ?? ["."]).map((s) => resolve(dir, s))
|
|
97
|
-
};
|
|
98
|
-
if (config.modules) rebased.modules = config.modules.map((m) => resolve(dir, m));
|
|
99
|
-
if (config.headFile !== void 0) rebased.headFile = resolve(dir, config.headFile);
|
|
100
|
-
if (config.exclude) rebased.exclude = config.exclude.map((e) => rebaseExclude(dir, e));
|
|
101
|
-
return rebased;
|
|
102
|
-
}
|
|
103
65
|
/**
|
|
104
66
|
* The project config less its output and format options. Those are ours: the
|
|
105
67
|
* project's own would write a file, compile for another format, or fetch one.
|
|
@@ -107,14 +69,6 @@ function rebaseConfig(dir, config) {
|
|
|
107
69
|
function withoutFormatOptions(config) {
|
|
108
70
|
return Object.fromEntries(Object.entries(config).filter(([key]) => !key.startsWith("format") && !key.startsWith("output") && key !== "useTweegoPath" && key !== "noRemote"));
|
|
109
71
|
}
|
|
110
|
-
/**
|
|
111
|
-
* `text` as a glob matching only itself, for Node's `path.matchesGlob` (which
|
|
112
|
-
* twee-ts uses): a metacharacter goes in brackets. Braces and `!` cannot be
|
|
113
|
-
* bracketed there, so each becomes `?` (any one character).
|
|
114
|
-
*/
|
|
115
|
-
function globLiteral(text) {
|
|
116
|
-
return text.replace(/[*?[\]()+@]/g, (c) => `[${c}]`).replace(/[{}!]/g, "?");
|
|
117
|
-
}
|
|
118
72
|
/** The compile sources and project config for a path (or glob). */
|
|
119
73
|
async function sourcesFor(tweeTs, path) {
|
|
120
74
|
let isDirectory = false;
|
|
@@ -138,10 +92,10 @@ async function sourcesFor(tweeTs, path) {
|
|
|
138
92
|
sources: [path],
|
|
139
93
|
config: {}
|
|
140
94
|
};
|
|
141
|
-
const config =
|
|
95
|
+
const config = found;
|
|
142
96
|
if (config.sources?.length === 0) throw new Error(`spindle: ${path} has a twee-ts config with no sources`);
|
|
143
97
|
return {
|
|
144
|
-
sources: config.sources ?? [],
|
|
98
|
+
sources: [...config.sources ?? [resolve(path)]],
|
|
145
99
|
config
|
|
146
100
|
};
|
|
147
101
|
}
|
|
@@ -9154,34 +9154,6 @@ function parse$1(input, options) {
|
|
|
9154
9154
|
}
|
|
9155
9155
|
//#endregion
|
|
9156
9156
|
//#region src/js-lexer.ts
|
|
9157
|
-
/**
|
|
9158
|
-
* The JavaScript in story markup, read with acorn.
|
|
9159
|
-
*
|
|
9160
|
-
* acorn reads the code: where string, template and regex literals and
|
|
9161
|
-
* comments begin and end, whether `/` opens a regex, and (`parseCode`) the
|
|
9162
|
-
* syntax tree. A plugin (`SigilParser`) teaches its tokenizer the two sigils
|
|
9163
|
-
* that are no JavaScript: `@name` (locals) anywhere, and `%name`
|
|
9164
|
-
* (transients) where an operand is expected, the same place acorn reads `/`
|
|
9165
|
-
* as a regex; `%` elsewhere is the modulo operator. `$name` and `_name` are
|
|
9166
|
-
* identifiers already; the syntax tree tells references from property names
|
|
9167
|
-
* and declarations.
|
|
9168
|
-
*
|
|
9169
|
-
* - `parseCode`: the references and string text of well-formed code, or a
|
|
9170
|
-
* `CodeSyntaxError` that says what is wrong and where. The expression
|
|
9171
|
-
* engine (`expression.ts`) rewrites the references it finds, and the
|
|
9172
|
-
* story-start check (`markup/validate.ts`) reports the errors.
|
|
9173
|
-
* - `findCodeEnd`: where the code in a `{…}` ends, for the markup grammar
|
|
9174
|
-
* (`markup/code-end.ts`). -1 when acorn can't read it: the grammar then
|
|
9175
|
-
* scans it leniently, as prose-like macro arguments (`{link Don't go}`)
|
|
9176
|
-
* need.
|
|
9177
|
-
* - `lexJs`: a lenient token walk for the macro argument splitters
|
|
9178
|
-
* (`components/macros/arg-utils.ts`), which also read text that is no
|
|
9179
|
-
* JavaScript.
|
|
9180
|
-
*
|
|
9181
|
-
* Restrictions (docs/variables.md "Code in passages"): sigil variables can't
|
|
9182
|
-
* be declared (`let _x`, `(_a) => …`) or be property names (`a.@x`), and
|
|
9183
|
-
* code must be valid modern JavaScript (a non-strict script).
|
|
9184
|
-
*/
|
|
9185
9157
|
/** Code runs through `new Function`: a non-strict script body. */
|
|
9186
9158
|
var OPTIONS = {
|
|
9187
9159
|
ecmaVersion: "latest",
|
|
@@ -9542,13 +9514,13 @@ function propertyName(node, src) {
|
|
|
9542
9514
|
* identifiers here are declared (a `let`, a parameter), where a sigil
|
|
9543
9515
|
* variable can't be. `shorthand`: the node is a shorthand property's value.
|
|
9544
9516
|
*/
|
|
9545
|
-
function walk(node, src, out, binding, shorthand) {
|
|
9517
|
+
function walk(node, src, out, binding, shorthand, access = "read") {
|
|
9546
9518
|
if (Array.isArray(node)) {
|
|
9547
|
-
for (const n of node) walk(n, src, out, binding, false);
|
|
9519
|
+
for (const n of node) walk(n, src, out, binding, false, access);
|
|
9548
9520
|
return;
|
|
9549
9521
|
}
|
|
9550
9522
|
if (!isNode(node)) return;
|
|
9551
|
-
const sub = (child, bind = false, short = false) => walk(child, src, out, bind, short);
|
|
9523
|
+
const sub = (child, bind = false, short = false, role = "read") => walk(child, src, out, bind, short, role);
|
|
9552
9524
|
switch (node.type) {
|
|
9553
9525
|
case "Identifier": {
|
|
9554
9526
|
const sigil = sigilOf(node, src);
|
|
@@ -9559,7 +9531,9 @@ function walk(node, src, out, binding, shorthand) {
|
|
|
9559
9531
|
name: sigil[1],
|
|
9560
9532
|
start: node.start,
|
|
9561
9533
|
end: node.end,
|
|
9562
|
-
shorthand
|
|
9534
|
+
shorthand,
|
|
9535
|
+
read: access !== "write",
|
|
9536
|
+
write: access !== "read"
|
|
9563
9537
|
});
|
|
9564
9538
|
return;
|
|
9565
9539
|
}
|
|
@@ -9580,10 +9554,10 @@ function walk(node, src, out, binding, shorthand) {
|
|
|
9580
9554
|
if (node.shorthand) {
|
|
9581
9555
|
const value = node.value;
|
|
9582
9556
|
if (value.type === "AssignmentPattern") {
|
|
9583
|
-
sub(value.left, binding, true);
|
|
9557
|
+
sub(value.left, binding, true, access);
|
|
9584
9558
|
sub(value.right);
|
|
9585
|
-
} else sub(value, binding, true);
|
|
9586
|
-
} else sub(node.value, binding);
|
|
9559
|
+
} else sub(value, binding, true, access);
|
|
9560
|
+
} else sub(node.value, binding, false, access);
|
|
9587
9561
|
return;
|
|
9588
9562
|
case "MethodDefinition":
|
|
9589
9563
|
case "PropertyDefinition":
|
|
@@ -9619,8 +9593,24 @@ function walk(node, src, out, binding, shorthand) {
|
|
|
9619
9593
|
sub(node.param, true);
|
|
9620
9594
|
sub(node.body);
|
|
9621
9595
|
return;
|
|
9596
|
+
case "AssignmentExpression":
|
|
9597
|
+
sub(node.left, false, false, node.operator === "=" ? "write" : "readwrite");
|
|
9598
|
+
sub(node.right);
|
|
9599
|
+
return;
|
|
9600
|
+
case "UnaryExpression":
|
|
9601
|
+
sub(node.argument, false, false, node.operator === "delete" ? "write" : "read");
|
|
9602
|
+
return;
|
|
9603
|
+
case "UpdateExpression":
|
|
9604
|
+
sub(node.argument, false, false, "readwrite");
|
|
9605
|
+
return;
|
|
9606
|
+
case "ForInStatement":
|
|
9607
|
+
case "ForOfStatement":
|
|
9608
|
+
sub(node.left, false, false, "write");
|
|
9609
|
+
sub(node.right);
|
|
9610
|
+
sub(node.body);
|
|
9611
|
+
return;
|
|
9622
9612
|
case "AssignmentPattern":
|
|
9623
|
-
sub(node.left, binding);
|
|
9613
|
+
sub(node.left, binding, false, access);
|
|
9624
9614
|
sub(node.right);
|
|
9625
9615
|
return;
|
|
9626
9616
|
case "ObjectPattern":
|
|
@@ -9630,7 +9620,7 @@ function walk(node, src, out, binding, shorthand) {
|
|
|
9630
9620
|
"properties",
|
|
9631
9621
|
"elements",
|
|
9632
9622
|
"argument"
|
|
9633
|
-
]) sub(node[key], binding);
|
|
9623
|
+
]) sub(node[key], binding, false, access);
|
|
9634
9624
|
return;
|
|
9635
9625
|
default: for (const key in node) {
|
|
9636
9626
|
if (key === "type" || key === "start" || key === "end") continue;
|
|
@@ -10485,14 +10475,15 @@ function unescapeQuoted(body) {
|
|
|
10485
10475
|
* Read the `"…"` or `'…'` string whose opening quote is at `start`.
|
|
10486
10476
|
* Returns its unescaped value and the index just past the closing quote, or
|
|
10487
10477
|
* `null` if there is no such quote at `start` or the string is unterminated.
|
|
10478
|
+
* With `tolerant`, an unterminated string instead extends to its scanned end.
|
|
10488
10479
|
*/
|
|
10489
|
-
function readQuoted(src, start) {
|
|
10480
|
+
function readQuoted(src, start, tolerant = false) {
|
|
10490
10481
|
const quote = src[start];
|
|
10491
10482
|
if (quote !== "\"" && quote !== "'") return null;
|
|
10492
10483
|
const { end, closed } = scanStringLiteral(src, start);
|
|
10493
|
-
if (!closed) return null;
|
|
10484
|
+
if (!closed && !tolerant) return null;
|
|
10494
10485
|
return {
|
|
10495
|
-
value: unescapeQuoted(src.slice(start + 1, end - 1)),
|
|
10486
|
+
value: unescapeQuoted(src.slice(start + 1, closed ? end - 1 : end)),
|
|
10496
10487
|
end
|
|
10497
10488
|
};
|
|
10498
10489
|
}
|
|
@@ -10687,11 +10678,16 @@ var MacroArgumentError = class extends Error {
|
|
|
10687
10678
|
};
|
|
10688
10679
|
/** Read `src`, at `place`, as `param` says. */
|
|
10689
10680
|
function readValue(param, src, place) {
|
|
10690
|
-
|
|
10681
|
+
const span = [place.at, place.at + src.length];
|
|
10682
|
+
place.spans?.set(param, span);
|
|
10683
|
+
place.toolingSpans?.set(param, span);
|
|
10691
10684
|
switch (param.type) {
|
|
10692
10685
|
case "string": {
|
|
10693
10686
|
const value = readWholeQuoted(src);
|
|
10694
|
-
if (value === null)
|
|
10687
|
+
if (value === null) {
|
|
10688
|
+
if (place.toolingSpans && (src[0] === "\"" || src[0] === "'")) return readText(src);
|
|
10689
|
+
throw new MacroArgumentError(param, src);
|
|
10690
|
+
}
|
|
10695
10691
|
return value;
|
|
10696
10692
|
}
|
|
10697
10693
|
case "text": return readText(src);
|
|
@@ -10722,20 +10718,21 @@ function matchAt(re, src, pos) {
|
|
|
10722
10718
|
* skipped. A keyword of a `string` parameter must be followed by a quoted
|
|
10723
10719
|
* string.
|
|
10724
10720
|
*/
|
|
10725
|
-
function readOptions(params, src, { at, spans }) {
|
|
10721
|
+
function readOptions(params, src, { at, spans, toolingSpans }) {
|
|
10726
10722
|
const options = {};
|
|
10727
10723
|
let i = 0;
|
|
10728
10724
|
while (i < src.length) {
|
|
10729
10725
|
const key = matchAt(WORD_RE$1, src, i);
|
|
10730
10726
|
if (!key) {
|
|
10731
|
-
i = readQuoted(src, i)?.end ?? i + 1;
|
|
10727
|
+
i = readQuoted(src, i, !!toolingSpans)?.end ?? i + 1;
|
|
10732
10728
|
continue;
|
|
10733
10729
|
}
|
|
10730
|
+
const keyAt = i;
|
|
10734
10731
|
i += key.length;
|
|
10735
10732
|
let val;
|
|
10736
10733
|
let j = i;
|
|
10737
10734
|
while (j < src.length && isWhitespace(src[j])) j++;
|
|
10738
|
-
const quoted = readQuoted(src, j);
|
|
10735
|
+
const quoted = readQuoted(src, j, !!toolingSpans);
|
|
10739
10736
|
const digits = quoted || j === i ? null : matchAt(NUMBER_RE$1, src, j);
|
|
10740
10737
|
if (quoted) {
|
|
10741
10738
|
val = quoted.value;
|
|
@@ -10746,7 +10743,8 @@ function readOptions(params, src, { at, spans }) {
|
|
|
10746
10743
|
}
|
|
10747
10744
|
const param = params.find((p) => p.name === key);
|
|
10748
10745
|
if (param && param.type !== "flag" && val !== void 0) spans?.set(param, [at + j, at + i]);
|
|
10749
|
-
if (param
|
|
10746
|
+
if (param) toolingSpans?.set(param, [at + keyAt, at + i]);
|
|
10747
|
+
if (param?.type === "string" && !quoted && !toolingSpans) throw new MacroArgumentError(param, matchAt(WORD_OR_PUNCT_RE, src, j) ?? "");
|
|
10750
10748
|
if (param?.type === "flag") options[key] = true;
|
|
10751
10749
|
else if (param?.type === "number") options[key] = Number(val ?? 0);
|
|
10752
10750
|
else if (param) options[key] = val;
|
|
@@ -10782,7 +10780,7 @@ function takeFlag(src, word, alone) {
|
|
|
10782
10780
|
* term with terms on both sides; `=` is an assignment at depth 0, not part
|
|
10783
10781
|
* of `==` or `!=`.
|
|
10784
10782
|
*/
|
|
10785
|
-
function findSeparator(src, word) {
|
|
10783
|
+
function findSeparator(src, word, tolerant = false) {
|
|
10786
10784
|
if (word === "=") {
|
|
10787
10785
|
const indices = topLevelIndices(src, (ch) => ch === "=");
|
|
10788
10786
|
for (let k = 0; k < indices.length; k++) {
|
|
@@ -10796,7 +10794,7 @@ function findSeparator(src, word) {
|
|
|
10796
10794
|
return null;
|
|
10797
10795
|
}
|
|
10798
10796
|
const spans = terms(src);
|
|
10799
|
-
for (let k = 1; k < spans.length - 1; k++) if (slice(src, spans[k]) === word) return [spans[k - 1][1], spans[k + 1][0]];
|
|
10797
|
+
for (let k = 1; k < spans.length - (tolerant ? 0 : 1); k++) if (slice(src, spans[k]) === word) return [spans[k - 1][1], spans[k + 1]?.[0] ?? src.length];
|
|
10800
10798
|
return null;
|
|
10801
10799
|
}
|
|
10802
10800
|
var takesRest = (param) => param.type === "expression" || param.type === "statements" || param.type === "passage" || param.type === "text";
|
|
@@ -10809,23 +10807,36 @@ function trimmed(src, at) {
|
|
|
10809
10807
|
* Read the parameters of one group from `src` (`null`: no text), which is
|
|
10810
10808
|
* at `at` in the raw arguments, into `args`.
|
|
10811
10809
|
*/
|
|
10812
|
-
function readGroup(params, src, args, { at: srcAt, spans }) {
|
|
10810
|
+
function readGroup(params, src, args, { at: srcAt, spans, toolingSpans }) {
|
|
10813
10811
|
let [text, at] = trimmed(src ?? "", srcAt);
|
|
10814
10812
|
const alone = !params.some((p) => p.type !== "flag" && p.required);
|
|
10815
10813
|
for (const flag of params.filter((p) => p.type === "flag")) {
|
|
10816
10814
|
const rest = src === null ? null : takeFlag(text, flag.name, alone);
|
|
10817
10815
|
args[flag.name] = rest !== null;
|
|
10818
|
-
if (rest)
|
|
10816
|
+
if (rest) {
|
|
10817
|
+
const flagSpan = rest[0] > 0 ? [0, text.indexOf(flag.name) + flag.name.length] : [text.lastIndexOf(flag.name), text.length];
|
|
10818
|
+
toolingSpans?.set(flag, [at + flagSpan[0], at + flagSpan[1]]);
|
|
10819
|
+
[text, at] = trimmed(slice(text, rest), at + rest[0]);
|
|
10820
|
+
}
|
|
10819
10821
|
}
|
|
10820
10822
|
const positional = params.filter((p) => p.type !== "flag");
|
|
10821
10823
|
if (positional.length === 0) return;
|
|
10822
10824
|
const lastText = positional.findIndex((p, i) => takesRest(p) && !positional.slice(i + 1).some(takesRest));
|
|
10823
10825
|
const restIndex = lastText < 0 ? positional.length - 1 : lastText;
|
|
10824
10826
|
/** Read `param` from the text from `start` to `end`. */
|
|
10825
|
-
const read = (param, [start, end]
|
|
10826
|
-
|
|
10827
|
-
|
|
10828
|
-
|
|
10827
|
+
const read = (param, [start, end], tolerant = true) => {
|
|
10828
|
+
if (start === end) return unset(param);
|
|
10829
|
+
try {
|
|
10830
|
+
return readValue(param, text.slice(start, end), {
|
|
10831
|
+
at: at + start,
|
|
10832
|
+
spans,
|
|
10833
|
+
toolingSpans
|
|
10834
|
+
});
|
|
10835
|
+
} catch (error) {
|
|
10836
|
+
if (toolingSpans && tolerant && error instanceof MacroArgumentError) return;
|
|
10837
|
+
throw error;
|
|
10838
|
+
}
|
|
10839
|
+
};
|
|
10829
10840
|
if (positional.length === 1) {
|
|
10830
10841
|
args[positional[0].name] = read(positional[0], [0, text.length]);
|
|
10831
10842
|
return;
|
|
@@ -10836,7 +10847,7 @@ function readGroup(params, src, args, { at: srcAt, spans }) {
|
|
|
10836
10847
|
for (const param of positional.slice(0, restIndex)) args[param.name] = from < to ? read(param, parts[from++]) : unset(param);
|
|
10837
10848
|
const readTrailing = (param, span) => {
|
|
10838
10849
|
try {
|
|
10839
|
-
return read(param, span);
|
|
10850
|
+
return read(param, span, false);
|
|
10840
10851
|
} catch (error) {
|
|
10841
10852
|
if (error instanceof MacroArgumentError) return void 0;
|
|
10842
10853
|
throw error;
|
|
@@ -10846,7 +10857,10 @@ function readGroup(params, src, args, { at: srcAt, spans }) {
|
|
|
10846
10857
|
const value = from < to && !endsWithOperator(text.slice(0, parts[to - 1][0])) ? readTrailing(param, parts[to - 1]) : void 0;
|
|
10847
10858
|
args[param.name] = value ?? unset(param);
|
|
10848
10859
|
if (value !== void 0) to--;
|
|
10849
|
-
else
|
|
10860
|
+
else {
|
|
10861
|
+
spans?.delete(param);
|
|
10862
|
+
toolingSpans?.delete(param);
|
|
10863
|
+
}
|
|
10850
10864
|
}
|
|
10851
10865
|
const rest = positional[restIndex];
|
|
10852
10866
|
args[rest.name] = from < to ? read(rest, [parts[from][0], parts[to - 1][1]]) : unset(rest);
|
|
@@ -10855,18 +10869,23 @@ function readGroup(params, src, args, { at: srcAt, spans }) {
|
|
|
10855
10869
|
* Read `rawArgs` into the declared `parameters` (see above). With `spans`,
|
|
10856
10870
|
* note there where each argument set was read.
|
|
10857
10871
|
*/
|
|
10858
|
-
function parseMacroArgs(rawArgs, parameters, spans) {
|
|
10872
|
+
function parseMacroArgs(rawArgs, parameters, spans, toolingSpans) {
|
|
10859
10873
|
const args = {};
|
|
10860
10874
|
let [rest, restAt] = trimmed(rawArgs, 0);
|
|
10861
10875
|
let groupStart = 0;
|
|
10862
10876
|
parameters.forEach((separator, i) => {
|
|
10863
10877
|
if (separator.type !== "separator") return;
|
|
10864
|
-
const at = rest === null ? null : findSeparator(rest, separator.name);
|
|
10878
|
+
const at = rest === null ? null : findSeparator(rest, separator.name, !!toolingSpans);
|
|
10865
10879
|
args[separator.name] = at !== null;
|
|
10880
|
+
if (at && toolingSpans) {
|
|
10881
|
+
const start = restAt + at[0] + rest.slice(at[0], at[1]).indexOf(separator.name);
|
|
10882
|
+
toolingSpans.set(separator, [start, start + separator.name.length]);
|
|
10883
|
+
}
|
|
10866
10884
|
const before = at ? rest.slice(0, at[0]) : rest;
|
|
10867
10885
|
readGroup(parameters.slice(groupStart, i), before, args, {
|
|
10868
10886
|
at: restAt,
|
|
10869
|
-
spans
|
|
10887
|
+
spans,
|
|
10888
|
+
toolingSpans
|
|
10870
10889
|
});
|
|
10871
10890
|
rest = at ? rest.slice(at[1]) : null;
|
|
10872
10891
|
if (at) restAt += at[1];
|
|
@@ -10874,7 +10893,8 @@ function parseMacroArgs(rawArgs, parameters, spans) {
|
|
|
10874
10893
|
});
|
|
10875
10894
|
readGroup(parameters.slice(groupStart), rest, args, {
|
|
10876
10895
|
at: restAt,
|
|
10877
|
-
spans
|
|
10896
|
+
spans,
|
|
10897
|
+
toolingSpans
|
|
10878
10898
|
});
|
|
10879
10899
|
return args;
|
|
10880
10900
|
}
|
|
@@ -10907,6 +10927,34 @@ function evaluatePassageName(expr, evaluate, { storyData, currentPassage }) {
|
|
|
10907
10927
|
if (storyData && !storyData.passages.has(name)) throw noPassageError(name, currentPassage);
|
|
10908
10928
|
return name;
|
|
10909
10929
|
}
|
|
10930
|
+
/**
|
|
10931
|
+
* The runtime's argument grouping, tolerant of missing or unclosed strings.
|
|
10932
|
+
* Includes quotes, flags, separators and option keywords; offsets are into
|
|
10933
|
+
* rawArgs. Absent arguments have no span. Options also have their enclosing
|
|
10934
|
+
* parameter span, so consumers can choose the most specific overlapping span.
|
|
10935
|
+
* No expressions are evaluated. Parameter identities are preserved.
|
|
10936
|
+
*/
|
|
10937
|
+
function macroArgumentSpans(rawArgs, parameters) {
|
|
10938
|
+
const spans = /* @__PURE__ */ new Map();
|
|
10939
|
+
parseMacroArgs(rawArgs, parameters, void 0, spans);
|
|
10940
|
+
const result = [];
|
|
10941
|
+
const add = (params, path) => {
|
|
10942
|
+
params.forEach((parameter, index) => {
|
|
10943
|
+
const parameterPath = [...path, index];
|
|
10944
|
+
const span = spans.get(parameter);
|
|
10945
|
+
if (span) result.push({
|
|
10946
|
+
parameter,
|
|
10947
|
+
parameterIndex: parameterPath[0],
|
|
10948
|
+
parameterPath,
|
|
10949
|
+
start: span[0],
|
|
10950
|
+
end: span[1]
|
|
10951
|
+
});
|
|
10952
|
+
if (parameter.parameters) add(parameter.parameters, parameterPath);
|
|
10953
|
+
});
|
|
10954
|
+
};
|
|
10955
|
+
add(parameters, []);
|
|
10956
|
+
return result.sort((a, b) => a.start - b.start || b.end - a.end);
|
|
10957
|
+
}
|
|
10910
10958
|
/** The parameters of the built-in sub-macros that take arguments. */
|
|
10911
10959
|
var SUB_MACRO_PARAMETERS = /* @__PURE__ */ new Map([["option", [{
|
|
10912
10960
|
name: "value",
|
|
@@ -11039,10 +11087,10 @@ function parameterLookup(macros) {
|
|
|
11039
11087
|
const widgets = new NameMap();
|
|
11040
11088
|
for (const { name, parameters: params, interpolate, widget } of macros) {
|
|
11041
11089
|
if (params) parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
|
|
11042
|
-
|
|
11043
|
-
if (declared) widgets.set(name, declared);
|
|
11090
|
+
if (widget) widgets.set(name, widgetParameters(widget.params, widget.holds) ?? []);
|
|
11044
11091
|
}
|
|
11045
11092
|
const lookup = (name) => {
|
|
11093
|
+
if (widgets.has(name)) return void 0;
|
|
11046
11094
|
let params = parameters.get(name);
|
|
11047
11095
|
if (!params) {
|
|
11048
11096
|
const sub = subMacroParameters(name);
|
|
@@ -11115,7 +11163,8 @@ function* codeAndText(src, tokens, parametersOf) {
|
|
|
11115
11163
|
}
|
|
11116
11164
|
else if (token.type === "macro" && !token.isClose) {
|
|
11117
11165
|
const name = token.name.toLowerCase();
|
|
11118
|
-
|
|
11166
|
+
const widget = parametersOf.widget?.(name);
|
|
11167
|
+
if (name === "do" && !widget) {
|
|
11119
11168
|
const close = closeOfTextBody(t, name);
|
|
11120
11169
|
if (close < 0) continue;
|
|
11121
11170
|
yield {
|
|
@@ -11129,7 +11178,7 @@ function* codeAndText(src, tokens, parametersOf) {
|
|
|
11129
11178
|
continue;
|
|
11130
11179
|
}
|
|
11131
11180
|
const args = token.rawArgs;
|
|
11132
|
-
if (PASSAGE_BODIES.has(name)) {
|
|
11181
|
+
if (PASSAGE_BODIES.has(name) && !widget) {
|
|
11133
11182
|
const close = closeOfTextBody(t, name);
|
|
11134
11183
|
if (close > t + 1) {
|
|
11135
11184
|
const text = src.slice(token.end, tokens[close].start);
|
|
@@ -11147,7 +11196,7 @@ function* codeAndText(src, tokens, parametersOf) {
|
|
|
11147
11196
|
if (!args) continue;
|
|
11148
11197
|
const label = `{${token.name} ${args}}`;
|
|
11149
11198
|
const argsAt = token.argsStart;
|
|
11150
|
-
if (CONDITION_MACROS.has(name)) {
|
|
11199
|
+
if (CONDITION_MACROS.has(name) && !widget) {
|
|
11151
11200
|
yield {
|
|
11152
11201
|
kind: "code",
|
|
11153
11202
|
code: args,
|
|
@@ -11159,7 +11208,6 @@ function* codeAndText(src, tokens, parametersOf) {
|
|
|
11159
11208
|
}
|
|
11160
11209
|
const params = parametersOf(name);
|
|
11161
11210
|
if (params) yield* argPieces(args, argsAt, params, token.name, label);
|
|
11162
|
-
const widget = parametersOf.widget?.(name);
|
|
11163
11211
|
if (widget) yield* widgetPieces(args, argsAt, widget, token.name, label);
|
|
11164
11212
|
}
|
|
11165
11213
|
}
|
|
@@ -12379,7 +12427,7 @@ function shapeSchema(shape) {
|
|
|
12379
12427
|
function initializerSyntaxError(expr) {
|
|
12380
12428
|
const prefix = "return (";
|
|
12381
12429
|
try {
|
|
12382
|
-
parse$1(prefix + expr + ")", {
|
|
12430
|
+
parse$1(prefix + expr + "\n)", {
|
|
12383
12431
|
ecmaVersion: "latest",
|
|
12384
12432
|
allowReturnOutsideFunction: true
|
|
12385
12433
|
});
|
|
@@ -12488,7 +12536,7 @@ function parseStoryVariables(content, sigil = "$") {
|
|
|
12488
12536
|
const expr = content.slice(valueStart, valueEnd);
|
|
12489
12537
|
let value;
|
|
12490
12538
|
try {
|
|
12491
|
-
value = new Function("return (" + expr + ")")();
|
|
12539
|
+
value = new Function("return (" + expr + "\n)")();
|
|
12492
12540
|
} catch (err) {
|
|
12493
12541
|
throw new Error(`${passageName}: Failed to evaluate "${sigil}${name} = ${expr}": ${errorMessage(err)}`);
|
|
12494
12542
|
}
|
|
@@ -12721,7 +12769,7 @@ function collectTokenRefs(content, tokens, at, emit, options) {
|
|
|
12721
12769
|
else if (token.type === "macro" && !token.isClose) {
|
|
12722
12770
|
scanArgs(token, parametersOf, at, emit, options);
|
|
12723
12771
|
scanSelectors(content, token, at, emit, options);
|
|
12724
|
-
if (storeVarMacros.has(token.name.toLowerCase())) {
|
|
12772
|
+
if (storeVarMacros.has(token.name.toLowerCase()) && !parametersOf.widget?.(token.name)) {
|
|
12725
12773
|
const lead = token.rawArgs.length - token.rawArgs.trimStart().length;
|
|
12726
12774
|
const first = token.rawArgs.trim().split(/\s+/)[0] ?? "";
|
|
12727
12775
|
const quoted = QUOTED_VAR_ARG_RE.exec(first);
|
|
@@ -12737,7 +12785,7 @@ function collectTokenRefs(content, tokens, at, emit, options) {
|
|
|
12737
12785
|
});
|
|
12738
12786
|
} else if (!/^["'`]/.test(first)) scanCode(first, (i) => at(token.argsStart + lead + i), emit);
|
|
12739
12787
|
}
|
|
12740
|
-
if (token.name.toLowerCase() === "do") {
|
|
12788
|
+
if (token.name.toLowerCase() === "do" && !parametersOf.widget?.(token.name)) {
|
|
12741
12789
|
let close = t + 1;
|
|
12742
12790
|
while (close < tokens.length) {
|
|
12743
12791
|
const c = tokens[close];
|
|
@@ -25392,4 +25440,4 @@ function validateVariableReferences(passages, declarations, macros = []) {
|
|
|
25392
25440
|
return checkVariableReferences(list.filter((p) => !p.tags?.some((t) => NOT_MARKUP_TAGS.includes(t))), declarations, referenceOptions(macros, false, list));
|
|
25393
25441
|
}
|
|
25394
25442
|
//#endregion
|
|
25395
|
-
export { MarkupError, SIGIL_SCOPES, checkParameterTypes, collectStoryPassageReferences, discoverMacros, endsWithOperator, evaluatePassageName, findCodeEnd, formatDiagnostic, formatPassage, isBlockMacro, isSigil, lexJs, lexTemplate, pairMarkup, parseDeclarations, parseSelectors, parseStoryVariables, parseWidgetDef, passagePieces, passagePiecesOfTokens, passageTarget, pieceOffset, readQuoted, scanStringLiteral, splitArgs, splitIncludeFlag, splitTopLevel, stripLooseQuotes, tokenizeMarkup, tokenizeMarkupTolerant, transform, unescapeQuoted, validateStoryMarkup, validateVariableReferences, variableReferences, widgetDefinitions };
|
|
25443
|
+
export { MarkupError, SIGIL_SCOPES, checkParameterTypes, collectStoryPassageReferences, discoverMacros, effectiveWidgetDefinitions, endsWithOperator, evaluatePassageName, findCodeEnd, formatDiagnostic, formatPassage, isBlockMacro, isSigil, lexJs, lexTemplate, macroArgumentSpans, pairMarkup, parseCode, parseDeclarations, parseSelectors, parseStoryVariables, parseWidgetDef, passagePieces, passagePiecesOfTokens, passageTarget, pieceOffset, readQuoted, scanStringLiteral, splitArgs, splitIncludeFlag, splitTopLevel, stripLooseQuotes, tokenizeMarkup, tokenizeMarkupTolerant, transform, unescapeQuoted, validateStoryMarkup, validateVariableReferences, variableReferences, widgetDefinitions };
|
package/dist/pkg/tooling.js
CHANGED
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rohal12/spindle",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.62.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A Preact-based story format for Twine 2.",
|
|
6
6
|
"license": "Unlicense",
|
|
7
7
|
"author": "Rohal12",
|
|
8
8
|
"engines": {
|
|
9
|
-
"node": ">=
|
|
9
|
+
"node": ">=24"
|
|
10
10
|
},
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"access": "public"
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
],
|
|
58
58
|
"scripts": {
|
|
59
59
|
"build": "vite build && bun run scripts/dump-macro-registry.ts && bun run scripts/build-format.ts",
|
|
60
|
-
"compile": "
|
|
60
|
+
"compile": "node scripts/compile-story.ts",
|
|
61
61
|
"preview": "bun run build && bun run compile",
|
|
62
62
|
"test": "vitest run",
|
|
63
63
|
"test:coverage": "vitest run --coverage",
|
|
@@ -84,7 +84,7 @@
|
|
|
84
84
|
"zustand": "^5.0.15"
|
|
85
85
|
},
|
|
86
86
|
"peerDependencies": {
|
|
87
|
-
"@rohal12/twee-ts": "^
|
|
87
|
+
"@rohal12/twee-ts": "^2.0.0",
|
|
88
88
|
"happy-dom": ">=20.14.6"
|
|
89
89
|
},
|
|
90
90
|
"peerDependenciesMeta": {
|
|
@@ -101,7 +101,7 @@
|
|
|
101
101
|
"devDependencies": {
|
|
102
102
|
"@fast-check/vitest": "^0.5.0",
|
|
103
103
|
"@preact/preset-vite": "^2.10.6",
|
|
104
|
-
"@rohal12/twee-ts": "^
|
|
104
|
+
"@rohal12/twee-ts": "^2.0.0",
|
|
105
105
|
"@types/codemirror": "^5.60.18",
|
|
106
106
|
"@types/node": "^26.6.5",
|
|
107
107
|
"@types/react": "^19.3.0",
|
package/src/code-check.ts
CHANGED
|
@@ -226,9 +226,10 @@ export function widgetParameters(
|
|
|
226
226
|
export interface ParametersOf {
|
|
227
227
|
(macro: string): readonly ParameterDef[] | undefined;
|
|
228
228
|
/**
|
|
229
|
-
* The parameters of the widget `name
|
|
230
|
-
* holds. Its
|
|
231
|
-
*
|
|
229
|
+
* The parameters of the widget `name`, or an empty array if it declares
|
|
230
|
+
* no holds. Its presence identifies a widget even without holds, so a
|
|
231
|
+
* shadowed macro's metadata cannot apply. Arguments are separated by
|
|
232
|
+
* commas or spaces (see splitArgs), not by parseMacroArgs.
|
|
232
233
|
*/
|
|
233
234
|
widget?(name: string): readonly WidgetParameter[] | undefined;
|
|
234
235
|
}
|
|
@@ -278,10 +279,11 @@ export function parameterLookup(
|
|
|
278
279
|
for (const { name, parameters: params, interpolate, widget } of macros) {
|
|
279
280
|
if (params)
|
|
280
281
|
parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
|
|
281
|
-
|
|
282
|
-
|
|
282
|
+
if (widget)
|
|
283
|
+
widgets.set(name, widgetParameters(widget.params, widget.holds) ?? []);
|
|
283
284
|
}
|
|
284
285
|
const lookup: ParametersOf = (name) => {
|
|
286
|
+
if (widgets.has(name)) return undefined;
|
|
285
287
|
let params = parameters.get(name);
|
|
286
288
|
if (!params) {
|
|
287
289
|
const sub = subMacroParameters(name);
|
|
@@ -396,7 +398,8 @@ export function* codeAndText(
|
|
|
396
398
|
}
|
|
397
399
|
} else if (token.type === 'macro' && !token.isClose) {
|
|
398
400
|
const name = token.name.toLowerCase();
|
|
399
|
-
|
|
401
|
+
const widget = parametersOf.widget?.(name);
|
|
402
|
+
if (name === 'do' && !widget) {
|
|
400
403
|
const close = closeOfTextBody(t, name);
|
|
401
404
|
if (close < 0) continue;
|
|
402
405
|
const body = src.slice(token.end, tokens[close]!.start);
|
|
@@ -411,7 +414,7 @@ export function* codeAndText(
|
|
|
411
414
|
continue;
|
|
412
415
|
}
|
|
413
416
|
const args = token.rawArgs;
|
|
414
|
-
if (PASSAGE_BODIES.has(name)) {
|
|
417
|
+
if (PASSAGE_BODIES.has(name) && !widget) {
|
|
415
418
|
// The body names the passage when it is only text
|
|
416
419
|
const close = closeOfTextBody(t, name);
|
|
417
420
|
if (close > t + 1) {
|
|
@@ -430,7 +433,7 @@ export function* codeAndText(
|
|
|
430
433
|
if (!args) continue;
|
|
431
434
|
const label = `{${token.name} ${args}}`;
|
|
432
435
|
const argsAt = token.argsStart;
|
|
433
|
-
if (CONDITION_MACROS.has(name)) {
|
|
436
|
+
if (CONDITION_MACROS.has(name) && !widget) {
|
|
434
437
|
yield {
|
|
435
438
|
kind: 'code',
|
|
436
439
|
code: args,
|
|
@@ -442,7 +445,6 @@ export function* codeAndText(
|
|
|
442
445
|
}
|
|
443
446
|
const params = parametersOf(name);
|
|
444
447
|
if (params) yield* argPieces(args, argsAt, params, token.name, label);
|
|
445
|
-
const widget = parametersOf.widget?.(name);
|
|
446
448
|
if (widget) yield* widgetPieces(args, argsAt, widget, token.name, label);
|
|
447
449
|
}
|
|
448
450
|
}
|