@lucidkiln/core 1.7.1 → 1.9.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.
package/dist/node.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { p as Loader } from "./environment-BQipJ3kg.js";
1
+ import { p as Loader } from "./environment-DcH713WK.js";
2
2
  //#region src/node.d.ts
3
3
  export type FileLoaderOptions = {
4
4
  /** Directory that template names are relative to. */
@@ -127,6 +127,11 @@ const lex = (text) => new Lexer(text).run();
127
127
  * Splits source into text, outputs, tags and comments as written, before whitespace control. Tools such as the formatter use it. Comments have no tokens.
128
128
  */
129
129
  const scanParts = (text) => new Lexer(text).scan();
130
+ /**
131
+ * Splits source into parts after whitespace control, so each text part's `from` and `to` are what it renders. Also
132
+ * returns the tags and comments removed with their lines, as `lex` does. The formatter uses it.
133
+ */
134
+ const scanControlled = (text) => new Lexer(text).controlled();
130
135
  /** Where a template's content starts: after a byte order mark, which is never content. */
131
136
  const contentStart = (text) => text.charCodeAt(0) === 65279 ? 1 : 0;
132
137
  var Lexer = class {
@@ -146,10 +151,16 @@ var Lexer = class {
146
151
  problems: this.#problems
147
152
  };
148
153
  }
149
- run() {
154
+ controlled() {
150
155
  const parts = this.#scanParts();
151
156
  applyTrimMarkers(this.#text, parts);
152
- const standalone = applyStandaloneLines(this.#text, parts);
157
+ return {
158
+ parts,
159
+ standalone: applyStandaloneLines(this.#text, parts)
160
+ };
161
+ }
162
+ run() {
163
+ const { parts, standalone } = this.controlled();
153
164
  const lines = textLines(this.#text, parts, standalone);
154
165
  const tokens = [];
155
166
  for (const part of parts) if (part.kind === "text") {
@@ -689,10 +700,15 @@ const textLines = (text, parts, removed) => {
689
700
  indentEnd
690
701
  });
691
702
  };
703
+ let newline = -2;
692
704
  for (const part of parts) {
693
705
  if (part.kind !== "text") continue;
694
- for (let newline = newlineIn(text, part.start, part.end); newline !== -1; newline = newlineIn(text, newline + 1, part.end)) {
706
+ let at = part.start;
707
+ for (;;) {
708
+ if (newline !== -1 && newline < at) newline = text.indexOf("\n", at);
709
+ if (newline === -1 || newline >= part.end) break;
695
710
  const start = newline + 1;
711
+ at = start;
696
712
  if (start < part.from || start > part.to) continue;
697
713
  add(start);
698
714
  }
@@ -747,33 +763,76 @@ const firstAtLeast = (items, value, key) => {
747
763
  return low;
748
764
  };
749
765
  /**
750
- * Finds `<pre>` and `<textarea>` tags, and the HTML comments, scripts and styles to skip, whose `<pre` is not a tag.
751
- * Each region runs to its end, or to the end of the source when it is not closed.
766
+ * The start of an HTML comment, script or style to skip, whose `<pre` is not a tag, or a `<pre>` or `<textarea>` tag.
767
+ * Takes the character after a name, which must be in the same text token.
752
768
  */
753
- const MARKUP = /<!--[\s\S]*?(?:-->|$)|<(script|style)(?=[\s/>])[\s\S]*?(?:<\/\1\s*>|$)|<(\/?)(pre|textarea)(?=[\s/>])/gi;
769
+ const MARKUP = /<(?:(!--)|(script|style)[\s/>]|(\/?)(pre|textarea)[\s/>])/gi;
770
+ const COMMENT_END = /-->/g;
771
+ const SCRIPT_END = /<\/script\s*>/gi;
772
+ const STYLE_END = /<\/style\s*>/gi;
754
773
  const MAY_PREFORMAT = /<(?:pre|textarea)/i;
755
- /** Where `<pre>` and `<textarea>` elements start and end in the template's text, whose whitespace is content. */
774
+ /**
775
+ * Finds a global `pattern`'s first match at or after an offset. Keeps the last match while it is still ahead, so
776
+ * offsets that only grow scan the text once, however many tokens ask.
777
+ */
778
+ const searcher = (text, pattern) => {
779
+ let found;
780
+ return (from) => {
781
+ if (found === void 0 || found !== null && found.index < from) {
782
+ pattern.lastIndex = from;
783
+ found = pattern.exec(text);
784
+ }
785
+ return found;
786
+ };
787
+ };
788
+ /**
789
+ * Where `<pre>` and `<textarea>` elements start and end in the template's text, whose whitespace is content.
790
+ *
791
+ * Reads only the text tokens. A tag or comment cannot be split by a Kiln tag or output, so `<pre{{ x }}>` is not a
792
+ * `<pre>`, but the inside of an HTML comment, script or style can. Each runs to its end, or to the end of the source.
793
+ */
756
794
  const preformatted = (text, textTokens) => {
757
795
  if (!MAY_PREFORMAT.test(text)) return [];
758
- let visible = "";
759
- for (const token of textTokens) {
760
- visible += "\0".repeat(token.start - visible.length);
761
- visible += text.slice(token.start, token.end);
762
- }
796
+ const markup = searcher(text, MARKUP);
797
+ const ends = {
798
+ comment: searcher(text, COMMENT_END),
799
+ script: searcher(text, SCRIPT_END),
800
+ style: searcher(text, STYLE_END)
801
+ };
763
802
  const ranges = [];
764
803
  let open = null;
765
- MARKUP.lastIndex = 0;
766
- for (let match = MARKUP.exec(visible); match; match = MARKUP.exec(visible)) {
767
- const tag = match[3];
768
- if (tag === void 0) continue;
769
- const name = tag.toLowerCase();
770
- if (open === null && match[2] === "") open = {
771
- name,
772
- start: match.index
773
- };
774
- else if (open?.name === name && match[2] === "/") {
775
- ranges.push([open.start, match.index]);
776
- open = null;
804
+ let skip = null;
805
+ for (const { start, end } of textTokens) {
806
+ let at = start;
807
+ while (at < end) {
808
+ if (skip) {
809
+ const close = ends[skip](at);
810
+ if (close === null || close.index + close[0].length > end) break;
811
+ at = close.index + close[0].length;
812
+ skip = null;
813
+ }
814
+ const match = markup(at);
815
+ const after = match ? match.index + match[0].length : 0;
816
+ if (match === null || after > end) break;
817
+ const [, comment, raw, slash, tag] = match;
818
+ if (comment) {
819
+ skip = "comment";
820
+ at = after;
821
+ continue;
822
+ }
823
+ at = after - 1;
824
+ if (raw) skip = raw.toLowerCase() === "script" ? "script" : "style";
825
+ else if (tag) {
826
+ const name = tag.toLowerCase();
827
+ if (open === null && slash === "") open = {
828
+ name,
829
+ start: match.index
830
+ };
831
+ else if (open?.name === name && slash === "/") {
832
+ ranges.push([open.start, match.index]);
833
+ open = null;
834
+ }
835
+ }
777
836
  }
778
837
  }
779
838
  if (open) ranges.push([open.start, text.length]);
@@ -935,6 +994,11 @@ const parse = (text, options) => {
935
994
  problems: [...lexed.problems, ...parser.problems]
936
995
  };
937
996
  };
997
+ /**
998
+ * Deepest nesting of expressions inside expressions, such as brackets or a call's arguments. Each of those levels takes
999
+ * far more of the call stack to parse than a tag's, so they have a lower limit.
1000
+ */
1001
+ const MAX_EXPRESSION_NESTING = 100;
938
1002
  var ParseFailure = class {
939
1003
  code;
940
1004
  message;
@@ -986,7 +1050,13 @@ var Parser = class {
986
1050
  #options;
987
1051
  #index = 0;
988
1052
  #lastEnd = 0;
989
- #nesting = 0;
1053
+ /**
1054
+ * How many levels the cursor is inside: tag bodies, expressions and operators that nest their operand, such as
1055
+ * `not`. The template's own body doesn't count.
1056
+ */
1057
+ #nesting = -1;
1058
+ /** How many expressions the cursor is inside, such as one in brackets inside another. */
1059
+ #expressions = 0;
990
1060
  /** End tag names of every open block tag, innermost last. */
991
1061
  #endStack = [];
992
1062
  constructor(tokens, options) {
@@ -1351,7 +1421,7 @@ var Parser = class {
1351
1421
  const alternate = [];
1352
1422
  for (;;) {
1353
1423
  const { body, end } = this.#parseBody([endName, ...branches]);
1354
- alternate.push(...body);
1424
+ for (const node of body) alternate.push(node);
1355
1425
  if (!end) {
1356
1426
  this.#unclosed(name, open, endName);
1357
1427
  return alternate;
@@ -1436,10 +1506,11 @@ var Parser = class {
1436
1506
  /** Reads parameters for `macro` and `props`. Only `props` may declare types, so macros' `type` is always null. */
1437
1507
  #parseParams(closing, typed = false) {
1438
1508
  const params = [];
1509
+ const names = /* @__PURE__ */ new Set();
1439
1510
  const done = () => closing === null ? this.#peek().type === "tagClose" || this.#isPunct("...") : this.#isPunct(closing);
1440
1511
  while (!done()) {
1441
1512
  const nameToken = this.#expectDeclarableName("a parameter name");
1442
- if (params.some((param) => param.name === nameToken.value)) this.#problem("duplicate-parameter", `"${nameToken.value}" is declared more than once`, nameToken);
1513
+ if (names.has(nameToken.value)) this.#problem("duplicate-parameter", `"${nameToken.value}" is declared more than once`, nameToken);
1443
1514
  let type = null;
1444
1515
  if (typed && this.#isPunct(":")) {
1445
1516
  this.#next();
@@ -1450,6 +1521,7 @@ var Parser = class {
1450
1521
  this.#next();
1451
1522
  defaultValue = this.#parseExpression();
1452
1523
  }
1524
+ names.add(nameToken.value);
1453
1525
  params.push({
1454
1526
  name: nameToken.value,
1455
1527
  type,
@@ -1479,7 +1551,7 @@ var Parser = class {
1479
1551
  type = [{ list: type }];
1480
1552
  }
1481
1553
  this.#nesting = nesting;
1482
- members.push(...type);
1554
+ for (const member of type) members.push(member);
1483
1555
  if (!this.#isPunct("|")) return members;
1484
1556
  this.#next();
1485
1557
  }
@@ -1512,16 +1584,18 @@ var Parser = class {
1512
1584
  #parseShape() {
1513
1585
  this.#expectPunct("{");
1514
1586
  const keys = [];
1587
+ const names = /* @__PURE__ */ new Set();
1515
1588
  while (!this.#isPunct("}")) {
1516
1589
  const token = this.#next();
1517
1590
  if (token.type !== "name" && token.type !== "string") this.#fail(`Expected a key but found ${describeToken(token)}`, token);
1518
- if (keys.some((key) => key.name === token.value)) this.#problem("duplicate-key", `"${token.value}" is declared more than once`, token);
1591
+ if (names.has(token.value)) this.#problem("duplicate-key", `"${token.value}" is declared more than once`, token);
1519
1592
  const optional = this.#isPunct("?:") || this.#isPunct("?");
1520
1593
  if (this.#isPunct("?:")) this.#next();
1521
1594
  else {
1522
1595
  if (optional) this.#next();
1523
1596
  this.#expectPunct(":");
1524
1597
  }
1598
+ names.add(token.value);
1525
1599
  keys.push({
1526
1600
  name: token.value,
1527
1601
  optional,
@@ -1644,9 +1718,10 @@ var Parser = class {
1644
1718
  });
1645
1719
  const body = [];
1646
1720
  const fills = [];
1721
+ const filled = /* @__PURE__ */ new Set();
1647
1722
  for (;;) {
1648
1723
  const part = this.#parseBody(["fill", "endcomponent"]);
1649
- body.push(...part.body);
1724
+ for (const node of part.body) body.push(node);
1650
1725
  if (!part.end) {
1651
1726
  this.#unclosed("component", open, "endcomponent");
1652
1727
  break;
@@ -1660,10 +1735,11 @@ var Parser = class {
1660
1735
  const fill = this.#parseBody(["endfill"]);
1661
1736
  if (fill.end) this.#finishEndTag();
1662
1737
  else this.#unclosed("fill", fillStart, "endfill");
1663
- if (fills.some((existing) => existing.name === name)) this.#problem("duplicate-fill", `Slot "${name}" is filled more than once`, {
1738
+ if (filled.has(name)) this.#problem("duplicate-fill", `Slot "${name}" is filled more than once`, {
1664
1739
  start: fillStart.start,
1665
1740
  end: fillStart.end
1666
1741
  });
1742
+ filled.add(name);
1667
1743
  fills.push({
1668
1744
  name,
1669
1745
  body: fill.body,
@@ -1973,11 +2049,23 @@ var Parser = class {
1973
2049
  this.#fail(`Template is nested more than 200 levels deep`, this.#peek(), "too-deep");
1974
2050
  }
1975
2051
  }
2052
+ /** Runs `parse` one level deeper. See `#enter`. */
2053
+ #nested(parse) {
2054
+ this.#enter();
2055
+ try {
2056
+ return parse();
2057
+ } finally {
2058
+ this.#nesting--;
2059
+ }
2060
+ }
2061
+ /** Reads an expression. Each one inside another, such as in brackets or a call's arguments, is a level deeper. */
1976
2062
  #parseExpression() {
1977
2063
  this.#enter();
1978
2064
  try {
2065
+ if (++this.#expressions > MAX_EXPRESSION_NESTING) this.#fail(`Expressions are nested inside each other more than ${MAX_EXPRESSION_NESTING} levels deep`, this.#peek(), "too-deep");
1979
2066
  return this.#parseConditional();
1980
2067
  } finally {
2068
+ this.#expressions--;
1981
2069
  this.#nesting--;
1982
2070
  }
1983
2071
  }
@@ -2025,12 +2113,7 @@ var Parser = class {
2025
2113
  const left = this.#parseOr();
2026
2114
  if (!this.#isPunct("??")) return left;
2027
2115
  this.#next();
2028
- this.#enter();
2029
- try {
2030
- return this.#binary("??", left, this.#parseCoalesce());
2031
- } finally {
2032
- this.#nesting--;
2033
- }
2116
+ return this.#nested(() => this.#binary("??", left, this.#parseCoalesce()));
2034
2117
  }
2035
2118
  #parseOr() {
2036
2119
  let left = this.#parseAnd();
@@ -2049,15 +2132,8 @@ var Parser = class {
2049
2132
  return left;
2050
2133
  }
2051
2134
  #parseNot() {
2052
- this.#enter();
2053
- try {
2054
- return this.#parseNotInner();
2055
- } finally {
2056
- this.#nesting--;
2057
- }
2058
- }
2059
- #parseNotInner() {
2060
- if (this.#isName("not")) {
2135
+ if (!this.#isName("not")) return this.#parseComparison();
2136
+ return this.#nested(() => {
2061
2137
  const token = this.#next();
2062
2138
  const operand = this.#parseNot();
2063
2139
  return {
@@ -2069,8 +2145,7 @@ var Parser = class {
2069
2145
  end: operand.span.end
2070
2146
  }
2071
2147
  };
2072
- }
2073
- return this.#parseComparison();
2148
+ });
2074
2149
  }
2075
2150
  /** Reads a comparison operator at the cursor without consuming it, and how many tokens it spans. */
2076
2151
  #comparisonAhead() {
@@ -2185,15 +2260,8 @@ var Parser = class {
2185
2260
  }
2186
2261
  }
2187
2262
  #parseUnary() {
2188
- this.#enter();
2189
- try {
2190
- return this.#parseUnaryInner();
2191
- } finally {
2192
- this.#nesting--;
2193
- }
2194
- }
2195
- #parseUnaryInner() {
2196
- if (this.#isPunct("-") || this.#isPunct("+")) {
2263
+ if (!this.#isPunct("-") && !this.#isPunct("+")) return this.#parsePower();
2264
+ return this.#nested(() => {
2197
2265
  const token = this.#next();
2198
2266
  const operand = this.#parseUnary();
2199
2267
  return {
@@ -2205,16 +2273,15 @@ var Parser = class {
2205
2273
  end: operand.span.end
2206
2274
  }
2207
2275
  };
2208
- }
2209
- return this.#parsePower();
2276
+ });
2210
2277
  }
2211
2278
  #parsePower() {
2212
2279
  const base = this.#parsePostfix();
2213
- if (this.#isPunct("**")) {
2280
+ if (!this.#isPunct("**")) return base;
2281
+ return this.#nested(() => {
2214
2282
  this.#next();
2215
2283
  return this.#binary("**", base, this.#parseUnary());
2216
- }
2217
- return base;
2284
+ });
2218
2285
  }
2219
2286
  #parsePostfix() {
2220
2287
  let expression = this.#parsePrimary();
@@ -2637,6 +2704,6 @@ var Parser = class {
2637
2704
  }
2638
2705
  };
2639
2706
  //#endregion
2640
- export { ESCAPE_STRATEGIES as a, DEFAULT_SLOT as i, contentStart as n, scanParts as r, parse as t };
2707
+ export { DEFAULT_SLOT as a, scanParts as i, contentStart as n, ESCAPE_STRATEGIES as o, scanControlled as r, parse as t };
2641
2708
 
2642
- //# sourceMappingURL=parser-BZpRlGWi.js.map
2709
+ //# sourceMappingURL=parser-CRWl5Lka.js.map