@marko/language-tools 2.5.5 → 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.
@@ -1,6 +1,7 @@
1
1
  import type { TaglibLookup } from "@marko/babel-utils";
2
2
  import type TS from "typescript/lib/tsserverlibrary";
3
3
  import { type Parsed } from "../../parser";
4
+ import type { Meta } from "../../util/project";
4
5
  /**
5
6
  * Iterate over the Marko CST and extract all the script content.
6
7
  */
@@ -14,5 +15,6 @@ export interface ExtractScriptOptions {
14
15
  lookup: TaglibLookup;
15
16
  scriptLang: ScriptLang;
16
17
  runtimeTypesCode?: string;
18
+ translator: Meta["config"]["translator"];
17
19
  }
18
20
  export declare function extractScript(opts: ExtractScriptOptions): import("../../util/extractor").Extracted;
@@ -0,0 +1,9 @@
1
+ import { Parsed } from "../../../parser";
2
+ export type RuntimeAPI = (typeof RuntimeAPI)[keyof typeof RuntimeAPI] | void;
3
+ export declare const RuntimeAPI: {
4
+ readonly tags: "tags";
5
+ readonly class: "class";
6
+ };
7
+ export declare function getRuntimeAPI(translator: {
8
+ preferAPI?: string;
9
+ } | undefined, parsed: Parsed): RuntimeAPI;
package/dist/index.js CHANGED
@@ -42,6 +42,7 @@ __export(src_exports, {
42
42
  getLines: () => import_htmljs_parser2.getLines,
43
43
  getLocation: () => import_htmljs_parser2.getLocation,
44
44
  getPosition: () => import_htmljs_parser2.getPosition,
45
+ isControlFlowTag: () => isControlFlowTag,
45
46
  isDefinitionFile: () => isDefinitionFile,
46
47
  parse: () => parse
47
48
  });
@@ -1653,6 +1654,120 @@ function tryReaddirSync(dir) {
1653
1654
  }
1654
1655
  }
1655
1656
 
1657
+ // src/extractors/script/util/get-runtime-api.ts
1658
+ var RuntimeAPI = {
1659
+ tags: "tags",
1660
+ class: "class"
1661
+ };
1662
+ function getRuntimeAPI(translator, parsed) {
1663
+ return detectAPIFromTranslator(translator) || detectAPIFromFileName(parsed.filename) || detectAPIFromProgram(parsed, parsed.program);
1664
+ }
1665
+ function detectAPIFromTranslator(translator) {
1666
+ switch (translator == null ? void 0 : translator.preferAPI) {
1667
+ case "class":
1668
+ return RuntimeAPI.class;
1669
+ case "tags":
1670
+ return RuntimeAPI.tags;
1671
+ }
1672
+ }
1673
+ function detectAPIFromFileName(filename) {
1674
+ for (let end = filename.length, i = end; --i; ) {
1675
+ switch (filename[i]) {
1676
+ case "/":
1677
+ case "\\":
1678
+ if (filename.startsWith("tags", i + 1)) {
1679
+ return RuntimeAPI.tags;
1680
+ } else if (filename.startsWith("components", i + 1)) {
1681
+ return;
1682
+ }
1683
+ end = i;
1684
+ break;
1685
+ }
1686
+ }
1687
+ }
1688
+ function detectAPIFromProgram(parsed, program) {
1689
+ if (program.comments) {
1690
+ switch (parsed.read(program.comments[0].value).trim()) {
1691
+ case "use tags":
1692
+ return RuntimeAPI.tags;
1693
+ case "use class":
1694
+ return RuntimeAPI.class;
1695
+ }
1696
+ }
1697
+ if (program.static) {
1698
+ for (const stmt of program.static) {
1699
+ switch (stmt.type) {
1700
+ case 26 /* Class */:
1701
+ case 27 /* Style */:
1702
+ return RuntimeAPI.class;
1703
+ }
1704
+ }
1705
+ }
1706
+ return detectAPIFromBody(parsed, program.body);
1707
+ }
1708
+ function detectAPIFromBody(parsed, body) {
1709
+ if (body) {
1710
+ for (const child of body) {
1711
+ const api = detectAPIFromChild(parsed, child);
1712
+ if (api) return api;
1713
+ }
1714
+ }
1715
+ }
1716
+ function detectAPIFromChild(parsed, child) {
1717
+ switch (child.type) {
1718
+ case 23 /* Scriptlet */:
1719
+ return RuntimeAPI.class;
1720
+ case 1 /* Tag */:
1721
+ case 16 /* AttrTag */:
1722
+ return detectAPIFromTag(parsed, child);
1723
+ }
1724
+ }
1725
+ function detectAPIFromTag(parsed, tag) {
1726
+ var _a;
1727
+ switch (tag.nameText) {
1728
+ case "macro":
1729
+ case "include-text":
1730
+ case "include-html":
1731
+ case "init-components":
1732
+ case "await-reorderer":
1733
+ case "while":
1734
+ case "module-code":
1735
+ return RuntimeAPI.class;
1736
+ case "const":
1737
+ case "debug":
1738
+ case "define":
1739
+ case "html-script":
1740
+ case "html-style":
1741
+ case "id":
1742
+ case "let":
1743
+ case "lifecycle":
1744
+ case "log":
1745
+ case "return":
1746
+ return RuntimeAPI.tags;
1747
+ }
1748
+ if (tag.var) {
1749
+ return RuntimeAPI.tags;
1750
+ }
1751
+ if (tag.attrs) {
1752
+ for (const attr of tag.attrs) {
1753
+ if (attr.type !== 15 /* AttrSpread */) {
1754
+ if (((_a = attr.value) == null ? void 0 : _a.type) === 13 /* AttrValue */ && attr.value.bound) {
1755
+ return RuntimeAPI.tags;
1756
+ }
1757
+ if (attr.args) {
1758
+ return RuntimeAPI.class;
1759
+ }
1760
+ if (/^(?:key|no-update(?:-body)?(?:-if)?)$|:(scoped:no-update)$/.test(
1761
+ parsed.read(attr.name)
1762
+ )) {
1763
+ return RuntimeAPI.class;
1764
+ }
1765
+ }
1766
+ }
1767
+ }
1768
+ return detectAPIFromBody(parsed, tag.body);
1769
+ }
1770
+
1656
1771
  // src/extractors/script/util/jsdoc-input-type.ts
1657
1772
  var MaybeInputTypedefReg = /@typedef\b[\s\S]*\bInput\b/;
1658
1773
  function getJSDocInputType(comment, ts) {
@@ -1758,7 +1873,7 @@ function getRuntimeOverrides(runtimeTypes, generics, applyGenerics) {
1758
1873
  }
1759
1874
 
1760
1875
  // src/extractors/script/util/script-parser.ts
1761
- var import_parser6 = require("@babel/parser");
1876
+ var import_parser7 = require("@babel/parser");
1762
1877
  var plugins = [
1763
1878
  "exportDefaultFrom",
1764
1879
  "importAssertions",
@@ -1772,7 +1887,7 @@ var ScriptParser = class {
1772
1887
  statementAt(startIndex, src) {
1773
1888
  const pos = this.#parsed.positionAt(startIndex);
1774
1889
  try {
1775
- return (0, import_parser6.parse)(src, {
1890
+ return (0, import_parser7.parse)(src, {
1776
1891
  plugins,
1777
1892
  startIndex,
1778
1893
  startLine: pos.line + 1,
@@ -1793,7 +1908,7 @@ var ScriptParser = class {
1793
1908
  expressionAt(startIndex, src) {
1794
1909
  const pos = this.#parsed.positionAt(startIndex);
1795
1910
  try {
1796
- return (0, import_parser6.parseExpression)(src, {
1911
+ return (0, import_parser7.parseExpression)(src, {
1797
1912
  plugins,
1798
1913
  startIndex,
1799
1914
  startLine: pos.line + 1,
@@ -1829,7 +1944,7 @@ var REG_TAG_IMPORT = /(?<=(['"]))<([^'">]+)>(?=\1)/;
1829
1944
  var REG_INPUT_TYPE = /\s*(interface|type)\s+Input\b/y;
1830
1945
  var REG_OBJECT_PROPERTY = /^[_$a-z][_$a-z0-9]*$/i;
1831
1946
  var REG_COMMENT_PRAGMA = /\/\/(?:\s*@ts-|\/\s*<)/y;
1832
- var REG_TAG_NAME_IDENTIFIER = /^[A-Z][a-zA-Z_$]+$/;
1947
+ var REG_TAG_NAME_IDENTIFIER = /^[A-Z][a-zA-Z0-9_$]+$/;
1833
1948
  var IF_TAG_ALTERNATES = /* @__PURE__ */ new WeakMap();
1834
1949
  var WROTE_COMMENT = /* @__PURE__ */ new WeakSet();
1835
1950
  var START_OF_FILE = { start: 0, end: 0 };
@@ -1849,11 +1964,14 @@ var ScriptExtractor = class {
1849
1964
  #scriptParser;
1850
1965
  #read;
1851
1966
  #lookup;
1967
+ #tagIds = /* @__PURE__ */ new Map();
1852
1968
  #renderIds = /* @__PURE__ */ new Map();
1853
1969
  #scriptLang;
1854
1970
  #ts;
1855
1971
  #runtimeTypes;
1972
+ #api;
1856
1973
  #mutationOffsets;
1974
+ #tagId = 1;
1857
1975
  #renderId = 1;
1858
1976
  constructor(opts) {
1859
1977
  const { parsed, lookup, scriptLang } = opts;
@@ -1867,6 +1985,7 @@ var ScriptExtractor = class {
1867
1985
  this.#extractor = new Extractor(parsed);
1868
1986
  this.#scriptParser = new ScriptParser(parsed);
1869
1987
  this.#read = parsed.read.bind(parsed);
1988
+ this.#api = getRuntimeAPI(opts.translator, parsed);
1870
1989
  this.#mutationOffsets = crawlProgramScope(this.#parsed, this.#scriptParser);
1871
1990
  this.#writeProgram(parsed.program);
1872
1991
  }
@@ -2025,8 +2144,8 @@ function ${templateName}() {
2025
2144
  ${varShared("noop")}({ input, component, state, out, $global, $signal });
2026
2145
  `);
2027
2146
  const body = this.#processBody(program);
2028
- if (body == null ? void 0 : body.renderBody) {
2029
- this.#writeChildren(program, body.renderBody);
2147
+ if (body == null ? void 0 : body.content) {
2148
+ this.#writeChildren(program, body.content);
2030
2149
  }
2031
2150
  const hoists = getHoists(program);
2032
2151
  if (hoists) {
@@ -2057,6 +2176,7 @@ function ${templateName}() {
2057
2176
  "ReturnWithScope"
2058
2177
  )}<${internalInput}, ${didReturn ? `typeof ${templateName + typeArgsStr} extends () => infer Return ? Return : never` : "void"}>)`;
2059
2178
  const templateOverrideClass = `${templateBaseClass}<{${this.#runtimeTypes ? getRuntimeOverrides(this.#runtimeTypes, typeParamsStr, typeArgsStr) : ""}
2179
+ ${this.#api ? `api: "${this.#api}",` : ""}
2060
2180
  _${typeParamsStr ? `<${internalApply} = 1>(): ${internalApply} extends 0
2061
2181
  ? ${typeParamsStr}() => <${internalInputWithExtends}>${renderAndReturn}
2062
2182
  : () => <${internalInputWithExtends}, ${typeParamsStr.slice(
@@ -2163,9 +2283,9 @@ constructor(_?: Return) {}
2163
2283
  this.#getRangeWithoutTrailingComma((_a = child.args) == null ? void 0 : _a.value) || this.#getAttrValue(child, ATTR_UNAMED2) || "undefined"
2164
2284
  ).write(") {\n");
2165
2285
  const ifBody = this.#processBody(child);
2166
- if (ifBody == null ? void 0 : ifBody.renderBody) {
2286
+ if (ifBody == null ? void 0 : ifBody.content) {
2167
2287
  const localBindings = getHoistSources(child);
2168
- this.#writeChildren(child, ifBody.renderBody, true);
2288
+ this.#writeChildren(child, ifBody.content, true);
2169
2289
  if (localBindings) {
2170
2290
  this.#extractor.write("return {\nscope:");
2171
2291
  this.#writeObjectKeys(localBindings);
@@ -2185,9 +2305,9 @@ constructor(_?: Return) {}
2185
2305
  this.#extractor.write("\n} else if (undefined) {\n");
2186
2306
  }
2187
2307
  const alternateBody = this.#processBody(node);
2188
- if (alternateBody == null ? void 0 : alternateBody.renderBody) {
2308
+ if (alternateBody == null ? void 0 : alternateBody.content) {
2189
2309
  const localBindings = getHoistSources(node);
2190
- this.#writeChildren(node, alternateBody.renderBody, true);
2310
+ this.#writeChildren(node, alternateBody.content, true);
2191
2311
  if (localBindings) {
2192
2312
  this.#extractor.write("return {\nscope:");
2193
2313
  this.#writeObjectKeys(localBindings);
@@ -2228,12 +2348,12 @@ constructor(_?: Return) {}
2228
2348
  }
2229
2349
  this.#extractor.write("\n) => {\n");
2230
2350
  const body = this.#processBody(child);
2231
- if (body == null ? void 0 : body.renderBody) {
2232
- this.#writeChildren(child, body.renderBody);
2351
+ if (body == null ? void 0 : body.content) {
2352
+ this.#writeChildren(child, body.content);
2233
2353
  }
2234
2354
  this.#writeReturn(
2235
2355
  void 0,
2236
- (body == null ? void 0 : body.renderBody) ? getHoistSources(child) : void 0
2356
+ (body == null ? void 0 : body.content) ? getHoistSources(child) : void 0
2237
2357
  );
2238
2358
  if (renderId) {
2239
2359
  this.#extractor.write("\n}));\n");
@@ -2248,8 +2368,8 @@ constructor(_?: Return) {}
2248
2368
  this.#getRangeWithoutTrailingComma((_b = child.args) == null ? void 0 : _b.value) || "undefined"
2249
2369
  ).write("\n) {\n");
2250
2370
  const body = this.#processBody(child);
2251
- if (body == null ? void 0 : body.renderBody) {
2252
- this.#writeChildren(child, body.renderBody);
2371
+ if (body == null ? void 0 : body.content) {
2372
+ this.#writeChildren(child, body.content);
2253
2373
  }
2254
2374
  this.#extractor.write("\n}\n");
2255
2375
  break;
@@ -2309,51 +2429,53 @@ constructor(_?: Return) {}
2309
2429
  #writeTag(tag) {
2310
2430
  const tagName = tag.nameText;
2311
2431
  const renderId = this.#getRenderId(tag);
2312
- let nestedTagType;
2432
+ const def = tagName ? this.#lookup.getTag(tagName) : void 0;
2313
2433
  if (renderId) {
2314
2434
  this.#extractor.write(
2315
- `${varShared("assertRendered")}(${varShared(
2316
- "rendered"
2317
- )}, ${renderId}, `
2435
+ `${varShared("assertRendered")}(${varShared("rendered")},${renderId},`
2318
2436
  );
2319
2437
  }
2320
- if (tagName) {
2321
- const def = this.#lookup.getTag(tagName);
2322
- if (def) {
2323
- const importPath = resolveTagImport(this.#filename, def);
2324
- const isMarkoFile = importPath == null ? void 0 : importPath.endsWith(".marko");
2325
- if (isMarkoFile) {
2326
- nestedTagType = `import("${importPath}").Input`;
2327
- }
2328
- const renderer = (importPath == null ? void 0 : importPath.endsWith(".marko")) ? `renderTemplate(import("${importPath}"))` : def.html ? `renderNativeTag("${def.name}")` : "missingTag";
2329
- if (!def.html && REG_TAG_NAME_IDENTIFIER.test(tagName)) {
2438
+ if (def == null ? void 0 : def.html) {
2439
+ this.#extractor.write(`${varShared("renderNativeTag")}("`).copy(tag.name).write('")');
2440
+ } else {
2441
+ const importPath = def && resolveTagImport(this.#filename, def);
2442
+ if (def && !importPath) {
2443
+ this.#extractor.write(varShared("missingTag"));
2444
+ } else {
2445
+ const tagId = this.#ensureTagId(tag);
2446
+ let isDynamic = true;
2447
+ this.#extractor.write(
2448
+ `(${varShared("assertTag")}(${varShared("tags")},${tagId},(
2449
+ `
2450
+ );
2451
+ if (tagName && REG_TAG_NAME_IDENTIFIER.test(tagName)) {
2452
+ if (importPath) {
2453
+ this.#extractor.write(
2454
+ `${varShared("fallbackTemplate")}(${tagName},import("${importPath}"))`
2455
+ );
2456
+ } else {
2457
+ this.#extractor.copy(tag.name);
2458
+ }
2459
+ } else if (importPath) {
2460
+ isDynamic = !importPath.endsWith(".marko");
2330
2461
  this.#extractor.write(
2331
- `${varShared("renderPreferLocal")}(
2332
- // @ts-expect-error We expect the compiler to error because we are checking if the tag is defined.
2333
- (${varShared("error")}, `
2334
- ).copy(tag.name).write(`),
2335
- ${varShared(renderer)})`);
2462
+ `${varShared("resolveTemplate")}(import("${importPath}"))`
2463
+ );
2336
2464
  } else {
2337
- this.#extractor.write(varShared(renderer));
2465
+ this.#writeDynamicTagName(tag);
2338
2466
  }
2339
- } else if (REG_TAG_NAME_IDENTIFIER.test(tagName)) {
2340
- nestedTagType = `Marko.Input<typeof ${this.#read(tag.name)}>`;
2341
- this.#extractor.write(`${varShared("renderDynamicTag")}(
2342
- `).copy(tag.name).write("\n)");
2343
- } else {
2344
- this.#extractor.write(`${varShared("missingTag")}`);
2467
+ this.#extractor.write(
2468
+ `
2469
+ )),${varShared(isDynamic ? "renderDynamicTag" : "renderTemplate")}(${varShared("tags")}[${tagId}]))`
2470
+ );
2345
2471
  }
2346
- } else {
2347
- this.#extractor.write(`${varShared("renderDynamicTag")}(`);
2348
- this.#writeDynamicTagName(tag);
2349
- this.#extractor.write(")");
2350
2472
  }
2351
2473
  if (tag.typeArgs) {
2352
2474
  this.#extractor.write(`<0>()`).copy(tag.typeArgs).write("()(");
2353
2475
  } else {
2354
2476
  this.#extractor.write("()()(");
2355
2477
  }
2356
- this.#writeTagInputObject(tag, nestedTagType);
2478
+ this.#writeTagInputObject(tag);
2357
2479
  if (renderId) {
2358
2480
  this.#extractor.write(`)`);
2359
2481
  }
@@ -2537,7 +2659,7 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2537
2659
  );
2538
2660
  return hasAttrs;
2539
2661
  }
2540
- #writeAttrTags({ staticAttrTags, dynamicAttrTagParents }, inMerge, nestedTagType) {
2662
+ #writeAttrTags({ staticAttrTags, dynamicAttrTagParents }, inMerge) {
2541
2663
  let wasMerge = false;
2542
2664
  if (dynamicAttrTagParents) {
2543
2665
  if (staticAttrTags) {
@@ -2553,7 +2675,7 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2553
2675
  }
2554
2676
  }
2555
2677
  if (staticAttrTags) {
2556
- this.#writeStaticAttrTags(staticAttrTags, inMerge, nestedTagType);
2678
+ this.#writeStaticAttrTags(staticAttrTags, inMerge);
2557
2679
  if (dynamicAttrTagParents)
2558
2680
  this.#extractor.write(`}${SEP_COMMA_NEW_LINE}`);
2559
2681
  }
@@ -2562,7 +2684,7 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2562
2684
  if (wasMerge) this.#extractor.write(`)${SEP_COMMA_NEW_LINE}`);
2563
2685
  }
2564
2686
  }
2565
- #writeStaticAttrTags(staticAttrTags, wasMerge, nestedTagType) {
2687
+ #writeStaticAttrTags(staticAttrTags, wasMerge) {
2566
2688
  if (!wasMerge) this.#extractor.write("...{");
2567
2689
  this.#extractor.write(
2568
2690
  `[${varShared("never")}](){
@@ -2583,36 +2705,40 @@ const attrTags = ${varShared(
2583
2705
  this.#extractor.write(SEP_COMMA_NEW_LINE);
2584
2706
  for (const nameText in staticAttrTags) {
2585
2707
  const attrTag = staticAttrTags[nameText];
2586
- const attrTagDef = this.#lookup.getTag(nameText);
2587
2708
  const isRepeated = attrTag.length > 1;
2588
2709
  const [firstAttrTag] = attrTag;
2589
- const name = (attrTagDef == null ? void 0 : attrTagDef.targetProperty) || nameText.slice(nameText.lastIndexOf(":") + 1);
2710
+ const name = this.#getAttrTagName(firstAttrTag);
2590
2711
  this.#extractor.write(`["${name}"`);
2591
2712
  this.#writeTagNameComment(firstAttrTag);
2592
2713
  this.#extractor.write("]: ");
2593
2714
  if (isRepeated) {
2594
- this.#extractor.write(`${varShared("repeatedAttrTag")}(
2595
- ...
2596
- `);
2597
- if (nestedTagType && this.#scriptLang === "js" /* js */) {
2715
+ const tagId = this.#tagIds.get(firstAttrTag.owner);
2716
+ if (tagId) {
2717
+ let accessor = `["${name}"]`;
2718
+ let curTag = firstAttrTag.parent;
2719
+ while (curTag) {
2720
+ if (curTag.type === 16 /* AttrTag */) {
2721
+ accessor = `["${this.#getAttrTagName(curTag)}"]${accessor}`;
2722
+ } else if (!isControlFlowTag(curTag)) {
2723
+ break;
2724
+ }
2725
+ curTag = curTag.parent;
2726
+ }
2598
2727
  this.#extractor.write(
2599
- `/** @satisfies {${nestedTagType}["${name}"][]} */
2728
+ `${varShared("attrTags")}(${varShared("input")}(${varShared("tags")}[${tagId}])${accessor})([
2600
2729
  `
2601
2730
  );
2602
- }
2603
- this.#extractor.write(`([
2731
+ } else {
2732
+ this.#extractor.write(`${varShared("attrTags")}()([
2604
2733
  `);
2734
+ }
2605
2735
  }
2606
2736
  for (const childNode of attrTag) {
2607
2737
  this.#writeTagInputObject(childNode);
2608
2738
  this.#extractor.write(SEP_COMMA_NEW_LINE);
2609
2739
  }
2610
2740
  if (isRepeated) {
2611
- this.#extractor.write("])");
2612
- if (nestedTagType && this.#scriptLang === "ts" /* ts */) {
2613
- this.#extractor.write(` satisfies ${nestedTagType}["${name}"][]`);
2614
- }
2615
- this.#extractor.write(`)${SEP_COMMA_NEW_LINE}`);
2741
+ this.#extractor.write(`])${SEP_COMMA_NEW_LINE}`);
2616
2742
  }
2617
2743
  }
2618
2744
  }
@@ -2675,7 +2801,7 @@ const attrTags = ${varShared(
2675
2801
  this.#extractor.write(SEP_COMMA_NEW_LINE);
2676
2802
  }
2677
2803
  }
2678
- #writeTagInputObject(tag, nestedTagType) {
2804
+ #writeTagInputObject(tag) {
2679
2805
  if (!tag.params) this.#writeComments(tag);
2680
2806
  let hasInput = false;
2681
2807
  this.#extractor.write("{\n");
@@ -2693,7 +2819,7 @@ const attrTags = ${varShared(
2693
2819
  hasInput = true;
2694
2820
  }
2695
2821
  const isScript = isTextOnlyScript(tag);
2696
- let hasRenderBody = false;
2822
+ let hasBodyContent = false;
2697
2823
  let body;
2698
2824
  if (isScript) {
2699
2825
  this.#extractor.write("async value(){");
@@ -2706,14 +2832,35 @@ const attrTags = ${varShared(
2706
2832
  body = this.#processBody(tag);
2707
2833
  if (body) {
2708
2834
  hasInput = true;
2709
- this.#writeAttrTags(body, false, nestedTagType);
2710
- hasRenderBody = body.renderBody !== void 0;
2835
+ this.#writeAttrTags(body, false);
2836
+ hasBodyContent = body.content !== void 0;
2711
2837
  } else if (tag.close) {
2712
- hasRenderBody = true;
2838
+ hasBodyContent = true;
2713
2839
  }
2714
2840
  }
2715
- if (tag.params || hasRenderBody) {
2716
- this.#extractor.write('["renderBody"');
2841
+ if (tag.params || hasBodyContent) {
2842
+ this.#extractor.write("[");
2843
+ switch (this.#api) {
2844
+ case RuntimeAPI.tags:
2845
+ this.#extractor.write('"content"');
2846
+ break;
2847
+ case RuntimeAPI.class:
2848
+ this.#extractor.write('"renderBody"');
2849
+ break;
2850
+ default: {
2851
+ const tagId = this.#tagIds.get(
2852
+ tag.type === 16 /* AttrTag */ ? tag.owner : tag
2853
+ );
2854
+ if (tagId) {
2855
+ this.#extractor.write(
2856
+ `${varShared("contentFor")}(${varShared("tags")}[${tagId}])`
2857
+ );
2858
+ } else {
2859
+ this.#extractor.write(varShared("content"));
2860
+ }
2861
+ break;
2862
+ }
2863
+ }
2717
2864
  this.#writeTagNameComment(tag);
2718
2865
  this.#extractor.write("]: ");
2719
2866
  if (tag.params) {
@@ -2727,8 +2874,8 @@ const attrTags = ${varShared(
2727
2874
  `);
2728
2875
  }
2729
2876
  let didReturn = false;
2730
- if (body == null ? void 0 : body.renderBody) {
2731
- didReturn = this.#writeChildren(tag, body.renderBody);
2877
+ if (body == null ? void 0 : body.content) {
2878
+ didReturn = this.#writeChildren(tag, body.content);
2732
2879
  }
2733
2880
  if (!tag.params) {
2734
2881
  this.#extractor.write(`return () => {
@@ -2817,7 +2964,7 @@ const attrTags = ${varShared(
2817
2964
  const { body } = parent;
2818
2965
  if (!body) return;
2819
2966
  const last = body.length - 1;
2820
- let renderBody;
2967
+ let content;
2821
2968
  let staticAttrTags;
2822
2969
  let dynamicAttrTagParents;
2823
2970
  let i = 0;
@@ -2906,48 +3053,48 @@ const attrTags = ${varShared(
2906
3053
  } else {
2907
3054
  dynamicAttrTagParents = [child];
2908
3055
  }
2909
- } else if (renderBody) {
2910
- renderBody.push(child);
3056
+ } else if (content) {
3057
+ content.push(child);
2911
3058
  } else {
2912
- renderBody = [child];
3059
+ content = [child];
2913
3060
  }
2914
3061
  break;
2915
3062
  }
2916
3063
  case 17 /* Text */: {
2917
3064
  if (!this.#isEmptyText(child)) {
2918
- if (renderBody) {
2919
- renderBody.push(child);
3065
+ if (content) {
3066
+ content.push(child);
2920
3067
  } else {
2921
- renderBody = [child];
3068
+ content = [child];
2922
3069
  }
2923
3070
  }
2924
3071
  break;
2925
3072
  }
2926
3073
  default:
2927
- if (renderBody) {
2928
- renderBody.push(child);
3074
+ if (content) {
3075
+ content.push(child);
2929
3076
  } else {
2930
- renderBody = [child];
3077
+ content = [child];
2931
3078
  }
2932
3079
  break;
2933
3080
  }
2934
3081
  }
2935
- if (renderBody || staticAttrTags || dynamicAttrTagParents) {
2936
- return { renderBody, staticAttrTags, dynamicAttrTagParents };
3082
+ if (content || staticAttrTags || dynamicAttrTagParents) {
3083
+ return { content, staticAttrTags, dynamicAttrTagParents };
2937
3084
  }
2938
3085
  }
2939
3086
  #writeDynamicAttrTagBody(tag) {
2940
3087
  const body = this.#processBody(tag);
2941
3088
  if (body) {
2942
- if (body.renderBody) {
3089
+ if (body.content) {
2943
3090
  this.#extractor.write("(() => {\n");
2944
- this.#writeChildren(tag, body.renderBody);
3091
+ this.#writeChildren(tag, body.content);
2945
3092
  this.#extractor.write("return ");
2946
3093
  }
2947
3094
  this.#extractor.write("{\n");
2948
3095
  this.#writeAttrTags(body, true);
2949
3096
  this.#extractor.write("}");
2950
- if (body.renderBody) {
3097
+ if (body.content) {
2951
3098
  this.#extractor.write(";\n})()");
2952
3099
  }
2953
3100
  } else {
@@ -3050,6 +3197,14 @@ const attrTags = ${varShared(
3050
3197
  }
3051
3198
  return renderId;
3052
3199
  }
3200
+ #ensureTagId(tag) {
3201
+ let tagId = this.#tagIds.get(tag);
3202
+ if (!tagId) {
3203
+ tagId = this.#tagId++;
3204
+ this.#tagIds.set(tag, tagId);
3205
+ }
3206
+ return tagId;
3207
+ }
3053
3208
  #getNamedAttrModifierIndex(attr) {
3054
3209
  const start = attr.name.start + 1;
3055
3210
  const end = attr.name.end - 1;
@@ -3058,6 +3213,11 @@ const attrTags = ${varShared(
3058
3213
  }
3059
3214
  return false;
3060
3215
  }
3216
+ #getAttrTagName(tag) {
3217
+ var _a;
3218
+ const { nameText } = tag;
3219
+ return ((_a = this.#lookup.getTag(nameText)) == null ? void 0 : _a.targetProperty) || nameText.slice(nameText.lastIndexOf(":") + 1);
3220
+ }
3061
3221
  #testAtIndex(reg, index) {
3062
3222
  reg.lastIndex = index;
3063
3223
  return reg.test(this.#code);
@@ -3547,6 +3707,7 @@ var marko_default = {
3547
3707
  return extractScript({
3548
3708
  ts,
3549
3709
  parsed,
3710
+ translator: getConfig(dir).translator,
3550
3711
  lookup: getTagLookup(dir),
3551
3712
  scriptLang: getScriptLang(
3552
3713
  fileName,
@@ -3697,6 +3858,7 @@ function isDefinitionFile(fileName) {
3697
3858
  getLines,
3698
3859
  getLocation,
3699
3860
  getPosition,
3861
+ isControlFlowTag,
3700
3862
  isDefinitionFile,
3701
3863
  parse
3702
3864
  });
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);
@@ -2192,12 +2311,12 @@ constructor(_?: Return) {}
2192
2311
  }
2193
2312
  this.#extractor.write("\n) => {\n");
2194
2313
  const body = this.#processBody(child);
2195
- if (body == null ? void 0 : body.renderBody) {
2196
- this.#writeChildren(child, body.renderBody);
2314
+ if (body == null ? void 0 : body.content) {
2315
+ this.#writeChildren(child, body.content);
2197
2316
  }
2198
2317
  this.#writeReturn(
2199
2318
  void 0,
2200
- (body == null ? void 0 : body.renderBody) ? getHoistSources(child) : void 0
2319
+ (body == null ? void 0 : body.content) ? getHoistSources(child) : void 0
2201
2320
  );
2202
2321
  if (renderId) {
2203
2322
  this.#extractor.write("\n}));\n");
@@ -2212,8 +2331,8 @@ constructor(_?: Return) {}
2212
2331
  this.#getRangeWithoutTrailingComma((_b = child.args) == null ? void 0 : _b.value) || "undefined"
2213
2332
  ).write("\n) {\n");
2214
2333
  const body = this.#processBody(child);
2215
- if (body == null ? void 0 : body.renderBody) {
2216
- this.#writeChildren(child, body.renderBody);
2334
+ if (body == null ? void 0 : body.content) {
2335
+ this.#writeChildren(child, body.content);
2217
2336
  }
2218
2337
  this.#extractor.write("\n}\n");
2219
2338
  break;
@@ -2273,51 +2392,53 @@ constructor(_?: Return) {}
2273
2392
  #writeTag(tag) {
2274
2393
  const tagName = tag.nameText;
2275
2394
  const renderId = this.#getRenderId(tag);
2276
- let nestedTagType;
2395
+ const def = tagName ? this.#lookup.getTag(tagName) : void 0;
2277
2396
  if (renderId) {
2278
2397
  this.#extractor.write(
2279
- `${varShared("assertRendered")}(${varShared(
2280
- "rendered"
2281
- )}, ${renderId}, `
2398
+ `${varShared("assertRendered")}(${varShared("rendered")},${renderId},`
2282
2399
  );
2283
2400
  }
2284
- if (tagName) {
2285
- const def = this.#lookup.getTag(tagName);
2286
- if (def) {
2287
- const importPath = resolveTagImport(this.#filename, def);
2288
- const isMarkoFile = importPath == null ? void 0 : importPath.endsWith(".marko");
2289
- if (isMarkoFile) {
2290
- nestedTagType = `import("${importPath}").Input`;
2291
- }
2292
- const renderer = (importPath == null ? void 0 : importPath.endsWith(".marko")) ? `renderTemplate(import("${importPath}"))` : def.html ? `renderNativeTag("${def.name}")` : "missingTag";
2293
- 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");
2294
2424
  this.#extractor.write(
2295
- `${varShared("renderPreferLocal")}(
2296
- // @ts-expect-error We expect the compiler to error because we are checking if the tag is defined.
2297
- (${varShared("error")}, `
2298
- ).copy(tag.name).write(`),
2299
- ${varShared(renderer)})`);
2425
+ `${varShared("resolveTemplate")}(import("${importPath}"))`
2426
+ );
2300
2427
  } else {
2301
- this.#extractor.write(varShared(renderer));
2428
+ this.#writeDynamicTagName(tag);
2302
2429
  }
2303
- } else if (REG_TAG_NAME_IDENTIFIER.test(tagName)) {
2304
- nestedTagType = `Marko.Input<typeof ${this.#read(tag.name)}>`;
2305
- this.#extractor.write(`${varShared("renderDynamicTag")}(
2306
- `).copy(tag.name).write("\n)");
2307
- } else {
2308
- this.#extractor.write(`${varShared("missingTag")}`);
2430
+ this.#extractor.write(
2431
+ `
2432
+ )),${varShared(isDynamic ? "renderDynamicTag" : "renderTemplate")}(${varShared("tags")}[${tagId}]))`
2433
+ );
2309
2434
  }
2310
- } else {
2311
- this.#extractor.write(`${varShared("renderDynamicTag")}(`);
2312
- this.#writeDynamicTagName(tag);
2313
- this.#extractor.write(")");
2314
2435
  }
2315
2436
  if (tag.typeArgs) {
2316
2437
  this.#extractor.write(`<0>()`).copy(tag.typeArgs).write("()(");
2317
2438
  } else {
2318
2439
  this.#extractor.write("()()(");
2319
2440
  }
2320
- this.#writeTagInputObject(tag, nestedTagType);
2441
+ this.#writeTagInputObject(tag);
2321
2442
  if (renderId) {
2322
2443
  this.#extractor.write(`)`);
2323
2444
  }
@@ -2501,7 +2622,7 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2501
2622
  );
2502
2623
  return hasAttrs;
2503
2624
  }
2504
- #writeAttrTags({ staticAttrTags, dynamicAttrTagParents }, inMerge, nestedTagType) {
2625
+ #writeAttrTags({ staticAttrTags, dynamicAttrTagParents }, inMerge) {
2505
2626
  let wasMerge = false;
2506
2627
  if (dynamicAttrTagParents) {
2507
2628
  if (staticAttrTags) {
@@ -2517,7 +2638,7 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2517
2638
  }
2518
2639
  }
2519
2640
  if (staticAttrTags) {
2520
- this.#writeStaticAttrTags(staticAttrTags, inMerge, nestedTagType);
2641
+ this.#writeStaticAttrTags(staticAttrTags, inMerge);
2521
2642
  if (dynamicAttrTagParents)
2522
2643
  this.#extractor.write(`}${SEP_COMMA_NEW_LINE}`);
2523
2644
  }
@@ -2526,7 +2647,7 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2526
2647
  if (wasMerge) this.#extractor.write(`)${SEP_COMMA_NEW_LINE}`);
2527
2648
  }
2528
2649
  }
2529
- #writeStaticAttrTags(staticAttrTags, wasMerge, nestedTagType) {
2650
+ #writeStaticAttrTags(staticAttrTags, wasMerge) {
2530
2651
  if (!wasMerge) this.#extractor.write("...{");
2531
2652
  this.#extractor.write(
2532
2653
  `[${varShared("never")}](){
@@ -2547,36 +2668,40 @@ const attrTags = ${varShared(
2547
2668
  this.#extractor.write(SEP_COMMA_NEW_LINE);
2548
2669
  for (const nameText in staticAttrTags) {
2549
2670
  const attrTag = staticAttrTags[nameText];
2550
- const attrTagDef = this.#lookup.getTag(nameText);
2551
2671
  const isRepeated = attrTag.length > 1;
2552
2672
  const [firstAttrTag] = attrTag;
2553
- const name = (attrTagDef == null ? void 0 : attrTagDef.targetProperty) || nameText.slice(nameText.lastIndexOf(":") + 1);
2673
+ const name = this.#getAttrTagName(firstAttrTag);
2554
2674
  this.#extractor.write(`["${name}"`);
2555
2675
  this.#writeTagNameComment(firstAttrTag);
2556
2676
  this.#extractor.write("]: ");
2557
2677
  if (isRepeated) {
2558
- this.#extractor.write(`${varShared("repeatedAttrTag")}(
2559
- ...
2560
- `);
2561
- 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
+ }
2562
2690
  this.#extractor.write(
2563
- `/** @satisfies {${nestedTagType}["${name}"][]} */
2691
+ `${varShared("attrTags")}(${varShared("input")}(${varShared("tags")}[${tagId}])${accessor})([
2564
2692
  `
2565
2693
  );
2566
- }
2567
- this.#extractor.write(`([
2694
+ } else {
2695
+ this.#extractor.write(`${varShared("attrTags")}()([
2568
2696
  `);
2697
+ }
2569
2698
  }
2570
2699
  for (const childNode of attrTag) {
2571
2700
  this.#writeTagInputObject(childNode);
2572
2701
  this.#extractor.write(SEP_COMMA_NEW_LINE);
2573
2702
  }
2574
2703
  if (isRepeated) {
2575
- this.#extractor.write("])");
2576
- if (nestedTagType && this.#scriptLang === "ts" /* ts */) {
2577
- this.#extractor.write(` satisfies ${nestedTagType}["${name}"][]`);
2578
- }
2579
- this.#extractor.write(`)${SEP_COMMA_NEW_LINE}`);
2704
+ this.#extractor.write(`])${SEP_COMMA_NEW_LINE}`);
2580
2705
  }
2581
2706
  }
2582
2707
  }
@@ -2639,7 +2764,7 @@ const attrTags = ${varShared(
2639
2764
  this.#extractor.write(SEP_COMMA_NEW_LINE);
2640
2765
  }
2641
2766
  }
2642
- #writeTagInputObject(tag, nestedTagType) {
2767
+ #writeTagInputObject(tag) {
2643
2768
  if (!tag.params) this.#writeComments(tag);
2644
2769
  let hasInput = false;
2645
2770
  this.#extractor.write("{\n");
@@ -2657,7 +2782,7 @@ const attrTags = ${varShared(
2657
2782
  hasInput = true;
2658
2783
  }
2659
2784
  const isScript = isTextOnlyScript(tag);
2660
- let hasRenderBody = false;
2785
+ let hasBodyContent = false;
2661
2786
  let body;
2662
2787
  if (isScript) {
2663
2788
  this.#extractor.write("async value(){");
@@ -2670,14 +2795,35 @@ const attrTags = ${varShared(
2670
2795
  body = this.#processBody(tag);
2671
2796
  if (body) {
2672
2797
  hasInput = true;
2673
- this.#writeAttrTags(body, false, nestedTagType);
2674
- hasRenderBody = body.renderBody !== void 0;
2798
+ this.#writeAttrTags(body, false);
2799
+ hasBodyContent = body.content !== void 0;
2675
2800
  } else if (tag.close) {
2676
- hasRenderBody = true;
2801
+ hasBodyContent = true;
2677
2802
  }
2678
2803
  }
2679
- if (tag.params || hasRenderBody) {
2680
- 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
+ }
2681
2827
  this.#writeTagNameComment(tag);
2682
2828
  this.#extractor.write("]: ");
2683
2829
  if (tag.params) {
@@ -2691,8 +2837,8 @@ const attrTags = ${varShared(
2691
2837
  `);
2692
2838
  }
2693
2839
  let didReturn = false;
2694
- if (body == null ? void 0 : body.renderBody) {
2695
- didReturn = this.#writeChildren(tag, body.renderBody);
2840
+ if (body == null ? void 0 : body.content) {
2841
+ didReturn = this.#writeChildren(tag, body.content);
2696
2842
  }
2697
2843
  if (!tag.params) {
2698
2844
  this.#extractor.write(`return () => {
@@ -2781,7 +2927,7 @@ const attrTags = ${varShared(
2781
2927
  const { body } = parent;
2782
2928
  if (!body) return;
2783
2929
  const last = body.length - 1;
2784
- let renderBody;
2930
+ let content;
2785
2931
  let staticAttrTags;
2786
2932
  let dynamicAttrTagParents;
2787
2933
  let i = 0;
@@ -2870,48 +3016,48 @@ const attrTags = ${varShared(
2870
3016
  } else {
2871
3017
  dynamicAttrTagParents = [child];
2872
3018
  }
2873
- } else if (renderBody) {
2874
- renderBody.push(child);
3019
+ } else if (content) {
3020
+ content.push(child);
2875
3021
  } else {
2876
- renderBody = [child];
3022
+ content = [child];
2877
3023
  }
2878
3024
  break;
2879
3025
  }
2880
3026
  case 17 /* Text */: {
2881
3027
  if (!this.#isEmptyText(child)) {
2882
- if (renderBody) {
2883
- renderBody.push(child);
3028
+ if (content) {
3029
+ content.push(child);
2884
3030
  } else {
2885
- renderBody = [child];
3031
+ content = [child];
2886
3032
  }
2887
3033
  }
2888
3034
  break;
2889
3035
  }
2890
3036
  default:
2891
- if (renderBody) {
2892
- renderBody.push(child);
3037
+ if (content) {
3038
+ content.push(child);
2893
3039
  } else {
2894
- renderBody = [child];
3040
+ content = [child];
2895
3041
  }
2896
3042
  break;
2897
3043
  }
2898
3044
  }
2899
- if (renderBody || staticAttrTags || dynamicAttrTagParents) {
2900
- return { renderBody, staticAttrTags, dynamicAttrTagParents };
3045
+ if (content || staticAttrTags || dynamicAttrTagParents) {
3046
+ return { content, staticAttrTags, dynamicAttrTagParents };
2901
3047
  }
2902
3048
  }
2903
3049
  #writeDynamicAttrTagBody(tag) {
2904
3050
  const body = this.#processBody(tag);
2905
3051
  if (body) {
2906
- if (body.renderBody) {
3052
+ if (body.content) {
2907
3053
  this.#extractor.write("(() => {\n");
2908
- this.#writeChildren(tag, body.renderBody);
3054
+ this.#writeChildren(tag, body.content);
2909
3055
  this.#extractor.write("return ");
2910
3056
  }
2911
3057
  this.#extractor.write("{\n");
2912
3058
  this.#writeAttrTags(body, true);
2913
3059
  this.#extractor.write("}");
2914
- if (body.renderBody) {
3060
+ if (body.content) {
2915
3061
  this.#extractor.write(";\n})()");
2916
3062
  }
2917
3063
  } else {
@@ -3014,6 +3160,14 @@ const attrTags = ${varShared(
3014
3160
  }
3015
3161
  return renderId;
3016
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
+ }
3017
3171
  #getNamedAttrModifierIndex(attr) {
3018
3172
  const start = attr.name.start + 1;
3019
3173
  const end = attr.name.end - 1;
@@ -3022,6 +3176,11 @@ const attrTags = ${varShared(
3022
3176
  }
3023
3177
  return false;
3024
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
+ }
3025
3184
  #testAtIndex(reg, index) {
3026
3185
  reg.lastIndex = index;
3027
3186
  return reg.test(this.#code);
@@ -3511,6 +3670,7 @@ var marko_default = {
3511
3670
  return extractScript({
3512
3671
  ts,
3513
3672
  parsed,
3673
+ translator: getConfig(dir).translator,
3514
3674
  lookup: getTagLookup(dir),
3515
3675
  scriptLang: getScriptLang(
3516
3676
  fileName,
@@ -3660,6 +3820,7 @@ export {
3660
3820
  getLines,
3661
3821
  getLocation,
3662
3822
  getPosition,
3823
+ isControlFlowTag,
3663
3824
  isDefinitionFile,
3664
3825
  parse
3665
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
  };
@@ -40,6 +40,19 @@ declare global {
40
40
  returns: Record<number, never>;
41
41
  };
42
42
 
43
+ export const tags: Record<number, unknown>;
44
+ export const content: DefaultBodyContentKey;
45
+
46
+ export function contentFor<Name>(
47
+ tag: Name,
48
+ ): Name extends { api: infer API }
49
+ ? API extends "tags"
50
+ ? "content"
51
+ : API extends "class"
52
+ ? "renderBody"
53
+ : DefaultBodyContentKey
54
+ : DefaultBodyContentKey;
55
+
43
56
  export const Template: new <Overrides = unknown>() => {
44
57
  [K in Exclude<
45
58
  keyof Marko.Template,
@@ -82,6 +95,12 @@ declare global {
82
95
  > &
83
96
  Record<any, never>;
84
97
 
98
+ export function assertTag<Index extends number, Tags, Tag>(
99
+ tags: Tags,
100
+ index: Index,
101
+ tag: Tag,
102
+ ): asserts tags is Tags & Record<Index, Tag>;
103
+
85
104
  export function assertRendered<Index extends number, Rendered, Result>(
86
105
  rendered: Rendered,
87
106
  index: Index,
@@ -134,16 +153,20 @@ declare global {
134
153
  : (...args: any) => any; // If typescript ever actually supports partial application maybe we do this.
135
154
 
136
155
  export function renderTemplate<Name extends Marko.Template>(
137
- imported: Promise<{ default: Name }>,
156
+ template: Name,
138
157
  ): TemplateRenderer<Name>;
139
158
  export function renderNativeTag<Name extends string>(
140
159
  tag: Name,
141
160
  ): NativeTagRenderer<Name>;
142
161
  export const missingTag: DefaultRenderer;
143
- export function renderPreferLocal<Name, Fallback>(
144
- name: Name,
145
- fallback: Fallback,
146
- ): [0] extends [1 & Name] ? Fallback : DynamicRenderer<Name>;
162
+ export function resolveTemplate<Template>(
163
+ imported: Promise<{ default: Template }>,
164
+ ): Template;
165
+ export function fallbackTemplate<Tag, Template>(
166
+ tag: Tag,
167
+ fallback: Promise<{ default: Template }>,
168
+ ): [0] extends [1 & Tag] ? Template : Tag;
169
+ export function input<Name>(tag: Name): Marko.Input<Name>;
147
170
  export function renderDynamicTag<Name>(tag: Name): DynamicRenderer<Name>;
148
171
 
149
172
  export function returnTag<
@@ -301,10 +324,18 @@ declare global {
301
324
  export function mergeAttrTags<Attrs extends readonly any[]>(
302
325
  ...attrs: Attrs
303
326
  ): MergeAttrTags<Attrs>;
304
-
305
- export function repeatedAttrTag<AttrTag>(
306
- ...attrTags: Marko.AttrTag<AttrTag>[]
307
- ): AttrTag;
327
+ export function attrTags<T>(
328
+ type?: T,
329
+ ): <AttrTag>(
330
+ attrTags: Relate<
331
+ AttrTag,
332
+ [0] extends [1 & T]
333
+ ? Marko.AttrTag<unknown>
334
+ : T extends Marko.AttrTag<infer Type>
335
+ ? Marko.AttrTag<Type>
336
+ : Marko.AttrTag<unknown>
337
+ >[],
338
+ ) => AttrTag;
308
339
 
309
340
  // TODO: this could be improved.
310
341
  // currently falls back to DefaultRenderer too eagerly.
@@ -316,12 +347,14 @@ declare global {
316
347
  ? NativeTagRenderer<Name>
317
348
  : [Name] extends [AnyMarkoBody]
318
349
  ? BodyRenderer<Name>
319
- : [Name] extends [{ renderBody?: AnyMarkoBody }]
320
- ? [Name["renderBody"]] extends [AnyMarkoBody]
321
- ? BodyRenderer<Name["renderBody"]>
350
+ : [Name] extends [{ [BodyContentKey]?: AnyMarkoBody }]
351
+ ? [Name[DefaultBodyContentKey]] extends [AnyMarkoBody]
352
+ ? BodyRenderer<Name[DefaultBodyContentKey]>
322
353
  : BaseRenderer<
323
354
  BodyContentInput<
324
- BodyParameters<Exclude<Name["renderBody"], void>>
355
+ BodyParameters<
356
+ Exclude<Name[DefaultBodyContentKey], void>
357
+ >
325
358
  >
326
359
  >
327
360
  : DefaultRenderer;
@@ -535,4 +568,9 @@ type UnionToIntersection<T> = (T extends any ? (_: T) => any : never) extends (
535
568
  ? U
536
569
  : never;
537
570
 
571
+ type DefaultBodyContentKey = keyof Exclude<
572
+ Marko.Renderable,
573
+ Marko.Template<any, any> | Marko.Body<any, any> | string
574
+ >;
575
+
538
576
  export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@marko/language-tools",
3
3
  "description": "Marko Language Tools",
4
- "version": "2.5.5",
4
+ "version": "2.5.6",
5
5
  "bugs": "https://github.com/marko-js/language-server/issues/new?template=Bug_report.md",
6
6
  "peerDependencies": {
7
7
  "@marko/compiler": "^5.28.4"
@@ -14,10 +14,10 @@
14
14
  },
15
15
  "devDependencies": {
16
16
  "@babel/code-frame": "^7.26.2",
17
- "@marko/compiler": "^5.39.4",
17
+ "@marko/compiler": "^5.39.6",
18
18
  "@types/babel__code-frame": "^7.0.6",
19
19
  "@typescript/vfs": "^1.6.0",
20
- "marko": "^5.37.4",
20
+ "marko": "^5.37.7",
21
21
  "mitata": "^1.0.21",
22
22
  "tsx": "^4.19.2"
23
23
  },