From 4da09b73bc4e84efdf1017481c30b8e427172f38 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Wed, 6 Apr 2016 15:49:53 -0700 Subject: [PATCH] Fixes extra parens around yield downleveled from await --- src/compiler/factory.ts | 14 +++++++++++--- src/compiler/transformers/ts.ts | 2 +- .../reference/FunctionDeclaration10_es6.js | 2 +- tests/baselines/reference/classExpressionES63.js | 8 ++++---- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 6fbc4f13037..573679b4f1a 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -1272,10 +1272,19 @@ namespace ts { // If `a ** d` is on the left of operator `**`, we need to parenthesize to preserve // the intended order of operations: `(a ** b) ** c` const binaryOperatorPrecedence = getOperatorPrecedence(SyntaxKind.BinaryExpression, binaryOperator); + const binaryOperatorAssociativity = getOperatorAssociativity(SyntaxKind.BinaryExpression, binaryOperator); const emittedOperand = skipPartiallyEmittedExpressions(operand); const operandPrecedence = getExpressionPrecedence(emittedOperand); switch (compareValues(operandPrecedence, binaryOperatorPrecedence)) { case Comparison.LessThan: + // If the operand is the right side of a right-associative binary operation + // and is a yield expression, then we do not need parentheses. + if (!isLeftSideOfBinary + && binaryOperatorAssociativity === Associativity.Right + && operand.kind === SyntaxKind.YieldExpression) { + return false; + } + return true; case Comparison.GreaterThan: @@ -1287,12 +1296,11 @@ namespace ts { // left associative: // (a*b)/x -> a*b/x // (a**b)/x -> a**b/x - + // // Parentheses are needed for the left operand when the binary operator is // right associative: // (a/b)**x -> (a/b)**x // (a**b)**x -> (a**b)**x - const binaryOperatorAssociativity = getOperatorAssociativity(SyntaxKind.BinaryExpression, binaryOperator); return binaryOperatorAssociativity === Associativity.Right; } else { @@ -1326,7 +1334,7 @@ namespace ts { // associative: // x/(a**b) -> x/a**b // x**(a**b) -> x**a**b - + // // Parentheses are needed for the right operand when the operand is left // associative: // x/(a*b) -> x/(a*b) diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 182ea518d30..1d8dd46edee 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -748,7 +748,7 @@ namespace ts { function transformConstructorParameters(constructor: ConstructorDeclaration, hasExtendsClause: boolean) { return constructor ? visitNodes(constructor.parameters, visitor, isParameter) - : hasExtendsClause ? [createRestParameter(createUniqueName("args"))] : []; + : hasExtendsClause ? [createRestParameter("args")] : []; } /** diff --git a/tests/baselines/reference/FunctionDeclaration10_es6.js b/tests/baselines/reference/FunctionDeclaration10_es6.js index 79a1740b17e..6ac2ec8d194 100644 --- a/tests/baselines/reference/FunctionDeclaration10_es6.js +++ b/tests/baselines/reference/FunctionDeclaration10_es6.js @@ -4,7 +4,7 @@ function * foo(a = yield => yield) { //// [FunctionDeclaration10_es6.js] function* foo(a) { - if (a === void 0) { a = (yield); } + if (a === void 0) { a = yield; } } yield; { diff --git a/tests/baselines/reference/classExpressionES63.js b/tests/baselines/reference/classExpressionES63.js index 7bb41817079..254014c9458 100644 --- a/tests/baselines/reference/classExpressionES63.js +++ b/tests/baselines/reference/classExpressionES63.js @@ -12,13 +12,13 @@ let C = class extends class extends class { this.a = 1; } } { - constructor(...args_1) { - super(...args_1); + constructor(...args) { + super(...args); this.b = 2; } } { - constructor(...args_2) { - super(...args_2); + constructor(...args) { + super(...args); this.c = 3; } };