@alloy-js/python 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/LICENSE +7 -0
  3. package/api-extractor.json +4 -0
  4. package/dist/src/builtins/python.d.ts +9 -0
  5. package/dist/src/builtins/python.d.ts.map +1 -0
  6. package/dist/src/builtins/python.js +17 -0
  7. package/dist/src/components/Atom.d.ts +19 -0
  8. package/dist/src/components/Atom.d.ts.map +1 -0
  9. package/dist/src/components/Atom.js +82 -0
  10. package/dist/src/components/CallSignature.d.ts +79 -0
  11. package/dist/src/components/CallSignature.d.ts.map +1 -0
  12. package/dist/src/components/CallSignature.js +201 -0
  13. package/dist/src/components/ClassDeclaration.d.ts +37 -0
  14. package/dist/src/components/ClassDeclaration.d.ts.map +1 -0
  15. package/dist/src/components/ClassDeclaration.js +83 -0
  16. package/dist/src/components/ClassInstantiation.d.ts +24 -0
  17. package/dist/src/components/ClassInstantiation.d.ts.map +1 -0
  18. package/dist/src/components/ClassInstantiation.js +35 -0
  19. package/dist/src/components/Declaration.d.ts +48 -0
  20. package/dist/src/components/Declaration.d.ts.map +1 -0
  21. package/dist/src/components/Declaration.js +37 -0
  22. package/dist/src/components/EnumDeclaration.d.ts +164 -0
  23. package/dist/src/components/EnumDeclaration.d.ts.map +1 -0
  24. package/dist/src/components/EnumDeclaration.js +278 -0
  25. package/dist/src/components/EnumMember.d.ts +46 -0
  26. package/dist/src/components/EnumMember.d.ts.map +1 -0
  27. package/dist/src/components/EnumMember.js +67 -0
  28. package/dist/src/components/FunctionCallExpression.d.ts +19 -0
  29. package/dist/src/components/FunctionCallExpression.d.ts.map +1 -0
  30. package/dist/src/components/FunctionCallExpression.js +40 -0
  31. package/dist/src/components/FunctionDeclaration.d.ts +47 -0
  32. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -0
  33. package/dist/src/components/FunctionDeclaration.js +107 -0
  34. package/dist/src/components/ImportStatement.d.ts +39 -0
  35. package/dist/src/components/ImportStatement.d.ts.map +1 -0
  36. package/dist/src/components/ImportStatement.js +104 -0
  37. package/dist/src/components/MemberExpression.d.ts +97 -0
  38. package/dist/src/components/MemberExpression.d.ts.map +1 -0
  39. package/dist/src/components/MemberExpression.js +308 -0
  40. package/dist/src/components/NoNamePolicy.d.ts +23 -0
  41. package/dist/src/components/NoNamePolicy.d.ts.map +1 -0
  42. package/dist/src/components/NoNamePolicy.js +27 -0
  43. package/dist/src/components/PyDoc.d.ts +90 -0
  44. package/dist/src/components/PyDoc.d.ts.map +1 -0
  45. package/dist/src/components/PyDoc.js +280 -0
  46. package/dist/src/components/PythonBlock.d.ts +23 -0
  47. package/dist/src/components/PythonBlock.d.ts.map +1 -0
  48. package/dist/src/components/PythonBlock.js +31 -0
  49. package/dist/src/components/Reference.d.ts +13 -0
  50. package/dist/src/components/Reference.d.ts.map +1 -0
  51. package/dist/src/components/Reference.js +18 -0
  52. package/dist/src/components/SourceFile.d.ts +46 -0
  53. package/dist/src/components/SourceFile.d.ts.map +1 -0
  54. package/dist/src/components/SourceFile.js +75 -0
  55. package/dist/src/components/StatementList.d.ts +25 -0
  56. package/dist/src/components/StatementList.d.ts.map +1 -0
  57. package/dist/src/components/StatementList.js +29 -0
  58. package/dist/src/components/VariableDeclaration.d.ts +62 -0
  59. package/dist/src/components/VariableDeclaration.d.ts.map +1 -0
  60. package/dist/src/components/VariableDeclaration.js +131 -0
  61. package/dist/src/components/index.d.ts +19 -0
  62. package/dist/src/components/index.d.ts.map +1 -0
  63. package/dist/src/components/index.js +18 -0
  64. package/dist/src/create-module.d.ts +16 -0
  65. package/dist/src/create-module.d.ts.map +1 -0
  66. package/dist/src/create-module.js +64 -0
  67. package/dist/src/index.d.ts +8 -0
  68. package/dist/src/index.d.ts.map +1 -0
  69. package/dist/src/index.js +7 -0
  70. package/dist/src/name-policy.d.ts +5 -0
  71. package/dist/src/name-policy.d.ts.map +1 -0
  72. package/dist/src/name-policy.js +47 -0
  73. package/dist/src/parameter-descriptor.d.ts +31 -0
  74. package/dist/src/parameter-descriptor.d.ts.map +1 -0
  75. package/dist/src/parameter-descriptor.js +1 -0
  76. package/dist/src/symbol-creation.d.ts +4 -0
  77. package/dist/src/symbol-creation.d.ts.map +1 -0
  78. package/dist/src/symbol-creation.js +24 -0
  79. package/dist/src/symbols/custom-output-scope.d.ts +10 -0
  80. package/dist/src/symbols/custom-output-scope.d.ts.map +1 -0
  81. package/dist/src/symbols/custom-output-scope.js +25 -0
  82. package/dist/src/symbols/index.d.ts +7 -0
  83. package/dist/src/symbols/index.d.ts.map +1 -0
  84. package/dist/src/symbols/index.js +6 -0
  85. package/dist/src/symbols/python-member-scope.d.ts +7 -0
  86. package/dist/src/symbols/python-member-scope.d.ts.map +1 -0
  87. package/dist/src/symbols/python-member-scope.js +9 -0
  88. package/dist/src/symbols/python-module-scope.d.ts +25 -0
  89. package/dist/src/symbols/python-module-scope.d.ts.map +1 -0
  90. package/dist/src/symbols/python-module-scope.js +52 -0
  91. package/dist/src/symbols/python-output-symbol.d.ts +19 -0
  92. package/dist/src/symbols/python-output-symbol.d.ts.map +1 -0
  93. package/dist/src/symbols/python-output-symbol.js +22 -0
  94. package/dist/src/symbols/reference.d.ts +4 -0
  95. package/dist/src/symbols/reference.d.ts.map +1 -0
  96. package/dist/src/symbols/reference.js +60 -0
  97. package/dist/src/symbols/scopes.d.ts +5 -0
  98. package/dist/src/symbols/scopes.d.ts.map +1 -0
  99. package/dist/src/symbols/scopes.js +4 -0
  100. package/dist/src/utils.d.ts +7 -0
  101. package/dist/src/utils.d.ts.map +1 -0
  102. package/dist/src/utils.js +12 -0
  103. package/dist/test/callsignatures.test.d.ts +2 -0
  104. package/dist/test/callsignatures.test.d.ts.map +1 -0
  105. package/dist/test/callsignatures.test.js +276 -0
  106. package/dist/test/classdeclarations.test.d.ts +2 -0
  107. package/dist/test/classdeclarations.test.d.ts.map +1 -0
  108. package/dist/test/classdeclarations.test.js +397 -0
  109. package/dist/test/classinstantiations.test.d.ts +2 -0
  110. package/dist/test/classinstantiations.test.d.ts.map +1 -0
  111. package/dist/test/classinstantiations.test.js +168 -0
  112. package/dist/test/enums.test.d.ts +2 -0
  113. package/dist/test/enums.test.d.ts.map +1 -0
  114. package/dist/test/enums.test.js +211 -0
  115. package/dist/test/externals.test.d.ts +2 -0
  116. package/dist/test/externals.test.d.ts.map +1 -0
  117. package/dist/test/externals.test.js +219 -0
  118. package/dist/test/functioncallexpressions.test.d.ts +2 -0
  119. package/dist/test/functioncallexpressions.test.d.ts.map +1 -0
  120. package/dist/test/functioncallexpressions.test.js +156 -0
  121. package/dist/test/functiondeclaration.test.d.ts +2 -0
  122. package/dist/test/functiondeclaration.test.d.ts.map +1 -0
  123. package/dist/test/functiondeclaration.test.js +363 -0
  124. package/dist/test/imports.test.d.ts +2 -0
  125. package/dist/test/imports.test.d.ts.map +1 -0
  126. package/dist/test/imports.test.js +262 -0
  127. package/dist/test/memberexpressions.test.d.ts +2 -0
  128. package/dist/test/memberexpressions.test.d.ts.map +1 -0
  129. package/dist/test/memberexpressions.test.js +879 -0
  130. package/dist/test/namepolicies.test.d.ts +2 -0
  131. package/dist/test/namepolicies.test.d.ts.map +1 -0
  132. package/dist/test/namepolicies.test.js +109 -0
  133. package/dist/test/pydocs.test.d.ts +2 -0
  134. package/dist/test/pydocs.test.d.ts.map +1 -0
  135. package/dist/test/pydocs.test.js +500 -0
  136. package/dist/test/references.test.d.ts +2 -0
  137. package/dist/test/references.test.d.ts.map +1 -0
  138. package/dist/test/references.test.js +49 -0
  139. package/dist/test/sourcefiles.test.d.ts +2 -0
  140. package/dist/test/sourcefiles.test.d.ts.map +1 -0
  141. package/dist/test/sourcefiles.test.js +198 -0
  142. package/dist/test/utils.d.ts +23 -0
  143. package/dist/test/utils.d.ts.map +1 -0
  144. package/dist/test/utils.js +88 -0
  145. package/dist/test/values.test.d.ts +2 -0
  146. package/dist/test/values.test.d.ts.map +1 -0
  147. package/dist/test/values.test.js +78 -0
  148. package/dist/test/variables.test.d.ts +2 -0
  149. package/dist/test/variables.test.d.ts.map +1 -0
  150. package/dist/test/variables.test.js +173 -0
  151. package/dist/tsconfig.tsbuildinfo +1 -0
  152. package/package.json +39 -0
  153. package/src/builtins/python.ts +20 -0
  154. package/src/components/Atom.tsx +76 -0
  155. package/src/components/CallSignature.tsx +251 -0
  156. package/src/components/ClassDeclaration.tsx +98 -0
  157. package/src/components/ClassInstantiation.tsx +54 -0
  158. package/src/components/Declaration.tsx +91 -0
  159. package/src/components/EnumDeclaration.tsx +291 -0
  160. package/src/components/EnumMember.tsx +92 -0
  161. package/src/components/FunctionCallExpression.tsx +36 -0
  162. package/src/components/FunctionDeclaration.tsx +121 -0
  163. package/src/components/ImportStatement.tsx +134 -0
  164. package/src/components/MemberExpression.tsx +456 -0
  165. package/src/components/NoNamePolicy.tsx +31 -0
  166. package/src/components/PyDoc.tsx +331 -0
  167. package/src/components/PythonBlock.tsx +26 -0
  168. package/src/components/Reference.tsx +21 -0
  169. package/src/components/SourceFile.tsx +93 -0
  170. package/src/components/StatementList.tsx +28 -0
  171. package/src/components/VariableDeclaration.tsx +180 -0
  172. package/src/components/index.ts +18 -0
  173. package/src/create-module.ts +102 -0
  174. package/src/index.ts +7 -0
  175. package/src/name-policy.ts +101 -0
  176. package/src/parameter-descriptor.ts +36 -0
  177. package/src/symbol-creation.ts +36 -0
  178. package/src/symbols/custom-output-scope.ts +35 -0
  179. package/src/symbols/index.ts +6 -0
  180. package/src/symbols/python-member-scope.ts +12 -0
  181. package/src/symbols/python-module-scope.ts +89 -0
  182. package/src/symbols/python-output-symbol.ts +36 -0
  183. package/src/symbols/reference.ts +99 -0
  184. package/src/symbols/scopes.ts +9 -0
  185. package/src/utils.ts +27 -0
  186. package/temp/api.json +7207 -0
  187. package/test/callsignatures.test.tsx +256 -0
  188. package/test/classdeclarations.test.tsx +320 -0
  189. package/test/classinstantiations.test.tsx +159 -0
  190. package/test/enums.test.tsx +203 -0
  191. package/test/externals.test.tsx +190 -0
  192. package/test/functioncallexpressions.test.tsx +145 -0
  193. package/test/functiondeclaration.test.tsx +327 -0
  194. package/test/imports.test.tsx +214 -0
  195. package/test/memberexpressions.test.tsx +725 -0
  196. package/test/namepolicies.test.tsx +109 -0
  197. package/test/pydocs.test.tsx +528 -0
  198. package/test/references.test.tsx +36 -0
  199. package/test/sourcefiles.test.tsx +131 -0
  200. package/test/utils.tsx +131 -0
  201. package/test/values.test.tsx +61 -0
  202. package/test/variables.test.tsx +153 -0
  203. package/tsconfig.json +12 -0
  204. package/tsdoc-metadata.json +11 -0
  205. package/vitest.config.ts +10 -0
  206. package/vitest.setup.ts +1 -0
@@ -0,0 +1,40 @@
1
+ import { memo as _$memo, createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { For, Indent, Wrap } from "@alloy-js/core";
3
+ /**
4
+ * A Python function call expression.
5
+ *
6
+ * @example
7
+ * ```tsx
8
+ * <FunctionCallExpression target="foo" args={["arg1", "arg2"]} />
9
+ * ```
10
+ * This will generate:
11
+ * ```python
12
+ * foo(arg1, arg2)
13
+ * ```
14
+ */
15
+ export function FunctionCallExpression(props) {
16
+ return _$createIntrinsic("group", {
17
+ get children() {
18
+ return [_$memo(() => props.target), "(", _$createComponent(Wrap, {
19
+ get when() {
20
+ return !!props.args && props.args.length > 1;
21
+ },
22
+ "with": Indent,
23
+ props: {
24
+ softline: true,
25
+ trailingBreak: true
26
+ },
27
+ get children() {
28
+ return _$createComponent(For, {
29
+ get each() {
30
+ return props.args ?? [];
31
+ },
32
+ comma: true,
33
+ line: true,
34
+ children: arg => arg
35
+ });
36
+ }
37
+ }), ")"];
38
+ }
39
+ });
40
+ }
@@ -0,0 +1,47 @@
1
+ import { CallSignatureProps } from "./CallSignature.jsx";
2
+ import { BaseDeclarationProps } from "./Declaration.js";
3
+ export interface FunctionDeclarationProps extends BaseDeclarationProps, CallSignatureProps {
4
+ async?: boolean;
5
+ }
6
+ /**
7
+ * A Python function declaration.
8
+ *
9
+ * @example
10
+ * ```tsx
11
+ * <FunctionDeclaration
12
+ * name="my_function"
13
+ * returnType="int"
14
+ * parameters=[{name: "a", type: "int"},{name: "b", type: "str"}]>
15
+ * return a + b
16
+ * </FunctionDeclaration>
17
+ * ```
18
+ * This will generate:
19
+ * ```python
20
+ * def my_function(a: int, b: str) -> int:
21
+ * return a + b
22
+ * ```
23
+ */
24
+ export declare function FunctionDeclaration(props: FunctionDeclarationProps): import("@alloy-js/core").Children;
25
+ /**
26
+ * A Python `__init__` function declaration.
27
+ *
28
+ * @example
29
+ * ```tsx
30
+ * <InitFunctionDeclaration>
31
+ * self.attribute = "value"
32
+ * </InitFunctionDeclaration>
33
+ * ```
34
+ * This will generate:
35
+ * ```python
36
+ * def __init__(self: MyClass) -> None:
37
+ * self.attribute = "value"
38
+ * ```
39
+ *
40
+ * @remarks
41
+ *
42
+ * This is a convenience component that sets the name to `__init__`, marks it as
43
+ * an instance function, and forces the name to be `__init__` without applying
44
+ * the name policy.
45
+ */
46
+ export declare function InitFunctionDeclaration(props: Omit<FunctionDeclarationProps, "name" | "instanceFunction" | "classFunction">): import("@alloy-js/core").Children;
47
+ //# sourceMappingURL=FunctionDeclaration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FunctionDeclaration.d.ts","sourceRoot":"","sources":["../../../src/components/FunctionDeclaration.tsx"],"names":[],"mappings":"AAYA,OAAO,EAAiB,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACxE,OAAO,EAAE,oBAAoB,EAAe,MAAM,kBAAkB,CAAC;AAIrE,MAAM,WAAW,wBACf,SAAQ,oBAAoB,EAC1B,kBAAkB;IACpB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,wBAAwB,qCAwClE;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,uBAAuB,CACrC,KAAK,EAAE,IAAI,CACT,wBAAwB,EACxB,MAAM,GAAG,kBAAkB,GAAG,eAAe,CAC9C,qCAYF"}
@@ -0,0 +1,107 @@
1
+ import { createComponent as _$createComponent, mergeProps as _$mergeProps, memo as _$memo } from "@alloy-js/core/jsx-runtime";
2
+ import { emitSymbol, Name, OutputSymbolFlags, Scope, Show, useMemberScope, useScope } from "@alloy-js/core";
3
+ import { createPythonSymbol } from "../symbol-creation.js";
4
+ import { getCallSignatureProps } from "../utils.js";
5
+ import { CallSignature } from "./CallSignature.js";
6
+ import { Declaration } from "./Declaration.js";
7
+ import { PythonBlock } from "./PythonBlock.js";
8
+ import { NoNamePolicy } from "./index.js";
9
+ /**
10
+ * A Python function declaration.
11
+ *
12
+ * @example
13
+ * ```tsx
14
+ * <FunctionDeclaration
15
+ * name="my_function"
16
+ * returnType="int"
17
+ * parameters=[{name: "a", type: "int"},{name: "b", type: "str"}]>
18
+ * return a + b
19
+ * </FunctionDeclaration>
20
+ * ```
21
+ * This will generate:
22
+ * ```python
23
+ * def my_function(a: int, b: str) -> int:
24
+ * return a + b
25
+ * ```
26
+ */
27
+ export function FunctionDeclaration(props) {
28
+ const asyncKwd = props.async ? "async " : "";
29
+ const callSignatureProps = getCallSignatureProps(props, {});
30
+ const memberScope = useMemberScope();
31
+ let scope = undefined;
32
+ if (memberScope !== undefined) {
33
+ scope = memberScope.instanceMembers;
34
+ } else {
35
+ scope = useScope();
36
+ }
37
+ const sym = createPythonSymbol(props.name, {
38
+ scope: scope,
39
+ refkeys: props.refkey,
40
+ flags: props.flags ?? OutputSymbolFlags.None
41
+ }, "function", false);
42
+ emitSymbol(sym);
43
+ return [_$createComponent(Declaration, _$mergeProps(props, {
44
+ nameKind: "function",
45
+ symbol: sym,
46
+ get children() {
47
+ return [asyncKwd, "def ", _$createComponent(Name, {}), _$createComponent(Scope, {
48
+ get name() {
49
+ return sym.name;
50
+ },
51
+ kind: "function",
52
+ get children() {
53
+ return [_$createComponent(CallSignature, _$mergeProps(callSignatureProps, {
54
+ get returnType() {
55
+ return props.returnType;
56
+ }
57
+ })), _$createComponent(PythonBlock, {
58
+ opener: ":",
59
+ get children() {
60
+ return [_$createComponent(Show, {
61
+ get when() {
62
+ return Boolean(props.doc);
63
+ },
64
+ get children() {
65
+ return props.doc;
66
+ }
67
+ }), _$memo(() => props.children ?? "pass")];
68
+ }
69
+ })];
70
+ }
71
+ })];
72
+ }
73
+ }))];
74
+ }
75
+
76
+ /**
77
+ * A Python `__init__` function declaration.
78
+ *
79
+ * @example
80
+ * ```tsx
81
+ * <InitFunctionDeclaration>
82
+ * self.attribute = "value"
83
+ * </InitFunctionDeclaration>
84
+ * ```
85
+ * This will generate:
86
+ * ```python
87
+ * def __init__(self: MyClass) -> None:
88
+ * self.attribute = "value"
89
+ * ```
90
+ *
91
+ * @remarks
92
+ *
93
+ * This is a convenience component that sets the name to `__init__`, marks it as
94
+ * an instance function, and forces the name to be `__init__` without applying
95
+ * the name policy.
96
+ */
97
+ export function InitFunctionDeclaration(props) {
98
+ return _$createComponent(NoNamePolicy, {
99
+ get children() {
100
+ return _$createComponent(FunctionDeclaration, _$mergeProps(props, {
101
+ name: "__init__",
102
+ instanceFunction: true,
103
+ classFunction: false
104
+ }));
105
+ }
106
+ });
107
+ }
@@ -0,0 +1,39 @@
1
+ import { ImportedSymbol, ImportRecords } from "../symbols/index.js";
2
+ export interface ImportStatementsProps {
3
+ records: ImportRecords;
4
+ joinImportsFromSameModule?: boolean;
5
+ }
6
+ /**
7
+ * A component that renders import statements based on the provided import records.
8
+ *
9
+ * @remarks
10
+ * This component will render import statements for each module and its symbols.
11
+ * If `joinImportsFromSameModule` is true, it will group imports from the same module
12
+ * into a single statement.
13
+ */
14
+ export declare function ImportStatements(props: ImportStatementsProps): () => import("@alloy-js/core").Children[];
15
+ export interface ImportStatementProps {
16
+ path: string;
17
+ symbols?: Set<ImportedSymbol>;
18
+ }
19
+ /**
20
+ * A Python import statement.
21
+ *
22
+ * @remarks
23
+ * This component renders an import statement for a given path and symbols.
24
+ * If no symbols are provided, it will render a simple import statement.
25
+ * If symbols are provided, it will render an import statement with the specified symbols.
26
+ *
27
+ * @example
28
+ * ```tsx
29
+ * <ImportStatement path="os" />
30
+ * <ImportStatement path="math" symbols={new Set([new ImportedSymbol("sqrt", "sqrt")])} />
31
+ * ```
32
+ * This will generate:
33
+ * ```python
34
+ * import os
35
+ * from math import sqrt
36
+ * ```
37
+ */
38
+ export declare function ImportStatement(props: ImportStatementProps): () => any[];
39
+ //# sourceMappingURL=ImportStatement.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ImportStatement.d.ts","sourceRoot":"","sources":["../../../src/components/ImportStatement.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpE,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,aAAa,CAAC;IACvB,yBAAyB,CAAC,EAAE,OAAO,CAAC;CACrC;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,6CA2C5D;AAED,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,GAAG,CAAC,cAAc,CAAC,CAAC;CAC/B;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,oBAAoB,eA8B1D"}
@@ -0,0 +1,104 @@
1
+ import { createComponent as _$createComponent, memo as _$memo, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { computed, mapJoin, memo } from "@alloy-js/core";
3
+ /**
4
+ * A component that renders import statements based on the provided import records.
5
+ *
6
+ * @remarks
7
+ * This component will render import statements for each module and its symbols.
8
+ * If `joinImportsFromSameModule` is true, it will group imports from the same module
9
+ * into a single statement.
10
+ */
11
+ export function ImportStatements(props) {
12
+ // Sort the import records by module name
13
+ const imports = computed(() => [...props.records].sort(([a], [b]) => {
14
+ return a.name.localeCompare(b.name);
15
+ }));
16
+ return mapJoin(() => imports.value, ([module, properties]) => {
17
+ // Only handling absolute imports for now
18
+ const targetPath = module.name;
19
+ if (properties.symbols && properties.symbols.size > 0) {
20
+ // Sort the symbols in a module by the imported name
21
+ const sortedSymbols = Array.from(properties.symbols).sort((a, b) => a.local.name.localeCompare(b.local.name));
22
+ if (props.joinImportsFromSameModule) {
23
+ // If joinImportsFromSameModule is true, we will group imports from the same module
24
+ return _$createComponent(ImportStatement, {
25
+ path: targetPath,
26
+ symbols: new Set(sortedSymbols)
27
+ });
28
+ } else {
29
+ return sortedSymbols.map((symbol, idx, arr) => [_$createComponent(ImportStatement, {
30
+ path: targetPath,
31
+ symbols: new Set([symbol])
32
+ }), _$memo(() => _$memo(() => idx < arr.length - 1)() && _$createIntrinsic("hbr", {}))]);
33
+ }
34
+ } else {
35
+ // If no symbols are specified, it's either a wildcard import or a module import
36
+ return _$createComponent(ImportStatement, {
37
+ path: targetPath,
38
+ get symbols() {
39
+ return properties.symbols;
40
+ }
41
+ });
42
+ }
43
+ });
44
+ }
45
+ /**
46
+ * A Python import statement.
47
+ *
48
+ * @remarks
49
+ * This component renders an import statement for a given path and symbols.
50
+ * If no symbols are provided, it will render a simple import statement.
51
+ * If symbols are provided, it will render an import statement with the specified symbols.
52
+ *
53
+ * @example
54
+ * ```tsx
55
+ * <ImportStatement path="os" />
56
+ * <ImportStatement path="math" symbols={new Set([new ImportedSymbol("sqrt", "sqrt")])} />
57
+ * ```
58
+ * This will generate:
59
+ * ```python
60
+ * import os
61
+ * from math import sqrt
62
+ * ```
63
+ */
64
+ export function ImportStatement(props) {
65
+ return memo(() => {
66
+ const {
67
+ path,
68
+ symbols
69
+ } = props;
70
+ const importSymbols = [];
71
+ if (symbols && symbols.size > 0) {
72
+ for (const sym of symbols) {
73
+ importSymbols.push(sym);
74
+ }
75
+ }
76
+ const parts = [];
77
+ if (!symbols || symbols.size === 0) {
78
+ parts.push(`import ${path}`);
79
+ } else {
80
+ importSymbols.sort((a, b) => {
81
+ return a.target.name.localeCompare(b.target.name);
82
+ });
83
+ parts.push(`from ${path} import `);
84
+ parts.push(mapJoin(() => importSymbols, nis => _$createComponent(ImportBinding, {
85
+ importedSymbol: nis
86
+ }), {
87
+ joiner: ", "
88
+ }));
89
+ }
90
+ return parts;
91
+ });
92
+ }
93
+ function ImportBinding(props) {
94
+ const text = memo(() => {
95
+ const localName = props.importedSymbol.local.name;
96
+ const targetName = props.importedSymbol.target.name;
97
+ if (localName === targetName) {
98
+ return targetName;
99
+ } else {
100
+ return `${targetName} as ${localName}`;
101
+ }
102
+ });
103
+ return [_$memo(text)];
104
+ }
@@ -0,0 +1,97 @@
1
+ import { Children, OutputSymbol, Refkey } from "@alloy-js/core";
2
+ export interface MemberExpressionProps {
3
+ children: Children;
4
+ }
5
+ /**
6
+ * Create a member expression from parts. Each part can provide one of
7
+ * the following:
8
+ *
9
+ * * **id**: The identifier for the member expression part
10
+ * * **refkey**: a refkey for a symbol whose name becomes the identifier
11
+ * * **symbol**: a symbol whose name becomes the identifier part
12
+ * * **args**: create a method call with the given args
13
+ * * **children**: arbitrary contents for the identifier part.
14
+ *
15
+ * @example
16
+ *
17
+ * ```tsx
18
+ * <MemberExpression>
19
+ * <MemberExpression.Part id="base" />
20
+ * <MemberExpression.Part refkey={rk} />
21
+ * <MemberExpression.Part symbol={sym} />
22
+ * <MemberExpression.Part args={["hello", "world"]} />
23
+ * <MemberExpression.Part>SomeValue</MemberExpression.Part>
24
+ * </MemberExpression>
25
+ * ```
26
+ *
27
+ * Assuming `rk` is a refkey to a symbol name "prop1", and `sym` is a symbol
28
+ * with a name of "prop2", this will render:
29
+ *
30
+ * ```ts
31
+ * base.prop1.prop2("hello", "world").SomeValue
32
+ * ```
33
+ */
34
+ export declare function MemberExpression(props: MemberExpressionProps): Children;
35
+ export declare namespace MemberExpression {
36
+ var Part: (props: MemberExpressionPartProps) => void;
37
+ }
38
+ export interface MemberExpressionPartProps {
39
+ /**
40
+ * The identifier for attribute access (obj.attr).
41
+ * Use this for Python attribute references.
42
+ */
43
+ id?: string | number;
44
+ /**
45
+ * Single key for subscription access (obj[key] or obj[0]).
46
+ * Use this for single subscription access.
47
+ */
48
+ key?: Children;
49
+ /**
50
+ * Multiple keys for tuple subscription access (obj[a, b] -\> obj[(a, b)]).
51
+ * Use this when you need tuple key access.
52
+ */
53
+ keys?: Children[];
54
+ /**
55
+ * Slice notation for subscription access (obj[start:stop:step]).
56
+ * Use this for Python slice syntax.
57
+ */
58
+ slice?: {
59
+ start?: Children;
60
+ stop?: Children;
61
+ step?: Children;
62
+ };
63
+ /**
64
+ * A refkey for a symbol whose name becomes the identifier.
65
+ */
66
+ refkey?: Refkey;
67
+ /**
68
+ * A symbol whose name becomes the identifier.
69
+ */
70
+ symbol?: OutputSymbol;
71
+ /**
72
+ * Arguments to construct a call expression.
73
+ */
74
+ args?: Children[] | boolean;
75
+ }
76
+ export interface SubscriptionProps {
77
+ /**
78
+ * Single key for subscription access (obj[key] or obj[0]).
79
+ * Use this for single subscription access.
80
+ */
81
+ key?: Children;
82
+ /**
83
+ * Multiple keys for tuple subscription access (obj[a, b] -\> obj[(a, b)]).
84
+ * Use this when you need tuple key access.
85
+ */
86
+ keys?: Children[];
87
+ /**
88
+ * Slice notation for subscription access (obj[start:stop:step]).
89
+ * Use this for Python slice syntax.
90
+ */
91
+ slice?: {
92
+ start?: Children;
93
+ stop?: Children;
94
+ step?: Children;
95
+ };
96
+ }
97
+ //# sourceMappingURL=MemberExpression.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MemberExpression.d.ts","sourceRoot":"","sources":["../../../src/components/MemberExpression.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EAMR,YAAY,EAGZ,MAAM,EAKP,MAAM,gBAAgB,CAAC;AAExB,MAAM,WAAW,qBAAqB;IACpC,QAAQ,EAAE,QAAQ,CAAC;CACpB;AAoCD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,GAAG,QAAQ,CAcvE;yBAde,gBAAgB;sBA8RS,yBAAyB;;AArDlE,MAAM,WAAW,yBAAyB;IACxC;;;OAGG;IACH,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAErB;;;OAGG;IACH,GAAG,CAAC,EAAE,QAAQ,CAAC;IAEf;;;OAGG;IACH,IAAI,CAAC,EAAE,QAAQ,EAAE,CAAC;IAElB;;;OAGG;IACH,KAAK,CAAC,EAAE;QACN,KAAK,CAAC,EAAE,QAAQ,CAAC;QACjB,IAAI,CAAC,EAAE,QAAQ,CAAC;QAChB,IAAI,CAAC,EAAE,QAAQ,CAAC;KACjB,CAAC;IAEF;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,MAAM,CAAC,EAAE,YAAY,CAAC;IAEtB;;OAEG;IACH,IAAI,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;CAC7B;AA4CD,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,GAAG,CAAC,EAAE,QAAQ,CAAC;IAEf;;;OAGG;IACH,IAAI,CAAC,EAAE,QAAQ,EAAE,CAAC;IAElB;;;OAGG;IACH,KAAK,CAAC,EAAE;QACN,KAAK,CAAC,EAAE,QAAQ,CAAC;QACjB,IAAI,CAAC,EAAE,QAAQ,CAAC;QAChB,IAAI,CAAC,EAAE,QAAQ,CAAC;KACjB,CAAC;CACH"}