@alloy-js/typescript 0.20.0-dev.0 → 0.20.0-dev.1

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 (134) hide show
  1. package/dist/src/builtins/node.d.ts +1 -1
  2. package/dist/src/components/ArrowFunction.d.ts.map +1 -1
  3. package/dist/src/components/ArrowFunction.js +7 -13
  4. package/dist/src/components/BlockScope.d.ts.map +1 -1
  5. package/dist/src/components/BlockScope.js +3 -2
  6. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  7. package/dist/src/components/ClassDeclaration.js +20 -30
  8. package/dist/src/components/Declaration.d.ts +2 -9
  9. package/dist/src/components/Declaration.d.ts.map +1 -1
  10. package/dist/src/components/Declaration.js +20 -43
  11. package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
  12. package/dist/src/components/EnumDeclaration.js +5 -9
  13. package/dist/src/components/EnumMember.d.ts.map +1 -1
  14. package/dist/src/components/EnumMember.js +3 -4
  15. package/dist/src/components/ExportStatement.d.ts.map +1 -1
  16. package/dist/src/components/ExportStatement.js +2 -1
  17. package/dist/src/components/FunctionBase.d.ts.map +1 -1
  18. package/dist/src/components/FunctionBase.js +15 -12
  19. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  20. package/dist/src/components/FunctionDeclaration.js +3 -3
  21. package/dist/src/components/FunctionExpression.d.ts.map +1 -1
  22. package/dist/src/components/FunctionExpression.js +7 -13
  23. package/dist/src/components/FunctionType.d.ts.map +1 -1
  24. package/dist/src/components/FunctionType.js +3 -3
  25. package/dist/src/components/IfStatement.d.ts.map +1 -1
  26. package/dist/src/components/IfStatement.js +2 -1
  27. package/dist/src/components/ImportStatement.d.ts.map +1 -1
  28. package/dist/src/components/ImportStatement.js +3 -4
  29. package/dist/src/components/Interface.d.ts.map +1 -1
  30. package/dist/src/components/Interface.js +14 -13
  31. package/dist/src/components/InterfaceMethod.d.ts.map +1 -1
  32. package/dist/src/components/InterfaceMethod.js +4 -3
  33. package/dist/src/components/LexicalScope.d.ts +12 -0
  34. package/dist/src/components/LexicalScope.d.ts.map +1 -0
  35. package/dist/src/components/LexicalScope.js +24 -0
  36. package/dist/src/components/MemberDeclaration.d.ts +1 -10
  37. package/dist/src/components/MemberDeclaration.d.ts.map +1 -1
  38. package/dist/src/components/MemberDeclaration.js +3 -7
  39. package/dist/src/components/MemberExpression.d.ts.map +1 -1
  40. package/dist/src/components/MemberExpression.js +13 -5
  41. package/dist/src/components/MemberScope.d.ts +15 -0
  42. package/dist/src/components/MemberScope.d.ts.map +1 -0
  43. package/dist/src/components/MemberScope.js +17 -0
  44. package/dist/src/components/NewExpression.d.ts.map +1 -1
  45. package/dist/src/components/NewExpression.js +5 -5
  46. package/dist/src/components/ObjectExpression.d.ts.map +1 -1
  47. package/dist/src/components/ObjectExpression.js +10 -14
  48. package/dist/src/components/PropertyName.js +1 -2
  49. package/dist/src/components/SourceFile.d.ts.map +1 -1
  50. package/dist/src/components/SourceFile.js +3 -2
  51. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  52. package/dist/src/components/VarDeclaration.js +14 -26
  53. package/dist/src/context/index.d.ts +1 -1
  54. package/dist/src/context/index.d.ts.map +1 -1
  55. package/dist/src/context/index.js +1 -1
  56. package/dist/src/create-package.d.ts.map +1 -1
  57. package/dist/src/create-package.js +11 -33
  58. package/dist/src/symbols/index.d.ts +19 -0
  59. package/dist/src/symbols/index.d.ts.map +1 -1
  60. package/dist/src/symbols/index.js +72 -1
  61. package/dist/src/symbols/reference.d.ts +2 -2
  62. package/dist/src/symbols/reference.d.ts.map +1 -1
  63. package/dist/src/symbols/reference.js +68 -70
  64. package/dist/src/symbols/scopes.d.ts +5 -8
  65. package/dist/src/symbols/scopes.d.ts.map +1 -1
  66. package/dist/src/symbols/scopes.js +26 -0
  67. package/dist/src/symbols/ts-lexical-scope.d.ts +12 -0
  68. package/dist/src/symbols/ts-lexical-scope.d.ts.map +1 -0
  69. package/dist/src/symbols/ts-lexical-scope.js +16 -0
  70. package/dist/src/symbols/ts-member-scope.d.ts +12 -4
  71. package/dist/src/symbols/ts-member-scope.d.ts.map +1 -1
  72. package/dist/src/symbols/ts-member-scope.js +15 -6
  73. package/dist/src/symbols/ts-module-scope.d.ts +8 -3
  74. package/dist/src/symbols/ts-module-scope.d.ts.map +1 -1
  75. package/dist/src/symbols/ts-module-scope.js +29 -18
  76. package/dist/src/symbols/ts-output-symbol.d.ts +19 -18
  77. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  78. package/dist/src/symbols/ts-output-symbol.js +60 -37
  79. package/dist/src/symbols/ts-package-scope.d.ts +1 -1
  80. package/dist/src/symbols/ts-package-scope.d.ts.map +1 -1
  81. package/dist/src/symbols/ts-package-scope.js +4 -5
  82. package/dist/src/utils.d.ts +4 -0
  83. package/dist/src/utils.d.ts.map +1 -1
  84. package/dist/src/utils.js +23 -1
  85. package/dist/test/class.test.js +4 -4
  86. package/dist/test/externals.test.js +2 -38
  87. package/dist/test/reference.test.js +3 -3
  88. package/dist/tsconfig.tsbuildinfo +1 -1
  89. package/package.json +3 -3
  90. package/src/components/ArrowFunction.tsx +7 -10
  91. package/src/components/BlockScope.tsx +3 -3
  92. package/src/components/ClassDeclaration.tsx +19 -44
  93. package/src/components/Declaration.tsx +22 -53
  94. package/src/components/EnumDeclaration.tsx +4 -11
  95. package/src/components/EnumMember.tsx +3 -3
  96. package/src/components/ExportStatement.tsx +2 -1
  97. package/src/components/FunctionBase.tsx +33 -28
  98. package/src/components/FunctionDeclaration.tsx +3 -3
  99. package/src/components/FunctionExpression.tsx +7 -10
  100. package/src/components/FunctionType.tsx +3 -3
  101. package/src/components/IfStatement.tsx +2 -1
  102. package/src/components/ImportStatement.tsx +4 -8
  103. package/src/components/Interface.tsx +15 -22
  104. package/src/components/InterfaceMethod.tsx +4 -4
  105. package/src/components/LexicalScope.tsx +27 -0
  106. package/src/components/MemberDeclaration.tsx +8 -20
  107. package/src/components/MemberExpression.tsx +25 -5
  108. package/src/components/MemberScope.tsx +29 -0
  109. package/src/components/NewExpression.tsx +4 -5
  110. package/src/components/ObjectExpression.tsx +11 -18
  111. package/src/components/PropertyName.tsx +2 -2
  112. package/src/components/SourceFile.tsx +3 -1
  113. package/src/components/VarDeclaration.tsx +14 -28
  114. package/src/context/index.ts +1 -1
  115. package/src/create-package.ts +10 -39
  116. package/src/symbols/index.ts +118 -0
  117. package/src/symbols/reference.tsx +163 -0
  118. package/src/symbols/scopes.ts +38 -13
  119. package/src/symbols/ts-lexical-scope.ts +18 -0
  120. package/src/symbols/ts-member-scope.ts +23 -6
  121. package/src/symbols/ts-module-scope.ts +31 -21
  122. package/src/symbols/ts-output-symbol.ts +111 -49
  123. package/src/symbols/ts-package-scope.ts +5 -5
  124. package/src/utils.ts +38 -1
  125. package/temp/api.json +1641 -713
  126. package/test/class.test.tsx +10 -4
  127. package/test/externals.test.tsx +1 -40
  128. package/test/reference.test.tsx +5 -5
  129. package/tsdoc-metadata.json +1 -1
  130. package/dist/src/context/private-scope.d.ts +0 -18
  131. package/dist/src/context/private-scope.d.ts.map +0 -1
  132. package/dist/src/context/private-scope.js +0 -8
  133. package/src/context/private-scope.ts +0 -23
  134. package/src/symbols/reference.ts +0 -197
@@ -1,14 +1,13 @@
1
1
  import {
2
2
  type Binder,
3
3
  getSymbolCreatorSymbol,
4
- OutputSymbolFlags,
4
+ OutputMemberSpace,
5
5
  type Refkey,
6
6
  refkey,
7
7
  SymbolCreator,
8
8
  } from "@alloy-js/core";
9
9
  import {
10
10
  TSModuleScope,
11
- TSOutputScope,
12
11
  TSOutputSymbol,
13
12
  TSPackageScope,
14
13
  } from "./symbols/index.js";
@@ -120,13 +119,12 @@ function assignMembers(
120
119
  keys: Record<string, any>,
121
120
  isStatic: boolean,
122
121
  ) {
123
- let scope: TSOutputScope;
122
+ let space: OutputMemberSpace;
123
+
124
124
  if (isStatic) {
125
- ownerSym.flags |= OutputSymbolFlags.StaticMemberContainer;
126
- scope = ownerSym.staticMemberScope!;
125
+ space = ownerSym.staticMembers!;
127
126
  } else {
128
- ownerSym.flags |= OutputSymbolFlags.InstanceMemberContainer;
129
- scope = ownerSym.instanceMemberScope!;
127
+ space = ownerSym.instanceMembers!;
130
128
  }
131
129
 
132
130
  const namespace = isStatic ? "static" : "instance";
@@ -138,28 +136,13 @@ function assignMembers(
138
136
  const memberKey = keys[namespace][memberObj.name];
139
137
  if (!memberKey) continue; // Skip if key doesn't exist
140
138
 
141
- let memberFlags =
142
- isStatic ?
143
- OutputSymbolFlags.StaticMember
144
- : OutputSymbolFlags.InstanceMember;
145
- if (memberObj.staticMembers?.length) {
146
- memberFlags |= OutputSymbolFlags.StaticMemberContainer;
147
- }
148
- if (memberObj.instanceMembers?.length) {
149
- memberFlags |= OutputSymbolFlags.InstanceMemberContainer;
150
- }
151
-
152
- const memberSym = new TSOutputSymbol(memberObj.name, {
153
- scope,
139
+ const memberSym = new TSOutputSymbol(memberObj.name, space, {
154
140
  binder,
155
141
  refkeys: memberKey,
156
142
  export: false,
157
143
  default: false,
158
- flags: memberFlags,
159
144
  });
160
145
 
161
- scope.symbols.add(memberSym);
162
-
163
146
  // Recursively handle nested static and instance members
164
147
  if (memberObj.staticMembers && isStatic) {
165
148
  assignMembers(
@@ -199,17 +182,15 @@ function createSymbols(
199
182
 
200
183
  for (const [path, symbols] of Object.entries(props.descriptor)) {
201
184
  const keys = path === "." ? refkeys : refkeys[path];
202
- const moduleScope = new TSModuleScope(path, {
185
+ const moduleScope = new TSModuleScope(path, pkgScope, {
203
186
  binder,
204
- parent: pkgScope,
205
187
  });
206
188
 
207
189
  pkgScope.exportedSymbols.set(path, moduleScope);
208
190
 
209
191
  if (symbols.default) {
210
192
  const key = keys.default;
211
- const sym = new TSOutputSymbol(symbols.default, {
212
- scope: moduleScope,
193
+ const sym = new TSOutputSymbol(symbols.default, moduleScope.values, {
213
194
  refkeys: key,
214
195
  export: true,
215
196
  default: true,
@@ -223,22 +204,12 @@ function createSymbols(
223
204
  { name: exportedName }
224
205
  : exportedName;
225
206
  const key = keys[namedRef.name];
226
-
227
- let flags = OutputSymbolFlags.None;
228
- if (namedRef.staticMembers?.length) {
229
- flags |= OutputSymbolFlags.StaticMemberContainer;
230
- }
231
- if (namedRef.instanceMembers?.length) {
232
- flags |= OutputSymbolFlags.InstanceMemberContainer;
233
- }
234
-
235
- const ownerSym = new TSOutputSymbol(namedRef.name, {
207
+ const ownerSym = new TSOutputSymbol(namedRef.name, moduleScope.values, {
236
208
  binder,
237
- scope: moduleScope,
238
209
  refkeys: key,
239
210
  export: true,
240
211
  default: false,
241
- flags,
212
+ hasInstanceMembers: !!namedRef.instanceMembers?.length,
242
213
  });
243
214
  moduleScope.exportedSymbols.set(key, ownerSym);
244
215
 
@@ -1,6 +1,124 @@
1
+ import { OutputScopeOptions } from "@alloy-js/core";
2
+ import { useLexicalScope, useMemberOwner } from "../utils.js";
3
+ import { useTSScope } from "./scopes.js";
4
+ import { TSLexicalScope } from "./ts-lexical-scope.js";
5
+ import { TSMemberScope } from "./ts-member-scope.js";
6
+ import { CreateTsSymbolOptions, TSOutputSymbol } from "./ts-output-symbol.js";
7
+
1
8
  export * from "./reference.js";
2
9
  export * from "./scopes.js";
10
+ export * from "./ts-lexical-scope.js";
3
11
  export * from "./ts-member-scope.js";
4
12
  export * from "./ts-module-scope.js";
5
13
  export * from "./ts-output-symbol.js";
6
14
  export * from "./ts-package-scope.js";
15
+
16
+ export function createTypeAndValueSymbol(
17
+ name: string,
18
+ options: CreateTsSymbolOptions = {},
19
+ ) {
20
+ const scope = useLexicalScope();
21
+ const spaces = scope ? [scope.types, scope.values] : [];
22
+ return new TSOutputSymbol(name, spaces, options);
23
+ }
24
+
25
+ export function createTypeSymbol(name: string, options: CreateTsSymbolOptions) {
26
+ const scope = useLexicalScope();
27
+ if (scope && !scope.types) {
28
+ throw new Error(
29
+ "Attempting to create a type symbol in a scope that does not have a type declaration space.",
30
+ );
31
+ }
32
+ const spaces = scope ? [scope.types] : [];
33
+ return new TSOutputSymbol(name, spaces, options);
34
+ }
35
+
36
+ export function createValueSymbol(
37
+ name: string,
38
+ options: CreateTsSymbolOptions = {},
39
+ ) {
40
+ const scope = useLexicalScope();
41
+ if (scope && !scope.values) {
42
+ throw new Error(
43
+ "Attempting to create a value symbol in a scope that does not have a value declaration space.",
44
+ );
45
+ }
46
+ const spaces = scope ? [scope.values] : [];
47
+ return new TSOutputSymbol(name, spaces, options);
48
+ }
49
+
50
+ export function createTransientValueSymbol() {
51
+ return new TSOutputSymbol("transient-value", undefined, {
52
+ transient: true,
53
+ });
54
+ }
55
+
56
+ export function createStaticMemberSymbol(
57
+ name: string,
58
+ options: CreateTsSymbolOptions = {},
59
+ ) {
60
+ const owner = useMemberOwner();
61
+ return new TSOutputSymbol(name, owner.staticMembers, options);
62
+ }
63
+
64
+ export function createInstanceMemberSymbol(
65
+ name: string,
66
+ options: CreateTsSymbolOptions = {},
67
+ ) {
68
+ const owner = useMemberOwner();
69
+ return new TSOutputSymbol(name, owner.instanceMembers, options);
70
+ }
71
+
72
+ export function createPrivateStaticMemberSymbol(
73
+ name: string,
74
+ options: CreateTsSymbolOptions = {},
75
+ ) {
76
+ const owner = useMemberOwner();
77
+ return new TSOutputSymbol(name, owner.privateStaticMembers, options);
78
+ }
79
+
80
+ export function createPrivateInstanceMemberSymbol(
81
+ name: string,
82
+ options: CreateTsSymbolOptions = {},
83
+ ) {
84
+ const owner = useMemberOwner();
85
+ return new TSOutputSymbol(name, owner.privateInstanceMembers, options);
86
+ }
87
+
88
+ export function createMemberSymbol(
89
+ name: string,
90
+ locationOptions: { jsPrivate?: boolean; static?: boolean },
91
+ options: CreateTsSymbolOptions = {},
92
+ ) {
93
+ if (locationOptions.jsPrivate) {
94
+ if (locationOptions.static) {
95
+ return createPrivateStaticMemberSymbol(name, options);
96
+ } else {
97
+ return createPrivateInstanceMemberSymbol(name, options);
98
+ }
99
+ } else {
100
+ if (locationOptions.static) {
101
+ return createStaticMemberSymbol(name, options);
102
+ } else {
103
+ return createInstanceMemberSymbol(name, options);
104
+ }
105
+ }
106
+ }
107
+
108
+ export function createLexicalScope(
109
+ name: string,
110
+ options: OutputScopeOptions = {},
111
+ ) {
112
+ const parent = useTSScope();
113
+
114
+ return new TSLexicalScope(name, parent, options);
115
+ }
116
+
117
+ export function createMemberScope(
118
+ name: string,
119
+ ownerSymbol: TSOutputSymbol,
120
+ options: OutputScopeOptions = {},
121
+ ) {
122
+ const parent = useTSScope();
123
+ return new TSMemberScope(name, parent, ownerSymbol, options);
124
+ }
@@ -0,0 +1,163 @@
1
+ import {
2
+ Children,
3
+ MemberResolutionContext,
4
+ MemberResolver,
5
+ memo,
6
+ Refkey,
7
+ resolve,
8
+ untrack,
9
+ useContext,
10
+ } from "@alloy-js/core";
11
+ import { MemberExpression } from "../components/MemberExpression.jsx";
12
+ import { usePackage } from "../components/PackageDirectory.jsx";
13
+ import { SourceFileContext } from "../components/SourceFile.jsx";
14
+ import { TSOutputScope } from "./scopes.js";
15
+ import { TSLexicalScope } from "./ts-lexical-scope.js";
16
+ import { TSModuleScope } from "./ts-module-scope.js";
17
+ import { TSOutputSymbol } from "./ts-output-symbol.js";
18
+ import { TSPackageScope } from "./ts-package-scope.js";
19
+
20
+ export interface RefOptions {
21
+ type?: boolean;
22
+ }
23
+ export function ref(
24
+ refkey: Refkey,
25
+ options?: RefOptions,
26
+ ): () => [Children, TSOutputSymbol | undefined] {
27
+ const sourceFile = useContext(SourceFileContext);
28
+ const resolveResult = resolve<TSOutputScope, TSOutputSymbol>(
29
+ refkey as Refkey,
30
+ { memberResolver },
31
+ );
32
+
33
+ return memo(() => {
34
+ if (resolveResult.value === undefined) {
35
+ return ["<Unresolved Symbol>", undefined];
36
+ }
37
+
38
+ const { symbol, pathDown, memberPath, lexicalDeclaration, commonScope } =
39
+ resolveResult.value;
40
+
41
+ validateSymbolReachable(pathDown);
42
+
43
+ // Where the target declaration is relative to the referencing scope.
44
+ // * package: target symbol is in a different package
45
+ // * module: target symbol is in a different module
46
+ // * local: target symbol is within the current module
47
+ const targetLocation =
48
+ pathDown[0] instanceof TSPackageScope ? "package"
49
+ : pathDown[0] instanceof TSModuleScope ? "module"
50
+ : "local";
51
+ let localSymbol: TSOutputSymbol | undefined;
52
+
53
+ if (targetLocation === "package") {
54
+ // need package import
55
+ const pkg = usePackage();
56
+ const sourcePackage = pathDown[0] as TSPackageScope;
57
+
58
+ if (pkg && !sourcePackage.builtin) {
59
+ pkg.scope.addDependency(sourcePackage);
60
+ }
61
+
62
+ // find public dependency
63
+ for (const module of sourcePackage.exportedSymbols.values()) {
64
+ for (const refkey of lexicalDeclaration.refkeys) {
65
+ if (module.exportedSymbols.has(refkey)) {
66
+ localSymbol = untrack(() =>
67
+ sourceFile!.scope.addImport(lexicalDeclaration, module, {
68
+ type: options?.type,
69
+ }),
70
+ );
71
+ break;
72
+ }
73
+ }
74
+ }
75
+
76
+ if (!localSymbol) {
77
+ throw new Error(
78
+ "The symbol " + symbol.name + " is not exported from package",
79
+ );
80
+ }
81
+ } else if (targetLocation === "module") {
82
+ localSymbol = untrack(() =>
83
+ sourceFile!.scope.addImport(
84
+ lexicalDeclaration,
85
+ pathDown[0] as TSModuleScope,
86
+ { type: options?.type },
87
+ ),
88
+ );
89
+ }
90
+
91
+ const parts = [];
92
+
93
+ if (commonScope && commonScope.isMemberScope) {
94
+ // we are referencing a member of a type we are inside
95
+ if (lexicalDeclaration.isInstanceMemberSymbol) {
96
+ parts.push(<MemberExpression.Part id="this" />);
97
+ } else {
98
+ parts.push(<MemberExpression.Part symbol={commonScope.ownerSymbol} />);
99
+ }
100
+ parts.push(<MemberExpression.Part symbol={lexicalDeclaration} />);
101
+ } else {
102
+ parts.push(
103
+ <MemberExpression.Part symbol={localSymbol ?? lexicalDeclaration} />,
104
+ );
105
+ }
106
+ for (const part of memberPath) {
107
+ parts.push(<MemberExpression.Part symbol={part} />);
108
+ }
109
+
110
+ return [<MemberExpression children={parts} />, localSymbol ?? symbol];
111
+ });
112
+ }
113
+
114
+ function validateSymbolReachable(path: TSOutputScope[]) {
115
+ for (const scope of path) {
116
+ if (!(scope instanceof TSModuleScope) && scope instanceof TSLexicalScope) {
117
+ throw new Error(
118
+ "Cannot reference a symbol inside a function from outside a function",
119
+ );
120
+ }
121
+ }
122
+ }
123
+
124
+ const memberResolver: MemberResolver<TSOutputScope, TSOutputSymbol> = function (
125
+ owner: TSOutputSymbol,
126
+ member: TSOutputSymbol,
127
+ context: MemberResolutionContext<TSOutputScope>,
128
+ ) {
129
+ if (context.isMemberAccess) {
130
+ if (!member.isMemberSymbol) {
131
+ throw new Error(`${member.name} is not a member symbol.`);
132
+ }
133
+
134
+ const memberOwner = owner.hasTypeSymbol ? owner.type : owner.dealias();
135
+ if (member.ownerSymbol !== memberOwner) {
136
+ throw new Error(`${member.name} is not a member of ${owner.name}.`);
137
+ }
138
+ } else {
139
+ if (member.isInstanceMemberSymbol) {
140
+ if (
141
+ !context.referencePath.some(
142
+ (s) => s.isMemberScope && s.ownerSymbol === owner,
143
+ )
144
+ ) {
145
+ throw new Error(
146
+ `Cannot reference instance member '${member.name}' without an instance of '${owner.name}' in scope`,
147
+ );
148
+ }
149
+ }
150
+
151
+ if (member.isPrivateMemberSymbol) {
152
+ if (
153
+ !context.referencePath.some(
154
+ (s) => s.isMemberScope && s.ownerSymbol === owner,
155
+ )
156
+ ) {
157
+ throw new Error(
158
+ `Cannot reference private member '${member.name}' outside an instance of '${owner.name} in scope'`,
159
+ );
160
+ }
161
+ }
162
+ }
163
+ };
@@ -1,23 +1,48 @@
1
- import { OutputScope, useScope } from "@alloy-js/core";
1
+ import { useScope } from "@alloy-js/core";
2
+ import { TSLexicalScope } from "./ts-lexical-scope.js";
2
3
  import { TSMemberScope } from "./ts-member-scope.js";
3
4
  import { TSModuleScope } from "./ts-module-scope.js";
4
5
  import { TSPackageScope } from "./ts-package-scope.js";
5
6
 
6
- export type TSOutputScope =
7
- | TSGlobalScope
8
- | TSPackageScope
9
- | TSModuleScope
10
- | TSFunctionScope
11
- | TSMemberScope;
7
+ export type TSOutputScope = TSPackageScope | TSModuleScope | TSLexicalScope;
12
8
 
13
- export interface TSGlobalScope extends OutputScope {
14
- kind: "global";
9
+ export function useTSScope() {
10
+ return useScope() as TSOutputScope;
15
11
  }
16
12
 
17
- export interface TSFunctionScope extends OutputScope {
18
- kind: "function";
13
+ export function useTSLexicalScope(): TSLexicalScope {
14
+ const scope = useTSScope();
15
+
16
+ if (!(scope instanceof TSLexicalScope)) {
17
+ throw new Error(
18
+ "Expected a TSLexicalScope, but got " + scope.constructor.name,
19
+ );
20
+ }
21
+
22
+ return scope;
19
23
  }
20
24
 
21
- export function useTSScope() {
22
- return useScope() as TSOutputScope;
25
+ export function useTSLexicalScopeIfPresent() {
26
+ const scope = useTSScope();
27
+ if (!scope) {
28
+ return undefined;
29
+ }
30
+ if (!(scope instanceof TSLexicalScope)) {
31
+ throw new Error(
32
+ "Expected a TSLexicalScope, but got " + scope.constructor.name,
33
+ );
34
+ }
35
+ return scope;
36
+ }
37
+
38
+ export function useTSMemberScope(): TSMemberScope {
39
+ const scope = useTSScope();
40
+
41
+ if (!(scope instanceof TSMemberScope)) {
42
+ throw new Error(
43
+ "Expected a TSMemberScope, but got " + scope.constructor.name,
44
+ );
45
+ }
46
+
47
+ return scope;
23
48
  }
@@ -0,0 +1,18 @@
1
+ import { OutputScope } from "@alloy-js/core";
2
+
3
+ /**
4
+ * A lexical scope for TypeScript, which contains declaration spaces for types
5
+ * and values. This scope is used to hold symbols for things like variable or
6
+ * interface declarations.
7
+ */
8
+ export class TSLexicalScope extends OutputScope {
9
+ public static readonly declarationSpaces = ["values", "types"];
10
+
11
+ get values() {
12
+ return this.spaceFor("values")!;
13
+ }
14
+
15
+ get types() {
16
+ return this.spaceFor("types")!;
17
+ }
18
+ }
@@ -1,12 +1,29 @@
1
- import { OutputScope } from "@alloy-js/core";
1
+ import { OutputScopeOptions } from "@alloy-js/core";
2
+ import { TSOutputScope } from "./scopes.js";
3
+ import { TSLexicalScope } from "./ts-lexical-scope.js";
2
4
  import { TSOutputSymbol } from "./ts-output-symbol.js";
3
5
 
4
- export class TSMemberScope extends OutputScope {
5
- get kind() {
6
- return "member" as const;
6
+ /**
7
+ * A member scope for TypeScript. Member declarations create symbols in this
8
+ * scope, which will be added to the owner symbol's members.
9
+ */
10
+ export class TSMemberScope extends TSLexicalScope {
11
+ public static readonly declarationSpaces = [];
12
+
13
+ constructor(
14
+ name: string,
15
+ parentScope: TSOutputScope | undefined,
16
+ ownerSymbol: TSOutputSymbol,
17
+ options: OutputScopeOptions = {},
18
+ ) {
19
+ options.ownerSymbol = ownerSymbol;
20
+ super(name, parentScope, options);
21
+ }
22
+ get ownerSymbol() {
23
+ return super.ownerSymbol as TSOutputSymbol;
7
24
  }
8
25
 
9
- get owner() {
10
- return super.owner as TSOutputSymbol;
26
+ get isMemberScope() {
27
+ return true;
11
28
  }
12
29
  }
@@ -1,4 +1,5 @@
1
- import { OutputScope, reactive, Refkey, shallowReactive } from "@alloy-js/core";
1
+ import { reactive, Refkey, shallowReactive } from "@alloy-js/core";
2
+ import { TSLexicalScope } from "./ts-lexical-scope.js";
2
3
  import { TSOutputSymbol, TSSymbolFlags } from "./ts-output-symbol.js";
3
4
 
4
5
  export interface ImportedSymbol {
@@ -11,11 +12,11 @@ export interface AddImportOptions {
11
12
  type?: boolean;
12
13
  }
13
14
 
14
- export class TSModuleScope extends OutputScope {
15
- get kind() {
16
- return "module" as const;
17
- }
18
-
15
+ /**
16
+ * A lexical scope for TypeScript which represents the top-level scope of a
17
+ * module. Manages the module's imported and exported symbols.
18
+ */
19
+ export class TSModuleScope extends TSLexicalScope {
19
20
  #exportedSymbols: Map<Refkey, TSOutputSymbol> = shallowReactive(new Map());
20
21
  get exportedSymbols() {
21
22
  return this.#exportedSymbols;
@@ -24,6 +25,7 @@ export class TSModuleScope extends OutputScope {
24
25
  #importedSymbols: Map<TSOutputSymbol, TSOutputSymbol> = shallowReactive(
25
26
  new Map(),
26
27
  );
28
+
27
29
  get importedSymbols() {
28
30
  return this.#importedSymbols;
29
31
  }
@@ -40,31 +42,28 @@ export class TSModuleScope extends OutputScope {
40
42
  ) {
41
43
  const existing = this.importedSymbols.get(targetSymbol);
42
44
  if (existing) {
43
- if (!options?.type && existing.tsFlags & TSSymbolFlags.TypeSymbol) {
44
- existing.tsFlags &= ~TSSymbolFlags.TypeSymbol;
45
+ if (!options?.type && existing.isTypeSymbol) {
46
+ existing.spaces.push(this.values);
45
47
  }
46
48
  return existing;
47
49
  }
48
50
 
49
- if (targetModule.kind !== "module") {
50
- throw new Error("Cannot import symbol that isn't in module scope");
51
- }
52
-
53
51
  if (!this.importedModules.has(targetModule)) {
54
52
  this.importedModules.set(targetModule, new Set());
55
53
  }
56
54
 
57
- const localSymbol = new TSOutputSymbol(targetSymbol.name, {
58
- binder: this.binder,
59
- scope: this,
60
- aliasTarget: targetSymbol,
61
- tsFlags: TSSymbolFlags.LocalImportSymbol,
62
- });
63
-
64
- targetSymbol.copyTo(localSymbol);
55
+ let localSymbol;
65
56
 
66
57
  if (options?.type) {
67
- localSymbol.tsFlags |= TSSymbolFlags.TypeSymbol;
58
+ localSymbol = new TSOutputSymbol(targetSymbol.name, this.types, {
59
+ aliasTarget: targetSymbol,
60
+ tsFlags: TSSymbolFlags.LocalImportSymbol,
61
+ });
62
+ } else {
63
+ localSymbol = new TSOutputSymbol(targetSymbol.name, this.values, {
64
+ aliasTarget: targetSymbol,
65
+ tsFlags: TSSymbolFlags.LocalImportSymbol,
66
+ });
68
67
  }
69
68
 
70
69
  this.importedSymbols.set(targetSymbol, localSymbol);
@@ -75,4 +74,15 @@ export class TSModuleScope extends OutputScope {
75
74
 
76
75
  return localSymbol;
77
76
  }
77
+
78
+ getAllSymbols() {
79
+ const allSymbols = new Set<TSOutputSymbol>();
80
+ for (const symbol of this.values) {
81
+ allSymbols.add(symbol as TSOutputSymbol);
82
+ }
83
+ for (const symbol of this.types) {
84
+ allSymbols.add(symbol as TSOutputSymbol);
85
+ }
86
+ return allSymbols;
87
+ }
78
88
  }