python2ts 1.3.1 → 1.3.2

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.
@@ -809,7 +809,22 @@ function transformNode(node, ctx) {
809
809
  }
810
810
  function transformScript(node, ctx) {
811
811
  const children = getChildren(node);
812
- const statements = children.filter((child) => child.name !== "Comment" || getNodeText(child, ctx.source).trim() !== "").map((child) => {
812
+ let moduleDocstring = "";
813
+ let startIndex = 0;
814
+ const filteredChildren = children.filter(
815
+ (child) => child.name !== "Comment" || getNodeText(child, ctx.source).trim() !== ""
816
+ );
817
+ if (filteredChildren.length > 1) {
818
+ const firstChild = filteredChildren[0];
819
+ if (firstChild && isDocstringNode(firstChild, ctx)) {
820
+ const content = extractDocstringContent(firstChild, ctx);
821
+ const parsed = parseDocstring(content);
822
+ const jsdoc = toJSDoc(parsed, "");
823
+ moduleDocstring = jsdoc.replace(" */", " * @module\n */");
824
+ startIndex = 1;
825
+ }
826
+ }
827
+ const statements = filteredChildren.slice(startIndex).map((child) => {
813
828
  const transformed = transformNode(child, ctx);
814
829
  if (transformed === "") {
815
830
  return "";
@@ -819,6 +834,9 @@ function transformScript(node, ctx) {
819
834
  }
820
835
  return transformed;
821
836
  }).filter((s) => s.trim() !== "");
837
+ if (moduleDocstring) {
838
+ return moduleDocstring + "\n" + statements.join("\n");
839
+ }
822
840
  return statements.join("\n");
823
841
  }
824
842
  function transformExpressionStatement(node, ctx) {
@@ -1302,9 +1320,26 @@ function transformCallExpression(node, ctx) {
1302
1320
  case "reversed":
1303
1321
  ctx.usesRuntime.add("reversed");
1304
1322
  return `reversed(${args})`;
1305
- case "isinstance":
1323
+ case "isinstance": {
1306
1324
  ctx.usesRuntime.add("isinstance");
1325
+ if (argList) {
1326
+ const argChildren = getChildren(argList).filter(
1327
+ (c) => c.name !== "(" && c.name !== ")" && c.name !== ","
1328
+ );
1329
+ if (argChildren.length >= 2) {
1330
+ const firstArg = argChildren[0];
1331
+ const secondArg = argChildren[1];
1332
+ if (firstArg && secondArg?.name === "TupleExpression") {
1333
+ const tupleChildren = getChildren(secondArg).filter(
1334
+ (c) => c.name !== "(" && c.name !== ")" && c.name !== ","
1335
+ );
1336
+ const typesCodes = tupleChildren.map((el) => transformNode(el, ctx));
1337
+ return `isinstance(${transformNode(firstArg, ctx)}, [${typesCodes.join(", ")}])`;
1338
+ }
1339
+ }
1340
+ }
1307
1341
  return `isinstance(${args})`;
1342
+ }
1308
1343
  case "type":
1309
1344
  ctx.usesRuntime.add("type");
1310
1345
  return `type(${args})`;
@@ -3673,8 +3708,10 @@ function transformDecoratedStatement(node, ctx) {
3673
3708
  const returnTypeStr = returnType ? `: ${returnType === "null" ? "void" : returnType}` : "";
3674
3709
  return `function ${funcName}(${params})${returnTypeStr}`;
3675
3710
  }
3711
+ const indent = " ".repeat(ctx.indentLevel);
3712
+ const { jsdoc, skipFirstStatement } = body ? extractDocstringFromBody(body, ctx, indent) : { jsdoc: null, skipFirstStatement: false };
3676
3713
  ctx.insideFunctionBody++;
3677
- const bodyCode = body ? transformBody(body, ctx, false, paramNames) : "";
3714
+ const bodyCode = body ? transformBody(body, ctx, skipFirstStatement, paramNames) : "";
3678
3715
  ctx.insideFunctionBody--;
3679
3716
  let funcExpr = `function ${funcName}(${params}) {
3680
3717
  ${bodyCode}
@@ -3688,7 +3725,9 @@ ${bodyCode}
3688
3725
  funcExpr = `${dec.name}(${funcExpr})`;
3689
3726
  }
3690
3727
  }
3691
- return `const ${funcName} = ${funcExpr}`;
3728
+ const declaration = `const ${funcName} = ${funcExpr}`;
3729
+ return jsdoc ? `${jsdoc}
3730
+ ${declaration}` : declaration;
3692
3731
  }
3693
3732
  function transformDecoratedClass(classDef, decorators, ctx) {
3694
3733
  const dataclassDecorator = decorators.find(
package/dist/cli/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  transpileAsync
4
- } from "../chunk-LNOL3BMB.js";
4
+ } from "../chunk-RNRRQS6K.js";
5
5
 
6
6
  // src/cli/index.ts
7
7
  import { parseArgs } from "util";
package/dist/index.js CHANGED
@@ -12,7 +12,7 @@ import {
12
12
  transpile,
13
13
  transpileAsync,
14
14
  walkTree
15
- } from "./chunk-LNOL3BMB.js";
15
+ } from "./chunk-RNRRQS6K.js";
16
16
  export {
17
17
  debugTree,
18
18
  formatCode,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "python2ts",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "AST-based Python to TypeScript transpiler. Convert Python code to clean, idiomatic TypeScript with full type preservation.",
5
5
  "homepage": "https://sebastian-software.github.io/python2ts/",
6
6
  "repository": {
@@ -53,7 +53,7 @@
53
53
  "eslint": "^9.39.2",
54
54
  "prettier": "^3.8.0",
55
55
  "typescript-eslint": "^8.53.1",
56
- "pythonlib": "2.0.1"
56
+ "pythonlib": "2.0.2"
57
57
  },
58
58
  "devDependencies": {
59
59
  "tsup": "^8.5.1",