@alloy-js/python 0.1.0-dev.4 → 0.1.0-dev.6

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 (181) hide show
  1. package/dist/src/builtins/python.js +2 -1
  2. package/dist/src/builtins/python.js.map +1 -0
  3. package/dist/src/components/Atom.js +2 -1
  4. package/dist/src/components/Atom.js.map +1 -0
  5. package/dist/src/components/CallSignature.d.ts.map +1 -1
  6. package/dist/src/components/CallSignature.js +10 -12
  7. package/dist/src/components/CallSignature.js.map +1 -0
  8. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  9. package/dist/src/components/ClassDeclaration.js +8 -21
  10. package/dist/src/components/ClassDeclaration.js.map +1 -0
  11. package/dist/src/components/ClassInstantiation.d.ts.map +1 -1
  12. package/dist/src/components/ClassInstantiation.js +6 -5
  13. package/dist/src/components/ClassInstantiation.js.map +1 -0
  14. package/dist/src/components/Declaration.d.ts +1 -5
  15. package/dist/src/components/Declaration.d.ts.map +1 -1
  16. package/dist/src/components/Declaration.js +8 -16
  17. package/dist/src/components/Declaration.js.map +1 -0
  18. package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
  19. package/dist/src/components/EnumDeclaration.js +35 -60
  20. package/dist/src/components/EnumDeclaration.js.map +1 -0
  21. package/dist/src/components/EnumMember.d.ts.map +1 -1
  22. package/dist/src/components/EnumMember.js +5 -5
  23. package/dist/src/components/EnumMember.js.map +1 -0
  24. package/dist/src/components/FunctionCallExpression.js +2 -1
  25. package/dist/src/components/FunctionCallExpression.js.map +1 -0
  26. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  27. package/dist/src/components/FunctionDeclaration.js +8 -16
  28. package/dist/src/components/FunctionDeclaration.js.map +1 -0
  29. package/dist/src/components/ImportStatement.js +2 -1
  30. package/dist/src/components/ImportStatement.js.map +1 -0
  31. package/dist/src/components/LexicalScope.d.ts +8 -0
  32. package/dist/src/components/LexicalScope.d.ts.map +1 -0
  33. package/dist/src/components/LexicalScope.js +22 -0
  34. package/dist/src/components/LexicalScope.js.map +1 -0
  35. package/dist/src/components/MemberExpression.js +2 -1
  36. package/dist/src/components/MemberExpression.js.map +1 -0
  37. package/dist/src/components/MemberScope.d.ts +8 -0
  38. package/dist/src/components/MemberScope.d.ts.map +1 -0
  39. package/dist/src/components/MemberScope.js +16 -0
  40. package/dist/src/components/MemberScope.js.map +1 -0
  41. package/dist/src/components/NoNamePolicy.js +2 -1
  42. package/dist/src/components/NoNamePolicy.js.map +1 -0
  43. package/dist/src/components/PyDoc.js +2 -1
  44. package/dist/src/components/PyDoc.js.map +1 -0
  45. package/dist/src/components/PythonBlock.js +2 -1
  46. package/dist/src/components/PythonBlock.js.map +1 -0
  47. package/dist/src/components/Reference.js +2 -1
  48. package/dist/src/components/Reference.js.map +1 -0
  49. package/dist/src/components/SourceFile.js +3 -3
  50. package/dist/src/components/SourceFile.js.map +1 -0
  51. package/dist/src/components/StatementList.js +2 -1
  52. package/dist/src/components/StatementList.js.map +1 -0
  53. package/dist/src/components/UnionTypeExpression.js +2 -1
  54. package/dist/src/components/UnionTypeExpression.js.map +1 -0
  55. package/dist/src/components/VariableDeclaration.d.ts.map +1 -1
  56. package/dist/src/components/VariableDeclaration.js +10 -32
  57. package/dist/src/components/VariableDeclaration.js.map +1 -0
  58. package/dist/src/components/index.d.ts +2 -0
  59. package/dist/src/components/index.d.ts.map +1 -1
  60. package/dist/src/components/index.js +4 -1
  61. package/dist/src/components/index.js.map +1 -0
  62. package/dist/src/create-module.d.ts.map +1 -1
  63. package/dist/src/create-module.js +5 -5
  64. package/dist/src/create-module.js.map +1 -0
  65. package/dist/src/index.js +2 -1
  66. package/dist/src/index.js.map +1 -0
  67. package/dist/src/name-conflict-resolver.d.ts +3 -0
  68. package/dist/src/name-conflict-resolver.d.ts.map +1 -0
  69. package/dist/src/name-conflict-resolver.js +8 -0
  70. package/dist/src/name-conflict-resolver.js.map +1 -0
  71. package/dist/src/name-policy.js +2 -1
  72. package/dist/src/name-policy.js.map +1 -0
  73. package/dist/src/parameter-descriptor.js +2 -1
  74. package/dist/src/parameter-descriptor.js.map +1 -0
  75. package/dist/src/symbol-creation.d.ts +13 -2
  76. package/dist/src/symbol-creation.d.ts.map +1 -1
  77. package/dist/src/symbol-creation.js +35 -10
  78. package/dist/src/symbol-creation.js.map +1 -0
  79. package/dist/src/symbols/index.d.ts +1 -1
  80. package/dist/src/symbols/index.d.ts.map +1 -1
  81. package/dist/src/symbols/index.js +3 -2
  82. package/dist/src/symbols/index.js.map +1 -0
  83. package/dist/src/symbols/python-lexical-scope.d.ts +7 -0
  84. package/dist/src/symbols/python-lexical-scope.d.ts.map +1 -0
  85. package/dist/src/symbols/python-lexical-scope.js +15 -0
  86. package/dist/src/symbols/python-lexical-scope.js.map +1 -0
  87. package/dist/src/symbols/python-member-scope.d.ts +3 -4
  88. package/dist/src/symbols/python-member-scope.d.ts.map +1 -1
  89. package/dist/src/symbols/python-member-scope.js +6 -8
  90. package/dist/src/symbols/python-member-scope.js.map +1 -0
  91. package/dist/src/symbols/python-module-scope.d.ts +4 -9
  92. package/dist/src/symbols/python-module-scope.d.ts.map +1 -1
  93. package/dist/src/symbols/python-module-scope.js +8 -15
  94. package/dist/src/symbols/python-module-scope.js.map +1 -0
  95. package/dist/src/symbols/python-output-symbol.d.ts +10 -7
  96. package/dist/src/symbols/python-output-symbol.d.ts.map +1 -1
  97. package/dist/src/symbols/python-output-symbol.js +25 -7
  98. package/dist/src/symbols/python-output-symbol.js.map +1 -0
  99. package/dist/src/symbols/reference.d.ts +2 -2
  100. package/dist/src/symbols/reference.d.ts.map +1 -1
  101. package/dist/src/symbols/reference.js +37 -36
  102. package/dist/src/symbols/reference.js.map +1 -0
  103. package/dist/src/symbols/scopes.d.ts +3 -1
  104. package/dist/src/symbols/scopes.d.ts.map +1 -1
  105. package/dist/src/symbols/scopes.js +10 -1
  106. package/dist/src/symbols/scopes.js.map +1 -0
  107. package/dist/src/utils.js +2 -1
  108. package/dist/src/utils.js.map +1 -0
  109. package/dist/test/callsignatures.test.js +2 -1
  110. package/dist/test/callsignatures.test.js.map +1 -0
  111. package/dist/test/classdeclarations.test.js +89 -53
  112. package/dist/test/classdeclarations.test.js.map +1 -0
  113. package/dist/test/classinstantiations.test.js +2 -1
  114. package/dist/test/classinstantiations.test.js.map +1 -0
  115. package/dist/test/enums.test.js +10 -3
  116. package/dist/test/enums.test.js.map +1 -0
  117. package/dist/test/externals.test.js +8 -6
  118. package/dist/test/externals.test.js.map +1 -0
  119. package/dist/test/functioncallexpressions.test.js +2 -1
  120. package/dist/test/functioncallexpressions.test.js.map +1 -0
  121. package/dist/test/functiondeclaration.test.js +75 -37
  122. package/dist/test/functiondeclaration.test.js.map +1 -0
  123. package/dist/test/imports.test.js +11 -37
  124. package/dist/test/imports.test.js.map +1 -0
  125. package/dist/test/memberexpressions.test.js +2 -1
  126. package/dist/test/memberexpressions.test.js.map +1 -0
  127. package/dist/test/namepolicies.test.js +2 -1
  128. package/dist/test/namepolicies.test.js.map +1 -0
  129. package/dist/test/pydocs.test.js +2 -1
  130. package/dist/test/pydocs.test.js.map +1 -0
  131. package/dist/test/references.test.js +2 -1
  132. package/dist/test/references.test.js.map +1 -0
  133. package/dist/test/sourcefiles.test.js +2 -1
  134. package/dist/test/sourcefiles.test.js.map +1 -0
  135. package/dist/test/uniontypeexpression.test.js +2 -1
  136. package/dist/test/uniontypeexpression.test.js.map +1 -0
  137. package/dist/test/utils.d.ts +2 -3
  138. package/dist/test/utils.d.ts.map +1 -1
  139. package/dist/test/utils.js +8 -4
  140. package/dist/test/utils.js.map +1 -0
  141. package/dist/test/values.test.js +2 -1
  142. package/dist/test/values.test.js.map +1 -0
  143. package/dist/test/variables.test.js +13 -12
  144. package/dist/test/variables.test.js.map +1 -0
  145. package/dist/tsconfig.tsbuildinfo +1 -1
  146. package/package.json +3 -3
  147. package/src/components/CallSignature.tsx +9 -17
  148. package/src/components/ClassDeclaration.tsx +5 -32
  149. package/src/components/ClassInstantiation.tsx +6 -12
  150. package/src/components/Declaration.tsx +1 -20
  151. package/src/components/EnumDeclaration.tsx +18 -41
  152. package/src/components/EnumMember.tsx +1 -3
  153. package/src/components/FunctionDeclaration.tsx +5 -24
  154. package/src/components/LexicalScope.tsx +24 -0
  155. package/src/components/MemberScope.tsx +18 -0
  156. package/src/components/SourceFile.tsx +2 -2
  157. package/src/components/VariableDeclaration.tsx +7 -35
  158. package/src/components/index.ts +2 -0
  159. package/src/create-module.ts +7 -13
  160. package/src/name-conflict-resolver.ts +21 -0
  161. package/src/symbol-creation.ts +50 -11
  162. package/src/symbols/index.ts +1 -1
  163. package/src/symbols/python-lexical-scope.ts +16 -0
  164. package/src/symbols/python-member-scope.ts +4 -8
  165. package/src/symbols/python-module-scope.ts +6 -32
  166. package/src/symbols/python-output-symbol.ts +36 -10
  167. package/src/symbols/reference.tsx +70 -0
  168. package/src/symbols/scopes.ts +15 -1
  169. package/temp/api.json +897 -539
  170. package/test/classdeclarations.test.tsx +66 -31
  171. package/test/enums.test.tsx +2 -2
  172. package/test/externals.test.tsx +6 -7
  173. package/test/functiondeclaration.test.tsx +48 -39
  174. package/test/imports.test.tsx +11 -36
  175. package/test/utils.tsx +11 -5
  176. package/test/variables.test.tsx +11 -7
  177. package/dist/src/symbols/custom-output-scope.d.ts +0 -10
  178. package/dist/src/symbols/custom-output-scope.d.ts.map +0 -1
  179. package/dist/src/symbols/custom-output-scope.js +0 -25
  180. package/src/symbols/custom-output-scope.ts +0 -35
  181. package/src/symbols/reference.ts +0 -99
@@ -4,6 +4,7 @@ import {
4
4
  createSymbolSlot,
5
5
  For,
6
6
  Show,
7
+ SymbolSlot,
7
8
  useContext,
8
9
  } from "@alloy-js/core";
9
10
  import { ParameterDescriptor } from "../parameter-descriptor.js";
@@ -87,15 +88,12 @@ export function CallSignatureParameters(props: CallSignatureParametersProps) {
87
88
  }
88
89
 
89
90
  function parameter(param: DeclaredParameterDescriptor) {
90
- const SymbolSlot = createSymbolSlot();
91
-
92
- SymbolSlot.instantiateInto(param.symbol);
93
-
91
+ const TypeSlot = param.TypeSlot!; // TypeSlot will always be present when param.type is true.
94
92
  return (
95
93
  <group>
96
94
  {param.symbol.name}
97
95
  <Show when={!!param.type}>
98
- : <SymbolSlot>{param.type}</SymbolSlot>
96
+ : <TypeSlot>{param.type}</TypeSlot>
99
97
  </Show>
100
98
  <Show when={!!param.optional}>
101
99
  <Show when={!param.type}>=</Show>
@@ -120,44 +118,38 @@ function parameter(param: DeclaredParameterDescriptor) {
120
118
  interface DeclaredParameterDescriptor
121
119
  extends Omit<ParameterDescriptor, "name"> {
122
120
  symbol: PythonOutputSymbol;
121
+ TypeSlot?: SymbolSlot;
123
122
  }
124
123
 
125
124
  function normalizeAndDeclareParameters(
126
125
  parameters: ParameterDescriptor[] | string[],
127
126
  ): DeclaredParameterDescriptor[] {
128
- const sfContext = useContext(PythonSourceFileContext);
129
- const module = sfContext?.module;
130
127
  if (parameters.length === 0) {
131
128
  return [];
132
129
  }
133
130
  if (typeof parameters[0] === "string") {
134
131
  return (parameters as string[]).map((paramName) => {
135
- const symbol = createPythonSymbol(
136
- paramName,
137
- {
138
- module: module,
139
- },
140
- "parameter",
141
- false,
142
- );
132
+ const symbol = createPythonSymbol(paramName, {}, "parameter");
143
133
 
144
134
  return { refkeys: symbol.refkeys, symbol };
145
135
  });
146
136
  } else {
147
137
  return (parameters as ParameterDescriptor[]).map((param) => {
138
+ const TypeSlot = createSymbolSlot();
139
+
148
140
  const symbol = createPythonSymbol(
149
141
  param.name,
150
142
  {
151
143
  refkeys: param.refkey,
152
- module: module,
144
+ type: TypeSlot.firstSymbol,
153
145
  },
154
146
  "parameter",
155
- false,
156
147
  );
157
148
 
158
149
  return {
159
150
  ...param,
160
151
  symbol,
152
+ TypeSlot,
161
153
  };
162
154
  });
163
155
  }
@@ -1,19 +1,7 @@
1
- import {
2
- Children,
3
- List,
4
- Name,
5
- OutputSymbolFlags,
6
- Scope,
7
- Show,
8
- childrenArray,
9
- takeSymbols,
10
- } from "@alloy-js/core";
1
+ import { Children, List, Name, Show, childrenArray } from "@alloy-js/core";
11
2
  import { createPythonSymbol } from "../symbol-creation.js";
12
- import {
13
- BaseDeclarationProps,
14
- Declaration,
15
- DeclarationProps,
16
- } from "./Declaration.js";
3
+ import { BaseDeclarationProps, Declaration } from "./Declaration.js";
4
+ import { MemberScope } from "./MemberScope.jsx";
17
5
  import { PythonBlock } from "./PythonBlock.jsx";
18
6
 
19
7
  export interface ClassDeclarationProps extends BaseDeclarationProps {
@@ -61,38 +49,23 @@ export function ClassDeclaration(props: ClassDeclarationProps) {
61
49
  props.name!,
62
50
  {
63
51
  refkeys: props.refkey,
64
- flags:
65
- (props.flags ?? OutputSymbolFlags.None) |
66
- OutputSymbolFlags.MemberContainer,
67
52
  },
68
53
  "class",
69
- true,
70
54
  );
71
55
 
72
- takeSymbols((memberSymbol) => {
73
- // Transform emitted symbols into instance/class members
74
- memberSymbol.flags |= OutputSymbolFlags.InstanceMember;
75
- });
76
-
77
- // Propagate the name after the name policy was applied
78
- const updatedProps: DeclarationProps = {
79
- ...props,
80
- name: sym.name,
81
- nameKind: "class",
82
- };
83
56
  const hasChildren =
84
57
  childrenArray(() => props.children).filter((c) => Boolean(c)).length > 0;
85
58
 
86
59
  return (
87
60
  <Declaration symbol={sym}>
88
61
  class <Name />
89
- <Scope name={updatedProps.name} kind="class">
62
+ <MemberScope ownerSymbol={sym}>
90
63
  {basesPart}
91
64
  <PythonBlock opener=":">
92
65
  <Show when={Boolean(props.doc)}>{props.doc}</Show>
93
66
  {hasChildren ? props.children : "pass"}
94
67
  </PythonBlock>
95
- </Scope>
68
+ </MemberScope>
96
69
  </Declaration>
97
70
  );
98
71
  }
@@ -1,7 +1,6 @@
1
1
  import {
2
2
  emitSymbol,
3
- instantiateTakenMembersTo as instantiateTakenSymbolsTo,
4
- OutputSymbolFlags,
3
+ instantiateTakenMembersTo,
5
4
  useContext,
6
5
  } from "@alloy-js/core";
7
6
  import { createPythonSymbol } from "../symbol-creation.js";
@@ -35,16 +34,11 @@ export interface ClassInstantiationProps extends FunctionCallExpressionProps {}
35
34
  export function ClassInstantiation(props: ClassInstantiationProps) {
36
35
  const sfContext = useContext(PythonSourceFileContext);
37
36
  const module = sfContext?.module;
38
- const sym = createPythonSymbol(
39
- "",
40
- {
41
- flags: OutputSymbolFlags.Transient,
42
- module: module,
43
- },
44
- undefined,
45
- false,
46
- );
47
- instantiateTakenSymbolsTo(sym);
37
+ const sym = createPythonSymbol("", {
38
+ transient: true,
39
+ module: module,
40
+ });
41
+ instantiateTakenMembersTo(sym, "static", "instance");
48
42
  emitSymbol(sym);
49
43
  return (
50
44
  <>
@@ -1,8 +1,6 @@
1
1
  import {
2
2
  Children,
3
3
  Declaration as CoreDeclaration,
4
- MemberScope,
5
- OutputSymbolFlags,
6
4
  Refkey,
7
5
  } from "@alloy-js/core";
8
6
  import { PythonElements } from "../name-policy.js";
@@ -21,11 +19,6 @@ export interface BaseDeclarationProps {
21
19
  */
22
20
  refkey?: Refkey | Refkey[];
23
21
 
24
- /**
25
- * Flags for the symbol created by this component.
26
- */
27
- flags?: OutputSymbolFlags;
28
-
29
22
  children?: Children;
30
23
 
31
24
  /**
@@ -70,22 +63,10 @@ export function Declaration(props: DeclarationProps) {
70
63
  props.name!,
71
64
  {
72
65
  refkeys: props.refkey,
73
- flags: props.flags,
74
66
  },
75
67
  props.nameKind!,
76
- true,
77
68
  );
78
69
  }
79
70
 
80
- function withMemberScope(children: Children) {
81
- return <MemberScope owner={sym}>{children}</MemberScope>;
82
- }
83
-
84
- let children: Children = () => props.children;
85
-
86
- if (sym.flags & OutputSymbolFlags.MemberContainer) {
87
- children = withMemberScope(children);
88
- }
89
-
90
- return <CoreDeclaration symbol={sym}>{children}</CoreDeclaration>;
71
+ return <CoreDeclaration symbol={sym}>{props.children}</CoreDeclaration>;
91
72
  }
@@ -2,19 +2,15 @@ import {
2
2
  Children,
3
3
  Declaration as CoreDeclaration,
4
4
  For,
5
- MemberScope,
6
- OutputSymbolFlags,
7
- Scope,
8
5
  Show,
9
- useBinder,
10
6
  } from "@alloy-js/core";
11
7
  import { enumModule } from "../builtins/python.js";
12
8
  import { createPythonSymbol } from "../symbol-creation.js";
13
- import { usePythonScope } from "../symbols/scopes.js";
14
9
  import { BaseDeclarationProps } from "./Declaration.js";
15
10
  import { EnumMember } from "./EnumMember.js";
16
- import { PythonBlock } from "./PythonBlock.jsx";
17
11
  import { SimpleCommentBlock } from "./index.js";
12
+ import { MemberScope } from "./MemberScope.jsx";
13
+ import { PythonBlock } from "./PythonBlock.jsx";
18
14
 
19
15
  export interface EnumProps extends BaseDeclarationProps {
20
16
  /**
@@ -115,18 +111,12 @@ export function EnumDeclaration(props: EnumProps) {
115
111
  * ```
116
112
  */
117
113
  export function FunctionalEnumDeclaration(props: EnumProps) {
118
- const binder = useBinder();
119
- const scope = usePythonScope();
120
114
  const sym = createPythonSymbol(
121
115
  props.name,
122
116
  {
123
- binder: binder,
124
- scope: scope,
125
117
  refkeys: props.refkey,
126
- flags: OutputSymbolFlags.StaticMemberContainer,
127
118
  },
128
119
  "enum",
129
- false,
130
120
  );
131
121
  const members = props.members ?? [];
132
122
  let opener, ender;
@@ -152,12 +142,7 @@ export function FunctionalEnumDeclaration(props: EnumProps) {
152
142
  <>
153
143
  <CoreDeclaration symbol={sym}>
154
144
  {sym.name} = {enumModule["."].Enum}('{sym.name}',{" "}
155
- <MemberScope owner={sym}>
156
- <Scope name={props.name} kind="enum">
157
- {memberExpr}
158
- </Scope>
159
- </MemberScope>
160
- )
145
+ <MemberScope ownerSymbol={sym}>{memberExpr}</MemberScope>)
161
146
  </CoreDeclaration>
162
147
  </>
163
148
  );
@@ -234,18 +219,12 @@ export function FunctionalEnumDeclaration(props: EnumProps) {
234
219
  */
235
220
  export function ClassEnumDeclaration(props: EnumProps) {
236
221
  const baseType = props.baseType || "Enum";
237
- const binder = useBinder();
238
- const scope = usePythonScope();
239
222
  const sym = createPythonSymbol(
240
223
  props.name,
241
224
  {
242
- binder: binder,
243
- scope: scope,
244
225
  refkeys: props.refkey,
245
- flags: OutputSymbolFlags.StaticMemberContainer,
246
226
  },
247
227
  "enum",
248
- false,
249
228
  );
250
229
  let memberList: Array<{
251
230
  name: string;
@@ -268,23 +247,21 @@ export function ClassEnumDeclaration(props: EnumProps) {
268
247
  <hbr />
269
248
  </Show>
270
249
  class {sym.name}({enumModule["."][baseType]})
271
- <MemberScope owner={sym}>
272
- <Scope name={sym.name} kind="enum">
273
- <PythonBlock opener=":">
274
- <For each={memberList} hardline>
275
- {(member) => (
276
- <EnumMember
277
- name={member.name}
278
- value={member.value}
279
- jsValue={member.jsValue}
280
- auto={member.auto}
281
- doc={member.doc}
282
- />
283
- )}
284
- </For>
285
- {props.children}
286
- </PythonBlock>
287
- </Scope>
250
+ <MemberScope ownerSymbol={sym}>
251
+ <PythonBlock opener=":">
252
+ <For each={memberList} hardline>
253
+ {(member) => (
254
+ <EnumMember
255
+ name={member.name}
256
+ value={member.value}
257
+ jsValue={member.jsValue}
258
+ auto={member.auto}
259
+ doc={member.doc}
260
+ />
261
+ )}
262
+ </For>
263
+ {props.children}
264
+ </PythonBlock>
288
265
  </MemberScope>
289
266
  </CoreDeclaration>
290
267
  );
@@ -1,4 +1,4 @@
1
- import { Children, OutputSymbolFlags, Refkey, Show } from "@alloy-js/core";
1
+ import { Children, Refkey, Show } from "@alloy-js/core";
2
2
  import { enumModule } from "../builtins/python.js";
3
3
  import { createPythonSymbol } from "../symbol-creation.js";
4
4
  import { PythonOutputSymbol } from "../symbols/index.js";
@@ -62,10 +62,8 @@ export function EnumMember(props: EnumMemberProps) {
62
62
  props.name,
63
63
  {
64
64
  refkeys: props.refkey,
65
- flags: OutputSymbolFlags.StaticMember,
66
65
  },
67
66
  "enum-member",
68
- true,
69
67
  );
70
68
  const valueCode =
71
69
  props.jsValue !== undefined ? <Atom jsValue={props.jsValue} /> : value;
@@ -1,19 +1,10 @@
1
- import {
2
- emitSymbol,
3
- Name,
4
- OutputScope,
5
- OutputSymbolFlags,
6
- Scope,
7
- Show,
8
- useMemberScope,
9
- useScope,
10
- } from "@alloy-js/core";
1
+ import { emitSymbol, Name, Show } from "@alloy-js/core";
11
2
  import { createPythonSymbol } from "../symbol-creation.js";
12
3
  import { getCallSignatureProps } from "../utils.js";
13
4
  import { CallSignature, CallSignatureProps } from "./CallSignature.jsx";
14
5
  import { BaseDeclarationProps, Declaration } from "./Declaration.js";
15
6
  import { PythonBlock } from "./PythonBlock.jsx";
16
- import { NoNamePolicy } from "./index.js";
7
+ import { LexicalScope, NoNamePolicy } from "./index.js";
17
8
 
18
9
  export interface FunctionDeclarationProps
19
10
  extends BaseDeclarationProps,
@@ -42,23 +33,13 @@ export interface FunctionDeclarationProps
42
33
  export function FunctionDeclaration(props: FunctionDeclarationProps) {
43
34
  const asyncKwd = props.async ? "async " : "";
44
35
  const callSignatureProps = getCallSignatureProps(props, {});
45
- const memberScope = useMemberScope();
46
- let scope: OutputScope | undefined = undefined;
47
- if (memberScope !== undefined) {
48
- scope = memberScope.instanceMembers!;
49
- } else {
50
- scope = useScope();
51
- }
52
-
53
36
  const sym = createPythonSymbol(
54
37
  props.name,
55
38
  {
56
- scope: scope,
39
+ instance: props.instanceFunction,
57
40
  refkeys: props.refkey,
58
- flags: props.flags ?? OutputSymbolFlags.None,
59
41
  },
60
42
  "function",
61
- false,
62
43
  );
63
44
  emitSymbol(sym);
64
45
 
@@ -66,7 +47,7 @@ export function FunctionDeclaration(props: FunctionDeclarationProps) {
66
47
  <>
67
48
  <Declaration {...props} nameKind="function" symbol={sym}>
68
49
  {asyncKwd}def <Name />
69
- <Scope name={sym.name} kind="function">
50
+ <LexicalScope name={sym.name}>
70
51
  <CallSignature
71
52
  {...callSignatureProps}
72
53
  returnType={props.returnType}
@@ -75,7 +56,7 @@ export function FunctionDeclaration(props: FunctionDeclarationProps) {
75
56
  <Show when={Boolean(props.doc)}>{props.doc}</Show>
76
57
  {props.children ?? "pass"}
77
58
  </PythonBlock>
78
- </Scope>
59
+ </LexicalScope>
79
60
  </Declaration>
80
61
  </>
81
62
  );
@@ -0,0 +1,24 @@
1
+ import { Scope, ScopePropsWithInfo, ScopePropsWithValue } from "@alloy-js/core";
2
+ import { createLexicalScope } from "../symbol-creation.js";
3
+ import { PythonLexicalScope } from "../symbols/python-lexical-scope.js";
4
+
5
+ export interface LexicalScopePropsWithScopeValue extends ScopePropsWithValue {}
6
+ export interface LeixcalScopePropsWithScopeInfo extends ScopePropsWithInfo {}
7
+
8
+ export type LexicalScopeProps =
9
+ | LexicalScopePropsWithScopeValue
10
+ | LeixcalScopePropsWithScopeInfo;
11
+
12
+ export function LexicalScope(props: LexicalScopeProps) {
13
+ let scope;
14
+ if ("value" in props) {
15
+ if (!(props.value instanceof PythonLexicalScope)) {
16
+ throw new Error("LexicalScope value must be a TSLexicalScope instance");
17
+ }
18
+ scope = props.value;
19
+ } else {
20
+ scope = createLexicalScope(props.name ?? "Lexical scope", props);
21
+ }
22
+
23
+ return <Scope value={scope}>{props.children}</Scope>;
24
+ }
@@ -0,0 +1,18 @@
1
+ import { Children, MemberScope as CoreMemberScope } from "@alloy-js/core";
2
+ import { PythonMemberScope } from "../symbols/python-member-scope.js";
3
+ import { PythonOutputSymbol } from "../symbols/python-output-symbol.js";
4
+ import { usePythonScope } from "../symbols/scopes.js";
5
+ export interface MemberScopeProps {
6
+ children: Children;
7
+ ownerSymbol: PythonOutputSymbol;
8
+ }
9
+
10
+ export function MemberScope(props: MemberScopeProps) {
11
+ const memberScope = new PythonMemberScope("member-scope", usePythonScope(), {
12
+ ownerSymbol: props.ownerSymbol,
13
+ });
14
+
15
+ return (
16
+ <CoreMemberScope value={memberScope}>{props.children}</CoreMemberScope>
17
+ );
18
+ }
@@ -70,7 +70,7 @@ export function SourceFile(props: SourceFileProps) {
70
70
  const path: string = join(currentDir, props.path)
71
71
  .replace(/\.py$/, "")
72
72
  .replace(/\//g, ".");
73
- const scope = new PythonModuleScope(path);
73
+ const scope = new PythonModuleScope(path, undefined);
74
74
  const sfContext: PythonSourceFileContext = {
75
75
  scope: scope,
76
76
  module: path,
@@ -84,7 +84,7 @@ export function SourceFile(props: SourceFileProps) {
84
84
  <hbr />
85
85
  </Show>
86
86
  <PythonSourceFileContext.Provider value={sfContext}>
87
- <Scope value={scope} kind="source-file">
87
+ <Scope value={scope}>
88
88
  <List doubleHardline>{props.children}</List>
89
89
  </Scope>
90
90
  </PythonSourceFileContext.Provider>
@@ -2,15 +2,10 @@ import {
2
2
  Children,
3
3
  Declaration as CoreDeclaration,
4
4
  Name,
5
- OutputScope,
6
- OutputSymbolFlags,
7
5
  Show,
8
6
  createSymbolSlot,
9
- effect,
10
7
  emitSymbol,
11
8
  memo,
12
- useMemberScope,
13
- useScope,
14
9
  } from "@alloy-js/core";
15
10
  import { createPythonSymbol } from "../symbol-creation.js";
16
11
  import { Atom } from "./Atom.jsx";
@@ -77,27 +72,23 @@ export interface VariableDeclarationProps extends BaseDeclarationProps {
77
72
  * ```
78
73
  */
79
74
  export function VariableDeclaration(props: VariableDeclarationProps) {
80
- const instanceVariable = props.instanceVariable ?? false;
81
75
  const TypeSymbolSlot = createSymbolSlot();
82
76
  const ValueTypeSymbolSlot = createSymbolSlot();
83
- const memberScope = useMemberScope();
84
- let scope: OutputScope | undefined = undefined;
85
- // Only consider the member scope if this is an instance variable
86
- if (memberScope !== undefined && instanceVariable) {
87
- scope = memberScope.instanceMembers!;
88
- } else {
89
- scope = useScope();
90
- }
91
77
 
92
78
  const sym = createPythonSymbol(
93
79
  props.name,
94
80
  {
95
- scope: scope,
81
+ instance: props.instanceVariable,
96
82
  refkeys: props.refkey,
83
+ type: props.type ? TypeSymbolSlot.firstSymbol : undefined,
97
84
  },
98
85
  "variable",
99
- true,
100
86
  );
87
+
88
+ if (!props.type) {
89
+ ValueTypeSymbolSlot.moveMembersTo(sym);
90
+ }
91
+
101
92
  emitSymbol(sym);
102
93
  // Handle optional type annotation
103
94
  const type = memo(() => {
@@ -109,25 +100,6 @@ export function VariableDeclaration(props: VariableDeclarationProps) {
109
100
  );
110
101
  });
111
102
 
112
- effect(() => {
113
- if (TypeSymbolSlot.ref.value) {
114
- const takenSymbols = TypeSymbolSlot.ref.value;
115
- for (const symbol of takenSymbols) {
116
- // If the symbol is a type, instantiate it
117
- symbol.instantiateTo(sym);
118
- }
119
- } else if (ValueTypeSymbolSlot.ref.value) {
120
- const takenSymbols = ValueTypeSymbolSlot.ref.value;
121
- for (const symbol of takenSymbols) {
122
- // ignore non-transient symbols (likely not the result of an
123
- // expression).
124
- if (symbol.flags & OutputSymbolFlags.Transient) {
125
- symbol.moveTo(sym);
126
- }
127
- }
128
- }
129
- });
130
-
131
103
  // If we receive a symbol, resolve it to a name
132
104
  const value =
133
105
  typeof props.initializer === "object" ?
@@ -8,7 +8,9 @@ export * from "./EnumMember.js";
8
8
  export * from "./FunctionCallExpression.js";
9
9
  export * from "./FunctionDeclaration.js";
10
10
  export * from "./ImportStatement.js";
11
+ export * from "./LexicalScope.js";
11
12
  export * from "./MemberExpression.js";
13
+ export * from "./MemberScope.jsx";
12
14
  export * from "./NoNamePolicy.js";
13
15
  export * from "./PyDoc.js";
14
16
  export * from "./PythonBlock.js";
@@ -36,8 +36,7 @@ function createSymbols(
36
36
  // Otherwise, we append the path to the module name
37
37
  const fullModuleScopeName = props.name + (path === "." ? "" : `.${path}`);
38
38
  const keys = refkeys[path];
39
- const moduleScope = new PythonModuleScope(fullModuleScopeName, {
40
- parent: undefined,
39
+ const moduleScope = new PythonModuleScope(fullModuleScopeName, undefined, {
41
40
  binder: binder,
42
41
  });
43
42
 
@@ -45,17 +44,12 @@ function createSymbols(
45
44
  for (const exportedName of symbols ?? []) {
46
45
  const key = keys[exportedName];
47
46
 
48
- const _ = createPythonSymbol(
49
- exportedName,
50
- {
51
- binder: binder,
52
- scope: moduleScope,
53
- refkeys: key,
54
- module: moduleScope.name,
55
- },
56
- undefined,
57
- false,
58
- );
47
+ const _ = createPythonSymbol(exportedName, {
48
+ space: moduleScope.symbols,
49
+ binder: binder,
50
+ refkeys: key,
51
+ module: moduleScope.name,
52
+ });
59
53
  }
60
54
  }
61
55
  }
@@ -0,0 +1,21 @@
1
+ import { PythonOutputSymbol } from "./symbols/python-output-symbol.js";
2
+
3
+ export function pythonNameConflictResolver(
4
+ _: string,
5
+ symbols: PythonOutputSymbol[],
6
+ ) {
7
+ for (let i = 1; i < symbols.length; i++) {
8
+ // Rename all but the first symbol to have a suffix of _2, _3, plus the scope name if available.
9
+ const symbol = symbols[i] as unknown as {
10
+ originalName: string;
11
+ name: string;
12
+ module?: string;
13
+ };
14
+ symbol.name =
15
+ symbol.originalName +
16
+ "_" +
17
+ (i + 1) +
18
+ "_" +
19
+ (symbols[i].aliasTarget?.scope?.name ?? symbol.module ?? "");
20
+ }
21
+ }