@marko/language-tools 2.5.30 → 2.5.32

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 +1,2 @@
1
- export declare function getRuntimeOverrides(runtimeTypes: string, generics: string, applyGenerics: string, returnType: string): string;
1
+ import { RuntimeAPI } from "./get-runtime-api";
2
+ export declare function getRuntimeOverrides(api: RuntimeAPI, runtimeTypes: string, generics: string, applyGenerics: string, returnType: string): string;
package/dist/index.js CHANGED
@@ -1823,7 +1823,7 @@ var RuntimeOverloads = /* @__PURE__ */ new Map();
1823
1823
  var commentsReg = /\/\*(?:[^*]|\*[^/])*\*\//gm;
1824
1824
  var replaceTokensReg = /\babstract\s+(\w+)|Marko\.(TemplateInput(?:<[^>]+>)?|Component)|\b(Return)\b/gm;
1825
1825
  var overrideBlockReg = /\/\*[*\s]*@marko-overload-start[*\s]*\*\/([\s\S]+)\/\*[*\s]*@marko-overload-end[*\s]*\*\//g;
1826
- function getRuntimeOverrides(runtimeTypes, generics, applyGenerics, returnType) {
1826
+ function getRuntimeOverrides(api, runtimeTypes, generics, applyGenerics, returnType) {
1827
1827
  let overloads = RuntimeOverloads.get(runtimeTypes);
1828
1828
  if (!overloads) {
1829
1829
  const match = overrideBlockReg.exec(runtimeTypes);
@@ -1853,7 +1853,7 @@ function getRuntimeOverrides(runtimeTypes, generics, applyGenerics, returnType)
1853
1853
  }
1854
1854
  let result = "";
1855
1855
  const appliedInput = `Marko.TemplateInput<Input${applyGenerics}>`;
1856
- const appliedComponent = `Component${applyGenerics}`;
1856
+ const appliedComponent = api === RuntimeAPI.tags ? "never" : `Component${applyGenerics}`;
1857
1857
  for (const part of overloads) {
1858
1858
  switch (part) {
1859
1859
  case 0 /* Generics */:
@@ -2154,15 +2154,13 @@ function ${templateName}() {
2154
2154
  }
2155
2155
  const hoists = getHoists(program);
2156
2156
  if (hoists) {
2157
- this.#extractor.write("const ");
2158
- this.#writeObjectKeys(hoists);
2159
2157
  this.#extractor.write(
2160
- " = " + this.#getBodyHoistScopeExpression(program.body)
2158
+ `const { ${hoists.join(SEP_COMMA_SPACE)} } = ${this.#getBodyHoistScopeExpression(program.body)};
2159
+ `
2161
2160
  );
2162
2161
  }
2163
2162
  this.#extractor.write(
2164
- `
2165
- ;${varShared("noop")}({ ${hoists ? hoists.join(SEP_COMMA_SPACE) + SEP_COMMA_SPACE : ""}${this.#api !== RuntimeAPI.tags ? "component, state, out, " : ""}input, $global, $signal });
2163
+ `${varShared("noop")}({ ${hoists ? hoists.join(SEP_COMMA_SPACE) + SEP_COMMA_SPACE : ""}${this.#api !== RuntimeAPI.tags ? "component, state, out, " : ""}input, $global, $signal });
2166
2164
  `
2167
2165
  );
2168
2166
  if (didReturn) {
@@ -2183,6 +2181,7 @@ function ${templateName}() {
2183
2181
  "ReturnWithScope"
2184
2182
  )}<${internalInput}, ${returnTypeStr}>)`;
2185
2183
  const templateOverrideClass = `${templateBaseClass}<{${this.#runtimeTypes ? getRuntimeOverrides(
2184
+ this.#api,
2186
2185
  this.#runtimeTypes,
2187
2186
  typeParamsStr,
2188
2187
  typeArgsStr,
@@ -2251,30 +2250,17 @@ function ${templateName}() {
2251
2250
  }
2252
2251
  }
2253
2252
  #writeReturn(returned, body) {
2254
- const hoistSources = getHoistSources(body);
2255
- const hoistScopes = this.#getBodyHoistScopeExpression(body);
2256
- const hasHoists2 = !!(hoistScopes || hoistSources);
2257
- if (!returned && !hasHoists2) {
2253
+ const scopeExpr = this.#getScopeExpression(body);
2254
+ if (!returned && !scopeExpr) {
2258
2255
  this.#extractor.write(`return ${varShared("voidReturn")};
2259
2256
  `);
2260
2257
  return;
2261
2258
  }
2262
2259
  this.#extractor.write(`return new (class MarkoReturn<Return = void> {
2263
2260
  `);
2264
- if (hasHoists2) {
2265
- this.#extractor.write("[Marko._.scope] = ");
2266
- if (hoistSources) {
2267
- if (hoistScopes) {
2268
- this.#extractor.write(`{ ...${hoistScopes}, ...`);
2269
- this.#writeObjectKeys(hoistSources);
2270
- this.#extractor.write(" }");
2271
- } else {
2272
- this.#writeObjectKeys(hoistSources);
2273
- }
2274
- } else {
2275
- this.#extractor.write(hoistScopes);
2276
- }
2277
- this.#extractor.write(";\n");
2261
+ if (scopeExpr) {
2262
+ this.#extractor.write(`[Marko._.scope] = ${scopeExpr};
2263
+ `);
2278
2264
  }
2279
2265
  this.#extractor.write(`declare return: Return;
2280
2266
  constructor(_?: Return) {}
@@ -2303,7 +2289,11 @@ constructor(_?: Return) {}
2303
2289
  renderId = this.#getRenderId(child);
2304
2290
  if (!renderId && alternates) {
2305
2291
  for (const { node } of alternates) {
2306
- if (renderId = this.#getRenderId(node)) break;
2292
+ if (renderId = this.#getRenderId(node)) {
2293
+ this.#renderIds.set(child, renderId);
2294
+ this.#renderIds.delete(node);
2295
+ break;
2296
+ }
2307
2297
  }
2308
2298
  }
2309
2299
  if (renderId) {
@@ -2319,12 +2309,13 @@ constructor(_?: Return) {}
2319
2309
  ).write(") {\n");
2320
2310
  const ifBody = this.#processBody(child);
2321
2311
  if (ifBody == null ? void 0 : ifBody.content) {
2322
- const localBindings = getHoistSources(child.body);
2323
2312
  this.#writeChildren(child, ifBody.content, true);
2324
- if (localBindings) {
2325
- this.#extractor.write("return {\nscope:");
2326
- this.#writeObjectKeys(localBindings);
2327
- this.#extractor.write("\n};\n");
2313
+ const scopeExpr = this.#getScopeExpression(child.body);
2314
+ if (scopeExpr) {
2315
+ this.#extractor.write(`return {
2316
+ scope: ${scopeExpr}
2317
+ };
2318
+ `);
2328
2319
  }
2329
2320
  }
2330
2321
  let needsAlternate = true;
@@ -2341,12 +2332,15 @@ constructor(_?: Return) {}
2341
2332
  }
2342
2333
  const alternateBody = this.#processBody(node);
2343
2334
  if (alternateBody == null ? void 0 : alternateBody.content) {
2344
- const localBindings = getHoistSources(node.body);
2345
2335
  this.#writeChildren(node, alternateBody.content, true);
2346
- if (localBindings) {
2347
- this.#extractor.write("return {\nscope:");
2348
- this.#writeObjectKeys(localBindings);
2349
- this.#extractor.write("\n};\n");
2336
+ const scopeExpr = this.#getScopeExpression(node.body);
2337
+ if (scopeExpr) {
2338
+ this.#extractor.write(
2339
+ `return {
2340
+ scope: ${scopeExpr}
2341
+ };
2342
+ `
2343
+ );
2350
2344
  }
2351
2345
  }
2352
2346
  }
@@ -2357,7 +2351,7 @@ constructor(_?: Return) {}
2357
2351
  this.#extractor.write("\n}\n");
2358
2352
  }
2359
2353
  if (renderId) {
2360
- this.#extractor.write("\n})()\n");
2354
+ this.#extractor.write("\n})();\n");
2361
2355
  }
2362
2356
  break;
2363
2357
  }
@@ -2922,13 +2916,6 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2922
2916
  this.#extractor.write("\n}");
2923
2917
  }
2924
2918
  }
2925
- #writeObjectKeys(keys) {
2926
- this.#extractor.write("{");
2927
- for (const key of keys) {
2928
- this.#extractor.write(key + SEP_COMMA_SPACE);
2929
- }
2930
- this.#extractor.write("}");
2931
- }
2932
2919
  #getCastedType(type) {
2933
2920
  return this.#scriptLang === "ts" /* ts */ ? `${varShared("any")} as ${type}` : `/** @type {${type}} */(${varShared("any")})`;
2934
2921
  }
@@ -3213,31 +3200,34 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
3213
3200
  }
3214
3201
  #getRenderId(tag) {
3215
3202
  let renderId = this.#renderIds.get(tag);
3216
- if (renderId === void 0 && tag.var || hasHoists(tag)) {
3203
+ if (!renderId && (tag.var || hasHoists(tag))) {
3217
3204
  renderId = this.#renderId++;
3218
3205
  this.#renderIds.set(tag, renderId);
3219
3206
  }
3220
3207
  return renderId;
3221
3208
  }
3209
+ #getScopeExpression(body) {
3210
+ const sources = getHoistSources(body);
3211
+ const hoists = this.#getBodyHoistScopeExpression(body);
3212
+ return sources ? `{ ${hoists ? `...${hoists}, ` : ""}${sources.join(SEP_COMMA_SPACE)} }` : hoists;
3213
+ }
3222
3214
  #getBodyHoistScopeExpression(body) {
3223
3215
  let hoistIds;
3224
3216
  if (body) {
3225
3217
  for (const child of body) {
3226
- if (child.type === 1 /* Tag */) {
3227
- const renderId = this.#renderIds.get(child);
3228
- if (renderId !== void 0 && hasHoists(child)) {
3229
- if (hoistIds) {
3230
- hoistIds.push(renderId);
3231
- } else {
3232
- hoistIds = [renderId];
3233
- }
3218
+ const renderId = child.type === 1 /* Tag */ && this.#renderIds.get(child);
3219
+ if (renderId && (!child.var || hasHoists(child))) {
3220
+ if (hoistIds) {
3221
+ hoistIds.push(renderId);
3222
+ } else {
3223
+ hoistIds = [renderId];
3234
3224
  }
3235
3225
  }
3236
3226
  }
3237
3227
  }
3238
3228
  if (hoistIds) {
3239
3229
  if (hoistIds.length === 1) {
3240
- return `${varLocal("rendered_" + hoistIds[0])}.scope`;
3230
+ return `${varShared("readScope")}(${varLocal("rendered_" + hoistIds[0])})`;
3241
3231
  }
3242
3232
  let result = `${varShared("readScopes")}({ `;
3243
3233
  let sep = "";
package/dist/index.mjs CHANGED
@@ -1783,7 +1783,7 @@ var RuntimeOverloads = /* @__PURE__ */ new Map();
1783
1783
  var commentsReg = /\/\*(?:[^*]|\*[^/])*\*\//gm;
1784
1784
  var replaceTokensReg = /\babstract\s+(\w+)|Marko\.(TemplateInput(?:<[^>]+>)?|Component)|\b(Return)\b/gm;
1785
1785
  var overrideBlockReg = /\/\*[*\s]*@marko-overload-start[*\s]*\*\/([\s\S]+)\/\*[*\s]*@marko-overload-end[*\s]*\*\//g;
1786
- function getRuntimeOverrides(runtimeTypes, generics, applyGenerics, returnType) {
1786
+ function getRuntimeOverrides(api, runtimeTypes, generics, applyGenerics, returnType) {
1787
1787
  let overloads = RuntimeOverloads.get(runtimeTypes);
1788
1788
  if (!overloads) {
1789
1789
  const match = overrideBlockReg.exec(runtimeTypes);
@@ -1813,7 +1813,7 @@ function getRuntimeOverrides(runtimeTypes, generics, applyGenerics, returnType)
1813
1813
  }
1814
1814
  let result = "";
1815
1815
  const appliedInput = `Marko.TemplateInput<Input${applyGenerics}>`;
1816
- const appliedComponent = `Component${applyGenerics}`;
1816
+ const appliedComponent = api === RuntimeAPI.tags ? "never" : `Component${applyGenerics}`;
1817
1817
  for (const part of overloads) {
1818
1818
  switch (part) {
1819
1819
  case 0 /* Generics */:
@@ -2117,15 +2117,13 @@ function ${templateName}() {
2117
2117
  }
2118
2118
  const hoists = getHoists(program);
2119
2119
  if (hoists) {
2120
- this.#extractor.write("const ");
2121
- this.#writeObjectKeys(hoists);
2122
2120
  this.#extractor.write(
2123
- " = " + this.#getBodyHoistScopeExpression(program.body)
2121
+ `const { ${hoists.join(SEP_COMMA_SPACE)} } = ${this.#getBodyHoistScopeExpression(program.body)};
2122
+ `
2124
2123
  );
2125
2124
  }
2126
2125
  this.#extractor.write(
2127
- `
2128
- ;${varShared("noop")}({ ${hoists ? hoists.join(SEP_COMMA_SPACE) + SEP_COMMA_SPACE : ""}${this.#api !== RuntimeAPI.tags ? "component, state, out, " : ""}input, $global, $signal });
2126
+ `${varShared("noop")}({ ${hoists ? hoists.join(SEP_COMMA_SPACE) + SEP_COMMA_SPACE : ""}${this.#api !== RuntimeAPI.tags ? "component, state, out, " : ""}input, $global, $signal });
2129
2127
  `
2130
2128
  );
2131
2129
  if (didReturn) {
@@ -2146,6 +2144,7 @@ function ${templateName}() {
2146
2144
  "ReturnWithScope"
2147
2145
  )}<${internalInput}, ${returnTypeStr}>)`;
2148
2146
  const templateOverrideClass = `${templateBaseClass}<{${this.#runtimeTypes ? getRuntimeOverrides(
2147
+ this.#api,
2149
2148
  this.#runtimeTypes,
2150
2149
  typeParamsStr,
2151
2150
  typeArgsStr,
@@ -2214,30 +2213,17 @@ function ${templateName}() {
2214
2213
  }
2215
2214
  }
2216
2215
  #writeReturn(returned, body) {
2217
- const hoistSources = getHoistSources(body);
2218
- const hoistScopes = this.#getBodyHoistScopeExpression(body);
2219
- const hasHoists2 = !!(hoistScopes || hoistSources);
2220
- if (!returned && !hasHoists2) {
2216
+ const scopeExpr = this.#getScopeExpression(body);
2217
+ if (!returned && !scopeExpr) {
2221
2218
  this.#extractor.write(`return ${varShared("voidReturn")};
2222
2219
  `);
2223
2220
  return;
2224
2221
  }
2225
2222
  this.#extractor.write(`return new (class MarkoReturn<Return = void> {
2226
2223
  `);
2227
- if (hasHoists2) {
2228
- this.#extractor.write("[Marko._.scope] = ");
2229
- if (hoistSources) {
2230
- if (hoistScopes) {
2231
- this.#extractor.write(`{ ...${hoistScopes}, ...`);
2232
- this.#writeObjectKeys(hoistSources);
2233
- this.#extractor.write(" }");
2234
- } else {
2235
- this.#writeObjectKeys(hoistSources);
2236
- }
2237
- } else {
2238
- this.#extractor.write(hoistScopes);
2239
- }
2240
- this.#extractor.write(";\n");
2224
+ if (scopeExpr) {
2225
+ this.#extractor.write(`[Marko._.scope] = ${scopeExpr};
2226
+ `);
2241
2227
  }
2242
2228
  this.#extractor.write(`declare return: Return;
2243
2229
  constructor(_?: Return) {}
@@ -2266,7 +2252,11 @@ constructor(_?: Return) {}
2266
2252
  renderId = this.#getRenderId(child);
2267
2253
  if (!renderId && alternates) {
2268
2254
  for (const { node } of alternates) {
2269
- if (renderId = this.#getRenderId(node)) break;
2255
+ if (renderId = this.#getRenderId(node)) {
2256
+ this.#renderIds.set(child, renderId);
2257
+ this.#renderIds.delete(node);
2258
+ break;
2259
+ }
2270
2260
  }
2271
2261
  }
2272
2262
  if (renderId) {
@@ -2282,12 +2272,13 @@ constructor(_?: Return) {}
2282
2272
  ).write(") {\n");
2283
2273
  const ifBody = this.#processBody(child);
2284
2274
  if (ifBody == null ? void 0 : ifBody.content) {
2285
- const localBindings = getHoistSources(child.body);
2286
2275
  this.#writeChildren(child, ifBody.content, true);
2287
- if (localBindings) {
2288
- this.#extractor.write("return {\nscope:");
2289
- this.#writeObjectKeys(localBindings);
2290
- this.#extractor.write("\n};\n");
2276
+ const scopeExpr = this.#getScopeExpression(child.body);
2277
+ if (scopeExpr) {
2278
+ this.#extractor.write(`return {
2279
+ scope: ${scopeExpr}
2280
+ };
2281
+ `);
2291
2282
  }
2292
2283
  }
2293
2284
  let needsAlternate = true;
@@ -2304,12 +2295,15 @@ constructor(_?: Return) {}
2304
2295
  }
2305
2296
  const alternateBody = this.#processBody(node);
2306
2297
  if (alternateBody == null ? void 0 : alternateBody.content) {
2307
- const localBindings = getHoistSources(node.body);
2308
2298
  this.#writeChildren(node, alternateBody.content, true);
2309
- if (localBindings) {
2310
- this.#extractor.write("return {\nscope:");
2311
- this.#writeObjectKeys(localBindings);
2312
- this.#extractor.write("\n};\n");
2299
+ const scopeExpr = this.#getScopeExpression(node.body);
2300
+ if (scopeExpr) {
2301
+ this.#extractor.write(
2302
+ `return {
2303
+ scope: ${scopeExpr}
2304
+ };
2305
+ `
2306
+ );
2313
2307
  }
2314
2308
  }
2315
2309
  }
@@ -2320,7 +2314,7 @@ constructor(_?: Return) {}
2320
2314
  this.#extractor.write("\n}\n");
2321
2315
  }
2322
2316
  if (renderId) {
2323
- this.#extractor.write("\n})()\n");
2317
+ this.#extractor.write("\n})();\n");
2324
2318
  }
2325
2319
  break;
2326
2320
  }
@@ -2885,13 +2879,6 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2885
2879
  this.#extractor.write("\n}");
2886
2880
  }
2887
2881
  }
2888
- #writeObjectKeys(keys) {
2889
- this.#extractor.write("{");
2890
- for (const key of keys) {
2891
- this.#extractor.write(key + SEP_COMMA_SPACE);
2892
- }
2893
- this.#extractor.write("}");
2894
- }
2895
2882
  #getCastedType(type) {
2896
2883
  return this.#scriptLang === "ts" /* ts */ ? `${varShared("any")} as ${type}` : `/** @type {${type}} */(${varShared("any")})`;
2897
2884
  }
@@ -3176,31 +3163,34 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
3176
3163
  }
3177
3164
  #getRenderId(tag) {
3178
3165
  let renderId = this.#renderIds.get(tag);
3179
- if (renderId === void 0 && tag.var || hasHoists(tag)) {
3166
+ if (!renderId && (tag.var || hasHoists(tag))) {
3180
3167
  renderId = this.#renderId++;
3181
3168
  this.#renderIds.set(tag, renderId);
3182
3169
  }
3183
3170
  return renderId;
3184
3171
  }
3172
+ #getScopeExpression(body) {
3173
+ const sources = getHoistSources(body);
3174
+ const hoists = this.#getBodyHoistScopeExpression(body);
3175
+ return sources ? `{ ${hoists ? `...${hoists}, ` : ""}${sources.join(SEP_COMMA_SPACE)} }` : hoists;
3176
+ }
3185
3177
  #getBodyHoistScopeExpression(body) {
3186
3178
  let hoistIds;
3187
3179
  if (body) {
3188
3180
  for (const child of body) {
3189
- if (child.type === 1 /* Tag */) {
3190
- const renderId = this.#renderIds.get(child);
3191
- if (renderId !== void 0 && hasHoists(child)) {
3192
- if (hoistIds) {
3193
- hoistIds.push(renderId);
3194
- } else {
3195
- hoistIds = [renderId];
3196
- }
3181
+ const renderId = child.type === 1 /* Tag */ && this.#renderIds.get(child);
3182
+ if (renderId && (!child.var || hasHoists(child))) {
3183
+ if (hoistIds) {
3184
+ hoistIds.push(renderId);
3185
+ } else {
3186
+ hoistIds = [renderId];
3197
3187
  }
3198
3188
  }
3199
3189
  }
3200
3190
  }
3201
3191
  if (hoistIds) {
3202
3192
  if (hoistIds.length === 1) {
3203
- return `${varLocal("rendered_" + hoistIds[0])}.scope`;
3193
+ return `${varShared("readScope")}(${varLocal("rendered_" + hoistIds[0])})`;
3204
3194
  }
3205
3195
  let result = `${varShared("readScopes")}({ `;
3206
3196
  let sep = "";
@@ -100,6 +100,23 @@ declare global {
100
100
  > &
101
101
  Record<any, never>;
102
102
 
103
+ export function readScope<Value>(
104
+ value: Value,
105
+ ): MergeScopes<
106
+ undefined extends Value
107
+ ? Value extends { scope: infer Scope }
108
+ ? [0] extends [1 & Scope]
109
+ ? never
110
+ : Partial<Scope>
111
+ : never
112
+ : Value extends { scope: infer Scope }
113
+ ? [0] extends [1 & Scope]
114
+ ? never
115
+ : Scope
116
+ : never
117
+ > &
118
+ Record<any, never>;
119
+
103
120
  export function mutable<Lookup>(lookup: Lookup): UnionToIntersection<
104
121
  Lookup extends readonly (infer Item)[]
105
122
  ? Item extends
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
1
  {
2
2
  "name": "@marko/language-tools",
3
3
  "description": "Marko Language Tools",
4
- "version": "2.5.30",
4
+ "version": "2.5.32",
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"
8
8
  },
9
9
  "dependencies": {
10
- "@babel/parser": "^7.28.0",
10
+ "@babel/parser": "^7.28.3",
11
11
  "@luxass/strip-json-comments": "^1.4.0",
12
- "htmljs-parser": "^5.7.3",
12
+ "htmljs-parser": "^5.7.4",
13
13
  "relative-import-path": "^1.0.0"
14
14
  },
15
15
  "devDependencies": {
16
16
  "@babel/code-frame": "^7.27.1",
17
- "@marko/compiler": "^5.39.32",
17
+ "@marko/compiler": "^5.39.33",
18
18
  "@types/babel__code-frame": "^7.0.6",
19
19
  "@typescript/vfs": "^1.6.1",
20
- "marko": "^5.37.43",
20
+ "marko": "^5.37.45",
21
21
  "mitata": "^1.0.34",
22
22
  "tsx": "^4.20.4"
23
23
  },