Skip to content

Commit 6340531

Browse files
Simplify indentation code in the emitter.
1 parent c6cd57d commit 6340531

File tree

1 file changed

+27
-64
lines changed

1 file changed

+27
-64
lines changed

src/compiler/emitter.ts

Lines changed: 27 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,7 +3033,7 @@ module ts {
30333033
return false;
30343034
}
30353035

3036-
function indentIfOnDifferentLines(parent: Node, node1: Node, node2: Node) {
3036+
function indentIfOnDifferentLines(parent: Node, node1: Node, node2: Node, insertIfNoIndentValue?: string) {
30373037
// Use a newline for existin code if the original had one, and we're preserving formatting.
30383038
var realNodesAreOnDifferentLines = preserveNewLines && !nodeIsSynthesized(parent) && !nodeEndIsOnSameLineAsNodeStart(node1, node2);
30393039

@@ -3045,8 +3045,12 @@ module ts {
30453045
writeLine();
30463046
return true;
30473047
}
3048-
3049-
return false;
3048+
else {
3049+
if (insertIfNoIndentValue) {
3050+
write(insertIfNoIndentValue);
3051+
}
3052+
return false;
3053+
}
30503054
}
30513055

30523056
function emitPropertyAccess(node: PropertyAccessExpression) {
@@ -3056,17 +3060,14 @@ module ts {
30563060

30573061
emit(node.expression);
30583062

3059-
var indented = indentIfOnDifferentLines(node, node.expression, node.dotToken);
3060-
3063+
var indentBeforeDot = indentIfOnDifferentLines(node, node.expression, node.dotToken);
30613064
write(".");
30623065

3063-
indented = indented || indentIfOnDifferentLines(node, node.dotToken, node.name);
3064-
3066+
var indentAfterDot = indentIfOnDifferentLines(node, node.dotToken, node.name);
30653067
emit(node.name);
30663068

3067-
if (indented) {
3068-
decreaseIndent();
3069-
}
3069+
decreaseIndentIf(indentBeforeDot);
3070+
decreaseIndentIf(indentAfterDot);
30703071
}
30713072

30723073
function emitQualifiedName(node: QualifiedName) {
@@ -3300,32 +3301,14 @@ module ts {
33003301
else {
33013302
emit(node.left);
33023303

3303-
// If there was a newline between the left side of the binary expression and the
3304-
// operator, then try to preserve that.
3305-
var indented1 = indentIfOnDifferentLines(node, node.left, node.operatorToken);
3306-
3307-
// Otherwise just emit the operator right afterwards. For everything but
3308-
// comma, emit a space before the operator.
3309-
if (!indented1 && node.operatorToken.kind !== SyntaxKind.CommaToken) {
3310-
write(" ");
3311-
}
3312-
3304+
var indentBeforeOperator = indentIfOnDifferentLines(node, node.left, node.operatorToken, node.operatorToken.kind !== SyntaxKind.CommaToken ? " " : undefined);
33133305
write(tokenToString(node.operatorToken.kind));
33143306

3315-
if (!indented1) {
3316-
var indented2 = indentIfOnDifferentLines(node, node.operatorToken, node.right);
3317-
}
3318-
3319-
if (!indented2) {
3320-
write(" ");
3321-
}
3322-
3307+
var indentAfterOperator = indentIfOnDifferentLines(node, node.operatorToken, node.right, " ");
33233308
emit(node.right);
33243309

3325-
// If we indented the left or the right side, then dedent now.
3326-
if (indented1 || indented2) {
3327-
decreaseIndent();
3328-
}
3310+
decreaseIndentIf(indentBeforeOperator);
3311+
decreaseIndentIf(indentAfterOperator);
33293312
}
33303313
}
33313314

@@ -3335,43 +3318,23 @@ module ts {
33353318

33363319
function emitConditionalExpression(node: ConditionalExpression) {
33373320
emit(node.condition);
3338-
var indent1 = indentIfOnDifferentLines(node, node.condition, node.questionToken);
3339-
if (!indent1) {
3340-
write(" ");
3341-
}
3342-
3321+
var indentBeforeQuestion = indentIfOnDifferentLines(node, node.condition, node.questionToken, " ");
33433322
write("?");
3344-
3345-
if (!indent1) {
3346-
var indent2 = indentIfOnDifferentLines(node, node.questionToken, node.whenTrue);
3347-
}
3348-
3349-
if (!indent2) {
3350-
write(" ");
3351-
}
3352-
3323+
var indentAfterQuestion = indentIfOnDifferentLines(node, node.questionToken, node.whenTrue, " ");
33533324
emit(node.whenTrue);
3325+
decreaseIndentIf(indentBeforeQuestion);
3326+
decreaseIndentIf(indentAfterQuestion);
33543327

3355-
if (indent1 || indent2) {
3356-
decreaseIndent();
3357-
}
3358-
3359-
var indent3 = indentIfOnDifferentLines(node, node.whenTrue, node.colonToken);
3360-
if (!indent3) {
3361-
write(" ");
3362-
}
3363-
3328+
var indentBeforeColon = indentIfOnDifferentLines(node, node.whenTrue, node.colonToken, " ");
33643329
write(":");
3365-
if (!indent3) {
3366-
var indent4 = indentIfOnDifferentLines(node, node.colonToken, node.whenFalse);
3367-
}
3368-
3369-
if (!indent4) {
3370-
write(" ");
3371-
}
3372-
3330+
var indentAfterColon = indentIfOnDifferentLines(node, node.colonToken, node.whenFalse, " ");
33733331
emit(node.whenFalse);
3374-
if (indent3 || indent4) {
3332+
decreaseIndentIf(indentBeforeColon);
3333+
decreaseIndentIf(indentAfterColon);
3334+
}
3335+
3336+
function decreaseIndentIf(value: boolean) {
3337+
if (value) {
33753338
decreaseIndent();
33763339
}
33773340
}

0 commit comments

Comments
 (0)