@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,52 @@
1
+ import { Children, OutputSymbolFlags, Refkey } from "@alloy-js/core";
2
+ import { useTSNamePolicy } from "../name-policy.js";
3
+ import { createTSSymbol, TSOutputSymbol } from "../symbols/ts-output-symbol.js";
4
+ import { ValueExpression } from "./ValueExpression.jsx";
5
+
6
+ export interface EnumMemberProps {
7
+ /**
8
+ * The name of the member.
9
+ *
10
+ * If a naming policy is provided in context, the policy name "enum-member" is
11
+ * used.
12
+ */
13
+ name: string;
14
+
15
+ /**
16
+ * Refkey for the enum member symbol. If the refkey is not provided, a symbol
17
+ * is not created and the member cannot be referenced by refkey.
18
+ */
19
+ refkey?: Refkey;
20
+
21
+ /**
22
+ * The value of the enum member.
23
+ */
24
+ value?: Children;
25
+
26
+ /**
27
+ * The JS value of the enum member.
28
+ */
29
+ jsValue?: string | number;
30
+ }
31
+
32
+ /**
33
+ * A TypeScript enum member.
34
+ */
35
+ export function EnumMember(props: EnumMemberProps) {
36
+ const namer = useTSNamePolicy();
37
+ const name = namer.getName(props.name, "enum-member");
38
+ let sym: TSOutputSymbol | undefined = undefined;
39
+ if (props.refkey) {
40
+ sym = createTSSymbol({
41
+ name,
42
+ refkey: props.refkey,
43
+ flags: OutputSymbolFlags.StaticMember,
44
+ });
45
+ }
46
+ const nameCode = sym ? sym.name : name;
47
+ const valueCode = props.jsValue ?
48
+ <ValueExpression jsValue={props.jsValue} />
49
+ : props.value;
50
+
51
+ return <>{nameCode} = {valueCode}</>;
52
+ }
@@ -0,0 +1,21 @@
1
+ import { dirname, relative } from "pathe";
2
+ import { TSModuleScope, TSOutputSymbol } from "../symbols/index.js";
3
+ import { modulePath } from "../utils.js";
4
+ import { useSourceFile } from "./SourceFile.js";
5
+
6
+ export interface ExportStatementProps {
7
+ star?: boolean;
8
+ from?: TSModuleScope;
9
+ }
10
+
11
+ export function ExportStatement(props: ExportStatementProps) {
12
+ if (props.star) {
13
+ const module = useSourceFile();
14
+ for (const symbol of props.from!.symbols.values()) {
15
+ module.scope.exportedSymbols.set(symbol.refkey, symbol as TSOutputSymbol);
16
+ }
17
+ return <>export * from "{modulePath(relative(dirname(module.scope.name), props.from!.name))}";</>;
18
+ }
19
+
20
+ return "";
21
+ }
@@ -0,0 +1,15 @@
1
+ import { Children, mapJoin, Refkey } from "@alloy-js/core";
2
+ import { Reference } from "./Reference.js";
3
+
4
+ export interface FunctionCallExpressionProps {
5
+ refkey: Refkey;
6
+ args?: Children[];
7
+ }
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
+ </>;
15
+ }
@@ -0,0 +1,132 @@
1
+ import {
2
+ childrenArray,
3
+ code,
4
+ findKeyedChild,
5
+ findUnkeyedChildren,
6
+ mapJoin,
7
+ Name,
8
+ Refkey,
9
+ refkey,
10
+ Scope,
11
+ taggedComponent,
12
+ } from "@alloy-js/core";
13
+ import { Children } from "@alloy-js/core/jsx-runtime";
14
+ import { useTSNamePolicy } from "../name-policy.js";
15
+ import { createTSSymbol, TSSymbolFlags, useTSScope } from "../symbols/index.js";
16
+ import { BaseDeclarationProps, Declaration } from "./Declaration.js";
17
+
18
+ export interface ParameterDescriptor {
19
+ type: Children;
20
+ refkey: Refkey;
21
+ optional?: boolean;
22
+ }
23
+
24
+ function isParameterDescriptor(
25
+ value: Children | ParameterDescriptor,
26
+ ): value is ParameterDescriptor {
27
+ return (
28
+ typeof value === "object" && value !== null && Object.hasOwn(value, "type")
29
+ );
30
+ }
31
+ export interface FunctionDeclarationProps extends BaseDeclarationProps {
32
+ async?: boolean;
33
+ parameters?: Record<string, Children | ParameterDescriptor>;
34
+ returnType?: Children;
35
+ children?: Children;
36
+ }
37
+
38
+ export const functionParametersTag = Symbol();
39
+ export const functionBodyTag = Symbol();
40
+
41
+ export function FunctionDeclaration(props: FunctionDeclarationProps) {
42
+ const children = childrenArray(() => props.children);
43
+ const parametersChild = findKeyedChild(children, functionParametersTag);
44
+ const bodyChild = findKeyedChild(children, functionBodyTag);
45
+ const filteredChildren = findUnkeyedChildren(children);
46
+ const returnType = getReturnType(props.returnType, { async: props.async });
47
+ const sReturnType = returnType ? <>: {returnType}</> : undefined;
48
+
49
+ const sParams =
50
+ parametersChild ?? <FunctionDeclaration.Parameters parameters={props.parameters} />;
51
+
52
+ const sBody =
53
+ bodyChild ?? <FunctionDeclaration.Body>{filteredChildren}</FunctionDeclaration.Body>;
54
+
55
+ const asyncKwd = props.async ? "async " : "";
56
+
57
+ return <Declaration {...props} nameKind="function">
58
+ {asyncKwd}function <Name /><Scope name={props.name} kind="function">
59
+ ({sParams}){sReturnType} {"{"}
60
+ {sBody}
61
+ {"}"}
62
+ </Scope>
63
+ </Declaration>;
64
+ }
65
+
66
+ function getReturnType(
67
+ returnType: Children | undefined,
68
+ options: { async?: boolean } = { async: false },
69
+ ) {
70
+ if (returnType) {
71
+ return options.async ? code`Promise<${returnType}>` : returnType;
72
+ }
73
+ }
74
+ export interface FunctionParametersProps {
75
+ parameters?: Record<string, Children | ParameterDescriptor>;
76
+ children?: Children;
77
+ }
78
+
79
+ FunctionDeclaration.Parameters = taggedComponent(
80
+ functionParametersTag,
81
+ function Parameters(props: FunctionParametersProps) {
82
+ const namePolicy = useTSNamePolicy();
83
+
84
+ let value;
85
+
86
+ 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;
118
+ }
119
+
120
+ return value;
121
+ },
122
+ );
123
+
124
+ export interface FunctionBodyProps {
125
+ children?: Children;
126
+ }
127
+
128
+ FunctionDeclaration.Body = taggedComponent(functionBodyTag, function Body(
129
+ props: FunctionBodyProps,
130
+ ) {
131
+ return props.children;
132
+ });
@@ -0,0 +1,131 @@
1
+ import {
2
+ mapJoin,
3
+ memo,
4
+ SourceDirectoryContext,
5
+ useContext,
6
+ } from "@alloy-js/core";
7
+ import { relative } from "pathe";
8
+ import {
9
+ ImportedSymbol,
10
+ ImportRecords,
11
+ TSPackageScope,
12
+ } from "../symbols/index.js";
13
+ import { modulePath } from "../utils.js";
14
+ import { usePackage } from "./PackageDirectory.js";
15
+
16
+ export interface ImportStatementsProps {
17
+ records: ImportRecords;
18
+ }
19
+
20
+ export function ImportStatements(props: ImportStatementsProps) {
21
+ const pkg = usePackage();
22
+
23
+ return memo(() => {
24
+ // todo: may be able to remove this (was just making sure to trigger
25
+ // reactivity based on props.record).
26
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
27
+ props.records.size;
28
+ return mapJoin(props.records, (module, importedSymbols) => {
29
+ let targetPath: string;
30
+
31
+ if (
32
+ (pkg && pkg.scope !== module.parent) ||
33
+ (!pkg && module.parent!.kind !== "global")
34
+ ) {
35
+ // importing from another package, so let's calculate the import.
36
+ const targetPackage = module.parent as TSPackageScope;
37
+ let foundPath: string | false = false;
38
+ for (const [
39
+ publicPath,
40
+ exportedModule,
41
+ ] of targetPackage.exportedSymbols) {
42
+ // a module could be exported from multiple paths, so here
43
+ // would be the place to handle that.
44
+
45
+ if (exportedModule === module) {
46
+ foundPath = publicPath;
47
+ }
48
+ }
49
+
50
+ if (!foundPath) {
51
+ throw new Error("Module not exported from package");
52
+ }
53
+
54
+ targetPath = targetPackage.name + foundPath.slice(1);
55
+ } else {
56
+ // local package import, so need relative import
57
+ const currentDir = useContext(SourceDirectoryContext)!.path;
58
+ // todo: don't allow importing non-exported symbols
59
+ targetPath = modulePath(relative(currentDir, module.name));
60
+ }
61
+
62
+ return <ImportStatement path={targetPath} symbols={importedSymbols} />;
63
+ });
64
+ });
65
+ }
66
+
67
+ export interface ImportStatementProps {
68
+ path: string;
69
+ symbols: Set<ImportedSymbol>;
70
+ }
71
+
72
+ export function ImportStatement(props: ImportStatementProps) {
73
+ return memo(() => {
74
+ let defaultImportSymbol: ImportedSymbol | undefined = undefined;
75
+ const namedImportSymbols: ImportedSymbol[] = [];
76
+
77
+ for (const sym of props.symbols) {
78
+ if (sym.target.default) {
79
+ defaultImportSymbol = sym;
80
+ } else {
81
+ namedImportSymbols.push(sym);
82
+ }
83
+ }
84
+
85
+ const parts: any[] = ["import "];
86
+ if (defaultImportSymbol) {
87
+ parts.push(
88
+ <ImportBinding default importedSymbol={defaultImportSymbol} />,
89
+ );
90
+ if (namedImportSymbols.length > 0) {
91
+ parts.push(", ");
92
+ }
93
+ }
94
+
95
+ if (namedImportSymbols.length > 0) {
96
+ parts.push("{ ");
97
+ parts.push(
98
+ mapJoin(
99
+ namedImportSymbols,
100
+ (nis) => <ImportBinding importedSymbol={nis} />,
101
+ { joiner: ", " },
102
+ ),
103
+ );
104
+ parts.push(" }");
105
+ }
106
+ parts.push(` from "${props.path}";`);
107
+
108
+ return parts;
109
+ });
110
+ }
111
+
112
+ interface ImportBindingProps {
113
+ importedSymbol: ImportedSymbol;
114
+ default?: boolean;
115
+ }
116
+
117
+ function ImportBinding(props: ImportBindingProps) {
118
+ const text = memo(() => {
119
+ const localName = props.importedSymbol.local.name;
120
+ const targetName = props.importedSymbol.target.name;
121
+ if (localName === targetName) {
122
+ return targetName;
123
+ } else if (props.default) {
124
+ return localName;
125
+ } else {
126
+ return `${targetName} as ${localName}`;
127
+ }
128
+ });
129
+
130
+ return text;
131
+ }
@@ -0,0 +1,62 @@
1
+ import { Children, code, Name } from "@alloy-js/core";
2
+ import { useTSNamePolicy } from "../name-policy.js";
3
+ import { BaseDeclarationProps, Declaration } from "./Declaration.js";
4
+
5
+ export interface InterfaceDeclarationProps extends BaseDeclarationProps {
6
+ extends?: Children;
7
+ }
8
+
9
+ /**
10
+ * Create a TypeScript interface declaration.
11
+ *
12
+ * @remarks
13
+ *
14
+ * This component will declare a symbol for this interface. The `export` and
15
+ * `default` boolean props determine whether and how this symbol is exported
16
+ * from the package.
17
+ */
18
+ export function InterfaceDeclaration(props: InterfaceDeclarationProps) {
19
+ const extendsPart = props.extends ? <> extends {props.extends}</> : "";
20
+
21
+ return <Declaration {...props} nameKind="interface">
22
+ interface <Name />{extendsPart} <InterfaceExpression>
23
+ {props.children}
24
+ </InterfaceExpression>
25
+ </Declaration>;
26
+ }
27
+
28
+ export interface InterfaceExpressionProps {
29
+ children?: Children;
30
+ }
31
+
32
+ export function InterfaceExpression(props: InterfaceExpressionProps) {
33
+ return code`
34
+ {
35
+ ${props.children}
36
+ }
37
+ `;
38
+ }
39
+
40
+ export interface InterfaceMemberProps {
41
+ name?: string;
42
+ indexer?: Children;
43
+ type?: Children;
44
+ children?: Children;
45
+ optional?: boolean;
46
+ readonly?: boolean;
47
+ }
48
+
49
+ /**
50
+ * Create a TypeScript interface declaration.
51
+ */
52
+ export function InterfaceMember(props: InterfaceMemberProps) {
53
+ const namer = useTSNamePolicy();
54
+ const type = props.type ?? props.children;
55
+ const optionality = props.optional ? "?" : "";
56
+ const readonly = props.readonly ? "readonly " : "";
57
+ if (props.indexer) {
58
+ return <>[{props.indexer}]: {type}</>;
59
+ } else {
60
+ return <>{readonly}{namer.getName(props.name!, "interface-member")}{optionality}: {type}</>;
61
+ }
62
+ }
@@ -0,0 +1,130 @@
1
+ import {
2
+ AssignmentContext,
3
+ Children,
4
+ computed,
5
+ createAssignmentContext,
6
+ getAssignmentSymbol,
7
+ mapJoin,
8
+ MemberScope,
9
+ memo,
10
+ OutputSymbolFlags,
11
+ Refkey,
12
+ } from "@alloy-js/core";
13
+ import { createTSSymbol } from "../symbols/index.js";
14
+ import { ValueExpression } from "./ValueExpression.js";
15
+
16
+ export interface ObjectExpressionProps {
17
+ children?: Children;
18
+ jsValue?:
19
+ | [string, unknown][]
20
+ | Map<string, unknown>
21
+ | Record<string, unknown>;
22
+ }
23
+
24
+ export function ObjectExpression(props: ObjectExpressionProps) {
25
+ const assignmentSymbol = getAssignmentSymbol();
26
+
27
+ if (assignmentSymbol) {
28
+ assignmentSymbol.binder.addStaticMembersToSymbol(assignmentSymbol);
29
+ }
30
+
31
+ const elements = computed(() => {
32
+ const jsValue = props.jsValue;
33
+ let properties: [string, unknown][];
34
+ if (Array.isArray(jsValue)) {
35
+ properties = jsValue;
36
+ } else if (jsValue instanceof Map) {
37
+ properties = [...jsValue.entries()];
38
+ } else if (jsValue !== undefined) {
39
+ properties = Object.entries(jsValue);
40
+ } else {
41
+ properties = [];
42
+ }
43
+ const elements = mapJoin(
44
+ properties,
45
+ ([name, value]) => {
46
+ return <ObjectProperty name={name} jsValue={value} />;
47
+ },
48
+ { joiner: ",\n" },
49
+ );
50
+
51
+ if (props.children) {
52
+ if (elements.length > 0) {
53
+ elements.push(",");
54
+ }
55
+ elements.push(props.children);
56
+ }
57
+
58
+ return elements;
59
+ });
60
+
61
+ return memo(() => {
62
+ if (elements.value.length === 0) {
63
+ return "{}";
64
+ } else {
65
+ if (assignmentSymbol) {
66
+ return <>
67
+ {"{"}
68
+ <MemberScope owner={assignmentSymbol}>
69
+ {elements.value}
70
+ </MemberScope>
71
+ {"}"}
72
+ </>;
73
+ } else {
74
+ return <>
75
+ {"{"}
76
+ {elements.value}
77
+ {"}"}
78
+ </>;
79
+ }
80
+ }
81
+ });
82
+ }
83
+
84
+ export interface ObjectPropertyProps {
85
+ name?: string;
86
+ nameExpression?: Children;
87
+ value?: Children;
88
+ jsValue?: unknown;
89
+ children?: Children;
90
+ refkey?: Refkey;
91
+ }
92
+
93
+ export function ObjectProperty(props: ObjectPropertyProps) {
94
+ let name;
95
+ if (props.name) {
96
+ name = props.name;
97
+ } else if (props.nameExpression) {
98
+ name = <>[{props.nameExpression}]</>;
99
+ } else {
100
+ throw new Error("ObjectProperty either a name or a nameExpression.");
101
+ }
102
+
103
+ let sym = undefined;
104
+ if (props.refkey && props.name) {
105
+ sym = createTSSymbol({
106
+ name: props.name,
107
+ refkey: props.refkey,
108
+ flags: OutputSymbolFlags.StaticMember,
109
+ });
110
+ }
111
+
112
+ let value;
113
+ if (props.value) {
114
+ value = props.value;
115
+ } else if (Object.prototype.hasOwnProperty.call(props, "jsValue")) {
116
+ // need the hasOwnProperty check because the value might be falsy.
117
+ value = <ValueExpression jsValue={props.jsValue} />;
118
+ } else if (props.children) {
119
+ value = props.children;
120
+ }
121
+
122
+ const assignmentContext: AssignmentContext | undefined = sym ?
123
+ createAssignmentContext(sym)
124
+ : undefined;
125
+ return <>
126
+ {sym ? sym.name : name}: <AssignmentContext.Provider value={assignmentContext}>
127
+ {value}
128
+ </AssignmentContext.Provider>
129
+ </>;
130
+ }
@@ -0,0 +1,104 @@
1
+ import {
2
+ Children,
3
+ ComponentContext,
4
+ createContext,
5
+ Ref,
6
+ Scope,
7
+ shallowRef,
8
+ SourceDirectory,
9
+ SourceDirectoryContext,
10
+ useBinder,
11
+ useContext,
12
+ useScope,
13
+ } from "@alloy-js/core";
14
+ import { join } from "pathe";
15
+ import { createTSPackageScope, TSPackageScope } from "../symbols/index.js";
16
+ import { modulePath } from "../utils.js";
17
+ import {
18
+ ExportPath,
19
+ PackageExports,
20
+ PackageJsonFile,
21
+ PackageJsonFileProps,
22
+ } from "./PackageJson.js";
23
+ import { TSConfigJson } from "./TsConfigJson.js";
24
+
25
+ export interface PackageDirectoryProps extends PackageJsonFileProps {
26
+ dependencies?: Record<string, string>;
27
+ exports?: PackageExports | ExportPath;
28
+ tsConfig?: { outDir?: string };
29
+ children?: Children;
30
+ path?: string;
31
+ }
32
+
33
+ export const PackageContext: ComponentContext<PackageContext> = createContext();
34
+
35
+ export function usePackage() {
36
+ const ctx = useContext(PackageContext);
37
+ return ctx;
38
+ }
39
+
40
+ export interface PackageContext {
41
+ scope: TSPackageScope;
42
+
43
+ /**
44
+ * A ref for a function that maps a ts file to a JS file.
45
+ */
46
+ outFileMapper: Ref<(path: string) => string>;
47
+ }
48
+
49
+ export function PackageDirectory(props: PackageDirectoryProps) {
50
+ const packageContext = createPackageContext(
51
+ props.name,
52
+ props.version,
53
+ props.path ?? ".",
54
+ );
55
+
56
+ if (props.exports) {
57
+ if (typeof props.exports === "string") {
58
+ packageContext.scope.addRawExport(".", props.exports);
59
+ } else {
60
+ for (const ex of Object.entries(props.exports)) {
61
+ packageContext.scope.addRawExport(ex[0], ex[1]);
62
+ }
63
+ }
64
+ }
65
+
66
+ if (props.dependencies) {
67
+ for (const dep of Object.entries(props.dependencies)) {
68
+ packageContext.scope.addRawDependency(dep[0], dep[1]);
69
+ }
70
+ }
71
+
72
+ return <SourceDirectory path={props.path ?? "."}>
73
+ <PackageContext.Provider value={packageContext}>
74
+ <Scope value={packageContext.scope}>
75
+ <PackageJsonFile {...props} />
76
+ <TSConfigJson {... props.tsConfig} />
77
+ {props.children}
78
+ </Scope>
79
+ </PackageContext.Provider>
80
+ </SourceDirectory>;
81
+ }
82
+
83
+ function createPackageContext(
84
+ name: string,
85
+ version: string,
86
+ path: string,
87
+ ): PackageContext {
88
+ const parentDir = useContext(SourceDirectoryContext);
89
+ // todo: this can probably just use context.
90
+ const fullPath = parentDir ? join(parentDir.path, path) : path;
91
+
92
+ const scope = createTSPackageScope(
93
+ useBinder(),
94
+ useScope(),
95
+ name,
96
+ version,
97
+ fullPath,
98
+ );
99
+
100
+ return {
101
+ scope,
102
+ outFileMapper: shallowRef((path: string) => modulePath(path)),
103
+ };
104
+ }