@alloy-js/typescript 0.5.0 → 0.7.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.
Files changed (174) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/src/components/ArrayExpression.d.ts +1 -1
  3. package/dist/src/components/ArrayExpression.d.ts.map +1 -1
  4. package/dist/src/components/ArrayExpression.js +18 -28
  5. package/dist/src/components/ArrayExpression.js.map +1 -1
  6. package/dist/src/components/BarrelFile.d.ts.map +1 -1
  7. package/dist/src/components/BarrelFile.js +11 -9
  8. package/dist/src/components/BarrelFile.js.map +1 -1
  9. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  10. package/dist/src/components/ClassDeclaration.js +8 -9
  11. package/dist/src/components/ClassDeclaration.js.map +1 -1
  12. package/dist/src/components/CommaList.d.ts +12 -0
  13. package/dist/src/components/CommaList.d.ts.map +1 -0
  14. package/dist/src/components/CommaList.js +19 -0
  15. package/dist/src/components/CommaList.js.map +1 -0
  16. package/dist/src/components/Declaration.d.ts +4 -0
  17. package/dist/src/components/Declaration.d.ts.map +1 -1
  18. package/dist/src/components/Declaration.js +3 -2
  19. package/dist/src/components/Declaration.js.map +1 -1
  20. package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
  21. package/dist/src/components/EnumDeclaration.js +21 -18
  22. package/dist/src/components/EnumDeclaration.js.map +1 -1
  23. package/dist/src/components/EnumMember.d.ts.map +1 -1
  24. package/dist/src/components/EnumMember.js.map +1 -1
  25. package/dist/src/components/ExportStatement.d.ts.map +1 -1
  26. package/dist/src/components/ExportStatement.js +3 -1
  27. package/dist/src/components/ExportStatement.js.map +1 -1
  28. package/dist/src/components/FunctionCallExpression.d.ts +2 -2
  29. package/dist/src/components/FunctionCallExpression.d.ts.map +1 -1
  30. package/dist/src/components/FunctionCallExpression.js +26 -9
  31. package/dist/src/components/FunctionCallExpression.js.map +1 -1
  32. package/dist/src/components/FunctionDeclaration.d.ts +10 -2
  33. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  34. package/dist/src/components/FunctionDeclaration.js +136 -33
  35. package/dist/src/components/FunctionDeclaration.js.map +1 -1
  36. package/dist/src/components/ImportStatement.d.ts.map +1 -1
  37. package/dist/src/components/ImportStatement.js +25 -31
  38. package/dist/src/components/ImportStatement.js.map +1 -1
  39. package/dist/src/components/Interface.d.ts +1 -1
  40. package/dist/src/components/Interface.d.ts.map +1 -1
  41. package/dist/src/components/Interface.js +5 -5
  42. package/dist/src/components/Interface.js.map +1 -1
  43. package/dist/src/components/JSDoc.d.ts +10 -0
  44. package/dist/src/components/JSDoc.d.ts.map +1 -0
  45. package/dist/src/components/JSDoc.js +24 -0
  46. package/dist/src/components/JSDoc.js.map +1 -0
  47. package/dist/src/components/JSDocComment.d.ts +11 -0
  48. package/dist/src/components/JSDocComment.d.ts.map +1 -0
  49. package/dist/src/components/JSDocComment.js +16 -0
  50. package/dist/src/components/JSDocComment.js.map +1 -0
  51. package/dist/src/components/JSDocExample.d.ts +12 -0
  52. package/dist/src/components/JSDocExample.d.ts.map +1 -0
  53. package/dist/src/components/JSDocExample.js +26 -0
  54. package/dist/src/components/JSDocExample.js.map +1 -0
  55. package/dist/src/components/JSDocParagraph.d.ts +10 -0
  56. package/dist/src/components/JSDocParagraph.d.ts.map +1 -0
  57. package/dist/src/components/JSDocParagraph.js +23 -0
  58. package/dist/src/components/JSDocParagraph.js.map +1 -0
  59. package/dist/src/components/MemberChainExpression.d.ts +11 -0
  60. package/dist/src/components/MemberChainExpression.d.ts.map +1 -0
  61. package/dist/src/components/MemberChainExpression.js +100 -0
  62. package/dist/src/components/MemberChainExpression.js.map +1 -0
  63. package/dist/src/components/ObjectExpression.d.ts +1 -1
  64. package/dist/src/components/ObjectExpression.d.ts.map +1 -1
  65. package/dist/src/components/ObjectExpression.js +45 -42
  66. package/dist/src/components/ObjectExpression.js.map +1 -1
  67. package/dist/src/components/PackageDirectory.d.ts +1 -3
  68. package/dist/src/components/PackageDirectory.d.ts.map +1 -1
  69. package/dist/src/components/PackageDirectory.js +9 -16
  70. package/dist/src/components/PackageDirectory.js.map +1 -1
  71. package/dist/src/components/PackageJson.d.ts +39 -0
  72. package/dist/src/components/PackageJson.d.ts.map +1 -1
  73. package/dist/src/components/PackageJson.js +30 -12
  74. package/dist/src/components/PackageJson.js.map +1 -1
  75. package/dist/src/components/Reference.js +1 -1
  76. package/dist/src/components/Reference.js.map +1 -1
  77. package/dist/src/components/SourceFile.d.ts.map +1 -1
  78. package/dist/src/components/SourceFile.js +13 -6
  79. package/dist/src/components/SourceFile.js.map +1 -1
  80. package/dist/src/components/TsConfigJson.d.ts.map +1 -1
  81. package/dist/src/components/TsConfigJson.js.map +1 -1
  82. package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
  83. package/dist/src/components/TypeDeclaration.js.map +1 -1
  84. package/dist/src/components/ValueExpression.d.ts.map +1 -1
  85. package/dist/src/components/ValueExpression.js.map +1 -1
  86. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  87. package/dist/src/components/VarDeclaration.js +5 -4
  88. package/dist/src/components/VarDeclaration.js.map +1 -1
  89. package/dist/src/components/index.d.ts +2 -1
  90. package/dist/src/components/index.d.ts.map +1 -1
  91. package/dist/src/components/index.js +2 -1
  92. package/dist/src/components/index.js.map +1 -1
  93. package/dist/src/components/stc/index.d.ts +21 -21
  94. package/dist/src/components/stc/index.d.ts.map +1 -1
  95. package/dist/src/components/stc/index.js +1 -1
  96. package/dist/src/components/stc/index.js.map +1 -1
  97. package/dist/src/symbols/reference.d.ts.map +1 -1
  98. package/dist/src/symbols/reference.js +5 -2
  99. package/dist/src/symbols/reference.js.map +1 -1
  100. package/dist/src/symbols/ts-output-symbol.d.ts +2 -1
  101. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  102. package/dist/src/symbols/ts-output-symbol.js +4 -1
  103. package/dist/src/symbols/ts-output-symbol.js.map +1 -1
  104. package/dist/src/symbols/ts-package-scope.d.ts +0 -15
  105. package/dist/src/symbols/ts-package-scope.d.ts.map +1 -1
  106. package/dist/src/symbols/ts-package-scope.js +0 -8
  107. package/dist/src/symbols/ts-package-scope.js.map +1 -1
  108. package/dist/test/imports.test.d.ts.map +1 -1
  109. package/dist/test/jsdoc.test.d.ts +2 -0
  110. package/dist/test/jsdoc.test.d.ts.map +1 -0
  111. package/dist/test/member-chain.test.d.ts +2 -0
  112. package/dist/test/member-chain.test.d.ts.map +1 -0
  113. package/dist/test/utils.d.ts +2 -2
  114. package/dist/test/utils.d.ts.map +1 -1
  115. package/dist/test/values.test.d.ts.map +1 -1
  116. package/dist/tsconfig.tsbuildinfo +1 -1
  117. package/package.json +3 -5
  118. package/src/components/ArrayExpression.tsx +22 -32
  119. package/src/components/BarrelFile.tsx +7 -13
  120. package/src/components/ClassDeclaration.tsx +33 -28
  121. package/src/components/CommaList.tsx +23 -0
  122. package/src/components/Declaration.tsx +14 -5
  123. package/src/components/EnumDeclaration.tsx +23 -16
  124. package/src/components/EnumMember.tsx +7 -4
  125. package/src/components/ExportStatement.tsx +9 -2
  126. package/src/components/FunctionCallExpression.tsx +18 -9
  127. package/src/components/FunctionDeclaration.tsx +173 -56
  128. package/src/components/ImportStatement.tsx +6 -9
  129. package/src/components/Interface.tsx +20 -13
  130. package/src/components/JSDoc.tsx +28 -0
  131. package/src/components/JSDocComment.tsx +23 -0
  132. package/src/components/JSDocExample.tsx +30 -0
  133. package/src/components/JSDocParagraph.tsx +35 -0
  134. package/src/components/MemberChainExpression.tsx +91 -0
  135. package/src/components/ObjectExpression.tsx +53 -48
  136. package/src/components/PackageDirectory.tsx +22 -30
  137. package/src/components/PackageJson.tsx +91 -29
  138. package/src/components/SourceFile.tsx +15 -8
  139. package/src/components/TsConfigJson.tsx +5 -3
  140. package/src/components/TypeDeclaration.tsx +5 -3
  141. package/src/components/ValueExpression.tsx +3 -1
  142. package/src/components/VarDeclaration.tsx +13 -7
  143. package/src/components/index.ts +2 -1
  144. package/src/components/stc/index.ts +1 -1
  145. package/src/symbols/reference.ts +7 -4
  146. package/src/symbols/ts-output-symbol.ts +6 -2
  147. package/src/symbols/ts-package-scope.ts +0 -28
  148. package/temp/api.json +802 -395
  149. package/test/array.test.tsx +40 -7
  150. package/test/basic.test.tsx +26 -21
  151. package/test/class.test.tsx +65 -53
  152. package/test/enum.test.tsx +18 -13
  153. package/test/externals.test.tsx +6 -7
  154. package/test/function-call-expression.test.tsx +39 -17
  155. package/test/function-declaration.test.tsx +96 -78
  156. package/test/imports.test.tsx +115 -75
  157. package/test/interface.test.tsx +32 -22
  158. package/test/jsdoc.test.tsx +154 -0
  159. package/test/member-chain.test.tsx +124 -0
  160. package/test/name-policy.test.tsx +33 -26
  161. package/test/package-external.test.tsx +106 -7
  162. package/test/package.test.tsx +57 -5
  163. package/test/reference.test.tsx +2 -6
  164. package/test/utils.tsx +3 -1
  165. package/test/value-expression.test.tsx +2 -6
  166. package/test/values.test.tsx +67 -73
  167. package/test/var-declaration.test.tsx +11 -5
  168. package/test/vitest.d.ts +6 -1
  169. package/tsconfig.json +8 -1
  170. package/dist/src/components/Block.d.ts +0 -6
  171. package/dist/src/components/Block.d.ts.map +0 -1
  172. package/dist/src/components/Block.js +0 -11
  173. package/dist/src/components/Block.js.map +0 -1
  174. package/src/components/Block.tsx +0 -12
@@ -1,23 +1,31 @@
1
1
  import {
2
+ Block,
2
3
  childrenArray,
3
4
  code,
4
5
  findKeyedChild,
5
6
  findUnkeyedChildren,
6
- mapJoin,
7
+ For,
8
+ Indent,
7
9
  Name,
8
10
  Refkey,
9
11
  refkey,
10
12
  Scope,
13
+ Show,
11
14
  taggedComponent,
12
15
  } from "@alloy-js/core";
13
- import { Children } from "@alloy-js/core/jsx-runtime";
16
+ import { Children, onCleanup } from "@alloy-js/core/jsx-runtime";
14
17
  import { useTSNamePolicy } from "../name-policy.js";
15
- import { createTSSymbol, TSSymbolFlags, useTSScope } from "../symbols/index.js";
18
+ import {
19
+ createTSSymbol,
20
+ TSOutputSymbol,
21
+ TSSymbolFlags,
22
+ } from "../symbols/index.js";
16
23
  import { BaseDeclarationProps, Declaration } from "./Declaration.js";
17
24
 
18
25
  export interface ParameterDescriptor {
19
- type: Children;
20
- refkey: Refkey;
26
+ type?: Children;
27
+ refkey?: Refkey;
28
+ refkeys?: Refkey[];
21
29
  optional?: boolean;
22
30
  }
23
31
 
@@ -31,36 +39,54 @@ function isParameterDescriptor(
31
39
  export interface FunctionDeclarationProps extends BaseDeclarationProps {
32
40
  async?: boolean;
33
41
  parameters?: Record<string, Children | ParameterDescriptor>;
42
+ typeParameters?: Record<string, Children | ParameterDescriptor> | string[];
34
43
  returnType?: Children;
35
44
  children?: Children;
36
45
  }
37
46
 
38
47
  export const functionParametersTag = Symbol();
48
+ export const functionTypeParametersTag = Symbol();
39
49
  export const functionBodyTag = Symbol();
40
50
 
41
51
  export function FunctionDeclaration(props: FunctionDeclarationProps) {
42
52
  const children = childrenArray(() => props.children);
53
+ const typeParametersChild = findKeyedChild(
54
+ children,
55
+ functionTypeParametersTag,
56
+ );
43
57
  const parametersChild = findKeyedChild(children, functionParametersTag);
44
58
  const bodyChild = findKeyedChild(children, functionBodyTag);
45
59
  const filteredChildren = findUnkeyedChildren(children);
46
60
  const returnType = getReturnType(props.returnType, { async: props.async });
47
61
  const sReturnType = returnType ? <>: {returnType}</> : undefined;
48
62
 
49
- const sParams =
50
- parametersChild ?? <FunctionDeclaration.Parameters parameters={props.parameters} />;
63
+ const sTypeParameters =
64
+ typeParametersChild ?
65
+ <>
66
+ {"<"}
67
+ {typeParametersChild}
68
+ {">"}
69
+ </>
70
+ : <FunctionDeclaration.TypeParameters parameters={props.typeParameters} />;
71
+
72
+ const sParams = parametersChild ?? (
73
+ <FunctionDeclaration.Parameters parameters={props.parameters} />
74
+ );
51
75
 
52
- const sBody =
53
- bodyChild ?? <FunctionDeclaration.Body>{filteredChildren}</FunctionDeclaration.Body>;
76
+ const sBody = bodyChild ?? (
77
+ <FunctionDeclaration.Body>{filteredChildren}</FunctionDeclaration.Body>
78
+ );
54
79
 
55
80
  const asyncKwd = props.async ? "async " : "";
56
81
 
57
- return <Declaration {...props} nameKind="function">
58
- {asyncKwd}function <Name /><Scope name={props.name} kind="function">
59
- ({sParams}){sReturnType} {"{"}
60
- {sBody}
61
- {"}"}
82
+ return (
83
+ <Declaration {...props} nameKind="function">
84
+ {asyncKwd}function <Name />
85
+ <Scope name={props.name} kind="function">
86
+ {sTypeParameters}({sParams}){sReturnType} {sBody}
62
87
  </Scope>
63
- </Declaration>;
88
+ </Declaration>
89
+ );
64
90
  }
65
91
 
66
92
  function getReturnType(
@@ -71,6 +97,108 @@ function getReturnType(
71
97
  return options.async ? code`Promise<${returnType}>` : returnType;
72
98
  }
73
99
  }
100
+
101
+ interface DeclaredParameterDescriptor extends ParameterDescriptor {
102
+ symbol: TSOutputSymbol;
103
+ }
104
+
105
+ function normalizeAndDeclareParameters(
106
+ parameters: Record<string, Children | ParameterDescriptor> | string[],
107
+ flags: TSSymbolFlags = TSSymbolFlags.ParameterSymbol,
108
+ ): DeclaredParameterDescriptor[] {
109
+ const namePolicy = useTSNamePolicy();
110
+ if (Array.isArray(parameters)) {
111
+ return parameters.map((paramName) => {
112
+ const symbol = createTSSymbol({
113
+ name: namePolicy.getName(
114
+ paramName,
115
+ flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
116
+ ),
117
+ refkey: refkey(),
118
+ tsFlags: flags,
119
+ });
120
+
121
+ return { refkeys: symbol.refkeys, symbol };
122
+ });
123
+ }
124
+
125
+ return Object.entries(parameters).map(([paramName, value]) => {
126
+ const descriptor: DeclaredParameterDescriptor =
127
+ isParameterDescriptor(value) ? value : (
128
+ ({
129
+ refkey: refkey(),
130
+ type: value,
131
+ } as any)
132
+ );
133
+
134
+ const symbol = createTSSymbol({
135
+ name: namePolicy.getName(
136
+ paramName,
137
+ flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
138
+ ),
139
+ refkey: descriptor.refkey,
140
+ refkeys: descriptor.refkeys,
141
+ tsFlags: flags,
142
+ });
143
+ descriptor.symbol = symbol;
144
+ return descriptor;
145
+ });
146
+ }
147
+
148
+ export interface FunctionTypeParametersProps {
149
+ parameters?: Record<string, Children | ParameterDescriptor> | string[];
150
+ children?: Children;
151
+ }
152
+
153
+ FunctionDeclaration.TypeParameters = taggedComponent(
154
+ functionTypeParametersTag,
155
+ function TypeParameters(props: FunctionTypeParametersProps) {
156
+ if (props.children) {
157
+ return props.children;
158
+ }
159
+
160
+ if (!props.parameters) {
161
+ return undefined;
162
+ }
163
+
164
+ const typeParameters = normalizeAndDeclareParameters(props.parameters);
165
+
166
+ onCleanup(() => {
167
+ for (const param of typeParameters) {
168
+ param.symbol.binder.deleteSymbol(param.symbol);
169
+ }
170
+ });
171
+
172
+ return (
173
+ <>
174
+ {"<"}
175
+ <group>
176
+ <Indent break="soft">
177
+ <For each={typeParameters} comma line>
178
+ {(param) => typeParameter(param)}
179
+ </For>
180
+ <ifBreak>,</ifBreak>
181
+ </Indent>
182
+ </group>
183
+ {">"}
184
+ </>
185
+ );
186
+ },
187
+ );
188
+
189
+ function typeParameter(param: DeclaredParameterDescriptor) {
190
+ return (
191
+ <group>
192
+ {param.symbol.name}
193
+ <Show when={!!param.type}>
194
+ {" "}
195
+ extends
196
+ <indent> {param.type}</indent>
197
+ </Show>
198
+ </group>
199
+ );
200
+ }
201
+
74
202
  export interface FunctionParametersProps {
75
203
  parameters?: Record<string, Children | ParameterDescriptor>;
76
204
  children?: Children;
@@ -79,54 +207,43 @@ export interface FunctionParametersProps {
79
207
  FunctionDeclaration.Parameters = taggedComponent(
80
208
  functionParametersTag,
81
209
  function Parameters(props: FunctionParametersProps) {
82
- const namePolicy = useTSNamePolicy();
83
-
84
- let value;
85
-
86
210
  if (props.children) {
87
- value = props.children;
88
- } else if (props.parameters) {
89
- const scope = useTSScope();
90
- if (scope.kind !== "function") {
91
- throw new Error(
92
- "Can't declare function parameter in non-function scope",
93
- );
94
- }
95
-
96
- value = mapJoin(
97
- new Map(Object.entries(props.parameters)),
98
- (key, value) => {
99
- const descriptor: ParameterDescriptor = isParameterDescriptor(value) ?
100
- value
101
- : {
102
- refkey: refkey(),
103
- type: value,
104
- };
105
- const optionality = descriptor.optional ? "?" : "";
106
- const sym = createTSSymbol({
107
- name: key,
108
- refkey: descriptor.refkey,
109
- tsFlags: TSSymbolFlags.ParameterSymbol,
110
- });
111
-
112
- return <>{namePolicy.getName(sym.name, "parameter")}{optionality}: {descriptor.type}</>;
113
- },
114
- { joiner: ", " },
115
- );
116
- } else {
117
- value = undefined;
211
+ return props.children;
118
212
  }
119
213
 
120
- return value;
214
+ const parameters = normalizeAndDeclareParameters(props.parameters ?? {});
215
+ return (
216
+ <group>
217
+ <Indent break="soft">
218
+ <For each={parameters} comma line>
219
+ {(param) => parameter(param)}
220
+ </For>
221
+ <ifBreak>,</ifBreak>
222
+ </Indent>
223
+ </group>
224
+ );
121
225
  },
122
226
  );
123
227
 
228
+ function parameter(param: DeclaredParameterDescriptor) {
229
+ return (
230
+ <group>
231
+ {param.symbol.name}
232
+ <Show when={!!param.optional}>?</Show>
233
+ <Show when={!!param.type}>
234
+ <indent>: {param.type}</indent>
235
+ </Show>
236
+ </group>
237
+ );
238
+ }
239
+
124
240
  export interface FunctionBodyProps {
125
241
  children?: Children;
126
242
  }
127
243
 
128
- FunctionDeclaration.Body = taggedComponent(functionBodyTag, function Body(
129
- props: FunctionBodyProps,
130
- ) {
131
- return props.children;
132
- });
244
+ FunctionDeclaration.Body = taggedComponent(
245
+ functionBodyTag,
246
+ function Body(props: FunctionBodyProps) {
247
+ return <Block>{props.children}</Block>;
248
+ },
249
+ );
@@ -20,12 +20,9 @@ export interface ImportStatementsProps {
20
20
  export function ImportStatements(props: ImportStatementsProps) {
21
21
  const pkg = usePackage();
22
22
 
23
- return memo(() => {
24
- // todo: may be able to remove this (was just making sure to trigger
25
- // reactivity based on props.record).
26
- // eslint-disable-next-line @typescript-eslint/no-unused-expressions
27
- props.records.size;
28
- return mapJoin(props.records, (module, importedSymbols) => {
23
+ return mapJoin(
24
+ () => props.records,
25
+ (module, importedSymbols) => {
29
26
  let targetPath: string;
30
27
 
31
28
  if (
@@ -60,8 +57,8 @@ export function ImportStatements(props: ImportStatementsProps) {
60
57
  }
61
58
 
62
59
  return <ImportStatement path={targetPath} symbols={importedSymbols} />;
63
- });
64
- });
60
+ },
61
+ );
65
62
  }
66
63
 
67
64
  export interface ImportStatementProps {
@@ -96,7 +93,7 @@ export function ImportStatement(props: ImportStatementProps) {
96
93
  parts.push("{ ");
97
94
  parts.push(
98
95
  mapJoin(
99
- namedImportSymbols,
96
+ () => namedImportSymbols,
100
97
  (nis) => <ImportBinding importedSymbol={nis} />,
101
98
  { joiner: ", " },
102
99
  ),
@@ -1,4 +1,4 @@
1
- import { Children, code, Name } from "@alloy-js/core";
1
+ import { Block, Children, Name } from "@alloy-js/core";
2
2
  import { useTSNamePolicy } from "../name-policy.js";
3
3
  import { BaseDeclarationProps, Declaration } from "./Declaration.js";
4
4
 
@@ -18,11 +18,12 @@ export interface InterfaceDeclarationProps extends BaseDeclarationProps {
18
18
  export function InterfaceDeclaration(props: InterfaceDeclarationProps) {
19
19
  const extendsPart = props.extends ? <> extends {props.extends}</> : "";
20
20
 
21
- return <Declaration {...props} nameKind="interface">
22
- interface <Name />{extendsPart} <InterfaceExpression>
23
- {props.children}
24
- </InterfaceExpression>
25
- </Declaration>;
21
+ return (
22
+ <Declaration {...props} nameKind="interface">
23
+ interface <Name />
24
+ {extendsPart} <InterfaceExpression>{props.children}</InterfaceExpression>
25
+ </Declaration>
26
+ );
26
27
  }
27
28
 
28
29
  export interface InterfaceExpressionProps {
@@ -30,11 +31,7 @@ export interface InterfaceExpressionProps {
30
31
  }
31
32
 
32
33
  export function InterfaceExpression(props: InterfaceExpressionProps) {
33
- return code`
34
- {
35
- ${props.children}
36
- }
37
- `;
34
+ return <Block>{props.children}</Block>;
38
35
  }
39
36
 
40
37
  export interface InterfaceMemberProps {
@@ -55,8 +52,18 @@ export function InterfaceMember(props: InterfaceMemberProps) {
55
52
  const optionality = props.optional ? "?" : "";
56
53
  const readonly = props.readonly ? "readonly " : "";
57
54
  if (props.indexer) {
58
- return <>[{props.indexer}]: {type}</>;
55
+ return (
56
+ <>
57
+ [{props.indexer}]: {type}
58
+ </>
59
+ );
59
60
  } else {
60
- return <>{readonly}{namer.getName(props.name!, "interface-member")}{optionality}: {type}</>;
61
+ return (
62
+ <>
63
+ {readonly}
64
+ {namer.getName(props.name!, "interface-member")}
65
+ {optionality}: {type}
66
+ </>
67
+ );
61
68
  }
62
69
  }
@@ -0,0 +1,28 @@
1
+ import { List, childrenArray } from "@alloy-js/core";
2
+ import { Children } from "@alloy-js/core/jsx-runtime";
3
+ import { JSDocComment } from "./JSDocComment.jsx";
4
+
5
+ export interface JSDocProps {
6
+ children: Children;
7
+ }
8
+
9
+ /**
10
+ * A JSDoc comment. The children of this component are joined with two hard
11
+ * linebreaks. This is useful for creating JSDoc comments with multiple paragraphs.
12
+ */
13
+ export function JSDoc(props: JSDocProps) {
14
+ return (
15
+ <JSDocComment>
16
+ <List
17
+ joiner={
18
+ <>
19
+ <hbr />
20
+ <hbr />
21
+ </>
22
+ }
23
+ >
24
+ {childrenArray(() => props.children)}
25
+ </List>
26
+ </JSDocComment>
27
+ );
28
+ }
@@ -0,0 +1,23 @@
1
+ import { Children } from "@alloy-js/core/jsx-runtime";
2
+
3
+ export interface JSDocCommentProps {
4
+ children: Children;
5
+ }
6
+
7
+ /**
8
+ * A JSDoc comment block. This is a low-level component that merely creates the
9
+ * block. Consider using {@link JSDoc} if you want to create more complex
10
+ * comments.
11
+ */
12
+ export function JSDocComment(props: JSDocCommentProps) {
13
+ return (
14
+ <>
15
+ /**
16
+ <align string=" * ">
17
+ <hbr />
18
+ {props.children}
19
+ </align>
20
+ <hbr /> **/
21
+ </>
22
+ );
23
+ }
@@ -0,0 +1,30 @@
1
+ import { Show } from "@alloy-js/core";
2
+ import { Children } from "@alloy-js/core/jsx-runtime";
3
+
4
+ export interface JSDocExampleProps {
5
+ fenced?: boolean;
6
+ language?: string;
7
+ children: Children;
8
+ }
9
+
10
+ /**
11
+ * Create a JSDoc example set off with `@example`. When the `fenced` prop is true, a `language` prop can
12
+ * also be set.
13
+ */
14
+ export function JSDocExample(props: JSDocExampleProps) {
15
+ return (
16
+ <>
17
+ @example
18
+ <hbr />
19
+ <Show when={props.fenced}>
20
+ ```{props.language}
21
+ <hbr />
22
+ </Show>
23
+ {props.children}
24
+ <Show when={props.fenced}>
25
+ <hbr />
26
+ ```
27
+ </Show>
28
+ </>
29
+ );
30
+ }
@@ -0,0 +1,35 @@
1
+ import { childrenArray, computed } from "@alloy-js/core";
2
+ import { Children } from "@alloy-js/core/jsx-runtime";
3
+
4
+ export interface JSDocParagraphProps {
5
+ children: Children;
6
+ }
7
+
8
+ /**
9
+ * Create a paragraph in a JSDoc comment. The text of this component is word
10
+ * wrapped.
11
+ */
12
+ export function JSDocParagraph(props: JSDocParagraphProps) {
13
+ const brokenChildren = computed(() => {
14
+ const children = childrenArray(() => props.children);
15
+ return children
16
+ .map((child) => {
17
+ if (typeof child === "string") {
18
+ return child
19
+ .trim()
20
+ .split(/\s+/)
21
+ .map((word) => (
22
+ <>
23
+ {word}
24
+ <br />
25
+ </>
26
+ ));
27
+ }
28
+
29
+ return child;
30
+ })
31
+ .flat(2);
32
+ });
33
+
34
+ return <fill>{brokenChildren.value}</fill>;
35
+ }
@@ -0,0 +1,91 @@
1
+ import { childrenArray, computed, For, List, Show } from "@alloy-js/core";
2
+ import { Children, isComponentCreator } from "@alloy-js/core/jsx-runtime";
3
+ import { FunctionCallExpression } from "./FunctionCallExpression.jsx";
4
+
5
+ export interface MemberChainExpressionProps {
6
+ children: Children;
7
+ }
8
+
9
+ /**
10
+ * Create a member chain expression, which is a member expression comprised of
11
+ * either children, {@link FunctionCallExpression} components, or nested member
12
+ * chain expression components. Other component types are ignored.
13
+ */
14
+ export function MemberChainExpression(props: MemberChainExpressionProps) {
15
+ // chunks are constructed by consuming as many non-call expressions as
16
+ // possible, then placing an indent and soft line break before and after the
17
+ // any subsequent call expressions
18
+
19
+ const chunks = computed(() => {
20
+ const children = flattenCallChains(childrenArray(() => props.children));
21
+ const chunks: Children[][] = [];
22
+
23
+ let currentChunk: Children[] = [];
24
+
25
+ for (const child of children) {
26
+ if (
27
+ isComponentCreator(child) &&
28
+ child.component === FunctionCallExpression
29
+ ) {
30
+ if (currentChunk.length > 0) {
31
+ chunks.push(currentChunk);
32
+ currentChunk = [];
33
+ }
34
+ chunks.push([child]);
35
+ } else {
36
+ currentChunk.push(child);
37
+ }
38
+ }
39
+
40
+ if (currentChunk.length > 0) {
41
+ chunks.push(currentChunk);
42
+ }
43
+
44
+ return [chunks[0], chunks.slice(1)];
45
+ });
46
+ const groupId = Symbol();
47
+
48
+ return (
49
+ <group>
50
+ <group id={groupId}>
51
+ <List joiner="." children={chunks.value[0]} />
52
+ </group>
53
+ <Show when={chunks.value[1].length === 1}>
54
+ <For each={chunks.value[1]} softline>
55
+ {(chunk) => (
56
+ <>
57
+ .<List joiner="." children={chunk} />
58
+ </>
59
+ )}
60
+ </For>
61
+ </Show>
62
+ <Show when={chunks.value[1].length > 1}>
63
+ <indent>
64
+ <For each={chunks.value[1]} softline>
65
+ {(chunk) => (
66
+ <>
67
+ .<List joiner="." children={chunk} />
68
+ </>
69
+ )}
70
+ </For>
71
+ </indent>
72
+ </Show>
73
+ </group>
74
+ );
75
+ }
76
+
77
+ function flattenCallChains(children: Children[]): Children[] {
78
+ const flatChildren = [];
79
+ for (const child of children) {
80
+ if (
81
+ isComponentCreator(child) &&
82
+ child.component === MemberChainExpression
83
+ ) {
84
+ flatChildren.push(...flattenCallChains(child.props.children));
85
+ } else {
86
+ flatChildren.push(child);
87
+ }
88
+ }
89
+
90
+ return flatChildren;
91
+ }