@alloy-js/csharp 0.20.0-dev.5 → 0.20.0-dev.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/components/SourceFile.d.ts +5 -0
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +18 -58
- package/dist/src/components/SourceFile.js.map +1 -1
- package/dist/src/components/access-expression/access-expression.d.ts +54 -0
- package/dist/src/components/access-expression/access-expression.d.ts.map +1 -0
- package/dist/src/components/access-expression/access-expression.js +277 -0
- package/dist/src/components/access-expression/access-expression.js.map +1 -0
- package/dist/src/components/access-expression/access-expression.test.d.ts +2 -0
- package/dist/src/components/access-expression/access-expression.test.d.ts.map +1 -0
- package/dist/src/components/access-expression/access-expression.test.js +336 -0
- package/dist/src/components/access-expression/access-expression.test.js.map +1 -0
- package/dist/src/components/access-expression/part-descriptors.d.ts +32 -0
- package/dist/src/components/access-expression/part-descriptors.d.ts.map +1 -0
- package/dist/src/components/access-expression/part-descriptors.js +99 -0
- package/dist/src/components/access-expression/part-descriptors.js.map +1 -0
- package/dist/src/components/class/declaration.d.ts.map +1 -1
- package/dist/src/components/class/declaration.js.map +1 -1
- package/dist/src/components/index.d.ts +1 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +1 -0
- package/dist/src/components/index.js.map +1 -1
- package/dist/src/components/invocation-expression/invocation-expression.d.ts +29 -0
- package/dist/src/components/invocation-expression/invocation-expression.d.ts.map +1 -0
- package/dist/src/components/invocation-expression/invocation-expression.js +70 -0
- package/dist/src/components/invocation-expression/invocation-expression.js.map +1 -0
- package/dist/src/components/invocation-expression/invocation-expression.test.d.ts +2 -0
- package/dist/src/components/invocation-expression/invocation-expression.test.d.ts.map +1 -0
- package/dist/src/components/invocation-expression/invocation-expression.test.js +105 -0
- package/dist/src/components/invocation-expression/invocation-expression.test.js.map +1 -0
- package/dist/src/components/parameters/parameters.d.ts +1 -2
- package/dist/src/components/parameters/parameters.d.ts.map +1 -1
- package/dist/src/components/parameters/parameters.js +2 -1
- package/dist/src/components/parameters/parameters.js.map +1 -1
- package/dist/src/components/parameters/parameters.test.js +66 -0
- package/dist/src/components/parameters/parameters.test.js.map +1 -1
- package/dist/src/components/property/property.d.ts +2 -2
- package/dist/src/components/property/property.d.ts.map +1 -1
- package/dist/src/components/property/property.js +10 -3
- package/dist/src/components/property/property.js.map +1 -1
- package/dist/src/scopes/csharp.d.ts +2 -0
- package/dist/src/scopes/csharp.d.ts.map +1 -1
- package/dist/src/scopes/csharp.js +3 -0
- package/dist/src/scopes/csharp.js.map +1 -1
- package/dist/src/symbols/csharp.d.ts +11 -0
- package/dist/src/symbols/csharp.d.ts.map +1 -1
- package/dist/src/symbols/csharp.js +23 -0
- package/dist/src/symbols/csharp.js.map +1 -1
- package/dist/src/symbols/factories.d.ts.map +1 -1
- package/dist/src/symbols/factories.js +12 -6
- package/dist/src/symbols/factories.js.map +1 -1
- package/dist/src/symbols/reference.d.ts +2 -2
- package/dist/src/symbols/reference.d.ts.map +1 -1
- package/dist/src/symbols/reference.js +17 -5
- package/dist/src/symbols/reference.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/components/SourceFile.tsx +27 -30
- package/src/components/access-expression/access-expression.test.tsx +284 -0
- package/src/components/access-expression/access-expression.tsx +375 -0
- package/src/components/access-expression/part-descriptors.ts +175 -0
- package/src/components/class/declaration.tsx +1 -0
- package/src/components/index.ts +1 -0
- package/src/components/invocation-expression/invocation-expression.test.tsx +101 -0
- package/src/components/invocation-expression/invocation-expression.tsx +60 -0
- package/src/components/parameters/parameters.test.tsx +46 -0
- package/src/components/parameters/parameters.tsx +2 -2
- package/src/components/property/property.tsx +8 -2
- package/src/scopes/csharp.ts +5 -0
- package/src/symbols/csharp.ts +32 -0
- package/src/symbols/factories.ts +31 -15
- package/src/symbols/{reference.ts → reference.tsx} +11 -9
- package/temp/api.json +271 -36
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Children,
|
|
3
|
+
childrenArray,
|
|
4
|
+
ComponentDefinition,
|
|
5
|
+
computed,
|
|
6
|
+
For,
|
|
7
|
+
isComponentCreator,
|
|
8
|
+
Refkey,
|
|
9
|
+
Show,
|
|
10
|
+
takeSymbols,
|
|
11
|
+
} from "@alloy-js/core";
|
|
12
|
+
import { CSharpSymbol } from "../../symbols/csharp.js";
|
|
13
|
+
import {
|
|
14
|
+
childrenToPartDescriptors,
|
|
15
|
+
isArgsPart,
|
|
16
|
+
isIdPart,
|
|
17
|
+
PartDescriptor,
|
|
18
|
+
PartDescriptorWithArgs,
|
|
19
|
+
PartDescriptorWithId,
|
|
20
|
+
PartDescriptorWithIndex,
|
|
21
|
+
} from "./part-descriptors.js";
|
|
22
|
+
|
|
23
|
+
export interface AccessExpressionProps {
|
|
24
|
+
children: Children;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function AccessExpression(props: AccessExpressionProps) {
|
|
28
|
+
const children = flattenAccessExpression(childrenArray(() => props.children));
|
|
29
|
+
const parts = childrenToPartDescriptors(children);
|
|
30
|
+
|
|
31
|
+
// any symbols emitted from the children won't be relevant to parent scopes.
|
|
32
|
+
takeSymbols();
|
|
33
|
+
|
|
34
|
+
if (parts.length === 0) {
|
|
35
|
+
return <></>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const isCallChain = computed(() => {
|
|
39
|
+
let callCount = 0;
|
|
40
|
+
for (const part of parts) {
|
|
41
|
+
if (isArgsPart(part)) callCount++;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return callCount > 1;
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// construct a member expression from the parts. When a part is nullish,
|
|
48
|
+
// and there is a subsequent part, we use `?.` instead of `.`. accessStyle determines
|
|
49
|
+
// whether we use dot or bracket notation.
|
|
50
|
+
|
|
51
|
+
return computed(() => {
|
|
52
|
+
return isCallChain.value ?
|
|
53
|
+
formatCallChain(parts)
|
|
54
|
+
: formatNonCallChain(parts);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Flattens nested access expressions into a single array of parts.
|
|
60
|
+
*/
|
|
61
|
+
function flattenAccessExpression(children: Children[]): Children[] {
|
|
62
|
+
const flattened: Children[] = [];
|
|
63
|
+
for (const child of children) {
|
|
64
|
+
if (isComponentCreator(child, AccessExpression)) {
|
|
65
|
+
flattened.push(
|
|
66
|
+
...flattenAccessExpression(childrenArray(() => child.props.children)),
|
|
67
|
+
);
|
|
68
|
+
} else {
|
|
69
|
+
flattened.push(child);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return flattened;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface AccessExpressionPartProps {
|
|
76
|
+
children?: Children;
|
|
77
|
+
/**
|
|
78
|
+
* Whether this part should use conditional access.
|
|
79
|
+
*/
|
|
80
|
+
conditional?: boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Emit a function call.
|
|
83
|
+
*/
|
|
84
|
+
args?: boolean | Children[];
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Type arguments to pass to a member access.
|
|
88
|
+
*/
|
|
89
|
+
typeArgs?: Children[];
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A refkey for the symbol whose name becomes this part's identifier. When a refkey is provided for the first
|
|
93
|
+
* part, it will be fully resolved. Otherwise, just the symbol's name is used.
|
|
94
|
+
*/
|
|
95
|
+
refkey?: Refkey;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The symbol whose name becomes this part's identifier.
|
|
99
|
+
*/
|
|
100
|
+
symbol?: CSharpSymbol;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The identifier to use for this part.
|
|
104
|
+
*/
|
|
105
|
+
id?: string;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Create an element access part with a single indexer argument. Mutually
|
|
109
|
+
* exclusive with the `indexerArgs` prop.
|
|
110
|
+
*/
|
|
111
|
+
index?: Children;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Create an element access part with multiple indexer arguments. Mutually
|
|
115
|
+
* exclusive with the `index` prop.
|
|
116
|
+
*/
|
|
117
|
+
indexerArgs?: Children[];
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Whether this part could possibly be null. Will guard member and element
|
|
121
|
+
* access with a conditional access operator. Passing this is not necessary if
|
|
122
|
+
* you provide a symbol or refkey and the symbol's nullable flag is set.
|
|
123
|
+
*/
|
|
124
|
+
nullable?: boolean;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
AccessExpression.Part = function (props: AccessExpressionPartProps) {
|
|
128
|
+
/** renders nothing, the parent AccessExpression will use these args */
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Formatting of call chains (i.e. member expressions which have more than one
|
|
133
|
+
* call in them). The general approach is that line breaks occur after each
|
|
134
|
+
* call, and there is only one call per line. When there are non-call elements,
|
|
135
|
+
* they occur prior to the call part. The first part of the member expression
|
|
136
|
+
* contains all but the last non-call part.
|
|
137
|
+
*
|
|
138
|
+
* The following is an example of proper formatting:
|
|
139
|
+
*
|
|
140
|
+
* ```ts
|
|
141
|
+
* z.dummy // all but the last non-call part for the first element
|
|
142
|
+
* .object({ // the first call part with line break after
|
|
143
|
+
* a: 1,
|
|
144
|
+
* })
|
|
145
|
+
* .dummy.partial() // the next call part with non-call parts before it
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
function formatCallChain(parts: PartDescriptor[]): Children {
|
|
149
|
+
return computed(() => {
|
|
150
|
+
const expression: Children[] = [];
|
|
151
|
+
|
|
152
|
+
// break the expression into parts.
|
|
153
|
+
const chunks: PartDescriptor[][] = [];
|
|
154
|
+
|
|
155
|
+
// the first part is all the non-call parts
|
|
156
|
+
let partIndex = 0;
|
|
157
|
+
|
|
158
|
+
function pushPart() {
|
|
159
|
+
const part = parts[partIndex];
|
|
160
|
+
if (!part) throw new Error("No part to push");
|
|
161
|
+
chunks.at(-1)!.push(part);
|
|
162
|
+
partIndex++;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function pushChunk() {
|
|
166
|
+
chunks.push([]);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// For the first chunk, take all the non-call parts except the last one
|
|
170
|
+
// and put them in a chunk.
|
|
171
|
+
pushChunk();
|
|
172
|
+
while (
|
|
173
|
+
partIndex < parts.length &&
|
|
174
|
+
(partIndex === parts.length - 1 ||
|
|
175
|
+
chunks.at(-1)!.length === 0 ||
|
|
176
|
+
!isArgsPart(parts[partIndex + 1]))
|
|
177
|
+
) {
|
|
178
|
+
pushPart();
|
|
179
|
+
if (isArgsPart(chunks.at(-1)!.at(-1)!)) {
|
|
180
|
+
// the first segment always ends after we see a call
|
|
181
|
+
// if we happen to take one
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// then for all remaining parts, collect all the non-call parts and end with
|
|
187
|
+
// a call chunk
|
|
188
|
+
while (partIndex < parts.length) {
|
|
189
|
+
pushChunk();
|
|
190
|
+
while (partIndex < parts.length && !isArgsPart(parts[partIndex])) {
|
|
191
|
+
pushPart();
|
|
192
|
+
}
|
|
193
|
+
while (partIndex < parts.length && isArgsPart(parts[partIndex])) {
|
|
194
|
+
pushPart();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
for (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
|
|
199
|
+
const chunk = chunks[chunkIndex];
|
|
200
|
+
const chunkExpression = [];
|
|
201
|
+
for (let partIndex = 0; partIndex < chunk.length; partIndex++) {
|
|
202
|
+
if (chunkIndex === 0 && partIndex === 0) {
|
|
203
|
+
// first part is just gonna be the id
|
|
204
|
+
const firstPart =
|
|
205
|
+
isIdPart(chunk[0]) ?
|
|
206
|
+
chunk[0].id
|
|
207
|
+
: (chunk[0] as PartDescriptorWithIndex).indexerArgs;
|
|
208
|
+
chunkExpression.push(firstPart);
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const part = chunk[partIndex];
|
|
212
|
+
const prevPart =
|
|
213
|
+
partIndex === 0 ?
|
|
214
|
+
chunks[chunkIndex - 1].at(-1)!
|
|
215
|
+
: chunk[partIndex - 1];
|
|
216
|
+
|
|
217
|
+
if (isArgsPart(part)) {
|
|
218
|
+
// For parts with only args (no name), append function call directly with appropriate nullish operator
|
|
219
|
+
chunkExpression.push(formatCallExpr(prevPart, part));
|
|
220
|
+
} else if (isIdPart(part)) {
|
|
221
|
+
chunkExpression.push(formatMemberAccess(prevPart, part, true));
|
|
222
|
+
} else {
|
|
223
|
+
// bracket notation - don't include the dot
|
|
224
|
+
chunkExpression.push(formatElementAccess(prevPart, part));
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
expression.push(
|
|
229
|
+
chunkIndex === 0 ? chunkExpression : (
|
|
230
|
+
<>
|
|
231
|
+
<sbr />
|
|
232
|
+
{chunkExpression}
|
|
233
|
+
</>
|
|
234
|
+
),
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return (
|
|
239
|
+
<group>
|
|
240
|
+
<indent>{expression}</indent>
|
|
241
|
+
</group>
|
|
242
|
+
);
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function formatNonCallChain(parts: PartDescriptor[]): Children {
|
|
247
|
+
return computed(() => {
|
|
248
|
+
const expression: Children[] = [];
|
|
249
|
+
|
|
250
|
+
for (let i = 0; i < parts.length; i++) {
|
|
251
|
+
const part = parts[i];
|
|
252
|
+
const base =
|
|
253
|
+
isIdPart(part) ?
|
|
254
|
+
part.id
|
|
255
|
+
: (part as PartDescriptorWithIndex).indexerArgs;
|
|
256
|
+
if (i === 0) {
|
|
257
|
+
expression.push(base, <TypeArgs args={(part as any).typeArgs} />);
|
|
258
|
+
} else {
|
|
259
|
+
// Determine if we should use nullish operator from previous part
|
|
260
|
+
const prevPart = parts[i - 1];
|
|
261
|
+
|
|
262
|
+
if (isArgsPart(part)) {
|
|
263
|
+
// For parts with only args (no name), append function call directly with appropriate nullish operator
|
|
264
|
+
expression.push(formatCallExpr(prevPart, part));
|
|
265
|
+
} else if (isIdPart(part)) {
|
|
266
|
+
expression.push(formatMemberAccess(prevPart, part));
|
|
267
|
+
} else {
|
|
268
|
+
// bracket notation - don't include the dot
|
|
269
|
+
expression.push(formatElementAccess(prevPart, part));
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return expression;
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function formatElementAccess(
|
|
279
|
+
prevPart: PartDescriptor,
|
|
280
|
+
part: PartDescriptorWithIndex,
|
|
281
|
+
) {
|
|
282
|
+
return (
|
|
283
|
+
<group>
|
|
284
|
+
{part.conditional || ("nullable" in prevPart && prevPart.nullable) ?
|
|
285
|
+
"?"
|
|
286
|
+
: ""}
|
|
287
|
+
[
|
|
288
|
+
<indent>
|
|
289
|
+
<sbr />
|
|
290
|
+
<For each={part.indexerArgs} comma line>
|
|
291
|
+
{(arg) => arg}
|
|
292
|
+
</For>
|
|
293
|
+
</indent>
|
|
294
|
+
<sbr />]
|
|
295
|
+
</group>
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function formatMemberAccess(
|
|
300
|
+
prevPart: PartDescriptor,
|
|
301
|
+
part: PartDescriptorWithId,
|
|
302
|
+
noIndent = false,
|
|
303
|
+
) {
|
|
304
|
+
let Wrapping: ComponentDefinition<{ children: Children }>;
|
|
305
|
+
if (noIndent) {
|
|
306
|
+
Wrapping = function (props) {
|
|
307
|
+
return (
|
|
308
|
+
<group>
|
|
309
|
+
<sbr />
|
|
310
|
+
{props.children}
|
|
311
|
+
</group>
|
|
312
|
+
);
|
|
313
|
+
};
|
|
314
|
+
} else {
|
|
315
|
+
Wrapping = function (props) {
|
|
316
|
+
return (
|
|
317
|
+
<group>
|
|
318
|
+
<indent>
|
|
319
|
+
<sbr />
|
|
320
|
+
{props.children}
|
|
321
|
+
</indent>
|
|
322
|
+
</group>
|
|
323
|
+
);
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return (
|
|
328
|
+
<Wrapping>
|
|
329
|
+
{part.conditional || ("nullable" in prevPart && prevPart.nullable) ?
|
|
330
|
+
"?."
|
|
331
|
+
: "."}
|
|
332
|
+
{isIdPart(part) ? part.id : (part as PartDescriptorWithIndex).indexerArgs}
|
|
333
|
+
<TypeArgs args={part.typeArgs} />
|
|
334
|
+
</Wrapping>
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function TypeArgs(props: { args?: Children[] }) {
|
|
339
|
+
return (
|
|
340
|
+
<Show when={props.args && props.args.length > 0}>
|
|
341
|
+
{"<"}
|
|
342
|
+
<group>
|
|
343
|
+
<indent>
|
|
344
|
+
<sbr />
|
|
345
|
+
<For each={props.args!} comma line>
|
|
346
|
+
{(arg) => arg}
|
|
347
|
+
</For>
|
|
348
|
+
</indent>
|
|
349
|
+
<sbr />
|
|
350
|
+
</group>
|
|
351
|
+
{">"}
|
|
352
|
+
</Show>
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function formatCallExpr(
|
|
357
|
+
prevPart: PartDescriptor,
|
|
358
|
+
part: PartDescriptorWithArgs,
|
|
359
|
+
) {
|
|
360
|
+
return (
|
|
361
|
+
<group>
|
|
362
|
+
(<Show when={part.args.length <= 1}>{part.args[0]}</Show>
|
|
363
|
+
<Show when={part.args.length > 1}>
|
|
364
|
+
<indent>
|
|
365
|
+
<sbr />
|
|
366
|
+
<For each={part.args} comma line>
|
|
367
|
+
{(arg) => arg}
|
|
368
|
+
</For>
|
|
369
|
+
</indent>
|
|
370
|
+
<sbr />
|
|
371
|
+
</Show>
|
|
372
|
+
)
|
|
373
|
+
</group>
|
|
374
|
+
);
|
|
375
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Children,
|
|
3
|
+
computed,
|
|
4
|
+
isComponentCreator,
|
|
5
|
+
reactive,
|
|
6
|
+
ref,
|
|
7
|
+
symbolForRefkey,
|
|
8
|
+
ToRefs,
|
|
9
|
+
} from "@alloy-js/core";
|
|
10
|
+
import { CSharpSymbol } from "../../index.js";
|
|
11
|
+
import {
|
|
12
|
+
AccessExpression,
|
|
13
|
+
AccessExpressionPartProps,
|
|
14
|
+
} from "./access-expression.jsx";
|
|
15
|
+
|
|
16
|
+
export interface PartDescriptorWithId extends PartDescriptorBase {
|
|
17
|
+
/**
|
|
18
|
+
* The identifier of the access expression part. Will use member access, so must be a valid
|
|
19
|
+
* C# identifier.
|
|
20
|
+
*/
|
|
21
|
+
id: Children;
|
|
22
|
+
conditional: boolean;
|
|
23
|
+
typeArgs?: Children[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function isIdPart(part: PartDescriptor): part is PartDescriptorWithId {
|
|
27
|
+
return "id" in part && part.id !== undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface PartDescriptorWithIndex extends PartDescriptorBase {
|
|
31
|
+
/**
|
|
32
|
+
* The index of the access expression part. Will use element access.
|
|
33
|
+
*/
|
|
34
|
+
indexerArgs: Children[];
|
|
35
|
+
conditional: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function isIndexPart(
|
|
39
|
+
part: PartDescriptor,
|
|
40
|
+
): part is PartDescriptorWithIndex {
|
|
41
|
+
return "indexerArgs" in part && part.indexerArgs !== undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface PartDescriptorWithArgs extends PartDescriptorBase {
|
|
45
|
+
args: Children[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function isArgsPart(
|
|
49
|
+
part: PartDescriptor,
|
|
50
|
+
): part is PartDescriptorWithArgs {
|
|
51
|
+
return "args" in part && part.args !== undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface PartDescriptorBase {
|
|
55
|
+
nullable: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type PartDescriptor =
|
|
59
|
+
| PartDescriptorWithId
|
|
60
|
+
| PartDescriptorWithIndex
|
|
61
|
+
| PartDescriptorWithArgs;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Build part descriptors from the children of a MemberExpression.
|
|
65
|
+
*/
|
|
66
|
+
export function childrenToPartDescriptors(
|
|
67
|
+
children: Children[],
|
|
68
|
+
): PartDescriptor[] {
|
|
69
|
+
const parts: PartDescriptor[] = [];
|
|
70
|
+
for (const child of children) {
|
|
71
|
+
if (!isComponentCreator(child, AccessExpression.Part)) {
|
|
72
|
+
// we ignore non-parts
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
parts.push(
|
|
77
|
+
createPartDescriptorFromProps(child.props, child === children[0]),
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return parts;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const exclusiveParts: (keyof AccessExpressionPartProps)[] = [
|
|
85
|
+
"children",
|
|
86
|
+
"args",
|
|
87
|
+
"refkey",
|
|
88
|
+
"symbol",
|
|
89
|
+
"id",
|
|
90
|
+
];
|
|
91
|
+
/**
|
|
92
|
+
* Creates a reactive part descriptor from the given part props.
|
|
93
|
+
*
|
|
94
|
+
* @param partProps The props for the part.
|
|
95
|
+
* @param first Whether this is the first part in the expression. Refkeys are
|
|
96
|
+
* handled specially for the first part.
|
|
97
|
+
*/
|
|
98
|
+
function createPartDescriptorFromProps(
|
|
99
|
+
partProps: AccessExpressionPartProps,
|
|
100
|
+
first: boolean,
|
|
101
|
+
) {
|
|
102
|
+
const foundProps = exclusiveParts.filter((key) => {
|
|
103
|
+
return key in partProps;
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
if (foundProps.length > 1) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
`Only one of ${foundProps.join(", ")} can be used for a MemberExpression part at a time`,
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const symbolSource = computed(() => {
|
|
113
|
+
if (partProps.refkey) {
|
|
114
|
+
return symbolForRefkey(partProps.refkey).value as CSharpSymbol;
|
|
115
|
+
} else if (partProps.symbol) {
|
|
116
|
+
return partProps.symbol;
|
|
117
|
+
} else {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const part: ToRefs<PartDescriptor> = {
|
|
123
|
+
id: computed(() => {
|
|
124
|
+
if (partProps.args || partProps.index || partProps.indexerArgs) {
|
|
125
|
+
return undefined;
|
|
126
|
+
} else if (partProps.children !== undefined) {
|
|
127
|
+
return partProps.children;
|
|
128
|
+
} else if (first && partProps.refkey) {
|
|
129
|
+
return partProps.refkey;
|
|
130
|
+
} else if (partProps.id !== undefined) {
|
|
131
|
+
return partProps.id;
|
|
132
|
+
} else if (symbolSource.value) {
|
|
133
|
+
return escapeId(symbolSource.value.name);
|
|
134
|
+
} else {
|
|
135
|
+
return "<unresolved symbol>";
|
|
136
|
+
}
|
|
137
|
+
}),
|
|
138
|
+
indexerArgs: computed(() => {
|
|
139
|
+
if (partProps.indexerArgs) {
|
|
140
|
+
return partProps.indexerArgs;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (partProps.index !== undefined) {
|
|
144
|
+
return [partProps.index];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return [];
|
|
148
|
+
}),
|
|
149
|
+
conditional: computed(() => {
|
|
150
|
+
return !!partProps.conditional;
|
|
151
|
+
}),
|
|
152
|
+
nullable: computed(() => {
|
|
153
|
+
if (partProps.nullable) {
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (symbolSource.value) {
|
|
158
|
+
return symbolSource.value.isNullable;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return false;
|
|
162
|
+
}),
|
|
163
|
+
args: ref<any>(partProps.args === true ? [] : partProps.args),
|
|
164
|
+
typeArgs: ref<any>(partProps.typeArgs),
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
return reactive(part);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* replaces quotes with escaped quotes
|
|
172
|
+
*/
|
|
173
|
+
function escapeId(id: string) {
|
|
174
|
+
return id.replace(/"/g, '\\"');
|
|
175
|
+
}
|
|
@@ -133,6 +133,7 @@ export function ClassDeclaration(props: ClassDeclarationProps) {
|
|
|
133
133
|
const thisClassSymbol = createNamedTypeSymbol(props.name, "class", {
|
|
134
134
|
refkeys: props.refkey,
|
|
135
135
|
});
|
|
136
|
+
|
|
136
137
|
const thisClassScope = createClassScope(thisClassSymbol);
|
|
137
138
|
|
|
138
139
|
const bases = [
|
package/src/components/index.ts
CHANGED
|
@@ -10,6 +10,7 @@ export * from "./field/field.jsx";
|
|
|
10
10
|
export * from "./interface/declaration.js";
|
|
11
11
|
export * from "./interface/method.js";
|
|
12
12
|
export * from "./interface/property.js";
|
|
13
|
+
export * from "./invocation-expression/invocation-expression.jsx";
|
|
13
14
|
export * from "./lexical-scope.jsx";
|
|
14
15
|
export * from "./method-scope.jsx";
|
|
15
16
|
export * from "./method/method.jsx";
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { ClassDeclaration } from "#components/class/declaration.jsx";
|
|
2
|
+
import { Method } from "#components/method/method.jsx";
|
|
3
|
+
import { TestNamespace } from "#test/utils.jsx";
|
|
4
|
+
import { namekey } from "@alloy-js/core";
|
|
5
|
+
import { describe, expect, it } from "vitest";
|
|
6
|
+
import { InvocationExpression } from "./invocation-expression.jsx";
|
|
7
|
+
|
|
8
|
+
it("makes a call with no arguments", () => {
|
|
9
|
+
const template = <InvocationExpression target="Foo" />;
|
|
10
|
+
expect(template).toRenderTo(`Foo()`);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("makes a call with arguments", () => {
|
|
14
|
+
const template = (
|
|
15
|
+
<InvocationExpression target="Foo" args={["42", `"string"`]} />
|
|
16
|
+
);
|
|
17
|
+
expect(template).toRenderTo(`Foo(42, "string")`);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("makes a call with type parameters", () => {
|
|
21
|
+
const template = (
|
|
22
|
+
<InvocationExpression target="Foo" typeArgs={["Bar", "Baz"]} />
|
|
23
|
+
);
|
|
24
|
+
expect(template).toRenderTo(`Foo<Bar, Baz>()`);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("makes a call to a method", () => {
|
|
28
|
+
const cls = namekey("TestClass");
|
|
29
|
+
const method = namekey("Method");
|
|
30
|
+
const template = (
|
|
31
|
+
<TestNamespace>
|
|
32
|
+
<ClassDeclaration name={cls}>
|
|
33
|
+
<Method name={method}>return 1;</Method>
|
|
34
|
+
</ClassDeclaration>
|
|
35
|
+
<hbr />
|
|
36
|
+
<InvocationExpression target={method} />;
|
|
37
|
+
</TestNamespace>
|
|
38
|
+
);
|
|
39
|
+
expect(template).toRenderTo(`
|
|
40
|
+
class TestClass
|
|
41
|
+
{
|
|
42
|
+
void Method()
|
|
43
|
+
{
|
|
44
|
+
return 1;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
TestClass.Method();
|
|
48
|
+
`);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("formatting", () => {
|
|
52
|
+
it("doesn't break one long argument", () => {
|
|
53
|
+
const template = (
|
|
54
|
+
<InvocationExpression target="Foo" args={["oneLongArgument"]} />
|
|
55
|
+
);
|
|
56
|
+
expect(template).toRenderTo(`Foo(oneLongArgument)`, { printWidth: 10 });
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("breaks multiple arguments", () => {
|
|
60
|
+
const template = (
|
|
61
|
+
<InvocationExpression
|
|
62
|
+
target="Foo"
|
|
63
|
+
args={["oneLongArgument", "anotherLongArgument"]}
|
|
64
|
+
/>
|
|
65
|
+
);
|
|
66
|
+
expect(template).toRenderTo(
|
|
67
|
+
`
|
|
68
|
+
Foo(
|
|
69
|
+
oneLongArgument,
|
|
70
|
+
anotherLongArgument
|
|
71
|
+
)
|
|
72
|
+
`,
|
|
73
|
+
{ printWidth: 10 },
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("doesn't break one long type argument", () => {
|
|
78
|
+
const template = (
|
|
79
|
+
<InvocationExpression target="Foo" typeArgs={["oneLongArgument"]} />
|
|
80
|
+
);
|
|
81
|
+
expect(template).toRenderTo(`Foo<oneLongArgument>()`, { printWidth: 10 });
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("breaks multiple type arguments", () => {
|
|
85
|
+
const template = (
|
|
86
|
+
<InvocationExpression
|
|
87
|
+
target="Foo"
|
|
88
|
+
typeArgs={["oneLongArgument", "anotherLongArgument"]}
|
|
89
|
+
/>
|
|
90
|
+
);
|
|
91
|
+
expect(template).toRenderTo(
|
|
92
|
+
`
|
|
93
|
+
Foo<
|
|
94
|
+
oneLongArgument,
|
|
95
|
+
anotherLongArgument
|
|
96
|
+
>()
|
|
97
|
+
`,
|
|
98
|
+
{ printWidth: 10 },
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
});
|