python2ts 1.4.0 → 1.4.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.
|
@@ -931,6 +931,42 @@ function transformExpressionStatement(node, ctx) {
|
|
|
931
931
|
function transformAssignStatement(node, ctx) {
|
|
932
932
|
const children = getChildren(node);
|
|
933
933
|
if (children.length < 3) return getNodeText(node, ctx.source);
|
|
934
|
+
const assignOpIndices = children.map((c, i) => c.name === "AssignOp" || c.name === "=" ? i : -1).filter((i) => i !== -1);
|
|
935
|
+
if (assignOpIndices.length > 1) {
|
|
936
|
+
const targets2 = [];
|
|
937
|
+
const lastAssignOpIndex = assignOpIndices[assignOpIndices.length - 1];
|
|
938
|
+
if (lastAssignOpIndex === void 0) return getNodeText(node, ctx.source);
|
|
939
|
+
for (let i = 0; i < assignOpIndices.length; i++) {
|
|
940
|
+
const opIndex = assignOpIndices[i];
|
|
941
|
+
if (opIndex === void 0) continue;
|
|
942
|
+
const prevOpIndex = i > 0 ? assignOpIndices[i - 1] : -1;
|
|
943
|
+
const startIdx = prevOpIndex !== void 0 ? prevOpIndex + 1 : 0;
|
|
944
|
+
const targetNodes = children.slice(startIdx, opIndex).filter((c) => c.name !== ",");
|
|
945
|
+
if (targetNodes.length === 1 && targetNodes[0]) {
|
|
946
|
+
targets2.push(targetNodes[0]);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
const valueNodes = children.slice(lastAssignOpIndex + 1).filter((c) => c.name !== ",");
|
|
950
|
+
if (valueNodes.length !== 1 || !valueNodes[0]) return getNodeText(node, ctx.source);
|
|
951
|
+
const valueCode = transformNode(valueNodes[0], ctx);
|
|
952
|
+
const results = [];
|
|
953
|
+
let lastVarName = valueCode;
|
|
954
|
+
for (let i = targets2.length - 1; i >= 0; i--) {
|
|
955
|
+
const target = targets2[i];
|
|
956
|
+
if (!target) continue;
|
|
957
|
+
const targetCode = transformNode(target, ctx);
|
|
958
|
+
const varName = getNodeText(target, ctx.source);
|
|
959
|
+
let needsDeclaration = false;
|
|
960
|
+
if (target.name === "VariableName" && !isVariableDeclared(ctx, varName)) {
|
|
961
|
+
needsDeclaration = true;
|
|
962
|
+
declareVariable(ctx, varName);
|
|
963
|
+
}
|
|
964
|
+
const keyword = needsDeclaration ? "let " : "";
|
|
965
|
+
results.push(`${keyword}${targetCode} = ${lastVarName}`);
|
|
966
|
+
lastVarName = targetCode;
|
|
967
|
+
}
|
|
968
|
+
return results.join(";\n");
|
|
969
|
+
}
|
|
934
970
|
const assignOpIndex = children.findIndex((c) => c.name === "AssignOp" || c.name === "=");
|
|
935
971
|
if (assignOpIndex === -1) return getNodeText(node, ctx.source);
|
|
936
972
|
const typeDef = children.slice(0, assignOpIndex).find((c) => c.name === "TypeDef");
|
|
@@ -3762,6 +3798,45 @@ function transformClassMethodBody(node, ctx, skipFirst = false, predeclaredVars
|
|
|
3762
3798
|
function transformClassAssignment(node, ctx) {
|
|
3763
3799
|
const children = getChildren(node);
|
|
3764
3800
|
if (children.length < 3) return getNodeText(node, ctx.source);
|
|
3801
|
+
const assignOpIndices = children.map((c, i) => c.name === "AssignOp" || c.name === "=" ? i : -1).filter((i) => i !== -1);
|
|
3802
|
+
if (assignOpIndices.length > 1) {
|
|
3803
|
+
const chainTargets = [];
|
|
3804
|
+
const lastAssignOpIndex = assignOpIndices[assignOpIndices.length - 1];
|
|
3805
|
+
if (lastAssignOpIndex === void 0) return getNodeText(node, ctx.source);
|
|
3806
|
+
for (let i = 0; i < assignOpIndices.length; i++) {
|
|
3807
|
+
const opIndex = assignOpIndices[i];
|
|
3808
|
+
if (opIndex === void 0) continue;
|
|
3809
|
+
const prevOpIndex = i > 0 ? assignOpIndices[i - 1] : -1;
|
|
3810
|
+
const startIdx = prevOpIndex !== void 0 ? prevOpIndex + 1 : 0;
|
|
3811
|
+
const targetNodes = children.slice(startIdx, opIndex).filter((c) => c.name !== ",");
|
|
3812
|
+
if (targetNodes.length === 1 && targetNodes[0]) {
|
|
3813
|
+
chainTargets.push(targetNodes[0]);
|
|
3814
|
+
}
|
|
3815
|
+
}
|
|
3816
|
+
const valueNodes = children.slice(lastAssignOpIndex + 1).filter((c) => c.name !== ",");
|
|
3817
|
+
if (valueNodes.length !== 1 || !valueNodes[0]) return getNodeText(node, ctx.source);
|
|
3818
|
+
const valueCode = transformNode(valueNodes[0], ctx);
|
|
3819
|
+
const results = [];
|
|
3820
|
+
let lastVarName = valueCode;
|
|
3821
|
+
const indent = " ".repeat(ctx.indentLevel);
|
|
3822
|
+
for (let i = chainTargets.length - 1; i >= 0; i--) {
|
|
3823
|
+
const target = chainTargets[i];
|
|
3824
|
+
if (!target) continue;
|
|
3825
|
+
const targetCode = transformNode(target, ctx);
|
|
3826
|
+
const varName = getNodeText(target, ctx.source);
|
|
3827
|
+
const isMemberAssignment = target.name === "MemberExpression";
|
|
3828
|
+
let needsDeclaration = false;
|
|
3829
|
+
if (!isMemberAssignment && target.name === "VariableName" && !isVariableDeclared(ctx, varName)) {
|
|
3830
|
+
needsDeclaration = true;
|
|
3831
|
+
declareVariable(ctx, varName);
|
|
3832
|
+
}
|
|
3833
|
+
const keyword = needsDeclaration ? "let " : "";
|
|
3834
|
+
results.push(`${keyword}${targetCode} = ${lastVarName}`);
|
|
3835
|
+
lastVarName = targetCode;
|
|
3836
|
+
}
|
|
3837
|
+
return results.join(`;
|
|
3838
|
+
${indent}`);
|
|
3839
|
+
}
|
|
3765
3840
|
const assignOpIndex = children.findIndex((c) => c.name === "AssignOp" || c.name === "=");
|
|
3766
3841
|
if (assignOpIndex === -1) return getNodeText(node, ctx.source);
|
|
3767
3842
|
const targets = children.slice(0, assignOpIndex).filter((c) => c.name !== ",");
|
|
@@ -4635,15 +4710,40 @@ function parseComprehensionClauses(children, ctx) {
|
|
|
4635
4710
|
continue;
|
|
4636
4711
|
}
|
|
4637
4712
|
if (item.name === "for" || item.name === "Keyword" && getNodeText(item, ctx.source) === "for") {
|
|
4638
|
-
|
|
4639
|
-
|
|
4640
|
-
|
|
4713
|
+
let inIndex = -1;
|
|
4714
|
+
for (let j = i + 1; j < items.length; j++) {
|
|
4715
|
+
const candidate = items[j];
|
|
4716
|
+
if (candidate && (candidate.name === "in" || candidate.name === "Keyword" && getNodeText(candidate, ctx.source) === "in")) {
|
|
4717
|
+
inIndex = j;
|
|
4718
|
+
break;
|
|
4719
|
+
}
|
|
4720
|
+
}
|
|
4721
|
+
if (inIndex === -1) {
|
|
4722
|
+
i++;
|
|
4723
|
+
continue;
|
|
4724
|
+
}
|
|
4725
|
+
const varNodes = [];
|
|
4726
|
+
for (let j = i + 1; j < inIndex; j++) {
|
|
4727
|
+
const varCandidate = items[j];
|
|
4728
|
+
if (varCandidate && varCandidate.name !== ",") {
|
|
4729
|
+
varNodes.push(varCandidate);
|
|
4730
|
+
}
|
|
4731
|
+
}
|
|
4732
|
+
const iterableNode = items[inIndex + 1];
|
|
4733
|
+
if (varNodes.length > 0 && iterableNode) {
|
|
4734
|
+
let variable;
|
|
4735
|
+
const firstVarNode = varNodes[0];
|
|
4736
|
+
if (varNodes.length === 1 && firstVarNode) {
|
|
4737
|
+
variable = transformNode(firstVarNode, ctx);
|
|
4738
|
+
} else {
|
|
4739
|
+
variable = "[" + varNodes.map((v) => transformNode(v, ctx)).join(", ") + "]";
|
|
4740
|
+
}
|
|
4641
4741
|
clauses.push({
|
|
4642
4742
|
type: "for",
|
|
4643
|
-
variable
|
|
4743
|
+
variable,
|
|
4644
4744
|
iterable: transformNode(iterableNode, ctx)
|
|
4645
4745
|
});
|
|
4646
|
-
i
|
|
4746
|
+
i = inIndex + 2;
|
|
4647
4747
|
} else {
|
|
4648
4748
|
i++;
|
|
4649
4749
|
}
|
package/dist/cli/index.js
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "python2ts",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.1",
|
|
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": {
|