Skip to content

Commit

Permalink
Merge pull request #14 from ttodua/access-modifiers
Browse files Browse the repository at this point in the history
access modifiers
  • Loading branch information
carlosmiei authored Sep 28, 2024
2 parents f249654 + 897505d commit a5c4fe9
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/baseTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class BaseTranspiler {
ArgTypeReplacements = {};

FuncModifiers = {};
defaultPropertyAccess = 'public';

uncamelcaseIdentifiers;
asyncTranspiling;
Expand Down Expand Up @@ -1694,8 +1695,7 @@ class BaseTranspiler {
}

printPropertyDeclaration(node, identation) {
let modifiers = this.printModifiers(node);
modifiers = modifiers ? modifiers + " " : modifiers;
const modifiers = this.printPropertyAccessModifiers(node);
const name = this.printNode(node.name, 0);
if (node.initializer) {
const initializer = this.printNode(node.initializer, 0);
Expand All @@ -1704,6 +1704,12 @@ class BaseTranspiler {
return this.getIden(identation) + modifiers + name + this.LINE_TERMINATOR;
}

printPropertyAccessModifiers (node) {
let modifiers = this.printModifiers(node);
modifiers = modifiers ? modifiers + " " : modifiers;
return modifiers;
}

printSpreadElement(node, identation) {
const expression = this.printNode(node.expression, 0);
return this.getIden(identation) + this.SPREAD_TOKEN + expression;
Expand Down
24 changes: 24 additions & 0 deletions src/csharpTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export class CSharpTranspiler extends BaseTranspiler {
'Dict': 'Dictionary<string, object>',
'Strings': 'List<string>',
'List': 'List<object>',
'boolean': 'bool',
};

this.ArgTypeReplacements = {
Expand All @@ -139,6 +140,7 @@ export class CSharpTranspiler extends BaseTranspiler {
'Dict': 'Dictionary<string, object>',
'Strings': 'List<string>',
'List': 'List<object>',
'boolean': 'bool',
};

this.binaryExpressionsWrappers = {
Expand Down Expand Up @@ -1058,6 +1060,28 @@ export class CSharpTranspiler extends BaseTranspiler {
// return this.getIden(identation) + this.THROW_TOKEN + throwExpression + this.LINE_TERMINATOR;
}

csModifiers = {

};

printPropertyAccessModifiers(node) {
let modifiers = this.printModifiers(node);
if (modifiers === '') {
modifiers = this.defaultPropertyAccess;
}
// add type
let typeText = 'object';
if (node.type) {
typeText = this.getType(node);
if (!typeText) {
if (node.type.kind === ts.SyntaxKind.AnyKeyword) {
typeText = this.OBJECT_KEYWORD + ' ';
}
}
}
return modifiers + ' ' + typeText + ' ';
}

// printLeadingComments(node, identation) {
// const fullText = global.src.getFullText();
// const commentsRangeList = ts.getLeadingCommentRanges(fullText, node.pos);
Expand Down
4 changes: 4 additions & 0 deletions src/goTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ export class GoTranspiler extends BaseTranspiler {
return struct + "\n" + classMethods;
}

printPropertyAccessModifiers (node) {
return "";
}

printMethodDeclaration(node, identation) {

const className = node.parent.name.escapedText;
Expand Down
5 changes: 5 additions & 0 deletions src/phpTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ export class PhpTranspiler extends BaseTranspiler {
return super.printFunctionBody(node, identation);
}

printPropertyAccessModifiers(node) {
const modifiers = super.printPropertyAccessModifiers(node);
return modifiers ? modifiers : "public "; // default to public
}

transformLeadingComment(comment) {
const commentRegex = [
[ /\{([\]\[\|a-zA-Z0-9_-]+?)\}/g, '~$1~' ], // eslint-disable-line -- resolve the "arrays vs url params" conflict (both are in {}-brackets)
Expand Down
4 changes: 4 additions & 0 deletions src/pythonTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ export class PythonTranspiler extends BaseTranspiler {
return this.printNodeCommentsIfAny(node, identation, forStm);
}

printPropertyAccessModifiers(node) {
return ""; // no access modifier in python
}

transformLeadingComment(comment) {
const commentRegex = [
[ /(^|\s)\/\//g, '$1#' ], // regular comments
Expand Down
29 changes: 29 additions & 0 deletions tests/csharpTranspiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,35 @@ describe('csharp transpiling tests', () => {
const output = transpiler.transpileCSharp(ts).content;
expect(output).toBe(csharp);
});
test('basic class declaration with props', () => {
const ts =
"class MyClass {\n" +
" public x: number = 10;\n" +
" public y: string = \"test\";\n" +
" public z1: string[] = [ 'a', 'b' ];\n" +
" public z2: any = whatever;\n" +
" public z3: any = {};\n" +
" mainFeature(message): void {\n" +
" console.log(\"Hello! I'm inside main class:\" + message)\n" +
" }\n" +
"}";
const cs =
"class MyClass\n" +
"{\n" +
" public object x = 10;\n" +
" public string y = \"test\";\n" +
" public List<object> z1 = new List<object>() {\"a\", \"b\"};\n" +
" public Dictionary<string, object> z2 = whatever;\n" +
" public Dictionary<string, object> z3 = new Dictionary<string, object>() {};\n" +
"\n" +
" public virtual void mainFeature(object message)\n" +
" {\n" +
" Console.WriteLine(add(\"Hello! I'm inside main class:\", message));\n" +
" }\n" +
"}";
const output = transpiler.transpileCSharp(ts).content;
expect(output).toBe(cs);
});
// test('basic basic declaration with default parameters', () => {
// const ts =
// "class T {\n" +
Expand Down
1 change: 1 addition & 0 deletions tests/integration/source/transpilable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

class Second {

public stringifyNumber(arg: number) {
return arg.toString();
}
Expand Down
6 changes: 6 additions & 0 deletions tests/phpTranspiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ describe('php transpiling tests', () => {
"class MyClass {\n" +
" public static x: number = 10;\n" +
" public static y: string = \"test\";\n" +
" public static a1: string[] = [ 'a', 'b' ];\n" +
" public static a2: any = whatever;\n" +
" public static a3: any = {};\n" +
" mainFeature(message) {\n" +
" console.log(\"Hello! I'm inside main class:\" + message)\n" +
" }\n" +
Expand All @@ -308,6 +311,9 @@ describe('php transpiling tests', () => {
"class MyClass {\n" +
" public static $x = 10;\n" +
" public static $y = 'test';\n" +
" public static $a1 = ['a', 'b'];\n" +
" public static $a2 = whatever;\n" +
" public static $a3 = array();\n" +
"\n" +
" public function mainFeature($message) {\n" +
" var_dump('Hello! I\\'m inside main class:' . $message);\n" +
Expand Down
8 changes: 7 additions & 1 deletion tests/pythonTranspiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,13 @@ describe('python tests', () => {
expect(output).toBe(python);
});
test('basic class declaration with props', () => {
const ts =
const ts =
"class MyClass {\n" +
" public static x: number = 10;\n" +
" public static y: string = \"test\";\n" +
" public static a1: string[] = [ 'a', 'b' ];\n" +
" public static a2: any = whatever;\n" +
" public static a3: any = {};\n" +
" mainFeature(message) {\n" +
" console.log(\"Hello! I'm inside main class:\" + message)\n" +
" }\n" +
Expand All @@ -257,6 +260,9 @@ describe('python tests', () => {
"class MyClass:\n" +
" x = 10\n" +
" y = 'test'\n" +
" a1 = ['a', 'b']\n" +
" a2 = whatever\n" +
" a3 = {}\n" +
"\n" +
" def mainFeature(self, message):\n" +
" print('Hello! I\\'m inside main class:' + message)"
Expand Down

0 comments on commit a5c4fe9

Please sign in to comment.