@alloy-js/typescript 0.23.0-dev.0 → 0.23.0-dev.4
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/src/components/FunctionType.d.ts +1 -1
- package/dist/src/components/FunctionType.js +1 -1
- package/dist/src/components/FunctionType.js.map +1 -1
- package/dist/src/components/IfStatement.d.ts.map +1 -1
- package/dist/src/components/IfStatement.js +2 -3
- package/dist/src/components/IfStatement.js.map +1 -1
- package/dist/src/components/Interface.d.ts +1 -1
- package/dist/src/components/Interface.d.ts.map +1 -1
- package/dist/src/components/Interface.js +14 -9
- package/dist/src/components/Interface.js.map +1 -1
- package/dist/src/components/MemberScope.d.ts.map +1 -1
- package/dist/src/components/MemberScope.js +6 -2
- package/dist/src/components/MemberScope.js.map +1 -1
- package/dist/src/components/PackageDirectory.d.ts.map +1 -1
- package/dist/src/components/PackageDirectory.js +2 -2
- package/dist/src/components/PackageDirectory.js.map +1 -1
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +4 -2
- package/dist/src/components/SourceFile.js.map +1 -1
- package/dist/src/components/TryStatement.d.ts +80 -0
- package/dist/src/components/TryStatement.d.ts.map +1 -0
- package/dist/src/components/TryStatement.js +112 -0
- package/dist/src/components/TryStatement.js.map +1 -0
- package/dist/src/components/TryStatement.test.d.ts +2 -0
- package/dist/src/components/TryStatement.test.d.ts.map +1 -0
- package/dist/src/components/TryStatement.test.js +121 -0
- package/dist/src/components/TryStatement.test.js.map +1 -0
- package/dist/src/components/TypeDeclaration.d.ts +1 -1
- package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
- package/dist/src/components/TypeDeclaration.js +1 -1
- package/dist/src/components/TypeDeclaration.js.map +1 -1
- package/dist/src/components/TypeRefContext.d.ts +1 -1
- package/dist/src/components/TypeRefContext.d.ts.map +1 -1
- package/dist/src/components/TypeRefContext.js +1 -1
- package/dist/src/components/TypeRefContext.js.map +1 -1
- package/dist/src/components/index.d.ts +1 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +1 -0
- package/dist/src/components/index.js.map +1 -1
- package/dist/src/context/type-ref-context.d.ts.map +1 -1
- package/dist/src/context/type-ref-context.js +2 -2
- package/dist/src/context/type-ref-context.js.map +1 -1
- package/dist/src/create-package.d.ts.map +1 -1
- package/dist/src/create-package.js +7 -6
- package/dist/src/create-package.js.map +1 -1
- package/dist/src/symbols/index.d.ts.map +1 -1
- package/dist/src/symbols/index.js +47 -11
- package/dist/src/symbols/index.js.map +1 -1
- package/dist/src/symbols/ts-module-scope.d.ts +1 -0
- package/dist/src/symbols/ts-module-scope.d.ts.map +1 -1
- package/dist/src/symbols/ts-module-scope.js +14 -3
- package/dist/src/symbols/ts-module-scope.js.map +1 -1
- package/dist/src/symbols/ts-output-symbol.d.ts +1 -0
- package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
- package/dist/src/symbols/ts-output-symbol.js +20 -3
- package/dist/src/symbols/ts-output-symbol.js.map +1 -1
- package/dist/src/symbols/ts-package-scope.d.ts +1 -0
- package/dist/src/symbols/ts-package-scope.d.ts.map +1 -1
- package/dist/src/symbols/ts-package-scope.js +8 -0
- package/dist/src/symbols/ts-package-scope.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -5
- package/src/components/FunctionType.tsx +1 -1
- package/src/components/IfStatement.tsx +3 -3
- package/src/components/Interface.tsx +24 -13
- package/src/components/MemberScope.tsx +13 -3
- package/src/components/PackageDirectory.tsx +2 -1
- package/src/components/SourceFile.tsx +4 -1
- package/src/components/TryStatement.test.tsx +123 -0
- package/src/components/TryStatement.tsx +141 -0
- package/src/components/TypeDeclaration.tsx +15 -15
- package/src/components/TypeRefContext.tsx +2 -2
- package/src/components/index.ts +1 -0
- package/src/context/type-ref-context.tsx +6 -2
- package/src/create-package.ts +29 -15
- package/src/symbols/index.ts +36 -10
- package/src/symbols/ts-module-scope.ts +36 -9
- package/src/symbols/ts-output-symbol.ts +21 -2
- package/src/symbols/ts-package-scope.ts +9 -0
- package/temp/api.json +471 -63
package/package.json
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alloy-js/typescript",
|
|
3
|
-
"version": "0.23.0-dev.
|
|
3
|
+
"version": "0.23.0-dev.4",
|
|
4
4
|
"description": "",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/alloy-framework/alloy.git"
|
|
8
|
+
},
|
|
5
9
|
"exports": {
|
|
6
10
|
".": {
|
|
7
11
|
"import": "./dist/src/index.js"
|
|
@@ -22,18 +26,18 @@
|
|
|
22
26
|
"author": "brian.terlson@microsoft.com",
|
|
23
27
|
"license": "MIT",
|
|
24
28
|
"dependencies": {
|
|
25
|
-
"@alloy-js/core": "~0.22.0 || >= 0.23.0-dev.
|
|
29
|
+
"@alloy-js/core": "~0.22.0 || >= 0.23.0-dev.8",
|
|
26
30
|
"change-case": "^5.4.4",
|
|
27
31
|
"pathe": "^2.0.3"
|
|
28
32
|
},
|
|
29
33
|
"devDependencies": {
|
|
30
|
-
"@alloy-js/cli": "~0.22.0 || >= 0.23.0-dev.
|
|
31
|
-
"@alloy-js/rollup-plugin": "~0.1.0 || >= 0.1.1-dev.
|
|
34
|
+
"@alloy-js/cli": "~0.22.0 || >= 0.23.0-dev.3",
|
|
35
|
+
"@alloy-js/rollup-plugin": "~0.1.0 || >= 0.1.1-dev.1",
|
|
32
36
|
"@microsoft/api-extractor": "~7.52.8",
|
|
33
37
|
"@rollup/plugin-typescript": "^12.1.2",
|
|
34
38
|
"concurrently": "^9.2.0",
|
|
35
39
|
"typescript": "^5.8.3",
|
|
36
|
-
"vitest": "
|
|
40
|
+
"vitest": "3.2.4"
|
|
37
41
|
},
|
|
38
42
|
"type": "module",
|
|
39
43
|
"scripts": {
|
|
@@ -31,7 +31,7 @@ export interface FunctionTypeProps extends CallSignatureProps {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
const FunctionTypeComponent = ensureTypeRefContext(
|
|
34
|
-
(props: FunctionTypeProps)
|
|
34
|
+
function FunctionTypeComponent(props: FunctionTypeProps) {
|
|
35
35
|
const children = childrenArray(() => props.children);
|
|
36
36
|
const typeParametersChildren =
|
|
37
37
|
findKeyedChild(children, TypeParameters.tag) ?? undefined;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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}) <
|
|
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 <
|
|
48
|
+
else <BlockScope>{props.children}</BlockScope>
|
|
49
49
|
</>
|
|
50
50
|
);
|
|
51
51
|
}
|
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
Block,
|
|
3
3
|
Children,
|
|
4
4
|
childrenArray,
|
|
5
|
+
createSymbol,
|
|
5
6
|
createSymbolSlot,
|
|
6
7
|
effect,
|
|
7
8
|
emitSymbol,
|
|
@@ -35,7 +36,7 @@ export interface InterfaceDeclarationProps extends CommonDeclarationProps {
|
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
const _InterfaceDeclaration = ensureTypeRefContext(
|
|
38
|
-
(props: InterfaceDeclarationProps)
|
|
39
|
+
function InterfaceDeclaration(props: InterfaceDeclarationProps) {
|
|
39
40
|
const ExprSlot = createSymbolSlot();
|
|
40
41
|
|
|
41
42
|
effect(() => {
|
|
@@ -68,13 +69,15 @@ const _InterfaceDeclaration = ensureTypeRefContext(
|
|
|
68
69
|
const filteredChildren = findUnkeyedChildren(children);
|
|
69
70
|
const currentScope = useTSLexicalScope();
|
|
70
71
|
|
|
71
|
-
const
|
|
72
|
+
const binder = currentScope?.binder;
|
|
73
|
+
const sym = createSymbol(TSOutputSymbol, props.name, currentScope.types, {
|
|
72
74
|
refkeys: props.refkey,
|
|
73
75
|
default: props.default,
|
|
74
76
|
export: props.export,
|
|
75
77
|
metadata: props.metadata,
|
|
76
78
|
tsFlags: TSSymbolFlags.TypeSymbol,
|
|
77
79
|
namePolicy: useTSNamePolicy().for("interface"),
|
|
80
|
+
binder,
|
|
78
81
|
});
|
|
79
82
|
return (
|
|
80
83
|
<>
|
|
@@ -115,9 +118,11 @@ export interface InterfaceExpressionProps {
|
|
|
115
118
|
}
|
|
116
119
|
|
|
117
120
|
export const InterfaceExpression = ensureTypeRefContext(
|
|
118
|
-
(props: InterfaceExpressionProps)
|
|
119
|
-
const
|
|
121
|
+
function InterfaceExpression(props: InterfaceExpressionProps) {
|
|
122
|
+
const scope = useTSLexicalScope();
|
|
123
|
+
const symbol = createSymbol(TSOutputSymbol, "", undefined, {
|
|
120
124
|
transient: true,
|
|
125
|
+
binder: scope?.binder,
|
|
121
126
|
});
|
|
122
127
|
|
|
123
128
|
emitSymbol(symbol);
|
|
@@ -180,15 +185,21 @@ export function InterfaceMember(props: InterfaceMemberProps) {
|
|
|
180
185
|
|
|
181
186
|
const optionality = props.optional ? "?" : "";
|
|
182
187
|
const scope = useTSMemberScope();
|
|
183
|
-
const sym =
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
:
|
|
190
|
-
|
|
191
|
-
|
|
188
|
+
const sym = createSymbol(
|
|
189
|
+
TSOutputSymbol,
|
|
190
|
+
props.name,
|
|
191
|
+
scope.ownerSymbol.staticMembers,
|
|
192
|
+
{
|
|
193
|
+
refkeys: props.refkey,
|
|
194
|
+
tsFlags:
|
|
195
|
+
TSSymbolFlags.TypeSymbol |
|
|
196
|
+
((props.nullish ?? props.optional) ?
|
|
197
|
+
TSSymbolFlags.Nullish
|
|
198
|
+
: TSSymbolFlags.None),
|
|
199
|
+
namePolicy: useTSNamePolicy().for("interface-member"),
|
|
200
|
+
binder: scope.binder,
|
|
201
|
+
},
|
|
202
|
+
);
|
|
192
203
|
|
|
193
204
|
const taken = takeSymbols();
|
|
194
205
|
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
Children,
|
|
3
|
+
MemberScope as CoreMemberScope,
|
|
4
|
+
createScope,
|
|
5
|
+
} from "@alloy-js/core";
|
|
2
6
|
import { useTSLexicalScopeIfPresent } from "../symbols/scopes.js";
|
|
3
7
|
import { TSMemberScope } from "../symbols/ts-member-scope.js";
|
|
4
8
|
import { TSOutputSymbol } from "../symbols/ts-output-symbol.js";
|
|
@@ -17,10 +21,16 @@ export interface MemberScopeProps {
|
|
|
17
21
|
* in this scope, which will be added to the owner symbol's members.
|
|
18
22
|
*/
|
|
19
23
|
export function MemberScope(props: MemberScopeProps) {
|
|
20
|
-
const
|
|
24
|
+
const parentScope = useTSLexicalScopeIfPresent();
|
|
25
|
+
const binder = props.ownerSymbol.binder ?? parentScope?.binder;
|
|
26
|
+
const memberScope = createScope(
|
|
27
|
+
TSMemberScope,
|
|
21
28
|
"member-scope",
|
|
22
|
-
|
|
29
|
+
parentScope,
|
|
23
30
|
props.ownerSymbol,
|
|
31
|
+
{
|
|
32
|
+
binder,
|
|
33
|
+
},
|
|
24
34
|
);
|
|
25
35
|
|
|
26
36
|
return (
|
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
Children,
|
|
3
3
|
ComponentContext,
|
|
4
4
|
createNamedContext,
|
|
5
|
+
createScope,
|
|
5
6
|
Ref,
|
|
6
7
|
Scope,
|
|
7
8
|
shallowRef,
|
|
@@ -122,7 +123,7 @@ function createPackageContext(
|
|
|
122
123
|
// todo: this can probably just use context.
|
|
123
124
|
const fullPath = parentDir ? join(parentDir.path, path) : path;
|
|
124
125
|
|
|
125
|
-
const scope =
|
|
126
|
+
const scope = createScope(TSPackageScope, name, version, fullPath);
|
|
126
127
|
|
|
127
128
|
return {
|
|
128
129
|
scope,
|
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
ComponentContext,
|
|
3
3
|
SourceFile as CoreSourceFile,
|
|
4
4
|
createNamedContext,
|
|
5
|
+
createScope,
|
|
5
6
|
Scope,
|
|
6
7
|
Show,
|
|
7
8
|
SourceDirectoryContext,
|
|
@@ -41,7 +42,9 @@ export function SourceFile(props: SourceFileProps) {
|
|
|
41
42
|
const currentDir = directoryContext.path;
|
|
42
43
|
const path: string = join(currentDir, props.path);
|
|
43
44
|
const parent = useScope();
|
|
44
|
-
const scope =
|
|
45
|
+
const scope = createScope(TSModuleScope, path, parent, {
|
|
46
|
+
binder: parent?.binder,
|
|
47
|
+
});
|
|
45
48
|
sdData.modules.add(scope);
|
|
46
49
|
const pkg = useContext(PackageContext);
|
|
47
50
|
if (pkg) {
|
|
@@ -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
|
+
}
|
|
@@ -5,18 +5,18 @@ import { ensureTypeRefContext } from "./TypeRefContext.jsx";
|
|
|
5
5
|
|
|
6
6
|
export interface TypeDeclarationProps extends CommonDeclarationProps {}
|
|
7
7
|
|
|
8
|
-
export const TypeDeclaration = ensureTypeRefContext(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
);
|
|
8
|
+
export const TypeDeclaration = ensureTypeRefContext(function TypeDeclaration(
|
|
9
|
+
props: TypeDeclarationProps,
|
|
10
|
+
) {
|
|
11
|
+
return (
|
|
12
|
+
<>
|
|
13
|
+
<Show when={Boolean(props.doc)}>
|
|
14
|
+
<JSDoc children={props.doc} />
|
|
15
|
+
<hbr />
|
|
16
|
+
</Show>
|
|
17
|
+
<Declaration {...props} kind="type" nameKind="type">
|
|
18
|
+
type <Name /> = {props.children};
|
|
19
|
+
</Declaration>
|
|
20
|
+
</>
|
|
21
|
+
);
|
|
22
|
+
});
|
|
@@ -33,8 +33,8 @@ export const TypeRefContext = ({ children }: TypeRefContextProps) => {
|
|
|
33
33
|
*/
|
|
34
34
|
export function ensureTypeRefContext<TProps>(
|
|
35
35
|
Comp: Component<TProps>,
|
|
36
|
-
):
|
|
37
|
-
return (props: TProps)
|
|
36
|
+
): Component<TProps> {
|
|
37
|
+
return function EnsureTypeRefContext(props: TProps) {
|
|
38
38
|
const ref = isTypeRefContext();
|
|
39
39
|
if (!ref) {
|
|
40
40
|
return (
|
package/src/components/index.ts
CHANGED
|
@@ -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";
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
ComponentContext,
|
|
3
|
+
createNamedContext,
|
|
4
|
+
useContext,
|
|
5
|
+
} from "@alloy-js/core";
|
|
2
6
|
|
|
3
7
|
interface TypeRefContext {
|
|
4
8
|
/** If in a type context */
|
|
@@ -9,7 +13,7 @@ interface TypeRefContext {
|
|
|
9
13
|
* Provides scopes for instance and static private members.
|
|
10
14
|
*/
|
|
11
15
|
export const TypeRefContext: ComponentContext<TypeRefContext> =
|
|
12
|
-
|
|
16
|
+
createNamedContext<TypeRefContext>("@alloy-js/typescript TypeRef");
|
|
13
17
|
|
|
14
18
|
/**
|
|
15
19
|
* @returns 'true' if in a type context, 'false' if in a value context.
|
package/src/create-package.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type Binder,
|
|
3
|
+
createScope,
|
|
4
|
+
createSymbol,
|
|
3
5
|
getSymbolCreatorSymbol,
|
|
4
6
|
OutputMemberSpace,
|
|
5
7
|
type Refkey,
|
|
@@ -136,7 +138,7 @@ function assignMembers(
|
|
|
136
138
|
const memberKey = keys[namespace][memberObj.name];
|
|
137
139
|
if (!memberKey) continue; // Skip if key doesn't exist
|
|
138
140
|
|
|
139
|
-
const memberSym =
|
|
141
|
+
const memberSym = createSymbol(TSOutputSymbol, memberObj.name, space, {
|
|
140
142
|
binder,
|
|
141
143
|
refkeys: memberKey,
|
|
142
144
|
export: false,
|
|
@@ -179,7 +181,8 @@ function createSymbols(
|
|
|
179
181
|
props: CreatePackageProps<PackageDescriptor>,
|
|
180
182
|
refkeys: Record<string | typeof packageScopeSymbol, any>,
|
|
181
183
|
) {
|
|
182
|
-
const pkgScope =
|
|
184
|
+
const pkgScope = createScope(
|
|
185
|
+
TSPackageScope,
|
|
183
186
|
props.name,
|
|
184
187
|
props.version,
|
|
185
188
|
`node_modules/${props.name}`,
|
|
@@ -193,7 +196,7 @@ function createSymbols(
|
|
|
193
196
|
|
|
194
197
|
for (const [path, symbols] of Object.entries(props.descriptor)) {
|
|
195
198
|
const keys = path === "." ? refkeys : refkeys[path];
|
|
196
|
-
const moduleScope =
|
|
199
|
+
const moduleScope = createScope(TSModuleScope, path, pkgScope, {
|
|
197
200
|
binder,
|
|
198
201
|
});
|
|
199
202
|
|
|
@@ -201,11 +204,17 @@ function createSymbols(
|
|
|
201
204
|
|
|
202
205
|
if (symbols.default) {
|
|
203
206
|
const key = keys.default;
|
|
204
|
-
const sym =
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
207
|
+
const sym = createSymbol(
|
|
208
|
+
TSOutputSymbol,
|
|
209
|
+
symbols.default,
|
|
210
|
+
moduleScope.values,
|
|
211
|
+
{
|
|
212
|
+
binder,
|
|
213
|
+
refkeys: key,
|
|
214
|
+
export: true,
|
|
215
|
+
default: true,
|
|
216
|
+
},
|
|
217
|
+
);
|
|
209
218
|
moduleScope.exportedSymbols.set(key, sym);
|
|
210
219
|
}
|
|
211
220
|
|
|
@@ -215,13 +224,18 @@ function createSymbols(
|
|
|
215
224
|
{ name: exportedName }
|
|
216
225
|
: exportedName;
|
|
217
226
|
const key = keys[namedRef.name];
|
|
218
|
-
const ownerSym =
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
227
|
+
const ownerSym = createSymbol(
|
|
228
|
+
TSOutputSymbol,
|
|
229
|
+
namedRef.name,
|
|
230
|
+
moduleScope.values,
|
|
231
|
+
{
|
|
232
|
+
binder,
|
|
233
|
+
refkeys: key,
|
|
234
|
+
export: true,
|
|
235
|
+
default: false,
|
|
236
|
+
hasInstanceMembers: !!namedRef.instanceMembers?.length,
|
|
237
|
+
},
|
|
238
|
+
);
|
|
225
239
|
moduleScope.exportedSymbols.set(key, ownerSym);
|
|
226
240
|
|
|
227
241
|
assignMembers(
|