@alloy-js/python 0.1.0-dev.3 → 0.1.0-dev.5

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 (107) hide show
  1. package/dist/src/components/CallSignature.d.ts.map +1 -1
  2. package/dist/src/components/CallSignature.js +8 -11
  3. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  4. package/dist/src/components/ClassDeclaration.js +6 -20
  5. package/dist/src/components/ClassInstantiation.d.ts.map +1 -1
  6. package/dist/src/components/ClassInstantiation.js +4 -4
  7. package/dist/src/components/Declaration.d.ts +1 -5
  8. package/dist/src/components/Declaration.d.ts.map +1 -1
  9. package/dist/src/components/Declaration.js +6 -15
  10. package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
  11. package/dist/src/components/EnumDeclaration.js +33 -59
  12. package/dist/src/components/EnumMember.d.ts.map +1 -1
  13. package/dist/src/components/EnumMember.js +3 -4
  14. package/dist/src/components/FunctionDeclaration.d.ts +3 -1
  15. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  16. package/dist/src/components/FunctionDeclaration.js +6 -16
  17. package/dist/src/components/LexicalScope.d.ts +8 -0
  18. package/dist/src/components/LexicalScope.d.ts.map +1 -0
  19. package/dist/src/components/LexicalScope.js +21 -0
  20. package/dist/src/components/MemberScope.d.ts +8 -0
  21. package/dist/src/components/MemberScope.d.ts.map +1 -0
  22. package/dist/src/components/MemberScope.js +15 -0
  23. package/dist/src/components/PythonBlock.d.ts +5 -4
  24. package/dist/src/components/PythonBlock.d.ts.map +1 -1
  25. package/dist/src/components/PythonBlock.js +0 -1
  26. package/dist/src/components/SourceFile.js +1 -2
  27. package/dist/src/components/VariableDeclaration.d.ts.map +1 -1
  28. package/dist/src/components/VariableDeclaration.js +8 -31
  29. package/dist/src/components/index.d.ts +2 -0
  30. package/dist/src/components/index.d.ts.map +1 -1
  31. package/dist/src/components/index.js +2 -0
  32. package/dist/src/create-module.d.ts.map +1 -1
  33. package/dist/src/create-module.js +3 -4
  34. package/dist/src/name-conflict-resolver.d.ts +3 -0
  35. package/dist/src/name-conflict-resolver.d.ts.map +1 -0
  36. package/dist/src/name-conflict-resolver.js +7 -0
  37. package/dist/src/symbol-creation.d.ts +13 -2
  38. package/dist/src/symbol-creation.d.ts.map +1 -1
  39. package/dist/src/symbol-creation.js +33 -9
  40. package/dist/src/symbols/index.d.ts +1 -1
  41. package/dist/src/symbols/index.d.ts.map +1 -1
  42. package/dist/src/symbols/index.js +1 -1
  43. package/dist/src/symbols/python-lexical-scope.d.ts +7 -0
  44. package/dist/src/symbols/python-lexical-scope.d.ts.map +1 -0
  45. package/dist/src/symbols/python-lexical-scope.js +14 -0
  46. package/dist/src/symbols/python-member-scope.d.ts +3 -4
  47. package/dist/src/symbols/python-member-scope.d.ts.map +1 -1
  48. package/dist/src/symbols/python-member-scope.js +4 -7
  49. package/dist/src/symbols/python-module-scope.d.ts +4 -9
  50. package/dist/src/symbols/python-module-scope.d.ts.map +1 -1
  51. package/dist/src/symbols/python-module-scope.js +6 -14
  52. package/dist/src/symbols/python-output-symbol.d.ts +10 -7
  53. package/dist/src/symbols/python-output-symbol.d.ts.map +1 -1
  54. package/dist/src/symbols/python-output-symbol.js +23 -6
  55. package/dist/src/symbols/reference.d.ts +2 -2
  56. package/dist/src/symbols/reference.d.ts.map +1 -1
  57. package/dist/src/symbols/reference.js +36 -36
  58. package/dist/src/symbols/scopes.d.ts +3 -1
  59. package/dist/src/symbols/scopes.d.ts.map +1 -1
  60. package/dist/src/symbols/scopes.js +8 -0
  61. package/dist/test/classdeclarations.test.js +87 -52
  62. package/dist/test/enums.test.js +8 -2
  63. package/dist/test/externals.test.js +6 -5
  64. package/dist/test/functiondeclaration.test.js +73 -36
  65. package/dist/test/imports.test.js +9 -36
  66. package/dist/test/utils.d.ts +2 -3
  67. package/dist/test/utils.d.ts.map +1 -1
  68. package/dist/test/utils.js +3 -2
  69. package/dist/test/variables.test.js +11 -11
  70. package/dist/tsconfig.tsbuildinfo +1 -1
  71. package/package.json +2 -2
  72. package/src/components/CallSignature.tsx +9 -17
  73. package/src/components/ClassDeclaration.tsx +5 -32
  74. package/src/components/ClassInstantiation.tsx +6 -12
  75. package/src/components/Declaration.tsx +1 -20
  76. package/src/components/EnumDeclaration.tsx +18 -41
  77. package/src/components/EnumMember.tsx +1 -3
  78. package/src/components/FunctionDeclaration.tsx +12 -30
  79. package/src/components/LexicalScope.tsx +24 -0
  80. package/src/components/MemberScope.tsx +18 -0
  81. package/src/components/PythonBlock.tsx +6 -1
  82. package/src/components/SourceFile.tsx +2 -2
  83. package/src/components/VariableDeclaration.tsx +7 -35
  84. package/src/components/index.ts +2 -0
  85. package/src/create-module.ts +7 -13
  86. package/src/name-conflict-resolver.ts +21 -0
  87. package/src/symbol-creation.ts +50 -11
  88. package/src/symbols/index.ts +1 -1
  89. package/src/symbols/python-lexical-scope.ts +16 -0
  90. package/src/symbols/python-member-scope.ts +4 -8
  91. package/src/symbols/python-module-scope.ts +6 -32
  92. package/src/symbols/python-output-symbol.ts +36 -10
  93. package/src/symbols/reference.tsx +70 -0
  94. package/src/symbols/scopes.ts +15 -1
  95. package/temp/api.json +1015 -561
  96. package/test/classdeclarations.test.tsx +66 -31
  97. package/test/enums.test.tsx +2 -2
  98. package/test/externals.test.tsx +6 -7
  99. package/test/functiondeclaration.test.tsx +48 -39
  100. package/test/imports.test.tsx +11 -36
  101. package/test/utils.tsx +9 -5
  102. package/test/variables.test.tsx +11 -7
  103. package/dist/src/symbols/custom-output-scope.d.ts +0 -10
  104. package/dist/src/symbols/custom-output-scope.d.ts.map +0 -1
  105. package/dist/src/symbols/custom-output-scope.js +0 -25
  106. package/src/symbols/custom-output-scope.ts +0 -35
  107. package/src/symbols/reference.ts +0 -99
@@ -34,8 +34,8 @@ describe("Python Class", () => {
34
34
  it("renders a class with base classes", () => {
35
35
  const result = toSourceText([
36
36
  <py.StatementList>
37
- <py.ClassDeclaration name="Base1" />
38
- <py.ClassDeclaration name="Base2" />
37
+ <py.ClassDeclaration name="Base1" refkey={refkey("Base1")} />
38
+ <py.ClassDeclaration name="Base2" refkey={refkey("Base2")} />
39
39
  <py.ClassDeclaration
40
40
  name="Baz"
41
41
  bases={[refkey("Base1"), refkey("Base2")]}
@@ -74,10 +74,14 @@ describe("Python Class", () => {
74
74
  it("renders classes across modules with inheritance", () => {
75
75
  const result = toSourceTextMultiple([
76
76
  <py.SourceFile path="mod1.py">
77
- <py.ClassDeclaration name="A" />
77
+ <py.ClassDeclaration name="A" refkey={refkey("A")} />
78
78
  </py.SourceFile>,
79
79
  <py.SourceFile path="folder/mod2.py">
80
- <py.ClassDeclaration name="B" bases={[refkey("A")]} />
80
+ <py.ClassDeclaration
81
+ name="B"
82
+ refkey={refkey("B")}
83
+ bases={[refkey("A")]}
84
+ />
81
85
  </py.SourceFile>,
82
86
  <py.SourceFile path="mod3.py">
83
87
  <py.ClassDeclaration name="C" bases={[refkey("B")]} />
@@ -113,7 +117,7 @@ describe("Python Class", () => {
113
117
  it("renders a class with class variables like foo: str, and also bar: A where A is another class", () => {
114
118
  const result = toSourceText([
115
119
  <py.StatementList>
116
- <py.ClassDeclaration name="A" />
120
+ <py.ClassDeclaration name="A" refkey={refkey("A")} />
117
121
  <py.ClassDeclaration name="B">
118
122
  <py.StatementList>
119
123
  <py.VariableDeclaration name="bar" type={refkey("A")} omitNone />
@@ -136,18 +140,33 @@ describe("Python Class", () => {
136
140
  });
137
141
 
138
142
  it("renders a class with class variables like foo: str, and another identical class", () => {
143
+ const fooKey = refkey();
144
+ const barKey = refkey();
145
+
139
146
  const result = toSourceText([
140
147
  <py.StatementList>
141
148
  <py.ClassDeclaration name="A">
142
149
  <py.StatementList>
143
- <py.VariableDeclaration name="foo" type="str" omitNone />
150
+ <py.VariableDeclaration
151
+ name="foo"
152
+ type="str"
153
+ refkey={fooKey}
154
+ omitNone
155
+ />
144
156
  </py.StatementList>
145
157
  </py.ClassDeclaration>
146
158
  <py.ClassDeclaration name="B">
147
159
  <py.StatementList>
148
- <py.VariableDeclaration name="foo" type="str" omitNone />
160
+ <py.VariableDeclaration
161
+ name="foo"
162
+ type="str"
163
+ refkey={barKey}
164
+ omitNone
165
+ />
149
166
  </py.StatementList>
150
167
  </py.ClassDeclaration>
168
+ {fooKey}
169
+ {barKey}
151
170
  </py.StatementList>,
152
171
  ]);
153
172
  const expected = d`
@@ -157,7 +176,8 @@ describe("Python Class", () => {
157
176
  class B:
158
177
  foo: str
159
178
 
160
-
179
+ A.foo
180
+ B.foo
161
181
  `;
162
182
  expect(result).toRenderTo(expected);
163
183
  });
@@ -167,17 +187,29 @@ describe("Python Class - VariableDeclaration", () => {
167
187
  it("renders a class with class fields", () => {
168
188
  const result = toSourceText([
169
189
  <py.StatementList>
170
- <py.ClassDeclaration name="Base"></py.ClassDeclaration>
171
- <py.ClassDeclaration name="A">
190
+ <py.ClassDeclaration
191
+ refkey={refkey("Base")}
192
+ name="Base"
193
+ ></py.ClassDeclaration>
194
+ <py.ClassDeclaration refkey={refkey("A")} name="A">
172
195
  <py.StatementList>
173
- <py.VariableDeclaration name="just_name" />
174
- <py.VariableDeclaration name="name_and_type" type="number" />
196
+ <py.VariableDeclaration instanceVariable name="just_name" />
175
197
  <py.VariableDeclaration
198
+ instanceVariable
199
+ name="name_and_type"
200
+ type="number"
201
+ />
202
+ <py.VariableDeclaration
203
+ instanceVariable
176
204
  name="name_type_and_value"
177
205
  type="number"
178
206
  initializer={12}
179
207
  />
180
- <py.VariableDeclaration name="class_based" type={refkey("Base")} />
208
+ <py.VariableDeclaration
209
+ instanceVariable
210
+ name="class_based"
211
+ type={refkey("Base")}
212
+ />
181
213
  </py.StatementList>
182
214
  </py.ClassDeclaration>
183
215
  </py.StatementList>,
@@ -210,12 +242,7 @@ describe("Python Class - VariableDeclaration", () => {
210
242
  name="one"
211
243
  refkey={v1Rk}
212
244
  type={classRk}
213
- initializer={
214
- <py.MemberExpression>
215
- <py.MemberExpression.Part refkey={classRk} />
216
- <py.MemberExpression.Part args />
217
- </py.MemberExpression>
218
- }
245
+ initializer={<py.ClassInstantiation target={classRk} />}
219
246
  />
220
247
  <>{memberRefkey(v1Rk, classMemberRk)}</>
221
248
  <>{memberRefkey(v1Rk, classMethodRk)}()</>
@@ -228,11 +255,11 @@ describe("Python Class - VariableDeclaration", () => {
228
255
  name="instanceProp"
229
256
  refkey={classMemberRk}
230
257
  initializer={42}
231
- instanceVariable={true}
258
+ instanceVariable
232
259
  />
233
260
  <py.FunctionDeclaration
234
261
  name="instanceMethod"
235
- instanceFunction={true}
262
+ instanceFunction
236
263
  refkey={classMethodRk}
237
264
  returnType="int"
238
265
  />
@@ -263,12 +290,16 @@ describe("Python Class - VariableDeclaration", () => {
263
290
 
264
291
  describe("Python Class - FunctionDeclaration", () => {
265
292
  it("renders a class with class fields and method", () => {
293
+ const methodRefkey = refkey();
294
+ const classMethodRefkey = refkey();
295
+ const staticMethodRefkey = refkey();
296
+
266
297
  const result = toSourceText([
267
- <>
298
+ <py.StatementList>
268
299
  <py.ClassDeclaration name="MyClass" bases={["BaseClass"]}>
269
300
  <py.StatementList>
270
- <py.VariableDeclaration name="a" type="int" />
271
- <py.VariableDeclaration name="b" type="int" />
301
+ <py.VariableDeclaration name="a" type="int" instanceVariable />
302
+ <py.VariableDeclaration name="b" type="int" instanceVariable />
272
303
  <py.FunctionDeclaration
273
304
  name="my_method"
274
305
  parameters={[
@@ -276,28 +307,31 @@ describe("Python Class - FunctionDeclaration", () => {
276
307
  { name: "b", type: "int" },
277
308
  ]}
278
309
  returnType="int"
279
- instanceFunction={true}
310
+ instanceFunction
311
+ refkey={methodRefkey}
280
312
  >
281
313
  return a + b
282
314
  </py.FunctionDeclaration>
283
315
  <py.FunctionDeclaration
284
316
  name="my_class_method"
285
- instanceFunction={false}
286
- classFunction={true}
317
+ classFunction
287
318
  returnType="int"
319
+ refkey={classMethodRefkey}
288
320
  >
289
321
  pass
290
322
  </py.FunctionDeclaration>
291
323
  <py.FunctionDeclaration
292
324
  name="my_standalone_function"
293
- instanceFunction={false}
294
325
  returnType="int"
326
+ refkey={staticMethodRefkey}
295
327
  >
296
328
  pass
297
329
  </py.FunctionDeclaration>
298
330
  </py.StatementList>
299
331
  </py.ClassDeclaration>
300
- </>,
332
+ {classMethodRefkey}
333
+ {staticMethodRefkey}
334
+ </py.StatementList>,
301
335
  ]);
302
336
  const expected = d`
303
337
  class MyClass(BaseClass):
@@ -312,8 +346,9 @@ describe("Python Class - FunctionDeclaration", () => {
312
346
  def my_standalone_function() -> int:
313
347
  pass
314
348
 
315
-
316
-
349
+
350
+ MyClass.my_class_method
351
+ MyClass.my_standalone_function
317
352
  `;
318
353
  expect(result).toRenderTo(expected);
319
354
  });
@@ -66,8 +66,8 @@ describe("Python Enum", () => {
66
66
  const result = toSourceText(
67
67
  [
68
68
  <py.StatementList>
69
- <py.ClassDeclaration name="Dog" />
70
- <py.ClassDeclaration name="Cat" />
69
+ <py.ClassDeclaration name="Dog" refkey={refkey("Dog")} />
70
+ <py.ClassDeclaration name="Cat" refkey={refkey("Cat")} />
71
71
  <py.EnumDeclaration
72
72
  name="Animal"
73
73
  baseType="Enum"
@@ -48,7 +48,6 @@ it("uses import from external library in multiple functions", () => {
48
48
  name={"getUser"}
49
49
  parameters={[{ name: "userId", type: "int" }]}
50
50
  returnType={py.requestsModule["models"]["Response"]}
51
- instanceFunction={true}
52
51
  >
53
52
  <py.StatementList>
54
53
  <py.VariableDeclaration
@@ -69,7 +68,6 @@ it("uses import from external library in multiple functions", () => {
69
68
  name={"createUser"}
70
69
  parameters={[{ name: "userName", type: "string" }]}
71
70
  returnType={py.requestsModule["models"]["Response"]}
72
- instanceFunction={true}
73
71
  >
74
72
  <py.StatementList>
75
73
  <py.VariableDeclaration
@@ -96,12 +94,12 @@ it("uses import from external library in multiple functions", () => {
96
94
  from requests import post
97
95
  from requests.models import Response
98
96
 
99
- def get_user(self, user_id: int) -> Response:
97
+ def get_user(user_id: int) -> Response:
100
98
  response = get(1)
101
99
  return response.json()
102
100
 
103
101
 
104
- def create_user(self, user_name: string) -> Response:
102
+ def create_user(user_name: string) -> Response:
105
103
  response = post(1)
106
104
  return response.json()
107
105
 
@@ -118,12 +116,13 @@ it("uses import from external library in multiple class methods", () => {
118
116
  name="some_var"
119
117
  initializer={12}
120
118
  instanceVariable
119
+ refkey={refkey("some_var")}
121
120
  />
122
121
  <py.FunctionDeclaration
123
- name={"getUser"}
122
+ name="getUser"
124
123
  parameters={[{ name: "userId", type: "int" }]}
125
124
  returnType={py.requestsModule["models"]["Response"]}
126
- instanceFunction={true}
125
+ instanceFunction
127
126
  >
128
127
  <py.StatementList>
129
128
  <py.VariableDeclaration
@@ -144,7 +143,7 @@ it("uses import from external library in multiple class methods", () => {
144
143
  name={"createUser"}
145
144
  parameters={[{ name: "userName", type: "string" }]}
146
145
  returnType={py.requestsModule["models"]["Response"]}
147
- instanceFunction={true}
146
+ instanceFunction
148
147
  >
149
148
  <py.StatementList>
150
149
  <py.VariableDeclaration
@@ -10,11 +10,9 @@ import {
10
10
 
11
11
  describe("Function Declaration", () => {
12
12
  it("renders a function with no body as 'pass'", () => {
13
- const result = toSourceText([
14
- <py.FunctionDeclaration name="foo" instanceFunction={true} />,
15
- ]);
13
+ const result = toSourceText([<py.FunctionDeclaration name="foo" />]);
16
14
  expect(result).toRenderTo(d`
17
- def foo(self):
15
+ def foo():
18
16
  pass
19
17
 
20
18
 
@@ -39,15 +37,10 @@ describe("Function Declaration", () => {
39
37
  <py.StatementList>
40
38
  <py.FunctionDeclaration
41
39
  name="foo"
42
- instanceFunction={true}
43
40
  returnType="int"
44
41
  refkey={refkeyFoo}
45
42
  />
46
- <py.FunctionDeclaration
47
- name="bar"
48
- instanceFunction={true}
49
- returnType="int"
50
- >
43
+ <py.FunctionDeclaration name="bar" returnType="int">
51
44
  <py.VariableDeclaration
52
45
  name="result"
53
46
  type="int"
@@ -59,10 +52,10 @@ describe("Function Declaration", () => {
59
52
  </py.StatementList>,
60
53
  ]);
61
54
  expect(result).toRenderTo(d`
62
- def foo(self) -> int:
55
+ def foo() -> int:
63
56
  pass
64
57
 
65
- def bar(self) -> int:
58
+ def bar() -> int:
66
59
  result: int = foo()
67
60
 
68
61
 
@@ -71,15 +64,19 @@ describe("Function Declaration", () => {
71
64
 
72
65
  it("renders an instance function with a body", () => {
73
66
  const result = toSourceText([
74
- <py.FunctionDeclaration name="bar" instanceFunction={true}>
75
- print('hi')
76
- </py.FunctionDeclaration>,
67
+ <py.ClassDeclaration name="MyClass">
68
+ <py.FunctionDeclaration name="bar" instanceFunction={true}>
69
+ print('hi')
70
+ </py.FunctionDeclaration>
71
+ </py.ClassDeclaration>,
77
72
  ]);
78
73
  expect(result).toRenderTo(d`
79
- def bar(self):
80
- print('hi')
74
+ class MyClass:
75
+ def bar(self):
76
+ print('hi')
81
77
 
82
78
 
79
+
83
80
  `);
84
81
  });
85
82
 
@@ -110,13 +107,17 @@ describe("Function Declaration", () => {
110
107
 
111
108
  it("renders an __init__ function with no body as 'pass'", () => {
112
109
  const result = toSourceText([
113
- <py.InitFunctionDeclaration parameters={[{ name: "x" }]} />,
110
+ <py.ClassDeclaration name="MyClass">
111
+ <py.InitFunctionDeclaration parameters={[{ name: "x" }]} />
112
+ </py.ClassDeclaration>,
114
113
  ]);
115
114
  expect(result).toRenderTo(d`
116
- def __init__(self, x):
117
- pass
115
+ class MyClass:
116
+ def __init__(self, x):
117
+ pass
118
118
 
119
-
119
+
120
+
120
121
  `);
121
122
  });
122
123
 
@@ -144,7 +145,7 @@ describe("Function Declaration", () => {
144
145
  expect(
145
146
  toSourceText([
146
147
  <py.StatementList>
147
- <py.ClassDeclaration name="Foo" />
148
+ <py.ClassDeclaration name="Foo" refkey={refkey("Foo")} />
148
149
  <py.FunctionDeclaration
149
150
  async
150
151
  name="foo"
@@ -195,32 +196,40 @@ describe("Function Declaration", () => {
195
196
  it("renders function with parameters", () => {
196
197
  const parameters = [{ name: "x", type: "int" }];
197
198
  const decl = (
198
- <py.FunctionDeclaration
199
- name="foo"
200
- instanceFunction={true}
201
- parameters={parameters}
202
- >
203
- self.attribute = "value"
204
- </py.FunctionDeclaration>
199
+ <py.ClassDeclaration name="MyClass">
200
+ <py.FunctionDeclaration
201
+ name="foo"
202
+ instanceFunction
203
+ parameters={parameters}
204
+ >
205
+ self.attribute = "value"
206
+ </py.FunctionDeclaration>
207
+ </py.ClassDeclaration>
205
208
  );
206
209
 
207
210
  expect(toSourceText([decl])).toBe(d`
208
- def foo(self, x: int):
209
- self.attribute = "value"
211
+ class MyClass:
212
+ def foo(self, x: int):
213
+ self.attribute = "value"
214
+
210
215
 
211
216
  `);
212
217
  });
213
218
  it("renders __init__ function with parameters", () => {
214
219
  const parameters = [{ name: "x", type: "int" }];
215
220
  const decl = (
216
- <py.InitFunctionDeclaration parameters={parameters}>
217
- self.attribute = "value"
218
- </py.InitFunctionDeclaration>
221
+ <py.ClassDeclaration name="MyClass">
222
+ <py.InitFunctionDeclaration parameters={parameters}>
223
+ self.attribute = "value"
224
+ </py.InitFunctionDeclaration>
225
+ </py.ClassDeclaration>
219
226
  );
220
227
 
221
228
  expect(toSourceText([decl])).toBe(d`
222
- def __init__(self, x: int):
223
- self.attribute = "value"
229
+ class MyClass:
230
+ def __init__(self, x: int):
231
+ self.attribute = "value"
232
+
224
233
 
225
234
  `);
226
235
  });
@@ -277,11 +286,11 @@ describe("Function Declaration", () => {
277
286
  it("renders complex typing structure", () => {
278
287
  const res = toSourceTextMultiple([
279
288
  <py.SourceFile path="mod1.py">
280
- <py.ClassDeclaration name="Foo" />
289
+ <py.ClassDeclaration name="Foo" refkey={refkey("Foo")} />
281
290
  </py.SourceFile>,
282
291
  <py.SourceFile path="mod2.py">
283
- <py.ClassDeclaration name="A" />
284
- <py.ClassDeclaration name="B" />
292
+ <py.ClassDeclaration name="A" refkey={refkey("A")} />
293
+ <py.ClassDeclaration name="B" refkey={refkey("B")} />
285
294
  </py.SourceFile>,
286
295
  <py.SourceFile path="usage.py">
287
296
  <py.FunctionDeclaration
@@ -19,14 +19,8 @@ describe("ImportStatement", () => {
19
19
  });
20
20
 
21
21
  it("renders named imports", () => {
22
- const sqrtSymbol = createPythonSymbol("sqrt", {
23
- binder: undefined,
24
- scope: undefined,
25
- });
26
- const piSymbol = createPythonSymbol("pi", {
27
- binder: undefined,
28
- scope: undefined,
29
- });
22
+ const sqrtSymbol = createPythonSymbol("sqrt", undefined);
23
+ const piSymbol = createPythonSymbol("pi", undefined);
30
24
  const symbols = new Set<ImportedSymbol>([
31
25
  new ImportedSymbol(sqrtSymbol, sqrtSymbol),
32
26
  new ImportedSymbol(piSymbol, piSymbol),
@@ -42,27 +36,20 @@ describe("ImportStatement", () => {
42
36
  describe("ImportStatements", () => {
43
37
  it("renders multiple import statements", () => {
44
38
  const pythonModuleScope = createPythonModuleScope("math", undefined);
45
- const sqrtSymbol = createPythonSymbol("sqrt", {
46
- binder: undefined,
47
- scope: undefined,
48
- });
49
- const piSymbol = createPythonSymbol("pi", {
50
- binder: undefined,
51
- scope: undefined,
52
- });
39
+ const sqrtSymbol = createPythonSymbol("sqrt", undefined);
40
+ const piSymbol = createPythonSymbol("pi", undefined);
41
+
53
42
  const mathSymbols = new Set<ImportedSymbol>([
54
43
  new ImportedSymbol(sqrtSymbol, sqrtSymbol),
55
44
  new ImportedSymbol(piSymbol, piSymbol),
56
45
  ]);
57
46
  const sysModuleScope = createPythonModuleScope("sys", undefined);
58
47
  const requestsScope = createPythonModuleScope("requests", undefined);
59
- const getSymbol = createPythonSymbol("get", {
60
- binder: undefined,
61
- scope: undefined,
62
- });
48
+ const getSymbol = createPythonSymbol("get", undefined);
63
49
  const requestsSymbols = new Set<ImportedSymbol>([
64
50
  new ImportedSymbol(getSymbol, getSymbol),
65
51
  ]);
52
+
66
53
  const records = new ImportRecords([
67
54
  [pythonModuleScope, { symbols: mathSymbols }],
68
55
  [requestsScope, { symbols: requestsSymbols }],
@@ -79,27 +66,15 @@ describe("ImportStatements", () => {
79
66
  });
80
67
  it("renders multiple import statements, but joining imports from the same module", () => {
81
68
  const pythonModuleScope = createPythonModuleScope("math", undefined);
82
- const sqrtSymbol = createPythonSymbol("sqrt", {
83
- binder: undefined,
84
- scope: undefined,
85
- });
86
- const piSymbol = createPythonSymbol("pi", {
87
- binder: undefined,
88
- scope: undefined,
89
- });
69
+ const sqrtSymbol = createPythonSymbol("sqrt", undefined);
70
+ const piSymbol = createPythonSymbol("pi", undefined);
90
71
  const mathSymbols = new Set<ImportedSymbol>([
91
72
  new ImportedSymbol(sqrtSymbol, sqrtSymbol),
92
73
  new ImportedSymbol(piSymbol, piSymbol),
93
74
  ]);
94
75
  const requestsScope = createPythonModuleScope("requests", undefined);
95
- const getSymbol = createPythonSymbol("get", {
96
- binder: undefined,
97
- scope: undefined,
98
- });
99
- const postSymbol = createPythonSymbol("post", {
100
- binder: undefined,
101
- scope: undefined,
102
- });
76
+ const getSymbol = createPythonSymbol("get", undefined);
77
+ const postSymbol = createPythonSymbol("post", undefined);
103
78
  const requestsSymbols = new Set<ImportedSymbol>([
104
79
  new ImportedSymbol(getSymbol, getSymbol),
105
80
  new ImportedSymbol(postSymbol, postSymbol),
package/test/utils.tsx CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  Output,
7
7
  OutputDirectory,
8
8
  OutputFile,
9
+ OutputScope,
9
10
  PrintTreeOptions,
10
11
  SymbolCreator,
11
12
  render,
@@ -13,8 +14,8 @@ import {
13
14
  import { dedent } from "@alloy-js/core/testing";
14
15
  import { expect } from "vitest";
15
16
  import * as py from "../src/components/index.js";
17
+ import { pythonNameConflictResolver } from "../src/name-conflict-resolver.js";
16
18
  import { createPythonNamePolicy } from "../src/name-policy.js";
17
- import { CustomOutputScope } from "../src/symbols/custom-output-scope.js";
18
19
  import { PythonModuleScope } from "../src/symbols/index.js";
19
20
 
20
21
  export function findFile(
@@ -86,7 +87,11 @@ export function toSourceTextMultiple(
86
87
  printOptions.tabWidth = 4;
87
88
  }
88
89
  const content = (
89
- <Output externals={mergedExternals} namePolicy={policy}>
90
+ <Output
91
+ externals={mergedExternals}
92
+ namePolicy={policy}
93
+ nameConflictResolver={pythonNameConflictResolver}
94
+ >
90
95
  {sourceFiles}
91
96
  </Output>
92
97
  );
@@ -121,11 +126,10 @@ export function toSourceText(
121
126
  // Helper function to create a PythonModuleScope to be used in tests
122
127
  export function createPythonModuleScope(
123
128
  name: string,
124
- parent: CustomOutputScope | undefined,
129
+ parent: OutputScope | undefined,
125
130
  binder: Binder | undefined = undefined,
126
131
  ): PythonModuleScope {
127
- return new PythonModuleScope(name, {
128
- parent: parent,
132
+ return new PythonModuleScope(name, parent, {
129
133
  binder: binder,
130
134
  });
131
135
  }
@@ -124,12 +124,13 @@ describe("Python Variable", () => {
124
124
  });
125
125
 
126
126
  it("declares a python variable with a class type", () => {
127
+ const classKey = refkey();
127
128
  const res = toSourceText([
128
129
  <py.StatementList>
129
- <py.ClassDeclaration name="MyClass" />
130
+ <py.ClassDeclaration name="MyClass" refkey={classKey} />
130
131
  <py.VariableDeclaration
131
132
  name="my_var"
132
- type={<py.Reference refkey={refkey("MyClass")} />}
133
+ type={<py.Reference refkey={classKey} />}
133
134
  />
134
135
  </py.StatementList>,
135
136
  ]);
@@ -141,14 +142,15 @@ describe("Python Variable", () => {
141
142
  });
142
143
 
143
144
  it("declares a python variable with a class type from a different module", () => {
145
+ const classKey = refkey();
144
146
  const res = toSourceTextMultiple([
145
147
  <py.SourceFile path="classes.py">
146
- <py.ClassDeclaration name="MyClass" />
148
+ <py.ClassDeclaration name="MyClass" refkey={classKey} />
147
149
  </py.SourceFile>,
148
150
  <py.SourceFile path="usage.py">
149
151
  <py.VariableDeclaration
150
152
  name="my_var"
151
- type={<py.Reference refkey={refkey("MyClass")} />}
153
+ type={<py.Reference refkey={classKey} />}
152
154
  />
153
155
  </py.SourceFile>,
154
156
  ]);
@@ -167,13 +169,15 @@ describe("Python Variable", () => {
167
169
  });
168
170
 
169
171
  it("declares a python variable receiving other variable as value", () => {
172
+ const varKey = refkey();
170
173
  const res = toSourceText([
171
174
  <py.StatementList>
172
- <py.VariableDeclaration name="my_var" initializer={42} />
173
175
  <py.VariableDeclaration
174
- name="my_other_var"
175
- initializer={refkey("my_var")}
176
+ name="my_var"
177
+ refkey={varKey}
178
+ initializer={42}
176
179
  />
180
+ <py.VariableDeclaration name="my_other_var" initializer={varKey} />
177
181
  </py.StatementList>,
178
182
  ]);
179
183
  expect(res).toBe(`my_var = 42\nmy_other_var = my_var`);
@@ -1,10 +0,0 @@
1
- import { OutputScope, OutputScopeOptions, SymbolTable } from "@alloy-js/core";
2
- export declare class CustomOutputScope extends OutputScope {
3
- #private;
4
- /**
5
- * The symbols defined within this scope.
6
- */
7
- get symbols(): SymbolTable;
8
- constructor(name: string, options?: OutputScopeOptions);
9
- }
10
- //# sourceMappingURL=custom-output-scope.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"custom-output-scope.d.ts","sourceRoot":"","sources":["../../../src/symbols/custom-output-scope.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAI9E,qBAAa,iBAAkB,SAAQ,WAAW;;IAEhD;;OAEG;IACH,IAAI,OAAO,gBAEV;gBAEW,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB;CAqB3D"}
@@ -1,25 +0,0 @@
1
- import { OutputScope, SymbolTable } from "@alloy-js/core";
2
-
3
- // This is named as CustomOutputScope and not PythonOutputScope to avoid confusion with the one defined in scopes.ts
4
- // We are creating a custom output scope so we can add out custom name conflict resolution logic.
5
- export class CustomOutputScope extends OutputScope {
6
- #symbols;
7
- /**
8
- * The symbols defined within this scope.
9
- */
10
- get symbols() {
11
- return this.#symbols;
12
- }
13
- constructor(name, options = {}) {
14
- super(name, options);
15
- this.#symbols = new SymbolTable(this, {
16
- nameConflictResolver: (_, symbols) => {
17
- for (let i = 1; i < symbols.length; i++) {
18
- // Rename all but the first symbol to have a suffix of _2, _3, plus the scope name if available.
19
- const symbol = symbols[i];
20
- symbol.name = symbol.originalName + "_" + (i + 1) + "_" + (symbols[i].aliasTarget?.scope?.name ?? symbol.module ?? "");
21
- }
22
- }
23
- });
24
- }
25
- }