From bfaa51b4e9bcc28ac9c3f8d1df735bfe2507db2b Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 6 Oct 2015 14:09:47 -0700 Subject: [PATCH] Add comment and address PR on comment --- src/compiler/emitter.ts | 13 +++++++- src/compiler/parser.ts | 69 +++++++++++++++++++++-------------------- src/compiler/types.ts | 13 ++++---- 3 files changed, 53 insertions(+), 42 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 6a09006d3e3..115979bd30b 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2515,7 +2515,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi let leftHandSideExpression = node.left; if (node.operatorToken.kind === SyntaxKind.AsteriskAsteriskEqualsToken) { let synthesizedLHS: ElementAccessExpression | PropertyAccessExpression; - // TODO (yuisu) : comment + + // This is used to decide whether to emit parenthesis around the expresison. + // Parenthesis is required for following cases: + // capture variable while emitting right-hand side operand. + // a[0] **= a[0] **= 2 + // _a = a, _a[0] = Math.pow(_a[0], (_b = a, _b[0] = Math.pow(_b[0], 2))); + // ^ -> required extra parenthesis controlled by shouldEmitParenthesis + // exponentiation compount in variable declaration + // var x = a[0] **= a[0] **= 2 + // var x = (_a = a, _a[0] = Math.pow(_a[0], (_b = a, _b[0] = Math.pow(_b[0], 2)))); + // ^ -> required extra parenthesis controlled by shouldEmitParenthesis + // Otherwise, false let shouldEmitParenthesis = false; if (isElementAccessExpression(leftHandSideExpression)) { diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 6141e3ba047..af46229d57f 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3015,8 +3015,8 @@ namespace ts { let newPrecedence = getBinaryOperatorPrecedence(); // Check the precedence to see if we should "take" this operator - // - For left associative operator (all operator but **), only consume the operator - // , recursively call the function below and parse binaryExpression as a rightOperand + // - For left associative operator (all operator but **), consume the operator, + // recursively call the function below, and parse binaryExpression as a rightOperand // of the caller if the new precendence of the operator is greater then or equal to the current precendence. // For example: // a - b - c; @@ -3025,7 +3025,7 @@ namespace ts { // ^token; leftOperand = b. Return b to the caller as a rightOperand // a - b * c; // ^token; leftOperand = b. Return b * c to the caller as a rightOperand - // - For right associative operator (**), only consume the operator, recursively call the function + // - For right associative operator (**), consume the operator, recursively call the function // and parse binaryExpression as a rightOperand of the caller if the new precendence of // the operator is strictly grater than the current precendence // For example: @@ -3187,33 +3187,6 @@ namespace ts { return finishNode(node); } - /** - * Check if the current token can possibly be in an ES7 increment expression. - * - * Increment Expression: - * LeftHandSideExpression[?Yield] - * LeftHandSideExpression[?Yield][no LineTerminator here]++ - * LeftHandSideExpression[?Yield][no LineTerminator here]-- - * ++LeftHandSideExpression[?Yield] - * --LeftHandSideExpression[?Yield] - */ - function isIncrementExpression(): boolean{ - // TODO(yuisu): Comment why we have to do what are we doing here - switch (token) { - case SyntaxKind.PlusToken: - case SyntaxKind.MinusToken: - case SyntaxKind.TildeToken: - case SyntaxKind.ExclamationToken: - case SyntaxKind.DeleteKeyword: - case SyntaxKind.TypeOfKeyword: - case SyntaxKind.VoidKeyword: - case SyntaxKind.LessThanToken: - return false; - default: - return true; - } - } - /** * Parse ES7 unary expression and await expression * @@ -3241,9 +3214,9 @@ namespace ts { } /** - * Parse ES7 simple-unary expression or higher: + * Parse ES7 simple-unary expression or higher: * - * SimpleUnaryExpression: + * ES7 SimpleUnaryExpression: * 1) IncrementExpression[?yield] * 2) delete UnaryExpression[?yield] * 3) void UnaryExpression[?yield] @@ -3283,9 +3256,37 @@ namespace ts { } /** - * Parse ES7 IncrementExpression. The IncrementExpression is used instead of ES6's PostFixExpression. + * Check if the current token can possibly be an ES7 increment expression. * - * IncrementExpression[yield]: + * ES7 IncrementExpression: + * LeftHandSideExpression[?Yield] + * LeftHandSideExpression[?Yield][no LineTerminator here]++ + * LeftHandSideExpression[?Yield][no LineTerminator here]-- + * ++LeftHandSideExpression[?Yield] + * --LeftHandSideExpression[?Yield] + */ + function isIncrementExpression(): boolean{ + // This function is called inside parseUnaryExpression to decide + // whether to call parseSimpleUnaryExpression or call parseIncrmentExpression directly + switch (token) { + case SyntaxKind.PlusToken: + case SyntaxKind.MinusToken: + case SyntaxKind.TildeToken: + case SyntaxKind.ExclamationToken: + case SyntaxKind.DeleteKeyword: + case SyntaxKind.TypeOfKeyword: + case SyntaxKind.VoidKeyword: + case SyntaxKind.LessThanToken: + return false; + default: + return true; + } + } + + /** + * Parse ES7 IncrementExpression. IncrementExpression is used instead of ES6's PostFixExpression. + * + * ES7 IncrementExpression[yield]: * 1) LeftHandSideExpression[?yield] * 2) LeftHandSideExpression[?yield] [[no LineTerminator here]]++ * 3) LeftHandSideExpression[?yield] [[no LineTerminator here]]-- diff --git a/src/compiler/types.ts b/src/compiler/types.ts index e885aa2b7f4..2b8c2624819 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -711,11 +711,10 @@ namespace ts { export interface IncrementExpression extends UnaryExpression { _incrementExpressionBrand: any; } - //export type IncrementExpression = PrefixUnaryExpression | PostfixUnaryExpression | LeftHandSideExpression; export interface PrefixUnaryExpression extends IncrementExpression { operator: SyntaxKind; - operand: UnaryExpression | BinaryExpression; + operand: UnaryExpression; } export interface PostfixUnaryExpression extends IncrementExpression { @@ -740,19 +739,19 @@ namespace ts { } export interface DeleteExpression extends UnaryExpression { - expression: UnaryExpression | BinaryExpression; + expression: UnaryExpression; } export interface TypeOfExpression extends UnaryExpression { - expression: UnaryExpression | BinaryExpression; + expression: UnaryExpression; } export interface VoidExpression extends UnaryExpression { - expression: UnaryExpression | BinaryExpression; + expression: UnaryExpression; } export interface AwaitExpression extends UnaryExpression { - expression: UnaryExpression | BinaryExpression; + expression: UnaryExpression; } export interface YieldExpression extends Expression { @@ -859,7 +858,7 @@ namespace ts { export interface TypeAssertion extends UnaryExpression { type: TypeNode; - expression: UnaryExpression | BinaryExpression; + expression: UnaryExpression; } export type AssertionExpression = TypeAssertion | AsExpression;