@alloy-js/typescript 0.10.0 → 0.11.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 +15 -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.js +2 -6
- 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 +207 -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 +39 -0
- package/dist/src/components/FunctionType.d.ts.map +1 -0
- package/dist/src/components/FunctionType.js +73 -0
- package/dist/src/components/IfStatement.js +2 -4
- package/dist/src/components/ImportStatement.js +1 -2
- package/dist/src/components/Interface.d.ts.map +1 -1
- package/dist/src/components/Interface.js +32 -11
- 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 +2 -4
- 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 +30 -0
- package/dist/src/components/MemberDeclaration.d.ts.map +1 -0
- package/dist/src/components/MemberDeclaration.js +29 -0
- package/dist/src/components/MemberExpression.js +2 -5
- 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.js +1 -2
- 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.js +2 -6
- package/dist/src/components/ValueExpression.js +1 -2
- package/dist/src/components/VarDeclaration.js +2 -5
- package/dist/src/components/index.d.ts +6 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +6 -1
- package/dist/src/components/stc/index.d.ts +7 -0
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +8 -2
- package/dist/src/context/private-scope.js +1 -2
- 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 +27 -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.js +2 -3
- 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.js +1 -2
- package/dist/src/symbols/ts-output-symbol.js +1 -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.js +434 -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 +359 -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 +12 -17
- package/src/components/ArrowFunction.tsx +87 -0
- package/src/components/Declaration.tsx +1 -1
- package/src/components/FunctionBase.tsx +271 -0
- package/src/components/FunctionDeclaration.tsx +13 -195
- package/src/components/FunctionExpression.tsx +86 -0
- package/src/components/FunctionType.tsx +88 -0
- package/src/components/Interface.tsx +33 -5
- package/src/components/InterfaceMethod.tsx +96 -0
- package/src/components/MemberDeclaration.tsx +70 -0
- package/src/components/index.ts +10 -0
- package/src/components/stc/index.ts +7 -0
- package/src/parameter-descriptor.ts +29 -18
- package/src/symbols/reference.ts +4 -1
- package/temp/api.json +2605 -680
- package/test/arrow-function.test.tsx +188 -0
- package/test/class.test.tsx +1 -2
- package/test/function-call-expression.test.tsx +1 -2
- 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/interface.test.tsx +256 -6
- 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,228 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { refkey, StatementList } from "@alloy-js/core";
|
|
3
|
+
import { d } from "@alloy-js/core/testing";
|
|
4
|
+
import { describe, expect, it } from "vitest";
|
|
5
|
+
import { FunctionDeclaration, VarDeclaration } from "../src/index.js";
|
|
6
|
+
import { toSourceText } from "./utils.js";
|
|
7
|
+
it("works", () => {
|
|
8
|
+
expect(toSourceText(_$createComponent(FunctionDeclaration, {
|
|
9
|
+
name: "foo"
|
|
10
|
+
}))).toBe(d`
|
|
11
|
+
function foo() {}
|
|
12
|
+
`);
|
|
13
|
+
});
|
|
14
|
+
it("can be exported", () => {
|
|
15
|
+
expect(toSourceText(_$createComponent(FunctionDeclaration, {
|
|
16
|
+
"export": true,
|
|
17
|
+
name: "foo"
|
|
18
|
+
}))).toBe(d`
|
|
19
|
+
export function foo() {}
|
|
20
|
+
`);
|
|
21
|
+
});
|
|
22
|
+
it("can be a default export", () => {
|
|
23
|
+
expect(toSourceText(_$createComponent(FunctionDeclaration, {
|
|
24
|
+
"export": true,
|
|
25
|
+
"default": true,
|
|
26
|
+
name: "foo"
|
|
27
|
+
}))).toBe(d`
|
|
28
|
+
export default function foo() {}
|
|
29
|
+
`);
|
|
30
|
+
});
|
|
31
|
+
it("can be an async function", () => {
|
|
32
|
+
expect(toSourceText(_$createComponent(FunctionDeclaration, {
|
|
33
|
+
async: true,
|
|
34
|
+
"export": true,
|
|
35
|
+
name: "foo"
|
|
36
|
+
}))).toBe(d`
|
|
37
|
+
export async function foo() {}
|
|
38
|
+
`);
|
|
39
|
+
});
|
|
40
|
+
it("can be an async function with returnType", () => {
|
|
41
|
+
expect(toSourceText(_$createComponent(FunctionDeclaration, {
|
|
42
|
+
async: true,
|
|
43
|
+
"export": true,
|
|
44
|
+
name: "foo",
|
|
45
|
+
returnType: "Foo"
|
|
46
|
+
}))).toBe(d`
|
|
47
|
+
export async function foo(): Promise<Foo> {}
|
|
48
|
+
`);
|
|
49
|
+
});
|
|
50
|
+
it("can be an async function with returnType element", () => {
|
|
51
|
+
function Foo(_props) {
|
|
52
|
+
return ["Foo"];
|
|
53
|
+
}
|
|
54
|
+
expect(toSourceText(_$createComponent(FunctionDeclaration, {
|
|
55
|
+
async: true,
|
|
56
|
+
"export": true,
|
|
57
|
+
name: "foo",
|
|
58
|
+
get returnType() {
|
|
59
|
+
return _$createComponent(Foo, {});
|
|
60
|
+
}
|
|
61
|
+
}))).toBe(d`
|
|
62
|
+
export async function foo(): Promise<Foo> {}
|
|
63
|
+
`);
|
|
64
|
+
});
|
|
65
|
+
it("supports parameters by element", () => {
|
|
66
|
+
const decl = _$createComponent(FunctionDeclaration, {
|
|
67
|
+
name: "foo",
|
|
68
|
+
get children() {
|
|
69
|
+
return ["return a + b;", _$createComponent(FunctionDeclaration.Parameters, {
|
|
70
|
+
children: "a, b"
|
|
71
|
+
})];
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
expect(toSourceText(decl)).toBe(d`
|
|
75
|
+
function foo(a, b) {
|
|
76
|
+
return a + b;
|
|
77
|
+
}
|
|
78
|
+
`);
|
|
79
|
+
});
|
|
80
|
+
it("supports type parameters by descriptor object", () => {
|
|
81
|
+
const decl = _$createComponent(FunctionDeclaration, {
|
|
82
|
+
name: "foo",
|
|
83
|
+
typeParameters: [{
|
|
84
|
+
name: "a",
|
|
85
|
+
extends: "any"
|
|
86
|
+
}, {
|
|
87
|
+
name: "b",
|
|
88
|
+
extends: "any"
|
|
89
|
+
}]
|
|
90
|
+
});
|
|
91
|
+
expect(toSourceText(decl)).toBe(d`
|
|
92
|
+
function foo<a extends any, b extends any>() {}
|
|
93
|
+
`);
|
|
94
|
+
});
|
|
95
|
+
it("supports type parameters by descriptor array", () => {
|
|
96
|
+
const decl = _$createComponent(FunctionDeclaration, {
|
|
97
|
+
name: "foo",
|
|
98
|
+
typeParameters: ["a", "b"]
|
|
99
|
+
});
|
|
100
|
+
expect(toSourceText(decl)).toBe(d`
|
|
101
|
+
function foo<a, b>() {}
|
|
102
|
+
`);
|
|
103
|
+
});
|
|
104
|
+
it("supports type parameters by element", () => {
|
|
105
|
+
const decl = _$createComponent(FunctionDeclaration, {
|
|
106
|
+
name: "foo",
|
|
107
|
+
get children() {
|
|
108
|
+
return _$createComponent(FunctionDeclaration.TypeParameters, {
|
|
109
|
+
children: "a, b"
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
expect(toSourceText(decl)).toBe(d`
|
|
114
|
+
function foo<a, b>() {}
|
|
115
|
+
`);
|
|
116
|
+
});
|
|
117
|
+
describe("symbols", () => {
|
|
118
|
+
it("creates a nested scope", () => {
|
|
119
|
+
const innerRefkey = refkey();
|
|
120
|
+
const outerRefkey = refkey();
|
|
121
|
+
const decl = _$createComponent(StatementList, {
|
|
122
|
+
get children() {
|
|
123
|
+
return [_$createComponent(FunctionDeclaration, {
|
|
124
|
+
name: "foo",
|
|
125
|
+
get children() {
|
|
126
|
+
return _$createComponent(StatementList, {
|
|
127
|
+
get children() {
|
|
128
|
+
return [innerRefkey, _$createComponent(VarDeclaration, {
|
|
129
|
+
name: "refme",
|
|
130
|
+
refkey: innerRefkey,
|
|
131
|
+
children: "1"
|
|
132
|
+
})];
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}), _$createComponent(VarDeclaration, {
|
|
137
|
+
name: "refme",
|
|
138
|
+
refkey: outerRefkey,
|
|
139
|
+
children: "2"
|
|
140
|
+
}), outerRefkey];
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
expect(toSourceText(decl)).toBe(d`
|
|
144
|
+
function foo() {
|
|
145
|
+
refme;
|
|
146
|
+
const refme = 1;
|
|
147
|
+
};
|
|
148
|
+
const refme = 2;
|
|
149
|
+
refme;
|
|
150
|
+
`);
|
|
151
|
+
});
|
|
152
|
+
it("throws an error when trying to access a symbol in a nested function scope", () => {
|
|
153
|
+
const innerRefkey = refkey();
|
|
154
|
+
const decl = [_$createComponent(FunctionDeclaration, {
|
|
155
|
+
name: "foo",
|
|
156
|
+
get children() {
|
|
157
|
+
return _$createComponent(VarDeclaration, {
|
|
158
|
+
name: "refme",
|
|
159
|
+
refkey: innerRefkey,
|
|
160
|
+
children: "1"
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}), ";", innerRefkey];
|
|
164
|
+
expect(() => toSourceText(decl)).toThrow(/Cannot reference a symbol/);
|
|
165
|
+
});
|
|
166
|
+
it("creates symbols for parameters", () => {
|
|
167
|
+
const rk = refkey();
|
|
168
|
+
const decl = [_$createComponent(FunctionDeclaration, {
|
|
169
|
+
name: "foo",
|
|
170
|
+
parameters: [{
|
|
171
|
+
name: "sym",
|
|
172
|
+
type: "any",
|
|
173
|
+
refkey: rk
|
|
174
|
+
}],
|
|
175
|
+
get children() {
|
|
176
|
+
return _$createComponent(FunctionDeclaration, {
|
|
177
|
+
name: "bar",
|
|
178
|
+
children: rk
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
})];
|
|
182
|
+
expect(toSourceText(decl)).toBe(d`
|
|
183
|
+
function foo(sym: any) {
|
|
184
|
+
function bar() {
|
|
185
|
+
sym
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
`);
|
|
189
|
+
});
|
|
190
|
+
it("creates symbols for parameters and addresses conflicts", () => {
|
|
191
|
+
const decl = [_$createComponent(FunctionDeclaration, {
|
|
192
|
+
name: "foo",
|
|
193
|
+
parameters: [{
|
|
194
|
+
name: "conflict",
|
|
195
|
+
type: "any"
|
|
196
|
+
}],
|
|
197
|
+
get children() {
|
|
198
|
+
return [_$createComponent(VarDeclaration, {
|
|
199
|
+
name: "conflict",
|
|
200
|
+
children: "1"
|
|
201
|
+
}), ";"];
|
|
202
|
+
}
|
|
203
|
+
})];
|
|
204
|
+
expect(toSourceText(decl)).toBe(d`
|
|
205
|
+
function foo(conflict: any) {
|
|
206
|
+
const conflict_2 = 1;
|
|
207
|
+
}
|
|
208
|
+
`);
|
|
209
|
+
});
|
|
210
|
+
it("create optional parameters", () => {
|
|
211
|
+
const paramDesc = {
|
|
212
|
+
name: "foo",
|
|
213
|
+
refkey: refkey(),
|
|
214
|
+
type: "any",
|
|
215
|
+
optional: true
|
|
216
|
+
};
|
|
217
|
+
const decl = [_$createComponent(FunctionDeclaration, {
|
|
218
|
+
name: "foo",
|
|
219
|
+
parameters: [paramDesc],
|
|
220
|
+
children: "console.log(foo);"
|
|
221
|
+
})];
|
|
222
|
+
expect(toSourceText(decl)).toBe(d`
|
|
223
|
+
function foo(foo?: any) {
|
|
224
|
+
console.log(foo);
|
|
225
|
+
}
|
|
226
|
+
`);
|
|
227
|
+
});
|
|
228
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-expression.test.d.ts","sourceRoot":"","sources":["../../test/function-expression.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { refkey, StatementList } from "@alloy-js/core";
|
|
3
|
+
import { d } from "@alloy-js/core/testing";
|
|
4
|
+
import { describe, expect, it } from "vitest";
|
|
5
|
+
import { FunctionExpression } from "../src/components/FunctionExpression.js";
|
|
6
|
+
import { VarDeclaration } from "../src/index.js";
|
|
7
|
+
import { toSourceText } from "./utils.js";
|
|
8
|
+
it("create basic function", () => {
|
|
9
|
+
expect(toSourceText(_$createComponent(FunctionExpression, {}))).toBe(d`
|
|
10
|
+
function () {}
|
|
11
|
+
`);
|
|
12
|
+
});
|
|
13
|
+
it("can be an async function", () => {
|
|
14
|
+
expect(toSourceText(_$createComponent(FunctionExpression, {
|
|
15
|
+
async: true
|
|
16
|
+
}))).toBe(d`
|
|
17
|
+
async function () {}
|
|
18
|
+
`);
|
|
19
|
+
});
|
|
20
|
+
it("can be an async function with returnType", () => {
|
|
21
|
+
expect(toSourceText(_$createComponent(FunctionExpression, {
|
|
22
|
+
async: true,
|
|
23
|
+
returnType: "Foo"
|
|
24
|
+
}))).toBe(d`
|
|
25
|
+
async function (): Promise<Foo> {}
|
|
26
|
+
`);
|
|
27
|
+
});
|
|
28
|
+
it("can be an async function with returnType element", () => {
|
|
29
|
+
function Foo(_props) {
|
|
30
|
+
return ["Foo"];
|
|
31
|
+
}
|
|
32
|
+
expect(toSourceText(_$createComponent(FunctionExpression, {
|
|
33
|
+
async: true,
|
|
34
|
+
get returnType() {
|
|
35
|
+
return _$createComponent(Foo, {});
|
|
36
|
+
}
|
|
37
|
+
}))).toBe(d`
|
|
38
|
+
async function (): Promise<Foo> {}
|
|
39
|
+
`);
|
|
40
|
+
});
|
|
41
|
+
it("supports parameters by element", () => {
|
|
42
|
+
const decl = _$createComponent(FunctionExpression, {
|
|
43
|
+
get children() {
|
|
44
|
+
return ["return a + b;", _$createComponent(FunctionExpression.Parameters, {
|
|
45
|
+
children: "a, b"
|
|
46
|
+
})];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
expect(toSourceText(decl)).toBe(d`
|
|
50
|
+
function (a, b) {
|
|
51
|
+
return a + b;
|
|
52
|
+
}
|
|
53
|
+
`);
|
|
54
|
+
});
|
|
55
|
+
it("supports type parameters by descriptor object", () => {
|
|
56
|
+
const decl = _$createComponent(FunctionExpression, {
|
|
57
|
+
typeParameters: [{
|
|
58
|
+
name: "a",
|
|
59
|
+
extends: "any"
|
|
60
|
+
}, {
|
|
61
|
+
name: "b",
|
|
62
|
+
extends: "any"
|
|
63
|
+
}]
|
|
64
|
+
});
|
|
65
|
+
expect(toSourceText(decl)).toBe(d`
|
|
66
|
+
function <a extends any, b extends any>() {}
|
|
67
|
+
`);
|
|
68
|
+
});
|
|
69
|
+
it("supports type parameters by descriptor array", () => {
|
|
70
|
+
const decl = _$createComponent(FunctionExpression, {
|
|
71
|
+
typeParameters: ["a", "b"]
|
|
72
|
+
});
|
|
73
|
+
expect(toSourceText(decl)).toBe(d`
|
|
74
|
+
function <a, b>() {}
|
|
75
|
+
`);
|
|
76
|
+
});
|
|
77
|
+
it("supports type parameters by element", () => {
|
|
78
|
+
const decl = _$createComponent(FunctionExpression, {
|
|
79
|
+
get children() {
|
|
80
|
+
return _$createComponent(FunctionExpression.TypeParameters, {
|
|
81
|
+
children: "a, b"
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
expect(toSourceText(decl)).toBe(d`
|
|
86
|
+
function <a, b>() {}
|
|
87
|
+
`);
|
|
88
|
+
});
|
|
89
|
+
describe("symbols", () => {
|
|
90
|
+
it("creates a nested scope", () => {
|
|
91
|
+
const innerRefkey = refkey();
|
|
92
|
+
const outerRefkey = refkey();
|
|
93
|
+
const decl = _$createComponent(StatementList, {
|
|
94
|
+
get children() {
|
|
95
|
+
return [_$createComponent(FunctionExpression, {
|
|
96
|
+
get children() {
|
|
97
|
+
return _$createComponent(StatementList, {
|
|
98
|
+
get children() {
|
|
99
|
+
return [innerRefkey, _$createComponent(VarDeclaration, {
|
|
100
|
+
name: "refme",
|
|
101
|
+
refkey: innerRefkey,
|
|
102
|
+
children: "1"
|
|
103
|
+
})];
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}), _$createComponent(VarDeclaration, {
|
|
108
|
+
name: "refme",
|
|
109
|
+
refkey: outerRefkey,
|
|
110
|
+
children: "2"
|
|
111
|
+
}), outerRefkey];
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
expect(toSourceText(decl)).toBe(d`
|
|
115
|
+
function () {
|
|
116
|
+
refme;
|
|
117
|
+
const refme = 1;
|
|
118
|
+
};
|
|
119
|
+
const refme = 2;
|
|
120
|
+
refme;
|
|
121
|
+
`);
|
|
122
|
+
});
|
|
123
|
+
it("throws an error when trying to access a symbol in a nested function scope", () => {
|
|
124
|
+
const innerRefkey = refkey();
|
|
125
|
+
const decl = [_$createComponent(FunctionExpression, {
|
|
126
|
+
get children() {
|
|
127
|
+
return _$createComponent(VarDeclaration, {
|
|
128
|
+
name: "refme",
|
|
129
|
+
refkey: innerRefkey,
|
|
130
|
+
children: "1"
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}), ";", innerRefkey];
|
|
134
|
+
expect(() => toSourceText(decl)).toThrow(/Cannot reference a symbol/);
|
|
135
|
+
});
|
|
136
|
+
it("creates symbols for parameters", () => {
|
|
137
|
+
const rk = refkey();
|
|
138
|
+
const decl = [_$createComponent(FunctionExpression, {
|
|
139
|
+
parameters: [{
|
|
140
|
+
name: "sym",
|
|
141
|
+
type: "any",
|
|
142
|
+
refkey: rk
|
|
143
|
+
}],
|
|
144
|
+
get children() {
|
|
145
|
+
return _$createComponent(FunctionExpression, {
|
|
146
|
+
children: rk
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
})];
|
|
150
|
+
expect(toSourceText(decl)).toBe(d`
|
|
151
|
+
function (sym: any) {
|
|
152
|
+
function () {
|
|
153
|
+
sym
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
`);
|
|
157
|
+
});
|
|
158
|
+
it("creates symbols for parameters and addresses conflicts", () => {
|
|
159
|
+
const decl = [_$createComponent(FunctionExpression, {
|
|
160
|
+
parameters: [{
|
|
161
|
+
name: "conflict",
|
|
162
|
+
type: "any"
|
|
163
|
+
}],
|
|
164
|
+
get children() {
|
|
165
|
+
return [_$createComponent(VarDeclaration, {
|
|
166
|
+
name: "conflict",
|
|
167
|
+
children: "1"
|
|
168
|
+
}), ";"];
|
|
169
|
+
}
|
|
170
|
+
})];
|
|
171
|
+
expect(toSourceText(decl)).toBe(d`
|
|
172
|
+
function (conflict: any) {
|
|
173
|
+
const conflict_2 = 1;
|
|
174
|
+
}
|
|
175
|
+
`);
|
|
176
|
+
});
|
|
177
|
+
it("create optional parameters", () => {
|
|
178
|
+
const paramDesc = {
|
|
179
|
+
name: "foo",
|
|
180
|
+
refkey: refkey(),
|
|
181
|
+
type: "any",
|
|
182
|
+
optional: true
|
|
183
|
+
};
|
|
184
|
+
const decl = [_$createComponent(FunctionExpression, {
|
|
185
|
+
parameters: [paramDesc],
|
|
186
|
+
children: "console.log(foo);"
|
|
187
|
+
})];
|
|
188
|
+
expect(toSourceText(decl)).toBe(d`
|
|
189
|
+
function (foo?: any) {
|
|
190
|
+
console.log(foo);
|
|
191
|
+
}
|
|
192
|
+
`);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-type.test.d.ts","sourceRoot":"","sources":["../../test/function-type.test.tsx"],"names":[],"mappings":"AACA,OAAO,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { refkey } from "@alloy-js/core";
|
|
3
|
+
import "@alloy-js/core/testing";
|
|
4
|
+
import { d } from "@alloy-js/core/testing";
|
|
5
|
+
import { describe, expect, it } from "vitest";
|
|
6
|
+
import { FunctionType } from "../src/components/FunctionType.js";
|
|
7
|
+
import * as ts from "../src/components/index.js";
|
|
8
|
+
import { toSourceText } from "./utils.js";
|
|
9
|
+
it("render basic", () => {
|
|
10
|
+
expect(toSourceText(_$createComponent(FunctionType, {}))).toBe(d`
|
|
11
|
+
() => void
|
|
12
|
+
`);
|
|
13
|
+
});
|
|
14
|
+
it("render in interface", () => {
|
|
15
|
+
expect(toSourceText(_$createComponent(ts.InterfaceDeclaration, {
|
|
16
|
+
name: "Foo",
|
|
17
|
+
get children() {
|
|
18
|
+
return _$createComponent(ts.InterfaceMember, {
|
|
19
|
+
name: "foo",
|
|
20
|
+
get type() {
|
|
21
|
+
return _$createComponent(FunctionType, {});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}))).toBe(d`
|
|
26
|
+
interface Foo {
|
|
27
|
+
foo: () => void
|
|
28
|
+
}
|
|
29
|
+
`);
|
|
30
|
+
});
|
|
31
|
+
describe("marking it as async", () => {
|
|
32
|
+
it("no return type change to Promise<void>", () => {
|
|
33
|
+
expect(toSourceText(_$createComponent(FunctionType, {
|
|
34
|
+
async: true
|
|
35
|
+
}))).toBe(d`
|
|
36
|
+
() => Promise<void>
|
|
37
|
+
`);
|
|
38
|
+
});
|
|
39
|
+
it("explicit returnType change to Promise<T>", () => {
|
|
40
|
+
expect(toSourceText(_$createComponent(FunctionType, {
|
|
41
|
+
async: true,
|
|
42
|
+
returnType: "Foo"
|
|
43
|
+
}))).toBe(d`
|
|
44
|
+
() => Promise<Foo>
|
|
45
|
+
`);
|
|
46
|
+
});
|
|
47
|
+
it("component returnType change to Promise<T>", () => {
|
|
48
|
+
function Foo(_props) {
|
|
49
|
+
return ["Foo"];
|
|
50
|
+
}
|
|
51
|
+
expect(toSourceText(_$createComponent(FunctionType, {
|
|
52
|
+
async: true,
|
|
53
|
+
get returnType() {
|
|
54
|
+
return _$createComponent(Foo, {});
|
|
55
|
+
}
|
|
56
|
+
}))).toBe(d`
|
|
57
|
+
() => Promise<Foo>
|
|
58
|
+
`);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
it("supports parameters by element", () => {
|
|
62
|
+
const decl = _$createComponent(FunctionType, {
|
|
63
|
+
get children() {
|
|
64
|
+
return _$createComponent(FunctionType.Parameters, {
|
|
65
|
+
children: "a, b"
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
expect(toSourceText(decl)).toBe(d`
|
|
70
|
+
(a, b) => void
|
|
71
|
+
`);
|
|
72
|
+
});
|
|
73
|
+
it("supports type parameters by descriptor object", () => {
|
|
74
|
+
const decl = _$createComponent(FunctionType, {
|
|
75
|
+
typeParameters: [{
|
|
76
|
+
name: "a",
|
|
77
|
+
extends: "any"
|
|
78
|
+
}, {
|
|
79
|
+
name: "b",
|
|
80
|
+
extends: "any"
|
|
81
|
+
}]
|
|
82
|
+
});
|
|
83
|
+
expect(toSourceText(decl)).toBe(d`
|
|
84
|
+
<a extends any, b extends any>() => void
|
|
85
|
+
`);
|
|
86
|
+
});
|
|
87
|
+
it("supports type parameters by descriptor array", () => {
|
|
88
|
+
const decl = _$createComponent(FunctionType, {
|
|
89
|
+
typeParameters: ["a", "b"]
|
|
90
|
+
});
|
|
91
|
+
expect(toSourceText(decl)).toBe(d`
|
|
92
|
+
<a, b>() => void
|
|
93
|
+
`);
|
|
94
|
+
});
|
|
95
|
+
it("supports type parameters by element", () => {
|
|
96
|
+
const decl = _$createComponent(FunctionType, {
|
|
97
|
+
get children() {
|
|
98
|
+
return _$createComponent(FunctionType.TypeParameters, {
|
|
99
|
+
children: "a, b"
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
expect(toSourceText(decl)).toBe(d`
|
|
104
|
+
<a, b>() => void
|
|
105
|
+
`);
|
|
106
|
+
});
|
|
107
|
+
describe("symbols", () => {
|
|
108
|
+
it("create optional parameters", () => {
|
|
109
|
+
const paramDesc = {
|
|
110
|
+
name: "foo",
|
|
111
|
+
refkey: refkey(),
|
|
112
|
+
type: "any",
|
|
113
|
+
optional: true
|
|
114
|
+
};
|
|
115
|
+
const decl = [_$createComponent(FunctionType, {
|
|
116
|
+
parameters: [paramDesc]
|
|
117
|
+
})];
|
|
118
|
+
expect(toSourceText(decl)).toBe(d`
|
|
119
|
+
(foo?: any) => void
|
|
120
|
+
`);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { d } from "@alloy-js/core/testing";
|
|
3
|
+
import { expect, it } from "vitest";
|
|
4
|
+
import { ElseClause, ElseIfClause, IfStatement } from "../src/components/IfStatement.js";
|
|
5
|
+
import { toSourceText } from "./utils.js";
|
|
6
|
+
it("works with blocks", () => {
|
|
7
|
+
const text = toSourceText([_$createComponent(IfStatement, {
|
|
8
|
+
condition: "x === 1",
|
|
9
|
+
children: "// do thing"
|
|
10
|
+
}), _$createComponent(ElseIfClause, {
|
|
11
|
+
condition: "x === 2",
|
|
12
|
+
children: "// do another thing"
|
|
13
|
+
}), _$createComponent(ElseClause, {
|
|
14
|
+
children: "// do default thing"
|
|
15
|
+
})]);
|
|
16
|
+
expect(text).toBe(d`
|
|
17
|
+
if (x === 1) {
|
|
18
|
+
// do thing
|
|
19
|
+
} else if (x === 2) {
|
|
20
|
+
// do another thing
|
|
21
|
+
} else {
|
|
22
|
+
// do default thing
|
|
23
|
+
}
|
|
24
|
+
`);
|
|
25
|
+
});
|