@alloy-js/typescript 0.22.0-dev.1 → 0.22.0-dev.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alloy-js/typescript",
3
- "version": "0.22.0-dev.1",
3
+ "version": "0.22.0-dev.3",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
@@ -22,7 +22,7 @@
22
22
  "author": "brian.terlson@microsoft.com",
23
23
  "license": "MIT",
24
24
  "dependencies": {
25
- "@alloy-js/core": "~0.21.0 || >= 0.22.0-dev.1",
25
+ "@alloy-js/core": "~0.21.0 || >= 0.22.0-dev.3",
26
26
  "change-case": "^5.4.4",
27
27
  "pathe": "^2.0.3"
28
28
  },
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  Block,
3
3
  Children,
4
+ For,
4
5
  MemberDeclaration,
5
6
  Name,
6
7
  Namekey,
@@ -28,6 +29,7 @@ import { TypeRefContext } from "./TypeRefContext.jsx";
28
29
 
29
30
  export interface ClassDeclarationProps extends CommonDeclarationProps {
30
31
  extends?: Children;
32
+ implements?: Children[];
31
33
  }
32
34
 
33
35
  /**
@@ -65,6 +67,15 @@ export interface ClassDeclarationProps extends CommonDeclarationProps {
65
67
  */
66
68
  export function ClassDeclaration(props: ClassDeclarationProps) {
67
69
  const extendsPart = props.extends && <> extends {props.extends}</>;
70
+ const implementsPart = props.implements && props.implements.length > 0 && (
71
+ <>
72
+ {" "}
73
+ implements{" "}
74
+ <For each={props.implements} comma space>
75
+ {(implement) => implement}
76
+ </For>
77
+ </>
78
+ );
68
79
  const sym = createTypeAndValueSymbol(props.name, {
69
80
  refkeys: props.refkey,
70
81
  export: props.export,
@@ -83,7 +94,8 @@ export function ClassDeclaration(props: ClassDeclarationProps) {
83
94
  <Declaration symbol={sym} export={props.export} default={props.default}>
84
95
  <MemberScope ownerSymbol={sym}>
85
96
  class <Name />
86
- {extendsPart} <Block>{props.children}</Block>
97
+ {extendsPart}
98
+ {implementsPart} <Block>{props.children}</Block>
87
99
  </MemberScope>
88
100
  </Declaration>
89
101
  </>
@@ -35,6 +35,10 @@ interface PartDescriptorBase {
35
35
  nullish: boolean;
36
36
  type: boolean;
37
37
  args?: Children[];
38
+ /**
39
+ * Whether to await the value that results from this part of the member expression.
40
+ */
41
+ await?: boolean;
38
42
  }
39
43
 
40
44
  type PartDescriptor = PartDescriptorWithId | PartDescriptorWithIndex;
@@ -47,7 +51,8 @@ type PartDescriptor = PartDescriptorWithId | PartDescriptorWithIndex;
47
51
  * * **refkey**: a refkey for a symbol whose name becomes the identifier
48
52
  * * **symbol**: a symbol whose name becomes the identifier part
49
53
  * * **args**: create a method call with the given args
50
- * * **children**: arbitrary contents for the identifier part.
54
+ * * **children**: arbitrary contents for the identifier part
55
+ * * **await**: whether to await the value that results from this part of the member expression
51
56
  *
52
57
  * Each part can have a nullish prop, which indicates that the part may be null
53
58
  * or undefined.
@@ -88,6 +93,10 @@ export function MemberExpression(props: MemberExpressionProps): Children {
88
93
  }
89
94
 
90
95
  const isCallChain = computed(() => {
96
+ for (const part of parts) {
97
+ if (part.await) return false;
98
+ }
99
+
91
100
  let callCount = 0;
92
101
  for (const part of parts) {
93
102
  if (part.args !== undefined) callCount++;
@@ -247,6 +256,7 @@ function createPartDescriptorFromProps(
247
256
  (symbolSource.value as TSOutputSymbol).isTypeSymbol
248
257
  );
249
258
  }),
259
+ await: computed(() => partProps.await),
250
260
  };
251
261
 
252
262
  return reactive(part);
@@ -389,27 +399,47 @@ function formatCallChain(parts: PartDescriptor[]): Children {
389
399
 
390
400
  function formatNonCallChain(parts: PartDescriptor[]): Children {
391
401
  return computed(() => {
392
- const expression: Children[] = [];
402
+ let expression: Children = [];
393
403
 
394
404
  for (let i = 0; i < parts.length; i++) {
395
405
  const part = parts[i];
396
406
  const base = isIdPartDescriptor(part) ? part.id : part.index;
397
407
  if (i === 0) {
398
- expression.push(base);
408
+ (expression as Children[]).push(base);
399
409
  } else {
400
410
  // Determine if we should use nullish operator from previous part
401
411
  const prevPart = parts[i - 1];
412
+ let partExpr;
402
413
 
403
414
  if (prevPart.type) {
404
- expression.push(formatArrayAccess(prevPart, part));
415
+ partExpr = formatArrayAccess(prevPart, part);
405
416
  } else if (part.args !== undefined) {
406
417
  // For parts with only args (no name), append function call directly with appropriate nullish operator
407
- expression.push(formatCallExpr(prevPart, part));
418
+ partExpr = formatCallExpr(prevPart, part);
408
419
  } else if (part.accessStyle === "dot") {
409
- expression.push(formatDotAccess(prevPart, part));
420
+ partExpr = formatDotAccess(prevPart, part);
410
421
  } else {
411
422
  // bracket notation - don't include the dot
412
- expression.push(formatArrayAccess(prevPart, part));
423
+ partExpr = formatArrayAccess(prevPart, part);
424
+ }
425
+
426
+ if (Array.isArray(expression)) {
427
+ expression.push(partExpr);
428
+ } else {
429
+ expression = (
430
+ <>
431
+ {expression}
432
+ {partExpr}
433
+ </>
434
+ );
435
+ }
436
+ }
437
+
438
+ if (part.await) {
439
+ if (i < parts.length - 1) {
440
+ expression = <>(await {expression})</>;
441
+ } else {
442
+ expression = <>await {expression}</>;
413
443
  }
414
444
  }
415
445
  }
@@ -524,6 +554,11 @@ export interface MemberExpressionPartProps {
524
554
  */
525
555
  args?: Children[] | boolean;
526
556
 
557
+ /**
558
+ * Whether to await the value that results from this part of the member expression.
559
+ */
560
+ await?: boolean;
561
+
527
562
  /**
528
563
  * This part is nullish. Subsequent parts use conditional access operators.
529
564
  */
@@ -543,7 +578,8 @@ export interface MemberExpressionPartProps {
543
578
  * * **refkey**: A refkey for a symbol whose name becomes the identifier
544
579
  * * **symbol**: a symbol whose name becomes the identifier part
545
580
  * * **args**: create a method call with the given args
546
- * * **children**: arbitrary contents for the identifier part.
581
+ * * **children**: arbitrary contents for the identifier part
582
+ * * **await**: whether to await the value that results from this part of the member expression
547
583
  *
548
584
  * Each part can have a nullish prop, which indicates that the part may be null
549
585
  * or undefined.
package/temp/api.json CHANGED
@@ -1695,6 +1695,38 @@
1695
1695
  "startIndex": 1,
1696
1696
  "endIndex": 2
1697
1697
  }
1698
+ },
1699
+ {
1700
+ "kind": "PropertySignature",
1701
+ "canonicalReference": "@alloy-js/typescript!ClassDeclarationProps#implements:member",
1702
+ "docComment": "",
1703
+ "excerptTokens": [
1704
+ {
1705
+ "kind": "Content",
1706
+ "text": "implements?: "
1707
+ },
1708
+ {
1709
+ "kind": "Reference",
1710
+ "text": "Children",
1711
+ "canonicalReference": "@alloy-js/core!Children:type"
1712
+ },
1713
+ {
1714
+ "kind": "Content",
1715
+ "text": "[]"
1716
+ },
1717
+ {
1718
+ "kind": "Content",
1719
+ "text": ";"
1720
+ }
1721
+ ],
1722
+ "isReadonly": false,
1723
+ "isOptional": true,
1724
+ "releaseTag": "Public",
1725
+ "name": "implements",
1726
+ "propertyTypeTokenRange": {
1727
+ "startIndex": 1,
1728
+ "endIndex": 3
1729
+ }
1698
1730
  }
1699
1731
  ],
1700
1732
  "extendsTokenRanges": [
@@ -8660,7 +8692,7 @@
8660
8692
  {
8661
8693
  "kind": "Function",
8662
8694
  "canonicalReference": "@alloy-js/typescript!MemberExpression:function(1)",
8663
- "docComment": "/**\n * Create a member expression from parts. Each part can provide one of\n * the following:\n *\n * * **id**: The identifier for the member expression part\n * * **refkey**: a refkey for a symbol whose name becomes the identifier\n * * **symbol**: a symbol whose name becomes the identifier part\n * * **args**: create a method call with the given args\n * * **children**: arbitrary contents for the identifier part.\n *\n * Each part can have a nullish prop, which indicates that the part may be null\n * or undefined.\n *\n * Each part can also have a quoteId prop, which indicates that the identifier\n * of the part should be quoted (i.e. `[\"foo\"]` instead of `.foo`). This is only\n * necessary when providing the children prop, otherwise it is determined\n * automatically.\n *\n * @example\n *\n *\n *\n * ```tsx\n * <MemberExpression>\n * <MemberExpression.Part id=\"base\" />\n * <MemberExpression.Part refkey={rk} nullish />\n * <MemberExpression.Part symbol={sym} />\n * <MemberExpression.Part args={[\"hello\", \"world\"]} />\n * <MemberExpression.Part>SomeValue</MemberExpression.Part>\n * </MemberExpression>\n * ```\n *\n *\n * Assuming `rk` is a refkey to a symbol name \"prop1\", and `sym` is a symbol\n * with a name of \"prop2\", this will render:\n *\n * ```ts\n * base.prop1?.prop2(\"hello\", \"world\").SomeValue\n * ```\n *\n */\n",
8695
+ "docComment": "/**\n * Create a member expression from parts. Each part can provide one of\n * the following:\n *\n * * **id**: The identifier for the member expression part\n * * **refkey**: a refkey for a symbol whose name becomes the identifier\n * * **symbol**: a symbol whose name becomes the identifier part\n * * **args**: create a method call with the given args\n * * **children**: arbitrary contents for the identifier part\n * * **await**: whether to await the value that results from this part of the member expression\n *\n * Each part can have a nullish prop, which indicates that the part may be null\n * or undefined.\n *\n * Each part can also have a quoteId prop, which indicates that the identifier\n * of the part should be quoted (i.e. `[\"foo\"]` instead of `.foo`). This is only\n * necessary when providing the children prop, otherwise it is determined\n * automatically.\n *\n * @example\n *\n *\n *\n * ```tsx\n * <MemberExpression>\n * <MemberExpression.Part id=\"base\" />\n * <MemberExpression.Part refkey={rk} nullish />\n * <MemberExpression.Part symbol={sym} />\n * <MemberExpression.Part args={[\"hello\", \"world\"]} />\n * <MemberExpression.Part>SomeValue</MemberExpression.Part>\n * </MemberExpression>\n * ```\n *\n *\n * Assuming `rk` is a refkey to a symbol name \"prop1\", and `sym` is a symbol\n * with a name of \"prop2\", this will render:\n *\n * ```ts\n * base.prop1?.prop2(\"hello\", \"world\").SomeValue\n * ```\n *\n */\n",
8664
8696
  "excerptTokens": [
8665
8697
  {
8666
8698
  "kind": "Content",
@@ -8809,6 +8841,33 @@
8809
8841
  "endIndex": 3
8810
8842
  }
8811
8843
  },
8844
+ {
8845
+ "kind": "PropertySignature",
8846
+ "canonicalReference": "@alloy-js/typescript!MemberExpressionPartProps#await:member",
8847
+ "docComment": "/**\n * Whether to await the value that results from this part of the member expression.\n */\n",
8848
+ "excerptTokens": [
8849
+ {
8850
+ "kind": "Content",
8851
+ "text": "await?: "
8852
+ },
8853
+ {
8854
+ "kind": "Content",
8855
+ "text": "boolean"
8856
+ },
8857
+ {
8858
+ "kind": "Content",
8859
+ "text": ";"
8860
+ }
8861
+ ],
8862
+ "isReadonly": false,
8863
+ "isOptional": true,
8864
+ "releaseTag": "Public",
8865
+ "name": "await",
8866
+ "propertyTypeTokenRange": {
8867
+ "startIndex": 1,
8868
+ "endIndex": 2
8869
+ }
8870
+ },
8812
8871
  {
8813
8872
  "kind": "PropertySignature",
8814
8873
  "canonicalReference": "@alloy-js/typescript!MemberExpressionPartProps#children:member",
@@ -27,6 +27,43 @@ it("creates extends", () => {
27
27
  `);
28
28
  });
29
29
 
30
+ it("creates implements", () => {
31
+ const res = toSourceText(
32
+ <ts.ClassDeclaration name="Foo" implements={["SomeInterface"]} />,
33
+ );
34
+
35
+ expect(res).toEqual(d`
36
+ class Foo implements SomeInterface {}
37
+ `);
38
+ });
39
+
40
+ it("creates implements with multiple", () => {
41
+ const res = toSourceText(
42
+ <ts.ClassDeclaration
43
+ name="Foo"
44
+ implements={["SomeInterface", "OtherInterface"]}
45
+ />,
46
+ );
47
+
48
+ expect(res).toEqual(d`
49
+ class Foo implements SomeInterface, OtherInterface {}
50
+ `);
51
+ });
52
+
53
+ it("creates implements with extends", () => {
54
+ const res = toSourceText(
55
+ <ts.ClassDeclaration
56
+ name="Foo"
57
+ extends="string"
58
+ implements={["SomeInterface"]}
59
+ />,
60
+ );
61
+
62
+ expect(res).toEqual(d`
63
+ class Foo extends string implements SomeInterface {}
64
+ `);
65
+ });
66
+
30
67
  it("takes namekeys for all elements", () => {
31
68
  const res = toSourceText(
32
69
  <ts.ClassDeclaration name={namekey("foo")}>
@@ -760,3 +760,81 @@ describe("formatting", () => {
760
760
  });
761
761
  });
762
762
  });
763
+
764
+ describe("with await", () => {
765
+ it("renders member expression with await", () => {
766
+ expect(
767
+ toSourceText(
768
+ <MemberExpression>
769
+ <MemberExpression.Part id="foo" />
770
+ <MemberExpression.Part id="bar" />
771
+ <MemberExpression.Part args await />
772
+ <MemberExpression.Part id="baz" />
773
+ </MemberExpression>,
774
+ ),
775
+ ).toBe(d`
776
+ (await foo.bar()).baz
777
+ `);
778
+ });
779
+
780
+ it("renders member expression with await at the end", () => {
781
+ expect(
782
+ toSourceText(
783
+ <MemberExpression>
784
+ <MemberExpression.Part id="foo" />
785
+ <MemberExpression.Part id="bar" />
786
+ <MemberExpression.Part args await />
787
+ </MemberExpression>,
788
+ ),
789
+ ).toBe(d`
790
+ await foo.bar()
791
+ `);
792
+ });
793
+
794
+ it("renders member expression with await in the middle of property access", () => {
795
+ expect(
796
+ toSourceText(
797
+ <MemberExpression>
798
+ <MemberExpression.Part id="foo" />
799
+ <MemberExpression.Part id="bar" await />
800
+ <MemberExpression.Part id="baz" />
801
+ </MemberExpression>,
802
+ ),
803
+ ).toBe(d`
804
+ (await foo.bar).baz
805
+ `);
806
+ });
807
+
808
+ it("renders member expression with multiple awaits", () => {
809
+ expect(
810
+ toSourceText(
811
+ <MemberExpression>
812
+ <MemberExpression.Part id="foo" />
813
+ <MemberExpression.Part id="bar" await />
814
+ <MemberExpression.Part id="baz" await />
815
+ <MemberExpression.Part id="qux" />
816
+ </MemberExpression>,
817
+ ),
818
+ ).toBe(d`
819
+ (await (await foo.bar).baz).qux
820
+ `);
821
+ });
822
+
823
+ it("renders member expression with await and call chain (disables formatting)", () => {
824
+ expect(
825
+ toSourceText(
826
+ <MemberExpression>
827
+ <MemberExpression.Part id="foo" />
828
+ <MemberExpression.Part id="bar" />
829
+ <MemberExpression.Part args />
830
+ <MemberExpression.Part id="baz" />
831
+ <MemberExpression.Part args await />
832
+ <MemberExpression.Part id="qux" />
833
+ <MemberExpression.Part args />
834
+ </MemberExpression>,
835
+ ),
836
+ ).toBe(d`
837
+ (await foo.bar().baz()).qux()
838
+ `);
839
+ });
840
+ });