@alloy-js/typescript 0.4.0 → 0.6.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 (170) hide show
  1. package/CHANGELOG.md +19 -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 +1 -1
  7. package/dist/src/components/BarrelFile.d.ts.map +1 -1
  8. package/dist/src/components/BarrelFile.js +11 -9
  9. package/dist/src/components/BarrelFile.js.map +1 -1
  10. package/dist/src/components/ClassDeclaration.d.ts +4 -4
  11. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  12. package/dist/src/components/ClassDeclaration.js +8 -9
  13. package/dist/src/components/ClassDeclaration.js.map +1 -1
  14. package/dist/src/components/CommaList.d.ts +12 -0
  15. package/dist/src/components/CommaList.d.ts.map +1 -0
  16. package/dist/src/components/CommaList.js +19 -0
  17. package/dist/src/components/CommaList.js.map +1 -0
  18. package/dist/src/components/Declaration.d.ts +1 -1
  19. package/dist/src/components/Declaration.d.ts.map +1 -1
  20. package/dist/src/components/Declaration.js.map +1 -1
  21. package/dist/src/components/EnumDeclaration.d.ts +1 -1
  22. package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
  23. package/dist/src/components/EnumDeclaration.js +19 -17
  24. package/dist/src/components/EnumDeclaration.js.map +1 -1
  25. package/dist/src/components/EnumMember.d.ts +1 -1
  26. package/dist/src/components/EnumMember.d.ts.map +1 -1
  27. package/dist/src/components/EnumMember.js.map +1 -1
  28. package/dist/src/components/ExportStatement.d.ts +1 -1
  29. package/dist/src/components/ExportStatement.d.ts.map +1 -1
  30. package/dist/src/components/ExportStatement.js.map +1 -1
  31. package/dist/src/components/FunctionCallExpression.d.ts +3 -3
  32. package/dist/src/components/FunctionCallExpression.d.ts.map +1 -1
  33. package/dist/src/components/FunctionCallExpression.js +26 -9
  34. package/dist/src/components/FunctionCallExpression.js.map +1 -1
  35. package/dist/src/components/FunctionDeclaration.d.ts +9 -2
  36. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  37. package/dist/src/components/FunctionDeclaration.js +135 -33
  38. package/dist/src/components/FunctionDeclaration.js.map +1 -1
  39. package/dist/src/components/ImportStatement.d.ts +1 -1
  40. package/dist/src/components/ImportStatement.d.ts.map +1 -1
  41. package/dist/src/components/ImportStatement.js +25 -31
  42. package/dist/src/components/ImportStatement.js.map +1 -1
  43. package/dist/src/components/Interface.d.ts +3 -3
  44. package/dist/src/components/Interface.d.ts.map +1 -1
  45. package/dist/src/components/Interface.js +5 -5
  46. package/dist/src/components/Interface.js.map +1 -1
  47. package/dist/src/components/JSDoc.d.ts +10 -0
  48. package/dist/src/components/JSDoc.d.ts.map +1 -0
  49. package/dist/src/components/JSDoc.js +24 -0
  50. package/dist/src/components/JSDoc.js.map +1 -0
  51. package/dist/src/components/JSDocComment.d.ts +11 -0
  52. package/dist/src/components/JSDocComment.d.ts.map +1 -0
  53. package/dist/src/components/JSDocComment.js +16 -0
  54. package/dist/src/components/JSDocComment.js.map +1 -0
  55. package/dist/src/components/JSDocExample.d.ts +12 -0
  56. package/dist/src/components/JSDocExample.d.ts.map +1 -0
  57. package/dist/src/components/JSDocExample.js +26 -0
  58. package/dist/src/components/JSDocExample.js.map +1 -0
  59. package/dist/src/components/JSDocParagraph.d.ts +10 -0
  60. package/dist/src/components/JSDocParagraph.d.ts.map +1 -0
  61. package/dist/src/components/JSDocParagraph.js +23 -0
  62. package/dist/src/components/JSDocParagraph.js.map +1 -0
  63. package/dist/src/components/MemberChainExpression.d.ts +11 -0
  64. package/dist/src/components/MemberChainExpression.d.ts.map +1 -0
  65. package/dist/src/components/MemberChainExpression.js +64 -0
  66. package/dist/src/components/MemberChainExpression.js.map +1 -0
  67. package/dist/src/components/ObjectExpression.d.ts +2 -2
  68. package/dist/src/components/ObjectExpression.d.ts.map +1 -1
  69. package/dist/src/components/ObjectExpression.js +45 -42
  70. package/dist/src/components/ObjectExpression.js.map +1 -1
  71. package/dist/src/components/PackageDirectory.d.ts +1 -1
  72. package/dist/src/components/PackageDirectory.d.ts.map +1 -1
  73. package/dist/src/components/PackageDirectory.js +1 -1
  74. package/dist/src/components/PackageDirectory.js.map +1 -1
  75. package/dist/src/components/PackageJson.d.ts +1 -1
  76. package/dist/src/components/PackageJson.d.ts.map +1 -1
  77. package/dist/src/components/PackageJson.js.map +1 -1
  78. package/dist/src/components/Reference.d.ts +1 -1
  79. package/dist/src/components/Reference.d.ts.map +1 -1
  80. package/dist/src/components/Reference.js +1 -1
  81. package/dist/src/components/Reference.js.map +1 -1
  82. package/dist/src/components/SourceFile.d.ts +1 -1
  83. package/dist/src/components/SourceFile.d.ts.map +1 -1
  84. package/dist/src/components/SourceFile.js +13 -6
  85. package/dist/src/components/SourceFile.js.map +1 -1
  86. package/dist/src/components/TsConfigJson.d.ts +1 -1
  87. package/dist/src/components/TsConfigJson.d.ts.map +1 -1
  88. package/dist/src/components/TsConfigJson.js.map +1 -1
  89. package/dist/src/components/TypeDeclaration.d.ts +1 -1
  90. package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
  91. package/dist/src/components/TypeDeclaration.js.map +1 -1
  92. package/dist/src/components/ValueExpression.d.ts +2 -1
  93. package/dist/src/components/ValueExpression.d.ts.map +1 -1
  94. package/dist/src/components/ValueExpression.js.map +1 -1
  95. package/dist/src/components/VarDeclaration.d.ts +1 -1
  96. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  97. package/dist/src/components/VarDeclaration.js +2 -2
  98. package/dist/src/components/VarDeclaration.js.map +1 -1
  99. package/dist/src/components/index.d.ts +2 -1
  100. package/dist/src/components/index.d.ts.map +1 -1
  101. package/dist/src/components/index.js +2 -1
  102. package/dist/src/components/index.js.map +1 -1
  103. package/dist/src/components/stc/index.d.ts +21 -21
  104. package/dist/src/components/stc/index.d.ts.map +1 -1
  105. package/dist/src/components/stc/index.js +1 -1
  106. package/dist/src/components/stc/index.js.map +1 -1
  107. package/dist/test/imports.test.d.ts.map +1 -1
  108. package/dist/test/jsdoc.test.d.ts +2 -0
  109. package/dist/test/jsdoc.test.d.ts.map +1 -0
  110. package/dist/test/member-chain.test.d.ts +2 -0
  111. package/dist/test/member-chain.test.d.ts.map +1 -0
  112. package/dist/test/utils.d.ts +2 -2
  113. package/dist/test/utils.d.ts.map +1 -1
  114. package/dist/test/values.test.d.ts.map +1 -1
  115. package/dist/tsconfig.tsbuildinfo +1 -1
  116. package/package.json +4 -6
  117. package/src/components/ArrayExpression.tsx +18 -32
  118. package/src/components/BarrelFile.tsx +7 -13
  119. package/src/components/ClassDeclaration.tsx +33 -28
  120. package/src/components/CommaList.tsx +23 -0
  121. package/src/components/Declaration.tsx +7 -3
  122. package/src/components/EnumDeclaration.tsx +21 -14
  123. package/src/components/EnumMember.tsx +7 -4
  124. package/src/components/ExportStatement.tsx +6 -1
  125. package/src/components/FunctionCallExpression.tsx +18 -9
  126. package/src/components/FunctionDeclaration.tsx +170 -55
  127. package/src/components/ImportStatement.tsx +6 -9
  128. package/src/components/Interface.tsx +20 -13
  129. package/src/components/JSDoc.tsx +28 -0
  130. package/src/components/JSDocComment.tsx +23 -0
  131. package/src/components/JSDocExample.tsx +30 -0
  132. package/src/components/JSDocParagraph.tsx +35 -0
  133. package/src/components/MemberChainExpression.tsx +59 -0
  134. package/src/components/ObjectExpression.tsx +53 -48
  135. package/src/components/PackageDirectory.tsx +11 -9
  136. package/src/components/PackageJson.tsx +9 -6
  137. package/src/components/SourceFile.tsx +15 -8
  138. package/src/components/TsConfigJson.tsx +5 -3
  139. package/src/components/TypeDeclaration.tsx +5 -3
  140. package/src/components/ValueExpression.tsx +7 -5
  141. package/src/components/VarDeclaration.tsx +11 -5
  142. package/src/components/index.ts +2 -1
  143. package/src/components/stc/index.ts +1 -1
  144. package/temp/api.json +618 -208
  145. package/test/array.test.tsx +28 -7
  146. package/test/basic.test.tsx +26 -21
  147. package/test/class.test.tsx +65 -53
  148. package/test/enum.test.tsx +18 -13
  149. package/test/externals.test.tsx +6 -7
  150. package/test/function-call-expression.test.tsx +39 -17
  151. package/test/function-declaration.test.tsx +96 -78
  152. package/test/imports.test.tsx +90 -65
  153. package/test/interface.test.tsx +29 -19
  154. package/test/jsdoc.test.tsx +154 -0
  155. package/test/member-chain.test.tsx +47 -0
  156. package/test/name-policy.test.tsx +33 -26
  157. package/test/package-external.test.tsx +8 -4
  158. package/test/package.test.tsx +1 -2
  159. package/test/reference.test.tsx +2 -6
  160. package/test/utils.tsx +3 -1
  161. package/test/value-expression.test.tsx +2 -6
  162. package/test/values.test.tsx +67 -73
  163. package/test/var-declaration.test.tsx +11 -5
  164. package/test/vitest.d.ts +6 -1
  165. package/tsconfig.json +8 -1
  166. package/dist/src/components/Block.d.ts +0 -6
  167. package/dist/src/components/Block.d.ts.map +0 -1
  168. package/dist/src/components/Block.js +0 -11
  169. package/dist/src/components/Block.js.map +0 -1
  170. package/src/components/Block.tsx +0 -12
package/package.json CHANGED
@@ -1,14 +1,12 @@
1
1
  {
2
2
  "name": "@alloy-js/typescript",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
7
- "development": "./src/index.ts",
8
7
  "import": "./dist/src/index.js"
9
8
  },
10
9
  "./stc": {
11
- "development": "./src/components/stc/index.ts",
12
10
  "import": "./dist/src/components/stc/index.js"
13
11
  }
14
12
  },
@@ -18,7 +16,7 @@
18
16
  "dependencies": {
19
17
  "change-case": "^5.4.4",
20
18
  "pathe": "^1.1.2",
21
- "@alloy-js/core": "~0.4.0"
19
+ "@alloy-js/core": "~0.6.0"
22
20
  },
23
21
  "devDependencies": {
24
22
  "@babel/cli": "^7.24.7",
@@ -27,9 +25,9 @@
27
25
  "@rollup/plugin-babel": "^6.0.4",
28
26
  "@rollup/plugin-typescript": "^11.1.6",
29
27
  "concurrently": "^8.2.2",
30
- "typescript": "^5.5.4",
28
+ "typescript": "^5.7.3",
31
29
  "vitest": "^3.0.4",
32
- "@alloy-js/babel-preset": "~0.1.1"
30
+ "@alloy-js/babel-preset": "~0.2.0"
33
31
  },
34
32
  "type": "module",
35
33
  "scripts": {
@@ -1,4 +1,4 @@
1
- import { Children, computed, Indent, mapJoin, memo } from "@alloy-js/core";
1
+ import { Children, For, Indent } from "@alloy-js/core";
2
2
  import { ValueExpression } from "./ValueExpression.js";
3
3
 
4
4
  export interface ArrayExpressionProps {
@@ -7,35 +7,21 @@ export interface ArrayExpressionProps {
7
7
  }
8
8
 
9
9
  export function ArrayExpression(props: ArrayExpressionProps) {
10
- const elements = computed(() => {
11
- const jsValue = props.jsValue;
12
- let elements: any[] = [];
13
-
14
- if (jsValue) {
15
- elements = mapJoin(
16
- jsValue,
17
- (value) => {
18
- return <ValueExpression jsValue={value} />;
19
- },
20
- { joiner: ",\n" },
21
- );
22
- }
23
-
24
- if (props.children) {
25
- if (elements.length > 0) {
26
- elements.push(",");
27
- }
28
- elements.push("\n", props.children);
29
- }
30
-
31
- return elements;
32
- });
33
-
34
- return memo(() => {
35
- if (elements.value.length === 0) {
36
- return "[]";
37
- } else {
38
- return ["[\n", <Indent>{elements.value}</Indent>, "\n]"];
39
- }
40
- });
10
+ return (
11
+ <group>
12
+ [
13
+ <Indent break="soft">
14
+ <For each={props.jsValue ?? []} comma line>
15
+ {(value) => <ValueExpression jsValue={value} />}
16
+ </For>
17
+ {props.children && (
18
+ <>
19
+ ,<br />
20
+ {props.children}
21
+ </>
22
+ )}
23
+ </Indent>
24
+ <sbr />]
25
+ </group>
26
+ );
41
27
  }
@@ -1,9 +1,4 @@
1
- import {
2
- mapJoin,
3
- memo,
4
- SourceDirectoryContext,
5
- useContext,
6
- } from "@alloy-js/core";
1
+ import { For, memo, SourceDirectoryContext, useContext } from "@alloy-js/core";
7
2
  import { basename } from "pathe";
8
3
  import { getSourceDirectoryData } from "../source-directory-data.js";
9
4
  import { TSModuleScope } from "../symbols/index.js";
@@ -84,13 +79,12 @@ export function BarrelFile(props: BarrelFileProps) {
84
79
  (m) => basename(m.name) !== "index.ts",
85
80
  );
86
81
 
87
- const allModules = [...sourceFiles, ...nestedBarrels];
88
- return mapJoin(allModules, (module) => {
89
- return <ExportStatement star from={module} />;
90
- });
82
+ return [...sourceFiles, ...nestedBarrels];
91
83
  });
92
84
 
93
- return <SourceFile path={path} export={props.export}>
94
- {exports}
95
- </SourceFile>;
85
+ return (
86
+ <SourceFile path={path} export={props.export}>
87
+ <For each={exports}>{(mod) => <ExportStatement star from={mod} />}</For>
88
+ </SourceFile>
89
+ );
96
90
  }
@@ -1,4 +1,5 @@
1
1
  import {
2
+ Block,
2
3
  Children,
3
4
  MemberDeclaration,
4
5
  MemberName,
@@ -56,11 +57,12 @@ export function ClassDeclaration(props: ClassDeclarationProps) {
56
57
  const extendsPart = props.extends && <> extends {props.extends}</>;
57
58
  const flags = OutputSymbolFlags.MemberContainer;
58
59
 
59
- return <Declaration {...props} flags={flags} nameKind="class">
60
- class <Name />{extendsPart} {"{"}
61
- {props.children}
62
- {"}"}
63
- </Declaration>;
60
+ return (
61
+ <Declaration {...props} flags={flags} nameKind="class">
62
+ class <Name />
63
+ {extendsPart} <Block>{props.children}</Block>
64
+ </Declaration>
65
+ );
64
66
  }
65
67
 
66
68
  export interface ClassMemberProps {
@@ -97,17 +99,15 @@ export function ClassMember(props: ClassMemberProps) {
97
99
  name = `#${name}`;
98
100
  }
99
101
 
100
- return <MemberDeclaration symbol={sym} name={name} refkey={props.refkey}>
101
- {
102
- props.public && "public "
103
- }{
104
- props.private && "private "
105
- }{
106
- props.protected && "protected "
107
- }{
108
- props.static && "static "
109
- }{props.children}
110
- </MemberDeclaration>;
102
+ return (
103
+ <MemberDeclaration symbol={sym} name={name} refkey={props.refkey}>
104
+ {props.public && "public "}
105
+ {props.private && "private "}
106
+ {props.protected && "protected "}
107
+ {props.static && "static "}
108
+ {props.children}
109
+ </MemberDeclaration>
110
+ );
111
111
  }
112
112
 
113
113
  export interface ClassFieldProps extends ClassMemberProps {
@@ -119,9 +119,13 @@ export function ClassField(props: ClassFieldProps) {
119
119
  const typeSection = props.type && <>: {props.type}</>;
120
120
  const initializerSection = props.children && <> = {props.children}</>;
121
121
 
122
- return <ClassMember {...props}>
123
- <MemberName />{typeSection}{initializerSection};
124
- </ClassMember>;
122
+ return (
123
+ <ClassMember {...props}>
124
+ <MemberName />
125
+ {typeSection}
126
+ {initializerSection}
127
+ </ClassMember>
128
+ );
125
129
  }
126
130
 
127
131
  export interface ClassMethodProps extends ClassMemberProps {
@@ -133,13 +137,14 @@ export interface ClassMethodProps extends ClassMemberProps {
133
137
 
134
138
  export function ClassMethod(props: ClassMethodProps) {
135
139
  const returnType = props.returnType && <>: {props.returnType}</>;
136
- return <ClassMember {...props}>
137
- {
138
- props.async && "async "
139
- }<MemberName /><Scope name={props.name} kind="function">
140
- (<FunctionDeclaration.Parameters parameters={props.parameters} />){returnType} {"{"}
141
- {props.children}
142
- {"}"}
143
- </Scope>
144
- </ClassMember>;
140
+ return (
141
+ <ClassMember {...props}>
142
+ {props.async && "async "}
143
+ <MemberName />
144
+ <Scope name={props.name} kind="function">
145
+ (<FunctionDeclaration.Parameters parameters={props.parameters} />)
146
+ {returnType} <Block>{props.children}</Block>
147
+ </Scope>
148
+ </ClassMember>
149
+ );
145
150
  }
@@ -0,0 +1,23 @@
1
+ import { Children, List, splitProps } from "@alloy-js/core";
2
+
3
+ export interface CommaListProps {
4
+ children: Children;
5
+ hardline?: boolean;
6
+ softline?: boolean;
7
+ }
8
+
9
+ /**
10
+ * A comma-separated list of items (e.g. arguments to a function call,
11
+ * parameters, etc).
12
+ */
13
+ export function CommaList(props: CommaListProps) {
14
+ return (
15
+ <List
16
+ comma
17
+ {...splitProps(props, ["hardline", "softline"])}
18
+ enderPunctuation
19
+ >
20
+ {props.children}
21
+ </List>
22
+ );
23
+ }
@@ -99,7 +99,11 @@ export function Declaration(props: DeclarationProps) {
99
99
  children = () => props.children;
100
100
  }
101
101
 
102
- return <CoreDeclaration symbol={sym}>
103
- {props.export ? "export " : ""}{props.default ? "default " : ""}{children}
104
- </CoreDeclaration>;
102
+ return (
103
+ <CoreDeclaration symbol={sym}>
104
+ {props.export ? "export " : ""}
105
+ {props.default ? "default " : ""}
106
+ {children}
107
+ </CoreDeclaration>
108
+ );
105
109
  }
@@ -1,9 +1,11 @@
1
1
  import {
2
+ Block,
3
+ computed,
2
4
  Declaration as CoreDeclaration,
5
+ For,
3
6
  MemberScope,
4
7
  Name,
5
8
  OutputSymbolFlags,
6
- mapJoin,
7
9
  refkey,
8
10
  useBinder,
9
11
  } from "@alloy-js/core";
@@ -35,19 +37,24 @@ export function EnumDeclaration(props: EnumDeclarationProps) {
35
37
  flags: OutputSymbolFlags.StaticMemberContainer,
36
38
  });
37
39
 
38
- const jsValueMembers = mapJoin(
39
- Object.entries(props.jsValue ?? {}),
40
- ([name, value]) => {
41
- return <EnumMember name={name} jsValue={value} />;
42
- },
43
- { joiner: ",\n" },
44
- );
45
-
46
- return <CoreDeclaration symbol={sym}>
47
- {props.export ? "export " : ""}{props.default ? "default " : ""}enum <Name /> {"{"}
40
+ const valueEntries = computed(() => Object.entries(props.jsValue ?? {}));
41
+ return (
42
+ <CoreDeclaration symbol={sym}>
43
+ {props.export ? "export " : ""}
44
+ {props.default ? "default " : ""}enum <Name />{" "}
48
45
  <MemberScope owner={sym}>
49
- {jsValueMembers}{jsValueMembers.length > 0 && props.children && ",\n"}{props.children}
46
+ <Block>
47
+ <For each={valueEntries} comma hardline enderPunctuation>
48
+ {([name, value]) => <EnumMember name={name} jsValue={value} />}
49
+ </For>
50
+ {props.children && (
51
+ <>
52
+ {valueEntries.value.length > 0 && <hbr />}
53
+ {props.children}
54
+ </>
55
+ )}
56
+ </Block>
50
57
  </MemberScope>
51
- {"}"}
52
- </CoreDeclaration>;
58
+ </CoreDeclaration>
59
+ );
53
60
  }
@@ -44,9 +44,12 @@ export function EnumMember(props: EnumMemberProps) {
44
44
  });
45
45
  }
46
46
  const nameCode = sym ? sym.name : name;
47
- const valueCode = props.jsValue ?
48
- <ValueExpression jsValue={props.jsValue} />
49
- : props.value;
47
+ const valueCode =
48
+ props.jsValue ? <ValueExpression jsValue={props.jsValue} /> : props.value;
50
49
 
51
- return <>{nameCode} = {valueCode}</>;
50
+ return (
51
+ <>
52
+ {nameCode} = {valueCode}
53
+ </>
54
+ );
52
55
  }
@@ -14,7 +14,12 @@ export function ExportStatement(props: ExportStatementProps) {
14
14
  for (const symbol of props.from!.symbols.values()) {
15
15
  module.scope.exportedSymbols.set(symbol.refkey, symbol as TSOutputSymbol);
16
16
  }
17
- return <>export * from "{modulePath(relative(dirname(module.scope.name), props.from!.name))}";</>;
17
+ return (
18
+ <>
19
+ export * from "
20
+ {modulePath(relative(dirname(module.scope.name), props.from!.name))}";
21
+ </>
22
+ );
18
23
  }
19
24
 
20
25
  return "";
@@ -1,15 +1,24 @@
1
- import { Children, mapJoin, Refkey } from "@alloy-js/core";
2
- import { Reference } from "./Reference.js";
1
+ import { Children, For, Indent, Wrap } from "@alloy-js/core";
3
2
 
4
3
  export interface FunctionCallExpressionProps {
5
- refkey: Refkey;
4
+ target: Children;
6
5
  args?: Children[];
7
6
  }
7
+
8
8
  export function FunctionCallExpression(props: FunctionCallExpressionProps) {
9
- const args = props.args && props.args.length ?
10
- mapJoin(props.args, (arg) => arg, { joiner: ", " })
11
- : null;
12
- return <>
13
- <Reference refkey={props.refkey} />({args})
14
- </>;
9
+ return (
10
+ <group>
11
+ {props.target}(
12
+ <Wrap
13
+ when={!!props.args && props.args.length > 1}
14
+ with={Indent}
15
+ props={{ break: "soft", trailingBreak: true }}
16
+ >
17
+ <For each={props.args ?? []} comma line>
18
+ {(arg) => arg}
19
+ </For>
20
+ </Wrap>
21
+ )
22
+ </group>
23
+ );
15
24
  }
@@ -1,22 +1,29 @@
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;
26
+ type?: Children;
20
27
  refkey: Refkey;
21
28
  optional?: boolean;
22
29
  }
@@ -31,36 +38,54 @@ function isParameterDescriptor(
31
38
  export interface FunctionDeclarationProps extends BaseDeclarationProps {
32
39
  async?: boolean;
33
40
  parameters?: Record<string, Children | ParameterDescriptor>;
41
+ typeParameters?: Record<string, Children | ParameterDescriptor> | string[];
34
42
  returnType?: Children;
35
43
  children?: Children;
36
44
  }
37
45
 
38
46
  export const functionParametersTag = Symbol();
47
+ export const functionTypeParametersTag = Symbol();
39
48
  export const functionBodyTag = Symbol();
40
49
 
41
50
  export function FunctionDeclaration(props: FunctionDeclarationProps) {
42
51
  const children = childrenArray(() => props.children);
52
+ const typeParametersChild = findKeyedChild(
53
+ children,
54
+ functionTypeParametersTag,
55
+ );
43
56
  const parametersChild = findKeyedChild(children, functionParametersTag);
44
57
  const bodyChild = findKeyedChild(children, functionBodyTag);
45
58
  const filteredChildren = findUnkeyedChildren(children);
46
59
  const returnType = getReturnType(props.returnType, { async: props.async });
47
60
  const sReturnType = returnType ? <>: {returnType}</> : undefined;
48
61
 
49
- const sParams =
50
- parametersChild ?? <FunctionDeclaration.Parameters parameters={props.parameters} />;
62
+ const sTypeParameters =
63
+ typeParametersChild ?
64
+ <>
65
+ {"<"}
66
+ {typeParametersChild}
67
+ {">"}
68
+ </>
69
+ : <FunctionDeclaration.TypeParameters parameters={props.typeParameters} />;
70
+
71
+ const sParams = parametersChild ?? (
72
+ <FunctionDeclaration.Parameters parameters={props.parameters} />
73
+ );
51
74
 
52
- const sBody =
53
- bodyChild ?? <FunctionDeclaration.Body>{filteredChildren}</FunctionDeclaration.Body>;
75
+ const sBody = bodyChild ?? (
76
+ <FunctionDeclaration.Body>{filteredChildren}</FunctionDeclaration.Body>
77
+ );
54
78
 
55
79
  const asyncKwd = props.async ? "async " : "";
56
80
 
57
- return <Declaration {...props} nameKind="function">
58
- {asyncKwd}function <Name /><Scope name={props.name} kind="function">
59
- ({sParams}){sReturnType} {"{"}
60
- {sBody}
61
- {"}"}
81
+ return (
82
+ <Declaration {...props} nameKind="function">
83
+ {asyncKwd}function <Name />
84
+ <Scope name={props.name} kind="function">
85
+ {sTypeParameters}({sParams}){sReturnType} {sBody}
62
86
  </Scope>
63
- </Declaration>;
87
+ </Declaration>
88
+ );
64
89
  }
65
90
 
66
91
  function getReturnType(
@@ -71,6 +96,107 @@ function getReturnType(
71
96
  return options.async ? code`Promise<${returnType}>` : returnType;
72
97
  }
73
98
  }
99
+
100
+ interface DeclaredParameterDescriptor extends ParameterDescriptor {
101
+ symbol: TSOutputSymbol;
102
+ }
103
+
104
+ function normalizeAndDeclareParameters(
105
+ parameters: Record<string, Children | ParameterDescriptor> | string[],
106
+ flags: TSSymbolFlags = TSSymbolFlags.ParameterSymbol,
107
+ ): DeclaredParameterDescriptor[] {
108
+ const namePolicy = useTSNamePolicy();
109
+ if (Array.isArray(parameters)) {
110
+ return parameters.map((paramName) => {
111
+ const symbol = createTSSymbol({
112
+ name: namePolicy.getName(
113
+ paramName,
114
+ flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
115
+ ),
116
+ refkey: refkey(),
117
+ tsFlags: flags,
118
+ });
119
+
120
+ return { refkey: symbol.refkey, symbol };
121
+ });
122
+ }
123
+
124
+ return Object.entries(parameters).map(([paramName, value]) => {
125
+ const descriptor: DeclaredParameterDescriptor =
126
+ isParameterDescriptor(value) ? value : (
127
+ ({
128
+ refkey: refkey(),
129
+ type: value,
130
+ } as any)
131
+ );
132
+
133
+ const symbol = createTSSymbol({
134
+ name: namePolicy.getName(
135
+ paramName,
136
+ flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
137
+ ),
138
+ refkey: descriptor.refkey,
139
+ tsFlags: flags,
140
+ });
141
+ descriptor.symbol = symbol;
142
+ return descriptor;
143
+ });
144
+ }
145
+
146
+ export interface FunctionTypeParametersProps {
147
+ parameters?: Record<string, Children | ParameterDescriptor> | string[];
148
+ children?: Children;
149
+ }
150
+
151
+ FunctionDeclaration.TypeParameters = taggedComponent(
152
+ functionTypeParametersTag,
153
+ function TypeParameters(props: FunctionTypeParametersProps) {
154
+ if (props.children) {
155
+ return props.children;
156
+ }
157
+
158
+ if (!props.parameters) {
159
+ return undefined;
160
+ }
161
+
162
+ const typeParameters = normalizeAndDeclareParameters(props.parameters);
163
+
164
+ onCleanup(() => {
165
+ for (const param of typeParameters) {
166
+ param.symbol.binder.deleteSymbol(param.symbol);
167
+ }
168
+ });
169
+
170
+ return (
171
+ <>
172
+ {"<"}
173
+ <group>
174
+ <Indent break="soft">
175
+ <For each={typeParameters} comma line>
176
+ {(param) => typeParameter(param)}
177
+ </For>
178
+ <ifBreak>,</ifBreak>
179
+ </Indent>
180
+ </group>
181
+ {">"}
182
+ </>
183
+ );
184
+ },
185
+ );
186
+
187
+ function typeParameter(param: DeclaredParameterDescriptor) {
188
+ return (
189
+ <group>
190
+ {param.symbol.name}
191
+ <Show when={!!param.type}>
192
+ {" "}
193
+ extends
194
+ <indent> {param.type}</indent>
195
+ </Show>
196
+ </group>
197
+ );
198
+ }
199
+
74
200
  export interface FunctionParametersProps {
75
201
  parameters?: Record<string, Children | ParameterDescriptor>;
76
202
  children?: Children;
@@ -79,54 +205,43 @@ export interface FunctionParametersProps {
79
205
  FunctionDeclaration.Parameters = taggedComponent(
80
206
  functionParametersTag,
81
207
  function Parameters(props: FunctionParametersProps) {
82
- const namePolicy = useTSNamePolicy();
83
-
84
- let value;
85
-
86
208
  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;
209
+ return props.children;
118
210
  }
119
211
 
120
- return value;
212
+ const parameters = normalizeAndDeclareParameters(props.parameters ?? {});
213
+ return (
214
+ <group>
215
+ <Indent break="soft">
216
+ <For each={parameters} comma line>
217
+ {(param) => parameter(param)}
218
+ </For>
219
+ <ifBreak>,</ifBreak>
220
+ </Indent>
221
+ </group>
222
+ );
121
223
  },
122
224
  );
123
225
 
226
+ function parameter(param: DeclaredParameterDescriptor) {
227
+ return (
228
+ <group>
229
+ {param.symbol.name}
230
+ <Show when={!!param.optional}>?</Show>
231
+ <Show when={!!param.type}>
232
+ <indent>: {param.type}</indent>
233
+ </Show>
234
+ </group>
235
+ );
236
+ }
237
+
124
238
  export interface FunctionBodyProps {
125
239
  children?: Children;
126
240
  }
127
241
 
128
- FunctionDeclaration.Body = taggedComponent(functionBodyTag, function Body(
129
- props: FunctionBodyProps,
130
- ) {
131
- return props.children;
132
- });
242
+ FunctionDeclaration.Body = taggedComponent(
243
+ functionBodyTag,
244
+ function Body(props: FunctionBodyProps) {
245
+ return <Block>{props.children}</Block>;
246
+ },
247
+ );