@marko/language-tools 2.5.4 → 2.5.6

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/index.mjs CHANGED
@@ -1614,6 +1614,120 @@ function tryReaddirSync(dir) {
1614
1614
  }
1615
1615
  }
1616
1616
 
1617
+ // src/extractors/script/util/get-runtime-api.ts
1618
+ var RuntimeAPI = {
1619
+ tags: "tags",
1620
+ class: "class"
1621
+ };
1622
+ function getRuntimeAPI(translator, parsed) {
1623
+ return detectAPIFromTranslator(translator) || detectAPIFromFileName(parsed.filename) || detectAPIFromProgram(parsed, parsed.program);
1624
+ }
1625
+ function detectAPIFromTranslator(translator) {
1626
+ switch (translator == null ? void 0 : translator.preferAPI) {
1627
+ case "class":
1628
+ return RuntimeAPI.class;
1629
+ case "tags":
1630
+ return RuntimeAPI.tags;
1631
+ }
1632
+ }
1633
+ function detectAPIFromFileName(filename) {
1634
+ for (let end = filename.length, i = end; --i; ) {
1635
+ switch (filename[i]) {
1636
+ case "/":
1637
+ case "\\":
1638
+ if (filename.startsWith("tags", i + 1)) {
1639
+ return RuntimeAPI.tags;
1640
+ } else if (filename.startsWith("components", i + 1)) {
1641
+ return;
1642
+ }
1643
+ end = i;
1644
+ break;
1645
+ }
1646
+ }
1647
+ }
1648
+ function detectAPIFromProgram(parsed, program) {
1649
+ if (program.comments) {
1650
+ switch (parsed.read(program.comments[0].value).trim()) {
1651
+ case "use tags":
1652
+ return RuntimeAPI.tags;
1653
+ case "use class":
1654
+ return RuntimeAPI.class;
1655
+ }
1656
+ }
1657
+ if (program.static) {
1658
+ for (const stmt of program.static) {
1659
+ switch (stmt.type) {
1660
+ case 26 /* Class */:
1661
+ case 27 /* Style */:
1662
+ return RuntimeAPI.class;
1663
+ }
1664
+ }
1665
+ }
1666
+ return detectAPIFromBody(parsed, program.body);
1667
+ }
1668
+ function detectAPIFromBody(parsed, body) {
1669
+ if (body) {
1670
+ for (const child of body) {
1671
+ const api = detectAPIFromChild(parsed, child);
1672
+ if (api) return api;
1673
+ }
1674
+ }
1675
+ }
1676
+ function detectAPIFromChild(parsed, child) {
1677
+ switch (child.type) {
1678
+ case 23 /* Scriptlet */:
1679
+ return RuntimeAPI.class;
1680
+ case 1 /* Tag */:
1681
+ case 16 /* AttrTag */:
1682
+ return detectAPIFromTag(parsed, child);
1683
+ }
1684
+ }
1685
+ function detectAPIFromTag(parsed, tag) {
1686
+ var _a;
1687
+ switch (tag.nameText) {
1688
+ case "macro":
1689
+ case "include-text":
1690
+ case "include-html":
1691
+ case "init-components":
1692
+ case "await-reorderer":
1693
+ case "while":
1694
+ case "module-code":
1695
+ return RuntimeAPI.class;
1696
+ case "const":
1697
+ case "debug":
1698
+ case "define":
1699
+ case "html-script":
1700
+ case "html-style":
1701
+ case "id":
1702
+ case "let":
1703
+ case "lifecycle":
1704
+ case "log":
1705
+ case "return":
1706
+ return RuntimeAPI.tags;
1707
+ }
1708
+ if (tag.var) {
1709
+ return RuntimeAPI.tags;
1710
+ }
1711
+ if (tag.attrs) {
1712
+ for (const attr of tag.attrs) {
1713
+ if (attr.type !== 15 /* AttrSpread */) {
1714
+ if (((_a = attr.value) == null ? void 0 : _a.type) === 13 /* AttrValue */ && attr.value.bound) {
1715
+ return RuntimeAPI.tags;
1716
+ }
1717
+ if (attr.args) {
1718
+ return RuntimeAPI.class;
1719
+ }
1720
+ if (/^(?:key|no-update(?:-body)?(?:-if)?)$|:(scoped:no-update)$/.test(
1721
+ parsed.read(attr.name)
1722
+ )) {
1723
+ return RuntimeAPI.class;
1724
+ }
1725
+ }
1726
+ }
1727
+ }
1728
+ return detectAPIFromBody(parsed, tag.body);
1729
+ }
1730
+
1617
1731
  // src/extractors/script/util/jsdoc-input-type.ts
1618
1732
  var MaybeInputTypedefReg = /@typedef\b[\s\S]*\bInput\b/;
1619
1733
  function getJSDocInputType(comment, ts) {
@@ -1793,7 +1907,7 @@ var REG_TAG_IMPORT = /(?<=(['"]))<([^'">]+)>(?=\1)/;
1793
1907
  var REG_INPUT_TYPE = /\s*(interface|type)\s+Input\b/y;
1794
1908
  var REG_OBJECT_PROPERTY = /^[_$a-z][_$a-z0-9]*$/i;
1795
1909
  var REG_COMMENT_PRAGMA = /\/\/(?:\s*@ts-|\/\s*<)/y;
1796
- var REG_TAG_NAME_IDENTIFIER = /^[A-Z][a-zA-Z_$]+$/;
1910
+ var REG_TAG_NAME_IDENTIFIER = /^[A-Z][a-zA-Z0-9_$]+$/;
1797
1911
  var IF_TAG_ALTERNATES = /* @__PURE__ */ new WeakMap();
1798
1912
  var WROTE_COMMENT = /* @__PURE__ */ new WeakSet();
1799
1913
  var START_OF_FILE = { start: 0, end: 0 };
@@ -1813,11 +1927,14 @@ var ScriptExtractor = class {
1813
1927
  #scriptParser;
1814
1928
  #read;
1815
1929
  #lookup;
1930
+ #tagIds = /* @__PURE__ */ new Map();
1816
1931
  #renderIds = /* @__PURE__ */ new Map();
1817
1932
  #scriptLang;
1818
1933
  #ts;
1819
1934
  #runtimeTypes;
1935
+ #api;
1820
1936
  #mutationOffsets;
1937
+ #tagId = 1;
1821
1938
  #renderId = 1;
1822
1939
  constructor(opts) {
1823
1940
  const { parsed, lookup, scriptLang } = opts;
@@ -1831,6 +1948,7 @@ var ScriptExtractor = class {
1831
1948
  this.#extractor = new Extractor(parsed);
1832
1949
  this.#scriptParser = new ScriptParser(parsed);
1833
1950
  this.#read = parsed.read.bind(parsed);
1951
+ this.#api = getRuntimeAPI(opts.translator, parsed);
1834
1952
  this.#mutationOffsets = crawlProgramScope(this.#parsed, this.#scriptParser);
1835
1953
  this.#writeProgram(parsed.program);
1836
1954
  }
@@ -1989,8 +2107,8 @@ function ${templateName}() {
1989
2107
  ${varShared("noop")}({ input, component, state, out, $global, $signal });
1990
2108
  `);
1991
2109
  const body = this.#processBody(program);
1992
- if (body == null ? void 0 : body.renderBody) {
1993
- this.#writeChildren(program, body.renderBody);
2110
+ if (body == null ? void 0 : body.content) {
2111
+ this.#writeChildren(program, body.content);
1994
2112
  }
1995
2113
  const hoists = getHoists(program);
1996
2114
  if (hoists) {
@@ -2021,6 +2139,7 @@ function ${templateName}() {
2021
2139
  "ReturnWithScope"
2022
2140
  )}<${internalInput}, ${didReturn ? `typeof ${templateName + typeArgsStr} extends () => infer Return ? Return : never` : "void"}>)`;
2023
2141
  const templateOverrideClass = `${templateBaseClass}<{${this.#runtimeTypes ? getRuntimeOverrides(this.#runtimeTypes, typeParamsStr, typeArgsStr) : ""}
2142
+ ${this.#api ? `api: "${this.#api}",` : ""}
2024
2143
  _${typeParamsStr ? `<${internalApply} = 1>(): ${internalApply} extends 0
2025
2144
  ? ${typeParamsStr}() => <${internalInputWithExtends}>${renderAndReturn}
2026
2145
  : () => <${internalInputWithExtends}, ${typeParamsStr.slice(
@@ -2127,9 +2246,9 @@ constructor(_?: Return) {}
2127
2246
  this.#getRangeWithoutTrailingComma((_a = child.args) == null ? void 0 : _a.value) || this.#getAttrValue(child, ATTR_UNAMED2) || "undefined"
2128
2247
  ).write(") {\n");
2129
2248
  const ifBody = this.#processBody(child);
2130
- if (ifBody == null ? void 0 : ifBody.renderBody) {
2249
+ if (ifBody == null ? void 0 : ifBody.content) {
2131
2250
  const localBindings = getHoistSources(child);
2132
- this.#writeChildren(child, ifBody.renderBody, true);
2251
+ this.#writeChildren(child, ifBody.content, true);
2133
2252
  if (localBindings) {
2134
2253
  this.#extractor.write("return {\nscope:");
2135
2254
  this.#writeObjectKeys(localBindings);
@@ -2149,9 +2268,9 @@ constructor(_?: Return) {}
2149
2268
  this.#extractor.write("\n} else if (undefined) {\n");
2150
2269
  }
2151
2270
  const alternateBody = this.#processBody(node);
2152
- if (alternateBody == null ? void 0 : alternateBody.renderBody) {
2271
+ if (alternateBody == null ? void 0 : alternateBody.content) {
2153
2272
  const localBindings = getHoistSources(node);
2154
- this.#writeChildren(node, alternateBody.renderBody, true);
2273
+ this.#writeChildren(node, alternateBody.content, true);
2155
2274
  if (localBindings) {
2156
2275
  this.#extractor.write("return {\nscope:");
2157
2276
  this.#writeObjectKeys(localBindings);
@@ -2179,25 +2298,26 @@ constructor(_?: Return) {}
2179
2298
  )}, ${renderId}, `
2180
2299
  );
2181
2300
  }
2182
- this.#extractor.write(`${varShared("forTag")}({
2183
- `);
2301
+ this.#extractor.write(
2302
+ `${varShared(getForTagRuntime(this.#parsed, child))}({
2303
+ `
2304
+ );
2305
+ this.#writeTagNameComment(child);
2184
2306
  this.#writeAttrs(child);
2185
- this.#extractor.write(`["renderBody"/*`).copy(child.name).write(`*/]: (`);
2307
+ this.#extractor.write("\n}" + SEP_COMMA_NEW_LINE).copy(child.typeParams).write("(\n");
2186
2308
  this.#writeComments(child);
2187
- this.#extractor.copy(child.typeParams).write("(\n");
2188
2309
  if (child.params) {
2189
2310
  this.#copyWithMutationsReplaced(child.params.value);
2190
2311
  }
2191
2312
  this.#extractor.write("\n) => {\n");
2192
2313
  const body = this.#processBody(child);
2193
- if (body == null ? void 0 : body.renderBody) {
2194
- this.#writeChildren(child, body.renderBody);
2314
+ if (body == null ? void 0 : body.content) {
2315
+ this.#writeChildren(child, body.content);
2195
2316
  }
2196
2317
  this.#writeReturn(
2197
2318
  void 0,
2198
- (body == null ? void 0 : body.renderBody) ? getHoistSources(child) : void 0
2319
+ (body == null ? void 0 : body.content) ? getHoistSources(child) : void 0
2199
2320
  );
2200
- this.#extractor.write("})");
2201
2321
  if (renderId) {
2202
2322
  this.#extractor.write("\n}));\n");
2203
2323
  } else {
@@ -2211,8 +2331,8 @@ constructor(_?: Return) {}
2211
2331
  this.#getRangeWithoutTrailingComma((_b = child.args) == null ? void 0 : _b.value) || "undefined"
2212
2332
  ).write("\n) {\n");
2213
2333
  const body = this.#processBody(child);
2214
- if (body == null ? void 0 : body.renderBody) {
2215
- this.#writeChildren(child, body.renderBody);
2334
+ if (body == null ? void 0 : body.content) {
2335
+ this.#writeChildren(child, body.content);
2216
2336
  }
2217
2337
  this.#extractor.write("\n}\n");
2218
2338
  break;
@@ -2272,51 +2392,53 @@ constructor(_?: Return) {}
2272
2392
  #writeTag(tag) {
2273
2393
  const tagName = tag.nameText;
2274
2394
  const renderId = this.#getRenderId(tag);
2275
- let nestedTagType;
2395
+ const def = tagName ? this.#lookup.getTag(tagName) : void 0;
2276
2396
  if (renderId) {
2277
2397
  this.#extractor.write(
2278
- `${varShared("assertRendered")}(${varShared(
2279
- "rendered"
2280
- )}, ${renderId}, `
2398
+ `${varShared("assertRendered")}(${varShared("rendered")},${renderId},`
2281
2399
  );
2282
2400
  }
2283
- if (tagName) {
2284
- const def = this.#lookup.getTag(tagName);
2285
- if (def) {
2286
- const importPath = resolveTagImport(this.#filename, def);
2287
- const isMarkoFile = importPath == null ? void 0 : importPath.endsWith(".marko");
2288
- if (isMarkoFile) {
2289
- nestedTagType = `import("${importPath}").Input`;
2290
- }
2291
- const renderer = (importPath == null ? void 0 : importPath.endsWith(".marko")) ? `renderTemplate(import("${importPath}"))` : def.html ? `renderNativeTag("${def.name}")` : "missingTag";
2292
- if (!def.html && REG_TAG_NAME_IDENTIFIER.test(tagName)) {
2401
+ if (def == null ? void 0 : def.html) {
2402
+ this.#extractor.write(`${varShared("renderNativeTag")}("`).copy(tag.name).write('")');
2403
+ } else {
2404
+ const importPath = def && resolveTagImport(this.#filename, def);
2405
+ if (def && !importPath) {
2406
+ this.#extractor.write(varShared("missingTag"));
2407
+ } else {
2408
+ const tagId = this.#ensureTagId(tag);
2409
+ let isDynamic = true;
2410
+ this.#extractor.write(
2411
+ `(${varShared("assertTag")}(${varShared("tags")},${tagId},(
2412
+ `
2413
+ );
2414
+ if (tagName && REG_TAG_NAME_IDENTIFIER.test(tagName)) {
2415
+ if (importPath) {
2416
+ this.#extractor.write(
2417
+ `${varShared("fallbackTemplate")}(${tagName},import("${importPath}"))`
2418
+ );
2419
+ } else {
2420
+ this.#extractor.copy(tag.name);
2421
+ }
2422
+ } else if (importPath) {
2423
+ isDynamic = !importPath.endsWith(".marko");
2293
2424
  this.#extractor.write(
2294
- `${varShared("renderPreferLocal")}(
2295
- // @ts-expect-error We expect the compiler to error because we are checking if the tag is defined.
2296
- (${varShared("error")}, `
2297
- ).copy(tag.name).write(`),
2298
- ${varShared(renderer)})`);
2425
+ `${varShared("resolveTemplate")}(import("${importPath}"))`
2426
+ );
2299
2427
  } else {
2300
- this.#extractor.write(varShared(renderer));
2428
+ this.#writeDynamicTagName(tag);
2301
2429
  }
2302
- } else if (REG_TAG_NAME_IDENTIFIER.test(tagName)) {
2303
- nestedTagType = `Marko.Input<typeof ${this.#read(tag.name)}>`;
2304
- this.#extractor.write(`${varShared("renderDynamicTag")}(
2305
- `).copy(tag.name).write("\n)");
2306
- } else {
2307
- this.#extractor.write(`${varShared("missingTag")}`);
2430
+ this.#extractor.write(
2431
+ `
2432
+ )),${varShared(isDynamic ? "renderDynamicTag" : "renderTemplate")}(${varShared("tags")}[${tagId}]))`
2433
+ );
2308
2434
  }
2309
- } else {
2310
- this.#extractor.write(`${varShared("renderDynamicTag")}(`);
2311
- this.#writeDynamicTagName(tag);
2312
- this.#extractor.write(")");
2313
2435
  }
2314
2436
  if (tag.typeArgs) {
2315
2437
  this.#extractor.write(`<0>()`).copy(tag.typeArgs).write("()(");
2316
2438
  } else {
2317
2439
  this.#extractor.write("()()(");
2318
2440
  }
2319
- this.#writeTagInputObject(tag, nestedTagType);
2441
+ this.#writeTagInputObject(tag);
2320
2442
  if (renderId) {
2321
2443
  this.#extractor.write(`)`);
2322
2444
  }
@@ -2500,7 +2622,7 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2500
2622
  );
2501
2623
  return hasAttrs;
2502
2624
  }
2503
- #writeAttrTags({ staticAttrTags, dynamicAttrTagParents }, inMerge, nestedTagType) {
2625
+ #writeAttrTags({ staticAttrTags, dynamicAttrTagParents }, inMerge) {
2504
2626
  let wasMerge = false;
2505
2627
  if (dynamicAttrTagParents) {
2506
2628
  if (staticAttrTags) {
@@ -2516,7 +2638,7 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2516
2638
  }
2517
2639
  }
2518
2640
  if (staticAttrTags) {
2519
- this.#writeStaticAttrTags(staticAttrTags, inMerge, nestedTagType);
2641
+ this.#writeStaticAttrTags(staticAttrTags, inMerge);
2520
2642
  if (dynamicAttrTagParents)
2521
2643
  this.#extractor.write(`}${SEP_COMMA_NEW_LINE}`);
2522
2644
  }
@@ -2525,7 +2647,7 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2525
2647
  if (wasMerge) this.#extractor.write(`)${SEP_COMMA_NEW_LINE}`);
2526
2648
  }
2527
2649
  }
2528
- #writeStaticAttrTags(staticAttrTags, wasMerge, nestedTagType) {
2650
+ #writeStaticAttrTags(staticAttrTags, wasMerge) {
2529
2651
  if (!wasMerge) this.#extractor.write("...{");
2530
2652
  this.#extractor.write(
2531
2653
  `[${varShared("never")}](){
@@ -2546,36 +2668,40 @@ const attrTags = ${varShared(
2546
2668
  this.#extractor.write(SEP_COMMA_NEW_LINE);
2547
2669
  for (const nameText in staticAttrTags) {
2548
2670
  const attrTag = staticAttrTags[nameText];
2549
- const attrTagDef = this.#lookup.getTag(nameText);
2550
2671
  const isRepeated = attrTag.length > 1;
2551
2672
  const [firstAttrTag] = attrTag;
2552
- const name = (attrTagDef == null ? void 0 : attrTagDef.targetProperty) || nameText.slice(nameText.lastIndexOf(":") + 1);
2673
+ const name = this.#getAttrTagName(firstAttrTag);
2553
2674
  this.#extractor.write(`["${name}"`);
2554
2675
  this.#writeTagNameComment(firstAttrTag);
2555
2676
  this.#extractor.write("]: ");
2556
2677
  if (isRepeated) {
2557
- this.#extractor.write(`${varShared("repeatedAttrTag")}(
2558
- ...
2559
- `);
2560
- if (nestedTagType && this.#scriptLang === "js" /* js */) {
2678
+ const tagId = this.#tagIds.get(firstAttrTag.owner);
2679
+ if (tagId) {
2680
+ let accessor = `["${name}"]`;
2681
+ let curTag = firstAttrTag.parent;
2682
+ while (curTag) {
2683
+ if (curTag.type === 16 /* AttrTag */) {
2684
+ accessor = `["${this.#getAttrTagName(curTag)}"]${accessor}`;
2685
+ } else if (!isControlFlowTag(curTag)) {
2686
+ break;
2687
+ }
2688
+ curTag = curTag.parent;
2689
+ }
2561
2690
  this.#extractor.write(
2562
- `/** @satisfies {${nestedTagType}["${name}"][]} */
2691
+ `${varShared("attrTags")}(${varShared("input")}(${varShared("tags")}[${tagId}])${accessor})([
2563
2692
  `
2564
2693
  );
2565
- }
2566
- this.#extractor.write(`([
2694
+ } else {
2695
+ this.#extractor.write(`${varShared("attrTags")}()([
2567
2696
  `);
2697
+ }
2568
2698
  }
2569
2699
  for (const childNode of attrTag) {
2570
2700
  this.#writeTagInputObject(childNode);
2571
2701
  this.#extractor.write(SEP_COMMA_NEW_LINE);
2572
2702
  }
2573
2703
  if (isRepeated) {
2574
- this.#extractor.write("])");
2575
- if (nestedTagType && this.#scriptLang === "ts" /* ts */) {
2576
- this.#extractor.write(` satisfies ${nestedTagType}["${name}"][]`);
2577
- }
2578
- this.#extractor.write(`)${SEP_COMMA_NEW_LINE}`);
2704
+ this.#extractor.write(`])${SEP_COMMA_NEW_LINE}`);
2579
2705
  }
2580
2706
  }
2581
2707
  }
@@ -2612,10 +2738,13 @@ const attrTags = ${varShared(
2612
2738
  break;
2613
2739
  }
2614
2740
  case "for": {
2615
- this.#extractor.write(`${varShared("forAttrTag")}({
2616
- `);
2617
- if (!this.#writeAttrs(tag)) this.#writeTagNameComment(tag);
2618
- this.#extractor.write("}, \n");
2741
+ this.#extractor.write(
2742
+ `${varShared(getForAttrTagRuntime(this.#parsed, tag))}({
2743
+ `
2744
+ );
2745
+ this.#writeTagNameComment(tag);
2746
+ this.#writeAttrs(tag);
2747
+ this.#extractor.write("\n}, \n");
2619
2748
  this.#writeComments(tag);
2620
2749
  this.#extractor.copy(tag.typeParams).write("(\n").copy((_b = tag.params) == null ? void 0 : _b.value).write("\n) => (");
2621
2750
  this.#writeDynamicAttrTagBody(tag);
@@ -2635,7 +2764,7 @@ const attrTags = ${varShared(
2635
2764
  this.#extractor.write(SEP_COMMA_NEW_LINE);
2636
2765
  }
2637
2766
  }
2638
- #writeTagInputObject(tag, nestedTagType) {
2767
+ #writeTagInputObject(tag) {
2639
2768
  if (!tag.params) this.#writeComments(tag);
2640
2769
  let hasInput = false;
2641
2770
  this.#extractor.write("{\n");
@@ -2653,7 +2782,7 @@ const attrTags = ${varShared(
2653
2782
  hasInput = true;
2654
2783
  }
2655
2784
  const isScript = isTextOnlyScript(tag);
2656
- let hasRenderBody = false;
2785
+ let hasBodyContent = false;
2657
2786
  let body;
2658
2787
  if (isScript) {
2659
2788
  this.#extractor.write("async value(){");
@@ -2666,14 +2795,35 @@ const attrTags = ${varShared(
2666
2795
  body = this.#processBody(tag);
2667
2796
  if (body) {
2668
2797
  hasInput = true;
2669
- this.#writeAttrTags(body, false, nestedTagType);
2670
- hasRenderBody = body.renderBody !== void 0;
2798
+ this.#writeAttrTags(body, false);
2799
+ hasBodyContent = body.content !== void 0;
2671
2800
  } else if (tag.close) {
2672
- hasRenderBody = true;
2801
+ hasBodyContent = true;
2673
2802
  }
2674
2803
  }
2675
- if (tag.params || hasRenderBody) {
2676
- this.#extractor.write('["renderBody"');
2804
+ if (tag.params || hasBodyContent) {
2805
+ this.#extractor.write("[");
2806
+ switch (this.#api) {
2807
+ case RuntimeAPI.tags:
2808
+ this.#extractor.write('"content"');
2809
+ break;
2810
+ case RuntimeAPI.class:
2811
+ this.#extractor.write('"renderBody"');
2812
+ break;
2813
+ default: {
2814
+ const tagId = this.#tagIds.get(
2815
+ tag.type === 16 /* AttrTag */ ? tag.owner : tag
2816
+ );
2817
+ if (tagId) {
2818
+ this.#extractor.write(
2819
+ `${varShared("contentFor")}(${varShared("tags")}[${tagId}])`
2820
+ );
2821
+ } else {
2822
+ this.#extractor.write(varShared("content"));
2823
+ }
2824
+ break;
2825
+ }
2826
+ }
2677
2827
  this.#writeTagNameComment(tag);
2678
2828
  this.#extractor.write("]: ");
2679
2829
  if (tag.params) {
@@ -2687,8 +2837,8 @@ const attrTags = ${varShared(
2687
2837
  `);
2688
2838
  }
2689
2839
  let didReturn = false;
2690
- if (body == null ? void 0 : body.renderBody) {
2691
- didReturn = this.#writeChildren(tag, body.renderBody);
2840
+ if (body == null ? void 0 : body.content) {
2841
+ didReturn = this.#writeChildren(tag, body.content);
2692
2842
  }
2693
2843
  if (!tag.params) {
2694
2844
  this.#extractor.write(`return () => {
@@ -2777,7 +2927,7 @@ const attrTags = ${varShared(
2777
2927
  const { body } = parent;
2778
2928
  if (!body) return;
2779
2929
  const last = body.length - 1;
2780
- let renderBody;
2930
+ let content;
2781
2931
  let staticAttrTags;
2782
2932
  let dynamicAttrTagParents;
2783
2933
  let i = 0;
@@ -2866,48 +3016,48 @@ const attrTags = ${varShared(
2866
3016
  } else {
2867
3017
  dynamicAttrTagParents = [child];
2868
3018
  }
2869
- } else if (renderBody) {
2870
- renderBody.push(child);
3019
+ } else if (content) {
3020
+ content.push(child);
2871
3021
  } else {
2872
- renderBody = [child];
3022
+ content = [child];
2873
3023
  }
2874
3024
  break;
2875
3025
  }
2876
3026
  case 17 /* Text */: {
2877
3027
  if (!this.#isEmptyText(child)) {
2878
- if (renderBody) {
2879
- renderBody.push(child);
3028
+ if (content) {
3029
+ content.push(child);
2880
3030
  } else {
2881
- renderBody = [child];
3031
+ content = [child];
2882
3032
  }
2883
3033
  }
2884
3034
  break;
2885
3035
  }
2886
3036
  default:
2887
- if (renderBody) {
2888
- renderBody.push(child);
3037
+ if (content) {
3038
+ content.push(child);
2889
3039
  } else {
2890
- renderBody = [child];
3040
+ content = [child];
2891
3041
  }
2892
3042
  break;
2893
3043
  }
2894
3044
  }
2895
- if (renderBody || staticAttrTags || dynamicAttrTagParents) {
2896
- return { renderBody, staticAttrTags, dynamicAttrTagParents };
3045
+ if (content || staticAttrTags || dynamicAttrTagParents) {
3046
+ return { content, staticAttrTags, dynamicAttrTagParents };
2897
3047
  }
2898
3048
  }
2899
3049
  #writeDynamicAttrTagBody(tag) {
2900
3050
  const body = this.#processBody(tag);
2901
3051
  if (body) {
2902
- if (body.renderBody) {
3052
+ if (body.content) {
2903
3053
  this.#extractor.write("(() => {\n");
2904
- this.#writeChildren(tag, body.renderBody);
3054
+ this.#writeChildren(tag, body.content);
2905
3055
  this.#extractor.write("return ");
2906
3056
  }
2907
3057
  this.#extractor.write("{\n");
2908
3058
  this.#writeAttrTags(body, true);
2909
3059
  this.#extractor.write("}");
2910
- if (body.renderBody) {
3060
+ if (body.content) {
2911
3061
  this.#extractor.write(";\n})()");
2912
3062
  }
2913
3063
  } else {
@@ -3010,6 +3160,14 @@ const attrTags = ${varShared(
3010
3160
  }
3011
3161
  return renderId;
3012
3162
  }
3163
+ #ensureTagId(tag) {
3164
+ let tagId = this.#tagIds.get(tag);
3165
+ if (!tagId) {
3166
+ tagId = this.#tagId++;
3167
+ this.#tagIds.set(tag, tagId);
3168
+ }
3169
+ return tagId;
3170
+ }
3013
3171
  #getNamedAttrModifierIndex(attr) {
3014
3172
  const start = attr.name.start + 1;
3015
3173
  const end = attr.name.end - 1;
@@ -3018,6 +3176,11 @@ const attrTags = ${varShared(
3018
3176
  }
3019
3177
  return false;
3020
3178
  }
3179
+ #getAttrTagName(tag) {
3180
+ var _a;
3181
+ const { nameText } = tag;
3182
+ return ((_a = this.#lookup.getTag(nameText)) == null ? void 0 : _a.targetProperty) || nameText.slice(nameText.lastIndexOf(":") + 1);
3183
+ }
3021
3184
  #testAtIndex(reg, index) {
3022
3185
  reg.lastIndex = index;
3023
3186
  return reg.test(this.#code);
@@ -3027,6 +3190,50 @@ const attrTags = ${varShared(
3027
3190
  return reg.exec(this.#code);
3028
3191
  }
3029
3192
  };
3193
+ function getForTagType(parsed, tag) {
3194
+ if (tag.attrs) {
3195
+ for (const attr of tag.attrs) {
3196
+ if (attr.type === 15 /* AttrSpread */) {
3197
+ return 0 /* unknown */;
3198
+ }
3199
+ switch (parsed.read(attr.name)) {
3200
+ case "of":
3201
+ return 1 /* of */;
3202
+ case "in":
3203
+ return 2 /* in */;
3204
+ case "to":
3205
+ case "from":
3206
+ case "step":
3207
+ return 3 /* to */;
3208
+ }
3209
+ }
3210
+ }
3211
+ return 0 /* unknown */;
3212
+ }
3213
+ function getForTagRuntime(parsed, tag) {
3214
+ switch (getForTagType(parsed, tag)) {
3215
+ case 1 /* of */:
3216
+ return "forOfTag";
3217
+ case 2 /* in */:
3218
+ return "forInTag";
3219
+ case 3 /* to */:
3220
+ return "forToTag";
3221
+ default:
3222
+ return "forTag";
3223
+ }
3224
+ }
3225
+ function getForAttrTagRuntime(parsed, tag) {
3226
+ switch (getForTagType(parsed, tag)) {
3227
+ case 1 /* of */:
3228
+ return "forOfAttrTag";
3229
+ case 2 /* in */:
3230
+ return "forInAttrTag";
3231
+ case 3 /* to */:
3232
+ return "forToAttrTag";
3233
+ default:
3234
+ return "forAttrTag";
3235
+ }
3236
+ }
3030
3237
  function varLocal(name) {
3031
3238
  return VAR_LOCAL_PREFIX + name;
3032
3239
  }
@@ -3463,6 +3670,7 @@ var marko_default = {
3463
3670
  return extractScript({
3464
3671
  ts,
3465
3672
  parsed,
3673
+ translator: getConfig(dir).translator,
3466
3674
  lookup: getTagLookup(dir),
3467
3675
  scriptLang: getScriptLang(
3468
3676
  fileName,
@@ -3612,6 +3820,7 @@ export {
3612
3820
  getLines,
3613
3821
  getLocation,
3614
3822
  getPosition,
3823
+ isControlFlowTag,
3615
3824
  isDefinitionFile,
3616
3825
  parse
3617
3826
  };
package/dist/parser.d.ts CHANGED
@@ -80,7 +80,7 @@ export declare namespace Node {
80
80
  interface AttrTag extends Range, Commentable {
81
81
  type: NodeType.AttrTag;
82
82
  parent: ParentTag;
83
- owner: ParentTag | undefined;
83
+ owner: Tag | undefined;
84
84
  concise: boolean;
85
85
  selfClosed: boolean;
86
86
  hasAttrTags: boolean;
@@ -225,3 +225,9 @@ export declare function parse(code: string, filename?: string): {
225
225
  program: Node.Program;
226
226
  code: string;
227
227
  };
228
+ /**
229
+ * Used to check if a node should be ignored as the parent of an attribute tag.
230
+ * When control flow is the parent of an attribute tag, we add the attribute tag to
231
+ * the closest non control flow ancestor attrs instead.
232
+ */
233
+ export declare function isControlFlowTag(node: Node.Tag): node is Node.ControlFlowTag;
@@ -7,6 +7,7 @@ export interface Meta {
7
7
  config: Omit<Compiler.Config, "cache" | "translator"> & {
8
8
  cache: Map<any, any>;
9
9
  translator: {
10
+ preferAPI?: string;
10
11
  runtimeTypes?: string;
11
12
  [x: string]: unknown;
12
13
  };
@@ -25,6 +26,7 @@ export declare function getCache(dir?: string): Map<any, any>;
25
26
  export declare function getConfig(dir?: string): Omit<Compiler.Config, "cache" | "translator"> & {
26
27
  cache: Map<any, any>;
27
28
  translator: {
29
+ preferAPI?: string;
28
30
  runtimeTypes?: string;
29
31
  [x: string]: unknown;
30
32
  };