@alloy-js/typescript 0.19.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 +7 -7
  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,15 @@
1
1
  import {
2
- OutputScopeFlags,
2
+ OutputSpace,
3
3
  OutputSymbol,
4
4
  OutputSymbolOptions,
5
5
  track,
6
6
  TrackOpTypes,
7
7
  trigger,
8
8
  TriggerOpTypes,
9
+ watch,
9
10
  } from "@alloy-js/core";
10
11
  import { TSOutputScope } from "./scopes.js";
11
- import { TSMemberScope } from "./ts-member-scope.js";
12
+ import { TSModuleScope } from "./ts-module-scope.js";
12
13
 
13
14
  // prettier-ignore
14
15
  export enum TSSymbolFlags {
@@ -16,51 +17,85 @@ export enum TSSymbolFlags {
16
17
  LocalImportSymbol = 1 << 0,
17
18
  TypeSymbol = 1 << 1,
18
19
  ParameterSymbol = 1 << 2,
19
- PrivateMember = 1 << 3,
20
- PrivateMemberContainer = 1 << 4,
21
- Nullish = 1 << 5,
20
+ Nullish = 1 << 3,
22
21
  }
23
22
 
24
23
  export interface CreateTsSymbolOptions extends OutputSymbolOptions {
25
- scope?: TSOutputScope;
26
- owner?: TSOutputSymbol;
27
24
  export?: boolean;
28
25
  default?: boolean;
29
26
  tsFlags?: TSSymbolFlags;
27
+ hasInstanceMembers?: boolean;
30
28
  }
31
29
 
32
30
  export class TSOutputSymbol extends OutputSymbol {
33
- constructor(name: string, options: CreateTsSymbolOptions = {}) {
34
- super(name, options);
31
+ static readonly memberSpaces = [
32
+ "static",
33
+ "instance",
34
+ "private-static",
35
+ "private-instance",
36
+ ] as const;
37
+
38
+ constructor(
39
+ name: string,
40
+ spaces: OutputSpace[] | OutputSpace | undefined,
41
+ options: CreateTsSymbolOptions = {},
42
+ ) {
43
+ super(name, spaces, options);
35
44
  this.#export = !!options.export;
36
45
  this.#default = !!options.default;
37
46
  this.#tsFlags = options.tsFlags ?? TSSymbolFlags.None;
38
- this.#privateMemberScope = undefined;
39
- this.#privateStaticMemberScope = undefined;
40
- if (this.#tsFlags & TSSymbolFlags.PrivateMemberContainer) {
41
- this.#privateMemberScope = new TSMemberScope("private members", {
42
- binder: this.binder,
43
- owner: this,
44
- flags: OutputScopeFlags.InstanceMemberScope,
45
- });
46
-
47
- this.#privateStaticMemberScope = new TSMemberScope(
48
- "private static members",
49
- {
50
- flags: OutputScopeFlags.StaticMemberScope,
51
- binder: this.binder,
52
- owner: this,
53
- },
54
- );
47
+ this.#hasInstanceMembers = !!options.hasInstanceMembers;
48
+ if (
49
+ this.ownerSymbol?.isTypeSymbol ||
50
+ (this.spaces.some((s) => s.key === "types") &&
51
+ !this.spaces.some((s) => s.key === "values"))
52
+ ) {
53
+ // Classes and enums are both types and values. The check above will treat them as values
54
+ // exclusively so that referencing occurs properly. This should instead use type or value
55
+ // referencing based on type context.
56
+ this.#tsFlags |= TSSymbolFlags.TypeSymbol;
55
57
  }
56
-
57
- if (options.export && this.scope.kind === "module") {
58
+ if (options.export && this.scope && this.scope instanceof TSModuleScope) {
58
59
  for (const refkey of this.refkeys) {
59
60
  this.scope.exportedSymbols.set(refkey, this);
60
61
  }
61
62
  }
62
63
  }
63
64
 
65
+ get ownerSymbol() {
66
+ return super.ownerSymbol as TSOutputSymbol | undefined;
67
+ }
68
+
69
+ copy() {
70
+ const copy = new TSOutputSymbol(this.name, undefined, {
71
+ binder: this.binder,
72
+ aliasTarget: this.aliasTarget,
73
+ default: this.default,
74
+ export: this.export,
75
+ tsFlags: this.tsFlags,
76
+ metadata: this.metadata,
77
+ });
78
+
79
+ this.initializeCopy(copy);
80
+
81
+ watch(
82
+ () => this.default,
83
+ (def) => (copy.default = def),
84
+ );
85
+
86
+ watch(
87
+ () => this.export,
88
+ (exp) => (copy.export = exp),
89
+ );
90
+
91
+ watch(
92
+ () => this.tsFlags,
93
+ (flags) => (copy.tsFlags = flags),
94
+ );
95
+
96
+ return copy;
97
+ }
98
+
64
99
  #export: boolean;
65
100
  get export() {
66
101
  track(this, TrackOpTypes.GET, "export");
@@ -94,6 +129,13 @@ export class TSOutputSymbol extends OutputSymbol {
94
129
  track(this, TrackOpTypes.GET, "tsFlags");
95
130
  return this.#tsFlags;
96
131
  }
132
+
133
+ #hasInstanceMembers: boolean = false;
134
+ get hasInstanceMembers() {
135
+ track(this, TrackOpTypes.GET, "hasInstanceMembers");
136
+ return this.#hasInstanceMembers;
137
+ }
138
+
97
139
  set tsFlags(value: TSSymbolFlags) {
98
140
  const oldValue = this.#tsFlags;
99
141
  if (oldValue === value) {
@@ -103,37 +145,57 @@ export class TSOutputSymbol extends OutputSymbol {
103
145
  trigger(this, TriggerOpTypes.SET, "tsFlags", value, oldValue);
104
146
  }
105
147
 
106
- get scope() {
107
- return super.scope as TSOutputScope;
148
+ get scope(): TSOutputScope | undefined {
149
+ return super.scope as TSOutputScope | undefined;
108
150
  }
109
- set scope(value: TSOutputScope) {
110
- super.scope = value;
151
+
152
+ get staticMembers() {
153
+ return this.memberSpaceFor("static")!;
111
154
  }
112
155
 
113
- get staticMemberScope() {
114
- return super.staticMemberScope as TSMemberScope | undefined;
156
+ get instanceMembers() {
157
+ return this.memberSpaceFor("instance")!;
115
158
  }
116
159
 
117
- get instanceMemberScope() {
118
- return super.instanceMemberScope as TSMemberScope | undefined;
160
+ get privateInstanceMembers() {
161
+ return this.memberSpaceFor("private-instance")!;
119
162
  }
120
163
 
121
- #privateMemberScope: TSMemberScope | undefined;
122
- get privateMemberScope() {
123
- return this.#privateMemberScope;
164
+ get privateStaticMembers() {
165
+ return this.memberSpaceFor("private-static")!;
124
166
  }
125
167
 
126
- #privateStaticMemberScope: TSMemberScope | undefined;
127
- get privateStaticMemberScope() {
128
- return this.#privateStaticMemberScope;
168
+ get isPrivateMemberSymbol() {
169
+ return (
170
+ this.spaces.length > 0 &&
171
+ this.spaces.filter(
172
+ (s) => s.key === "private-static" || s.key === "private-instance",
173
+ ).length > 0
174
+ );
129
175
  }
130
176
 
131
- protected createMemberScope(
132
- name: string,
133
- options: { owner?: OutputSymbol; flags?: OutputScopeFlags },
134
- ): TSMemberScope {
135
- return new TSMemberScope(name, {
136
- ...options,
137
- });
177
+ get isPublicMemberSymbol() {
178
+ return !this.isPrivateMemberSymbol;
179
+ }
180
+
181
+ get isStaticMemberSymbol() {
182
+ return !this.isInstanceMemberSymbol;
183
+ }
184
+
185
+ get isInstanceMemberSymbol() {
186
+ return (
187
+ this.spaces.length > 0 &&
188
+ this.spaces.filter(
189
+ (s) => s.key === "instance" || s.key === "private-instance",
190
+ ).length > 0
191
+ );
192
+ }
193
+
194
+ get isTypeSymbol() {
195
+ return !!(this.tsFlags & TSSymbolFlags.TypeSymbol);
196
+ }
197
+
198
+ get isValueSymbol() {
199
+ return this.spaces.some((s) => s.key === "values");
138
200
  }
139
201
  }
@@ -12,10 +12,6 @@ export interface TSPackageScopeOptions extends OutputScopeOptions {
12
12
  }
13
13
 
14
14
  export class TSPackageScope extends OutputScope {
15
- get kind() {
16
- return "package" as const;
17
- }
18
-
19
15
  /**
20
16
  * The version of the this package.
21
17
  */
@@ -74,7 +70,7 @@ export class TSPackageScope extends OutputScope {
74
70
  path: string,
75
71
  options: TSPackageScopeOptions = {},
76
72
  ) {
77
- super(name, options);
73
+ super(name, undefined, options);
78
74
  this.#version = version;
79
75
  this.#path = path;
80
76
  this.#builtin = !!options.builtin;
@@ -99,4 +95,8 @@ export class TSPackageScope extends OutputScope {
99
95
 
100
96
  return null;
101
97
  }
98
+
99
+ copyTo(): TSPackageScope {
100
+ throw new Error("Not supported");
101
+ }
102
102
  }
package/src/utils.ts CHANGED
@@ -1,5 +1,12 @@
1
- import { defaultProps, splitProps } from "@alloy-js/core";
1
+ import {
2
+ defaultProps,
3
+ splitProps,
4
+ useMemberContext,
5
+ useScope,
6
+ } from "@alloy-js/core";
2
7
  import { CallSignatureProps } from "./components/CallSignature.jsx";
8
+ import { TSLexicalScope } from "./symbols/ts-lexical-scope.js";
9
+ import { TSOutputSymbol } from "./symbols/ts-output-symbol.js";
3
10
 
4
11
  export function modulePath(path: string) {
5
12
  if (path[0] !== ".") {
@@ -38,3 +45,33 @@ const identifierRegex =
38
45
  export function isValidJSIdentifier(str: string) {
39
46
  return identifierRegex.test(str);
40
47
  }
48
+
49
+ export function useLexicalScope(): TSLexicalScope | undefined {
50
+ const scope = useScope();
51
+ if (!scope) {
52
+ return undefined;
53
+ }
54
+
55
+ if (!(scope instanceof TSLexicalScope)) {
56
+ throw new Error(
57
+ "A lexical scope is required but the current scope is not a lexical scope",
58
+ );
59
+ }
60
+
61
+ return scope;
62
+ }
63
+
64
+ export function useMemberOwner(): TSOutputSymbol {
65
+ const memberScope = useMemberContext();
66
+ if (!memberScope) {
67
+ throw new Error("A member owner is required but no member scope was found");
68
+ }
69
+
70
+ if (!(memberScope.ownerSymbol instanceof TSOutputSymbol)) {
71
+ throw new Error(
72
+ "A member owner is required but the current owner is not a TSOutputSymbol",
73
+ );
74
+ }
75
+
76
+ return memberScope.ownerSymbol;
77
+ }