@alloy-js/typescript 0.8.0 → 0.10.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.
@@ -0,0 +1,237 @@
1
+ import {
2
+ Children,
3
+ childrenArray,
4
+ computed,
5
+ For,
6
+ isComponentCreator,
7
+ OutputSymbol,
8
+ Ref,
9
+ ref,
10
+ Refkey,
11
+ toRef,
12
+ useBinder,
13
+ } from "@alloy-js/core";
14
+ import { isValidJSIdentifier } from "../utils.js";
15
+
16
+ export interface MemberExpressionProps {
17
+ children: Children;
18
+ }
19
+
20
+ interface PartDescriptor {
21
+ base: string | Ref<string>;
22
+ accessStyle: Ref<"dot" | "bracket">;
23
+ nullish: Ref<boolean>;
24
+ args?: Children[] | boolean;
25
+ }
26
+
27
+ /**
28
+ * Create a member expression from parts. Each part can provide one of
29
+ * the following:
30
+ *
31
+ * * **id**: The identifier for the member expression part
32
+ * * **refkey**: A refkey for a symbol whose name becomes the identifier
33
+ * * **symbol**: a symbol whose name becomes the identifier part
34
+ * * **args**: create a method call with the given args
35
+ *
36
+ * Additionally, a part can be `nullish`, which means subsequent parts
37
+ * will use the appropriate conditional access operator e.g. `?.`.
38
+ *
39
+ * @example
40
+ *
41
+ * ```tsx
42
+ * <MemberExpression>
43
+ * <MemberExpression.Part id="base" />
44
+ * <MemberExpression.Part refkey={rk} nullish />
45
+ * <MemberExpression.Part symbol={sym} />
46
+ * <MemberExpression.Part args={["hello", "world"]} />
47
+ * </MemberExpression>
48
+ * ```
49
+ *
50
+ * Assuming `rk` is a refkey to a symbol name "prop1", and `sym` is a symbol
51
+ * with a name of "prop2", this will render:
52
+ *
53
+ * ```ts
54
+ * base.prop1?.prop2("hello", "world")
55
+ * ```
56
+ */
57
+ export function MemberExpression(props: MemberExpressionProps): Children {
58
+ const children = flattenMemberExpression(childrenArray(() => props.children));
59
+ const parts: PartDescriptor[] = [];
60
+ for (const child of children) {
61
+ if (!isComponentCreator(child, MemberExpression.Part)) {
62
+ // we ignore non-parts
63
+ continue;
64
+ }
65
+
66
+ const partProps = child.props;
67
+ const part: PartDescriptor = {
68
+ base: "",
69
+ accessStyle: ref("dot"),
70
+ nullish: ref(!!partProps.nullish),
71
+ };
72
+
73
+ if (partProps.id) {
74
+ part.base = partProps.id;
75
+ part.accessStyle.value = accessStyleForMemberName(partProps.id);
76
+ } else if (partProps.refkey) {
77
+ const binder = useBinder();
78
+ const symbolRef = binder.getSymbolForRefkey(partProps.refkey);
79
+ part.base = computed(() => {
80
+ if (symbolRef.value) {
81
+ part.nullish.value = !!symbolRef.value.metadata.nullish;
82
+ part.accessStyle.value = accessStyleForMemberName(
83
+ symbolRef.value.name,
84
+ );
85
+ return symbolRef.value.name;
86
+ } else {
87
+ return "<unresolved symbol>";
88
+ }
89
+ });
90
+ } else if (partProps.symbol) {
91
+ part.nullish.value = !!partProps.symbol.metadata.nullish;
92
+ part.accessStyle = computed(() => {
93
+ return accessStyleForMemberName(partProps.symbol!.name);
94
+ });
95
+ part.base = toRef(partProps.symbol, "name");
96
+ } else if (partProps.args) {
97
+ part.args = partProps.args;
98
+ }
99
+
100
+ parts.push(part);
101
+ }
102
+
103
+ if (parts.length === 0) {
104
+ return <></>;
105
+ }
106
+
107
+ // construct a member expression from the parts. When a part is nullish,
108
+ // and there is a subsequent part, we use `?.` instead of `.`. accessStyle determines
109
+ // whether we use dot or bracket notation.
110
+
111
+ const result = computed(() => {
112
+ const expression: Children[] = [];
113
+
114
+ for (let i = 0; i < parts.length; i++) {
115
+ const part = parts[i];
116
+ const base = typeof part.base === "string" ? part.base : part.base.value;
117
+ const hasName = base !== "";
118
+
119
+ if (i === 0) {
120
+ expression.push(base);
121
+ } else {
122
+ // Determine if we should use nullish operator from previous part
123
+ const prevPart = parts[i - 1];
124
+ const isNullish = prevPart.nullish.value;
125
+
126
+ // Handle parts with name vs. parts with only args differently
127
+ if (hasName) {
128
+ // Apply dot or bracket notation based on accessStyle for named parts
129
+ if (part.accessStyle.value === "dot") {
130
+ expression.push(
131
+ <group>
132
+ <indent>
133
+ <sbr />
134
+ {isNullish ? "?." : "."}
135
+ {base}
136
+ </indent>
137
+ </group>,
138
+ );
139
+ } else {
140
+ // bracket notation - don't include the dot
141
+ expression.push(
142
+ <group>
143
+ {isNullish ? "?." : ""}[
144
+ <indent>
145
+ <sbr />
146
+ {JSON.stringify(base)}
147
+ </indent>
148
+ <sbr />]
149
+ </group>,
150
+ );
151
+ }
152
+ } else if (part.args !== undefined) {
153
+ // For parts with only args (no name), append function call directly with appropriate nullish operator
154
+ expression.push(
155
+ <group>
156
+ {isNullish ? "?." : ""}
157
+ <group>
158
+ (
159
+ <For
160
+ each={typeof part.args === "boolean" ? [] : (part.args ?? [])}
161
+ comma
162
+ line
163
+ >
164
+ {(arg) => arg}
165
+ </For>
166
+ )
167
+ </group>
168
+ </group>,
169
+ );
170
+ }
171
+ }
172
+ }
173
+
174
+ return expression;
175
+ });
176
+ return <>{result.value}</>;
177
+ }
178
+
179
+ function accessStyleForMemberName(name: string) {
180
+ if (isValidJSIdentifier(name)) {
181
+ return "dot";
182
+ }
183
+ return "bracket";
184
+ }
185
+
186
+ function flattenMemberExpression(children: Children[]): Children[] {
187
+ const flattened: Children[] = [];
188
+ for (const child of children) {
189
+ if (isComponentCreator(child, MemberExpression)) {
190
+ flattened.push(
191
+ ...flattenMemberExpression(childrenArray(() => child.props.children)),
192
+ );
193
+ } else {
194
+ flattened.push(child);
195
+ }
196
+ }
197
+ return flattened;
198
+ }
199
+
200
+ export interface MemberExpressionPartProps {
201
+ /**
202
+ * The identifier for this part of the member expression.
203
+ */
204
+ id?: string;
205
+ /**
206
+ * A refkey for a symbol whose name becomes the identifier.
207
+ */
208
+ refkey?: Refkey;
209
+ /**
210
+ * A symbol whose name becomes the identifier.
211
+ */
212
+ symbol?: OutputSymbol;
213
+ /**
214
+ * Arguments to construct a call expression.
215
+ */
216
+ args?: Children[] | boolean;
217
+
218
+ /**
219
+ * This part is nullish. Subsequent parts use conditional access operators.
220
+ */
221
+ nullish?: boolean;
222
+ }
223
+ /**
224
+ * A part of a member expression. Each part can provide one of the following
225
+ * props:
226
+ *
227
+ * * **id**: The identifier for the member expression part
228
+ * * **refkey**: A refkey for a symbol whose name becomes the identifier
229
+ * * **symbol**: a symbol whose name becomes the identifier part
230
+ * * **args**: create a method call with the given args
231
+ */
232
+ MemberExpression.Part = function (props: MemberExpressionPartProps) {
233
+ /**
234
+ * This component does nothing except hold props which are retrieved by
235
+ * the `MemberExpression` component.
236
+ */
237
+ };
@@ -92,7 +92,7 @@ export interface ObjectPropertyProps {
92
92
  value?: Children;
93
93
  jsValue?: unknown;
94
94
  children?: Children;
95
- refkey?: Refkey;
95
+ refkey?: Refkey | Refkey[];
96
96
  }
97
97
 
98
98
  export function ObjectProperty(props: ObjectPropertyProps) {
@@ -17,6 +17,7 @@ export * from "./JSDoc.jsx";
17
17
  export * from "./JSDocComment.jsx";
18
18
  export * from "./JSDocExample.jsx";
19
19
  export * from "./MemberChainExpression.jsx";
20
+ export * from "./MemberExpression.jsx";
20
21
  export * from "./ObjectExpression.js";
21
22
  export * from "./PackageDirectory.js";
22
23
  export * from "./PackageJson.js";
@@ -23,6 +23,7 @@ export const InterfaceMember = stc(base.InterfaceMember);
23
23
  export const JSDoc = stc(base.JSDoc);
24
24
  export const JSDocExample = stc(base.JSDocExample);
25
25
  export const JSDocComment = stc(base.JSDocComment);
26
+ export const MemberExpression = stc(base.MemberExpression);
26
27
  export const MemberChainExpression = stc(base.MemberChainExpression);
27
28
  export const ObjectExpression = stc(base.ObjectExpression);
28
29
  export const ObjectProperty = stc(base.ObjectProperty);
package/temp/api.json CHANGED
@@ -5936,6 +5936,317 @@
5936
5936
  ],
5937
5937
  "extendsTokenRanges": []
5938
5938
  },
5939
+ {
5940
+ "kind": "Function",
5941
+ "canonicalReference": "@alloy-js/typescript!MemberExpression:function(1)",
5942
+ "docComment": "/**\n * Create a member expression from parts. Each part can provide one of\n * the following:\n *\n * * **id**: The identifier for the member expression part\n * * **refkey**: A refkey for a symbol whose name becomes the identifier\n * * **symbol**: a symbol whose name becomes the identifier part\n * * **args**: create a method call with the given args\n *\n * Additionally, a part can be `nullish`, which means subsequent parts\n * will use the appropriate conditional access operator e.g. `?.`.\n *\n * @example\n *\n *\n *\n * ```tsx\n * <MemberExpression>\n * <MemberExpression.Part id=\"base\" />\n * <MemberExpression.Part refkey={rk} nullish />\n * <MemberExpression.Part symbol={sym} />\n * <MemberExpression.Part args={[\"hello\", \"world\"]} />\n * </MemberExpression>\n * ```\n *\n *\n * Assuming `rk` is a refkey to a symbol name \"prop1\", and `sym` is a symbol\n * with a name of \"prop2\", this will render:\n *\n * ```ts\n * base.prop1?.prop2(\"hello\", \"world\")\n * ```\n *\n */\n",
5943
+ "excerptTokens": [
5944
+ {
5945
+ "kind": "Content",
5946
+ "text": "export declare function MemberExpression(props: "
5947
+ },
5948
+ {
5949
+ "kind": "Reference",
5950
+ "text": "MemberExpressionProps",
5951
+ "canonicalReference": "@alloy-js/typescript!MemberExpressionProps:interface"
5952
+ },
5953
+ {
5954
+ "kind": "Content",
5955
+ "text": "): "
5956
+ },
5957
+ {
5958
+ "kind": "Reference",
5959
+ "text": "Children",
5960
+ "canonicalReference": "@alloy-js/core!Children:type"
5961
+ },
5962
+ {
5963
+ "kind": "Content",
5964
+ "text": ";"
5965
+ }
5966
+ ],
5967
+ "fileUrlPath": "src/components/MemberExpression.tsx",
5968
+ "returnTypeTokenRange": {
5969
+ "startIndex": 3,
5970
+ "endIndex": 4
5971
+ },
5972
+ "releaseTag": "Public",
5973
+ "overloadIndex": 1,
5974
+ "parameters": [
5975
+ {
5976
+ "parameterName": "props",
5977
+ "parameterTypeTokenRange": {
5978
+ "startIndex": 1,
5979
+ "endIndex": 2
5980
+ },
5981
+ "isOptional": false
5982
+ }
5983
+ ],
5984
+ "name": "MemberExpression"
5985
+ },
5986
+ {
5987
+ "kind": "Namespace",
5988
+ "canonicalReference": "@alloy-js/typescript!MemberExpression:namespace",
5989
+ "docComment": "",
5990
+ "excerptTokens": [
5991
+ {
5992
+ "kind": "Content",
5993
+ "text": "export declare namespace MemberExpression "
5994
+ }
5995
+ ],
5996
+ "fileUrlPath": "src/components/MemberExpression.tsx",
5997
+ "releaseTag": "Public",
5998
+ "name": "MemberExpression",
5999
+ "preserveMemberOrder": false,
6000
+ "members": [
6001
+ {
6002
+ "kind": "Function",
6003
+ "canonicalReference": "@alloy-js/typescript!MemberExpression.Part:function(1)",
6004
+ "docComment": "",
6005
+ "excerptTokens": [
6006
+ {
6007
+ "kind": "Content",
6008
+ "text": "Part: (props: "
6009
+ },
6010
+ {
6011
+ "kind": "Reference",
6012
+ "text": "MemberExpressionPartProps",
6013
+ "canonicalReference": "@alloy-js/typescript!MemberExpressionPartProps:interface"
6014
+ },
6015
+ {
6016
+ "kind": "Content",
6017
+ "text": ") => "
6018
+ },
6019
+ {
6020
+ "kind": "Content",
6021
+ "text": "void"
6022
+ }
6023
+ ],
6024
+ "returnTypeTokenRange": {
6025
+ "startIndex": 3,
6026
+ "endIndex": 4
6027
+ },
6028
+ "releaseTag": "Public",
6029
+ "overloadIndex": 1,
6030
+ "parameters": [
6031
+ {
6032
+ "parameterName": "props",
6033
+ "parameterTypeTokenRange": {
6034
+ "startIndex": 1,
6035
+ "endIndex": 2
6036
+ },
6037
+ "isOptional": false
6038
+ }
6039
+ ],
6040
+ "name": "Part"
6041
+ }
6042
+ ]
6043
+ },
6044
+ {
6045
+ "kind": "Interface",
6046
+ "canonicalReference": "@alloy-js/typescript!MemberExpressionPartProps:interface",
6047
+ "docComment": "",
6048
+ "excerptTokens": [
6049
+ {
6050
+ "kind": "Content",
6051
+ "text": "export interface MemberExpressionPartProps "
6052
+ }
6053
+ ],
6054
+ "fileUrlPath": "src/components/MemberExpression.tsx",
6055
+ "releaseTag": "Public",
6056
+ "name": "MemberExpressionPartProps",
6057
+ "preserveMemberOrder": false,
6058
+ "members": [
6059
+ {
6060
+ "kind": "PropertySignature",
6061
+ "canonicalReference": "@alloy-js/typescript!MemberExpressionPartProps#args:member",
6062
+ "docComment": "/**\n * Arguments to construct a call expression.\n */\n",
6063
+ "excerptTokens": [
6064
+ {
6065
+ "kind": "Content",
6066
+ "text": "args?: "
6067
+ },
6068
+ {
6069
+ "kind": "Reference",
6070
+ "text": "Children",
6071
+ "canonicalReference": "@alloy-js/core!Children:type"
6072
+ },
6073
+ {
6074
+ "kind": "Content",
6075
+ "text": "[] | boolean"
6076
+ },
6077
+ {
6078
+ "kind": "Content",
6079
+ "text": ";"
6080
+ }
6081
+ ],
6082
+ "isReadonly": false,
6083
+ "isOptional": true,
6084
+ "releaseTag": "Public",
6085
+ "name": "args",
6086
+ "propertyTypeTokenRange": {
6087
+ "startIndex": 1,
6088
+ "endIndex": 3
6089
+ }
6090
+ },
6091
+ {
6092
+ "kind": "PropertySignature",
6093
+ "canonicalReference": "@alloy-js/typescript!MemberExpressionPartProps#id:member",
6094
+ "docComment": "/**\n * The identifier for this part of the member expression.\n */\n",
6095
+ "excerptTokens": [
6096
+ {
6097
+ "kind": "Content",
6098
+ "text": "id?: "
6099
+ },
6100
+ {
6101
+ "kind": "Content",
6102
+ "text": "string"
6103
+ },
6104
+ {
6105
+ "kind": "Content",
6106
+ "text": ";"
6107
+ }
6108
+ ],
6109
+ "isReadonly": false,
6110
+ "isOptional": true,
6111
+ "releaseTag": "Public",
6112
+ "name": "id",
6113
+ "propertyTypeTokenRange": {
6114
+ "startIndex": 1,
6115
+ "endIndex": 2
6116
+ }
6117
+ },
6118
+ {
6119
+ "kind": "PropertySignature",
6120
+ "canonicalReference": "@alloy-js/typescript!MemberExpressionPartProps#nullish:member",
6121
+ "docComment": "/**\n * This part is nullish. Subsequent parts use conditional access operators.\n */\n",
6122
+ "excerptTokens": [
6123
+ {
6124
+ "kind": "Content",
6125
+ "text": "nullish?: "
6126
+ },
6127
+ {
6128
+ "kind": "Content",
6129
+ "text": "boolean"
6130
+ },
6131
+ {
6132
+ "kind": "Content",
6133
+ "text": ";"
6134
+ }
6135
+ ],
6136
+ "isReadonly": false,
6137
+ "isOptional": true,
6138
+ "releaseTag": "Public",
6139
+ "name": "nullish",
6140
+ "propertyTypeTokenRange": {
6141
+ "startIndex": 1,
6142
+ "endIndex": 2
6143
+ }
6144
+ },
6145
+ {
6146
+ "kind": "PropertySignature",
6147
+ "canonicalReference": "@alloy-js/typescript!MemberExpressionPartProps#refkey:member",
6148
+ "docComment": "/**\n * A refkey for a symbol whose name becomes the identifier.\n */\n",
6149
+ "excerptTokens": [
6150
+ {
6151
+ "kind": "Content",
6152
+ "text": "refkey?: "
6153
+ },
6154
+ {
6155
+ "kind": "Reference",
6156
+ "text": "Refkey",
6157
+ "canonicalReference": "@alloy-js/core!Refkey:type"
6158
+ },
6159
+ {
6160
+ "kind": "Content",
6161
+ "text": ";"
6162
+ }
6163
+ ],
6164
+ "isReadonly": false,
6165
+ "isOptional": true,
6166
+ "releaseTag": "Public",
6167
+ "name": "refkey",
6168
+ "propertyTypeTokenRange": {
6169
+ "startIndex": 1,
6170
+ "endIndex": 2
6171
+ }
6172
+ },
6173
+ {
6174
+ "kind": "PropertySignature",
6175
+ "canonicalReference": "@alloy-js/typescript!MemberExpressionPartProps#symbol:member",
6176
+ "docComment": "/**\n * A symbol whose name becomes the identifier.\n */\n",
6177
+ "excerptTokens": [
6178
+ {
6179
+ "kind": "Content",
6180
+ "text": "symbol?: "
6181
+ },
6182
+ {
6183
+ "kind": "Reference",
6184
+ "text": "OutputSymbol",
6185
+ "canonicalReference": "@alloy-js/core!OutputSymbol:interface"
6186
+ },
6187
+ {
6188
+ "kind": "Content",
6189
+ "text": ";"
6190
+ }
6191
+ ],
6192
+ "isReadonly": false,
6193
+ "isOptional": true,
6194
+ "releaseTag": "Public",
6195
+ "name": "symbol",
6196
+ "propertyTypeTokenRange": {
6197
+ "startIndex": 1,
6198
+ "endIndex": 2
6199
+ }
6200
+ }
6201
+ ],
6202
+ "extendsTokenRanges": []
6203
+ },
6204
+ {
6205
+ "kind": "Interface",
6206
+ "canonicalReference": "@alloy-js/typescript!MemberExpressionProps:interface",
6207
+ "docComment": "",
6208
+ "excerptTokens": [
6209
+ {
6210
+ "kind": "Content",
6211
+ "text": "export interface MemberExpressionProps "
6212
+ }
6213
+ ],
6214
+ "fileUrlPath": "src/components/MemberExpression.tsx",
6215
+ "releaseTag": "Public",
6216
+ "name": "MemberExpressionProps",
6217
+ "preserveMemberOrder": false,
6218
+ "members": [
6219
+ {
6220
+ "kind": "PropertySignature",
6221
+ "canonicalReference": "@alloy-js/typescript!MemberExpressionProps#children:member",
6222
+ "docComment": "",
6223
+ "excerptTokens": [
6224
+ {
6225
+ "kind": "Content",
6226
+ "text": "children: "
6227
+ },
6228
+ {
6229
+ "kind": "Reference",
6230
+ "text": "Children",
6231
+ "canonicalReference": "@alloy-js/core!Children:type"
6232
+ },
6233
+ {
6234
+ "kind": "Content",
6235
+ "text": ";"
6236
+ }
6237
+ ],
6238
+ "isReadonly": false,
6239
+ "isOptional": false,
6240
+ "releaseTag": "Public",
6241
+ "name": "children",
6242
+ "propertyTypeTokenRange": {
6243
+ "startIndex": 1,
6244
+ "endIndex": 2
6245
+ }
6246
+ }
6247
+ ],
6248
+ "extendsTokenRanges": []
6249
+ },
5939
6250
  {
5940
6251
  "kind": "Interface",
5941
6252
  "canonicalReference": "@alloy-js/typescript!ModuleSymbolsDescriptor:interface",
@@ -7949,6 +8260,19 @@
7949
8260
  "text": "Refkey",
7950
8261
  "canonicalReference": "@alloy-js/core!Refkey:type"
7951
8262
  },
8263
+ {
8264
+ "kind": "Content",
8265
+ "text": " | "
8266
+ },
8267
+ {
8268
+ "kind": "Reference",
8269
+ "text": "Refkey",
8270
+ "canonicalReference": "@alloy-js/core!Refkey:type"
8271
+ },
8272
+ {
8273
+ "kind": "Content",
8274
+ "text": "[]"
8275
+ },
7952
8276
  {
7953
8277
  "kind": "Content",
7954
8278
  "text": ";"
@@ -7960,7 +8284,7 @@
7960
8284
  "name": "refkey",
7961
8285
  "propertyTypeTokenRange": {
7962
8286
  "startIndex": 1,
7963
- "endIndex": 2
8287
+ "endIndex": 5
7964
8288
  }
7965
8289
  },
7966
8290
  {