@rohal12/spindle 0.59.28 → 0.59.29
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 +163 -33
- package/dist/pkg/story-variables.js +245 -152
- package/package.json +1 -1
- package/src/code-check.ts +104 -8
- package/src/components/macros/Widget.tsx +2 -0
- package/src/components/macros/arg-utils.ts +58 -21
- package/src/index.tsx +2 -2
- package/src/store.ts +27 -1
- package/src/story-api.ts +2 -1
- package/src/tooling.ts +16 -3
- package/src/widgets/register-widget-def.ts +7 -8
- package/src/widgets/widget-def.ts +41 -4
- package/src/widgets/widget-registry.ts +14 -0
|
@@ -4833,7 +4833,7 @@ var skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
|
|
|
4833
4833
|
var ref = Object.prototype;
|
|
4834
4834
|
var hasOwnProperty = ref.hasOwnProperty;
|
|
4835
4835
|
var toString = ref.toString;
|
|
4836
|
-
var hasOwn = Object.hasOwn || (function(obj, propName) {
|
|
4836
|
+
var hasOwn$1 = Object.hasOwn || (function(obj, propName) {
|
|
4837
4837
|
return hasOwnProperty.call(obj, propName);
|
|
4838
4838
|
});
|
|
4839
4839
|
var isArray = Array.isArray || (function(obj) {
|
|
@@ -4895,7 +4895,7 @@ var defaultOptions = {
|
|
|
4895
4895
|
var warnedAboutEcmaVersion = false;
|
|
4896
4896
|
function getOptions(opts) {
|
|
4897
4897
|
var options = {};
|
|
4898
|
-
for (var opt in defaultOptions) options[opt] = opts && hasOwn(opts, opt) ? opts[opt] : defaultOptions[opt];
|
|
4898
|
+
for (var opt in defaultOptions) options[opt] = opts && hasOwn$1(opts, opt) ? opts[opt] : defaultOptions[opt];
|
|
4899
4899
|
if (options.ecmaVersion === "latest") options.ecmaVersion = 1e8;
|
|
4900
4900
|
else if (options.ecmaVersion == null) {
|
|
4901
4901
|
if (!warnedAboutEcmaVersion && typeof console === "object" && console.warn) {
|
|
@@ -5814,7 +5814,7 @@ pp$8.exitClassBody = function() {
|
|
|
5814
5814
|
var parent = len === 0 ? null : this.privateNameStack[len - 1];
|
|
5815
5815
|
for (var i = 0; i < used.length; ++i) {
|
|
5816
5816
|
var id = used[i];
|
|
5817
|
-
if (!hasOwn(declared, id.name)) {
|
|
5817
|
+
if (!hasOwn$1(declared, id.name)) {
|
|
5818
5818
|
if (parent) parent.used.push(id);
|
|
5819
5819
|
else this.raiseRecoverable(id.start, "Private field '#" + id.name + "' must be declared in an enclosing class");
|
|
5820
5820
|
}
|
|
@@ -5910,7 +5910,7 @@ pp$8.parseExportDefaultDeclaration = function() {
|
|
|
5910
5910
|
pp$8.checkExport = function(exports$1, name, pos) {
|
|
5911
5911
|
if (!exports$1) return;
|
|
5912
5912
|
if (typeof name !== "string") name = name.type === "Identifier" ? name.name : name.value;
|
|
5913
|
-
if (hasOwn(exports$1, name)) this.raiseRecoverable(pos, "Duplicate export '" + name + "'");
|
|
5913
|
+
if (hasOwn$1(exports$1, name)) this.raiseRecoverable(pos, "Duplicate export '" + name + "'");
|
|
5914
5914
|
exports$1[name] = true;
|
|
5915
5915
|
};
|
|
5916
5916
|
pp$8.checkPatternExport = function(exports$1, pat) {
|
|
@@ -6029,7 +6029,7 @@ pp$8.parseWithClause = function() {
|
|
|
6029
6029
|
} else first = false;
|
|
6030
6030
|
var attr = this.parseImportAttribute();
|
|
6031
6031
|
var keyName = attr.key.type === "Identifier" ? attr.key.name : attr.key.value;
|
|
6032
|
-
if (hasOwn(attributeKeys, keyName)) this.raiseRecoverable(attr.key.start, "Duplicate attribute key '" + keyName + "'");
|
|
6032
|
+
if (hasOwn$1(attributeKeys, keyName)) this.raiseRecoverable(attr.key.start, "Duplicate attribute key '" + keyName + "'");
|
|
6033
6033
|
attributeKeys[keyName] = true;
|
|
6034
6034
|
nodes.push(attr);
|
|
6035
6035
|
}
|
|
@@ -6187,7 +6187,7 @@ pp$7.checkLValSimple = function(expr, bindingType, checkClashes) {
|
|
|
6187
6187
|
if (isBind) {
|
|
6188
6188
|
if (bindingType === BIND_LEXICAL && expr.name === "let") this.raiseRecoverable(expr.start, "let is disallowed as a lexically bound name");
|
|
6189
6189
|
if (checkClashes) {
|
|
6190
|
-
if (hasOwn(checkClashes, expr.name)) this.raiseRecoverable(expr.start, "Argument name clash");
|
|
6190
|
+
if (hasOwn$1(checkClashes, expr.name)) this.raiseRecoverable(expr.start, "Argument name clash");
|
|
6191
6191
|
checkClashes[expr.name] = true;
|
|
6192
6192
|
}
|
|
6193
6193
|
if (bindingType !== BIND_OUTSIDE) this.declareName(expr.name, bindingType, expr.start);
|
|
@@ -7900,7 +7900,7 @@ pp$1.regexp_eatUnicodePropertyValueExpression = function(state) {
|
|
|
7900
7900
|
return CharSetNone;
|
|
7901
7901
|
};
|
|
7902
7902
|
pp$1.regexp_validateUnicodePropertyNameAndValue = function(state, name, value) {
|
|
7903
|
-
if (!hasOwn(state.unicodeProperties.nonBinary, name)) state.raise("Invalid property name");
|
|
7903
|
+
if (!hasOwn$1(state.unicodeProperties.nonBinary, name)) state.raise("Invalid property name");
|
|
7904
7904
|
if (!state.unicodeProperties.nonBinary[name].test(value)) state.raise("Invalid property value");
|
|
7905
7905
|
};
|
|
7906
7906
|
pp$1.regexp_validateUnicodePropertyNameOrValue = function(state, nameOrValue) {
|
|
@@ -10161,19 +10161,27 @@ function topLevelIndices(src, isSeparator) {
|
|
|
10161
10161
|
return indices;
|
|
10162
10162
|
}
|
|
10163
10163
|
/**
|
|
10164
|
-
*
|
|
10165
|
-
*
|
|
10164
|
+
* The segments of `src` between its depth-0 separators (see
|
|
10165
|
+
* `topLevelIndices`), as spans: untrimmed, and empty segments are kept, so
|
|
10166
10166
|
* `n` separators always give `n + 1` segments.
|
|
10167
10167
|
*/
|
|
10168
|
-
function
|
|
10169
|
-
const
|
|
10168
|
+
function topLevelSpans(src, isSeparator) {
|
|
10169
|
+
const spans = [];
|
|
10170
10170
|
let from = 0;
|
|
10171
10171
|
for (const i of topLevelIndices(src, isSeparator)) {
|
|
10172
|
-
|
|
10172
|
+
spans.push([from, i]);
|
|
10173
10173
|
from = i + 1;
|
|
10174
10174
|
}
|
|
10175
|
-
|
|
10176
|
-
return
|
|
10175
|
+
spans.push([from, src.length]);
|
|
10176
|
+
return spans;
|
|
10177
|
+
}
|
|
10178
|
+
/**
|
|
10179
|
+
* Split `src` at every depth-0 separator (see `topLevelIndices`). Segments
|
|
10180
|
+
* are returned as written, untrimmed, and empty segments are kept, so
|
|
10181
|
+
* `n` separators always give `n + 1` segments.
|
|
10182
|
+
*/
|
|
10183
|
+
function splitTopLevel(src, isSeparator) {
|
|
10184
|
+
return topLevelSpans(src, isSeparator).map(([a, b]) => src.slice(a, b));
|
|
10177
10185
|
}
|
|
10178
10186
|
/**
|
|
10179
10187
|
* Check whether a whitespace-delimited token looks like a standalone value
|
|
@@ -10198,29 +10206,44 @@ function startsLikeValue(token) {
|
|
|
10198
10206
|
* Try to split a raw string on whitespace at depth 0 (respecting strings,
|
|
10199
10207
|
* template literals, parentheses, brackets, and braces). Each resulting token
|
|
10200
10208
|
* must pass `isStandaloneValue()` or the split is rejected and `null` is
|
|
10201
|
-
* returned.
|
|
10209
|
+
* returned. The spans are those of the tokens in `raw`.
|
|
10202
10210
|
*/
|
|
10203
10211
|
function trySplitOnWhitespace(raw) {
|
|
10204
|
-
const args =
|
|
10212
|
+
const args = topLevelSpans(raw, isWhitespace).filter(([a, b]) => b > a);
|
|
10205
10213
|
if (args.length < 2) return null;
|
|
10206
|
-
for (const
|
|
10214
|
+
for (const [a, b] of args) if (!isStandaloneValue(raw.slice(a, b))) return null;
|
|
10207
10215
|
return args;
|
|
10208
10216
|
}
|
|
10217
|
+
/** `span` less the whitespace at its ends. */
|
|
10218
|
+
function trimmed$1(src, [start, end]) {
|
|
10219
|
+
const text = src.slice(start, end);
|
|
10220
|
+
const lead = text.length - text.trimStart().length;
|
|
10221
|
+
return [start + lead, Math.max(start + lead, end - (text.length - text.trimEnd().length))];
|
|
10222
|
+
}
|
|
10209
10223
|
/**
|
|
10210
|
-
*
|
|
10211
|
-
*
|
|
10212
|
-
* string literals separated by whitespace (e.g. `"Label" "target"`).
|
|
10224
|
+
* Where the arguments in `raw` are written (see `splitArgs`), as spans of
|
|
10225
|
+
* `raw`.
|
|
10213
10226
|
*/
|
|
10214
|
-
function
|
|
10215
|
-
const args =
|
|
10227
|
+
function splitArgSpans(raw) {
|
|
10228
|
+
const args = topLevelSpans(raw, (ch) => ch === ",").map((span) => trimmed$1(raw, span));
|
|
10216
10229
|
const hasComma = args.length > 1;
|
|
10217
|
-
|
|
10230
|
+
const last = args[args.length - 1];
|
|
10231
|
+
if (last[0] === last[1]) args.pop();
|
|
10218
10232
|
if (!hasComma && args.length === 1) {
|
|
10219
|
-
const
|
|
10220
|
-
|
|
10233
|
+
const [start, end] = args[0];
|
|
10234
|
+
const split = trySplitOnWhitespace(raw.slice(start, end));
|
|
10235
|
+
if (split) return split.map(([a, b]) => [start + a, start + b]);
|
|
10221
10236
|
}
|
|
10222
10237
|
return args;
|
|
10223
10238
|
}
|
|
10239
|
+
/**
|
|
10240
|
+
* Split rawArgs by commas, respecting parentheses, brackets, braces, and
|
|
10241
|
+
* strings. When no top-level commas are present, also supports adjacent quoted
|
|
10242
|
+
* string literals separated by whitespace (e.g. `"Label" "target"`).
|
|
10243
|
+
*/
|
|
10244
|
+
function splitArgs(raw) {
|
|
10245
|
+
return splitArgSpans(raw).map(([a, b]) => raw.slice(a, b));
|
|
10246
|
+
}
|
|
10224
10247
|
//#endregion
|
|
10225
10248
|
//#region src/components/macros/macro-args.ts
|
|
10226
10249
|
/**
|
|
@@ -10486,6 +10509,86 @@ function evaluatePassageName(expr, evaluate, { storyData, currentPassage }) {
|
|
|
10486
10509
|
if (storyData && !storyData.passages.has(name)) throw noPassageError(name, currentPassage);
|
|
10487
10510
|
return name;
|
|
10488
10511
|
}
|
|
10512
|
+
new NameMap();
|
|
10513
|
+
new NameMap();
|
|
10514
|
+
new NameSet();
|
|
10515
|
+
/**
|
|
10516
|
+
* How a macro argument is read (see components/macros/macro-args.ts). Every
|
|
10517
|
+
* declared parameter names one: there is no default.
|
|
10518
|
+
* Quoted strings accept `\"`, `\'` and `\\` escapes.
|
|
10519
|
+
* - `expression`: code, as written.
|
|
10520
|
+
* - `statements`: code run as statements (`{set}`), as written.
|
|
10521
|
+
* - `passage`: a passage name: a quoted string or an expression, as written.
|
|
10522
|
+
* - `variable`: a variable reference such as `$name` or `"$name"`, as written.
|
|
10523
|
+
* - `string`: one quoted string; anything else leaves the argument unset.
|
|
10524
|
+
* - `text`: one quoted string, or text with any loose quotes stripped.
|
|
10525
|
+
* - `names`: a comma-separated list of names (`@item, @i`).
|
|
10526
|
+
* - `delay`: a duration (`2s`, `500ms`, `300`) in milliseconds.
|
|
10527
|
+
* - `number`: a number.
|
|
10528
|
+
* - `flag`: a keyword, the parameter's name, at the start or end; a boolean.
|
|
10529
|
+
* - `separator`: a word (`of`) or `=` separating the parameters before it
|
|
10530
|
+
* from those after it; a boolean.
|
|
10531
|
+
* - `options`: keywords, the names of its `parameters`, each followed by a
|
|
10532
|
+
* quoted string or a number unless it is a flag.
|
|
10533
|
+
*/
|
|
10534
|
+
var PARAMETER_TYPES = [
|
|
10535
|
+
"expression",
|
|
10536
|
+
"statements",
|
|
10537
|
+
"passage",
|
|
10538
|
+
"variable",
|
|
10539
|
+
"string",
|
|
10540
|
+
"text",
|
|
10541
|
+
"names",
|
|
10542
|
+
"delay",
|
|
10543
|
+
"number",
|
|
10544
|
+
"flag",
|
|
10545
|
+
"separator",
|
|
10546
|
+
"options"
|
|
10547
|
+
];
|
|
10548
|
+
/**
|
|
10549
|
+
* What the value of a `string` or `text` argument holds, for the check at
|
|
10550
|
+
* story start and for tooling (see code-check.ts):
|
|
10551
|
+
* - `markup`: markup the macro renders (`{button}`'s label): its markup
|
|
10552
|
+
* is checked.
|
|
10553
|
+
* - `text`: plain text the macro uses as written (`{checkbox}`'s label).
|
|
10554
|
+
* - `passage`: a passage name (`{watch}`'s `goto`): the passage must exist.
|
|
10555
|
+
* - `expression`, `statements`: code (`{watch}`'s condition and `run`): it
|
|
10556
|
+
* is checked as code, and its variable references against the schema.
|
|
10557
|
+
*
|
|
10558
|
+
* Without it, the argument of a macro with `interpolate` holds `markup`,
|
|
10559
|
+
* any other `text`.
|
|
10560
|
+
*/
|
|
10561
|
+
var STRING_HOLDS = [
|
|
10562
|
+
"markup",
|
|
10563
|
+
"text",
|
|
10564
|
+
"passage",
|
|
10565
|
+
"expression",
|
|
10566
|
+
"statements"
|
|
10567
|
+
];
|
|
10568
|
+
/**
|
|
10569
|
+
* Throw if a parameter `macro` declares (or an option of one) has no type,
|
|
10570
|
+
* or one that isn't a ParameterType: arguments are read by their type, and
|
|
10571
|
+
* there is no default. Throw too if it declares what it `holds` wrongly.
|
|
10572
|
+
*/
|
|
10573
|
+
function checkParameterTypes(macro, parameters) {
|
|
10574
|
+
const holdsFix = `Give a \`string\` or \`text\` parameter one of ${STRING_HOLDS.join(", ")} to hold (see docs/custom-macros.md#what-a-string-holds).`;
|
|
10575
|
+
for (const param of parameters) {
|
|
10576
|
+
const type = param.type;
|
|
10577
|
+
if (!PARAMETER_TYPES.includes(type)) throw parameterError(macro, param, type === void 0 ? "has no type" : `has the unknown type ${JSON.stringify(type)}`, `Give it one of the types ${PARAMETER_TYPES.join(", ")} (see docs/custom-macros.md#parameter-types), or declare no parameters and read props.rawArgs.`);
|
|
10578
|
+
const holds = param.holds;
|
|
10579
|
+
if (holds !== void 0 && type !== "string" && type !== "text") throw parameterError(macro, param, `is of the type ${type}, which holds what it is`, holdsFix);
|
|
10580
|
+
if (holds !== void 0 && !STRING_HOLDS.includes(holds)) throw parameterError(macro, param, `holds the unknown ${JSON.stringify(holds)}`, holdsFix);
|
|
10581
|
+
if (param.parameters) checkParameterTypes(macro, param.parameters);
|
|
10582
|
+
}
|
|
10583
|
+
}
|
|
10584
|
+
/** The error for the parameter `param` of `macro`: what is wrong, and the fix. */
|
|
10585
|
+
function parameterError(macro, param, problem, fix) {
|
|
10586
|
+
return /* @__PURE__ */ new Error(`spindle: The parameter "${param.name}" of the macro {${macro}} ${problem}. ${fix}`);
|
|
10587
|
+
}
|
|
10588
|
+
var metadataRegistry = new NameMap();
|
|
10589
|
+
function getMacroRegistry() {
|
|
10590
|
+
return Array.from(metadataRegistry.values());
|
|
10591
|
+
}
|
|
10489
10592
|
/** The parameters of the built-in sub-macros that take arguments. */
|
|
10490
10593
|
var SUB_MACRO_PARAMETERS = /* @__PURE__ */ new Map([["option", [{
|
|
10491
10594
|
name: "value",
|
|
@@ -10498,6 +10601,37 @@ function subMacroParameters(name) {
|
|
|
10498
10601
|
return SUB_MACRO_PARAMETERS.get(name.toLowerCase());
|
|
10499
10602
|
}
|
|
10500
10603
|
//#endregion
|
|
10604
|
+
//#region src/utils/namespace.ts
|
|
10605
|
+
/** The variable name no namespace can hold. */
|
|
10606
|
+
var RESERVED_NAME = "__proto__";
|
|
10607
|
+
/**
|
|
10608
|
+
* Why `name` (without its sigil) cannot be a variable name, or undefined if
|
|
10609
|
+
* it can. `label` is the name as the author wrote it (e.g. `$__proto__`).
|
|
10610
|
+
*/
|
|
10611
|
+
function variableNameError(name, label = name) {
|
|
10612
|
+
return name === "__proto__" ? `"${label}" cannot be used as a variable name (${RESERVED_NAME} is reserved)` : void 0;
|
|
10613
|
+
}
|
|
10614
|
+
/** Whether `obj` holds `key` as an own property. */
|
|
10615
|
+
var hasOwn = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
|
|
10616
|
+
/**
|
|
10617
|
+
* A record with no prototype holding the own entries of `sources`, or only
|
|
10618
|
+
* those whose value passes `keep`.
|
|
10619
|
+
*/
|
|
10620
|
+
function ownEntries(sources, keep = () => true) {
|
|
10621
|
+
const record = Object.create(null);
|
|
10622
|
+
for (const source of sources) {
|
|
10623
|
+
if (!source) continue;
|
|
10624
|
+
for (const key of Object.keys(source)) {
|
|
10625
|
+
const value = source[key];
|
|
10626
|
+
if (keep(value)) record[key] = value;
|
|
10627
|
+
}
|
|
10628
|
+
}
|
|
10629
|
+
return record;
|
|
10630
|
+
}
|
|
10631
|
+
/** A namespace with no prototype holding the own entries of `sources`. */
|
|
10632
|
+
var createNamespace = (...sources) => ownEntries(sources);
|
|
10633
|
+
Object.freeze(createNamespace());
|
|
10634
|
+
//#endregion
|
|
10501
10635
|
//#region src/code-check.ts
|
|
10502
10636
|
/** While a pass runs: the code it parsed so far, by goal and source. */
|
|
10503
10637
|
var parses = null;
|
|
@@ -10545,6 +10679,18 @@ var CONDITION_MACROS = /* @__PURE__ */ new Set([
|
|
|
10545
10679
|
/** Block macros whose body is the name of a passage. */
|
|
10546
10680
|
var PASSAGE_BODIES = /* @__PURE__ */ new Set(["dialog"]);
|
|
10547
10681
|
/**
|
|
10682
|
+
* The parameters of a widget given its `@` parameters and what they declare
|
|
10683
|
+
* to hold, or none when it declares nothing (a call is then not read by
|
|
10684
|
+
* parameter).
|
|
10685
|
+
*/
|
|
10686
|
+
function widgetParameters(params, holds = {}) {
|
|
10687
|
+
if (!params.some((param) => hasOwn(holds, param))) return void 0;
|
|
10688
|
+
return params.map((name) => ({
|
|
10689
|
+
name,
|
|
10690
|
+
...hasOwn(holds, name) ? { holds: holds[name] } : {}
|
|
10691
|
+
}));
|
|
10692
|
+
}
|
|
10693
|
+
/**
|
|
10548
10694
|
* `params` with each `string` and `text` parameter (options too) saying
|
|
10549
10695
|
* what it holds: what it declares, else `holds`.
|
|
10550
10696
|
*/
|
|
@@ -10566,11 +10712,17 @@ function withHolds(params, holds) {
|
|
|
10566
10712
|
* collection and tooling read arguments by. Each `string` and `text`
|
|
10567
10713
|
* parameter says what it holds: what it declares, else `markup` for a macro
|
|
10568
10714
|
* with `interpolate` (which can resolve markup) and `text` for any other.
|
|
10715
|
+
* A widget among `macros` says what its parameters hold (see ParametersOf).
|
|
10569
10716
|
*/
|
|
10570
10717
|
function parameterLookup(macros) {
|
|
10571
10718
|
const parameters = new NameMap();
|
|
10572
|
-
|
|
10573
|
-
|
|
10719
|
+
const widgets = new NameMap();
|
|
10720
|
+
for (const { name, parameters: params, interpolate, widget } of macros) {
|
|
10721
|
+
if (params) parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
|
|
10722
|
+
const declared = widget && widgetParameters(widget.params, widget.holds);
|
|
10723
|
+
if (declared) widgets.set(name, declared);
|
|
10724
|
+
}
|
|
10725
|
+
const lookup = (name) => {
|
|
10574
10726
|
let params = parameters.get(name);
|
|
10575
10727
|
if (!params) {
|
|
10576
10728
|
const sub = subMacroParameters(name);
|
|
@@ -10578,6 +10730,8 @@ function parameterLookup(macros) {
|
|
|
10578
10730
|
}
|
|
10579
10731
|
return params;
|
|
10580
10732
|
};
|
|
10733
|
+
if (widgets.size > 0) lookup.widget = (name) => widgets.get(name);
|
|
10734
|
+
return lookup;
|
|
10581
10735
|
}
|
|
10582
10736
|
/** What a `string` or `text` argument holds when its parameter doesn't say. */
|
|
10583
10737
|
var holdsByDefault = (interpolate) => interpolate ? "markup" : "text";
|
|
@@ -10680,6 +10834,8 @@ function* codeAndText(src, tokens, parametersOf) {
|
|
|
10680
10834
|
}
|
|
10681
10835
|
const params = parametersOf(name);
|
|
10682
10836
|
if (params) yield* argPieces(args, argsAt, params, token.name, label);
|
|
10837
|
+
const widget = parametersOf.widget?.(name);
|
|
10838
|
+
if (widget) yield* widgetPieces(args, argsAt, widget, token.name, label);
|
|
10683
10839
|
}
|
|
10684
10840
|
}
|
|
10685
10841
|
}
|
|
@@ -10771,7 +10927,6 @@ function escapeOffsets(raw, at, value) {
|
|
|
10771
10927
|
}
|
|
10772
10928
|
/** The code and text in the arguments `args` (at `offset`) of `macro`. */
|
|
10773
10929
|
function* argPieces(args, offset, params, macro, label) {
|
|
10774
|
-
const name = macro.toLowerCase();
|
|
10775
10930
|
let values;
|
|
10776
10931
|
const spans = /* @__PURE__ */ new Map();
|
|
10777
10932
|
try {
|
|
@@ -10784,10 +10939,44 @@ function* argPieces(args, offset, params, macro, label) {
|
|
|
10784
10939
|
offset,
|
|
10785
10940
|
length: args.length,
|
|
10786
10941
|
label,
|
|
10787
|
-
macro:
|
|
10942
|
+
macro: macro.toLowerCase()
|
|
10788
10943
|
};
|
|
10789
10944
|
return;
|
|
10790
10945
|
}
|
|
10946
|
+
yield* valuePieces(args, offset, params, values, spans, macro, label);
|
|
10947
|
+
}
|
|
10948
|
+
/**
|
|
10949
|
+
* The code and text in the arguments `args` (at `offset`) of the widget
|
|
10950
|
+
* `macro`, the ones whose parameter declares what it holds. Such an argument
|
|
10951
|
+
* is checked when it is one quoted string, as a `string` parameter with that
|
|
10952
|
+
* `holds` of a macro is; any other expression is its value at run time.
|
|
10953
|
+
*/
|
|
10954
|
+
function* widgetPieces(args, offset, params, macro, label) {
|
|
10955
|
+
const written = splitArgSpans(args);
|
|
10956
|
+
const declared = [];
|
|
10957
|
+
const values = {};
|
|
10958
|
+
const spans = /* @__PURE__ */ new Map();
|
|
10959
|
+
params.forEach(({ name, holds }, i) => {
|
|
10960
|
+
const span = written[i];
|
|
10961
|
+
const value = span && stringLiteralValue(args.slice(...span));
|
|
10962
|
+
if (!holds || value == null) return;
|
|
10963
|
+
const param = {
|
|
10964
|
+
name,
|
|
10965
|
+
type: "string",
|
|
10966
|
+
holds
|
|
10967
|
+
};
|
|
10968
|
+
declared.push(param);
|
|
10969
|
+
values[name] = value;
|
|
10970
|
+
spans.set(param, span);
|
|
10971
|
+
});
|
|
10972
|
+
yield* valuePieces(args, offset, declared, values, spans, macro, label);
|
|
10973
|
+
}
|
|
10974
|
+
/**
|
|
10975
|
+
* The pieces of the `params` of `macro` with the `values` read from `args`
|
|
10976
|
+
* (at `offset`), whose `spans` tell where each was written.
|
|
10977
|
+
*/
|
|
10978
|
+
function* valuePieces(args, offset, params, values, spans, macro, label) {
|
|
10979
|
+
const name = macro.toLowerCase();
|
|
10791
10980
|
/** The pieces of the parameters `list`, with their values in `from`. */
|
|
10792
10981
|
function* visit(list, from) {
|
|
10793
10982
|
for (const param of list) {
|
|
@@ -10949,86 +11138,6 @@ function referencesOf(pieces) {
|
|
|
10949
11138
|
});
|
|
10950
11139
|
return refs;
|
|
10951
11140
|
}
|
|
10952
|
-
new NameMap();
|
|
10953
|
-
new NameMap();
|
|
10954
|
-
new NameSet();
|
|
10955
|
-
/**
|
|
10956
|
-
* How a macro argument is read (see components/macros/macro-args.ts). Every
|
|
10957
|
-
* declared parameter names one: there is no default.
|
|
10958
|
-
* Quoted strings accept `\"`, `\'` and `\\` escapes.
|
|
10959
|
-
* - `expression`: code, as written.
|
|
10960
|
-
* - `statements`: code run as statements (`{set}`), as written.
|
|
10961
|
-
* - `passage`: a passage name: a quoted string or an expression, as written.
|
|
10962
|
-
* - `variable`: a variable reference such as `$name` or `"$name"`, as written.
|
|
10963
|
-
* - `string`: one quoted string; anything else leaves the argument unset.
|
|
10964
|
-
* - `text`: one quoted string, or text with any loose quotes stripped.
|
|
10965
|
-
* - `names`: a comma-separated list of names (`@item, @i`).
|
|
10966
|
-
* - `delay`: a duration (`2s`, `500ms`, `300`) in milliseconds.
|
|
10967
|
-
* - `number`: a number.
|
|
10968
|
-
* - `flag`: a keyword, the parameter's name, at the start or end; a boolean.
|
|
10969
|
-
* - `separator`: a word (`of`) or `=` separating the parameters before it
|
|
10970
|
-
* from those after it; a boolean.
|
|
10971
|
-
* - `options`: keywords, the names of its `parameters`, each followed by a
|
|
10972
|
-
* quoted string or a number unless it is a flag.
|
|
10973
|
-
*/
|
|
10974
|
-
var PARAMETER_TYPES = [
|
|
10975
|
-
"expression",
|
|
10976
|
-
"statements",
|
|
10977
|
-
"passage",
|
|
10978
|
-
"variable",
|
|
10979
|
-
"string",
|
|
10980
|
-
"text",
|
|
10981
|
-
"names",
|
|
10982
|
-
"delay",
|
|
10983
|
-
"number",
|
|
10984
|
-
"flag",
|
|
10985
|
-
"separator",
|
|
10986
|
-
"options"
|
|
10987
|
-
];
|
|
10988
|
-
/**
|
|
10989
|
-
* What the value of a `string` or `text` argument holds, for the check at
|
|
10990
|
-
* story start and for tooling (see code-check.ts):
|
|
10991
|
-
* - `markup`: markup the macro renders (`{button}`'s label): its markup
|
|
10992
|
-
* is checked.
|
|
10993
|
-
* - `text`: plain text the macro uses as written (`{checkbox}`'s label).
|
|
10994
|
-
* - `passage`: a passage name (`{watch}`'s `goto`): the passage must exist.
|
|
10995
|
-
* - `expression`, `statements`: code (`{watch}`'s condition and `run`): it
|
|
10996
|
-
* is checked as code, and its variable references against the schema.
|
|
10997
|
-
*
|
|
10998
|
-
* Without it, the argument of a macro with `interpolate` holds `markup`,
|
|
10999
|
-
* any other `text`.
|
|
11000
|
-
*/
|
|
11001
|
-
var STRING_HOLDS = [
|
|
11002
|
-
"markup",
|
|
11003
|
-
"text",
|
|
11004
|
-
"passage",
|
|
11005
|
-
"expression",
|
|
11006
|
-
"statements"
|
|
11007
|
-
];
|
|
11008
|
-
/**
|
|
11009
|
-
* Throw if a parameter `macro` declares (or an option of one) has no type,
|
|
11010
|
-
* or one that isn't a ParameterType: arguments are read by their type, and
|
|
11011
|
-
* there is no default. Throw too if it declares what it `holds` wrongly.
|
|
11012
|
-
*/
|
|
11013
|
-
function checkParameterTypes(macro, parameters) {
|
|
11014
|
-
const holdsFix = `Give a \`string\` or \`text\` parameter one of ${STRING_HOLDS.join(", ")} to hold (see docs/custom-macros.md#what-a-string-holds).`;
|
|
11015
|
-
for (const param of parameters) {
|
|
11016
|
-
const type = param.type;
|
|
11017
|
-
if (!PARAMETER_TYPES.includes(type)) throw parameterError(macro, param, type === void 0 ? "has no type" : `has the unknown type ${JSON.stringify(type)}`, `Give it one of the types ${PARAMETER_TYPES.join(", ")} (see docs/custom-macros.md#parameter-types), or declare no parameters and read props.rawArgs.`);
|
|
11018
|
-
const holds = param.holds;
|
|
11019
|
-
if (holds !== void 0 && type !== "string" && type !== "text") throw parameterError(macro, param, `is of the type ${type}, which holds what it is`, holdsFix);
|
|
11020
|
-
if (holds !== void 0 && !STRING_HOLDS.includes(holds)) throw parameterError(macro, param, `holds the unknown ${JSON.stringify(holds)}`, holdsFix);
|
|
11021
|
-
if (param.parameters) checkParameterTypes(macro, param.parameters);
|
|
11022
|
-
}
|
|
11023
|
-
}
|
|
11024
|
-
/** The error for the parameter `param` of `macro`: what is wrong, and the fix. */
|
|
11025
|
-
function parameterError(macro, param, problem, fix) {
|
|
11026
|
-
return /* @__PURE__ */ new Error(`spindle: The parameter "${param.name}" of the macro {${macro}} ${problem}. ${fix}`);
|
|
11027
|
-
}
|
|
11028
|
-
var metadataRegistry = new NameMap();
|
|
11029
|
-
function getMacroRegistry() {
|
|
11030
|
-
return Array.from(metadataRegistry.values());
|
|
11031
|
-
}
|
|
11032
11141
|
//#endregion
|
|
11033
11142
|
//#region src/widgets/ast-scanner.ts
|
|
11034
11143
|
/** The declared parameters of the macros registered so far. */
|
|
@@ -11116,13 +11225,31 @@ var WIDGET_PARAMETERS = [{
|
|
|
11116
11225
|
/**
|
|
11117
11226
|
* The widget a {widget} definition's arguments declare: its name and its
|
|
11118
11227
|
* parameters, the words after it that start with `@` (docs/widgets.md),
|
|
11119
|
-
* separated by spaces or commas (`@a, @b`).
|
|
11120
|
-
*
|
|
11228
|
+
* separated by spaces or commas (`@a, @b`). A parameter may say what its
|
|
11229
|
+
* argument holds after a colon (`@target:passage`, one of the `holds` of a
|
|
11230
|
+
* macro parameter; another word is a `badHolds`). Other words, such as
|
|
11231
|
+
* `$name`, are not parameters.
|
|
11121
11232
|
*/
|
|
11122
11233
|
function widgetDef({ name = "", parameters = "" }) {
|
|
11234
|
+
const params = [];
|
|
11235
|
+
const holds = {};
|
|
11236
|
+
const badHolds = [];
|
|
11237
|
+
for (const word of parameters.split(/[\s,]+/)) {
|
|
11238
|
+
if (!word.startsWith("@")) continue;
|
|
11239
|
+
const [param = word, written] = word.split(/:(.*)/s);
|
|
11240
|
+
params.push(param);
|
|
11241
|
+
if (written === void 0) continue;
|
|
11242
|
+
if (STRING_HOLDS.includes(written)) holds[param] = written;
|
|
11243
|
+
else badHolds.push({
|
|
11244
|
+
param,
|
|
11245
|
+
written
|
|
11246
|
+
});
|
|
11247
|
+
}
|
|
11123
11248
|
return {
|
|
11124
11249
|
name,
|
|
11125
|
-
params
|
|
11250
|
+
params,
|
|
11251
|
+
...Object.keys(holds).length > 0 ? { holds } : {},
|
|
11252
|
+
...badHolds.length > 0 ? { badHolds } : {}
|
|
11126
11253
|
};
|
|
11127
11254
|
}
|
|
11128
11255
|
/** Read the arguments of a {widget} definition (see widgetDef). */
|
|
@@ -11186,15 +11313,6 @@ function widgetDefinitions$1(passages, parametersOf = registeredParameters) {
|
|
|
11186
11313
|
}
|
|
11187
11314
|
return found;
|
|
11188
11315
|
}
|
|
11189
|
-
/**
|
|
11190
|
-
* The names of the block widgets (those whose body renders `{@children}`)
|
|
11191
|
-
* that StoryInit and the passages tagged `widget` define, closed. They must
|
|
11192
|
-
* be known as block macros before any passage is parsed, so that passages
|
|
11193
|
-
* invoking them nest their content whatever the passage order.
|
|
11194
|
-
*/
|
|
11195
|
-
function blockWidgetNames(passages, parametersOf = registeredParameters) {
|
|
11196
|
-
return widgetDefinitions$1(passages, parametersOf).filter((def) => def.block && def.closeStart !== void 0).map((def) => def.name);
|
|
11197
|
-
}
|
|
11198
11316
|
//#endregion
|
|
11199
11317
|
//#region src/markup/validate.ts
|
|
11200
11318
|
/**
|
|
@@ -11398,35 +11516,6 @@ function validateMarkup(passages, options) {
|
|
|
11398
11516
|
return diagnostics;
|
|
11399
11517
|
}
|
|
11400
11518
|
//#endregion
|
|
11401
|
-
//#region src/utils/namespace.ts
|
|
11402
|
-
/** The variable name no namespace can hold. */
|
|
11403
|
-
var RESERVED_NAME = "__proto__";
|
|
11404
|
-
/**
|
|
11405
|
-
* Why `name` (without its sigil) cannot be a variable name, or undefined if
|
|
11406
|
-
* it can. `label` is the name as the author wrote it (e.g. `$__proto__`).
|
|
11407
|
-
*/
|
|
11408
|
-
function variableNameError(name, label = name) {
|
|
11409
|
-
return name === "__proto__" ? `"${label}" cannot be used as a variable name (${RESERVED_NAME} is reserved)` : void 0;
|
|
11410
|
-
}
|
|
11411
|
-
/**
|
|
11412
|
-
* A record with no prototype holding the own entries of `sources`, or only
|
|
11413
|
-
* those whose value passes `keep`.
|
|
11414
|
-
*/
|
|
11415
|
-
function ownEntries(sources, keep = () => true) {
|
|
11416
|
-
const record = Object.create(null);
|
|
11417
|
-
for (const source of sources) {
|
|
11418
|
-
if (!source) continue;
|
|
11419
|
-
for (const key of Object.keys(source)) {
|
|
11420
|
-
const value = source[key];
|
|
11421
|
-
if (keep(value)) record[key] = value;
|
|
11422
|
-
}
|
|
11423
|
-
}
|
|
11424
|
-
return record;
|
|
11425
|
-
}
|
|
11426
|
-
/** A namespace with no prototype holding the own entries of `sources`. */
|
|
11427
|
-
var createNamespace = (...sources) => ownEntries(sources);
|
|
11428
|
-
Object.freeze(createNamespace());
|
|
11429
|
-
//#endregion
|
|
11430
11519
|
//#region src/static-literal.ts
|
|
11431
11520
|
/**
|
|
11432
11521
|
* The shape of a JavaScript literal, read from its text without running it:
|
|
@@ -12591,8 +12680,12 @@ function validateStoryMarkup(passages, macros, options = {}) {
|
|
|
12591
12680
|
const list = [...passages];
|
|
12592
12681
|
const all = [...macros];
|
|
12593
12682
|
const known = /* @__PURE__ */ new Set();
|
|
12594
|
-
const
|
|
12595
|
-
const blocks = new Set(
|
|
12683
|
+
const widgets = widgetDefinitions$1(list, parameterLookup(all));
|
|
12684
|
+
const blocks = new Set(widgets.filter((def) => def.block && def.closeStart !== void 0).map((def) => def.name.toLowerCase()));
|
|
12685
|
+
const parametersOf = parameterLookup([...all, ...widgets.map((widget) => ({
|
|
12686
|
+
name: widget.name,
|
|
12687
|
+
widget
|
|
12688
|
+
}))]);
|
|
12596
12689
|
for (const macro of all) {
|
|
12597
12690
|
const name = macro.name.toLowerCase();
|
|
12598
12691
|
known.add(name);
|