@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.
- package/dist/src/builtins/python.js +2 -1
- package/dist/src/builtins/python.js.map +1 -0
- package/dist/src/components/Atom.js +2 -1
- package/dist/src/components/Atom.js.map +1 -0
- package/dist/src/components/CallSignature.d.ts.map +1 -1
- package/dist/src/components/CallSignature.js +10 -12
- package/dist/src/components/CallSignature.js.map +1 -0
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
- package/dist/src/components/ClassDeclaration.js +8 -21
- package/dist/src/components/ClassDeclaration.js.map +1 -0
- package/dist/src/components/ClassInstantiation.d.ts.map +1 -1
- package/dist/src/components/ClassInstantiation.js +6 -5
- package/dist/src/components/ClassInstantiation.js.map +1 -0
- package/dist/src/components/Declaration.d.ts +1 -5
- package/dist/src/components/Declaration.d.ts.map +1 -1
- package/dist/src/components/Declaration.js +8 -16
- package/dist/src/components/Declaration.js.map +1 -0
- package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
- package/dist/src/components/EnumDeclaration.js +35 -60
- package/dist/src/components/EnumDeclaration.js.map +1 -0
- package/dist/src/components/EnumMember.d.ts.map +1 -1
- package/dist/src/components/EnumMember.js +5 -5
- package/dist/src/components/EnumMember.js.map +1 -0
- package/dist/src/components/FunctionCallExpression.js +2 -1
- package/dist/src/components/FunctionCallExpression.js.map +1 -0
- package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
- package/dist/src/components/FunctionDeclaration.js +8 -16
- package/dist/src/components/FunctionDeclaration.js.map +1 -0
- package/dist/src/components/ImportStatement.js +2 -1
- package/dist/src/components/ImportStatement.js.map +1 -0
- package/dist/src/components/LexicalScope.d.ts +8 -0
- package/dist/src/components/LexicalScope.d.ts.map +1 -0
- package/dist/src/components/LexicalScope.js +22 -0
- package/dist/src/components/LexicalScope.js.map +1 -0
- package/dist/src/components/MemberExpression.js +2 -1
- package/dist/src/components/MemberExpression.js.map +1 -0
- package/dist/src/components/MemberScope.d.ts +8 -0
- package/dist/src/components/MemberScope.d.ts.map +1 -0
- package/dist/src/components/MemberScope.js +16 -0
- package/dist/src/components/MemberScope.js.map +1 -0
- package/dist/src/components/NoNamePolicy.js +2 -1
- package/dist/src/components/NoNamePolicy.js.map +1 -0
- package/dist/src/components/PyDoc.js +2 -1
- package/dist/src/components/PyDoc.js.map +1 -0
- package/dist/src/components/PythonBlock.js +2 -1
- package/dist/src/components/PythonBlock.js.map +1 -0
- package/dist/src/components/Reference.js +2 -1
- package/dist/src/components/Reference.js.map +1 -0
- package/dist/src/components/SourceFile.js +3 -3
- package/dist/src/components/SourceFile.js.map +1 -0
- package/dist/src/components/StatementList.js +2 -1
- package/dist/src/components/StatementList.js.map +1 -0
- package/dist/src/components/UnionTypeExpression.js +2 -1
- package/dist/src/components/UnionTypeExpression.js.map +1 -0
- package/dist/src/components/VariableDeclaration.d.ts.map +1 -1
- package/dist/src/components/VariableDeclaration.js +10 -32
- package/dist/src/components/VariableDeclaration.js.map +1 -0
- package/dist/src/components/index.d.ts +2 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +4 -1
- package/dist/src/components/index.js.map +1 -0
- package/dist/src/create-module.d.ts.map +1 -1
- package/dist/src/create-module.js +5 -5
- package/dist/src/create-module.js.map +1 -0
- package/dist/src/index.js +2 -1
- package/dist/src/index.js.map +1 -0
- package/dist/src/name-conflict-resolver.d.ts +3 -0
- package/dist/src/name-conflict-resolver.d.ts.map +1 -0
- package/dist/src/name-conflict-resolver.js +8 -0
- package/dist/src/name-conflict-resolver.js.map +1 -0
- package/dist/src/name-policy.js +2 -1
- package/dist/src/name-policy.js.map +1 -0
- package/dist/src/parameter-descriptor.js +2 -1
- package/dist/src/parameter-descriptor.js.map +1 -0
- package/dist/src/symbol-creation.d.ts +13 -2
- package/dist/src/symbol-creation.d.ts.map +1 -1
- package/dist/src/symbol-creation.js +35 -10
- package/dist/src/symbol-creation.js.map +1 -0
- package/dist/src/symbols/index.d.ts +1 -1
- package/dist/src/symbols/index.d.ts.map +1 -1
- package/dist/src/symbols/index.js +3 -2
- package/dist/src/symbols/index.js.map +1 -0
- package/dist/src/symbols/python-lexical-scope.d.ts +7 -0
- package/dist/src/symbols/python-lexical-scope.d.ts.map +1 -0
- package/dist/src/symbols/python-lexical-scope.js +15 -0
- package/dist/src/symbols/python-lexical-scope.js.map +1 -0
- package/dist/src/symbols/python-member-scope.d.ts +3 -4
- package/dist/src/symbols/python-member-scope.d.ts.map +1 -1
- package/dist/src/symbols/python-member-scope.js +6 -8
- package/dist/src/symbols/python-member-scope.js.map +1 -0
- package/dist/src/symbols/python-module-scope.d.ts +4 -9
- package/dist/src/symbols/python-module-scope.d.ts.map +1 -1
- package/dist/src/symbols/python-module-scope.js +8 -15
- package/dist/src/symbols/python-module-scope.js.map +1 -0
- package/dist/src/symbols/python-output-symbol.d.ts +10 -7
- package/dist/src/symbols/python-output-symbol.d.ts.map +1 -1
- package/dist/src/symbols/python-output-symbol.js +25 -7
- package/dist/src/symbols/python-output-symbol.js.map +1 -0
- package/dist/src/symbols/reference.d.ts +2 -2
- package/dist/src/symbols/reference.d.ts.map +1 -1
- package/dist/src/symbols/reference.js +37 -36
- package/dist/src/symbols/reference.js.map +1 -0
- package/dist/src/symbols/scopes.d.ts +3 -1
- package/dist/src/symbols/scopes.d.ts.map +1 -1
- package/dist/src/symbols/scopes.js +10 -1
- package/dist/src/symbols/scopes.js.map +1 -0
- package/dist/src/utils.js +2 -1
- package/dist/src/utils.js.map +1 -0
- package/dist/test/callsignatures.test.js +2 -1
- package/dist/test/callsignatures.test.js.map +1 -0
- package/dist/test/classdeclarations.test.js +89 -53
- package/dist/test/classdeclarations.test.js.map +1 -0
- package/dist/test/classinstantiations.test.js +2 -1
- package/dist/test/classinstantiations.test.js.map +1 -0
- package/dist/test/enums.test.js +10 -3
- package/dist/test/enums.test.js.map +1 -0
- package/dist/test/externals.test.js +8 -6
- package/dist/test/externals.test.js.map +1 -0
- package/dist/test/functioncallexpressions.test.js +2 -1
- package/dist/test/functioncallexpressions.test.js.map +1 -0
- package/dist/test/functiondeclaration.test.js +75 -37
- package/dist/test/functiondeclaration.test.js.map +1 -0
- package/dist/test/imports.test.js +11 -37
- package/dist/test/imports.test.js.map +1 -0
- package/dist/test/memberexpressions.test.js +2 -1
- package/dist/test/memberexpressions.test.js.map +1 -0
- package/dist/test/namepolicies.test.js +2 -1
- package/dist/test/namepolicies.test.js.map +1 -0
- package/dist/test/pydocs.test.js +2 -1
- package/dist/test/pydocs.test.js.map +1 -0
- package/dist/test/references.test.js +2 -1
- package/dist/test/references.test.js.map +1 -0
- package/dist/test/sourcefiles.test.js +2 -1
- package/dist/test/sourcefiles.test.js.map +1 -0
- package/dist/test/uniontypeexpression.test.js +2 -1
- package/dist/test/uniontypeexpression.test.js.map +1 -0
- package/dist/test/utils.d.ts +2 -3
- package/dist/test/utils.d.ts.map +1 -1
- package/dist/test/utils.js +8 -4
- package/dist/test/utils.js.map +1 -0
- package/dist/test/values.test.js +2 -1
- package/dist/test/values.test.js.map +1 -0
- package/dist/test/variables.test.js +13 -12
- package/dist/test/variables.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/components/CallSignature.tsx +9 -17
- package/src/components/ClassDeclaration.tsx +5 -32
- package/src/components/ClassInstantiation.tsx +6 -12
- package/src/components/Declaration.tsx +1 -20
- package/src/components/EnumDeclaration.tsx +18 -41
- package/src/components/EnumMember.tsx +1 -3
- package/src/components/FunctionDeclaration.tsx +5 -24
- package/src/components/LexicalScope.tsx +24 -0
- package/src/components/MemberScope.tsx +18 -0
- package/src/components/SourceFile.tsx +2 -2
- package/src/components/VariableDeclaration.tsx +7 -35
- package/src/components/index.ts +2 -0
- package/src/create-module.ts +7 -13
- package/src/name-conflict-resolver.ts +21 -0
- package/src/symbol-creation.ts +50 -11
- package/src/symbols/index.ts +1 -1
- package/src/symbols/python-lexical-scope.ts +16 -0
- package/src/symbols/python-member-scope.ts +4 -8
- package/src/symbols/python-module-scope.ts +6 -32
- package/src/symbols/python-output-symbol.ts +36 -10
- package/src/symbols/reference.tsx +70 -0
- package/src/symbols/scopes.ts +15 -1
- package/temp/api.json +897 -539
- package/test/classdeclarations.test.tsx +66 -31
- package/test/enums.test.tsx +2 -2
- package/test/externals.test.tsx +6 -7
- package/test/functiondeclaration.test.tsx +48 -39
- package/test/imports.test.tsx +11 -36
- package/test/utils.tsx +11 -5
- package/test/variables.test.tsx +11 -7
- package/dist/src/symbols/custom-output-scope.d.ts +0 -10
- package/dist/src/symbols/custom-output-scope.d.ts.map +0 -1
- package/dist/src/symbols/custom-output-scope.js +0 -25
- package/src/symbols/custom-output-scope.ts +0 -35
- 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
|
|
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
|
|
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
|
|
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
|
|
171
|
-
|
|
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
|
|
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
|
|
258
|
+
instanceVariable
|
|
232
259
|
/>
|
|
233
260
|
<py.FunctionDeclaration
|
|
234
261
|
name="instanceMethod"
|
|
235
|
-
instanceFunction
|
|
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
|
|
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
|
-
|
|
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
|
});
|
package/test/enums.test.tsx
CHANGED
|
@@ -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"
|
package/test/externals.test.tsx
CHANGED
|
@@ -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(
|
|
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(
|
|
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=
|
|
122
|
+
name="getUser"
|
|
124
123
|
parameters={[{ name: "userId", type: "int" }]}
|
|
125
124
|
returnType={py.requestsModule["models"]["Response"]}
|
|
126
|
-
instanceFunction
|
|
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
|
|
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(
|
|
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(
|
|
55
|
+
def foo() -> int:
|
|
63
56
|
pass
|
|
64
57
|
|
|
65
|
-
def bar(
|
|
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.
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
80
|
-
|
|
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.
|
|
110
|
+
<py.ClassDeclaration name="MyClass">
|
|
111
|
+
<py.InitFunctionDeclaration parameters={[{ name: "x" }]} />
|
|
112
|
+
</py.ClassDeclaration>,
|
|
114
113
|
]);
|
|
115
114
|
expect(result).toRenderTo(d`
|
|
116
|
-
|
|
117
|
-
|
|
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.
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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
|
-
|
|
209
|
-
self
|
|
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.
|
|
217
|
-
|
|
218
|
-
|
|
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
|
-
|
|
223
|
-
self
|
|
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
|
package/test/imports.test.tsx
CHANGED
|
@@ -19,14 +19,8 @@ describe("ImportStatement", () => {
|
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
it("renders named imports", () => {
|
|
22
|
-
const sqrtSymbol = createPythonSymbol("sqrt",
|
|
23
|
-
|
|
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
|
-
|
|
47
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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(
|
|
@@ -81,12 +82,18 @@ export function toSourceTextMultiple(
|
|
|
81
82
|
printOptions = {
|
|
82
83
|
printWidth: 80,
|
|
83
84
|
tabWidth: 4,
|
|
85
|
+
insertFinalNewLine: false,
|
|
84
86
|
};
|
|
85
87
|
} else {
|
|
88
|
+
printOptions.insertFinalNewLine = false;
|
|
86
89
|
printOptions.tabWidth = 4;
|
|
87
90
|
}
|
|
88
91
|
const content = (
|
|
89
|
-
<Output
|
|
92
|
+
<Output
|
|
93
|
+
externals={mergedExternals}
|
|
94
|
+
namePolicy={policy}
|
|
95
|
+
nameConflictResolver={pythonNameConflictResolver}
|
|
96
|
+
>
|
|
90
97
|
{sourceFiles}
|
|
91
98
|
</Output>
|
|
92
99
|
);
|
|
@@ -121,11 +128,10 @@ export function toSourceText(
|
|
|
121
128
|
// Helper function to create a PythonModuleScope to be used in tests
|
|
122
129
|
export function createPythonModuleScope(
|
|
123
130
|
name: string,
|
|
124
|
-
parent:
|
|
131
|
+
parent: OutputScope | undefined,
|
|
125
132
|
binder: Binder | undefined = undefined,
|
|
126
133
|
): PythonModuleScope {
|
|
127
|
-
return new PythonModuleScope(name, {
|
|
128
|
-
parent: parent,
|
|
134
|
+
return new PythonModuleScope(name, parent, {
|
|
129
135
|
binder: binder,
|
|
130
136
|
});
|
|
131
137
|
}
|
package/test/variables.test.tsx
CHANGED
|
@@ -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={
|
|
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={
|
|
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="
|
|
175
|
-
|
|
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
|
-
}
|