mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 07:19:22 +00:00
add parenthesis for binary/unary operator arguments when necessary
This commit is contained in:
parent
1b4948e625
commit
e84a1b0bae
@ -4949,10 +4949,12 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void printBinaryRightOperand(JCBinary binary) {
|
protected void printBinaryRightOperand(JCBinary binary) {
|
||||||
|
addInlinedExpression(binary.rhs);
|
||||||
print(binary.rhs);
|
print(binary.rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void printBinaryLeftOperand(JCBinary binary) {
|
protected void printBinaryLeftOperand(JCBinary binary) {
|
||||||
|
addInlinedExpression(binary.lhs);
|
||||||
print(binary.lhs);
|
print(binary.lhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -5272,6 +5274,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
@Override
|
@Override
|
||||||
public void visitUnary(JCUnary unary) {
|
public void visitUnary(JCUnary unary) {
|
||||||
if (!getAdapter().substituteUnaryOperator(new UnaryOperatorElementSupport(unary))) {
|
if (!getAdapter().substituteUnaryOperator(new UnaryOperatorElementSupport(unary))) {
|
||||||
|
addInlinedExpression(unary.arg);
|
||||||
if (!inRollback) {
|
if (!inRollback) {
|
||||||
JCStatement statement = null;
|
JCStatement statement = null;
|
||||||
VarSymbol[] staticInitializedField = { null };
|
VarSymbol[] staticInitializedField = { null };
|
||||||
|
|||||||
@ -1289,8 +1289,14 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
|
|||||||
if (types().isSameType(invocationElement.getTargetExpression().getType(),
|
if (types().isSameType(invocationElement.getTargetExpression().getType(),
|
||||||
util().getType(String.class))
|
util().getType(String.class))
|
||||||
|| util().isNumber(invocationElement.getTargetExpression().getType())) {
|
|| util().isNumber(invocationElement.getTargetExpression().getType())) {
|
||||||
|
if(isInlinedExpression(invocationElement)) {
|
||||||
|
print("(");
|
||||||
|
}
|
||||||
print(invocationElement.getTargetExpression()).print(" === ")
|
print(invocationElement.getTargetExpression()).print(" === ")
|
||||||
.print(invocationElement.getArgument(0));
|
.print(invocationElement.getArgument(0));
|
||||||
|
if(isInlinedExpression(invocationElement)) {
|
||||||
|
print(")");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
printMacroName(targetMethodName);
|
printMacroName(targetMethodName);
|
||||||
print("(<any>((o1: any, o2: any) => { if(o1 && o1.equals) { return o1.equals(o2); } else { return o1 === o2; } })(");
|
print("(<any>((o1: any, o2: any) => { if(o1 && o1.equals) { return o1.equals(o2); } else { return o1 === o2; } })(");
|
||||||
|
|||||||
@ -1177,4 +1177,16 @@ public class PrinterAdapter {
|
|||||||
public final String getHeader(String key) {
|
public final String getHeader(String key) {
|
||||||
return context.getHeader(key);
|
return context.getHeader(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tells if this element is an inlined expression. An inlined expression
|
||||||
|
* typically requires parenthesis (on contrary to top-level statement for instance).
|
||||||
|
*
|
||||||
|
* @param element the element to check
|
||||||
|
* @return true if an inlined expression
|
||||||
|
*/
|
||||||
|
public final boolean isInlinedExpression(ExtendedElement element) {
|
||||||
|
return printer.isInlinedExpression(((ExtendedElementSupport<?>) element).getTree());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.jsweet.transpiler.util;
|
package org.jsweet.transpiler.util;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
@ -51,6 +53,30 @@ public abstract class AbstractTreePrinter extends AbstractTreeScanner {
|
|||||||
|
|
||||||
private Stack<Position> positionStack = new Stack<>();
|
private Stack<Position> positionStack = new Stack<>();
|
||||||
|
|
||||||
|
private Set<JCTree> inlinedExpressions = new HashSet<JCTree>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tells if the given expression is an inlined expression
|
||||||
|
* (and would require parenthesis).
|
||||||
|
*/
|
||||||
|
public boolean isInlinedExpression(JCTree tree) {
|
||||||
|
return this.inlinedExpressions.contains(tree);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an inlined expression.
|
||||||
|
*/
|
||||||
|
public void addInlinedExpression(JCTree tree) {
|
||||||
|
this.inlinedExpressions.add(tree);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears the inlined expressions.
|
||||||
|
*/
|
||||||
|
public void clearInlinedExpression() {
|
||||||
|
this.inlinedExpressions.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A footer to be printed at the end of the output.
|
* A footer to be printed at the end of the output.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user