@rohal12/spindle 0.59.28 → 0.59.30
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 +1 -0
- package/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +718 -300
- package/dist/pkg/macro-registry.js +19 -0
- package/dist/pkg/macro-registry.json +19 -0
- package/dist/pkg/story-variables.js +731 -234
- package/package.json +1 -1
- package/src/action-registry.ts +59 -5
- package/src/automation/load-yaml.ts +111 -20
- package/src/automation/runner.ts +15 -13
- package/src/automation/types.ts +7 -5
- package/src/class-registry.ts +8 -5
- package/src/code-check.ts +187 -24
- package/src/components/macros/Checkbox.tsx +5 -2
- package/src/components/macros/Cycle.tsx +1 -0
- package/src/components/macros/For.tsx +1 -7
- package/src/components/macros/Listbox.tsx +1 -0
- package/src/components/macros/MenubarButtons.tsx +6 -1
- package/src/components/macros/Nobr.tsx +1 -0
- package/src/components/macros/PassageDisplay.tsx +42 -0
- package/src/components/macros/Radiobutton.tsx +1 -0
- package/src/components/macros/SaveManager.tsx +19 -4
- package/src/components/macros/SettingsControls.tsx +19 -4
- package/src/components/macros/Span.tsx +1 -0
- package/src/components/macros/StoryTitle.tsx +1 -0
- package/src/components/macros/VarDisplay.tsx +2 -3
- package/src/components/macros/Widget.tsx +2 -0
- package/src/components/macros/WidgetInvocation.tsx +5 -6
- package/src/components/macros/arg-utils.ts +58 -21
- package/src/components/macros/input-macro.ts +1 -0
- package/src/components/macros/locals-scope.tsx +19 -8
- package/src/components/macros/macro-args.ts +11 -3
- package/src/expression.ts +34 -18
- package/src/hooks/use-merged-locals.ts +17 -2
- package/src/hooks/use-modal-focus.ts +190 -129
- package/src/index.tsx +23 -13
- package/src/js-bindings.ts +332 -0
- package/src/js-lexer.ts +12 -3
- package/src/macro-discovery.ts +25 -22
- package/src/static-literal.ts +87 -16
- package/src/store.ts +31 -2
- package/src/story-api.ts +15 -5
- package/src/story-variables.ts +91 -28
- package/src/structural.ts +19 -15
- package/src/tooling.ts +36 -10
- package/src/transform.ts +22 -7
- package/src/utils/namespace.ts +4 -0
- package/src/utils/object-path.ts +2 -2
- package/src/utils/stable-key.ts +17 -6
- package/src/utils/value-kinds.ts +38 -1
- 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
- package/types/index.d.ts +12 -5
- package/types/tooling.d.ts +12 -2
|
@@ -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) {
|
|
@@ -8989,7 +8989,7 @@ function transientAssignment(p, end) {
|
|
|
8989
8989
|
if (t === types$1.eof) return false;
|
|
8990
8990
|
if (depth > 0) {
|
|
8991
8991
|
if (OPENERS.has(t)) depth++;
|
|
8992
|
-
else if (CLOSERS.has(t)) depth--;
|
|
8992
|
+
else if (CLOSERS$1.has(t)) depth--;
|
|
8993
8993
|
} else if (t === types$1.bracketL) depth = 1;
|
|
8994
8994
|
else if (t === types$1.dot) {
|
|
8995
8995
|
q.nextToken();
|
|
@@ -9006,7 +9006,7 @@ var OPENERS = /* @__PURE__ */ new Set([
|
|
|
9006
9006
|
types$1.braceL,
|
|
9007
9007
|
types$1.dollarBraceL
|
|
9008
9008
|
]);
|
|
9009
|
-
var CLOSERS = /* @__PURE__ */ new Set([
|
|
9009
|
+
var CLOSERS$1 = /* @__PURE__ */ new Set([
|
|
9010
9010
|
types$1.parenR,
|
|
9011
9011
|
types$1.bracketR,
|
|
9012
9012
|
types$1.braceR
|
|
@@ -9550,7 +9550,7 @@ function walkTokens(src, from, handlers, nesting, goal, oneTemplate) {
|
|
|
9550
9550
|
if (t === types$1.name) {
|
|
9551
9551
|
const sigil = sigilAt(src, tok.start, tok.value);
|
|
9552
9552
|
const property = prev === types$1.dot || prev === types$1.questionDot || (sigil === "$" || sigil === "_") && keyPosition(src, tok.end, prev);
|
|
9553
|
-
if (sigil && !property) handlers.variable?.(sigil,
|
|
9553
|
+
if (sigil && !property) handlers.variable?.(sigil, tok.value.slice(1), tok.start, nesting, tok.end);
|
|
9554
9554
|
else code(tok.start, tok.end);
|
|
9555
9555
|
} else if (t === types$1.string || t === types$1.regexp || t === types$1.template || t === types$1.invalidTemplate) literal(tok.start, tok.end);
|
|
9556
9556
|
else if (t === types$1.backQuote) {
|
|
@@ -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
|
/**
|
|
@@ -10341,10 +10364,12 @@ var LEADING_OPERATOR_RE = /^(?:[-+*/&|^=<>?:,.]|%(?![A-Za-z_]))/;
|
|
|
10341
10364
|
* Where the rest of `src` is without the flag `word` as its last or first
|
|
10342
10365
|
* term, or `null` if it has no such flag. Next to a binary operator the
|
|
10343
10366
|
* word is an operand (`"a" + inline`), not a flag; the closing `/` of a
|
|
10344
|
-
* regex literal is not one.
|
|
10367
|
+
* regex literal is not one. A word with nothing else beside it is the flag
|
|
10368
|
+
* only when `alone` says no argument is waiting for it (#499).
|
|
10345
10369
|
*/
|
|
10346
|
-
function takeFlag(src, word) {
|
|
10370
|
+
function takeFlag(src, word, alone) {
|
|
10347
10371
|
const spans = terms(src);
|
|
10372
|
+
if (spans.length === 1) return alone && slice(src, spans[0]) === word ? [src.length, src.length] : null;
|
|
10348
10373
|
if (spans.length < 2) return null;
|
|
10349
10374
|
if (slice(src, spans[spans.length - 1]) === word) {
|
|
10350
10375
|
const rest = [0, spans[spans.length - 2][1]];
|
|
@@ -10391,8 +10416,9 @@ function trimmed(src, at) {
|
|
|
10391
10416
|
*/
|
|
10392
10417
|
function readGroup(params, src, args, { at: srcAt, spans }) {
|
|
10393
10418
|
let [text, at] = trimmed(src ?? "", srcAt);
|
|
10419
|
+
const alone = !params.some((p) => p.type !== "flag" && p.required);
|
|
10394
10420
|
for (const flag of params.filter((p) => p.type === "flag")) {
|
|
10395
|
-
const rest = src === null ? null : takeFlag(text, flag.name);
|
|
10421
|
+
const rest = src === null ? null : takeFlag(text, flag.name, alone);
|
|
10396
10422
|
args[flag.name] = rest !== null;
|
|
10397
10423
|
if (rest) [text, at] = trimmed(slice(text, rest), at + rest[0]);
|
|
10398
10424
|
}
|
|
@@ -10486,6 +10512,86 @@ function evaluatePassageName(expr, evaluate, { storyData, currentPassage }) {
|
|
|
10486
10512
|
if (storyData && !storyData.passages.has(name)) throw noPassageError(name, currentPassage);
|
|
10487
10513
|
return name;
|
|
10488
10514
|
}
|
|
10515
|
+
new NameMap();
|
|
10516
|
+
new NameMap();
|
|
10517
|
+
new NameSet();
|
|
10518
|
+
/**
|
|
10519
|
+
* How a macro argument is read (see components/macros/macro-args.ts). Every
|
|
10520
|
+
* declared parameter names one: there is no default.
|
|
10521
|
+
* Quoted strings accept `\"`, `\'` and `\\` escapes.
|
|
10522
|
+
* - `expression`: code, as written.
|
|
10523
|
+
* - `statements`: code run as statements (`{set}`), as written.
|
|
10524
|
+
* - `passage`: a passage name: a quoted string or an expression, as written.
|
|
10525
|
+
* - `variable`: a variable reference such as `$name` or `"$name"`, as written.
|
|
10526
|
+
* - `string`: one quoted string; anything else leaves the argument unset.
|
|
10527
|
+
* - `text`: one quoted string, or text with any loose quotes stripped.
|
|
10528
|
+
* - `names`: a comma-separated list of names (`@item, @i`).
|
|
10529
|
+
* - `delay`: a duration (`2s`, `500ms`, `300`) in milliseconds.
|
|
10530
|
+
* - `number`: a number.
|
|
10531
|
+
* - `flag`: a keyword, the parameter's name, at the start or end; a boolean.
|
|
10532
|
+
* - `separator`: a word (`of`) or `=` separating the parameters before it
|
|
10533
|
+
* from those after it; a boolean.
|
|
10534
|
+
* - `options`: keywords, the names of its `parameters`, each followed by a
|
|
10535
|
+
* quoted string or a number unless it is a flag.
|
|
10536
|
+
*/
|
|
10537
|
+
var PARAMETER_TYPES = [
|
|
10538
|
+
"expression",
|
|
10539
|
+
"statements",
|
|
10540
|
+
"passage",
|
|
10541
|
+
"variable",
|
|
10542
|
+
"string",
|
|
10543
|
+
"text",
|
|
10544
|
+
"names",
|
|
10545
|
+
"delay",
|
|
10546
|
+
"number",
|
|
10547
|
+
"flag",
|
|
10548
|
+
"separator",
|
|
10549
|
+
"options"
|
|
10550
|
+
];
|
|
10551
|
+
/**
|
|
10552
|
+
* What the value of a `string` or `text` argument holds, for the check at
|
|
10553
|
+
* story start and for tooling (see code-check.ts):
|
|
10554
|
+
* - `markup`: markup the macro renders (`{button}`'s label): its markup
|
|
10555
|
+
* is checked.
|
|
10556
|
+
* - `text`: plain text the macro uses as written (`{checkbox}`'s label).
|
|
10557
|
+
* - `passage`: a passage name (`{watch}`'s `goto`): the passage must exist.
|
|
10558
|
+
* - `expression`, `statements`: code (`{watch}`'s condition and `run`): it
|
|
10559
|
+
* is checked as code, and its variable references against the schema.
|
|
10560
|
+
*
|
|
10561
|
+
* Without it, the argument of a macro with `interpolate` holds `markup`,
|
|
10562
|
+
* any other `text`.
|
|
10563
|
+
*/
|
|
10564
|
+
var STRING_HOLDS = [
|
|
10565
|
+
"markup",
|
|
10566
|
+
"text",
|
|
10567
|
+
"passage",
|
|
10568
|
+
"expression",
|
|
10569
|
+
"statements"
|
|
10570
|
+
];
|
|
10571
|
+
/**
|
|
10572
|
+
* Throw if a parameter `macro` declares (or an option of one) has no type,
|
|
10573
|
+
* or one that isn't a ParameterType: arguments are read by their type, and
|
|
10574
|
+
* there is no default. Throw too if it declares what it `holds` wrongly.
|
|
10575
|
+
*/
|
|
10576
|
+
function checkParameterTypes(macro, parameters) {
|
|
10577
|
+
const holdsFix = `Give a \`string\` or \`text\` parameter one of ${STRING_HOLDS.join(", ")} to hold (see docs/custom-macros.md#what-a-string-holds).`;
|
|
10578
|
+
for (const param of parameters) {
|
|
10579
|
+
const type = param.type;
|
|
10580
|
+
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.`);
|
|
10581
|
+
const holds = param.holds;
|
|
10582
|
+
if (holds !== void 0 && type !== "string" && type !== "text") throw parameterError(macro, param, `is of the type ${type}, which holds what it is`, holdsFix);
|
|
10583
|
+
if (holds !== void 0 && !STRING_HOLDS.includes(holds)) throw parameterError(macro, param, `holds the unknown ${JSON.stringify(holds)}`, holdsFix);
|
|
10584
|
+
if (param.parameters) checkParameterTypes(macro, param.parameters);
|
|
10585
|
+
}
|
|
10586
|
+
}
|
|
10587
|
+
/** The error for the parameter `param` of `macro`: what is wrong, and the fix. */
|
|
10588
|
+
function parameterError(macro, param, problem, fix) {
|
|
10589
|
+
return /* @__PURE__ */ new Error(`spindle: The parameter "${param.name}" of the macro {${macro}} ${problem}. ${fix}`);
|
|
10590
|
+
}
|
|
10591
|
+
var metadataRegistry = new NameMap();
|
|
10592
|
+
function getMacroRegistry() {
|
|
10593
|
+
return Array.from(metadataRegistry.values());
|
|
10594
|
+
}
|
|
10489
10595
|
/** The parameters of the built-in sub-macros that take arguments. */
|
|
10490
10596
|
var SUB_MACRO_PARAMETERS = /* @__PURE__ */ new Map([["option", [{
|
|
10491
10597
|
name: "value",
|
|
@@ -10498,6 +10604,39 @@ function subMacroParameters(name) {
|
|
|
10498
10604
|
return SUB_MACRO_PARAMETERS.get(name.toLowerCase());
|
|
10499
10605
|
}
|
|
10500
10606
|
//#endregion
|
|
10607
|
+
//#region src/utils/namespace.ts
|
|
10608
|
+
/** The variable name no namespace can hold. */
|
|
10609
|
+
var RESERVED_NAME = "__proto__";
|
|
10610
|
+
/**
|
|
10611
|
+
* Why `name` (without its sigil) cannot be a variable name, or undefined if
|
|
10612
|
+
* it can. `label` is the name as the author wrote it (e.g. `$__proto__`).
|
|
10613
|
+
*/
|
|
10614
|
+
function variableNameError(name, label = name) {
|
|
10615
|
+
return name === "__proto__" ? `"${label}" cannot be used as a variable name (${RESERVED_NAME} is reserved)` : void 0;
|
|
10616
|
+
}
|
|
10617
|
+
/** Whether a property key is an array index (`"0"`, `"12"`; not `"01"`). */
|
|
10618
|
+
var isArrayIndex = (key) => /^(?:0|[1-9]\d*)$/.test(key);
|
|
10619
|
+
/** Whether `obj` holds `key` as an own property. */
|
|
10620
|
+
var hasOwn = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
|
|
10621
|
+
/**
|
|
10622
|
+
* A record with no prototype holding the own entries of `sources`, or only
|
|
10623
|
+
* those whose value passes `keep`.
|
|
10624
|
+
*/
|
|
10625
|
+
function ownEntries(sources, keep = () => true) {
|
|
10626
|
+
const record = Object.create(null);
|
|
10627
|
+
for (const source of sources) {
|
|
10628
|
+
if (!source) continue;
|
|
10629
|
+
for (const key of Object.keys(source)) {
|
|
10630
|
+
const value = source[key];
|
|
10631
|
+
if (keep(value)) record[key] = value;
|
|
10632
|
+
}
|
|
10633
|
+
}
|
|
10634
|
+
return record;
|
|
10635
|
+
}
|
|
10636
|
+
/** A namespace with no prototype holding the own entries of `sources`. */
|
|
10637
|
+
var createNamespace = (...sources) => ownEntries(sources);
|
|
10638
|
+
Object.freeze(createNamespace());
|
|
10639
|
+
//#endregion
|
|
10501
10640
|
//#region src/code-check.ts
|
|
10502
10641
|
/** While a pass runs: the code it parsed so far, by goal and source. */
|
|
10503
10642
|
var parses = null;
|
|
@@ -10545,6 +10684,18 @@ var CONDITION_MACROS = /* @__PURE__ */ new Set([
|
|
|
10545
10684
|
/** Block macros whose body is the name of a passage. */
|
|
10546
10685
|
var PASSAGE_BODIES = /* @__PURE__ */ new Set(["dialog"]);
|
|
10547
10686
|
/**
|
|
10687
|
+
* The parameters of a widget given its `@` parameters and what they declare
|
|
10688
|
+
* to hold, or none when it declares nothing (a call is then not read by
|
|
10689
|
+
* parameter).
|
|
10690
|
+
*/
|
|
10691
|
+
function widgetParameters(params, holds = {}) {
|
|
10692
|
+
if (!params.some((param) => hasOwn(holds, param))) return void 0;
|
|
10693
|
+
return params.map((name) => ({
|
|
10694
|
+
name,
|
|
10695
|
+
...hasOwn(holds, name) ? { holds: holds[name] } : {}
|
|
10696
|
+
}));
|
|
10697
|
+
}
|
|
10698
|
+
/**
|
|
10548
10699
|
* `params` with each `string` and `text` parameter (options too) saying
|
|
10549
10700
|
* what it holds: what it declares, else `holds`.
|
|
10550
10701
|
*/
|
|
@@ -10566,11 +10717,17 @@ function withHolds(params, holds) {
|
|
|
10566
10717
|
* collection and tooling read arguments by. Each `string` and `text`
|
|
10567
10718
|
* parameter says what it holds: what it declares, else `markup` for a macro
|
|
10568
10719
|
* with `interpolate` (which can resolve markup) and `text` for any other.
|
|
10720
|
+
* A widget among `macros` says what its parameters hold (see ParametersOf).
|
|
10569
10721
|
*/
|
|
10570
10722
|
function parameterLookup(macros) {
|
|
10571
10723
|
const parameters = new NameMap();
|
|
10572
|
-
|
|
10573
|
-
|
|
10724
|
+
const widgets = new NameMap();
|
|
10725
|
+
for (const { name, parameters: params, interpolate, widget } of macros) {
|
|
10726
|
+
if (params) parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
|
|
10727
|
+
const declared = widget && widgetParameters(widget.params, widget.holds);
|
|
10728
|
+
if (declared) widgets.set(name, declared);
|
|
10729
|
+
}
|
|
10730
|
+
const lookup = (name) => {
|
|
10574
10731
|
let params = parameters.get(name);
|
|
10575
10732
|
if (!params) {
|
|
10576
10733
|
const sub = subMacroParameters(name);
|
|
@@ -10578,6 +10735,8 @@ function parameterLookup(macros) {
|
|
|
10578
10735
|
}
|
|
10579
10736
|
return params;
|
|
10580
10737
|
};
|
|
10738
|
+
if (widgets.size > 0) lookup.widget = (name) => widgets.get(name);
|
|
10739
|
+
return lookup;
|
|
10581
10740
|
}
|
|
10582
10741
|
/** What a `string` or `text` argument holds when its parameter doesn't say. */
|
|
10583
10742
|
var holdsByDefault = (interpolate) => interpolate ? "markup" : "text";
|
|
@@ -10680,6 +10839,8 @@ function* codeAndText(src, tokens, parametersOf) {
|
|
|
10680
10839
|
}
|
|
10681
10840
|
const params = parametersOf(name);
|
|
10682
10841
|
if (params) yield* argPieces(args, argsAt, params, token.name, label);
|
|
10842
|
+
const widget = parametersOf.widget?.(name);
|
|
10843
|
+
if (widget) yield* widgetPieces(args, argsAt, widget, token.name, label);
|
|
10683
10844
|
}
|
|
10684
10845
|
}
|
|
10685
10846
|
}
|
|
@@ -10747,31 +10908,56 @@ function nestedPiece(piece, toRoot, where) {
|
|
|
10747
10908
|
...nested
|
|
10748
10909
|
};
|
|
10749
10910
|
}
|
|
10911
|
+
/** An escape sequence of a JavaScript string literal (with its backslash). */
|
|
10912
|
+
var JS_ESCAPE_RE = /\\(?:u\{[0-9a-fA-F]+\}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|[0-3][0-7]{0,2}|[4-7][0-7]?|\r\n|[^])/y;
|
|
10913
|
+
/**
|
|
10914
|
+
* The escape written at `raw[i]` (a backslash): how long it is as written and
|
|
10915
|
+
* what it stands for, or none when it is not one under these `escapes`.
|
|
10916
|
+
*/
|
|
10917
|
+
function escapeAt(raw, i, escapes) {
|
|
10918
|
+
if (escapes === "quotes") {
|
|
10919
|
+
const next = raw.charAt(i + 1);
|
|
10920
|
+
return /["'\\]/.test(next) ? {
|
|
10921
|
+
length: 2,
|
|
10922
|
+
text: next
|
|
10923
|
+
} : void 0;
|
|
10924
|
+
}
|
|
10925
|
+
JS_ESCAPE_RE.lastIndex = i;
|
|
10926
|
+
const written = JS_ESCAPE_RE.exec(raw)?.[0];
|
|
10927
|
+
const text = written && stringLiteralValue(`"${written}"`);
|
|
10928
|
+
return typeof text === "string" ? {
|
|
10929
|
+
length: written.length,
|
|
10930
|
+
text
|
|
10931
|
+
} : void 0;
|
|
10932
|
+
}
|
|
10750
10933
|
/**
|
|
10751
10934
|
* Where each character of `value`, the unescaped text of a quoted string
|
|
10752
|
-
* written as `raw` from `at` on (
|
|
10753
|
-
*
|
|
10754
|
-
*
|
|
10755
|
-
*
|
|
10935
|
+
* written as `raw` from `at` on (its escapes are those of `escapes`), is in
|
|
10936
|
+
* the source, and where the text ends. A character that an escape stands for
|
|
10937
|
+
* is where the escape begins. None when the text is where its length says, or
|
|
10938
|
+
* is not that string.
|
|
10756
10939
|
*/
|
|
10757
|
-
function escapeOffsets(raw, at, value) {
|
|
10940
|
+
function escapeOffsets(raw, at, value, escapes) {
|
|
10758
10941
|
const offsets = [];
|
|
10759
10942
|
let escaped = false;
|
|
10760
10943
|
let i = 0;
|
|
10761
10944
|
while (offsets.length < value.length) {
|
|
10762
10945
|
if (i >= raw.length) return void 0;
|
|
10763
|
-
|
|
10764
|
-
if (
|
|
10765
|
-
|
|
10766
|
-
|
|
10767
|
-
}
|
|
10946
|
+
const escape = raw[i] === "\\" ? escapeAt(raw, i, escapes) : void 0;
|
|
10947
|
+
if (!escape) {
|
|
10948
|
+
offsets.push(at + i++);
|
|
10949
|
+
continue;
|
|
10950
|
+
}
|
|
10951
|
+
if (!value.startsWith(escape.text, offsets.length)) return void 0;
|
|
10952
|
+
for (let k = 0; k < escape.text.length; k++) offsets.push(at + i);
|
|
10953
|
+
escaped = true;
|
|
10954
|
+
i += escape.length;
|
|
10768
10955
|
}
|
|
10769
10956
|
offsets.push(at + i);
|
|
10770
10957
|
return escaped ? offsets : void 0;
|
|
10771
10958
|
}
|
|
10772
10959
|
/** The code and text in the arguments `args` (at `offset`) of `macro`. */
|
|
10773
10960
|
function* argPieces(args, offset, params, macro, label) {
|
|
10774
|
-
const name = macro.toLowerCase();
|
|
10775
10961
|
let values;
|
|
10776
10962
|
const spans = /* @__PURE__ */ new Map();
|
|
10777
10963
|
try {
|
|
@@ -10784,10 +10970,45 @@ function* argPieces(args, offset, params, macro, label) {
|
|
|
10784
10970
|
offset,
|
|
10785
10971
|
length: args.length,
|
|
10786
10972
|
label,
|
|
10787
|
-
macro:
|
|
10973
|
+
macro: macro.toLowerCase()
|
|
10788
10974
|
};
|
|
10789
10975
|
return;
|
|
10790
10976
|
}
|
|
10977
|
+
yield* valuePieces(args, offset, params, values, spans, macro, label, "quotes");
|
|
10978
|
+
}
|
|
10979
|
+
/**
|
|
10980
|
+
* The code and text in the arguments `args` (at `offset`) of the widget
|
|
10981
|
+
* `macro`, the ones whose parameter declares what it holds. Such an argument
|
|
10982
|
+
* is checked when it is one quoted string, as a `string` parameter with that
|
|
10983
|
+
* `holds` of a macro is; any other expression is its value at run time.
|
|
10984
|
+
*/
|
|
10985
|
+
function* widgetPieces(args, offset, params, macro, label) {
|
|
10986
|
+
const written = splitArgSpans(args);
|
|
10987
|
+
const declared = [];
|
|
10988
|
+
const values = {};
|
|
10989
|
+
const spans = /* @__PURE__ */ new Map();
|
|
10990
|
+
params.forEach(({ name, holds }, i) => {
|
|
10991
|
+
const span = written[i];
|
|
10992
|
+
const value = span && stringLiteralValue(args.slice(...span));
|
|
10993
|
+
if (!holds || value == null) return;
|
|
10994
|
+
const param = {
|
|
10995
|
+
name,
|
|
10996
|
+
type: "string",
|
|
10997
|
+
holds
|
|
10998
|
+
};
|
|
10999
|
+
declared.push(param);
|
|
11000
|
+
values[name] = value;
|
|
11001
|
+
spans.set(param, span);
|
|
11002
|
+
});
|
|
11003
|
+
yield* valuePieces(args, offset, declared, values, spans, macro, label, "javascript");
|
|
11004
|
+
}
|
|
11005
|
+
/**
|
|
11006
|
+
* The pieces of the `params` of `macro` with the `values` read from `args`
|
|
11007
|
+
* (at `offset`), whose `spans` tell where each was written and whose strings
|
|
11008
|
+
* are unescaped as `escapes` says.
|
|
11009
|
+
*/
|
|
11010
|
+
function* valuePieces(args, offset, params, values, spans, macro, label, escapes) {
|
|
11011
|
+
const name = macro.toLowerCase();
|
|
10791
11012
|
/** The pieces of the parameters `list`, with their values in `from`. */
|
|
10792
11013
|
function* visit(list, from) {
|
|
10793
11014
|
for (const param of list) {
|
|
@@ -10834,7 +11055,7 @@ function* argPieces(args, offset, params, macro, label) {
|
|
|
10834
11055
|
}
|
|
10835
11056
|
const at = offset + start + (value !== written && /^["']/.test(written) ? 1 : 0);
|
|
10836
11057
|
const holds = stringHolds(param);
|
|
10837
|
-
const sourceOffsets = escapeOffsets(args.slice(at - offset), at, value);
|
|
11058
|
+
const sourceOffsets = escapeOffsets(args.slice(at - offset), at, value, escapes);
|
|
10838
11059
|
if (holds === "expression" || holds === "statements") {
|
|
10839
11060
|
const piece = {
|
|
10840
11061
|
kind: "code",
|
|
@@ -10900,20 +11121,21 @@ function attributeValues(src, token) {
|
|
|
10900
11121
|
}
|
|
10901
11122
|
/**
|
|
10902
11123
|
* The `{$…}` expressions in the value of a code attribute (`onclick`, see
|
|
10903
|
-
* isCodeAttribute), with where each is in the value.
|
|
11124
|
+
* isCodeAttribute), with where each is in the value. The parts of the value
|
|
11125
|
+
* are written out as they are (splitSigilTemplate decodes nothing), so each
|
|
11126
|
+
* starts where the one before it ends: an expression is not found by its
|
|
11127
|
+
* text, which a string of the code may repeat.
|
|
10904
11128
|
*/
|
|
10905
11129
|
function sigilExpressions(value) {
|
|
10906
11130
|
const found = [];
|
|
10907
11131
|
let from = 0;
|
|
10908
|
-
for (const part of splitSigilTemplate(value)) {
|
|
10909
|
-
if (!("expr" in part)) continue;
|
|
10910
|
-
const at = locate(value, part.expr, from);
|
|
10911
|
-
from = at + part.expr.length;
|
|
11132
|
+
for (const part of splitSigilTemplate(value)) if ("expr" in part) {
|
|
10912
11133
|
found.push({
|
|
10913
11134
|
expr: part.expr,
|
|
10914
|
-
at
|
|
11135
|
+
at: from + 1
|
|
10915
11136
|
});
|
|
10916
|
-
|
|
11137
|
+
from += part.expr.length + 2;
|
|
11138
|
+
} else from += ("text" in part ? part.text : part.verbatim).length;
|
|
10917
11139
|
return found;
|
|
10918
11140
|
}
|
|
10919
11141
|
/**
|
|
@@ -10949,86 +11171,6 @@ function referencesOf(pieces) {
|
|
|
10949
11171
|
});
|
|
10950
11172
|
return refs;
|
|
10951
11173
|
}
|
|
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
11174
|
//#endregion
|
|
11033
11175
|
//#region src/widgets/ast-scanner.ts
|
|
11034
11176
|
/** The declared parameters of the macros registered so far. */
|
|
@@ -11116,13 +11258,31 @@ var WIDGET_PARAMETERS = [{
|
|
|
11116
11258
|
/**
|
|
11117
11259
|
* The widget a {widget} definition's arguments declare: its name and its
|
|
11118
11260
|
* parameters, the words after it that start with `@` (docs/widgets.md),
|
|
11119
|
-
* separated by spaces or commas (`@a, @b`).
|
|
11120
|
-
*
|
|
11261
|
+
* separated by spaces or commas (`@a, @b`). A parameter may say what its
|
|
11262
|
+
* argument holds after a colon (`@target:passage`, one of the `holds` of a
|
|
11263
|
+
* macro parameter; another word is a `badHolds`). Other words, such as
|
|
11264
|
+
* `$name`, are not parameters.
|
|
11121
11265
|
*/
|
|
11122
11266
|
function widgetDef({ name = "", parameters = "" }) {
|
|
11267
|
+
const params = [];
|
|
11268
|
+
const holds = {};
|
|
11269
|
+
const badHolds = [];
|
|
11270
|
+
for (const word of parameters.split(/[\s,]+/)) {
|
|
11271
|
+
if (!word.startsWith("@")) continue;
|
|
11272
|
+
const [param = word, written] = word.split(/:(.*)/s);
|
|
11273
|
+
params.push(param);
|
|
11274
|
+
if (written === void 0) continue;
|
|
11275
|
+
if (STRING_HOLDS.includes(written)) holds[param] = written;
|
|
11276
|
+
else badHolds.push({
|
|
11277
|
+
param,
|
|
11278
|
+
written
|
|
11279
|
+
});
|
|
11280
|
+
}
|
|
11123
11281
|
return {
|
|
11124
11282
|
name,
|
|
11125
|
-
params
|
|
11283
|
+
params,
|
|
11284
|
+
...Object.keys(holds).length > 0 ? { holds } : {},
|
|
11285
|
+
...badHolds.length > 0 ? { badHolds } : {}
|
|
11126
11286
|
};
|
|
11127
11287
|
}
|
|
11128
11288
|
/** Read the arguments of a {widget} definition (see widgetDef). */
|
|
@@ -11186,15 +11346,6 @@ function widgetDefinitions$1(passages, parametersOf = registeredParameters) {
|
|
|
11186
11346
|
}
|
|
11187
11347
|
return found;
|
|
11188
11348
|
}
|
|
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
11349
|
//#endregion
|
|
11199
11350
|
//#region src/markup/validate.ts
|
|
11200
11351
|
/**
|
|
@@ -11398,35 +11549,6 @@ function validateMarkup(passages, options) {
|
|
|
11398
11549
|
return diagnostics;
|
|
11399
11550
|
}
|
|
11400
11551
|
//#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
11552
|
//#region src/static-literal.ts
|
|
11431
11553
|
/**
|
|
11432
11554
|
* The shape of a JavaScript literal, read from its text without running it:
|
|
@@ -11437,10 +11559,12 @@ Object.freeze(createNamespace());
|
|
|
11437
11559
|
* an operator, a reference, a spread) is "not static", which is no error: the
|
|
11438
11560
|
* value is simply not known without running the code.
|
|
11439
11561
|
*/
|
|
11440
|
-
var NUMBER_RE = /(?:0[xX][\da-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][+-]?\d
|
|
11562
|
+
var NUMBER_RE = /(?:0[xX][\da-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][+-]?\d[\d_]*)?)(n?)/y;
|
|
11441
11563
|
var IDENT_RE = /[A-Za-z_$][\w$]*/y;
|
|
11442
11564
|
var KEY_RE = /[\w$]+/y;
|
|
11443
11565
|
var ARROW_RE = /(?:async\s+)?(?:\((?:[^()]|\([^()]*\))*\)|[A-Za-z_$][\w$]*)\s*=>/y;
|
|
11566
|
+
/** The end of a line: the four line terminators of JavaScript. */
|
|
11567
|
+
var LINE_END_RE = /[\n\r\u2028\u2029]/g;
|
|
11444
11568
|
var ARRAY_INDEX_RE = /^(?:0|[1-9]\d{0,8})$/;
|
|
11445
11569
|
var UNSUPPORTED_UNDEFINED = {
|
|
11446
11570
|
unsupported: "undefined",
|
|
@@ -11462,6 +11586,22 @@ function entryOrder(keys) {
|
|
|
11462
11586
|
const all = [...keys];
|
|
11463
11587
|
return [...all.filter((key) => ARRAY_INDEX_RE.test(key)).sort((a, b) => Number(a) - Number(b)), ...all.filter((key) => !ARRAY_INDEX_RE.test(key))];
|
|
11464
11588
|
}
|
|
11589
|
+
/**
|
|
11590
|
+
* Where the regex literals in `src` are, by their start: JavaScript text
|
|
11591
|
+
* cannot tell a regex from a division without the code before it, which the
|
|
11592
|
+
* lexer reads. `handlers` see the same walk.
|
|
11593
|
+
*/
|
|
11594
|
+
function regexLiterals(src, goal = "expression", handlers = {}) {
|
|
11595
|
+
const ends = /* @__PURE__ */ new Map();
|
|
11596
|
+
lexJs(src, {
|
|
11597
|
+
...handlers,
|
|
11598
|
+
literal(text, index, nesting) {
|
|
11599
|
+
if (text.charAt(0) === "/" && !/^\/[/*]/.test(text)) ends.set(index, index + text.length);
|
|
11600
|
+
handlers.literal?.(text, index, nesting);
|
|
11601
|
+
}
|
|
11602
|
+
}, goal);
|
|
11603
|
+
return ends;
|
|
11604
|
+
}
|
|
11465
11605
|
/** `String(value)` of a BigInt literal's digits, or `undefined` if they are no BigInt. */
|
|
11466
11606
|
function bigintValue(digits) {
|
|
11467
11607
|
try {
|
|
@@ -11475,9 +11615,15 @@ function bigintValue(digits) {
|
|
|
11475
11615
|
* helpers that skip what is not read are shared (see macro-discovery.ts).
|
|
11476
11616
|
*/
|
|
11477
11617
|
var Reader = class {
|
|
11478
|
-
|
|
11618
|
+
/**
|
|
11619
|
+
* `regexes` (see regexLiterals) are read from `src` when they are first
|
|
11620
|
+
* needed, unless the caller has them already.
|
|
11621
|
+
*/
|
|
11622
|
+
constructor(src, options = {}) {
|
|
11479
11623
|
this.src = src;
|
|
11480
11624
|
this.i = 0;
|
|
11625
|
+
this.goal = options.goal ?? "expression";
|
|
11626
|
+
this.regexes = options.regexes;
|
|
11481
11627
|
}
|
|
11482
11628
|
/** Skip whitespace and comments. */
|
|
11483
11629
|
space() {
|
|
@@ -11485,8 +11631,8 @@ var Reader = class {
|
|
|
11485
11631
|
for (;;) {
|
|
11486
11632
|
while (/\s/.test(src.charAt(this.i))) this.i++;
|
|
11487
11633
|
if (src.startsWith("//", this.i)) {
|
|
11488
|
-
|
|
11489
|
-
this.i =
|
|
11634
|
+
LINE_END_RE.lastIndex = this.i;
|
|
11635
|
+
this.i = LINE_END_RE.test(src) ? LINE_END_RE.lastIndex - 1 : src.length;
|
|
11490
11636
|
} else if (src.startsWith("/*", this.i)) {
|
|
11491
11637
|
const end = src.indexOf("*/", this.i + 2);
|
|
11492
11638
|
this.i = end < 0 ? src.length : end + 2;
|
|
@@ -11545,10 +11691,9 @@ var Reader = class {
|
|
|
11545
11691
|
i++;
|
|
11546
11692
|
while (/\s/.test(src.charAt(i))) i++;
|
|
11547
11693
|
}
|
|
11548
|
-
|
|
11549
|
-
const match = NUMBER_RE.exec(src);
|
|
11694
|
+
const match = this.numberAt(i);
|
|
11550
11695
|
if (match) {
|
|
11551
|
-
this.i =
|
|
11696
|
+
this.i = i + match[0].length;
|
|
11552
11697
|
if (!match[1]) return { shape: { type: "number" } };
|
|
11553
11698
|
if (sign === "+") return { throws: "Cannot convert a BigInt value to a number" };
|
|
11554
11699
|
const digits = bigintValue(match[0].slice(0, -1));
|
|
@@ -11558,11 +11703,17 @@ var Reader = class {
|
|
|
11558
11703
|
};
|
|
11559
11704
|
}
|
|
11560
11705
|
IDENT_RE.lastIndex = i;
|
|
11561
|
-
|
|
11706
|
+
const word = IDENT_RE.exec(src)?.[0];
|
|
11707
|
+
if (word === "Infinity" || word === "NaN") {
|
|
11562
11708
|
this.i = IDENT_RE.lastIndex;
|
|
11563
11709
|
return { shape: { type: "number" } };
|
|
11564
11710
|
}
|
|
11565
11711
|
}
|
|
11712
|
+
/** The numeric literal (a BigInt's `n` is `match[1]`) at `at`, if there is one. */
|
|
11713
|
+
numberAt(at) {
|
|
11714
|
+
NUMBER_RE.lastIndex = at;
|
|
11715
|
+
return NUMBER_RE.exec(this.src);
|
|
11716
|
+
}
|
|
11566
11717
|
/** A keyword, a function, or a name (which is not static). */
|
|
11567
11718
|
word() {
|
|
11568
11719
|
const { src } = this;
|
|
@@ -11582,6 +11733,7 @@ var Reader = class {
|
|
|
11582
11733
|
this.i = end;
|
|
11583
11734
|
return { shape: { type: "null" } };
|
|
11584
11735
|
case "NaN":
|
|
11736
|
+
case "Infinity":
|
|
11585
11737
|
this.i = end;
|
|
11586
11738
|
return { shape: { type: "number" } };
|
|
11587
11739
|
case "undefined":
|
|
@@ -11722,14 +11874,22 @@ var Reader = class {
|
|
|
11722
11874
|
const { src } = this;
|
|
11723
11875
|
const c = src.charAt(this.i);
|
|
11724
11876
|
if (c === "\"" || c === "'") return this.stringValue();
|
|
11877
|
+
if (/[\d.]/.test(c)) return this.numberKey();
|
|
11725
11878
|
KEY_RE.lastIndex = this.i;
|
|
11726
11879
|
const found = KEY_RE.exec(src)?.[0];
|
|
11727
11880
|
if (found === void 0) return void 0;
|
|
11728
11881
|
this.i = KEY_RE.lastIndex;
|
|
11729
|
-
|
|
11730
|
-
|
|
11882
|
+
return found;
|
|
11883
|
+
}
|
|
11884
|
+
/** A number as a key: the whole literal, named as the runtime names it. */
|
|
11885
|
+
numberKey() {
|
|
11886
|
+
const match = this.numberAt(this.i);
|
|
11887
|
+
if (!match) return void 0;
|
|
11888
|
+
this.i += match[0].length;
|
|
11889
|
+
if (/[\w$]/.test(this.src.charAt(this.i))) return void 0;
|
|
11890
|
+
const digits = match[0].replace(/_/g, "");
|
|
11731
11891
|
if (/^0\d/.test(digits)) return void 0;
|
|
11732
|
-
if (
|
|
11892
|
+
if (match[1]) return bigintValue(digits.slice(0, -1));
|
|
11733
11893
|
const value = Number(digits);
|
|
11734
11894
|
return Number.isNaN(value) ? void 0 : String(value);
|
|
11735
11895
|
}
|
|
@@ -11772,6 +11932,14 @@ var Reader = class {
|
|
|
11772
11932
|
this.space();
|
|
11773
11933
|
return true;
|
|
11774
11934
|
}
|
|
11935
|
+
if (c === "/") {
|
|
11936
|
+
this.regexes ?? (this.regexes = regexLiterals(src, this.goal));
|
|
11937
|
+
const end = this.regexes.get(this.i);
|
|
11938
|
+
if (end !== void 0) {
|
|
11939
|
+
this.i = end;
|
|
11940
|
+
return true;
|
|
11941
|
+
}
|
|
11942
|
+
}
|
|
11775
11943
|
if (c === "[" || c === "{" || c === "(") return this.skipBalanced();
|
|
11776
11944
|
if (c === "]" || c === "}" || c === ")") return false;
|
|
11777
11945
|
this.i++;
|
|
@@ -12026,6 +12194,7 @@ function checkReference(ref, declared) {
|
|
|
12026
12194
|
const part = ref.path[i];
|
|
12027
12195
|
if (current.type === "array") return null;
|
|
12028
12196
|
if (current.type === "null") return null;
|
|
12197
|
+
if (current.type === "string" && isArrayIndex(part)) continue;
|
|
12029
12198
|
const sample = PRIMITIVE_SAMPLES[current.type];
|
|
12030
12199
|
if (sample && part in sample) {
|
|
12031
12200
|
const memberType = typeof sample[part];
|
|
@@ -12042,10 +12211,28 @@ function checkReference(ref, declared) {
|
|
|
12042
12211
|
return null;
|
|
12043
12212
|
}
|
|
12044
12213
|
var NO_STORE_VAR_MACROS = /* @__PURE__ */ new Set();
|
|
12045
|
-
/** A
|
|
12046
|
-
|
|
12214
|
+
/** A `.field` path written right after a reference, which may be empty. */
|
|
12215
|
+
var FIELD_PATH_RE = /(?:\.\w+)*/y;
|
|
12216
|
+
/**
|
|
12217
|
+
* The reference to variable `name`, which JavaScript reads at `[start, end)`
|
|
12218
|
+
* of `code` (the sigil and the name as written, which may be spelled with
|
|
12219
|
+
* escapes), with the dot path written after it.
|
|
12220
|
+
*/
|
|
12221
|
+
function referenceAt(code, sigil, name, start, end, at) {
|
|
12222
|
+
FIELD_PATH_RE.lastIndex = end;
|
|
12223
|
+
const fields = FIELD_PATH_RE.exec(code)[0];
|
|
12224
|
+
return {
|
|
12225
|
+
sigil,
|
|
12226
|
+
name,
|
|
12227
|
+
path: fields ? fields.slice(1).split(".") : [],
|
|
12228
|
+
start: at(start),
|
|
12229
|
+
end: at(end + fields.length)
|
|
12230
|
+
};
|
|
12231
|
+
}
|
|
12232
|
+
/** A reference written at `index` of `term`, a `variable` parameter as written. */
|
|
12233
|
+
function termReferenceAt(term, index, at) {
|
|
12047
12234
|
VAR_PATH_RE.lastIndex = index;
|
|
12048
|
-
const match = VAR_PATH_RE.exec(
|
|
12235
|
+
const match = VAR_PATH_RE.exec(term);
|
|
12049
12236
|
if (!match) return void 0;
|
|
12050
12237
|
const [name, ...path] = match[2].split(".");
|
|
12051
12238
|
return {
|
|
@@ -12090,46 +12277,51 @@ function scanCode(code, at, emit, goal = "expression") {
|
|
|
12090
12277
|
}
|
|
12091
12278
|
for (const ref of parsed.refs) {
|
|
12092
12279
|
if (ref.sigil !== "$" && ref.sigil !== "%") continue;
|
|
12093
|
-
|
|
12094
|
-
if (found) emit(found);
|
|
12280
|
+
emit(referenceAt(code, ref.sigil, ref.name, ref.start, ref.end, at));
|
|
12095
12281
|
}
|
|
12096
12282
|
}
|
|
12097
12283
|
/** `scanCode` for code acorn can't parse: lexed leniently. */
|
|
12098
12284
|
function scanCodeLeniently(code, at, emit, goal) {
|
|
12099
12285
|
lexJs(code, {
|
|
12100
|
-
variable(sigil,
|
|
12286
|
+
variable(sigil, name, index, _nesting, end) {
|
|
12101
12287
|
if (sigil !== "$" && sigil !== "%") return;
|
|
12102
|
-
|
|
12103
|
-
if (found) emit(found);
|
|
12288
|
+
emit(referenceAt(code, sigil, name, index, end, at));
|
|
12104
12289
|
},
|
|
12105
12290
|
literal() {}
|
|
12106
12291
|
}, goal);
|
|
12107
12292
|
}
|
|
12293
|
+
/** Report the references in the pieces of code and markup text of arguments. */
|
|
12294
|
+
function scanPieces(pieces, at, emit, options) {
|
|
12295
|
+
for (const piece of pieces) {
|
|
12296
|
+
const pieceAt = (i) => at(pieceOffset(piece, i));
|
|
12297
|
+
if (piece.kind === "code") scanCode(piece.code, pieceAt, emit, piece.goal);
|
|
12298
|
+
else if (piece.kind === "text") scanInterpolations(piece.text, pieceAt, emit, {
|
|
12299
|
+
...options,
|
|
12300
|
+
storeVarMacros: NO_STORE_VAR_MACROS
|
|
12301
|
+
});
|
|
12302
|
+
}
|
|
12303
|
+
}
|
|
12108
12304
|
/**
|
|
12109
12305
|
* Report the references in the arguments of a macro by what its parameters
|
|
12110
12306
|
* say each holds (see ParameterDef.holds): code is scanned as code and
|
|
12111
12307
|
* markup as markup, but literal text (a placeholder, a passage name) holds
|
|
12112
12308
|
* none. A macro without declared parameters, or arguments that do not
|
|
12113
|
-
* parse, are scanned as code.
|
|
12309
|
+
* parse, are scanned as code. The arguments of a widget that declares what
|
|
12310
|
+
* some parameters hold are also read by those (see widgetPieces).
|
|
12114
12311
|
*/
|
|
12115
|
-
function scanArgs(token,
|
|
12312
|
+
function scanArgs(token, parametersOf, at, emit, options) {
|
|
12116
12313
|
if (!token.rawArgs) return;
|
|
12314
|
+
const params = parametersOf(token.name);
|
|
12315
|
+
let byParameters = false;
|
|
12117
12316
|
if (params) {
|
|
12118
12317
|
if (options.all && !options.storeVarMacros.has(token.name.toLowerCase())) scanVariableParameters(token, params, at, emit);
|
|
12119
12318
|
const pieces = [...argPieces(token.rawArgs, token.argsStart, params, token.name, "")];
|
|
12120
|
-
|
|
12121
|
-
|
|
12122
|
-
const pieceAt = (i) => at(pieceOffset(piece, i));
|
|
12123
|
-
if (piece.kind === "code") scanCode(piece.code, pieceAt, emit, piece.goal);
|
|
12124
|
-
else if (piece.kind === "text") scanInterpolations(piece.text, pieceAt, emit, {
|
|
12125
|
-
...options,
|
|
12126
|
-
storeVarMacros: NO_STORE_VAR_MACROS
|
|
12127
|
-
});
|
|
12128
|
-
}
|
|
12129
|
-
return;
|
|
12130
|
-
}
|
|
12319
|
+
byParameters = !pieces.some((piece) => piece.kind === "argument-error");
|
|
12320
|
+
if (byParameters) scanPieces(pieces, at, emit, options);
|
|
12131
12321
|
}
|
|
12132
|
-
scanCode(token.rawArgs, (i) => at(token.argsStart + i), emit);
|
|
12322
|
+
if (!byParameters) scanCode(token.rawArgs, (i) => at(token.argsStart + i), emit);
|
|
12323
|
+
const widget = parametersOf.widget?.(token.name);
|
|
12324
|
+
if (widget) scanPieces(widgetPieces(token.rawArgs, token.argsStart, widget, token.name, ""), at, emit, options);
|
|
12133
12325
|
}
|
|
12134
12326
|
/**
|
|
12135
12327
|
* Report the variables the `variable` parameters of a macro name
|
|
@@ -12147,7 +12339,7 @@ function scanVariableParameters(token, params, at, emit) {
|
|
|
12147
12339
|
for (const param of params) {
|
|
12148
12340
|
const span = spans.get(param);
|
|
12149
12341
|
if (param.type !== "variable" || !span) continue;
|
|
12150
|
-
const ref =
|
|
12342
|
+
const ref = termReferenceAt(token.rawArgs.slice(span[0], span[1]), 0, (i) => at(token.argsStart + span[0] + i));
|
|
12151
12343
|
if (ref) emit(ref);
|
|
12152
12344
|
}
|
|
12153
12345
|
}
|
|
@@ -12188,7 +12380,7 @@ function collectTokenRefs(content, tokens, at, emit, options) {
|
|
|
12188
12380
|
else scanInterpolations(value, valueIn, emit, options);
|
|
12189
12381
|
}
|
|
12190
12382
|
else if (token.type === "macro" && !token.isClose) {
|
|
12191
|
-
scanArgs(token, parametersOf
|
|
12383
|
+
scanArgs(token, parametersOf, at, emit, options);
|
|
12192
12384
|
scanSelectors(content, token, at, emit, options);
|
|
12193
12385
|
if (storeVarMacros.has(token.name.toLowerCase())) {
|
|
12194
12386
|
const lead = token.rawArgs.length - token.rawArgs.trimStart().length;
|
|
@@ -12231,7 +12423,7 @@ function collectTokenRefs(content, tokens, at, emit, options) {
|
|
|
12231
12423
|
function collectVariableReferences(content, options) {
|
|
12232
12424
|
const found = [];
|
|
12233
12425
|
withParseCache(() => collectTokenRefs(content, tokensOf(content, false, options), (i) => i, (ref) => found.push(ref), options));
|
|
12234
|
-
return found;
|
|
12426
|
+
return found.sort((a, b) => a.start - b.start);
|
|
12235
12427
|
}
|
|
12236
12428
|
/**
|
|
12237
12429
|
* The invalid variable references of the passages, against what is declared,
|
|
@@ -12259,6 +12451,273 @@ function checkVariableReferences(passages, declared, options) {
|
|
|
12259
12451
|
return diagnostics;
|
|
12260
12452
|
}
|
|
12261
12453
|
//#endregion
|
|
12454
|
+
//#region src/js-bindings.ts
|
|
12455
|
+
var CLOSERS = {
|
|
12456
|
+
"}": "{",
|
|
12457
|
+
")": "(",
|
|
12458
|
+
"]": "["
|
|
12459
|
+
};
|
|
12460
|
+
/** What `(…) {` is when a word like this is before the `(`: a block. */
|
|
12461
|
+
var BLOCK_WORDS = /* @__PURE__ */ new Set([
|
|
12462
|
+
"if",
|
|
12463
|
+
"for",
|
|
12464
|
+
"while",
|
|
12465
|
+
"switch",
|
|
12466
|
+
"with",
|
|
12467
|
+
"catch"
|
|
12468
|
+
]);
|
|
12469
|
+
var NOT_PARAMETERS = /* @__PURE__ */ new Set([
|
|
12470
|
+
"if",
|
|
12471
|
+
"for",
|
|
12472
|
+
"while",
|
|
12473
|
+
"switch",
|
|
12474
|
+
"with"
|
|
12475
|
+
]);
|
|
12476
|
+
var DECLARATIONS = /* @__PURE__ */ new Set([
|
|
12477
|
+
"const",
|
|
12478
|
+
"let",
|
|
12479
|
+
"var"
|
|
12480
|
+
]);
|
|
12481
|
+
/** Before a name in a pattern or parameter list, by the group around it. */
|
|
12482
|
+
var BEFORE_BINDING = {
|
|
12483
|
+
"{": "{,:",
|
|
12484
|
+
"[": "[,",
|
|
12485
|
+
"(": "(,"
|
|
12486
|
+
};
|
|
12487
|
+
var isWordChar = (c) => !!c && /[\w$]/.test(c);
|
|
12488
|
+
/** Resolve the declaration of variables in `code` (see blankedCode in macro-discovery). */
|
|
12489
|
+
var Bindings = class {
|
|
12490
|
+
constructor(code) {
|
|
12491
|
+
this.code = code;
|
|
12492
|
+
this.groups = [];
|
|
12493
|
+
this.byOpen = /* @__PURE__ */ new Map();
|
|
12494
|
+
this.byClose = /* @__PURE__ */ new Map();
|
|
12495
|
+
this.functions = /* @__PURE__ */ new Map();
|
|
12496
|
+
this.top = {
|
|
12497
|
+
start: -1,
|
|
12498
|
+
end: Infinity
|
|
12499
|
+
};
|
|
12500
|
+
this.readGroups();
|
|
12501
|
+
}
|
|
12502
|
+
/**
|
|
12503
|
+
* The `{` of the object literal that `name`, used at `at`, is declared as
|
|
12504
|
+
* (`const`, `let` or `var`), when that is the declaration it names: the
|
|
12505
|
+
* innermost one in the scope of the use, that comes before it unless the
|
|
12506
|
+
* use is in a function of its own. `undefined` if it names another value,
|
|
12507
|
+
* or more than one declaration is the innermost.
|
|
12508
|
+
*/
|
|
12509
|
+
objectOf(name, at) {
|
|
12510
|
+
const { code } = this;
|
|
12511
|
+
const escaped = name.replace(/\$/g, "\\$");
|
|
12512
|
+
const bindings = [];
|
|
12513
|
+
for (const m of code.matchAll(new RegExp(`(?<![\\w$])${escaped}(?![\\w$])`, "g"))) {
|
|
12514
|
+
const binding = this.bindingAt(m.index, name.length);
|
|
12515
|
+
if (binding) bindings.push(binding);
|
|
12516
|
+
}
|
|
12517
|
+
const useFunction = this.functionStart(at);
|
|
12518
|
+
const visible = bindings.filter((b) => b.scope.start <= at && at < b.scope.end && (!b.ordered || b.at < at || this.functionStart(b.at) !== useFunction));
|
|
12519
|
+
const innermost = Math.max(...visible.map((b) => b.scope.start));
|
|
12520
|
+
const found = visible.filter((b) => b.scope.start === innermost);
|
|
12521
|
+
return found.length === 1 ? found[0].object : void 0;
|
|
12522
|
+
}
|
|
12523
|
+
readGroups() {
|
|
12524
|
+
const { code, groups } = this;
|
|
12525
|
+
const stack = [];
|
|
12526
|
+
for (let i = 0; i < code.length; i++) {
|
|
12527
|
+
const c = code.charAt(i);
|
|
12528
|
+
if (c === "{" || c === "(" || c === "[") {
|
|
12529
|
+
const group = {
|
|
12530
|
+
kind: c,
|
|
12531
|
+
open: i,
|
|
12532
|
+
close: code.length,
|
|
12533
|
+
parent: stack[stack.length - 1]
|
|
12534
|
+
};
|
|
12535
|
+
groups.push(group);
|
|
12536
|
+
stack.push(group);
|
|
12537
|
+
this.byOpen.set(i, group);
|
|
12538
|
+
} else if (CLOSERS[c]) {
|
|
12539
|
+
let k = stack.length - 1;
|
|
12540
|
+
while (k >= 0 && stack[k].kind !== CLOSERS[c]) k--;
|
|
12541
|
+
if (k >= 0) {
|
|
12542
|
+
stack[k].close = i;
|
|
12543
|
+
this.byClose.set(i, stack[k]);
|
|
12544
|
+
stack.length = k;
|
|
12545
|
+
}
|
|
12546
|
+
}
|
|
12547
|
+
}
|
|
12548
|
+
}
|
|
12549
|
+
/** The innermost group that has `index` inside it, of this kind if given. */
|
|
12550
|
+
innermost(index, kind) {
|
|
12551
|
+
const { groups } = this;
|
|
12552
|
+
for (let k = groups.length - 1; k >= 0; k--) {
|
|
12553
|
+
const g = groups[k];
|
|
12554
|
+
if (g.open < index && index < g.close && (!kind || g.kind === kind)) return g;
|
|
12555
|
+
}
|
|
12556
|
+
}
|
|
12557
|
+
/** Whether the `{` group is the body of a function (or method). */
|
|
12558
|
+
isFunctionBody(group) {
|
|
12559
|
+
if (group.kind !== "{") return false;
|
|
12560
|
+
let known = this.functions.get(group);
|
|
12561
|
+
if (known === void 0) {
|
|
12562
|
+
const before = this.backFrom(group.open);
|
|
12563
|
+
const { code } = this;
|
|
12564
|
+
if (code.charAt(before) === ")") {
|
|
12565
|
+
const params = this.byClose.get(before);
|
|
12566
|
+
const word = params && this.wordBefore(this.backFrom(params.open) + 1);
|
|
12567
|
+
known = !!params && !BLOCK_WORDS.has(word.text);
|
|
12568
|
+
} else known = code.slice(before - 1, before + 1) === "=>";
|
|
12569
|
+
this.functions.set(group, known);
|
|
12570
|
+
}
|
|
12571
|
+
return known;
|
|
12572
|
+
}
|
|
12573
|
+
/** The start of the function that has `index` in its body, or -1. */
|
|
12574
|
+
functionStart(index) {
|
|
12575
|
+
for (let g = this.innermost(index, "{"); g; g = g.parent) if (g.kind === "{" && this.isFunctionBody(g)) return g.open;
|
|
12576
|
+
return -1;
|
|
12577
|
+
}
|
|
12578
|
+
scopeOf(group) {
|
|
12579
|
+
return group ? {
|
|
12580
|
+
start: group.open,
|
|
12581
|
+
end: group.close
|
|
12582
|
+
} : this.top;
|
|
12583
|
+
}
|
|
12584
|
+
/** The block `index` is in: what `let` and `const` declare for. */
|
|
12585
|
+
blockAt(index) {
|
|
12586
|
+
return this.scopeOf(this.innermost(index, "{"));
|
|
12587
|
+
}
|
|
12588
|
+
/** The function (or the whole text) `index` is in: what `var` declares for. */
|
|
12589
|
+
functionAt(index) {
|
|
12590
|
+
const start = this.functionStart(index);
|
|
12591
|
+
return start < 0 ? this.top : this.scopeOf(this.byOpen.get(start));
|
|
12592
|
+
}
|
|
12593
|
+
/** What the declaration keyword `word` (`var`, `let`, `const`) declares for. */
|
|
12594
|
+
declaredIn(word) {
|
|
12595
|
+
return word.text === "var" ? this.functionAt(word.start) : this.blockAt(word.start);
|
|
12596
|
+
}
|
|
12597
|
+
/** What a parameter list, whose `)` is `close`, is the scope of: the body. */
|
|
12598
|
+
bodyAfter(close) {
|
|
12599
|
+
const next = this.forward(close + 1);
|
|
12600
|
+
if (this.code.charAt(next) === "{") return this.scopeOf(this.byOpen.get(next));
|
|
12601
|
+
return this.code.startsWith("=>", next) ? this.arrowBody(next + 2) : void 0;
|
|
12602
|
+
}
|
|
12603
|
+
/** The body of an arrow function, whose `=>` ends before `from`. */
|
|
12604
|
+
arrowBody(from) {
|
|
12605
|
+
const { code } = this;
|
|
12606
|
+
const start = this.forward(from);
|
|
12607
|
+
if (code.charAt(start) === "{") return this.scopeOf(this.byOpen.get(start));
|
|
12608
|
+
let end = start;
|
|
12609
|
+
while (end < code.length && !",;)]}".includes(code.charAt(end))) {
|
|
12610
|
+
end = this.byOpen.get(end)?.close ?? end;
|
|
12611
|
+
end++;
|
|
12612
|
+
}
|
|
12613
|
+
return {
|
|
12614
|
+
start,
|
|
12615
|
+
end
|
|
12616
|
+
};
|
|
12617
|
+
}
|
|
12618
|
+
/** The index of the last character that is no space before `index`, or -1. */
|
|
12619
|
+
backFrom(index) {
|
|
12620
|
+
let i = index - 1;
|
|
12621
|
+
while (i >= 0 && /\s/.test(this.code.charAt(i))) i--;
|
|
12622
|
+
return i;
|
|
12623
|
+
}
|
|
12624
|
+
/** The index of the first character that is no space from `index` on. */
|
|
12625
|
+
forward(index) {
|
|
12626
|
+
let i = index;
|
|
12627
|
+
while (i < this.code.length && /\s/.test(this.code.charAt(i))) i++;
|
|
12628
|
+
return i;
|
|
12629
|
+
}
|
|
12630
|
+
/** The word that ends before `end`, and where it starts. */
|
|
12631
|
+
wordBefore(end) {
|
|
12632
|
+
let start = end;
|
|
12633
|
+
while (start > 0 && isWordChar(this.code.charAt(start - 1))) start--;
|
|
12634
|
+
return {
|
|
12635
|
+
text: this.code.slice(start, end),
|
|
12636
|
+
start
|
|
12637
|
+
};
|
|
12638
|
+
}
|
|
12639
|
+
/** What the name of `length` characters at `at` declares, if it does. */
|
|
12640
|
+
bindingAt(at, length) {
|
|
12641
|
+
const { code } = this;
|
|
12642
|
+
let before = this.backFrom(at);
|
|
12643
|
+
const spread = code.slice(before - 2, before + 1) === "...";
|
|
12644
|
+
if (code.charAt(before) === "." && !spread) return void 0;
|
|
12645
|
+
if (spread) before = this.backFrom(before - 2);
|
|
12646
|
+
const after = this.forward(at + length);
|
|
12647
|
+
return this.declarationAt(at, before, spread) ?? this.patternAt(at, before, after) ?? this.arrowParameterAt(at, after) ?? this.assignmentAt(at, before, after);
|
|
12648
|
+
}
|
|
12649
|
+
/** `const x`, `let x`, `var x`, `function x`, `class x`. */
|
|
12650
|
+
declarationAt(at, before, spread) {
|
|
12651
|
+
const word = spread ? void 0 : this.wordBefore(before + 1);
|
|
12652
|
+
if (!word || this.code.charAt(word.start - 1) === ".") return void 0;
|
|
12653
|
+
if (DECLARATIONS.has(word.text)) {
|
|
12654
|
+
const scope = this.declaredIn(word);
|
|
12655
|
+
const init = /\s*=\s*(?=\{)/y;
|
|
12656
|
+
init.lastIndex = this.nameEnd(at);
|
|
12657
|
+
return {
|
|
12658
|
+
scope,
|
|
12659
|
+
at,
|
|
12660
|
+
ordered: true,
|
|
12661
|
+
object: init.test(this.code) ? init.lastIndex : void 0
|
|
12662
|
+
};
|
|
12663
|
+
}
|
|
12664
|
+
if (word.text === "function" || word.text === "class") return {
|
|
12665
|
+
scope: this.blockAt(word.start),
|
|
12666
|
+
at,
|
|
12667
|
+
ordered: word.text === "class"
|
|
12668
|
+
};
|
|
12669
|
+
}
|
|
12670
|
+
nameEnd(at) {
|
|
12671
|
+
let end = at;
|
|
12672
|
+
while (isWordChar(this.code.charAt(end))) end++;
|
|
12673
|
+
return end;
|
|
12674
|
+
}
|
|
12675
|
+
/** A name in a pattern (`const { x } = …`) or a parameter list. */
|
|
12676
|
+
patternAt(at, before, after) {
|
|
12677
|
+
const { code } = this;
|
|
12678
|
+
let group = this.innermost(at);
|
|
12679
|
+
if (!group || !BEFORE_BINDING[group.kind].includes(code.charAt(before))) return;
|
|
12680
|
+
if (group.kind === "{" && code.charAt(after) === ":") return void 0;
|
|
12681
|
+
for (; group; group = group.parent) {
|
|
12682
|
+
if (group.kind === "(") {
|
|
12683
|
+
const word = this.wordBefore(this.backFrom(group.open) + 1).text;
|
|
12684
|
+
const scope = NOT_PARAMETERS.has(word) ? void 0 : this.bodyAfter(group.close);
|
|
12685
|
+
return scope && {
|
|
12686
|
+
scope,
|
|
12687
|
+
at,
|
|
12688
|
+
ordered: false
|
|
12689
|
+
};
|
|
12690
|
+
}
|
|
12691
|
+
const open = this.backFrom(group.open);
|
|
12692
|
+
const word = this.wordBefore(open + 1);
|
|
12693
|
+
if (DECLARATIONS.has(word.text)) return {
|
|
12694
|
+
scope: this.declaredIn(word),
|
|
12695
|
+
at,
|
|
12696
|
+
ordered: true
|
|
12697
|
+
};
|
|
12698
|
+
if (!"({[,:".includes(code.charAt(open))) return void 0;
|
|
12699
|
+
}
|
|
12700
|
+
}
|
|
12701
|
+
/** `x => …`. */
|
|
12702
|
+
arrowParameterAt(at, after) {
|
|
12703
|
+
return this.code.startsWith("=>", after) ? {
|
|
12704
|
+
scope: this.arrowBody(after + 2),
|
|
12705
|
+
at,
|
|
12706
|
+
ordered: false
|
|
12707
|
+
} : void 0;
|
|
12708
|
+
}
|
|
12709
|
+
/** `x = …`: a declaration in a list, or an assignment: a value not known. */
|
|
12710
|
+
assignmentAt(at, before, after) {
|
|
12711
|
+
const { code } = this;
|
|
12712
|
+
if (code.charAt(after) !== "=" || "=>".includes(code.charAt(after + 1))) return;
|
|
12713
|
+
return {
|
|
12714
|
+
scope: code.charAt(before) === "," ? this.functionAt(at) : this.blockAt(at),
|
|
12715
|
+
at,
|
|
12716
|
+
ordered: true
|
|
12717
|
+
};
|
|
12718
|
+
}
|
|
12719
|
+
};
|
|
12720
|
+
//#endregion
|
|
12262
12721
|
//#region src/macro-discovery.ts
|
|
12263
12722
|
/**
|
|
12264
12723
|
* What `defineMacro({ … })` calls in JavaScript declare, read from the text
|
|
@@ -12385,13 +12844,19 @@ var ConfigReader = class extends Reader {
|
|
|
12385
12844
|
this.failed = true;
|
|
12386
12845
|
}
|
|
12387
12846
|
};
|
|
12388
|
-
/**
|
|
12847
|
+
/**
|
|
12848
|
+
* The text with everything but code (strings, comments, regexes) blanked, and
|
|
12849
|
+
* where its regex literals are.
|
|
12850
|
+
*/
|
|
12389
12851
|
function codeOnly(source) {
|
|
12390
12852
|
const chars = Array.from({ length: source.length }, () => " ");
|
|
12391
|
-
|
|
12853
|
+
const regexes = regexLiterals(source, "statements", { code(ch, index) {
|
|
12392
12854
|
for (let k = 0; k < ch.length; k++) chars[index + k] = ch[k];
|
|
12393
|
-
} }
|
|
12394
|
-
return
|
|
12855
|
+
} });
|
|
12856
|
+
return {
|
|
12857
|
+
code: chars.join(""),
|
|
12858
|
+
regexes
|
|
12859
|
+
};
|
|
12395
12860
|
}
|
|
12396
12861
|
var stringOf = (d) => d?.type === "string" ? d : void 0;
|
|
12397
12862
|
var booleanOf = (d) => d?.type === "boolean" ? d.value : void 0;
|
|
@@ -12465,8 +12930,9 @@ function macroOf(config) {
|
|
|
12465
12930
|
* `source` (JavaScript: a script passage, the body of a `{do}`, a project
|
|
12466
12931
|
* file) declare, in source order, read from the text without running it. A
|
|
12467
12932
|
* call whose config is an object literal (or a variable declared as one with
|
|
12468
|
-
* `const`, `let` or `var`
|
|
12469
|
-
*
|
|
12933
|
+
* `const`, `let` or `var` that the call sees, see Bindings: not a parameter, a
|
|
12934
|
+
* pattern or an assignment of the name) and whose `name` is a string is found,
|
|
12935
|
+
* wherever it is, even inside a function. Only what is written out is read: a `block`,
|
|
12470
12936
|
* `interpolate`, `storeVar` or `merged` that is a boolean literal, the
|
|
12471
12937
|
* strings of `subMacros`, and `parameters` when every one is an object
|
|
12472
12938
|
* literal with a string `name` and `type` (and `holds`, `required`,
|
|
@@ -12476,18 +12942,23 @@ function macroOf(config) {
|
|
|
12476
12942
|
* throws for none.
|
|
12477
12943
|
*/
|
|
12478
12944
|
function discoverMacros(source) {
|
|
12479
|
-
const code = codeOnly(source);
|
|
12945
|
+
const { code, regexes } = codeOnly(source);
|
|
12480
12946
|
const found = [];
|
|
12947
|
+
let bindings;
|
|
12481
12948
|
for (const call of code.matchAll(CALL_RE)) {
|
|
12482
12949
|
let at = call.index + call[0].length;
|
|
12483
12950
|
if (code.charAt(at) !== "{") {
|
|
12484
12951
|
const variable = IDENTIFIER_ARGUMENT_RE.exec(code.slice(at))?.[1];
|
|
12485
12952
|
if (!variable) continue;
|
|
12486
|
-
|
|
12487
|
-
|
|
12488
|
-
|
|
12953
|
+
bindings ?? (bindings = new Bindings(code));
|
|
12954
|
+
const object = bindings.objectOf(variable, call.index);
|
|
12955
|
+
if (object === void 0) continue;
|
|
12956
|
+
at = object;
|
|
12489
12957
|
}
|
|
12490
|
-
const reader = new ConfigReader(source
|
|
12958
|
+
const reader = new ConfigReader(source, {
|
|
12959
|
+
goal: "statements",
|
|
12960
|
+
regexes
|
|
12961
|
+
});
|
|
12491
12962
|
reader.i = at;
|
|
12492
12963
|
const config = reader.datum();
|
|
12493
12964
|
const macro = config && macroOf(config);
|
|
@@ -12530,17 +13001,29 @@ function splitIncludeFlag(rawArgs) {
|
|
|
12530
13001
|
* (see src/tooling.ts).
|
|
12531
13002
|
*/
|
|
12532
13003
|
var TRANSFORM_CACHE_MAX = 500;
|
|
12533
|
-
|
|
12534
|
-
|
|
12535
|
-
|
|
12536
|
-
|
|
12537
|
-
|
|
13004
|
+
/**
|
|
13005
|
+
* The identifiers generated code reads the namespaces through, and the
|
|
13006
|
+
* compiled function takes them under (see src/expression.ts). Authored
|
|
13007
|
+
* JavaScript may declare identifiers of its own (`const variables = {}` in
|
|
13008
|
+
* a helper), so these are ones it cannot: an identifier that starts with
|
|
13009
|
+
* `_` or `$` and has more word characters after it is a sigil reference,
|
|
13010
|
+
* and the lexer refuses to declare one (`__variables` is the temporary
|
|
13011
|
+
* variable `_variables`, rewritten like any other). No declaration or
|
|
13012
|
+
* parameter of the author's can then capture a reference or clash with the
|
|
13013
|
+
* generated bindings.
|
|
13014
|
+
*/
|
|
13015
|
+
var BINDINGS = {
|
|
13016
|
+
$: "__variables",
|
|
13017
|
+
_: "__temporary",
|
|
13018
|
+
"@": "__locals",
|
|
13019
|
+
"%": "__transient"
|
|
12538
13020
|
};
|
|
12539
13021
|
/** Ends with an identifier character. */
|
|
12540
13022
|
var IDENT_END_RE = /[\p{ID_Continue}$\u200c\u200d]$/u;
|
|
12541
13023
|
/**
|
|
12542
13024
|
* Transform expression: $var → variables["var"], _var → temporary["var"],
|
|
12543
|
-
* @var → locals["var"], %var → transient["var"]
|
|
13025
|
+
* @var → locals["var"], %var → transient["var"], where `variables` and the
|
|
13026
|
+
* others stand for the generated names in BINDINGS.
|
|
12544
13027
|
* The lexer (`lexJs`) finds the references: only in code (string, template
|
|
12545
13028
|
* and regex literal text and comments are left untouched), only where an
|
|
12546
13029
|
* identifier starts (not in `a$b`, nor as the property name in `obj._x`),
|
|
@@ -12563,7 +13046,7 @@ function transform(expr, goal = "expression") {
|
|
|
12563
13046
|
result += expr.slice(at, ref.start);
|
|
12564
13047
|
if (ref.shorthand) result += `${ref.name}: `;
|
|
12565
13048
|
else if (IDENT_END_RE.test(result.slice(-2))) result += " ";
|
|
12566
|
-
result += `${
|
|
13049
|
+
result += `${BINDINGS[ref.sigil]}["${ref.name}"]`;
|
|
12567
13050
|
at = ref.end;
|
|
12568
13051
|
}
|
|
12569
13052
|
result += expr.slice(at);
|
|
@@ -12591,8 +13074,9 @@ function validateStoryMarkup(passages, macros, options = {}) {
|
|
|
12591
13074
|
const list = [...passages];
|
|
12592
13075
|
const all = [...macros];
|
|
12593
13076
|
const known = /* @__PURE__ */ new Set();
|
|
12594
|
-
const
|
|
12595
|
-
const blocks = new Set(
|
|
13077
|
+
const widgets = widgetDefinitions$1(list, parameterLookup(all));
|
|
13078
|
+
const blocks = new Set(widgets.filter((def) => def.block && def.closeStart !== void 0).map((def) => def.name.toLowerCase()));
|
|
13079
|
+
const parametersOf = parameterLookup([...all, ...definedWidgetMacros(widgets)]);
|
|
12596
13080
|
for (const macro of all) {
|
|
12597
13081
|
const name = macro.name.toLowerCase();
|
|
12598
13082
|
known.add(name);
|
|
@@ -12608,6 +13092,13 @@ function validateStoryMarkup(passages, macros, options = {}) {
|
|
|
12608
13092
|
tolerant: options.tolerant
|
|
12609
13093
|
});
|
|
12610
13094
|
}
|
|
13095
|
+
/** The widgets as macros that declare what their parameters hold. */
|
|
13096
|
+
function definedWidgetMacros(widgets) {
|
|
13097
|
+
return widgets.map((widget) => ({
|
|
13098
|
+
name: widget.name,
|
|
13099
|
+
widget
|
|
13100
|
+
}));
|
|
13101
|
+
}
|
|
12611
13102
|
/** Whether a macro takes a body, among `macros` and the built-in ones. */
|
|
12612
13103
|
function blockLookup(macros) {
|
|
12613
13104
|
const blocks = new Set(macros.filter((m) => m.block).map((m) => m.name.toLowerCase()));
|
|
@@ -12651,13 +13142,18 @@ function widgetDefinitions(passages, macros = []) {
|
|
|
12651
13142
|
}
|
|
12652
13143
|
/** Passages tagged so hold code or styles, not markup. */
|
|
12653
13144
|
var NOT_MARKUP_TAGS = ["script", "stylesheet"];
|
|
12654
|
-
/**
|
|
12655
|
-
|
|
13145
|
+
/**
|
|
13146
|
+
* What reading the variable references of markup depends on: the `macros`,
|
|
13147
|
+
* and the widgets of the `passages` that are given (the calls of those that
|
|
13148
|
+
* declare what their parameters hold are read by them).
|
|
13149
|
+
*/
|
|
13150
|
+
function referenceOptions(macros, all = false, passages = []) {
|
|
12656
13151
|
const list = [...macros];
|
|
13152
|
+
const widgets = widgetDefinitions$1(passages, parameterLookup(list));
|
|
12657
13153
|
return {
|
|
12658
13154
|
all,
|
|
12659
13155
|
storeVarMacros: new Set(list.filter((m) => m.storeVar).map((m) => m.name.toLowerCase())),
|
|
12660
|
-
parametersOf: parameterLookup(list),
|
|
13156
|
+
parametersOf: parameterLookup([...list, ...definedWidgetMacros(widgets)]),
|
|
12661
13157
|
tolerant: true
|
|
12662
13158
|
};
|
|
12663
13159
|
}
|
|
@@ -12688,7 +13184,8 @@ function variableReferences(source, macros = [], options = {}) {
|
|
|
12688
13184
|
* tolerant of half-typed markup, and it evaluates no project code.
|
|
12689
13185
|
*/
|
|
12690
13186
|
function validateVariableReferences(passages, declarations, macros = []) {
|
|
12691
|
-
|
|
13187
|
+
const list = [...passages];
|
|
13188
|
+
return checkVariableReferences(list.filter((p) => !p.tags?.some((t) => NOT_MARKUP_TAGS.includes(t))), declarations, referenceOptions(macros, false, list));
|
|
12692
13189
|
}
|
|
12693
13190
|
//#endregion
|
|
12694
13191
|
export { MarkupError, SIGIL_SCOPES, checkParameterTypes, collectStoryPassageReferences, discoverMacros, endsWithOperator, evaluatePassageName, findCodeEnd, formatDiagnostic, isBlockMacro, isSigil, lexJs, lexTemplate, pairMarkup, parseDeclarations, parseSelectors, parseStoryVariables, parseWidgetDef, passagePieces, passageTarget, pieceOffset, readQuoted, scanStringLiteral, splitArgs, splitIncludeFlag, splitTopLevel, stripLooseQuotes, tokenizeMarkup, tokenizeMarkupTolerant, transform, unescapeQuoted, validateStoryMarkup, validateVariableReferences, variableReferences, widgetDefinitions };
|