Simplify code to emit indent code.

This commit is contained in:
Cyrus Najmabadi 2015-03-07 13:33:02 -08:00
parent 680e48f507
commit dddc4660a1
11 changed files with 80 additions and 45 deletions

View File

@ -2917,6 +2917,7 @@ module ts {
function createPropertyAccessExpression(expression: LeftHandSideExpression, name: Identifier): PropertyAccessExpression {
var result = <PropertyAccessExpression>createSynthesizedNode(SyntaxKind.PropertyAccessExpression);
result.expression = expression;
result.dotToken = createSynthesizedNode(SyntaxKind.DotToken);
result.name = name;
return result;
@ -3032,6 +3033,20 @@ module ts {
return false;
}
function indentIfOnDifferentLines(parent: Node, node1: Node, node2: Node) {
var isSynthesized = nodeIsSynthesized(parent);
var realNodesAreOnDifferentLines = !isSynthesized && !nodeEndIsOnSameLineAsNodeStart(node1, node2);
var synthesizedNodeIsOnDifferentLine = synthesizedNodeStartsOnNewLine(node2);
if (realNodesAreOnDifferentLines || synthesizedNodeIsOnDifferentLine) {
increaseIndent();
writeLine();
return true;
}
return false;
}
function emitPropertyAccess(node: PropertyAccessExpression) {
if (tryEmitConstantValue(node)) {
return;
@ -3039,21 +3054,11 @@ module ts {
emit(node.expression);
var indented = false;
var isSynthesied = nodeIsSynthesized(node);
if (!isSynthesied && !nodeEndIsOnSameLineAsNodeStart(node.expression, node.dotToken)) {
indented = true;
increaseIndent();
writeLine();
}
var indented = indentIfOnDifferentLines(node, node.expression, node.dotToken);
write(".");
if (!isSynthesied && !nodeEndIsOnSameLineAsNodeStart(node.dotToken, node.name) && !indented) {
indented = true;
increaseIndent();
writeLine();
}
indented = indented || indentIfOnDifferentLines(node, node.dotToken, node.name);
emit(node.name);
@ -3292,42 +3297,28 @@ module ts {
// If there was a newline between the left side of the binary expression and the
// operator, then try to preserve that.
var indented = false;
var isSynthesied = nodeIsSynthesized(node);
if (!isSynthesied && !nodeEndIsOnSameLineAsNodeStart(node.left, node.operatorToken)) {
indented = true;
increaseIndent();
writeLine();
}
else {
// Otherwise just emit the operator right afterwards. For everything but
// comma, emit a space before the operator.
if (node.operatorToken.kind !== SyntaxKind.CommaToken) {
write(" ");
}
var indented1 = indentIfOnDifferentLines(node, node.left, node.operatorToken);
// Otherwise just emit the operator right afterwards. For everything but
// comma, emit a space before the operator.
if (!indented1 && node.operatorToken.kind !== SyntaxKind.CommaToken) {
write(" ");
}
write(tokenToString(node.operatorToken.kind));
// If there was a newline after the operator (or this is a synthesized node that
// wants to be on a new line), then put a newline in. But only if we haven't
// already done this for the left side.
var wantsIndent = (!isSynthesied && !nodeEndIsOnSameLineAsNodeStart(node.operatorToken, node.right)) ||
synthesizedNodeStartsOnNewLine(node.right);
if (!indented1) {
var indented2 = indentIfOnDifferentLines(node, node.operatorToken, node.right);
}
if (wantsIndent && !indented) {
indented = true;
increaseIndent();
writeLine();
emit(node.right);
}
else {
if (!indented2) {
write(" ");
emit(node.right);
}
emit(node.right);
// If we indented the left or the right side, then dedent now.
if (indented) {
if (indented1 || indented2) {
decreaseIndent();
}
}
@ -3741,10 +3732,7 @@ module ts {
if (propName.kind !== SyntaxKind.Identifier) {
return createElementAccess(object, propName);
}
var node = <PropertyAccessExpression>createSynthesizedNode(SyntaxKind.PropertyAccessExpression);
node.expression = parenthesizeForAccess(object);
node.name = propName;
return node;
return createPropertyAccessExpression(parenthesizeForAccess(object), propName);
}
function createElementAccess(object: Expression, index: Expression): Expression {

View File

@ -157,7 +157,9 @@ module ts {
visitNode(cbNode, (<BinaryExpression>node).right);
case SyntaxKind.ConditionalExpression:
return visitNode(cbNode, (<ConditionalExpression>node).condition) ||
visitNode(cbNode, (<ConditionalExpression>node).questionToken) ||
visitNode(cbNode, (<ConditionalExpression>node).whenTrue) ||
visitNode(cbNode, (<ConditionalExpression>node).colonToken) ||
visitNode(cbNode, (<ConditionalExpression>node).whenFalse);
case SyntaxKind.SpreadElementExpression:
return visitNode(cbNode, (<SpreadElementExpression>node).expression);
@ -3177,7 +3179,8 @@ module ts {
function parseConditionalExpressionRest(leftOperand: Expression): Expression {
// Note: we are passed in an expression which was produced from parseBinaryExpressionOrHigher.
if (!parseOptional(SyntaxKind.QuestionToken)) {
var questionToken = parseOptionalToken(SyntaxKind.QuestionToken);
if (!questionToken) {
return leftOperand;
}
@ -3185,8 +3188,10 @@ module ts {
// we do not that for the 'whenFalse' part.
var node = <ConditionalExpression>createNode(SyntaxKind.ConditionalExpression, leftOperand.pos);
node.condition = leftOperand;
node.questionToken = questionToken;
node.whenTrue = allowInAnd(parseAssignmentExpressionOrHigher);
parseExpected(SyntaxKind.ColonToken);
node.colonToken = parseExpectedToken(SyntaxKind.ColonToken, /*reportAtCurrentPosition:*/ false,
Diagnostics._0_expected, tokenToString(SyntaxKind.ColonToken));
node.whenFalse = parseAssignmentExpressionOrHigher();
return finishNode(node);
}

View File

@ -640,7 +640,9 @@ module ts {
export interface ConditionalExpression extends Expression {
condition: Expression;
questionToken: Node;
whenTrue: Expression;
colonToken: Node;
whenFalse: Expression;
}

View File

@ -542,7 +542,9 @@ declare module "typescript" {
}
interface ConditionalExpression extends Expression {
condition: Expression;
questionToken: Node;
whenTrue: Expression;
colonToken: Node;
whenFalse: Expression;
}
interface FunctionExpression extends PrimaryExpression, FunctionLikeDeclaration {

View File

@ -1633,10 +1633,18 @@ declare module "typescript" {
>condition : Expression
>Expression : Expression
questionToken: Node;
>questionToken : Node
>Node : Node
whenTrue: Expression;
>whenTrue : Expression
>Expression : Expression
colonToken: Node;
>colonToken : Node
>Node : Node
whenFalse: Expression;
>whenFalse : Expression
>Expression : Expression

View File

@ -573,7 +573,9 @@ declare module "typescript" {
}
interface ConditionalExpression extends Expression {
condition: Expression;
questionToken: Node;
whenTrue: Expression;
colonToken: Node;
whenFalse: Expression;
}
interface FunctionExpression extends PrimaryExpression, FunctionLikeDeclaration {

View File

@ -1779,10 +1779,18 @@ declare module "typescript" {
>condition : Expression
>Expression : Expression
questionToken: Node;
>questionToken : Node
>Node : Node
whenTrue: Expression;
>whenTrue : Expression
>Expression : Expression
colonToken: Node;
>colonToken : Node
>Node : Node
whenFalse: Expression;
>whenFalse : Expression
>Expression : Expression

View File

@ -574,7 +574,9 @@ declare module "typescript" {
}
interface ConditionalExpression extends Expression {
condition: Expression;
questionToken: Node;
whenTrue: Expression;
colonToken: Node;
whenFalse: Expression;
}
interface FunctionExpression extends PrimaryExpression, FunctionLikeDeclaration {

View File

@ -1729,10 +1729,18 @@ declare module "typescript" {
>condition : Expression
>Expression : Expression
questionToken: Node;
>questionToken : Node
>Node : Node
whenTrue: Expression;
>whenTrue : Expression
>Expression : Expression
colonToken: Node;
>colonToken : Node
>Node : Node
whenFalse: Expression;
>whenFalse : Expression
>Expression : Expression

View File

@ -611,7 +611,9 @@ declare module "typescript" {
}
interface ConditionalExpression extends Expression {
condition: Expression;
questionToken: Node;
whenTrue: Expression;
colonToken: Node;
whenFalse: Expression;
}
interface FunctionExpression extends PrimaryExpression, FunctionLikeDeclaration {

View File

@ -1902,10 +1902,18 @@ declare module "typescript" {
>condition : Expression
>Expression : Expression
questionToken: Node;
>questionToken : Node
>Node : Node
whenTrue: Expression;
>whenTrue : Expression
>Expression : Expression
colonToken: Node;
>colonToken : Node
>Node : Node
whenFalse: Expression;
>whenFalse : Expression
>Expression : Expression