@rohal12/spindle 0.59.29 → 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.
Files changed (50) hide show
  1. package/README.md +1 -0
  2. package/dist/pkg/format.js +1 -1
  3. package/dist/pkg/headless.js +560 -272
  4. package/dist/pkg/macro-registry.js +19 -0
  5. package/dist/pkg/macro-registry.json +19 -0
  6. package/dist/pkg/story-variables.js +494 -90
  7. package/package.json +1 -1
  8. package/src/action-registry.ts +59 -5
  9. package/src/automation/load-yaml.ts +111 -20
  10. package/src/automation/runner.ts +15 -13
  11. package/src/automation/types.ts +7 -5
  12. package/src/class-registry.ts +8 -5
  13. package/src/code-check.ts +87 -20
  14. package/src/components/macros/Checkbox.tsx +5 -2
  15. package/src/components/macros/Cycle.tsx +1 -0
  16. package/src/components/macros/For.tsx +1 -7
  17. package/src/components/macros/Listbox.tsx +1 -0
  18. package/src/components/macros/MenubarButtons.tsx +6 -1
  19. package/src/components/macros/Nobr.tsx +1 -0
  20. package/src/components/macros/PassageDisplay.tsx +42 -0
  21. package/src/components/macros/Radiobutton.tsx +1 -0
  22. package/src/components/macros/SaveManager.tsx +19 -4
  23. package/src/components/macros/SettingsControls.tsx +19 -4
  24. package/src/components/macros/Span.tsx +1 -0
  25. package/src/components/macros/StoryTitle.tsx +1 -0
  26. package/src/components/macros/VarDisplay.tsx +2 -3
  27. package/src/components/macros/WidgetInvocation.tsx +5 -6
  28. package/src/components/macros/input-macro.ts +1 -0
  29. package/src/components/macros/locals-scope.tsx +19 -8
  30. package/src/components/macros/macro-args.ts +11 -3
  31. package/src/expression.ts +34 -18
  32. package/src/hooks/use-merged-locals.ts +17 -2
  33. package/src/hooks/use-modal-focus.ts +190 -129
  34. package/src/index.tsx +22 -12
  35. package/src/js-bindings.ts +332 -0
  36. package/src/js-lexer.ts +12 -3
  37. package/src/macro-discovery.ts +25 -22
  38. package/src/static-literal.ts +87 -16
  39. package/src/store.ts +4 -1
  40. package/src/story-api.ts +13 -4
  41. package/src/story-variables.ts +91 -28
  42. package/src/structural.ts +19 -15
  43. package/src/tooling.ts +21 -8
  44. package/src/transform.ts +22 -7
  45. package/src/utils/namespace.ts +4 -0
  46. package/src/utils/object-path.ts +2 -2
  47. package/src/utils/stable-key.ts +17 -6
  48. package/src/utils/value-kinds.ts +38 -1
  49. package/types/index.d.ts +12 -5
  50. package/types/tooling.d.ts +12 -2
@@ -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, src.slice(tok.start + 1, tok.end), tok.start, nesting);
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) {
@@ -10364,10 +10364,12 @@ var LEADING_OPERATOR_RE = /^(?:[-+*/&|^=<>?:,.]|%(?![A-Za-z_]))/;
10364
10364
  * Where the rest of `src` is without the flag `word` as its last or first
10365
10365
  * term, or `null` if it has no such flag. Next to a binary operator the
10366
10366
  * word is an operand (`"a" + inline`), not a flag; the closing `/` of a
10367
- * 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).
10368
10369
  */
10369
- function takeFlag(src, word) {
10370
+ function takeFlag(src, word, alone) {
10370
10371
  const spans = terms(src);
10372
+ if (spans.length === 1) return alone && slice(src, spans[0]) === word ? [src.length, src.length] : null;
10371
10373
  if (spans.length < 2) return null;
10372
10374
  if (slice(src, spans[spans.length - 1]) === word) {
10373
10375
  const rest = [0, spans[spans.length - 2][1]];
@@ -10414,8 +10416,9 @@ function trimmed(src, at) {
10414
10416
  */
10415
10417
  function readGroup(params, src, args, { at: srcAt, spans }) {
10416
10418
  let [text, at] = trimmed(src ?? "", srcAt);
10419
+ const alone = !params.some((p) => p.type !== "flag" && p.required);
10417
10420
  for (const flag of params.filter((p) => p.type === "flag")) {
10418
- const rest = src === null ? null : takeFlag(text, flag.name);
10421
+ const rest = src === null ? null : takeFlag(text, flag.name, alone);
10419
10422
  args[flag.name] = rest !== null;
10420
10423
  if (rest) [text, at] = trimmed(slice(text, rest), at + rest[0]);
10421
10424
  }
@@ -10611,6 +10614,8 @@ var RESERVED_NAME = "__proto__";
10611
10614
  function variableNameError(name, label = name) {
10612
10615
  return name === "__proto__" ? `"${label}" cannot be used as a variable name (${RESERVED_NAME} is reserved)` : void 0;
10613
10616
  }
10617
+ /** Whether a property key is an array index (`"0"`, `"12"`; not `"01"`). */
10618
+ var isArrayIndex = (key) => /^(?:0|[1-9]\d*)$/.test(key);
10614
10619
  /** Whether `obj` holds `key` as an own property. */
10615
10620
  var hasOwn = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
10616
10621
  /**
@@ -10903,24 +10908,50 @@ function nestedPiece(piece, toRoot, where) {
10903
10908
  ...nested
10904
10909
  };
10905
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
+ }
10906
10933
  /**
10907
10934
  * Where each character of `value`, the unescaped text of a quoted string
10908
- * written as `raw` from `at` on (a backslash before a quote or a backslash
10909
- * is dropped), is
10910
- * in the source, and where the text ends. None when the text is where its
10911
- * length says, or is not that string.
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.
10912
10939
  */
10913
- function escapeOffsets(raw, at, value) {
10940
+ function escapeOffsets(raw, at, value, escapes) {
10914
10941
  const offsets = [];
10915
10942
  let escaped = false;
10916
10943
  let i = 0;
10917
10944
  while (offsets.length < value.length) {
10918
10945
  if (i >= raw.length) return void 0;
10919
- offsets.push(at + i);
10920
- if (raw[i] === "\\" && /["'\\]/.test(raw.charAt(i + 1))) {
10921
- escaped = true;
10922
- i += 2;
10923
- } else i++;
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;
10924
10955
  }
10925
10956
  offsets.push(at + i);
10926
10957
  return escaped ? offsets : void 0;
@@ -10943,7 +10974,7 @@ function* argPieces(args, offset, params, macro, label) {
10943
10974
  };
10944
10975
  return;
10945
10976
  }
10946
- yield* valuePieces(args, offset, params, values, spans, macro, label);
10977
+ yield* valuePieces(args, offset, params, values, spans, macro, label, "quotes");
10947
10978
  }
10948
10979
  /**
10949
10980
  * The code and text in the arguments `args` (at `offset`) of the widget
@@ -10969,13 +11000,14 @@ function* widgetPieces(args, offset, params, macro, label) {
10969
11000
  values[name] = value;
10970
11001
  spans.set(param, span);
10971
11002
  });
10972
- yield* valuePieces(args, offset, declared, values, spans, macro, label);
11003
+ yield* valuePieces(args, offset, declared, values, spans, macro, label, "javascript");
10973
11004
  }
10974
11005
  /**
10975
11006
  * The pieces of the `params` of `macro` with the `values` read from `args`
10976
- * (at `offset`), whose `spans` tell where each was written.
11007
+ * (at `offset`), whose `spans` tell where each was written and whose strings
11008
+ * are unescaped as `escapes` says.
10977
11009
  */
10978
- function* valuePieces(args, offset, params, values, spans, macro, label) {
11010
+ function* valuePieces(args, offset, params, values, spans, macro, label, escapes) {
10979
11011
  const name = macro.toLowerCase();
10980
11012
  /** The pieces of the parameters `list`, with their values in `from`. */
10981
11013
  function* visit(list, from) {
@@ -11023,7 +11055,7 @@ function* valuePieces(args, offset, params, values, spans, macro, label) {
11023
11055
  }
11024
11056
  const at = offset + start + (value !== written && /^["']/.test(written) ? 1 : 0);
11025
11057
  const holds = stringHolds(param);
11026
- const sourceOffsets = escapeOffsets(args.slice(at - offset), at, value);
11058
+ const sourceOffsets = escapeOffsets(args.slice(at - offset), at, value, escapes);
11027
11059
  if (holds === "expression" || holds === "statements") {
11028
11060
  const piece = {
11029
11061
  kind: "code",
@@ -11089,20 +11121,21 @@ function attributeValues(src, token) {
11089
11121
  }
11090
11122
  /**
11091
11123
  * The `{$…}` expressions in the value of a code attribute (`onclick`, see
11092
- * 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.
11093
11128
  */
11094
11129
  function sigilExpressions(value) {
11095
11130
  const found = [];
11096
11131
  let from = 0;
11097
- for (const part of splitSigilTemplate(value)) {
11098
- if (!("expr" in part)) continue;
11099
- const at = locate(value, part.expr, from);
11100
- from = at + part.expr.length;
11132
+ for (const part of splitSigilTemplate(value)) if ("expr" in part) {
11101
11133
  found.push({
11102
11134
  expr: part.expr,
11103
- at
11135
+ at: from + 1
11104
11136
  });
11105
- }
11137
+ from += part.expr.length + 2;
11138
+ } else from += ("text" in part ? part.text : part.verbatim).length;
11106
11139
  return found;
11107
11140
  }
11108
11141
  /**
@@ -11526,10 +11559,12 @@ function validateMarkup(passages, options) {
11526
11559
  * an operator, a reference, a spread) is "not static", which is no error: the
11527
11560
  * value is simply not known without running the code.
11528
11561
  */
11529
- var NUMBER_RE = /(?:0[xX][\da-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][+-]?\d+)?)(n?)/y;
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;
11530
11563
  var IDENT_RE = /[A-Za-z_$][\w$]*/y;
11531
11564
  var KEY_RE = /[\w$]+/y;
11532
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;
11533
11568
  var ARRAY_INDEX_RE = /^(?:0|[1-9]\d{0,8})$/;
11534
11569
  var UNSUPPORTED_UNDEFINED = {
11535
11570
  unsupported: "undefined",
@@ -11551,6 +11586,22 @@ function entryOrder(keys) {
11551
11586
  const all = [...keys];
11552
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))];
11553
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
+ }
11554
11605
  /** `String(value)` of a BigInt literal's digits, or `undefined` if they are no BigInt. */
11555
11606
  function bigintValue(digits) {
11556
11607
  try {
@@ -11564,9 +11615,15 @@ function bigintValue(digits) {
11564
11615
  * helpers that skip what is not read are shared (see macro-discovery.ts).
11565
11616
  */
11566
11617
  var Reader = class {
11567
- constructor(src) {
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 = {}) {
11568
11623
  this.src = src;
11569
11624
  this.i = 0;
11625
+ this.goal = options.goal ?? "expression";
11626
+ this.regexes = options.regexes;
11570
11627
  }
11571
11628
  /** Skip whitespace and comments. */
11572
11629
  space() {
@@ -11574,8 +11631,8 @@ var Reader = class {
11574
11631
  for (;;) {
11575
11632
  while (/\s/.test(src.charAt(this.i))) this.i++;
11576
11633
  if (src.startsWith("//", this.i)) {
11577
- const end = src.indexOf("\n", this.i);
11578
- this.i = end < 0 ? src.length : end;
11634
+ LINE_END_RE.lastIndex = this.i;
11635
+ this.i = LINE_END_RE.test(src) ? LINE_END_RE.lastIndex - 1 : src.length;
11579
11636
  } else if (src.startsWith("/*", this.i)) {
11580
11637
  const end = src.indexOf("*/", this.i + 2);
11581
11638
  this.i = end < 0 ? src.length : end + 2;
@@ -11634,10 +11691,9 @@ var Reader = class {
11634
11691
  i++;
11635
11692
  while (/\s/.test(src.charAt(i))) i++;
11636
11693
  }
11637
- NUMBER_RE.lastIndex = i;
11638
- const match = NUMBER_RE.exec(src);
11694
+ const match = this.numberAt(i);
11639
11695
  if (match) {
11640
- this.i = NUMBER_RE.lastIndex;
11696
+ this.i = i + match[0].length;
11641
11697
  if (!match[1]) return { shape: { type: "number" } };
11642
11698
  if (sign === "+") return { throws: "Cannot convert a BigInt value to a number" };
11643
11699
  const digits = bigintValue(match[0].slice(0, -1));
@@ -11647,11 +11703,17 @@ var Reader = class {
11647
11703
  };
11648
11704
  }
11649
11705
  IDENT_RE.lastIndex = i;
11650
- if (IDENT_RE.exec(src)?.[0] === "Infinity") {
11706
+ const word = IDENT_RE.exec(src)?.[0];
11707
+ if (word === "Infinity" || word === "NaN") {
11651
11708
  this.i = IDENT_RE.lastIndex;
11652
11709
  return { shape: { type: "number" } };
11653
11710
  }
11654
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
+ }
11655
11717
  /** A keyword, a function, or a name (which is not static). */
11656
11718
  word() {
11657
11719
  const { src } = this;
@@ -11671,6 +11733,7 @@ var Reader = class {
11671
11733
  this.i = end;
11672
11734
  return { shape: { type: "null" } };
11673
11735
  case "NaN":
11736
+ case "Infinity":
11674
11737
  this.i = end;
11675
11738
  return { shape: { type: "number" } };
11676
11739
  case "undefined":
@@ -11811,14 +11874,22 @@ var Reader = class {
11811
11874
  const { src } = this;
11812
11875
  const c = src.charAt(this.i);
11813
11876
  if (c === "\"" || c === "'") return this.stringValue();
11877
+ if (/[\d.]/.test(c)) return this.numberKey();
11814
11878
  KEY_RE.lastIndex = this.i;
11815
11879
  const found = KEY_RE.exec(src)?.[0];
11816
11880
  if (found === void 0) return void 0;
11817
11881
  this.i = KEY_RE.lastIndex;
11818
- if (!/^\d/.test(found)) return found;
11819
- const digits = found.replace(/_/g, "");
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, "");
11820
11891
  if (/^0\d/.test(digits)) return void 0;
11821
- if (digits.endsWith("n") && !/^0[xX]/.test(digits)) return bigintValue(digits.slice(0, -1));
11892
+ if (match[1]) return bigintValue(digits.slice(0, -1));
11822
11893
  const value = Number(digits);
11823
11894
  return Number.isNaN(value) ? void 0 : String(value);
11824
11895
  }
@@ -11861,6 +11932,14 @@ var Reader = class {
11861
11932
  this.space();
11862
11933
  return true;
11863
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
+ }
11864
11943
  if (c === "[" || c === "{" || c === "(") return this.skipBalanced();
11865
11944
  if (c === "]" || c === "}" || c === ")") return false;
11866
11945
  this.i++;
@@ -12115,6 +12194,7 @@ function checkReference(ref, declared) {
12115
12194
  const part = ref.path[i];
12116
12195
  if (current.type === "array") return null;
12117
12196
  if (current.type === "null") return null;
12197
+ if (current.type === "string" && isArrayIndex(part)) continue;
12118
12198
  const sample = PRIMITIVE_SAMPLES[current.type];
12119
12199
  if (sample && part in sample) {
12120
12200
  const memberType = typeof sample[part];
@@ -12131,10 +12211,28 @@ function checkReference(ref, declared) {
12131
12211
  return null;
12132
12212
  }
12133
12213
  var NO_STORE_VAR_MACROS = /* @__PURE__ */ new Set();
12134
- /** A reference written at `index` of `code`, with its dot path. */
12135
- function referenceAt(code, index, at) {
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) {
12136
12234
  VAR_PATH_RE.lastIndex = index;
12137
- const match = VAR_PATH_RE.exec(code);
12235
+ const match = VAR_PATH_RE.exec(term);
12138
12236
  if (!match) return void 0;
12139
12237
  const [name, ...path] = match[2].split(".");
12140
12238
  return {
@@ -12179,46 +12277,51 @@ function scanCode(code, at, emit, goal = "expression") {
12179
12277
  }
12180
12278
  for (const ref of parsed.refs) {
12181
12279
  if (ref.sigil !== "$" && ref.sigil !== "%") continue;
12182
- const found = referenceAt(code, ref.start, at);
12183
- if (found) emit(found);
12280
+ emit(referenceAt(code, ref.sigil, ref.name, ref.start, ref.end, at));
12184
12281
  }
12185
12282
  }
12186
12283
  /** `scanCode` for code acorn can't parse: lexed leniently. */
12187
12284
  function scanCodeLeniently(code, at, emit, goal) {
12188
12285
  lexJs(code, {
12189
- variable(sigil, _name, index) {
12286
+ variable(sigil, name, index, _nesting, end) {
12190
12287
  if (sigil !== "$" && sigil !== "%") return;
12191
- const found = referenceAt(code, index, at);
12192
- if (found) emit(found);
12288
+ emit(referenceAt(code, sigil, name, index, end, at));
12193
12289
  },
12194
12290
  literal() {}
12195
12291
  }, goal);
12196
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
+ }
12197
12304
  /**
12198
12305
  * Report the references in the arguments of a macro by what its parameters
12199
12306
  * say each holds (see ParameterDef.holds): code is scanned as code and
12200
12307
  * markup as markup, but literal text (a placeholder, a passage name) holds
12201
12308
  * none. A macro without declared parameters, or arguments that do not
12202
- * 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).
12203
12311
  */
12204
- function scanArgs(token, params, at, emit, options) {
12312
+ function scanArgs(token, parametersOf, at, emit, options) {
12205
12313
  if (!token.rawArgs) return;
12314
+ const params = parametersOf(token.name);
12315
+ let byParameters = false;
12206
12316
  if (params) {
12207
12317
  if (options.all && !options.storeVarMacros.has(token.name.toLowerCase())) scanVariableParameters(token, params, at, emit);
12208
12318
  const pieces = [...argPieces(token.rawArgs, token.argsStart, params, token.name, "")];
12209
- if (!pieces.some((piece) => piece.kind === "argument-error")) {
12210
- for (const piece of pieces) {
12211
- const pieceAt = (i) => at(pieceOffset(piece, i));
12212
- if (piece.kind === "code") scanCode(piece.code, pieceAt, emit, piece.goal);
12213
- else if (piece.kind === "text") scanInterpolations(piece.text, pieceAt, emit, {
12214
- ...options,
12215
- storeVarMacros: NO_STORE_VAR_MACROS
12216
- });
12217
- }
12218
- return;
12219
- }
12319
+ byParameters = !pieces.some((piece) => piece.kind === "argument-error");
12320
+ if (byParameters) scanPieces(pieces, at, emit, options);
12220
12321
  }
12221
- 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);
12222
12325
  }
12223
12326
  /**
12224
12327
  * Report the variables the `variable` parameters of a macro name
@@ -12236,7 +12339,7 @@ function scanVariableParameters(token, params, at, emit) {
12236
12339
  for (const param of params) {
12237
12340
  const span = spans.get(param);
12238
12341
  if (param.type !== "variable" || !span) continue;
12239
- const ref = referenceAt(token.rawArgs.slice(span[0], span[1]), 0, (i) => at(token.argsStart + span[0] + i));
12342
+ const ref = termReferenceAt(token.rawArgs.slice(span[0], span[1]), 0, (i) => at(token.argsStart + span[0] + i));
12240
12343
  if (ref) emit(ref);
12241
12344
  }
12242
12345
  }
@@ -12277,7 +12380,7 @@ function collectTokenRefs(content, tokens, at, emit, options) {
12277
12380
  else scanInterpolations(value, valueIn, emit, options);
12278
12381
  }
12279
12382
  else if (token.type === "macro" && !token.isClose) {
12280
- scanArgs(token, parametersOf(token.name), at, emit, options);
12383
+ scanArgs(token, parametersOf, at, emit, options);
12281
12384
  scanSelectors(content, token, at, emit, options);
12282
12385
  if (storeVarMacros.has(token.name.toLowerCase())) {
12283
12386
  const lead = token.rawArgs.length - token.rawArgs.trimStart().length;
@@ -12320,7 +12423,7 @@ function collectTokenRefs(content, tokens, at, emit, options) {
12320
12423
  function collectVariableReferences(content, options) {
12321
12424
  const found = [];
12322
12425
  withParseCache(() => collectTokenRefs(content, tokensOf(content, false, options), (i) => i, (ref) => found.push(ref), options));
12323
- return found;
12426
+ return found.sort((a, b) => a.start - b.start);
12324
12427
  }
12325
12428
  /**
12326
12429
  * The invalid variable references of the passages, against what is declared,
@@ -12348,6 +12451,273 @@ function checkVariableReferences(passages, declared, options) {
12348
12451
  return diagnostics;
12349
12452
  }
12350
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
12351
12721
  //#region src/macro-discovery.ts
12352
12722
  /**
12353
12723
  * What `defineMacro({ … })` calls in JavaScript declare, read from the text
@@ -12474,13 +12844,19 @@ var ConfigReader = class extends Reader {
12474
12844
  this.failed = true;
12475
12845
  }
12476
12846
  };
12477
- /** The text with everything but code (strings, comments, regexes) blanked. */
12847
+ /**
12848
+ * The text with everything but code (strings, comments, regexes) blanked, and
12849
+ * where its regex literals are.
12850
+ */
12478
12851
  function codeOnly(source) {
12479
12852
  const chars = Array.from({ length: source.length }, () => " ");
12480
- lexJs(source, { code(ch, index) {
12853
+ const regexes = regexLiterals(source, "statements", { code(ch, index) {
12481
12854
  for (let k = 0; k < ch.length; k++) chars[index + k] = ch[k];
12482
- } }, "statements");
12483
- return chars.join("");
12855
+ } });
12856
+ return {
12857
+ code: chars.join(""),
12858
+ regexes
12859
+ };
12484
12860
  }
12485
12861
  var stringOf = (d) => d?.type === "string" ? d : void 0;
12486
12862
  var booleanOf = (d) => d?.type === "boolean" ? d.value : void 0;
@@ -12554,8 +12930,9 @@ function macroOf(config) {
12554
12930
  * `source` (JavaScript: a script passage, the body of a `{do}`, a project
12555
12931
  * file) declare, in source order, read from the text without running it. A
12556
12932
  * call whose config is an object literal (or a variable declared as one with
12557
- * `const`, `let` or `var`) and whose `name` is a string is found, wherever it
12558
- * is, even inside a function. Only what is written out is read: a `block`,
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`,
12559
12936
  * `interpolate`, `storeVar` or `merged` that is a boolean literal, the
12560
12937
  * strings of `subMacros`, and `parameters` when every one is an object
12561
12938
  * literal with a string `name` and `type` (and `holds`, `required`,
@@ -12565,18 +12942,23 @@ function macroOf(config) {
12565
12942
  * throws for none.
12566
12943
  */
12567
12944
  function discoverMacros(source) {
12568
- const code = codeOnly(source);
12945
+ const { code, regexes } = codeOnly(source);
12569
12946
  const found = [];
12947
+ let bindings;
12570
12948
  for (const call of code.matchAll(CALL_RE)) {
12571
12949
  let at = call.index + call[0].length;
12572
12950
  if (code.charAt(at) !== "{") {
12573
12951
  const variable = IDENTIFIER_ARGUMENT_RE.exec(code.slice(at))?.[1];
12574
12952
  if (!variable) continue;
12575
- const declaration = new RegExp(`(?<![\\w$.])(?:const|let|var)\\s+${variable.replace(/\$/g, "\\$")}\\s*=\\s*(?=\\{)`).exec(code);
12576
- if (!declaration) continue;
12577
- at = declaration.index + declaration[0].length;
12953
+ bindings ?? (bindings = new Bindings(code));
12954
+ const object = bindings.objectOf(variable, call.index);
12955
+ if (object === void 0) continue;
12956
+ at = object;
12578
12957
  }
12579
- const reader = new ConfigReader(source);
12958
+ const reader = new ConfigReader(source, {
12959
+ goal: "statements",
12960
+ regexes
12961
+ });
12580
12962
  reader.i = at;
12581
12963
  const config = reader.datum();
12582
12964
  const macro = config && macroOf(config);
@@ -12619,17 +13001,29 @@ function splitIncludeFlag(rawArgs) {
12619
13001
  * (see src/tooling.ts).
12620
13002
  */
12621
13003
  var TRANSFORM_CACHE_MAX = 500;
12622
- var NAMESPACES = {
12623
- $: "variables",
12624
- _: "temporary",
12625
- "@": "locals",
12626
- "%": "transient"
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"
12627
13020
  };
12628
13021
  /** Ends with an identifier character. */
12629
13022
  var IDENT_END_RE = /[\p{ID_Continue}$\u200c\u200d]$/u;
12630
13023
  /**
12631
13024
  * Transform expression: $var → variables["var"], _var → temporary["var"],
12632
- * @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.
12633
13027
  * The lexer (`lexJs`) finds the references: only in code (string, template
12634
13028
  * and regex literal text and comments are left untouched), only where an
12635
13029
  * identifier starts (not in `a$b`, nor as the property name in `obj._x`),
@@ -12652,7 +13046,7 @@ function transform(expr, goal = "expression") {
12652
13046
  result += expr.slice(at, ref.start);
12653
13047
  if (ref.shorthand) result += `${ref.name}: `;
12654
13048
  else if (IDENT_END_RE.test(result.slice(-2))) result += " ";
12655
- result += `${NAMESPACES[ref.sigil]}["${ref.name}"]`;
13049
+ result += `${BINDINGS[ref.sigil]}["${ref.name}"]`;
12656
13050
  at = ref.end;
12657
13051
  }
12658
13052
  result += expr.slice(at);
@@ -12682,10 +13076,7 @@ function validateStoryMarkup(passages, macros, options = {}) {
12682
13076
  const known = /* @__PURE__ */ new Set();
12683
13077
  const widgets = widgetDefinitions$1(list, parameterLookup(all));
12684
13078
  const blocks = new Set(widgets.filter((def) => def.block && def.closeStart !== void 0).map((def) => def.name.toLowerCase()));
12685
- const parametersOf = parameterLookup([...all, ...widgets.map((widget) => ({
12686
- name: widget.name,
12687
- widget
12688
- }))]);
13079
+ const parametersOf = parameterLookup([...all, ...definedWidgetMacros(widgets)]);
12689
13080
  for (const macro of all) {
12690
13081
  const name = macro.name.toLowerCase();
12691
13082
  known.add(name);
@@ -12701,6 +13092,13 @@ function validateStoryMarkup(passages, macros, options = {}) {
12701
13092
  tolerant: options.tolerant
12702
13093
  });
12703
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
+ }
12704
13102
  /** Whether a macro takes a body, among `macros` and the built-in ones. */
12705
13103
  function blockLookup(macros) {
12706
13104
  const blocks = new Set(macros.filter((m) => m.block).map((m) => m.name.toLowerCase()));
@@ -12744,13 +13142,18 @@ function widgetDefinitions(passages, macros = []) {
12744
13142
  }
12745
13143
  /** Passages tagged so hold code or styles, not markup. */
12746
13144
  var NOT_MARKUP_TAGS = ["script", "stylesheet"];
12747
- /** What reading the variable references of markup depends on. */
12748
- function referenceOptions(macros, all = false) {
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 = []) {
12749
13151
  const list = [...macros];
13152
+ const widgets = widgetDefinitions$1(passages, parameterLookup(list));
12750
13153
  return {
12751
13154
  all,
12752
13155
  storeVarMacros: new Set(list.filter((m) => m.storeVar).map((m) => m.name.toLowerCase())),
12753
- parametersOf: parameterLookup(list),
13156
+ parametersOf: parameterLookup([...list, ...definedWidgetMacros(widgets)]),
12754
13157
  tolerant: true
12755
13158
  };
12756
13159
  }
@@ -12781,7 +13184,8 @@ function variableReferences(source, macros = [], options = {}) {
12781
13184
  * tolerant of half-typed markup, and it evaluates no project code.
12782
13185
  */
12783
13186
  function validateVariableReferences(passages, declarations, macros = []) {
12784
- return checkVariableReferences([...passages].filter((p) => !p.tags?.some((t) => NOT_MARKUP_TAGS.includes(t))), declarations, referenceOptions(macros));
13187
+ const list = [...passages];
13188
+ return checkVariableReferences(list.filter((p) => !p.tags?.some((t) => NOT_MARKUP_TAGS.includes(t))), declarations, referenceOptions(macros, false, list));
12785
13189
  }
12786
13190
  //#endregion
12787
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 };