@marko/language-tools 2.0.5 → 2.0.7

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.js CHANGED
@@ -2000,7 +2000,7 @@ constructor(_?: Return) {}
2000
2000
  }
2001
2001
  this.#writeComments(child);
2002
2002
  this.#extractor.write("if (").copy(
2003
- ((_a = child.args) == null ? void 0 : _a.value) || this.#getAttrValue(child, ATTR_UNAMED2) || "undefined"
2003
+ this.#getRangeWithoutTrailingComma((_a = child.args) == null ? void 0 : _a.value) || this.#getAttrValue(child, ATTR_UNAMED2) || "undefined"
2004
2004
  ).write(") {\n");
2005
2005
  if (child.body) {
2006
2006
  const localBindings = getHoistSources(child);
@@ -2081,7 +2081,9 @@ constructor(_?: Return) {}
2081
2081
  }
2082
2082
  case "while": {
2083
2083
  this.#writeComments(child);
2084
- this.#extractor.write("while (\n").copy(((_b = child.args) == null ? void 0 : _b.value) || "undefined").write("\n) {\n");
2084
+ this.#extractor.write("while (\n").copy(
2085
+ this.#getRangeWithoutTrailingComma((_b = child.args) == null ? void 0 : _b.value) || "undefined"
2086
+ ).write("\n) {\n");
2085
2087
  const body = this.#processBody(child);
2086
2088
  if (body == null ? void 0 : body.renderBody) {
2087
2089
  this.#writeChildren(child, body.renderBody);
@@ -2259,9 +2261,13 @@ ${varShared(renderer)})`);
2259
2261
  break;
2260
2262
  case 10 /* AttrNamed */: {
2261
2263
  const isDefault = isEmptyRange(attr.name);
2262
- const name = isDefault ? ATTR_UNAMED2 : attr.name;
2263
2264
  const value = attr.value;
2265
+ const modifierIndex = !isDefault && (!value || value.type === 13 /* AttrValue */) && this.#getNamedAttrModifierIndex(attr);
2264
2266
  const defaultMapPosition = isDefault ? attr.name : void 0;
2267
+ let name = isDefault ? ATTR_UNAMED2 : attr.name;
2268
+ if (modifierIndex !== false) {
2269
+ name = { start: attr.name.start, end: modifierIndex };
2270
+ }
2265
2271
  if (value) {
2266
2272
  switch (value.type) {
2267
2273
  case 14 /* AttrMethod */:
@@ -2342,7 +2348,7 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2342
2348
  `).copy(attr.args.value).write("\n)");
2343
2349
  }
2344
2350
  } else {
2345
- this.#extractor.write('"').copy(defaultMapPosition).copy(name).write('": true');
2351
+ this.#extractor.write('"').copy(defaultMapPosition).copy(name).write(`": ${modifierIndex === false ? "true" : '""'}`);
2346
2352
  }
2347
2353
  break;
2348
2354
  }
@@ -2432,13 +2438,9 @@ const attrTags = ${varShared(
2432
2438
  const alternates = IF_TAG_ALTERNATES.get(tag);
2433
2439
  this.#writeComments(tag);
2434
2440
  this.#extractor.write("((\n").copy(
2435
- ((_a = tag.args) == null ? void 0 : _a.value) || this.#getAttrValue(tag, ATTR_UNAMED2) || "undefined"
2441
+ this.#getRangeWithoutTrailingComma((_a = tag.args) == null ? void 0 : _a.value) || this.#getAttrValue(tag, ATTR_UNAMED2) || "undefined"
2436
2442
  ).write("\n) ? ");
2437
- this.#extractor.write("{\n");
2438
- const body = this.#processBody(tag);
2439
- if (body)
2440
- this.#writeAttrTags(body, true);
2441
- this.#extractor.write("\n} ");
2443
+ this.#writeDynamicAttrTagBody(tag);
2442
2444
  let needsAlternate = true;
2443
2445
  if (alternates) {
2444
2446
  for (const { node, condition } of alternates) {
@@ -2451,14 +2453,7 @@ const attrTags = ${varShared(
2451
2453
  } else {
2452
2454
  this.#extractor.write(": undefined ? ");
2453
2455
  }
2454
- const body2 = this.#processBody(tag);
2455
- if (body2) {
2456
- this.#extractor.write("{\n");
2457
- this.#writeAttrTags(body2, true);
2458
- this.#extractor.write("}");
2459
- } else {
2460
- this.#extractor.write("{}");
2461
- }
2456
+ this.#writeDynamicAttrTagBody(node);
2462
2457
  }
2463
2458
  }
2464
2459
  if (needsAlternate) {
@@ -2474,20 +2469,19 @@ const attrTags = ${varShared(
2474
2469
  this.#writeTagNameComment(tag);
2475
2470
  this.#extractor.write("}, \n");
2476
2471
  this.#writeComments(tag);
2477
- this.#extractor.copy(tag.typeParams).write("(\n").copy((_b = tag.params) == null ? void 0 : _b.value).write("\n) => ({\n");
2478
- const body = this.#processBody(tag);
2479
- if (body)
2480
- this.#writeAttrTags(body, true);
2481
- this.#extractor.write("}))");
2472
+ this.#extractor.copy(tag.typeParams).write("(\n").copy((_b = tag.params) == null ? void 0 : _b.value).write("\n) => (");
2473
+ this.#writeDynamicAttrTagBody(tag);
2474
+ this.#extractor.write("))");
2482
2475
  break;
2483
2476
  }
2484
2477
  case "while": {
2485
2478
  this.#writeComments(tag);
2486
2479
  this.#extractor.write(`${varShared("mergeAttrTags")}((
2487
- `).copy(((_c = tag.args) == null ? void 0 : _c.value) || "undefined").write("\n) ? [{\n");
2488
- const body = this.#processBody(tag);
2489
- this.#writeAttrTags(body, true);
2490
- this.#extractor.write("}] : [])");
2480
+ `).copy(
2481
+ this.#getRangeWithoutTrailingComma((_c = tag.args) == null ? void 0 : _c.value) || "undefined"
2482
+ ).write("\n) ? [");
2483
+ this.#writeDynamicAttrTagBody(tag);
2484
+ this.#extractor.write("] : [])");
2491
2485
  break;
2492
2486
  }
2493
2487
  }
@@ -2673,7 +2667,9 @@ const attrTags = ${varShared(
2673
2667
  switch (nextChild.nameText) {
2674
2668
  case "else-if": {
2675
2669
  const alternate = {
2676
- condition: ((_a = nextChild.args) == null ? void 0 : _a.value) || this.#getAttrValue(nextChild, ATTR_UNAMED2),
2670
+ condition: this.#getRangeWithoutTrailingComma(
2671
+ (_a = nextChild.args) == null ? void 0 : _a.value
2672
+ ) || this.#getAttrValue(nextChild, ATTR_UNAMED2),
2677
2673
  node: nextChild
2678
2674
  };
2679
2675
  hasDynamicAttrTags ||= nextChild.hasAttrTags;
@@ -2746,6 +2742,24 @@ const attrTags = ${varShared(
2746
2742
  return { renderBody, staticAttrTags, dynamicAttrTagParents };
2747
2743
  }
2748
2744
  }
2745
+ #writeDynamicAttrTagBody(tag) {
2746
+ const body = this.#processBody(tag);
2747
+ if (body) {
2748
+ if (body.renderBody) {
2749
+ this.#extractor.write("(() => {\n");
2750
+ this.#writeChildren(tag, body.renderBody);
2751
+ this.#extractor.write("return ");
2752
+ }
2753
+ this.#extractor.write("{\n");
2754
+ this.#writeAttrTags(body, true);
2755
+ this.#extractor.write("}");
2756
+ if (body.renderBody) {
2757
+ this.#extractor.write(";\n})()");
2758
+ }
2759
+ } else {
2760
+ this.#extractor.write("{}");
2761
+ }
2762
+ }
2749
2763
  #getAttrValue(tag, name) {
2750
2764
  if (tag.attrs) {
2751
2765
  for (const attr of tag.attrs) {
@@ -2755,6 +2769,22 @@ const attrTags = ${varShared(
2755
2769
  }
2756
2770
  }
2757
2771
  }
2772
+ #getRangeWithoutTrailingComma(range) {
2773
+ if (!range)
2774
+ return void 0;
2775
+ const { start } = range;
2776
+ let end = range.end - 1;
2777
+ while (end >= start) {
2778
+ if (isWhitespaceCode(this.#code.charCodeAt(end))) {
2779
+ end--;
2780
+ } else if (this.#code.charAt(end) === ",") {
2781
+ return { start, end };
2782
+ } else {
2783
+ break;
2784
+ }
2785
+ }
2786
+ return range;
2787
+ }
2758
2788
  #isEmptyText(text) {
2759
2789
  let pos = text.start;
2760
2790
  while (pos < text.end) {
@@ -2828,6 +2858,15 @@ const attrTags = ${varShared(
2828
2858
  }
2829
2859
  return renderId;
2830
2860
  }
2861
+ #getNamedAttrModifierIndex(attr) {
2862
+ const start = attr.name.start + 1;
2863
+ const end = attr.name.end - 1;
2864
+ for (let i = end; i-- > start; ) {
2865
+ if (this.#code.charAt(i) === ":")
2866
+ return i;
2867
+ }
2868
+ return false;
2869
+ }
2831
2870
  #testAtIndex(reg, index) {
2832
2871
  reg.lastIndex = index;
2833
2872
  return reg.test(this.#code);
package/dist/index.mjs CHANGED
@@ -1969,7 +1969,7 @@ constructor(_?: Return) {}
1969
1969
  }
1970
1970
  this.#writeComments(child);
1971
1971
  this.#extractor.write("if (").copy(
1972
- ((_a = child.args) == null ? void 0 : _a.value) || this.#getAttrValue(child, ATTR_UNAMED2) || "undefined"
1972
+ this.#getRangeWithoutTrailingComma((_a = child.args) == null ? void 0 : _a.value) || this.#getAttrValue(child, ATTR_UNAMED2) || "undefined"
1973
1973
  ).write(") {\n");
1974
1974
  if (child.body) {
1975
1975
  const localBindings = getHoistSources(child);
@@ -2050,7 +2050,9 @@ constructor(_?: Return) {}
2050
2050
  }
2051
2051
  case "while": {
2052
2052
  this.#writeComments(child);
2053
- this.#extractor.write("while (\n").copy(((_b = child.args) == null ? void 0 : _b.value) || "undefined").write("\n) {\n");
2053
+ this.#extractor.write("while (\n").copy(
2054
+ this.#getRangeWithoutTrailingComma((_b = child.args) == null ? void 0 : _b.value) || "undefined"
2055
+ ).write("\n) {\n");
2054
2056
  const body = this.#processBody(child);
2055
2057
  if (body == null ? void 0 : body.renderBody) {
2056
2058
  this.#writeChildren(child, body.renderBody);
@@ -2228,9 +2230,13 @@ ${varShared(renderer)})`);
2228
2230
  break;
2229
2231
  case 10 /* AttrNamed */: {
2230
2232
  const isDefault = isEmptyRange(attr.name);
2231
- const name = isDefault ? ATTR_UNAMED2 : attr.name;
2232
2233
  const value = attr.value;
2234
+ const modifierIndex = !isDefault && (!value || value.type === 13 /* AttrValue */) && this.#getNamedAttrModifierIndex(attr);
2233
2235
  const defaultMapPosition = isDefault ? attr.name : void 0;
2236
+ let name = isDefault ? ATTR_UNAMED2 : attr.name;
2237
+ if (modifierIndex !== false) {
2238
+ name = { start: attr.name.start, end: modifierIndex };
2239
+ }
2234
2240
  if (value) {
2235
2241
  switch (value.type) {
2236
2242
  case 14 /* AttrMethod */:
@@ -2311,7 +2317,7 @@ ${isMutatedVar(tag.parent, valueLiteral) ? `${varLocal("return")}.mutate.` : ""}
2311
2317
  `).copy(attr.args.value).write("\n)");
2312
2318
  }
2313
2319
  } else {
2314
- this.#extractor.write('"').copy(defaultMapPosition).copy(name).write('": true');
2320
+ this.#extractor.write('"').copy(defaultMapPosition).copy(name).write(`": ${modifierIndex === false ? "true" : '""'}`);
2315
2321
  }
2316
2322
  break;
2317
2323
  }
@@ -2401,13 +2407,9 @@ const attrTags = ${varShared(
2401
2407
  const alternates = IF_TAG_ALTERNATES.get(tag);
2402
2408
  this.#writeComments(tag);
2403
2409
  this.#extractor.write("((\n").copy(
2404
- ((_a = tag.args) == null ? void 0 : _a.value) || this.#getAttrValue(tag, ATTR_UNAMED2) || "undefined"
2410
+ this.#getRangeWithoutTrailingComma((_a = tag.args) == null ? void 0 : _a.value) || this.#getAttrValue(tag, ATTR_UNAMED2) || "undefined"
2405
2411
  ).write("\n) ? ");
2406
- this.#extractor.write("{\n");
2407
- const body = this.#processBody(tag);
2408
- if (body)
2409
- this.#writeAttrTags(body, true);
2410
- this.#extractor.write("\n} ");
2412
+ this.#writeDynamicAttrTagBody(tag);
2411
2413
  let needsAlternate = true;
2412
2414
  if (alternates) {
2413
2415
  for (const { node, condition } of alternates) {
@@ -2420,14 +2422,7 @@ const attrTags = ${varShared(
2420
2422
  } else {
2421
2423
  this.#extractor.write(": undefined ? ");
2422
2424
  }
2423
- const body2 = this.#processBody(tag);
2424
- if (body2) {
2425
- this.#extractor.write("{\n");
2426
- this.#writeAttrTags(body2, true);
2427
- this.#extractor.write("}");
2428
- } else {
2429
- this.#extractor.write("{}");
2430
- }
2425
+ this.#writeDynamicAttrTagBody(node);
2431
2426
  }
2432
2427
  }
2433
2428
  if (needsAlternate) {
@@ -2443,20 +2438,19 @@ const attrTags = ${varShared(
2443
2438
  this.#writeTagNameComment(tag);
2444
2439
  this.#extractor.write("}, \n");
2445
2440
  this.#writeComments(tag);
2446
- this.#extractor.copy(tag.typeParams).write("(\n").copy((_b = tag.params) == null ? void 0 : _b.value).write("\n) => ({\n");
2447
- const body = this.#processBody(tag);
2448
- if (body)
2449
- this.#writeAttrTags(body, true);
2450
- this.#extractor.write("}))");
2441
+ this.#extractor.copy(tag.typeParams).write("(\n").copy((_b = tag.params) == null ? void 0 : _b.value).write("\n) => (");
2442
+ this.#writeDynamicAttrTagBody(tag);
2443
+ this.#extractor.write("))");
2451
2444
  break;
2452
2445
  }
2453
2446
  case "while": {
2454
2447
  this.#writeComments(tag);
2455
2448
  this.#extractor.write(`${varShared("mergeAttrTags")}((
2456
- `).copy(((_c = tag.args) == null ? void 0 : _c.value) || "undefined").write("\n) ? [{\n");
2457
- const body = this.#processBody(tag);
2458
- this.#writeAttrTags(body, true);
2459
- this.#extractor.write("}] : [])");
2449
+ `).copy(
2450
+ this.#getRangeWithoutTrailingComma((_c = tag.args) == null ? void 0 : _c.value) || "undefined"
2451
+ ).write("\n) ? [");
2452
+ this.#writeDynamicAttrTagBody(tag);
2453
+ this.#extractor.write("] : [])");
2460
2454
  break;
2461
2455
  }
2462
2456
  }
@@ -2642,7 +2636,9 @@ const attrTags = ${varShared(
2642
2636
  switch (nextChild.nameText) {
2643
2637
  case "else-if": {
2644
2638
  const alternate = {
2645
- condition: ((_a = nextChild.args) == null ? void 0 : _a.value) || this.#getAttrValue(nextChild, ATTR_UNAMED2),
2639
+ condition: this.#getRangeWithoutTrailingComma(
2640
+ (_a = nextChild.args) == null ? void 0 : _a.value
2641
+ ) || this.#getAttrValue(nextChild, ATTR_UNAMED2),
2646
2642
  node: nextChild
2647
2643
  };
2648
2644
  hasDynamicAttrTags ||= nextChild.hasAttrTags;
@@ -2715,6 +2711,24 @@ const attrTags = ${varShared(
2715
2711
  return { renderBody, staticAttrTags, dynamicAttrTagParents };
2716
2712
  }
2717
2713
  }
2714
+ #writeDynamicAttrTagBody(tag) {
2715
+ const body = this.#processBody(tag);
2716
+ if (body) {
2717
+ if (body.renderBody) {
2718
+ this.#extractor.write("(() => {\n");
2719
+ this.#writeChildren(tag, body.renderBody);
2720
+ this.#extractor.write("return ");
2721
+ }
2722
+ this.#extractor.write("{\n");
2723
+ this.#writeAttrTags(body, true);
2724
+ this.#extractor.write("}");
2725
+ if (body.renderBody) {
2726
+ this.#extractor.write(";\n})()");
2727
+ }
2728
+ } else {
2729
+ this.#extractor.write("{}");
2730
+ }
2731
+ }
2718
2732
  #getAttrValue(tag, name) {
2719
2733
  if (tag.attrs) {
2720
2734
  for (const attr of tag.attrs) {
@@ -2724,6 +2738,22 @@ const attrTags = ${varShared(
2724
2738
  }
2725
2739
  }
2726
2740
  }
2741
+ #getRangeWithoutTrailingComma(range) {
2742
+ if (!range)
2743
+ return void 0;
2744
+ const { start } = range;
2745
+ let end = range.end - 1;
2746
+ while (end >= start) {
2747
+ if (isWhitespaceCode(this.#code.charCodeAt(end))) {
2748
+ end--;
2749
+ } else if (this.#code.charAt(end) === ",") {
2750
+ return { start, end };
2751
+ } else {
2752
+ break;
2753
+ }
2754
+ }
2755
+ return range;
2756
+ }
2727
2757
  #isEmptyText(text) {
2728
2758
  let pos = text.start;
2729
2759
  while (pos < text.end) {
@@ -2797,6 +2827,15 @@ const attrTags = ${varShared(
2797
2827
  }
2798
2828
  return renderId;
2799
2829
  }
2830
+ #getNamedAttrModifierIndex(attr) {
2831
+ const start = attr.name.start + 1;
2832
+ const end = attr.name.end - 1;
2833
+ for (let i = end; i-- > start; ) {
2834
+ if (this.#code.charAt(i) === ":")
2835
+ return i;
2836
+ }
2837
+ return false;
2838
+ }
2800
2839
  #testAtIndex(reg, index) {
2801
2840
  reg.lastIndex = index;
2802
2841
  return reg.test(this.#code);
@@ -25,6 +25,8 @@ export declare function getCompiler(dir?: string): typeof defaultCompiler;
25
25
  export declare function getCache(dir?: string): Map<any, any>;
26
26
  export declare function getConfig(dir?: string): Omit<{
27
27
  output?: "source" | "html" | "dom" | "hydrate" | "migrate" | undefined;
28
+ errorRecovery?: boolean | undefined;
29
+ applyFixes?: Map<number, unknown> | undefined;
28
30
  stripTypes?: boolean | undefined;
29
31
  runtimeId?: string | null | undefined;
30
32
  ast?: boolean | undefined;
package/package.json CHANGED
@@ -1,25 +1,25 @@
1
1
  {
2
2
  "name": "@marko/language-tools",
3
3
  "description": "Marko Language Tools",
4
- "version": "2.0.5",
4
+ "version": "2.0.7",
5
5
  "bugs": "https://github.com/marko-js/language-server/issues/new?template=Bug_report.md",
6
6
  "dependencies": {
7
- "@babel/helper-validator-identifier": "^7.19.1",
8
- "@babel/parser": "^7.21.4",
9
- "@marko/compiler": "5.27.7",
10
- "@marko/translator-default": "5.25.7",
11
- "htmljs-parser": "^5.4.3",
7
+ "@babel/helper-validator-identifier": "^7.22.5",
8
+ "@babel/parser": "^7.22.5",
9
+ "@marko/compiler": "5.28.4",
10
+ "@marko/translator-default": "5.26.4",
11
+ "htmljs-parser": "^5.5.0",
12
12
  "relative-import-path": "^1.0.0"
13
13
  },
14
14
  "devDependencies": {
15
- "@babel/code-frame": "^7.21.4",
16
- "@marko/compiler": "^5.27.7",
15
+ "@babel/code-frame": "^7.22.5",
16
+ "@marko/compiler": "^5.28.3",
17
17
  "@types/babel__code-frame": "^7.0.3",
18
18
  "@types/babel__helper-validator-identifier": "^7.15.0",
19
19
  "@typescript/vfs": "^1.4.0",
20
- "marko": "^5.25.8",
20
+ "marko": "^5.26.4",
21
21
  "mitata": "^0.1.6",
22
- "tsx": "^3.12.6"
22
+ "tsx": "^3.12.7"
23
23
  },
24
24
  "exports": {
25
25
  ".": {