@rohal12/spindle 0.59.26 → 0.59.28

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.
@@ -8879,6 +8879,9 @@ Parser.acorn = {
8879
8879
  lineBreakG,
8880
8880
  nonASCIIwhitespace
8881
8881
  };
8882
+ function parse$1(input, options) {
8883
+ return Parser.parse(input, options);
8884
+ }
8882
8885
  //#endregion
8883
8886
  //#region src/js-lexer.ts
8884
8887
  /**
@@ -11273,9 +11276,11 @@ function textContainsChildren(text, parametersOf) {
11273
11276
  * Whether a tag renders a `{@children}` in an attribute value or a macro
11274
11277
  * argument that holds markup (`<span title="{@children}">`,
11275
11278
  * `{button "{@children}"}`). Literal arguments and code are not markup.
11279
+ * `src` is the text a flat token's offsets index into (its attribute values
11280
+ * are read from it); a token made from an AST has none.
11276
11281
  */
11277
- function tokenTextContainsChildren(token, parametersOf = registeredParameters) {
11278
- for (const piece of codeAndText("", [token], parametersOf)) if (piece.kind === "text" && textContainsChildren(piece.text, parametersOf)) return true;
11282
+ function tokenTextContainsChildren(token, parametersOf = registeredParameters, src = "") {
11283
+ for (const piece of codeAndText(src, [token], parametersOf)) if (piece.kind === "text" && textContainsChildren(piece.text, parametersOf)) return true;
11279
11284
  return false;
11280
11285
  }
11281
11286
  /**
@@ -11335,13 +11340,14 @@ var WIDGET_PARAMETERS = [{
11335
11340
  }];
11336
11341
  /**
11337
11342
  * The widget a {widget} definition's arguments declare: its name and its
11338
- * parameters, the words after it that start with `@` (docs/widgets.md).
11339
- * Other words, such as `$name`, are not parameters.
11343
+ * parameters, the words after it that start with `@` (docs/widgets.md),
11344
+ * separated by spaces or commas (`@a, @b`). Other words, such as `$name`, are
11345
+ * not parameters.
11340
11346
  */
11341
11347
  function widgetDef({ name = "", parameters = "" }) {
11342
11348
  return {
11343
11349
  name,
11344
- params: parameters.split(/\s+/).filter((word) => word.startsWith("@"))
11350
+ params: parameters.split(/[\s,]+/).filter((word) => word.startsWith("@"))
11345
11351
  };
11346
11352
  }
11347
11353
  /** Read the arguments of a {widget} definition (see widgetDef). */
@@ -11385,7 +11391,7 @@ function widgetDefinitions(passages, parametersOf = registeredParameters) {
11385
11391
  closeStart: token.start
11386
11392
  });
11387
11393
  }
11388
- } else if (open.length > 0 && (token.type === "variable" && token.scope === "local" && token.name === "children" || tokenTextContainsChildren(token, parametersOf))) open[open.length - 1].isBlock = true;
11394
+ } else if (open.length > 0 && (token.type === "variable" && token.scope === "local" && token.name === "children" || tokenTextContainsChildren(token, parametersOf, passage.content))) open[open.length - 1].isBlock = true;
11389
11395
  done.push(...open);
11390
11396
  const defs = [];
11391
11397
  for (const { token, isBlock, closeStart } of done) try {
@@ -13006,11 +13012,25 @@ function resolveAttributeValue(name, value, scope, errors) {
13006
13012
  function isBlankText(n) {
13007
13013
  return n.type === "text" && (n.comment === true || n.value.trim() === "");
13008
13014
  }
13009
- /** The index of the summary element opening `nodes`, or -1 when none does. */
13010
- function leadingSummaryIndex(nodes) {
13015
+ /**
13016
+ * The element a container must open with, to stay its direct child: the
13017
+ * summary of a details element (#382), the legend of a fieldset (#475), whose
13018
+ * first legend exempts the controls in it from the fieldset's `disabled`.
13019
+ */
13020
+ var LEADING_PARTS = {
13021
+ details: "summary",
13022
+ fieldset: "legend"
13023
+ };
13024
+ /**
13025
+ * The index of the node opening `nodes` as the leading part of the `tag`
13026
+ * container, or -1 when none does: the element itself, or a macro, which may
13027
+ * render it ({if}, {include}, a widget: #474).
13028
+ */
13029
+ function leadingPartIndex(tag, nodes) {
13030
+ const part = LEADING_PARTS[tag];
13011
13031
  const at = nodes.findIndex((n) => !isBlankText(n));
13012
13032
  const first = nodes[at];
13013
- return first?.type === "html" && first.tag.toLowerCase() === "summary" ? at : -1;
13033
+ return part && (first?.type === "macro" || first?.type === "html" && first.tag.toLowerCase() === part) ? at : -1;
13014
13034
  }
13015
13035
  function HtmlNodeRenderer({ node }) {
13016
13036
  const scope = useTextScope();
@@ -13018,6 +13038,8 @@ function HtmlNodeRenderer({ node }) {
13018
13038
  const inSvg = useContext(SvgContext);
13019
13039
  const tag = node.tag.toLowerCase();
13020
13040
  const isSvgRoot = tag === "svg";
13041
+ const isHtmlRoot = inSvg && tag === "foreignobject";
13042
+ const rawContent = inRaw && !isHtmlRoot;
13021
13043
  const isRawRoot = !inRaw && (isSvgRoot || PREFORMATTED_ELEMENTS.has(tag));
13022
13044
  const errors = [];
13023
13045
  const { props, direct } = splitAttributes(Object.entries(node.attributes).map(([k, v]) => [
@@ -13032,19 +13054,19 @@ function HtmlNodeRenderer({ node }) {
13032
13054
  let structural = false;
13033
13055
  if (node.children.length > 0) {
13034
13056
  if (tag === "style") children = node.children.map((n) => n.type === "text" ? n.value : renderSingleNode(n));
13035
- else if (inRaw || isRawRoot) children = renderInlineNodes(node.children);
13057
+ else if (rawContent || isRawRoot) children = renderInlineNodes(node.children);
13036
13058
  else {
13037
- const summaryAt = tag === "details" ? leadingSummaryIndex(node.children) : -1;
13059
+ const leadAt = leadingPartIndex(tag, node.children);
13038
13060
  const body = (nodes) => renderNodes(nodes, {
13039
13061
  nobr,
13040
13062
  locals,
13041
13063
  inline: isInline
13042
13064
  });
13043
13065
  structural = STRUCTURAL_ELEMENTS.has(tag) && node.children.every((n) => n.type !== "text" || isBlankText(n));
13044
- children = structural ? node.children.filter((n) => n.type !== "text").map((n) => renderSingleNode(n)) : summaryAt === -1 ? body(node.children) : /* @__PURE__ */ jsxs(Fragment$1, { children: [
13045
- body(node.children.slice(0, summaryAt)),
13046
- renderSingleNode(node.children[summaryAt]),
13047
- body(node.children.slice(summaryAt + 1))
13066
+ children = structural ? node.children.filter((n) => n.type !== "text").map((n) => renderSingleNode(n)) : leadAt === -1 ? body(node.children) : /* @__PURE__ */ jsxs(Fragment$1, { children: [
13067
+ body(node.children.slice(0, leadAt)),
13068
+ renderSingleNode(node.children[leadAt]),
13069
+ body(node.children.slice(leadAt + 1))
13048
13070
  ] });
13049
13071
  if (isInline !== parentInline) children = /* @__PURE__ */ jsx(InlineContext.Provider, {
13050
13072
  value: isInline,
@@ -13056,9 +13078,12 @@ function HtmlNodeRenderer({ node }) {
13056
13078
  });
13057
13079
  }
13058
13080
  }
13059
- if (inSvg && tag === "foreignobject" && children) children = /* @__PURE__ */ jsx(SvgContext.Provider, {
13081
+ if (isHtmlRoot && children) children = /* @__PURE__ */ jsx(SvgContext.Provider, {
13060
13082
  value: false,
13061
- children
13083
+ children: /* @__PURE__ */ jsx(RawTextContext.Provider, {
13084
+ value: false,
13085
+ children
13086
+ })
13062
13087
  });
13063
13088
  let element = h(node.tag, props, children);
13064
13089
  if (isSvgRoot && !inSvg) element = /* @__PURE__ */ jsx(SvgContext.Provider, {
@@ -16748,11 +16773,22 @@ var clonersFor = (paths) => {
16748
16773
  function frozenCopy(value) {
16749
16774
  return freeze(cloneValue(value), true);
16750
16775
  }
16776
+ /**
16777
+ * Whether a built-in's mutators can still change a frozen `value` (#473):
16778
+ * Object.freeze() locks properties, not internal slots. Immer locks a Map or
16779
+ * Set it freezes by overriding its mutators with own properties; a Date, URL
16780
+ * or URLSearchParams has no such lock, and one frozen by hand has none either.
16781
+ */
16782
+ function hasUnlockedSlots(value) {
16783
+ if (value instanceof Map) return !hasOwn(value, "set");
16784
+ if (value instanceof Set) return !hasOwn(value, "add");
16785
+ return value instanceof Date || value instanceof URL || value instanceof URLSearchParams;
16786
+ }
16751
16787
  /** Whether `value` and everything it holds is frozen. */
16752
16788
  function deeplyFrozen(value, seen = /* @__PURE__ */ new Set()) {
16753
16789
  if (typeof value !== "object" || value === null || seen.has(value)) return true;
16754
16790
  if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) return false;
16755
- if (!Object.isFrozen(value)) return false;
16791
+ if (!Object.isFrozen(value) || hasUnlockedSlots(value)) return false;
16756
16792
  seen.add(value);
16757
16793
  if (value instanceof Map) {
16758
16794
  for (const [key, entry] of mapEntries(value)) if (!deeplyFrozen(key, seen) || !deeplyFrozen(entry, seen)) return false;
@@ -20043,19 +20079,30 @@ function validateMarkup(passages, options) {
20043
20079
  const parsed = [];
20044
20080
  const widgets = /* @__PURE__ */ new Set();
20045
20081
  const passageNames = /* @__PURE__ */ new Set();
20082
+ const addWidgets = (tokens) => {
20083
+ for (const token of tokens) {
20084
+ if (token.type !== "macro" || token.isClose) continue;
20085
+ if (token.name.toLowerCase() !== "widget") continue;
20086
+ const name = widgetName(token.rawArgs);
20087
+ if (name) widgets.add(name);
20088
+ }
20089
+ };
20046
20090
  for (const passage of passages) {
20047
20091
  passageNames.add(passage.name);
20048
20092
  if (NOT_MARKUP.has(passage.name)) continue;
20049
20093
  if (passage.tags?.some((tag) => NOT_MARKUP_TAGS.includes(tag))) continue;
20050
20094
  const reported = !options.only || options.only(passage);
20095
+ if (options.tolerant) {
20096
+ const { tokens, errors } = analyzeMarkup(passage.content, { hooks });
20097
+ addWidgets(tokens);
20098
+ if (!reported) continue;
20099
+ for (const err of errors) reportError(passage, err, "");
20100
+ parsed.push([passage, tokens]);
20101
+ continue;
20102
+ }
20051
20103
  try {
20052
20104
  const tokens = tokenizeMarkup(passage.content);
20053
- for (const token of tokens) {
20054
- if (token.type !== "macro" || token.isClose) continue;
20055
- if (token.name.toLowerCase() !== "widget") continue;
20056
- const name = widgetName(token.rawArgs);
20057
- if (name) widgets.add(name);
20058
- }
20105
+ addWidgets(tokens);
20059
20106
  if (!reported) continue;
20060
20107
  parseMarkup(passage.content, { hooks });
20061
20108
  parsed.push([passage, tokens]);
@@ -20145,7 +20192,13 @@ function validateMarkup(passages, options) {
20145
20192
  */
20146
20193
  var NUMBER_RE = /(?:0[xX][\da-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][+-]?\d+)?)(n?)/y;
20147
20194
  var IDENT_RE = /[A-Za-z_$][\w$]*/y;
20195
+ var KEY_RE = /[\w$]+/y;
20148
20196
  var ARROW_RE = /(?:async\s+)?(?:\((?:[^()]|\([^()]*\))*\)|[A-Za-z_$][\w$]*)\s*=>/y;
20197
+ var ARRAY_INDEX_RE = /^(?:0|[1-9]\d{0,8})$/;
20198
+ var UNSUPPORTED_UNDEFINED = {
20199
+ unsupported: "undefined",
20200
+ value: "undefined"
20201
+ };
20149
20202
  /** Read `src`, one literal and nothing else. */
20150
20203
  function staticLiteral(src) {
20151
20204
  const reader = new Reader(src);
@@ -20153,6 +20206,27 @@ function staticLiteral(src) {
20153
20206
  reader.space();
20154
20207
  return reader.i === src.length ? value : void 0;
20155
20208
  }
20209
+ /**
20210
+ * The keys of an object in the order `Object.entries` lists them (array
20211
+ * indices first, ascending, then the others as they were added): the order
20212
+ * the runtime meets the members in.
20213
+ */
20214
+ function entryOrder(keys) {
20215
+ const all = [...keys];
20216
+ return [...all.filter((key) => ARRAY_INDEX_RE.test(key)).sort((a, b) => Number(a) - Number(b)), ...all.filter((key) => !ARRAY_INDEX_RE.test(key))];
20217
+ }
20218
+ /** `String(value)` of a BigInt literal's digits, or `undefined` if they are no BigInt. */
20219
+ function bigintValue(digits) {
20220
+ try {
20221
+ return BigInt(digits.replace(/_/g, "")).toString();
20222
+ } catch {
20223
+ return;
20224
+ }
20225
+ }
20226
+ /**
20227
+ * The cursor over a JavaScript text that the literal reader moves: the
20228
+ * helpers that skip what is not read are shared (see macro-discovery.ts).
20229
+ */
20156
20230
  var Reader = class {
20157
20231
  constructor(src) {
20158
20232
  this.src = src;
@@ -20194,6 +20268,15 @@ var Reader = class {
20194
20268
  closed
20195
20269
  };
20196
20270
  }
20271
+ /**
20272
+ * Skip the string literal at the cursor and read its value, or `undefined`
20273
+ * if it is not closed or not well-formed.
20274
+ */
20275
+ stringValue() {
20276
+ const at = this.i;
20277
+ const { closed } = this.string();
20278
+ return closed ? stringLiteralValue(this.src.slice(at, this.i)) ?? void 0 : void 0;
20279
+ }
20197
20280
  /** A template literal: a string, but for `${}`. */
20198
20281
  template() {
20199
20282
  const { src } = this;
@@ -20210,7 +20293,8 @@ var Reader = class {
20210
20293
  number() {
20211
20294
  const { src } = this;
20212
20295
  let i = this.i;
20213
- if (src.charAt(i) === "+" || src.charAt(i) === "-") {
20296
+ const sign = src.charAt(i);
20297
+ if (sign === "+" || sign === "-") {
20214
20298
  i++;
20215
20299
  while (/\s/.test(src.charAt(i))) i++;
20216
20300
  }
@@ -20218,7 +20302,13 @@ var Reader = class {
20218
20302
  const match = NUMBER_RE.exec(src);
20219
20303
  if (match) {
20220
20304
  this.i = NUMBER_RE.lastIndex;
20221
- return match[1] ? { unsupported: "bigint" } : { shape: { type: "number" } };
20305
+ if (!match[1]) return { shape: { type: "number" } };
20306
+ if (sign === "+") return { throws: "Cannot convert a BigInt value to a number" };
20307
+ const digits = bigintValue(match[0].slice(0, -1));
20308
+ return digits === void 0 ? void 0 : {
20309
+ unsupported: "bigint",
20310
+ value: sign === "-" && digits !== "0" ? `-${digits}` : digits
20311
+ };
20222
20312
  }
20223
20313
  IDENT_RE.lastIndex = i;
20224
20314
  if (IDENT_RE.exec(src)?.[0] === "Infinity") {
@@ -20229,8 +20319,9 @@ var Reader = class {
20229
20319
  /** A keyword, a function, or a name (which is not static). */
20230
20320
  word() {
20231
20321
  const { src } = this;
20232
- ARROW_RE.lastIndex = this.i;
20233
- if (ARROW_RE.test(src)) return this.function();
20322
+ const start = this.i;
20323
+ const arrow = this.arrow();
20324
+ if (arrow !== void 0) return arrow ? this.functionFrom(start) : void 0;
20234
20325
  IDENT_RE.lastIndex = this.i;
20235
20326
  const word = IDENT_RE.exec(src)?.[0];
20236
20327
  if (word === void 0) return void 0;
@@ -20248,72 +20339,152 @@ var Reader = class {
20248
20339
  return { shape: { type: "number" } };
20249
20340
  case "undefined":
20250
20341
  this.i = end;
20251
- return { unsupported: "undefined" };
20342
+ return UNSUPPORTED_UNDEFINED;
20343
+ case "void": {
20344
+ this.i = end;
20345
+ const operand = this.value();
20346
+ return operand && !("throws" in operand) ? UNSUPPORTED_UNDEFINED : operand;
20347
+ }
20252
20348
  case "function":
20253
20349
  case "class":
20254
- case "async": return this.function();
20350
+ case "async": return this.skipDeclaration(word) ? this.functionFrom(start) : void 0;
20255
20351
  }
20256
20352
  }
20257
- /** A function (or class): the rest is its body, which is not read. */
20258
- function() {
20259
- return this.unsupported({ unsupported: "function" });
20353
+ /** The function (or class) that was skipped since `start`. */
20354
+ functionFrom(start) {
20355
+ return {
20356
+ unsupported: "function",
20357
+ value: this.src.slice(start, this.i).trimEnd()
20358
+ };
20260
20359
  }
20261
- /** `value`, which makes the whole literal unsupported: the rest is not read. */
20262
- unsupported(value) {
20263
- this.i = this.src.length;
20264
- return value;
20360
+ /**
20361
+ * Skip the parameters of an arrow function and its body, which ends at a
20362
+ * `,` or `}` outside brackets, or at the end of the text. `undefined` if the
20363
+ * text at the cursor is no arrow function, false if it cannot be skipped.
20364
+ */
20365
+ arrow() {
20366
+ const { src } = this;
20367
+ ARROW_RE.lastIndex = this.i;
20368
+ if (!ARROW_RE.test(src)) return void 0;
20369
+ this.i = ARROW_RE.lastIndex;
20370
+ this.space();
20371
+ if (src.charAt(this.i) === "{") return this.skipBalanced();
20372
+ while (this.i < src.length && !",}".includes(src.charAt(this.i))) if (!this.skipOne()) break;
20373
+ return true;
20374
+ }
20375
+ /**
20376
+ * Skip a `function`, `async function` or `class` (its keyword `word` is at
20377
+ * the cursor), up to the end of its body.
20378
+ */
20379
+ skipDeclaration(word) {
20380
+ const { src } = this;
20381
+ this.i += word.length;
20382
+ this.space();
20383
+ if (word === "async") {
20384
+ IDENT_RE.lastIndex = this.i;
20385
+ if (IDENT_RE.exec(src)?.[0] !== "function") return false;
20386
+ return this.skipDeclaration("function");
20387
+ }
20388
+ if (word === "function") {
20389
+ if (src.charAt(this.i) === "*") this.i++;
20390
+ this.space();
20391
+ IDENT_RE.lastIndex = this.i;
20392
+ if (IDENT_RE.test(src)) this.i = IDENT_RE.lastIndex;
20393
+ this.space();
20394
+ if (src.charAt(this.i) !== "(" || !this.skipBalanced()) return false;
20395
+ this.space();
20396
+ } else while (this.i < src.length && src.charAt(this.i) !== "{") if (!this.skipOne()) return false;
20397
+ return src.charAt(this.i) === "{" && this.skipBalanced();
20265
20398
  }
20266
20399
  /** An object literal: the members that are static. */
20267
20400
  object() {
20268
20401
  const { src } = this;
20269
- const fields = /* @__PURE__ */ new Map();
20402
+ const members = /* @__PURE__ */ new Map();
20403
+ let thrown;
20270
20404
  this.i++;
20271
20405
  for (;;) {
20272
20406
  this.space();
20273
20407
  const c = src.charAt(this.i);
20274
20408
  if (c === "}") {
20275
20409
  this.i++;
20276
- return { shape: {
20277
- type: "object",
20278
- fields
20279
- } };
20410
+ return thrown === void 0 ? this.objectOf(members) : { throws: thrown };
20280
20411
  }
20281
20412
  if (c === ",") {
20282
20413
  this.i++;
20283
20414
  continue;
20284
20415
  }
20285
20416
  if (this.i >= src.length) return void 0;
20417
+ const start = this.i;
20286
20418
  const key = this.key();
20419
+ if (key === "__proto__") return void 0;
20287
20420
  this.space();
20288
- if (key !== void 0 && src.charAt(this.i) === ":") {
20421
+ const next = src.charAt(this.i);
20422
+ if (key !== void 0 && next === ":") {
20289
20423
  this.i++;
20290
20424
  const at = this.i;
20291
20425
  const value = this.value();
20292
20426
  this.space();
20293
- if (value && "unsupported" in value) return this.unsupported(value);
20294
- if (value && ",}".includes(src.charAt(this.i))) {
20295
- fields.set(key, value.shape);
20427
+ if (value && this.atMemberEnd()) {
20428
+ if ("throws" in value) thrown ??= value.throws;
20429
+ else members.set(key, "shape" in value ? value.shape : value);
20296
20430
  continue;
20297
20431
  }
20432
+ members.set(key, null);
20298
20433
  this.i = at;
20299
- } else if (key !== void 0 && src.charAt(this.i) === "(") return this.unsupported({ unsupported: "function" });
20434
+ } else if (key !== void 0 && next === "(") {
20435
+ if (!this.skipBalanced()) return void 0;
20436
+ this.space();
20437
+ if (src.charAt(this.i) === "{" && this.skipBalanced()) {
20438
+ members.set(key, {
20439
+ unsupported: "function",
20440
+ value: src.slice(start, this.i)
20441
+ });
20442
+ continue;
20443
+ }
20444
+ return;
20445
+ } else if (key !== void 0 && (next === "," || next === "}")) {
20446
+ members.set(key, null);
20447
+ continue;
20448
+ } else members.clear();
20300
20449
  if (!this.skipMember()) return void 0;
20301
20450
  }
20302
20451
  }
20452
+ /** Whether a member ends at the cursor. */
20453
+ atMemberEnd() {
20454
+ return this.i < this.src.length && ",}".includes(this.src.charAt(this.i));
20455
+ }
20456
+ /**
20457
+ * The object literal with these members: a value no variable can hold
20458
+ * anywhere in it (the first the runtime meets) is what it reads as.
20459
+ */
20460
+ objectOf(members) {
20461
+ const fields = /* @__PURE__ */ new Map();
20462
+ for (const key of entryOrder(members.keys())) {
20463
+ const member = members.get(key);
20464
+ if (!member) continue;
20465
+ if ("unsupported" in member) return member;
20466
+ fields.set(key, member);
20467
+ }
20468
+ return { shape: {
20469
+ type: "object",
20470
+ fields
20471
+ } };
20472
+ }
20303
20473
  /** The key of a member (a name, a string or a number), if it has one. */
20304
20474
  key() {
20305
20475
  const { src } = this;
20306
20476
  const c = src.charAt(this.i);
20307
- if (c === "\"" || c === "'") {
20308
- const { body, closed } = this.string();
20309
- return closed ? body : void 0;
20310
- }
20311
- const word = /[\w$]+/y;
20312
- word.lastIndex = this.i;
20313
- const found = word.exec(src)?.[0];
20477
+ if (c === "\"" || c === "'") return this.stringValue();
20478
+ KEY_RE.lastIndex = this.i;
20479
+ const found = KEY_RE.exec(src)?.[0];
20314
20480
  if (found === void 0) return void 0;
20315
- this.i = word.lastIndex;
20316
- return found;
20481
+ this.i = KEY_RE.lastIndex;
20482
+ if (!/^\d/.test(found)) return found;
20483
+ const digits = found.replace(/_/g, "");
20484
+ if (/^0\d/.test(digits)) return void 0;
20485
+ if (digits.endsWith("n") && !/^0[xX]/.test(digits)) return bigintValue(digits.slice(0, -1));
20486
+ const value = Number(digits);
20487
+ return Number.isNaN(value) ? void 0 : String(value);
20317
20488
  }
20318
20489
  /** Skip to the `,` or `}` that ends a member, balancing what is inside. */
20319
20490
  skipMember() {
@@ -20447,6 +20618,27 @@ function shapeSchema(shape) {
20447
20618
  };
20448
20619
  }
20449
20620
  /**
20621
+ * What is wrong with the syntax of an initializer, as it is evaluated
20622
+ * (`return (expr)` in a function body), or `undefined`. `pos` is where, as an
20623
+ * index into `expr` (it may be past its end, for an unexpected end).
20624
+ */
20625
+ function initializerSyntaxError(expr) {
20626
+ const prefix = "return (";
20627
+ try {
20628
+ parse$1(prefix + expr + ")", {
20629
+ ecmaVersion: "latest",
20630
+ allowReturnOutsideFunction: true
20631
+ });
20632
+ } catch (err) {
20633
+ if (!(err instanceof SyntaxError)) throw err;
20634
+ const { pos } = err;
20635
+ return {
20636
+ message: err.message.replace(/ \(\d+:\d+\)$/, ""),
20637
+ pos: Math.max(0, pos - 8)
20638
+ };
20639
+ }
20640
+ }
20641
+ /**
20450
20642
  * Read the declarations of a StoryVariables (`$name = value`) or
20451
20643
  * StoryTransients (`%name = value`) passage, without evaluating them and
20452
20644
  * without throwing: every valid declaration, and every invalid line as an
@@ -20454,9 +20646,10 @@ function shapeSchema(shape) {
20454
20646
  *
20455
20647
  * A declaration is a line of the form `$name = expression`; blank lines are
20456
20648
  * skipped. A line that is not, and a name that cannot be a variable, are
20457
- * errors. So is an initializer that no variable can hold (a function,
20458
- * `undefined`), when it is a literal; whether any other initializer can only
20459
- * be known by running it. A name declared again is an error too, but both
20649
+ * errors. So is an initializer with a syntax error, and one that no variable
20650
+ * can hold (a function, `undefined`) when it is a literal, or one that throws
20651
+ * when it is a literal (`+1n`); whether any other initializer can only be
20652
+ * known by running it. A name declared again is an error too, but both
20460
20653
  * declarations are returned (the later wins when evaluated).
20461
20654
  */
20462
20655
  function parseDeclarations(content, sigil = "$") {
@@ -20499,10 +20692,19 @@ function parseDeclarations(content, sigil = "$") {
20499
20692
  };
20500
20693
  if (seen.has(name)) error("duplicate-declaration", nameStart, nameEnd, `Duplicate declaration of ${sigil}${name}`);
20501
20694
  seen.add(name);
20502
- const literal = staticLiteral(expr);
20503
- if (literal && "shape" in literal) declaration.schema = shapeSchema(literal.shape);
20504
- else if (literal) error("unsupported-value", declaration.valueStart, valueEnd, `Unsupported type "${literal.unsupported}" for value ${expr}. ${UNSUPPORTED_TYPE}`);
20505
20695
  declarations.push(declaration);
20696
+ const failed = `Failed to evaluate "${sigil}${name} = ${expr}"`;
20697
+ const syntax = initializerSyntaxError(expr);
20698
+ if (syntax) {
20699
+ const at = Math.min(syntax.pos, expr.length - 1);
20700
+ error("syntax", declaration.valueStart + at, declaration.valueStart + at + 1, `${failed}: ${syntax.message}`);
20701
+ continue;
20702
+ }
20703
+ const literal = staticLiteral(expr);
20704
+ if (!literal) continue;
20705
+ if ("shape" in literal) declaration.schema = shapeSchema(literal.shape);
20706
+ else if ("throws" in literal) error("unsupported-value", declaration.valueStart, valueEnd, `${failed}: ${literal.throws}`);
20707
+ else error("unsupported-value", declaration.valueStart, valueEnd, `Unsupported type "${literal.unsupported}" for value ${literal.value}. ${UNSUPPORTED_TYPE}`);
20506
20708
  }
20507
20709
  return {
20508
20710
  declarations,
@@ -20679,6 +20881,7 @@ function scanCodeLeniently(code, at, emit, goal) {
20679
20881
  function scanArgs(token, params, at, emit, options) {
20680
20882
  if (!token.rawArgs) return;
20681
20883
  if (params) {
20884
+ if (options.all && !options.storeVarMacros.has(token.name.toLowerCase())) scanVariableParameters(token, params, at, emit);
20682
20885
  const pieces = [...argPieces(token.rawArgs, token.argsStart, params, token.name, "")];
20683
20886
  if (!pieces.some((piece) => piece.kind === "argument-error")) {
20684
20887
  for (const piece of pieces) {
@@ -20694,11 +20897,41 @@ function scanArgs(token, params, at, emit, options) {
20694
20897
  }
20695
20898
  scanCode(token.rawArgs, (i) => at(token.argsStart + i), emit);
20696
20899
  }
20900
+ /**
20901
+ * Report the variables the `variable` parameters of a macro name
20902
+ * (`{unset $x}`, `{computed $x = …}`), when they are `$` or `%` variables.
20903
+ * Arguments that do not parse hold none.
20904
+ */
20905
+ function scanVariableParameters(token, params, at, emit) {
20906
+ const spans = /* @__PURE__ */ new Map();
20907
+ try {
20908
+ parseMacroArgs(token.rawArgs, params, spans);
20909
+ } catch (err) {
20910
+ if (err instanceof MacroArgumentError) return;
20911
+ throw err;
20912
+ }
20913
+ for (const param of params) {
20914
+ const span = spans.get(param);
20915
+ if (param.type !== "variable" || !span) continue;
20916
+ const ref = referenceAt(token.rawArgs.slice(span[0], span[1]), 0, (i) => at(token.argsStart + span[0] + i));
20917
+ if (ref) emit(ref);
20918
+ }
20919
+ }
20920
+ /** Report the references in the selectors of a token, which are interpolated. */
20921
+ function scanSelectors(content, token, at, emit, options) {
20922
+ if (!("selectorsStart" in token) || token.selectorsStart === void 0 || token.selectorsEnd === void 0) return;
20923
+ const from = token.selectorsStart;
20924
+ scanInterpolations(content.slice(from, token.selectorsEnd), (i) => at(from + i), emit, {
20925
+ ...options,
20926
+ storeVarMacros: NO_STORE_VAR_MACROS
20927
+ });
20928
+ }
20697
20929
  /** Report the `$var` references in the tokens of `content`. */
20698
20930
  function collectTokenRefs(content, tokens, at, emit, options) {
20699
20931
  const { storeVarMacros, parametersOf } = options;
20700
20932
  for (let t = 0; t < tokens.length; t++) {
20701
20933
  const token = tokens[t];
20934
+ if (options.all && token.type !== "macro" && token.type !== "html" && token.type !== "text") scanSelectors(content, token, at, emit, options);
20702
20935
  if (token.type === "variable") {
20703
20936
  if ((token.scope === "variable" || token.scope === "transient") && token.name) {
20704
20937
  const [name, ...path] = token.name.split(".");
@@ -20722,13 +20955,7 @@ function collectTokenRefs(content, tokens, at, emit, options) {
20722
20955
  }
20723
20956
  else if (token.type === "macro" && !token.isClose) {
20724
20957
  scanArgs(token, parametersOf(token.name), at, emit, options);
20725
- if (token.selectorsStart !== void 0 && token.selectorsEnd !== void 0) {
20726
- const from = token.selectorsStart;
20727
- scanInterpolations(content.slice(from, token.selectorsEnd), (i) => at(from + i), emit, {
20728
- ...options,
20729
- storeVarMacros: NO_STORE_VAR_MACROS
20730
- });
20731
- }
20958
+ scanSelectors(content, token, at, emit, options);
20732
20959
  if (storeVarMacros.has(token.name.toLowerCase())) {
20733
20960
  const lead = token.rawArgs.length - token.rawArgs.trimStart().length;
20734
20961
  const first = token.rawArgs.trim().split(/\s+/)[0] ?? "";
@@ -23256,6 +23483,18 @@ function stopWithErrors(errors) {
23256
23483
  throw new Error(`spindle: ${errors.length} validation error(s):\n${errors.join("\n")}`);
23257
23484
  }
23258
23485
  /**
23486
+ * The result of `parse`, or, when it throws, the error shown on the page
23487
+ * instead of the story (a declaration that does not evaluate, a widget passage
23488
+ * that does not parse), prefixed with `where` when that is not in the message.
23489
+ */
23490
+ function orStop(parse, where = "") {
23491
+ try {
23492
+ return parse();
23493
+ } catch (err) {
23494
+ return stopWithErrors([where + errorMessage(err)]);
23495
+ }
23496
+ }
23497
+ /**
23259
23498
  * The undeclared variables the passages refer to, with the macros known now:
23260
23499
  * the input macros that bind a variable (registered by author JS or
23261
23500
  * StoryInit) and the roles of their parameters decide what is a reference.
@@ -23312,14 +23551,14 @@ function boot() {
23312
23551
  if (root) renderErrors(root, [msg]);
23313
23552
  throw new Error(`spindle: ${msg}`);
23314
23553
  }
23315
- const schema = parseStoryVariables(storyVarsPassage.content);
23316
- for (const name of blockWidgetNames(storyData.passages.values())) registerBlockMacro(name);
23554
+ const schema = orStop(() => parseStoryVariables(storyVarsPassage.content));
23555
+ for (const name of orStop(() => blockWidgetNames(storyData.passages.values()))) registerBlockMacro(name);
23317
23556
  const errors = variableErrors(storyData, schema);
23318
23557
  errors.push(...markupErrors(storyData, (p) => p.name === "StoryInit"));
23319
23558
  let transientDefaults = {};
23320
23559
  const storyTransientsPassage = storyData.passages.get("StoryTransients");
23321
23560
  if (storyTransientsPassage) {
23322
- const transientSchema = parseStoryVariables(storyTransientsPassage.content, "%");
23561
+ const transientSchema = orStop(() => parseStoryVariables(storyTransientsPassage.content, "%"));
23323
23562
  for (const name of transientSchema.keys()) if (schema.has(name)) errors.push(`StoryTransients: Variable "${name}" is already declared in StoryVariables. Names must be unique across scopes.`);
23324
23563
  transientDefaults = extractDefaults(transientSchema);
23325
23564
  }
@@ -23328,7 +23567,7 @@ function boot() {
23328
23567
  setDeclaredVariables(Object.keys(defaults), Object.keys(transientDefaults));
23329
23568
  useStoryStore.getState().init(storyData, defaults, transientDefaults);
23330
23569
  enterRuntimePhase();
23331
- for (const [, passage] of storyData.passages) if (passage.tags.includes("widget")) registerWidgetDefinitions(parseMarkup(passage.content), passage.name);
23570
+ for (const [, passage] of storyData.passages) if (passage.tags.includes("widget")) orStop(() => registerWidgetDefinitions(parseMarkup(passage.content), passage.name), `Passage "${passage.name}": `);
23332
23571
  initializeStory(loadSession(storyData.ifid));
23333
23572
  const markup = [...variableErrors(storyData, schema), ...markupErrors(storyData, (p) => p.name !== "StoryInit")];
23334
23573
  if (markup.length > 0) stopWithErrors(markup);