@alloy-js/typescript 0.15.0 → 0.17.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 +25 -0
- package/dist/src/builtins/node.d.ts +9 -41
- package/dist/src/builtins/node.d.ts.map +1 -1
- package/dist/src/components/ArrowFunction.d.ts +3 -3
- package/dist/src/components/BarrelFile.d.ts +1 -1
- package/dist/src/components/BarrelFile.d.ts.map +1 -1
- package/dist/src/components/BlockScope.d.ts +1 -1
- package/dist/src/components/BlockScope.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 +1 -1
- 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/FunctionBase.d.ts +4 -4
- 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 +3 -3
- package/dist/src/components/FunctionExpression.d.ts +3 -3
- package/dist/src/components/FunctionType.d.ts +2 -2
- package/dist/src/components/ImportStatement.d.ts +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 +2 -2
- package/dist/src/components/MemberDeclaration.d.ts +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 +27 -5
- package/dist/src/components/MemberExpression.d.ts.map +1 -1
- package/dist/src/components/MemberExpression.js +327 -89
- 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 +14 -15
- package/dist/src/components/PackageDirectory.d.ts.map +1 -1
- package/dist/src/components/PackageDirectory.js +3 -3
- package/dist/src/components/PackageJson.d.ts +1 -1
- package/dist/src/components/PackageJson.d.ts.map +1 -1
- package/dist/src/components/PropertyName.d.ts +1 -1
- package/dist/src/components/PropertyName.d.ts.map +1 -1
- package/dist/src/components/Reference.d.ts +1 -1
- package/dist/src/components/Reference.d.ts.map +1 -1
- package/dist/src/components/Reference.js +5 -1
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +3 -3
- package/dist/src/components/TsConfigJson.d.ts +1 -1
- package/dist/src/components/TsConfigJson.d.ts.map +1 -1
- package/dist/src/components/TypeDeclaration.d.ts +1 -1
- package/dist/src/components/TypeDeclaration.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 -1
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +1 -1
- package/dist/src/components/stc/index.d.ts +45 -44
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +46 -45
- package/dist/src/create-package.d.ts +62 -18
- package/dist/src/create-package.d.ts.map +1 -1
- package/dist/src/create-package.js +153 -15
- 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/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 +391 -5
- 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/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 +4 -4
- 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 +25 -18
- package/src/components/ImportStatement.tsx +0 -1
- package/src/components/Interface.tsx +1 -1
- package/src/components/MemberDeclaration.tsx +5 -8
- package/src/components/MemberExpression.tsx +416 -103
- package/src/components/NewExpression.tsx +25 -0
- package/src/components/ObjectExpression.tsx +18 -16
- package/src/components/PackageDirectory.tsx +2 -10
- package/src/components/Reference.tsx +4 -2
- package/src/components/SourceFile.tsx +2 -5
- package/src/components/VarDeclaration.tsx +34 -11
- package/src/components/index.ts +1 -1
- package/src/components/stc/index.ts +46 -45
- package/src/create-package.ts +274 -35
- 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/temp/api.json +1665 -1733
- 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 +312 -8
- package/test/new-expression.test.tsx +42 -0
- package/test/values.test.tsx +1 -0
- package/test/var-declaration.test.tsx +101 -1
- package/tsdoc-metadata.json +1 -1
- package/dist/src/components/MemberChainExpression.d.ts +0 -11
- package/dist/src/components/MemberChainExpression.d.ts.map +0 -1
- package/dist/src/components/MemberChainExpression.js +0 -98
- package/dist/test/member-chain.test.d.ts +0 -2
- package/dist/test/member-chain.test.d.ts.map +0 -1
- package/dist/test/member-chain.test.js +0 -141
- package/src/components/MemberChainExpression.tsx +0 -91
- package/test/member-chain.test.tsx +0 -124
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
|
});
|
|
@@ -1,20 +1,26 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
code,
|
|
3
|
+
List,
|
|
4
|
+
Output,
|
|
5
|
+
refkey,
|
|
6
|
+
render,
|
|
7
|
+
StatementList,
|
|
8
|
+
} from "@alloy-js/core";
|
|
2
9
|
import { d } from "@alloy-js/core/testing";
|
|
3
10
|
import { describe, expect, it } from "vitest";
|
|
11
|
+
import { InterfaceMember, ObjectExpression } from "../src/components/index.js";
|
|
4
12
|
import { MemberExpression } from "../src/components/MemberExpression.jsx";
|
|
5
|
-
import {
|
|
6
|
-
InterfaceMember,
|
|
7
|
-
ObjectExpression,
|
|
8
|
-
} from "../src/components/stc/index.js";
|
|
9
13
|
import { VarDeclaration } from "../src/components/VarDeclaration.jsx";
|
|
10
14
|
import {
|
|
11
15
|
ClassDeclaration,
|
|
12
16
|
ClassField,
|
|
13
17
|
FunctionDeclaration,
|
|
14
18
|
InterfaceDeclaration,
|
|
19
|
+
ObjectProperty,
|
|
15
20
|
ParameterDescriptor,
|
|
21
|
+
SourceFile,
|
|
16
22
|
} from "../src/index.js";
|
|
17
|
-
import { toSourceText } from "./utils.js";
|
|
23
|
+
import { assertFileContents, toSourceText } from "./utils.js";
|
|
18
24
|
|
|
19
25
|
it("renders basic member expression with dot notation", () => {
|
|
20
26
|
expect(
|
|
@@ -29,6 +35,97 @@ it("renders basic member expression with dot notation", () => {
|
|
|
29
35
|
`);
|
|
30
36
|
});
|
|
31
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
|
+
|
|
32
129
|
it("renders member expression with bracket notation for invalid identifiers", () => {
|
|
33
130
|
expect(
|
|
34
131
|
toSourceText(
|
|
@@ -42,6 +139,19 @@ it("renders member expression with bracket notation for invalid identifiers", ()
|
|
|
42
139
|
`);
|
|
43
140
|
});
|
|
44
141
|
|
|
142
|
+
it("renders member expressions with quotes", () => {
|
|
143
|
+
expect(
|
|
144
|
+
toSourceText(
|
|
145
|
+
<MemberExpression>
|
|
146
|
+
<MemberExpression.Part id="obj" />
|
|
147
|
+
<MemberExpression.Part id={`property-"name"`} />
|
|
148
|
+
</MemberExpression>,
|
|
149
|
+
),
|
|
150
|
+
).toBe(d`
|
|
151
|
+
obj["property-\\"name\\""]
|
|
152
|
+
`);
|
|
153
|
+
});
|
|
154
|
+
|
|
45
155
|
it("handles nullish chaining", () => {
|
|
46
156
|
expect(
|
|
47
157
|
toSourceText(
|
|
@@ -83,6 +193,19 @@ it("ignores non-part children", () => {
|
|
|
83
193
|
`);
|
|
84
194
|
});
|
|
85
195
|
|
|
196
|
+
it("ignores allows parts to define children", () => {
|
|
197
|
+
expect(
|
|
198
|
+
toSourceText(
|
|
199
|
+
<MemberExpression>
|
|
200
|
+
<MemberExpression.Part>well</MemberExpression.Part>
|
|
201
|
+
<MemberExpression.Part>hello</MemberExpression.Part>
|
|
202
|
+
</MemberExpression>,
|
|
203
|
+
),
|
|
204
|
+
).toBe(d`
|
|
205
|
+
well.hello
|
|
206
|
+
`);
|
|
207
|
+
});
|
|
208
|
+
|
|
86
209
|
it("flattens nested member expressions", () => {
|
|
87
210
|
expect(
|
|
88
211
|
toSourceText(
|
|
@@ -131,6 +254,46 @@ it("handles nullish chaining at multiple levels", () => {
|
|
|
131
254
|
`);
|
|
132
255
|
});
|
|
133
256
|
|
|
257
|
+
it("throws an error when providing conflicting part props", () => {
|
|
258
|
+
expect(() =>
|
|
259
|
+
toSourceText(
|
|
260
|
+
<MemberExpression>
|
|
261
|
+
<MemberExpression.Part id="obj" />
|
|
262
|
+
<MemberExpression.Part id="property" nullish={true} args={[1, 2]} />
|
|
263
|
+
</MemberExpression>,
|
|
264
|
+
),
|
|
265
|
+
).toThrowError(
|
|
266
|
+
`Only one of args, id can be used for a MemberExpression part at a time`,
|
|
267
|
+
);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("takes children for the id part", () => {
|
|
271
|
+
expect(
|
|
272
|
+
toSourceText(
|
|
273
|
+
<List>
|
|
274
|
+
<MemberExpression>
|
|
275
|
+
<MemberExpression.Part>child1</MemberExpression.Part>
|
|
276
|
+
<MemberExpression.Part quoteId>child2</MemberExpression.Part>
|
|
277
|
+
</MemberExpression>
|
|
278
|
+
<MemberExpression>
|
|
279
|
+
<MemberExpression.Part>child1</MemberExpression.Part>
|
|
280
|
+
<MemberExpression.Part quoteId>child2</MemberExpression.Part>
|
|
281
|
+
<MemberExpression.Part args />
|
|
282
|
+
<MemberExpression.Part quoteId nullish>
|
|
283
|
+
child3
|
|
284
|
+
</MemberExpression.Part>
|
|
285
|
+
<MemberExpression.Part args />
|
|
286
|
+
<MemberExpression.Part>"foo" + 1</MemberExpression.Part>
|
|
287
|
+
<MemberExpression.Part args />
|
|
288
|
+
</MemberExpression>
|
|
289
|
+
</List>,
|
|
290
|
+
),
|
|
291
|
+
).toBe(d`
|
|
292
|
+
child1["child2"]
|
|
293
|
+
child1["child2"]()["child3"]?.()["foo" + 1]()
|
|
294
|
+
`);
|
|
295
|
+
});
|
|
296
|
+
|
|
134
297
|
describe("with refkeys", () => {
|
|
135
298
|
it("handles symbols correctly", () => {
|
|
136
299
|
const rk1 = refkey();
|
|
@@ -332,6 +495,42 @@ describe("with refkeys", () => {
|
|
|
332
495
|
const test1_2 = 2;
|
|
333
496
|
`);
|
|
334
497
|
});
|
|
498
|
+
|
|
499
|
+
it("creates a full reference to the first refkey", () => {
|
|
500
|
+
const rk1 = refkey();
|
|
501
|
+
const res = render(
|
|
502
|
+
<Output>
|
|
503
|
+
<SourceFile path="source.ts">
|
|
504
|
+
<VarDeclaration name="importMe">
|
|
505
|
+
<ObjectExpression>
|
|
506
|
+
<ObjectProperty name="prop" refkey={rk1} />
|
|
507
|
+
</ObjectExpression>
|
|
508
|
+
</VarDeclaration>
|
|
509
|
+
</SourceFile>
|
|
510
|
+
<SourceFile path="index.ts">
|
|
511
|
+
<StatementList>
|
|
512
|
+
<MemberExpression>
|
|
513
|
+
<MemberExpression.Part refkey={rk1} />
|
|
514
|
+
<MemberExpression.Part id="foo" />
|
|
515
|
+
</MemberExpression>
|
|
516
|
+
<MemberExpression>
|
|
517
|
+
<MemberExpression.Part>{rk1}</MemberExpression.Part>
|
|
518
|
+
<MemberExpression.Part id="foo" />
|
|
519
|
+
</MemberExpression>
|
|
520
|
+
</StatementList>
|
|
521
|
+
</SourceFile>
|
|
522
|
+
</Output>,
|
|
523
|
+
);
|
|
524
|
+
|
|
525
|
+
assertFileContents(res, {
|
|
526
|
+
"index.ts": d`
|
|
527
|
+
import { importMe } from "./source.js";
|
|
528
|
+
|
|
529
|
+
importMe.prop.foo;
|
|
530
|
+
importMe.prop.foo;
|
|
531
|
+
`,
|
|
532
|
+
});
|
|
533
|
+
});
|
|
335
534
|
});
|
|
336
535
|
|
|
337
536
|
describe("with function calls", () => {
|
|
@@ -378,6 +577,20 @@ describe("with function calls", () => {
|
|
|
378
577
|
myFunction(1, 2)?.prop
|
|
379
578
|
`);
|
|
380
579
|
});
|
|
580
|
+
|
|
581
|
+
it("handles function calls returning nullish correctly", () => {
|
|
582
|
+
expect(
|
|
583
|
+
toSourceText(
|
|
584
|
+
<MemberExpression>
|
|
585
|
+
<MemberExpression.Part id="myFunction" />
|
|
586
|
+
<MemberExpression.Part args={[1, 2]} nullish />
|
|
587
|
+
<MemberExpression.Part id="prop" />
|
|
588
|
+
</MemberExpression>,
|
|
589
|
+
),
|
|
590
|
+
).toBe(d`
|
|
591
|
+
myFunction(1, 2)?.prop
|
|
592
|
+
`);
|
|
593
|
+
});
|
|
381
594
|
});
|
|
382
595
|
|
|
383
596
|
describe("formatting", () => {
|
|
@@ -439,8 +652,33 @@ describe("formatting", () => {
|
|
|
439
652
|
})
|
|
440
653
|
`);
|
|
441
654
|
});
|
|
442
|
-
|
|
443
|
-
|
|
655
|
+
it("handles single calls with multiple parameters", () => {
|
|
656
|
+
expect(
|
|
657
|
+
toSourceText(
|
|
658
|
+
<MemberExpression>
|
|
659
|
+
<MemberExpression.Part id="z" />
|
|
660
|
+
<MemberExpression.Part id="object" />
|
|
661
|
+
<MemberExpression.Part
|
|
662
|
+
args={[
|
|
663
|
+
<ObjectExpression jsValue={{ x: 1 }} />,
|
|
664
|
+
<ObjectExpression jsValue={{ y: 2 }} />,
|
|
665
|
+
]}
|
|
666
|
+
/>
|
|
667
|
+
</MemberExpression>,
|
|
668
|
+
{ printWidth: 12 },
|
|
669
|
+
),
|
|
670
|
+
).toBe(d`
|
|
671
|
+
z.object(
|
|
672
|
+
{
|
|
673
|
+
x: 1,
|
|
674
|
+
},
|
|
675
|
+
{
|
|
676
|
+
y: 2,
|
|
677
|
+
}
|
|
678
|
+
)
|
|
679
|
+
`);
|
|
680
|
+
});
|
|
681
|
+
it("handles multiple calls", () => {
|
|
444
682
|
expect(
|
|
445
683
|
toSourceText(
|
|
446
684
|
<MemberExpression>
|
|
@@ -462,5 +700,71 @@ describe("formatting", () => {
|
|
|
462
700
|
.partial()
|
|
463
701
|
`);
|
|
464
702
|
});
|
|
703
|
+
|
|
704
|
+
it("renders multiple calls on the same line when there are no breaks and they fit", () => {
|
|
705
|
+
expect(
|
|
706
|
+
toSourceText(
|
|
707
|
+
<MemberExpression>
|
|
708
|
+
<MemberExpression.Part id="z" />
|
|
709
|
+
<MemberExpression.Part id="object" />
|
|
710
|
+
<MemberExpression.Part args />
|
|
711
|
+
<MemberExpression.Part id="partial" />
|
|
712
|
+
<MemberExpression.Part args />
|
|
713
|
+
<MemberExpression.Part id="optional" />
|
|
714
|
+
<MemberExpression.Part args />
|
|
715
|
+
</MemberExpression>,
|
|
716
|
+
),
|
|
717
|
+
).toBe(d`
|
|
718
|
+
z.object().partial().optional()
|
|
719
|
+
`);
|
|
720
|
+
});
|
|
721
|
+
it("handles multiple calls with id parts", () => {
|
|
722
|
+
expect(
|
|
723
|
+
toSourceText(
|
|
724
|
+
<MemberExpression>
|
|
725
|
+
<MemberExpression.Part id="z" />
|
|
726
|
+
<MemberExpression.Part id="z1" nullish />
|
|
727
|
+
<MemberExpression.Part id="object" />
|
|
728
|
+
<MemberExpression.Part
|
|
729
|
+
args={[<ObjectExpression jsValue={{ x: 1 }} />]}
|
|
730
|
+
/>
|
|
731
|
+
<MemberExpression.Part id="foo" />
|
|
732
|
+
<MemberExpression.Part id="partial" />
|
|
733
|
+
<MemberExpression.Part args={[]} />
|
|
734
|
+
</MemberExpression>,
|
|
735
|
+
),
|
|
736
|
+
).toBe(d`
|
|
737
|
+
z.z1
|
|
738
|
+
?.object({
|
|
739
|
+
x: 1,
|
|
740
|
+
})
|
|
741
|
+
.foo.partial()
|
|
742
|
+
`);
|
|
743
|
+
});
|
|
744
|
+
|
|
745
|
+
it("handles the first part being a call", () => {
|
|
746
|
+
expect(
|
|
747
|
+
toSourceText(
|
|
748
|
+
<MemberExpression>
|
|
749
|
+
<MemberExpression.Part id="z" />
|
|
750
|
+
<MemberExpression.Part args />
|
|
751
|
+
<MemberExpression.Part id="z1" nullish />
|
|
752
|
+
<MemberExpression.Part id="object" />
|
|
753
|
+
<MemberExpression.Part
|
|
754
|
+
args={[<ObjectExpression jsValue={{ x: 1 }} />]}
|
|
755
|
+
/>
|
|
756
|
+
<MemberExpression.Part id="foo" />
|
|
757
|
+
<MemberExpression.Part id="partial" />
|
|
758
|
+
<MemberExpression.Part args={[]} />
|
|
759
|
+
</MemberExpression>,
|
|
760
|
+
),
|
|
761
|
+
).toBe(d`
|
|
762
|
+
z()
|
|
763
|
+
.z1?.object({
|
|
764
|
+
x: 1,
|
|
765
|
+
})
|
|
766
|
+
.foo.partial()
|
|
767
|
+
`);
|
|
768
|
+
});
|
|
465
769
|
});
|
|
466
770
|
});
|
|
@@ -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
|
+
});
|