@marko/language-tools 1.0.0 → 1.0.2

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
@@ -1748,6 +1748,7 @@ var ScriptExtractor = class {
1748
1748
  let typeParamsStr = "";
1749
1749
  let typeArgsStr = "";
1750
1750
  let jsDocTemplateTagsStr = "";
1751
+ const hasComponent = componentClassBody || componentFileName;
1751
1752
  if (inputType) {
1752
1753
  if (inputType.typeParameters) {
1753
1754
  let sep = SEP_EMPTY;
@@ -1767,35 +1768,43 @@ var ScriptExtractor = class {
1767
1768
  }
1768
1769
  }
1769
1770
  } else {
1770
- const hasComponent = componentClassBody || componentFileName;
1771
1771
  if (this.#scriptLang === "ts" /* ts */) {
1772
1772
  this.#extractor.write(
1773
- hasComponent ? 'export type Input = Component["input"];\n' : `export interface Input {}
1773
+ hasComponent ? "export type Input = Component['input'];\n" : `export interface Input {}
1774
1774
  `
1775
1775
  );
1776
1776
  } else {
1777
1777
  this.#extractor.write(
1778
- `/** @typedef {${hasComponent ? 'Component["input"]' : "Record<string, unknown>"}} Input */
1778
+ `/** @typedef {${hasComponent ? "Component['input']" : "Record<string, unknown>"}} Input */
1779
1779
  `
1780
1780
  );
1781
1781
  }
1782
1782
  }
1783
1783
  if (!componentClassBody && componentFileName) {
1784
- this.#extractor.write(
1785
- `import Component from "${stripExt(
1786
- (0, import_relative_import_path.relativeImportPath)(this.#filename, componentFileName)
1787
- )}";
1784
+ if (this.#scriptLang === "ts" /* ts */) {
1785
+ this.#extractor.write(
1786
+ `import type Component from "${stripExt(
1787
+ (0, import_relative_import_path.relativeImportPath)(this.#filename, componentFileName)
1788
+ )}";
1788
1789
  `
1789
- );
1790
+ );
1791
+ } else {
1792
+ this.#extractor.write(
1793
+ `/** @typedef {import("${stripExt(
1794
+ (0, import_relative_import_path.relativeImportPath)(this.#filename, componentFileName)
1795
+ )}").default} Component */
1796
+ `
1797
+ );
1798
+ }
1790
1799
  } else {
1791
1800
  const body2 = componentClassBody || " {}";
1792
1801
  if (this.#scriptLang === "ts" /* ts */) {
1793
1802
  this.#extractor.write(
1794
- `abstract class Component${typeParamsStr} extends Marko.Component<Input${typeArgsStr}>`
1803
+ `abstract class Component${typeParamsStr} extends Marko.Component<${hasComponent && !inputType ? "{}" : `Input${typeArgsStr}`}>`
1795
1804
  ).copy(body2).write("\nexport { type Component }\n");
1796
1805
  } else {
1797
1806
  this.#extractor.write(`/**${jsDocTemplateTagsStr}
1798
- * @extends {Marko.Component<Input${typeArgsStr}>}
1807
+ * @extends {Marko.Component${hasComponent && !inputType ? "" : `<Input${typeArgsStr}>`}}
1799
1808
  * @abstract
1800
1809
  */
1801
1810
  `);
@@ -1808,24 +1817,26 @@ var ScriptExtractor = class {
1808
1817
  this.#extractor.write("(");
1809
1818
  }
1810
1819
  if (this.#scriptLang === "ts" /* ts */) {
1811
- this.#extractor.write(`function ${templateName}${typeParamsStr}(this: void) {
1812
- const input = 1 as any as Input${typeArgsStr};
1813
- const component = 1 as any as Component${typeArgsStr};
1814
- `);
1820
+ this.#extractor.write(
1821
+ `function ${templateName}${typeParamsStr}(this: void) {
1822
+ `
1823
+ );
1815
1824
  } else {
1816
1825
  this.#extractor.write(`/**${jsDocTemplateTagsStr}
1817
1826
  * @this {void}
1818
1827
  */
1819
1828
  function ${templateName}() {
1820
- const input = /** @type {Input${typeArgsStr}} */(${varShared("any")});
1821
- const component = /** @type {Component${typeArgsStr}} */(${varShared(
1822
- "any"
1823
- )});
1824
1829
  `);
1825
1830
  }
1826
- this.#extractor.write(` const out = ${varShared("out")};
1831
+ this.#extractor.write(` const input = ${this.#getCastedType(`Input${typeArgsStr}`)};
1832
+ const component = ${this.#getCastedType(`Component${typeArgsStr}`)};
1827
1833
  const state = ${varShared("state")}(component);
1828
- ${varShared("noop")}({ input, out, component, state });
1834
+ const $global = ${varShared("getGlobal")}(
1835
+ // @ts-expect-error We expect the compiler to error because we are checking if the MarkoRun.Context is defined.
1836
+ (${varShared("error")}, ${this.#getCastedType("MarkoRun.Context")})
1837
+ );
1838
+ const out = ${varShared("out")};
1839
+ ${varShared("noop")}({ input, component, state, out, $global });
1829
1840
  `);
1830
1841
  const body = this.#processBody(program);
1831
1842
  if (body == null ? void 0 : body.renderBody) {
@@ -2510,6 +2521,12 @@ const attrTags = ${varShared(
2510
2521
  } else {
2511
2522
  this.#extractor.write("}\n})()");
2512
2523
  }
2524
+ this.#extractor.write(SEP_COMMA_NEW_LINE);
2525
+ }
2526
+ if (tag.type === 16 /* AttrTag */) {
2527
+ this.#extractor.write(
2528
+ `[Symbol.iterator]: ${varShared("any")}${SEP_COMMA_NEW_LINE}`
2529
+ );
2513
2530
  }
2514
2531
  if (!hasInput) {
2515
2532
  this.#writeTagNameComment(tag);
@@ -2523,6 +2540,9 @@ const attrTags = ${varShared(
2523
2540
  }
2524
2541
  this.#extractor.write("}");
2525
2542
  }
2543
+ #getCastedType(type) {
2544
+ return this.#scriptLang === "ts" /* ts */ ? `${varShared("any")} as ${type}` : `/** @type {${type}} */(${varShared("any")})`;
2545
+ }
2526
2546
  #copyWithMutationsReplaced(range) {
2527
2547
  const mutations = this.#mutationOffsets;
2528
2548
  if (!mutations)
package/dist/index.mjs CHANGED
@@ -1715,6 +1715,7 @@ var ScriptExtractor = class {
1715
1715
  let typeParamsStr = "";
1716
1716
  let typeArgsStr = "";
1717
1717
  let jsDocTemplateTagsStr = "";
1718
+ const hasComponent = componentClassBody || componentFileName;
1718
1719
  if (inputType) {
1719
1720
  if (inputType.typeParameters) {
1720
1721
  let sep = SEP_EMPTY;
@@ -1734,35 +1735,43 @@ var ScriptExtractor = class {
1734
1735
  }
1735
1736
  }
1736
1737
  } else {
1737
- const hasComponent = componentClassBody || componentFileName;
1738
1738
  if (this.#scriptLang === "ts" /* ts */) {
1739
1739
  this.#extractor.write(
1740
- hasComponent ? 'export type Input = Component["input"];\n' : `export interface Input {}
1740
+ hasComponent ? "export type Input = Component['input'];\n" : `export interface Input {}
1741
1741
  `
1742
1742
  );
1743
1743
  } else {
1744
1744
  this.#extractor.write(
1745
- `/** @typedef {${hasComponent ? 'Component["input"]' : "Record<string, unknown>"}} Input */
1745
+ `/** @typedef {${hasComponent ? "Component['input']" : "Record<string, unknown>"}} Input */
1746
1746
  `
1747
1747
  );
1748
1748
  }
1749
1749
  }
1750
1750
  if (!componentClassBody && componentFileName) {
1751
- this.#extractor.write(
1752
- `import Component from "${stripExt(
1753
- relativeImportPath(this.#filename, componentFileName)
1754
- )}";
1751
+ if (this.#scriptLang === "ts" /* ts */) {
1752
+ this.#extractor.write(
1753
+ `import type Component from "${stripExt(
1754
+ relativeImportPath(this.#filename, componentFileName)
1755
+ )}";
1755
1756
  `
1756
- );
1757
+ );
1758
+ } else {
1759
+ this.#extractor.write(
1760
+ `/** @typedef {import("${stripExt(
1761
+ relativeImportPath(this.#filename, componentFileName)
1762
+ )}").default} Component */
1763
+ `
1764
+ );
1765
+ }
1757
1766
  } else {
1758
1767
  const body2 = componentClassBody || " {}";
1759
1768
  if (this.#scriptLang === "ts" /* ts */) {
1760
1769
  this.#extractor.write(
1761
- `abstract class Component${typeParamsStr} extends Marko.Component<Input${typeArgsStr}>`
1770
+ `abstract class Component${typeParamsStr} extends Marko.Component<${hasComponent && !inputType ? "{}" : `Input${typeArgsStr}`}>`
1762
1771
  ).copy(body2).write("\nexport { type Component }\n");
1763
1772
  } else {
1764
1773
  this.#extractor.write(`/**${jsDocTemplateTagsStr}
1765
- * @extends {Marko.Component<Input${typeArgsStr}>}
1774
+ * @extends {Marko.Component${hasComponent && !inputType ? "" : `<Input${typeArgsStr}>`}}
1766
1775
  * @abstract
1767
1776
  */
1768
1777
  `);
@@ -1775,24 +1784,26 @@ var ScriptExtractor = class {
1775
1784
  this.#extractor.write("(");
1776
1785
  }
1777
1786
  if (this.#scriptLang === "ts" /* ts */) {
1778
- this.#extractor.write(`function ${templateName}${typeParamsStr}(this: void) {
1779
- const input = 1 as any as Input${typeArgsStr};
1780
- const component = 1 as any as Component${typeArgsStr};
1781
- `);
1787
+ this.#extractor.write(
1788
+ `function ${templateName}${typeParamsStr}(this: void) {
1789
+ `
1790
+ );
1782
1791
  } else {
1783
1792
  this.#extractor.write(`/**${jsDocTemplateTagsStr}
1784
1793
  * @this {void}
1785
1794
  */
1786
1795
  function ${templateName}() {
1787
- const input = /** @type {Input${typeArgsStr}} */(${varShared("any")});
1788
- const component = /** @type {Component${typeArgsStr}} */(${varShared(
1789
- "any"
1790
- )});
1791
1796
  `);
1792
1797
  }
1793
- this.#extractor.write(` const out = ${varShared("out")};
1798
+ this.#extractor.write(` const input = ${this.#getCastedType(`Input${typeArgsStr}`)};
1799
+ const component = ${this.#getCastedType(`Component${typeArgsStr}`)};
1794
1800
  const state = ${varShared("state")}(component);
1795
- ${varShared("noop")}({ input, out, component, state });
1801
+ const $global = ${varShared("getGlobal")}(
1802
+ // @ts-expect-error We expect the compiler to error because we are checking if the MarkoRun.Context is defined.
1803
+ (${varShared("error")}, ${this.#getCastedType("MarkoRun.Context")})
1804
+ );
1805
+ const out = ${varShared("out")};
1806
+ ${varShared("noop")}({ input, component, state, out, $global });
1796
1807
  `);
1797
1808
  const body = this.#processBody(program);
1798
1809
  if (body == null ? void 0 : body.renderBody) {
@@ -2477,6 +2488,12 @@ const attrTags = ${varShared(
2477
2488
  } else {
2478
2489
  this.#extractor.write("}\n})()");
2479
2490
  }
2491
+ this.#extractor.write(SEP_COMMA_NEW_LINE);
2492
+ }
2493
+ if (tag.type === 16 /* AttrTag */) {
2494
+ this.#extractor.write(
2495
+ `[Symbol.iterator]: ${varShared("any")}${SEP_COMMA_NEW_LINE}`
2496
+ );
2480
2497
  }
2481
2498
  if (!hasInput) {
2482
2499
  this.#writeTagNameComment(tag);
@@ -2490,6 +2507,9 @@ const attrTags = ${varShared(
2490
2507
  }
2491
2508
  this.#extractor.write("}");
2492
2509
  }
2510
+ #getCastedType(type) {
2511
+ return this.#scriptLang === "ts" /* ts */ ? `${varShared("any")} as ${type}` : `/** @type {${type}} */(${varShared("any")})`;
2512
+ }
2493
2513
  #copyWithMutationsReplaced(range) {
2494
2514
  const mutations = this.#mutationOffsets;
2495
2515
  if (!mutations)
@@ -11,17 +11,17 @@ export declare class Extractor {
11
11
  #private;
12
12
  constructor(parsed: Parsed);
13
13
  write(str: string): this;
14
- copy(range: Range | string | false | void | null): this;
14
+ copy(range: Range | string | false | void | undefined | null): this;
15
15
  end(): Extracted;
16
16
  }
17
17
  export declare class Extracted {
18
18
  #private;
19
19
  constructor(parsed: Parsed, generated: string, tokens: Token[]);
20
- sourceOffsetAt(generatedOffset: number): number | void;
21
- sourcePositionAt(generatedOffset: number): Position | void;
20
+ sourceOffsetAt(generatedOffset: number): number | undefined;
21
+ sourcePositionAt(generatedOffset: number): Position | undefined;
22
22
  sourceLocationAt(generatedStart: number, generatedEnd: number): Location | undefined;
23
- generatedOffsetAt(sourceOffset: number): number | void;
24
- generatedPositionAt(sourceOffset: number): Position | void;
23
+ generatedOffsetAt(sourceOffset: number): number | undefined;
24
+ generatedPositionAt(sourceOffset: number): Position | undefined;
25
25
  generatedLocationAt(sourceStart: number, sourceEnd: number): Location | undefined;
26
26
  toString(): string;
27
27
  }
@@ -19,6 +19,10 @@ declare global {
19
19
  export const never: never;
20
20
  export const any: any;
21
21
 
22
+ export function getGlobal<Override>(
23
+ override: Override
24
+ ): 1 extends Override & 0 ? Marko.Global : Override;
25
+
22
26
  export function attrTagNames<Input, Keys extends keyof Input>(
23
27
  input: Input
24
28
  ): Record<string, never> & {
@@ -389,22 +393,20 @@ type ComponentEventHandlers<Component extends Marko.Component> = {
389
393
  >]: Component[K] extends (...args: any) => any ? Component[K] : never;
390
394
  };
391
395
 
392
- type FlatScopes<
393
- Input extends object,
394
- Objects = Input
395
- > = Input[keyof Input] extends infer Prop
396
- ? Prop extends (...args: any) => { [Marko._.scope]: infer Scope }
396
+ type FlatScopes<Input extends object, Objects = Input> = Input[keyof Input &
397
+ (string | number)] extends infer Prop
398
+ ? 0 extends 1 & Prop
399
+ ? unknown
400
+ : Prop extends (...args: any) => { [Marko._.scope]: infer Scope }
397
401
  ? unknown extends Scope
398
402
  ? never
399
403
  : Scope
400
- : 0 extends 1 & Prop
401
- ? never
402
404
  : Prop extends object
403
405
  ? Prop extends Extract<Objects, Prop>
404
406
  ? never
405
407
  : FlatScopes<Prop, Objects | Prop>
406
408
  : never
407
- : never;
409
+ : unknown;
408
410
 
409
411
  type MergeScopes<Scopes> = {
410
412
  [K in Scopes extends Scopes ? keyof Scopes : never]: Scopes extends Scopes
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
1
  {
2
2
  "name": "@marko/language-tools",
3
3
  "description": "Marko Language Tools",
4
- "version": "1.0.0",
4
+ "version": "1.0.2",
5
5
  "bugs": "https://github.com/marko-js/language-server/issues/new?template=Bug_report.md",
6
6
  "dependencies": {
7
7
  "@babel/helper-validator-identifier": "^7.19.1",
8
- "@babel/parser": "^7.21.2",
9
- "htmljs-parser": "^5.2.4",
8
+ "@babel/parser": "^7.21.3",
9
+ "htmljs-parser": "^5.4.3",
10
10
  "relative-import-path": "^1.0.0"
11
11
  },
12
12
  "devDependencies": {
13
13
  "@babel/code-frame": "^7.18.6",
14
- "@marko/compiler": "^5.23.6",
14
+ "@marko/compiler": "^5.27.1",
15
15
  "@types/babel__code-frame": "^7.0.3",
16
16
  "@types/babel__helper-validator-identifier": "^7.15.0",
17
17
  "@typescript/vfs": "^1.4.0",
18
- "marko": "^5.22.7",
18
+ "marko": "^5.25.1",
19
19
  "mitata": "^0.1.6",
20
- "tsx": "^3.12.3"
20
+ "tsx": "^3.12.5"
21
21
  },
22
22
  "exports": {
23
23
  ".": {