@rohal12/spindle 0.62.6 → 0.62.7

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.
@@ -29,8 +29,10 @@ import {
29
29
  import { getMacroParameters, type ParameterDef } from './registry';
30
30
  import {
31
31
  MacroArgumentError,
32
+ parseBoundVariable,
32
33
  parseMacroArgs,
33
34
  type ArgSpans,
35
+ type BoundVariable,
34
36
  } from './components/macros/macro-args';
35
37
  import { NOT_MARKUP } from './markup/validate';
36
38
  import {
@@ -88,8 +90,6 @@ function declarationRegex(sigil: string): RegExp {
88
90
  }
89
91
  /** A `$name` or `%name` reference with its dot path, at a sigil the lexer found. */
90
92
  const VAR_PATH_RE = /([$%])(\w+(?:\.\w+)*)/y;
91
- /** Quoted first argument of an input macro naming a story variable. */
92
- const QUOTED_VAR_ARG_RE = /^["']\$(\w+(?:\.\w+)*)["']?$/;
93
93
 
94
94
  const VALID_VAR_TYPES = new Set<string>(['number', 'string', 'boolean']);
95
95
 
@@ -376,6 +376,12 @@ export interface VariableReference {
376
376
  /** Where `$a.b.c` is written in the markup, as `[start, end)` UTF-16 offsets. */
377
377
  start: number;
378
378
  end: number;
379
+ /**
380
+ * For an input binding, its decoded identity and editable root span.
381
+ * Offsets are into the markup, including inside escaped labels. The sigil
382
+ * is semantically `$` even when `binding.explicitSigil` is false.
383
+ */
384
+ binding?: BoundVariable;
379
385
  }
380
386
 
381
387
  /** What a check of a variable reference found wrong with it. */
@@ -753,6 +759,38 @@ function scanSelectors(
753
759
  );
754
760
  }
755
761
 
762
+ /** Read a runtime binding and map its editable spans into the passage. */
763
+ function boundReference(
764
+ token: Extract<Token, { type: 'macro' }>,
765
+ at: At,
766
+ all: boolean | undefined,
767
+ ): VariableReference | undefined {
768
+ try {
769
+ const binding = parseBoundVariable(token.rawArgs);
770
+ // Sigilless bindings are part of the complete editor list, but do not
771
+ // change which references story startup checks.
772
+ if (!binding || (!all && !binding.explicitSigil)) return undefined;
773
+ const mapped = {
774
+ ...binding,
775
+ start: at(token.argsStart + binding.start),
776
+ end: at(token.argsStart + binding.end),
777
+ nameStart: at(token.argsStart + binding.nameStart),
778
+ nameEnd: at(token.argsStart + binding.nameEnd),
779
+ };
780
+ return {
781
+ sigil: '$',
782
+ name: binding.name,
783
+ path: binding.path,
784
+ start: mapped.start,
785
+ end: mapped.end,
786
+ binding: mapped,
787
+ };
788
+ } catch {
789
+ // Tolerant tooling skips bindings the runtime rejects.
790
+ return undefined;
791
+ }
792
+ }
793
+
756
794
  /** Report the `$var` references in the tokens of `content`. */
757
795
  function collectTokenRefs(
758
796
  content: string,
@@ -805,33 +843,27 @@ function collectTokenRefs(
805
843
  else scanInterpolations(value, valueIn, emit, options);
806
844
  }
807
845
  } else if (token.type === 'macro' && !token.isClose) {
808
- scanArgs(token, parametersOf, at, emit, options);
846
+ const binding =
847
+ storeVarMacros.has(token.name.toLowerCase()) &&
848
+ !parametersOf.widget?.(token.name)
849
+ ? boundReference(token, at, options.all)
850
+ : undefined;
851
+ scanArgs(
852
+ token,
853
+ parametersOf,
854
+ at,
855
+ (ref) => {
856
+ // A parameterless custom storeVar macro also scans its arguments
857
+ // as code. Keep the binding's decoded path and span just once.
858
+ if (!binding || ref.start < binding.start || ref.start >= binding.end)
859
+ emit(ref);
860
+ },
861
+ options,
862
+ );
809
863
  // Selectors (`{.{$cls} button}`) are interpolated when rendered
810
864
  scanSelectors(content, token, at, emit, options);
811
865
 
812
- if (
813
- storeVarMacros.has(token.name.toLowerCase()) &&
814
- !parametersOf.widget?.(token.name)
815
- ) {
816
- const lead = token.rawArgs.length - token.rawArgs.trimStart().length;
817
- const first = token.rawArgs.trim().split(/\s+/)[0] ?? '';
818
- const quoted = QUOTED_VAR_ARG_RE.exec(first);
819
- if (quoted) {
820
- const [name, ...path] = quoted[1]!.split('.');
821
- const start = token.argsStart + lead + 1;
822
- emit({
823
- sigil: '$',
824
- name: name!,
825
- path,
826
- start: at(start),
827
- end: at(start + 1 + quoted[1]!.length),
828
- });
829
- }
830
- // Unquoted, the variable is an argument of no declared role
831
- else if (!/^["'`]/.test(first)) {
832
- scanCode(first, (i) => at(token.argsStart + lead + i), emit);
833
- }
834
- }
866
+ if (binding) emit(binding);
835
867
 
836
868
  if (executesRawBody(token.name, parametersOf)) {
837
869
  // A {do} body is JavaScript: scan its source text as code, however
package/src/tooling.ts CHANGED
@@ -134,11 +134,13 @@ export {
134
134
  export { splitIncludeFlag } from './components/macros/include-args';
135
135
  export {
136
136
  macroArgumentSpans,
137
+ parseBoundVariable,
137
138
  evaluatePassageName,
138
139
  passageTarget,
139
140
  } from './components/macros/macro-args';
140
141
  export type {
141
142
  MacroArgumentSpan,
143
+ BoundVariable,
142
144
  PassageTarget,
143
145
  } from './components/macros/macro-args';
144
146
  export { transform } from './transform';
@@ -407,13 +409,15 @@ function referenceOptions(
407
409
  * in source order, with the dotted path of fields accessed and where each is
408
410
  * written (offsets into `source`, UTF-16): `{$var}` displays, code in
409
411
  * expressions, conditions, `{do}` bodies and macro arguments, quoted names
410
- * bound by input macros, and the markup in labels and HTML attributes. Prose
412
+ * bound by input macros, and the markup in labels and HTML attributes. Input
413
+ * bindings include decoded root/path names and editable `binding` spans. Prose
411
414
  * is literal text. Malformed tags are skipped, so it reads half-typed markup.
412
415
  * The macros are those the markup may use, built-in and user-defined.
413
416
  *
414
417
  * These are the references the story start checks. With `all: true`, the
415
418
  * list is complete (the checked ones are among it, in source order): it also
416
- * has the variable a macro names in a `variable` parameter, such as the
419
+ * has sigilless input bindings (`{textbox "name"}`), the variable a macro
420
+ * names in a `variable` parameter, such as the
417
421
  * target of `{unset}` and `{computed}`, and the `{$name}` in the selectors of
418
422
  * a link, display or expression (`[[.c{$name} Go->T]]`). A rename or a find
419
423
  * of references needs these too.
@@ -96,6 +96,15 @@ type SameShape<A, B> = Same<A, B> extends true ? Same<keyof A, keyof B> : false;
96
96
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
97
97
  type AssertTrue<T extends readonly true[]> = T;
98
98
  type ToolingChecks = [
99
+ Same<
100
+ typeof SourceTooling.parseBoundVariable,
101
+ typeof PublishedTooling.parseBoundVariable
102
+ >,
103
+ SameShape<SourceTooling.BoundVariable, PublishedTooling.BoundVariable>,
104
+ SameShape<
105
+ SourceTooling.VariableReference,
106
+ PublishedTooling.VariableReference
107
+ >,
99
108
  Same<typeof SourceTooling.parseCode, typeof PublishedTooling.parseCode>,
100
109
  Same<
101
110
  typeof SourceTooling.macroArgumentSpans,
@@ -712,6 +712,12 @@ export interface VariableReference {
712
712
  /** Where `$a.b.c` is written: `[start, end)` UTF-16 offsets into the markup. */
713
713
  start: number;
714
714
  end: number;
715
+ /**
716
+ * For an input binding, its decoded identity and editable root span.
717
+ * Offsets are into the markup, including inside escaped labels. The sigil
718
+ * is semantically `$` even when `binding.explicitSigil` is false.
719
+ */
720
+ binding?: BoundVariable;
715
721
  }
716
722
 
717
723
  /**
@@ -749,10 +755,12 @@ export interface VariableDiagnostic {
749
755
  * The variable references the markup of a passage evaluates, in source order,
750
756
  * with their positions. Malformed tags are skipped. The macros are those the
751
757
  * markup may use (`builtinMacros` and the user-defined ones).
758
+ * Input bindings include decoded root/path names and editable `binding` spans.
752
759
  *
753
760
  * These are the references the story start checks. With `all: true` the list
754
761
  * is complete (the checked ones are among it, in source order): it also has
755
- * the variable a macro names in a `variable` parameter, such as the target of
762
+ * sigilless input bindings (`{textbox "name"}`), the variable a macro names
763
+ * in a `variable` parameter, such as the target of
756
764
  * `{unset}` and `{computed}`, and the `{$name}` in the selectors of a link,
757
765
  * display or expression (`[[.c{$name} Go->T]]`).
758
766
  */
@@ -1227,6 +1235,32 @@ export interface ParsedCode {
1227
1235
  */
1228
1236
  export declare function parseCode(src: string, goal?: JsGoal): ParsedCode;
1229
1237
 
1238
+ /** A runtime input binding, with UTF-16 spans into the raw arguments. */
1239
+ export interface BoundVariable {
1240
+ /** Decoded story-variable root and dot members. */
1241
+ name: string;
1242
+ path: string[];
1243
+ /** Whether the author wrote the optional literal `$` sigil. */
1244
+ explicitSigil: boolean;
1245
+ /** The binding spelling, excluding quotes and including any sigil. */
1246
+ start: number;
1247
+ end: number;
1248
+ /** Editable root spelling, excluding quotes, sigil and dot members. */
1249
+ nameStart: number;
1250
+ nameEnd: number;
1251
+ }
1252
+
1253
+ /**
1254
+ * Read the first term as the runtime's story-variable binding. Quotes are
1255
+ * stripped, but this is an identifier path, not a JavaScript string literal:
1256
+ * only identifier escapes are decoded. Returns null for a missing binding;
1257
+ * throws for rejected namespaces, invalid escapes and reserved path names.
1258
+ * All offsets are UTF-16 offsets into rawArgs.
1259
+ */
1260
+ export declare function parseBoundVariable(
1261
+ rawArgs: string,
1262
+ ): BoundVariable | null;
1263
+
1230
1264
  /** A consumed argument and its original parameter (including option keywords). */
1231
1265
  export interface MacroArgumentSpan {
1232
1266
  parameter: ParameterDef;