@alloy-js/typescript 0.23.0-dev.0 → 0.23.0-dev.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alloy-js/typescript",
3
- "version": "0.23.0-dev.0",
3
+ "version": "0.23.0-dev.2",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
@@ -1,4 +1,4 @@
1
- import { Block, type Children } from "@alloy-js/core";
1
+ import { type Children } from "@alloy-js/core";
2
2
  import { BlockScope } from "./BlockScope.jsx";
3
3
 
4
4
  export interface IfStatementProps {
@@ -29,7 +29,7 @@ export function ElseIfClause(props: ElseIfClauseProps) {
29
29
  return (
30
30
  <>
31
31
  {" "}
32
- else if ({props.condition}) <Block>{props.children}</Block>
32
+ else if ({props.condition}) <BlockScope>{props.children}</BlockScope>
33
33
  </>
34
34
  );
35
35
  }
@@ -45,7 +45,7 @@ export function ElseClause(props: ElseClauseProps) {
45
45
  return (
46
46
  <>
47
47
  {" "}
48
- else <Block>{props.children}</Block>
48
+ else <BlockScope>{props.children}</BlockScope>
49
49
  </>
50
50
  );
51
51
  }
@@ -0,0 +1,123 @@
1
+ import { refkey, StatementList } from "@alloy-js/core";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { expect, it } from "vitest";
4
+ import { toSourceText } from "../../test/utils.jsx";
5
+ import { CatchClause, FinallyClause, TryStatement } from "./TryStatement.jsx";
6
+ import { VarDeclaration } from "./VarDeclaration.jsx";
7
+
8
+ it("works with try-catch", () => {
9
+ const text = toSourceText(
10
+ <>
11
+ <TryStatement>// try something</TryStatement>
12
+ <CatchClause parameter="error">// handle error</CatchClause>
13
+ </>,
14
+ );
15
+
16
+ expect(text).toBe(d`
17
+ try {
18
+ // try something
19
+ } catch (error) {
20
+ // handle error
21
+ }
22
+ `);
23
+ });
24
+
25
+ it("works with try-catch-finally", () => {
26
+ const text = toSourceText(
27
+ <>
28
+ <TryStatement>// try something</TryStatement>
29
+ <CatchClause parameter="error">// handle error</CatchClause>
30
+ <FinallyClause>// cleanup</FinallyClause>
31
+ </>,
32
+ );
33
+
34
+ expect(text).toBe(d`
35
+ try {
36
+ // try something
37
+ } catch (error) {
38
+ // handle error
39
+ } finally {
40
+ // cleanup
41
+ }
42
+ `);
43
+ });
44
+
45
+ it("works with try-finally", () => {
46
+ const text = toSourceText(
47
+ <>
48
+ <TryStatement>// try something</TryStatement>
49
+ <FinallyClause>// cleanup</FinallyClause>
50
+ </>,
51
+ );
52
+
53
+ expect(text).toBe(d`
54
+ try {
55
+ // try something
56
+ } finally {
57
+ // cleanup
58
+ }
59
+ `);
60
+ });
61
+
62
+ it("works with catch without parameter", () => {
63
+ const text = toSourceText(
64
+ <>
65
+ <TryStatement>// try something</TryStatement>
66
+ <CatchClause>// handle error without parameter</CatchClause>
67
+ </>,
68
+ );
69
+
70
+ expect(text).toBe(d`
71
+ try {
72
+ // try something
73
+ } catch {
74
+ // handle error without parameter
75
+ }
76
+ `);
77
+ });
78
+
79
+ it("works with typed catch parameter", () => {
80
+ const text = toSourceText(
81
+ <>
82
+ <TryStatement>// try something</TryStatement>
83
+ <CatchClause parameter={{ name: "error", type: "Error" }}>
84
+ // handle typed error
85
+ </CatchClause>
86
+ </>,
87
+ );
88
+
89
+ expect(text).toBe(d`
90
+ try {
91
+ // try something
92
+ } catch (error: Error) {
93
+ // handle typed error
94
+ }
95
+ `);
96
+ });
97
+
98
+ it("creates symbols for catch parameters", () => {
99
+ const rk = refkey();
100
+
101
+ const text = toSourceText(
102
+ <>
103
+ <TryStatement>// try something</TryStatement>
104
+ <CatchClause parameter={{ name: "error", type: "Error", refkey: rk }}>
105
+ <StatementList>
106
+ {rk}
107
+ <VarDeclaration name="message" type="string">
108
+ {rk}.message
109
+ </VarDeclaration>
110
+ </StatementList>
111
+ </CatchClause>
112
+ </>,
113
+ );
114
+
115
+ expect(text).toBe(d`
116
+ try {
117
+ // try something
118
+ } catch (error: Error) {
119
+ error;
120
+ const message: string = error.message;
121
+ }
122
+ `);
123
+ });
@@ -0,0 +1,141 @@
1
+ import { onCleanup, type Children } from "@alloy-js/core";
2
+ import { useTSNamePolicy } from "../name-policy.js";
3
+ import type { ParameterDescriptor } from "../parameter-descriptor.js";
4
+ import { createValueSymbol, TSSymbolFlags } from "../symbols/index.js";
5
+ import { BlockScope } from "./BlockScope.jsx";
6
+
7
+ export interface TryStatementProps {
8
+ children: Children;
9
+ }
10
+
11
+ /**
12
+ * A try statement.
13
+ *
14
+ * @example
15
+ * ```tsx
16
+ * <TryStatement>
17
+ * // risky operation
18
+ * </TryStatement>
19
+ * <CatchClause parameter="error">
20
+ * console.error(error);
21
+ * </CatchClause>
22
+ * ```
23
+ * renders to
24
+ * ```ts
25
+ * try {
26
+ * // risky operation
27
+ * } catch (error) {
28
+ * console.error(error);
29
+ * }
30
+ * ```
31
+ */
32
+ export function TryStatement(props: TryStatementProps) {
33
+ return (
34
+ <>
35
+ try <BlockScope>{props.children}</BlockScope>
36
+ </>
37
+ );
38
+ }
39
+
40
+ export interface CatchClauseProps {
41
+ parameter?: ParameterDescriptor | string;
42
+ children: Children;
43
+ }
44
+
45
+ /**
46
+ * A catch clause of a try statement.
47
+ *
48
+ * @example
49
+ * ```tsx
50
+ * <CatchClause parameter={{ name: "error", type: "unknown" }}>
51
+ * console.error(error);
52
+ * </CatchClause>
53
+ * ```
54
+ * renders to
55
+ * ```ts
56
+ * catch (error: unknown) {
57
+ * console.error(error);
58
+ * }
59
+ * ```
60
+ *
61
+ * @remarks
62
+ * The parameter can be omitted for a catch-all clause, or provided as a string
63
+ * or {@link ParameterDescriptor}. When using a descriptor, a refkey can be provided
64
+ * to reference the parameter within the catch block. Note that TypeScript only allows
65
+ * `any` or `unknown` as catch parameter types.
66
+ */
67
+ export function CatchClause(props: CatchClauseProps) {
68
+ if (!props.parameter) {
69
+ return (
70
+ <>
71
+ {" "}
72
+ catch <BlockScope>{props.children}</BlockScope>
73
+ </>
74
+ );
75
+ }
76
+
77
+ const namePolicy = useTSNamePolicy();
78
+ const paramDesc =
79
+ typeof props.parameter === "string" ?
80
+ { name: props.parameter }
81
+ : props.parameter;
82
+
83
+ const symbol = createValueSymbol(paramDesc.name, {
84
+ refkeys: paramDesc.refkey,
85
+ tsFlags: TSSymbolFlags.ParameterSymbol,
86
+ metadata: paramDesc.metadata,
87
+ namePolicy: namePolicy.for("parameter"),
88
+ });
89
+
90
+ onCleanup(() => {
91
+ symbol.delete();
92
+ });
93
+
94
+ const param = (
95
+ <>
96
+ {symbol.name}
97
+ {paramDesc.type && <>: {paramDesc.type}</>}
98
+ </>
99
+ );
100
+
101
+ return (
102
+ <>
103
+ {" "}
104
+ catch ({param}) <BlockScope>{props.children}</BlockScope>
105
+ </>
106
+ );
107
+ }
108
+
109
+ export interface FinallyClauseProps {
110
+ children: Children;
111
+ }
112
+
113
+ /**
114
+ * A finally clause of a try statement.
115
+ *
116
+ * @example
117
+ * ```tsx
118
+ * <TryStatement>
119
+ * // risky operation
120
+ * </TryStatement>
121
+ * <FinallyClause>
122
+ * // cleanup
123
+ * </FinallyClause>
124
+ * ```
125
+ * renders to
126
+ * ```ts
127
+ * try {
128
+ * // risky operation
129
+ * } finally {
130
+ * // cleanup
131
+ * }
132
+ * ```
133
+ */
134
+ export function FinallyClause(props: FinallyClauseProps) {
135
+ return (
136
+ <>
137
+ {" "}
138
+ finally <BlockScope>{props.children}</BlockScope>
139
+ </>
140
+ );
141
+ }
@@ -35,6 +35,7 @@ export * from "./Reference.js";
35
35
  export * from "./SingleLineCommentBlock.jsx";
36
36
  export * from "./SourceFile.js";
37
37
  export * from "./SwitchStatement.jsx";
38
+ export * from "./TryStatement.jsx";
38
39
  export * from "./TsConfigJson.js";
39
40
  export * from "./TypeDeclaration.js";
40
41
  export * from "./TypeRefContext.jsx";
package/temp/api.json CHANGED
@@ -1597,6 +1597,131 @@
1597
1597
  }
1598
1598
  ]
1599
1599
  },
1600
+ {
1601
+ "kind": "Function",
1602
+ "canonicalReference": "@alloy-js/typescript!CatchClause:function(1)",
1603
+ "docComment": "/**\n * A catch clause of a try statement.\n *\n * @remarks\n *\n *\n * The parameter can be omitted for a catch-all clause, or provided as a string\n * or {@link ParameterDescriptor}. When using a descriptor, a refkey can be provided\n * to reference the parameter within the catch block. Note that TypeScript only allows\n * `any` or `unknown` as catch parameter types.\n *\n * @example\n * ```tsx\n * <CatchClause parameter={{ name: \"error\", type: \"unknown\" }}>\n * console.error(error);\n * </CatchClause>\n * ```\n *\n * renders to\n * ```ts\n * catch (error: unknown) {\n * console.error(error);\n * }\n * ```\n *\n */\n",
1604
+ "excerptTokens": [
1605
+ {
1606
+ "kind": "Content",
1607
+ "text": "export declare function CatchClause(props: "
1608
+ },
1609
+ {
1610
+ "kind": "Reference",
1611
+ "text": "CatchClauseProps",
1612
+ "canonicalReference": "@alloy-js/typescript!CatchClauseProps:interface"
1613
+ },
1614
+ {
1615
+ "kind": "Content",
1616
+ "text": "): "
1617
+ },
1618
+ {
1619
+ "kind": "Reference",
1620
+ "text": "Children",
1621
+ "canonicalReference": "@alloy-js/core!Children:type"
1622
+ },
1623
+ {
1624
+ "kind": "Content",
1625
+ "text": ";"
1626
+ }
1627
+ ],
1628
+ "fileUrlPath": "src/components/TryStatement.tsx",
1629
+ "returnTypeTokenRange": {
1630
+ "startIndex": 3,
1631
+ "endIndex": 4
1632
+ },
1633
+ "releaseTag": "Public",
1634
+ "overloadIndex": 1,
1635
+ "parameters": [
1636
+ {
1637
+ "parameterName": "props",
1638
+ "parameterTypeTokenRange": {
1639
+ "startIndex": 1,
1640
+ "endIndex": 2
1641
+ },
1642
+ "isOptional": false
1643
+ }
1644
+ ],
1645
+ "name": "CatchClause"
1646
+ },
1647
+ {
1648
+ "kind": "Interface",
1649
+ "canonicalReference": "@alloy-js/typescript!CatchClauseProps:interface",
1650
+ "docComment": "",
1651
+ "excerptTokens": [
1652
+ {
1653
+ "kind": "Content",
1654
+ "text": "export interface CatchClauseProps "
1655
+ }
1656
+ ],
1657
+ "fileUrlPath": "src/components/TryStatement.tsx",
1658
+ "releaseTag": "Public",
1659
+ "name": "CatchClauseProps",
1660
+ "preserveMemberOrder": false,
1661
+ "members": [
1662
+ {
1663
+ "kind": "PropertySignature",
1664
+ "canonicalReference": "@alloy-js/typescript!CatchClauseProps#children:member",
1665
+ "docComment": "",
1666
+ "excerptTokens": [
1667
+ {
1668
+ "kind": "Content",
1669
+ "text": "children: "
1670
+ },
1671
+ {
1672
+ "kind": "Reference",
1673
+ "text": "Children",
1674
+ "canonicalReference": "@alloy-js/core!Children:type"
1675
+ },
1676
+ {
1677
+ "kind": "Content",
1678
+ "text": ";"
1679
+ }
1680
+ ],
1681
+ "isReadonly": false,
1682
+ "isOptional": false,
1683
+ "releaseTag": "Public",
1684
+ "name": "children",
1685
+ "propertyTypeTokenRange": {
1686
+ "startIndex": 1,
1687
+ "endIndex": 2
1688
+ }
1689
+ },
1690
+ {
1691
+ "kind": "PropertySignature",
1692
+ "canonicalReference": "@alloy-js/typescript!CatchClauseProps#parameter:member",
1693
+ "docComment": "",
1694
+ "excerptTokens": [
1695
+ {
1696
+ "kind": "Content",
1697
+ "text": "parameter?: "
1698
+ },
1699
+ {
1700
+ "kind": "Reference",
1701
+ "text": "ParameterDescriptor",
1702
+ "canonicalReference": "@alloy-js/typescript!ParameterDescriptor:interface"
1703
+ },
1704
+ {
1705
+ "kind": "Content",
1706
+ "text": " | string"
1707
+ },
1708
+ {
1709
+ "kind": "Content",
1710
+ "text": ";"
1711
+ }
1712
+ ],
1713
+ "isReadonly": false,
1714
+ "isOptional": true,
1715
+ "releaseTag": "Public",
1716
+ "name": "parameter",
1717
+ "propertyTypeTokenRange": {
1718
+ "startIndex": 1,
1719
+ "endIndex": 3
1720
+ }
1721
+ }
1722
+ ],
1723
+ "extendsTokenRanges": []
1724
+ },
1600
1725
  {
1601
1726
  "kind": "Function",
1602
1727
  "canonicalReference": "@alloy-js/typescript!ClassDeclaration:function(1)",
@@ -5337,6 +5462,99 @@
5337
5462
  ],
5338
5463
  "extendsTokenRanges": []
5339
5464
  },
5465
+ {
5466
+ "kind": "Function",
5467
+ "canonicalReference": "@alloy-js/typescript!FinallyClause:function(1)",
5468
+ "docComment": "/**\n * A finally clause of a try statement.\n *\n * @example\n * ```tsx\n * <TryStatement>\n * // risky operation\n * </TryStatement>\n * <FinallyClause>\n * // cleanup\n * </FinallyClause>\n * ```\n *\n * renders to\n * ```ts\n * try {\n * // risky operation\n * } finally {\n * // cleanup\n * }\n * ```\n *\n */\n",
5469
+ "excerptTokens": [
5470
+ {
5471
+ "kind": "Content",
5472
+ "text": "export declare function FinallyClause(props: "
5473
+ },
5474
+ {
5475
+ "kind": "Reference",
5476
+ "text": "FinallyClauseProps",
5477
+ "canonicalReference": "@alloy-js/typescript!FinallyClauseProps:interface"
5478
+ },
5479
+ {
5480
+ "kind": "Content",
5481
+ "text": "): "
5482
+ },
5483
+ {
5484
+ "kind": "Reference",
5485
+ "text": "Children",
5486
+ "canonicalReference": "@alloy-js/core!Children:type"
5487
+ },
5488
+ {
5489
+ "kind": "Content",
5490
+ "text": ";"
5491
+ }
5492
+ ],
5493
+ "fileUrlPath": "src/components/TryStatement.tsx",
5494
+ "returnTypeTokenRange": {
5495
+ "startIndex": 3,
5496
+ "endIndex": 4
5497
+ },
5498
+ "releaseTag": "Public",
5499
+ "overloadIndex": 1,
5500
+ "parameters": [
5501
+ {
5502
+ "parameterName": "props",
5503
+ "parameterTypeTokenRange": {
5504
+ "startIndex": 1,
5505
+ "endIndex": 2
5506
+ },
5507
+ "isOptional": false
5508
+ }
5509
+ ],
5510
+ "name": "FinallyClause"
5511
+ },
5512
+ {
5513
+ "kind": "Interface",
5514
+ "canonicalReference": "@alloy-js/typescript!FinallyClauseProps:interface",
5515
+ "docComment": "",
5516
+ "excerptTokens": [
5517
+ {
5518
+ "kind": "Content",
5519
+ "text": "export interface FinallyClauseProps "
5520
+ }
5521
+ ],
5522
+ "fileUrlPath": "src/components/TryStatement.tsx",
5523
+ "releaseTag": "Public",
5524
+ "name": "FinallyClauseProps",
5525
+ "preserveMemberOrder": false,
5526
+ "members": [
5527
+ {
5528
+ "kind": "PropertySignature",
5529
+ "canonicalReference": "@alloy-js/typescript!FinallyClauseProps#children:member",
5530
+ "docComment": "",
5531
+ "excerptTokens": [
5532
+ {
5533
+ "kind": "Content",
5534
+ "text": "children: "
5535
+ },
5536
+ {
5537
+ "kind": "Reference",
5538
+ "text": "Children",
5539
+ "canonicalReference": "@alloy-js/core!Children:type"
5540
+ },
5541
+ {
5542
+ "kind": "Content",
5543
+ "text": ";"
5544
+ }
5545
+ ],
5546
+ "isReadonly": false,
5547
+ "isOptional": false,
5548
+ "releaseTag": "Public",
5549
+ "name": "children",
5550
+ "propertyTypeTokenRange": {
5551
+ "startIndex": 1,
5552
+ "endIndex": 2
5553
+ }
5554
+ }
5555
+ ],
5556
+ "extendsTokenRanges": []
5557
+ },
5340
5558
  {
5341
5559
  "kind": "Interface",
5342
5560
  "canonicalReference": "@alloy-js/typescript!FunctionBodyProps:interface",
@@ -13869,6 +14087,99 @@
13869
14087
  ],
13870
14088
  "extendsTokenRanges": []
13871
14089
  },
14090
+ {
14091
+ "kind": "Function",
14092
+ "canonicalReference": "@alloy-js/typescript!TryStatement:function(1)",
14093
+ "docComment": "/**\n * A try statement.\n *\n * @example\n * ```tsx\n * <TryStatement>\n * // risky operation\n * </TryStatement>\n * <CatchClause parameter=\"error\">\n * console.error(error);\n * </CatchClause>\n * ```\n *\n * renders to\n * ```ts\n * try {\n * // risky operation\n * } catch (error) {\n * console.error(error);\n * }\n * ```\n *\n */\n",
14094
+ "excerptTokens": [
14095
+ {
14096
+ "kind": "Content",
14097
+ "text": "export declare function TryStatement(props: "
14098
+ },
14099
+ {
14100
+ "kind": "Reference",
14101
+ "text": "TryStatementProps",
14102
+ "canonicalReference": "@alloy-js/typescript!TryStatementProps:interface"
14103
+ },
14104
+ {
14105
+ "kind": "Content",
14106
+ "text": "): "
14107
+ },
14108
+ {
14109
+ "kind": "Reference",
14110
+ "text": "Children",
14111
+ "canonicalReference": "@alloy-js/core!Children:type"
14112
+ },
14113
+ {
14114
+ "kind": "Content",
14115
+ "text": ";"
14116
+ }
14117
+ ],
14118
+ "fileUrlPath": "src/components/TryStatement.tsx",
14119
+ "returnTypeTokenRange": {
14120
+ "startIndex": 3,
14121
+ "endIndex": 4
14122
+ },
14123
+ "releaseTag": "Public",
14124
+ "overloadIndex": 1,
14125
+ "parameters": [
14126
+ {
14127
+ "parameterName": "props",
14128
+ "parameterTypeTokenRange": {
14129
+ "startIndex": 1,
14130
+ "endIndex": 2
14131
+ },
14132
+ "isOptional": false
14133
+ }
14134
+ ],
14135
+ "name": "TryStatement"
14136
+ },
14137
+ {
14138
+ "kind": "Interface",
14139
+ "canonicalReference": "@alloy-js/typescript!TryStatementProps:interface",
14140
+ "docComment": "",
14141
+ "excerptTokens": [
14142
+ {
14143
+ "kind": "Content",
14144
+ "text": "export interface TryStatementProps "
14145
+ }
14146
+ ],
14147
+ "fileUrlPath": "src/components/TryStatement.tsx",
14148
+ "releaseTag": "Public",
14149
+ "name": "TryStatementProps",
14150
+ "preserveMemberOrder": false,
14151
+ "members": [
14152
+ {
14153
+ "kind": "PropertySignature",
14154
+ "canonicalReference": "@alloy-js/typescript!TryStatementProps#children:member",
14155
+ "docComment": "",
14156
+ "excerptTokens": [
14157
+ {
14158
+ "kind": "Content",
14159
+ "text": "children: "
14160
+ },
14161
+ {
14162
+ "kind": "Reference",
14163
+ "text": "Children",
14164
+ "canonicalReference": "@alloy-js/core!Children:type"
14165
+ },
14166
+ {
14167
+ "kind": "Content",
14168
+ "text": ";"
14169
+ }
14170
+ ],
14171
+ "isReadonly": false,
14172
+ "isOptional": false,
14173
+ "releaseTag": "Public",
14174
+ "name": "children",
14175
+ "propertyTypeTokenRange": {
14176
+ "startIndex": 1,
14177
+ "endIndex": 2
14178
+ }
14179
+ }
14180
+ ],
14181
+ "extendsTokenRanges": []
14182
+ },
13872
14183
  {
13873
14184
  "kind": "Function",
13874
14185
  "canonicalReference": "@alloy-js/typescript!TSConfigJson:function(1)",