@alloy-js/python 0.0.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 (206) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/LICENSE +7 -0
  3. package/api-extractor.json +4 -0
  4. package/dist/src/builtins/python.d.ts +9 -0
  5. package/dist/src/builtins/python.d.ts.map +1 -0
  6. package/dist/src/builtins/python.js +17 -0
  7. package/dist/src/components/Atom.d.ts +19 -0
  8. package/dist/src/components/Atom.d.ts.map +1 -0
  9. package/dist/src/components/Atom.js +82 -0
  10. package/dist/src/components/CallSignature.d.ts +79 -0
  11. package/dist/src/components/CallSignature.d.ts.map +1 -0
  12. package/dist/src/components/CallSignature.js +201 -0
  13. package/dist/src/components/ClassDeclaration.d.ts +37 -0
  14. package/dist/src/components/ClassDeclaration.d.ts.map +1 -0
  15. package/dist/src/components/ClassDeclaration.js +83 -0
  16. package/dist/src/components/ClassInstantiation.d.ts +24 -0
  17. package/dist/src/components/ClassInstantiation.d.ts.map +1 -0
  18. package/dist/src/components/ClassInstantiation.js +35 -0
  19. package/dist/src/components/Declaration.d.ts +48 -0
  20. package/dist/src/components/Declaration.d.ts.map +1 -0
  21. package/dist/src/components/Declaration.js +37 -0
  22. package/dist/src/components/EnumDeclaration.d.ts +164 -0
  23. package/dist/src/components/EnumDeclaration.d.ts.map +1 -0
  24. package/dist/src/components/EnumDeclaration.js +278 -0
  25. package/dist/src/components/EnumMember.d.ts +46 -0
  26. package/dist/src/components/EnumMember.d.ts.map +1 -0
  27. package/dist/src/components/EnumMember.js +67 -0
  28. package/dist/src/components/FunctionCallExpression.d.ts +19 -0
  29. package/dist/src/components/FunctionCallExpression.d.ts.map +1 -0
  30. package/dist/src/components/FunctionCallExpression.js +40 -0
  31. package/dist/src/components/FunctionDeclaration.d.ts +47 -0
  32. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -0
  33. package/dist/src/components/FunctionDeclaration.js +107 -0
  34. package/dist/src/components/ImportStatement.d.ts +39 -0
  35. package/dist/src/components/ImportStatement.d.ts.map +1 -0
  36. package/dist/src/components/ImportStatement.js +104 -0
  37. package/dist/src/components/MemberExpression.d.ts +97 -0
  38. package/dist/src/components/MemberExpression.d.ts.map +1 -0
  39. package/dist/src/components/MemberExpression.js +308 -0
  40. package/dist/src/components/NoNamePolicy.d.ts +23 -0
  41. package/dist/src/components/NoNamePolicy.d.ts.map +1 -0
  42. package/dist/src/components/NoNamePolicy.js +27 -0
  43. package/dist/src/components/PyDoc.d.ts +90 -0
  44. package/dist/src/components/PyDoc.d.ts.map +1 -0
  45. package/dist/src/components/PyDoc.js +280 -0
  46. package/dist/src/components/PythonBlock.d.ts +23 -0
  47. package/dist/src/components/PythonBlock.d.ts.map +1 -0
  48. package/dist/src/components/PythonBlock.js +31 -0
  49. package/dist/src/components/Reference.d.ts +13 -0
  50. package/dist/src/components/Reference.d.ts.map +1 -0
  51. package/dist/src/components/Reference.js +18 -0
  52. package/dist/src/components/SourceFile.d.ts +46 -0
  53. package/dist/src/components/SourceFile.d.ts.map +1 -0
  54. package/dist/src/components/SourceFile.js +75 -0
  55. package/dist/src/components/StatementList.d.ts +25 -0
  56. package/dist/src/components/StatementList.d.ts.map +1 -0
  57. package/dist/src/components/StatementList.js +29 -0
  58. package/dist/src/components/VariableDeclaration.d.ts +62 -0
  59. package/dist/src/components/VariableDeclaration.d.ts.map +1 -0
  60. package/dist/src/components/VariableDeclaration.js +131 -0
  61. package/dist/src/components/index.d.ts +19 -0
  62. package/dist/src/components/index.d.ts.map +1 -0
  63. package/dist/src/components/index.js +18 -0
  64. package/dist/src/create-module.d.ts +16 -0
  65. package/dist/src/create-module.d.ts.map +1 -0
  66. package/dist/src/create-module.js +64 -0
  67. package/dist/src/index.d.ts +8 -0
  68. package/dist/src/index.d.ts.map +1 -0
  69. package/dist/src/index.js +7 -0
  70. package/dist/src/name-policy.d.ts +5 -0
  71. package/dist/src/name-policy.d.ts.map +1 -0
  72. package/dist/src/name-policy.js +47 -0
  73. package/dist/src/parameter-descriptor.d.ts +31 -0
  74. package/dist/src/parameter-descriptor.d.ts.map +1 -0
  75. package/dist/src/parameter-descriptor.js +1 -0
  76. package/dist/src/symbol-creation.d.ts +4 -0
  77. package/dist/src/symbol-creation.d.ts.map +1 -0
  78. package/dist/src/symbol-creation.js +24 -0
  79. package/dist/src/symbols/custom-output-scope.d.ts +10 -0
  80. package/dist/src/symbols/custom-output-scope.d.ts.map +1 -0
  81. package/dist/src/symbols/custom-output-scope.js +25 -0
  82. package/dist/src/symbols/index.d.ts +7 -0
  83. package/dist/src/symbols/index.d.ts.map +1 -0
  84. package/dist/src/symbols/index.js +6 -0
  85. package/dist/src/symbols/python-member-scope.d.ts +7 -0
  86. package/dist/src/symbols/python-member-scope.d.ts.map +1 -0
  87. package/dist/src/symbols/python-member-scope.js +9 -0
  88. package/dist/src/symbols/python-module-scope.d.ts +25 -0
  89. package/dist/src/symbols/python-module-scope.d.ts.map +1 -0
  90. package/dist/src/symbols/python-module-scope.js +52 -0
  91. package/dist/src/symbols/python-output-symbol.d.ts +19 -0
  92. package/dist/src/symbols/python-output-symbol.d.ts.map +1 -0
  93. package/dist/src/symbols/python-output-symbol.js +22 -0
  94. package/dist/src/symbols/reference.d.ts +4 -0
  95. package/dist/src/symbols/reference.d.ts.map +1 -0
  96. package/dist/src/symbols/reference.js +60 -0
  97. package/dist/src/symbols/scopes.d.ts +5 -0
  98. package/dist/src/symbols/scopes.d.ts.map +1 -0
  99. package/dist/src/symbols/scopes.js +4 -0
  100. package/dist/src/utils.d.ts +7 -0
  101. package/dist/src/utils.d.ts.map +1 -0
  102. package/dist/src/utils.js +12 -0
  103. package/dist/test/callsignatures.test.d.ts +2 -0
  104. package/dist/test/callsignatures.test.d.ts.map +1 -0
  105. package/dist/test/callsignatures.test.js +276 -0
  106. package/dist/test/classdeclarations.test.d.ts +2 -0
  107. package/dist/test/classdeclarations.test.d.ts.map +1 -0
  108. package/dist/test/classdeclarations.test.js +397 -0
  109. package/dist/test/classinstantiations.test.d.ts +2 -0
  110. package/dist/test/classinstantiations.test.d.ts.map +1 -0
  111. package/dist/test/classinstantiations.test.js +168 -0
  112. package/dist/test/enums.test.d.ts +2 -0
  113. package/dist/test/enums.test.d.ts.map +1 -0
  114. package/dist/test/enums.test.js +211 -0
  115. package/dist/test/externals.test.d.ts +2 -0
  116. package/dist/test/externals.test.d.ts.map +1 -0
  117. package/dist/test/externals.test.js +219 -0
  118. package/dist/test/functioncallexpressions.test.d.ts +2 -0
  119. package/dist/test/functioncallexpressions.test.d.ts.map +1 -0
  120. package/dist/test/functioncallexpressions.test.js +156 -0
  121. package/dist/test/functiondeclaration.test.d.ts +2 -0
  122. package/dist/test/functiondeclaration.test.d.ts.map +1 -0
  123. package/dist/test/functiondeclaration.test.js +363 -0
  124. package/dist/test/imports.test.d.ts +2 -0
  125. package/dist/test/imports.test.d.ts.map +1 -0
  126. package/dist/test/imports.test.js +262 -0
  127. package/dist/test/memberexpressions.test.d.ts +2 -0
  128. package/dist/test/memberexpressions.test.d.ts.map +1 -0
  129. package/dist/test/memberexpressions.test.js +879 -0
  130. package/dist/test/namepolicies.test.d.ts +2 -0
  131. package/dist/test/namepolicies.test.d.ts.map +1 -0
  132. package/dist/test/namepolicies.test.js +109 -0
  133. package/dist/test/pydocs.test.d.ts +2 -0
  134. package/dist/test/pydocs.test.d.ts.map +1 -0
  135. package/dist/test/pydocs.test.js +500 -0
  136. package/dist/test/references.test.d.ts +2 -0
  137. package/dist/test/references.test.d.ts.map +1 -0
  138. package/dist/test/references.test.js +49 -0
  139. package/dist/test/sourcefiles.test.d.ts +2 -0
  140. package/dist/test/sourcefiles.test.d.ts.map +1 -0
  141. package/dist/test/sourcefiles.test.js +198 -0
  142. package/dist/test/utils.d.ts +23 -0
  143. package/dist/test/utils.d.ts.map +1 -0
  144. package/dist/test/utils.js +88 -0
  145. package/dist/test/values.test.d.ts +2 -0
  146. package/dist/test/values.test.d.ts.map +1 -0
  147. package/dist/test/values.test.js +78 -0
  148. package/dist/test/variables.test.d.ts +2 -0
  149. package/dist/test/variables.test.d.ts.map +1 -0
  150. package/dist/test/variables.test.js +173 -0
  151. package/dist/tsconfig.tsbuildinfo +1 -0
  152. package/package.json +39 -0
  153. package/src/builtins/python.ts +20 -0
  154. package/src/components/Atom.tsx +76 -0
  155. package/src/components/CallSignature.tsx +251 -0
  156. package/src/components/ClassDeclaration.tsx +98 -0
  157. package/src/components/ClassInstantiation.tsx +54 -0
  158. package/src/components/Declaration.tsx +91 -0
  159. package/src/components/EnumDeclaration.tsx +291 -0
  160. package/src/components/EnumMember.tsx +92 -0
  161. package/src/components/FunctionCallExpression.tsx +36 -0
  162. package/src/components/FunctionDeclaration.tsx +121 -0
  163. package/src/components/ImportStatement.tsx +134 -0
  164. package/src/components/MemberExpression.tsx +456 -0
  165. package/src/components/NoNamePolicy.tsx +31 -0
  166. package/src/components/PyDoc.tsx +331 -0
  167. package/src/components/PythonBlock.tsx +26 -0
  168. package/src/components/Reference.tsx +21 -0
  169. package/src/components/SourceFile.tsx +93 -0
  170. package/src/components/StatementList.tsx +28 -0
  171. package/src/components/VariableDeclaration.tsx +180 -0
  172. package/src/components/index.ts +18 -0
  173. package/src/create-module.ts +102 -0
  174. package/src/index.ts +7 -0
  175. package/src/name-policy.ts +101 -0
  176. package/src/parameter-descriptor.ts +36 -0
  177. package/src/symbol-creation.ts +36 -0
  178. package/src/symbols/custom-output-scope.ts +35 -0
  179. package/src/symbols/index.ts +6 -0
  180. package/src/symbols/python-member-scope.ts +12 -0
  181. package/src/symbols/python-module-scope.ts +89 -0
  182. package/src/symbols/python-output-symbol.ts +36 -0
  183. package/src/symbols/reference.ts +99 -0
  184. package/src/symbols/scopes.ts +9 -0
  185. package/src/utils.ts +27 -0
  186. package/temp/api.json +7207 -0
  187. package/test/callsignatures.test.tsx +256 -0
  188. package/test/classdeclarations.test.tsx +320 -0
  189. package/test/classinstantiations.test.tsx +159 -0
  190. package/test/enums.test.tsx +203 -0
  191. package/test/externals.test.tsx +190 -0
  192. package/test/functioncallexpressions.test.tsx +145 -0
  193. package/test/functiondeclaration.test.tsx +327 -0
  194. package/test/imports.test.tsx +214 -0
  195. package/test/memberexpressions.test.tsx +725 -0
  196. package/test/namepolicies.test.tsx +109 -0
  197. package/test/pydocs.test.tsx +528 -0
  198. package/test/references.test.tsx +36 -0
  199. package/test/sourcefiles.test.tsx +131 -0
  200. package/test/utils.tsx +131 -0
  201. package/test/values.test.tsx +61 -0
  202. package/test/variables.test.tsx +153 -0
  203. package/tsconfig.json +12 -0
  204. package/tsdoc-metadata.json +11 -0
  205. package/vitest.config.ts +10 -0
  206. package/vitest.setup.ts +1 -0
@@ -0,0 +1,363 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { refkey } from "@alloy-js/core";
3
+ import { d } from "@alloy-js/core/testing";
4
+ import { describe, expect, it } from "vitest";
5
+ import * as py from "../src/index.js";
6
+ import { assertFileContents, toSourceText, toSourceTextMultiple } from "./utils.js";
7
+ describe("Function Declaration", () => {
8
+ it("renders a function with no body as 'pass'", () => {
9
+ const result = toSourceText([_$createComponent(py.FunctionDeclaration, {
10
+ name: "foo",
11
+ instanceFunction: true
12
+ })]);
13
+ expect(result).toRenderTo(d`
14
+ def foo(self):
15
+ pass
16
+
17
+
18
+ `);
19
+ });
20
+ it("renders a function with no body as 'pass' with return type", () => {
21
+ const result = toSourceText([_$createComponent(py.FunctionDeclaration, {
22
+ name: "foo",
23
+ returnType: "int"
24
+ })]);
25
+ expect(result).toRenderTo(d`
26
+ def foo() -> int:
27
+ pass
28
+
29
+
30
+ `);
31
+ });
32
+ it("renders a function that calls another function", () => {
33
+ const refkeyFoo = refkey();
34
+ const result = toSourceText([_$createComponent(py.StatementList, {
35
+ get children() {
36
+ return [_$createComponent(py.FunctionDeclaration, {
37
+ name: "foo",
38
+ instanceFunction: true,
39
+ returnType: "int",
40
+ refkey: refkeyFoo
41
+ }), _$createComponent(py.FunctionDeclaration, {
42
+ name: "bar",
43
+ instanceFunction: true,
44
+ returnType: "int",
45
+ get children() {
46
+ return _$createComponent(py.VariableDeclaration, {
47
+ name: "result",
48
+ type: "int",
49
+ get initializer() {
50
+ return _$createComponent(py.FunctionCallExpression, {
51
+ target: refkeyFoo,
52
+ args: []
53
+ });
54
+ }
55
+ });
56
+ }
57
+ })];
58
+ }
59
+ })]);
60
+ expect(result).toRenderTo(d`
61
+ def foo(self) -> int:
62
+ pass
63
+
64
+ def bar(self) -> int:
65
+ result: int = foo()
66
+
67
+
68
+ `);
69
+ });
70
+ it("renders an instance function with a body", () => {
71
+ const result = toSourceText([_$createComponent(py.FunctionDeclaration, {
72
+ name: "bar",
73
+ instanceFunction: true,
74
+ children: "print('hi')"
75
+ })]);
76
+ expect(result).toRenderTo(d`
77
+ def bar(self):
78
+ print('hi')
79
+
80
+
81
+ `);
82
+ });
83
+ it("renders a function with parameters", () => {
84
+ const result = toSourceText([_$createComponent(py.FunctionDeclaration, {
85
+ name: "baz",
86
+ parameters: [{
87
+ name: "x",
88
+ type: "int"
89
+ }, {
90
+ name: "y",
91
+ default: 0
92
+ }, {
93
+ name: "z",
94
+ type: "int",
95
+ default: 42
96
+ }],
97
+ args: true,
98
+ kwargs: true,
99
+ children: "print(x, y)"
100
+ })]);
101
+ expect(result).toRenderTo(d`
102
+ def baz(x: int, y=0, z: int = 42, *args, **kwargs):
103
+ print(x, y)
104
+
105
+
106
+ `);
107
+ });
108
+ it("renders an __init__ function with no body as 'pass'", () => {
109
+ const result = toSourceText([_$createComponent(py.InitFunctionDeclaration, {
110
+ parameters: [{
111
+ name: "x"
112
+ }]
113
+ })]);
114
+ expect(result).toRenderTo(d`
115
+ def __init__(self, x):
116
+ pass
117
+
118
+
119
+ `);
120
+ });
121
+ it("can be an async function", () => {
122
+ expect(toSourceText([_$createComponent(py.FunctionDeclaration, {
123
+ async: true,
124
+ name: "foo"
125
+ })])).toBe(d`
126
+ async def foo():
127
+ pass
128
+
129
+ `);
130
+ });
131
+ it("can be an async function with returnType", () => {
132
+ expect(toSourceText([_$createComponent(py.FunctionDeclaration, {
133
+ async: true,
134
+ name: "foo",
135
+ returnType: "Foo"
136
+ })])).toBe(d`
137
+ async def foo() -> Foo:
138
+ pass
139
+
140
+ `);
141
+ });
142
+ it("can be an async function with returnType element with Reference", () => {
143
+ expect(toSourceText([_$createComponent(py.StatementList, {
144
+ get children() {
145
+ return [_$createComponent(py.ClassDeclaration, {
146
+ name: "Foo"
147
+ }), _$createComponent(py.FunctionDeclaration, {
148
+ async: true,
149
+ name: "foo",
150
+ get returnType() {
151
+ return _$createComponent(py.Reference, {
152
+ get refkey() {
153
+ return refkey("Foo");
154
+ }
155
+ });
156
+ }
157
+ })];
158
+ }
159
+ })])).toBe(d`
160
+ class Foo:
161
+ pass
162
+
163
+ async def foo() -> Foo:
164
+ pass
165
+
166
+ `);
167
+ });
168
+ it("supports parameters", () => {
169
+ const decl = _$createComponent(py.FunctionDeclaration, {
170
+ name: "foo",
171
+ parameters: ["a", "b"],
172
+ children: "return a + b"
173
+ });
174
+ expect(toSourceText([decl])).toBe(d`
175
+ def foo(a, b):
176
+ return a + b
177
+
178
+ `);
179
+ });
180
+ it("supports type parameters", () => {
181
+ const decl = _$createComponent(py.FunctionDeclaration, {
182
+ name: "foo",
183
+ parameters: ["a", "b"],
184
+ typeParameters: ["T", "U"],
185
+ children: "return a + b"
186
+ });
187
+ expect(toSourceText([decl])).toBe(d`
188
+ def foo[T, U](a, b):
189
+ return a + b
190
+
191
+ `);
192
+ });
193
+ it("renders function with parameters", () => {
194
+ const parameters = [{
195
+ name: "x",
196
+ type: "int"
197
+ }];
198
+ const decl = _$createComponent(py.FunctionDeclaration, {
199
+ name: "foo",
200
+ instanceFunction: true,
201
+ parameters: parameters,
202
+ children: "self.attribute = \"value\""
203
+ });
204
+ expect(toSourceText([decl])).toBe(d`
205
+ def foo(self, x: int):
206
+ self.attribute = "value"
207
+
208
+ `);
209
+ });
210
+ it("renders __init__ function with parameters", () => {
211
+ const parameters = [{
212
+ name: "x",
213
+ type: "int"
214
+ }];
215
+ const decl = _$createComponent(py.InitFunctionDeclaration, {
216
+ parameters: parameters,
217
+ children: "self.attribute = \"value\""
218
+ });
219
+ expect(toSourceText([decl])).toBe(d`
220
+ def __init__(self, x: int):
221
+ self.attribute = "value"
222
+
223
+ `);
224
+ });
225
+ it("renders nested functions", () => {
226
+ const parameters = [{
227
+ name: "x",
228
+ type: "int"
229
+ }];
230
+ const parameters_nested = [{
231
+ name: "y",
232
+ type: "int"
233
+ }];
234
+ const parameters_nested_nested = [{
235
+ name: "z",
236
+ type: "int"
237
+ }];
238
+ const fooRef = refkey();
239
+ const barRef = refkey();
240
+ const foobarRef = refkey();
241
+ const decl = _$createComponent(py.FunctionDeclaration, {
242
+ name: "foo",
243
+ parameters: parameters,
244
+ refkey: fooRef,
245
+ get children() {
246
+ return [_$createComponent(py.FunctionDeclaration, {
247
+ name: "bar",
248
+ parameters: parameters_nested,
249
+ refkey: barRef,
250
+ get children() {
251
+ return [_$createComponent(py.FunctionDeclaration, {
252
+ name: "foobar",
253
+ parameters: parameters_nested_nested,
254
+ refkey: foobarRef,
255
+ children: "return z * 2"
256
+ }), "return", " ", _$createComponent(py.FunctionCallExpression, {
257
+ target: foobarRef,
258
+ get args() {
259
+ return [_$createComponent(py.Atom, {
260
+ jsValue: 2
261
+ })];
262
+ }
263
+ })];
264
+ }
265
+ }), "return", " ", _$createComponent(py.FunctionCallExpression, {
266
+ target: barRef,
267
+ get args() {
268
+ return [_$createComponent(py.Atom, {
269
+ jsValue: 3
270
+ })];
271
+ }
272
+ })];
273
+ }
274
+ });
275
+ expect(toSourceText([decl])).toBe(d`
276
+ def foo(x: int):
277
+ def bar(y: int):
278
+ def foobar(z: int):
279
+ return z * 2
280
+ return foobar(2)
281
+ return bar(3)
282
+
283
+ `);
284
+ });
285
+ it("renders complex typing structure", () => {
286
+ const res = toSourceTextMultiple([_$createComponent(py.SourceFile, {
287
+ path: "mod1.py",
288
+ get children() {
289
+ return _$createComponent(py.ClassDeclaration, {
290
+ name: "Foo"
291
+ });
292
+ }
293
+ }), _$createComponent(py.SourceFile, {
294
+ path: "mod2.py",
295
+ get children() {
296
+ return [_$createComponent(py.ClassDeclaration, {
297
+ name: "A"
298
+ }), _$createComponent(py.ClassDeclaration, {
299
+ name: "B"
300
+ })];
301
+ }
302
+ }), _$createComponent(py.SourceFile, {
303
+ path: "usage.py",
304
+ get children() {
305
+ return _$createComponent(py.FunctionDeclaration, {
306
+ async: true,
307
+ name: "foo",
308
+ get parameters() {
309
+ return [{
310
+ name: "x",
311
+ type: _$createComponent(py.Reference, {
312
+ get refkey() {
313
+ return refkey("A");
314
+ }
315
+ })
316
+ }, {
317
+ name: "y",
318
+ type: _$createComponent(py.Reference, {
319
+ get refkey() {
320
+ return refkey("B");
321
+ }
322
+ })
323
+ }];
324
+ },
325
+ args: true,
326
+ kwargs: true,
327
+ get returnType() {
328
+ return _$createComponent(py.Reference, {
329
+ get refkey() {
330
+ return refkey("Foo");
331
+ }
332
+ });
333
+ }
334
+ });
335
+ }
336
+ })]);
337
+ assertFileContents(res, {
338
+ "mod1.py": `
339
+ class Foo:
340
+ pass
341
+
342
+ `,
343
+ "mod2.py": `
344
+ class A:
345
+ pass
346
+
347
+
348
+ class B:
349
+ pass
350
+
351
+ `,
352
+ "usage.py": `
353
+ from mod1 import Foo
354
+ from mod2 import A
355
+ from mod2 import B
356
+
357
+ async def foo(x: A, y: B, *args, **kwargs) -> Foo:
358
+ pass
359
+
360
+ `
361
+ });
362
+ });
363
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=imports.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"imports.test.d.ts","sourceRoot":"","sources":["../../test/imports.test.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,262 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { refkey } from "@alloy-js/core";
3
+ import { describe, expect, it } from "vitest";
4
+ import { ImportStatement } from "../src/components/ImportStatement.js";
5
+ import * as py from "../src/index.js";
6
+ import { createPythonSymbol } from "../src/symbol-creation.js";
7
+ import { ImportedSymbol, ImportRecords } from "../src/symbols/index.js";
8
+ import { assertFileContents, createPythonModuleScope, toSourceText, toSourceTextMultiple } from "./utils.js";
9
+ describe("ImportStatement", () => {
10
+ it("renders module import", () => {
11
+ const result = toSourceText([_$createComponent(ImportStatement, {
12
+ path: "sys"
13
+ })]);
14
+ const expected = `import sys`;
15
+ expect(result).toRenderTo(expected);
16
+ });
17
+ it("renders named imports", () => {
18
+ const sqrtSymbol = createPythonSymbol("sqrt", {
19
+ binder: undefined,
20
+ scope: undefined
21
+ });
22
+ const piSymbol = createPythonSymbol("pi", {
23
+ binder: undefined,
24
+ scope: undefined
25
+ });
26
+ const symbols = new Set([new ImportedSymbol(sqrtSymbol, sqrtSymbol), new ImportedSymbol(piSymbol, piSymbol)]);
27
+ const result = toSourceText([_$createComponent(ImportStatement, {
28
+ path: "math",
29
+ symbols: symbols
30
+ })]);
31
+ const expected = `from math import pi, sqrt`;
32
+ expect(result).toRenderTo(expected);
33
+ });
34
+ });
35
+ describe("ImportStatements", () => {
36
+ it("renders multiple import statements", () => {
37
+ const pythonModuleScope = createPythonModuleScope("math", undefined);
38
+ const sqrtSymbol = createPythonSymbol("sqrt", {
39
+ binder: undefined,
40
+ scope: undefined
41
+ });
42
+ const piSymbol = createPythonSymbol("pi", {
43
+ binder: undefined,
44
+ scope: undefined
45
+ });
46
+ const mathSymbols = new Set([new ImportedSymbol(sqrtSymbol, sqrtSymbol), new ImportedSymbol(piSymbol, piSymbol)]);
47
+ const sysModuleScope = createPythonModuleScope("sys", undefined);
48
+ const requestsScope = createPythonModuleScope("requests", undefined);
49
+ const getSymbol = createPythonSymbol("get", {
50
+ binder: undefined,
51
+ scope: undefined
52
+ });
53
+ const requestsSymbols = new Set([new ImportedSymbol(getSymbol, getSymbol)]);
54
+ const records = new ImportRecords([[pythonModuleScope, {
55
+ symbols: mathSymbols
56
+ }], [requestsScope, {
57
+ symbols: requestsSymbols
58
+ }], [sysModuleScope, {
59
+ symbols: new Set()
60
+ }]]);
61
+ const result = toSourceText([_$createComponent(py.ImportStatements, {
62
+ records: records
63
+ })]);
64
+ const expected = `
65
+ from math import pi
66
+ from math import sqrt
67
+ from requests import get
68
+ import sys`;
69
+ expect(result).toRenderTo(expected);
70
+ });
71
+ it("renders multiple import statements, but joining imports from the same module", () => {
72
+ const pythonModuleScope = createPythonModuleScope("math", undefined);
73
+ const sqrtSymbol = createPythonSymbol("sqrt", {
74
+ binder: undefined,
75
+ scope: undefined
76
+ });
77
+ const piSymbol = createPythonSymbol("pi", {
78
+ binder: undefined,
79
+ scope: undefined
80
+ });
81
+ const mathSymbols = new Set([new ImportedSymbol(sqrtSymbol, sqrtSymbol), new ImportedSymbol(piSymbol, piSymbol)]);
82
+ const requestsScope = createPythonModuleScope("requests", undefined);
83
+ const getSymbol = createPythonSymbol("get", {
84
+ binder: undefined,
85
+ scope: undefined
86
+ });
87
+ const postSymbol = createPythonSymbol("post", {
88
+ binder: undefined,
89
+ scope: undefined
90
+ });
91
+ const requestsSymbols = new Set([new ImportedSymbol(getSymbol, getSymbol), new ImportedSymbol(postSymbol, postSymbol)]);
92
+ const records = new ImportRecords([[pythonModuleScope, {
93
+ symbols: mathSymbols
94
+ }], [requestsScope, {
95
+ symbols: requestsSymbols
96
+ }]]);
97
+ const result = toSourceText([_$createComponent(py.ImportStatements, {
98
+ records: records,
99
+ joinImportsFromSameModule: true
100
+ })]);
101
+ const expected = `
102
+ from math import pi, sqrt
103
+ from requests import get, post`;
104
+ expect(result).toRenderTo(expected);
105
+ });
106
+ });
107
+ describe("Imports being used", () => {
108
+ it("works with importing the same name many times from different files with the default name conflict resolver", () => {
109
+ const rk1 = refkey();
110
+ const rk2 = refkey();
111
+ const rk3 = refkey();
112
+ const result = toSourceTextMultiple([_$createComponent(py.SourceFile, {
113
+ path: "test_1.py",
114
+ get children() {
115
+ return _$createComponent(py.VariableDeclaration, {
116
+ name: "conflict",
117
+ refkey: rk1
118
+ });
119
+ }
120
+ }), _$createComponent(py.SourceFile, {
121
+ path: "test_3.py",
122
+ get children() {
123
+ return _$createComponent(py.VariableDeclaration, {
124
+ name: "conflict",
125
+ refkey: rk3
126
+ });
127
+ }
128
+ }), _$createComponent(py.SourceFile, {
129
+ path: "test_2.py",
130
+ get children() {
131
+ return _$createComponent(py.VariableDeclaration, {
132
+ name: "conflict",
133
+ refkey: rk2
134
+ });
135
+ }
136
+ }), _$createComponent(py.SourceFile, {
137
+ path: "test.py",
138
+ get children() {
139
+ return _$createComponent(py.StatementList, {
140
+ get children() {
141
+ return [_$createComponent(py.VariableDeclaration, {
142
+ name: "one",
143
+ initializer: rk1
144
+ }), _$createComponent(py.VariableDeclaration, {
145
+ name: "three",
146
+ initializer: rk3
147
+ }), _$createComponent(py.VariableDeclaration, {
148
+ name: "two",
149
+ initializer: rk2
150
+ })];
151
+ }
152
+ });
153
+ }
154
+ })]);
155
+ assertFileContents(result, {
156
+ "test.py": `
157
+ from test_1 import conflict
158
+ from test_2 import conflict as conflict_3_test_2
159
+ from test_3 import conflict as conflict_2_test_3
160
+
161
+ one = conflict
162
+ three = conflict_2_test_3
163
+ two = conflict_3_test_2
164
+ `
165
+ });
166
+ });
167
+ it("works with importing the same name many times from different files and with the correct order", () => {
168
+ const rk1 = refkey();
169
+ const rk2 = refkey();
170
+ const rk3 = refkey();
171
+ const rk4 = refkey();
172
+ const rk5 = refkey();
173
+ const rk6 = refkey();
174
+ const rk7 = refkey();
175
+ const result = toSourceTextMultiple([_$createComponent(py.SourceFile, {
176
+ path: "test_1.py",
177
+ get children() {
178
+ return [_$createComponent(py.VariableDeclaration, {
179
+ name: "conflict",
180
+ refkey: rk1
181
+ }), _$createComponent(py.VariableDeclaration, {
182
+ name: "something_else",
183
+ refkey: rk4
184
+ })];
185
+ }
186
+ }), _$createComponent(py.SourceFile, {
187
+ path: "test_2.py",
188
+ get children() {
189
+ return [_$createComponent(py.VariableDeclaration, {
190
+ name: "conflict",
191
+ refkey: rk2
192
+ }), _$createComponent(py.VariableDeclaration, {
193
+ name: "something",
194
+ refkey: rk6
195
+ }), _$createComponent(py.VariableDeclaration, {
196
+ name: "something_else",
197
+ refkey: rk5
198
+ })];
199
+ }
200
+ }), _$createComponent(py.SourceFile, {
201
+ path: "test_3.py",
202
+ get children() {
203
+ return [_$createComponent(py.VariableDeclaration, {
204
+ name: "conflict",
205
+ refkey: rk3
206
+ }), _$createComponent(py.VariableDeclaration, {
207
+ name: "something",
208
+ refkey: rk7
209
+ })];
210
+ }
211
+ }), _$createComponent(py.SourceFile, {
212
+ path: "test.py",
213
+ get children() {
214
+ return _$createComponent(py.StatementList, {
215
+ get children() {
216
+ return [_$createComponent(py.VariableDeclaration, {
217
+ name: "one",
218
+ initializer: rk1
219
+ }), _$createComponent(py.VariableDeclaration, {
220
+ name: "two",
221
+ initializer: rk2
222
+ }), _$createComponent(py.VariableDeclaration, {
223
+ name: "three",
224
+ initializer: rk3
225
+ }), _$createComponent(py.VariableDeclaration, {
226
+ name: "something_else",
227
+ initializer: rk4
228
+ }), _$createComponent(py.VariableDeclaration, {
229
+ name: "something_else_two",
230
+ initializer: rk5
231
+ }), _$createComponent(py.VariableDeclaration, {
232
+ name: "something",
233
+ initializer: rk6
234
+ }), _$createComponent(py.VariableDeclaration, {
235
+ name: "something_two",
236
+ initializer: rk7
237
+ })];
238
+ }
239
+ });
240
+ }
241
+ })]);
242
+ assertFileContents(result, {
243
+ "test.py": `
244
+ from test_1 import conflict
245
+ from test_1 import something_else as something_else_2_test_1
246
+ from test_2 import conflict as conflict_2_test_2
247
+ from test_2 import something as something_2_test_2
248
+ from test_2 import something_else as something_else_3_test_2
249
+ from test_3 import conflict as conflict_3_test_3
250
+ from test_3 import something as something_3_test_3
251
+
252
+ one = conflict
253
+ two = conflict_2_test_2
254
+ three = conflict_3_test_3
255
+ something_else = something_else_2_test_1
256
+ something_else_two = something_else_3_test_2
257
+ something = something_2_test_2
258
+ something_two = something_3_test_3
259
+ `
260
+ });
261
+ });
262
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=memberexpressions.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memberexpressions.test.d.ts","sourceRoot":"","sources":["../../test/memberexpressions.test.tsx"],"names":[],"mappings":""}