@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
@@ -102,7 +102,9 @@ describe("instance members", () => {
102
102
  {one}
103
103
  </>,
104
104
  );
105
- }).toThrow(/Cannot resolve member symbols/);
105
+ }).toThrow(
106
+ /Cannot reference instance member 'one' without an instance of 'Foo'/,
107
+ );
106
108
  });
107
109
 
108
110
  it("cannot be referenced outside the class when protected", () => {
@@ -120,7 +122,9 @@ describe("instance members", () => {
120
122
  {one}
121
123
  </>,
122
124
  );
123
- }).toThrow(/Cannot resolve member symbols/);
125
+ }).toThrow(
126
+ /Cannot reference instance member 'one' without an instance of 'Foo'/,
127
+ );
124
128
  });
125
129
 
126
130
  it("cannot be referenced outside the class when private", () => {
@@ -138,7 +142,9 @@ describe("instance members", () => {
138
142
  {one}
139
143
  </>,
140
144
  );
141
- }).toThrow(/Cannot resolve private member symbols/);
145
+ }).toThrow(
146
+ /Cannot reference instance member 'one' without an instance of 'Foo'/,
147
+ );
142
148
  });
143
149
 
144
150
  it("works with invalid identifier names", () => {
@@ -310,7 +316,7 @@ describe("static members", () => {
310
316
  {two};
311
317
  </>,
312
318
  ),
313
- ).toThrow(/Cannot resolve/);
319
+ ).toThrow(/Cannot reference/);
314
320
  });
315
321
  });
316
322
 
@@ -1,12 +1,11 @@
1
1
  import { Output, render } from "@alloy-js/core";
2
- import { expect, it } from "vitest";
2
+ import { it } from "vitest";
3
3
  import { fs } from "../src/builtins/node.js";
4
4
  import {
5
5
  createPackage,
6
6
  FunctionDeclaration,
7
7
  PackageDirectory,
8
8
  SourceFile,
9
- TSOutputSymbol,
10
9
  } from "../src/index.js";
11
10
  import { assertFileContents } from "./utils.js";
12
11
 
@@ -196,41 +195,3 @@ it("can import static members", () => {
196
195
  `,
197
196
  });
198
197
  });
199
-
200
- it("can import instance members", () => {
201
- const mcpSdk = createPackage({
202
- name: "@modelcontextprotocol/sdk",
203
- version: "^3.23.0",
204
- descriptor: {
205
- "./server/index.js": {
206
- named: [{ name: "Server", instanceMembers: ["instanceHandler"] }],
207
- },
208
- },
209
- });
210
-
211
- function RunTest() {
212
- const sym = new TSOutputSymbol("foo");
213
- const binder = sym.binder!;
214
-
215
- expect(binder).toBeDefined();
216
-
217
- const source = binder.getSymbolForRefkey(
218
- mcpSdk["./server/index.js"].Server,
219
- ).value!;
220
- expect(source).toBeDefined();
221
- expect(source.instanceMemberScope?.symbols.size).toBe(1);
222
-
223
- source.instantiateTo(sym);
224
-
225
- expect(sym.staticMemberScope?.symbols.size).toBe(1);
226
- expect([...sym.staticMemberScope!.symbols][0].name).toBe("instanceHandler");
227
- }
228
-
229
- render(
230
- <Output externals={[mcpSdk]}>
231
- <SourceFile path="index.ts">
232
- <RunTest />
233
- </SourceFile>
234
- </Output>,
235
- );
236
- });
@@ -1,4 +1,4 @@
1
- import { Declaration, Output, refkey, render } from "@alloy-js/core";
1
+ import { Output, refkey, render } from "@alloy-js/core";
2
2
  import "@alloy-js/core/testing";
3
3
  import { it } from "vitest";
4
4
  import * as ts from "../src/components/index.js";
@@ -9,9 +9,9 @@ it("works with back references", () => {
9
9
  const res = render(
10
10
  <Output>
11
11
  <ts.SourceFile path="test1.ts">
12
- <Declaration name="foo" refkey={refkey("foo")}>
12
+ <ts.Declaration name="foo" refkey={refkey("foo")}>
13
13
  const foo = 1;
14
- </Declaration>
14
+ </ts.Declaration>
15
15
  </ts.SourceFile>
16
16
 
17
17
  <ts.SourceFile path="test2.ts">
@@ -39,9 +39,9 @@ it("works with forward references", () => {
39
39
  const v = <Reference refkey={refkey("foo")} />;
40
40
  </ts.SourceFile>
41
41
  <ts.SourceFile path="test1.ts">
42
- <Declaration name="foo" refkey={refkey("foo")}>
42
+ <ts.Declaration name="foo" refkey={refkey("foo")}>
43
43
  const foo = 1;
44
- </Declaration>
44
+ </ts.Declaration>
45
45
  </ts.SourceFile>
46
46
  </Output>,
47
47
  );
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.52.8"
8
+ "packageVersion": "7.52.9"
9
9
  }
10
10
  ]
11
11
  }
@@ -1,18 +0,0 @@
1
- import { ComponentContext } from "@alloy-js/core";
2
- import { TSMemberScope } from "../symbols/ts-member-scope.js";
3
- export interface PrivateScopeContext {
4
- /**
5
- * Scope for private static members.
6
- */
7
- staticMembers: TSMemberScope;
8
- /**
9
- * Scope for private instance members.
10
- */
11
- instanceMembers: TSMemberScope;
12
- }
13
- /**
14
- * Provides scopes for instance and static private members.
15
- */
16
- export declare const PrivateScopeContext: ComponentContext<PrivateScopeContext>;
17
- export declare function usePrivateScope(): PrivateScopeContext | undefined;
18
- //# sourceMappingURL=private-scope.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"private-scope.d.ts","sourceRoot":"","sources":["../../../src/context/private-scope.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAA6B,MAAM,gBAAgB,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAE9D,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,aAAa,EAAE,aAAa,CAAC;IAC7B;;OAEG;IACH,eAAe,EAAE,aAAa,CAAC;CAChC;AAED;;GAEG;AACH,eAAO,MAAM,mBAAmB,EAAE,gBAAgB,CAAC,mBAAmB,CAChC,CAAC;AAEvC,wBAAgB,eAAe,oCAE9B"}
@@ -1,8 +0,0 @@
1
- import { createContext, useContext } from "@alloy-js/core";
2
- /**
3
- * Provides scopes for instance and static private members.
4
- */
5
- export const PrivateScopeContext = createContext();
6
- export function usePrivateScope() {
7
- return useContext(PrivateScopeContext);
8
- }
@@ -1,23 +0,0 @@
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
- }
@@ -1,197 +0,0 @@
1
- import {
2
- memo,
3
- OutputSymbolFlags,
4
- Refkey,
5
- resolve,
6
- untrack,
7
- useContext,
8
- useMemberScope,
9
- } from "@alloy-js/core";
10
- import { usePackage } from "../components/PackageDirectory.jsx";
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";
17
- import { TSOutputScope } from "./scopes.js";
18
- import { TSMemberScope } from "./ts-member-scope.js";
19
- import { TSModuleScope } from "./ts-module-scope.js";
20
- import { TSOutputSymbol, TSSymbolFlags } from "./ts-output-symbol.js";
21
- import { TSPackageScope } from "./ts-package-scope.js";
22
-
23
- export interface RefOptions {
24
- type?: boolean;
25
- }
26
- export function ref(
27
- refkey: Refkey,
28
- options?: RefOptions,
29
- ): () => [string, TSOutputSymbol | undefined] {
30
- const sourceFile = useContext(SourceFileContext);
31
- const resolveResult = resolve<TSOutputScope, TSOutputSymbol>(
32
- refkey as Refkey,
33
- );
34
- const currentScope = useMemberScope();
35
- const currentPrivateScope = usePrivateScope();
36
-
37
- return memo(() => {
38
- if (resolveResult.value === undefined) {
39
- return ["<Unresolved Symbol>", undefined];
40
- }
41
-
42
- const { targetDeclaration, pathDown, memberPath } = resolveResult.value;
43
- // if we resolved a instance member, check if we should be able to access
44
- // it.
45
- if (targetDeclaration.flags & OutputSymbolFlags.InstanceMember) {
46
- if (targetDeclaration.tsFlags & TSSymbolFlags.PrivateMember) {
47
- if (currentPrivateScope?.instanceMembers !== targetDeclaration.scope) {
48
- throw new Error(
49
- "Cannot resolve private member symbols from a different scope",
50
- );
51
- }
52
- } else {
53
- if (currentScope?.instanceMembers !== targetDeclaration.scope) {
54
- throw new Error(
55
- "Cannot resolve member symbols from a different member scope",
56
- );
57
- }
58
- }
59
- }
60
-
61
- validateSymbolReachable(pathDown, memberPath, currentPrivateScope);
62
-
63
- // Where the target declaration is relative to the referencing scope.
64
- // * package: target symbol is in a different package
65
- // * module: target symbol is in a different module
66
- // * local: target symbol is within the current module
67
- const targetLocation = pathDown[0]?.kind ?? "local";
68
- let localSymbol: TSOutputSymbol | undefined;
69
-
70
- if (targetLocation === "package") {
71
- // need package import
72
- const pkg = usePackage();
73
- const sourcePackage = pathDown[0] as TSPackageScope;
74
-
75
- if (pkg && !sourcePackage.builtin) {
76
- pkg.scope.addDependency(sourcePackage);
77
- }
78
- const symbolPath = [
79
- ...(pathDown.slice(2) as TSMemberScope[]).map((s) => s.owner),
80
- targetDeclaration,
81
- ];
82
-
83
- const importSymbol = symbolPath[0];
84
-
85
- // find public dependency
86
- for (const module of sourcePackage.exportedSymbols.values()) {
87
- for (const refkey of importSymbol.refkeys) {
88
- if (module.exportedSymbols.has(refkey)) {
89
- localSymbol = untrack(() =>
90
- sourceFile!.scope.addImport(importSymbol, module, {
91
- type: options?.type,
92
- }),
93
- );
94
- break;
95
- }
96
- }
97
- }
98
-
99
- if (!localSymbol) {
100
- throw new Error(
101
- "The symbol " +
102
- targetDeclaration.name +
103
- " is not exported from package",
104
- );
105
- }
106
- } else if (targetLocation === "module") {
107
- const symbolPath = [
108
- ...(pathDown.slice(1) as TSMemberScope[]).map((s) => s.owner),
109
- targetDeclaration,
110
- ];
111
-
112
- const importSymbol = symbolPath[0];
113
-
114
- localSymbol = untrack(() =>
115
- sourceFile!.scope.addImport(
116
- importSymbol,
117
- pathDown[0] as TSModuleScope,
118
- { type: options?.type },
119
- ),
120
- );
121
- }
122
-
123
- if (memberPath && memberPath.length > 0) {
124
- if (localSymbol) {
125
- memberPath[0] = localSymbol;
126
- }
127
-
128
- return [buildMemberExpression(memberPath), memberPath.at(-1)];
129
- } else {
130
- return [
131
- buildMemberExpression([localSymbol ?? targetDeclaration]),
132
- localSymbol ?? targetDeclaration,
133
- ];
134
- }
135
- });
136
- }
137
-
138
- function buildMemberExpression(path: TSOutputSymbol[]) {
139
- let memberExpr = "";
140
-
141
- const base = path[0];
142
- if (base.flags & OutputSymbolFlags.InstanceMember) {
143
- memberExpr += "this";
144
- } else {
145
- memberExpr += base.name;
146
- path = path.slice(1);
147
- }
148
-
149
- let prevNullish = base.tsFlags & TSSymbolFlags.Nullish;
150
- for (const sym of path) {
151
- const optional = prevNullish ? "?" : "";
152
- if (sym.tsFlags & TSSymbolFlags.PrivateMember) {
153
- memberExpr += `${optional}.#${sym.name}`;
154
- } else if (
155
- isValidJSIdentifier(sym.name) &&
156
- !(sym.tsFlags & TSSymbolFlags.TypeSymbol)
157
- ) {
158
- memberExpr += `${optional}.${sym.name}`;
159
- } else {
160
- const joiner = sym.tsFlags & TSSymbolFlags.Nullish ? "?." : "";
161
- memberExpr += `${joiner}[${JSON.stringify(sym.name)}]`;
162
- }
163
- prevNullish = sym.tsFlags & TSSymbolFlags.Nullish;
164
- }
165
-
166
- return memberExpr;
167
- }
168
-
169
- function validateSymbolReachable(
170
- path: TSOutputScope[],
171
- memberPath: TSOutputSymbol[] | undefined,
172
- currentPrivateScope: PrivateScopeContext | undefined,
173
- ) {
174
- for (const scope of path) {
175
- if (scope.kind === "function") {
176
- throw new Error(
177
- "Cannot reference a symbol inside a function from outside a function",
178
- );
179
- }
180
- }
181
-
182
- if (memberPath) {
183
- for (const sym of memberPath) {
184
- // make sure we're not trying to access a static private from outside
185
- // the static member scope.
186
- if (
187
- sym.flags & OutputSymbolFlags.StaticMember &&
188
- sym.tsFlags & TSSymbolFlags.PrivateMember &&
189
- currentPrivateScope?.staticMembers !== sym.scope
190
- ) {
191
- throw new Error(
192
- "Cannot resolve private static member symbols from a different scope",
193
- );
194
- }
195
- }
196
- }
197
- }