@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.
- package/CHANGELOG.md +3 -0
- package/LICENSE +7 -0
- package/api-extractor.json +4 -0
- package/dist/src/builtins/python.d.ts +9 -0
- package/dist/src/builtins/python.d.ts.map +1 -0
- package/dist/src/builtins/python.js +17 -0
- package/dist/src/components/Atom.d.ts +19 -0
- package/dist/src/components/Atom.d.ts.map +1 -0
- package/dist/src/components/Atom.js +82 -0
- package/dist/src/components/CallSignature.d.ts +79 -0
- package/dist/src/components/CallSignature.d.ts.map +1 -0
- package/dist/src/components/CallSignature.js +201 -0
- package/dist/src/components/ClassDeclaration.d.ts +37 -0
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -0
- package/dist/src/components/ClassDeclaration.js +83 -0
- package/dist/src/components/ClassInstantiation.d.ts +24 -0
- package/dist/src/components/ClassInstantiation.d.ts.map +1 -0
- package/dist/src/components/ClassInstantiation.js +35 -0
- package/dist/src/components/Declaration.d.ts +48 -0
- package/dist/src/components/Declaration.d.ts.map +1 -0
- package/dist/src/components/Declaration.js +37 -0
- package/dist/src/components/EnumDeclaration.d.ts +164 -0
- package/dist/src/components/EnumDeclaration.d.ts.map +1 -0
- package/dist/src/components/EnumDeclaration.js +278 -0
- package/dist/src/components/EnumMember.d.ts +46 -0
- package/dist/src/components/EnumMember.d.ts.map +1 -0
- package/dist/src/components/EnumMember.js +67 -0
- package/dist/src/components/FunctionCallExpression.d.ts +19 -0
- package/dist/src/components/FunctionCallExpression.d.ts.map +1 -0
- package/dist/src/components/FunctionCallExpression.js +40 -0
- package/dist/src/components/FunctionDeclaration.d.ts +47 -0
- package/dist/src/components/FunctionDeclaration.d.ts.map +1 -0
- package/dist/src/components/FunctionDeclaration.js +107 -0
- package/dist/src/components/ImportStatement.d.ts +39 -0
- package/dist/src/components/ImportStatement.d.ts.map +1 -0
- package/dist/src/components/ImportStatement.js +104 -0
- package/dist/src/components/MemberExpression.d.ts +97 -0
- package/dist/src/components/MemberExpression.d.ts.map +1 -0
- package/dist/src/components/MemberExpression.js +308 -0
- package/dist/src/components/NoNamePolicy.d.ts +23 -0
- package/dist/src/components/NoNamePolicy.d.ts.map +1 -0
- package/dist/src/components/NoNamePolicy.js +27 -0
- package/dist/src/components/PyDoc.d.ts +90 -0
- package/dist/src/components/PyDoc.d.ts.map +1 -0
- package/dist/src/components/PyDoc.js +280 -0
- package/dist/src/components/PythonBlock.d.ts +23 -0
- package/dist/src/components/PythonBlock.d.ts.map +1 -0
- package/dist/src/components/PythonBlock.js +31 -0
- package/dist/src/components/Reference.d.ts +13 -0
- package/dist/src/components/Reference.d.ts.map +1 -0
- package/dist/src/components/Reference.js +18 -0
- package/dist/src/components/SourceFile.d.ts +46 -0
- package/dist/src/components/SourceFile.d.ts.map +1 -0
- package/dist/src/components/SourceFile.js +75 -0
- package/dist/src/components/StatementList.d.ts +25 -0
- package/dist/src/components/StatementList.d.ts.map +1 -0
- package/dist/src/components/StatementList.js +29 -0
- package/dist/src/components/VariableDeclaration.d.ts +62 -0
- package/dist/src/components/VariableDeclaration.d.ts.map +1 -0
- package/dist/src/components/VariableDeclaration.js +131 -0
- package/dist/src/components/index.d.ts +19 -0
- package/dist/src/components/index.d.ts.map +1 -0
- package/dist/src/components/index.js +18 -0
- package/dist/src/create-module.d.ts +16 -0
- package/dist/src/create-module.d.ts.map +1 -0
- package/dist/src/create-module.js +64 -0
- package/dist/src/index.d.ts +8 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +7 -0
- package/dist/src/name-policy.d.ts +5 -0
- package/dist/src/name-policy.d.ts.map +1 -0
- package/dist/src/name-policy.js +47 -0
- package/dist/src/parameter-descriptor.d.ts +31 -0
- package/dist/src/parameter-descriptor.d.ts.map +1 -0
- package/dist/src/parameter-descriptor.js +1 -0
- package/dist/src/symbol-creation.d.ts +4 -0
- package/dist/src/symbol-creation.d.ts.map +1 -0
- package/dist/src/symbol-creation.js +24 -0
- package/dist/src/symbols/custom-output-scope.d.ts +10 -0
- package/dist/src/symbols/custom-output-scope.d.ts.map +1 -0
- package/dist/src/symbols/custom-output-scope.js +25 -0
- package/dist/src/symbols/index.d.ts +7 -0
- package/dist/src/symbols/index.d.ts.map +1 -0
- package/dist/src/symbols/index.js +6 -0
- package/dist/src/symbols/python-member-scope.d.ts +7 -0
- package/dist/src/symbols/python-member-scope.d.ts.map +1 -0
- package/dist/src/symbols/python-member-scope.js +9 -0
- package/dist/src/symbols/python-module-scope.d.ts +25 -0
- package/dist/src/symbols/python-module-scope.d.ts.map +1 -0
- package/dist/src/symbols/python-module-scope.js +52 -0
- package/dist/src/symbols/python-output-symbol.d.ts +19 -0
- package/dist/src/symbols/python-output-symbol.d.ts.map +1 -0
- package/dist/src/symbols/python-output-symbol.js +22 -0
- package/dist/src/symbols/reference.d.ts +4 -0
- package/dist/src/symbols/reference.d.ts.map +1 -0
- package/dist/src/symbols/reference.js +60 -0
- package/dist/src/symbols/scopes.d.ts +5 -0
- package/dist/src/symbols/scopes.d.ts.map +1 -0
- package/dist/src/symbols/scopes.js +4 -0
- package/dist/src/utils.d.ts +7 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +12 -0
- package/dist/test/callsignatures.test.d.ts +2 -0
- package/dist/test/callsignatures.test.d.ts.map +1 -0
- package/dist/test/callsignatures.test.js +276 -0
- package/dist/test/classdeclarations.test.d.ts +2 -0
- package/dist/test/classdeclarations.test.d.ts.map +1 -0
- package/dist/test/classdeclarations.test.js +397 -0
- package/dist/test/classinstantiations.test.d.ts +2 -0
- package/dist/test/classinstantiations.test.d.ts.map +1 -0
- package/dist/test/classinstantiations.test.js +168 -0
- package/dist/test/enums.test.d.ts +2 -0
- package/dist/test/enums.test.d.ts.map +1 -0
- package/dist/test/enums.test.js +211 -0
- package/dist/test/externals.test.d.ts +2 -0
- package/dist/test/externals.test.d.ts.map +1 -0
- package/dist/test/externals.test.js +219 -0
- package/dist/test/functioncallexpressions.test.d.ts +2 -0
- package/dist/test/functioncallexpressions.test.d.ts.map +1 -0
- package/dist/test/functioncallexpressions.test.js +156 -0
- package/dist/test/functiondeclaration.test.d.ts +2 -0
- package/dist/test/functiondeclaration.test.d.ts.map +1 -0
- package/dist/test/functiondeclaration.test.js +363 -0
- package/dist/test/imports.test.d.ts +2 -0
- package/dist/test/imports.test.d.ts.map +1 -0
- package/dist/test/imports.test.js +262 -0
- package/dist/test/memberexpressions.test.d.ts +2 -0
- package/dist/test/memberexpressions.test.d.ts.map +1 -0
- package/dist/test/memberexpressions.test.js +879 -0
- package/dist/test/namepolicies.test.d.ts +2 -0
- package/dist/test/namepolicies.test.d.ts.map +1 -0
- package/dist/test/namepolicies.test.js +109 -0
- package/dist/test/pydocs.test.d.ts +2 -0
- package/dist/test/pydocs.test.d.ts.map +1 -0
- package/dist/test/pydocs.test.js +500 -0
- package/dist/test/references.test.d.ts +2 -0
- package/dist/test/references.test.d.ts.map +1 -0
- package/dist/test/references.test.js +49 -0
- package/dist/test/sourcefiles.test.d.ts +2 -0
- package/dist/test/sourcefiles.test.d.ts.map +1 -0
- package/dist/test/sourcefiles.test.js +198 -0
- package/dist/test/utils.d.ts +23 -0
- package/dist/test/utils.d.ts.map +1 -0
- package/dist/test/utils.js +88 -0
- package/dist/test/values.test.d.ts +2 -0
- package/dist/test/values.test.d.ts.map +1 -0
- package/dist/test/values.test.js +78 -0
- package/dist/test/variables.test.d.ts +2 -0
- package/dist/test/variables.test.d.ts.map +1 -0
- package/dist/test/variables.test.js +173 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +39 -0
- package/src/builtins/python.ts +20 -0
- package/src/components/Atom.tsx +76 -0
- package/src/components/CallSignature.tsx +251 -0
- package/src/components/ClassDeclaration.tsx +98 -0
- package/src/components/ClassInstantiation.tsx +54 -0
- package/src/components/Declaration.tsx +91 -0
- package/src/components/EnumDeclaration.tsx +291 -0
- package/src/components/EnumMember.tsx +92 -0
- package/src/components/FunctionCallExpression.tsx +36 -0
- package/src/components/FunctionDeclaration.tsx +121 -0
- package/src/components/ImportStatement.tsx +134 -0
- package/src/components/MemberExpression.tsx +456 -0
- package/src/components/NoNamePolicy.tsx +31 -0
- package/src/components/PyDoc.tsx +331 -0
- package/src/components/PythonBlock.tsx +26 -0
- package/src/components/Reference.tsx +21 -0
- package/src/components/SourceFile.tsx +93 -0
- package/src/components/StatementList.tsx +28 -0
- package/src/components/VariableDeclaration.tsx +180 -0
- package/src/components/index.ts +18 -0
- package/src/create-module.ts +102 -0
- package/src/index.ts +7 -0
- package/src/name-policy.ts +101 -0
- package/src/parameter-descriptor.ts +36 -0
- package/src/symbol-creation.ts +36 -0
- package/src/symbols/custom-output-scope.ts +35 -0
- package/src/symbols/index.ts +6 -0
- package/src/symbols/python-member-scope.ts +12 -0
- package/src/symbols/python-module-scope.ts +89 -0
- package/src/symbols/python-output-symbol.ts +36 -0
- package/src/symbols/reference.ts +99 -0
- package/src/symbols/scopes.ts +9 -0
- package/src/utils.ts +27 -0
- package/temp/api.json +7207 -0
- package/test/callsignatures.test.tsx +256 -0
- package/test/classdeclarations.test.tsx +320 -0
- package/test/classinstantiations.test.tsx +159 -0
- package/test/enums.test.tsx +203 -0
- package/test/externals.test.tsx +190 -0
- package/test/functioncallexpressions.test.tsx +145 -0
- package/test/functiondeclaration.test.tsx +327 -0
- package/test/imports.test.tsx +214 -0
- package/test/memberexpressions.test.tsx +725 -0
- package/test/namepolicies.test.tsx +109 -0
- package/test/pydocs.test.tsx +528 -0
- package/test/references.test.tsx +36 -0
- package/test/sourcefiles.test.tsx +131 -0
- package/test/utils.tsx +131 -0
- package/test/values.test.tsx +61 -0
- package/test/variables.test.tsx +153 -0
- package/tsconfig.json +12 -0
- package/tsdoc-metadata.json +11 -0
- package/vitest.config.ts +10 -0
- package/vitest.setup.ts +1 -0
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
import { createComponent as _$createComponent, memo as _$memo } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { memberRefkey, 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("Python Class", () => {
|
|
8
|
+
it("renders a class with no body as 'pass'", () => {
|
|
9
|
+
const result = toSourceText([_$createComponent(py.ClassDeclaration, {
|
|
10
|
+
name: "Foo"
|
|
11
|
+
})]);
|
|
12
|
+
expect(result).toRenderTo(d`
|
|
13
|
+
class Foo:
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
`);
|
|
18
|
+
});
|
|
19
|
+
it("renders a class with a body", () => {
|
|
20
|
+
const result = toSourceText([_$createComponent(py.ClassDeclaration, {
|
|
21
|
+
name: "Bar",
|
|
22
|
+
children: "print('hi')"
|
|
23
|
+
})]);
|
|
24
|
+
expect(result).toRenderTo(d`
|
|
25
|
+
class Bar:
|
|
26
|
+
print('hi')
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
`);
|
|
30
|
+
});
|
|
31
|
+
it("renders a class with base classes", () => {
|
|
32
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
33
|
+
get children() {
|
|
34
|
+
return [_$createComponent(py.ClassDeclaration, {
|
|
35
|
+
name: "Base1"
|
|
36
|
+
}), _$createComponent(py.ClassDeclaration, {
|
|
37
|
+
name: "Base2"
|
|
38
|
+
}), _$createComponent(py.ClassDeclaration, {
|
|
39
|
+
name: "Baz",
|
|
40
|
+
get bases() {
|
|
41
|
+
return [refkey("Base1"), refkey("Base2")];
|
|
42
|
+
}
|
|
43
|
+
})];
|
|
44
|
+
}
|
|
45
|
+
})]);
|
|
46
|
+
const expected = d`
|
|
47
|
+
class Base1:
|
|
48
|
+
pass
|
|
49
|
+
|
|
50
|
+
class Base2:
|
|
51
|
+
pass
|
|
52
|
+
|
|
53
|
+
class Baz(Base1, Base2):
|
|
54
|
+
pass
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
`;
|
|
58
|
+
expect(result).toRenderTo(expected);
|
|
59
|
+
});
|
|
60
|
+
it("renders a class with base classes and body", () => {
|
|
61
|
+
const result = toSourceText([_$createComponent(py.ClassDeclaration, {
|
|
62
|
+
name: "Qux",
|
|
63
|
+
bases: ["Base"],
|
|
64
|
+
children: "print('hello')"
|
|
65
|
+
})]);
|
|
66
|
+
expect(result).toRenderTo(d`
|
|
67
|
+
class Qux(Base):
|
|
68
|
+
print('hello')
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
`);
|
|
72
|
+
});
|
|
73
|
+
it("renders classes across modules with inheritance", () => {
|
|
74
|
+
const result = toSourceTextMultiple([_$createComponent(py.SourceFile, {
|
|
75
|
+
path: "mod1.py",
|
|
76
|
+
get children() {
|
|
77
|
+
return _$createComponent(py.ClassDeclaration, {
|
|
78
|
+
name: "A"
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}), _$createComponent(py.SourceFile, {
|
|
82
|
+
path: "folder/mod2.py",
|
|
83
|
+
get children() {
|
|
84
|
+
return _$createComponent(py.ClassDeclaration, {
|
|
85
|
+
name: "B",
|
|
86
|
+
get bases() {
|
|
87
|
+
return [refkey("A")];
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}), _$createComponent(py.SourceFile, {
|
|
92
|
+
path: "mod3.py",
|
|
93
|
+
get children() {
|
|
94
|
+
return _$createComponent(py.ClassDeclaration, {
|
|
95
|
+
name: "C",
|
|
96
|
+
get bases() {
|
|
97
|
+
return [refkey("B")];
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
})]);
|
|
102
|
+
const mod1Expected = d`
|
|
103
|
+
class A:
|
|
104
|
+
pass
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
`;
|
|
108
|
+
const mod2Expected = d`
|
|
109
|
+
from mod1 import A
|
|
110
|
+
|
|
111
|
+
class B(A):
|
|
112
|
+
pass
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
`;
|
|
116
|
+
const mod3Expected = d`
|
|
117
|
+
from folder.mod2 import B
|
|
118
|
+
|
|
119
|
+
class C(B):
|
|
120
|
+
pass
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
`;
|
|
124
|
+
assertFileContents(result, {
|
|
125
|
+
"mod1.py": mod1Expected
|
|
126
|
+
});
|
|
127
|
+
assertFileContents(result, {
|
|
128
|
+
"folder/mod2.py": mod2Expected
|
|
129
|
+
});
|
|
130
|
+
assertFileContents(result, {
|
|
131
|
+
"mod3.py": mod3Expected
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
it("renders a class with class variables like foo: str, and also bar: A where A is another class", () => {
|
|
135
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
136
|
+
get children() {
|
|
137
|
+
return [_$createComponent(py.ClassDeclaration, {
|
|
138
|
+
name: "A"
|
|
139
|
+
}), _$createComponent(py.ClassDeclaration, {
|
|
140
|
+
name: "B",
|
|
141
|
+
get children() {
|
|
142
|
+
return _$createComponent(py.StatementList, {
|
|
143
|
+
get children() {
|
|
144
|
+
return [_$createComponent(py.VariableDeclaration, {
|
|
145
|
+
name: "bar",
|
|
146
|
+
get type() {
|
|
147
|
+
return refkey("A");
|
|
148
|
+
},
|
|
149
|
+
omitNone: true
|
|
150
|
+
}), _$createComponent(py.VariableDeclaration, {
|
|
151
|
+
name: "foo",
|
|
152
|
+
type: "str",
|
|
153
|
+
omitNone: true
|
|
154
|
+
})];
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
})];
|
|
159
|
+
}
|
|
160
|
+
})]);
|
|
161
|
+
const expected = d`
|
|
162
|
+
class A:
|
|
163
|
+
pass
|
|
164
|
+
|
|
165
|
+
class B:
|
|
166
|
+
bar: A
|
|
167
|
+
foo: str
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
`;
|
|
171
|
+
expect(result).toRenderTo(expected);
|
|
172
|
+
});
|
|
173
|
+
it("renders a class with class variables like foo: str, and another identical class", () => {
|
|
174
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
175
|
+
get children() {
|
|
176
|
+
return [_$createComponent(py.ClassDeclaration, {
|
|
177
|
+
name: "A",
|
|
178
|
+
get children() {
|
|
179
|
+
return _$createComponent(py.StatementList, {
|
|
180
|
+
get children() {
|
|
181
|
+
return _$createComponent(py.VariableDeclaration, {
|
|
182
|
+
name: "foo",
|
|
183
|
+
type: "str",
|
|
184
|
+
omitNone: true
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}), _$createComponent(py.ClassDeclaration, {
|
|
190
|
+
name: "B",
|
|
191
|
+
get children() {
|
|
192
|
+
return _$createComponent(py.StatementList, {
|
|
193
|
+
get children() {
|
|
194
|
+
return _$createComponent(py.VariableDeclaration, {
|
|
195
|
+
name: "foo",
|
|
196
|
+
type: "str",
|
|
197
|
+
omitNone: true
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
})];
|
|
203
|
+
}
|
|
204
|
+
})]);
|
|
205
|
+
const expected = d`
|
|
206
|
+
class A:
|
|
207
|
+
foo: str
|
|
208
|
+
|
|
209
|
+
class B:
|
|
210
|
+
foo: str
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
`;
|
|
214
|
+
expect(result).toRenderTo(expected);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
describe("Python Class - VariableDeclaration", () => {
|
|
218
|
+
it("renders a class with class fields", () => {
|
|
219
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
220
|
+
get children() {
|
|
221
|
+
return [_$createComponent(py.ClassDeclaration, {
|
|
222
|
+
name: "Base"
|
|
223
|
+
}), _$createComponent(py.ClassDeclaration, {
|
|
224
|
+
name: "A",
|
|
225
|
+
get children() {
|
|
226
|
+
return _$createComponent(py.StatementList, {
|
|
227
|
+
get children() {
|
|
228
|
+
return [_$createComponent(py.VariableDeclaration, {
|
|
229
|
+
name: "just_name"
|
|
230
|
+
}), _$createComponent(py.VariableDeclaration, {
|
|
231
|
+
name: "name_and_type",
|
|
232
|
+
type: "number"
|
|
233
|
+
}), _$createComponent(py.VariableDeclaration, {
|
|
234
|
+
name: "name_type_and_value",
|
|
235
|
+
type: "number",
|
|
236
|
+
initializer: 12
|
|
237
|
+
}), _$createComponent(py.VariableDeclaration, {
|
|
238
|
+
name: "class_based",
|
|
239
|
+
get type() {
|
|
240
|
+
return refkey("Base");
|
|
241
|
+
}
|
|
242
|
+
})];
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
})];
|
|
247
|
+
}
|
|
248
|
+
})]);
|
|
249
|
+
const expected = d`
|
|
250
|
+
class Base:
|
|
251
|
+
pass
|
|
252
|
+
|
|
253
|
+
class A:
|
|
254
|
+
just_name = None
|
|
255
|
+
name_and_type: number = None
|
|
256
|
+
name_type_and_value: number = 12
|
|
257
|
+
class_based: Base = None
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
`;
|
|
261
|
+
expect(result).toRenderTo(expected);
|
|
262
|
+
});
|
|
263
|
+
it("correctly access members of its type", () => {
|
|
264
|
+
const classRk = refkey();
|
|
265
|
+
const classMemberRk = refkey();
|
|
266
|
+
const classMethodRk = refkey();
|
|
267
|
+
const v1Rk = refkey();
|
|
268
|
+
const res = toSourceTextMultiple([_$createComponent(py.SourceFile, {
|
|
269
|
+
path: "inst.py",
|
|
270
|
+
get children() {
|
|
271
|
+
return _$createComponent(py.StatementList, {
|
|
272
|
+
get children() {
|
|
273
|
+
return [_$createComponent(py.VariableDeclaration, {
|
|
274
|
+
name: "one",
|
|
275
|
+
refkey: v1Rk,
|
|
276
|
+
type: classRk,
|
|
277
|
+
get initializer() {
|
|
278
|
+
return _$createComponent(py.MemberExpression, {
|
|
279
|
+
get children() {
|
|
280
|
+
return [_$createComponent(py.MemberExpression.Part, {
|
|
281
|
+
refkey: classRk
|
|
282
|
+
}), _$createComponent(py.MemberExpression.Part, {
|
|
283
|
+
args: true
|
|
284
|
+
})];
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
}), [_$memo(() => memberRefkey(v1Rk, classMemberRk))], [_$memo(() => memberRefkey(v1Rk, classMethodRk)), "()"]];
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}), _$createComponent(py.SourceFile, {
|
|
293
|
+
path: "decl.py",
|
|
294
|
+
get children() {
|
|
295
|
+
return _$createComponent(py.ClassDeclaration, {
|
|
296
|
+
name: "Bar",
|
|
297
|
+
refkey: classRk,
|
|
298
|
+
get children() {
|
|
299
|
+
return _$createComponent(py.StatementList, {
|
|
300
|
+
get children() {
|
|
301
|
+
return [_$createComponent(py.VariableDeclaration, {
|
|
302
|
+
name: "instanceProp",
|
|
303
|
+
refkey: classMemberRk,
|
|
304
|
+
initializer: 42,
|
|
305
|
+
instanceVariable: true
|
|
306
|
+
}), _$createComponent(py.FunctionDeclaration, {
|
|
307
|
+
name: "instanceMethod",
|
|
308
|
+
instanceFunction: true,
|
|
309
|
+
refkey: classMethodRk,
|
|
310
|
+
returnType: "int"
|
|
311
|
+
})];
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
})]);
|
|
318
|
+
assertFileContents(res, {
|
|
319
|
+
"inst.py": `
|
|
320
|
+
from decl import Bar
|
|
321
|
+
|
|
322
|
+
one: Bar = Bar()
|
|
323
|
+
one.instance_prop
|
|
324
|
+
one.instance_method()
|
|
325
|
+
`,
|
|
326
|
+
"decl.py": `
|
|
327
|
+
class Bar:
|
|
328
|
+
instance_prop = 42
|
|
329
|
+
def instance_method(self) -> int:
|
|
330
|
+
pass
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
`
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
describe("Python Class - FunctionDeclaration", () => {
|
|
338
|
+
it("renders a class with class fields and method", () => {
|
|
339
|
+
const result = toSourceText([[_$createComponent(py.ClassDeclaration, {
|
|
340
|
+
name: "MyClass",
|
|
341
|
+
bases: ["BaseClass"],
|
|
342
|
+
get children() {
|
|
343
|
+
return _$createComponent(py.StatementList, {
|
|
344
|
+
get children() {
|
|
345
|
+
return [_$createComponent(py.VariableDeclaration, {
|
|
346
|
+
name: "a",
|
|
347
|
+
type: "int"
|
|
348
|
+
}), _$createComponent(py.VariableDeclaration, {
|
|
349
|
+
name: "b",
|
|
350
|
+
type: "int"
|
|
351
|
+
}), _$createComponent(py.FunctionDeclaration, {
|
|
352
|
+
name: "my_method",
|
|
353
|
+
parameters: [{
|
|
354
|
+
name: "a",
|
|
355
|
+
type: "int"
|
|
356
|
+
}, {
|
|
357
|
+
name: "b",
|
|
358
|
+
type: "int"
|
|
359
|
+
}],
|
|
360
|
+
returnType: "int",
|
|
361
|
+
instanceFunction: true,
|
|
362
|
+
children: "return a + b"
|
|
363
|
+
}), _$createComponent(py.FunctionDeclaration, {
|
|
364
|
+
name: "my_class_method",
|
|
365
|
+
instanceFunction: false,
|
|
366
|
+
classFunction: true,
|
|
367
|
+
returnType: "int",
|
|
368
|
+
children: "pass"
|
|
369
|
+
}), _$createComponent(py.FunctionDeclaration, {
|
|
370
|
+
name: "my_standalone_function",
|
|
371
|
+
instanceFunction: false,
|
|
372
|
+
returnType: "int",
|
|
373
|
+
children: "pass"
|
|
374
|
+
})];
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
})]]);
|
|
379
|
+
const expected = d`
|
|
380
|
+
class MyClass(BaseClass):
|
|
381
|
+
a: int = None
|
|
382
|
+
b: int = None
|
|
383
|
+
def my_method(self, a: int, b: int) -> int:
|
|
384
|
+
return a + b
|
|
385
|
+
|
|
386
|
+
def my_class_method(cls) -> int:
|
|
387
|
+
pass
|
|
388
|
+
|
|
389
|
+
def my_standalone_function() -> int:
|
|
390
|
+
pass
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
`;
|
|
395
|
+
expect(result).toRenderTo(expected);
|
|
396
|
+
});
|
|
397
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classinstantiations.test.d.ts","sourceRoot":"","sources":["../../test/classinstantiations.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,168 @@
|
|
|
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 { expect, it } from "vitest";
|
|
5
|
+
import { createModule } from "../src/create-module.js";
|
|
6
|
+
import * as py from "../src/index.js";
|
|
7
|
+
import { toSourceText } from "./utils.js";
|
|
8
|
+
it("declaration of class instance with variables", () => {
|
|
9
|
+
// Creating the reference separately so the naming policy doesn't interfere
|
|
10
|
+
const classRef = refkey();
|
|
11
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
12
|
+
get children() {
|
|
13
|
+
return [_$createComponent(py.ClassDeclaration, {
|
|
14
|
+
name: "one-class",
|
|
15
|
+
refkey: classRef
|
|
16
|
+
}), _$createComponent(py.ClassInstantiation, {
|
|
17
|
+
target: classRef,
|
|
18
|
+
get args() {
|
|
19
|
+
return [_$createComponent(py.Atom, {
|
|
20
|
+
jsValue: "A name"
|
|
21
|
+
}), _$createComponent(py.Atom, {
|
|
22
|
+
jsValue: 42
|
|
23
|
+
}), _$createComponent(py.Atom, {
|
|
24
|
+
jsValue: true
|
|
25
|
+
})];
|
|
26
|
+
}
|
|
27
|
+
})];
|
|
28
|
+
}
|
|
29
|
+
})]);
|
|
30
|
+
const expected = d`
|
|
31
|
+
class OneClass:
|
|
32
|
+
pass
|
|
33
|
+
|
|
34
|
+
OneClass("A name", 42, True)
|
|
35
|
+
`;
|
|
36
|
+
expect(result).toRenderTo(expected);
|
|
37
|
+
});
|
|
38
|
+
it("correct resolving of external module", () => {
|
|
39
|
+
const requestsLib = createModule({
|
|
40
|
+
name: "requests",
|
|
41
|
+
descriptor: {
|
|
42
|
+
models: ["Request"]
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
46
|
+
get children() {
|
|
47
|
+
return _$createComponent(py.ClassInstantiation, {
|
|
48
|
+
get target() {
|
|
49
|
+
return requestsLib["models"].Request;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
})], {
|
|
54
|
+
externals: [requestsLib]
|
|
55
|
+
});
|
|
56
|
+
const expected = d`
|
|
57
|
+
from requests.models import Request
|
|
58
|
+
|
|
59
|
+
Request()
|
|
60
|
+
`;
|
|
61
|
+
expect(result).toRenderTo(expected);
|
|
62
|
+
});
|
|
63
|
+
it("Class instantiation without a reference", () => {
|
|
64
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
65
|
+
get children() {
|
|
66
|
+
return _$createComponent(py.ClassInstantiation, {
|
|
67
|
+
target: "ExampleClass",
|
|
68
|
+
get args() {
|
|
69
|
+
return [_$createComponent(py.Atom, {
|
|
70
|
+
jsValue: "A name"
|
|
71
|
+
}), _$createComponent(py.Atom, {
|
|
72
|
+
jsValue: 42
|
|
73
|
+
}), _$createComponent(py.Atom, {
|
|
74
|
+
jsValue: true
|
|
75
|
+
})];
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
})]);
|
|
80
|
+
const expected = d`
|
|
81
|
+
ExampleClass("A name", 42, True)
|
|
82
|
+
`;
|
|
83
|
+
expect(result).toRenderTo(expected);
|
|
84
|
+
});
|
|
85
|
+
it("Class instantiation without a reference and with call statement vars", () => {
|
|
86
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
87
|
+
get children() {
|
|
88
|
+
return _$createComponent(py.ClassInstantiation, {
|
|
89
|
+
target: "ExampleClass",
|
|
90
|
+
get args() {
|
|
91
|
+
return [_$createComponent(py.VariableDeclaration, {
|
|
92
|
+
name: "name",
|
|
93
|
+
initializer: "A name",
|
|
94
|
+
callStatementVar: true
|
|
95
|
+
}), _$createComponent(py.VariableDeclaration, {
|
|
96
|
+
name: "number",
|
|
97
|
+
initializer: 42,
|
|
98
|
+
callStatementVar: true
|
|
99
|
+
}), _$createComponent(py.VariableDeclaration, {
|
|
100
|
+
name: "flag",
|
|
101
|
+
initializer: true,
|
|
102
|
+
callStatementVar: true
|
|
103
|
+
})];
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
})]);
|
|
108
|
+
const expected = d`
|
|
109
|
+
ExampleClass(name="A name", number=42, flag=True)
|
|
110
|
+
`;
|
|
111
|
+
expect(result).toRenderTo(expected);
|
|
112
|
+
});
|
|
113
|
+
it("Class instantiation without a reference mixing unnamed and named vars", () => {
|
|
114
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
115
|
+
get children() {
|
|
116
|
+
return _$createComponent(py.ClassInstantiation, {
|
|
117
|
+
target: "ExampleClass",
|
|
118
|
+
get args() {
|
|
119
|
+
return [_$createComponent(py.Atom, {
|
|
120
|
+
jsValue: "A name"
|
|
121
|
+
}), _$createComponent(py.VariableDeclaration, {
|
|
122
|
+
name: "number",
|
|
123
|
+
initializer: 42,
|
|
124
|
+
callStatementVar: true
|
|
125
|
+
}), _$createComponent(py.VariableDeclaration, {
|
|
126
|
+
name: "flag",
|
|
127
|
+
initializer: true,
|
|
128
|
+
callStatementVar: true
|
|
129
|
+
})];
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
})]);
|
|
134
|
+
const expected = d`
|
|
135
|
+
ExampleClass("A name", number=42, flag=True)
|
|
136
|
+
`;
|
|
137
|
+
expect(result).toRenderTo(expected);
|
|
138
|
+
});
|
|
139
|
+
it("incorrect Class instantiation works", () => {
|
|
140
|
+
const result = toSourceText([_$createComponent(py.ClassInstantiation, {
|
|
141
|
+
target: "MyClass",
|
|
142
|
+
get args() {
|
|
143
|
+
return [_$createComponent(py.ClassDeclaration, {
|
|
144
|
+
name: "NestedClass"
|
|
145
|
+
}), _$createComponent(py.FunctionDeclaration, {
|
|
146
|
+
name: "myFunc"
|
|
147
|
+
}), _$createComponent(py.StatementList, {
|
|
148
|
+
get children() {
|
|
149
|
+
return _$createComponent(py.VariableDeclaration, {
|
|
150
|
+
name: "x"
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
})];
|
|
154
|
+
}
|
|
155
|
+
})]);
|
|
156
|
+
const expected = d`
|
|
157
|
+
MyClass(
|
|
158
|
+
class NestedClass:
|
|
159
|
+
pass
|
|
160
|
+
,
|
|
161
|
+
def my_func():
|
|
162
|
+
pass
|
|
163
|
+
,
|
|
164
|
+
x = None
|
|
165
|
+
)
|
|
166
|
+
`;
|
|
167
|
+
expect(result).toRenderTo(expected);
|
|
168
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enums.test.d.ts","sourceRoot":"","sources":["../../test/enums.test.tsx"],"names":[],"mappings":""}
|