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

fix(rosetta): fix translation of ! and === operators #3052

Merged
merged 4 commits into from
Oct 11, 2021
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
25 changes: 16 additions & 9 deletions packages/jsii-rosetta/lib/languages/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,23 @@ export abstract class DefaultVisitor<C> implements AstHandler<C> {
}

public binaryExpression(node: ts.BinaryExpression, context: AstRenderer<C>): 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<C>): 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<C>): OTree {
Expand Down Expand Up @@ -153,6 +159,7 @@ export abstract class DefaultVisitor<C> implements AstHandler<C> {
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.typeChecker), context);
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions packages/jsii-rosetta/lib/languages/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ export class PythonVisitor extends DefaultVisitor<PythonLanguageContext> {
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,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Console.WriteLine(-3);
Console.WriteLine(!false);
Console.WriteLine(a == b);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
System.out.println(-3);
System.out.println(!false);
System.out.println(a == b);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
print(-3)
print(not False)
print(a == b)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log(-3);
console.log(!false);
console.log(a === b);