@rohal12/spindle 0.59.30 → 0.59.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.
@@ -3333,6 +3333,9 @@ var NameSet = class {
3333
3333
  delete(name) {
3334
3334
  this.names.delete(name);
3335
3335
  }
3336
+ clear() {
3337
+ this.names.clear();
3338
+ }
3336
3339
  };
3337
3340
  //#endregion
3338
3341
  //#region src/markup/ast.ts
@@ -3357,6 +3360,12 @@ var BLOCK_MACROS = new NameSet([
3357
3360
  "span",
3358
3361
  "nobr"
3359
3362
  ]);
3363
+ /**
3364
+ * The block widgets (those whose body renders `{@children}`), kept apart from
3365
+ * BLOCK_MACROS: a widget is redefined, so it is a block only while its latest
3366
+ * definition is, whereas the macros in BLOCK_MACROS stay blocks.
3367
+ */
3368
+ var BLOCK_WIDGETS = new NameSet();
3360
3369
  /** Macros whose body is JavaScript source, kept verbatim. */
3361
3370
  var RAW_BODY_MACROS = /* @__PURE__ */ new Set(["do"]);
3362
3371
  /** Whether a macro's body is JavaScript, kept verbatim (`{do}`). */
@@ -3365,7 +3374,7 @@ function isRawMacro(name) {
3365
3374
  }
3366
3375
  /** Whether a macro takes a body closed by `{/name}`. */
3367
3376
  function isBlockMacro(name) {
3368
- return BLOCK_MACROS.has(name);
3377
+ return BLOCK_MACROS.has(name) || BLOCK_WIDGETS.has(name);
3369
3378
  }
3370
3379
  /** Macros whose branches render as one: the node holds one per branch. */
3371
3380
  var BRANCHING = /* @__PURE__ */ new Set([
@@ -8950,15 +8959,15 @@ var SigilParser = class extends Parser {
8950
8959
  }
8951
8960
  const self = this;
8952
8961
  const prevType = self.type;
8953
- this.prevValue = self.value;
8954
- const property = type.keyword !== void 0 && (prevType === types$1.dot || prevType === types$1.questionDot) || (type === types$1._function || type === types$1._class) && notABody(self.input, self.pos);
8962
+ this.prevValue = isMemberDot(this.typeBeforeLast) ? void 0 : self.value;
8963
+ const property = type.keyword !== void 0 && isMemberDot(prevType) || (type === types$1._function || type === types$1._class) && notABody(self.input, self.pos);
8955
8964
  if (!property && (type === types$1._function || type === types$1._class)) {
8956
8965
  const before = type === types$1._function && prevType === types$1.name && this.prevValue === "async" && !this.breakHere ? this.typeBeforeLast : prevType;
8957
8966
  self.type = before === types$1.colon && this.colonEndsTernary ? types$1.parenL : before;
8958
8967
  }
8959
8968
  this.typeBeforeLast = prevType;
8960
8969
  super.finishToken(property ? types$1.name : type, value);
8961
- if (type === types$1.name && value === "of" && (prevType === types$1._const || prevType === types$1._var || this.prevValue === "let")) self.exprAllowed = false;
8970
+ if (type === types$1.name && (isMemberDot(prevType) || value === "of" && (prevType === types$1._const || prevType === types$1._var || this.prevValue === "let"))) self.exprAllowed = false;
8962
8971
  }
8963
8972
  braceIsBlock(prevType) {
8964
8973
  if (prevType === types$1.colon && this.colonEndsTernary) return false;
@@ -9000,6 +9009,10 @@ function transientAssignment(p, end) {
9000
9009
  return false;
9001
9010
  }
9002
9011
  }
9012
+ /** Is `type` the `.` or `?.` of a member access? */
9013
+ function isMemberDot(type) {
9014
+ return type === types$1.dot || type === types$1.questionDot;
9015
+ }
9003
9016
  var OPENERS = /* @__PURE__ */ new Set([
9004
9017
  types$1.parenL,
9005
9018
  types$1.bracketL,
@@ -9135,6 +9148,10 @@ function bracketsBefore(src, pos) {
9135
9148
  * sigil variable declared (`let _x`) or used as a property name (`a.@x`).
9136
9149
  */
9137
9150
  function parseCode(src, goal = "expression") {
9151
+ return parseWithAst(src, goal).out;
9152
+ }
9153
+ /** What parseCode finds in `src`, and the syntax tree it is in. */
9154
+ function parseWithAst(src, goal) {
9138
9155
  const percentAt = /* @__PURE__ */ new Map();
9139
9156
  for (;;) {
9140
9157
  const transientsRead = /* @__PURE__ */ new Set();
@@ -9146,7 +9163,10 @@ function parseCode(src, goal = "expression") {
9146
9163
  };
9147
9164
  walk(ast, src, out, false, false);
9148
9165
  out.refs.sort((a, b) => a.start - b.start);
9149
- return out;
9166
+ return {
9167
+ out,
9168
+ ast
9169
+ };
9150
9170
  } catch (error) {
9151
9171
  const pos = error.pos ?? -1;
9152
9172
  if (!(error instanceof SyntaxError) || percentAt.has(pos)) throw error;
@@ -11203,7 +11223,9 @@ function tokenTextContainsChildren(token, parametersOf = registeredParameters, s
11203
11223
  /**
11204
11224
  * Recursively scan an AST node array for a VariableNode
11205
11225
  * with scope 'local' and name 'children' (@children), in the content and in
11206
- * the attributes and labels that render markup.
11226
+ * the attributes and labels that render markup. A nested `{widget}` is not
11227
+ * entered: its slot belongs to the innermost widget (as widgetDefinitions
11228
+ * reads it), and a definition renders nothing of its own.
11207
11229
  */
11208
11230
  function astContainsChildren(nodes, parametersOf = registeredParameters) {
11209
11231
  for (const node of nodes) {
@@ -11221,6 +11243,7 @@ function astContainsChildren(nodes, parametersOf = registeredParameters) {
11221
11243
  }, parametersOf) || astContainsChildren(node.children, parametersOf)) return true;
11222
11244
  }
11223
11245
  if (node.type === "macro") {
11246
+ if (node.name.toLowerCase() === "widget") continue;
11224
11247
  if (tokenTextContainsChildren({
11225
11248
  isClose: false,
11226
11249
  nameStart: 0,
@@ -11346,6 +11369,19 @@ function widgetDefinitions$1(passages, parametersOf = registeredParameters) {
11346
11369
  }
11347
11370
  return found;
11348
11371
  }
11372
+ /**
11373
+ * The definitions the runtime ends up with, one per widget name: the closed
11374
+ * ones, the latest winning as registration replaces a widget. The widget
11375
+ * passages register before StoryInit runs, so StoryInit's definitions come
11376
+ * last whatever the passage order. Read with the flat scanner, so a nested
11377
+ * definition owns its own `{@children}`.
11378
+ */
11379
+ function effectiveWidgetDefinitions(definitions) {
11380
+ const inInit = (def) => def.passage === "StoryInit";
11381
+ const effective = new NameMap();
11382
+ for (const def of [...definitions.filter((def) => !inInit(def)), ...definitions.filter(inInit)]) if (def.closeStart !== void 0) effective.set(def.name, def);
11383
+ return [...effective.values()];
11384
+ }
11349
11385
  //#endregion
11350
11386
  //#region src/markup/validate.ts
11351
11387
  /**
@@ -12478,6 +12514,21 @@ var DECLARATIONS = /* @__PURE__ */ new Set([
12478
12514
  "let",
12479
12515
  "var"
12480
12516
  ]);
12517
+ /** After a name: `=`, `+=`, `&&=`, `++` and the other ways to give it a value. */
12518
+ var WRITE_RE = /(?:(?:\*\*|<<|>>>?|&&|\|\||\?\?|[-+*/%&|^])=(?!=)|=(?![=>])|\+\+|--)/y;
12519
+ /** A call of these on `x.list` changes `x`; as does one of these on `x` itself. */
12520
+ var MUTATORS = /* @__PURE__ */ new Set([
12521
+ "push",
12522
+ "pop",
12523
+ "shift",
12524
+ "unshift",
12525
+ "splice",
12526
+ "sort",
12527
+ "reverse",
12528
+ "fill",
12529
+ "copyWithin"
12530
+ ]);
12531
+ var MUTATING_CALL_RE = /(?:Object|Reflect)\s*\.\s*(?:assign|defineProperty|defineProperties|setPrototypeOf|set|deleteProperty)\s*$/;
12481
12532
  /** Before a name in a pattern or parameter list, by the group around it. */
12482
12533
  var BEFORE_BINDING = {
12483
12534
  "{": "{,:",
@@ -12504,21 +12555,32 @@ var Bindings = class {
12504
12555
  * (`const`, `let` or `var`), when that is the declaration it names: the
12505
12556
  * innermost one in the scope of the use, that comes before it unless the
12506
12557
  * use is in a function of its own. `undefined` if it names another value,
12507
- * or more than one declaration is the innermost.
12558
+ * more than one declaration is the innermost, or the value is written to
12559
+ * (given another, or one of its `properties` set) where the use can follow.
12508
12560
  */
12509
- objectOf(name, at) {
12561
+ objectOf(name, at, properties) {
12510
12562
  const { code } = this;
12511
12563
  const escaped = name.replace(/\$/g, "\\$");
12512
12564
  const bindings = [];
12513
12565
  for (const m of code.matchAll(new RegExp(`(?<![\\w$])${escaped}(?![\\w$])`, "g"))) {
12514
- const binding = this.bindingAt(m.index, name.length);
12566
+ const binding = this.bindingAt(m.index, name.length, properties);
12515
12567
  if (binding) bindings.push(binding);
12516
12568
  }
12569
+ const declarations = bindings.filter((b) => !b.write);
12570
+ for (const b of bindings) if (b.write) b.scope = this.innermostVisible(declarations, b.at)[0]?.scope ?? this.top;
12571
+ const found = this.innermostVisible(bindings, at);
12572
+ return found.length === 1 ? found[0].object : void 0;
12573
+ }
12574
+ /**
12575
+ * Of the `bindings` the use at `at` can see, those in the innermost scope:
12576
+ * the scope is the use's, and a binding comes before it, unless the use is
12577
+ * in a function of its own.
12578
+ */
12579
+ innermostVisible(bindings, at) {
12517
12580
  const useFunction = this.functionStart(at);
12518
12581
  const visible = bindings.filter((b) => b.scope.start <= at && at < b.scope.end && (!b.ordered || b.at < at || this.functionStart(b.at) !== useFunction));
12519
12582
  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;
12583
+ return visible.filter((b) => b.scope.start === innermost);
12522
12584
  }
12523
12585
  readGroups() {
12524
12586
  const { code, groups } = this;
@@ -12554,7 +12616,20 @@ var Bindings = class {
12554
12616
  if (g.open < index && index < g.close && (!kind || g.kind === kind)) return g;
12555
12617
  }
12556
12618
  }
12557
- /** Whether the `{` group is the body of a function (or method). */
12619
+ /**
12620
+ * The word before a `(` group (`if`, `for`, `catch`, a function's name…);
12621
+ * `for await` is `for`.
12622
+ */
12623
+ ownerWord(paren) {
12624
+ const word = this.wordBefore(this.backFrom(paren.open) + 1);
12625
+ if (word.text !== "await") return word.text;
12626
+ const before = this.wordBefore(this.backFrom(word.start) + 1).text;
12627
+ return before === "for" ? before : word.text;
12628
+ }
12629
+ /**
12630
+ * Whether the `{` group is the body of a function (or method), or of a
12631
+ * class static block, which has the scope of a `var` of its own.
12632
+ */
12558
12633
  isFunctionBody(group) {
12559
12634
  if (group.kind !== "{") return false;
12560
12635
  let known = this.functions.get(group);
@@ -12563,9 +12638,8 @@ var Bindings = class {
12563
12638
  const { code } = this;
12564
12639
  if (code.charAt(before) === ")") {
12565
12640
  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) === "=>";
12641
+ known = !!params && !BLOCK_WORDS.has(this.ownerWord(params));
12642
+ } else known = code.slice(before - 1, before + 1) === "=>" || this.wordBefore(before + 1).text === "static";
12569
12643
  this.functions.set(group, known);
12570
12644
  }
12571
12645
  return known;
@@ -12590,9 +12664,56 @@ var Bindings = class {
12590
12664
  const start = this.functionStart(index);
12591
12665
  return start < 0 ? this.top : this.scopeOf(this.byOpen.get(start));
12592
12666
  }
12593
- /** What the declaration keyword `word` (`var`, `let`, `const`) declares for. */
12667
+ /**
12668
+ * What the declaration keyword `word` (`var`, `let`, `const`) declares for:
12669
+ * the function for `var`, the block for the others, or the `for` statement,
12670
+ * header and body, when the declaration is in its header.
12671
+ */
12594
12672
  declaredIn(word) {
12595
- return word.text === "var" ? this.functionAt(word.start) : this.blockAt(word.start);
12673
+ if (word.text === "var") return this.functionAt(word.start);
12674
+ const group = this.innermost(word.start);
12675
+ if (group?.kind === "(" && this.ownerWord(group) === "for") return {
12676
+ start: group.open,
12677
+ end: this.statementEnd(group.close + 1)
12678
+ };
12679
+ return this.blockAt(word.start);
12680
+ }
12681
+ /**
12682
+ * Where the statement that starts at `from` ends: a block, the body of an
12683
+ * `if`, `for`, `while` or `with` (and the `else` of an `if`), or else up to
12684
+ * a `;`, the closer of the group it is in, or a line break that is followed
12685
+ * by a statement of its own.
12686
+ */
12687
+ statementEnd(from) {
12688
+ const { code } = this;
12689
+ const start = this.forward(from);
12690
+ const block = this.byOpen.get(start);
12691
+ if (block?.kind === "{") return Math.min(block.close + 1, code.length);
12692
+ const word = this.wordAt(start);
12693
+ if (/^(?:if|for|while|with)$/.test(word)) {
12694
+ let paren = this.forward(start + word.length);
12695
+ if (word === "for" && this.wordAt(paren) === "await") paren = this.forward(paren + 5);
12696
+ const header = this.byOpen.get(paren);
12697
+ if (header) {
12698
+ const end = this.statementEnd(header.close + 1);
12699
+ const next = this.forward(end);
12700
+ return word === "if" && this.wordAt(next) === "else" ? this.statementEnd(next + 4) : end;
12701
+ }
12702
+ }
12703
+ for (let end = start; end < code.length; end++) {
12704
+ const c = code.charAt(end);
12705
+ if (c === ";") return end + 1;
12706
+ if (CLOSERS[c]) return end;
12707
+ const group = this.byOpen.get(end);
12708
+ if (group) end = group.close;
12709
+ else if (c === "\n" && this.startsStatement(end)) return end;
12710
+ }
12711
+ return code.length;
12712
+ }
12713
+ /** Whether the line break at `index` ends a statement, and one follows. */
12714
+ startsStatement(index) {
12715
+ const next = this.forward(index);
12716
+ return /[\w$)\]}]/.test(this.code.charAt(this.backFrom(index))) && isWordChar(this.code.charAt(next)) && !/^(?:in|of|instanceof|else)$/.test(this.wordAt(next));
12596
12717
  }
12597
12718
  /** What a parameter list, whose `)` is `close`, is the scope of: the body. */
12598
12719
  bodyAfter(close) {
@@ -12627,6 +12748,10 @@ var Bindings = class {
12627
12748
  while (i < this.code.length && /\s/.test(this.code.charAt(i))) i++;
12628
12749
  return i;
12629
12750
  }
12751
+ /** The word that starts at `index` (empty if there is none). */
12752
+ wordAt(index) {
12753
+ return this.code.slice(index, this.nameEnd(index));
12754
+ }
12630
12755
  /** The word that ends before `end`, and where it starts. */
12631
12756
  wordBefore(end) {
12632
12757
  let start = end;
@@ -12636,19 +12761,23 @@ var Bindings = class {
12636
12761
  start
12637
12762
  };
12638
12763
  }
12639
- /** What the name of `length` characters at `at` declares, if it does. */
12640
- bindingAt(at, length) {
12764
+ /**
12765
+ * What the name of `length` characters at `at` declares, or writes to, if it
12766
+ * does. A write to a `properties` of it counts, one to another does not.
12767
+ */
12768
+ bindingAt(at, length, properties) {
12641
12769
  const { code } = this;
12642
12770
  let before = this.backFrom(at);
12643
12771
  const spread = code.slice(before - 2, before + 1) === "...";
12644
12772
  if (code.charAt(before) === "." && !spread) return void 0;
12645
12773
  if (spread) before = this.backFrom(before - 2);
12646
12774
  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);
12775
+ return this.declarationAt(at, before, spread) ?? this.patternAt(at, before, after) ?? this.arrowParameterAt(at, after) ?? (spread ? void 0 : this.writeAt(at, before, after, properties));
12648
12776
  }
12649
- /** `const x`, `let x`, `var x`, `function x`, `class x`. */
12777
+ /** `const x`, `let x`, `var x`, a later `y` of `let x, y`, `function x`, `class x`. */
12650
12778
  declarationAt(at, before, spread) {
12651
- const word = spread ? void 0 : this.wordBefore(before + 1);
12779
+ if (spread) return void 0;
12780
+ const word = this.code.charAt(before) === "," ? this.listKeyword(before) : this.wordBefore(before + 1);
12652
12781
  if (!word || this.code.charAt(word.start - 1) === ".") return void 0;
12653
12782
  if (DECLARATIONS.has(word.text)) {
12654
12783
  const scope = this.declaredIn(word);
@@ -12667,36 +12796,75 @@ var Bindings = class {
12667
12796
  ordered: word.text === "class"
12668
12797
  };
12669
12798
  }
12799
+ /**
12800
+ * The `const`, `let` or `var` of the declaration list that the `,` at
12801
+ * `comma` is in (`let a = 1, b`, also in a `for` header): the nearest one
12802
+ * before it in the same brackets, that no `;` is between.
12803
+ */
12804
+ listKeyword(comma) {
12805
+ const { code } = this;
12806
+ const floor = this.innermost(comma)?.open ?? -1;
12807
+ for (let i = comma - 1; i > floor; i--) {
12808
+ const c = code.charAt(i);
12809
+ if (c === ";") return void 0;
12810
+ const group = CLOSERS[c] ? this.byClose.get(i) : void 0;
12811
+ if (group) i = group.open;
12812
+ else if (isWordChar(c)) {
12813
+ const word = this.wordBefore(i + 1);
12814
+ if (DECLARATIONS.has(word.text) && code.charAt(word.start - 1) !== ".") return word;
12815
+ i = word.start;
12816
+ }
12817
+ }
12818
+ }
12670
12819
  nameEnd(at) {
12671
12820
  let end = at;
12672
12821
  while (isWordChar(this.code.charAt(end))) end++;
12673
12822
  return end;
12674
12823
  }
12675
- /** A name in a pattern (`const { x } = …`) or a parameter list. */
12824
+ /**
12825
+ * A name in a pattern (`const { x } = …`) or a parameter list; or one a
12826
+ * pattern assigns to (`({ x } = …)`).
12827
+ */
12676
12828
  patternAt(at, before, after) {
12677
12829
  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) {
12830
+ const inner = this.innermost(at);
12831
+ if (!inner || !BEFORE_BINDING[inner.kind].includes(code.charAt(before))) return;
12832
+ if (inner.kind === "{" && code.charAt(after) === ":") return void 0;
12833
+ let pattern;
12834
+ for (let group = inner; group; group = group.parent) {
12682
12835
  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 && {
12836
+ const scope = NOT_PARAMETERS.has(this.ownerWord(group)) ? void 0 : this.bodyAfter(group.close);
12837
+ return scope ? {
12686
12838
  scope,
12687
12839
  at,
12688
12840
  ordered: false
12689
- };
12841
+ } : this.patternWriteAt(at, pattern);
12690
12842
  }
12843
+ pattern = group;
12691
12844
  const open = this.backFrom(group.open);
12692
- const word = this.wordBefore(open + 1);
12693
- if (DECLARATIONS.has(word.text)) return {
12845
+ const word = code.charAt(open) === "," ? this.listKeyword(open) : this.wordBefore(open + 1);
12846
+ if (word && DECLARATIONS.has(word.text)) return {
12694
12847
  scope: this.declaredIn(word),
12695
12848
  at,
12696
12849
  ordered: true
12697
12850
  };
12698
- if (!"({[,:".includes(code.charAt(open))) return void 0;
12851
+ if (!"({[,:".includes(code.charAt(open))) break;
12699
12852
  }
12853
+ return this.patternWriteAt(at, pattern);
12854
+ }
12855
+ /** `[x] = …`, `({ x } = …)`, `for ({ x } of …)`: a name that is assigned to. */
12856
+ patternWriteAt(at, pattern) {
12857
+ if (!pattern) return void 0;
12858
+ const { code } = this;
12859
+ const open = this.backFrom(pattern.open);
12860
+ if (/[\w$)\]]/.test(code.charAt(open))) return void 0;
12861
+ const next = this.forward(pattern.close + 1);
12862
+ return pattern.parent?.kind === "(" && this.ownerWord(pattern.parent) === "for" && /^(?:of|in)(?![\w$])/.test(code.slice(next)) || this.isWriteOperator(next) ? {
12863
+ scope: this.top,
12864
+ at,
12865
+ ordered: true,
12866
+ write: true
12867
+ } : void 0;
12700
12868
  }
12701
12869
  /** `x => …`. */
12702
12870
  arrowParameterAt(at, after) {
@@ -12706,16 +12874,72 @@ var Bindings = class {
12706
12874
  ordered: false
12707
12875
  } : void 0;
12708
12876
  }
12709
- /** `x = …`: a declaration in a list, or an assignment: a value not known. */
12710
- assignmentAt(at, before, after) {
12877
+ /** Whether an assignment, `++` or `--` operator is at `index`. */
12878
+ isWriteOperator(index) {
12879
+ WRITE_RE.lastIndex = index;
12880
+ return WRITE_RE.test(this.code);
12881
+ }
12882
+ /**
12883
+ * `x = …`, `x += …`, `x++`, `for (x of …)`, and the writes to what it
12884
+ * holds that change the config it is (`x.name = …`, `delete x.name`,
12885
+ * `x.parameters.push(…)`, `Object.assign(x, …)`): a value not known.
12886
+ */
12887
+ writeAt(at, before, after, properties) {
12711
12888
  const { code } = this;
12712
- if (code.charAt(after) !== "=" || "=>".includes(code.charAt(after + 1))) return;
12889
+ if (this.isClassBody(this.innermost(at))) return void 0;
12890
+ const { keys, end } = this.memberChain(after);
12891
+ const written = ["++", "--"].includes(code.slice(before - 1, before + 1)) || this.isWriteOperator(end) || keys.length > 0 && (this.wordBefore(before + 1).text === "delete" || keys.length > 1 && code.charAt(end) === "(" && MUTATORS.has(keys[keys.length - 1])) || keys.length === 0 && this.isForTarget(before, end) || keys.length === 0 && this.isMutatedArgument(before, end);
12892
+ const first = keys[0];
12893
+ if (!written || first && !properties.has(first)) return void 0;
12713
12894
  return {
12714
- scope: code.charAt(before) === "," ? this.functionAt(at) : this.blockAt(at),
12895
+ scope: this.top,
12715
12896
  at,
12716
- ordered: true
12897
+ ordered: true,
12898
+ write: true
12717
12899
  };
12718
12900
  }
12901
+ /** Whether the group is the body of a class: its members are its own. */
12902
+ isClassBody(group) {
12903
+ if (group?.kind !== "{") return false;
12904
+ let start = group.open;
12905
+ while (start > 0 && !"{};".includes(this.code.charAt(start - 1))) start--;
12906
+ return /(?<![\w$.])class(?![\w$])/.test(this.code.slice(start, group.open));
12907
+ }
12908
+ /**
12909
+ * The properties read from a name at `from` (`.a.b[c]`, a key is null if
12910
+ * computed), and where the chain ends.
12911
+ */
12912
+ memberChain(from) {
12913
+ const { code } = this;
12914
+ const keys = [];
12915
+ let end = from;
12916
+ for (;;) {
12917
+ let i = end;
12918
+ if (code.startsWith("?.", i)) i += 2;
12919
+ else if (code.charAt(i) === "." && code.charAt(i + 1) !== ".") i++;
12920
+ else if (code.charAt(i) !== "[") break;
12921
+ i = this.forward(i);
12922
+ const bracket = code.charAt(i) === "[" ? this.byOpen.get(i) : void 0;
12923
+ const key = bracket ? "" : this.wordAt(i);
12924
+ if (!bracket && !key) break;
12925
+ keys.push(bracket ? null : key);
12926
+ end = this.forward(bracket ? bracket.close + 1 : i + key.length);
12927
+ }
12928
+ return {
12929
+ keys,
12930
+ end
12931
+ };
12932
+ }
12933
+ /** `for (x of …)`: the name is assigned each item. */
12934
+ isForTarget(before, end) {
12935
+ const group = this.byOpen.get(before);
12936
+ return group?.kind === "(" && this.ownerWord(group) === "for" && /^(?:of|in)(?![\w$])/.test(this.code.slice(end));
12937
+ }
12938
+ /** `Object.assign(x, …)` and the like: the name is the first argument. */
12939
+ isMutatedArgument(before, end) {
12940
+ const group = this.byOpen.get(before);
12941
+ return group?.kind === "(" && ",)".includes(this.code.charAt(end)) && MUTATING_CALL_RE.test(this.code.slice(0, group.open));
12942
+ }
12719
12943
  };
12720
12944
  //#endregion
12721
12945
  //#region src/macro-discovery.ts
@@ -12728,6 +12952,20 @@ var Bindings = class {
12728
12952
  var CALL_RE = /(?<![\w$])defineMacro\s*\(\s*/g;
12729
12953
  var IDENTIFIER_ARGUMENT_RE = /^([A-Za-z_$][\w$]*)\s*[,)]/;
12730
12954
  var WORD_RE = /[A-Za-z_]+/y;
12955
+ /** Before the key of a member that is an accessor, an async method or a generator. */
12956
+ var MODIFIER_RE = /^(?:get|set|async)?$/;
12957
+ var KEY_START_RE = /[\w$'"[.*]/;
12958
+ /** The properties of a config that its macro is read from. */
12959
+ var CONFIG_KEYS = /* @__PURE__ */ new Set([
12960
+ "name",
12961
+ "block",
12962
+ "subMacros",
12963
+ "parameters",
12964
+ "interpolate",
12965
+ "storeVar",
12966
+ "merged",
12967
+ "description"
12968
+ ]);
12731
12969
  /** The literals of a config object, read to the end of what is static. */
12732
12970
  var ConfigReader = class extends Reader {
12733
12971
  constructor(..._args) {
@@ -12827,18 +13065,47 @@ var ConfigReader = class extends Reader {
12827
13065
  }
12828
13066
  record() {
12829
13067
  const props = /* @__PURE__ */ new Map();
12830
- return this.elements("}", () => {
12831
- const key = this.key();
12832
- this.space();
12833
- if (key !== void 0 && this.src.charAt(this.i) === ":") {
12834
- this.i++;
12835
- props.set(key, this.member());
12836
- } else if (!this.skipMember()) this.fail();
12837
- }) ? {
13068
+ return this.elements("}", () => this.property(props)) ? {
12838
13069
  type: "object",
12839
13070
  props
12840
13071
  } : void 0;
12841
13072
  }
13073
+ /**
13074
+ * A member: what it sets replaces an earlier member of the key. One that is
13075
+ * not a literal (a method, an accessor, a shorthand) sets its key to a
13076
+ * value not known; a spread of an object literal sets the members of it, a
13077
+ * spread of anything else or a computed key can set any key.
13078
+ */
13079
+ property(props) {
13080
+ const { src } = this;
13081
+ if (src.startsWith("...", this.i)) {
13082
+ this.i += 3;
13083
+ this.space();
13084
+ const spread = src.charAt(this.i) === "{" ? this.record() : void 0;
13085
+ this.space();
13086
+ if (spread?.type === "object" && this.atElementEnd()) {
13087
+ for (const [key, value] of spread.props) props.set(key, value);
13088
+ return;
13089
+ }
13090
+ props.clear();
13091
+ } else {
13092
+ let key = this.key();
13093
+ this.space();
13094
+ if (key !== void 0 && src.charAt(this.i) === ":") {
13095
+ this.i++;
13096
+ props.set(key, this.member());
13097
+ return;
13098
+ }
13099
+ if (MODIFIER_RE.test(key ?? "") && KEY_START_RE.test(src.charAt(this.i))) {
13100
+ if (src.charAt(this.i) === "*") this.i++;
13101
+ this.space();
13102
+ key = this.key();
13103
+ }
13104
+ if (key === void 0) props.clear();
13105
+ else props.set(key, void 0);
13106
+ }
13107
+ if (!this.skipMember()) this.fail();
13108
+ }
12842
13109
  /** Stop reading: the text cannot be skipped through. */
12843
13110
  fail() {
12844
13111
  this.failed = true;
@@ -12931,14 +13198,16 @@ function macroOf(config) {
12931
13198
  * file) declare, in source order, read from the text without running it. A
12932
13199
  * call whose config is an object literal (or a variable declared as one with
12933
13200
  * `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,
13201
+ * pattern or an assignment of the name, or a write to a property the macro
13202
+ * is read from) and whose `name` is a string is found,
12935
13203
  * wherever it is, even inside a function. Only what is written out is read: a `block`,
12936
13204
  * `interpolate`, `storeVar` or `merged` that is a boolean literal, the
12937
13205
  * strings of `subMacros`, and `parameters` when every one is an object
12938
13206
  * literal with a string `name` and `type` (and `holds`, `required`,
12939
13207
  * `description` and nested `parameters` of options when written out as such);
12940
- * a list with a parameter that is not, or that `defineMacro` would refuse,
12941
- * declares none. It is tolerant: text it cannot read is skipped, and it
13208
+ * a later member that sets one of these again (a spread, a method, an
13209
+ * accessor, a computed key) replaces it; a list with a parameter that is not,
13210
+ * or that `defineMacro` would refuse, declares none. It is tolerant: text it cannot read is skipped, and it
12942
13211
  * throws for none.
12943
13212
  */
12944
13213
  function discoverMacros(source) {
@@ -12951,7 +13220,7 @@ function discoverMacros(source) {
12951
13220
  const variable = IDENTIFIER_ARGUMENT_RE.exec(code.slice(at))?.[1];
12952
13221
  if (!variable) continue;
12953
13222
  bindings ?? (bindings = new Bindings(code));
12954
- const object = bindings.objectOf(variable, call.index);
13223
+ const object = bindings.objectOf(variable, call.index, CONFIG_KEYS);
12955
13224
  if (object === void 0) continue;
12956
13225
  at = object;
12957
13226
  }
@@ -13075,7 +13344,7 @@ function validateStoryMarkup(passages, macros, options = {}) {
13075
13344
  const all = [...macros];
13076
13345
  const known = /* @__PURE__ */ new Set();
13077
13346
  const widgets = widgetDefinitions$1(list, parameterLookup(all));
13078
- const blocks = new Set(widgets.filter((def) => def.block && def.closeStart !== void 0).map((def) => def.name.toLowerCase()));
13347
+ const blocks = new Set(effectiveWidgetDefinitions(widgets).filter((def) => def.block).map((def) => def.name.toLowerCase()));
13079
13348
  const parametersOf = parameterLookup([...all, ...definedWidgetMacros(widgets)]);
13080
13349
  for (const macro of all) {
13081
13350
  const name = macro.name.toLowerCase();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.59.30",
3
+ "version": "0.59.32",
4
4
  "type": "module",
5
5
  "description": "A Preact-based story format for Twine 2.",
6
6
  "license": "Unlicense",
@@ -15,7 +15,9 @@ export type ActionType =
15
15
  | 'restart'
16
16
  | 'save'
17
17
  | 'load'
18
- | 'dialog';
18
+ | 'dialog'
19
+ | 'open-dialog'
20
+ | 'close-dialog';
19
21
 
20
22
  export interface StoryAction {
21
23
  id: string;
@@ -60,6 +60,10 @@ async function executeStep(
60
60
  ): Promise<void> {
61
61
  if (step.set) {
62
62
  story.set(step.set);
63
+ // Controls shown or removed by the new values register (or go) when the
64
+ // UI has rendered, which is after set(): an assert, or an action in the
65
+ // same step, would otherwise see the previous step's controls (#514)
66
+ await story.waitForActions();
63
67
  }
64
68
 
65
69
  if (step.action !== undefined) {