@alloy-js/csharp 0.20.0-dev.2 → 0.20.0-dev.4

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/dist/src/components/Declaration.d.ts +2 -2
  2. package/dist/src/components/Declaration.d.ts.map +1 -1
  3. package/dist/src/components/Declaration.js +3 -13
  4. package/dist/src/components/Reference.d.ts.map +1 -1
  5. package/dist/src/components/Reference.js +5 -1
  6. package/dist/src/components/SourceFile.d.ts +8 -9
  7. package/dist/src/components/SourceFile.d.ts.map +1 -1
  8. package/dist/src/components/SourceFile.js +69 -45
  9. package/dist/src/components/UsingDirective.d.ts +5 -1
  10. package/dist/src/components/UsingDirective.d.ts.map +1 -1
  11. package/dist/src/components/UsingDirective.js +3 -1
  12. package/dist/src/components/class/declaration.d.ts +2 -2
  13. package/dist/src/components/class/declaration.d.ts.map +1 -1
  14. package/dist/src/components/class/declaration.js +19 -33
  15. package/dist/src/components/class/declaration.test.js +192 -193
  16. package/dist/src/components/constructor/constructor.d.ts.map +1 -1
  17. package/dist/src/components/constructor/constructor.js +11 -19
  18. package/dist/src/components/constructor/constructor.test.js +10 -19
  19. package/dist/src/components/{EnumDeclaration.d.ts → enum/declaration.d.ts} +6 -11
  20. package/dist/src/components/enum/declaration.d.ts.map +1 -0
  21. package/dist/src/components/enum/declaration.js +54 -0
  22. package/dist/src/components/enum/declaration.ref.test.d.ts +2 -0
  23. package/dist/src/components/enum/declaration.ref.test.d.ts.map +1 -0
  24. package/dist/src/components/enum/declaration.ref.test.js +120 -0
  25. package/dist/src/components/enum/declaration.test.d.ts +2 -0
  26. package/dist/src/components/enum/declaration.test.d.ts.map +1 -0
  27. package/dist/src/components/enum/declaration.test.js +39 -0
  28. package/dist/src/components/enum/member.d.ts +7 -0
  29. package/dist/src/components/enum/member.d.ts.map +1 -0
  30. package/dist/src/components/enum/member.js +29 -0
  31. package/dist/src/components/enum/member.test.d.ts +2 -0
  32. package/dist/src/components/enum/member.test.d.ts.map +1 -0
  33. package/dist/src/components/enum/member.test.js +28 -0
  34. package/dist/src/components/field/field.d.ts.map +1 -1
  35. package/dist/src/components/field/field.js +9 -17
  36. package/dist/src/components/field/field.test.js +7 -7
  37. package/dist/src/components/index.d.ts +5 -2
  38. package/dist/src/components/index.d.ts.map +1 -1
  39. package/dist/src/components/index.js +5 -2
  40. package/dist/src/components/interface/declaration.d.ts.map +1 -1
  41. package/dist/src/components/interface/declaration.js +19 -21
  42. package/dist/src/components/interface/declaration.test.js +26 -35
  43. package/dist/src/components/interface/method.d.ts.map +1 -1
  44. package/dist/src/components/interface/method.js +7 -18
  45. package/dist/src/components/interface/method.test.js +38 -47
  46. package/dist/src/components/interface/property.d.ts.map +1 -1
  47. package/dist/src/components/interface/property.js +21 -39
  48. package/dist/src/components/interface/property.test.js +11 -11
  49. package/dist/src/components/lexical-scope.d.ts +8 -0
  50. package/dist/src/components/lexical-scope.d.ts.map +1 -0
  51. package/dist/src/components/lexical-scope.js +24 -0
  52. package/dist/src/components/method/method.d.ts +5 -2
  53. package/dist/src/components/method/method.d.ts.map +1 -1
  54. package/dist/src/components/method/method.js +7 -16
  55. package/dist/src/components/method/method.test.js +11 -11
  56. package/dist/src/components/method-scope.d.ts +8 -0
  57. package/dist/src/components/method-scope.d.ts.map +1 -0
  58. package/dist/src/components/method-scope.js +24 -0
  59. package/dist/src/components/namespace-scopes.d.ts +13 -0
  60. package/dist/src/components/namespace-scopes.d.ts.map +1 -0
  61. package/dist/src/components/namespace-scopes.js +44 -0
  62. package/dist/src/components/namespace.d.ts +7 -0
  63. package/dist/src/components/namespace.d.ts.map +1 -0
  64. package/dist/src/components/namespace.js +46 -0
  65. package/dist/src/components/namespace.ref.test.d.ts +2 -0
  66. package/dist/src/components/namespace.ref.test.d.ts.map +1 -0
  67. package/dist/src/components/namespace.ref.test.js +181 -0
  68. package/dist/src/components/namespace.test.d.ts.map +1 -0
  69. package/dist/src/components/namespace.test.js +78 -0
  70. package/dist/src/components/parameters/parameters.d.ts +2 -2
  71. package/dist/src/components/parameters/parameters.d.ts.map +1 -1
  72. package/dist/src/components/parameters/parameters.js +12 -14
  73. package/dist/src/components/parameters/parameters.test.d.ts +2 -0
  74. package/dist/src/components/parameters/parameters.test.d.ts.map +1 -0
  75. package/dist/src/components/parameters/parameters.test.js +87 -0
  76. package/dist/src/components/property/property.d.ts.map +1 -1
  77. package/dist/src/components/property/property.js +21 -39
  78. package/dist/src/components/property/property.test.js +14 -14
  79. package/dist/src/components/record/declaration.d.ts +18 -0
  80. package/dist/src/components/record/declaration.d.ts.map +1 -1
  81. package/dist/src/components/record/declaration.js +19 -13
  82. package/dist/src/components/record/declaration.test.js +50 -3
  83. package/dist/src/components/struct/declaration.d.ts.map +1 -1
  84. package/dist/src/components/struct/declaration.js +4 -6
  85. package/dist/src/components/struct/declaration.test.js +30 -39
  86. package/dist/src/components/type-parameters/type-parameter.d.ts.map +1 -1
  87. package/dist/src/components/type-parameters/type-parameter.js +7 -10
  88. package/dist/src/components/type-parameters/type-parameters.test.js +3 -3
  89. package/dist/src/components/var/declaration.d.ts.map +1 -1
  90. package/dist/src/components/var/declaration.js +6 -8
  91. package/dist/src/components/var/declaration.test.js +11 -20
  92. package/dist/src/contexts/global-namespace.d.ts +5 -0
  93. package/dist/src/contexts/global-namespace.d.ts.map +1 -0
  94. package/dist/src/contexts/global-namespace.js +24 -0
  95. package/dist/src/contexts/namespace.d.ts +8 -0
  96. package/dist/src/contexts/namespace.d.ts.map +1 -0
  97. package/dist/src/contexts/namespace.js +5 -0
  98. package/dist/src/index.d.ts +1 -0
  99. package/dist/src/index.d.ts.map +1 -1
  100. package/dist/src/index.js +1 -0
  101. package/dist/src/modifiers.d.ts +11 -0
  102. package/dist/src/modifiers.d.ts.map +1 -1
  103. package/dist/src/scopes/class.d.ts +10 -0
  104. package/dist/src/scopes/class.d.ts.map +1 -0
  105. package/dist/src/scopes/class.js +10 -0
  106. package/dist/src/scopes/contexts.d.ts +9 -0
  107. package/dist/src/scopes/contexts.d.ts.map +1 -0
  108. package/dist/src/scopes/contexts.js +33 -0
  109. package/dist/src/scopes/csharp.d.ts +8 -0
  110. package/dist/src/scopes/csharp.d.ts.map +1 -0
  111. package/dist/src/scopes/csharp.js +11 -0
  112. package/dist/src/scopes/factories.d.ts +9 -0
  113. package/dist/src/scopes/factories.d.ts.map +1 -0
  114. package/dist/src/scopes/factories.js +23 -0
  115. package/dist/src/scopes/index.d.ts +10 -0
  116. package/dist/src/scopes/index.d.ts.map +1 -0
  117. package/dist/src/scopes/index.js +9 -0
  118. package/dist/src/scopes/lexical.d.ts +7 -0
  119. package/dist/src/scopes/lexical.d.ts.map +1 -0
  120. package/dist/src/scopes/lexical.js +7 -0
  121. package/dist/src/scopes/method.d.ts +9 -0
  122. package/dist/src/scopes/method.d.ts.map +1 -0
  123. package/dist/src/scopes/method.js +13 -0
  124. package/dist/src/scopes/named-type.d.ts +22 -0
  125. package/dist/src/scopes/named-type.d.ts.map +1 -0
  126. package/dist/src/scopes/named-type.js +32 -0
  127. package/dist/src/scopes/namespace.d.ts +11 -0
  128. package/dist/src/scopes/namespace.d.ts.map +1 -0
  129. package/dist/src/scopes/namespace.js +38 -0
  130. package/dist/src/scopes/source-file.d.ts +16 -0
  131. package/dist/src/scopes/source-file.d.ts.map +1 -0
  132. package/dist/src/scopes/source-file.js +43 -0
  133. package/dist/src/symbols/csharp.d.ts +76 -0
  134. package/dist/src/symbols/csharp.d.ts.map +1 -0
  135. package/dist/src/symbols/csharp.js +222 -0
  136. package/dist/src/symbols/factories.d.ts +25 -0
  137. package/dist/src/symbols/factories.d.ts.map +1 -0
  138. package/dist/src/symbols/factories.js +86 -0
  139. package/dist/src/symbols/index.d.ts +5 -2
  140. package/dist/src/symbols/index.d.ts.map +1 -1
  141. package/dist/src/symbols/index.js +6 -3
  142. package/dist/src/symbols/method.d.ts +13 -0
  143. package/dist/src/symbols/method.d.ts.map +1 -0
  144. package/dist/src/symbols/method.js +15 -0
  145. package/dist/src/symbols/named-type.d.ts +19 -0
  146. package/dist/src/symbols/named-type.d.ts.map +1 -0
  147. package/dist/src/symbols/named-type.js +30 -0
  148. package/dist/src/symbols/namespace.d.ts +22 -0
  149. package/dist/src/symbols/namespace.d.ts.map +1 -0
  150. package/dist/src/symbols/namespace.js +44 -0
  151. package/dist/src/symbols/reference.d.ts +2 -2
  152. package/dist/src/symbols/reference.d.ts.map +1 -1
  153. package/dist/src/symbols/reference.js +36 -43
  154. package/dist/test/project-directory.test.js +12 -16
  155. package/dist/test/sourcefile.test.js +7 -19
  156. package/dist/test/using.test.js +18 -16
  157. package/dist/test/utils.js +2 -2
  158. package/dist/tsconfig.tsbuildinfo +1 -1
  159. package/package.json +8 -4
  160. package/src/components/Declaration.tsx +3 -6
  161. package/src/components/Reference.tsx +3 -1
  162. package/src/components/SourceFile.tsx +68 -55
  163. package/src/components/UsingDirective.tsx +12 -2
  164. package/src/components/class/declaration.test.tsx +138 -143
  165. package/src/components/class/declaration.tsx +20 -33
  166. package/src/components/constructor/constructor.test.tsx +10 -16
  167. package/src/components/constructor/constructor.tsx +13 -32
  168. package/src/components/enum/declaration.ref.test.tsx +93 -0
  169. package/src/components/enum/declaration.test.tsx +34 -0
  170. package/src/components/enum/declaration.tsx +65 -0
  171. package/src/components/enum/member.test.tsx +24 -0
  172. package/src/components/enum/member.tsx +36 -0
  173. package/src/components/field/field.test.tsx +7 -7
  174. package/src/components/field/field.tsx +12 -22
  175. package/src/components/index.ts +5 -2
  176. package/src/components/interface/declaration.test.tsx +26 -32
  177. package/src/components/interface/declaration.tsx +16 -18
  178. package/src/components/interface/method.test.tsx +38 -44
  179. package/src/components/interface/method.tsx +7 -20
  180. package/src/components/interface/property.test.tsx +11 -11
  181. package/src/components/interface/property.tsx +15 -34
  182. package/src/components/lexical-scope.tsx +29 -0
  183. package/src/components/method/method.test.tsx +11 -11
  184. package/src/components/method/method.tsx +12 -23
  185. package/src/components/method-scope.tsx +27 -0
  186. package/src/components/namespace-scopes.tsx +44 -0
  187. package/src/components/namespace.ref.test.tsx +139 -0
  188. package/src/components/namespace.test.tsx +52 -0
  189. package/src/components/namespace.tsx +45 -0
  190. package/src/components/parameters/parameters.test.tsx +71 -0
  191. package/src/components/parameters/parameters.tsx +9 -21
  192. package/src/components/property/property.test.tsx +14 -14
  193. package/src/components/property/property.tsx +17 -41
  194. package/src/components/record/declaration.test.tsx +47 -3
  195. package/src/components/record/declaration.tsx +34 -11
  196. package/src/components/struct/declaration.test.tsx +16 -22
  197. package/src/components/struct/declaration.tsx +4 -6
  198. package/src/components/type-parameters/type-parameter.tsx +14 -11
  199. package/src/components/type-parameters/type-parameters.test.tsx +4 -4
  200. package/src/components/var/declaration.test.tsx +8 -14
  201. package/src/components/var/declaration.tsx +12 -6
  202. package/src/contexts/global-namespace.ts +29 -0
  203. package/src/contexts/namespace.ts +13 -0
  204. package/src/index.ts +1 -0
  205. package/src/modifiers.ts +14 -0
  206. package/src/scopes/class.ts +12 -0
  207. package/src/scopes/contexts.ts +45 -0
  208. package/src/scopes/csharp.ts +18 -0
  209. package/src/scopes/factories.ts +45 -0
  210. package/src/scopes/index.ts +9 -0
  211. package/src/scopes/lexical.ts +10 -0
  212. package/src/scopes/method.ts +22 -0
  213. package/src/scopes/named-type.ts +44 -0
  214. package/src/scopes/namespace.ts +59 -0
  215. package/src/scopes/source-file.ts +67 -0
  216. package/src/symbols/csharp.ts +307 -0
  217. package/src/symbols/factories.ts +199 -0
  218. package/src/symbols/index.ts +5 -2
  219. package/src/symbols/method.ts +26 -0
  220. package/src/symbols/named-type.ts +54 -0
  221. package/src/symbols/namespace.ts +62 -0
  222. package/src/symbols/reference.ts +47 -50
  223. package/temp/api.json +6152 -2151
  224. package/test/project-directory.test.tsx +12 -16
  225. package/test/sourcefile.test.tsx +7 -21
  226. package/test/using.test.tsx +18 -16
  227. package/test/utils.tsx +1 -1
  228. package/tsdoc-metadata.json +1 -1
  229. package/dist/src/components/EnumDeclaration.d.ts.map +0 -1
  230. package/dist/src/components/EnumDeclaration.js +0 -85
  231. package/dist/src/components/Namespace.d.ts +0 -11
  232. package/dist/src/components/Namespace.d.ts.map +0 -1
  233. package/dist/src/components/Namespace.js +0 -33
  234. package/dist/src/symbols/csharp-output-symbol.d.ts +0 -8
  235. package/dist/src/symbols/csharp-output-symbol.d.ts.map +0 -1
  236. package/dist/src/symbols/csharp-output-symbol.js +0 -19
  237. package/dist/src/symbols/scopes.d.ts +0 -14
  238. package/dist/src/symbols/scopes.d.ts.map +0 -1
  239. package/dist/src/symbols/scopes.js +0 -31
  240. package/dist/test/enum.test.d.ts +0 -2
  241. package/dist/test/enum.test.d.ts.map +0 -1
  242. package/dist/test/enum.test.js +0 -178
  243. package/dist/test/namespace.test.d.ts.map +0 -1
  244. package/dist/test/namespace.test.js +0 -81
  245. package/src/components/EnumDeclaration.tsx +0 -99
  246. package/src/components/Namespace.tsx +0 -35
  247. package/src/symbols/csharp-output-symbol.ts +0 -22
  248. package/src/symbols/scopes.ts +0 -41
  249. package/test/enum.test.tsx +0 -149
  250. package/test/namespace.test.tsx +0 -55
  251. /package/dist/{test → src/components}/namespace.test.d.ts +0 -0
@@ -1,18 +1,15 @@
1
1
  import * as core from "@alloy-js/core";
2
- import { CSharpOutputSymbol } from "../symbols/csharp-output-symbol.js";
3
2
 
4
3
  // properties for creating a declaration
5
4
  export interface DeclarationProps {
6
5
  name: string;
7
- refkey?: core.Refkey;
6
+ refkey?: core.Refkey | core.Refkey[];
8
7
  children?: core.Children;
9
8
  }
10
9
 
11
10
  // declares a symbol in the program (class, enum, interface etc)
12
11
  export function Declaration(props: DeclarationProps) {
13
- const sym = new CSharpOutputSymbol(props.name, {
14
- refkeys: props.refkey,
15
- });
12
+ throw new Error("Not supported");
16
13
 
17
- return <core.Declaration symbol={sym}>{props.children}</core.Declaration>;
14
+ //return <core.Declaration symbol={sym}>{props.children}</core.Declaration>;
18
15
  }
@@ -8,6 +8,8 @@ export interface ReferenceProps {
8
8
  // used to resolve refkey references within source files
9
9
  export function Reference({ refkey }: ReferenceProps) {
10
10
  const reference = ref(refkey);
11
+ const symbolRef = core.computed(() => reference()[1]);
11
12
 
12
- return <>{reference}</>;
13
+ core.emitSymbol(symbolRef);
14
+ return <>{reference()[0]}</>;
13
15
  }
@@ -1,78 +1,91 @@
1
- import * as core from "@alloy-js/core";
2
- import { useNamespace } from "./Namespace.jsx";
1
+ import {
2
+ Children,
3
+ computed,
4
+ SourceFile as CoreSourceFile,
5
+ Scope,
6
+ Show,
7
+ useBinder,
8
+ } from "@alloy-js/core";
9
+ import { getGlobalNamespace } from "../contexts/global-namespace.js";
10
+ import { useNamespaceContext } from "../contexts/namespace.js";
11
+ import { CSharpSourceFileScope } from "../scopes/source-file.js";
12
+ import { NamespaceSymbol } from "../symbols/namespace.js";
13
+ import { NamespaceScope, NamespaceScopes } from "./namespace-scopes.jsx";
3
14
  import { Reference } from "./Reference.jsx";
4
15
  import { UsingDirective } from "./UsingDirective.jsx";
5
16
 
6
- // contains the info for the current source file
7
- export interface SourceFileContext {
8
- // adds a namespace to the array of using statements
9
- addUsing(namespace: string): void;
10
- }
11
-
12
- const SourceFileContext = core.createContext<SourceFileContext>();
13
-
14
- // returns the current source file
15
- export function useSourceFile(): SourceFileContext | undefined {
16
- return core.useContext(SourceFileContext) as SourceFileContext;
17
- }
18
-
19
17
  export interface SourceFileProps {
20
18
  /** Path of the source file */
21
19
  path: string;
22
20
 
23
- /** A list of namespaces to use */
24
- using?: Array<string>;
21
+ children?: Children;
25
22
 
26
- children?: core.Children;
23
+ /**
24
+ * A list of using directives to explicitly include. Note that providing
25
+ * explicit usings is not necessary when referencing symbols via refkeys.
26
+ */
27
+ using?: string[];
27
28
  }
28
29
 
29
30
  // a C# source file. exists within the context of a namespace
30
31
  // contains using statements and declarations
31
32
  export function SourceFile(props: SourceFileProps) {
32
- const namespaceCtx = useNamespace();
33
-
34
- if (!namespaceCtx) {
35
- throw new Error("SourceFile must be declared inside a namespace");
36
- }
37
-
38
- const using: Array<string> = core.reactive(new Array<string>());
39
- if (props.using) {
40
- using.push(...props.using);
41
- }
42
-
43
- // adds the specified namespace to the array of using statements.
44
- // called via SourceFileContext.addUsing when resolving refkeys.
45
- function addUsing(namespace: string): void {
46
- if (!using.includes(namespace)) {
47
- using.push(namespace);
48
- }
49
- }
50
-
51
- const sourceFileCtx: SourceFileContext = {
52
- addUsing,
53
- };
33
+ const sourceFileScope = new CSharpSourceFileScope(props.path);
54
34
 
35
+ const nsContext = useNamespaceContext();
36
+ const globalNs = getGlobalNamespace(useBinder());
37
+ const nsSymbol = nsContext ? nsContext.symbol : globalNs;
38
+ const nsRef = nsContext ? nsContext.symbol.name : undefined;
39
+ const usings = computed(() => {
40
+ return (
41
+ Array.from(sourceFileScope.usings) as (NamespaceSymbol | string)[]
42
+ ).concat(props.using ?? []);
43
+ });
55
44
  return (
56
- <core.SourceFile
45
+ <CoreSourceFile
57
46
  path={props.path}
58
47
  filetype="cs"
59
48
  reference={Reference}
60
49
  tabWidth={4}
61
50
  printWidth={120}
62
51
  >
63
- <SourceFileContext.Provider value={sourceFileCtx}>
64
- <core.Scope name={props.path} kind="source-file">
65
- {using.length > 0 && (
66
- <>
67
- <UsingDirective namespaces={using} />
68
- <hbr />
69
- <hbr />
70
- </>
71
- )}
72
- namespace {namespaceCtx.name}
73
- <core.Block newline>{props.children}</core.Block>
74
- </core.Scope>
75
- </SourceFileContext.Provider>
76
- </core.SourceFile>
52
+ <Scope value={sourceFileScope}>
53
+ {(sourceFileScope.usings.size > 0 ||
54
+ (props.using && props.using.length > 0)) && (
55
+ <>
56
+ <UsingDirective namespaces={usings.value} />
57
+ <hbr />
58
+ <hbr />
59
+ </>
60
+ )}
61
+ <Show when={!!nsContext && nsSymbol !== globalNs}>
62
+ <Show when={sourceFileScope.hasBlockNamespace}>
63
+ namespace {nsRef}
64
+ {" {"}
65
+ <hbr />
66
+ {" "}
67
+ </Show>
68
+
69
+ <Show when={!sourceFileScope.hasBlockNamespace}>
70
+ namespace {nsRef};<hbr />
71
+ <hbr />
72
+ </Show>
73
+ <align width={sourceFileScope.hasBlockNamespace ? 4 : 0}>
74
+ <NamespaceScopes symbol={nsContext!.symbol}>
75
+ {props.children}
76
+ </NamespaceScopes>
77
+ </align>
78
+ <Show when={sourceFileScope.hasBlockNamespace}>
79
+ <hbr />
80
+ {"}"}
81
+ </Show>
82
+ </Show>
83
+ <Show when={!nsContext || nsSymbol === globalNs}>
84
+ <NamespaceScope symbol={getGlobalNamespace(useBinder())}>
85
+ {props.children}
86
+ </NamespaceScope>
87
+ </Show>
88
+ </Scope>
89
+ </CoreSourceFile>
77
90
  );
78
91
  }
@@ -1,13 +1,23 @@
1
1
  import * as core from "@alloy-js/core";
2
+ import { NamespaceSymbol } from "../symbols/namespace.js";
2
3
 
3
4
  export interface UsingDirectiveProps {
4
- namespaces: Array<string>;
5
+ /**
6
+ * Namespace symbols or namespace names to use to generate using statements.
7
+ */
8
+ namespaces?: (NamespaceSymbol | string)[];
5
9
  }
6
10
 
7
11
  // one ore more C# using directives
8
12
  export function UsingDirective(props: UsingDirectiveProps) {
9
13
  const sortedNamespaces = core.computed(() => {
10
- return props.namespaces.sort();
14
+ return props
15
+ .namespaces!.map((ns) =>
16
+ typeof ns === "string" ? ns : (
17
+ ns.getFullyQualifiedName({ omitGlobal: true })
18
+ ),
19
+ )
20
+ .sort((n1, n2) => n1.localeCompare(n2));
11
21
  });
12
22
 
13
23
  return (
@@ -1,3 +1,6 @@
1
+ import { Constructor } from "#components/constructor/constructor.jsx";
2
+ import { EnumDeclaration } from "#components/enum/declaration.jsx";
3
+ import { EnumMember } from "#components/enum/member.jsx";
1
4
  import {
2
5
  Children,
3
6
  code,
@@ -15,19 +18,13 @@ import { createCSharpNamePolicy } from "../../name-policy.js";
15
18
  import { Attribute } from "../attributes/attributes.jsx";
16
19
  import { Field } from "../field/field.jsx";
17
20
  import { Method } from "../method/method.jsx";
18
- import { Namespace } from "../Namespace.jsx";
19
21
  import { Property } from "../property/property.jsx";
20
22
  import { SourceFile } from "../SourceFile.jsx";
21
- import { Constructor, EnumDeclaration, EnumMember } from "../stc/index.js";
22
23
  import { TypeParameterProps } from "../type-parameters/type-parameter.jsx";
23
24
  import { ClassDeclaration } from "./declaration.jsx";
24
25
 
25
26
  function Wrapper({ children }: { children: Children }) {
26
- return (
27
- <TestNamespace>
28
- <SourceFile path="Test.cs">{children}</SourceFile>
29
- </TestNamespace>
30
- );
27
+ return <TestNamespace>{children}</TestNamespace>;
31
28
  }
32
29
 
33
30
  it("declares class with no members", () => {
@@ -70,6 +67,24 @@ describe("modifiers", () => {
70
67
  public abstract partial class TestClass;
71
68
  `);
72
69
  });
70
+
71
+ it("places visibility, attributes, and modifiers in the correct order", () => {
72
+ expect(
73
+ <TestNamespace>
74
+ <ClassDeclaration
75
+ partial
76
+ public
77
+ abstract
78
+ sealed
79
+ name="TestClass"
80
+ attributes={[<Attribute name="Test" />]}
81
+ />
82
+ </TestNamespace>,
83
+ ).toRenderTo(`
84
+ [Test]
85
+ public abstract sealed partial class TestClass;
86
+ `);
87
+ });
73
88
  });
74
89
 
75
90
  describe("base", () => {
@@ -109,45 +124,43 @@ describe("base", () => {
109
124
  });
110
125
 
111
126
  it("declares class with some members", () => {
112
- const res = toSourceText(
113
- <ClassDeclaration public name="TestClass">
114
- <List>
115
- <Field public name="MemberOne" type="string" />
116
- <Field public name="MemberTwo" type="int" />
117
- </List>
118
- </ClassDeclaration>,
127
+ const tree = (
128
+ <TestNamespace>
129
+ <ClassDeclaration public name="TestClass">
130
+ <List>
131
+ <Field public name="MemberOne" type="string" />
132
+ <Field public name="MemberTwo" type="int" />
133
+ </List>
134
+ </ClassDeclaration>
135
+ </TestNamespace>
119
136
  );
120
137
 
121
- expect(res).toBe(coretest.d`
122
- namespace TestCode
138
+ expect(tree).toRenderTo(coretest.d`
139
+ public class TestClass
123
140
  {
124
- public class TestClass
125
- {
126
- public string MemberOne;
127
- public int MemberTwo;
128
- }
141
+ public string MemberOne;
142
+ public int MemberTwo;
129
143
  }
130
144
  `);
131
145
  });
132
146
 
133
147
  it("declares class with some methods", () => {
134
- const res = toSourceText(
135
- <ClassDeclaration public name="TestClass">
136
- <List>
137
- <Method public name="MethodOne" />
138
- <Method private virtual name="MethodTwo" />
139
- </List>
140
- </ClassDeclaration>,
148
+ const tree = (
149
+ <TestNamespace>
150
+ <ClassDeclaration public name="TestClass">
151
+ <List>
152
+ <Method public name="MethodOne" />
153
+ <Method private virtual name="MethodTwo" />
154
+ </List>
155
+ </ClassDeclaration>
156
+ </TestNamespace>
141
157
  );
142
158
 
143
- expect(res).toBe(coretest.d`
144
- namespace TestCode
159
+ expect(tree).toRenderTo(coretest.d`
160
+ public class TestClass
145
161
  {
146
- public class TestClass
147
- {
148
- public void MethodOne() {}
149
- private virtual void MethodTwo() {}
150
- }
162
+ public void MethodOne() {}
163
+ private virtual void MethodTwo() {}
151
164
  }
152
165
  `);
153
166
  });
@@ -170,57 +183,52 @@ it("uses refkeys for members, params, and return type", () => {
170
183
 
171
184
  const res = render(
172
185
  <Output namePolicy={createCSharpNamePolicy()}>
173
- <Namespace name="TestCode">
174
- <SourceFile path="Test.cs">
175
- <EnumDeclaration public name="TestEnum" refkey={enumTypeRefkey}>
176
- <List comma hardline>
177
- <EnumMember name="One" />
178
- <EnumMember name="Two" />
179
- </List>
180
- </EnumDeclaration>
181
- <hbr />
182
- <ClassDeclaration public name="TestInput" refkey={inputTypeRefkey} />
186
+ <SourceFile path="Test.cs">
187
+ <EnumDeclaration public name="TestEnum" refkey={enumTypeRefkey}>
188
+ <List comma hardline>
189
+ <EnumMember name="One" />
190
+ <EnumMember name="Two" />
191
+ </List>
192
+ </EnumDeclaration>
193
+ <hbr />
194
+ <ClassDeclaration public name="TestInput" refkey={inputTypeRefkey} />
195
+ <hbr />
196
+ <ClassDeclaration
197
+ public
198
+ name="TestResult"
199
+ refkey={testResultTypeRefkey}
200
+ />
201
+ <hbr />
202
+ <ClassDeclaration public name="TestClass">
203
+ <Field private name="MemberOne" type={enumTypeRefkey} />
183
204
  <hbr />
184
- <ClassDeclaration
205
+ <Method
185
206
  public
186
- name="TestResult"
187
- refkey={testResultTypeRefkey}
188
- />
189
- <hbr />
190
- <ClassDeclaration public name="TestClass">
191
- <Field private name="MemberOne" type={enumTypeRefkey} />
192
- <hbr />
193
- <Method
194
- public
195
- name="MethodOne"
196
- parameters={params}
197
- returns={testResultTypeRefkey}
198
- >
199
- return new {testResultTypeRefkey}();
200
- </Method>
201
- </ClassDeclaration>
202
- </SourceFile>
203
- </Namespace>
207
+ name="MethodOne"
208
+ parameters={params}
209
+ returns={testResultTypeRefkey}
210
+ >
211
+ return new {testResultTypeRefkey}();
212
+ </Method>
213
+ </ClassDeclaration>
214
+ </SourceFile>
204
215
  </Output>,
205
216
  );
206
217
 
207
218
  expect(findFile(res, "Test.cs").contents).toBe(coretest.d`
208
- namespace TestCode
219
+ public enum TestEnum
209
220
  {
210
- public enum TestEnum
211
- {
212
- One,
213
- Two
214
- }
215
- public class TestInput;
216
- public class TestResult;
217
- public class TestClass
221
+ One,
222
+ Two
223
+ }
224
+ public class TestInput;
225
+ public class TestResult;
226
+ public class TestClass
227
+ {
228
+ private TestEnum _memberOne;
229
+ public TestResult MethodOne(int intParam, TestInput bodyParam)
218
230
  {
219
- private TestEnum _memberOne;
220
- public TestResult MethodOne(int intParam, TestInput bodyParam)
221
- {
222
- return new TestResult();
223
- }
231
+ return new TestResult();
224
232
  }
225
233
  }
226
234
  `);
@@ -241,27 +249,22 @@ describe("with type parameters", () => {
241
249
 
242
250
  expect(
243
251
  <TestNamespace>
244
- <SourceFile path="Test.cs">
245
- <ClassDeclaration
246
- public
247
- name="TestClass"
248
- typeParameters={typeParameters}
249
- >
250
- <List>
251
- <Property name="PropA" type={typeParameters[0].refkey} get set />
252
- <Property name="PropB" type={typeParameters[1].refkey} get set />
253
- </List>
254
- </ClassDeclaration>
255
- </SourceFile>
252
+ <ClassDeclaration
253
+ public
254
+ name="TestClass"
255
+ typeParameters={typeParameters}
256
+ >
257
+ <List>
258
+ <Property name="PropA" type={typeParameters[0].refkey} get set />
259
+ <Property name="PropB" type={typeParameters[1].refkey} get set />
260
+ </List>
261
+ </ClassDeclaration>
256
262
  </TestNamespace>,
257
263
  ).toRenderTo(`
258
- namespace TestCode
264
+ public class TestClass<T, U>
259
265
  {
260
- public class TestClass<T, U>
261
- {
262
- T PropA { get; set; }
263
- U PropB { get; set; }
264
- }
266
+ T PropA { get; set; }
267
+ U PropB { get; set; }
265
268
  }
266
269
  `);
267
270
  });
@@ -290,10 +293,10 @@ describe("with type parameters", () => {
290
293
  </TestNamespace>,
291
294
  ).toRenderTo(`
292
295
  public class TestClass<T, U>
293
- where T : IFoo
294
- where U : IBar
296
+ where T : IFoo
297
+ where U : IBar
295
298
  {
296
- // Body
299
+ // Body
297
300
  }
298
301
  `);
299
302
  });
@@ -308,25 +311,24 @@ it("declares class with invalid members", () => {
308
311
  );
309
312
 
310
313
  expect(() => toSourceText(decl)).toThrow(
311
- "can't define an enum member outside of an enum-decl scope",
314
+ "EnumMember must be used within an EnumDeclaration.",
312
315
  );
313
316
  });
314
317
 
315
318
  describe("constructor", () => {
316
319
  it("declares with constructor", () => {
317
- const res = toSourceText(
318
- <ClassDeclaration public name="TestClass">
319
- <Constructor public />
320
- </ClassDeclaration>,
320
+ const tree = (
321
+ <TestNamespace>
322
+ <ClassDeclaration public name="TestClass">
323
+ <Constructor public />
324
+ </ClassDeclaration>
325
+ </TestNamespace>
321
326
  );
322
327
 
323
- expect(res).toBe(coretest.d`
324
- namespace TestCode
328
+ expect(tree).toRenderTo(coretest.d`
329
+ public class TestClass
325
330
  {
326
- public class TestClass
327
- {
328
- public TestClass() {}
329
- }
331
+ public TestClass() {}
330
332
  }
331
333
  `);
332
334
  });
@@ -350,33 +352,32 @@ describe("constructor", () => {
350
352
  },
351
353
  ];
352
354
 
353
- const res = toSourceText(
354
- <ClassDeclaration public name="TestClass">
355
- <Field private name="name" type="string" refkey={thisNameRefkey} />
356
- <hbr />
357
- <Field private name="size" type="int" refkey={thisSizeRefkey} />
358
- <hbr />
359
- <Constructor public parameters={ctorParams}>
360
- {thisNameRefkey} = {paramNameRefkey};<hbr />
361
- {thisSizeRefkey} = {paramSizeRefkey};
362
- </Constructor>
363
- </ClassDeclaration>,
355
+ const tree = (
356
+ <TestNamespace>
357
+ <ClassDeclaration public name="TestClass">
358
+ <Field private name="name" type="string" refkey={thisNameRefkey} />
359
+ <hbr />
360
+ <Field private name="size" type="int" refkey={thisSizeRefkey} />
361
+ <hbr />
362
+ <Constructor public parameters={ctorParams}>
363
+ {thisNameRefkey} = {paramNameRefkey};<hbr />
364
+ {thisSizeRefkey} = {paramSizeRefkey};
365
+ </Constructor>
366
+ </ClassDeclaration>
367
+ </TestNamespace>
364
368
  );
365
369
 
366
370
  // TODO: assignments to members should have this. prefix
367
371
  // e.g. this.name = name;
368
- expect(res).toBe(coretest.d`
369
- namespace TestCode
372
+ expect(tree).toRenderTo(coretest.d`
373
+ public class TestClass
370
374
  {
371
- public class TestClass
375
+ private string _name;
376
+ private int _size;
377
+ public TestClass(string name, int size)
372
378
  {
373
- private string _name;
374
- private int _size;
375
- public TestClass(string name, int size)
376
- {
377
- _name = name;
378
- _size = size;
379
- }
379
+ _name = name;
380
+ _size = size;
380
381
  }
381
382
  }
382
383
  `);
@@ -415,12 +416,9 @@ describe("constructor", () => {
415
416
  </ClassDeclaration>
416
417
  </Wrapper>,
417
418
  ).toRenderTo(`
418
- namespace TestCode
419
+ public class TestClass(string name, int size)
419
420
  {
420
- public class TestClass(string name, int size)
421
- {
422
- string PrettyName { get; } = $"{name} {size}";
423
- }
421
+ string PrettyName { get; } = $"{name} {size}";
424
422
  }
425
423
  `);
426
424
  });
@@ -441,12 +439,9 @@ describe("constructor", () => {
441
439
  </NamePolicyContext.Provider>
442
440
  </Wrapper>,
443
441
  ).toRenderTo(`
444
- namespace TestCode
442
+ public class TestClass(string name)
445
443
  {
446
- public class TestClass(string name)
447
- {
448
- string name_2;
449
- }
444
+ string name_2;
450
445
  }
451
446
  `);
452
447
  });
@@ -474,8 +469,8 @@ it("supports class member doc comments", () => {
474
469
  /// This is a test
475
470
  class Test
476
471
  {
477
- /// This is a member
478
- public int Member;
472
+ /// This is a member
473
+ public int Member;
479
474
  }
480
475
  `);
481
476
  });