@alloy-js/python 0.1.0-dev.3 → 0.1.0-dev.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/components/CallSignature.d.ts.map +1 -1
- package/dist/src/components/CallSignature.js +8 -11
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
- package/dist/src/components/ClassDeclaration.js +6 -20
- package/dist/src/components/ClassInstantiation.d.ts.map +1 -1
- package/dist/src/components/ClassInstantiation.js +4 -4
- 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 +6 -15
- package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
- package/dist/src/components/EnumDeclaration.js +33 -59
- package/dist/src/components/EnumMember.d.ts.map +1 -1
- package/dist/src/components/EnumMember.js +3 -4
- package/dist/src/components/FunctionDeclaration.d.ts +3 -1
- package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
- package/dist/src/components/FunctionDeclaration.js +6 -16
- 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 +21 -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 +15 -0
- package/dist/src/components/PythonBlock.d.ts +5 -4
- package/dist/src/components/PythonBlock.d.ts.map +1 -1
- package/dist/src/components/PythonBlock.js +0 -1
- package/dist/src/components/SourceFile.js +1 -2
- package/dist/src/components/VariableDeclaration.d.ts.map +1 -1
- package/dist/src/components/VariableDeclaration.js +8 -31
- 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 +2 -0
- package/dist/src/create-module.d.ts.map +1 -1
- package/dist/src/create-module.js +3 -4
- 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 +7 -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 +33 -9
- 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 +1 -1
- 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 +14 -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 +4 -7
- 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 +6 -14
- 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 +23 -6
- 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 +36 -36
- 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 +8 -0
- package/dist/test/classdeclarations.test.js +87 -52
- package/dist/test/enums.test.js +8 -2
- package/dist/test/externals.test.js +6 -5
- package/dist/test/functiondeclaration.test.js +73 -36
- package/dist/test/imports.test.js +9 -36
- package/dist/test/utils.d.ts +2 -3
- package/dist/test/utils.d.ts.map +1 -1
- package/dist/test/utils.js +3 -2
- package/dist/test/variables.test.js +11 -11
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- 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 +12 -30
- package/src/components/LexicalScope.tsx +24 -0
- package/src/components/MemberScope.tsx +18 -0
- package/src/components/PythonBlock.tsx +6 -1
- 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 +1015 -561
- 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 +9 -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
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
createSymbolSlot,
|
|
5
5
|
For,
|
|
6
6
|
Show,
|
|
7
|
+
SymbolSlot,
|
|
7
8
|
useContext,
|
|
8
9
|
} from "@alloy-js/core";
|
|
9
10
|
import { ParameterDescriptor } from "../parameter-descriptor.js";
|
|
@@ -87,15 +88,12 @@ export function CallSignatureParameters(props: CallSignatureParametersProps) {
|
|
|
87
88
|
}
|
|
88
89
|
|
|
89
90
|
function parameter(param: DeclaredParameterDescriptor) {
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
SymbolSlot.instantiateInto(param.symbol);
|
|
93
|
-
|
|
91
|
+
const TypeSlot = param.TypeSlot!; // TypeSlot will always be present when param.type is true.
|
|
94
92
|
return (
|
|
95
93
|
<group>
|
|
96
94
|
{param.symbol.name}
|
|
97
95
|
<Show when={!!param.type}>
|
|
98
|
-
: <
|
|
96
|
+
: <TypeSlot>{param.type}</TypeSlot>
|
|
99
97
|
</Show>
|
|
100
98
|
<Show when={!!param.optional}>
|
|
101
99
|
<Show when={!param.type}>=</Show>
|
|
@@ -120,44 +118,38 @@ function parameter(param: DeclaredParameterDescriptor) {
|
|
|
120
118
|
interface DeclaredParameterDescriptor
|
|
121
119
|
extends Omit<ParameterDescriptor, "name"> {
|
|
122
120
|
symbol: PythonOutputSymbol;
|
|
121
|
+
TypeSlot?: SymbolSlot;
|
|
123
122
|
}
|
|
124
123
|
|
|
125
124
|
function normalizeAndDeclareParameters(
|
|
126
125
|
parameters: ParameterDescriptor[] | string[],
|
|
127
126
|
): DeclaredParameterDescriptor[] {
|
|
128
|
-
const sfContext = useContext(PythonSourceFileContext);
|
|
129
|
-
const module = sfContext?.module;
|
|
130
127
|
if (parameters.length === 0) {
|
|
131
128
|
return [];
|
|
132
129
|
}
|
|
133
130
|
if (typeof parameters[0] === "string") {
|
|
134
131
|
return (parameters as string[]).map((paramName) => {
|
|
135
|
-
const symbol = createPythonSymbol(
|
|
136
|
-
paramName,
|
|
137
|
-
{
|
|
138
|
-
module: module,
|
|
139
|
-
},
|
|
140
|
-
"parameter",
|
|
141
|
-
false,
|
|
142
|
-
);
|
|
132
|
+
const symbol = createPythonSymbol(paramName, {}, "parameter");
|
|
143
133
|
|
|
144
134
|
return { refkeys: symbol.refkeys, symbol };
|
|
145
135
|
});
|
|
146
136
|
} else {
|
|
147
137
|
return (parameters as ParameterDescriptor[]).map((param) => {
|
|
138
|
+
const TypeSlot = createSymbolSlot();
|
|
139
|
+
|
|
148
140
|
const symbol = createPythonSymbol(
|
|
149
141
|
param.name,
|
|
150
142
|
{
|
|
151
143
|
refkeys: param.refkey,
|
|
152
|
-
|
|
144
|
+
type: TypeSlot.firstSymbol,
|
|
153
145
|
},
|
|
154
146
|
"parameter",
|
|
155
|
-
false,
|
|
156
147
|
);
|
|
157
148
|
|
|
158
149
|
return {
|
|
159
150
|
...param,
|
|
160
151
|
symbol,
|
|
152
|
+
TypeSlot,
|
|
161
153
|
};
|
|
162
154
|
});
|
|
163
155
|
}
|
|
@@ -1,19 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Children,
|
|
3
|
-
List,
|
|
4
|
-
Name,
|
|
5
|
-
OutputSymbolFlags,
|
|
6
|
-
Scope,
|
|
7
|
-
Show,
|
|
8
|
-
childrenArray,
|
|
9
|
-
takeSymbols,
|
|
10
|
-
} from "@alloy-js/core";
|
|
1
|
+
import { Children, List, Name, Show, childrenArray } from "@alloy-js/core";
|
|
11
2
|
import { createPythonSymbol } from "../symbol-creation.js";
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
Declaration,
|
|
15
|
-
DeclarationProps,
|
|
16
|
-
} from "./Declaration.js";
|
|
3
|
+
import { BaseDeclarationProps, Declaration } from "./Declaration.js";
|
|
4
|
+
import { MemberScope } from "./MemberScope.jsx";
|
|
17
5
|
import { PythonBlock } from "./PythonBlock.jsx";
|
|
18
6
|
|
|
19
7
|
export interface ClassDeclarationProps extends BaseDeclarationProps {
|
|
@@ -61,38 +49,23 @@ export function ClassDeclaration(props: ClassDeclarationProps) {
|
|
|
61
49
|
props.name!,
|
|
62
50
|
{
|
|
63
51
|
refkeys: props.refkey,
|
|
64
|
-
flags:
|
|
65
|
-
(props.flags ?? OutputSymbolFlags.None) |
|
|
66
|
-
OutputSymbolFlags.MemberContainer,
|
|
67
52
|
},
|
|
68
53
|
"class",
|
|
69
|
-
true,
|
|
70
54
|
);
|
|
71
55
|
|
|
72
|
-
takeSymbols((memberSymbol) => {
|
|
73
|
-
// Transform emitted symbols into instance/class members
|
|
74
|
-
memberSymbol.flags |= OutputSymbolFlags.InstanceMember;
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
// Propagate the name after the name policy was applied
|
|
78
|
-
const updatedProps: DeclarationProps = {
|
|
79
|
-
...props,
|
|
80
|
-
name: sym.name,
|
|
81
|
-
nameKind: "class",
|
|
82
|
-
};
|
|
83
56
|
const hasChildren =
|
|
84
57
|
childrenArray(() => props.children).filter((c) => Boolean(c)).length > 0;
|
|
85
58
|
|
|
86
59
|
return (
|
|
87
60
|
<Declaration symbol={sym}>
|
|
88
61
|
class <Name />
|
|
89
|
-
<
|
|
62
|
+
<MemberScope ownerSymbol={sym}>
|
|
90
63
|
{basesPart}
|
|
91
64
|
<PythonBlock opener=":">
|
|
92
65
|
<Show when={Boolean(props.doc)}>{props.doc}</Show>
|
|
93
66
|
{hasChildren ? props.children : "pass"}
|
|
94
67
|
</PythonBlock>
|
|
95
|
-
</
|
|
68
|
+
</MemberScope>
|
|
96
69
|
</Declaration>
|
|
97
70
|
);
|
|
98
71
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
emitSymbol,
|
|
3
|
-
instantiateTakenMembersTo
|
|
4
|
-
OutputSymbolFlags,
|
|
3
|
+
instantiateTakenMembersTo,
|
|
5
4
|
useContext,
|
|
6
5
|
} from "@alloy-js/core";
|
|
7
6
|
import { createPythonSymbol } from "../symbol-creation.js";
|
|
@@ -35,16 +34,11 @@ export interface ClassInstantiationProps extends FunctionCallExpressionProps {}
|
|
|
35
34
|
export function ClassInstantiation(props: ClassInstantiationProps) {
|
|
36
35
|
const sfContext = useContext(PythonSourceFileContext);
|
|
37
36
|
const module = sfContext?.module;
|
|
38
|
-
const sym = createPythonSymbol(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
},
|
|
44
|
-
undefined,
|
|
45
|
-
false,
|
|
46
|
-
);
|
|
47
|
-
instantiateTakenSymbolsTo(sym);
|
|
37
|
+
const sym = createPythonSymbol("", {
|
|
38
|
+
transient: true,
|
|
39
|
+
module: module,
|
|
40
|
+
});
|
|
41
|
+
instantiateTakenMembersTo(sym, "static", "instance");
|
|
48
42
|
emitSymbol(sym);
|
|
49
43
|
return (
|
|
50
44
|
<>
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Children,
|
|
3
3
|
Declaration as CoreDeclaration,
|
|
4
|
-
MemberScope,
|
|
5
|
-
OutputSymbolFlags,
|
|
6
4
|
Refkey,
|
|
7
5
|
} from "@alloy-js/core";
|
|
8
6
|
import { PythonElements } from "../name-policy.js";
|
|
@@ -21,11 +19,6 @@ export interface BaseDeclarationProps {
|
|
|
21
19
|
*/
|
|
22
20
|
refkey?: Refkey | Refkey[];
|
|
23
21
|
|
|
24
|
-
/**
|
|
25
|
-
* Flags for the symbol created by this component.
|
|
26
|
-
*/
|
|
27
|
-
flags?: OutputSymbolFlags;
|
|
28
|
-
|
|
29
22
|
children?: Children;
|
|
30
23
|
|
|
31
24
|
/**
|
|
@@ -70,22 +63,10 @@ export function Declaration(props: DeclarationProps) {
|
|
|
70
63
|
props.name!,
|
|
71
64
|
{
|
|
72
65
|
refkeys: props.refkey,
|
|
73
|
-
flags: props.flags,
|
|
74
66
|
},
|
|
75
67
|
props.nameKind!,
|
|
76
|
-
true,
|
|
77
68
|
);
|
|
78
69
|
}
|
|
79
70
|
|
|
80
|
-
|
|
81
|
-
return <MemberScope owner={sym}>{children}</MemberScope>;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
let children: Children = () => props.children;
|
|
85
|
-
|
|
86
|
-
if (sym.flags & OutputSymbolFlags.MemberContainer) {
|
|
87
|
-
children = withMemberScope(children);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return <CoreDeclaration symbol={sym}>{children}</CoreDeclaration>;
|
|
71
|
+
return <CoreDeclaration symbol={sym}>{props.children}</CoreDeclaration>;
|
|
91
72
|
}
|
|
@@ -2,19 +2,15 @@ import {
|
|
|
2
2
|
Children,
|
|
3
3
|
Declaration as CoreDeclaration,
|
|
4
4
|
For,
|
|
5
|
-
MemberScope,
|
|
6
|
-
OutputSymbolFlags,
|
|
7
|
-
Scope,
|
|
8
5
|
Show,
|
|
9
|
-
useBinder,
|
|
10
6
|
} from "@alloy-js/core";
|
|
11
7
|
import { enumModule } from "../builtins/python.js";
|
|
12
8
|
import { createPythonSymbol } from "../symbol-creation.js";
|
|
13
|
-
import { usePythonScope } from "../symbols/scopes.js";
|
|
14
9
|
import { BaseDeclarationProps } from "./Declaration.js";
|
|
15
10
|
import { EnumMember } from "./EnumMember.js";
|
|
16
|
-
import { PythonBlock } from "./PythonBlock.jsx";
|
|
17
11
|
import { SimpleCommentBlock } from "./index.js";
|
|
12
|
+
import { MemberScope } from "./MemberScope.jsx";
|
|
13
|
+
import { PythonBlock } from "./PythonBlock.jsx";
|
|
18
14
|
|
|
19
15
|
export interface EnumProps extends BaseDeclarationProps {
|
|
20
16
|
/**
|
|
@@ -115,18 +111,12 @@ export function EnumDeclaration(props: EnumProps) {
|
|
|
115
111
|
* ```
|
|
116
112
|
*/
|
|
117
113
|
export function FunctionalEnumDeclaration(props: EnumProps) {
|
|
118
|
-
const binder = useBinder();
|
|
119
|
-
const scope = usePythonScope();
|
|
120
114
|
const sym = createPythonSymbol(
|
|
121
115
|
props.name,
|
|
122
116
|
{
|
|
123
|
-
binder: binder,
|
|
124
|
-
scope: scope,
|
|
125
117
|
refkeys: props.refkey,
|
|
126
|
-
flags: OutputSymbolFlags.StaticMemberContainer,
|
|
127
118
|
},
|
|
128
119
|
"enum",
|
|
129
|
-
false,
|
|
130
120
|
);
|
|
131
121
|
const members = props.members ?? [];
|
|
132
122
|
let opener, ender;
|
|
@@ -152,12 +142,7 @@ export function FunctionalEnumDeclaration(props: EnumProps) {
|
|
|
152
142
|
<>
|
|
153
143
|
<CoreDeclaration symbol={sym}>
|
|
154
144
|
{sym.name} = {enumModule["."].Enum}('{sym.name}',{" "}
|
|
155
|
-
<MemberScope
|
|
156
|
-
<Scope name={props.name} kind="enum">
|
|
157
|
-
{memberExpr}
|
|
158
|
-
</Scope>
|
|
159
|
-
</MemberScope>
|
|
160
|
-
)
|
|
145
|
+
<MemberScope ownerSymbol={sym}>{memberExpr}</MemberScope>)
|
|
161
146
|
</CoreDeclaration>
|
|
162
147
|
</>
|
|
163
148
|
);
|
|
@@ -234,18 +219,12 @@ export function FunctionalEnumDeclaration(props: EnumProps) {
|
|
|
234
219
|
*/
|
|
235
220
|
export function ClassEnumDeclaration(props: EnumProps) {
|
|
236
221
|
const baseType = props.baseType || "Enum";
|
|
237
|
-
const binder = useBinder();
|
|
238
|
-
const scope = usePythonScope();
|
|
239
222
|
const sym = createPythonSymbol(
|
|
240
223
|
props.name,
|
|
241
224
|
{
|
|
242
|
-
binder: binder,
|
|
243
|
-
scope: scope,
|
|
244
225
|
refkeys: props.refkey,
|
|
245
|
-
flags: OutputSymbolFlags.StaticMemberContainer,
|
|
246
226
|
},
|
|
247
227
|
"enum",
|
|
248
|
-
false,
|
|
249
228
|
);
|
|
250
229
|
let memberList: Array<{
|
|
251
230
|
name: string;
|
|
@@ -268,23 +247,21 @@ export function ClassEnumDeclaration(props: EnumProps) {
|
|
|
268
247
|
<hbr />
|
|
269
248
|
</Show>
|
|
270
249
|
class {sym.name}({enumModule["."][baseType]})
|
|
271
|
-
<MemberScope
|
|
272
|
-
<
|
|
273
|
-
<
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
</PythonBlock>
|
|
287
|
-
</Scope>
|
|
250
|
+
<MemberScope ownerSymbol={sym}>
|
|
251
|
+
<PythonBlock opener=":">
|
|
252
|
+
<For each={memberList} hardline>
|
|
253
|
+
{(member) => (
|
|
254
|
+
<EnumMember
|
|
255
|
+
name={member.name}
|
|
256
|
+
value={member.value}
|
|
257
|
+
jsValue={member.jsValue}
|
|
258
|
+
auto={member.auto}
|
|
259
|
+
doc={member.doc}
|
|
260
|
+
/>
|
|
261
|
+
)}
|
|
262
|
+
</For>
|
|
263
|
+
{props.children}
|
|
264
|
+
</PythonBlock>
|
|
288
265
|
</MemberScope>
|
|
289
266
|
</CoreDeclaration>
|
|
290
267
|
);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Children,
|
|
1
|
+
import { Children, Refkey, Show } from "@alloy-js/core";
|
|
2
2
|
import { enumModule } from "../builtins/python.js";
|
|
3
3
|
import { createPythonSymbol } from "../symbol-creation.js";
|
|
4
4
|
import { PythonOutputSymbol } from "../symbols/index.js";
|
|
@@ -62,10 +62,8 @@ export function EnumMember(props: EnumMemberProps) {
|
|
|
62
62
|
props.name,
|
|
63
63
|
{
|
|
64
64
|
refkeys: props.refkey,
|
|
65
|
-
flags: OutputSymbolFlags.StaticMember,
|
|
66
65
|
},
|
|
67
66
|
"enum-member",
|
|
68
|
-
true,
|
|
69
67
|
);
|
|
70
68
|
const valueCode =
|
|
71
69
|
props.jsValue !== undefined ? <Atom jsValue={props.jsValue} /> : value;
|
|
@@ -1,19 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
emitSymbol,
|
|
3
|
-
Name,
|
|
4
|
-
OutputScope,
|
|
5
|
-
OutputSymbolFlags,
|
|
6
|
-
Scope,
|
|
7
|
-
Show,
|
|
8
|
-
useMemberScope,
|
|
9
|
-
useScope,
|
|
10
|
-
} from "@alloy-js/core";
|
|
1
|
+
import { emitSymbol, Name, Show } from "@alloy-js/core";
|
|
11
2
|
import { createPythonSymbol } from "../symbol-creation.js";
|
|
12
3
|
import { getCallSignatureProps } from "../utils.js";
|
|
13
4
|
import { CallSignature, CallSignatureProps } from "./CallSignature.jsx";
|
|
14
5
|
import { BaseDeclarationProps, Declaration } from "./Declaration.js";
|
|
15
6
|
import { PythonBlock } from "./PythonBlock.jsx";
|
|
16
|
-
import { NoNamePolicy } from "./index.js";
|
|
7
|
+
import { LexicalScope, NoNamePolicy } from "./index.js";
|
|
17
8
|
|
|
18
9
|
export interface FunctionDeclarationProps
|
|
19
10
|
extends BaseDeclarationProps,
|
|
@@ -42,23 +33,13 @@ export interface FunctionDeclarationProps
|
|
|
42
33
|
export function FunctionDeclaration(props: FunctionDeclarationProps) {
|
|
43
34
|
const asyncKwd = props.async ? "async " : "";
|
|
44
35
|
const callSignatureProps = getCallSignatureProps(props, {});
|
|
45
|
-
const memberScope = useMemberScope();
|
|
46
|
-
let scope: OutputScope | undefined = undefined;
|
|
47
|
-
if (memberScope !== undefined) {
|
|
48
|
-
scope = memberScope.instanceMembers!;
|
|
49
|
-
} else {
|
|
50
|
-
scope = useScope();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
36
|
const sym = createPythonSymbol(
|
|
54
37
|
props.name,
|
|
55
38
|
{
|
|
56
|
-
|
|
39
|
+
instance: props.instanceFunction,
|
|
57
40
|
refkeys: props.refkey,
|
|
58
|
-
flags: props.flags ?? OutputSymbolFlags.None,
|
|
59
41
|
},
|
|
60
42
|
"function",
|
|
61
|
-
false,
|
|
62
43
|
);
|
|
63
44
|
emitSymbol(sym);
|
|
64
45
|
|
|
@@ -66,7 +47,7 @@ export function FunctionDeclaration(props: FunctionDeclarationProps) {
|
|
|
66
47
|
<>
|
|
67
48
|
<Declaration {...props} nameKind="function" symbol={sym}>
|
|
68
49
|
{asyncKwd}def <Name />
|
|
69
|
-
<
|
|
50
|
+
<LexicalScope name={sym.name}>
|
|
70
51
|
<CallSignature
|
|
71
52
|
{...callSignatureProps}
|
|
72
53
|
returnType={props.returnType}
|
|
@@ -75,12 +56,18 @@ export function FunctionDeclaration(props: FunctionDeclarationProps) {
|
|
|
75
56
|
<Show when={Boolean(props.doc)}>{props.doc}</Show>
|
|
76
57
|
{props.children ?? "pass"}
|
|
77
58
|
</PythonBlock>
|
|
78
|
-
</
|
|
59
|
+
</LexicalScope>
|
|
79
60
|
</Declaration>
|
|
80
61
|
</>
|
|
81
62
|
);
|
|
82
63
|
}
|
|
83
64
|
|
|
65
|
+
export interface InitFunctionDeclarationProps
|
|
66
|
+
extends Omit<
|
|
67
|
+
FunctionDeclarationProps,
|
|
68
|
+
"name" | "instanceFunction" | "classFunction"
|
|
69
|
+
> {}
|
|
70
|
+
|
|
84
71
|
/**
|
|
85
72
|
* A Python `__init__` function declaration.
|
|
86
73
|
*
|
|
@@ -102,12 +89,7 @@ export function FunctionDeclaration(props: FunctionDeclarationProps) {
|
|
|
102
89
|
* an instance function, and forces the name to be `__init__` without applying
|
|
103
90
|
* the name policy.
|
|
104
91
|
*/
|
|
105
|
-
export function InitFunctionDeclaration(
|
|
106
|
-
props: Omit<
|
|
107
|
-
FunctionDeclarationProps,
|
|
108
|
-
"name" | "instanceFunction" | "classFunction"
|
|
109
|
-
>,
|
|
110
|
-
) {
|
|
92
|
+
export function InitFunctionDeclaration(props: InitFunctionDeclarationProps) {
|
|
111
93
|
return (
|
|
112
94
|
<NoNamePolicy>
|
|
113
95
|
<FunctionDeclaration
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Scope, ScopePropsWithInfo, ScopePropsWithValue } from "@alloy-js/core";
|
|
2
|
+
import { createLexicalScope } from "../symbol-creation.js";
|
|
3
|
+
import { PythonLexicalScope } from "../symbols/python-lexical-scope.js";
|
|
4
|
+
|
|
5
|
+
export interface LexicalScopePropsWithScopeValue extends ScopePropsWithValue {}
|
|
6
|
+
export interface LeixcalScopePropsWithScopeInfo extends ScopePropsWithInfo {}
|
|
7
|
+
|
|
8
|
+
export type LexicalScopeProps =
|
|
9
|
+
| LexicalScopePropsWithScopeValue
|
|
10
|
+
| LeixcalScopePropsWithScopeInfo;
|
|
11
|
+
|
|
12
|
+
export function LexicalScope(props: LexicalScopeProps) {
|
|
13
|
+
let scope;
|
|
14
|
+
if ("value" in props) {
|
|
15
|
+
if (!(props.value instanceof PythonLexicalScope)) {
|
|
16
|
+
throw new Error("LexicalScope value must be a TSLexicalScope instance");
|
|
17
|
+
}
|
|
18
|
+
scope = props.value;
|
|
19
|
+
} else {
|
|
20
|
+
scope = createLexicalScope(props.name ?? "Lexical scope", props);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return <Scope value={scope}>{props.children}</Scope>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Children, MemberScope as CoreMemberScope } from "@alloy-js/core";
|
|
2
|
+
import { PythonMemberScope } from "../symbols/python-member-scope.js";
|
|
3
|
+
import { PythonOutputSymbol } from "../symbols/python-output-symbol.js";
|
|
4
|
+
import { usePythonScope } from "../symbols/scopes.js";
|
|
5
|
+
export interface MemberScopeProps {
|
|
6
|
+
children: Children;
|
|
7
|
+
ownerSymbol: PythonOutputSymbol;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function MemberScope(props: MemberScopeProps) {
|
|
11
|
+
const memberScope = new PythonMemberScope("member-scope", usePythonScope(), {
|
|
12
|
+
ownerSymbol: props.ownerSymbol,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
return (
|
|
16
|
+
<CoreMemberScope value={memberScope}>{props.children}</CoreMemberScope>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { Block, Children } from "@alloy-js/core";
|
|
2
2
|
|
|
3
|
+
export interface PythonBlockProps {
|
|
4
|
+
children: Children;
|
|
5
|
+
opener?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* A Python block component that can be used to render a block of Python code.
|
|
5
10
|
*
|
|
@@ -17,7 +22,7 @@ import { Block, Children } from "@alloy-js/core";
|
|
|
17
22
|
* y: str = None
|
|
18
23
|
* ```
|
|
19
24
|
*/
|
|
20
|
-
export function PythonBlock(props:
|
|
25
|
+
export function PythonBlock(props: PythonBlockProps) {
|
|
21
26
|
return (
|
|
22
27
|
<Block opener={props.opener ?? ""} closer="">
|
|
23
28
|
{props.children}
|
|
@@ -70,7 +70,7 @@ export function SourceFile(props: SourceFileProps) {
|
|
|
70
70
|
const path: string = join(currentDir, props.path)
|
|
71
71
|
.replace(/\.py$/, "")
|
|
72
72
|
.replace(/\//g, ".");
|
|
73
|
-
const scope = new PythonModuleScope(path);
|
|
73
|
+
const scope = new PythonModuleScope(path, undefined);
|
|
74
74
|
const sfContext: PythonSourceFileContext = {
|
|
75
75
|
scope: scope,
|
|
76
76
|
module: path,
|
|
@@ -84,7 +84,7 @@ export function SourceFile(props: SourceFileProps) {
|
|
|
84
84
|
<hbr />
|
|
85
85
|
</Show>
|
|
86
86
|
<PythonSourceFileContext.Provider value={sfContext}>
|
|
87
|
-
<Scope value={scope}
|
|
87
|
+
<Scope value={scope}>
|
|
88
88
|
<List doubleHardline>{props.children}</List>
|
|
89
89
|
</Scope>
|
|
90
90
|
</PythonSourceFileContext.Provider>
|
|
@@ -2,15 +2,10 @@ import {
|
|
|
2
2
|
Children,
|
|
3
3
|
Declaration as CoreDeclaration,
|
|
4
4
|
Name,
|
|
5
|
-
OutputScope,
|
|
6
|
-
OutputSymbolFlags,
|
|
7
5
|
Show,
|
|
8
6
|
createSymbolSlot,
|
|
9
|
-
effect,
|
|
10
7
|
emitSymbol,
|
|
11
8
|
memo,
|
|
12
|
-
useMemberScope,
|
|
13
|
-
useScope,
|
|
14
9
|
} from "@alloy-js/core";
|
|
15
10
|
import { createPythonSymbol } from "../symbol-creation.js";
|
|
16
11
|
import { Atom } from "./Atom.jsx";
|
|
@@ -77,27 +72,23 @@ export interface VariableDeclarationProps extends BaseDeclarationProps {
|
|
|
77
72
|
* ```
|
|
78
73
|
*/
|
|
79
74
|
export function VariableDeclaration(props: VariableDeclarationProps) {
|
|
80
|
-
const instanceVariable = props.instanceVariable ?? false;
|
|
81
75
|
const TypeSymbolSlot = createSymbolSlot();
|
|
82
76
|
const ValueTypeSymbolSlot = createSymbolSlot();
|
|
83
|
-
const memberScope = useMemberScope();
|
|
84
|
-
let scope: OutputScope | undefined = undefined;
|
|
85
|
-
// Only consider the member scope if this is an instance variable
|
|
86
|
-
if (memberScope !== undefined && instanceVariable) {
|
|
87
|
-
scope = memberScope.instanceMembers!;
|
|
88
|
-
} else {
|
|
89
|
-
scope = useScope();
|
|
90
|
-
}
|
|
91
77
|
|
|
92
78
|
const sym = createPythonSymbol(
|
|
93
79
|
props.name,
|
|
94
80
|
{
|
|
95
|
-
|
|
81
|
+
instance: props.instanceVariable,
|
|
96
82
|
refkeys: props.refkey,
|
|
83
|
+
type: props.type ? TypeSymbolSlot.firstSymbol : undefined,
|
|
97
84
|
},
|
|
98
85
|
"variable",
|
|
99
|
-
true,
|
|
100
86
|
);
|
|
87
|
+
|
|
88
|
+
if (!props.type) {
|
|
89
|
+
ValueTypeSymbolSlot.moveMembersTo(sym);
|
|
90
|
+
}
|
|
91
|
+
|
|
101
92
|
emitSymbol(sym);
|
|
102
93
|
// Handle optional type annotation
|
|
103
94
|
const type = memo(() => {
|
|
@@ -109,25 +100,6 @@ export function VariableDeclaration(props: VariableDeclarationProps) {
|
|
|
109
100
|
);
|
|
110
101
|
});
|
|
111
102
|
|
|
112
|
-
effect(() => {
|
|
113
|
-
if (TypeSymbolSlot.ref.value) {
|
|
114
|
-
const takenSymbols = TypeSymbolSlot.ref.value;
|
|
115
|
-
for (const symbol of takenSymbols) {
|
|
116
|
-
// If the symbol is a type, instantiate it
|
|
117
|
-
symbol.instantiateTo(sym);
|
|
118
|
-
}
|
|
119
|
-
} else if (ValueTypeSymbolSlot.ref.value) {
|
|
120
|
-
const takenSymbols = ValueTypeSymbolSlot.ref.value;
|
|
121
|
-
for (const symbol of takenSymbols) {
|
|
122
|
-
// ignore non-transient symbols (likely not the result of an
|
|
123
|
-
// expression).
|
|
124
|
-
if (symbol.flags & OutputSymbolFlags.Transient) {
|
|
125
|
-
symbol.moveTo(sym);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
|
|
131
103
|
// If we receive a symbol, resolve it to a name
|
|
132
104
|
const value =
|
|
133
105
|
typeof props.initializer === "object" ?
|
package/src/components/index.ts
CHANGED
|
@@ -8,7 +8,9 @@ export * from "./EnumMember.js";
|
|
|
8
8
|
export * from "./FunctionCallExpression.js";
|
|
9
9
|
export * from "./FunctionDeclaration.js";
|
|
10
10
|
export * from "./ImportStatement.js";
|
|
11
|
+
export * from "./LexicalScope.js";
|
|
11
12
|
export * from "./MemberExpression.js";
|
|
13
|
+
export * from "./MemberScope.jsx";
|
|
12
14
|
export * from "./NoNamePolicy.js";
|
|
13
15
|
export * from "./PyDoc.js";
|
|
14
16
|
export * from "./PythonBlock.js";
|
package/src/create-module.ts
CHANGED
|
@@ -36,8 +36,7 @@ function createSymbols(
|
|
|
36
36
|
// Otherwise, we append the path to the module name
|
|
37
37
|
const fullModuleScopeName = props.name + (path === "." ? "" : `.${path}`);
|
|
38
38
|
const keys = refkeys[path];
|
|
39
|
-
const moduleScope = new PythonModuleScope(fullModuleScopeName, {
|
|
40
|
-
parent: undefined,
|
|
39
|
+
const moduleScope = new PythonModuleScope(fullModuleScopeName, undefined, {
|
|
41
40
|
binder: binder,
|
|
42
41
|
});
|
|
43
42
|
|
|
@@ -45,17 +44,12 @@ function createSymbols(
|
|
|
45
44
|
for (const exportedName of symbols ?? []) {
|
|
46
45
|
const key = keys[exportedName];
|
|
47
46
|
|
|
48
|
-
const _ = createPythonSymbol(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
module: moduleScope.name,
|
|
55
|
-
},
|
|
56
|
-
undefined,
|
|
57
|
-
false,
|
|
58
|
-
);
|
|
47
|
+
const _ = createPythonSymbol(exportedName, {
|
|
48
|
+
space: moduleScope.symbols,
|
|
49
|
+
binder: binder,
|
|
50
|
+
refkeys: key,
|
|
51
|
+
module: moduleScope.name,
|
|
52
|
+
});
|
|
59
53
|
}
|
|
60
54
|
}
|
|
61
55
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { PythonOutputSymbol } from "./symbols/python-output-symbol.js";
|
|
2
|
+
|
|
3
|
+
export function pythonNameConflictResolver(
|
|
4
|
+
_: string,
|
|
5
|
+
symbols: PythonOutputSymbol[],
|
|
6
|
+
) {
|
|
7
|
+
for (let i = 1; i < symbols.length; i++) {
|
|
8
|
+
// Rename all but the first symbol to have a suffix of _2, _3, plus the scope name if available.
|
|
9
|
+
const symbol = symbols[i] as unknown as {
|
|
10
|
+
originalName: string;
|
|
11
|
+
name: string;
|
|
12
|
+
module?: string;
|
|
13
|
+
};
|
|
14
|
+
symbol.name =
|
|
15
|
+
symbol.originalName +
|
|
16
|
+
"_" +
|
|
17
|
+
(i + 1) +
|
|
18
|
+
"_" +
|
|
19
|
+
(symbols[i].aliasTarget?.scope?.name ?? symbol.module ?? "");
|
|
20
|
+
}
|
|
21
|
+
}
|