fix(ivy): ngtsc - do not wrap arguments unnecessarily (#30349)

Previously we defensively wrapped expressions in case they ran afoul of
precedence rules. For example, it would be easy to create the TS AST structure
Call(Ternary(a, b, c)), but might result in printed code of:

```
a ? b : c()
```

Whereas the actual structure we meant to generate is:

```
(a ? b : c)()
```

However the TypeScript renderer appears to be clever enough to provide
parenthesis as necessary.

This commit removes these defensive paraenthesis in the cases of binary
and ternary operations.

FW-1273

PR Close #30349
This commit is contained in:
Pete Bacon Darwin
2019-05-09 11:23:30 +01:00
committed by Jason Aden
parent 0937062a64
commit eda09e69ea
6 changed files with 48 additions and 58 deletions

View File

@ -264,10 +264,10 @@ class ExpressionTranslatorVisitor implements ExpressionVisitor, StatementVisitor
}
}
visitConditionalExpr(ast: ConditionalExpr, context: Context): ts.ParenthesizedExpression {
return ts.createParen(ts.createConditional(
visitConditionalExpr(ast: ConditionalExpr, context: Context): ts.ConditionalExpression {
return ts.createConditional(
ast.condition.visitExpression(this, context), ast.trueCase.visitExpression(this, context),
ast.falseCase !.visitExpression(this, context)));
ast.falseCase !.visitExpression(this, context));
}
visitNotExpr(ast: NotExpr, context: Context): ts.PrefixUnaryExpression {
@ -296,10 +296,9 @@ class ExpressionTranslatorVisitor implements ExpressionVisitor, StatementVisitor
if (!BINARY_OPERATORS.has(ast.operator)) {
throw new Error(`Unknown binary operator: ${BinaryOperator[ast.operator]}`);
}
const binEx = ts.createBinary(
return ts.createBinary(
ast.lhs.visitExpression(this, context), BINARY_OPERATORS.get(ast.operator) !,
ast.rhs.visitExpression(this, context));
return ast.parens ? ts.createParen(binEx) : binEx;
}
visitReadPropExpr(ast: ReadPropExpr, context: Context): ts.PropertyAccessExpression {
@ -513,12 +512,3 @@ export class TypeTranslatorVisitor implements ExpressionVisitor, TypeVisitor {
return ts.createTypeQueryNode(expr as ts.Identifier);
}
}
function entityNameToExpr(entity: ts.EntityName): ts.Expression {
if (ts.isIdentifier(entity)) {
return entity;
}
const {left, right} = entity;
const leftExpr = ts.isIdentifier(left) ? left : entityNameToExpr(left);
return ts.createPropertyAccess(leftExpr, right);
}