Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transpiler): add replaceAll support #20

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/baseTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@

let method = undefined;

let parentClass = (ts as any).getAllSuperTypeNodes(node.parent)[0];

Check warning on line 341 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected any. Specify a different type

while (parentClass !== undefined) {
const parentClassType = global.checker.getTypeAtLocation(parentClass);
Expand All @@ -355,13 +355,13 @@
if (ts.isMethodDeclaration(elem)) {

const name = elem.name.getText().trim();
if ((node as any).name.escapedText === name) {

Check warning on line 358 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected any. Specify a different type
method = elem;
}
}
});

parentClass = (ts as any).getAllSuperTypeNodes(parentClassDecl)[0] ?? undefined;

Check warning on line 364 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected any. Specify a different type
}


Expand All @@ -374,7 +374,7 @@
return this.DEFAULT_IDENTATION.repeat(parseInt(num));
}

getBlockOpen(identation){

Check warning on line 377 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'identation' is defined but never used
return this.SPACE_BEFORE_BLOCK_OPENING + this.BLOCK_OPENING_TOKEN + "\n";
}

Expand Down Expand Up @@ -439,11 +439,11 @@
return this.getIden(identation) + `${left} instanceof ${right}`;
}

getCustomOperatorIfAny(left, right, operator) {

Check warning on line 442 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'left' is defined but never used

Check warning on line 442 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'right' is defined but never used

Check warning on line 442 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'operator' is defined but never used
return undefined;
}

printCustomBinaryExpressionIfAny(node, identation) {

Check warning on line 446 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'node' is defined but never used

Check warning on line 446 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'identation' is defined but never used
return undefined; // stub to override
}

Expand Down Expand Up @@ -491,7 +491,7 @@
return leftVar +" "+ operator + " " + rightVar.trim();
}

transformPropertyAcessExpressionIfNeeded (node) {

Check warning on line 494 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'node' is defined but never used
return undefined;
}

Expand Down Expand Up @@ -1118,6 +1118,10 @@
return undefined; // stub
}

printReplaceAllCall(node, identation, name = undefined, parsedArg = undefined, parsedArg2 = undefined) {
return undefined; // stub
}

printToStringCall(node, identation, name = undefined) {
return undefined; // stub
}
Expand Down Expand Up @@ -1264,6 +1268,8 @@
return this.printSliceCall(node, identation, name, parsedArg, parsedArg2);
case 'replace':
return this.printReplaceCall(node, identation, name, parsedArg, parsedArg2);
case 'replaceAll':
return this.printReplaceAllCall(node, identation, name, parsedArg, parsedArg2);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/csharpTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,10 @@ export class CSharpTranspiler extends BaseTranspiler {
return `((string)${name}).Replace((string)${parsedArg}, (string)${parsedArg2})`;
}

printReplaceAllCall(node, identation, name = undefined, parsedArg = undefined, parsedArg2 = undefined) {
return `((string)${name}).Replace((string)${parsedArg}, (string)${parsedArg2})`;
}

printPadEndCall(node, identation, name, parsedArg, parsedArg2) {
return `(${name} as String).PadRight(Convert.ToInt32(${parsedArg}), Convert.ToChar(${parsedArg2}))`;
}
Expand Down
4 changes: 4 additions & 0 deletions src/goTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,10 @@ ${this.getIden(identation)}return ${rightPart}`;
return `Replace(${name}, ${parsedArg}, ${parsedArg2})`;
}

printReplaceAllCall(node, identation, name = undefined, parsedArg = undefined, parsedArg2 = undefined) {
return `Replace(${name}, ${parsedArg}, ${parsedArg2})`;
}

printPadEndCall(node, identation, name, parsedArg, parsedArg2) {
return `PadEnd(${name}, ${parsedArg}, ${parsedArg2})`;
}
Expand Down
4 changes: 4 additions & 0 deletions src/phpTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ export class PhpTranspiler extends BaseTranspiler {
return `str_replace(${parsedArg}, ${parsedArg2}, ${name})`;
}

printReplaceAllCall(node, identation, name = undefined, parsedArg = undefined, parsedArg2 = undefined) {
return `str_replace(${parsedArg}, ${parsedArg2}, ${name})`;
}

printIncludesCall(node, identation, name = undefined, parsedArg = undefined) {
// "ol".includes("o") -> str_contains("ol", "o") or [12,3,4].includes(3) -> in_array(3, [12,3,4])
const leftSide = node.expression?.expression;
Expand Down
4 changes: 4 additions & 0 deletions src/pythonTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ export class PythonTranspiler extends BaseTranspiler {
return `${name}.replace(${parsedArg}, ${parsedArg2})`;
}

printReplaceAllCall(node: any, identation: any, name?: any, parsedArg?: any, parsedArg2?: any) {
return `${name}.replace(${parsedArg}, ${parsedArg2})`;
}

printElementAccessExpressionExceptionIfAny(node) {
if (node.expression.kind === SyntaxKind.ThisKeyword) {
return "getattr(self, " + this.printNode(node.argumentExpression, 0) + ")";
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/source/transpilable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class Test {
const both = firstConcat.concat(secondConcat);
console.log(both.length); // should print 4
console.log(both[2]); // should print "c"

const baseString = "aabba";
const replacedAllString = baseString.replaceAll("a", "");
console.log(replacedAllString); // should print "bb"
}
}

Expand Down
Loading