@alloy-js/typescript 0.10.0 → 0.12.0
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/CHANGELOG.md +28 -0
- package/dist/src/builtins/node.js +1 -2
- package/dist/src/components/ArrayExpression.js +2 -5
- package/dist/src/components/ArrowFunction.d.ts +40 -0
- package/dist/src/components/ArrowFunction.d.ts.map +1 -0
- package/dist/src/components/ArrowFunction.js +67 -0
- package/dist/src/components/BarrelFile.js +1 -2
- package/dist/src/components/BlockScope.js +2 -4
- package/dist/src/components/CallSignature.js +2 -4
- package/dist/src/components/ClassDeclaration.d.ts +2 -0
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
- package/dist/src/components/ClassDeclaration.js +16 -8
- package/dist/src/components/CommaList.js +2 -5
- package/dist/src/components/Declaration.js +3 -5
- package/dist/src/components/EnumDeclaration.js +2 -5
- package/dist/src/components/EnumMember.js +2 -4
- package/dist/src/components/ExportStatement.js +1 -2
- package/dist/src/components/FunctionBase.d.ts +60 -0
- package/dist/src/components/FunctionBase.d.ts.map +1 -0
- package/dist/src/components/FunctionBase.js +217 -0
- package/dist/src/components/FunctionCallExpression.js +2 -5
- package/dist/src/components/FunctionDeclaration.d.ts +3 -18
- package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
- package/dist/src/components/FunctionDeclaration.js +9 -157
- package/dist/src/components/FunctionExpression.d.ts +39 -0
- package/dist/src/components/FunctionExpression.d.ts.map +1 -0
- package/dist/src/components/FunctionExpression.js +66 -0
- package/dist/src/components/FunctionType.d.ts +38 -0
- package/dist/src/components/FunctionType.d.ts.map +1 -0
- package/dist/src/components/FunctionType.js +77 -0
- package/dist/src/components/IfStatement.js +2 -4
- package/dist/src/components/ImportStatement.d.ts.map +1 -1
- package/dist/src/components/ImportStatement.js +10 -4
- package/dist/src/components/Interface.d.ts +3 -2
- package/dist/src/components/Interface.d.ts.map +1 -1
- package/dist/src/components/Interface.js +40 -15
- package/dist/src/components/InterfaceMethod.d.ts +43 -0
- package/dist/src/components/InterfaceMethod.d.ts.map +1 -0
- package/dist/src/components/InterfaceMethod.js +87 -0
- package/dist/src/components/JSDoc.js +1 -2
- package/dist/src/components/JSDocComment.js +3 -5
- package/dist/src/components/JSDocExample.js +2 -5
- package/dist/src/components/JSDocParam.js +2 -5
- package/dist/src/components/MemberChainExpression.js +2 -4
- package/dist/src/components/MemberDeclaration.d.ts +34 -0
- package/dist/src/components/MemberDeclaration.d.ts.map +1 -0
- package/dist/src/components/MemberDeclaration.js +29 -0
- package/dist/src/components/MemberExpression.d.ts.map +1 -1
- package/dist/src/components/MemberExpression.js +5 -7
- package/dist/src/components/ObjectExpression.js +2 -5
- package/dist/src/components/PackageDirectory.js +2 -5
- package/dist/src/components/PackageJson.js +1 -2
- package/dist/src/components/PropertyName.js +1 -2
- package/dist/src/components/Reference.d.ts +8 -1
- package/dist/src/components/Reference.d.ts.map +1 -1
- package/dist/src/components/Reference.js +8 -4
- package/dist/src/components/SingleLineCommentBlock.js +2 -4
- package/dist/src/components/SourceFile.js +2 -4
- package/dist/src/components/SwitchStatement.js +2 -5
- package/dist/src/components/TsConfigJson.js +1 -2
- 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 +4 -7
- package/dist/src/components/TypeRefContext.d.ts +22 -0
- package/dist/src/components/TypeRefContext.d.ts.map +1 -0
- package/dist/src/components/TypeRefContext.js +41 -0
- package/dist/src/components/ValueExpression.js +1 -2
- package/dist/src/components/VarDeclaration.d.ts +1 -0
- package/dist/src/components/VarDeclaration.d.ts.map +1 -1
- package/dist/src/components/VarDeclaration.js +11 -8
- package/dist/src/components/index.d.ts +7 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +7 -1
- package/dist/src/components/stc/index.d.ts +8 -0
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +9 -2
- package/dist/src/context/private-scope.js +1 -2
- package/dist/src/context/type-ref-context.d.ts +15 -0
- package/dist/src/context/type-ref-context.d.ts.map +1 -0
- package/dist/src/context/type-ref-context.js +12 -0
- package/dist/src/create-package.js +1 -2
- package/dist/src/index.js +1 -2
- package/dist/src/name-conflict-resolver.js +1 -2
- package/dist/src/name-policy.js +1 -2
- package/dist/src/parameter-descriptor.d.ts +33 -17
- package/dist/src/parameter-descriptor.d.ts.map +1 -1
- package/dist/src/source-directory-data.js +1 -2
- package/dist/src/symbols/index.js +1 -2
- package/dist/src/symbols/reference.d.ts +4 -1
- package/dist/src/symbols/reference.d.ts.map +1 -1
- package/dist/src/symbols/reference.js +13 -8
- package/dist/src/symbols/scopes.js +1 -2
- package/dist/src/symbols/ts-member-scope.js +1 -2
- package/dist/src/symbols/ts-module-scope.d.ts +4 -1
- package/dist/src/symbols/ts-module-scope.d.ts.map +1 -1
- package/dist/src/symbols/ts-module-scope.js +11 -5
- package/dist/src/symbols/ts-output-symbol.d.ts +2 -1
- package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
- package/dist/src/symbols/ts-output-symbol.js +2 -2
- package/dist/src/symbols/ts-package-scope.js +1 -2
- package/dist/src/utils.js +1 -2
- package/dist/test/array.test.js +105 -0
- package/dist/test/arrow-function.test.d.ts +2 -0
- package/dist/test/arrow-function.test.d.ts.map +1 -0
- package/dist/test/arrow-function.test.js +194 -0
- package/dist/test/barrel.test.js +53 -0
- package/dist/test/basic.test.js +91 -0
- package/dist/test/block-scope.test.js +30 -0
- package/dist/test/class.test.js +438 -0
- package/dist/test/enum-jsdoc.test.js +88 -0
- package/dist/test/enum.test.js +247 -0
- package/dist/test/externals.test.js +145 -0
- package/dist/test/function-call-expression.test.js +87 -0
- package/dist/test/function-declaration-jsdoc.test.js +91 -0
- package/dist/test/function-declaration.test.js +228 -0
- package/dist/test/function-expression.test.d.ts +2 -0
- package/dist/test/function-expression.test.d.ts.map +1 -0
- package/dist/test/function-expression.test.js +194 -0
- package/dist/test/function-type.test.d.ts +2 -0
- package/dist/test/function-type.test.d.ts.map +1 -0
- package/dist/test/function-type.test.js +122 -0
- package/dist/test/if-statement.test.js +25 -0
- package/dist/test/imports.test.d.ts.map +1 -1
- package/dist/test/imports.test.js +640 -0
- package/dist/test/interface-jsdoc.test.js +178 -0
- package/dist/test/interface.test.d.ts.map +1 -1
- package/dist/test/interface.test.js +490 -0
- package/dist/test/jscomment.test.js +70 -0
- package/dist/test/jsdoc.test.js +134 -0
- package/dist/test/jsdocparam.test.js +164 -0
- package/dist/test/member-chain.test.js +141 -0
- package/dist/test/member-expression.test.js +575 -0
- package/dist/test/name-conflict.test.js +108 -0
- package/dist/test/name-policy.test.js +122 -0
- package/dist/test/package-external.test.js +196 -0
- package/dist/test/package-json.test.js +93 -0
- package/dist/test/package.test.js +136 -0
- package/dist/test/reference.test.js +81 -0
- package/dist/test/source-file.test.js +36 -0
- package/dist/test/switch-statement.test.js +61 -0
- package/dist/test/utils.js +45 -0
- package/dist/test/value-expression.test.js +30 -0
- package/dist/test/values.test.js +351 -0
- package/dist/test/var-declaration-jsdoc.test.js +50 -0
- package/dist/test/var-declaration.test.js +63 -0
- package/dist/test/vitest.d.js +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +13 -18
- package/src/components/ArrowFunction.tsx +87 -0
- package/src/components/ClassDeclaration.tsx +19 -3
- package/src/components/Declaration.tsx +1 -1
- package/src/components/FunctionBase.tsx +282 -0
- package/src/components/FunctionDeclaration.tsx +13 -195
- package/src/components/FunctionExpression.tsx +86 -0
- package/src/components/FunctionType.tsx +93 -0
- package/src/components/ImportStatement.tsx +30 -3
- package/src/components/Interface.tsx +58 -23
- package/src/components/InterfaceMethod.tsx +96 -0
- package/src/components/JSDocComment.tsx +1 -1
- package/src/components/MemberDeclaration.tsx +76 -0
- package/src/components/MemberExpression.tsx +7 -2
- package/src/components/Reference.tsx +12 -2
- package/src/components/TypeDeclaration.tsx +16 -13
- package/src/components/TypeRefContext.tsx +49 -0
- package/src/components/VarDeclaration.tsx +6 -2
- package/src/components/index.ts +11 -0
- package/src/components/stc/index.ts +8 -0
- package/src/context/type-ref-context.tsx +19 -0
- package/src/parameter-descriptor.ts +35 -17
- package/src/symbols/reference.ts +21 -6
- package/src/symbols/ts-module-scope.ts +19 -4
- package/src/symbols/ts-output-symbol.ts +1 -0
- package/temp/api.json +4999 -2681
- package/test/arrow-function.test.tsx +188 -0
- package/test/class.test.tsx +5 -6
- package/test/enum-jsdoc.test.tsx +6 -6
- package/test/function-call-expression.test.tsx +1 -2
- package/test/function-declaration-jsdoc.test.tsx +3 -3
- package/test/function-declaration.test.tsx +1 -2
- package/test/function-expression.test.tsx +195 -0
- package/test/function-type.test.tsx +118 -0
- package/test/imports.test.tsx +165 -1
- package/test/interface-jsdoc.test.tsx +9 -9
- package/test/interface.test.tsx +256 -6
- package/test/jsdoc.test.tsx +5 -5
- package/test/jsdocparam.test.tsx +8 -8
- package/test/member-expression.test.tsx +171 -2
- package/test/var-declaration-jsdoc.test.tsx +3 -3
- package/test/var-declaration.test.tsx +1 -1
- package/tsdoc-metadata.json +1 -1
- package/babel.config.cjs +0 -4
- package/dist/src/builtins/index.js.map +0 -1
- package/dist/src/builtins/node.js.map +0 -1
- package/dist/src/components/ArrayExpression.js.map +0 -1
- package/dist/src/components/BarrelFile.js.map +0 -1
- package/dist/src/components/BlockScope.js.map +0 -1
- package/dist/src/components/CallSignature.js.map +0 -1
- package/dist/src/components/ClassDeclaration.js.map +0 -1
- package/dist/src/components/CommaList.js.map +0 -1
- package/dist/src/components/Declaration.js.map +0 -1
- package/dist/src/components/EnumDeclaration.js.map +0 -1
- package/dist/src/components/EnumMember.js.map +0 -1
- package/dist/src/components/ExportStatement.js.map +0 -1
- package/dist/src/components/FunctionCallExpression.js.map +0 -1
- package/dist/src/components/FunctionDeclaration.js.map +0 -1
- package/dist/src/components/IfStatement.js.map +0 -1
- package/dist/src/components/ImportStatement.js.map +0 -1
- package/dist/src/components/Interface.js.map +0 -1
- package/dist/src/components/JSDoc.js.map +0 -1
- package/dist/src/components/JSDocComment.js.map +0 -1
- package/dist/src/components/JSDocExample.js.map +0 -1
- package/dist/src/components/JSDocParam.js.map +0 -1
- package/dist/src/components/MemberChainExpression.js.map +0 -1
- package/dist/src/components/MemberExpression.js.map +0 -1
- package/dist/src/components/ObjectExpression.js.map +0 -1
- package/dist/src/components/PackageDirectory.js.map +0 -1
- package/dist/src/components/PackageJson.js.map +0 -1
- package/dist/src/components/PropertyName.js.map +0 -1
- package/dist/src/components/Reference.js.map +0 -1
- package/dist/src/components/SingleLineCommentBlock.js.map +0 -1
- package/dist/src/components/SourceFile.js.map +0 -1
- package/dist/src/components/SwitchStatement.js.map +0 -1
- package/dist/src/components/TsConfigJson.js.map +0 -1
- package/dist/src/components/TypeDeclaration.js.map +0 -1
- package/dist/src/components/ValueExpression.js.map +0 -1
- package/dist/src/components/VarDeclaration.js.map +0 -1
- package/dist/src/components/index.js.map +0 -1
- package/dist/src/components/stc/index.js.map +0 -1
- package/dist/src/context/index.js.map +0 -1
- package/dist/src/context/private-scope.js.map +0 -1
- package/dist/src/create-package.js.map +0 -1
- package/dist/src/index.js.map +0 -1
- package/dist/src/name-conflict-resolver.js.map +0 -1
- package/dist/src/name-policy.js.map +0 -1
- package/dist/src/parameter-descriptor.js.map +0 -1
- package/dist/src/source-directory-data.js.map +0 -1
- package/dist/src/symbols/index.js.map +0 -1
- package/dist/src/symbols/reference.js.map +0 -1
- package/dist/src/symbols/scopes.js.map +0 -1
- package/dist/src/symbols/ts-member-scope.js.map +0 -1
- package/dist/src/symbols/ts-module-scope.js.map +0 -1
- package/dist/src/symbols/ts-output-symbol.js.map +0 -1
- package/dist/src/symbols/ts-package-scope.js.map +0 -1
- package/dist/src/utils.js.map +0 -1
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {
|
|
2
|
+
childrenArray,
|
|
3
|
+
findKeyedChild,
|
|
4
|
+
findUnkeyedChildren,
|
|
5
|
+
Scope,
|
|
6
|
+
} from "@alloy-js/core";
|
|
7
|
+
import { Children } from "@alloy-js/core/jsx-runtime";
|
|
8
|
+
import { getCallSignatureProps } from "../utils.js";
|
|
9
|
+
import { CallSignature, CallSignatureProps } from "./CallSignature.jsx";
|
|
10
|
+
import { Declaration } from "./Declaration.jsx";
|
|
11
|
+
import {
|
|
12
|
+
FunctionBody,
|
|
13
|
+
FunctionParameters,
|
|
14
|
+
getReturnType,
|
|
15
|
+
TypeParameters,
|
|
16
|
+
} from "./FunctionBase.jsx";
|
|
17
|
+
|
|
18
|
+
export interface ArrowFunctionProps extends CallSignatureProps {
|
|
19
|
+
async?: boolean;
|
|
20
|
+
children?: Children;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A TypeScript arrow function expression.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* <FunctionExpression async parameters={[{ name: "a", type: "number" }, { name: "b", type: "number" }]}>
|
|
29
|
+
* return a + b;
|
|
30
|
+
* <FunctionExpression>
|
|
31
|
+
* ```
|
|
32
|
+
* renders to
|
|
33
|
+
* ```ts
|
|
34
|
+
* async (a, b) => { return a + b; }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @remarks
|
|
38
|
+
*
|
|
39
|
+
* Providing parameters and type parameters can be accomplished in one of three
|
|
40
|
+
* ways:
|
|
41
|
+
*
|
|
42
|
+
* 1. As an array of {@link ParameterDescriptor}s or
|
|
43
|
+
* {@link TypeParameterDescriptor}s.
|
|
44
|
+
* 2. As raw content via the `parametersChildren` or `typeParametersChildren`
|
|
45
|
+
* props.
|
|
46
|
+
* 3. As a child of this component via the
|
|
47
|
+
* {@link (ArrowFunction:namespace).Parameters} or
|
|
48
|
+
* {@link (ArrowFunction:namespace).TypeParameters} components.
|
|
49
|
+
*/
|
|
50
|
+
export function ArrowFunction(props: ArrowFunctionProps) {
|
|
51
|
+
const children = childrenArray(() => props.children);
|
|
52
|
+
const typeParametersChildren =
|
|
53
|
+
findKeyedChild(children, TypeParameters.tag) ?? undefined;
|
|
54
|
+
const parametersChildren =
|
|
55
|
+
findKeyedChild(children, FunctionParameters.tag) ?? undefined;
|
|
56
|
+
const bodyChildren = findKeyedChild(children, FunctionBody.tag) ?? undefined;
|
|
57
|
+
const filteredChildren = findUnkeyedChildren(children);
|
|
58
|
+
const returnType = getReturnType(props.returnType, { async: props.async });
|
|
59
|
+
|
|
60
|
+
const sBody = bodyChildren ?? (
|
|
61
|
+
<ArrowFunction.Body>{filteredChildren}</ArrowFunction.Body>
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const asyncKwd = props.async ? "async " : "";
|
|
65
|
+
|
|
66
|
+
const callSignatureProps = getCallSignatureProps(props, {
|
|
67
|
+
parametersChildren,
|
|
68
|
+
typeParametersChildren,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<>
|
|
73
|
+
<Declaration {...props} nameKind="function">
|
|
74
|
+
{asyncKwd}
|
|
75
|
+
<Scope kind="function">
|
|
76
|
+
<CallSignature {...callSignatureProps} returnType={returnType} />
|
|
77
|
+
{" => "}
|
|
78
|
+
{sBody}
|
|
79
|
+
</Scope>
|
|
80
|
+
</Declaration>
|
|
81
|
+
</>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
ArrowFunction.TypeParameters = TypeParameters;
|
|
86
|
+
ArrowFunction.Parameters = FunctionParameters;
|
|
87
|
+
ArrowFunction.Body = FunctionBody;
|
|
@@ -22,6 +22,7 @@ import { BaseDeclarationProps, Declaration } from "./Declaration.jsx";
|
|
|
22
22
|
import { JSDoc } from "./JSDoc.jsx";
|
|
23
23
|
import { JSDocParams } from "./JSDocParam.jsx";
|
|
24
24
|
import { PropertyName } from "./PropertyName.jsx";
|
|
25
|
+
import { TypeRefContext } from "./TypeRefContext.jsx";
|
|
25
26
|
|
|
26
27
|
export interface ClassDeclarationProps extends BaseDeclarationProps {
|
|
27
28
|
extends?: Children;
|
|
@@ -101,6 +102,7 @@ export interface ClassMemberProps {
|
|
|
101
102
|
static?: boolean;
|
|
102
103
|
children?: Children;
|
|
103
104
|
doc?: Children;
|
|
105
|
+
nullish?: boolean;
|
|
104
106
|
}
|
|
105
107
|
|
|
106
108
|
export function ClassMember(props: ClassMemberProps) {
|
|
@@ -128,12 +130,19 @@ export function ClassMember(props: ClassMemberProps) {
|
|
|
128
130
|
: memberScope.instanceMembers!) as TSOutputScope;
|
|
129
131
|
}
|
|
130
132
|
|
|
133
|
+
let tsFlags =
|
|
134
|
+
props.jsPrivate ? TSSymbolFlags.PrivateMember : TSSymbolFlags.None;
|
|
135
|
+
|
|
136
|
+
if (props.nullish) {
|
|
137
|
+
tsFlags |= TSSymbolFlags.Nullish;
|
|
138
|
+
}
|
|
139
|
+
|
|
131
140
|
const sym = createTSSymbol({
|
|
132
141
|
name,
|
|
133
142
|
scope,
|
|
134
143
|
refkey: props.refkey,
|
|
135
144
|
flags,
|
|
136
|
-
tsFlags
|
|
145
|
+
tsFlags,
|
|
137
146
|
});
|
|
138
147
|
|
|
139
148
|
return (
|
|
@@ -155,15 +164,22 @@ export function ClassMember(props: ClassMemberProps) {
|
|
|
155
164
|
|
|
156
165
|
export interface ClassFieldProps extends ClassMemberProps {
|
|
157
166
|
type?: Children;
|
|
167
|
+
optional?: boolean;
|
|
158
168
|
children?: Children;
|
|
159
169
|
}
|
|
160
170
|
|
|
161
171
|
export function ClassField(props: ClassFieldProps) {
|
|
162
|
-
const
|
|
172
|
+
const optionality = props.optional ? "?" : "";
|
|
173
|
+
const typeSection = props.type && (
|
|
174
|
+
<>
|
|
175
|
+
{optionality}: <TypeRefContext>{props.type}</TypeRefContext>
|
|
176
|
+
</>
|
|
177
|
+
);
|
|
163
178
|
const initializerSection = props.children && <> = {props.children}</>;
|
|
179
|
+
const nullish = props.nullish ?? props.optional;
|
|
164
180
|
|
|
165
181
|
return (
|
|
166
|
-
<ClassMember {...props}>
|
|
182
|
+
<ClassMember {...props} nullish={nullish}>
|
|
167
183
|
<PropertyName />
|
|
168
184
|
{typeSection}
|
|
169
185
|
{initializerSection}
|
|
@@ -109,7 +109,7 @@ export function Declaration(props: DeclarationProps) {
|
|
|
109
109
|
|
|
110
110
|
let tsFlags: TSSymbolFlags = TSSymbolFlags.None;
|
|
111
111
|
if (props.kind && props.kind === "type") {
|
|
112
|
-
tsFlags
|
|
112
|
+
tsFlags |= TSSymbolFlags.TypeSymbol;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
sym = createTSSymbol({
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Block,
|
|
3
|
+
code,
|
|
4
|
+
For,
|
|
5
|
+
Indent,
|
|
6
|
+
onCleanup,
|
|
7
|
+
Show,
|
|
8
|
+
taggedComponent,
|
|
9
|
+
} from "@alloy-js/core";
|
|
10
|
+
import type { Children } from "@alloy-js/core/jsx-runtime";
|
|
11
|
+
import { useTSNamePolicy } from "../name-policy.js";
|
|
12
|
+
import type {
|
|
13
|
+
FunctionTypeParameterDescriptor,
|
|
14
|
+
ParameterDescriptor,
|
|
15
|
+
} from "../parameter-descriptor.js";
|
|
16
|
+
import { TypeParameterDescriptor } from "../parameter-descriptor.js";
|
|
17
|
+
import {
|
|
18
|
+
createTSSymbol,
|
|
19
|
+
TSOutputSymbol,
|
|
20
|
+
TSSymbolFlags,
|
|
21
|
+
} from "../symbols/index.js";
|
|
22
|
+
import { TypeRefContext } from "./TypeRefContext.jsx";
|
|
23
|
+
|
|
24
|
+
const functionParametersTag = Symbol();
|
|
25
|
+
const typeParametersTag = Symbol();
|
|
26
|
+
const functionBodyTag = Symbol();
|
|
27
|
+
|
|
28
|
+
export interface FunctionBodyProps {
|
|
29
|
+
readonly children?: Children;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Props for function parameters */
|
|
33
|
+
export interface FunctionParametersProps {
|
|
34
|
+
/** Parameters */
|
|
35
|
+
readonly parameters?: ParameterDescriptor[] | string[];
|
|
36
|
+
/** Jsx Children */
|
|
37
|
+
readonly children?: Children;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Props for a function type(FunctionType or InterfaceMethod) */
|
|
41
|
+
export interface FunctionTypeParametersProps {
|
|
42
|
+
/** Parameters */
|
|
43
|
+
readonly parameters?: ParameterDescriptor[] | string[];
|
|
44
|
+
/** Jsx Children */
|
|
45
|
+
readonly children?: Children;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Props for type parameters */
|
|
49
|
+
export interface TypeParametersProps {
|
|
50
|
+
/** Parameters */
|
|
51
|
+
parameters?: TypeParameterDescriptor[] | string[];
|
|
52
|
+
/** Jsx Children */
|
|
53
|
+
children?: Children;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const FunctionBody = taggedComponent(
|
|
57
|
+
functionBodyTag,
|
|
58
|
+
function Body(props: FunctionBodyProps) {
|
|
59
|
+
return <Block>{props.children}</Block>;
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Represent function parameters(FunctionDeclaration, FunctionExpression, ArrowFunction, etc.)
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* a: string, b: string = "abc", c?: string,
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export const FunctionParameters = taggedComponent(
|
|
72
|
+
functionParametersTag,
|
|
73
|
+
function Parameters(props: FunctionParametersProps) {
|
|
74
|
+
if (props.children) {
|
|
75
|
+
return props.children;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const parameters = normalizeAndDeclareParameters(props.parameters ?? []);
|
|
79
|
+
return (
|
|
80
|
+
<group>
|
|
81
|
+
<Indent softline trailingBreak>
|
|
82
|
+
<For each={parameters} comma line>
|
|
83
|
+
{(param) => parameter(param)}
|
|
84
|
+
</For>
|
|
85
|
+
<ifBreak>,</ifBreak>
|
|
86
|
+
</Indent>
|
|
87
|
+
</group>
|
|
88
|
+
);
|
|
89
|
+
},
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Represent parameters for a function type(FunctionType or InterfaceMethod)
|
|
94
|
+
* This differ from {@link FunctionParameters} in that it doesn't allow default values.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* a: string, b?: string
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export const FunctionTypeParameters = taggedComponent(
|
|
102
|
+
functionParametersTag,
|
|
103
|
+
function Parameters(props: FunctionTypeParametersProps) {
|
|
104
|
+
if (props.children) {
|
|
105
|
+
return props.children;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const parameters = normalizeAndDeclareParameters(props.parameters ?? []);
|
|
109
|
+
return (
|
|
110
|
+
<group>
|
|
111
|
+
<Indent softline trailingBreak>
|
|
112
|
+
<For each={parameters} comma line>
|
|
113
|
+
{(param) => parameter(param)}
|
|
114
|
+
</For>
|
|
115
|
+
<ifBreak>,</ifBreak>
|
|
116
|
+
</Indent>
|
|
117
|
+
</group>
|
|
118
|
+
);
|
|
119
|
+
},
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
function parameter(param: DeclaredParameterDescriptor) {
|
|
123
|
+
return (
|
|
124
|
+
<group>
|
|
125
|
+
{param.symbol.name}
|
|
126
|
+
<Show when={!!param.optional}>?</Show>
|
|
127
|
+
<Show when={!!param.type}>
|
|
128
|
+
<indent>
|
|
129
|
+
: <TypeRefContext>{param.type}</TypeRefContext>
|
|
130
|
+
</indent>
|
|
131
|
+
</Show>
|
|
132
|
+
</group>
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
interface DeclaredParameterDescriptor
|
|
137
|
+
extends Omit<ParameterDescriptor, "name"> {
|
|
138
|
+
symbol: TSOutputSymbol;
|
|
139
|
+
}
|
|
140
|
+
interface DeclaredFunctionTypeParameterDescriptor
|
|
141
|
+
extends Omit<FunctionTypeParameterDescriptor, "name"> {
|
|
142
|
+
symbol: TSOutputSymbol;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
interface DeclaredTypeParameterDescriptor
|
|
146
|
+
extends Omit<TypeParameterDescriptor, "name"> {
|
|
147
|
+
symbol: TSOutputSymbol;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function normalizeAndDeclareParameters(
|
|
151
|
+
parameters: FunctionTypeParameterDescriptor[] | string[],
|
|
152
|
+
flags?: TSSymbolFlags,
|
|
153
|
+
): DeclaredTypeParameterDescriptor[];
|
|
154
|
+
function normalizeAndDeclareParameters(
|
|
155
|
+
parameters: TypeParameterDescriptor[] | string[],
|
|
156
|
+
flags?: TSSymbolFlags,
|
|
157
|
+
): DeclaredFunctionTypeParameterDescriptor[];
|
|
158
|
+
function normalizeAndDeclareParameters(
|
|
159
|
+
parameters: ParameterDescriptor[] | string[],
|
|
160
|
+
flags?: TSSymbolFlags,
|
|
161
|
+
): DeclaredParameterDescriptor[];
|
|
162
|
+
function normalizeAndDeclareParameters(
|
|
163
|
+
parameters: string[],
|
|
164
|
+
flags?: TSSymbolFlags,
|
|
165
|
+
): DeclaredParameterDescriptor[] | DeclaredTypeParameterDescriptor[];
|
|
166
|
+
function normalizeAndDeclareParameters(
|
|
167
|
+
parameters:
|
|
168
|
+
| ParameterDescriptor[]
|
|
169
|
+
| FunctionTypeParameterDescriptor[]
|
|
170
|
+
| TypeParameterDescriptor[]
|
|
171
|
+
| string[],
|
|
172
|
+
flags: TSSymbolFlags = TSSymbolFlags.ParameterSymbol,
|
|
173
|
+
): DeclaredParameterDescriptor[] | DeclaredTypeParameterDescriptor[] {
|
|
174
|
+
const namePolicy = useTSNamePolicy();
|
|
175
|
+
if (parameters.length === 0) {
|
|
176
|
+
return [];
|
|
177
|
+
}
|
|
178
|
+
if (typeof parameters[0] === "string") {
|
|
179
|
+
return (parameters as string[]).map((paramName) => {
|
|
180
|
+
const symbol = createTSSymbol({
|
|
181
|
+
name: namePolicy.getName(
|
|
182
|
+
paramName,
|
|
183
|
+
flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
|
|
184
|
+
),
|
|
185
|
+
tsFlags: flags,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
return { refkeys: symbol.refkeys, symbol };
|
|
189
|
+
});
|
|
190
|
+
} else {
|
|
191
|
+
return (parameters as ParameterDescriptor[]).map((param) => {
|
|
192
|
+
const nullishFlag =
|
|
193
|
+
(param.nullish ?? param.optional) ?
|
|
194
|
+
TSSymbolFlags.Nullish
|
|
195
|
+
: TSSymbolFlags.None;
|
|
196
|
+
|
|
197
|
+
const symbol = createTSSymbol({
|
|
198
|
+
name: namePolicy.getName(
|
|
199
|
+
param.name,
|
|
200
|
+
flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
|
|
201
|
+
),
|
|
202
|
+
refkey: param.refkey,
|
|
203
|
+
tsFlags: flags | nullishFlag,
|
|
204
|
+
metadata: param.metadata,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
return {
|
|
208
|
+
...param,
|
|
209
|
+
symbol,
|
|
210
|
+
};
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Represent type parameters
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts
|
|
220
|
+
* <A, B extends string>
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
export const TypeParameters = taggedComponent(
|
|
224
|
+
typeParametersTag,
|
|
225
|
+
function TypeParameters(props: TypeParametersProps) {
|
|
226
|
+
if (props.children) {
|
|
227
|
+
return props.children;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (!props.parameters) {
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const typeParameters = normalizeAndDeclareParameters(props.parameters);
|
|
235
|
+
|
|
236
|
+
onCleanup(() => {
|
|
237
|
+
for (const param of typeParameters) {
|
|
238
|
+
param.symbol.binder.deleteSymbol(param.symbol);
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
return (
|
|
243
|
+
<>
|
|
244
|
+
{"<"}
|
|
245
|
+
<group>
|
|
246
|
+
<Indent softline>
|
|
247
|
+
<For each={typeParameters} comma line>
|
|
248
|
+
{(param) => typeParameter(param)}
|
|
249
|
+
</For>
|
|
250
|
+
<ifBreak>,</ifBreak>
|
|
251
|
+
</Indent>
|
|
252
|
+
</group>
|
|
253
|
+
{">"}
|
|
254
|
+
</>
|
|
255
|
+
);
|
|
256
|
+
},
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
function typeParameter(param: DeclaredTypeParameterDescriptor) {
|
|
260
|
+
return (
|
|
261
|
+
<group>
|
|
262
|
+
{param.symbol.name}
|
|
263
|
+
<Show when={!!param.extends}>
|
|
264
|
+
{" "}
|
|
265
|
+
extends
|
|
266
|
+
<indent>
|
|
267
|
+
{" "}
|
|
268
|
+
<TypeRefContext>{param.extends}</TypeRefContext>
|
|
269
|
+
</indent>
|
|
270
|
+
</Show>
|
|
271
|
+
</group>
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export function getReturnType(
|
|
276
|
+
returnType: Children | undefined,
|
|
277
|
+
options: { async?: boolean } = { async: false },
|
|
278
|
+
) {
|
|
279
|
+
if (returnType) {
|
|
280
|
+
return options.async ? code`Promise<${returnType}>` : returnType;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
@@ -1,31 +1,22 @@
|
|
|
1
1
|
import {
|
|
2
|
-
Block,
|
|
3
2
|
childrenArray,
|
|
4
|
-
code,
|
|
5
3
|
findKeyedChild,
|
|
6
4
|
findUnkeyedChildren,
|
|
7
|
-
For,
|
|
8
|
-
Indent,
|
|
9
5
|
Name,
|
|
10
6
|
Prose,
|
|
11
7
|
Scope,
|
|
12
8
|
Show,
|
|
13
|
-
taggedComponent,
|
|
14
9
|
} from "@alloy-js/core";
|
|
15
|
-
import { Children
|
|
16
|
-
import { useTSNamePolicy } from "../name-policy.js";
|
|
17
|
-
import {
|
|
18
|
-
ParameterDescriptor,
|
|
19
|
-
TypeParameterDescriptor,
|
|
20
|
-
} from "../parameter-descriptor.js";
|
|
21
|
-
import {
|
|
22
|
-
createTSSymbol,
|
|
23
|
-
TSOutputSymbol,
|
|
24
|
-
TSSymbolFlags,
|
|
25
|
-
} from "../symbols/index.js";
|
|
10
|
+
import { Children } from "@alloy-js/core/jsx-runtime";
|
|
26
11
|
import { getCallSignatureProps } from "../utils.js";
|
|
27
12
|
import { CallSignature, CallSignatureProps } from "./CallSignature.jsx";
|
|
28
13
|
import { BaseDeclarationProps, Declaration } from "./Declaration.js";
|
|
14
|
+
import {
|
|
15
|
+
FunctionBody,
|
|
16
|
+
FunctionParameters,
|
|
17
|
+
getReturnType,
|
|
18
|
+
TypeParameters,
|
|
19
|
+
} from "./FunctionBase.jsx";
|
|
29
20
|
import { JSDoc } from "./JSDoc.jsx";
|
|
30
21
|
import { JSDocParams } from "./JSDocParam.jsx";
|
|
31
22
|
|
|
@@ -36,10 +27,6 @@ export interface FunctionDeclarationProps
|
|
|
36
27
|
children?: Children;
|
|
37
28
|
}
|
|
38
29
|
|
|
39
|
-
export const functionParametersTag = Symbol();
|
|
40
|
-
export const functionTypeParametersTag = Symbol();
|
|
41
|
-
export const functionBodyTag = Symbol();
|
|
42
|
-
|
|
43
30
|
/**
|
|
44
31
|
* A TypeScript function declaration.
|
|
45
32
|
*
|
|
@@ -59,10 +46,10 @@ export const functionBodyTag = Symbol();
|
|
|
59
46
|
export function FunctionDeclaration(props: FunctionDeclarationProps) {
|
|
60
47
|
const children = childrenArray(() => props.children);
|
|
61
48
|
const typeParametersChildren =
|
|
62
|
-
findKeyedChild(children,
|
|
49
|
+
findKeyedChild(children, TypeParameters.tag) ?? undefined;
|
|
63
50
|
const parametersChildren =
|
|
64
|
-
findKeyedChild(children,
|
|
65
|
-
const bodyChildren = findKeyedChild(children,
|
|
51
|
+
findKeyedChild(children, FunctionParameters.tag) ?? undefined;
|
|
52
|
+
const bodyChildren = findKeyedChild(children, FunctionBody.tag) ?? undefined;
|
|
66
53
|
const filteredChildren = findUnkeyedChildren(children);
|
|
67
54
|
const returnType = getReturnType(props.returnType, { async: props.async });
|
|
68
55
|
|
|
@@ -99,175 +86,6 @@ export function FunctionDeclaration(props: FunctionDeclarationProps) {
|
|
|
99
86
|
);
|
|
100
87
|
}
|
|
101
88
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
) {
|
|
106
|
-
if (returnType) {
|
|
107
|
-
return options.async ? code`Promise<${returnType}>` : returnType;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
interface DeclaredParameterDescriptor
|
|
112
|
-
extends Omit<ParameterDescriptor, "name"> {
|
|
113
|
-
symbol: TSOutputSymbol;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
interface DeclaredTypeParameterDescriptor
|
|
117
|
-
extends Omit<TypeParameterDescriptor, "name"> {
|
|
118
|
-
symbol: TSOutputSymbol;
|
|
119
|
-
}
|
|
120
|
-
function normalizeAndDeclareParameters(
|
|
121
|
-
parameters: TypeParameterDescriptor[] | string[],
|
|
122
|
-
flags?: TSSymbolFlags,
|
|
123
|
-
): DeclaredTypeParameterDescriptor[];
|
|
124
|
-
function normalizeAndDeclareParameters(
|
|
125
|
-
parameters: ParameterDescriptor[] | string[],
|
|
126
|
-
flags?: TSSymbolFlags,
|
|
127
|
-
): DeclaredParameterDescriptor[];
|
|
128
|
-
function normalizeAndDeclareParameters(
|
|
129
|
-
parameters: string[],
|
|
130
|
-
flags?: TSSymbolFlags,
|
|
131
|
-
): DeclaredParameterDescriptor[] | DeclaredTypeParameterDescriptor[];
|
|
132
|
-
function normalizeAndDeclareParameters(
|
|
133
|
-
parameters: ParameterDescriptor[] | TypeParameterDescriptor[] | string[],
|
|
134
|
-
flags: TSSymbolFlags = TSSymbolFlags.ParameterSymbol,
|
|
135
|
-
): DeclaredParameterDescriptor[] | DeclaredTypeParameterDescriptor[] {
|
|
136
|
-
const namePolicy = useTSNamePolicy();
|
|
137
|
-
if (parameters.length === 0) {
|
|
138
|
-
return [];
|
|
139
|
-
}
|
|
140
|
-
if (typeof parameters[0] === "string") {
|
|
141
|
-
return (parameters as string[]).map((paramName) => {
|
|
142
|
-
const symbol = createTSSymbol({
|
|
143
|
-
name: namePolicy.getName(
|
|
144
|
-
paramName,
|
|
145
|
-
flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
|
|
146
|
-
),
|
|
147
|
-
tsFlags: flags,
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
return { refkeys: symbol.refkeys, symbol };
|
|
151
|
-
});
|
|
152
|
-
} else {
|
|
153
|
-
return (parameters as ParameterDescriptor[]).map((param) => {
|
|
154
|
-
const symbol = createTSSymbol({
|
|
155
|
-
name: namePolicy.getName(
|
|
156
|
-
param.name,
|
|
157
|
-
flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
|
|
158
|
-
),
|
|
159
|
-
refkey: param.refkey,
|
|
160
|
-
tsFlags: flags,
|
|
161
|
-
metadata: param.metadata,
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
return {
|
|
165
|
-
...param,
|
|
166
|
-
symbol,
|
|
167
|
-
};
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export interface FunctionTypeParametersProps {
|
|
173
|
-
parameters?: ParameterDescriptor[] | string[];
|
|
174
|
-
children?: Children;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
FunctionDeclaration.TypeParameters = taggedComponent(
|
|
178
|
-
functionTypeParametersTag,
|
|
179
|
-
function TypeParameters(props: FunctionTypeParametersProps) {
|
|
180
|
-
if (props.children) {
|
|
181
|
-
return props.children;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (!props.parameters) {
|
|
185
|
-
return undefined;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const typeParameters = normalizeAndDeclareParameters(props.parameters);
|
|
189
|
-
|
|
190
|
-
onCleanup(() => {
|
|
191
|
-
for (const param of typeParameters) {
|
|
192
|
-
param.symbol.binder.deleteSymbol(param.symbol);
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
return (
|
|
197
|
-
<>
|
|
198
|
-
{"<"}
|
|
199
|
-
<group>
|
|
200
|
-
<Indent softline>
|
|
201
|
-
<For each={typeParameters} comma line>
|
|
202
|
-
{(param) => typeParameter(param)}
|
|
203
|
-
</For>
|
|
204
|
-
<ifBreak>,</ifBreak>
|
|
205
|
-
</Indent>
|
|
206
|
-
</group>
|
|
207
|
-
{">"}
|
|
208
|
-
</>
|
|
209
|
-
);
|
|
210
|
-
},
|
|
211
|
-
);
|
|
212
|
-
|
|
213
|
-
function typeParameter(param: DeclaredTypeParameterDescriptor) {
|
|
214
|
-
return (
|
|
215
|
-
<group>
|
|
216
|
-
{param.symbol.name}
|
|
217
|
-
<Show when={!!param.extends}>
|
|
218
|
-
{" "}
|
|
219
|
-
extends
|
|
220
|
-
<indent> {param.extends}</indent>
|
|
221
|
-
</Show>
|
|
222
|
-
</group>
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
export interface FunctionParametersProps {
|
|
227
|
-
parameters?: ParameterDescriptor[] | string[];
|
|
228
|
-
children?: Children;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
FunctionDeclaration.Parameters = taggedComponent(
|
|
232
|
-
functionParametersTag,
|
|
233
|
-
function Parameters(props: FunctionParametersProps) {
|
|
234
|
-
if (props.children) {
|
|
235
|
-
return props.children;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const parameters = normalizeAndDeclareParameters(props.parameters ?? []);
|
|
239
|
-
return (
|
|
240
|
-
<group>
|
|
241
|
-
<Indent softline trailingBreak>
|
|
242
|
-
<For each={parameters} comma line>
|
|
243
|
-
{(param) => parameter(param)}
|
|
244
|
-
</For>
|
|
245
|
-
<ifBreak>,</ifBreak>
|
|
246
|
-
</Indent>
|
|
247
|
-
</group>
|
|
248
|
-
);
|
|
249
|
-
},
|
|
250
|
-
);
|
|
251
|
-
|
|
252
|
-
function parameter(param: DeclaredParameterDescriptor) {
|
|
253
|
-
return (
|
|
254
|
-
<group>
|
|
255
|
-
{param.symbol.name}
|
|
256
|
-
<Show when={!!param.optional}>?</Show>
|
|
257
|
-
<Show when={!!param.type}>
|
|
258
|
-
<indent>: {param.type}</indent>
|
|
259
|
-
</Show>
|
|
260
|
-
</group>
|
|
261
|
-
);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
export interface FunctionBodyProps {
|
|
265
|
-
children?: Children;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
FunctionDeclaration.Body = taggedComponent(
|
|
269
|
-
functionBodyTag,
|
|
270
|
-
function Body(props: FunctionBodyProps) {
|
|
271
|
-
return <Block>{props.children}</Block>;
|
|
272
|
-
},
|
|
273
|
-
);
|
|
89
|
+
FunctionDeclaration.TypeParameters = TypeParameters;
|
|
90
|
+
FunctionDeclaration.Parameters = FunctionParameters;
|
|
91
|
+
FunctionDeclaration.Body = FunctionBody;
|