@alloy-js/typescript 0.16.0 → 0.18.0-dev.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/dist/src/builtins/node.d.ts +11 -42
- package/dist/src/builtins/node.d.ts.map +1 -1
- package/dist/src/builtins/node.js +3 -0
- package/dist/src/components/ArrowFunction.d.ts +1 -1
- package/dist/src/components/ArrowFunction.d.ts.map +1 -1
- package/dist/src/components/CallSignature.d.ts +1 -1
- package/dist/src/components/CallSignature.d.ts.map +1 -1
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
- package/dist/src/components/ClassDeclaration.js +5 -7
- package/dist/src/components/Declaration.d.ts.map +1 -1
- package/dist/src/components/Declaration.js +3 -4
- package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
- package/dist/src/components/EnumDeclaration.js +3 -4
- package/dist/src/components/EnumMember.d.ts.map +1 -1
- package/dist/src/components/EnumMember.js +3 -4
- package/dist/src/components/ExportStatement.d.ts +1 -1
- package/dist/src/components/ExportStatement.d.ts.map +1 -1
- package/dist/src/components/FunctionBase.d.ts +1 -1
- package/dist/src/components/FunctionBase.d.ts.map +1 -1
- package/dist/src/components/FunctionBase.js +15 -10
- package/dist/src/components/FunctionDeclaration.d.ts +1 -1
- package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
- package/dist/src/components/FunctionExpression.d.ts +1 -1
- package/dist/src/components/FunctionExpression.d.ts.map +1 -1
- package/dist/src/components/FunctionType.d.ts +1 -1
- package/dist/src/components/FunctionType.d.ts.map +1 -1
- package/dist/src/components/IfStatement.d.ts +1 -1
- package/dist/src/components/IfStatement.d.ts.map +1 -1
- package/dist/src/components/ImportStatement.d.ts.map +1 -1
- package/dist/src/components/Interface.js +1 -1
- package/dist/src/components/InterfaceMethod.d.ts +1 -2
- package/dist/src/components/InterfaceMethod.d.ts.map +1 -1
- package/dist/src/components/JSDoc.d.ts +1 -1
- package/dist/src/components/JSDoc.d.ts.map +1 -1
- package/dist/src/components/JSDocComment.d.ts +1 -1
- package/dist/src/components/JSDocComment.d.ts.map +1 -1
- package/dist/src/components/JSDocExample.d.ts +1 -1
- package/dist/src/components/JSDocExample.d.ts.map +1 -1
- package/dist/src/components/JSDocParam.d.ts +1 -1
- package/dist/src/components/JSDocParam.d.ts.map +1 -1
- package/dist/src/components/MemberDeclaration.d.ts.map +1 -1
- package/dist/src/components/MemberDeclaration.js +4 -4
- package/dist/src/components/MemberExpression.d.ts +6 -1
- package/dist/src/components/MemberExpression.d.ts.map +1 -1
- package/dist/src/components/MemberExpression.js +38 -14
- package/dist/src/components/NewExpression.d.ts +5 -0
- package/dist/src/components/NewExpression.d.ts.map +1 -0
- package/dist/src/components/NewExpression.js +12 -0
- package/dist/src/components/ObjectExpression.d.ts.map +1 -1
- package/dist/src/components/ObjectExpression.js +17 -18
- package/dist/src/components/PackageDirectory.d.ts.map +1 -1
- package/dist/src/components/PackageDirectory.js +3 -3
- package/dist/src/components/Reference.d.ts.map +1 -1
- package/dist/src/components/Reference.js +5 -1
- package/dist/src/components/SingleLineCommentBlock.d.ts +1 -1
- package/dist/src/components/SingleLineCommentBlock.d.ts.map +1 -1
- package/dist/src/components/SourceFile.d.ts +1 -2
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +3 -3
- package/dist/src/components/SwitchStatement.d.ts +1 -1
- package/dist/src/components/SwitchStatement.d.ts.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/VarDeclaration.d.ts.map +1 -1
- package/dist/src/components/VarDeclaration.js +30 -10
- 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/stc/index.d.ts +45 -43
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +46 -44
- package/dist/src/create-package.d.ts +63 -19
- package/dist/src/create-package.d.ts.map +1 -1
- package/dist/src/create-package.js +153 -15
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -0
- package/dist/src/parameter-descriptor.d.ts +1 -1
- package/dist/src/parameter-descriptor.d.ts.map +1 -1
- package/dist/src/source-directory-data.d.ts.map +1 -1
- package/dist/src/source-directory-data.js +2 -2
- package/dist/src/symbols/reference.d.ts +2 -1
- package/dist/src/symbols/reference.d.ts.map +1 -1
- package/dist/src/symbols/reference.js +3 -4
- package/dist/src/symbols/scopes.d.ts +1 -4
- package/dist/src/symbols/scopes.d.ts.map +1 -1
- package/dist/src/symbols/ts-member-scope.d.ts +4 -6
- package/dist/src/symbols/ts-member-scope.d.ts.map +1 -1
- package/dist/src/symbols/ts-member-scope.js +8 -10
- package/dist/src/symbols/ts-module-scope.d.ts +8 -11
- package/dist/src/symbols/ts-module-scope.d.ts.map +1 -1
- package/dist/src/symbols/ts-module-scope.js +47 -40
- package/dist/src/symbols/ts-output-symbol.d.ts +23 -16
- package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
- package/dist/src/symbols/ts-output-symbol.js +88 -27
- package/dist/src/symbols/ts-package-scope.d.ts +18 -14
- package/dist/src/symbols/ts-package-scope.d.ts.map +1 -1
- package/dist/src/symbols/ts-package-scope.js +78 -27
- package/dist/src/utils.js +1 -1
- package/dist/test/class.test.js +2 -1
- package/dist/test/externals.test.js +87 -2
- package/dist/test/function-declaration.test.js +74 -3
- package/dist/test/member-expression.test.js +108 -2
- package/dist/test/new-expression.test.d.ts +2 -0
- package/dist/test/new-expression.test.d.ts.map +1 -0
- package/dist/test/new-expression.test.js +62 -0
- package/dist/test/values.test.d.ts.map +1 -1
- package/dist/test/values.test.js +48 -1
- package/dist/test/var-declaration.test.d.ts.map +1 -1
- package/dist/test/var-declaration.test.js +145 -2
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +22 -21
- package/src/builtins/node.ts +4 -0
- package/src/components/ArrowFunction.tsx +1 -1
- package/src/components/CallSignature.tsx +1 -1
- package/src/components/ClassDeclaration.tsx +5 -7
- package/src/components/Declaration.tsx +3 -8
- package/src/components/EnumDeclaration.tsx +3 -4
- package/src/components/EnumMember.tsx +3 -4
- package/src/components/FunctionBase.tsx +26 -19
- package/src/components/FunctionDeclaration.tsx +1 -1
- package/src/components/FunctionExpression.tsx +1 -1
- package/src/components/FunctionType.tsx +1 -1
- package/src/components/IfStatement.tsx +1 -2
- package/src/components/ImportStatement.tsx +0 -1
- package/src/components/Interface.tsx +1 -1
- package/src/components/InterfaceMethod.tsx +1 -1
- package/src/components/JSDoc.tsx +1 -2
- package/src/components/JSDocComment.tsx +1 -1
- package/src/components/JSDocExample.tsx +1 -2
- package/src/components/JSDocParam.tsx +1 -2
- package/src/components/MemberDeclaration.tsx +5 -8
- package/src/components/MemberExpression.tsx +67 -15
- package/src/components/NewExpression.tsx +25 -0
- package/src/components/ObjectExpression.tsx +21 -19
- package/src/components/PackageDirectory.tsx +2 -10
- package/src/components/Reference.tsx +4 -2
- package/src/components/SingleLineCommentBlock.tsx +1 -2
- package/src/components/SourceFile.tsx +3 -6
- package/src/components/SwitchStatement.tsx +1 -2
- package/src/components/TypeRefContext.tsx +1 -1
- package/src/components/VarDeclaration.tsx +34 -11
- package/src/components/index.ts +1 -0
- package/src/components/stc/index.ts +46 -44
- package/src/create-package.ts +276 -37
- package/src/index.ts +2 -0
- package/src/parameter-descriptor.ts +1 -1
- package/src/source-directory-data.ts +2 -2
- package/src/symbols/reference.ts +10 -5
- package/src/symbols/scopes.ts +1 -6
- package/src/symbols/ts-member-scope.ts +8 -23
- package/src/symbols/ts-module-scope.ts +58 -62
- package/src/symbols/ts-output-symbol.ts +108 -50
- package/src/symbols/ts-package-scope.ts +71 -53
- package/src/utils.ts +1 -1
- package/temp/api.json +3743 -3667
- package/test/class.test.tsx +2 -1
- package/test/externals.test.tsx +99 -1
- package/test/function-declaration.test.tsx +75 -2
- package/test/member-expression.test.tsx +93 -2
- package/test/new-expression.test.tsx +42 -0
- package/test/values.test.tsx +34 -0
- package/test/var-declaration.test.tsx +101 -1
- package/tsconfig.json +2 -1
- package/tsdoc-metadata.json +1 -1
- package/LICENSE +0 -7
- package/dist/test/vitest.d.js +0 -1
- package/test/vitest.d.ts +0 -15
package/test/class.test.tsx
CHANGED
package/test/externals.test.tsx
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { Output, render } from "@alloy-js/core";
|
|
2
|
-
import { it } from "vitest";
|
|
2
|
+
import { expect, it } from "vitest";
|
|
3
3
|
import { fs } from "../src/builtins/node.js";
|
|
4
4
|
import {
|
|
5
5
|
createPackage,
|
|
6
6
|
FunctionDeclaration,
|
|
7
7
|
PackageDirectory,
|
|
8
8
|
SourceFile,
|
|
9
|
+
TSOutputSymbol,
|
|
9
10
|
} from "../src/index.js";
|
|
10
11
|
import { assertFileContents } from "./utils.js";
|
|
11
12
|
|
|
@@ -136,3 +137,100 @@ it("can import builtins without a package", () => {
|
|
|
136
137
|
`,
|
|
137
138
|
});
|
|
138
139
|
});
|
|
140
|
+
|
|
141
|
+
it("can import static members", () => {
|
|
142
|
+
const mcpSdk = createPackage({
|
|
143
|
+
name: "@modelcontextprotocol/sdk",
|
|
144
|
+
version: "^3.23.0",
|
|
145
|
+
descriptor: {
|
|
146
|
+
"./server/index.js": {
|
|
147
|
+
named: [
|
|
148
|
+
{
|
|
149
|
+
name: "server",
|
|
150
|
+
staticMembers: [
|
|
151
|
+
"setRequestHandler",
|
|
152
|
+
{ name: "nested", staticMembers: ["nestedHandler"] },
|
|
153
|
+
],
|
|
154
|
+
instanceMembers: ["instanceHandler"],
|
|
155
|
+
},
|
|
156
|
+
{ name: "noMembers" },
|
|
157
|
+
"simpleName",
|
|
158
|
+
],
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const res = render(
|
|
164
|
+
<Output externals={[mcpSdk]}>
|
|
165
|
+
<SourceFile path="index.ts">
|
|
166
|
+
<FunctionDeclaration name="foo">
|
|
167
|
+
{mcpSdk["./server/index.js"].server}();
|
|
168
|
+
<hbr />
|
|
169
|
+
{mcpSdk["./server/index.js"].server.static.setRequestHandler}();
|
|
170
|
+
<hbr />
|
|
171
|
+
{
|
|
172
|
+
mcpSdk["./server/index.js"].server.static.nested.static
|
|
173
|
+
.nestedHandler
|
|
174
|
+
}
|
|
175
|
+
();
|
|
176
|
+
<hbr />
|
|
177
|
+
{mcpSdk["./server/index.js"].noMembers}();
|
|
178
|
+
<hbr />
|
|
179
|
+
{mcpSdk["./server/index.js"].simpleName}();
|
|
180
|
+
</FunctionDeclaration>
|
|
181
|
+
</SourceFile>
|
|
182
|
+
</Output>,
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
assertFileContents(res, {
|
|
186
|
+
"index.ts": `
|
|
187
|
+
import { noMembers, server, simpleName } from "@modelcontextprotocol/sdk/server/index.js";
|
|
188
|
+
|
|
189
|
+
function foo() {
|
|
190
|
+
server();
|
|
191
|
+
server.setRequestHandler();
|
|
192
|
+
server.nested.nestedHandler();
|
|
193
|
+
noMembers();
|
|
194
|
+
simpleName();
|
|
195
|
+
}
|
|
196
|
+
`,
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("can import instance members", () => {
|
|
201
|
+
const mcpSdk = createPackage({
|
|
202
|
+
name: "@modelcontextprotocol/sdk",
|
|
203
|
+
version: "^3.23.0",
|
|
204
|
+
descriptor: {
|
|
205
|
+
"./server/index.js": {
|
|
206
|
+
named: [{ name: "Server", instanceMembers: ["instanceHandler"] }],
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
function RunTest() {
|
|
212
|
+
const sym = new TSOutputSymbol("foo");
|
|
213
|
+
const binder = sym.binder!;
|
|
214
|
+
|
|
215
|
+
expect(binder).toBeDefined();
|
|
216
|
+
|
|
217
|
+
const source = binder.getSymbolForRefkey(
|
|
218
|
+
mcpSdk["./server/index.js"].Server,
|
|
219
|
+
).value!;
|
|
220
|
+
expect(source).toBeDefined();
|
|
221
|
+
expect(source.instanceMemberScope?.symbols.size).toBe(1);
|
|
222
|
+
|
|
223
|
+
source.instantiateTo(sym);
|
|
224
|
+
|
|
225
|
+
expect(sym.staticMemberScope?.symbols.size).toBe(1);
|
|
226
|
+
expect([...sym.staticMemberScope!.symbols][0].name).toBe("instanceHandler");
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
render(
|
|
230
|
+
<Output externals={[mcpSdk]}>
|
|
231
|
+
<SourceFile path="index.ts">
|
|
232
|
+
<RunTest />
|
|
233
|
+
</SourceFile>
|
|
234
|
+
</Output>,
|
|
235
|
+
);
|
|
236
|
+
});
|
|
@@ -1,7 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
List,
|
|
3
|
+
memberRefkey,
|
|
4
|
+
Props,
|
|
5
|
+
refkey,
|
|
6
|
+
StatementList,
|
|
7
|
+
} from "@alloy-js/core";
|
|
2
8
|
import { d } from "@alloy-js/core/testing";
|
|
3
9
|
import { describe, expect, it } from "vitest";
|
|
4
|
-
import {
|
|
10
|
+
import { ClassField } from "../src/components/stc/index.js";
|
|
11
|
+
import {
|
|
12
|
+
ClassDeclaration,
|
|
13
|
+
FunctionDeclaration,
|
|
14
|
+
InterfaceDeclaration,
|
|
15
|
+
InterfaceMember,
|
|
16
|
+
VarDeclaration,
|
|
17
|
+
} from "../src/index.js";
|
|
5
18
|
import { ParameterDescriptor } from "../src/parameter-descriptor.js";
|
|
6
19
|
import { toSourceText } from "./utils.js";
|
|
7
20
|
|
|
@@ -241,4 +254,64 @@ describe("symbols", () => {
|
|
|
241
254
|
}
|
|
242
255
|
`);
|
|
243
256
|
});
|
|
257
|
+
|
|
258
|
+
it("adds symbols for members of parameters when a type is provided", () => {
|
|
259
|
+
const ifaceRk = refkey();
|
|
260
|
+
const ifaceMemberRk = refkey();
|
|
261
|
+
const clsRk = refkey();
|
|
262
|
+
const clsMemberRk = refkey();
|
|
263
|
+
const ifaceParamRk = refkey();
|
|
264
|
+
const clsParamRk = refkey();
|
|
265
|
+
|
|
266
|
+
const ifaceParam: ParameterDescriptor = {
|
|
267
|
+
name: "ifaceParam",
|
|
268
|
+
refkey: ifaceParamRk,
|
|
269
|
+
type: ifaceRk,
|
|
270
|
+
optional: true,
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const clsParam: ParameterDescriptor = {
|
|
274
|
+
name: "classParam",
|
|
275
|
+
refkey: clsParamRk,
|
|
276
|
+
type: clsRk,
|
|
277
|
+
optional: true,
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
const decl = (
|
|
281
|
+
<List doubleHardline>
|
|
282
|
+
<InterfaceDeclaration name="IFace" refkey={ifaceRk}>
|
|
283
|
+
<InterfaceMember name="ifaceProp" refkey={ifaceMemberRk}>
|
|
284
|
+
string
|
|
285
|
+
</InterfaceMember>
|
|
286
|
+
</InterfaceDeclaration>
|
|
287
|
+
|
|
288
|
+
<ClassDeclaration name="Class" refkey={clsRk}>
|
|
289
|
+
<ClassField name="classProp" refkey={clsMemberRk}>
|
|
290
|
+
42
|
|
291
|
+
</ClassField>
|
|
292
|
+
</ClassDeclaration>
|
|
293
|
+
<FunctionDeclaration name="fn" parameters={[ifaceParam, clsParam]}>
|
|
294
|
+
<StatementList>
|
|
295
|
+
<>console.log({memberRefkey(ifaceParamRk, ifaceMemberRk)})</>
|
|
296
|
+
<>console.log({memberRefkey(clsParamRk, clsMemberRk)});</>
|
|
297
|
+
</StatementList>
|
|
298
|
+
</FunctionDeclaration>
|
|
299
|
+
</List>
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
expect(toSourceText(decl)).toBe(d`
|
|
303
|
+
interface IFace {
|
|
304
|
+
ifaceProp: string
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
class Class {
|
|
308
|
+
classProp = 42
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function fn(ifaceParam?: IFace, classParam?: Class) {
|
|
312
|
+
console.log(ifaceParam?.ifaceProp);
|
|
313
|
+
console.log(classParam?.classProp);;
|
|
314
|
+
}
|
|
315
|
+
`);
|
|
316
|
+
});
|
|
244
317
|
});
|
|
@@ -35,6 +35,97 @@ it("renders basic member expression with dot notation", () => {
|
|
|
35
35
|
`);
|
|
36
36
|
});
|
|
37
37
|
|
|
38
|
+
it("renders basic member expression with numeric id", () => {
|
|
39
|
+
expect(
|
|
40
|
+
toSourceText(
|
|
41
|
+
<MemberExpression>
|
|
42
|
+
<MemberExpression.Part id="arr" />
|
|
43
|
+
<MemberExpression.Part id={0} />
|
|
44
|
+
<MemberExpression.Part id="foo-bar" />
|
|
45
|
+
</MemberExpression>,
|
|
46
|
+
),
|
|
47
|
+
).toBe(d`
|
|
48
|
+
arr[0]["foo-bar"]
|
|
49
|
+
`);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("renders basic member expression with index", () => {
|
|
53
|
+
expect(
|
|
54
|
+
toSourceText(
|
|
55
|
+
<MemberExpression>
|
|
56
|
+
<MemberExpression.Part id="arr" />
|
|
57
|
+
<MemberExpression.Part index={0} />
|
|
58
|
+
<MemberExpression.Part id="foo-bar" />
|
|
59
|
+
</MemberExpression>,
|
|
60
|
+
),
|
|
61
|
+
).toBe(d`
|
|
62
|
+
arr[0]["foo-bar"]
|
|
63
|
+
`);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("renders basic member expression with numeric children", () => {
|
|
67
|
+
expect(
|
|
68
|
+
toSourceText(
|
|
69
|
+
<MemberExpression>
|
|
70
|
+
<MemberExpression.Part id="arr" />
|
|
71
|
+
<MemberExpression.Part>0</MemberExpression.Part>
|
|
72
|
+
<MemberExpression.Part id="foo-bar" />
|
|
73
|
+
</MemberExpression>,
|
|
74
|
+
),
|
|
75
|
+
).toBe(d`
|
|
76
|
+
arr[0]["foo-bar"]
|
|
77
|
+
`);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("renders basic member expression with string children", () => {
|
|
81
|
+
expect(
|
|
82
|
+
toSourceText(
|
|
83
|
+
<MemberExpression>
|
|
84
|
+
<MemberExpression.Part id="arr" />
|
|
85
|
+
<MemberExpression.Part>"0"</MemberExpression.Part>
|
|
86
|
+
<MemberExpression.Part id="foo-bar" />
|
|
87
|
+
</MemberExpression>,
|
|
88
|
+
),
|
|
89
|
+
).toBe(d`
|
|
90
|
+
arr["0"]["foo-bar"]
|
|
91
|
+
`);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("renders basic member expression with an expression index", () => {
|
|
95
|
+
const xRefkey = refkey();
|
|
96
|
+
expect(
|
|
97
|
+
toSourceText(
|
|
98
|
+
<StatementList>
|
|
99
|
+
<VarDeclaration name="x" initializer={1} refkey={xRefkey} />
|
|
100
|
+
<MemberExpression>
|
|
101
|
+
<MemberExpression.Part id="arr" />
|
|
102
|
+
<MemberExpression.Part>{xRefkey} + 1</MemberExpression.Part>
|
|
103
|
+
<MemberExpression.Part id="foo-bar" />
|
|
104
|
+
</MemberExpression>
|
|
105
|
+
</StatementList>,
|
|
106
|
+
),
|
|
107
|
+
).toBe(d`
|
|
108
|
+
const x = 1;
|
|
109
|
+
arr[x + 1]["foo-bar"];
|
|
110
|
+
`);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("renders basic member expression with an expression id", () => {
|
|
114
|
+
expect(
|
|
115
|
+
toSourceText(
|
|
116
|
+
<StatementList>
|
|
117
|
+
<MemberExpression>
|
|
118
|
+
<MemberExpression.Part id="arr" />
|
|
119
|
+
<MemberExpression.Part>"foo" + 1</MemberExpression.Part>
|
|
120
|
+
<MemberExpression.Part id="foo-bar" />
|
|
121
|
+
</MemberExpression>
|
|
122
|
+
</StatementList>,
|
|
123
|
+
),
|
|
124
|
+
).toBe(d`
|
|
125
|
+
arr["foo" + 1]["foo-bar"];
|
|
126
|
+
`);
|
|
127
|
+
});
|
|
128
|
+
|
|
38
129
|
it("renders member expression with bracket notation for invalid identifiers", () => {
|
|
39
130
|
expect(
|
|
40
131
|
toSourceText(
|
|
@@ -192,14 +283,14 @@ it("takes children for the id part", () => {
|
|
|
192
283
|
child3
|
|
193
284
|
</MemberExpression.Part>
|
|
194
285
|
<MemberExpression.Part args />
|
|
195
|
-
<MemberExpression.Part>
|
|
286
|
+
<MemberExpression.Part>"foo" + 1</MemberExpression.Part>
|
|
196
287
|
<MemberExpression.Part args />
|
|
197
288
|
</MemberExpression>
|
|
198
289
|
</List>,
|
|
199
290
|
),
|
|
200
291
|
).toBe(d`
|
|
201
292
|
child1["child2"]
|
|
202
|
-
child1["child2"]()["child3"]?.()
|
|
293
|
+
child1["child2"]()["child3"]?.()["foo" + 1]()
|
|
203
294
|
`);
|
|
204
295
|
});
|
|
205
296
|
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Output, refkey, render, StatementList } from "@alloy-js/core";
|
|
2
|
+
import { d } from "@alloy-js/core/testing";
|
|
3
|
+
import { expect, it } from "vitest";
|
|
4
|
+
import * as ts from "../src/index.js";
|
|
5
|
+
|
|
6
|
+
it("should instantiate classes", () => {
|
|
7
|
+
const varRk = refkey();
|
|
8
|
+
const classRk = refkey();
|
|
9
|
+
const memberRk = refkey();
|
|
10
|
+
|
|
11
|
+
const tree = render(
|
|
12
|
+
<Output>
|
|
13
|
+
<ts.SourceFile path="inst.ts">
|
|
14
|
+
<StatementList>
|
|
15
|
+
<ts.VarDeclaration export name="one" refkey={varRk}>
|
|
16
|
+
<ts.NewExpression target={classRk} />
|
|
17
|
+
</ts.VarDeclaration>
|
|
18
|
+
</StatementList>
|
|
19
|
+
</ts.SourceFile>
|
|
20
|
+
<ts.SourceFile path="decl.ts">
|
|
21
|
+
<ts.ClassDeclaration name="Foo" refkey={classRk}>
|
|
22
|
+
<StatementList>
|
|
23
|
+
<ts.ClassField name="instanceProp" refkey={memberRk}>
|
|
24
|
+
42
|
|
25
|
+
</ts.ClassField>
|
|
26
|
+
</StatementList>
|
|
27
|
+
</ts.ClassDeclaration>
|
|
28
|
+
</ts.SourceFile>
|
|
29
|
+
</Output>,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
expect(tree.contents[0].contents).toEqual(d`
|
|
33
|
+
import { Foo } from "./decl.js";
|
|
34
|
+
|
|
35
|
+
export const one = new Foo();
|
|
36
|
+
`);
|
|
37
|
+
expect(tree.contents[1].contents).toEqual(d`
|
|
38
|
+
class Foo {
|
|
39
|
+
instanceProp = 42;
|
|
40
|
+
}
|
|
41
|
+
`);
|
|
42
|
+
});
|
package/test/values.test.tsx
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
List,
|
|
2
3
|
mapJoin,
|
|
3
4
|
Output,
|
|
4
5
|
printTree,
|
|
@@ -236,6 +237,7 @@ describe("symbols", () => {
|
|
|
236
237
|
<ts.SourceFile path="bar.ts">console.log({barRefkey});</ts.SourceFile>
|
|
237
238
|
</Output>
|
|
238
239
|
);
|
|
240
|
+
|
|
239
241
|
expect(decl).toRenderTo(`
|
|
240
242
|
export const refme = {
|
|
241
243
|
foo: {
|
|
@@ -283,4 +285,36 @@ describe("symbols", () => {
|
|
|
283
285
|
`,
|
|
284
286
|
});
|
|
285
287
|
});
|
|
288
|
+
|
|
289
|
+
it("uses name policy", () => {
|
|
290
|
+
const key1 = refkey();
|
|
291
|
+
const res = render(
|
|
292
|
+
<Output namePolicy={ts.createTSNamePolicy()}>
|
|
293
|
+
<ts.SourceFile path="test.ts">
|
|
294
|
+
<ts.VarDeclaration const name="dispatcher">
|
|
295
|
+
<ts.ObjectExpression>
|
|
296
|
+
<List comma enderPunctuation>
|
|
297
|
+
<ts.ObjectProperty
|
|
298
|
+
name={"foo_bar"}
|
|
299
|
+
value={"null"}
|
|
300
|
+
refkey={key1}
|
|
301
|
+
/>
|
|
302
|
+
</List>
|
|
303
|
+
</ts.ObjectExpression>
|
|
304
|
+
</ts.VarDeclaration>
|
|
305
|
+
<line />
|
|
306
|
+
<ts.FunctionCallExpression target={key1} args={["data"]} />
|
|
307
|
+
</ts.SourceFile>
|
|
308
|
+
</Output>,
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
assertFileContents(res, {
|
|
312
|
+
"test.ts": `
|
|
313
|
+
const dispatcher = {
|
|
314
|
+
fooBar: null,
|
|
315
|
+
}
|
|
316
|
+
dispatcher.fooBar(data)
|
|
317
|
+
`,
|
|
318
|
+
});
|
|
319
|
+
});
|
|
286
320
|
});
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
memberRefkey,
|
|
3
|
+
Output,
|
|
4
|
+
refkey,
|
|
5
|
+
render,
|
|
6
|
+
StatementList,
|
|
7
|
+
} from "@alloy-js/core";
|
|
2
8
|
import "@alloy-js/core/testing";
|
|
3
9
|
import { expect, it } from "vitest";
|
|
4
10
|
import * as ts from "../src/index.js";
|
|
@@ -49,3 +55,97 @@ it("works end-to-end", () => {
|
|
|
49
55
|
`,
|
|
50
56
|
});
|
|
51
57
|
});
|
|
58
|
+
|
|
59
|
+
it("instantiates symbols from its type", () => {
|
|
60
|
+
const ifaceRk = refkey();
|
|
61
|
+
const ifaceMemberRk = refkey();
|
|
62
|
+
const classRk = refkey();
|
|
63
|
+
const classMemberRk = refkey();
|
|
64
|
+
const v1Rk = refkey();
|
|
65
|
+
const v2Rk = refkey();
|
|
66
|
+
|
|
67
|
+
const res = render(
|
|
68
|
+
<Output>
|
|
69
|
+
<ts.SourceFile path="inst.ts">
|
|
70
|
+
<StatementList>
|
|
71
|
+
<ts.VarDeclaration export name="one" refkey={v1Rk} type={classRk}>
|
|
72
|
+
"test"
|
|
73
|
+
</ts.VarDeclaration>
|
|
74
|
+
<>{memberRefkey(v1Rk, classMemberRk)}</>
|
|
75
|
+
<ts.VarDeclaration export name="two" refkey={v2Rk} type={ifaceRk}>
|
|
76
|
+
"test"
|
|
77
|
+
</ts.VarDeclaration>
|
|
78
|
+
<>{memberRefkey(v2Rk, ifaceMemberRk)}</>
|
|
79
|
+
</StatementList>
|
|
80
|
+
</ts.SourceFile>
|
|
81
|
+
<ts.SourceFile path="decl.ts">
|
|
82
|
+
<ts.InterfaceDeclaration name="Foo" refkey={ifaceRk}>
|
|
83
|
+
<StatementList>
|
|
84
|
+
<ts.InterfaceMember name="instanceProp" refkey={ifaceMemberRk}>
|
|
85
|
+
42
|
|
86
|
+
</ts.InterfaceMember>
|
|
87
|
+
</StatementList>
|
|
88
|
+
</ts.InterfaceDeclaration>
|
|
89
|
+
<ts.ClassDeclaration name="Bar" refkey={classRk}>
|
|
90
|
+
<StatementList>
|
|
91
|
+
<ts.ClassField name="instanceProp" refkey={classMemberRk}>
|
|
92
|
+
42
|
|
93
|
+
</ts.ClassField>
|
|
94
|
+
</StatementList>
|
|
95
|
+
</ts.ClassDeclaration>
|
|
96
|
+
</ts.SourceFile>
|
|
97
|
+
</Output>,
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
assertFileContents(res, {
|
|
101
|
+
"inst.ts": `
|
|
102
|
+
import type { Bar, Foo } from "./decl.js";
|
|
103
|
+
|
|
104
|
+
export const one: Bar = "test";
|
|
105
|
+
one.instanceProp;
|
|
106
|
+
export const two: Foo = "test";
|
|
107
|
+
two.instanceProp;
|
|
108
|
+
`,
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("instantiates symbols from type even when an expression is passed", () => {
|
|
113
|
+
const classRk = refkey();
|
|
114
|
+
const classMemberRk = refkey();
|
|
115
|
+
const v1Rk = refkey();
|
|
116
|
+
|
|
117
|
+
const res = render(
|
|
118
|
+
<Output>
|
|
119
|
+
<ts.SourceFile path="inst.ts">
|
|
120
|
+
<StatementList>
|
|
121
|
+
<ts.VarDeclaration export name="one" refkey={v1Rk} type={classRk}>
|
|
122
|
+
<ts.ObjectExpression>
|
|
123
|
+
<ts.ObjectProperty name="noProp" refkey={refkey()} value="1" />
|
|
124
|
+
</ts.ObjectExpression>
|
|
125
|
+
</ts.VarDeclaration>
|
|
126
|
+
<>{memberRefkey(v1Rk, classMemberRk)}</>
|
|
127
|
+
</StatementList>
|
|
128
|
+
</ts.SourceFile>
|
|
129
|
+
<ts.SourceFile path="decl.ts">
|
|
130
|
+
<ts.ClassDeclaration name="Bar" refkey={classRk}>
|
|
131
|
+
<StatementList>
|
|
132
|
+
<ts.ClassField name="instanceProp" refkey={classMemberRk}>
|
|
133
|
+
42
|
|
134
|
+
</ts.ClassField>
|
|
135
|
+
</StatementList>
|
|
136
|
+
</ts.ClassDeclaration>
|
|
137
|
+
</ts.SourceFile>
|
|
138
|
+
</Output>,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
assertFileContents(res, {
|
|
142
|
+
"inst.ts": `
|
|
143
|
+
import type { Bar } from "./decl.js";
|
|
144
|
+
|
|
145
|
+
export const one: Bar = {
|
|
146
|
+
noProp: 1
|
|
147
|
+
};
|
|
148
|
+
one.instanceProp;
|
|
149
|
+
`,
|
|
150
|
+
});
|
|
151
|
+
});
|
package/tsconfig.json
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
"compilerOptions": {
|
|
4
4
|
"emitDeclarationOnly": true,
|
|
5
5
|
"declaration": true,
|
|
6
|
-
"outDir": "dist"
|
|
6
|
+
"outDir": "dist",
|
|
7
|
+
"types": ["@alloy-js/core/testing/matchers"]
|
|
7
8
|
},
|
|
8
9
|
"references": [{ "path": "../core" }],
|
|
9
10
|
"include": ["src/**/*.ts", "src/**/*.tsx", "test/**/*.ts", "test/**/*.tsx"],
|
package/tsdoc-metadata.json
CHANGED
package/LICENSE
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
Copyright 2024 Microsoft
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
-
|
|
5
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
-
|
|
7
|
-
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/dist/test/vitest.d.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import "vitest";
|
package/test/vitest.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import "vitest";
|
|
2
|
-
|
|
3
|
-
interface ToRenderToRenderOptions {
|
|
4
|
-
printWidth?: number;
|
|
5
|
-
tabWidth?: number;
|
|
6
|
-
useTabs?: boolean;
|
|
7
|
-
}
|
|
8
|
-
interface CustomMatchers<R = unknown> {
|
|
9
|
-
toRenderTo: (str: string, options?: ToRenderToRenderOptions) => R;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
declare module "vitest" {
|
|
13
|
-
interface Assertion<T = any> extends CustomMatchers<T> {}
|
|
14
|
-
interface AsymmetricMatchersContaining extends CustomMatchers {}
|
|
15
|
-
}
|