@rohal12/spindle 0.64.31 → 0.64.32
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 +435 -240
- package/dist/pkg/story-variables.js +278 -226
- package/package.json +1 -1
- package/src/code-check.ts +66 -13
- package/src/components/macros/Checkbox.tsx +10 -6
- package/src/components/macros/Listbox.tsx +11 -7
- package/src/components/macros/Radiobutton.tsx +25 -6
- package/src/components/macros/input-macro.ts +190 -9
- package/src/components/macros/macro-args.ts +29 -10
- package/src/define-macro.ts +2 -0
- package/src/format/format-passage.ts +29 -17
- package/src/markup/code-end.ts +23 -5
- package/src/markup/render.tsx +1 -1
- package/src/observe/observable-text.ts +2 -1
- package/src/story-api.ts +6 -1
- package/src/utils/variable-writes.ts +24 -0
- package/types/tooling.d.ts +9 -2
|
@@ -10377,6 +10377,201 @@ function completionIdentifier(source, cursor, goal = "expression") {
|
|
|
10377
10377
|
previous = token;
|
|
10378
10378
|
}
|
|
10379
10379
|
}
|
|
10380
|
+
// src/components/macros/arg-utils.ts
|
|
10381
|
+
/**
|
|
10382
|
+
* Shared helpers for macros that split or read their raw argument text by
|
|
10383
|
+
* hand. Their lexical model is the expression lexer's (`js-lexer.ts`):
|
|
10384
|
+
* string, template and regex literals and comments are opaque (a backslash
|
|
10385
|
+
* inside a literal escapes the character after it, and `${…}` parts of a
|
|
10386
|
+
* template are code), `/` opens a regex only where an operand is expected,
|
|
10387
|
+
* and `(`/`[`/`{` nest.
|
|
10388
|
+
*/
|
|
10389
|
+
/** True for a whitespace character. */
|
|
10390
|
+
function isWhitespace(ch) {
|
|
10391
|
+
return /\s/.test(ch);
|
|
10392
|
+
}
|
|
10393
|
+
function isOpener(ch) {
|
|
10394
|
+
return ch === "(" || ch === "[" || ch === "{";
|
|
10395
|
+
}
|
|
10396
|
+
function isCloser(ch) {
|
|
10397
|
+
return ch === ")" || ch === "]" || ch === "}";
|
|
10398
|
+
}
|
|
10399
|
+
/**
|
|
10400
|
+
* Undo the escapes allowed in a literal quoted macro argument: `\"`, `\'`
|
|
10401
|
+
* and `\\` become `"`, `'` and `\`. Other backslash sequences (`\n`, `\d`)
|
|
10402
|
+
* are kept as written, so code and regex text inside the string survive.
|
|
10403
|
+
*/
|
|
10404
|
+
function unescapeQuoted(body) {
|
|
10405
|
+
return body.replace(/\\(["'\\])/g, "$1");
|
|
10406
|
+
}
|
|
10407
|
+
/**
|
|
10408
|
+
* Read the `"…"` or `'…'` string whose opening quote is at `start`.
|
|
10409
|
+
* Returns its unescaped value and the index just past the closing quote, or
|
|
10410
|
+
* `null` if there is no such quote at `start` or the string is unterminated.
|
|
10411
|
+
* With `tolerant`, an unterminated string instead extends to its scanned end.
|
|
10412
|
+
*/
|
|
10413
|
+
function readQuoted(src, start, tolerant = false) {
|
|
10414
|
+
const quote = src[start];
|
|
10415
|
+
if (quote !== "\"" && quote !== "'") return null;
|
|
10416
|
+
const { end, closed } = scanStringLiteral(src, start);
|
|
10417
|
+
if (!closed && !tolerant) return null;
|
|
10418
|
+
return {
|
|
10419
|
+
value: unescapeQuoted(src.slice(start + 1, closed ? end - 1 : end)),
|
|
10420
|
+
end
|
|
10421
|
+
};
|
|
10422
|
+
}
|
|
10423
|
+
/**
|
|
10424
|
+
* The unescaped value of `src` when it is exactly one `"…"` or `'…'` string
|
|
10425
|
+
* (as with `readQuoted`), otherwise `null`.
|
|
10426
|
+
*/
|
|
10427
|
+
function readWholeQuoted(src) {
|
|
10428
|
+
const quoted = readQuoted(src, 0);
|
|
10429
|
+
return quoted && quoted.end === src.length ? quoted.value : null;
|
|
10430
|
+
}
|
|
10431
|
+
/** A dialog body's passage name, optionally enclosed in matching quotes. */
|
|
10432
|
+
function readPassageBody(src) {
|
|
10433
|
+
const name = src.trim();
|
|
10434
|
+
return readWholeQuoted(name) ?? name;
|
|
10435
|
+
}
|
|
10436
|
+
/**
|
|
10437
|
+
* Strip an optional quote from each end of an unquoted-or-loosely-quoted
|
|
10438
|
+
* argument (`"Red`, `Red'`), leaving at least one character. This is the
|
|
10439
|
+
* lenient fallback for labels that are not one well-formed quoted string.
|
|
10440
|
+
*/
|
|
10441
|
+
function stripLooseQuotes(src) {
|
|
10442
|
+
return /^["']?(.+?)["']?$/s.exec(src)?.[1] ?? src;
|
|
10443
|
+
}
|
|
10444
|
+
/** An operator character, or an operator keyword, at the end of code. */
|
|
10445
|
+
var TRAILING_OPERATOR_RE = /(?:[-+*/%&|^!~=<>?:,.]|(?:^|[^\w$@.])(?:typeof|void|new|delete|in|instanceof|await|yield))$/;
|
|
10446
|
+
/**
|
|
10447
|
+
* Whether `src` ends with an operator that still needs an operand (`$a +`,
|
|
10448
|
+
* `$a+`, `$x ==`, `typeof`), so it cannot be a complete value on its own.
|
|
10449
|
+
* Only code counts: the closing `/` of a regex literal is not division, a
|
|
10450
|
+
* variable reference (`$a`, `%name`, ...) is an operand, trailing comments are skipped,
|
|
10451
|
+
* and a postfix `++` or `--` completes its operand.
|
|
10452
|
+
*/
|
|
10453
|
+
function endsWithOperator(src) {
|
|
10454
|
+
let end = -1;
|
|
10455
|
+
lexJs(src, {
|
|
10456
|
+
code(ch, i, nesting) {
|
|
10457
|
+
if (nesting === 0 && !isWhitespace(ch)) end = i + 1;
|
|
10458
|
+
},
|
|
10459
|
+
literal(text, _i, nesting) {
|
|
10460
|
+
const comment = text.startsWith("//") || text.startsWith("/*");
|
|
10461
|
+
if (nesting === 0 && !comment) end = -1;
|
|
10462
|
+
},
|
|
10463
|
+
variable(_sigil, _name, _i, nesting) {
|
|
10464
|
+
if (nesting === 0) end = -1;
|
|
10465
|
+
}
|
|
10466
|
+
});
|
|
10467
|
+
if (end < 0) return false;
|
|
10468
|
+
const code = src.slice(0, end);
|
|
10469
|
+
if (/(?:\+\+|--)$/.test(code)) return false;
|
|
10470
|
+
return TRAILING_OPERATOR_RE.test(code);
|
|
10471
|
+
}
|
|
10472
|
+
/**
|
|
10473
|
+
* Indices of the characters in `src` that satisfy `isSeparator` and sit at
|
|
10474
|
+
* depth 0: in code outside string, template and regex literals and comments,
|
|
10475
|
+
* and outside `()`, `[]` and `{}` pairs.
|
|
10476
|
+
*/
|
|
10477
|
+
function topLevelIndices(src, isSeparator) {
|
|
10478
|
+
const indices = [];
|
|
10479
|
+
let depth = 0;
|
|
10480
|
+
lexJs(src, { code(ch, i, nesting) {
|
|
10481
|
+
if (nesting > 0) return;
|
|
10482
|
+
if (isOpener(ch)) depth++;
|
|
10483
|
+
else if (isCloser(ch)) depth--;
|
|
10484
|
+
else if (depth === 0 && isSeparator(ch)) indices.push(i);
|
|
10485
|
+
} });
|
|
10486
|
+
return indices;
|
|
10487
|
+
}
|
|
10488
|
+
/**
|
|
10489
|
+
* The segments of `src` between its depth-0 separators (see
|
|
10490
|
+
* `topLevelIndices`), as spans: untrimmed, and empty segments are kept, so
|
|
10491
|
+
* `n` separators always give `n + 1` segments.
|
|
10492
|
+
*/
|
|
10493
|
+
function topLevelSpans(src, isSeparator) {
|
|
10494
|
+
const spans = [];
|
|
10495
|
+
let from = 0;
|
|
10496
|
+
for (const i of topLevelIndices(src, isSeparator)) {
|
|
10497
|
+
spans.push([from, i]);
|
|
10498
|
+
from = i + 1;
|
|
10499
|
+
}
|
|
10500
|
+
spans.push([from, src.length]);
|
|
10501
|
+
return spans;
|
|
10502
|
+
}
|
|
10503
|
+
/**
|
|
10504
|
+
* Split `src` at every depth-0 separator (see `topLevelIndices`). Segments
|
|
10505
|
+
* are returned as written, untrimmed, and empty segments are kept, so
|
|
10506
|
+
* `n` separators always give `n + 1` segments.
|
|
10507
|
+
*/
|
|
10508
|
+
function splitTopLevel(src, isSeparator) {
|
|
10509
|
+
return topLevelSpans(src, isSeparator).map(([a, b]) => src.slice(a, b));
|
|
10510
|
+
}
|
|
10511
|
+
/**
|
|
10512
|
+
* Check whether a whitespace-delimited token looks like a standalone value
|
|
10513
|
+
* (not an operator or partial expression): it starts like a value and does
|
|
10514
|
+
* not end with an operator still waiting for its operand.
|
|
10515
|
+
*/
|
|
10516
|
+
function isStandaloneValue(token) {
|
|
10517
|
+
return startsLikeValue(token) && !endsWithOperator(token);
|
|
10518
|
+
}
|
|
10519
|
+
function startsLikeValue(token) {
|
|
10520
|
+
const first = token[0];
|
|
10521
|
+
if (first === "\"" || first === "'" || first === "`") return true;
|
|
10522
|
+
if (/^[$_@]\w|^%[A-Za-z_]/.test(token)) return true;
|
|
10523
|
+
if (/^[+-]?(?:\d|\.\d)/.test(token)) return true;
|
|
10524
|
+
if (first === "(" || first === "[" || first === "{") return true;
|
|
10525
|
+
if (/^(?:true|false|null|undefined)(?![\w$])/.test(token)) return true;
|
|
10526
|
+
if (first === "!" && token.length > 1 && token[1] !== "=") return true;
|
|
10527
|
+
return false;
|
|
10528
|
+
}
|
|
10529
|
+
/**
|
|
10530
|
+
* Try to split a raw string on whitespace at depth 0 (respecting strings,
|
|
10531
|
+
* template literals, parentheses, brackets, and braces). Each resulting token
|
|
10532
|
+
* must pass `isStandaloneValue()` or the split is rejected and `null` is
|
|
10533
|
+
* returned. The spans are those of the tokens in `raw`.
|
|
10534
|
+
*/
|
|
10535
|
+
function trySplitOnWhitespace(raw) {
|
|
10536
|
+
const args = topLevelSpans(raw, isWhitespace).filter(([a, b]) => b > a);
|
|
10537
|
+
if (args.length < 2) return null;
|
|
10538
|
+
for (const [a, b] of args) if (!isStandaloneValue(raw.slice(a, b))) return null;
|
|
10539
|
+
return args;
|
|
10540
|
+
}
|
|
10541
|
+
/** `span` less the whitespace at its ends. */
|
|
10542
|
+
function trimmed$1(src, [start, end]) {
|
|
10543
|
+
const text = src.slice(start, end);
|
|
10544
|
+
const lead = text.length - text.trimStart().length;
|
|
10545
|
+
return [start + lead, Math.max(start + lead, end - (text.length - text.trimEnd().length))];
|
|
10546
|
+
}
|
|
10547
|
+
/**
|
|
10548
|
+
* Where the arguments in `raw` are written (see `splitArgs`), as spans of
|
|
10549
|
+
* `raw`.
|
|
10550
|
+
*/
|
|
10551
|
+
function splitArgSpans(raw) {
|
|
10552
|
+
const args = topLevelSpans(raw, (ch) => ch === ",").map((span) => trimmed$1(raw, span));
|
|
10553
|
+
const hasComma = args.length > 1;
|
|
10554
|
+
const last = args[args.length - 1];
|
|
10555
|
+
if (last[0] === last[1]) args.pop();
|
|
10556
|
+
if (!hasComma && args.length === 1) {
|
|
10557
|
+
const [start, end] = args[0];
|
|
10558
|
+
const split = trySplitOnWhitespace(raw.slice(start, end));
|
|
10559
|
+
if (split) return split.map(([a, b]) => [start + a, start + b]);
|
|
10560
|
+
}
|
|
10561
|
+
return args;
|
|
10562
|
+
}
|
|
10563
|
+
/**
|
|
10564
|
+
* Split rawArgs by commas, respecting parentheses, brackets, braces, and
|
|
10565
|
+
* strings. When no top-level commas are present, also supports adjacent quoted
|
|
10566
|
+
* string literals separated by whitespace (e.g. `"Label" "target"`).
|
|
10567
|
+
*/
|
|
10568
|
+
function splitArgs(raw) {
|
|
10569
|
+
return splitArgSpans(raw).map(([a, b]) => raw.slice(a, b));
|
|
10570
|
+
}
|
|
10571
|
+
/** `text` as a regular expression source that matches it literally. */
|
|
10572
|
+
function escapeRegExp(text) {
|
|
10573
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
10574
|
+
}
|
|
10380
10575
|
// src/markup/code-end.ts
|
|
10381
10576
|
/**
|
|
10382
10577
|
* A quote directly after a letter/digit is an apostrophe (don't), not a
|
|
@@ -10481,6 +10676,8 @@ function scanLenient(input, start) {
|
|
|
10481
10676
|
* input (see `OnOpenCodeEnd`); the ends it finds are the same.
|
|
10482
10677
|
*/
|
|
10483
10678
|
function jsCodeEnd(onOpenEnd) {
|
|
10679
|
+
let indexedInput = "";
|
|
10680
|
+
const rawClosers = /* @__PURE__ */ new Map();
|
|
10484
10681
|
return {
|
|
10485
10682
|
closeBrace(input, codeStart, lenientStart) {
|
|
10486
10683
|
let ranOut = false;
|
|
@@ -10491,10 +10688,18 @@ function jsCodeEnd(onOpenEnd) {
|
|
|
10491
10688
|
return end;
|
|
10492
10689
|
},
|
|
10493
10690
|
rawBodyEnd(input, bodyStart, name) {
|
|
10494
|
-
|
|
10495
|
-
|
|
10496
|
-
|
|
10497
|
-
|
|
10691
|
+
if (indexedInput !== input) {
|
|
10692
|
+
indexedInput = input;
|
|
10693
|
+
rawClosers.clear();
|
|
10694
|
+
}
|
|
10695
|
+
name = name.toLowerCase();
|
|
10696
|
+
const closer = `\\{/${escapeRegExp(name)}\\s*\\}`;
|
|
10697
|
+
let candidates = rawClosers.get(name);
|
|
10698
|
+
if (!candidates) {
|
|
10699
|
+
candidates = Array.from(input.matchAll(new RegExp(closer, "gi")), (m) => m.index);
|
|
10700
|
+
rawClosers.set(name, candidates);
|
|
10701
|
+
}
|
|
10702
|
+
const firstAt = candidates[upperBound(candidates, bodyStart - 1)] ?? -1;
|
|
10498
10703
|
if (firstAt === -1) return -1;
|
|
10499
10704
|
const at = new RegExp(closer, "iy");
|
|
10500
10705
|
let ranOut = false;
|
|
@@ -10507,7 +10712,7 @@ function jsCodeEnd(onOpenEnd) {
|
|
|
10507
10712
|
onRunOut: () => ranOut = true
|
|
10508
10713
|
});
|
|
10509
10714
|
if (end !== -1) return end;
|
|
10510
|
-
if (onOpenEnd && (ranOut || new RegExp(`\\{/${name}\\s*$`, "i").test(input))) onOpenEnd(input, bodyStart);
|
|
10715
|
+
if (onOpenEnd && (ranOut || new RegExp(`\\{/${escapeRegExp(name)}\\s*$`, "i").test(input))) onOpenEnd(input, bodyStart);
|
|
10511
10716
|
return firstAt;
|
|
10512
10717
|
}
|
|
10513
10718
|
};
|
|
@@ -10947,201 +11152,6 @@ function ownEntries(sources, keep = () => true) {
|
|
|
10947
11152
|
/** A namespace with no prototype holding the own entries of `sources`. */
|
|
10948
11153
|
var createNamespace = (...sources) => ownEntries(sources);
|
|
10949
11154
|
Object.freeze(createNamespace());
|
|
10950
|
-
// src/components/macros/arg-utils.ts
|
|
10951
|
-
/**
|
|
10952
|
-
* Shared helpers for macros that split or read their raw argument text by
|
|
10953
|
-
* hand. Their lexical model is the expression lexer's (`js-lexer.ts`):
|
|
10954
|
-
* string, template and regex literals and comments are opaque (a backslash
|
|
10955
|
-
* inside a literal escapes the character after it, and `${…}` parts of a
|
|
10956
|
-
* template are code), `/` opens a regex only where an operand is expected,
|
|
10957
|
-
* and `(`/`[`/`{` nest.
|
|
10958
|
-
*/
|
|
10959
|
-
/** True for a whitespace character. */
|
|
10960
|
-
function isWhitespace(ch) {
|
|
10961
|
-
return /\s/.test(ch);
|
|
10962
|
-
}
|
|
10963
|
-
function isOpener(ch) {
|
|
10964
|
-
return ch === "(" || ch === "[" || ch === "{";
|
|
10965
|
-
}
|
|
10966
|
-
function isCloser(ch) {
|
|
10967
|
-
return ch === ")" || ch === "]" || ch === "}";
|
|
10968
|
-
}
|
|
10969
|
-
/**
|
|
10970
|
-
* Undo the escapes allowed in a literal quoted macro argument: `\"`, `\'`
|
|
10971
|
-
* and `\\` become `"`, `'` and `\`. Other backslash sequences (`\n`, `\d`)
|
|
10972
|
-
* are kept as written, so code and regex text inside the string survive.
|
|
10973
|
-
*/
|
|
10974
|
-
function unescapeQuoted(body) {
|
|
10975
|
-
return body.replace(/\\(["'\\])/g, "$1");
|
|
10976
|
-
}
|
|
10977
|
-
/**
|
|
10978
|
-
* Read the `"…"` or `'…'` string whose opening quote is at `start`.
|
|
10979
|
-
* Returns its unescaped value and the index just past the closing quote, or
|
|
10980
|
-
* `null` if there is no such quote at `start` or the string is unterminated.
|
|
10981
|
-
* With `tolerant`, an unterminated string instead extends to its scanned end.
|
|
10982
|
-
*/
|
|
10983
|
-
function readQuoted(src, start, tolerant = false) {
|
|
10984
|
-
const quote = src[start];
|
|
10985
|
-
if (quote !== "\"" && quote !== "'") return null;
|
|
10986
|
-
const { end, closed } = scanStringLiteral(src, start);
|
|
10987
|
-
if (!closed && !tolerant) return null;
|
|
10988
|
-
return {
|
|
10989
|
-
value: unescapeQuoted(src.slice(start + 1, closed ? end - 1 : end)),
|
|
10990
|
-
end
|
|
10991
|
-
};
|
|
10992
|
-
}
|
|
10993
|
-
/**
|
|
10994
|
-
* The unescaped value of `src` when it is exactly one `"…"` or `'…'` string
|
|
10995
|
-
* (as with `readQuoted`), otherwise `null`.
|
|
10996
|
-
*/
|
|
10997
|
-
function readWholeQuoted(src) {
|
|
10998
|
-
const quoted = readQuoted(src, 0);
|
|
10999
|
-
return quoted && quoted.end === src.length ? quoted.value : null;
|
|
11000
|
-
}
|
|
11001
|
-
/** A dialog body's passage name, optionally enclosed in matching quotes. */
|
|
11002
|
-
function readPassageBody(src) {
|
|
11003
|
-
const name = src.trim();
|
|
11004
|
-
return readWholeQuoted(name) ?? name;
|
|
11005
|
-
}
|
|
11006
|
-
/**
|
|
11007
|
-
* Strip an optional quote from each end of an unquoted-or-loosely-quoted
|
|
11008
|
-
* argument (`"Red`, `Red'`), leaving at least one character. This is the
|
|
11009
|
-
* lenient fallback for labels that are not one well-formed quoted string.
|
|
11010
|
-
*/
|
|
11011
|
-
function stripLooseQuotes(src) {
|
|
11012
|
-
return /^["']?(.+?)["']?$/s.exec(src)?.[1] ?? src;
|
|
11013
|
-
}
|
|
11014
|
-
/** An operator character, or an operator keyword, at the end of code. */
|
|
11015
|
-
var TRAILING_OPERATOR_RE = /(?:[-+*/%&|^!~=<>?:,.]|(?:^|[^\w$@.])(?:typeof|void|new|delete|in|instanceof|await|yield))$/;
|
|
11016
|
-
/**
|
|
11017
|
-
* Whether `src` ends with an operator that still needs an operand (`$a +`,
|
|
11018
|
-
* `$a+`, `$x ==`, `typeof`), so it cannot be a complete value on its own.
|
|
11019
|
-
* Only code counts: the closing `/` of a regex literal is not division, a
|
|
11020
|
-
* variable reference (`$a`, `%name`, ...) is an operand, trailing comments are skipped,
|
|
11021
|
-
* and a postfix `++` or `--` completes its operand.
|
|
11022
|
-
*/
|
|
11023
|
-
function endsWithOperator(src) {
|
|
11024
|
-
let end = -1;
|
|
11025
|
-
lexJs(src, {
|
|
11026
|
-
code(ch, i, nesting) {
|
|
11027
|
-
if (nesting === 0 && !isWhitespace(ch)) end = i + 1;
|
|
11028
|
-
},
|
|
11029
|
-
literal(text, _i, nesting) {
|
|
11030
|
-
const comment = text.startsWith("//") || text.startsWith("/*");
|
|
11031
|
-
if (nesting === 0 && !comment) end = -1;
|
|
11032
|
-
},
|
|
11033
|
-
variable(_sigil, _name, _i, nesting) {
|
|
11034
|
-
if (nesting === 0) end = -1;
|
|
11035
|
-
}
|
|
11036
|
-
});
|
|
11037
|
-
if (end < 0) return false;
|
|
11038
|
-
const code = src.slice(0, end);
|
|
11039
|
-
if (/(?:\+\+|--)$/.test(code)) return false;
|
|
11040
|
-
return TRAILING_OPERATOR_RE.test(code);
|
|
11041
|
-
}
|
|
11042
|
-
/**
|
|
11043
|
-
* Indices of the characters in `src` that satisfy `isSeparator` and sit at
|
|
11044
|
-
* depth 0: in code outside string, template and regex literals and comments,
|
|
11045
|
-
* and outside `()`, `[]` and `{}` pairs.
|
|
11046
|
-
*/
|
|
11047
|
-
function topLevelIndices(src, isSeparator) {
|
|
11048
|
-
const indices = [];
|
|
11049
|
-
let depth = 0;
|
|
11050
|
-
lexJs(src, { code(ch, i, nesting) {
|
|
11051
|
-
if (nesting > 0) return;
|
|
11052
|
-
if (isOpener(ch)) depth++;
|
|
11053
|
-
else if (isCloser(ch)) depth--;
|
|
11054
|
-
else if (depth === 0 && isSeparator(ch)) indices.push(i);
|
|
11055
|
-
} });
|
|
11056
|
-
return indices;
|
|
11057
|
-
}
|
|
11058
|
-
/**
|
|
11059
|
-
* The segments of `src` between its depth-0 separators (see
|
|
11060
|
-
* `topLevelIndices`), as spans: untrimmed, and empty segments are kept, so
|
|
11061
|
-
* `n` separators always give `n + 1` segments.
|
|
11062
|
-
*/
|
|
11063
|
-
function topLevelSpans(src, isSeparator) {
|
|
11064
|
-
const spans = [];
|
|
11065
|
-
let from = 0;
|
|
11066
|
-
for (const i of topLevelIndices(src, isSeparator)) {
|
|
11067
|
-
spans.push([from, i]);
|
|
11068
|
-
from = i + 1;
|
|
11069
|
-
}
|
|
11070
|
-
spans.push([from, src.length]);
|
|
11071
|
-
return spans;
|
|
11072
|
-
}
|
|
11073
|
-
/**
|
|
11074
|
-
* Split `src` at every depth-0 separator (see `topLevelIndices`). Segments
|
|
11075
|
-
* are returned as written, untrimmed, and empty segments are kept, so
|
|
11076
|
-
* `n` separators always give `n + 1` segments.
|
|
11077
|
-
*/
|
|
11078
|
-
function splitTopLevel(src, isSeparator) {
|
|
11079
|
-
return topLevelSpans(src, isSeparator).map(([a, b]) => src.slice(a, b));
|
|
11080
|
-
}
|
|
11081
|
-
/**
|
|
11082
|
-
* Check whether a whitespace-delimited token looks like a standalone value
|
|
11083
|
-
* (not an operator or partial expression): it starts like a value and does
|
|
11084
|
-
* not end with an operator still waiting for its operand.
|
|
11085
|
-
*/
|
|
11086
|
-
function isStandaloneValue(token) {
|
|
11087
|
-
return startsLikeValue(token) && !endsWithOperator(token);
|
|
11088
|
-
}
|
|
11089
|
-
function startsLikeValue(token) {
|
|
11090
|
-
const first = token[0];
|
|
11091
|
-
if (first === "\"" || first === "'" || first === "`") return true;
|
|
11092
|
-
if (/^[$_@]\w|^%[A-Za-z_]/.test(token)) return true;
|
|
11093
|
-
if (/^[+-]?(?:\d|\.\d)/.test(token)) return true;
|
|
11094
|
-
if (first === "(" || first === "[" || first === "{") return true;
|
|
11095
|
-
if (/^(?:true|false|null|undefined)(?![\w$])/.test(token)) return true;
|
|
11096
|
-
if (first === "!" && token.length > 1 && token[1] !== "=") return true;
|
|
11097
|
-
return false;
|
|
11098
|
-
}
|
|
11099
|
-
/**
|
|
11100
|
-
* Try to split a raw string on whitespace at depth 0 (respecting strings,
|
|
11101
|
-
* template literals, parentheses, brackets, and braces). Each resulting token
|
|
11102
|
-
* must pass `isStandaloneValue()` or the split is rejected and `null` is
|
|
11103
|
-
* returned. The spans are those of the tokens in `raw`.
|
|
11104
|
-
*/
|
|
11105
|
-
function trySplitOnWhitespace(raw) {
|
|
11106
|
-
const args = topLevelSpans(raw, isWhitespace).filter(([a, b]) => b > a);
|
|
11107
|
-
if (args.length < 2) return null;
|
|
11108
|
-
for (const [a, b] of args) if (!isStandaloneValue(raw.slice(a, b))) return null;
|
|
11109
|
-
return args;
|
|
11110
|
-
}
|
|
11111
|
-
/** `span` less the whitespace at its ends. */
|
|
11112
|
-
function trimmed$1(src, [start, end]) {
|
|
11113
|
-
const text = src.slice(start, end);
|
|
11114
|
-
const lead = text.length - text.trimStart().length;
|
|
11115
|
-
return [start + lead, Math.max(start + lead, end - (text.length - text.trimEnd().length))];
|
|
11116
|
-
}
|
|
11117
|
-
/**
|
|
11118
|
-
* Where the arguments in `raw` are written (see `splitArgs`), as spans of
|
|
11119
|
-
* `raw`.
|
|
11120
|
-
*/
|
|
11121
|
-
function splitArgSpans(raw) {
|
|
11122
|
-
const args = topLevelSpans(raw, (ch) => ch === ",").map((span) => trimmed$1(raw, span));
|
|
11123
|
-
const hasComma = args.length > 1;
|
|
11124
|
-
const last = args[args.length - 1];
|
|
11125
|
-
if (last[0] === last[1]) args.pop();
|
|
11126
|
-
if (!hasComma && args.length === 1) {
|
|
11127
|
-
const [start, end] = args[0];
|
|
11128
|
-
const split = trySplitOnWhitespace(raw.slice(start, end));
|
|
11129
|
-
if (split) return split.map(([a, b]) => [start + a, start + b]);
|
|
11130
|
-
}
|
|
11131
|
-
return args;
|
|
11132
|
-
}
|
|
11133
|
-
/**
|
|
11134
|
-
* Split rawArgs by commas, respecting parentheses, brackets, braces, and
|
|
11135
|
-
* strings. When no top-level commas are present, also supports adjacent quoted
|
|
11136
|
-
* string literals separated by whitespace (e.g. `"Label" "target"`).
|
|
11137
|
-
*/
|
|
11138
|
-
function splitArgs(raw) {
|
|
11139
|
-
return splitArgSpans(raw).map(([a, b]) => raw.slice(a, b));
|
|
11140
|
-
}
|
|
11141
|
-
/** `text` as a regular expression source that matches it literally. */
|
|
11142
|
-
function escapeRegExp(text) {
|
|
11143
|
-
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
11144
|
-
}
|
|
11145
11155
|
// src/components/macros/macro-args.ts
|
|
11146
11156
|
/**
|
|
11147
11157
|
* The macro argument layer: reads a macro's raw argument text into the
|
|
@@ -11321,14 +11331,28 @@ function trimmed(src, at) {
|
|
|
11321
11331
|
function readGroup(params, src, args, { at: srcAt, spans, toolingSpans }) {
|
|
11322
11332
|
let [text, at] = trimmed(src ?? "", srcAt);
|
|
11323
11333
|
const alone = !params.some((p) => p.type !== "flag" && p.required);
|
|
11324
|
-
|
|
11325
|
-
|
|
11326
|
-
|
|
11327
|
-
|
|
11328
|
-
|
|
11329
|
-
|
|
11330
|
-
|
|
11334
|
+
const flags = params.filter((p) => p.type === "flag");
|
|
11335
|
+
for (const flag of flags) args[flag.name] = false;
|
|
11336
|
+
const byName = new Map(flags.map((flag) => [flag.name, flag]));
|
|
11337
|
+
while (text && byName.size) {
|
|
11338
|
+
const parts = terms(text);
|
|
11339
|
+
if (parts.length === 0) break;
|
|
11340
|
+
let changed = false;
|
|
11341
|
+
for (const name of [slice(text, parts[parts.length - 1]), slice(text, parts[0])]) {
|
|
11342
|
+
const flag = byName.get(name);
|
|
11343
|
+
if (!flag || args[flag.name]) continue;
|
|
11344
|
+
const rest = src === null ? null : takeFlag(text, flag.name, alone);
|
|
11345
|
+
if (rest) {
|
|
11346
|
+
args[flag.name] = true;
|
|
11347
|
+
byName.delete(flag.name);
|
|
11348
|
+
changed = true;
|
|
11349
|
+
const flagSpan = rest[0] > 0 ? [0, text.indexOf(flag.name) + flag.name.length] : [text.lastIndexOf(flag.name), text.length];
|
|
11350
|
+
toolingSpans?.set(flag, [at + flagSpan[0], at + flagSpan[1]]);
|
|
11351
|
+
[text, at] = trimmed(slice(text, rest), at + rest[0]);
|
|
11352
|
+
break;
|
|
11353
|
+
}
|
|
11331
11354
|
}
|
|
11355
|
+
if (!changed) break;
|
|
11332
11356
|
}
|
|
11333
11357
|
const positional = params.filter((p) => p.type !== "flag");
|
|
11334
11358
|
if (positional.length === 0) return;
|
|
@@ -11583,13 +11607,13 @@ function parseOrError(code, goal) {
|
|
|
11583
11607
|
parses?.set(key, result);
|
|
11584
11608
|
return result;
|
|
11585
11609
|
}
|
|
11586
|
-
/** Where
|
|
11610
|
+
/** Where boundary `index` of a piece's code, text or name is in the source. */
|
|
11587
11611
|
function pieceOffset(piece, index) {
|
|
11588
11612
|
return piece.sourceOffsets?.[index] ?? piece.offset + index;
|
|
11589
11613
|
}
|
|
11590
11614
|
/** How much of a piece's own text or code there is (not its source extent). */
|
|
11591
11615
|
function extentOf(piece) {
|
|
11592
|
-
return piece.kind === "code" ? piece.code.length : piece.kind === "text" ? piece.text.length : piece.length;
|
|
11616
|
+
return piece.kind === "code" ? piece.code.length : piece.kind === "text" ? piece.text.length : piece.kind === "passage" ? piece.name.length : piece.length;
|
|
11593
11617
|
}
|
|
11594
11618
|
/** Macros whose whole argument text is one expression: branch conditions. */
|
|
11595
11619
|
var CONDITION_MACROS = /* @__PURE__ */ new Set([
|
|
@@ -11786,13 +11810,17 @@ function* codeAndText(src, tokens, parametersOf) {
|
|
|
11786
11810
|
if (close > t + 1) {
|
|
11787
11811
|
const text = src.slice(token.end, tokens[close].start);
|
|
11788
11812
|
const written = text.trim();
|
|
11813
|
+
const at = token.end + text.indexOf(written);
|
|
11814
|
+
const nameValue = readPassageBody(written);
|
|
11815
|
+
const sourceOffsets = readWholeQuoted(written) !== null ? escapeOffsets(written.slice(1, -1), at + 1, nameValue, "quotes", true) : void 0;
|
|
11789
11816
|
yield {
|
|
11790
11817
|
kind: "passage",
|
|
11791
|
-
name:
|
|
11792
|
-
offset:
|
|
11818
|
+
name: nameValue,
|
|
11819
|
+
offset: at,
|
|
11793
11820
|
length: written.length,
|
|
11794
11821
|
macro: name,
|
|
11795
|
-
label: src.slice(token.start, token.end)
|
|
11822
|
+
label: src.slice(token.start, token.end),
|
|
11823
|
+
...sourceOffsets && { sourceOffsets }
|
|
11796
11824
|
};
|
|
11797
11825
|
}
|
|
11798
11826
|
}
|
|
@@ -11882,7 +11910,7 @@ function nestedPiece(piece, toRoot, where) {
|
|
|
11882
11910
|
nested: true,
|
|
11883
11911
|
where
|
|
11884
11912
|
};
|
|
11885
|
-
if (piece.kind === "
|
|
11913
|
+
if (piece.kind === "argument-error") {
|
|
11886
11914
|
const length = toRoot(piece.offset + piece.length) - offset;
|
|
11887
11915
|
return {
|
|
11888
11916
|
...piece,
|
|
@@ -11892,7 +11920,7 @@ function nestedPiece(piece, toRoot, where) {
|
|
|
11892
11920
|
}
|
|
11893
11921
|
const extent = extentOf(piece);
|
|
11894
11922
|
const sourceOffsets = piece.sourceOffsets ? piece.sourceOffsets.map(toRoot) : toRoot(piece.offset + extent) - offset !== extent ? Array.from({ length: extent + 1 }, (_, i) => toRoot(piece.offset + i)) : void 0;
|
|
11895
|
-
|
|
11923
|
+
const mapped = sourceOffsets ? {
|
|
11896
11924
|
...piece,
|
|
11897
11925
|
...nested,
|
|
11898
11926
|
sourceOffsets
|
|
@@ -11900,6 +11928,10 @@ function nestedPiece(piece, toRoot, where) {
|
|
|
11900
11928
|
...piece,
|
|
11901
11929
|
...nested
|
|
11902
11930
|
};
|
|
11931
|
+
return mapped.kind === "passage" ? {
|
|
11932
|
+
...mapped,
|
|
11933
|
+
length: toRoot(piece.offset + mapped.length) - offset
|
|
11934
|
+
} : mapped;
|
|
11903
11935
|
}
|
|
11904
11936
|
/** An escape sequence of a JavaScript string literal (with its backslash). */
|
|
11905
11937
|
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;
|
|
@@ -11928,9 +11960,10 @@ function escapeAt(raw, i, escapes) {
|
|
|
11928
11960
|
* written as `raw` from `at` on (its escapes are those of `escapes`), is in
|
|
11929
11961
|
* the source, and where the text ends. A character that an escape stands for
|
|
11930
11962
|
* is where the escape begins. None when the text is where its length says, or
|
|
11931
|
-
* is not that string.
|
|
11963
|
+
* is not that string. With `includeLinear`, return the map even without
|
|
11964
|
+
* escapes (a passage piece's offset includes the opening quote).
|
|
11932
11965
|
*/
|
|
11933
|
-
function escapeOffsets(raw, at, value, escapes) {
|
|
11966
|
+
function escapeOffsets(raw, at, value, escapes, includeLinear = false) {
|
|
11934
11967
|
const offsets = [];
|
|
11935
11968
|
let escaped = false;
|
|
11936
11969
|
let i = 0;
|
|
@@ -11946,8 +11979,14 @@ function escapeOffsets(raw, at, value, escapes) {
|
|
|
11946
11979
|
escaped = true;
|
|
11947
11980
|
i += escape.length;
|
|
11948
11981
|
}
|
|
11982
|
+
while (raw[i] === "\\") {
|
|
11983
|
+
const escape = escapeAt(raw, i, escapes);
|
|
11984
|
+
if (!escape || escape.text.length !== 0) break;
|
|
11985
|
+
escaped = true;
|
|
11986
|
+
i += escape.length;
|
|
11987
|
+
}
|
|
11949
11988
|
offsets.push(at + i);
|
|
11950
|
-
return escaped ? offsets : void 0;
|
|
11989
|
+
return escaped || includeLinear ? offsets : void 0;
|
|
11951
11990
|
}
|
|
11952
11991
|
/** The code and text in the arguments `args` (at `offset`) of `macro`. */
|
|
11953
11992
|
function* argPieces(args, offset, params, macro, label, navigation = []) {
|
|
@@ -12015,20 +12054,21 @@ function* valuePieces(args, offset, params, values, spans, macro, label, escapes
|
|
|
12015
12054
|
const [start, end] = span;
|
|
12016
12055
|
const written = args.slice(start, end);
|
|
12017
12056
|
/** The name as written (quotes included), at the argument. */
|
|
12018
|
-
const passage = (passageName) => ({
|
|
12057
|
+
const passage = (passageName, at, mode = escapes) => ({
|
|
12019
12058
|
kind: "passage",
|
|
12020
12059
|
name: passageName,
|
|
12021
12060
|
offset: offset + start,
|
|
12022
12061
|
length: end - start,
|
|
12023
12062
|
macro: name,
|
|
12024
12063
|
label,
|
|
12064
|
+
sourceOffsets: escapeOffsets(args.slice(at - offset), at, passageName, mode, true),
|
|
12025
12065
|
...navigation.includes(param.name) && { navigation: true }
|
|
12026
12066
|
});
|
|
12027
12067
|
const goal = codeGoal(param);
|
|
12028
12068
|
if (param.type === "passage") {
|
|
12029
12069
|
const target = passageTarget(value);
|
|
12030
12070
|
if (target.kind === "name") {
|
|
12031
|
-
yield passage(target.name);
|
|
12071
|
+
yield passage(target.name, offset + start + 1, "javascript");
|
|
12032
12072
|
continue;
|
|
12033
12073
|
}
|
|
12034
12074
|
}
|
|
@@ -12061,7 +12101,7 @@ function* valuePieces(args, offset, params, values, spans, macro, label, escapes
|
|
|
12061
12101
|
};
|
|
12062
12102
|
if (sourceOffsets) piece.sourceOffsets = sourceOffsets;
|
|
12063
12103
|
yield piece;
|
|
12064
|
-
} else if (holds === "passage") yield passage(value);
|
|
12104
|
+
} else if (holds === "passage") yield passage(value, at);
|
|
12065
12105
|
else if (holds === "markup") {
|
|
12066
12106
|
const piece = textPiece(value, at, `In the ${param.name} of {${macro}}: `);
|
|
12067
12107
|
if (sourceOffsets) piece.sourceOffsets = sourceOffsets;
|
|
@@ -25829,13 +25869,23 @@ function joinSpans(text, spans, sentinel) {
|
|
|
25829
25869
|
}
|
|
25830
25870
|
return out + text.slice(last);
|
|
25831
25871
|
}
|
|
25872
|
+
/** Collapse the trailing blank lines without probing every whitespace suffix. */
|
|
25873
|
+
function trimBlankLines(text) {
|
|
25874
|
+
let end = text.length;
|
|
25875
|
+
let newline = false;
|
|
25876
|
+
while (end > 0 && /[ \t\n]/.test(text[end - 1])) {
|
|
25877
|
+
newline || (newline = text[end - 1] === "\n");
|
|
25878
|
+
end--;
|
|
25879
|
+
}
|
|
25880
|
+
return newline ? text.slice(0, end) + "\n" : text;
|
|
25881
|
+
}
|
|
25832
25882
|
async function formatLf(text, options) {
|
|
25833
25883
|
const { prettier, prettierOptions } = options;
|
|
25834
25884
|
let body = text;
|
|
25835
25885
|
if (options.kind === "script" || options.kind === "stylesheet") {
|
|
25836
25886
|
const format = options.kind === "script" ? formatJS : formatCSS;
|
|
25837
25887
|
const code = body.trim();
|
|
25838
|
-
return (await format(code, prettier, prettierOptions) ?? code).trim()
|
|
25888
|
+
return (await format(code, prettier, prettierOptions) ?? code).trim() + "\n";
|
|
25839
25889
|
}
|
|
25840
25890
|
const isBlock = options.isBlock ?? defaultIsBlock(body, options.blockMacros);
|
|
25841
25891
|
const isDedenting = options.isDedentingSubMacro ?? ((name) => DEFAULT_DEDENTING.has(name.toLowerCase()));
|
|
@@ -25845,13 +25895,13 @@ async function formatLf(text, options) {
|
|
|
25845
25895
|
const literalMarkers = /* @__PURE__ */ new Set();
|
|
25846
25896
|
const fenced = markdownLines(body, isBlock, /* @__PURE__ */ new Set(["codeFenced"]));
|
|
25847
25897
|
const literalHtml = whitespaceSensitiveLines(body, isBlock);
|
|
25848
|
-
body = body.split("\n").map((line, index) => {
|
|
25898
|
+
body = trimBlankLines(body.split("\n").map((line, index) => {
|
|
25849
25899
|
if (!fenced.has(index) && !literalHtml.has(index)) return line;
|
|
25850
25900
|
const marker = `${literalSentinel}L${literalLines.size}${literalSentinel}`;
|
|
25851
25901
|
literalLines.set(marker, line);
|
|
25852
25902
|
literalMarkers.add(marker);
|
|
25853
25903
|
return (line.match(/^[ \t]*/)?.[0] ?? "") + marker;
|
|
25854
|
-
}).join("\n")
|
|
25904
|
+
}).join("\n"));
|
|
25855
25905
|
const lines = [];
|
|
25856
25906
|
/** Indices in lines whose trailing two spaces are a hard line break. */
|
|
25857
25907
|
const hardBreakLines = /* @__PURE__ */ new Set();
|
|
@@ -25908,8 +25958,10 @@ async function formatLf(text, options) {
|
|
|
25908
25958
|
lines.push(line);
|
|
25909
25959
|
}
|
|
25910
25960
|
}
|
|
25911
|
-
let output = lines.map((l, i) => hardBreakLines.has(i) || preservedLines.has(i) ? l : l.
|
|
25912
|
-
|
|
25961
|
+
let output = lines.map((l, i) => hardBreakLines.has(i) || preservedLines.has(i) ? l : l.trimEnd()).join("\n");
|
|
25962
|
+
let end = output.length;
|
|
25963
|
+
while (end > 0 && output[end - 1] === "\n") end--;
|
|
25964
|
+
output = output.slice(0, end) + "\n";
|
|
25913
25965
|
return replaceAll(output.split("\n").map((line) => literalLines.get(line.trim()) ?? line).join("\n"), sentinel, "\n");
|
|
25914
25966
|
}
|
|
25915
25967
|
/** Literal HTML and opaque SVG, whose text can inherit significant whitespace. */
|
|
@@ -26075,7 +26127,7 @@ function indentMacros(lines, isBlock, isDedenting, literalMarkers) {
|
|
|
26075
26127
|
col = body.keepOriginal ? line.col : base + (body.keepRelative ? line.col - (isRoot ? 0 : body.minCol) : 0);
|
|
26076
26128
|
}
|
|
26077
26129
|
outCol.push(col);
|
|
26078
|
-
const hardBreak =
|
|
26130
|
+
const hardBreak = lines[i].endsWith(" ") && info[i + 1] ? " " : "";
|
|
26079
26131
|
result.push(" ".repeat(col) + line.text + hardBreak);
|
|
26080
26132
|
}
|
|
26081
26133
|
return result;
|