Skip to content

Commit df18dfc

Browse files
author
Yui T
committed
Address PR
1 parent a3f5666 commit df18dfc

2 files changed

Lines changed: 34 additions & 10 deletions

File tree

src/compiler/parser.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3015,11 +3015,31 @@ namespace ts {
30153015
let newPrecedence = getBinaryOperatorPrecedence();
30163016

30173017
// Check the precedence to see if we should "take" this operator
3018-
if (token === SyntaxKind.AsteriskAsteriskToken && newPrecedence < precedence) {
3019-
// ** operator is right-assocative
3020-
break;
3021-
}
3022-
else if (token !== SyntaxKind.AsteriskAsteriskToken && newPrecedence <= precedence) {
3018+
// - For left associative operator (all operator but **), only consume the operator
3019+
// , recursively call the function below and parse binaryExpression as a rightOperand
3020+
// of the caller if the new precendence of the operator is greater then or equal to the current precendence.
3021+
// For example:
3022+
// a - b - c;
3023+
// ^token; leftOperand = b. Return b to the caller as a rightOperand
3024+
// a * b - c
3025+
// ^token; leftOperand = b. Return b to the caller as a rightOperand
3026+
// a - b * c;
3027+
// ^token; leftOperand = b. Return b * c to the caller as a rightOperand
3028+
// - For right associative operator (**), only consume the operator, recursively call the function
3029+
// and parse binaryExpression as a rightOperand of the caller if the new precendence of
3030+
// the operator is strictly grater than the current precendence
3031+
// For example:
3032+
// a ** b ** c;
3033+
// ^^token; leftOperand = b. Return b ** c to the caller as a rightOperand
3034+
// a - b ** c;
3035+
// ^^token; leftOperand = b. Return b ** c to the caller as a rightOperand
3036+
// a ** b - c
3037+
// ^token; leftOperand = b. Return b to the caller as a rightOperand
3038+
const consumeCurrentOperator = token === SyntaxKind.AsteriskAsteriskToken ?
3039+
newPrecedence >= precedence :
3040+
newPrecedence > precedence;
3041+
3042+
if (!consumeCurrentOperator) {
30233043
break;
30243044
}
30253045

@@ -3226,6 +3246,7 @@ namespace ts {
32263246
* 3) LeftHandSideExpression[?yield] [[no LineTerminator here]]--
32273247
* 4) ++LeftHandSideExpression[?yield]
32283248
* 5) --LeftHandSideExpression[?yield]
3249+
* In TypeScript (2), (3) are parsed as PostfixUnaryExpression. (4), (5) are parsed as PrefixUnaryExpression
32293250
*/
32303251
function parseIncrementExpression(): IncrementExpression {
32313252
if (token === SyntaxKind.PlusPlusToken || token === SyntaxKind.MinusMinusToken) {

src/compiler/types.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -707,15 +707,18 @@ namespace ts {
707707
export interface UnaryExpression extends Expression {
708708
_unaryExpressionBrand: any;
709709
}
710+
711+
export interface IncrementExpression extends UnaryExpression {
712+
_incrementExpressionBrand: any;
713+
}
714+
//export type IncrementExpression = PrefixUnaryExpression | PostfixUnaryExpression | LeftHandSideExpression;
710715

711-
export interface IncrementExpression extends UnaryExpression { }
712-
713-
export interface PrefixUnaryExpression extends UnaryExpression {
716+
export interface PrefixUnaryExpression extends IncrementExpression {
714717
operator: SyntaxKind;
715718
operand: UnaryExpression | BinaryExpression;
716719
}
717720

718-
export interface PostfixUnaryExpression extends PostfixExpression {
721+
export interface PostfixUnaryExpression extends IncrementExpression {
719722
operand: LeftHandSideExpression;
720723
operator: SyntaxKind;
721724
}
@@ -724,7 +727,7 @@ namespace ts {
724727
_postfixExpressionBrand: any;
725728
}
726729

727-
export interface LeftHandSideExpression extends PostfixExpression {
730+
export interface LeftHandSideExpression extends IncrementExpression {
728731
_leftHandSideExpressionBrand: any;
729732
}
730733

0 commit comments

Comments
 (0)