@alloy-js/typescript 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/src/builtins/node.d.ts +9 -41
  3. package/dist/src/builtins/node.d.ts.map +1 -1
  4. package/dist/src/components/ArrowFunction.d.ts +3 -3
  5. package/dist/src/components/BarrelFile.d.ts +1 -1
  6. package/dist/src/components/BarrelFile.d.ts.map +1 -1
  7. package/dist/src/components/BlockScope.d.ts +1 -1
  8. package/dist/src/components/BlockScope.d.ts.map +1 -1
  9. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  10. package/dist/src/components/ClassDeclaration.js +5 -7
  11. package/dist/src/components/Declaration.d.ts.map +1 -1
  12. package/dist/src/components/Declaration.js +3 -4
  13. package/dist/src/components/EnumDeclaration.d.ts +1 -1
  14. package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
  15. package/dist/src/components/EnumDeclaration.js +3 -4
  16. package/dist/src/components/EnumMember.d.ts.map +1 -1
  17. package/dist/src/components/EnumMember.js +3 -4
  18. package/dist/src/components/FunctionBase.d.ts +4 -4
  19. package/dist/src/components/FunctionBase.d.ts.map +1 -1
  20. package/dist/src/components/FunctionBase.js +15 -10
  21. package/dist/src/components/FunctionDeclaration.d.ts +3 -3
  22. package/dist/src/components/FunctionExpression.d.ts +3 -3
  23. package/dist/src/components/FunctionType.d.ts +2 -2
  24. package/dist/src/components/ImportStatement.d.ts +1 -1
  25. package/dist/src/components/ImportStatement.d.ts.map +1 -1
  26. package/dist/src/components/Interface.js +1 -1
  27. package/dist/src/components/InterfaceMethod.d.ts +2 -2
  28. package/dist/src/components/MemberDeclaration.d.ts +1 -1
  29. package/dist/src/components/MemberDeclaration.d.ts.map +1 -1
  30. package/dist/src/components/MemberDeclaration.js +4 -4
  31. package/dist/src/components/MemberExpression.d.ts +6 -1
  32. package/dist/src/components/MemberExpression.d.ts.map +1 -1
  33. package/dist/src/components/MemberExpression.js +38 -14
  34. package/dist/src/components/NewExpression.d.ts +5 -0
  35. package/dist/src/components/NewExpression.d.ts.map +1 -0
  36. package/dist/src/components/NewExpression.js +12 -0
  37. package/dist/src/components/ObjectExpression.d.ts.map +1 -1
  38. package/dist/src/components/ObjectExpression.js +14 -15
  39. package/dist/src/components/PackageDirectory.d.ts.map +1 -1
  40. package/dist/src/components/PackageDirectory.js +3 -3
  41. package/dist/src/components/PackageJson.d.ts +1 -1
  42. package/dist/src/components/PackageJson.d.ts.map +1 -1
  43. package/dist/src/components/PropertyName.d.ts +1 -1
  44. package/dist/src/components/PropertyName.d.ts.map +1 -1
  45. package/dist/src/components/Reference.d.ts +1 -1
  46. package/dist/src/components/Reference.d.ts.map +1 -1
  47. package/dist/src/components/Reference.js +5 -1
  48. package/dist/src/components/SourceFile.d.ts.map +1 -1
  49. package/dist/src/components/SourceFile.js +3 -3
  50. package/dist/src/components/TsConfigJson.d.ts +1 -1
  51. package/dist/src/components/TsConfigJson.d.ts.map +1 -1
  52. package/dist/src/components/TypeDeclaration.d.ts +1 -1
  53. package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
  54. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  55. package/dist/src/components/VarDeclaration.js +30 -10
  56. package/dist/src/components/index.d.ts +1 -0
  57. package/dist/src/components/index.d.ts.map +1 -1
  58. package/dist/src/components/index.js +1 -0
  59. package/dist/src/components/stc/index.d.ts +45 -43
  60. package/dist/src/components/stc/index.d.ts.map +1 -1
  61. package/dist/src/components/stc/index.js +46 -44
  62. package/dist/src/create-package.d.ts +62 -18
  63. package/dist/src/create-package.d.ts.map +1 -1
  64. package/dist/src/create-package.js +153 -15
  65. package/dist/src/source-directory-data.d.ts.map +1 -1
  66. package/dist/src/source-directory-data.js +2 -2
  67. package/dist/src/symbols/reference.d.ts +2 -1
  68. package/dist/src/symbols/reference.d.ts.map +1 -1
  69. package/dist/src/symbols/reference.js +3 -4
  70. package/dist/src/symbols/scopes.d.ts +1 -4
  71. package/dist/src/symbols/scopes.d.ts.map +1 -1
  72. package/dist/src/symbols/ts-member-scope.d.ts +4 -6
  73. package/dist/src/symbols/ts-member-scope.d.ts.map +1 -1
  74. package/dist/src/symbols/ts-member-scope.js +8 -10
  75. package/dist/src/symbols/ts-module-scope.d.ts +8 -11
  76. package/dist/src/symbols/ts-module-scope.d.ts.map +1 -1
  77. package/dist/src/symbols/ts-module-scope.js +47 -40
  78. package/dist/src/symbols/ts-output-symbol.d.ts +23 -16
  79. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  80. package/dist/src/symbols/ts-output-symbol.js +88 -27
  81. package/dist/src/symbols/ts-package-scope.d.ts +18 -14
  82. package/dist/src/symbols/ts-package-scope.d.ts.map +1 -1
  83. package/dist/src/symbols/ts-package-scope.js +78 -27
  84. package/dist/test/class.test.js +2 -1
  85. package/dist/test/externals.test.js +87 -2
  86. package/dist/test/function-declaration.test.js +74 -3
  87. package/dist/test/member-expression.test.js +108 -2
  88. package/dist/test/new-expression.test.d.ts +2 -0
  89. package/dist/test/new-expression.test.d.ts.map +1 -0
  90. package/dist/test/new-expression.test.js +62 -0
  91. package/dist/test/var-declaration.test.d.ts.map +1 -1
  92. package/dist/test/var-declaration.test.js +145 -2
  93. package/dist/tsconfig.tsbuildinfo +1 -1
  94. package/package.json +4 -4
  95. package/src/components/ClassDeclaration.tsx +5 -7
  96. package/src/components/Declaration.tsx +3 -8
  97. package/src/components/EnumDeclaration.tsx +3 -4
  98. package/src/components/EnumMember.tsx +3 -4
  99. package/src/components/FunctionBase.tsx +25 -18
  100. package/src/components/ImportStatement.tsx +0 -1
  101. package/src/components/Interface.tsx +1 -1
  102. package/src/components/MemberDeclaration.tsx +5 -8
  103. package/src/components/MemberExpression.tsx +67 -15
  104. package/src/components/NewExpression.tsx +25 -0
  105. package/src/components/ObjectExpression.tsx +18 -16
  106. package/src/components/PackageDirectory.tsx +2 -10
  107. package/src/components/Reference.tsx +4 -2
  108. package/src/components/SourceFile.tsx +2 -5
  109. package/src/components/VarDeclaration.tsx +34 -11
  110. package/src/components/index.ts +1 -0
  111. package/src/components/stc/index.ts +46 -44
  112. package/src/create-package.ts +274 -35
  113. package/src/source-directory-data.ts +2 -2
  114. package/src/symbols/reference.ts +10 -5
  115. package/src/symbols/scopes.ts +1 -6
  116. package/src/symbols/ts-member-scope.ts +8 -23
  117. package/src/symbols/ts-module-scope.ts +58 -62
  118. package/src/symbols/ts-output-symbol.ts +108 -50
  119. package/src/symbols/ts-package-scope.ts +71 -53
  120. package/temp/api.json +1589 -1619
  121. package/test/class.test.tsx +2 -1
  122. package/test/externals.test.tsx +99 -1
  123. package/test/function-declaration.test.tsx +75 -2
  124. package/test/member-expression.test.tsx +93 -2
  125. package/test/new-expression.test.tsx +42 -0
  126. package/test/values.test.tsx +1 -0
  127. package/test/var-declaration.test.tsx +101 -1
  128. package/tsdoc-metadata.json +1 -1
@@ -1,13 +1,14 @@
1
1
  import {
2
- AssignmentContext,
3
2
  Children,
4
3
  Declaration as CoreDeclaration,
5
- createAssignmentContext,
4
+ createSymbolSlot,
5
+ effect,
6
6
  Name,
7
+ OutputSymbolFlags,
7
8
  Show,
8
9
  } from "@alloy-js/core";
9
10
  import { useTSNamePolicy } from "../name-policy.js";
10
- import { createTSSymbol, TSSymbolFlags } from "../symbols/index.js";
11
+ import { TSOutputSymbol, TSSymbolFlags } from "../symbols/ts-output-symbol.js";
11
12
  import { BaseDeclarationProps } from "./Declaration.js";
12
13
  import { JSDoc } from "./JSDoc.jsx";
13
14
  import { TypeRefContext } from "./TypeRefContext.jsx";
@@ -22,24 +23,46 @@ export interface VarDeclarationProps extends BaseDeclarationProps {
22
23
  }
23
24
 
24
25
  export function VarDeclaration(props: VarDeclarationProps) {
26
+ const TypeSymbolSlot = createSymbolSlot();
27
+ const ValueTypeSymbolSlot = createSymbolSlot();
28
+
29
+ effect(() => {
30
+ if (TypeSymbolSlot.ref.value) {
31
+ const takenSymbols = TypeSymbolSlot.ref.value;
32
+ for (const symbol of takenSymbols) {
33
+ symbol.instantiateTo(sym);
34
+ }
35
+ } else if (ValueTypeSymbolSlot.ref.value) {
36
+ const takenSymbols = ValueTypeSymbolSlot.ref.value;
37
+ for (const symbol of takenSymbols) {
38
+ // ignore non-transient symbols (likely not the result of an
39
+ // expression).
40
+ if (symbol.flags & OutputSymbolFlags.Transient) {
41
+ symbol.moveTo(sym);
42
+ }
43
+ }
44
+ }
45
+ });
46
+
25
47
  const keyword =
26
48
  props.var ? "var"
27
49
  : props.let ? "let"
28
50
  : "const";
29
51
  const type =
30
- props.type ? <TypeRefContext>: {props.type}</TypeRefContext> : undefined;
52
+ props.type ?
53
+ <TypeRefContext>
54
+ : <TypeSymbolSlot>{props.type}</TypeSymbolSlot>
55
+ </TypeRefContext>
56
+ : undefined;
31
57
  const name = useTSNamePolicy().getName(props.name, "variable");
32
- const sym = createTSSymbol({
33
- name: name,
34
- refkey: props.refkey,
58
+ const sym = new TSOutputSymbol(name, {
59
+ refkeys: props.refkey,
35
60
  default: props.default,
36
61
  export: props.export,
37
62
  metadata: props.metadata,
38
63
  tsFlags: props.nullish ? TSSymbolFlags.Nullish : TSSymbolFlags.None,
39
64
  });
40
65
 
41
- const assignmentContext = createAssignmentContext(sym);
42
-
43
66
  return (
44
67
  <>
45
68
  <Show when={Boolean(props.doc)}>
@@ -51,9 +74,9 @@ export function VarDeclaration(props: VarDeclarationProps) {
51
74
  {props.default ? "default " : ""}
52
75
  {keyword} <Name />
53
76
  {type} ={" "}
54
- <AssignmentContext.Provider value={assignmentContext}>
77
+ <ValueTypeSymbolSlot>
55
78
  {props.initializer ?? props.children}
56
- </AssignmentContext.Provider>
79
+ </ValueTypeSymbolSlot>
57
80
  </CoreDeclaration>
58
81
  </>
59
82
  );
@@ -27,6 +27,7 @@ export * from "./JSDoc.jsx";
27
27
  export * from "./JSDocComment.jsx";
28
28
  export * from "./JSDocExample.jsx";
29
29
  export * from "./MemberExpression.jsx";
30
+ export * from "./NewExpression.jsx";
30
31
  export * from "./ObjectExpression.js";
31
32
  export * from "./PackageDirectory.js";
32
33
  export * from "./PackageJson.js";
@@ -1,46 +1,48 @@
1
- import { stc } from "@alloy-js/core";
1
+ // prettier-ignore
2
+ import * as core from "@alloy-js/core";
2
3
  import * as base from "../index.js";
3
4
 
4
- export const ArrayExpression = stc(base.ArrayExpression);
5
- export const ArrowFunction = stc(base.ArrowFunction);
6
- export const BarrelFile = stc(base.BarrelFile);
7
- export const CallSignature = stc(base.CallSignature);
8
- export const ClassDeclaration = stc(base.ClassDeclaration);
9
- export const ClassField = stc(base.ClassField);
10
- export const ClassMember = stc(base.ClassMember);
11
- export const ClassMethod = stc(base.ClassMethod);
12
- export const CommaList = stc(base.CommaList);
13
- export const Declaration = stc(base.Declaration);
14
- export const EnumDeclaration = stc(base.EnumDeclaration);
15
- export const EnumMember = stc(base.EnumMember);
16
- export const ExportStatement = stc(base.ExportStatement);
17
- export const FunctionCallExpression = stc(base.FunctionCallExpression);
18
- export const FunctionDeclaration = stc(base.FunctionDeclaration);
19
- export const FunctionExpression = stc(base.FunctionExpression);
20
- export const IfStatement = stc(base.IfStatement);
21
- export const ElseClause = stc(base.ElseClause);
22
- export const ElseIfClause = stc(base.ElseIfClause);
23
- export const ImportStatement = stc(base.ImportStatement);
24
- export const ImportStatements = stc(base.ImportStatements);
25
- export const InterfaceDeclaration = stc(base.InterfaceDeclaration);
26
- export const InterfaceExpression = stc(base.InterfaceExpression);
27
- export const InterfaceMember = stc(base.InterfaceMember);
28
- export const InterfaceMethod = stc(base.InterfaceMethod);
29
- export const JSDoc = stc(base.JSDoc);
30
- export const JSDocExample = stc(base.JSDocExample);
31
- export const JSDocComment = stc(base.JSDocComment);
32
- export const MemberExpression = stc(base.MemberExpression);
33
- export const ObjectExpression = stc(base.ObjectExpression);
34
- export const ObjectProperty = stc(base.ObjectProperty);
35
- export const PackageDirectory = stc(base.PackageDirectory);
36
- export const PackageJsonFile = stc(base.PackageJsonFile);
37
- export const Reference = stc(base.Reference);
38
- export const SourceFile = stc(base.SourceFile);
39
- export const SwitchStatement = stc(base.SwitchStatement);
40
- export const CaseClause = stc(base.CaseClause);
41
- export const TSConfigJson = stc(base.TSConfigJson);
42
- export const FunctionType = stc(base.FunctionType);
43
- export const TypeDeclaration = stc(base.TypeDeclaration);
44
- export const TypeRefContext = stc(base.TypeRefContext);
45
- export const ValueExpression = stc(base.ValueExpression);
46
- export const VarDeclaration = stc(base.VarDeclaration);
5
+ export const ArrayExpression = core.stc(base.ArrayExpression);
6
+ export const ArrowFunction = core.stc(base.ArrowFunction);
7
+ export const BarrelFile = core.stc(base.BarrelFile);
8
+ export const CallSignature = core.stc(base.CallSignature);
9
+ export const ClassDeclaration = core.stc(base.ClassDeclaration);
10
+ export const ClassField = core.stc(base.ClassField);
11
+ export const ClassMember = core.stc(base.ClassMember);
12
+ export const ClassMethod = core.stc(base.ClassMethod);
13
+ export const CommaList = core.stc(base.CommaList);
14
+ export const Declaration = core.stc(base.Declaration);
15
+ export const EnumDeclaration = core.stc(base.EnumDeclaration);
16
+ export const EnumMember = core.stc(base.EnumMember);
17
+ export const ExportStatement = core.stc(base.ExportStatement);
18
+ export const FunctionCallExpression = core.stc(base.FunctionCallExpression);
19
+ export const FunctionDeclaration = core.stc(base.FunctionDeclaration);
20
+ export const FunctionExpression = core.stc(base.FunctionExpression);
21
+ export const IfStatement = core.stc(base.IfStatement);
22
+ export const ElseClause = core.stc(base.ElseClause);
23
+ export const ElseIfClause = core.stc(base.ElseIfClause);
24
+ export const ImportStatement = core.stc(base.ImportStatement);
25
+ export const ImportStatements = core.stc(base.ImportStatements);
26
+ export const InterfaceDeclaration = core.stc(base.InterfaceDeclaration);
27
+ export const InterfaceExpression = core.stc(base.InterfaceExpression);
28
+ export const InterfaceMember = core.stc(base.InterfaceMember);
29
+ export const InterfaceMethod = core.stc(base.InterfaceMethod);
30
+ export const JSDoc = core.stc(base.JSDoc);
31
+ export const JSDocExample = core.stc(base.JSDocExample);
32
+ export const JSDocComment = core.stc(base.JSDocComment);
33
+ export const MemberExpression = core.stc(base.MemberExpression);
34
+ export const NewExpression = core.stc(base.NewExpression);
35
+ export const ObjectExpression = core.stc(base.ObjectExpression);
36
+ export const ObjectProperty = core.stc(base.ObjectProperty);
37
+ export const PackageDirectory = core.stc(base.PackageDirectory);
38
+ export const PackageJsonFile = core.stc(base.PackageJsonFile);
39
+ export const Reference = core.stc(base.Reference);
40
+ export const SourceFile = core.stc(base.SourceFile);
41
+ export const SwitchStatement = core.stc(base.SwitchStatement);
42
+ export const CaseClause = core.stc(base.CaseClause);
43
+ export const TSConfigJson = core.stc(base.TSConfigJson);
44
+ export const FunctionType = core.stc(base.FunctionType);
45
+ export const TypeDeclaration = core.stc(base.TypeDeclaration);
46
+ export const TypeRefContext = core.stc(base.TypeRefContext);
47
+ export const ValueExpression = core.stc(base.ValueExpression);
48
+ export const VarDeclaration = core.stc(base.VarDeclaration);
@@ -1,24 +1,185 @@
1
1
  import {
2
2
  Binder,
3
3
  getSymbolCreatorSymbol,
4
+ OutputSymbolFlags,
4
5
  Refkey,
5
6
  refkey,
6
7
  SymbolCreator,
7
8
  } from "@alloy-js/core";
8
-
9
9
  import {
10
- createTSModuleScope,
11
- createTSPackageScope,
12
- createTSSymbol,
10
+ TSModuleScope,
11
+ TSOutputScope,
12
+ TSOutputSymbol,
13
+ TSPackageScope,
13
14
  } from "./symbols/index.js";
14
15
 
15
16
  export interface PackageDescriptor {
16
17
  [path: string]: ModuleSymbolsDescriptor;
17
18
  }
18
19
 
20
+ /**
21
+ * Describes the structure of a module descriptor.
22
+ *
23
+ * A module descriptor can either be a string (representing the module name)
24
+ * or an object with the following properties:
25
+ * - `name`: The name of the module.
26
+ * - `staticMembers`: An optional array of named module descriptors representing
27
+ * static members of the module.
28
+ * - `instanceMembers`: An optional array of named module descriptors representing
29
+ * instance members of the module.
30
+ */
31
+ export type NamedModuleDescriptor =
32
+ | string
33
+ | {
34
+ name: string;
35
+ staticMembers?: Array<NamedModuleDescriptor>;
36
+ instanceMembers?: Array<NamedModuleDescriptor>;
37
+ };
38
+
39
+ /**
40
+ * Describes the symbols exported by a module.
41
+ */
19
42
  export interface ModuleSymbolsDescriptor {
20
43
  default?: string;
21
- named?: readonly string[];
44
+ named?: NamedModuleDescriptor[];
45
+ }
46
+
47
+ /**
48
+ * Props for creating a package with a specific name, version, and descriptor.
49
+ */
50
+ export interface CreatePackageProps<T extends PackageDescriptor> {
51
+ name: string;
52
+ version: string;
53
+ descriptor: T;
54
+ builtin?: boolean;
55
+ }
56
+
57
+ /**
58
+ * Infers the exported members of a module based on its descriptor.
59
+ *
60
+ * D - The module descriptor, which may specify a `default` export (as a string)
61
+ * and/or an array of named exports (`named`).
62
+ *
63
+ * If `D` includes a `default` property of type `string`, the resulting type will include a
64
+ * `default` property of type `Refkey`. If `D` includes a `named` property (an array of
65
+ * `NamedModuleDescriptor`), the resulting type will include the mapped named exports as
66
+ * defined by `NamedMap`.
67
+ */
68
+ export type ModuleExports<
69
+ D extends { default?: string; named?: NamedModuleDescriptor[] },
70
+ > = (D extends { default: string } ? { default: Refkey } : {}) &
71
+ (D["named"] extends NamedModuleDescriptor[] ? NamedMap<D["named"]> : {});
72
+
73
+ export type PackageRefkeys<
74
+ PD extends Record<
75
+ string,
76
+ { default?: string; named?: NamedModuleDescriptor[] }
77
+ >,
78
+ // “Root” module descriptor at key `"."` becomes the “flat” exports on mcpSdk.*
79
+ > = ModuleExports<PD["."]> & {
80
+ // Every other module-path (e.g. "./foo/bar.js") lives under its own index:
81
+ // mcpSdk["./foo/bar.js"].<exports>
82
+ [P in keyof PD as P extends "." ? never : P]: ModuleExports<PD[P]>;
83
+ };
84
+
85
+ // NamedMap is a utility type that maps the named module descriptors
86
+ // to their corresponding Refkey types. It handles both string and object
87
+ // entries in the array of named module descriptors.
88
+ // It creates a mapping where:
89
+ // For each TDescriptor extends NamedModuleDescriptor[]:
90
+ // • string entries ➜ { [s]: Refkey }
91
+ // • object entries ➜ { [name]: Refkey & { static: …; instance: … } }
92
+ // ────────────────────────────────────────────────────────────────
93
+ export type NamedMap<TDescriptor extends readonly NamedModuleDescriptor[]> =
94
+ // plain-string exports
95
+ {
96
+ [S in Extract<TDescriptor[number], string>]: Refkey;
97
+ } & {
98
+ // object exports, each one is BOTH a Refkey _and_ has .static/.instance
99
+ [O in Extract<
100
+ TDescriptor[number],
101
+ { name: string }
102
+ > as O["name"]]: Refkey & {
103
+ static: O extends (
104
+ { staticMembers: infer SM extends NamedModuleDescriptor[] }
105
+ ) ?
106
+ NamedMap<SM>
107
+ : {};
108
+ instance: O extends (
109
+ { instanceMembers: infer IM extends NamedModuleDescriptor[] }
110
+ ) ?
111
+ NamedMap<IM>
112
+ : {};
113
+ };
114
+ };
115
+
116
+ function assignMembers(
117
+ binder: Binder,
118
+ ownerSym: TSOutputSymbol,
119
+ members: NamedModuleDescriptor[],
120
+ keys: Record<string, any>,
121
+ isStatic: boolean,
122
+ ) {
123
+ let scope: TSOutputScope;
124
+ if (isStatic) {
125
+ ownerSym.flags |= OutputSymbolFlags.StaticMemberContainer;
126
+ scope = ownerSym.staticMemberScope!;
127
+ } else {
128
+ ownerSym.flags |= OutputSymbolFlags.InstanceMemberContainer;
129
+ scope = ownerSym.instanceMemberScope!;
130
+ }
131
+
132
+ const namespace = isStatic ? "static" : "instance";
133
+
134
+ for (const member of members) {
135
+ const memberObj = typeof member === "object" ? member : { name: member };
136
+
137
+ // The refkey is located in the appropriate namespace
138
+ const memberKey = keys[namespace][memberObj.name];
139
+ if (!memberKey) continue; // Skip if key doesn't exist
140
+
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,
154
+ binder,
155
+ refkeys: memberKey,
156
+ export: false,
157
+ default: false,
158
+ flags: memberFlags,
159
+ });
160
+
161
+ scope.symbols.add(memberSym);
162
+
163
+ // Recursively handle nested static and instance members
164
+ if (memberObj.staticMembers && isStatic) {
165
+ assignMembers(
166
+ binder,
167
+ memberSym,
168
+ memberObj.staticMembers,
169
+ keys[namespace][memberObj.name],
170
+ true,
171
+ );
172
+ }
173
+ if (memberObj.instanceMembers && !isStatic) {
174
+ assignMembers(
175
+ binder,
176
+ memberSym,
177
+ memberObj.instanceMembers,
178
+ keys[namespace][memberObj.name],
179
+ false,
180
+ );
181
+ }
182
+ }
22
183
  }
23
184
 
24
185
  function createSymbols(
@@ -26,63 +187,100 @@ function createSymbols(
26
187
  props: CreatePackageProps<PackageDescriptor>,
27
188
  refkeys: Record<string, any>,
28
189
  ) {
29
- const pkgScope = createTSPackageScope(
30
- binder,
31
- undefined,
190
+ const pkgScope = new TSPackageScope(
32
191
  props.name,
33
192
  props.version,
34
193
  `node_modules/${props.name}`,
35
- props.builtin,
194
+ {
195
+ binder,
196
+ builtin: props.builtin,
197
+ },
36
198
  );
199
+
37
200
  for (const [path, symbols] of Object.entries(props.descriptor)) {
38
201
  const keys = path === "." ? refkeys : refkeys[path];
39
- const moduleScope = createTSModuleScope(binder, pkgScope, path);
202
+ const moduleScope = new TSModuleScope(path, {
203
+ binder,
204
+ parent: pkgScope,
205
+ });
40
206
 
41
207
  pkgScope.exportedSymbols.set(path, moduleScope);
42
208
 
43
209
  if (symbols.default) {
44
210
  const key = keys.default;
45
- const sym = createTSSymbol({
46
- name: symbols.default,
211
+ const sym = new TSOutputSymbol(symbols.default, {
47
212
  scope: moduleScope,
48
- refkey: key,
213
+ refkeys: key,
49
214
  export: true,
50
215
  default: true,
51
216
  });
52
-
53
217
  moduleScope.exportedSymbols.set(key, sym);
54
218
  }
55
219
 
56
220
  for (const exportedName of symbols.named ?? []) {
57
- const key = keys[exportedName];
58
- const sym = createTSSymbol({
59
- name: exportedName,
221
+ const namedRef =
222
+ typeof exportedName === "string" ?
223
+ { name: exportedName }
224
+ : exportedName;
225
+ 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, {
236
+ binder,
60
237
  scope: moduleScope,
61
- refkey: key,
238
+ refkeys: key,
62
239
  export: true,
63
240
  default: false,
241
+ flags,
64
242
  });
65
- moduleScope.exportedSymbols.set(key, sym);
243
+ moduleScope.exportedSymbols.set(key, ownerSym);
244
+
245
+ assignMembers(
246
+ binder,
247
+ ownerSym,
248
+ namedRef.staticMembers ?? [],
249
+ keys[namedRef.name],
250
+ /* isStatic */ true,
251
+ );
252
+ assignMembers(
253
+ binder,
254
+ ownerSym,
255
+ namedRef.instanceMembers ?? [],
256
+ keys[namedRef.name],
257
+ /* isStatic */ false,
258
+ );
66
259
  }
67
260
  }
68
261
  }
69
262
 
70
- export type PackageRefkeys<T extends PackageDescriptor> = {
71
- [K in keyof T as K extends "." ? never : K]: {
72
- [N in T[K]["named"] extends readonly string[] ? T[K]["named"][number]
73
- : never]: Refkey;
74
- } & (T[K] extends { default: string } ? { default: Refkey } : {});
75
- } & (T["."] extends { default: string } ? { default: Refkey } : {}) &
76
- (T["."] extends { named: readonly string[] } ?
77
- { [N in T["."]["named"][number]]: Refkey }
78
- : {});
263
+ function createRefkeysForMembers(
264
+ members: NamedModuleDescriptor[],
265
+ keys: Record<string, any>,
266
+ namespace: "static" | "instance",
267
+ ) {
268
+ keys[namespace] ??= {};
79
269
 
80
- export interface CreatePackageProps<T extends PackageDescriptor> {
81
- name: string;
82
- version: string;
83
- descriptor: T;
84
- builtin?: boolean;
270
+ for (const member of members) {
271
+ const memberObj = typeof member === "string" ? { name: member } : member;
272
+ const memberKey = refkey();
273
+ keys[namespace][memberObj.name] = memberKey;
274
+
275
+ if (memberObj.staticMembers?.length) {
276
+ createRefkeysForMembers(memberObj.staticMembers, memberKey, "static");
277
+ }
278
+ if (memberObj.instanceMembers?.length) {
279
+ createRefkeysForMembers(memberObj.instanceMembers, memberKey, "instance");
280
+ }
281
+ }
85
282
  }
283
+
86
284
  export function createPackage<const T extends PackageDescriptor>(
87
285
  props: CreatePackageProps<T>,
88
286
  ): PackageRefkeys<T> & SymbolCreator {
@@ -93,14 +291,55 @@ export function createPackage<const T extends PackageDescriptor>(
93
291
  };
94
292
 
95
293
  for (const [path, symbols] of Object.entries(props.descriptor)) {
96
- const keys = path === "." ? refkeys : ((refkeys[path] = {}), refkeys[path]);
294
+ const keys = path === "." ? refkeys : (refkeys[path] = {});
97
295
 
98
296
  if (symbols.default) {
99
297
  keys.default = refkey(props.descriptor, path, "default");
100
298
  }
101
299
 
102
300
  for (const named of symbols.named ?? []) {
103
- keys[named] = refkey(props.descriptor, path, named);
301
+ const namedObj = typeof named === "string" ? { name: named } : named;
302
+ keys[namedObj.name] = refkey();
303
+
304
+ if (namedObj.staticMembers?.length) {
305
+ createRefkeysForMembers(
306
+ namedObj.staticMembers,
307
+ keys[namedObj.name],
308
+ "static",
309
+ );
310
+ }
311
+
312
+ if (namedObj.instanceMembers?.length) {
313
+ createRefkeysForMembers(
314
+ namedObj.instanceMembers,
315
+ keys[namedObj.name],
316
+ "instance",
317
+ );
318
+ }
319
+
320
+ keys[namedObj.name][getSymbolCreatorSymbol()] = (
321
+ binder: Binder,
322
+ parentSym: TSOutputSymbol,
323
+ ) => {
324
+ if (namedObj.staticMembers?.length) {
325
+ assignMembers(
326
+ binder,
327
+ parentSym,
328
+ namedObj.staticMembers,
329
+ keys[namedObj.name],
330
+ true,
331
+ );
332
+ }
333
+ if (namedObj.instanceMembers?.length) {
334
+ assignMembers(
335
+ binder,
336
+ parentSym,
337
+ namedObj.instanceMembers,
338
+ keys[namedObj.name],
339
+ false,
340
+ );
341
+ }
342
+ };
104
343
  }
105
344
  }
106
345
 
@@ -1,4 +1,4 @@
1
- import { reactive, SourceDirectoryContext } from "@alloy-js/core";
1
+ import { shallowReactive, SourceDirectoryContext } from "@alloy-js/core";
2
2
  import { TSModuleScope } from "./symbols/index.js";
3
3
 
4
4
  export interface SourceDirectoryData {
@@ -16,7 +16,7 @@ export function getSourceDirectoryData(sdContext: SourceDirectoryContext) {
16
16
  }
17
17
 
18
18
  const sdData: SourceDirectoryData = {
19
- modules: reactive(new Set()),
19
+ modules: shallowReactive(new Set()),
20
20
  };
21
21
 
22
22
  sourceDirectoryData.set(sdContext, sdData);
@@ -23,7 +23,10 @@ import { TSPackageScope } from "./ts-package-scope.js";
23
23
  export interface RefOptions {
24
24
  type?: boolean;
25
25
  }
26
- export function ref(refkey: Refkey, options?: RefOptions): () => string {
26
+ export function ref(
27
+ refkey: Refkey,
28
+ options?: RefOptions,
29
+ ): () => [string, TSOutputSymbol | undefined] {
27
30
  const sourceFile = useContext(SourceFileContext);
28
31
  const resolveResult = resolve<TSOutputScope, TSOutputSymbol>(
29
32
  refkey as Refkey,
@@ -33,11 +36,10 @@ export function ref(refkey: Refkey, options?: RefOptions): () => string {
33
36
 
34
37
  return memo(() => {
35
38
  if (resolveResult.value === undefined) {
36
- return "<Unresolved Symbol>";
39
+ return ["<Unresolved Symbol>", undefined];
37
40
  }
38
41
 
39
42
  const { targetDeclaration, pathDown, memberPath } = resolveResult.value;
40
-
41
43
  // if we resolved a instance member, check if we should be able to access
42
44
  // it.
43
45
  if (targetDeclaration.flags & OutputSymbolFlags.InstanceMember) {
@@ -123,9 +125,12 @@ export function ref(refkey: Refkey, options?: RefOptions): () => string {
123
125
  memberPath[0] = localSymbol;
124
126
  }
125
127
 
126
- return buildMemberExpression(memberPath);
128
+ return [buildMemberExpression(memberPath), memberPath.at(-1)];
127
129
  } else {
128
- return buildMemberExpression([localSymbol ?? targetDeclaration]);
130
+ return [
131
+ buildMemberExpression([localSymbol ?? targetDeclaration]),
132
+ localSymbol ?? targetDeclaration,
133
+ ];
129
134
  }
130
135
  });
131
136
  }
@@ -8,8 +8,7 @@ export type TSOutputScope =
8
8
  | TSPackageScope
9
9
  | TSModuleScope
10
10
  | TSFunctionScope
11
- | TSMemberScope
12
- | TSOtherScope;
11
+ | TSMemberScope;
13
12
 
14
13
  export interface TSGlobalScope extends OutputScope {
15
14
  kind: "global";
@@ -19,10 +18,6 @@ export interface TSFunctionScope extends OutputScope {
19
18
  kind: "function";
20
19
  }
21
20
 
22
- export interface TSOtherScope extends OutputScope {
23
- kind: never;
24
- }
25
-
26
21
  export function useTSScope() {
27
22
  return useScope() as TSOutputScope;
28
23
  }
@@ -1,27 +1,12 @@
1
- import { Binder, OutputScope, OutputScopeFlags } from "@alloy-js/core";
1
+ import { OutputScope } from "@alloy-js/core";
2
2
  import { TSOutputSymbol } from "./ts-output-symbol.js";
3
3
 
4
- export interface TSMemberScope extends OutputScope {
5
- kind: "member";
6
- owner: TSOutputSymbol;
7
- isStatic: boolean;
8
- }
4
+ export class TSMemberScope extends OutputScope {
5
+ get kind() {
6
+ return "member" as const;
7
+ }
9
8
 
10
- export function createTSMemberScope(
11
- binder: Binder,
12
- parent: OutputScope | undefined,
13
- owner: TSOutputSymbol,
14
- isStatic: boolean = false,
15
- ): TSMemberScope {
16
- return binder.createScope<TSMemberScope>({
17
- kind: "member",
18
- name: "members",
19
- flags:
20
- isStatic ?
21
- OutputScopeFlags.StaticMemberScope
22
- : OutputScopeFlags.MemberScope,
23
- owner,
24
- parent,
25
- isStatic,
26
- });
9
+ get owner() {
10
+ return super.owner as TSOutputSymbol;
11
+ }
27
12
  }