@rohal12/spindle 0.59.26 → 0.59.27
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 +254 -57
- package/dist/pkg/story-variables.js +514 -71
- package/dist/pkg/tooling.js +1 -0
- package/package.json +1 -1
- package/src/macro-discovery.ts +299 -0
- package/src/markup/validate.ts +27 -6
- package/src/static-literal.ts +222 -50
- package/src/story-variables.ts +133 -20
- package/src/tooling.ts +28 -5
- package/src/types-drift-check.ts +17 -0
- package/src/widgets/ast-scanner.ts +4 -1
- package/src/widgets/widget-def.ts +5 -4
- package/types/tooling.d.ts +52 -4
package/dist/pkg/headless.js
CHANGED
|
@@ -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(
|
|
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
|
|
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(
|
|
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 {
|
|
@@ -20043,19 +20049,30 @@ function validateMarkup(passages, options) {
|
|
|
20043
20049
|
const parsed = [];
|
|
20044
20050
|
const widgets = /* @__PURE__ */ new Set();
|
|
20045
20051
|
const passageNames = /* @__PURE__ */ new Set();
|
|
20052
|
+
const addWidgets = (tokens) => {
|
|
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
|
+
}
|
|
20059
|
+
};
|
|
20046
20060
|
for (const passage of passages) {
|
|
20047
20061
|
passageNames.add(passage.name);
|
|
20048
20062
|
if (NOT_MARKUP.has(passage.name)) continue;
|
|
20049
20063
|
if (passage.tags?.some((tag) => NOT_MARKUP_TAGS.includes(tag))) continue;
|
|
20050
20064
|
const reported = !options.only || options.only(passage);
|
|
20065
|
+
if (options.tolerant) {
|
|
20066
|
+
const { tokens, errors } = analyzeMarkup(passage.content, { hooks });
|
|
20067
|
+
addWidgets(tokens);
|
|
20068
|
+
if (!reported) continue;
|
|
20069
|
+
for (const err of errors) reportError(passage, err, "");
|
|
20070
|
+
parsed.push([passage, tokens]);
|
|
20071
|
+
continue;
|
|
20072
|
+
}
|
|
20051
20073
|
try {
|
|
20052
20074
|
const tokens = tokenizeMarkup(passage.content);
|
|
20053
|
-
|
|
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
|
-
}
|
|
20075
|
+
addWidgets(tokens);
|
|
20059
20076
|
if (!reported) continue;
|
|
20060
20077
|
parseMarkup(passage.content, { hooks });
|
|
20061
20078
|
parsed.push([passage, tokens]);
|
|
@@ -20145,7 +20162,13 @@ function validateMarkup(passages, options) {
|
|
|
20145
20162
|
*/
|
|
20146
20163
|
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
20164
|
var IDENT_RE = /[A-Za-z_$][\w$]*/y;
|
|
20165
|
+
var KEY_RE = /[\w$]+/y;
|
|
20148
20166
|
var ARROW_RE = /(?:async\s+)?(?:\((?:[^()]|\([^()]*\))*\)|[A-Za-z_$][\w$]*)\s*=>/y;
|
|
20167
|
+
var ARRAY_INDEX_RE = /^(?:0|[1-9]\d{0,8})$/;
|
|
20168
|
+
var UNSUPPORTED_UNDEFINED = {
|
|
20169
|
+
unsupported: "undefined",
|
|
20170
|
+
value: "undefined"
|
|
20171
|
+
};
|
|
20149
20172
|
/** Read `src`, one literal and nothing else. */
|
|
20150
20173
|
function staticLiteral(src) {
|
|
20151
20174
|
const reader = new Reader(src);
|
|
@@ -20153,6 +20176,27 @@ function staticLiteral(src) {
|
|
|
20153
20176
|
reader.space();
|
|
20154
20177
|
return reader.i === src.length ? value : void 0;
|
|
20155
20178
|
}
|
|
20179
|
+
/**
|
|
20180
|
+
* The keys of an object in the order `Object.entries` lists them (array
|
|
20181
|
+
* indices first, ascending, then the others as they were added): the order
|
|
20182
|
+
* the runtime meets the members in.
|
|
20183
|
+
*/
|
|
20184
|
+
function entryOrder(keys) {
|
|
20185
|
+
const all = [...keys];
|
|
20186
|
+
return [...all.filter((key) => ARRAY_INDEX_RE.test(key)).sort((a, b) => Number(a) - Number(b)), ...all.filter((key) => !ARRAY_INDEX_RE.test(key))];
|
|
20187
|
+
}
|
|
20188
|
+
/** `String(value)` of a BigInt literal's digits, or `undefined` if they are no BigInt. */
|
|
20189
|
+
function bigintValue(digits) {
|
|
20190
|
+
try {
|
|
20191
|
+
return BigInt(digits.replace(/_/g, "")).toString();
|
|
20192
|
+
} catch {
|
|
20193
|
+
return;
|
|
20194
|
+
}
|
|
20195
|
+
}
|
|
20196
|
+
/**
|
|
20197
|
+
* The cursor over a JavaScript text that the literal reader moves: the
|
|
20198
|
+
* helpers that skip what is not read are shared (see macro-discovery.ts).
|
|
20199
|
+
*/
|
|
20156
20200
|
var Reader = class {
|
|
20157
20201
|
constructor(src) {
|
|
20158
20202
|
this.src = src;
|
|
@@ -20194,6 +20238,15 @@ var Reader = class {
|
|
|
20194
20238
|
closed
|
|
20195
20239
|
};
|
|
20196
20240
|
}
|
|
20241
|
+
/**
|
|
20242
|
+
* Skip the string literal at the cursor and read its value, or `undefined`
|
|
20243
|
+
* if it is not closed or not well-formed.
|
|
20244
|
+
*/
|
|
20245
|
+
stringValue() {
|
|
20246
|
+
const at = this.i;
|
|
20247
|
+
const { closed } = this.string();
|
|
20248
|
+
return closed ? stringLiteralValue(this.src.slice(at, this.i)) ?? void 0 : void 0;
|
|
20249
|
+
}
|
|
20197
20250
|
/** A template literal: a string, but for `${}`. */
|
|
20198
20251
|
template() {
|
|
20199
20252
|
const { src } = this;
|
|
@@ -20210,7 +20263,8 @@ var Reader = class {
|
|
|
20210
20263
|
number() {
|
|
20211
20264
|
const { src } = this;
|
|
20212
20265
|
let i = this.i;
|
|
20213
|
-
|
|
20266
|
+
const sign = src.charAt(i);
|
|
20267
|
+
if (sign === "+" || sign === "-") {
|
|
20214
20268
|
i++;
|
|
20215
20269
|
while (/\s/.test(src.charAt(i))) i++;
|
|
20216
20270
|
}
|
|
@@ -20218,7 +20272,13 @@ var Reader = class {
|
|
|
20218
20272
|
const match = NUMBER_RE.exec(src);
|
|
20219
20273
|
if (match) {
|
|
20220
20274
|
this.i = NUMBER_RE.lastIndex;
|
|
20221
|
-
|
|
20275
|
+
if (!match[1]) return { shape: { type: "number" } };
|
|
20276
|
+
if (sign === "+") return { throws: "Cannot convert a BigInt value to a number" };
|
|
20277
|
+
const digits = bigintValue(match[0].slice(0, -1));
|
|
20278
|
+
return digits === void 0 ? void 0 : {
|
|
20279
|
+
unsupported: "bigint",
|
|
20280
|
+
value: sign === "-" && digits !== "0" ? `-${digits}` : digits
|
|
20281
|
+
};
|
|
20222
20282
|
}
|
|
20223
20283
|
IDENT_RE.lastIndex = i;
|
|
20224
20284
|
if (IDENT_RE.exec(src)?.[0] === "Infinity") {
|
|
@@ -20229,8 +20289,9 @@ var Reader = class {
|
|
|
20229
20289
|
/** A keyword, a function, or a name (which is not static). */
|
|
20230
20290
|
word() {
|
|
20231
20291
|
const { src } = this;
|
|
20232
|
-
|
|
20233
|
-
|
|
20292
|
+
const start = this.i;
|
|
20293
|
+
const arrow = this.arrow();
|
|
20294
|
+
if (arrow !== void 0) return arrow ? this.functionFrom(start) : void 0;
|
|
20234
20295
|
IDENT_RE.lastIndex = this.i;
|
|
20235
20296
|
const word = IDENT_RE.exec(src)?.[0];
|
|
20236
20297
|
if (word === void 0) return void 0;
|
|
@@ -20248,72 +20309,152 @@ var Reader = class {
|
|
|
20248
20309
|
return { shape: { type: "number" } };
|
|
20249
20310
|
case "undefined":
|
|
20250
20311
|
this.i = end;
|
|
20251
|
-
return
|
|
20312
|
+
return UNSUPPORTED_UNDEFINED;
|
|
20313
|
+
case "void": {
|
|
20314
|
+
this.i = end;
|
|
20315
|
+
const operand = this.value();
|
|
20316
|
+
return operand && !("throws" in operand) ? UNSUPPORTED_UNDEFINED : operand;
|
|
20317
|
+
}
|
|
20252
20318
|
case "function":
|
|
20253
20319
|
case "class":
|
|
20254
|
-
case "async": return this.
|
|
20320
|
+
case "async": return this.skipDeclaration(word) ? this.functionFrom(start) : void 0;
|
|
20255
20321
|
}
|
|
20256
20322
|
}
|
|
20257
|
-
/**
|
|
20258
|
-
|
|
20259
|
-
return
|
|
20323
|
+
/** The function (or class) that was skipped since `start`. */
|
|
20324
|
+
functionFrom(start) {
|
|
20325
|
+
return {
|
|
20326
|
+
unsupported: "function",
|
|
20327
|
+
value: this.src.slice(start, this.i).trimEnd()
|
|
20328
|
+
};
|
|
20329
|
+
}
|
|
20330
|
+
/**
|
|
20331
|
+
* Skip the parameters of an arrow function and its body, which ends at a
|
|
20332
|
+
* `,` or `}` outside brackets, or at the end of the text. `undefined` if the
|
|
20333
|
+
* text at the cursor is no arrow function, false if it cannot be skipped.
|
|
20334
|
+
*/
|
|
20335
|
+
arrow() {
|
|
20336
|
+
const { src } = this;
|
|
20337
|
+
ARROW_RE.lastIndex = this.i;
|
|
20338
|
+
if (!ARROW_RE.test(src)) return void 0;
|
|
20339
|
+
this.i = ARROW_RE.lastIndex;
|
|
20340
|
+
this.space();
|
|
20341
|
+
if (src.charAt(this.i) === "{") return this.skipBalanced();
|
|
20342
|
+
while (this.i < src.length && !",}".includes(src.charAt(this.i))) if (!this.skipOne()) break;
|
|
20343
|
+
return true;
|
|
20260
20344
|
}
|
|
20261
|
-
/**
|
|
20262
|
-
|
|
20263
|
-
|
|
20264
|
-
|
|
20345
|
+
/**
|
|
20346
|
+
* Skip a `function`, `async function` or `class` (its keyword `word` is at
|
|
20347
|
+
* the cursor), up to the end of its body.
|
|
20348
|
+
*/
|
|
20349
|
+
skipDeclaration(word) {
|
|
20350
|
+
const { src } = this;
|
|
20351
|
+
this.i += word.length;
|
|
20352
|
+
this.space();
|
|
20353
|
+
if (word === "async") {
|
|
20354
|
+
IDENT_RE.lastIndex = this.i;
|
|
20355
|
+
if (IDENT_RE.exec(src)?.[0] !== "function") return false;
|
|
20356
|
+
return this.skipDeclaration("function");
|
|
20357
|
+
}
|
|
20358
|
+
if (word === "function") {
|
|
20359
|
+
if (src.charAt(this.i) === "*") this.i++;
|
|
20360
|
+
this.space();
|
|
20361
|
+
IDENT_RE.lastIndex = this.i;
|
|
20362
|
+
if (IDENT_RE.test(src)) this.i = IDENT_RE.lastIndex;
|
|
20363
|
+
this.space();
|
|
20364
|
+
if (src.charAt(this.i) !== "(" || !this.skipBalanced()) return false;
|
|
20365
|
+
this.space();
|
|
20366
|
+
} else while (this.i < src.length && src.charAt(this.i) !== "{") if (!this.skipOne()) return false;
|
|
20367
|
+
return src.charAt(this.i) === "{" && this.skipBalanced();
|
|
20265
20368
|
}
|
|
20266
20369
|
/** An object literal: the members that are static. */
|
|
20267
20370
|
object() {
|
|
20268
20371
|
const { src } = this;
|
|
20269
|
-
const
|
|
20372
|
+
const members = /* @__PURE__ */ new Map();
|
|
20373
|
+
let thrown;
|
|
20270
20374
|
this.i++;
|
|
20271
20375
|
for (;;) {
|
|
20272
20376
|
this.space();
|
|
20273
20377
|
const c = src.charAt(this.i);
|
|
20274
20378
|
if (c === "}") {
|
|
20275
20379
|
this.i++;
|
|
20276
|
-
return {
|
|
20277
|
-
type: "object",
|
|
20278
|
-
fields
|
|
20279
|
-
} };
|
|
20380
|
+
return thrown === void 0 ? this.objectOf(members) : { throws: thrown };
|
|
20280
20381
|
}
|
|
20281
20382
|
if (c === ",") {
|
|
20282
20383
|
this.i++;
|
|
20283
20384
|
continue;
|
|
20284
20385
|
}
|
|
20285
20386
|
if (this.i >= src.length) return void 0;
|
|
20387
|
+
const start = this.i;
|
|
20286
20388
|
const key = this.key();
|
|
20389
|
+
if (key === "__proto__") return void 0;
|
|
20287
20390
|
this.space();
|
|
20288
|
-
|
|
20391
|
+
const next = src.charAt(this.i);
|
|
20392
|
+
if (key !== void 0 && next === ":") {
|
|
20289
20393
|
this.i++;
|
|
20290
20394
|
const at = this.i;
|
|
20291
20395
|
const value = this.value();
|
|
20292
20396
|
this.space();
|
|
20293
|
-
if (value &&
|
|
20294
|
-
|
|
20295
|
-
|
|
20397
|
+
if (value && this.atMemberEnd()) {
|
|
20398
|
+
if ("throws" in value) thrown ??= value.throws;
|
|
20399
|
+
else members.set(key, "shape" in value ? value.shape : value);
|
|
20296
20400
|
continue;
|
|
20297
20401
|
}
|
|
20402
|
+
members.set(key, null);
|
|
20298
20403
|
this.i = at;
|
|
20299
|
-
} else if (key !== void 0 &&
|
|
20404
|
+
} else if (key !== void 0 && next === "(") {
|
|
20405
|
+
if (!this.skipBalanced()) return void 0;
|
|
20406
|
+
this.space();
|
|
20407
|
+
if (src.charAt(this.i) === "{" && this.skipBalanced()) {
|
|
20408
|
+
members.set(key, {
|
|
20409
|
+
unsupported: "function",
|
|
20410
|
+
value: src.slice(start, this.i)
|
|
20411
|
+
});
|
|
20412
|
+
continue;
|
|
20413
|
+
}
|
|
20414
|
+
return;
|
|
20415
|
+
} else if (key !== void 0 && (next === "," || next === "}")) {
|
|
20416
|
+
members.set(key, null);
|
|
20417
|
+
continue;
|
|
20418
|
+
} else members.clear();
|
|
20300
20419
|
if (!this.skipMember()) return void 0;
|
|
20301
20420
|
}
|
|
20302
20421
|
}
|
|
20422
|
+
/** Whether a member ends at the cursor. */
|
|
20423
|
+
atMemberEnd() {
|
|
20424
|
+
return this.i < this.src.length && ",}".includes(this.src.charAt(this.i));
|
|
20425
|
+
}
|
|
20426
|
+
/**
|
|
20427
|
+
* The object literal with these members: a value no variable can hold
|
|
20428
|
+
* anywhere in it (the first the runtime meets) is what it reads as.
|
|
20429
|
+
*/
|
|
20430
|
+
objectOf(members) {
|
|
20431
|
+
const fields = /* @__PURE__ */ new Map();
|
|
20432
|
+
for (const key of entryOrder(members.keys())) {
|
|
20433
|
+
const member = members.get(key);
|
|
20434
|
+
if (!member) continue;
|
|
20435
|
+
if ("unsupported" in member) return member;
|
|
20436
|
+
fields.set(key, member);
|
|
20437
|
+
}
|
|
20438
|
+
return { shape: {
|
|
20439
|
+
type: "object",
|
|
20440
|
+
fields
|
|
20441
|
+
} };
|
|
20442
|
+
}
|
|
20303
20443
|
/** The key of a member (a name, a string or a number), if it has one. */
|
|
20304
20444
|
key() {
|
|
20305
20445
|
const { src } = this;
|
|
20306
20446
|
const c = src.charAt(this.i);
|
|
20307
|
-
if (c === "\"" || c === "'")
|
|
20308
|
-
|
|
20309
|
-
|
|
20310
|
-
}
|
|
20311
|
-
const word = /[\w$]+/y;
|
|
20312
|
-
word.lastIndex = this.i;
|
|
20313
|
-
const found = word.exec(src)?.[0];
|
|
20447
|
+
if (c === "\"" || c === "'") return this.stringValue();
|
|
20448
|
+
KEY_RE.lastIndex = this.i;
|
|
20449
|
+
const found = KEY_RE.exec(src)?.[0];
|
|
20314
20450
|
if (found === void 0) return void 0;
|
|
20315
|
-
this.i =
|
|
20316
|
-
return found;
|
|
20451
|
+
this.i = KEY_RE.lastIndex;
|
|
20452
|
+
if (!/^\d/.test(found)) return found;
|
|
20453
|
+
const digits = found.replace(/_/g, "");
|
|
20454
|
+
if (/^0\d/.test(digits)) return void 0;
|
|
20455
|
+
if (digits.endsWith("n") && !/^0[xX]/.test(digits)) return bigintValue(digits.slice(0, -1));
|
|
20456
|
+
const value = Number(digits);
|
|
20457
|
+
return Number.isNaN(value) ? void 0 : String(value);
|
|
20317
20458
|
}
|
|
20318
20459
|
/** Skip to the `,` or `}` that ends a member, balancing what is inside. */
|
|
20319
20460
|
skipMember() {
|
|
@@ -20447,6 +20588,27 @@ function shapeSchema(shape) {
|
|
|
20447
20588
|
};
|
|
20448
20589
|
}
|
|
20449
20590
|
/**
|
|
20591
|
+
* What is wrong with the syntax of an initializer, as it is evaluated
|
|
20592
|
+
* (`return (expr)` in a function body), or `undefined`. `pos` is where, as an
|
|
20593
|
+
* index into `expr` (it may be past its end, for an unexpected end).
|
|
20594
|
+
*/
|
|
20595
|
+
function initializerSyntaxError(expr) {
|
|
20596
|
+
const prefix = "return (";
|
|
20597
|
+
try {
|
|
20598
|
+
parse$1(prefix + expr + ")", {
|
|
20599
|
+
ecmaVersion: "latest",
|
|
20600
|
+
allowReturnOutsideFunction: true
|
|
20601
|
+
});
|
|
20602
|
+
} catch (err) {
|
|
20603
|
+
if (!(err instanceof SyntaxError)) throw err;
|
|
20604
|
+
const { pos } = err;
|
|
20605
|
+
return {
|
|
20606
|
+
message: err.message.replace(/ \(\d+:\d+\)$/, ""),
|
|
20607
|
+
pos: Math.max(0, pos - 8)
|
|
20608
|
+
};
|
|
20609
|
+
}
|
|
20610
|
+
}
|
|
20611
|
+
/**
|
|
20450
20612
|
* Read the declarations of a StoryVariables (`$name = value`) or
|
|
20451
20613
|
* StoryTransients (`%name = value`) passage, without evaluating them and
|
|
20452
20614
|
* without throwing: every valid declaration, and every invalid line as an
|
|
@@ -20454,9 +20616,10 @@ function shapeSchema(shape) {
|
|
|
20454
20616
|
*
|
|
20455
20617
|
* A declaration is a line of the form `$name = expression`; blank lines are
|
|
20456
20618
|
* skipped. A line that is not, and a name that cannot be a variable, are
|
|
20457
|
-
* errors. So is an initializer
|
|
20458
|
-
* `undefined`)
|
|
20459
|
-
*
|
|
20619
|
+
* errors. So is an initializer with a syntax error, and one that no variable
|
|
20620
|
+
* can hold (a function, `undefined`) when it is a literal, or one that throws
|
|
20621
|
+
* when it is a literal (`+1n`); whether any other initializer can only be
|
|
20622
|
+
* known by running it. A name declared again is an error too, but both
|
|
20460
20623
|
* declarations are returned (the later wins when evaluated).
|
|
20461
20624
|
*/
|
|
20462
20625
|
function parseDeclarations(content, sigil = "$") {
|
|
@@ -20499,10 +20662,19 @@ function parseDeclarations(content, sigil = "$") {
|
|
|
20499
20662
|
};
|
|
20500
20663
|
if (seen.has(name)) error("duplicate-declaration", nameStart, nameEnd, `Duplicate declaration of ${sigil}${name}`);
|
|
20501
20664
|
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
20665
|
declarations.push(declaration);
|
|
20666
|
+
const failed = `Failed to evaluate "${sigil}${name} = ${expr}"`;
|
|
20667
|
+
const syntax = initializerSyntaxError(expr);
|
|
20668
|
+
if (syntax) {
|
|
20669
|
+
const at = Math.min(syntax.pos, expr.length - 1);
|
|
20670
|
+
error("syntax", declaration.valueStart + at, declaration.valueStart + at + 1, `${failed}: ${syntax.message}`);
|
|
20671
|
+
continue;
|
|
20672
|
+
}
|
|
20673
|
+
const literal = staticLiteral(expr);
|
|
20674
|
+
if (!literal) continue;
|
|
20675
|
+
if ("shape" in literal) declaration.schema = shapeSchema(literal.shape);
|
|
20676
|
+
else if ("throws" in literal) error("unsupported-value", declaration.valueStart, valueEnd, `${failed}: ${literal.throws}`);
|
|
20677
|
+
else error("unsupported-value", declaration.valueStart, valueEnd, `Unsupported type "${literal.unsupported}" for value ${literal.value}. ${UNSUPPORTED_TYPE}`);
|
|
20506
20678
|
}
|
|
20507
20679
|
return {
|
|
20508
20680
|
declarations,
|
|
@@ -20679,6 +20851,7 @@ function scanCodeLeniently(code, at, emit, goal) {
|
|
|
20679
20851
|
function scanArgs(token, params, at, emit, options) {
|
|
20680
20852
|
if (!token.rawArgs) return;
|
|
20681
20853
|
if (params) {
|
|
20854
|
+
if (options.all && !options.storeVarMacros.has(token.name.toLowerCase())) scanVariableParameters(token, params, at, emit);
|
|
20682
20855
|
const pieces = [...argPieces(token.rawArgs, token.argsStart, params, token.name, "")];
|
|
20683
20856
|
if (!pieces.some((piece) => piece.kind === "argument-error")) {
|
|
20684
20857
|
for (const piece of pieces) {
|
|
@@ -20694,11 +20867,41 @@ function scanArgs(token, params, at, emit, options) {
|
|
|
20694
20867
|
}
|
|
20695
20868
|
scanCode(token.rawArgs, (i) => at(token.argsStart + i), emit);
|
|
20696
20869
|
}
|
|
20870
|
+
/**
|
|
20871
|
+
* Report the variables the `variable` parameters of a macro name
|
|
20872
|
+
* (`{unset $x}`, `{computed $x = …}`), when they are `$` or `%` variables.
|
|
20873
|
+
* Arguments that do not parse hold none.
|
|
20874
|
+
*/
|
|
20875
|
+
function scanVariableParameters(token, params, at, emit) {
|
|
20876
|
+
const spans = /* @__PURE__ */ new Map();
|
|
20877
|
+
try {
|
|
20878
|
+
parseMacroArgs(token.rawArgs, params, spans);
|
|
20879
|
+
} catch (err) {
|
|
20880
|
+
if (err instanceof MacroArgumentError) return;
|
|
20881
|
+
throw err;
|
|
20882
|
+
}
|
|
20883
|
+
for (const param of params) {
|
|
20884
|
+
const span = spans.get(param);
|
|
20885
|
+
if (param.type !== "variable" || !span) continue;
|
|
20886
|
+
const ref = referenceAt(token.rawArgs.slice(span[0], span[1]), 0, (i) => at(token.argsStart + span[0] + i));
|
|
20887
|
+
if (ref) emit(ref);
|
|
20888
|
+
}
|
|
20889
|
+
}
|
|
20890
|
+
/** Report the references in the selectors of a token, which are interpolated. */
|
|
20891
|
+
function scanSelectors(content, token, at, emit, options) {
|
|
20892
|
+
if (!("selectorsStart" in token) || token.selectorsStart === void 0 || token.selectorsEnd === void 0) return;
|
|
20893
|
+
const from = token.selectorsStart;
|
|
20894
|
+
scanInterpolations(content.slice(from, token.selectorsEnd), (i) => at(from + i), emit, {
|
|
20895
|
+
...options,
|
|
20896
|
+
storeVarMacros: NO_STORE_VAR_MACROS
|
|
20897
|
+
});
|
|
20898
|
+
}
|
|
20697
20899
|
/** Report the `$var` references in the tokens of `content`. */
|
|
20698
20900
|
function collectTokenRefs(content, tokens, at, emit, options) {
|
|
20699
20901
|
const { storeVarMacros, parametersOf } = options;
|
|
20700
20902
|
for (let t = 0; t < tokens.length; t++) {
|
|
20701
20903
|
const token = tokens[t];
|
|
20904
|
+
if (options.all && token.type !== "macro" && token.type !== "html" && token.type !== "text") scanSelectors(content, token, at, emit, options);
|
|
20702
20905
|
if (token.type === "variable") {
|
|
20703
20906
|
if ((token.scope === "variable" || token.scope === "transient") && token.name) {
|
|
20704
20907
|
const [name, ...path] = token.name.split(".");
|
|
@@ -20722,13 +20925,7 @@ function collectTokenRefs(content, tokens, at, emit, options) {
|
|
|
20722
20925
|
}
|
|
20723
20926
|
else if (token.type === "macro" && !token.isClose) {
|
|
20724
20927
|
scanArgs(token, parametersOf(token.name), at, emit, options);
|
|
20725
|
-
|
|
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
|
-
}
|
|
20928
|
+
scanSelectors(content, token, at, emit, options);
|
|
20732
20929
|
if (storeVarMacros.has(token.name.toLowerCase())) {
|
|
20733
20930
|
const lead = token.rawArgs.length - token.rawArgs.trimStart().length;
|
|
20734
20931
|
const first = token.rawArgs.trim().split(/\s+/)[0] ?? "";
|