@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
|
@@ -5,12 +5,15 @@ import {
|
|
|
5
5
|
For,
|
|
6
6
|
isComponentCreator,
|
|
7
7
|
OutputSymbol,
|
|
8
|
-
|
|
8
|
+
reactive,
|
|
9
9
|
ref,
|
|
10
10
|
Refkey,
|
|
11
|
-
|
|
11
|
+
Show,
|
|
12
|
+
takeSymbols,
|
|
13
|
+
ToRefs,
|
|
12
14
|
useBinder,
|
|
13
15
|
} from "@alloy-js/core";
|
|
16
|
+
|
|
14
17
|
import { TSOutputSymbol, TSSymbolFlags } from "../symbols/ts-output-symbol.js";
|
|
15
18
|
import { isValidJSIdentifier } from "../utils.js";
|
|
16
19
|
|
|
@@ -18,24 +21,39 @@ export interface MemberExpressionProps {
|
|
|
18
21
|
children: Children;
|
|
19
22
|
}
|
|
20
23
|
|
|
21
|
-
interface
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
interface PartDescriptorWithId extends PartDescriptorBase {
|
|
25
|
+
id: Children;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface PartDescriptorWithIndex extends PartDescriptorBase {
|
|
29
|
+
index: number;
|
|
30
|
+
}
|
|
31
|
+
interface PartDescriptorBase {
|
|
32
|
+
accessStyle: "dot" | "bracket";
|
|
33
|
+
quoteId: boolean;
|
|
34
|
+
nullish: boolean;
|
|
35
|
+
args?: Children[];
|
|
26
36
|
}
|
|
27
37
|
|
|
38
|
+
type PartDescriptor = PartDescriptorWithId | PartDescriptorWithIndex;
|
|
39
|
+
|
|
28
40
|
/**
|
|
29
41
|
* Create a member expression from parts. Each part can provide one of
|
|
30
42
|
* the following:
|
|
31
43
|
*
|
|
32
44
|
* * **id**: The identifier for the member expression part
|
|
33
|
-
* * **refkey**:
|
|
45
|
+
* * **refkey**: a refkey for a symbol whose name becomes the identifier
|
|
34
46
|
* * **symbol**: a symbol whose name becomes the identifier part
|
|
35
47
|
* * **args**: create a method call with the given args
|
|
48
|
+
* * **children**: arbitrary contents for the identifier part.
|
|
36
49
|
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
50
|
+
* Each part can have a nullish prop, which indicates that the part may be null
|
|
51
|
+
* or undefined.
|
|
52
|
+
*
|
|
53
|
+
* Each part can also have a quoteId prop, which indicates that the identifier
|
|
54
|
+
* of the part should be quoted (i.e. `["foo"]` instead of `.foo`). This is only
|
|
55
|
+
* necessary when providing the children prop, otherwise it is determined
|
|
56
|
+
* automatically.
|
|
39
57
|
*
|
|
40
58
|
* @example
|
|
41
59
|
*
|
|
@@ -45,6 +63,7 @@ interface PartDescriptor {
|
|
|
45
63
|
* <MemberExpression.Part refkey={rk} nullish />
|
|
46
64
|
* <MemberExpression.Part symbol={sym} />
|
|
47
65
|
* <MemberExpression.Part args={["hello", "world"]} />
|
|
66
|
+
* <MemberExpression.Part>SomeValue</MemberExpression.Part>
|
|
48
67
|
* </MemberExpression>
|
|
49
68
|
* ```
|
|
50
69
|
*
|
|
@@ -52,11 +71,44 @@ interface PartDescriptor {
|
|
|
52
71
|
* with a name of "prop2", this will render:
|
|
53
72
|
*
|
|
54
73
|
* ```ts
|
|
55
|
-
* base.prop1?.prop2("hello", "world")
|
|
74
|
+
* base.prop1?.prop2("hello", "world").SomeValue
|
|
56
75
|
* ```
|
|
57
76
|
*/
|
|
58
77
|
export function MemberExpression(props: MemberExpressionProps): Children {
|
|
59
78
|
const children = flattenMemberExpression(childrenArray(() => props.children));
|
|
79
|
+
const parts = childrenToPartDescriptors(children);
|
|
80
|
+
// any symbols emitted from the children won't be relevant to
|
|
81
|
+
// parent scopes. TODO: emit the proper symbol if we know it?
|
|
82
|
+
takeSymbols();
|
|
83
|
+
|
|
84
|
+
if (parts.length === 0) {
|
|
85
|
+
return <></>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const isCallChain = computed(() => {
|
|
89
|
+
let callCount = 0;
|
|
90
|
+
for (const part of parts) {
|
|
91
|
+
if (part.args !== undefined) callCount++;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return callCount > 1;
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// construct a member expression from the parts. When a part is nullish,
|
|
98
|
+
// and there is a subsequent part, we use `?.` instead of `.`. accessStyle determines
|
|
99
|
+
// whether we use dot or bracket notation.
|
|
100
|
+
|
|
101
|
+
return computed(() => {
|
|
102
|
+
return isCallChain.value ?
|
|
103
|
+
formatCallChain(parts)
|
|
104
|
+
: formatNonCallChain(parts);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Build part descriptors from the children of a MemberExpression.
|
|
110
|
+
*/
|
|
111
|
+
function childrenToPartDescriptors(children: Children[]): PartDescriptor[] {
|
|
60
112
|
const parts: PartDescriptor[] = [];
|
|
61
113
|
for (const child of children) {
|
|
62
114
|
if (!isComponentCreator(child, MemberExpression.Part)) {
|
|
@@ -64,121 +116,340 @@ export function MemberExpression(props: MemberExpressionProps): Children {
|
|
|
64
116
|
continue;
|
|
65
117
|
}
|
|
66
118
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
119
|
+
parts.push(
|
|
120
|
+
createPartDescriptorFromProps(child.props, child === children[0]),
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return parts;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const exclusiveParts: (keyof MemberExpressionPartProps)[] = [
|
|
128
|
+
"children",
|
|
129
|
+
"args",
|
|
130
|
+
"refkey",
|
|
131
|
+
"symbol",
|
|
132
|
+
"id",
|
|
133
|
+
];
|
|
134
|
+
/**
|
|
135
|
+
* Creates a reactive part descriptor from the given part props.
|
|
136
|
+
*
|
|
137
|
+
* @param partProps The props for the part.
|
|
138
|
+
* @param first Whether this is the first part in the expression. Refkeys are
|
|
139
|
+
* handled specially for the first part.
|
|
140
|
+
*/
|
|
141
|
+
function createPartDescriptorFromProps(
|
|
142
|
+
partProps: MemberExpressionPartProps,
|
|
143
|
+
first: boolean,
|
|
144
|
+
) {
|
|
145
|
+
const foundProps = exclusiveParts.filter((key) => {
|
|
146
|
+
return key in partProps;
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
if (foundProps.length > 1) {
|
|
150
|
+
throw new Error(
|
|
151
|
+
`Only one of ${foundProps.join(", ")} can be used for a MemberExpression part at a time`,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const symbolSource = computed(() => {
|
|
156
|
+
if (partProps.refkey) {
|
|
157
|
+
return getSymbolForRefkey(partProps.refkey).value;
|
|
158
|
+
} else if (partProps.symbol) {
|
|
159
|
+
return partProps.symbol;
|
|
160
|
+
} else {
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const part: ToRefs<PartDescriptor> = {
|
|
166
|
+
id: computed(() => {
|
|
167
|
+
if (partProps.args) {
|
|
168
|
+
return undefined;
|
|
169
|
+
} else if (partProps.children !== undefined) {
|
|
170
|
+
return partProps.children;
|
|
171
|
+
} else if (first && partProps.refkey) {
|
|
172
|
+
return partProps.refkey;
|
|
173
|
+
} else if (partProps.id !== undefined) {
|
|
174
|
+
return isNumericIdentifier(partProps.id) ?
|
|
175
|
+
partProps.id
|
|
176
|
+
: escapeId(partProps.id);
|
|
177
|
+
} else if (partProps.index !== undefined) {
|
|
178
|
+
return partProps.index;
|
|
179
|
+
} else if (symbolSource.value) {
|
|
180
|
+
return escapeId(symbolSource.value.name);
|
|
181
|
+
} else {
|
|
182
|
+
return "<unresolved symbol>";
|
|
183
|
+
}
|
|
184
|
+
}),
|
|
185
|
+
accessStyle: computed(() => {
|
|
186
|
+
if (partProps.children !== undefined) {
|
|
187
|
+
if (typeof partProps.children === "string") {
|
|
188
|
+
return partProps.quoteId ? "bracket" : (
|
|
189
|
+
accessStyleForMemberName(partProps.children)
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return "bracket";
|
|
194
|
+
} else if (partProps.args) {
|
|
195
|
+
// not used
|
|
196
|
+
return "dot";
|
|
197
|
+
} else if (partProps.quoteId) {
|
|
198
|
+
return "bracket";
|
|
199
|
+
} else if (partProps.id !== undefined) {
|
|
200
|
+
return isNumericIdentifier(partProps.id) ? "bracket" : (
|
|
201
|
+
accessStyleForMemberName(partProps.id)
|
|
84
202
|
);
|
|
85
|
-
|
|
86
|
-
|
|
203
|
+
} else if (partProps.index !== undefined) {
|
|
204
|
+
return "bracket";
|
|
205
|
+
} else if (symbolSource.value) {
|
|
206
|
+
return accessStyleForMemberName(symbolSource.value.name);
|
|
207
|
+
} else {
|
|
208
|
+
return "dot";
|
|
209
|
+
}
|
|
210
|
+
}),
|
|
211
|
+
quoteId: computed(() => {
|
|
212
|
+
if (partProps.quoteId) {
|
|
213
|
+
return partProps.quoteId;
|
|
214
|
+
} else if (partProps.id !== undefined) {
|
|
215
|
+
return isNumericIdentifier(partProps.id) ? false : (
|
|
216
|
+
!isValidJSIdentifier(partProps.id)
|
|
87
217
|
);
|
|
88
|
-
|
|
218
|
+
} else if (symbolSource.value) {
|
|
219
|
+
return !isValidJSIdentifier(symbolSource.value.name);
|
|
220
|
+
} else {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
}),
|
|
224
|
+
nullish: computed(() => {
|
|
225
|
+
if (partProps.nullish) {
|
|
226
|
+
return partProps.nullish;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (symbolSource.value) {
|
|
230
|
+
return isNullish(symbolSource.value);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return false;
|
|
234
|
+
}),
|
|
235
|
+
args: ref<any>(partProps.args === true ? [] : partProps.args),
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
return reactive(part);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* replaces quotes with escaped quotes
|
|
243
|
+
*/
|
|
244
|
+
function escapeId(id: string) {
|
|
245
|
+
return id.replace(/"/g, '\\"');
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Convert a refkey to a symbol ref using the current binder.
|
|
250
|
+
*/
|
|
251
|
+
function getSymbolForRefkey(refkey: Refkey) {
|
|
252
|
+
const binder = useBinder();
|
|
253
|
+
return binder!.getSymbolForRefkey(refkey);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Check if the given symbol is nullish.
|
|
258
|
+
*/
|
|
259
|
+
function isNullish(symbol: OutputSymbol): boolean {
|
|
260
|
+
return !!((symbol as TSOutputSymbol).tsFlags & TSSymbolFlags.Nullish);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Formatting of call chains (i.e. member expressions which have more than one
|
|
265
|
+
* call in them). The general approach is that line breaks occur after each
|
|
266
|
+
* call, and there is only one call per line. When there are non-call elements,
|
|
267
|
+
* they occur prior to the call part. The first part of the member expression
|
|
268
|
+
* contains all but the last non-call part.
|
|
269
|
+
*
|
|
270
|
+
* The following is an example of proper formatting:
|
|
271
|
+
*
|
|
272
|
+
* ```ts
|
|
273
|
+
* z.dummy // all but the last non-call part for the first element
|
|
274
|
+
* .object({ // the first call part with line break after
|
|
275
|
+
* a: 1,
|
|
276
|
+
* })
|
|
277
|
+
* .dummy.partial() // the next call part with non-call parts before it
|
|
278
|
+
* ```
|
|
279
|
+
*/
|
|
280
|
+
function formatCallChain(parts: PartDescriptor[]): Children {
|
|
281
|
+
return computed(() => {
|
|
282
|
+
const expression: Children[] = [];
|
|
283
|
+
|
|
284
|
+
// break the expression into parts.
|
|
285
|
+
const chunks: PartDescriptor[][] = [];
|
|
286
|
+
|
|
287
|
+
// the first part is all the non-call parts
|
|
288
|
+
let partIndex = 0;
|
|
289
|
+
|
|
290
|
+
function pushPart() {
|
|
291
|
+
const part = parts[partIndex];
|
|
292
|
+
if (!part) throw new Error("No part to push");
|
|
293
|
+
chunks.at(-1)!.push(part);
|
|
294
|
+
partIndex++;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function pushChunk() {
|
|
298
|
+
chunks.push([]);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// For the first chunk, take all the non-call parts except the last one
|
|
302
|
+
// and put them in a chunk.
|
|
303
|
+
pushChunk();
|
|
304
|
+
while (
|
|
305
|
+
partIndex < parts.length &&
|
|
306
|
+
(partIndex === parts.length - 1 ||
|
|
307
|
+
chunks.at(-1)!.length === 0 ||
|
|
308
|
+
parts[partIndex + 1].args === undefined)
|
|
309
|
+
) {
|
|
310
|
+
pushPart();
|
|
311
|
+
if (chunks.at(-1)!.at(-1)!.args !== undefined) {
|
|
312
|
+
// the first segment always ends after we see a call
|
|
313
|
+
// if we happen to take one
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// then for all remaining parts, collect all the non-call parts and end with
|
|
319
|
+
// a call chunk
|
|
320
|
+
while (partIndex < parts.length) {
|
|
321
|
+
pushChunk();
|
|
322
|
+
while (partIndex < parts.length && !parts[partIndex].args) {
|
|
323
|
+
pushPart();
|
|
324
|
+
}
|
|
325
|
+
while (partIndex < parts.length && parts[partIndex].args) {
|
|
326
|
+
pushPart();
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
for (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
|
|
331
|
+
const chunk = chunks[chunkIndex];
|
|
332
|
+
const chunkExpression = [];
|
|
333
|
+
for (let partIndex = 0; partIndex < chunk.length; partIndex++) {
|
|
334
|
+
if (chunkIndex === 0 && partIndex === 0) {
|
|
335
|
+
// first part is just gonna be the id
|
|
336
|
+
const firstPart =
|
|
337
|
+
isIdPartDescriptor(chunk[0]) ? chunk[0].id : chunk[0].index;
|
|
338
|
+
chunkExpression.push(firstPart);
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
const part = chunk[partIndex];
|
|
342
|
+
const prevPart =
|
|
343
|
+
partIndex === 0 ?
|
|
344
|
+
chunks[chunkIndex - 1].at(-1)!
|
|
345
|
+
: chunk[partIndex - 1];
|
|
346
|
+
|
|
347
|
+
if (part.args !== undefined) {
|
|
348
|
+
// For parts with only args (no name), append function call directly with appropriate nullish operator
|
|
349
|
+
chunkExpression.push(formatCallExpr(prevPart, part));
|
|
350
|
+
} else if (part.accessStyle === "dot") {
|
|
351
|
+
chunkExpression.push(formatDotAccess(prevPart, part));
|
|
89
352
|
} else {
|
|
90
|
-
|
|
353
|
+
// bracket notation - don't include the dot
|
|
354
|
+
chunkExpression.push(formatArrayAccess(prevPart, part));
|
|
91
355
|
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
expression.push(
|
|
359
|
+
chunkIndex === 0 ? chunkExpression : (
|
|
360
|
+
<>
|
|
361
|
+
<sbr />
|
|
362
|
+
{chunkExpression}
|
|
363
|
+
</>
|
|
364
|
+
),
|
|
96
365
|
);
|
|
97
|
-
part.accessStyle = computed(() => {
|
|
98
|
-
return accessStyleForMemberName(partProps.symbol!.name);
|
|
99
|
-
});
|
|
100
|
-
part.base = toRef(partProps.symbol, "name");
|
|
101
|
-
} else if (partProps.args) {
|
|
102
|
-
part.args = partProps.args;
|
|
103
366
|
}
|
|
104
367
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// construct a member expression from the parts. When a part is nullish,
|
|
113
|
-
// and there is a subsequent part, we use `?.` instead of `.`. accessStyle determines
|
|
114
|
-
// whether we use dot or bracket notation.
|
|
368
|
+
return (
|
|
369
|
+
<group>
|
|
370
|
+
<indent>{expression}</indent>
|
|
371
|
+
</group>
|
|
372
|
+
);
|
|
373
|
+
});
|
|
374
|
+
}
|
|
115
375
|
|
|
116
|
-
|
|
376
|
+
function formatNonCallChain(parts: PartDescriptor[]): Children {
|
|
377
|
+
return computed(() => {
|
|
117
378
|
const expression: Children[] = [];
|
|
118
379
|
|
|
119
380
|
for (let i = 0; i < parts.length; i++) {
|
|
120
381
|
const part = parts[i];
|
|
121
|
-
const base =
|
|
122
|
-
const hasName = base !== "";
|
|
123
|
-
|
|
382
|
+
const base = isIdPartDescriptor(part) ? part.id : part.index;
|
|
124
383
|
if (i === 0) {
|
|
125
384
|
expression.push(base);
|
|
126
385
|
} else {
|
|
127
386
|
// Determine if we should use nullish operator from previous part
|
|
128
387
|
const prevPart = parts[i - 1];
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
// Handle parts with name vs. parts with only args differently
|
|
132
|
-
if (hasName) {
|
|
133
|
-
// Apply dot or bracket notation based on accessStyle for named parts
|
|
134
|
-
if (part.accessStyle.value === "dot") {
|
|
135
|
-
expression.push(
|
|
136
|
-
<group>
|
|
137
|
-
<indent>
|
|
138
|
-
<sbr />
|
|
139
|
-
{isNullish ? "?." : "."}
|
|
140
|
-
{base}
|
|
141
|
-
</indent>
|
|
142
|
-
</group>,
|
|
143
|
-
);
|
|
144
|
-
} else {
|
|
145
|
-
// bracket notation - don't include the dot
|
|
146
|
-
expression.push(
|
|
147
|
-
<group>
|
|
148
|
-
{isNullish ? "?." : ""}[
|
|
149
|
-
<indent>
|
|
150
|
-
<sbr />
|
|
151
|
-
{JSON.stringify(base)}
|
|
152
|
-
</indent>
|
|
153
|
-
<sbr />]
|
|
154
|
-
</group>,
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
} else if (part.args !== undefined) {
|
|
388
|
+
|
|
389
|
+
if (part.args !== undefined) {
|
|
158
390
|
// For parts with only args (no name), append function call directly with appropriate nullish operator
|
|
159
|
-
expression.push(
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
each={typeof part.args === "boolean" ? [] : (part.args ?? [])}
|
|
166
|
-
comma
|
|
167
|
-
line
|
|
168
|
-
>
|
|
169
|
-
{(arg) => arg}
|
|
170
|
-
</For>
|
|
171
|
-
)
|
|
172
|
-
</group>
|
|
173
|
-
</group>,
|
|
174
|
-
);
|
|
391
|
+
expression.push(formatCallExpr(prevPart, part));
|
|
392
|
+
} else if (part.accessStyle === "dot") {
|
|
393
|
+
expression.push(formatDotAccess(prevPart, part));
|
|
394
|
+
} else {
|
|
395
|
+
// bracket notation - don't include the dot
|
|
396
|
+
expression.push(formatArrayAccess(prevPart, part));
|
|
175
397
|
}
|
|
176
398
|
}
|
|
177
399
|
}
|
|
178
400
|
|
|
179
401
|
return expression;
|
|
180
402
|
});
|
|
181
|
-
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function formatArrayAccess(prevPart: PartDescriptor, part: PartDescriptor) {
|
|
406
|
+
return (
|
|
407
|
+
<group>
|
|
408
|
+
{prevPart.nullish ? "?." : ""}[
|
|
409
|
+
<indent>
|
|
410
|
+
<sbr />
|
|
411
|
+
{part.quoteId && '"'}
|
|
412
|
+
{isIdPartDescriptor(part) ? part.id : part.index}
|
|
413
|
+
{part.quoteId && '"'}
|
|
414
|
+
</indent>
|
|
415
|
+
<sbr />]
|
|
416
|
+
</group>
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function formatDotAccess(prevPart: PartDescriptor, part: PartDescriptor) {
|
|
421
|
+
return (
|
|
422
|
+
<group>
|
|
423
|
+
<indent>
|
|
424
|
+
<sbr />
|
|
425
|
+
{prevPart.nullish ? "?." : "."}
|
|
426
|
+
{isIdPartDescriptor(part) ? part.id : part.index}
|
|
427
|
+
</indent>
|
|
428
|
+
</group>
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function formatCallExpr(prevPart: PartDescriptor, part: PartDescriptor) {
|
|
433
|
+
const args = computed(() => {
|
|
434
|
+
return typeof part.args === "boolean" ? [] : (part.args ?? []);
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
return (
|
|
438
|
+
<group>
|
|
439
|
+
{prevPart.nullish ? "?." : ""}(
|
|
440
|
+
<Show when={args.value.length <= 1}>{args.value[0]}</Show>
|
|
441
|
+
<Show when={args.value.length > 1}>
|
|
442
|
+
<indent>
|
|
443
|
+
<sbr />
|
|
444
|
+
<For each={args} comma line>
|
|
445
|
+
{(arg) => arg}
|
|
446
|
+
</For>
|
|
447
|
+
</indent>
|
|
448
|
+
<sbr />
|
|
449
|
+
</Show>
|
|
450
|
+
)
|
|
451
|
+
</group>
|
|
452
|
+
);
|
|
182
453
|
}
|
|
183
454
|
|
|
184
455
|
function accessStyleForMemberName(name: string) {
|
|
@@ -206,7 +477,20 @@ export interface MemberExpressionPartProps {
|
|
|
206
477
|
/**
|
|
207
478
|
* The identifier for this part of the member expression.
|
|
208
479
|
*/
|
|
209
|
-
id?: string;
|
|
480
|
+
id?: string | number;
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* The index for this part of the member expression. This is used when
|
|
484
|
+
* the part is an array access (i.e. `foo[0]`).
|
|
485
|
+
*/
|
|
486
|
+
index?: number;
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Whether the identifier should be quoted or not. Only needed when
|
|
490
|
+
* providing the `children` prop, otherwise it is determined automatically.
|
|
491
|
+
*/
|
|
492
|
+
quoteId?: boolean;
|
|
493
|
+
|
|
210
494
|
/**
|
|
211
495
|
* A refkey for a symbol whose name becomes the identifier.
|
|
212
496
|
*/
|
|
@@ -224,6 +508,12 @@ export interface MemberExpressionPartProps {
|
|
|
224
508
|
* This part is nullish. Subsequent parts use conditional access operators.
|
|
225
509
|
*/
|
|
226
510
|
nullish?: boolean;
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* The contents of this part. When passed, overrides the other props except
|
|
514
|
+
* nullish.
|
|
515
|
+
*/
|
|
516
|
+
children?: Children;
|
|
227
517
|
}
|
|
228
518
|
/**
|
|
229
519
|
* A part of a member expression. Each part can provide one of the following
|
|
@@ -233,6 +523,15 @@ export interface MemberExpressionPartProps {
|
|
|
233
523
|
* * **refkey**: A refkey for a symbol whose name becomes the identifier
|
|
234
524
|
* * **symbol**: a symbol whose name becomes the identifier part
|
|
235
525
|
* * **args**: create a method call with the given args
|
|
526
|
+
* * **children**: arbitrary contents for the identifier part.
|
|
527
|
+
*
|
|
528
|
+
* Each part can have a nullish prop, which indicates that the part may be null
|
|
529
|
+
* or undefined.
|
|
530
|
+
*
|
|
531
|
+
* Each part can also have a quoteId prop, which indicates that the identifier
|
|
532
|
+
* of the part should be quoted (i.e. `["foo"]` instead of `.foo`). This is only
|
|
533
|
+
* necessary when providing the children prop, otherwise it is determined
|
|
534
|
+
* automatically.
|
|
236
535
|
*/
|
|
237
536
|
MemberExpression.Part = function (props: MemberExpressionPartProps) {
|
|
238
537
|
/**
|
|
@@ -240,3 +539,17 @@ MemberExpression.Part = function (props: MemberExpressionPartProps) {
|
|
|
240
539
|
* the `MemberExpression` component.
|
|
241
540
|
*/
|
|
242
541
|
};
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Check if the given id is a numeric identifier.
|
|
545
|
+
*
|
|
546
|
+
*/
|
|
547
|
+
function isNumericIdentifier(id: Children) {
|
|
548
|
+
return typeof id === "number";
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
function isIdPartDescriptor(
|
|
552
|
+
part: PartDescriptor,
|
|
553
|
+
): part is PartDescriptorWithId {
|
|
554
|
+
return "id" in part;
|
|
555
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {
|
|
2
|
+
emitSymbol,
|
|
3
|
+
instantiateTakenMembersTo as instantiateTakenSymbolsTo,
|
|
4
|
+
OutputSymbolFlags,
|
|
5
|
+
} from "@alloy-js/core";
|
|
6
|
+
import { TSOutputSymbol } from "../symbols/ts-output-symbol.js";
|
|
7
|
+
import {
|
|
8
|
+
FunctionCallExpression,
|
|
9
|
+
FunctionCallExpressionProps,
|
|
10
|
+
} from "./FunctionCallExpression.jsx";
|
|
11
|
+
|
|
12
|
+
export interface NewExpressionProps extends FunctionCallExpressionProps {}
|
|
13
|
+
|
|
14
|
+
export function NewExpression(props: NewExpressionProps) {
|
|
15
|
+
const sym = new TSOutputSymbol("", {
|
|
16
|
+
flags: OutputSymbolFlags.Transient,
|
|
17
|
+
});
|
|
18
|
+
instantiateTakenSymbolsTo(sym);
|
|
19
|
+
emitSymbol(sym);
|
|
20
|
+
return (
|
|
21
|
+
<>
|
|
22
|
+
new <FunctionCallExpression {...props} />
|
|
23
|
+
</>
|
|
24
|
+
);
|
|
25
|
+
}
|