@alloy-js/typescript 0.6.0 → 0.8.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 (230) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/dist/src/components/ArrayExpression.d.ts.map +1 -1
  3. package/dist/src/components/ArrayExpression.js +9 -3
  4. package/dist/src/components/ArrayExpression.js.map +1 -1
  5. package/dist/src/components/BlockScope.d.ts +13 -0
  6. package/dist/src/components/BlockScope.d.ts.map +1 -0
  7. package/dist/src/components/BlockScope.js +20 -0
  8. package/dist/src/components/BlockScope.js.map +1 -0
  9. package/dist/src/components/CallSignature.d.ts +38 -0
  10. package/dist/src/components/CallSignature.d.ts.map +1 -0
  11. package/dist/src/components/CallSignature.js +26 -0
  12. package/dist/src/components/CallSignature.js.map +1 -0
  13. package/dist/src/components/ClassDeclaration.d.ts +3 -4
  14. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  15. package/dist/src/components/ClassDeclaration.js +99 -41
  16. package/dist/src/components/ClassDeclaration.js.map +1 -1
  17. package/dist/src/components/Declaration.d.ts +21 -4
  18. package/dist/src/components/Declaration.d.ts.map +1 -1
  19. package/dist/src/components/Declaration.js +40 -21
  20. package/dist/src/components/Declaration.js.map +1 -1
  21. package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
  22. package/dist/src/components/EnumDeclaration.js +19 -6
  23. package/dist/src/components/EnumDeclaration.js.map +1 -1
  24. package/dist/src/components/EnumMember.d.ts +8 -0
  25. package/dist/src/components/EnumMember.d.ts.map +1 -1
  26. package/dist/src/components/EnumMember.js +27 -5
  27. package/dist/src/components/EnumMember.js.map +1 -1
  28. package/dist/src/components/ExportStatement.d.ts.map +1 -1
  29. package/dist/src/components/ExportStatement.js +3 -1
  30. package/dist/src/components/ExportStatement.js.map +1 -1
  31. package/dist/src/components/FunctionCallExpression.js +1 -1
  32. package/dist/src/components/FunctionCallExpression.js.map +1 -1
  33. package/dist/src/components/FunctionDeclaration.d.ts +21 -12
  34. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  35. package/dist/src/components/FunctionDeclaration.js +80 -45
  36. package/dist/src/components/FunctionDeclaration.js.map +1 -1
  37. package/dist/src/components/IfStatement.d.ts +25 -0
  38. package/dist/src/components/IfStatement.d.ts.map +1 -0
  39. package/dist/src/components/IfStatement.js +34 -0
  40. package/dist/src/components/IfStatement.js.map +1 -0
  41. package/dist/src/components/Interface.d.ts +10 -2
  42. package/dist/src/components/Interface.d.ts.map +1 -1
  43. package/dist/src/components/Interface.js +66 -6
  44. package/dist/src/components/Interface.js.map +1 -1
  45. package/dist/src/components/JSDoc.d.ts.map +1 -1
  46. package/dist/src/components/JSDoc.js +1 -5
  47. package/dist/src/components/JSDoc.js.map +1 -1
  48. package/dist/src/components/JSDocParam.d.ts +22 -0
  49. package/dist/src/components/JSDocParam.d.ts.map +1 -0
  50. package/dist/src/components/JSDocParam.js +121 -0
  51. package/dist/src/components/JSDocParam.js.map +1 -0
  52. package/dist/src/components/MemberChainExpression.d.ts +2 -2
  53. package/dist/src/components/MemberChainExpression.d.ts.map +1 -1
  54. package/dist/src/components/MemberChainExpression.js +43 -7
  55. package/dist/src/components/MemberChainExpression.js.map +1 -1
  56. package/dist/src/components/ObjectExpression.d.ts.map +1 -1
  57. package/dist/src/components/ObjectExpression.js +9 -2
  58. package/dist/src/components/ObjectExpression.js.map +1 -1
  59. package/dist/src/components/PackageDirectory.d.ts +1 -3
  60. package/dist/src/components/PackageDirectory.d.ts.map +1 -1
  61. package/dist/src/components/PackageDirectory.js +9 -16
  62. package/dist/src/components/PackageDirectory.js.map +1 -1
  63. package/dist/src/components/PackageJson.d.ts +39 -0
  64. package/dist/src/components/PackageJson.d.ts.map +1 -1
  65. package/dist/src/components/PackageJson.js +30 -12
  66. package/dist/src/components/PackageJson.js.map +1 -1
  67. package/dist/src/components/PropertyName.d.ts +19 -0
  68. package/dist/src/components/PropertyName.d.ts.map +1 -0
  69. package/dist/src/components/PropertyName.js +36 -0
  70. package/dist/src/components/PropertyName.js.map +1 -0
  71. package/dist/src/components/SingleLineCommentBlock.d.ts +10 -0
  72. package/dist/src/components/SingleLineCommentBlock.d.ts.map +1 -0
  73. package/dist/src/components/SingleLineCommentBlock.js +20 -0
  74. package/dist/src/components/SingleLineCommentBlock.js.map +1 -0
  75. package/dist/src/components/SourceFile.d.ts +6 -0
  76. package/dist/src/components/SourceFile.d.ts.map +1 -1
  77. package/dist/src/components/SourceFile.js +31 -0
  78. package/dist/src/components/SourceFile.js.map +1 -1
  79. package/dist/src/components/SwitchStatement.d.ts +31 -0
  80. package/dist/src/components/SwitchStatement.d.ts.map +1 -0
  81. package/dist/src/components/SwitchStatement.js +48 -0
  82. package/dist/src/components/SwitchStatement.js.map +1 -0
  83. package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
  84. package/dist/src/components/TypeDeclaration.js +16 -3
  85. package/dist/src/components/TypeDeclaration.js.map +1 -1
  86. package/dist/src/components/VarDeclaration.d.ts +1 -1
  87. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  88. package/dist/src/components/VarDeclaration.js +20 -6
  89. package/dist/src/components/VarDeclaration.js.map +1 -1
  90. package/dist/src/components/index.d.ts +7 -0
  91. package/dist/src/components/index.d.ts.map +1 -1
  92. package/dist/src/components/index.js +7 -0
  93. package/dist/src/components/index.js.map +1 -1
  94. package/dist/src/components/stc/index.d.ts +35 -112
  95. package/dist/src/components/stc/index.d.ts.map +1 -1
  96. package/dist/src/components/stc/index.js +8 -1
  97. package/dist/src/components/stc/index.js.map +1 -1
  98. package/dist/src/context/index.d.ts +2 -0
  99. package/dist/src/context/index.d.ts.map +1 -0
  100. package/dist/src/context/index.js +1 -0
  101. package/dist/src/context/index.js.map +1 -0
  102. package/dist/src/context/private-scope.d.ts +18 -0
  103. package/dist/src/context/private-scope.d.ts.map +1 -0
  104. package/dist/src/context/private-scope.js +9 -0
  105. package/dist/src/context/private-scope.js.map +1 -0
  106. package/dist/src/index.d.ts +1 -0
  107. package/dist/src/index.d.ts.map +1 -1
  108. package/dist/src/index.js +1 -0
  109. package/dist/src/index.js.map +1 -1
  110. package/dist/src/parameter-descriptor.d.ts +60 -0
  111. package/dist/src/parameter-descriptor.d.ts.map +1 -0
  112. package/dist/src/parameter-descriptor.js +1 -0
  113. package/dist/src/parameter-descriptor.js.map +1 -0
  114. package/dist/src/symbols/reference.d.ts.map +1 -1
  115. package/dist/src/symbols/reference.js +55 -14
  116. package/dist/src/symbols/reference.js.map +1 -1
  117. package/dist/src/symbols/ts-member-scope.d.ts +1 -1
  118. package/dist/src/symbols/ts-member-scope.d.ts.map +1 -1
  119. package/dist/src/symbols/ts-member-scope.js +2 -0
  120. package/dist/src/symbols/ts-member-scope.js.map +1 -1
  121. package/dist/src/symbols/ts-output-symbol.d.ts +7 -3
  122. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  123. package/dist/src/symbols/ts-output-symbol.js +14 -2
  124. package/dist/src/symbols/ts-output-symbol.js.map +1 -1
  125. package/dist/src/symbols/ts-package-scope.d.ts +0 -15
  126. package/dist/src/symbols/ts-package-scope.d.ts.map +1 -1
  127. package/dist/src/symbols/ts-package-scope.js +0 -8
  128. package/dist/src/symbols/ts-package-scope.js.map +1 -1
  129. package/dist/src/utils.d.ts +7 -0
  130. package/dist/src/utils.d.ts.map +1 -1
  131. package/dist/src/utils.js +17 -0
  132. package/dist/src/utils.js.map +1 -1
  133. package/dist/test/block-scope.test.d.ts +2 -0
  134. package/dist/test/block-scope.test.d.ts.map +1 -0
  135. package/dist/test/enum-jsdoc.test.d.ts +2 -0
  136. package/dist/test/enum-jsdoc.test.d.ts.map +1 -0
  137. package/dist/test/function-declaration-jsdoc.test.d.ts +2 -0
  138. package/dist/test/function-declaration-jsdoc.test.d.ts.map +1 -0
  139. package/dist/test/if-statement.test.d.ts +2 -0
  140. package/dist/test/if-statement.test.d.ts.map +1 -0
  141. package/dist/test/interface-jsdoc.test.d.ts +2 -0
  142. package/dist/test/interface-jsdoc.test.d.ts.map +1 -0
  143. package/dist/test/jscomment.test.d.ts +2 -0
  144. package/dist/test/jscomment.test.d.ts.map +1 -0
  145. package/dist/test/jsdoc.test.d.ts.map +1 -1
  146. package/dist/test/jsdocparam.test.d.ts +2 -0
  147. package/dist/test/jsdocparam.test.d.ts.map +1 -0
  148. package/dist/test/name-conflict.test.d.ts +2 -0
  149. package/dist/test/name-conflict.test.d.ts.map +1 -0
  150. package/dist/test/package-json.test.d.ts +2 -0
  151. package/dist/test/package-json.test.d.ts.map +1 -0
  152. package/dist/test/source-file.test.d.ts +2 -0
  153. package/dist/test/source-file.test.d.ts.map +1 -0
  154. package/dist/test/switch-statement.test.d.ts +2 -0
  155. package/dist/test/switch-statement.test.d.ts.map +1 -0
  156. package/dist/test/var-declaration-jsdoc.test.d.ts +2 -0
  157. package/dist/test/var-declaration-jsdoc.test.d.ts.map +1 -0
  158. package/dist/tsconfig.tsbuildinfo +1 -1
  159. package/package.json +3 -3
  160. package/src/components/ArrayExpression.tsx +13 -8
  161. package/src/components/BlockScope.tsx +31 -0
  162. package/src/components/CallSignature.tsx +67 -0
  163. package/src/components/ClassDeclaration.tsx +103 -50
  164. package/src/components/Declaration.tsx +72 -22
  165. package/src/components/EnumDeclaration.tsx +27 -19
  166. package/src/components/EnumMember.tsx +24 -4
  167. package/src/components/ExportStatement.tsx +3 -1
  168. package/src/components/FunctionCallExpression.tsx +1 -1
  169. package/src/components/FunctionDeclaration.tsx +107 -81
  170. package/src/components/IfStatement.tsx +51 -0
  171. package/src/components/Interface.tsx +61 -14
  172. package/src/components/JSDoc.tsx +1 -10
  173. package/src/components/JSDocParam.tsx +115 -0
  174. package/src/components/MemberChainExpression.tsx +37 -5
  175. package/src/components/ObjectExpression.tsx +7 -2
  176. package/src/components/PackageDirectory.tsx +12 -22
  177. package/src/components/PackageJson.tsx +86 -27
  178. package/src/components/PropertyName.tsx +50 -0
  179. package/src/components/SingleLineCommentBlock.tsx +21 -0
  180. package/src/components/SourceFile.tsx +29 -0
  181. package/src/components/SwitchStatement.tsx +78 -0
  182. package/src/components/TypeDeclaration.tsx +11 -4
  183. package/src/components/VarDeclaration.tsx +20 -12
  184. package/src/components/index.ts +7 -0
  185. package/src/components/stc/index.ts +8 -1
  186. package/src/context/index.ts +1 -0
  187. package/src/context/private-scope.ts +23 -0
  188. package/src/index.ts +1 -0
  189. package/src/parameter-descriptor.ts +71 -0
  190. package/src/symbols/reference.ts +77 -22
  191. package/src/symbols/ts-member-scope.ts +6 -2
  192. package/src/symbols/ts-output-symbol.ts +27 -10
  193. package/src/symbols/ts-package-scope.ts +0 -28
  194. package/src/utils.ts +33 -0
  195. package/temp/api.json +5372 -3410
  196. package/test/array.test.tsx +72 -0
  197. package/test/basic.test.tsx +2 -2
  198. package/test/block-scope.test.tsx +29 -0
  199. package/test/class.test.tsx +173 -5
  200. package/test/enum-jsdoc.test.tsx +74 -0
  201. package/test/enum.test.tsx +50 -3
  202. package/test/function-call-expression.test.tsx +5 -2
  203. package/test/function-declaration-jsdoc.test.tsx +89 -0
  204. package/test/function-declaration.test.tsx +13 -9
  205. package/test/if-statement.test.tsx +28 -0
  206. package/test/imports.test.tsx +25 -10
  207. package/test/interface-jsdoc.test.tsx +164 -0
  208. package/test/interface.test.tsx +17 -3
  209. package/test/jscomment.test.tsx +104 -0
  210. package/test/jsdoc.test.tsx +9 -15
  211. package/test/jsdocparam.test.tsx +188 -0
  212. package/test/member-chain.test.tsx +77 -0
  213. package/test/name-conflict.test.tsx +105 -0
  214. package/test/name-policy.test.tsx +1 -1
  215. package/test/package-external.test.tsx +98 -3
  216. package/test/package-json.test.tsx +93 -0
  217. package/test/package.test.tsx +56 -3
  218. package/test/reference.test.tsx +6 -2
  219. package/test/source-file.test.tsx +38 -0
  220. package/test/switch-statement.test.tsx +50 -0
  221. package/test/value-expression.test.tsx +9 -0
  222. package/test/var-declaration-jsdoc.test.tsx +70 -0
  223. package/test/var-declaration.test.tsx +1 -1
  224. package/tsconfig.json +1 -8
  225. package/vitest.config.ts +2 -10
  226. package/dist/src/components/JSDocParagraph.d.ts +0 -10
  227. package/dist/src/components/JSDocParagraph.d.ts.map +0 -1
  228. package/dist/src/components/JSDocParagraph.js +0 -23
  229. package/dist/src/components/JSDocParagraph.js.map +0 -1
  230. package/src/components/JSDocParagraph.tsx +0 -35
@@ -7,6 +7,7 @@ import {
7
7
  shallowRef,
8
8
  SourceDirectory,
9
9
  SourceDirectoryContext,
10
+ splitProps,
10
11
  useBinder,
11
12
  useContext,
12
13
  useScope,
@@ -14,17 +15,10 @@ import {
14
15
  import { join } from "pathe";
15
16
  import { createTSPackageScope, TSPackageScope } from "../symbols/index.js";
16
17
  import { modulePath } from "../utils.js";
17
- import {
18
- ExportPath,
19
- PackageExports,
20
- PackageJsonFile,
21
- PackageJsonFileProps,
22
- } from "./PackageJson.js";
18
+ import { PackageJsonFile, PackageJsonFileProps } from "./PackageJson.js";
23
19
  import { TSConfigJson } from "./TsConfigJson.js";
24
20
 
25
21
  export interface PackageDirectoryProps extends PackageJsonFileProps {
26
- dependencies?: Record<string, string>;
27
- exports?: PackageExports | ExportPath;
28
22
  tsConfig?: { outDir?: string };
29
23
  children?: Children;
30
24
  path?: string;
@@ -54,27 +48,23 @@ export function PackageDirectory(props: PackageDirectoryProps) {
54
48
  props.path ?? ".",
55
49
  );
56
50
 
57
- if (props.exports) {
58
- if (typeof props.exports === "string") {
59
- packageContext.scope.addRawExport(".", props.exports);
60
- } else {
61
- for (const ex of Object.entries(props.exports)) {
62
- packageContext.scope.addRawExport(ex[0], ex[1]);
63
- }
64
- }
51
+ const devDeps = props.devDependencies ?? {};
52
+ if (!devDeps["typescript"]) {
53
+ devDeps["typescript"] = "^5.5.2";
65
54
  }
66
55
 
67
- if (props.dependencies) {
68
- for (const dep of Object.entries(props.dependencies)) {
69
- packageContext.scope.addRawDependency(dep[0], dep[1]);
70
- }
71
- }
56
+ const [_, pkgJsonProps] = splitProps(props, [
57
+ "tsConfig",
58
+ "children",
59
+ "path",
60
+ "devDependencies",
61
+ ]);
72
62
 
73
63
  return (
74
64
  <SourceDirectory path={props.path ?? "."}>
75
65
  <PackageContext.Provider value={packageContext}>
76
66
  <Scope value={packageContext.scope}>
77
- <PackageJsonFile {...props} />
67
+ <PackageJsonFile {...pkgJsonProps} devDependencies={devDeps} />
78
68
  <TSConfigJson {...props.tsConfig} />
79
69
  {props.children}
80
70
  </Scope>
@@ -3,10 +3,47 @@ import { modulePath } from "../utils.js";
3
3
  import { usePackage } from "./PackageDirectory.js";
4
4
 
5
5
  export interface PackageJsonFileProps {
6
+ /** The name of the package */
6
7
  name: string;
8
+
9
+ /** The version of the package */
7
10
  version: string;
11
+
12
+ /** The description of the package */
13
+ description?: string;
14
+
15
+ /** The license of the package */
16
+ license?: string;
17
+
18
+ /** The author of the package */
19
+ author?: string;
20
+
21
+ /** The homepage of the package */
22
+ homepage?: string;
23
+
24
+ /** The repository of the package */
25
+ repository?: string | { type: string; url: string };
26
+
27
+ /** The keywords of the package */
28
+ keywords?: string[];
29
+
30
+ /** Whether this is a commonjs or module. Defaults to module. */
8
31
  type?: "module" | "commonjs";
32
+
33
+ /**
34
+ * The hard-coded dependencies of the package. References to external packages
35
+ * will add to the dependency list automatically. Only list dependencies here
36
+ * that are not referenced via refkey.
37
+ */
38
+ dependencies?: Record<string, string>;
39
+
40
+ /** The hard-coded exports of the package. */
41
+ exports?: PackageExports | ExportPath;
42
+
43
+ /** The dev dependencies of the package. */
9
44
  devDependencies?: Record<string, string>;
45
+
46
+ /** The scripts entries of the package. */
10
47
  scripts?: Record<string, string>;
11
48
  }
12
49
 
@@ -20,46 +57,68 @@ export interface PackageExports {
20
57
  [key: string]: ExportPath;
21
58
  }
22
59
 
60
+ /**
61
+ * A file containing a package.json that defines a package. When placed inside a
62
+ * {@link PackageDirectory}, it will contain dependencies and exports for
63
+ * symbols defined inside the package. Additional dependencies and exports can
64
+ * be provided.
65
+ *
66
+ * @example
67
+ * ```tsx
68
+ * <PackageJsonFile name="my-package" version="1.0.0" />
69
+ * ```
70
+ */
23
71
  export function PackageJsonFile(props: PackageJsonFileProps) {
24
72
  const pkg = usePackage();
25
- if (!pkg) {
26
- throw new Error(
27
- "Package json component needs to be inside a PackageDirectory",
28
- );
29
- }
30
73
 
31
74
  const jsonContent = memo(() => {
32
75
  const pkgJson = {
33
76
  name: props.name,
34
77
  version: props.version,
78
+ author: props.author,
79
+ description: props.description,
80
+ license: props.license,
81
+ homepage: props.homepage,
35
82
  type: props.type ?? "module",
36
- dependencies: Object.fromEntries([
37
- ...pkg.scope.rawDependencies.entries(),
38
- ...Array.from(pkg.scope.dependencies).map((dependency) => [
39
- dependency.name,
40
- dependency.version,
41
- ]),
42
- ]),
43
- devDependencies: {
44
- ...props.devDependencies,
45
- typescript: "^5.5.2",
46
- },
83
+ dependencies:
84
+ props.dependencies || (pkg && pkg.scope.dependencies.size > 0) ?
85
+ Object.fromEntries([
86
+ ...Object.entries(props.dependencies ?? {}),
87
+ ...(pkg ?
88
+ Array.from(pkg.scope.dependencies).map((dependency) => [
89
+ dependency.name,
90
+ dependency.version,
91
+ ])
92
+ : []),
93
+ ])
94
+ : undefined,
95
+ devDependencies: props.devDependencies,
47
96
  scripts: props.scripts,
48
97
  exports: undefined as any,
49
98
  };
50
99
 
51
- const exportsEntries: [string, ExportPath][] = [];
52
- for (const [publicPath, module] of pkg.scope.exportedSymbols) {
53
- exportsEntries.push([
54
- publicPath,
55
- modulePath(pkg.outFileMapper.value(module.name)),
56
- ]);
57
- }
100
+ if (typeof props.exports === "string") {
101
+ pkgJson.exports = props.exports;
102
+ } else {
103
+ const exportsEntries: [string, ExportPath][] = [];
104
+ for (const entry of Object.entries(props.exports ?? {})) {
105
+ exportsEntries.push(entry);
106
+ }
107
+
108
+ if (pkg) {
109
+ for (const [publicPath, module] of pkg.scope.exportedSymbols) {
110
+ exportsEntries.push([
111
+ publicPath,
112
+ modulePath(pkg.outFileMapper.value(module.name)),
113
+ ]);
114
+ }
115
+ }
58
116
 
59
- pkgJson.exports =
60
- exportsEntries.length === 0 ?
61
- undefined
62
- : Object.fromEntries(exportsEntries);
117
+ pkgJson.exports =
118
+ exportsEntries.length === 0 ?
119
+ undefined
120
+ : Object.fromEntries(exportsEntries);
121
+ }
63
122
 
64
123
  return JSON.stringify(pkgJson, null, 2);
65
124
  });
@@ -0,0 +1,50 @@
1
+ import { MemberDeclarationContext, useContext } from "@alloy-js/core";
2
+ import { TSOutputSymbol, TSSymbolFlags } from "../symbols/ts-output-symbol.js";
3
+ import { isValidJSIdentifier } from "../utils.js";
4
+
5
+ export interface PropertyNameProps {
6
+ /**
7
+ * The name of the property.
8
+ */
9
+ name?: string;
10
+
11
+ /**
12
+ * Whether the property is a private property. If `true`, the property will be
13
+ * prefixed with `#`.
14
+ */
15
+ private?: boolean;
16
+ }
17
+
18
+ /**
19
+ * A TypeScript property name for an interface, class, or object member. If the
20
+ * name is not a valid JavaScript identifier, it will be quoted. If a `name` prop
21
+ * is provided, it will be used as the property name. Otherwise, the name will be
22
+ * taken from the {@link (MemberDeclarationContext:variable)}.
23
+ */
24
+ export function PropertyName(props: PropertyNameProps) {
25
+ if (props.name) {
26
+ if (props.private) {
27
+ return "#" + props.name;
28
+ }
29
+ return quoteIfNeeded(props.name);
30
+ } else {
31
+ const declSymbol = useContext(MemberDeclarationContext) as TSOutputSymbol;
32
+ if (!declSymbol) {
33
+ return "(no member declaration context)";
34
+ }
35
+
36
+ if (declSymbol.tsFlags & TSSymbolFlags.PrivateMember) {
37
+ return <>#{declSymbol.name}</>;
38
+ } else {
39
+ return <>{quoteIfNeeded(declSymbol.name)}</>;
40
+ }
41
+ }
42
+ }
43
+
44
+ function quoteIfNeeded(name: string) {
45
+ if (isValidJSIdentifier(name)) {
46
+ return name;
47
+ } else {
48
+ return `"${name.replace(/"/g, '\\"')}"`;
49
+ }
50
+ }
@@ -0,0 +1,21 @@
1
+ import { Prose } from "@alloy-js/core";
2
+ import { Children } from "@alloy-js/core/jsx-runtime";
3
+
4
+ export interface SingleLineCommentBlockProps {
5
+ children: Children;
6
+ }
7
+
8
+ /**
9
+ * A single line comment block. The children are rendered as a prose element, which means that they
10
+ * are broken into multiple lines
11
+ */
12
+ export function SingleLineCommentBlock(props: SingleLineCommentBlockProps) {
13
+ return (
14
+ <>
15
+ //{" "}
16
+ <align string="// ">
17
+ <Prose>{props.children}</Prose>
18
+ </align>
19
+ </>
20
+ );
21
+ }
@@ -17,6 +17,7 @@ import { createTSModuleScope, TSModuleScope } from "../symbols/index.js";
17
17
  import { ImportStatements } from "./ImportStatement.js";
18
18
  import { PackageContext } from "./PackageDirectory.js";
19
19
  import { Reference } from "./Reference.js";
20
+ import { SingleLineCommentBlock } from "./SingleLineCommentBlock.jsx";
20
21
  export interface SourceFileContext {
21
22
  scope: TSModuleScope;
22
23
  }
@@ -30,6 +31,8 @@ export function useSourceFile() {
30
31
  export interface SourceFileProps {
31
32
  path: string;
32
33
  children?: Children;
34
+ header?: Children;
35
+ headerComment?: string;
33
36
  export?: boolean | string;
34
37
  }
35
38
 
@@ -59,11 +62,20 @@ export function SourceFile(props: SourceFileProps) {
59
62
  }
60
63
  }
61
64
 
65
+ const header =
66
+ props.header || props.headerComment ?
67
+ <SourceFileHeader
68
+ header={props.header}
69
+ headerComment={props.headerComment}
70
+ />
71
+ : undefined;
72
+
62
73
  return (
63
74
  <CoreSourceFile
64
75
  path={props.path}
65
76
  filetype="typescript"
66
77
  reference={Reference}
78
+ header={header}
67
79
  >
68
80
  <Show when={scope.importedModules.size > 0}>
69
81
  <ImportStatements records={scope.importedModules} />
@@ -76,3 +88,20 @@ export function SourceFile(props: SourceFileProps) {
76
88
  </CoreSourceFile>
77
89
  );
78
90
  }
91
+
92
+ export interface SourceFileHeaderProps {
93
+ header?: Children;
94
+ headerComment?: string;
95
+ }
96
+
97
+ function SourceFileHeader(props: SourceFileHeaderProps) {
98
+ return (
99
+ <>
100
+ <Show when={props.headerComment !== undefined}>
101
+ <SingleLineCommentBlock>{props.headerComment}</SingleLineCommentBlock>
102
+ </Show>
103
+ <Show when={props.header !== undefined}>{props.header}</Show>
104
+ <hbr />
105
+ </>
106
+ );
107
+ }
@@ -0,0 +1,78 @@
1
+ import { Block, Indent, Match, Switch } from "@alloy-js/core";
2
+ import { Children } from "@alloy-js/core/jsx-runtime";
3
+ import { BlockScope } from "./BlockScope.jsx";
4
+ import { ValueExpression } from "./ValueExpression.jsx";
5
+
6
+ export interface SwitchStatementProps {
7
+ expression: Children;
8
+ children: Children;
9
+ }
10
+
11
+ /**
12
+ * A switch statement.
13
+ */
14
+ export function SwitchStatement(props: SwitchStatementProps) {
15
+ return (
16
+ <>
17
+ switch ({props.expression}) <Block>{props.children}</Block>
18
+ </>
19
+ );
20
+ }
21
+
22
+ export interface CaseClausePropsBase {
23
+ children: Children;
24
+ block?: boolean;
25
+ break?: boolean;
26
+ }
27
+
28
+ export interface CaseClausePropsWithExpression extends CaseClausePropsBase {
29
+ expression: Children;
30
+ }
31
+
32
+ export interface CaseClausePropsWithValue extends CaseClausePropsBase {
33
+ jsValue: unknown;
34
+ }
35
+
36
+ export interface CaseClausePropsWithDefault extends CaseClausePropsBase {
37
+ default: true;
38
+ }
39
+
40
+ export type CaseClauseProps =
41
+ | CaseClausePropsWithExpression
42
+ | CaseClausePropsWithValue
43
+ | CaseClausePropsWithDefault;
44
+
45
+ /**
46
+ * A case clause in a switch statement. Provide either an expression or a JS
47
+ * value to compare to the switch expression. Can optionally be wrapped in a
48
+ * block. Pass `break` to finish with a break statement.
49
+ */
50
+ export function CaseClause(props: CaseClauseProps) {
51
+ const Wrapper = props.block ? BlockScope : Indent;
52
+ return (
53
+ <>
54
+ <Switch>
55
+ <Match when={"default" in props && props.default}>default </Match>
56
+ <Match else>
57
+ case{" "}
58
+ {"expression" in props ?
59
+ props.expression
60
+ : <ValueExpression
61
+ jsValue={(props as CaseClausePropsWithValue).jsValue}
62
+ />
63
+ }
64
+ </Match>
65
+ </Switch>
66
+ {": "}
67
+ <Wrapper>
68
+ {props.children}
69
+ {props.break && (
70
+ <>
71
+ <hbr />
72
+ break;
73
+ </>
74
+ )}
75
+ </Wrapper>
76
+ </>
77
+ );
78
+ }
@@ -1,12 +1,19 @@
1
- import { Name } from "@alloy-js/core";
1
+ import { Name, Show } from "@alloy-js/core";
2
2
  import { BaseDeclarationProps, Declaration } from "./Declaration.js";
3
+ import { JSDoc } from "./JSDoc.jsx";
3
4
 
4
5
  export interface TypeDeclarationProps extends BaseDeclarationProps {}
5
6
 
6
7
  export function TypeDeclaration(props: TypeDeclarationProps) {
7
8
  return (
8
- <Declaration {...props} nameKind="type">
9
- type <Name /> = {props.children};
10
- </Declaration>
9
+ <>
10
+ <Show when={Boolean(props.doc)}>
11
+ <JSDoc children={props.doc} />
12
+ <hbr />
13
+ </Show>
14
+ <Declaration {...props} nameKind="type">
15
+ type <Name /> = {props.children};
16
+ </Declaration>
17
+ </>
11
18
  );
12
19
  }
@@ -4,17 +4,18 @@ import {
4
4
  Declaration as CoreDeclaration,
5
5
  createAssignmentContext,
6
6
  Name,
7
- refkey,
7
+ Show,
8
8
  } from "@alloy-js/core";
9
9
  import { useTSNamePolicy } from "../name-policy.js";
10
10
  import { createTSSymbol } from "../symbols/index.js";
11
11
  import { BaseDeclarationProps } from "./Declaration.js";
12
+ import { JSDoc } from "./JSDoc.jsx";
12
13
 
13
14
  export interface VarDeclarationProps extends BaseDeclarationProps {
14
15
  const?: boolean;
15
16
  let?: boolean;
16
17
  var?: boolean;
17
- value?: Children;
18
+ initializer?: Children;
18
19
  type?: Children;
19
20
  }
20
21
 
@@ -27,22 +28,29 @@ export function VarDeclaration(props: VarDeclarationProps) {
27
28
  const name = useTSNamePolicy().getName(props.name, "variable");
28
29
  const sym = createTSSymbol({
29
30
  name: name,
30
- refkey: props.refkey ?? refkey(name),
31
+ refkey: props.refkey,
31
32
  default: props.default,
32
33
  export: props.export,
34
+ metadata: props.metadata,
33
35
  });
34
36
 
35
37
  const assignmentContext = createAssignmentContext(sym);
36
38
 
37
39
  return (
38
- <CoreDeclaration symbol={sym}>
39
- {props.export ? "export " : ""}
40
- {props.default ? "default " : ""}
41
- {keyword} <Name />
42
- {type} ={" "}
43
- <AssignmentContext.Provider value={assignmentContext}>
44
- {props.value ?? props.children}
45
- </AssignmentContext.Provider>
46
- </CoreDeclaration>
40
+ <>
41
+ <Show when={Boolean(props.doc)}>
42
+ <JSDoc children={props.doc} />
43
+ <hbr />
44
+ </Show>
45
+ <CoreDeclaration symbol={sym}>
46
+ {props.export ? "export " : ""}
47
+ {props.default ? "default " : ""}
48
+ {keyword} <Name />
49
+ {type} ={" "}
50
+ <AssignmentContext.Provider value={assignmentContext}>
51
+ {props.initializer ?? props.children}
52
+ </AssignmentContext.Provider>
53
+ </CoreDeclaration>
54
+ </>
47
55
  );
48
56
  }
@@ -1,5 +1,8 @@
1
+ export * from "../parameter-descriptor.js";
1
2
  export * from "./ArrayExpression.js";
2
3
  export * from "./BarrelFile.js";
4
+ export * from "./BlockScope.js";
5
+ export * from "./CallSignature.jsx";
3
6
  export * from "./ClassDeclaration.jsx";
4
7
  export * from "./CommaList.jsx";
5
8
  export * from "./Declaration.js";
@@ -10,12 +13,16 @@ export * from "./FunctionCallExpression.js";
10
13
  export * from "./FunctionDeclaration.js";
11
14
  export * from "./ImportStatement.js";
12
15
  export * from "./Interface.js";
16
+ export * from "./JSDoc.jsx";
17
+ export * from "./JSDocComment.jsx";
18
+ export * from "./JSDocExample.jsx";
13
19
  export * from "./MemberChainExpression.jsx";
14
20
  export * from "./ObjectExpression.js";
15
21
  export * from "./PackageDirectory.js";
16
22
  export * from "./PackageJson.js";
17
23
  export * from "./Reference.js";
18
24
  export * from "./SourceFile.js";
25
+ export * from "./SwitchStatement.jsx";
19
26
  export * from "./TsConfigJson.js";
20
27
  export * from "./TypeDeclaration.js";
21
28
  export * from "./ValueExpression.js";
@@ -3,10 +3,12 @@ import * as base from "../index.js";
3
3
 
4
4
  export const ArrayExpression = stc(base.ArrayExpression);
5
5
  export const BarrelFile = stc(base.BarrelFile);
6
+ export const CallSignature = stc(base.CallSignature);
6
7
  export const ClassDeclaration = stc(base.ClassDeclaration);
7
8
  export const ClassField = stc(base.ClassField);
8
9
  export const ClassMember = stc(base.ClassMember);
9
10
  export const ClassMethod = stc(base.ClassMethod);
11
+ export const CommaList = stc(base.CommaList);
10
12
  export const Declaration = stc(base.Declaration);
11
13
  export const EnumDeclaration = stc(base.EnumDeclaration);
12
14
  export const EnumMember = stc(base.EnumMember);
@@ -18,13 +20,18 @@ export const ImportStatements = stc(base.ImportStatements);
18
20
  export const InterfaceDeclaration = stc(base.InterfaceDeclaration);
19
21
  export const InterfaceExpression = stc(base.InterfaceExpression);
20
22
  export const InterfaceMember = stc(base.InterfaceMember);
21
- export const MemberchainExpression = stc(base.MemberChainExpression);
23
+ export const JSDoc = stc(base.JSDoc);
24
+ export const JSDocExample = stc(base.JSDocExample);
25
+ export const JSDocComment = stc(base.JSDocComment);
26
+ export const MemberChainExpression = stc(base.MemberChainExpression);
22
27
  export const ObjectExpression = stc(base.ObjectExpression);
23
28
  export const ObjectProperty = stc(base.ObjectProperty);
24
29
  export const PackageDirectory = stc(base.PackageDirectory);
25
30
  export const PackageJsonFile = stc(base.PackageJsonFile);
26
31
  export const Reference = stc(base.Reference);
27
32
  export const SourceFile = stc(base.SourceFile);
33
+ export const SwitchStatement = stc(base.SwitchStatement);
34
+ export const CaseClause = stc(base.CaseClause);
28
35
  export const TSConfigJson = stc(base.TSConfigJson);
29
36
  export const TypeDeclaration = stc(base.TypeDeclaration);
30
37
  export const ValueExpression = stc(base.ValueExpression);
@@ -0,0 +1 @@
1
+ export * from "./private-scope.js";
@@ -0,0 +1,23 @@
1
+ import { ComponentContext, createContext, useContext } from "@alloy-js/core";
2
+ import { TSMemberScope } from "../symbols/ts-member-scope.js";
3
+
4
+ export interface PrivateScopeContext {
5
+ /**
6
+ * Scope for private static members.
7
+ */
8
+ staticMembers: TSMemberScope;
9
+ /**
10
+ * Scope for private instance members.
11
+ */
12
+ instanceMembers: TSMemberScope;
13
+ }
14
+
15
+ /**
16
+ * Provides scopes for instance and static private members.
17
+ */
18
+ export const PrivateScopeContext: ComponentContext<PrivateScopeContext> =
19
+ createContext<PrivateScopeContext>();
20
+
21
+ export function usePrivateScope() {
22
+ return useContext(PrivateScopeContext);
23
+ }
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./builtins/index.js";
2
2
  export * from "./components/index.js";
3
+ export * from "./context/index.js";
3
4
  export * from "./create-package.js";
4
5
  export * from "./name-conflict-resolver.js";
5
6
  export * from "./name-policy.js";
@@ -0,0 +1,71 @@
1
+ import { Children, Refkey } from "@alloy-js/core";
2
+
3
+ /**
4
+ * Information for a TypeScript function parameter.
5
+ */
6
+ export interface ParameterDescriptor {
7
+ /**
8
+ * The name of the parameter.
9
+ */
10
+ name: string;
11
+
12
+ /**
13
+ * The type of the parameter.
14
+ */
15
+ type?: Children;
16
+
17
+ /**
18
+ * The default value of the parameter.
19
+ */
20
+ default?: Children;
21
+
22
+ /**
23
+ * The refkey for this parameter.
24
+ */
25
+ refkey?: Refkey | Refkey[];
26
+
27
+ /**
28
+ * Whether the parameter is optional.
29
+ */
30
+ optional?: boolean;
31
+
32
+ /**
33
+ * Arbitrary metadata for the parameter symbol.
34
+ */
35
+ metadata?: Record<string, unknown>;
36
+
37
+ /**
38
+ * Documentation for the parameter.
39
+ */
40
+ doc?: Children;
41
+ }
42
+
43
+ /**
44
+ * Information for a TypeScript generic type parameter.
45
+ */
46
+ export interface TypeParameterDescriptor {
47
+ /**
48
+ * The name of the type parameter.
49
+ */
50
+ name: string;
51
+
52
+ /**
53
+ * The extends constraint for the type parameter.
54
+ */
55
+ extends?: Children;
56
+
57
+ /**
58
+ * The default type of the type parameter.
59
+ */
60
+ default?: Children;
61
+
62
+ /**
63
+ * A refkey or array of refkeys for this type parameter.
64
+ */
65
+ refkey?: Refkey | Refkey[];
66
+
67
+ /**
68
+ * Arbitrary metadata for the type parameter symbol.
69
+ */
70
+ metadata?: Record<string, unknown>;
71
+ }