@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.
- package/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +79 -44
- package/dist/pkg/story-variables.js +156 -55
- package/dist/pkg/tooling.js +1 -0
- package/package.json +1 -1
- package/src/components/macros/macro-args.ts +84 -1
- package/src/define-macro.ts +3 -57
- package/src/story-variables.ts +58 -26
- package/src/tooling.ts +6 -2
- package/src/types-drift-check.ts +9 -0
- package/types/tooling.d.ts +35 -1
package/dist/pkg/headless.js
CHANGED
|
@@ -11787,6 +11787,53 @@ function readBoundVariable(rawArgs) {
|
|
|
11787
11787
|
const first = terms(src)[0];
|
|
11788
11788
|
return first ? slice(src, first) : "";
|
|
11789
11789
|
}
|
|
11790
|
+
/**
|
|
11791
|
+
* Read the first term as the runtime's story-variable binding. Quotes are
|
|
11792
|
+
* stripped, but this is an identifier path, not a JavaScript string literal:
|
|
11793
|
+
* only identifier escapes are decoded. Returns null for a missing binding;
|
|
11794
|
+
* throws for rejected namespaces, invalid escapes and reserved path names.
|
|
11795
|
+
* The full span excludes quotes and includes the sigil when written.
|
|
11796
|
+
*/
|
|
11797
|
+
function parseBoundVariable(rawArgs) {
|
|
11798
|
+
const term = readBoundVariable(rawArgs);
|
|
11799
|
+
if (!term) return null;
|
|
11800
|
+
const lead = rawArgs.length - rawArgs.trimStart().length;
|
|
11801
|
+
const offsets = [];
|
|
11802
|
+
for (let i = 0; i < term.length; i++) if (term[i] !== "\"" && term[i] !== "'") offsets.push(lead + i);
|
|
11803
|
+
const firstToken = term.replace(/["']/g, "");
|
|
11804
|
+
const kind = {
|
|
11805
|
+
"%": "transient variables",
|
|
11806
|
+
_: "temporary variables",
|
|
11807
|
+
"@": "locals"
|
|
11808
|
+
}[firstToken.charAt(0)];
|
|
11809
|
+
if (kind) throw new Error(`${kind} (${firstToken}) cannot be bound to input macros`);
|
|
11810
|
+
const explicitSigil = firstToken.startsWith("$");
|
|
11811
|
+
if (!explicitSigil && firstToken.split(".")[0].includes("\\")) throw new Error("Escaped binding identifiers require a literal $ story-variable sigil");
|
|
11812
|
+
const segments = firstToken.replace(/^\$/, "").split(".").map((segment, index) => {
|
|
11813
|
+
if (!segment.includes("\\")) return segment;
|
|
11814
|
+
if (index > 0) {
|
|
11815
|
+
const name = parseMemberIdentifier(segment);
|
|
11816
|
+
if (name === null) throw new Error("Invalid bound identifier");
|
|
11817
|
+
return name;
|
|
11818
|
+
}
|
|
11819
|
+
const reference = parseReferencePath(`$${segment}`);
|
|
11820
|
+
if (!reference || reference.nested) throw new Error("Invalid bound identifier");
|
|
11821
|
+
return reference.name;
|
|
11822
|
+
});
|
|
11823
|
+
if (segments.includes("__proto__")) throw new Error(`"$${segments.join(".")}" cannot be bound: ${RESERVED_NAME} is reserved`);
|
|
11824
|
+
const rootStart = explicitSigil ? 1 : 0;
|
|
11825
|
+
const rootEnd = firstToken.indexOf(".", rootStart);
|
|
11826
|
+
const nameEnd = rootEnd < 0 ? firstToken.length : rootEnd;
|
|
11827
|
+
return {
|
|
11828
|
+
name: segments[0],
|
|
11829
|
+
path: segments.slice(1),
|
|
11830
|
+
explicitSigil,
|
|
11831
|
+
start: offsets[0] ?? lead,
|
|
11832
|
+
end: offsets.length ? offsets[offsets.length - 1] + 1 : lead,
|
|
11833
|
+
nameStart: offsets[rootStart] ?? lead + rootStart,
|
|
11834
|
+
nameEnd: offsets[nameEnd - 1] === void 0 ? lead : offsets[nameEnd - 1] + 1
|
|
11835
|
+
};
|
|
11836
|
+
}
|
|
11790
11837
|
//#endregion
|
|
11791
11838
|
//#region src/components/macros/option-utils.ts
|
|
11792
11839
|
/** `$var "placeholder"`, the arguments of the input macros. */
|
|
@@ -21352,39 +21399,17 @@ function defineMacro(config, source = "builtin") {
|
|
|
21352
21399
|
ctx.evaluate = (expr) => evaluateDisplay(expr, [...merged], displayOnly);
|
|
21353
21400
|
}
|
|
21354
21401
|
if (config.storeVar) {
|
|
21355
|
-
const firstToken = readBoundVariable(props.rawArgs).replace(/["']/g, "");
|
|
21356
|
-
const kind = {
|
|
21357
|
-
"%": "transient variables",
|
|
21358
|
-
_: "temporary variables",
|
|
21359
|
-
"@": "locals"
|
|
21360
|
-
}[firstToken.charAt(0)];
|
|
21361
|
-
if (kind) return h("span", { class: "error" }, `{${config.name}}: ${kind} (${firstToken}) cannot be bound to input macros`);
|
|
21362
|
-
if (!firstToken.startsWith("$") && firstToken.split(".")[0].includes("\\")) return h(MacroError, {
|
|
21363
|
-
macro: config.name,
|
|
21364
|
-
error: /* @__PURE__ */ new Error("Escaped binding identifiers require a literal $ story-variable sigil")
|
|
21365
|
-
});
|
|
21366
21402
|
let segments;
|
|
21367
21403
|
try {
|
|
21368
|
-
|
|
21369
|
-
|
|
21370
|
-
if (index > 0) {
|
|
21371
|
-
const name = parseMemberIdentifier(segment);
|
|
21372
|
-
if (name === null) throw new Error("Invalid bound identifier");
|
|
21373
|
-
return name;
|
|
21374
|
-
}
|
|
21375
|
-
const reference = parseReferencePath(`$${segment}`);
|
|
21376
|
-
if (!reference || reference.nested) throw new Error("Invalid bound identifier");
|
|
21377
|
-
return reference.name;
|
|
21378
|
-
});
|
|
21404
|
+
const binding = parseBoundVariable(props.rawArgs);
|
|
21405
|
+
segments = binding ? [binding.name, ...binding.path] : [""];
|
|
21379
21406
|
} catch (error) {
|
|
21380
21407
|
return h(MacroError, {
|
|
21381
21408
|
macro: config.name,
|
|
21382
21409
|
error
|
|
21383
21410
|
});
|
|
21384
21411
|
}
|
|
21385
|
-
|
|
21386
|
-
if (segments.includes("__proto__")) return h("span", { class: "error" }, `{${config.name}}: "$${varExpr}" cannot be bound: ${RESERVED_NAME} is reserved`);
|
|
21387
|
-
ctx.varName = varExpr;
|
|
21412
|
+
ctx.varName = segments.join(".");
|
|
21388
21413
|
ctx.value = getByPath(useStoryFields("variables").variables, segments);
|
|
21389
21414
|
ctx.getValue = () => getByPath(readState().variables, segments);
|
|
21390
21415
|
ctx.setValue = displayOnly ? refuse : (value) => {
|
|
@@ -22794,8 +22819,6 @@ function declarationRegex(sigil) {
|
|
|
22794
22819
|
}
|
|
22795
22820
|
/** A `$name` or `%name` reference with its dot path, at a sigil the lexer found. */
|
|
22796
22821
|
var VAR_PATH_RE = /([$%])(\w+(?:\.\w+)*)/y;
|
|
22797
|
-
/** Quoted first argument of an input macro naming a story variable. */
|
|
22798
|
-
var QUOTED_VAR_ARG_RE = /^["']\$(\w+(?:\.\w+)*)["']?$/;
|
|
22799
22822
|
var VALID_VAR_TYPES = /* @__PURE__ */ new Set([
|
|
22800
22823
|
"number",
|
|
22801
22824
|
"string",
|
|
@@ -23175,6 +23198,30 @@ function scanSelectors(content, token, at, emit, options) {
|
|
|
23175
23198
|
storeVarMacros: NO_STORE_VAR_MACROS
|
|
23176
23199
|
});
|
|
23177
23200
|
}
|
|
23201
|
+
/** Read a runtime binding and map its editable spans into the passage. */
|
|
23202
|
+
function boundReference(token, at, all) {
|
|
23203
|
+
try {
|
|
23204
|
+
const binding = parseBoundVariable(token.rawArgs);
|
|
23205
|
+
if (!binding || !all && !binding.explicitSigil) return void 0;
|
|
23206
|
+
const mapped = {
|
|
23207
|
+
...binding,
|
|
23208
|
+
start: at(token.argsStart + binding.start),
|
|
23209
|
+
end: at(token.argsStart + binding.end),
|
|
23210
|
+
nameStart: at(token.argsStart + binding.nameStart),
|
|
23211
|
+
nameEnd: at(token.argsStart + binding.nameEnd)
|
|
23212
|
+
};
|
|
23213
|
+
return {
|
|
23214
|
+
sigil: "$",
|
|
23215
|
+
name: binding.name,
|
|
23216
|
+
path: binding.path,
|
|
23217
|
+
start: mapped.start,
|
|
23218
|
+
end: mapped.end,
|
|
23219
|
+
binding: mapped
|
|
23220
|
+
};
|
|
23221
|
+
} catch {
|
|
23222
|
+
return;
|
|
23223
|
+
}
|
|
23224
|
+
}
|
|
23178
23225
|
/** Report the `$var` references in the tokens of `content`. */
|
|
23179
23226
|
function collectTokenRefs(content, tokens, at, emit, options) {
|
|
23180
23227
|
const { storeVarMacros, parametersOf } = options;
|
|
@@ -23203,24 +23250,12 @@ function collectTokenRefs(content, tokens, at, emit, options) {
|
|
|
23203
23250
|
else scanInterpolations(value, valueIn, emit, options);
|
|
23204
23251
|
}
|
|
23205
23252
|
else if (token.type === "macro" && !token.isClose) {
|
|
23206
|
-
|
|
23253
|
+
const binding = storeVarMacros.has(token.name.toLowerCase()) && !parametersOf.widget?.(token.name) ? boundReference(token, at, options.all) : void 0;
|
|
23254
|
+
scanArgs(token, parametersOf, at, (ref) => {
|
|
23255
|
+
if (!binding || ref.start < binding.start || ref.start >= binding.end) emit(ref);
|
|
23256
|
+
}, options);
|
|
23207
23257
|
scanSelectors(content, token, at, emit, options);
|
|
23208
|
-
if (
|
|
23209
|
-
const lead = token.rawArgs.length - token.rawArgs.trimStart().length;
|
|
23210
|
-
const first = token.rawArgs.trim().split(/\s+/)[0] ?? "";
|
|
23211
|
-
const quoted = QUOTED_VAR_ARG_RE.exec(first);
|
|
23212
|
-
if (quoted) {
|
|
23213
|
-
const [name, ...path] = quoted[1].split(".");
|
|
23214
|
-
const start = token.argsStart + lead + 1;
|
|
23215
|
-
emit({
|
|
23216
|
-
sigil: "$",
|
|
23217
|
-
name,
|
|
23218
|
-
path,
|
|
23219
|
-
start: at(start),
|
|
23220
|
-
end: at(start + 1 + quoted[1].length)
|
|
23221
|
-
});
|
|
23222
|
-
} else if (!/^["'`]/.test(first)) scanCode(first, (i) => at(token.argsStart + lead + i), emit);
|
|
23223
|
-
}
|
|
23258
|
+
if (binding) emit(binding);
|
|
23224
23259
|
if (executesRawBody(token.name, parametersOf)) {
|
|
23225
23260
|
let close = t + 1;
|
|
23226
23261
|
while (close < tokens.length) {
|
|
@@ -9572,6 +9572,39 @@ function parseWithAst(src, goal) {
|
|
|
9572
9572
|
}
|
|
9573
9573
|
}
|
|
9574
9574
|
/**
|
|
9575
|
+
* Read `src` as a reference path: a variable with any number of `.name` and
|
|
9576
|
+
* `[expression]` members after it (`$data["field"]`, `@list[_i].x`), and
|
|
9577
|
+
* nothing else. Returns null for any other code (`$a + 1`, `$a.b()`, `$a,
|
|
9578
|
+
* $b`, `$a = 1`), and throws a `CodeSyntaxError` for code that is not
|
|
9579
|
+
* well-formed.
|
|
9580
|
+
*/
|
|
9581
|
+
function parseReferencePath(src) {
|
|
9582
|
+
const { ast } = parseWithAst(src, "expression");
|
|
9583
|
+
let node = ast.type === "ChainExpression" ? ast.expression : ast;
|
|
9584
|
+
let nested = false;
|
|
9585
|
+
while (node.type === "MemberExpression") {
|
|
9586
|
+
node = node.object;
|
|
9587
|
+
nested = true;
|
|
9588
|
+
}
|
|
9589
|
+
if (node.type !== "Identifier") return null;
|
|
9590
|
+
const sigil = sigilOf(node, src);
|
|
9591
|
+
if (!sigil || node.start !== 0) return null;
|
|
9592
|
+
return {
|
|
9593
|
+
sigil: sigil[0],
|
|
9594
|
+
name: sigil[1],
|
|
9595
|
+
nested
|
|
9596
|
+
};
|
|
9597
|
+
}
|
|
9598
|
+
/** Decode one dot-member identifier, including keywords and Unicode names. */
|
|
9599
|
+
function parseMemberIdentifier(src) {
|
|
9600
|
+
const { ast } = parseWithAst(`x.${src}`, "expression");
|
|
9601
|
+
if (ast.type !== "MemberExpression" || ast.computed) return null;
|
|
9602
|
+
const object = ast.object;
|
|
9603
|
+
const property = ast.property;
|
|
9604
|
+
if (object.type !== "Identifier" || object.start !== 0 || object.end !== 1 || property.type !== "Identifier" || property.start !== 2 || property.end !== src.length + 2) return null;
|
|
9605
|
+
return property.name;
|
|
9606
|
+
}
|
|
9607
|
+
/**
|
|
9575
9608
|
* Parse `src` as `goal`, reading `%` as `percentAt` says where it says, and
|
|
9576
9609
|
* noting where it read a transient in `transientsRead`.
|
|
9577
9610
|
*/
|
|
@@ -10601,6 +10634,39 @@ function noPassageError(name, from) {
|
|
|
10601
10634
|
}
|
|
10602
10635
|
listeners.subscribe;
|
|
10603
10636
|
//#endregion
|
|
10637
|
+
//#region src/utils/namespace.ts
|
|
10638
|
+
/** The variable name no namespace can hold. */
|
|
10639
|
+
var RESERVED_NAME = "__proto__";
|
|
10640
|
+
/**
|
|
10641
|
+
* Why `name` (without its sigil) cannot be a variable name, or undefined if
|
|
10642
|
+
* it can. `label` is the name as the author wrote it (e.g. `$__proto__`).
|
|
10643
|
+
*/
|
|
10644
|
+
function variableNameError(name, label = name) {
|
|
10645
|
+
return name === "__proto__" ? `"${label}" cannot be used as a variable name (${RESERVED_NAME} is reserved)` : void 0;
|
|
10646
|
+
}
|
|
10647
|
+
/** Whether a property key is an array index (`"0"`, `"12"`; not `"01"`). */
|
|
10648
|
+
var isArrayIndex = (key) => /^(?:0|[1-9]\d*)$/.test(key);
|
|
10649
|
+
/** Whether `obj` holds `key` as an own property. */
|
|
10650
|
+
var hasOwn = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
|
|
10651
|
+
/**
|
|
10652
|
+
* A record with no prototype holding the own entries of `sources`, or only
|
|
10653
|
+
* those whose value passes `keep`.
|
|
10654
|
+
*/
|
|
10655
|
+
function ownEntries(sources, keep = () => true) {
|
|
10656
|
+
const record = Object.create(null);
|
|
10657
|
+
for (const source of sources) {
|
|
10658
|
+
if (!source) continue;
|
|
10659
|
+
for (const key of Object.keys(source)) {
|
|
10660
|
+
const value = source[key];
|
|
10661
|
+
if (keep(value)) record[key] = value;
|
|
10662
|
+
}
|
|
10663
|
+
}
|
|
10664
|
+
return record;
|
|
10665
|
+
}
|
|
10666
|
+
/** A namespace with no prototype holding the own entries of `sources`. */
|
|
10667
|
+
var createNamespace = (...sources) => ownEntries(sources);
|
|
10668
|
+
Object.freeze(createNamespace());
|
|
10669
|
+
//#endregion
|
|
10604
10670
|
//#region src/components/macros/arg-utils.ts
|
|
10605
10671
|
/**
|
|
10606
10672
|
* Shared helpers for macros that split or read their raw argument text by
|
|
@@ -11085,6 +11151,62 @@ function evaluatePassageName(expr, evaluate, { storyData, currentPassage }) {
|
|
|
11085
11151
|
return name;
|
|
11086
11152
|
}
|
|
11087
11153
|
/**
|
|
11154
|
+
* The variable a `storeVar` macro binds: its first term, as written. Custom
|
|
11155
|
+
* macros may bind one without declaring parameters.
|
|
11156
|
+
*/
|
|
11157
|
+
function readBoundVariable(rawArgs) {
|
|
11158
|
+
const src = rawArgs.trim();
|
|
11159
|
+
const first = terms(src)[0];
|
|
11160
|
+
return first ? slice(src, first) : "";
|
|
11161
|
+
}
|
|
11162
|
+
/**
|
|
11163
|
+
* Read the first term as the runtime's story-variable binding. Quotes are
|
|
11164
|
+
* stripped, but this is an identifier path, not a JavaScript string literal:
|
|
11165
|
+
* only identifier escapes are decoded. Returns null for a missing binding;
|
|
11166
|
+
* throws for rejected namespaces, invalid escapes and reserved path names.
|
|
11167
|
+
* The full span excludes quotes and includes the sigil when written.
|
|
11168
|
+
*/
|
|
11169
|
+
function parseBoundVariable(rawArgs) {
|
|
11170
|
+
const term = readBoundVariable(rawArgs);
|
|
11171
|
+
if (!term) return null;
|
|
11172
|
+
const lead = rawArgs.length - rawArgs.trimStart().length;
|
|
11173
|
+
const offsets = [];
|
|
11174
|
+
for (let i = 0; i < term.length; i++) if (term[i] !== "\"" && term[i] !== "'") offsets.push(lead + i);
|
|
11175
|
+
const firstToken = term.replace(/["']/g, "");
|
|
11176
|
+
const kind = {
|
|
11177
|
+
"%": "transient variables",
|
|
11178
|
+
_: "temporary variables",
|
|
11179
|
+
"@": "locals"
|
|
11180
|
+
}[firstToken.charAt(0)];
|
|
11181
|
+
if (kind) throw new Error(`${kind} (${firstToken}) cannot be bound to input macros`);
|
|
11182
|
+
const explicitSigil = firstToken.startsWith("$");
|
|
11183
|
+
if (!explicitSigil && firstToken.split(".")[0].includes("\\")) throw new Error("Escaped binding identifiers require a literal $ story-variable sigil");
|
|
11184
|
+
const segments = firstToken.replace(/^\$/, "").split(".").map((segment, index) => {
|
|
11185
|
+
if (!segment.includes("\\")) return segment;
|
|
11186
|
+
if (index > 0) {
|
|
11187
|
+
const name = parseMemberIdentifier(segment);
|
|
11188
|
+
if (name === null) throw new Error("Invalid bound identifier");
|
|
11189
|
+
return name;
|
|
11190
|
+
}
|
|
11191
|
+
const reference = parseReferencePath(`$${segment}`);
|
|
11192
|
+
if (!reference || reference.nested) throw new Error("Invalid bound identifier");
|
|
11193
|
+
return reference.name;
|
|
11194
|
+
});
|
|
11195
|
+
if (segments.includes("__proto__")) throw new Error(`"$${segments.join(".")}" cannot be bound: ${RESERVED_NAME} is reserved`);
|
|
11196
|
+
const rootStart = explicitSigil ? 1 : 0;
|
|
11197
|
+
const rootEnd = firstToken.indexOf(".", rootStart);
|
|
11198
|
+
const nameEnd = rootEnd < 0 ? firstToken.length : rootEnd;
|
|
11199
|
+
return {
|
|
11200
|
+
name: segments[0],
|
|
11201
|
+
path: segments.slice(1),
|
|
11202
|
+
explicitSigil,
|
|
11203
|
+
start: offsets[0] ?? lead,
|
|
11204
|
+
end: offsets.length ? offsets[offsets.length - 1] + 1 : lead,
|
|
11205
|
+
nameStart: offsets[rootStart] ?? lead + rootStart,
|
|
11206
|
+
nameEnd: offsets[nameEnd - 1] === void 0 ? lead : offsets[nameEnd - 1] + 1
|
|
11207
|
+
};
|
|
11208
|
+
}
|
|
11209
|
+
/**
|
|
11088
11210
|
* The runtime's argument grouping, tolerant of missing or unclosed strings.
|
|
11089
11211
|
* Includes quotes, flags, separators and option keywords; offsets are into
|
|
11090
11212
|
* rawArgs. Absent arguments have no span. Options also have their enclosing
|
|
@@ -11124,39 +11246,6 @@ function subMacroParameters(name) {
|
|
|
11124
11246
|
return SUB_MACRO_PARAMETERS.get(name.toLowerCase());
|
|
11125
11247
|
}
|
|
11126
11248
|
//#endregion
|
|
11127
|
-
//#region src/utils/namespace.ts
|
|
11128
|
-
/** The variable name no namespace can hold. */
|
|
11129
|
-
var RESERVED_NAME = "__proto__";
|
|
11130
|
-
/**
|
|
11131
|
-
* Why `name` (without its sigil) cannot be a variable name, or undefined if
|
|
11132
|
-
* it can. `label` is the name as the author wrote it (e.g. `$__proto__`).
|
|
11133
|
-
*/
|
|
11134
|
-
function variableNameError(name, label = name) {
|
|
11135
|
-
return name === "__proto__" ? `"${label}" cannot be used as a variable name (${RESERVED_NAME} is reserved)` : void 0;
|
|
11136
|
-
}
|
|
11137
|
-
/** Whether a property key is an array index (`"0"`, `"12"`; not `"01"`). */
|
|
11138
|
-
var isArrayIndex = (key) => /^(?:0|[1-9]\d*)$/.test(key);
|
|
11139
|
-
/** Whether `obj` holds `key` as an own property. */
|
|
11140
|
-
var hasOwn = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
|
|
11141
|
-
/**
|
|
11142
|
-
* A record with no prototype holding the own entries of `sources`, or only
|
|
11143
|
-
* those whose value passes `keep`.
|
|
11144
|
-
*/
|
|
11145
|
-
function ownEntries(sources, keep = () => true) {
|
|
11146
|
-
const record = Object.create(null);
|
|
11147
|
-
for (const source of sources) {
|
|
11148
|
-
if (!source) continue;
|
|
11149
|
-
for (const key of Object.keys(source)) {
|
|
11150
|
-
const value = source[key];
|
|
11151
|
-
if (keep(value)) record[key] = value;
|
|
11152
|
-
}
|
|
11153
|
-
}
|
|
11154
|
-
return record;
|
|
11155
|
-
}
|
|
11156
|
-
/** A namespace with no prototype holding the own entries of `sources`. */
|
|
11157
|
-
var createNamespace = (...sources) => ownEntries(sources);
|
|
11158
|
-
Object.freeze(createNamespace());
|
|
11159
|
-
//#endregion
|
|
11160
11249
|
//#region src/code-check.ts
|
|
11161
11250
|
/** While a pass runs: the code it parsed so far, by goal and source. */
|
|
11162
11251
|
var parses = null;
|
|
@@ -12704,8 +12793,6 @@ function declarationRegex(sigil) {
|
|
|
12704
12793
|
}
|
|
12705
12794
|
/** A `$name` or `%name` reference with its dot path, at a sigil the lexer found. */
|
|
12706
12795
|
var VAR_PATH_RE = /([$%])(\w+(?:\.\w+)*)/y;
|
|
12707
|
-
/** Quoted first argument of an input macro naming a story variable. */
|
|
12708
|
-
var QUOTED_VAR_ARG_RE = /^["']\$(\w+(?:\.\w+)*)["']?$/;
|
|
12709
12796
|
var VALID_VAR_TYPES = /* @__PURE__ */ new Set([
|
|
12710
12797
|
"number",
|
|
12711
12798
|
"string",
|
|
@@ -13072,6 +13159,30 @@ function scanSelectors(content, token, at, emit, options) {
|
|
|
13072
13159
|
storeVarMacros: NO_STORE_VAR_MACROS
|
|
13073
13160
|
});
|
|
13074
13161
|
}
|
|
13162
|
+
/** Read a runtime binding and map its editable spans into the passage. */
|
|
13163
|
+
function boundReference(token, at, all) {
|
|
13164
|
+
try {
|
|
13165
|
+
const binding = parseBoundVariable(token.rawArgs);
|
|
13166
|
+
if (!binding || !all && !binding.explicitSigil) return void 0;
|
|
13167
|
+
const mapped = {
|
|
13168
|
+
...binding,
|
|
13169
|
+
start: at(token.argsStart + binding.start),
|
|
13170
|
+
end: at(token.argsStart + binding.end),
|
|
13171
|
+
nameStart: at(token.argsStart + binding.nameStart),
|
|
13172
|
+
nameEnd: at(token.argsStart + binding.nameEnd)
|
|
13173
|
+
};
|
|
13174
|
+
return {
|
|
13175
|
+
sigil: "$",
|
|
13176
|
+
name: binding.name,
|
|
13177
|
+
path: binding.path,
|
|
13178
|
+
start: mapped.start,
|
|
13179
|
+
end: mapped.end,
|
|
13180
|
+
binding: mapped
|
|
13181
|
+
};
|
|
13182
|
+
} catch {
|
|
13183
|
+
return;
|
|
13184
|
+
}
|
|
13185
|
+
}
|
|
13075
13186
|
/** Report the `$var` references in the tokens of `content`. */
|
|
13076
13187
|
function collectTokenRefs(content, tokens, at, emit, options) {
|
|
13077
13188
|
const { storeVarMacros, parametersOf } = options;
|
|
@@ -13100,24 +13211,12 @@ function collectTokenRefs(content, tokens, at, emit, options) {
|
|
|
13100
13211
|
else scanInterpolations(value, valueIn, emit, options);
|
|
13101
13212
|
}
|
|
13102
13213
|
else if (token.type === "macro" && !token.isClose) {
|
|
13103
|
-
|
|
13214
|
+
const binding = storeVarMacros.has(token.name.toLowerCase()) && !parametersOf.widget?.(token.name) ? boundReference(token, at, options.all) : void 0;
|
|
13215
|
+
scanArgs(token, parametersOf, at, (ref) => {
|
|
13216
|
+
if (!binding || ref.start < binding.start || ref.start >= binding.end) emit(ref);
|
|
13217
|
+
}, options);
|
|
13104
13218
|
scanSelectors(content, token, at, emit, options);
|
|
13105
|
-
if (
|
|
13106
|
-
const lead = token.rawArgs.length - token.rawArgs.trimStart().length;
|
|
13107
|
-
const first = token.rawArgs.trim().split(/\s+/)[0] ?? "";
|
|
13108
|
-
const quoted = QUOTED_VAR_ARG_RE.exec(first);
|
|
13109
|
-
if (quoted) {
|
|
13110
|
-
const [name, ...path] = quoted[1].split(".");
|
|
13111
|
-
const start = token.argsStart + lead + 1;
|
|
13112
|
-
emit({
|
|
13113
|
-
sigil: "$",
|
|
13114
|
-
name,
|
|
13115
|
-
path,
|
|
13116
|
-
start: at(start),
|
|
13117
|
-
end: at(start + 1 + quoted[1].length)
|
|
13118
|
-
});
|
|
13119
|
-
} else if (!/^["'`]/.test(first)) scanCode(first, (i) => at(token.argsStart + lead + i), emit);
|
|
13120
|
-
}
|
|
13219
|
+
if (binding) emit(binding);
|
|
13121
13220
|
if (executesRawBody(token.name, parametersOf)) {
|
|
13122
13221
|
let close = t + 1;
|
|
13123
13222
|
while (close < tokens.length) {
|
|
@@ -25806,13 +25905,15 @@ function referenceOptions(macros, all = false, passages = []) {
|
|
|
25806
25905
|
* in source order, with the dotted path of fields accessed and where each is
|
|
25807
25906
|
* written (offsets into `source`, UTF-16): `{$var}` displays, code in
|
|
25808
25907
|
* expressions, conditions, `{do}` bodies and macro arguments, quoted names
|
|
25809
|
-
* bound by input macros, and the markup in labels and HTML attributes.
|
|
25908
|
+
* bound by input macros, and the markup in labels and HTML attributes. Input
|
|
25909
|
+
* bindings include decoded root/path names and editable `binding` spans. Prose
|
|
25810
25910
|
* is literal text. Malformed tags are skipped, so it reads half-typed markup.
|
|
25811
25911
|
* The macros are those the markup may use, built-in and user-defined.
|
|
25812
25912
|
*
|
|
25813
25913
|
* These are the references the story start checks. With `all: true`, the
|
|
25814
25914
|
* list is complete (the checked ones are among it, in source order): it also
|
|
25815
|
-
* has
|
|
25915
|
+
* has sigilless input bindings (`{textbox "name"}`), the variable a macro
|
|
25916
|
+
* names in a `variable` parameter, such as the
|
|
25816
25917
|
* target of `{unset}` and `{computed}`, and the `{$name}` in the selectors of
|
|
25817
25918
|
* a link, display or expression (`[[.c{$name} Go->T]]`). A rename or a find
|
|
25818
25919
|
* of references needs these too.
|
|
@@ -25832,4 +25933,4 @@ function validateVariableReferences(passages, declarations, macros = []) {
|
|
|
25832
25933
|
return checkVariableReferences(list.filter((p) => !p.tags?.some((t) => NOT_MARKUP_TAGS.includes(t))), declarations, referenceOptions(macros, false, list));
|
|
25833
25934
|
}
|
|
25834
25935
|
//#endregion
|
|
25835
|
-
export { MarkupError, SIGIL_SCOPES, checkParameterTypes, collectStoryPassageReferences, completionContext, discoverMacros, effectiveWidgetDefinitions, endsWithOperator, evaluatePassageName, findCodeEnd, formatDiagnostic, formatPassage, isBlockMacro, isSigil, lexJs, lexTemplate, macroArgumentSpans, pairMarkup, parseCode, parseDeclarations, parseSelectors, parseStoryVariables, parseWidgetDef, passagePieces, passagePiecesOfTokens, passageTarget, pieceOffset, readQuoted, scanStringLiteral, splitArgs, splitIncludeFlag, splitTopLevel, stripLooseQuotes, tokenizeMarkup, tokenizeMarkupTolerant, transform, unescapeQuoted, validateStoryMarkup, validateVariableReferences, variableReferences, widgetDefinitions };
|
|
25936
|
+
export { MarkupError, SIGIL_SCOPES, checkParameterTypes, collectStoryPassageReferences, completionContext, discoverMacros, effectiveWidgetDefinitions, endsWithOperator, evaluatePassageName, findCodeEnd, formatDiagnostic, formatPassage, isBlockMacro, isSigil, lexJs, lexTemplate, macroArgumentSpans, pairMarkup, parseBoundVariable, parseCode, parseDeclarations, parseSelectors, parseStoryVariables, parseWidgetDef, passagePieces, passagePiecesOfTokens, passageTarget, pieceOffset, readQuoted, scanStringLiteral, splitArgs, splitIncludeFlag, splitTopLevel, stripLooseQuotes, tokenizeMarkup, tokenizeMarkupTolerant, transform, unescapeQuoted, validateStoryMarkup, validateVariableReferences, variableReferences, widgetDefinitions };
|
package/dist/pkg/tooling.js
CHANGED
package/package.json
CHANGED
|
@@ -19,7 +19,12 @@ import type { Span as SourceSpan } from '../../markup/tokens';
|
|
|
19
19
|
import type { MacroArgs, ParameterDef } from '../../registry';
|
|
20
20
|
import type { StoryState } from '../../store';
|
|
21
21
|
import { noPassageError } from '../../runtime-errors';
|
|
22
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
parseMemberIdentifier,
|
|
24
|
+
parseReferencePath,
|
|
25
|
+
stringLiteralValue,
|
|
26
|
+
} from '../../js-lexer';
|
|
27
|
+
import { RESERVED_NAME } from '../../utils/namespace';
|
|
23
28
|
import {
|
|
24
29
|
endsWithOperator,
|
|
25
30
|
isWhitespace,
|
|
@@ -451,6 +456,84 @@ export function readBoundVariable(rawArgs: string): string {
|
|
|
451
456
|
return first ? slice(src, first) : '';
|
|
452
457
|
}
|
|
453
458
|
|
|
459
|
+
/** A runtime input binding, with UTF-16 spans into the raw arguments. */
|
|
460
|
+
export interface BoundVariable extends SourceSpan {
|
|
461
|
+
/** Decoded story-variable root and dot members. */
|
|
462
|
+
name: string;
|
|
463
|
+
path: string[];
|
|
464
|
+
/** Whether the author wrote the optional literal `$` sigil. */
|
|
465
|
+
explicitSigil: boolean;
|
|
466
|
+
/** Editable root spelling, excluding quotes, sigil and dot members. */
|
|
467
|
+
nameStart: number;
|
|
468
|
+
nameEnd: number;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Read the first term as the runtime's story-variable binding. Quotes are
|
|
473
|
+
* stripped, but this is an identifier path, not a JavaScript string literal:
|
|
474
|
+
* only identifier escapes are decoded. Returns null for a missing binding;
|
|
475
|
+
* throws for rejected namespaces, invalid escapes and reserved path names.
|
|
476
|
+
* The full span excludes quotes and includes the sigil when written.
|
|
477
|
+
*/
|
|
478
|
+
export function parseBoundVariable(rawArgs: string): BoundVariable | null {
|
|
479
|
+
const term = readBoundVariable(rawArgs);
|
|
480
|
+
if (!term) return null;
|
|
481
|
+
const lead = rawArgs.length - rawArgs.trimStart().length;
|
|
482
|
+
const offsets: number[] = [];
|
|
483
|
+
for (let i = 0; i < term.length; i++) {
|
|
484
|
+
if (term[i] !== '"' && term[i] !== "'") offsets.push(lead + i);
|
|
485
|
+
}
|
|
486
|
+
const firstToken = term.replace(/["']/g, '');
|
|
487
|
+
const unbindable: Record<string, string> = {
|
|
488
|
+
'%': 'transient variables',
|
|
489
|
+
_: 'temporary variables',
|
|
490
|
+
'@': 'locals',
|
|
491
|
+
};
|
|
492
|
+
const kind = unbindable[firstToken.charAt(0)];
|
|
493
|
+
if (kind) {
|
|
494
|
+
throw new Error(`${kind} (${firstToken}) cannot be bound to input macros`);
|
|
495
|
+
}
|
|
496
|
+
const explicitSigil = firstToken.startsWith('$');
|
|
497
|
+
if (!explicitSigil && firstToken.split('.')[0]!.includes('\\')) {
|
|
498
|
+
throw new Error(
|
|
499
|
+
'Escaped binding identifiers require a literal $ story-variable sigil',
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
const segments = firstToken
|
|
503
|
+
.replace(/^\$/, '')
|
|
504
|
+
.split('.')
|
|
505
|
+
.map((segment, index) => {
|
|
506
|
+
if (!segment.includes('\\')) return segment;
|
|
507
|
+
if (index > 0) {
|
|
508
|
+
const name = parseMemberIdentifier(segment);
|
|
509
|
+
if (name === null) throw new Error('Invalid bound identifier');
|
|
510
|
+
return name;
|
|
511
|
+
}
|
|
512
|
+
const reference = parseReferencePath(`$${segment}`);
|
|
513
|
+
if (!reference || reference.nested)
|
|
514
|
+
throw new Error('Invalid bound identifier');
|
|
515
|
+
return reference.name;
|
|
516
|
+
});
|
|
517
|
+
if (segments.includes(RESERVED_NAME)) {
|
|
518
|
+
throw new Error(
|
|
519
|
+
`"$${segments.join('.')}" cannot be bound: ${RESERVED_NAME} is reserved`,
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
const rootStart = explicitSigil ? 1 : 0;
|
|
523
|
+
const rootEnd = firstToken.indexOf('.', rootStart);
|
|
524
|
+
const nameEnd = rootEnd < 0 ? firstToken.length : rootEnd;
|
|
525
|
+
return {
|
|
526
|
+
name: segments[0]!,
|
|
527
|
+
path: segments.slice(1),
|
|
528
|
+
explicitSigil,
|
|
529
|
+
start: offsets[0] ?? lead,
|
|
530
|
+
end: offsets.length ? offsets[offsets.length - 1]! + 1 : lead,
|
|
531
|
+
nameStart: offsets[rootStart] ?? lead + rootStart,
|
|
532
|
+
nameEnd:
|
|
533
|
+
offsets[nameEnd - 1] === undefined ? lead : offsets[nameEnd - 1]! + 1,
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
|
|
454
537
|
/** A consumed argument and its declared parameter, with UTF-16 source offsets. */
|
|
455
538
|
export interface MacroArgumentSpan extends SourceSpan {
|
|
456
539
|
/** The original parameter object, including for nested keyword options. */
|
package/src/define-macro.ts
CHANGED
|
@@ -29,8 +29,6 @@ import { executeMutation, mutateState, readState } from './execute-mutation';
|
|
|
29
29
|
import { evaluateDisplay } from './expression';
|
|
30
30
|
import { useStoryFields } from './hooks/use-story-fields';
|
|
31
31
|
import { getByPath, setByPath } from './utils/object-path';
|
|
32
|
-
import { RESERVED_NAME } from './utils/namespace';
|
|
33
|
-
import { parseMemberIdentifier, parseReferencePath } from './js-lexer';
|
|
34
32
|
import { useAction } from './hooks/use-action';
|
|
35
33
|
import type { UseActionOptions } from './hooks/use-action';
|
|
36
34
|
import { collectText } from './utils/extract-text';
|
|
@@ -40,7 +38,7 @@ import { wrapContent } from './components/macros/display';
|
|
|
40
38
|
import {
|
|
41
39
|
MacroArgumentError,
|
|
42
40
|
parseMacroArgs,
|
|
43
|
-
|
|
41
|
+
parseBoundVariable,
|
|
44
42
|
} from './components/macros/macro-args';
|
|
45
43
|
import { MacroError } from './components/macros/MacroError';
|
|
46
44
|
import {
|
|
@@ -241,66 +239,14 @@ export function defineMacro<const P extends readonly ParameterDef[] = []>(
|
|
|
241
239
|
}
|
|
242
240
|
|
|
243
241
|
if (config.storeVar) {
|
|
244
|
-
const firstToken = readBoundVariable(props.rawArgs).replace(/["']/g, '');
|
|
245
|
-
|
|
246
|
-
// Only story variables ($name) can be bound: another sigil would be
|
|
247
|
-
// read as a story key spelled with it (#414)
|
|
248
|
-
const unbindable: Record<string, string> = {
|
|
249
|
-
'%': 'transient variables',
|
|
250
|
-
_: 'temporary variables',
|
|
251
|
-
'@': 'locals',
|
|
252
|
-
};
|
|
253
|
-
const kind = unbindable[firstToken.charAt(0)];
|
|
254
|
-
if (kind) {
|
|
255
|
-
return h(
|
|
256
|
-
'span',
|
|
257
|
-
{ class: 'error' },
|
|
258
|
-
`{${config.name}}: ${kind} (${firstToken}) cannot be bound to input macros`,
|
|
259
|
-
);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
if (
|
|
263
|
-
!firstToken.startsWith('$') &&
|
|
264
|
-
firstToken.split('.')[0]!.includes('\\')
|
|
265
|
-
) {
|
|
266
|
-
return h(MacroError, {
|
|
267
|
-
macro: config.name,
|
|
268
|
-
error: new Error(
|
|
269
|
-
'Escaped binding identifiers require a literal $ story-variable sigil',
|
|
270
|
-
),
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
|
|
274
242
|
let segments: string[];
|
|
275
243
|
try {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
.split('.')
|
|
279
|
-
.map((segment, index) => {
|
|
280
|
-
if (!segment.includes('\\')) return segment;
|
|
281
|
-
// Members follow JavaScript's identifier grammar, which accepts
|
|
282
|
-
// more names than story-variable roots (for example $foo and é).
|
|
283
|
-
if (index > 0) {
|
|
284
|
-
const name = parseMemberIdentifier(segment);
|
|
285
|
-
if (name === null) throw new Error('Invalid bound identifier');
|
|
286
|
-
return name;
|
|
287
|
-
}
|
|
288
|
-
const reference = parseReferencePath(`$${segment}`);
|
|
289
|
-
if (!reference || reference.nested)
|
|
290
|
-
throw new Error('Invalid bound identifier');
|
|
291
|
-
return reference.name;
|
|
292
|
-
});
|
|
244
|
+
const binding = parseBoundVariable(props.rawArgs);
|
|
245
|
+
segments = binding ? [binding.name, ...binding.path] : [''];
|
|
293
246
|
} catch (error) {
|
|
294
247
|
return h(MacroError, { macro: config.name, error });
|
|
295
248
|
}
|
|
296
249
|
const varExpr = segments.join('.');
|
|
297
|
-
if (segments.includes(RESERVED_NAME)) {
|
|
298
|
-
return h(
|
|
299
|
-
'span',
|
|
300
|
-
{ class: 'error' },
|
|
301
|
-
`{${config.name}}: "$${varExpr}" cannot be bound: ${RESERVED_NAME} is reserved`,
|
|
302
|
-
);
|
|
303
|
-
}
|
|
304
250
|
ctx.varName = varExpr;
|
|
305
251
|
ctx.value = getByPath(useStoryFields('variables').variables, segments);
|
|
306
252
|
// In program order, also when mutation code performs the input
|