@alloy-js/typescript 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (202) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/src/components/ArrayExpression.d.ts.map +1 -1
  3. package/dist/src/components/ArrayExpression.js +9 -3
  4. package/dist/src/components/ArrayExpression.js.map +1 -1
  5. package/dist/src/components/BlockScope.d.ts +13 -0
  6. package/dist/src/components/BlockScope.d.ts.map +1 -0
  7. package/dist/src/components/BlockScope.js +20 -0
  8. package/dist/src/components/BlockScope.js.map +1 -0
  9. package/dist/src/components/CallSignature.d.ts +38 -0
  10. package/dist/src/components/CallSignature.d.ts.map +1 -0
  11. package/dist/src/components/CallSignature.js +26 -0
  12. package/dist/src/components/CallSignature.js.map +1 -0
  13. package/dist/src/components/ClassDeclaration.d.ts +3 -4
  14. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  15. package/dist/src/components/ClassDeclaration.js +99 -41
  16. package/dist/src/components/ClassDeclaration.js.map +1 -1
  17. package/dist/src/components/Declaration.d.ts +21 -8
  18. package/dist/src/components/Declaration.d.ts.map +1 -1
  19. package/dist/src/components/Declaration.js +39 -21
  20. package/dist/src/components/Declaration.js.map +1 -1
  21. package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
  22. package/dist/src/components/EnumDeclaration.js +18 -6
  23. package/dist/src/components/EnumDeclaration.js.map +1 -1
  24. package/dist/src/components/EnumMember.d.ts +8 -0
  25. package/dist/src/components/EnumMember.d.ts.map +1 -1
  26. package/dist/src/components/EnumMember.js +27 -5
  27. package/dist/src/components/EnumMember.js.map +1 -1
  28. package/dist/src/components/FunctionCallExpression.js +1 -1
  29. package/dist/src/components/FunctionCallExpression.js.map +1 -1
  30. package/dist/src/components/FunctionDeclaration.d.ts +21 -13
  31. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  32. package/dist/src/components/FunctionDeclaration.js +79 -45
  33. package/dist/src/components/FunctionDeclaration.js.map +1 -1
  34. package/dist/src/components/IfStatement.d.ts +25 -0
  35. package/dist/src/components/IfStatement.d.ts.map +1 -0
  36. package/dist/src/components/IfStatement.js +34 -0
  37. package/dist/src/components/IfStatement.js.map +1 -0
  38. package/dist/src/components/Interface.d.ts +10 -2
  39. package/dist/src/components/Interface.d.ts.map +1 -1
  40. package/dist/src/components/Interface.js +66 -6
  41. package/dist/src/components/Interface.js.map +1 -1
  42. package/dist/src/components/JSDoc.d.ts.map +1 -1
  43. package/dist/src/components/JSDoc.js +1 -5
  44. package/dist/src/components/JSDoc.js.map +1 -1
  45. package/dist/src/components/JSDocParam.d.ts +22 -0
  46. package/dist/src/components/JSDocParam.d.ts.map +1 -0
  47. package/dist/src/components/JSDocParam.js +121 -0
  48. package/dist/src/components/JSDocParam.js.map +1 -0
  49. package/dist/src/components/ObjectExpression.d.ts.map +1 -1
  50. package/dist/src/components/ObjectExpression.js +9 -2
  51. package/dist/src/components/ObjectExpression.js.map +1 -1
  52. package/dist/src/components/PropertyName.d.ts +19 -0
  53. package/dist/src/components/PropertyName.d.ts.map +1 -0
  54. package/dist/src/components/PropertyName.js +36 -0
  55. package/dist/src/components/PropertyName.js.map +1 -0
  56. package/dist/src/components/SingleLineCommentBlock.d.ts +10 -0
  57. package/dist/src/components/SingleLineCommentBlock.d.ts.map +1 -0
  58. package/dist/src/components/SingleLineCommentBlock.js +20 -0
  59. package/dist/src/components/SingleLineCommentBlock.js.map +1 -0
  60. package/dist/src/components/SourceFile.d.ts +6 -0
  61. package/dist/src/components/SourceFile.d.ts.map +1 -1
  62. package/dist/src/components/SourceFile.js +31 -0
  63. package/dist/src/components/SourceFile.js.map +1 -1
  64. package/dist/src/components/SwitchStatement.d.ts +31 -0
  65. package/dist/src/components/SwitchStatement.d.ts.map +1 -0
  66. package/dist/src/components/SwitchStatement.js +48 -0
  67. package/dist/src/components/SwitchStatement.js.map +1 -0
  68. package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
  69. package/dist/src/components/TypeDeclaration.js +16 -3
  70. package/dist/src/components/TypeDeclaration.js.map +1 -1
  71. package/dist/src/components/VarDeclaration.d.ts +1 -1
  72. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  73. package/dist/src/components/VarDeclaration.js +19 -6
  74. package/dist/src/components/VarDeclaration.js.map +1 -1
  75. package/dist/src/components/index.d.ts +7 -0
  76. package/dist/src/components/index.d.ts.map +1 -1
  77. package/dist/src/components/index.js +7 -0
  78. package/dist/src/components/index.js.map +1 -1
  79. package/dist/src/components/stc/index.d.ts +35 -112
  80. package/dist/src/components/stc/index.d.ts.map +1 -1
  81. package/dist/src/components/stc/index.js +8 -1
  82. package/dist/src/components/stc/index.js.map +1 -1
  83. package/dist/src/context/index.d.ts +2 -0
  84. package/dist/src/context/index.d.ts.map +1 -0
  85. package/dist/src/context/index.js +1 -0
  86. package/dist/src/context/index.js.map +1 -0
  87. package/dist/src/context/private-scope.d.ts +18 -0
  88. package/dist/src/context/private-scope.d.ts.map +1 -0
  89. package/dist/src/context/private-scope.js +9 -0
  90. package/dist/src/context/private-scope.js.map +1 -0
  91. package/dist/src/index.d.ts +1 -0
  92. package/dist/src/index.d.ts.map +1 -1
  93. package/dist/src/index.js +1 -0
  94. package/dist/src/index.js.map +1 -1
  95. package/dist/src/parameter-descriptor.d.ts +60 -0
  96. package/dist/src/parameter-descriptor.d.ts.map +1 -0
  97. package/dist/src/parameter-descriptor.js +1 -0
  98. package/dist/src/parameter-descriptor.js.map +1 -0
  99. package/dist/src/symbols/reference.d.ts.map +1 -1
  100. package/dist/src/symbols/reference.js +50 -12
  101. package/dist/src/symbols/reference.js.map +1 -1
  102. package/dist/src/symbols/ts-member-scope.d.ts +1 -1
  103. package/dist/src/symbols/ts-member-scope.d.ts.map +1 -1
  104. package/dist/src/symbols/ts-member-scope.js +2 -0
  105. package/dist/src/symbols/ts-member-scope.js.map +1 -1
  106. package/dist/src/symbols/ts-output-symbol.d.ts +7 -4
  107. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  108. package/dist/src/symbols/ts-output-symbol.js +11 -2
  109. package/dist/src/symbols/ts-output-symbol.js.map +1 -1
  110. package/dist/src/utils.d.ts +7 -0
  111. package/dist/src/utils.d.ts.map +1 -1
  112. package/dist/src/utils.js +17 -0
  113. package/dist/src/utils.js.map +1 -1
  114. package/dist/test/block-scope.test.d.ts +2 -0
  115. package/dist/test/block-scope.test.d.ts.map +1 -0
  116. package/dist/test/enum-jsdoc.test.d.ts +2 -0
  117. package/dist/test/enum-jsdoc.test.d.ts.map +1 -0
  118. package/dist/test/function-declaration-jsdoc.test.d.ts +2 -0
  119. package/dist/test/function-declaration-jsdoc.test.d.ts.map +1 -0
  120. package/dist/test/if-statement.test.d.ts +2 -0
  121. package/dist/test/if-statement.test.d.ts.map +1 -0
  122. package/dist/test/interface-jsdoc.test.d.ts +2 -0
  123. package/dist/test/interface-jsdoc.test.d.ts.map +1 -0
  124. package/dist/test/jscomment.test.d.ts +2 -0
  125. package/dist/test/jscomment.test.d.ts.map +1 -0
  126. package/dist/test/jsdoc.test.d.ts.map +1 -1
  127. package/dist/test/jsdocparam.test.d.ts +2 -0
  128. package/dist/test/jsdocparam.test.d.ts.map +1 -0
  129. package/dist/test/name-conflict.test.d.ts +2 -0
  130. package/dist/test/name-conflict.test.d.ts.map +1 -0
  131. package/dist/test/package-json.test.d.ts +2 -0
  132. package/dist/test/package-json.test.d.ts.map +1 -0
  133. package/dist/test/source-file.test.d.ts +2 -0
  134. package/dist/test/source-file.test.d.ts.map +1 -0
  135. package/dist/test/switch-statement.test.d.ts +2 -0
  136. package/dist/test/switch-statement.test.d.ts.map +1 -0
  137. package/dist/test/var-declaration-jsdoc.test.d.ts +2 -0
  138. package/dist/test/var-declaration-jsdoc.test.d.ts.map +1 -0
  139. package/dist/tsconfig.tsbuildinfo +1 -1
  140. package/package.json +3 -3
  141. package/src/components/ArrayExpression.tsx +13 -12
  142. package/src/components/BlockScope.tsx +31 -0
  143. package/src/components/CallSignature.tsx +67 -0
  144. package/src/components/ClassDeclaration.tsx +103 -50
  145. package/src/components/Declaration.tsx +72 -27
  146. package/src/components/EnumDeclaration.tsx +26 -18
  147. package/src/components/EnumMember.tsx +24 -4
  148. package/src/components/FunctionCallExpression.tsx +1 -1
  149. package/src/components/FunctionDeclaration.tsx +106 -82
  150. package/src/components/IfStatement.tsx +51 -0
  151. package/src/components/Interface.tsx +61 -14
  152. package/src/components/JSDoc.tsx +1 -10
  153. package/src/components/JSDocParam.tsx +115 -0
  154. package/src/components/ObjectExpression.tsx +7 -2
  155. package/src/components/PropertyName.tsx +50 -0
  156. package/src/components/SingleLineCommentBlock.tsx +21 -0
  157. package/src/components/SourceFile.tsx +29 -0
  158. package/src/components/SwitchStatement.tsx +78 -0
  159. package/src/components/TypeDeclaration.tsx +11 -4
  160. package/src/components/VarDeclaration.tsx +19 -11
  161. package/src/components/index.ts +7 -0
  162. package/src/components/stc/index.ts +8 -1
  163. package/src/context/index.ts +1 -0
  164. package/src/context/private-scope.ts +23 -0
  165. package/src/index.ts +1 -0
  166. package/src/parameter-descriptor.ts +71 -0
  167. package/src/symbols/reference.ts +70 -18
  168. package/src/symbols/ts-member-scope.ts +6 -2
  169. package/src/symbols/ts-output-symbol.ts +24 -11
  170. package/src/utils.ts +33 -0
  171. package/temp/api.json +3816 -1925
  172. package/test/array.test.tsx +60 -0
  173. package/test/basic.test.tsx +2 -2
  174. package/test/block-scope.test.tsx +29 -0
  175. package/test/class.test.tsx +173 -5
  176. package/test/enum-jsdoc.test.tsx +74 -0
  177. package/test/enum.test.tsx +50 -3
  178. package/test/function-call-expression.test.tsx +5 -2
  179. package/test/function-declaration-jsdoc.test.tsx +89 -0
  180. package/test/function-declaration.test.tsx +13 -9
  181. package/test/if-statement.test.tsx +28 -0
  182. package/test/interface-jsdoc.test.tsx +164 -0
  183. package/test/interface.test.tsx +14 -0
  184. package/test/jscomment.test.tsx +104 -0
  185. package/test/jsdoc.test.tsx +9 -15
  186. package/test/jsdocparam.test.tsx +188 -0
  187. package/test/name-conflict.test.tsx +105 -0
  188. package/test/name-policy.test.tsx +1 -1
  189. package/test/package-json.test.tsx +93 -0
  190. package/test/reference.test.tsx +6 -2
  191. package/test/source-file.test.tsx +38 -0
  192. package/test/switch-statement.test.tsx +50 -0
  193. package/test/value-expression.test.tsx +9 -0
  194. package/test/var-declaration-jsdoc.test.tsx +70 -0
  195. package/test/var-declaration.test.tsx +1 -1
  196. package/tsconfig.json +1 -8
  197. package/vitest.config.ts +2 -10
  198. package/dist/src/components/JSDocParagraph.d.ts +0 -10
  199. package/dist/src/components/JSDocParagraph.d.ts.map +0 -1
  200. package/dist/src/components/JSDocParagraph.js +0 -23
  201. package/dist/src/components/JSDocParagraph.js.map +0 -1
  202. package/src/components/JSDocParagraph.tsx +0 -35
@@ -17,6 +17,7 @@ import { createTSModuleScope, TSModuleScope } from "../symbols/index.js";
17
17
  import { ImportStatements } from "./ImportStatement.js";
18
18
  import { PackageContext } from "./PackageDirectory.js";
19
19
  import { Reference } from "./Reference.js";
20
+ import { SingleLineCommentBlock } from "./SingleLineCommentBlock.jsx";
20
21
  export interface SourceFileContext {
21
22
  scope: TSModuleScope;
22
23
  }
@@ -30,6 +31,8 @@ export function useSourceFile() {
30
31
  export interface SourceFileProps {
31
32
  path: string;
32
33
  children?: Children;
34
+ header?: Children;
35
+ headerComment?: string;
33
36
  export?: boolean | string;
34
37
  }
35
38
 
@@ -59,11 +62,20 @@ export function SourceFile(props: SourceFileProps) {
59
62
  }
60
63
  }
61
64
 
65
+ const header =
66
+ props.header || props.headerComment ?
67
+ <SourceFileHeader
68
+ header={props.header}
69
+ headerComment={props.headerComment}
70
+ />
71
+ : undefined;
72
+
62
73
  return (
63
74
  <CoreSourceFile
64
75
  path={props.path}
65
76
  filetype="typescript"
66
77
  reference={Reference}
78
+ header={header}
67
79
  >
68
80
  <Show when={scope.importedModules.size > 0}>
69
81
  <ImportStatements records={scope.importedModules} />
@@ -76,3 +88,20 @@ export function SourceFile(props: SourceFileProps) {
76
88
  </CoreSourceFile>
77
89
  );
78
90
  }
91
+
92
+ export interface SourceFileHeaderProps {
93
+ header?: Children;
94
+ headerComment?: string;
95
+ }
96
+
97
+ function SourceFileHeader(props: SourceFileHeaderProps) {
98
+ return (
99
+ <>
100
+ <Show when={props.headerComment !== undefined}>
101
+ <SingleLineCommentBlock>{props.headerComment}</SingleLineCommentBlock>
102
+ </Show>
103
+ <Show when={props.header !== undefined}>{props.header}</Show>
104
+ <hbr />
105
+ </>
106
+ );
107
+ }
@@ -0,0 +1,78 @@
1
+ import { Block, Indent, Match, Switch } from "@alloy-js/core";
2
+ import { Children } from "@alloy-js/core/jsx-runtime";
3
+ import { BlockScope } from "./BlockScope.jsx";
4
+ import { ValueExpression } from "./ValueExpression.jsx";
5
+
6
+ export interface SwitchStatementProps {
7
+ expression: Children;
8
+ children: Children;
9
+ }
10
+
11
+ /**
12
+ * A switch statement.
13
+ */
14
+ export function SwitchStatement(props: SwitchStatementProps) {
15
+ return (
16
+ <>
17
+ switch ({props.expression}) <Block>{props.children}</Block>
18
+ </>
19
+ );
20
+ }
21
+
22
+ export interface CaseClausePropsBase {
23
+ children: Children;
24
+ block?: boolean;
25
+ break?: boolean;
26
+ }
27
+
28
+ export interface CaseClausePropsWithExpression extends CaseClausePropsBase {
29
+ expression: Children;
30
+ }
31
+
32
+ export interface CaseClausePropsWithValue extends CaseClausePropsBase {
33
+ jsValue: unknown;
34
+ }
35
+
36
+ export interface CaseClausePropsWithDefault extends CaseClausePropsBase {
37
+ default: true;
38
+ }
39
+
40
+ export type CaseClauseProps =
41
+ | CaseClausePropsWithExpression
42
+ | CaseClausePropsWithValue
43
+ | CaseClausePropsWithDefault;
44
+
45
+ /**
46
+ * A case clause in a switch statement. Provide either an expression or a JS
47
+ * value to compare to the switch expression. Can optionally be wrapped in a
48
+ * block. Pass `break` to finish with a break statement.
49
+ */
50
+ export function CaseClause(props: CaseClauseProps) {
51
+ const Wrapper = props.block ? BlockScope : Indent;
52
+ return (
53
+ <>
54
+ <Switch>
55
+ <Match when={"default" in props && props.default}>default </Match>
56
+ <Match else>
57
+ case{" "}
58
+ {"expression" in props ?
59
+ props.expression
60
+ : <ValueExpression
61
+ jsValue={(props as CaseClausePropsWithValue).jsValue}
62
+ />
63
+ }
64
+ </Match>
65
+ </Switch>
66
+ {": "}
67
+ <Wrapper>
68
+ {props.children}
69
+ {props.break && (
70
+ <>
71
+ <hbr />
72
+ break;
73
+ </>
74
+ )}
75
+ </Wrapper>
76
+ </>
77
+ );
78
+ }
@@ -1,12 +1,19 @@
1
- import { Name } from "@alloy-js/core";
1
+ import { Name, Show } from "@alloy-js/core";
2
2
  import { BaseDeclarationProps, Declaration } from "./Declaration.js";
3
+ import { JSDoc } from "./JSDoc.jsx";
3
4
 
4
5
  export interface TypeDeclarationProps extends BaseDeclarationProps {}
5
6
 
6
7
  export function TypeDeclaration(props: TypeDeclarationProps) {
7
8
  return (
8
- <Declaration {...props} nameKind="type">
9
- type <Name /> = {props.children};
10
- </Declaration>
9
+ <>
10
+ <Show when={Boolean(props.doc)}>
11
+ <JSDoc children={props.doc} />
12
+ <hbr />
13
+ </Show>
14
+ <Declaration {...props} nameKind="type">
15
+ type <Name /> = {props.children};
16
+ </Declaration>
17
+ </>
11
18
  );
12
19
  }
@@ -4,16 +4,18 @@ import {
4
4
  Declaration as CoreDeclaration,
5
5
  createAssignmentContext,
6
6
  Name,
7
+ Show,
7
8
  } from "@alloy-js/core";
8
9
  import { useTSNamePolicy } from "../name-policy.js";
9
10
  import { createTSSymbol } from "../symbols/index.js";
10
11
  import { BaseDeclarationProps } from "./Declaration.js";
12
+ import { JSDoc } from "./JSDoc.jsx";
11
13
 
12
14
  export interface VarDeclarationProps extends BaseDeclarationProps {
13
15
  const?: boolean;
14
16
  let?: boolean;
15
17
  var?: boolean;
16
- value?: Children;
18
+ initializer?: Children;
17
19
  type?: Children;
18
20
  }
19
21
 
@@ -27,22 +29,28 @@ export function VarDeclaration(props: VarDeclarationProps) {
27
29
  const sym = createTSSymbol({
28
30
  name: name,
29
31
  refkey: props.refkey,
30
- refkeys: props.refkeys,
31
32
  default: props.default,
32
33
  export: props.export,
34
+ metadata: props.metadata,
33
35
  });
34
36
 
35
37
  const assignmentContext = createAssignmentContext(sym);
36
38
 
37
39
  return (
38
- <CoreDeclaration symbol={sym}>
39
- {props.export ? "export " : ""}
40
- {props.default ? "default " : ""}
41
- {keyword} <Name />
42
- {type} ={" "}
43
- <AssignmentContext.Provider value={assignmentContext}>
44
- {props.value ?? props.children}
45
- </AssignmentContext.Provider>
46
- </CoreDeclaration>
40
+ <>
41
+ <Show when={Boolean(props.doc)}>
42
+ <JSDoc children={props.doc} />
43
+ <hbr />
44
+ </Show>
45
+ <CoreDeclaration symbol={sym}>
46
+ {props.export ? "export " : ""}
47
+ {props.default ? "default " : ""}
48
+ {keyword} <Name />
49
+ {type} ={" "}
50
+ <AssignmentContext.Provider value={assignmentContext}>
51
+ {props.initializer ?? props.children}
52
+ </AssignmentContext.Provider>
53
+ </CoreDeclaration>
54
+ </>
47
55
  );
48
56
  }
@@ -1,5 +1,8 @@
1
+ export * from "../parameter-descriptor.js";
1
2
  export * from "./ArrayExpression.js";
2
3
  export * from "./BarrelFile.js";
4
+ export * from "./BlockScope.js";
5
+ export * from "./CallSignature.jsx";
3
6
  export * from "./ClassDeclaration.jsx";
4
7
  export * from "./CommaList.jsx";
5
8
  export * from "./Declaration.js";
@@ -10,12 +13,16 @@ export * from "./FunctionCallExpression.js";
10
13
  export * from "./FunctionDeclaration.js";
11
14
  export * from "./ImportStatement.js";
12
15
  export * from "./Interface.js";
16
+ export * from "./JSDoc.jsx";
17
+ export * from "./JSDocComment.jsx";
18
+ export * from "./JSDocExample.jsx";
13
19
  export * from "./MemberChainExpression.jsx";
14
20
  export * from "./ObjectExpression.js";
15
21
  export * from "./PackageDirectory.js";
16
22
  export * from "./PackageJson.js";
17
23
  export * from "./Reference.js";
18
24
  export * from "./SourceFile.js";
25
+ export * from "./SwitchStatement.jsx";
19
26
  export * from "./TsConfigJson.js";
20
27
  export * from "./TypeDeclaration.js";
21
28
  export * from "./ValueExpression.js";
@@ -3,10 +3,12 @@ import * as base from "../index.js";
3
3
 
4
4
  export const ArrayExpression = stc(base.ArrayExpression);
5
5
  export const BarrelFile = stc(base.BarrelFile);
6
+ export const CallSignature = stc(base.CallSignature);
6
7
  export const ClassDeclaration = stc(base.ClassDeclaration);
7
8
  export const ClassField = stc(base.ClassField);
8
9
  export const ClassMember = stc(base.ClassMember);
9
10
  export const ClassMethod = stc(base.ClassMethod);
11
+ export const CommaList = stc(base.CommaList);
10
12
  export const Declaration = stc(base.Declaration);
11
13
  export const EnumDeclaration = stc(base.EnumDeclaration);
12
14
  export const EnumMember = stc(base.EnumMember);
@@ -18,13 +20,18 @@ export const ImportStatements = stc(base.ImportStatements);
18
20
  export const InterfaceDeclaration = stc(base.InterfaceDeclaration);
19
21
  export const InterfaceExpression = stc(base.InterfaceExpression);
20
22
  export const InterfaceMember = stc(base.InterfaceMember);
21
- export const MemberchainExpression = stc(base.MemberChainExpression);
23
+ export const JSDoc = stc(base.JSDoc);
24
+ export const JSDocExample = stc(base.JSDocExample);
25
+ export const JSDocComment = stc(base.JSDocComment);
26
+ export const MemberChainExpression = stc(base.MemberChainExpression);
22
27
  export const ObjectExpression = stc(base.ObjectExpression);
23
28
  export const ObjectProperty = stc(base.ObjectProperty);
24
29
  export const PackageDirectory = stc(base.PackageDirectory);
25
30
  export const PackageJsonFile = stc(base.PackageJsonFile);
26
31
  export const Reference = stc(base.Reference);
27
32
  export const SourceFile = stc(base.SourceFile);
33
+ export const SwitchStatement = stc(base.SwitchStatement);
34
+ export const CaseClause = stc(base.CaseClause);
28
35
  export const TSConfigJson = stc(base.TSConfigJson);
29
36
  export const TypeDeclaration = stc(base.TypeDeclaration);
30
37
  export const ValueExpression = stc(base.ValueExpression);
@@ -0,0 +1 @@
1
+ export * from "./private-scope.js";
@@ -0,0 +1,23 @@
1
+ import { ComponentContext, createContext, useContext } from "@alloy-js/core";
2
+ import { TSMemberScope } from "../symbols/ts-member-scope.js";
3
+
4
+ export interface PrivateScopeContext {
5
+ /**
6
+ * Scope for private static members.
7
+ */
8
+ staticMembers: TSMemberScope;
9
+ /**
10
+ * Scope for private instance members.
11
+ */
12
+ instanceMembers: TSMemberScope;
13
+ }
14
+
15
+ /**
16
+ * Provides scopes for instance and static private members.
17
+ */
18
+ export const PrivateScopeContext: ComponentContext<PrivateScopeContext> =
19
+ createContext<PrivateScopeContext>();
20
+
21
+ export function usePrivateScope() {
22
+ return useContext(PrivateScopeContext);
23
+ }
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./builtins/index.js";
2
2
  export * from "./components/index.js";
3
+ export * from "./context/index.js";
3
4
  export * from "./create-package.js";
4
5
  export * from "./name-conflict-resolver.js";
5
6
  export * from "./name-policy.js";
@@ -0,0 +1,71 @@
1
+ import { Children, Refkey } from "@alloy-js/core";
2
+
3
+ /**
4
+ * Information for a TypeScript function parameter.
5
+ */
6
+ export interface ParameterDescriptor {
7
+ /**
8
+ * The name of the parameter.
9
+ */
10
+ name: string;
11
+
12
+ /**
13
+ * The type of the parameter.
14
+ */
15
+ type?: Children;
16
+
17
+ /**
18
+ * The default value of the parameter.
19
+ */
20
+ default?: Children;
21
+
22
+ /**
23
+ * The refkey for this parameter.
24
+ */
25
+ refkey?: Refkey | Refkey[];
26
+
27
+ /**
28
+ * Whether the parameter is optional.
29
+ */
30
+ optional?: boolean;
31
+
32
+ /**
33
+ * Arbitrary metadata for the parameter symbol.
34
+ */
35
+ metadata?: Record<string, unknown>;
36
+
37
+ /**
38
+ * Documentation for the parameter.
39
+ */
40
+ doc?: Children;
41
+ }
42
+
43
+ /**
44
+ * Information for a TypeScript generic type parameter.
45
+ */
46
+ export interface TypeParameterDescriptor {
47
+ /**
48
+ * The name of the type parameter.
49
+ */
50
+ name: string;
51
+
52
+ /**
53
+ * The extends constraint for the type parameter.
54
+ */
55
+ extends?: Children;
56
+
57
+ /**
58
+ * The default type of the type parameter.
59
+ */
60
+ default?: Children;
61
+
62
+ /**
63
+ * A refkey or array of refkeys for this type parameter.
64
+ */
65
+ refkey?: Refkey | Refkey[];
66
+
67
+ /**
68
+ * Arbitrary metadata for the type parameter symbol.
69
+ */
70
+ metadata?: Record<string, unknown>;
71
+ }
@@ -5,13 +5,19 @@ import {
5
5
  resolve,
6
6
  untrack,
7
7
  useContext,
8
+ useMemberScope,
8
9
  } from "@alloy-js/core";
9
10
  import { usePackage } from "../components/PackageDirectory.jsx";
10
11
  import { SourceFileContext } from "../components/SourceFile.jsx";
12
+ import {
13
+ PrivateScopeContext,
14
+ usePrivateScope,
15
+ } from "../context/private-scope.js";
16
+ import { isValidJSIdentifier } from "../utils.js";
11
17
  import { TSOutputScope } from "./scopes.js";
12
18
  import { TSMemberScope } from "./ts-member-scope.js";
13
19
  import { TSModuleScope } from "./ts-module-scope.js";
14
- import { TSOutputSymbol } from "./ts-output-symbol.js";
20
+ import { TSOutputSymbol, TSSymbolFlags } from "./ts-output-symbol.js";
15
21
  import { TSPackageScope } from "./ts-package-scope.js";
16
22
 
17
23
  export function ref(refkey: Refkey): () => string {
@@ -19,6 +25,8 @@ export function ref(refkey: Refkey): () => string {
19
25
  const resolveResult = resolve<TSOutputScope, TSOutputSymbol>(
20
26
  refkey as Refkey,
21
27
  );
28
+ const currentScope = useMemberScope();
29
+ const currentPrivateScope = usePrivateScope();
22
30
 
23
31
  return memo(() => {
24
32
  if (resolveResult.value === undefined) {
@@ -27,7 +35,25 @@ export function ref(refkey: Refkey): () => string {
27
35
 
28
36
  const { targetDeclaration, pathDown, memberPath } = resolveResult.value;
29
37
 
30
- validateSymbolReachable(pathDown);
38
+ // if we resolved a instance member, check if we should be able to access
39
+ // it.
40
+ if (targetDeclaration.flags & OutputSymbolFlags.InstanceMember) {
41
+ if (targetDeclaration.tsFlags & TSSymbolFlags.PrivateMember) {
42
+ if (currentPrivateScope?.instanceMembers !== targetDeclaration.scope) {
43
+ throw new Error(
44
+ "Cannot resolve private member symbols from a different scope",
45
+ );
46
+ }
47
+ } else {
48
+ if (currentScope?.instanceMembers !== targetDeclaration.scope) {
49
+ throw new Error(
50
+ "Cannot resolve member symbols from a different member scope",
51
+ );
52
+ }
53
+ }
54
+ }
55
+
56
+ validateSymbolReachable(pathDown, memberPath, currentPrivateScope);
31
57
 
32
58
  // Where the target declaration is relative to the referencing scope.
33
59
  // * package: target symbol is in a different package
@@ -87,32 +113,42 @@ export function ref(refkey: Refkey): () => string {
87
113
  memberPath[0] = localSymbol;
88
114
  }
89
115
 
90
- return addThisPrefix(
91
- targetDeclaration,
92
- buildMemberExpression(memberPath),
93
- );
116
+ return buildMemberExpression(memberPath);
94
117
  } else {
95
- return addThisPrefix(
96
- localSymbol ?? targetDeclaration,
97
- localSymbol ? localSymbol.name : targetDeclaration.name,
98
- );
118
+ return buildMemberExpression([localSymbol ?? targetDeclaration]);
99
119
  }
100
120
  });
101
121
  }
102
122
 
103
- function addThisPrefix(rootSymbol: TSOutputSymbol, name: string) {
104
- if (rootSymbol.flags & OutputSymbolFlags.InstanceMember) {
105
- return `this.${name}`;
123
+ function buildMemberExpression(path: TSOutputSymbol[]) {
124
+ let memberExpr = "";
125
+
126
+ const base = path[0];
127
+ if (base.flags & OutputSymbolFlags.InstanceMember) {
128
+ memberExpr += "this";
106
129
  } else {
107
- return name;
130
+ memberExpr += base.name;
131
+ path = path.slice(1);
108
132
  }
109
- }
110
133
 
111
- function buildMemberExpression(symbolPath: TSOutputSymbol[]) {
112
- return symbolPath.map((sym) => sym.name).join(".");
134
+ for (const sym of path) {
135
+ if (sym.tsFlags & TSSymbolFlags.PrivateMember) {
136
+ memberExpr += `.#${sym.name}`;
137
+ } else if (isValidJSIdentifier(sym.name)) {
138
+ memberExpr += `.${sym.name}`;
139
+ } else {
140
+ memberExpr += `[${JSON.stringify(sym.name)}]`;
141
+ }
142
+ }
143
+
144
+ return memberExpr;
113
145
  }
114
146
 
115
- function validateSymbolReachable(path: TSOutputScope[]) {
147
+ function validateSymbolReachable(
148
+ path: TSOutputScope[],
149
+ memberPath: TSOutputSymbol[] | undefined,
150
+ currentPrivateScope: PrivateScopeContext | undefined,
151
+ ) {
116
152
  for (const scope of path) {
117
153
  if (scope.kind === "function") {
118
154
  throw new Error(
@@ -120,4 +156,20 @@ function validateSymbolReachable(path: TSOutputScope[]) {
120
156
  );
121
157
  }
122
158
  }
159
+
160
+ if (memberPath) {
161
+ for (const sym of memberPath) {
162
+ // make sure we're not trying to access a static private from outside
163
+ // the static member scope.
164
+ if (
165
+ sym.flags & OutputSymbolFlags.StaticMember &&
166
+ sym.tsFlags & TSSymbolFlags.PrivateMember &&
167
+ currentPrivateScope?.staticMembers !== sym.scope
168
+ ) {
169
+ throw new Error(
170
+ "Cannot resolve private static member symbols from a different scope",
171
+ );
172
+ }
173
+ }
174
+ }
123
175
  }
@@ -1,4 +1,4 @@
1
- import { Binder, OutputScope } from "@alloy-js/core";
1
+ import { Binder, OutputScope, OutputScopeFlags } from "@alloy-js/core";
2
2
  import { TSOutputSymbol } from "./ts-output-symbol.js";
3
3
 
4
4
  export interface TSMemberScope extends OutputScope {
@@ -9,13 +9,17 @@ export interface TSMemberScope extends OutputScope {
9
9
 
10
10
  export function createTSMemberScope(
11
11
  binder: Binder,
12
- parent: OutputScope,
12
+ parent: OutputScope | undefined,
13
13
  owner: TSOutputSymbol,
14
14
  isStatic: boolean = false,
15
15
  ): TSMemberScope {
16
16
  return binder.createScope<TSMemberScope>({
17
17
  kind: "member",
18
18
  name: "members",
19
+ flags:
20
+ isStatic ?
21
+ OutputScopeFlags.StaticMemberScope
22
+ : OutputScopeFlags.MemberScope,
19
23
  owner,
20
24
  parent,
21
25
  isStatic,
@@ -6,14 +6,16 @@ import {
6
6
  useDefaultScope,
7
7
  } from "@alloy-js/core";
8
8
  import { TSOutputScope } from "./scopes.js";
9
- import { TSMemberScope } from "./ts-member-scope.js";
9
+ import { createTSMemberScope, TSMemberScope } from "./ts-member-scope.js";
10
10
 
11
11
  // prettier-ignore
12
12
  export enum TSSymbolFlags {
13
- None = 0,
14
- LocalImportSymbol = 1 << 0,
15
- TypeSymbol = 1 << 1,
16
- ParameterSymbol = 1 << 2,
13
+ None = 0,
14
+ LocalImportSymbol = 1 << 0,
15
+ TypeSymbol = 1 << 1,
16
+ ParameterSymbol = 1 << 2,
17
+ PrivateMember = 1 << 3,
18
+ PrivateMemberContainer = 1 << 4,
17
19
  }
18
20
 
19
21
  export interface TSOutputSymbol extends OutputSymbol {
@@ -21,19 +23,20 @@ export interface TSOutputSymbol extends OutputSymbol {
21
23
  export: boolean;
22
24
  default: boolean;
23
25
  tsFlags: TSSymbolFlags;
24
- memberScope?: TSMemberScope;
26
+ privateMemberScope?: TSMemberScope;
27
+ privateStaticMemberScope?: TSMemberScope;
25
28
  }
26
29
 
27
30
  export interface createTsSymbolOptions {
28
31
  name: string;
29
- refkey?: Refkey;
30
- refkeys?: Refkey[];
32
+ refkey?: Refkey | Refkey[];
31
33
  binder?: Binder;
32
34
  scope?: TSOutputScope;
33
35
  export?: boolean;
34
36
  default?: boolean;
35
37
  flags?: OutputSymbolFlags;
36
38
  tsFlags?: TSSymbolFlags;
39
+ metadata?: Record<string, unknown>;
37
40
  }
38
41
 
39
42
  export function createTSSymbol(options: createTsSymbolOptions): TSOutputSymbol {
@@ -45,18 +48,28 @@ export function createTSSymbol(options: createTsSymbolOptions): TSOutputSymbol {
45
48
  }
46
49
 
47
50
  const binder = scope.binder;
48
-
51
+ const tsFlags = options.tsFlags ?? TSSymbolFlags.None;
49
52
  const sym = binder.createSymbol<TSOutputSymbol>({
50
53
  name: options.name,
51
54
  scope,
52
55
  refkey: options.refkey,
53
- refkeys: options.refkeys,
54
56
  export: !!options.export,
55
57
  default: !!options.default,
56
58
  flags: options.flags ?? OutputSymbolFlags.None,
57
- tsFlags: options.tsFlags ?? TSSymbolFlags.None,
59
+ tsFlags,
60
+ metadata: options.metadata,
58
61
  });
59
62
 
63
+ if (tsFlags & TSSymbolFlags.PrivateMemberContainer) {
64
+ sym.privateMemberScope = createTSMemberScope(binder, undefined, sym);
65
+ sym.privateStaticMemberScope = createTSMemberScope(
66
+ binder,
67
+ undefined,
68
+ sym,
69
+ true,
70
+ );
71
+ }
72
+
60
73
  if (options.export && scope.kind === "module") {
61
74
  for (const refkey of sym.refkeys) {
62
75
  scope.exportedSymbols.set(refkey, sym);
package/src/utils.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { defaultProps, splitProps } from "@alloy-js/core/jsx-runtime";
2
+ import { CallSignatureProps } from "./components/CallSignature.jsx";
3
+
1
4
  export function modulePath(path: string) {
2
5
  if (path[0] !== ".") {
3
6
  path = "./" + path;
@@ -5,3 +8,33 @@ export function modulePath(path: string) {
5
8
 
6
9
  return path.replace(/\.ts$/, ".js");
7
10
  }
11
+
12
+ /**
13
+ * Extract only the call signature props from a props object which extends
14
+ * `CallSignatureProps`. You can provide default values for the props.
15
+ */
16
+ export function getCallSignatureProps(
17
+ props: CallSignatureProps,
18
+ defaults?: Partial<CallSignatureProps>,
19
+ ) {
20
+ const [callSignatureProps] = splitProps(props, [
21
+ "parameters",
22
+ "parametersChildren",
23
+ "typeParameters",
24
+ "typeParametersChildren",
25
+ "returnType",
26
+ ]);
27
+
28
+ if (!defaults) {
29
+ return callSignatureProps;
30
+ }
31
+
32
+ return defaultProps(callSignatureProps, defaults);
33
+ }
34
+
35
+ const identifierRegex =
36
+ /^(?:[\p{ID_Start}$_])(?:[\p{ID_Continue}$\u200C\u200D])*$/u;
37
+
38
+ export function isValidJSIdentifier(str: string) {
39
+ return identifierRegex.test(str);
40
+ }