From d5428fe650c8efa4f3fe84b4e2f9c54a4cfa837c Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Mon, 11 Oct 2021 10:03:42 +0200 Subject: [PATCH 1/2] fix(rosetta): fix translation of `!` and `===` operators These were translated as `~` and not translated at all, respectively. Fixes #2993. --- .../jsii-rosetta/lib/languages/default.ts | 25 ++++++++++++------- packages/jsii-rosetta/lib/languages/python.ts | 7 ++++++ .../expressions/prefix_unary_expression.cs | 1 - .../expressions/prefix_unary_expression.java | 1 - .../expressions/prefix_unary_expression.py | 1 - .../expressions/prefix_unary_expression.ts | 1 - .../expressions/unary_and_binary_operators.cs | 3 +++ .../unary_and_binary_operators.java | 3 +++ .../expressions/unary_and_binary_operators.py | 3 +++ .../expressions/unary_and_binary_operators.ts | 3 +++ 10 files changed, 35 insertions(+), 13 deletions(-) delete mode 100644 packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.cs delete mode 100644 packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.java delete mode 100644 packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.py delete mode 100644 packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.ts create mode 100644 packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.cs create mode 100644 packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.java create mode 100644 packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.py create mode 100644 packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.ts diff --git a/packages/jsii-rosetta/lib/languages/default.ts b/packages/jsii-rosetta/lib/languages/default.ts index d3de4ec2fe..c86058770c 100644 --- a/packages/jsii-rosetta/lib/languages/default.ts +++ b/packages/jsii-rosetta/lib/languages/default.ts @@ -63,19 +63,26 @@ export abstract class DefaultVisitor implements AstHandler { } public binaryExpression(node: ts.BinaryExpression, context: AstRenderer): OTree { - return new OTree([ - context.convert(node.left), - ' ', - context.textOf(node.operatorToken), - ' ', - context.convert(node.right), - ]); + const operatorToken = this.translateBinaryOperator(context.textOf(node.operatorToken)); + return new OTree([context.convert(node.left), ' ', operatorToken, ' ', context.convert(node.right)]); } public prefixUnaryExpression(node: ts.PrefixUnaryExpression, context: AstRenderer): OTree { - return new OTree([UNARY_OPS[node.operator], context.convert(node.operand)]); + return new OTree([this.translateUnaryOperator(node.operator), context.convert(node.operand)]); } + public translateUnaryOperator(operator: ts.PrefixUnaryOperator) { + return UNARY_OPS[operator]; + } + + public translateBinaryOperator(operator: string) { + if (operator === '===') { + return '=='; + } + return operator; + } + + public ifStatement(node: ts.IfStatement, context: AstRenderer): OTree { return this.notImplemented(node, context); } @@ -329,7 +336,7 @@ const UNARY_OPS: { [op in ts.PrefixUnaryOperator]: string } = { [ts.SyntaxKind.PlusToken]: '+', [ts.SyntaxKind.MinusToken]: '-', [ts.SyntaxKind.TildeToken]: '~', - [ts.SyntaxKind.ExclamationToken]: '~', + [ts.SyntaxKind.ExclamationToken]: '!', }; function findEnclosingCallExpression(node?: ts.Node): ts.CallLikeExpression | undefined { diff --git a/packages/jsii-rosetta/lib/languages/python.ts b/packages/jsii-rosetta/lib/languages/python.ts index 94b4b494e2..fd81dd0bc4 100644 --- a/packages/jsii-rosetta/lib/languages/python.ts +++ b/packages/jsii-rosetta/lib/languages/python.ts @@ -353,6 +353,13 @@ export class PythonVisitor extends DefaultVisitor { return this.renderObjectLiteralExpression('{', '}', false, node, context); } + public translateUnaryOperator(operator: ts.PrefixUnaryOperator) { + if (operator === ts.SyntaxKind.ExclamationToken) { + return 'not '; + } + return super.translateUnaryOperator(operator); + } + public renderObjectLiteralExpression( prefix: string, suffix: string, diff --git a/packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.cs b/packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.cs deleted file mode 100644 index 4de303a857..0000000000 --- a/packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.cs +++ /dev/null @@ -1 +0,0 @@ -Console.WriteLine(-3); \ No newline at end of file diff --git a/packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.java b/packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.java deleted file mode 100644 index 572f800c15..0000000000 --- a/packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.java +++ /dev/null @@ -1 +0,0 @@ -System.out.println(-3); diff --git a/packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.py b/packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.py deleted file mode 100644 index 5e293fbf99..0000000000 --- a/packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.py +++ /dev/null @@ -1 +0,0 @@ -print(-3) \ No newline at end of file diff --git a/packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.ts b/packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.ts deleted file mode 100644 index 381a20f076..0000000000 --- a/packages/jsii-rosetta/test/translations/expressions/prefix_unary_expression.ts +++ /dev/null @@ -1 +0,0 @@ -console.log(-3); \ No newline at end of file diff --git a/packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.cs b/packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.cs new file mode 100644 index 0000000000..526e07e929 --- /dev/null +++ b/packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.cs @@ -0,0 +1,3 @@ +Console.WriteLine(-3); +Console.WriteLine(!false); +Console.WriteLine(a == b); \ No newline at end of file diff --git a/packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.java b/packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.java new file mode 100644 index 0000000000..be9673272d --- /dev/null +++ b/packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.java @@ -0,0 +1,3 @@ +System.out.println(-3); +System.out.println(!false); +System.out.println(a == b); diff --git a/packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.py b/packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.py new file mode 100644 index 0000000000..dd1b9534b2 --- /dev/null +++ b/packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.py @@ -0,0 +1,3 @@ +print(-3) +print(not False) +print(a == b) \ No newline at end of file diff --git a/packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.ts b/packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.ts new file mode 100644 index 0000000000..73badb8f0b --- /dev/null +++ b/packages/jsii-rosetta/test/translations/expressions/unary_and_binary_operators.ts @@ -0,0 +1,3 @@ +console.log(-3); +console.log(!false); +console.log(a === b); \ No newline at end of file From 4359fe1fd73693bc662117ec9f96bb023a77540e Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Mon, 11 Oct 2021 10:55:18 +0200 Subject: [PATCH 2/2] Fix eslint --- packages/jsii-rosetta/lib/languages/default.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jsii-rosetta/lib/languages/default.ts b/packages/jsii-rosetta/lib/languages/default.ts index c86058770c..842905ce17 100644 --- a/packages/jsii-rosetta/lib/languages/default.ts +++ b/packages/jsii-rosetta/lib/languages/default.ts @@ -82,7 +82,6 @@ export abstract class DefaultVisitor implements AstHandler { return operator; } - public ifStatement(node: ts.IfStatement, context: AstRenderer): OTree { return this.notImplemented(node, context); } @@ -160,6 +159,7 @@ export abstract class DefaultVisitor implements AstHandler { return this.unknownTypeObjectLiteralExpression(node, context); } if (isKnownStruct) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion return this.knownStructObjectLiteralExpression(node, type!, context); } return this.keyValueObjectLiteralExpression(node, type && mapElementType(type, context), context);