@alloy-js/typescript 0.10.0 → 0.12.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 (244) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/src/builtins/node.js +1 -2
  3. package/dist/src/components/ArrayExpression.js +2 -5
  4. package/dist/src/components/ArrowFunction.d.ts +40 -0
  5. package/dist/src/components/ArrowFunction.d.ts.map +1 -0
  6. package/dist/src/components/ArrowFunction.js +67 -0
  7. package/dist/src/components/BarrelFile.js +1 -2
  8. package/dist/src/components/BlockScope.js +2 -4
  9. package/dist/src/components/CallSignature.js +2 -4
  10. package/dist/src/components/ClassDeclaration.d.ts +2 -0
  11. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  12. package/dist/src/components/ClassDeclaration.js +16 -8
  13. package/dist/src/components/CommaList.js +2 -5
  14. package/dist/src/components/Declaration.js +3 -5
  15. package/dist/src/components/EnumDeclaration.js +2 -5
  16. package/dist/src/components/EnumMember.js +2 -4
  17. package/dist/src/components/ExportStatement.js +1 -2
  18. package/dist/src/components/FunctionBase.d.ts +60 -0
  19. package/dist/src/components/FunctionBase.d.ts.map +1 -0
  20. package/dist/src/components/FunctionBase.js +217 -0
  21. package/dist/src/components/FunctionCallExpression.js +2 -5
  22. package/dist/src/components/FunctionDeclaration.d.ts +3 -18
  23. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  24. package/dist/src/components/FunctionDeclaration.js +9 -157
  25. package/dist/src/components/FunctionExpression.d.ts +39 -0
  26. package/dist/src/components/FunctionExpression.d.ts.map +1 -0
  27. package/dist/src/components/FunctionExpression.js +66 -0
  28. package/dist/src/components/FunctionType.d.ts +38 -0
  29. package/dist/src/components/FunctionType.d.ts.map +1 -0
  30. package/dist/src/components/FunctionType.js +77 -0
  31. package/dist/src/components/IfStatement.js +2 -4
  32. package/dist/src/components/ImportStatement.d.ts.map +1 -1
  33. package/dist/src/components/ImportStatement.js +10 -4
  34. package/dist/src/components/Interface.d.ts +3 -2
  35. package/dist/src/components/Interface.d.ts.map +1 -1
  36. package/dist/src/components/Interface.js +40 -15
  37. package/dist/src/components/InterfaceMethod.d.ts +43 -0
  38. package/dist/src/components/InterfaceMethod.d.ts.map +1 -0
  39. package/dist/src/components/InterfaceMethod.js +87 -0
  40. package/dist/src/components/JSDoc.js +1 -2
  41. package/dist/src/components/JSDocComment.js +3 -5
  42. package/dist/src/components/JSDocExample.js +2 -5
  43. package/dist/src/components/JSDocParam.js +2 -5
  44. package/dist/src/components/MemberChainExpression.js +2 -4
  45. package/dist/src/components/MemberDeclaration.d.ts +34 -0
  46. package/dist/src/components/MemberDeclaration.d.ts.map +1 -0
  47. package/dist/src/components/MemberDeclaration.js +29 -0
  48. package/dist/src/components/MemberExpression.d.ts.map +1 -1
  49. package/dist/src/components/MemberExpression.js +5 -7
  50. package/dist/src/components/ObjectExpression.js +2 -5
  51. package/dist/src/components/PackageDirectory.js +2 -5
  52. package/dist/src/components/PackageJson.js +1 -2
  53. package/dist/src/components/PropertyName.js +1 -2
  54. package/dist/src/components/Reference.d.ts +8 -1
  55. package/dist/src/components/Reference.d.ts.map +1 -1
  56. package/dist/src/components/Reference.js +8 -4
  57. package/dist/src/components/SingleLineCommentBlock.js +2 -4
  58. package/dist/src/components/SourceFile.js +2 -4
  59. package/dist/src/components/SwitchStatement.js +2 -5
  60. package/dist/src/components/TsConfigJson.js +1 -2
  61. package/dist/src/components/TypeDeclaration.d.ts +1 -1
  62. package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
  63. package/dist/src/components/TypeDeclaration.js +4 -7
  64. package/dist/src/components/TypeRefContext.d.ts +22 -0
  65. package/dist/src/components/TypeRefContext.d.ts.map +1 -0
  66. package/dist/src/components/TypeRefContext.js +41 -0
  67. package/dist/src/components/ValueExpression.js +1 -2
  68. package/dist/src/components/VarDeclaration.d.ts +1 -0
  69. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  70. package/dist/src/components/VarDeclaration.js +11 -8
  71. package/dist/src/components/index.d.ts +7 -0
  72. package/dist/src/components/index.d.ts.map +1 -1
  73. package/dist/src/components/index.js +7 -1
  74. package/dist/src/components/stc/index.d.ts +8 -0
  75. package/dist/src/components/stc/index.d.ts.map +1 -1
  76. package/dist/src/components/stc/index.js +9 -2
  77. package/dist/src/context/private-scope.js +1 -2
  78. package/dist/src/context/type-ref-context.d.ts +15 -0
  79. package/dist/src/context/type-ref-context.d.ts.map +1 -0
  80. package/dist/src/context/type-ref-context.js +12 -0
  81. package/dist/src/create-package.js +1 -2
  82. package/dist/src/index.js +1 -2
  83. package/dist/src/name-conflict-resolver.js +1 -2
  84. package/dist/src/name-policy.js +1 -2
  85. package/dist/src/parameter-descriptor.d.ts +33 -17
  86. package/dist/src/parameter-descriptor.d.ts.map +1 -1
  87. package/dist/src/source-directory-data.js +1 -2
  88. package/dist/src/symbols/index.js +1 -2
  89. package/dist/src/symbols/reference.d.ts +4 -1
  90. package/dist/src/symbols/reference.d.ts.map +1 -1
  91. package/dist/src/symbols/reference.js +13 -8
  92. package/dist/src/symbols/scopes.js +1 -2
  93. package/dist/src/symbols/ts-member-scope.js +1 -2
  94. package/dist/src/symbols/ts-module-scope.d.ts +4 -1
  95. package/dist/src/symbols/ts-module-scope.d.ts.map +1 -1
  96. package/dist/src/symbols/ts-module-scope.js +11 -5
  97. package/dist/src/symbols/ts-output-symbol.d.ts +2 -1
  98. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  99. package/dist/src/symbols/ts-output-symbol.js +2 -2
  100. package/dist/src/symbols/ts-package-scope.js +1 -2
  101. package/dist/src/utils.js +1 -2
  102. package/dist/test/array.test.js +105 -0
  103. package/dist/test/arrow-function.test.d.ts +2 -0
  104. package/dist/test/arrow-function.test.d.ts.map +1 -0
  105. package/dist/test/arrow-function.test.js +194 -0
  106. package/dist/test/barrel.test.js +53 -0
  107. package/dist/test/basic.test.js +91 -0
  108. package/dist/test/block-scope.test.js +30 -0
  109. package/dist/test/class.test.js +438 -0
  110. package/dist/test/enum-jsdoc.test.js +88 -0
  111. package/dist/test/enum.test.js +247 -0
  112. package/dist/test/externals.test.js +145 -0
  113. package/dist/test/function-call-expression.test.js +87 -0
  114. package/dist/test/function-declaration-jsdoc.test.js +91 -0
  115. package/dist/test/function-declaration.test.js +228 -0
  116. package/dist/test/function-expression.test.d.ts +2 -0
  117. package/dist/test/function-expression.test.d.ts.map +1 -0
  118. package/dist/test/function-expression.test.js +194 -0
  119. package/dist/test/function-type.test.d.ts +2 -0
  120. package/dist/test/function-type.test.d.ts.map +1 -0
  121. package/dist/test/function-type.test.js +122 -0
  122. package/dist/test/if-statement.test.js +25 -0
  123. package/dist/test/imports.test.d.ts.map +1 -1
  124. package/dist/test/imports.test.js +640 -0
  125. package/dist/test/interface-jsdoc.test.js +178 -0
  126. package/dist/test/interface.test.d.ts.map +1 -1
  127. package/dist/test/interface.test.js +490 -0
  128. package/dist/test/jscomment.test.js +70 -0
  129. package/dist/test/jsdoc.test.js +134 -0
  130. package/dist/test/jsdocparam.test.js +164 -0
  131. package/dist/test/member-chain.test.js +141 -0
  132. package/dist/test/member-expression.test.js +575 -0
  133. package/dist/test/name-conflict.test.js +108 -0
  134. package/dist/test/name-policy.test.js +122 -0
  135. package/dist/test/package-external.test.js +196 -0
  136. package/dist/test/package-json.test.js +93 -0
  137. package/dist/test/package.test.js +136 -0
  138. package/dist/test/reference.test.js +81 -0
  139. package/dist/test/source-file.test.js +36 -0
  140. package/dist/test/switch-statement.test.js +61 -0
  141. package/dist/test/utils.js +45 -0
  142. package/dist/test/value-expression.test.js +30 -0
  143. package/dist/test/values.test.js +351 -0
  144. package/dist/test/var-declaration-jsdoc.test.js +50 -0
  145. package/dist/test/var-declaration.test.js +63 -0
  146. package/dist/test/vitest.d.js +1 -0
  147. package/dist/tsconfig.tsbuildinfo +1 -1
  148. package/package.json +13 -18
  149. package/src/components/ArrowFunction.tsx +87 -0
  150. package/src/components/ClassDeclaration.tsx +19 -3
  151. package/src/components/Declaration.tsx +1 -1
  152. package/src/components/FunctionBase.tsx +282 -0
  153. package/src/components/FunctionDeclaration.tsx +13 -195
  154. package/src/components/FunctionExpression.tsx +86 -0
  155. package/src/components/FunctionType.tsx +93 -0
  156. package/src/components/ImportStatement.tsx +30 -3
  157. package/src/components/Interface.tsx +58 -23
  158. package/src/components/InterfaceMethod.tsx +96 -0
  159. package/src/components/JSDocComment.tsx +1 -1
  160. package/src/components/MemberDeclaration.tsx +76 -0
  161. package/src/components/MemberExpression.tsx +7 -2
  162. package/src/components/Reference.tsx +12 -2
  163. package/src/components/TypeDeclaration.tsx +16 -13
  164. package/src/components/TypeRefContext.tsx +49 -0
  165. package/src/components/VarDeclaration.tsx +6 -2
  166. package/src/components/index.ts +11 -0
  167. package/src/components/stc/index.ts +8 -0
  168. package/src/context/type-ref-context.tsx +19 -0
  169. package/src/parameter-descriptor.ts +35 -17
  170. package/src/symbols/reference.ts +21 -6
  171. package/src/symbols/ts-module-scope.ts +19 -4
  172. package/src/symbols/ts-output-symbol.ts +1 -0
  173. package/temp/api.json +4999 -2681
  174. package/test/arrow-function.test.tsx +188 -0
  175. package/test/class.test.tsx +5 -6
  176. package/test/enum-jsdoc.test.tsx +6 -6
  177. package/test/function-call-expression.test.tsx +1 -2
  178. package/test/function-declaration-jsdoc.test.tsx +3 -3
  179. package/test/function-declaration.test.tsx +1 -2
  180. package/test/function-expression.test.tsx +195 -0
  181. package/test/function-type.test.tsx +118 -0
  182. package/test/imports.test.tsx +165 -1
  183. package/test/interface-jsdoc.test.tsx +9 -9
  184. package/test/interface.test.tsx +256 -6
  185. package/test/jsdoc.test.tsx +5 -5
  186. package/test/jsdocparam.test.tsx +8 -8
  187. package/test/member-expression.test.tsx +171 -2
  188. package/test/var-declaration-jsdoc.test.tsx +3 -3
  189. package/test/var-declaration.test.tsx +1 -1
  190. package/tsdoc-metadata.json +1 -1
  191. package/babel.config.cjs +0 -4
  192. package/dist/src/builtins/index.js.map +0 -1
  193. package/dist/src/builtins/node.js.map +0 -1
  194. package/dist/src/components/ArrayExpression.js.map +0 -1
  195. package/dist/src/components/BarrelFile.js.map +0 -1
  196. package/dist/src/components/BlockScope.js.map +0 -1
  197. package/dist/src/components/CallSignature.js.map +0 -1
  198. package/dist/src/components/ClassDeclaration.js.map +0 -1
  199. package/dist/src/components/CommaList.js.map +0 -1
  200. package/dist/src/components/Declaration.js.map +0 -1
  201. package/dist/src/components/EnumDeclaration.js.map +0 -1
  202. package/dist/src/components/EnumMember.js.map +0 -1
  203. package/dist/src/components/ExportStatement.js.map +0 -1
  204. package/dist/src/components/FunctionCallExpression.js.map +0 -1
  205. package/dist/src/components/FunctionDeclaration.js.map +0 -1
  206. package/dist/src/components/IfStatement.js.map +0 -1
  207. package/dist/src/components/ImportStatement.js.map +0 -1
  208. package/dist/src/components/Interface.js.map +0 -1
  209. package/dist/src/components/JSDoc.js.map +0 -1
  210. package/dist/src/components/JSDocComment.js.map +0 -1
  211. package/dist/src/components/JSDocExample.js.map +0 -1
  212. package/dist/src/components/JSDocParam.js.map +0 -1
  213. package/dist/src/components/MemberChainExpression.js.map +0 -1
  214. package/dist/src/components/MemberExpression.js.map +0 -1
  215. package/dist/src/components/ObjectExpression.js.map +0 -1
  216. package/dist/src/components/PackageDirectory.js.map +0 -1
  217. package/dist/src/components/PackageJson.js.map +0 -1
  218. package/dist/src/components/PropertyName.js.map +0 -1
  219. package/dist/src/components/Reference.js.map +0 -1
  220. package/dist/src/components/SingleLineCommentBlock.js.map +0 -1
  221. package/dist/src/components/SourceFile.js.map +0 -1
  222. package/dist/src/components/SwitchStatement.js.map +0 -1
  223. package/dist/src/components/TsConfigJson.js.map +0 -1
  224. package/dist/src/components/TypeDeclaration.js.map +0 -1
  225. package/dist/src/components/ValueExpression.js.map +0 -1
  226. package/dist/src/components/VarDeclaration.js.map +0 -1
  227. package/dist/src/components/index.js.map +0 -1
  228. package/dist/src/components/stc/index.js.map +0 -1
  229. package/dist/src/context/index.js.map +0 -1
  230. package/dist/src/context/private-scope.js.map +0 -1
  231. package/dist/src/create-package.js.map +0 -1
  232. package/dist/src/index.js.map +0 -1
  233. package/dist/src/name-conflict-resolver.js.map +0 -1
  234. package/dist/src/name-policy.js.map +0 -1
  235. package/dist/src/parameter-descriptor.js.map +0 -1
  236. package/dist/src/source-directory-data.js.map +0 -1
  237. package/dist/src/symbols/index.js.map +0 -1
  238. package/dist/src/symbols/reference.js.map +0 -1
  239. package/dist/src/symbols/scopes.js.map +0 -1
  240. package/dist/src/symbols/ts-member-scope.js.map +0 -1
  241. package/dist/src/symbols/ts-module-scope.js.map +0 -1
  242. package/dist/src/symbols/ts-output-symbol.js.map +0 -1
  243. package/dist/src/symbols/ts-package-scope.js.map +0 -1
  244. package/dist/src/utils.js.map +0 -1
@@ -0,0 +1,86 @@
1
+ import {
2
+ childrenArray,
3
+ findKeyedChild,
4
+ findUnkeyedChildren,
5
+ Scope,
6
+ } from "@alloy-js/core";
7
+ import { Children } from "@alloy-js/core/jsx-runtime";
8
+ import { getCallSignatureProps } from "../utils.js";
9
+ import { CallSignature, CallSignatureProps } from "./CallSignature.jsx";
10
+ import { Declaration } from "./Declaration.jsx";
11
+ import {
12
+ FunctionBody,
13
+ FunctionParameters,
14
+ getReturnType,
15
+ TypeParameters,
16
+ } from "./FunctionBase.jsx";
17
+
18
+ export interface FunctionExpressionProps extends CallSignatureProps {
19
+ async?: boolean;
20
+ children?: Children;
21
+ }
22
+
23
+ /**
24
+ * A TypeScript function expression.
25
+ *
26
+ * @example
27
+ * ```tsx
28
+ * <FunctionExpression async parameters={[{ name: "a", type: "number" }, { name: "b", type: "number" }]}>
29
+ * return a + b;
30
+ * <FunctionExpression>
31
+ * ```
32
+ * renders to
33
+ * ```ts
34
+ * async function (a, b) { return a + b; }
35
+ * ```
36
+ * @remarks
37
+ *
38
+ * Providing parameters and type parameters can be accomplished in one of three
39
+ * ways:
40
+ *
41
+ * 1. As an array of {@link ParameterDescriptor}s or
42
+ * {@link TypeParameterDescriptor}s.
43
+ * 2. As raw content via the `parametersChildren` or `typeParametersChildren`
44
+ * props.
45
+ * 3. As a child of this component via the
46
+ * {@link (FunctionExpression:namespace).Parameters} or
47
+ * {@link (FunctionExpression:namespace).TypeParameters} components.
48
+ */
49
+ export function FunctionExpression(props: FunctionExpressionProps) {
50
+ const children = childrenArray(() => props.children);
51
+ const typeParametersChildren =
52
+ findKeyedChild(children, TypeParameters.tag) ?? undefined;
53
+ const parametersChildren =
54
+ findKeyedChild(children, FunctionParameters.tag) ?? undefined;
55
+ const bodyChildren = findKeyedChild(children, FunctionBody.tag) ?? undefined;
56
+ const filteredChildren = findUnkeyedChildren(children);
57
+ const returnType = getReturnType(props.returnType, { async: props.async });
58
+
59
+ const sBody = bodyChildren ?? (
60
+ <FunctionExpression.Body>{filteredChildren}</FunctionExpression.Body>
61
+ );
62
+
63
+ const asyncKwd = props.async ? "async " : "";
64
+
65
+ const callSignatureProps = getCallSignatureProps(props, {
66
+ parametersChildren,
67
+ typeParametersChildren,
68
+ });
69
+
70
+ return (
71
+ <>
72
+ <Declaration {...props} nameKind="function">
73
+ {asyncKwd}
74
+ {"function "}
75
+ <Scope kind="function">
76
+ <CallSignature {...callSignatureProps} returnType={returnType} />{" "}
77
+ {sBody}
78
+ </Scope>
79
+ </Declaration>
80
+ </>
81
+ );
82
+ }
83
+
84
+ FunctionExpression.TypeParameters = TypeParameters;
85
+ FunctionExpression.Parameters = FunctionParameters;
86
+ FunctionExpression.Body = FunctionBody;
@@ -0,0 +1,93 @@
1
+ import {
2
+ childrenArray,
3
+ findKeyedChild,
4
+ Prose,
5
+ Scope,
6
+ Show,
7
+ } from "@alloy-js/core";
8
+ import { Children } from "@alloy-js/core/jsx-runtime";
9
+ import { getCallSignatureProps } from "../utils.js";
10
+ import { CallSignature, CallSignatureProps } from "./CallSignature.jsx";
11
+ import {
12
+ FunctionParameters,
13
+ FunctionTypeParameters,
14
+ getReturnType,
15
+ TypeParameters,
16
+ } from "./FunctionBase.jsx";
17
+ import { JSDoc } from "./JSDoc.jsx";
18
+ import { JSDocParams } from "./JSDocParam.jsx";
19
+ import { ensureTypeRefContext } from "./TypeRefContext.jsx";
20
+
21
+ /**
22
+ * Options for {@link FunctionType} component.
23
+ */
24
+ export interface FunctionTypeProps extends CallSignatureProps {
25
+ /** If the method is async */
26
+ readonly async?: boolean;
27
+ /** Documentation for this method. */
28
+ readonly doc?: Children;
29
+ /** Jsx content */
30
+ readonly children?: Children;
31
+ }
32
+
33
+ const FunctionTypeComponent = ensureTypeRefContext(
34
+ (props: FunctionTypeProps) => {
35
+ const children = childrenArray(() => props.children);
36
+ const typeParametersChildren =
37
+ findKeyedChild(children, TypeParameters.tag) ?? undefined;
38
+ const parametersChildren =
39
+ findKeyedChild(children, FunctionParameters.tag) ?? undefined;
40
+ const returnType = getReturnType(props.returnType ?? "void", {
41
+ async: props.async,
42
+ });
43
+
44
+ const callSignatureProps = getCallSignatureProps(props, {
45
+ parametersChildren,
46
+ typeParametersChildren,
47
+ });
48
+
49
+ return (
50
+ <>
51
+ <Show when={Boolean(props.doc)}>
52
+ <JSDoc>
53
+ {props.doc && <Prose children={props.doc} />}
54
+ {Array.isArray(props.parameters) && (
55
+ <JSDocParams parameters={props.parameters} />
56
+ )}
57
+ </JSDoc>
58
+ <hbr />
59
+ </Show>
60
+ <Scope kind="function">
61
+ <CallSignature {...callSignatureProps} returnType={null} />
62
+ {" => "}
63
+ {returnType}
64
+ </Scope>
65
+ </>
66
+ );
67
+ },
68
+ );
69
+
70
+ /**
71
+ * A TypeScript function declaration.
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * (foo: string, bar: number) => void
76
+ * ```
77
+ * @remarks
78
+ *
79
+ * Providing parameters and type parameters can be accomplished in one of three
80
+ * ways:
81
+ *
82
+ * 1. As an array of {@link TypeParameterDescriptor}s or
83
+ * {@link TypeParameterDescriptor}s.
84
+ * 2. As raw content via the `parametersChildren` or `typeParametersChildren`
85
+ * props.
86
+ * 3. As a child of this component via the
87
+ * {@link FunctionType.Parameters} or
88
+ * {@link FunctionType.TypeParameters} components.
89
+ */
90
+ export const FunctionType = Object.assign(FunctionTypeComponent, {
91
+ TypeParameters,
92
+ Parameters: FunctionTypeParameters,
93
+ });
@@ -9,6 +9,7 @@ import {
9
9
  ImportedSymbol,
10
10
  ImportRecords,
11
11
  TSPackageScope,
12
+ TSSymbolFlags,
12
13
  } from "../symbols/index.js";
13
14
  import { modulePath } from "../utils.js";
14
15
  import { usePackage } from "./PackageDirectory.js";
@@ -90,11 +91,23 @@ export function ImportStatement(props: ImportStatementProps) {
90
91
  }
91
92
 
92
93
  if (namedImportSymbols.length > 0) {
94
+ const allNamedImportsAreTypes = namedImportSymbols.every(
95
+ (nis) => nis.local.tsFlags & TSSymbolFlags.TypeSymbol,
96
+ );
97
+
98
+ if (allNamedImportsAreTypes) {
99
+ parts.push("type ");
100
+ }
93
101
  parts.push("{ ");
94
102
  parts.push(
95
103
  mapJoin(
96
104
  () => namedImportSymbols,
97
- (nis) => <ImportBinding importedSymbol={nis} />,
105
+ (nis) => (
106
+ <ImportBinding
107
+ importedSymbol={nis}
108
+ inTypeImport={allNamedImportsAreTypes}
109
+ />
110
+ ),
98
111
  { joiner: ", " },
99
112
  ),
100
113
  );
@@ -109,9 +122,10 @@ export function ImportStatement(props: ImportStatementProps) {
109
122
  interface ImportBindingProps {
110
123
  importedSymbol: ImportedSymbol;
111
124
  default?: boolean;
125
+ inTypeImport?: boolean;
112
126
  }
113
127
 
114
- function ImportBinding(props: ImportBindingProps) {
128
+ function ImportBinding(props: Readonly<ImportBindingProps>) {
115
129
  const text = memo(() => {
116
130
  const localName = props.importedSymbol.local.name;
117
131
  const targetName = props.importedSymbol.target.name;
@@ -124,5 +138,18 @@ function ImportBinding(props: ImportBindingProps) {
124
138
  }
125
139
  });
126
140
 
127
- return text;
141
+ const prefix = memo(() =>
142
+ (
143
+ !props.inTypeImport &&
144
+ props.importedSymbol.local.tsFlags & TSSymbolFlags.TypeSymbol
145
+ ) ?
146
+ "type "
147
+ : "",
148
+ );
149
+ return (
150
+ <>
151
+ {prefix}
152
+ {text}
153
+ </>
154
+ );
128
155
  }
@@ -1,17 +1,22 @@
1
1
  import {
2
2
  Block,
3
3
  Children,
4
- MemberDeclaration,
4
+ MemberScope,
5
5
  Name,
6
6
  OutputSymbolFlags,
7
7
  Refkey,
8
8
  Show,
9
+ useMemberDeclaration,
10
+ useMemberScope,
11
+ Wrap,
9
12
  } from "@alloy-js/core";
10
13
  import { useTSNamePolicy } from "../name-policy.js";
11
14
  import { BaseDeclarationProps, Declaration } from "./Declaration.js";
12
15
  import { JSDoc } from "./JSDoc.jsx";
13
16
 
17
+ import { MemberDeclaration } from "./MemberDeclaration.jsx";
14
18
  import { PropertyName } from "./PropertyName.jsx";
19
+ import { ensureTypeRefContext } from "./TypeRefContext.jsx";
15
20
 
16
21
  export interface InterfaceDeclarationProps extends BaseDeclarationProps {
17
22
  extends?: Children;
@@ -26,32 +31,51 @@ export interface InterfaceDeclarationProps extends BaseDeclarationProps {
26
31
  * `default` boolean props determine whether and how this symbol is exported
27
32
  * from the package.
28
33
  */
29
- export function InterfaceDeclaration(props: InterfaceDeclarationProps) {
30
- const extendsPart = props.extends ? <> extends {props.extends}</> : "";
31
- const flags = OutputSymbolFlags.StaticMemberContainer;
34
+ export const InterfaceDeclaration = ensureTypeRefContext(
35
+ (props: InterfaceDeclarationProps) => {
36
+ const extendsPart = props.extends ? <> extends {props.extends}</> : "";
37
+ const flags = OutputSymbolFlags.StaticMemberContainer;
32
38
 
33
- return (
34
- <>
35
- <Show when={Boolean(props.doc)}>
36
- <JSDoc children={props.doc} />
37
- <hbr />
38
- </Show>
39
- <Declaration {...props} nameKind="interface" flags={flags}>
40
- interface <Name />
41
- {extendsPart}{" "}
42
- <InterfaceExpression>{props.children}</InterfaceExpression>
43
- </Declaration>
44
- </>
45
- );
46
- }
39
+ return (
40
+ <>
41
+ <Show when={Boolean(props.doc)}>
42
+ <JSDoc children={props.doc} />
43
+ <hbr />
44
+ </Show>
45
+ <Declaration {...props} nameKind="interface" flags={flags} kind="type">
46
+ interface <Name />
47
+ {extendsPart}{" "}
48
+ <InterfaceExpression>{props.children}</InterfaceExpression>
49
+ </Declaration>
50
+ </>
51
+ );
52
+ },
53
+ );
47
54
 
48
55
  export interface InterfaceExpressionProps {
49
56
  children?: Children;
50
57
  }
51
58
 
52
- export function InterfaceExpression(props: InterfaceExpressionProps) {
53
- return <Block>{props.children}</Block>;
54
- }
59
+ export const InterfaceExpression = ensureTypeRefContext(
60
+ (props: InterfaceExpressionProps) => {
61
+ const parentMemberSym = useMemberDeclaration();
62
+
63
+ if (parentMemberSym) {
64
+ parentMemberSym.binder.addStaticMembersToSymbol(parentMemberSym);
65
+ }
66
+ return (
67
+ <group>
68
+ <Wrap
69
+ when={!!parentMemberSym}
70
+ with={MemberScope}
71
+ props={{ owner: parentMemberSym! }}
72
+ >
73
+ <Block>{props.children}</Block>
74
+ </Wrap>
75
+ </group>
76
+ );
77
+ },
78
+ );
55
79
 
56
80
  export interface InterfaceMemberProps {
57
81
  name?: string;
@@ -59,6 +83,7 @@ export interface InterfaceMemberProps {
59
83
  type?: Children;
60
84
  children?: Children;
61
85
  optional?: boolean;
86
+ nullish?: boolean;
62
87
  readonly?: boolean;
63
88
  doc?: Children;
64
89
  refkey?: Refkey | Refkey[];
@@ -87,9 +112,19 @@ export function InterfaceMember(props: InterfaceMemberProps) {
87
112
  } else {
88
113
  const namer = useTSNamePolicy();
89
114
  const name = namer.getName(props.name!, "interface-member");
90
- if (props.refkey) {
115
+ const memberScope = useMemberScope();
116
+
117
+ if (memberScope) {
91
118
  return (
92
- <MemberDeclaration static name={name} refkey={props.refkey}>
119
+ <MemberDeclaration
120
+ static
121
+ exactName={name}
122
+ kind="type"
123
+ nameKind="interface-member"
124
+ flags={OutputSymbolFlags.StaticMember}
125
+ nullish={props.nullish ?? props.optional}
126
+ refkey={props.refkey}
127
+ >
93
128
  <Show when={Boolean(props.doc)}>
94
129
  <JSDoc children={props.doc} />
95
130
  <hbr />
@@ -0,0 +1,96 @@
1
+ import {
2
+ childrenArray,
3
+ findKeyedChild,
4
+ MemberDeclaration,
5
+ Prose,
6
+ Refkey,
7
+ Scope,
8
+ Show,
9
+ } from "@alloy-js/core";
10
+ import { Children } from "@alloy-js/core/jsx-runtime";
11
+ import { useTSNamePolicy } from "../name-policy.js";
12
+ import { getCallSignatureProps } from "../utils.js";
13
+ import { CallSignature, CallSignatureProps } from "./CallSignature.jsx";
14
+ import {
15
+ FunctionParameters,
16
+ FunctionTypeParameters,
17
+ getReturnType,
18
+ TypeParameters,
19
+ } from "./FunctionBase.jsx";
20
+ import { JSDoc } from "./JSDoc.jsx";
21
+ import { JSDocParams } from "./JSDocParam.jsx";
22
+
23
+ /** Props for {@link (InterfaceMethod:namespace)} component */
24
+ export interface InterfaceMethodProps extends CallSignatureProps {
25
+ /** Interface member name */
26
+ readonly name: string;
27
+ /** If the method is async. It will change the returnType from `T` to `Promise<T>` */
28
+ readonly async?: boolean;
29
+ /** Documentation for this method. */
30
+ readonly doc?: Children;
31
+ /** Jsx content */
32
+ readonly children?: Children;
33
+ /** Ref key for the member declaration */
34
+ readonly refkey?: Refkey | Refkey[];
35
+ }
36
+
37
+ /**
38
+ * An interface method declaration.
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * (foo: string, bar: number) => void
43
+ * ```
44
+ *
45
+ * @remarks
46
+ *
47
+ * Providing parameters and type parameters can be accomplished in one of three
48
+ * ways:
49
+ *
50
+ * 1. As an array of {@link TypeParameterDescriptor}s or
51
+ * {@link TypeParameterDescriptor}s.
52
+ * 2. As raw content via the `parametersChildren` or `typeParametersChildren`
53
+ * props.
54
+ * 3. As a child of this component via the
55
+ * {@link (InterfaceMethod:namespace).Parameters} or
56
+ * {@link (InterfaceMethod:namespace).TypeParameters} components.
57
+ */
58
+ export function InterfaceMethod(props: InterfaceMethodProps) {
59
+ const children = childrenArray(() => props.children);
60
+ const typeParametersChildren =
61
+ findKeyedChild(children, TypeParameters.tag) ?? undefined;
62
+ const parametersChildren =
63
+ findKeyedChild(children, FunctionParameters.tag) ?? undefined;
64
+ const returnType = getReturnType(props.returnType ?? "void", {
65
+ async: props.async,
66
+ });
67
+
68
+ const callSignatureProps = getCallSignatureProps(props, {
69
+ parametersChildren,
70
+ typeParametersChildren,
71
+ });
72
+ const namer = useTSNamePolicy();
73
+ const name = namer.getName(props.name!, "interface-member");
74
+ return (
75
+ <>
76
+ <Show when={Boolean(props.doc)}>
77
+ <JSDoc>
78
+ {props.doc && <Prose children={props.doc} />}
79
+ {Array.isArray(props.parameters) && (
80
+ <JSDocParams parameters={props.parameters} />
81
+ )}
82
+ </JSDoc>
83
+ <hbr />
84
+ </Show>
85
+ <MemberDeclaration static {...props} refkey={props.refkey}>
86
+ {name}
87
+ <Scope name={props.name} kind="function">
88
+ <CallSignature {...callSignatureProps} returnType={returnType} />
89
+ </Scope>
90
+ </MemberDeclaration>
91
+ </>
92
+ );
93
+ }
94
+
95
+ InterfaceMethod.TypeParameters = TypeParameters;
96
+ InterfaceMethod.Parameters = FunctionTypeParameters;
@@ -17,7 +17,7 @@ export function JSDocComment(props: JSDocCommentProps) {
17
17
  <hbr />
18
18
  {props.children}
19
19
  </align>
20
- <hbr /> **/
20
+ <hbr /> */
21
21
  </>
22
22
  );
23
23
  }
@@ -0,0 +1,76 @@
1
+ import {
2
+ MemberDeclaration as CoreMemberDeclaration,
3
+ MemberDeclarationPropsWithInfo,
4
+ OutputSymbolFlags,
5
+ } from "@alloy-js/core";
6
+ import { TypeScriptElements, useTSNamePolicy } from "../name-policy.js";
7
+ import {
8
+ createTSSymbol,
9
+ TSOutputSymbol,
10
+ TSSymbolFlags,
11
+ } from "../symbols/index.js";
12
+
13
+ export interface MemberDeclarationProps
14
+ extends Omit<MemberDeclarationPropsWithInfo, "name"> {
15
+ /**
16
+ * The name of this member declaration.
17
+ */
18
+ name?: string;
19
+
20
+ /** Name that shouldn't go through the name policy resolver again. */
21
+ exactName?: string;
22
+
23
+ /**
24
+ * Whether this is a memberdeclaration of a type (e.g. interface, type alias) or a
25
+ * value (e.g. var, const, let).
26
+ */
27
+ kind?: "type" | "value";
28
+
29
+ /**
30
+ * The name policy kind to apply to the memberdeclaration.
31
+ */
32
+ nameKind?: TypeScriptElements;
33
+
34
+ /**
35
+ * Flags for the symbol created by this component.
36
+ */
37
+ flags?: OutputSymbolFlags;
38
+
39
+ /**
40
+ * The symbol to use for this memberdeclaration.
41
+ */
42
+ symbol?: TSOutputSymbol;
43
+ /**
44
+ * Whether this member can be null or undefined.
45
+ */
46
+ nullish?: boolean;
47
+ }
48
+
49
+ export function MemberDeclaration(props: Readonly<MemberDeclarationProps>) {
50
+ let sym: TSOutputSymbol;
51
+
52
+ if (props.symbol) {
53
+ sym = props.symbol;
54
+ } else {
55
+ const namePolicy = useTSNamePolicy();
56
+
57
+ let tsFlags: TSSymbolFlags =
58
+ props.nullish ? TSSymbolFlags.Nullish : TSSymbolFlags.None;
59
+
60
+ if (props.kind === "type") {
61
+ tsFlags |= TSSymbolFlags.TypeSymbol;
62
+ }
63
+
64
+ sym = createTSSymbol({
65
+ name: props.exactName ?? namePolicy.getName(props.name!, props.nameKind!),
66
+ refkey: props.refkey,
67
+ flags: props.flags,
68
+ tsFlags,
69
+ metadata: props.metadata,
70
+ });
71
+ }
72
+
73
+ return (
74
+ <CoreMemberDeclaration symbol={sym}>{props.children}</CoreMemberDeclaration>
75
+ );
76
+ }
@@ -11,6 +11,7 @@ import {
11
11
  toRef,
12
12
  useBinder,
13
13
  } from "@alloy-js/core";
14
+ import { TSOutputSymbol, TSSymbolFlags } from "../symbols/ts-output-symbol.js";
14
15
  import { isValidJSIdentifier } from "../utils.js";
15
16
 
16
17
  export interface MemberExpressionProps {
@@ -78,7 +79,9 @@ export function MemberExpression(props: MemberExpressionProps): Children {
78
79
  const symbolRef = binder.getSymbolForRefkey(partProps.refkey);
79
80
  part.base = computed(() => {
80
81
  if (symbolRef.value) {
81
- part.nullish.value = !!symbolRef.value.metadata.nullish;
82
+ part.nullish.value = !!(
83
+ (symbolRef.value as TSOutputSymbol).tsFlags & TSSymbolFlags.Nullish
84
+ );
82
85
  part.accessStyle.value = accessStyleForMemberName(
83
86
  symbolRef.value.name,
84
87
  );
@@ -88,7 +91,9 @@ export function MemberExpression(props: MemberExpressionProps): Children {
88
91
  }
89
92
  });
90
93
  } else if (partProps.symbol) {
91
- part.nullish.value = !!partProps.symbol.metadata.nullish;
94
+ part.nullish.value = !!(
95
+ (partProps.symbol as TSOutputSymbol).tsFlags & TSSymbolFlags.Nullish
96
+ );
92
97
  part.accessStyle = computed(() => {
93
98
  return accessStyleForMemberName(partProps.symbol!.name);
94
99
  });
@@ -1,12 +1,22 @@
1
1
  import { Refkey } from "@alloy-js/core";
2
+ import { isTypeRefContext } from "../context/type-ref-context.js";
2
3
  import { ref } from "../symbols/index.js";
3
4
 
4
5
  export interface ReferenceProps {
5
6
  refkey: Refkey;
7
+
8
+ /**
9
+ * Whether this is a reference to a type.
10
+ *
11
+ * @remarks
12
+ * This affect things like import where `type` keyword might be used.
13
+ */
14
+ type?: boolean;
6
15
  }
7
16
 
8
- export function Reference({ refkey }: ReferenceProps) {
9
- const reference = ref(refkey);
17
+ export function Reference({ refkey, type }: ReferenceProps) {
18
+ const inTypeRef = isTypeRefContext();
19
+ const reference = ref(refkey, { type: type ?? inTypeRef });
10
20
 
11
21
  return <>{reference}</>;
12
22
  }
@@ -1,19 +1,22 @@
1
1
  import { Name, Show } from "@alloy-js/core";
2
2
  import { BaseDeclarationProps, Declaration } from "./Declaration.js";
3
3
  import { JSDoc } from "./JSDoc.jsx";
4
+ import { ensureTypeRefContext } from "./TypeRefContext.jsx";
4
5
 
5
6
  export interface TypeDeclarationProps extends BaseDeclarationProps {}
6
7
 
7
- export function TypeDeclaration(props: TypeDeclarationProps) {
8
- return (
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
- </>
18
- );
19
- }
8
+ export const TypeDeclaration = ensureTypeRefContext(
9
+ (props: TypeDeclarationProps) => {
10
+ return (
11
+ <>
12
+ <Show when={Boolean(props.doc)}>
13
+ <JSDoc children={props.doc} />
14
+ <hbr />
15
+ </Show>
16
+ <Declaration {...props} nameKind="type">
17
+ type <Name /> = {props.children};
18
+ </Declaration>
19
+ </>
20
+ );
21
+ },
22
+ );
@@ -0,0 +1,49 @@
1
+ import type { Children } from "@alloy-js/core/jsx-runtime";
2
+ import {
3
+ isTypeRefContext,
4
+ TypeRefContext as TypeRefContextDef,
5
+ } from "../context/type-ref-context.jsx";
6
+
7
+ export interface TypeRefContextProps {
8
+ /**
9
+ * Children
10
+ */
11
+ children: Children;
12
+ }
13
+
14
+ /**
15
+ * Set the current context of reference to be type reference.
16
+ *
17
+ * @remarks
18
+ * References used inside the children of this component will be treated as type reference {@link Reference}.
19
+ *
20
+ * Prefer using {@link ensureTypeRefContext} when wrapping the whole component to reduce tree nodes.
21
+ */
22
+ export const TypeRefContext = ({ children }: TypeRefContextProps) => {
23
+ return (
24
+ <TypeRefContextDef.Provider value={{ type: true }}>
25
+ {children}
26
+ </TypeRefContextDef.Provider>
27
+ );
28
+ };
29
+
30
+ /** Ensure the current component is inside a type ref context.
31
+ * If not it will wrap in a {@link TypeRefContext} component.
32
+ * If yes it will not add an extra node and return the original component.
33
+ */
34
+ export function ensureTypeRefContext<A extends unknown[]>(
35
+ fn: (...args: A) => Children,
36
+ ): (...args: A) => Children {
37
+ return (...args: A) => {
38
+ const ref = isTypeRefContext();
39
+ if (!ref) {
40
+ return (
41
+ <TypeRefContextDef.Provider value={{ type: true }}>
42
+ {fn(...args)}
43
+ </TypeRefContextDef.Provider>
44
+ );
45
+ }
46
+
47
+ return fn(...args);
48
+ };
49
+ }