@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,211 @@
|
|
|
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 { enumModule } from "../src/builtins/python.js";
|
|
6
|
+
import * as py from "../src/index.js";
|
|
7
|
+
import { toSourceText } from "./utils.js";
|
|
8
|
+
describe("Python Enum", () => {
|
|
9
|
+
it("classic enum with explicit values", () => {
|
|
10
|
+
const result = toSourceText([_$createComponent(py.EnumDeclaration, {
|
|
11
|
+
name: "Color",
|
|
12
|
+
baseType: "IntEnum",
|
|
13
|
+
members: [{
|
|
14
|
+
name: "RED",
|
|
15
|
+
value: "1"
|
|
16
|
+
}, {
|
|
17
|
+
name: "GREEN",
|
|
18
|
+
value: "2"
|
|
19
|
+
}, {
|
|
20
|
+
name: "BLUE",
|
|
21
|
+
value: "3"
|
|
22
|
+
}]
|
|
23
|
+
})], {
|
|
24
|
+
externals: [enumModule]
|
|
25
|
+
});
|
|
26
|
+
const expected = d`
|
|
27
|
+
from enum import IntEnum
|
|
28
|
+
|
|
29
|
+
class Color(IntEnum):
|
|
30
|
+
RED = 1
|
|
31
|
+
GREEN = 2
|
|
32
|
+
BLUE = 3
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
`;
|
|
36
|
+
expect(result).toRenderTo(expected);
|
|
37
|
+
});
|
|
38
|
+
it("classic enum with jsValues", () => {
|
|
39
|
+
const result = toSourceText([_$createComponent(py.EnumDeclaration, {
|
|
40
|
+
name: "Color",
|
|
41
|
+
baseType: "IntEnum",
|
|
42
|
+
members: [{
|
|
43
|
+
name: "RED",
|
|
44
|
+
jsValue: "1"
|
|
45
|
+
}, {
|
|
46
|
+
name: "GREEN",
|
|
47
|
+
jsValue: 2
|
|
48
|
+
}, {
|
|
49
|
+
name: "BLUE",
|
|
50
|
+
jsValue: "3"
|
|
51
|
+
}]
|
|
52
|
+
})], {
|
|
53
|
+
externals: [enumModule]
|
|
54
|
+
});
|
|
55
|
+
const expected = d`
|
|
56
|
+
from enum import IntEnum
|
|
57
|
+
|
|
58
|
+
class Color(IntEnum):
|
|
59
|
+
RED = "1"
|
|
60
|
+
GREEN = 2
|
|
61
|
+
BLUE = "3"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
`;
|
|
65
|
+
expect(result).toRenderTo(expected);
|
|
66
|
+
});
|
|
67
|
+
it("classic enum with a refkey as jsValue", () => {
|
|
68
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
69
|
+
get children() {
|
|
70
|
+
return [_$createComponent(py.ClassDeclaration, {
|
|
71
|
+
name: "Dog"
|
|
72
|
+
}), _$createComponent(py.ClassDeclaration, {
|
|
73
|
+
name: "Cat"
|
|
74
|
+
}), _$createComponent(py.EnumDeclaration, {
|
|
75
|
+
name: "Animal",
|
|
76
|
+
baseType: "Enum",
|
|
77
|
+
get members() {
|
|
78
|
+
return [{
|
|
79
|
+
name: "DOG",
|
|
80
|
+
value: refkey("Dog")
|
|
81
|
+
}, {
|
|
82
|
+
name: "CAT",
|
|
83
|
+
value: refkey("Cat")
|
|
84
|
+
}];
|
|
85
|
+
}
|
|
86
|
+
})];
|
|
87
|
+
}
|
|
88
|
+
})], {
|
|
89
|
+
externals: [enumModule]
|
|
90
|
+
});
|
|
91
|
+
const expected = d`
|
|
92
|
+
from enum import Enum
|
|
93
|
+
|
|
94
|
+
class Dog:
|
|
95
|
+
pass
|
|
96
|
+
|
|
97
|
+
class Cat:
|
|
98
|
+
pass
|
|
99
|
+
|
|
100
|
+
class Animal(Enum):
|
|
101
|
+
DOG = Dog
|
|
102
|
+
CAT = Cat
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
`;
|
|
106
|
+
expect(result).toRenderTo(expected);
|
|
107
|
+
});
|
|
108
|
+
it("enum with auto() values", () => {
|
|
109
|
+
const result = toSourceText([_$createComponent(py.EnumDeclaration, {
|
|
110
|
+
name: "Animal",
|
|
111
|
+
style: "auto",
|
|
112
|
+
members: [{
|
|
113
|
+
name: "DOG"
|
|
114
|
+
}, {
|
|
115
|
+
name: "CAT"
|
|
116
|
+
}, {
|
|
117
|
+
name: "RABBIT"
|
|
118
|
+
}]
|
|
119
|
+
})], {
|
|
120
|
+
externals: [enumModule]
|
|
121
|
+
});
|
|
122
|
+
const expected = d`
|
|
123
|
+
from enum import auto
|
|
124
|
+
from enum import Enum
|
|
125
|
+
|
|
126
|
+
class Animal(Enum):
|
|
127
|
+
DOG = auto()
|
|
128
|
+
CAT = auto()
|
|
129
|
+
RABBIT = auto()
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
`;
|
|
133
|
+
expect(result).toRenderTo(expected);
|
|
134
|
+
});
|
|
135
|
+
it("enum with mixed manual and auto() values", () => {
|
|
136
|
+
const result = toSourceText([_$createComponent(py.EnumDeclaration, {
|
|
137
|
+
name: "Permission",
|
|
138
|
+
baseType: "Flag",
|
|
139
|
+
style: "auto",
|
|
140
|
+
members: [{
|
|
141
|
+
name: "READ",
|
|
142
|
+
value: 1
|
|
143
|
+
}, {
|
|
144
|
+
name: "WRITE"
|
|
145
|
+
}, {
|
|
146
|
+
name: "EXECUTE"
|
|
147
|
+
}]
|
|
148
|
+
})], {
|
|
149
|
+
externals: [enumModule]
|
|
150
|
+
});
|
|
151
|
+
const expected = d`
|
|
152
|
+
from enum import auto
|
|
153
|
+
from enum import Flag
|
|
154
|
+
|
|
155
|
+
class Permission(Flag):
|
|
156
|
+
READ = 1
|
|
157
|
+
WRITE = auto()
|
|
158
|
+
EXECUTE = auto()
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
`;
|
|
162
|
+
expect(result).toRenderTo(expected);
|
|
163
|
+
});
|
|
164
|
+
it("functional enum with list", () => {
|
|
165
|
+
const result = toSourceText([_$createComponent(py.EnumDeclaration, {
|
|
166
|
+
name: "Direction",
|
|
167
|
+
style: "functional",
|
|
168
|
+
members: [{
|
|
169
|
+
name: "NORTH"
|
|
170
|
+
}, {
|
|
171
|
+
name: "SOUTH"
|
|
172
|
+
}, {
|
|
173
|
+
name: "EAST"
|
|
174
|
+
}, {
|
|
175
|
+
name: "WEST"
|
|
176
|
+
}]
|
|
177
|
+
})], {
|
|
178
|
+
externals: [enumModule]
|
|
179
|
+
});
|
|
180
|
+
const expected = d`
|
|
181
|
+
from enum import Enum
|
|
182
|
+
|
|
183
|
+
Direction = Enum('Direction', ['NORTH', 'SOUTH', 'EAST', 'WEST'])
|
|
184
|
+
`;
|
|
185
|
+
expect(result).toRenderTo(expected);
|
|
186
|
+
});
|
|
187
|
+
it("functional enum with mapping", () => {
|
|
188
|
+
const result = toSourceText([_$createComponent(py.EnumDeclaration, {
|
|
189
|
+
name: "Priority",
|
|
190
|
+
style: "functional",
|
|
191
|
+
members: [{
|
|
192
|
+
name: "HIGH",
|
|
193
|
+
value: 1
|
|
194
|
+
}, {
|
|
195
|
+
name: "MEDIUM",
|
|
196
|
+
value: 2
|
|
197
|
+
}, {
|
|
198
|
+
name: "LOW",
|
|
199
|
+
value: 3
|
|
200
|
+
}]
|
|
201
|
+
})], {
|
|
202
|
+
externals: [enumModule]
|
|
203
|
+
});
|
|
204
|
+
const expected = d`
|
|
205
|
+
from enum import Enum
|
|
206
|
+
|
|
207
|
+
Priority = Enum('Priority', {'HIGH' : 1, 'MEDIUM' : 2, 'LOW' : 3})
|
|
208
|
+
`;
|
|
209
|
+
expect(result).toRenderTo(expected);
|
|
210
|
+
});
|
|
211
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"externals.test.d.ts","sourceRoot":"","sources":["../../test/externals.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { memo as _$memo, createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { code, refkey } from "@alloy-js/core";
|
|
3
|
+
import { d } from "@alloy-js/core/testing";
|
|
4
|
+
import { expect, it } from "vitest";
|
|
5
|
+
import * as py from "../src/index.js";
|
|
6
|
+
import { createModule } from "../src/index.js";
|
|
7
|
+
import { toSourceText } from "./utils.js";
|
|
8
|
+
it("uses import from external library", () => {
|
|
9
|
+
const requestsLib = createModule({
|
|
10
|
+
name: "requests",
|
|
11
|
+
descriptor: {
|
|
12
|
+
".": ["get", "post"],
|
|
13
|
+
models: ["Response", "Request"],
|
|
14
|
+
"models.anothermodule": ["something"]
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
18
|
+
get children() {
|
|
19
|
+
return [_$memo(() => requestsLib["."].get), _$memo(() => requestsLib["."].post), _$memo(() => requestsLib["models"].Request), _$memo(() => requestsLib["models"].Response), _$memo(() => requestsLib["models.anothermodule"].something)];
|
|
20
|
+
}
|
|
21
|
+
})], {
|
|
22
|
+
externals: [requestsLib]
|
|
23
|
+
});
|
|
24
|
+
const expected = d`
|
|
25
|
+
from requests import get
|
|
26
|
+
from requests import post
|
|
27
|
+
from requests.models import Request
|
|
28
|
+
from requests.models import Response
|
|
29
|
+
from requests.models.anothermodule import something
|
|
30
|
+
|
|
31
|
+
get
|
|
32
|
+
post
|
|
33
|
+
Request
|
|
34
|
+
Response
|
|
35
|
+
something
|
|
36
|
+
`;
|
|
37
|
+
expect(result).toRenderTo(expected);
|
|
38
|
+
});
|
|
39
|
+
it("uses import from external library in multiple functions", () => {
|
|
40
|
+
const functionDeclarations = [_$createComponent(py.FunctionDeclaration, {
|
|
41
|
+
name: "getUser",
|
|
42
|
+
parameters: [{
|
|
43
|
+
name: "userId",
|
|
44
|
+
type: "int"
|
|
45
|
+
}],
|
|
46
|
+
get returnType() {
|
|
47
|
+
return py.requestsModule["models"]["Response"];
|
|
48
|
+
},
|
|
49
|
+
instanceFunction: true,
|
|
50
|
+
get children() {
|
|
51
|
+
return _$createComponent(py.StatementList, {
|
|
52
|
+
get children() {
|
|
53
|
+
return [_$createComponent(py.VariableDeclaration, {
|
|
54
|
+
name: "response",
|
|
55
|
+
get initializer() {
|
|
56
|
+
return _$createComponent(py.FunctionCallExpression, {
|
|
57
|
+
get target() {
|
|
58
|
+
return py.requestsModule["."]["get"];
|
|
59
|
+
},
|
|
60
|
+
args: [1]
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}), code`
|
|
64
|
+
return response.json()
|
|
65
|
+
`];
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}), _$createComponent(py.FunctionDeclaration, {
|
|
70
|
+
name: "createUser",
|
|
71
|
+
parameters: [{
|
|
72
|
+
name: "userName",
|
|
73
|
+
type: "string"
|
|
74
|
+
}],
|
|
75
|
+
get returnType() {
|
|
76
|
+
return py.requestsModule["models"]["Response"];
|
|
77
|
+
},
|
|
78
|
+
instanceFunction: true,
|
|
79
|
+
get children() {
|
|
80
|
+
return _$createComponent(py.StatementList, {
|
|
81
|
+
get children() {
|
|
82
|
+
return [_$createComponent(py.VariableDeclaration, {
|
|
83
|
+
name: "response",
|
|
84
|
+
get initializer() {
|
|
85
|
+
return _$createComponent(py.FunctionCallExpression, {
|
|
86
|
+
get target() {
|
|
87
|
+
return py.requestsModule["."]["post"];
|
|
88
|
+
},
|
|
89
|
+
args: [1]
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}), code`
|
|
93
|
+
return response.json()
|
|
94
|
+
`];
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
})];
|
|
99
|
+
const result = toSourceText(functionDeclarations, {
|
|
100
|
+
externals: [py.requestsModule]
|
|
101
|
+
});
|
|
102
|
+
const expected = d`
|
|
103
|
+
from requests import get
|
|
104
|
+
from requests import post
|
|
105
|
+
from requests.models import Response
|
|
106
|
+
|
|
107
|
+
def get_user(self, user_id: int) -> Response:
|
|
108
|
+
response = get(1)
|
|
109
|
+
return response.json()
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def create_user(self, user_name: string) -> Response:
|
|
113
|
+
response = post(1)
|
|
114
|
+
return response.json()
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
`;
|
|
118
|
+
expect(result).toRenderTo(expected);
|
|
119
|
+
});
|
|
120
|
+
it("uses import from external library in multiple class methods", () => {
|
|
121
|
+
const functionDeclarations = [_$createComponent(py.ClassDeclaration, {
|
|
122
|
+
name: "UserClient",
|
|
123
|
+
get children() {
|
|
124
|
+
return _$createComponent(py.StatementList, {
|
|
125
|
+
get children() {
|
|
126
|
+
return [_$createComponent(py.VariableDeclaration, {
|
|
127
|
+
name: "some_var",
|
|
128
|
+
initializer: 12,
|
|
129
|
+
instanceVariable: true
|
|
130
|
+
}), _$createComponent(py.FunctionDeclaration, {
|
|
131
|
+
name: "getUser",
|
|
132
|
+
parameters: [{
|
|
133
|
+
name: "userId",
|
|
134
|
+
type: "int"
|
|
135
|
+
}],
|
|
136
|
+
get returnType() {
|
|
137
|
+
return py.requestsModule["models"]["Response"];
|
|
138
|
+
},
|
|
139
|
+
instanceFunction: true,
|
|
140
|
+
get children() {
|
|
141
|
+
return _$createComponent(py.StatementList, {
|
|
142
|
+
get children() {
|
|
143
|
+
return [_$createComponent(py.VariableDeclaration, {
|
|
144
|
+
name: "response",
|
|
145
|
+
get initializer() {
|
|
146
|
+
return _$createComponent(py.FunctionCallExpression, {
|
|
147
|
+
get target() {
|
|
148
|
+
return py.requestsModule["."]["get"];
|
|
149
|
+
},
|
|
150
|
+
get args() {
|
|
151
|
+
return [refkey("some_var")];
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}), code`
|
|
156
|
+
return response.json()
|
|
157
|
+
`];
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}), _$createComponent(py.FunctionDeclaration, {
|
|
162
|
+
name: "createUser",
|
|
163
|
+
parameters: [{
|
|
164
|
+
name: "userName",
|
|
165
|
+
type: "string"
|
|
166
|
+
}],
|
|
167
|
+
get returnType() {
|
|
168
|
+
return py.requestsModule["models"]["Response"];
|
|
169
|
+
},
|
|
170
|
+
instanceFunction: true,
|
|
171
|
+
get children() {
|
|
172
|
+
return _$createComponent(py.StatementList, {
|
|
173
|
+
get children() {
|
|
174
|
+
return [_$createComponent(py.VariableDeclaration, {
|
|
175
|
+
name: "response",
|
|
176
|
+
get initializer() {
|
|
177
|
+
return _$createComponent(py.FunctionCallExpression, {
|
|
178
|
+
get target() {
|
|
179
|
+
return py.requestsModule["."]["post"];
|
|
180
|
+
},
|
|
181
|
+
get args() {
|
|
182
|
+
return [refkey("some_var")];
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}), code`
|
|
187
|
+
return response.json()
|
|
188
|
+
`];
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
})];
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
})];
|
|
197
|
+
const result = toSourceText(functionDeclarations, {
|
|
198
|
+
externals: [py.requestsModule]
|
|
199
|
+
});
|
|
200
|
+
const expected = d`
|
|
201
|
+
from requests import get
|
|
202
|
+
from requests import post
|
|
203
|
+
from requests.models import Response
|
|
204
|
+
|
|
205
|
+
class UserClient:
|
|
206
|
+
some_var = 12
|
|
207
|
+
def get_user(self, user_id: int) -> Response:
|
|
208
|
+
response = get(self.some_var)
|
|
209
|
+
return response.json()
|
|
210
|
+
|
|
211
|
+
def create_user(self, user_name: string) -> Response:
|
|
212
|
+
response = post(self.some_var)
|
|
213
|
+
return response.json()
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
`;
|
|
218
|
+
expect(result).toRenderTo(expected);
|
|
219
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"functioncallexpressions.test.d.ts","sourceRoot":"","sources":["../../test/functioncallexpressions.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,156 @@
|
|
|
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 { toSourceText } from "./utils.js";
|
|
7
|
+
describe("FunctionCallExpression", () => {
|
|
8
|
+
it("renders", () => {
|
|
9
|
+
const result = toSourceText([_$createComponent(py.FunctionCallExpression, {
|
|
10
|
+
target: "foo"
|
|
11
|
+
})]);
|
|
12
|
+
expect(result).toRenderTo(d`
|
|
13
|
+
foo()
|
|
14
|
+
`);
|
|
15
|
+
});
|
|
16
|
+
it("renders with args", () => {
|
|
17
|
+
const result = toSourceText([_$createComponent(py.FunctionCallExpression, {
|
|
18
|
+
target: "foo",
|
|
19
|
+
args: ["a", "b"]
|
|
20
|
+
})]);
|
|
21
|
+
expect(result).toRenderTo(d`
|
|
22
|
+
foo(a, b)
|
|
23
|
+
`);
|
|
24
|
+
});
|
|
25
|
+
it("function call with variables", () => {
|
|
26
|
+
// Creating the reference separately so the naming policy doesn't interfere
|
|
27
|
+
const methodRef = refkey();
|
|
28
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
29
|
+
get children() {
|
|
30
|
+
return [_$createComponent(py.FunctionDeclaration, {
|
|
31
|
+
name: "runFunc",
|
|
32
|
+
refkey: methodRef
|
|
33
|
+
}), _$createComponent(py.FunctionCallExpression, {
|
|
34
|
+
target: methodRef,
|
|
35
|
+
get args() {
|
|
36
|
+
return [_$createComponent(py.Atom, {
|
|
37
|
+
jsValue: "A name"
|
|
38
|
+
}), _$createComponent(py.Atom, {
|
|
39
|
+
jsValue: 42
|
|
40
|
+
}), _$createComponent(py.Atom, {
|
|
41
|
+
jsValue: true
|
|
42
|
+
})];
|
|
43
|
+
}
|
|
44
|
+
})];
|
|
45
|
+
}
|
|
46
|
+
})]);
|
|
47
|
+
const expected = d`
|
|
48
|
+
def run_func():
|
|
49
|
+
pass
|
|
50
|
+
|
|
51
|
+
run_func("A name", 42, True)
|
|
52
|
+
`;
|
|
53
|
+
expect(result).toRenderTo(expected);
|
|
54
|
+
});
|
|
55
|
+
it("function call with variables and assignment", () => {
|
|
56
|
+
// Creating the reference separately so the naming policy doesn't interfere
|
|
57
|
+
const methodRef = refkey();
|
|
58
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
59
|
+
get children() {
|
|
60
|
+
return [_$createComponent(py.FunctionDeclaration, {
|
|
61
|
+
name: "runFunc",
|
|
62
|
+
returnType: "str",
|
|
63
|
+
refkey: methodRef,
|
|
64
|
+
parameters: [{
|
|
65
|
+
name: "name",
|
|
66
|
+
type: "str"
|
|
67
|
+
}, {
|
|
68
|
+
name: "number",
|
|
69
|
+
type: "int"
|
|
70
|
+
}, {
|
|
71
|
+
name: "flag",
|
|
72
|
+
type: "bool"
|
|
73
|
+
}]
|
|
74
|
+
}), _$createComponent(py.VariableDeclaration, {
|
|
75
|
+
name: "result",
|
|
76
|
+
type: "str",
|
|
77
|
+
get initializer() {
|
|
78
|
+
return _$createComponent(py.FunctionCallExpression, {
|
|
79
|
+
target: methodRef,
|
|
80
|
+
get args() {
|
|
81
|
+
return [_$createComponent(py.Atom, {
|
|
82
|
+
jsValue: "A name"
|
|
83
|
+
}), _$createComponent(py.Atom, {
|
|
84
|
+
jsValue: 42
|
|
85
|
+
}), _$createComponent(py.Atom, {
|
|
86
|
+
jsValue: true
|
|
87
|
+
})];
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
})];
|
|
92
|
+
}
|
|
93
|
+
})]);
|
|
94
|
+
const expected = d`
|
|
95
|
+
def run_func(name: str, number: int, flag: bool) -> str:
|
|
96
|
+
pass
|
|
97
|
+
|
|
98
|
+
result: str = run_func("A name", 42, True)
|
|
99
|
+
`;
|
|
100
|
+
expect(result).toRenderTo(expected);
|
|
101
|
+
});
|
|
102
|
+
it("Method call without a reference and with call statement vars", () => {
|
|
103
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
104
|
+
get children() {
|
|
105
|
+
return _$createComponent(py.FunctionCallExpression, {
|
|
106
|
+
target: "example_method",
|
|
107
|
+
get args() {
|
|
108
|
+
return [_$createComponent(py.VariableDeclaration, {
|
|
109
|
+
name: "name",
|
|
110
|
+
initializer: "A name",
|
|
111
|
+
callStatementVar: true
|
|
112
|
+
}), _$createComponent(py.VariableDeclaration, {
|
|
113
|
+
name: "number",
|
|
114
|
+
initializer: 42,
|
|
115
|
+
callStatementVar: true
|
|
116
|
+
}), _$createComponent(py.VariableDeclaration, {
|
|
117
|
+
name: "flag",
|
|
118
|
+
initializer: true,
|
|
119
|
+
callStatementVar: true
|
|
120
|
+
})];
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
})]);
|
|
125
|
+
const expected = d`
|
|
126
|
+
example_method(name="A name", number=42, flag=True)
|
|
127
|
+
`;
|
|
128
|
+
expect(result).toRenderTo(expected);
|
|
129
|
+
});
|
|
130
|
+
it("Method call without a reference mixing unnamed and named vars", () => {
|
|
131
|
+
const result = toSourceText([_$createComponent(py.StatementList, {
|
|
132
|
+
get children() {
|
|
133
|
+
return _$createComponent(py.FunctionCallExpression, {
|
|
134
|
+
target: "example_method",
|
|
135
|
+
get args() {
|
|
136
|
+
return [_$createComponent(py.Atom, {
|
|
137
|
+
jsValue: "A name"
|
|
138
|
+
}), _$createComponent(py.VariableDeclaration, {
|
|
139
|
+
name: "number",
|
|
140
|
+
initializer: 42,
|
|
141
|
+
callStatementVar: true
|
|
142
|
+
}), _$createComponent(py.VariableDeclaration, {
|
|
143
|
+
name: "flag",
|
|
144
|
+
initializer: true,
|
|
145
|
+
callStatementVar: true
|
|
146
|
+
})];
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
})]);
|
|
151
|
+
const expected = d`
|
|
152
|
+
example_method("A name", number=42, flag=True)
|
|
153
|
+
`;
|
|
154
|
+
expect(result).toRenderTo(expected);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"functiondeclaration.test.d.ts","sourceRoot":"","sources":["../../test/functiondeclaration.test.tsx"],"names":[],"mappings":""}
|