Skip to content
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
5 changes: 4 additions & 1 deletion src/baseTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { IFileImport, IFileExport, TranspilationError, IMethodType, IParameterType } from './types.js';
import { unCamelCase } from "./utils.js";
import { Logger } from "./logger.js";
import { timingSafeEqual } from 'crypto';

Check warning on line 5 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'timingSafeEqual' is defined but never used
class BaseTranspiler {

NUM_LINES_BETWEEN_CLASS_MEMBERS = 1;
Expand Down Expand Up @@ -358,7 +358,7 @@

let method = undefined;

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

Check warning on line 361 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 @@ -375,13 +375,13 @@
if (ts.isMethodDeclaration(elem)) {

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

Check warning on line 378 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 384 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected any. Specify a different type
}


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

getBlockOpen(identation){

Check warning on line 397 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 @@ -459,11 +459,11 @@
return this.getIden(identation) + `${left} instanceof ${right}`;
}

getCustomOperatorIfAny(left, right, operator) {

Check warning on line 462 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'operator' is defined but never used

Check warning on line 462 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 462 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

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

printCustomBinaryExpressionIfAny(node, identation) {

Check warning on line 466 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'identation' is defined but never used

Check warning on line 466 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

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

Expand Down Expand Up @@ -962,13 +962,16 @@
if (text in this.StringLiteralReplacements) {
return this.StringLiteralReplacements[text];
}
// trick to preserve valid backslashes (e.g. \n, \t)
const backslashPlaceholder = "\x00";
text = text.replaceAll("\\", backslashPlaceholder);
text = text.replaceAll("\b", "\\b");
text = text.replaceAll("\f", "\\f");
text = text.replaceAll("\n", "\\n");
text = text.replaceAll("\r", "\\r");
text = text.replaceAll("\t", "\\t");
text = text.replaceAll(backslashPlaceholder, "\\\\");
if (token === "'") {
text = text.replaceAll("\\\"", "\""); // unscape double quotes
text = text.replaceAll("'", "\\'"); // escape single quotes
} else if (token === "\"") {
text = text.replaceAll("\"", "\\\""); // escape double quotes
Expand Down
12 changes: 10 additions & 2 deletions src/goTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,17 @@ export class GoTranspiler extends BaseTranspiler {
if (text in this.StringLiteralReplacements) {
return this.StringLiteralReplacements[text];
}
text = text.replaceAll("'", "\\\\" + "'");
text = text.replaceAll("\"", "\\" + "\"");
// Preserve real backslashes
const backslashPlaceholder = "\x00";
text = text.replaceAll("\\", backslashPlaceholder);
text = text.replaceAll("\b", "\\b");
text = text.replaceAll("\f", "\\f");
text = text.replaceAll("\n", "\\n");
text = text.replaceAll("\r", "\\r");
text = text.replaceAll("\t", "\\t");
text = text.replaceAll(backslashPlaceholder, "\\\\");
text = text.replaceAll("'", "\\'");
text = text.replaceAll("\"", "\\\"");
return token + text + token;
}

Expand Down
13 changes: 9 additions & 4 deletions src/rustTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,16 @@ export class RustTranspiler extends BaseTranspiler {
if (text in this.StringLiteralReplacements) {
return this.StringLiteralReplacements[text];
}
text = text.replaceAll('\\', '\\\\');
// Preserve real backslashes
const backslashPlaceholder = "\x00";
text = text.replaceAll("\\", backslashPlaceholder);
text = text.replaceAll("\b", "\\b");
text = text.replaceAll("\f", "\\f");
text = text.replaceAll("\n", "\\n");
text = text.replaceAll("\r", "\\r");
text = text.replaceAll("\t", "\\t");
text = text.replaceAll(backslashPlaceholder, "\\\\");
text = text.replaceAll('"', '\\"');
text = text.replaceAll('\n', '\\n');
text = text.replaceAll('\r', '\\r');
text = text.replaceAll('\t', '\\t');
return `Value::Str("${text}".to_string())`;
}

Expand Down
6 changes: 6 additions & 0 deletions tests/pythonTranspiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,12 @@ describe('python tests', () => {
const output = transpiler.transpilePython(ts).content;
expect(output).toBe(python);
});
test('string literal with backslash escapes', () => {
const ts = "const string2 = '{\"k\":123.1,\"k2\":\"{\\\\\"k3\\\\\":456}\"}';";
const python = "string2 = '{\"k\":123.1,\"k2\":\"{\\\\\"k3\\\\\":456}\"}'";
const output = transpiler.transpilePython(ts).content;
expect(output).toBe(python);
});
test('should convert isArray', () => {
const ts = "Array.isArray(x)";
const result = "isinstance(x, list)";
Expand Down
Loading