@marko/language-tools 2.5.30 → 2.5.31

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) {}
@@ -2319,12 +2305,13 @@ constructor(_?: Return) {}
2319
2305
  ).write(") {\n");
2320
2306
  const ifBody = this.#processBody(child);
2321
2307
  if (ifBody == null ? void 0 : ifBody.content) {
2322
- const localBindings = getHoistSources(child.body);
2308
+ const scopeExpr = this.#getScopeExpression(child.body);
2323
2309
  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");
2310
+ if (scopeExpr) {
2311
+ this.#extractor.write(`return {
2312
+ scope: ${scopeExpr}
2313
+ };
2314
+ `);
2328
2315
  }
2329
2316
  }
2330
2317
  let needsAlternate = true;
@@ -2341,12 +2328,15 @@ constructor(_?: Return) {}
2341
2328
  }
2342
2329
  const alternateBody = this.#processBody(node);
2343
2330
  if (alternateBody == null ? void 0 : alternateBody.content) {
2344
- const localBindings = getHoistSources(node.body);
2331
+ const scopeExpr = this.#getScopeExpression(node.body);
2345
2332
  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");
2333
+ if (scopeExpr) {
2334
+ this.#extractor.write(
2335
+ `return {
2336
+ scope: ${scopeExpr}
2337
+ };
2338
+ `
2339
+ );
2350
2340
  }
2351
2341
  }
2352
2342
  }
@@ -2357,7 +2347,7 @@ constructor(_?: Return) {}
2357
2347
  this.#extractor.write("\n}\n");
2358
2348
  }
2359
2349
  if (renderId) {
2360
- this.#extractor.write("\n})()\n");
2350
+ this.#extractor.write("\n})();\n");
2361
2351
  }
2362
2352
  break;
2363
2353
  }
@@ -2922,13 +2912,6 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2922
2912
  this.#extractor.write("\n}");
2923
2913
  }
2924
2914
  }
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
2915
  #getCastedType(type) {
2933
2916
  return this.#scriptLang === "ts" /* ts */ ? `${varShared("any")} as ${type}` : `/** @type {${type}} */(${varShared("any")})`;
2934
2917
  }
@@ -3213,31 +3196,34 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
3213
3196
  }
3214
3197
  #getRenderId(tag) {
3215
3198
  let renderId = this.#renderIds.get(tag);
3216
- if (renderId === void 0 && tag.var || hasHoists(tag)) {
3199
+ if (renderId === void 0 && (tag.var || hasHoists(tag))) {
3217
3200
  renderId = this.#renderId++;
3218
3201
  this.#renderIds.set(tag, renderId);
3219
3202
  }
3220
3203
  return renderId;
3221
3204
  }
3205
+ #getScopeExpression(body) {
3206
+ const sources = getHoistSources(body);
3207
+ const hoists = this.#getBodyHoistScopeExpression(body);
3208
+ return sources ? `{ ${hoists ? `...${hoists}, ` : ""}${sources.join(SEP_COMMA_SPACE)} }` : hoists;
3209
+ }
3222
3210
  #getBodyHoistScopeExpression(body) {
3223
3211
  let hoistIds;
3224
3212
  if (body) {
3225
3213
  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
- }
3214
+ if (child.type === 1 /* Tag */ && hasHoists(child)) {
3215
+ const renderId = this.#getRenderId(child);
3216
+ if (hoistIds) {
3217
+ hoistIds.push(renderId);
3218
+ } else {
3219
+ hoistIds = [renderId];
3234
3220
  }
3235
3221
  }
3236
3222
  }
3237
3223
  }
3238
3224
  if (hoistIds) {
3239
3225
  if (hoistIds.length === 1) {
3240
- return `${varLocal("rendered_" + hoistIds[0])}.scope`;
3226
+ return `${varShared("readScope")}(${varLocal("rendered_" + hoistIds[0])})`;
3241
3227
  }
3242
3228
  let result = `${varShared("readScopes")}({ `;
3243
3229
  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) {}
@@ -2282,12 +2268,13 @@ constructor(_?: Return) {}
2282
2268
  ).write(") {\n");
2283
2269
  const ifBody = this.#processBody(child);
2284
2270
  if (ifBody == null ? void 0 : ifBody.content) {
2285
- const localBindings = getHoistSources(child.body);
2271
+ const scopeExpr = this.#getScopeExpression(child.body);
2286
2272
  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");
2273
+ if (scopeExpr) {
2274
+ this.#extractor.write(`return {
2275
+ scope: ${scopeExpr}
2276
+ };
2277
+ `);
2291
2278
  }
2292
2279
  }
2293
2280
  let needsAlternate = true;
@@ -2304,12 +2291,15 @@ constructor(_?: Return) {}
2304
2291
  }
2305
2292
  const alternateBody = this.#processBody(node);
2306
2293
  if (alternateBody == null ? void 0 : alternateBody.content) {
2307
- const localBindings = getHoistSources(node.body);
2294
+ const scopeExpr = this.#getScopeExpression(node.body);
2308
2295
  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");
2296
+ if (scopeExpr) {
2297
+ this.#extractor.write(
2298
+ `return {
2299
+ scope: ${scopeExpr}
2300
+ };
2301
+ `
2302
+ );
2313
2303
  }
2314
2304
  }
2315
2305
  }
@@ -2320,7 +2310,7 @@ constructor(_?: Return) {}
2320
2310
  this.#extractor.write("\n}\n");
2321
2311
  }
2322
2312
  if (renderId) {
2323
- this.#extractor.write("\n})()\n");
2313
+ this.#extractor.write("\n})();\n");
2324
2314
  }
2325
2315
  break;
2326
2316
  }
@@ -2885,13 +2875,6 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2885
2875
  this.#extractor.write("\n}");
2886
2876
  }
2887
2877
  }
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
2878
  #getCastedType(type) {
2896
2879
  return this.#scriptLang === "ts" /* ts */ ? `${varShared("any")} as ${type}` : `/** @type {${type}} */(${varShared("any")})`;
2897
2880
  }
@@ -3176,31 +3159,34 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
3176
3159
  }
3177
3160
  #getRenderId(tag) {
3178
3161
  let renderId = this.#renderIds.get(tag);
3179
- if (renderId === void 0 && tag.var || hasHoists(tag)) {
3162
+ if (renderId === void 0 && (tag.var || hasHoists(tag))) {
3180
3163
  renderId = this.#renderId++;
3181
3164
  this.#renderIds.set(tag, renderId);
3182
3165
  }
3183
3166
  return renderId;
3184
3167
  }
3168
+ #getScopeExpression(body) {
3169
+ const sources = getHoistSources(body);
3170
+ const hoists = this.#getBodyHoistScopeExpression(body);
3171
+ return sources ? `{ ${hoists ? `...${hoists}, ` : ""}${sources.join(SEP_COMMA_SPACE)} }` : hoists;
3172
+ }
3185
3173
  #getBodyHoistScopeExpression(body) {
3186
3174
  let hoistIds;
3187
3175
  if (body) {
3188
3176
  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
- }
3177
+ if (child.type === 1 /* Tag */ && hasHoists(child)) {
3178
+ const renderId = this.#getRenderId(child);
3179
+ if (hoistIds) {
3180
+ hoistIds.push(renderId);
3181
+ } else {
3182
+ hoistIds = [renderId];
3197
3183
  }
3198
3184
  }
3199
3185
  }
3200
3186
  }
3201
3187
  if (hoistIds) {
3202
3188
  if (hoistIds.length === 1) {
3203
- return `${varLocal("rendered_" + hoistIds[0])}.scope`;
3189
+ return `${varShared("readScope")}(${varLocal("rendered_" + hoistIds[0])})`;
3204
3190
  }
3205
3191
  let result = `${varShared("readScopes")}({ `;
3206
3192
  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.31",
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.44",
21
21
  "mitata": "^1.0.34",
22
22
  "tsx": "^4.20.4"
23
23
  },