@rohal12/spindle 0.61.7 → 0.62.0

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.
@@ -9656,34 +9656,6 @@ function parse$1(input, options) {
9656
9656
  }
9657
9657
  //#endregion
9658
9658
  //#region src/js-lexer.ts
9659
- /**
9660
- * The JavaScript in story markup, read with acorn.
9661
- *
9662
- * acorn reads the code: where string, template and regex literals and
9663
- * comments begin and end, whether `/` opens a regex, and (`parseCode`) the
9664
- * syntax tree. A plugin (`SigilParser`) teaches its tokenizer the two sigils
9665
- * that are no JavaScript: `@name` (locals) anywhere, and `%name`
9666
- * (transients) where an operand is expected, the same place acorn reads `/`
9667
- * as a regex; `%` elsewhere is the modulo operator. `$name` and `_name` are
9668
- * identifiers already; the syntax tree tells references from property names
9669
- * and declarations.
9670
- *
9671
- * - `parseCode`: the references and string text of well-formed code, or a
9672
- * `CodeSyntaxError` that says what is wrong and where. The expression
9673
- * engine (`expression.ts`) rewrites the references it finds, and the
9674
- * story-start check (`markup/validate.ts`) reports the errors.
9675
- * - `findCodeEnd`: where the code in a `{…}` ends, for the markup grammar
9676
- * (`markup/code-end.ts`). -1 when acorn can't read it: the grammar then
9677
- * scans it leniently, as prose-like macro arguments (`{link Don't go}`)
9678
- * need.
9679
- * - `lexJs`: a lenient token walk for the macro argument splitters
9680
- * (`components/macros/arg-utils.ts`), which also read text that is no
9681
- * JavaScript.
9682
- *
9683
- * Restrictions (docs/variables.md "Code in passages"): sigil variables can't
9684
- * be declared (`let _x`, `(_a) => …`) or be property names (`a.@x`), and
9685
- * code must be valid modern JavaScript (a non-strict script).
9686
- */
9687
9659
  /** Code runs through `new Function`: a non-strict script body. */
9688
9660
  var OPTIONS = {
9689
9661
  ecmaVersion: "latest",
@@ -10077,13 +10049,13 @@ function propertyName(node, src) {
10077
10049
  * identifiers here are declared (a `let`, a parameter), where a sigil
10078
10050
  * variable can't be. `shorthand`: the node is a shorthand property's value.
10079
10051
  */
10080
- function walk(node, src, out, binding, shorthand) {
10052
+ function walk(node, src, out, binding, shorthand, access = "read") {
10081
10053
  if (Array.isArray(node)) {
10082
- for (const n of node) walk(n, src, out, binding, false);
10054
+ for (const n of node) walk(n, src, out, binding, false, access);
10083
10055
  return;
10084
10056
  }
10085
10057
  if (!isNode(node)) return;
10086
- const sub = (child, bind = false, short = false) => walk(child, src, out, bind, short);
10058
+ const sub = (child, bind = false, short = false, role = "read") => walk(child, src, out, bind, short, role);
10087
10059
  switch (node.type) {
10088
10060
  case "Identifier": {
10089
10061
  const sigil = sigilOf(node, src);
@@ -10094,7 +10066,9 @@ function walk(node, src, out, binding, shorthand) {
10094
10066
  name: sigil[1],
10095
10067
  start: node.start,
10096
10068
  end: node.end,
10097
- shorthand
10069
+ shorthand,
10070
+ read: access !== "write",
10071
+ write: access !== "read"
10098
10072
  });
10099
10073
  return;
10100
10074
  }
@@ -10115,10 +10089,10 @@ function walk(node, src, out, binding, shorthand) {
10115
10089
  if (node.shorthand) {
10116
10090
  const value = node.value;
10117
10091
  if (value.type === "AssignmentPattern") {
10118
- sub(value.left, binding, true);
10092
+ sub(value.left, binding, true, access);
10119
10093
  sub(value.right);
10120
- } else sub(value, binding, true);
10121
- } else sub(node.value, binding);
10094
+ } else sub(value, binding, true, access);
10095
+ } else sub(node.value, binding, false, access);
10122
10096
  return;
10123
10097
  case "MethodDefinition":
10124
10098
  case "PropertyDefinition":
@@ -10154,8 +10128,24 @@ function walk(node, src, out, binding, shorthand) {
10154
10128
  sub(node.param, true);
10155
10129
  sub(node.body);
10156
10130
  return;
10131
+ case "AssignmentExpression":
10132
+ sub(node.left, false, false, node.operator === "=" ? "write" : "readwrite");
10133
+ sub(node.right);
10134
+ return;
10135
+ case "UnaryExpression":
10136
+ sub(node.argument, false, false, node.operator === "delete" ? "write" : "read");
10137
+ return;
10138
+ case "UpdateExpression":
10139
+ sub(node.argument, false, false, "readwrite");
10140
+ return;
10141
+ case "ForInStatement":
10142
+ case "ForOfStatement":
10143
+ sub(node.left, false, false, "write");
10144
+ sub(node.right);
10145
+ sub(node.body);
10146
+ return;
10157
10147
  case "AssignmentPattern":
10158
- sub(node.left, binding);
10148
+ sub(node.left, binding, false, access);
10159
10149
  sub(node.right);
10160
10150
  return;
10161
10151
  case "ObjectPattern":
@@ -10165,7 +10155,7 @@ function walk(node, src, out, binding, shorthand) {
10165
10155
  "properties",
10166
10156
  "elements",
10167
10157
  "argument"
10168
- ]) sub(node[key], binding);
10158
+ ]) sub(node[key], binding, false, access);
10169
10159
  return;
10170
10160
  default: for (const key in node) {
10171
10161
  if (key === "type" || key === "start" || key === "end") continue;
@@ -11161,14 +11151,15 @@ function unescapeQuoted(body) {
11161
11151
  * Read the `"…"` or `'…'` string whose opening quote is at `start`.
11162
11152
  * Returns its unescaped value and the index just past the closing quote, or
11163
11153
  * `null` if there is no such quote at `start` or the string is unterminated.
11154
+ * With `tolerant`, an unterminated string instead extends to its scanned end.
11164
11155
  */
11165
- function readQuoted(src, start) {
11156
+ function readQuoted(src, start, tolerant = false) {
11166
11157
  const quote = src[start];
11167
11158
  if (quote !== "\"" && quote !== "'") return null;
11168
11159
  const { end, closed } = scanStringLiteral(src, start);
11169
- if (!closed) return null;
11160
+ if (!closed && !tolerant) return null;
11170
11161
  return {
11171
- value: unescapeQuoted(src.slice(start + 1, end - 1)),
11162
+ value: unescapeQuoted(src.slice(start + 1, closed ? end - 1 : end)),
11172
11163
  end
11173
11164
  };
11174
11165
  }
@@ -11355,11 +11346,16 @@ var MacroArgumentError = class extends Error {
11355
11346
  };
11356
11347
  /** Read `src`, at `place`, as `param` says. */
11357
11348
  function readValue(param, src, place) {
11358
- place.spans?.set(param, [place.at, place.at + src.length]);
11349
+ const span = [place.at, place.at + src.length];
11350
+ place.spans?.set(param, span);
11351
+ place.toolingSpans?.set(param, span);
11359
11352
  switch (param.type) {
11360
11353
  case "string": {
11361
11354
  const value = readWholeQuoted(src);
11362
- if (value === null) throw new MacroArgumentError(param, src);
11355
+ if (value === null) {
11356
+ if (place.toolingSpans && (src[0] === "\"" || src[0] === "'")) return readText(src);
11357
+ throw new MacroArgumentError(param, src);
11358
+ }
11363
11359
  return value;
11364
11360
  }
11365
11361
  case "text": return readText(src);
@@ -11390,20 +11386,21 @@ function matchAt(re, src, pos) {
11390
11386
  * skipped. A keyword of a `string` parameter must be followed by a quoted
11391
11387
  * string.
11392
11388
  */
11393
- function readOptions(params, src, { at, spans }) {
11389
+ function readOptions(params, src, { at, spans, toolingSpans }) {
11394
11390
  const options = {};
11395
11391
  let i = 0;
11396
11392
  while (i < src.length) {
11397
11393
  const key = matchAt(WORD_RE, src, i);
11398
11394
  if (!key) {
11399
- i = readQuoted(src, i)?.end ?? i + 1;
11395
+ i = readQuoted(src, i, !!toolingSpans)?.end ?? i + 1;
11400
11396
  continue;
11401
11397
  }
11398
+ const keyAt = i;
11402
11399
  i += key.length;
11403
11400
  let val;
11404
11401
  let j = i;
11405
11402
  while (j < src.length && isWhitespace(src[j])) j++;
11406
- const quoted = readQuoted(src, j);
11403
+ const quoted = readQuoted(src, j, !!toolingSpans);
11407
11404
  const digits = quoted || j === i ? null : matchAt(NUMBER_RE$1, src, j);
11408
11405
  if (quoted) {
11409
11406
  val = quoted.value;
@@ -11414,7 +11411,8 @@ function readOptions(params, src, { at, spans }) {
11414
11411
  }
11415
11412
  const param = params.find((p) => p.name === key);
11416
11413
  if (param && param.type !== "flag" && val !== void 0) spans?.set(param, [at + j, at + i]);
11417
- if (param?.type === "string" && !quoted) throw new MacroArgumentError(param, matchAt(WORD_OR_PUNCT_RE, src, j) ?? "");
11414
+ if (param) toolingSpans?.set(param, [at + keyAt, at + i]);
11415
+ if (param?.type === "string" && !quoted && !toolingSpans) throw new MacroArgumentError(param, matchAt(WORD_OR_PUNCT_RE, src, j) ?? "");
11418
11416
  if (param?.type === "flag") options[key] = true;
11419
11417
  else if (param?.type === "number") options[key] = Number(val ?? 0);
11420
11418
  else if (param) options[key] = val;
@@ -11450,7 +11448,7 @@ function takeFlag(src, word, alone) {
11450
11448
  * term with terms on both sides; `=` is an assignment at depth 0, not part
11451
11449
  * of `==` or `!=`.
11452
11450
  */
11453
- function findSeparator(src, word) {
11451
+ function findSeparator(src, word, tolerant = false) {
11454
11452
  if (word === "=") {
11455
11453
  const indices = topLevelIndices(src, (ch) => ch === "=");
11456
11454
  for (let k = 0; k < indices.length; k++) {
@@ -11464,7 +11462,7 @@ function findSeparator(src, word) {
11464
11462
  return null;
11465
11463
  }
11466
11464
  const spans = terms(src);
11467
- for (let k = 1; k < spans.length - 1; k++) if (slice(src, spans[k]) === word) return [spans[k - 1][1], spans[k + 1][0]];
11465
+ for (let k = 1; k < spans.length - (tolerant ? 0 : 1); k++) if (slice(src, spans[k]) === word) return [spans[k - 1][1], spans[k + 1]?.[0] ?? src.length];
11468
11466
  return null;
11469
11467
  }
11470
11468
  var takesRest = (param) => param.type === "expression" || param.type === "statements" || param.type === "passage" || param.type === "text";
@@ -11477,23 +11475,36 @@ function trimmed(src, at) {
11477
11475
  * Read the parameters of one group from `src` (`null`: no text), which is
11478
11476
  * at `at` in the raw arguments, into `args`.
11479
11477
  */
11480
- function readGroup(params, src, args, { at: srcAt, spans }) {
11478
+ function readGroup(params, src, args, { at: srcAt, spans, toolingSpans }) {
11481
11479
  let [text, at] = trimmed(src ?? "", srcAt);
11482
11480
  const alone = !params.some((p) => p.type !== "flag" && p.required);
11483
11481
  for (const flag of params.filter((p) => p.type === "flag")) {
11484
11482
  const rest = src === null ? null : takeFlag(text, flag.name, alone);
11485
11483
  args[flag.name] = rest !== null;
11486
- if (rest) [text, at] = trimmed(slice(text, rest), at + rest[0]);
11484
+ if (rest) {
11485
+ const flagSpan = rest[0] > 0 ? [0, text.indexOf(flag.name) + flag.name.length] : [text.lastIndexOf(flag.name), text.length];
11486
+ toolingSpans?.set(flag, [at + flagSpan[0], at + flagSpan[1]]);
11487
+ [text, at] = trimmed(slice(text, rest), at + rest[0]);
11488
+ }
11487
11489
  }
11488
11490
  const positional = params.filter((p) => p.type !== "flag");
11489
11491
  if (positional.length === 0) return;
11490
11492
  const lastText = positional.findIndex((p, i) => takesRest(p) && !positional.slice(i + 1).some(takesRest));
11491
11493
  const restIndex = lastText < 0 ? positional.length - 1 : lastText;
11492
11494
  /** Read `param` from the text from `start` to `end`. */
11493
- const read = (param, [start, end]) => start === end ? unset(param) : readValue(param, text.slice(start, end), {
11494
- at: at + start,
11495
- spans
11496
- });
11495
+ const read = (param, [start, end], tolerant = true) => {
11496
+ if (start === end) return unset(param);
11497
+ try {
11498
+ return readValue(param, text.slice(start, end), {
11499
+ at: at + start,
11500
+ spans,
11501
+ toolingSpans
11502
+ });
11503
+ } catch (error) {
11504
+ if (toolingSpans && tolerant && error instanceof MacroArgumentError) return;
11505
+ throw error;
11506
+ }
11507
+ };
11497
11508
  if (positional.length === 1) {
11498
11509
  args[positional[0].name] = read(positional[0], [0, text.length]);
11499
11510
  return;
@@ -11504,7 +11515,7 @@ function readGroup(params, src, args, { at: srcAt, spans }) {
11504
11515
  for (const param of positional.slice(0, restIndex)) args[param.name] = from < to ? read(param, parts[from++]) : unset(param);
11505
11516
  const readTrailing = (param, span) => {
11506
11517
  try {
11507
- return read(param, span);
11518
+ return read(param, span, false);
11508
11519
  } catch (error) {
11509
11520
  if (error instanceof MacroArgumentError) return void 0;
11510
11521
  throw error;
@@ -11514,7 +11525,10 @@ function readGroup(params, src, args, { at: srcAt, spans }) {
11514
11525
  const value = from < to && !endsWithOperator(text.slice(0, parts[to - 1][0])) ? readTrailing(param, parts[to - 1]) : void 0;
11515
11526
  args[param.name] = value ?? unset(param);
11516
11527
  if (value !== void 0) to--;
11517
- else spans?.delete(param);
11528
+ else {
11529
+ spans?.delete(param);
11530
+ toolingSpans?.delete(param);
11531
+ }
11518
11532
  }
11519
11533
  const rest = positional[restIndex];
11520
11534
  args[rest.name] = from < to ? read(rest, [parts[from][0], parts[to - 1][1]]) : unset(rest);
@@ -11529,18 +11543,23 @@ var DELAY_PARAMETER = {
11529
11543
  * Read `rawArgs` into the declared `parameters` (see above). With `spans`,
11530
11544
  * note there where each argument set was read.
11531
11545
  */
11532
- function parseMacroArgs(rawArgs, parameters, spans) {
11546
+ function parseMacroArgs(rawArgs, parameters, spans, toolingSpans) {
11533
11547
  const args = {};
11534
11548
  let [rest, restAt] = trimmed(rawArgs, 0);
11535
11549
  let groupStart = 0;
11536
11550
  parameters.forEach((separator, i) => {
11537
11551
  if (separator.type !== "separator") return;
11538
- const at = rest === null ? null : findSeparator(rest, separator.name);
11552
+ const at = rest === null ? null : findSeparator(rest, separator.name, !!toolingSpans);
11539
11553
  args[separator.name] = at !== null;
11554
+ if (at && toolingSpans) {
11555
+ const start = restAt + at[0] + rest.slice(at[0], at[1]).indexOf(separator.name);
11556
+ toolingSpans.set(separator, [start, start + separator.name.length]);
11557
+ }
11540
11558
  const before = at ? rest.slice(0, at[0]) : rest;
11541
11559
  readGroup(parameters.slice(groupStart, i), before, args, {
11542
11560
  at: restAt,
11543
- spans
11561
+ spans,
11562
+ toolingSpans
11544
11563
  });
11545
11564
  rest = at ? rest.slice(at[1]) : null;
11546
11565
  if (at) restAt += at[1];
@@ -11548,7 +11567,8 @@ function parseMacroArgs(rawArgs, parameters, spans) {
11548
11567
  });
11549
11568
  readGroup(parameters.slice(groupStart), rest, args, {
11550
11569
  at: restAt,
11551
- spans
11570
+ spans,
11571
+ toolingSpans
11552
11572
  });
11553
11573
  return args;
11554
11574
  }
package/dist/pkg/play.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { glob, readFile, stat } from "node:fs/promises";
2
- import { isAbsolute, relative, resolve, sep } from "node:path";
2
+ import { resolve } from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
4
  import { Worker } from "node:worker_threads";
5
5
  //#region src/utils/error-message.ts
@@ -62,44 +62,6 @@ async function expandGlob(pattern) {
62
62
  for await (const file of glob(pattern)) matches.push(file);
63
63
  return matches.sort();
64
64
  }
65
- /** A path segment with glob syntax in it (as `path.matchesGlob` reads it). */
66
- var GLOB_SEGMENT = /[*?[\]{}()!+@]/;
67
- /**
68
- * An `exclude` glob of the project config as twee-ts matches it: against
69
- * paths relative to the working directory. A relative glob is relative to the
70
- * project directory; an absolute one has its literal leading folders made
71
- * relative. Either way the folder part added or rewritten is escaped.
72
- */
73
- function rebaseExclude(dir, pattern) {
74
- let base = dir;
75
- let rest = pattern.replace(/^\.\//, "");
76
- if (isAbsolute(pattern)) {
77
- const parts = pattern.split("/");
78
- let literal = parts.findIndex((part) => GLOB_SEGMENT.test(part));
79
- if (literal === -1) literal = parts.length;
80
- base = parts.slice(0, literal).join("/") || "/";
81
- rest = parts.slice(literal).join("/");
82
- }
83
- const fromCwd = globLiteral(relative(process.cwd(), base).split(sep).join("/"));
84
- if (fromCwd === "") return rest;
85
- return rest === "" ? fromCwd : `${fromCwd}/${rest}`;
86
- }
87
- /**
88
- * The project config with its paths made independent of the working
89
- * directory. twee-ts reads `sources`, `modules` and `headFile` relative to
90
- * the working directory, and matches `exclude` globs against paths relative
91
- * to it, so each is rebased from the project directory.
92
- */
93
- function rebaseConfig(dir, config) {
94
- const rebased = {
95
- ...config,
96
- sources: (config.sources ?? ["."]).map((s) => resolve(dir, s))
97
- };
98
- if (config.modules) rebased.modules = config.modules.map((m) => resolve(dir, m));
99
- if (config.headFile !== void 0) rebased.headFile = resolve(dir, config.headFile);
100
- if (config.exclude) rebased.exclude = config.exclude.map((e) => rebaseExclude(dir, e));
101
- return rebased;
102
- }
103
65
  /**
104
66
  * The project config less its output and format options. Those are ours: the
105
67
  * project's own would write a file, compile for another format, or fetch one.
@@ -107,14 +69,6 @@ function rebaseConfig(dir, config) {
107
69
  function withoutFormatOptions(config) {
108
70
  return Object.fromEntries(Object.entries(config).filter(([key]) => !key.startsWith("format") && !key.startsWith("output") && key !== "useTweegoPath" && key !== "noRemote"));
109
71
  }
110
- /**
111
- * `text` as a glob matching only itself, for Node's `path.matchesGlob` (which
112
- * twee-ts uses): a metacharacter goes in brackets. Braces and `!` cannot be
113
- * bracketed there, so each becomes `?` (any one character).
114
- */
115
- function globLiteral(text) {
116
- return text.replace(/[*?[\]()+@]/g, (c) => `[${c}]`).replace(/[{}!]/g, "?");
117
- }
118
72
  /** The compile sources and project config for a path (or glob). */
119
73
  async function sourcesFor(tweeTs, path) {
120
74
  let isDirectory = false;
@@ -138,10 +92,10 @@ async function sourcesFor(tweeTs, path) {
138
92
  sources: [path],
139
93
  config: {}
140
94
  };
141
- const config = rebaseConfig(resolve(path), found);
95
+ const config = found;
142
96
  if (config.sources?.length === 0) throw new Error(`spindle: ${path} has a twee-ts config with no sources`);
143
97
  return {
144
- sources: config.sources ?? [],
98
+ sources: [...config.sources ?? [resolve(path)]],
145
99
  config
146
100
  };
147
101
  }