Skip to content

Commit f061e00

Browse files
Merge pull request #2267 from Microsoft/indentSimplification
Simplify indentation code in the emitter.
2 parents eb92b53 + aa96475 commit f061e00

File tree

1 file changed

+32
-71
lines changed

1 file changed

+32
-71
lines changed

src/compiler/emitter.ts

Lines changed: 32 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,8 +3033,10 @@ module ts {
30333033
return false;
30343034
}
30353035

3036-
function indentIfOnDifferentLines(parent: Node, node1: Node, node2: Node) {
3037-
// Use a newline for existing code if the original had one, and we're preserving formatting.
3036+
// Returns 'true' if the code was actually indented, false otherwise.
3037+
// If the code is not indented, an optional valueToWriteWhenNotIndenting will be
3038+
// emitted instead.
3039+
function indentIfOnDifferentLines(parent: Node, node1: Node, node2: Node, valueToWriteWhenNotIndenting?: string) {
30383040
var realNodesAreOnDifferentLines = preserveNewLines && !nodeIsSynthesized(parent) && !nodeEndIsOnSameLineAsNodeStart(node1, node2);
30393041

30403042
// Always use a newline for synthesized code if the synthesizer desires it.
@@ -3045,8 +3047,12 @@ module ts {
30453047
writeLine();
30463048
return true;
30473049
}
3048-
3049-
return false;
3050+
else {
3051+
if (valueToWriteWhenNotIndenting) {
3052+
write(valueToWriteWhenNotIndenting);
3053+
}
3054+
return false;
3055+
}
30503056
}
30513057

30523058
function emitPropertyAccess(node: PropertyAccessExpression) {
@@ -3055,18 +3061,11 @@ module ts {
30553061
}
30563062

30573063
emit(node.expression);
3058-
3059-
var indented = indentIfOnDifferentLines(node, node.expression, node.dotToken);
3060-
3064+
var indentedBeforeDot = indentIfOnDifferentLines(node, node.expression, node.dotToken);
30613065
write(".");
3062-
3063-
indented = indented || indentIfOnDifferentLines(node, node.dotToken, node.name);
3064-
3066+
var indentedAfterDot = indentIfOnDifferentLines(node, node.dotToken, node.name);
30653067
emit(node.name);
3066-
3067-
if (indented) {
3068-
decreaseIndent();
3069-
}
3068+
decreaseIndentIf(indentedBeforeDot, indentedAfterDot);
30703069
}
30713070

30723071
function emitQualifiedName(node: QualifiedName) {
@@ -3299,33 +3298,11 @@ module ts {
32993298
}
33003299
else {
33013300
emit(node.left);
3302-
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-
3301+
var indentedBeforeOperator = indentIfOnDifferentLines(node, node.left, node.operatorToken, node.operatorToken.kind !== SyntaxKind.CommaToken ? " " : undefined);
33133302
write(tokenToString(node.operatorToken.kind));
3314-
3315-
if (!indented1) {
3316-
var indented2 = indentIfOnDifferentLines(node, node.operatorToken, node.right);
3317-
}
3318-
3319-
if (!indented2) {
3320-
write(" ");
3321-
}
3322-
3303+
var indentedAfterOperator = indentIfOnDifferentLines(node, node.operatorToken, node.right, " ");
33233304
emit(node.right);
3324-
3325-
// If we indented the left or the right side, then dedent now.
3326-
if (indented1 || indented2) {
3327-
decreaseIndent();
3328-
}
3305+
decreaseIndentIf(indentedBeforeOperator, indentedAfterOperator);
33293306
}
33303307
}
33313308

@@ -3335,43 +3312,27 @@ module ts {
33353312

33363313
function emitConditionalExpression(node: ConditionalExpression) {
33373314
emit(node.condition);
3338-
var indent1 = indentIfOnDifferentLines(node, node.condition, node.questionToken);
3339-
if (!indent1) {
3340-
write(" ");
3341-
}
3342-
3315+
var indentedBeforeQuestion = indentIfOnDifferentLines(node, node.condition, node.questionToken, " ");
33433316
write("?");
3344-
3345-
if (!indent1) {
3346-
var indent2 = indentIfOnDifferentLines(node, node.questionToken, node.whenTrue);
3347-
}
3348-
3349-
if (!indent2) {
3350-
write(" ");
3351-
}
3352-
3317+
var indentedAfterQuestion = indentIfOnDifferentLines(node, node.questionToken, node.whenTrue, " ");
33533318
emit(node.whenTrue);
3319+
decreaseIndentIf(indentedBeforeQuestion, indentedAfterQuestion);
3320+
var indentedBeforeColon = indentIfOnDifferentLines(node, node.whenTrue, node.colonToken, " ");
3321+
write(":");
3322+
var indentedAfterColon = indentIfOnDifferentLines(node, node.colonToken, node.whenFalse, " ");
3323+
emit(node.whenFalse);
3324+
decreaseIndentIf(indentedBeforeColon, indentedAfterColon);
3325+
}
33543326

3355-
if (indent1 || indent2) {
3327+
// Helper function to decrease the indent if we previously indented. Allows multiple
3328+
// previous indent values to be considered at a time. This also allows caller to just
3329+
// call this once, passing in all their appropriate indent values, instead of needing
3330+
// to call this helper function multiple times.
3331+
function decreaseIndentIf(value1: boolean, value2?: boolean) {
3332+
if (value1) {
33563333
decreaseIndent();
33573334
}
3358-
3359-
var indent3 = indentIfOnDifferentLines(node, node.whenTrue, node.colonToken);
3360-
if (!indent3) {
3361-
write(" ");
3362-
}
3363-
3364-
write(":");
3365-
if (!indent3) {
3366-
var indent4 = indentIfOnDifferentLines(node, node.colonToken, node.whenFalse);
3367-
}
3368-
3369-
if (!indent4) {
3370-
write(" ");
3371-
}
3372-
3373-
emit(node.whenFalse);
3374-
if (indent3 || indent4) {
3335+
if (value2) {
33753336
decreaseIndent();
33763337
}
33773338
}

0 commit comments

Comments
 (0)