@alloy-js/typescript 0.1.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 (251) hide show
  1. package/LICENSE.txt +7 -0
  2. package/api-extractor.json +4 -0
  3. package/babel.config.cjs +4 -0
  4. package/dist/src/builtins/index.d.ts +2 -0
  5. package/dist/src/builtins/index.d.ts.map +1 -0
  6. package/dist/src/builtins/index.js +1 -0
  7. package/dist/src/builtins/index.js.map +1 -0
  8. package/dist/src/builtins/node.d.ts +183 -0
  9. package/dist/src/builtins/node.d.ts.map +1 -0
  10. package/dist/src/builtins/node.js +39 -0
  11. package/dist/src/builtins/node.js.map +1 -0
  12. package/dist/src/components/ArrayExpression.d.ts +7 -0
  13. package/dist/src/components/ArrayExpression.d.ts.map +1 -0
  14. package/dist/src/components/ArrayExpression.js +37 -0
  15. package/dist/src/components/ArrayExpression.js.map +1 -0
  16. package/dist/src/components/BarrelFile.d.ts +49 -0
  17. package/dist/src/components/BarrelFile.d.ts.map +1 -0
  18. package/dist/src/components/BarrelFile.js +73 -0
  19. package/dist/src/components/BarrelFile.js.map +1 -0
  20. package/dist/src/components/Block.d.ts +6 -0
  21. package/dist/src/components/Block.d.ts.map +1 -0
  22. package/dist/src/components/Block.js +11 -0
  23. package/dist/src/components/Block.js.map +1 -0
  24. package/dist/src/components/ClassDeclaration.d.ts +64 -0
  25. package/dist/src/components/ClassDeclaration.d.ts.map +1 -0
  26. package/dist/src/components/ClassDeclaration.js +122 -0
  27. package/dist/src/components/ClassDeclaration.js.map +1 -0
  28. package/dist/src/components/Declaration.d.ts +53 -0
  29. package/dist/src/components/Declaration.d.ts.map +1 -0
  30. package/dist/src/components/Declaration.js +60 -0
  31. package/dist/src/components/Declaration.js.map +1 -0
  32. package/dist/src/components/EnumDeclaration.d.ts +12 -0
  33. package/dist/src/components/EnumDeclaration.d.ts.map +1 -0
  34. package/dist/src/components/EnumDeclaration.js +48 -0
  35. package/dist/src/components/EnumDeclaration.js.map +1 -0
  36. package/dist/src/components/EnumMember.d.ts +28 -0
  37. package/dist/src/components/EnumMember.d.ts.map +1 -0
  38. package/dist/src/components/EnumMember.js +28 -0
  39. package/dist/src/components/EnumMember.js.map +1 -0
  40. package/dist/src/components/ExportStatement.d.ts +7 -0
  41. package/dist/src/components/ExportStatement.d.ts.map +1 -0
  42. package/dist/src/components/ExportStatement.js +15 -0
  43. package/dist/src/components/ExportStatement.js.map +1 -0
  44. package/dist/src/components/FunctionCallExpression.d.ts +7 -0
  45. package/dist/src/components/FunctionCallExpression.d.ts.map +1 -0
  46. package/dist/src/components/FunctionCallExpression.js +14 -0
  47. package/dist/src/components/FunctionCallExpression.js.map +1 -0
  48. package/dist/src/components/FunctionDeclaration.d.ts +29 -0
  49. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -0
  50. package/dist/src/components/FunctionDeclaration.js +91 -0
  51. package/dist/src/components/FunctionDeclaration.js.map +1 -0
  52. package/dist/src/components/ImportStatement.d.ts +11 -0
  53. package/dist/src/components/ImportStatement.d.ts.map +1 -0
  54. package/dist/src/components/ImportStatement.js +92 -0
  55. package/dist/src/components/ImportStatement.js.map +1 -0
  56. package/dist/src/components/Interface.d.ts +32 -0
  57. package/dist/src/components/Interface.d.ts.map +1 -0
  58. package/dist/src/components/Interface.js +50 -0
  59. package/dist/src/components/Interface.js.map +1 -0
  60. package/dist/src/components/ObjectExpression.d.ts +16 -0
  61. package/dist/src/components/ObjectExpression.d.ts.map +1 -0
  62. package/dist/src/components/ObjectExpression.js +101 -0
  63. package/dist/src/components/ObjectExpression.js.map +1 -0
  64. package/dist/src/components/PackageDirectory.d.ts +23 -0
  65. package/dist/src/components/PackageDirectory.d.ts.map +1 -0
  66. package/dist/src/components/PackageDirectory.js +62 -0
  67. package/dist/src/components/PackageDirectory.js.map +1 -0
  68. package/dist/src/components/PackageJson.d.ts +16 -0
  69. package/dist/src/components/PackageJson.d.ts.map +1 -0
  70. package/dist/src/components/PackageJson.js +36 -0
  71. package/dist/src/components/PackageJson.js.map +1 -0
  72. package/dist/src/components/Reference.d.ts +6 -0
  73. package/dist/src/components/Reference.d.ts.map +1 -0
  74. package/dist/src/components/Reference.js +8 -0
  75. package/dist/src/components/Reference.js.map +1 -0
  76. package/dist/src/components/SourceFile.d.ts +15 -0
  77. package/dist/src/components/SourceFile.d.ts.map +1 -0
  78. package/dist/src/components/SourceFile.js +62 -0
  79. package/dist/src/components/SourceFile.js.map +1 -0
  80. package/dist/src/components/TsConfigJson.d.ts +5 -0
  81. package/dist/src/components/TsConfigJson.d.ts.map +1 -0
  82. package/dist/src/components/TsConfigJson.js +53 -0
  83. package/dist/src/components/TsConfigJson.js.map +1 -0
  84. package/dist/src/components/TypeDeclaration.d.ts +5 -0
  85. package/dist/src/components/TypeDeclaration.d.ts.map +1 -0
  86. package/dist/src/components/TypeDeclaration.js +14 -0
  87. package/dist/src/components/TypeDeclaration.js.map +1 -0
  88. package/dist/src/components/ValueExpression.d.ts +5 -0
  89. package/dist/src/components/ValueExpression.d.ts.map +1 -0
  90. package/dist/src/components/ValueExpression.js +32 -0
  91. package/dist/src/components/ValueExpression.js.map +1 -0
  92. package/dist/src/components/VarDeclaration.d.ts +11 -0
  93. package/dist/src/components/VarDeclaration.d.ts.map +1 -0
  94. package/dist/src/components/VarDeclaration.js +29 -0
  95. package/dist/src/components/VarDeclaration.js.map +1 -0
  96. package/dist/src/components/index.d.ts +22 -0
  97. package/dist/src/components/index.d.ts.map +1 -0
  98. package/dist/src/components/index.js +22 -0
  99. package/dist/src/components/index.js.map +1 -0
  100. package/dist/src/components/stc/index.d.ts +102 -0
  101. package/dist/src/components/stc/index.d.ts.map +1 -0
  102. package/dist/src/components/stc/index.js +28 -0
  103. package/dist/src/components/stc/index.js.map +1 -0
  104. package/dist/src/create-package.d.ts +33 -0
  105. package/dist/src/create-package.d.ts.map +1 -0
  106. package/dist/src/create-package.js +50 -0
  107. package/dist/src/create-package.js.map +1 -0
  108. package/dist/src/index.d.ts +7 -0
  109. package/dist/src/index.d.ts.map +1 -0
  110. package/dist/src/index.js +7 -0
  111. package/dist/src/index.js.map +1 -0
  112. package/dist/src/name-conflict-resolver.d.ts +3 -0
  113. package/dist/src/name-conflict-resolver.d.ts.map +1 -0
  114. package/dist/src/name-conflict-resolver.js +15 -0
  115. package/dist/src/name-conflict-resolver.js.map +1 -0
  116. package/dist/src/name-policy.d.ts +5 -0
  117. package/dist/src/name-policy.d.ts.map +1 -0
  118. package/dist/src/name-policy.js +23 -0
  119. package/dist/src/name-policy.js.map +1 -0
  120. package/dist/src/source-directory-data.d.ts +7 -0
  121. package/dist/src/source-directory-data.d.ts.map +1 -0
  122. package/dist/src/source-directory-data.js +13 -0
  123. package/dist/src/source-directory-data.js.map +1 -0
  124. package/dist/src/symbols/index.d.ts +7 -0
  125. package/dist/src/symbols/index.d.ts.map +1 -0
  126. package/dist/src/symbols/index.js +7 -0
  127. package/dist/src/symbols/index.js.map +1 -0
  128. package/dist/src/symbols/reference.d.ts +3 -0
  129. package/dist/src/symbols/reference.d.ts.map +1 -0
  130. package/dist/src/symbols/reference.js +75 -0
  131. package/dist/src/symbols/reference.js.map +1 -0
  132. package/dist/src/symbols/scopes.d.ts +16 -0
  133. package/dist/src/symbols/scopes.d.ts.map +1 -0
  134. package/dist/src/symbols/scopes.js +5 -0
  135. package/dist/src/symbols/scopes.js.map +1 -0
  136. package/dist/src/symbols/ts-member-scope.d.ts +9 -0
  137. package/dist/src/symbols/ts-member-scope.d.ts.map +1 -0
  138. package/dist/src/symbols/ts-member-scope.js +10 -0
  139. package/dist/src/symbols/ts-member-scope.js.map +1 -0
  140. package/dist/src/symbols/ts-module-scope.d.ts +19 -0
  141. package/dist/src/symbols/ts-module-scope.d.ts.map +1 -0
  142. package/dist/src/symbols/ts-module-scope.js +36 -0
  143. package/dist/src/symbols/ts-module-scope.js.map +1 -0
  144. package/dist/src/symbols/ts-output-symbol.d.ts +28 -0
  145. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -0
  146. package/dist/src/symbols/ts-output-symbol.js +30 -0
  147. package/dist/src/symbols/ts-output-symbol.js.map +1 -0
  148. package/dist/src/symbols/ts-package-scope.d.ts +54 -0
  149. package/dist/src/symbols/ts-package-scope.d.ts.map +1 -0
  150. package/dist/src/symbols/ts-package-scope.js +40 -0
  151. package/dist/src/symbols/ts-package-scope.js.map +1 -0
  152. package/dist/src/utils.d.ts +2 -0
  153. package/dist/src/utils.d.ts.map +1 -0
  154. package/dist/src/utils.js +7 -0
  155. package/dist/src/utils.js.map +1 -0
  156. package/dist/test/array.test.d.ts +2 -0
  157. package/dist/test/array.test.d.ts.map +1 -0
  158. package/dist/test/barrel.test.d.ts +2 -0
  159. package/dist/test/barrel.test.d.ts.map +1 -0
  160. package/dist/test/basic.test.d.ts +2 -0
  161. package/dist/test/basic.test.d.ts.map +1 -0
  162. package/dist/test/class.test.d.ts +2 -0
  163. package/dist/test/class.test.d.ts.map +1 -0
  164. package/dist/test/enum.test.d.ts +2 -0
  165. package/dist/test/enum.test.d.ts.map +1 -0
  166. package/dist/test/externals.test.d.ts +2 -0
  167. package/dist/test/externals.test.d.ts.map +1 -0
  168. package/dist/test/function-call-expression.test.d.ts +2 -0
  169. package/dist/test/function-call-expression.test.d.ts.map +1 -0
  170. package/dist/test/function-declaration.test.d.ts +2 -0
  171. package/dist/test/function-declaration.test.d.ts.map +1 -0
  172. package/dist/test/imports.test.d.ts +2 -0
  173. package/dist/test/imports.test.d.ts.map +1 -0
  174. package/dist/test/interface.test.d.ts +2 -0
  175. package/dist/test/interface.test.d.ts.map +1 -0
  176. package/dist/test/name-policy.test.d.ts +2 -0
  177. package/dist/test/name-policy.test.d.ts.map +1 -0
  178. package/dist/test/package-external.test.d.ts +2 -0
  179. package/dist/test/package-external.test.d.ts.map +1 -0
  180. package/dist/test/package.test.d.ts +2 -0
  181. package/dist/test/package.test.d.ts.map +1 -0
  182. package/dist/test/reference.test.d.ts +2 -0
  183. package/dist/test/reference.test.d.ts.map +1 -0
  184. package/dist/test/utils.d.ts +5 -0
  185. package/dist/test/utils.d.ts.map +1 -0
  186. package/dist/test/values.test.d.ts +2 -0
  187. package/dist/test/values.test.d.ts.map +1 -0
  188. package/dist/test/var-declaration.test.d.ts +2 -0
  189. package/dist/test/var-declaration.test.d.ts.map +1 -0
  190. package/dist/tsconfig.tsbuildinfo +1 -0
  191. package/package.json +49 -0
  192. package/src/builtins/index.ts +1 -0
  193. package/src/builtins/node.ts +219 -0
  194. package/src/components/ArrayExpression.tsx +41 -0
  195. package/src/components/BarrelFile.tsx +96 -0
  196. package/src/components/Block.tsx +12 -0
  197. package/src/components/ClassDeclaration.tsx +145 -0
  198. package/src/components/Declaration.tsx +105 -0
  199. package/src/components/EnumDeclaration.tsx +53 -0
  200. package/src/components/EnumMember.tsx +52 -0
  201. package/src/components/ExportStatement.tsx +21 -0
  202. package/src/components/FunctionCallExpression.tsx +15 -0
  203. package/src/components/FunctionDeclaration.tsx +132 -0
  204. package/src/components/ImportStatement.tsx +131 -0
  205. package/src/components/Interface.tsx +62 -0
  206. package/src/components/ObjectExpression.tsx +130 -0
  207. package/src/components/PackageDirectory.tsx +104 -0
  208. package/src/components/PackageJson.tsx +69 -0
  209. package/src/components/Reference.tsx +12 -0
  210. package/src/components/SourceFile.tsx +71 -0
  211. package/src/components/TsConfigJson.tsx +62 -0
  212. package/src/components/TypeDeclaration.tsx +10 -0
  213. package/src/components/ValueExpression.tsx +32 -0
  214. package/src/components/VarDeclaration.tsx +42 -0
  215. package/src/components/index.ts +21 -0
  216. package/src/components/stc/index.ts +28 -0
  217. package/src/create-package.ts +108 -0
  218. package/src/index.ts +6 -0
  219. package/src/name-conflict-resolver.ts +22 -0
  220. package/src/name-policy.ts +39 -0
  221. package/src/source-directory-data.ts +25 -0
  222. package/src/symbols/index.ts +6 -0
  223. package/src/symbols/reference.ts +120 -0
  224. package/src/symbols/scopes.ts +28 -0
  225. package/src/symbols/ts-member-scope.ts +23 -0
  226. package/src/symbols/ts-module-scope.ts +66 -0
  227. package/src/symbols/ts-output-symbol.ts +63 -0
  228. package/src/symbols/ts-package-scope.ts +112 -0
  229. package/src/utils.ts +7 -0
  230. package/temp/api.json +9866 -0
  231. package/test/array.test.tsx +14 -0
  232. package/test/barrel.test.tsx +46 -0
  233. package/test/basic.test.tsx +78 -0
  234. package/test/class.test.tsx +181 -0
  235. package/test/enum.test.tsx +132 -0
  236. package/test/externals.test.tsx +139 -0
  237. package/test/function-call-expression.test.tsx +49 -0
  238. package/test/function-declaration.test.tsx +201 -0
  239. package/test/imports.test.tsx +213 -0
  240. package/test/interface.test.tsx +124 -0
  241. package/test/name-policy.test.tsx +62 -0
  242. package/test/package-external.test.tsx +75 -0
  243. package/test/package.test.tsx +68 -0
  244. package/test/reference.test.tsx +59 -0
  245. package/test/utils.tsx +59 -0
  246. package/test/values.test.tsx +292 -0
  247. package/test/var-declaration.test.tsx +45 -0
  248. package/test/vitest.d.ts +10 -0
  249. package/tsconfig.json +11 -0
  250. package/tsdoc-metadata.json +11 -0
  251. package/vitest.config.ts +18 -0
@@ -0,0 +1,69 @@
1
+ import { memo, SourceFile } from "@alloy-js/core";
2
+ import { modulePath } from "../utils.js";
3
+ import { usePackage } from "./PackageDirectory.js";
4
+
5
+ export interface PackageJsonFileProps {
6
+ name: string;
7
+ version: string;
8
+ type?: "module" | "commonjs";
9
+ devDependencies?: Record<string, string>;
10
+ scripts?: Record<string, string>;
11
+ }
12
+
13
+ export interface ExportConditions {
14
+ [condition: string]: string | ExportConditions;
15
+ }
16
+
17
+ export type ExportPath = string | ExportConditions;
18
+
19
+ export interface PackageExports {
20
+ [key: string]: ExportPath;
21
+ }
22
+
23
+ export function PackageJsonFile(props: PackageJsonFileProps) {
24
+ const pkg = usePackage();
25
+ if (!pkg) {
26
+ throw new Error(
27
+ "Package json component needs to be inside a PackageDirectory",
28
+ );
29
+ }
30
+
31
+ const jsonContent = memo(() => {
32
+ const pkgJson = {
33
+ name: props.name,
34
+ version: props.version,
35
+ 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
+ },
47
+ scripts: props.scripts,
48
+ exports: undefined as any,
49
+ };
50
+
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
+ }
58
+
59
+ pkgJson.exports = exportsEntries.length === 0 ?
60
+ undefined
61
+ : Object.fromEntries(exportsEntries);
62
+
63
+ return JSON.stringify(pkgJson, null, 2);
64
+ });
65
+
66
+ return <SourceFile path="package.json" filetype="json">
67
+ {jsonContent}
68
+ </SourceFile>;
69
+ }
@@ -0,0 +1,12 @@
1
+ import { Refkey } from "@alloy-js/core";
2
+ import { ref } from "../symbols/index.js";
3
+
4
+ export interface ReferenceProps {
5
+ refkey: Refkey;
6
+ }
7
+
8
+ export function Reference({ refkey }: ReferenceProps) {
9
+ const reference = ref(refkey);
10
+
11
+ return <>{reference}</>;
12
+ }
@@ -0,0 +1,71 @@
1
+ import {
2
+ ComponentContext,
3
+ SourceFile as CoreSourceFile,
4
+ createContext,
5
+ Scope,
6
+ SourceDirectoryContext,
7
+ useBinder,
8
+ useContext,
9
+ useScope,
10
+ } from "@alloy-js/core";
11
+
12
+ import { Children } from "@alloy-js/core/jsx-runtime";
13
+ import { join } from "pathe";
14
+ import { getSourceDirectoryData } from "../source-directory-data.js";
15
+ import { createTSModuleScope, TSModuleScope } from "../symbols/index.js";
16
+ import { ImportStatements } from "./ImportStatement.js";
17
+ import { PackageContext } from "./PackageDirectory.js";
18
+ import { Reference } from "./Reference.js";
19
+ export interface SourceFileContext {
20
+ scope: TSModuleScope;
21
+ }
22
+
23
+ export const SourceFileContext: ComponentContext<SourceFileContext> =
24
+ createContext();
25
+
26
+ export function useSourceFile() {
27
+ return useContext(SourceFileContext)!;
28
+ }
29
+ export interface SourceFileProps {
30
+ path: string;
31
+ children?: Children;
32
+ export?: boolean | string;
33
+ }
34
+
35
+ export function SourceFile(props: SourceFileProps) {
36
+ const directoryContext = useContext(SourceDirectoryContext)!;
37
+ const sdData = getSourceDirectoryData(directoryContext);
38
+ const currentDir = directoryContext.path;
39
+ const path: string = join(currentDir, props.path);
40
+ const scope = createTSModuleScope(useBinder(), useScope(), path);
41
+ sdData.modules.add(scope);
42
+ const pkg = useContext(PackageContext);
43
+ if (pkg) {
44
+ pkg.scope.addModule(scope);
45
+ }
46
+
47
+ const sfContext: SourceFileContext = {
48
+ scope,
49
+ };
50
+
51
+ if (props.export) {
52
+ if (pkg) {
53
+ if (typeof props.export === "boolean") {
54
+ pkg.scope.addExport(path, scope);
55
+ } else {
56
+ pkg.scope.addExport(props.export, scope);
57
+ }
58
+ }
59
+ }
60
+
61
+ return <CoreSourceFile path={props.path} filetype="typescript" reference={Reference}>
62
+ {scope.importedModules.size > 0 ? (
63
+ <>
64
+ <ImportStatements records={scope.importedModules} />
65
+ {"\n"}
66
+ </>
67
+ ) : undefined}<SourceFileContext.Provider value={sfContext}>
68
+ <Scope value={scope}>{props.children}</Scope>
69
+ </SourceFileContext.Provider>
70
+ </CoreSourceFile>;
71
+ }
@@ -0,0 +1,62 @@
1
+ import { effect, memo, SourceFile } from "@alloy-js/core";
2
+ import { dirname, join, relative, sep } from "pathe";
3
+ import { usePackage } from "./PackageDirectory.js";
4
+
5
+ export interface TSConfigJsonProps {
6
+ outDir?: string;
7
+ }
8
+
9
+ export function TSConfigJson(props: TSConfigJsonProps) {
10
+ const pkg = usePackage();
11
+ if (!pkg) {
12
+ throw new Error(
13
+ "TSConfigJson component needs to be inside a PackageDirectory",
14
+ );
15
+ }
16
+ const outDir = props.outDir ?? "dist";
17
+ const includeDirs = memo(() => {
18
+ const rootDirs = new Set<string>();
19
+ for (const mod of pkg.scope.modules) {
20
+ const rootDir = dirname(mod.name).split(sep)[0];
21
+ rootDirs.add(rootDir);
22
+ }
23
+
24
+ if (rootDirs.has(".")) {
25
+ return ["."];
26
+ } else {
27
+ return Array.from(rootDirs);
28
+ }
29
+ });
30
+
31
+ effect(() => {
32
+ const dirs = includeDirs();
33
+ if (dirs.length === 1) {
34
+ pkg.outFileMapper.value = (path: string) =>
35
+ join(outDir, relative(dirs[0], path));
36
+ } else {
37
+ pkg.outFileMapper.value = (path: string) => join(outDir, path);
38
+ }
39
+ });
40
+
41
+ const jsonContent = memo(() => {
42
+ const jsonContent = {
43
+ compilerOptions: {
44
+ target: "esnext",
45
+ module: "nodenext",
46
+ strict: true,
47
+ declaration: true,
48
+ sourceMap: true,
49
+ declarationMap: true,
50
+ outDir: "dist",
51
+ },
52
+ include: includeDirs().map((d) => `${d}/**/*.ts`),
53
+ exclude: ["node_modules", "dist"],
54
+ };
55
+
56
+ return JSON.stringify(jsonContent, null, 2);
57
+ });
58
+
59
+ return <SourceFile path="tsconfig.json" filetype="json">
60
+ {jsonContent}
61
+ </SourceFile>;
62
+ }
@@ -0,0 +1,10 @@
1
+ import { Name } from "@alloy-js/core";
2
+ import { BaseDeclarationProps, Declaration } from "./Declaration.js";
3
+
4
+ export interface TypeDeclarationProps extends BaseDeclarationProps {}
5
+
6
+ export function TypeDeclaration(props: TypeDeclarationProps) {
7
+ return <Declaration {...props} nameKind="type">
8
+ type <Name /> = {props.children};
9
+ </Declaration>;
10
+ }
@@ -0,0 +1,32 @@
1
+ import { memo } from "@alloy-js/core";
2
+ import { ArrayExpression } from "./ArrayExpression.js";
3
+ import { ObjectExpression } from "./ObjectExpression.js";
4
+
5
+ export interface ValueExpressionProps {
6
+ jsValue?: unknown;
7
+ }
8
+
9
+ export function ValueExpression(props: ValueExpressionProps) {
10
+ return memo(() => {
11
+ const jsValue = props.jsValue;
12
+
13
+ if (typeof jsValue === "undefined") {
14
+ return "undefined";
15
+ } else if (typeof jsValue === "number" || typeof jsValue === "boolean") {
16
+ return String(jsValue);
17
+ } else if (typeof jsValue === "string") {
18
+ return `"${jsValue}"`;
19
+ } else if (typeof jsValue === "object") {
20
+ if (jsValue === null) {
21
+ return "null";
22
+ } else if (Array.isArray(jsValue)) {
23
+ return <ArrayExpression jsValue={jsValue as unknown[]} />;
24
+ } else {
25
+ return <ObjectExpression jsValue={jsValue as Record<string, unknown>} />;
26
+ }
27
+ } else if (typeof jsValue === "function") {
28
+ // functions are inserted as-is.
29
+ return jsValue;
30
+ }
31
+ });
32
+ }
@@ -0,0 +1,42 @@
1
+ import {
2
+ AssignmentContext,
3
+ Children,
4
+ Declaration as CoreDeclaration,
5
+ createAssignmentContext,
6
+ Name,
7
+ refkey,
8
+ } from "@alloy-js/core";
9
+ import { useTSNamePolicy } from "../name-policy.js";
10
+ import { createTSSymbol } from "../symbols/index.js";
11
+ import { BaseDeclarationProps } from "./Declaration.js";
12
+
13
+ export interface VarDeclarationProps extends BaseDeclarationProps {
14
+ const?: boolean;
15
+ let?: boolean;
16
+ var?: boolean;
17
+ value?: Children;
18
+ type?: Children;
19
+ }
20
+
21
+ export function VarDeclaration(props: VarDeclarationProps) {
22
+ const keyword =
23
+ props.var ? "var"
24
+ : props.let ? "let"
25
+ : "const";
26
+ const type = props.type ? <>: {props.type}</> : undefined;
27
+ const name = useTSNamePolicy().getName(props.name, "variable");
28
+ const sym = createTSSymbol({
29
+ name: name,
30
+ refkey: props.refkey ?? refkey(name),
31
+ default: props.default,
32
+ export: props.export,
33
+ });
34
+
35
+ const assignmentContext = createAssignmentContext(sym);
36
+
37
+ return <CoreDeclaration symbol={sym}>
38
+ {props.export ? "export " : ""}{props.default ? "default " : ""}{keyword} <Name />{type} = <AssignmentContext.Provider value={assignmentContext}>
39
+ {props.value ?? props.children};
40
+ </AssignmentContext.Provider>
41
+ </CoreDeclaration>;
42
+ }
@@ -0,0 +1,21 @@
1
+ export * from "./ArrayExpression.js";
2
+ export * from "./BarrelFile.js";
3
+ export * from "./Block.js";
4
+ export * from "./ClassDeclaration.jsx";
5
+ export * from "./Declaration.js";
6
+ export * from "./EnumDeclaration.js";
7
+ export * from "./EnumMember.js";
8
+ export * from "./ExportStatement.js";
9
+ export * from "./FunctionCallExpression.js";
10
+ export * from "./FunctionDeclaration.js";
11
+ export * from "./ImportStatement.js";
12
+ export * from "./Interface.js";
13
+ export * from "./ObjectExpression.js";
14
+ export * from "./PackageDirectory.js";
15
+ export * from "./PackageJson.js";
16
+ export * from "./Reference.js";
17
+ export * from "./SourceFile.js";
18
+ export * from "./TsConfigJson.js";
19
+ export * from "./TypeDeclaration.js";
20
+ export * from "./ValueExpression.js";
21
+ export * from "./VarDeclaration.js";
@@ -0,0 +1,28 @@
1
+ import { stc } from "@alloy-js/core";
2
+ import * as base from "../index.js";
3
+
4
+ export const ArrayExpression = stc(base.ArrayExpression);
5
+ export const BarrelFile = stc(base.BarrelFile);
6
+ export const Block = stc(base.Block);
7
+ export const Declaration = stc(base.Declaration);
8
+ export const EnumDeclaration = stc(base.EnumDeclaration);
9
+ export const EnumMember = stc(base.EnumMember);
10
+ export const ExportStatement = stc(base.ExportStatement);
11
+ export const FunctionDeclaration = stc(base.FunctionDeclaration);
12
+ export const ImportStatement = stc(base.ImportStatement);
13
+ export const ImportStatements = stc(base.ImportStatements);
14
+ export const InterfaceDeclaration = stc(base.InterfaceDeclaration);
15
+ export const InterfaceExpression = stc(base.InterfaceExpression);
16
+ export const InterfaceMember = stc(base.InterfaceMember);
17
+ export const ObjectExpression = stc(base.ObjectExpression);
18
+ export const ObjectProperty = stc(base.ObjectProperty);
19
+ export const PackageDirectory = stc(base.PackageDirectory);
20
+ export const PackageJsonFile = stc(base.PackageJsonFile);
21
+ export const Reference = stc(base.Reference);
22
+ export const SourceFile = stc(base.SourceFile);
23
+ export const TSConfigJson = stc(base.TSConfigJson);
24
+ export const TypeDeclaration = stc(base.TypeDeclaration);
25
+ export const ValueExpression = stc(base.ValueExpression);
26
+ export const VarDeclaration = stc(base.VarDeclaration);
27
+ export const ClassDeclaration = stc(base.ClassDeclaration);
28
+ export const ClassField = stc(base.ClassField);
@@ -0,0 +1,108 @@
1
+ import {
2
+ Binder,
3
+ getSymbolCreatorSymbol,
4
+ Refkey,
5
+ refkey,
6
+ SymbolCreator,
7
+ } from "@alloy-js/core";
8
+
9
+ import {
10
+ createTSModuleScope,
11
+ createTSPackageScope,
12
+ createTSSymbol,
13
+ } from "./symbols/index.js";
14
+
15
+ export interface PackageDescriptor {
16
+ [path: string]: ModuleSymbolsDescriptor;
17
+ }
18
+
19
+ export interface ModuleSymbolsDescriptor {
20
+ default?: string;
21
+ named?: readonly string[];
22
+ }
23
+
24
+ function createSymbols(
25
+ binder: Binder,
26
+ props: CreatePackageProps<PackageDescriptor>,
27
+ refkeys: Record<string, any>,
28
+ ) {
29
+ const pkgScope = createTSPackageScope(
30
+ binder,
31
+ undefined,
32
+ props.name,
33
+ props.version,
34
+ `node_modules/${props.name}`,
35
+ props.builtin,
36
+ );
37
+ for (const [path, symbols] of Object.entries(props.descriptor)) {
38
+ const keys = path === "." ? refkeys : refkeys[path];
39
+ const moduleScope = createTSModuleScope(binder, pkgScope, path);
40
+
41
+ pkgScope.exportedSymbols.set(path, moduleScope);
42
+
43
+ if (symbols.default) {
44
+ const key = keys.default;
45
+ const sym = createTSSymbol({
46
+ name: symbols.default,
47
+ scope: moduleScope,
48
+ refkey: key,
49
+ export: true,
50
+ default: true,
51
+ });
52
+
53
+ moduleScope.exportedSymbols.set(key, sym);
54
+ }
55
+
56
+ for (const exportedName of symbols.named ?? []) {
57
+ const key = keys[exportedName];
58
+ const sym = createTSSymbol({
59
+ name: exportedName,
60
+ scope: moduleScope,
61
+ refkey: key,
62
+ export: true,
63
+ default: false,
64
+ });
65
+ moduleScope.exportedSymbols.set(key, sym);
66
+ }
67
+ }
68
+ }
69
+
70
+ export type PackageRefkeys<T extends PackageDescriptor> = {
71
+ [K in keyof T as K extends "." ? never : K]: {
72
+ [N in T[K]["named"] extends readonly string[] ? T[K]["named"][number]
73
+ : never]: Refkey;
74
+ } & (T[K] extends { default: string } ? { default: Refkey } : {});
75
+ } & (T["."] extends { default: string } ? { default: Refkey } : {}) &
76
+ (T["."] extends { named: readonly string[] } ?
77
+ { [N in T["."]["named"][number]]: Refkey }
78
+ : {});
79
+
80
+ export interface CreatePackageProps<T extends PackageDescriptor> {
81
+ name: string;
82
+ version: string;
83
+ descriptor: T;
84
+ builtin?: boolean;
85
+ }
86
+ export function createPackage<const T extends PackageDescriptor>(
87
+ props: CreatePackageProps<T>,
88
+ ): PackageRefkeys<T> & SymbolCreator {
89
+ const refkeys: any = {
90
+ [getSymbolCreatorSymbol()](binder: Binder) {
91
+ createSymbols(binder, props, refkeys);
92
+ },
93
+ };
94
+
95
+ for (const [path, symbols] of Object.entries(props.descriptor)) {
96
+ const keys = path === "." ? refkeys : ((refkeys[path] = {}), refkeys[path]);
97
+
98
+ if (symbols.default) {
99
+ keys.default = refkey(props.descriptor, path, "default");
100
+ }
101
+
102
+ for (const named of symbols.named ?? []) {
103
+ keys[named] = refkey(props.descriptor, path, named);
104
+ }
105
+ }
106
+
107
+ return refkeys;
108
+ }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from "./builtins/index.js";
2
+ export * from "./components/index.js";
3
+ export * from "./create-package.js";
4
+ export * from "./name-conflict-resolver.js";
5
+ export * from "./name-policy.js";
6
+ export * from "./symbols/index.js";
@@ -0,0 +1,22 @@
1
+ import { TSOutputSymbol, TSSymbolFlags } from "./symbols/index.js";
2
+
3
+ export function tsNameConflictResolver(
4
+ name: string,
5
+ symbols: TSOutputSymbol[],
6
+ ) {
7
+ const goodNamedSymbols = symbols.filter(
8
+ (s) => ~s.tsFlags & TSSymbolFlags.LocalImportSymbol,
9
+ );
10
+ const badNamedSymbols = symbols.filter(
11
+ (s) => s.tsFlags & TSSymbolFlags.LocalImportSymbol,
12
+ );
13
+ let nameCount = 1;
14
+ if (goodNamedSymbols.length > 1) {
15
+ for (const sym of goodNamedSymbols.slice(1)) {
16
+ sym.name = name + "_" + nameCount++;
17
+ }
18
+ }
19
+ for (const sym of badNamedSymbols) {
20
+ sym.name = name + "_" + nameCount++;
21
+ }
22
+ }
@@ -0,0 +1,39 @@
1
+ import { createNamePolicy, NamePolicy, useNamePolicy } from "@alloy-js/core";
2
+ import { camelCase, pascalCase } from "change-case";
3
+
4
+ export type TypeScriptElements =
5
+ | "function"
6
+ | "parameter"
7
+ | "class"
8
+ | "variable"
9
+ | "enum"
10
+ | "object-member-data"
11
+ | "object-member-getter"
12
+ | "class-member-data"
13
+ | "class-member-getter"
14
+ | "enum-member"
15
+ | "interface"
16
+ | "interface-member"
17
+ | "type";
18
+
19
+ export function createTSNamePolicy(): NamePolicy<TypeScriptElements> {
20
+ const caseOptions = {
21
+ prefixCharacters: "$_",
22
+ suffixCharacters: "$_",
23
+ };
24
+ return createNamePolicy((name, element) => {
25
+ switch (element) {
26
+ case "class":
27
+ case "type":
28
+ case "interface":
29
+ case "enum":
30
+ return pascalCase(name, caseOptions);
31
+ default:
32
+ return camelCase(name, caseOptions);
33
+ }
34
+ });
35
+ }
36
+
37
+ export function useTSNamePolicy(): NamePolicy<TypeScriptElements> {
38
+ return useNamePolicy();
39
+ }
@@ -0,0 +1,25 @@
1
+ import { reactive, SourceDirectoryContext } from "@alloy-js/core";
2
+ import { TSModuleScope } from "./symbols/index.js";
3
+
4
+ export interface SourceDirectoryData {
5
+ modules: Set<TSModuleScope>;
6
+ }
7
+
8
+ const sourceDirectoryData = new WeakMap<
9
+ SourceDirectoryContext,
10
+ SourceDirectoryData
11
+ >();
12
+
13
+ export function getSourceDirectoryData(sdContext: SourceDirectoryContext) {
14
+ if (sourceDirectoryData.has(sdContext)) {
15
+ return sourceDirectoryData.get(sdContext)!;
16
+ }
17
+
18
+ const sdData: SourceDirectoryData = {
19
+ modules: reactive(new Set()),
20
+ };
21
+
22
+ sourceDirectoryData.set(sdContext, sdData);
23
+
24
+ return sdData;
25
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./reference.js";
2
+ export * from "./scopes.js";
3
+ export * from "./ts-member-scope.js";
4
+ export * from "./ts-module-scope.js";
5
+ export * from "./ts-output-symbol.js";
6
+ export * from "./ts-package-scope.js";
@@ -0,0 +1,120 @@
1
+ import {
2
+ memo,
3
+ OutputSymbolFlags,
4
+ Refkey,
5
+ resolve,
6
+ untrack,
7
+ useContext,
8
+ } from "@alloy-js/core";
9
+ import { usePackage } from "../components/PackageDirectory.jsx";
10
+ import { SourceFileContext } from "../components/SourceFile.jsx";
11
+ import { TSOutputScope } from "./scopes.js";
12
+ import { TSMemberScope } from "./ts-member-scope.js";
13
+ import { TSModuleScope } from "./ts-module-scope.js";
14
+ import { TSOutputSymbol } from "./ts-output-symbol.js";
15
+ import { TSPackageScope } from "./ts-package-scope.js";
16
+
17
+ export function ref(refkey: Refkey): () => string {
18
+ const sourceFile = useContext(SourceFileContext);
19
+ const resolveResult = resolve<TSOutputScope, TSOutputSymbol>(
20
+ refkey as Refkey,
21
+ );
22
+
23
+ return memo(() => {
24
+ if (resolveResult.value === undefined) {
25
+ return "<Unresolved Symbol>";
26
+ }
27
+
28
+ const { targetDeclaration, pathDown, memberPath } = resolveResult.value;
29
+
30
+ validateSymbolReachable(pathDown);
31
+
32
+ // Where the target declaration is relative to the referencing scope.
33
+ // * package: target symbol is in a different package
34
+ // * module: target symbol is in a different module
35
+ // * local: target symbol is within the current module
36
+ const targetLocation = pathDown[0]?.kind ?? "local";
37
+ let localSymbol: TSOutputSymbol | undefined;
38
+
39
+ if (targetLocation === "package") {
40
+ // need package import
41
+ const pkg = usePackage();
42
+ const sourcePackage = pathDown[0] as TSPackageScope;
43
+
44
+ if (pkg && !sourcePackage.builtin) {
45
+ pkg.scope.addDependency(sourcePackage);
46
+ }
47
+ const symbolPath = [
48
+ ...(pathDown.slice(2) as TSMemberScope[]).map((s) => s.owner),
49
+ targetDeclaration,
50
+ ];
51
+
52
+ const importSymbol = symbolPath[0];
53
+
54
+ // find public dependency
55
+ for (const module of sourcePackage.exportedSymbols.values()) {
56
+ if (module.exportedSymbols.has(importSymbol.refkey)) {
57
+ localSymbol = untrack(() =>
58
+ sourceFile!.scope.addImport(importSymbol, module),
59
+ );
60
+ }
61
+ }
62
+
63
+ if (!localSymbol) {
64
+ throw new Error(
65
+ "The symbol " +
66
+ targetDeclaration.name +
67
+ " is not exported from package",
68
+ );
69
+ }
70
+ } else if (targetLocation === "module") {
71
+ const symbolPath = [
72
+ ...(pathDown.slice(1) as TSMemberScope[]).map((s) => s.owner),
73
+ targetDeclaration,
74
+ ];
75
+
76
+ const importSymbol = symbolPath[0];
77
+ localSymbol = untrack(() =>
78
+ sourceFile!.scope.addImport(importSymbol, pathDown[0] as TSModuleScope),
79
+ );
80
+ }
81
+
82
+ if (memberPath && memberPath.length > 0) {
83
+ if (localSymbol) {
84
+ memberPath[0] = localSymbol;
85
+ }
86
+
87
+ return addThisPrefix(
88
+ targetDeclaration,
89
+ buildMemberExpression(memberPath),
90
+ );
91
+ } else {
92
+ return addThisPrefix(
93
+ localSymbol ?? targetDeclaration,
94
+ localSymbol ? localSymbol.name : targetDeclaration.name,
95
+ );
96
+ }
97
+ });
98
+ }
99
+
100
+ function addThisPrefix(rootSymbol: TSOutputSymbol, name: string) {
101
+ if (rootSymbol.flags & OutputSymbolFlags.InstanceMember) {
102
+ return `this.${name}`;
103
+ } else {
104
+ return name;
105
+ }
106
+ }
107
+
108
+ function buildMemberExpression(symbolPath: TSOutputSymbol[]) {
109
+ return symbolPath.map((sym) => sym.name).join(".");
110
+ }
111
+
112
+ function validateSymbolReachable(path: TSOutputScope[]) {
113
+ for (const scope of path) {
114
+ if (scope.kind === "function") {
115
+ throw new Error(
116
+ "Cannot reference a symbol inside a function from outside a function",
117
+ );
118
+ }
119
+ }
120
+ }