python2ts 1.3.3 → 1.4.0

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.
@@ -791,6 +791,8 @@ function transformNode(node, ctx) {
791
791
  return transformExpressionStatement(node, ctx);
792
792
  case "AssignStatement":
793
793
  return transformAssignStatement(node, ctx);
794
+ case "UpdateStatement":
795
+ return transformUpdateStatement(node, ctx);
794
796
  case "BinaryExpression":
795
797
  return transformBinaryExpression(node, ctx);
796
798
  case "UnaryExpression":
@@ -807,6 +809,8 @@ function transformNode(node, ctx) {
807
809
  return transformString(node, ctx);
808
810
  case "FormatString":
809
811
  return transformFormatString(node, ctx);
812
+ case "ContinuedString":
813
+ return transformContinuedString(node, ctx);
810
814
  case "Boolean":
811
815
  return transformBoolean(node, ctx);
812
816
  case "None":
@@ -1025,6 +1029,23 @@ function extractVariableNames(nodes, source) {
1025
1029
  }
1026
1030
  return names;
1027
1031
  }
1032
+ function transformUpdateStatement(node, ctx) {
1033
+ const children = getChildren(node);
1034
+ const target = children.find(
1035
+ (c) => c.name === "VariableName" || c.name === "MemberExpression" || c.name === "Subscript"
1036
+ );
1037
+ const op = children.find((c) => c.name === "UpdateOp");
1038
+ const value = children.find(
1039
+ (c) => c !== target && c.name !== "UpdateOp" && c.name !== "(" && c.name !== ")" && c.name !== "," && c.name !== ":"
1040
+ );
1041
+ if (!target || !op || !value) {
1042
+ return getNodeText(node, ctx.source);
1043
+ }
1044
+ const targetCode = transformNode(target, ctx);
1045
+ const opText = getNodeText(op, ctx.source);
1046
+ const valueCode = transformNode(value, ctx);
1047
+ return `${targetCode} ${opText} ${valueCode}`;
1048
+ }
1028
1049
  function isSliceExpression(node) {
1029
1050
  const children = getChildren(node);
1030
1051
  return children.some((c) => c.name === ":");
@@ -1317,6 +1338,51 @@ function transformFormatString(node, ctx) {
1317
1338
  result += "`";
1318
1339
  return result;
1319
1340
  }
1341
+ function transformContinuedString(node, ctx) {
1342
+ const children = getChildren(node);
1343
+ const hasFormatString = children.some((c) => c.name === "FormatString");
1344
+ if (hasFormatString) {
1345
+ const parts = children.filter((c) => c.name === "String" || c.name === "FormatString").map((c) => {
1346
+ if (c.name === "FormatString") {
1347
+ return transformFormatString(c, ctx);
1348
+ } else {
1349
+ const text = getNodeText(c, ctx.source);
1350
+ let content;
1351
+ if (/^[rR]['"]/.test(text)) {
1352
+ content = text.slice(2, -1);
1353
+ } else if (/^[rR]"""/.test(text) || /^[rR]'''/.test(text)) {
1354
+ content = text.slice(4, -3);
1355
+ } else if (text.startsWith('"""') || text.startsWith("'''")) {
1356
+ content = text.slice(3, -3);
1357
+ } else {
1358
+ content = text.slice(1, -1);
1359
+ }
1360
+ return "`" + content.replace(/`/g, "\\`") + "`";
1361
+ }
1362
+ });
1363
+ return parts.join(" + ");
1364
+ } else {
1365
+ const parts = children.filter((c) => c.name === "String").map((c) => {
1366
+ const text = getNodeText(c, ctx.source);
1367
+ let content;
1368
+ if (/^[rR]['"]/.test(text)) {
1369
+ content = text.slice(2, -1);
1370
+ } else if (/^[rR]"""/.test(text) || /^[rR]'''/.test(text)) {
1371
+ content = text.slice(4, -3);
1372
+ } else if (text.startsWith('"""') || text.startsWith("'''")) {
1373
+ content = text.slice(3, -3);
1374
+ } else {
1375
+ content = text.slice(1, -1);
1376
+ }
1377
+ return content;
1378
+ });
1379
+ const joined = parts.join("");
1380
+ if (joined.includes('"') && !joined.includes("'")) {
1381
+ return "'" + joined + "'";
1382
+ }
1383
+ return '"' + joined + '"';
1384
+ }
1385
+ }
1320
1386
  function transformBoolean(node, ctx) {
1321
1387
  const text = getNodeText(node, ctx.source);
1322
1388
  return text === "True" ? "true" : "false";
@@ -3140,7 +3206,12 @@ function transformWithStatement(node, ctx) {
3140
3206
  body = child;
3141
3207
  break;
3142
3208
  }
3143
- if (child.name !== "as" && child.name !== ":" && child.name !== "VariableName") {
3209
+ if (child.name === "as" || child.name === ":") {
3210
+ i++;
3211
+ continue;
3212
+ }
3213
+ const isContextManagerExpr = child.name !== "VariableName" || children[i + 1]?.name === "as" || children[i + 1]?.name === "Body" || children[i + 1]?.name === ",";
3214
+ if (isContextManagerExpr) {
3144
3215
  const expr = child;
3145
3216
  let varName = null;
3146
3217
  const nextChild = children[i + 1];
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-TBFKEIAB.js";
4
+ } from "../chunk-2VKIPKNY.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-TBFKEIAB.js";
15
+ } from "./chunk-2VKIPKNY.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.3",
3
+ "version": "1.4.0",
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": {