Skip to content

Commit 36f7f30

Browse files
author
Andy
authored
lineAction: Use an enum instead of true | false | undefined (microsoft#20086)
1 parent 1298945 commit 36f7f30

1 file changed

Lines changed: 20 additions & 21 deletions

File tree

src/services/formatting/formatting.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -792,28 +792,25 @@ namespace ts.formatting {
792792
processTrivia(currentTokenInfo.leadingTrivia, parent, childContextNode, dynamicIndentation);
793793
}
794794

795-
let lineAdded: boolean;
795+
let lineAction = LineAction.None;
796796
const isTokenInRange = rangeContainsRange(originalRange, currentTokenInfo.token);
797797

798798
const tokenStart = sourceFile.getLineAndCharacterOfPosition(currentTokenInfo.token.pos);
799799
if (isTokenInRange) {
800800
const rangeHasError = rangeContainsError(currentTokenInfo.token);
801801
// save previousRange since processRange will overwrite this value with current one
802802
const savePreviousRange = previousRange;
803-
lineAdded = processRange(currentTokenInfo.token, tokenStart, parent, childContextNode, dynamicIndentation);
804-
if (rangeHasError) {
805-
// do not indent comments\token if token range overlaps with some error
806-
indentToken = false;
807-
}
808-
else {
809-
if (lineAdded !== undefined) {
810-
indentToken = lineAdded;
811-
}
812-
else {
803+
lineAction = processRange(currentTokenInfo.token, tokenStart, parent, childContextNode, dynamicIndentation);
804+
// do not indent comments\token if token range overlaps with some error
805+
if (!rangeHasError) {
806+
if (lineAction === LineAction.None) {
813807
// indent token only if end line of previous range does not match start line of the token
814808
const prevEndLine = savePreviousRange && sourceFile.getLineAndCharacterOfPosition(savePreviousRange.end).line;
815809
indentToken = lastTriviaWasNewLine && tokenStart.line !== prevEndLine;
816810
}
811+
else {
812+
indentToken = lineAction === LineAction.LineAdded;
813+
}
817814
}
818815
}
819816

@@ -854,7 +851,7 @@ namespace ts.formatting {
854851

855852
// indent token only if is it is in target range and does not overlap with any error ranges
856853
if (tokenIndentation !== Constants.Unknown && indentNextTokenOrTrivia) {
857-
insertIndentation(currentTokenInfo.token.pos, tokenIndentation, lineAdded);
854+
insertIndentation(currentTokenInfo.token.pos, tokenIndentation, lineAction === LineAction.LineAdded);
858855

859856
lastIndentedLine = tokenStart.line;
860857
indentationOnLastIndentedLine = tokenIndentation;
@@ -880,18 +877,18 @@ namespace ts.formatting {
880877
rangeStart: LineAndCharacter,
881878
parent: Node,
882879
contextNode: Node,
883-
dynamicIndentation: DynamicIndentation): boolean {
880+
dynamicIndentation: DynamicIndentation): LineAction {
884881

885882
const rangeHasError = rangeContainsError(range);
886-
let lineAdded: boolean;
883+
let lineAction = LineAction.None;
887884
if (!rangeHasError) {
888885
if (!previousRange) {
889886
// trim whitespaces starting from the beginning of the span up to the current line
890887
const originalStart = sourceFile.getLineAndCharacterOfPosition(originalRange.pos);
891888
trimTrailingWhitespacesForLines(originalStart.line, rangeStart.line);
892889
}
893890
else {
894-
lineAdded =
891+
lineAction =
895892
processPair(range, rangeStart.line, parent, previousRange, previousRangeStartLine, previousParent, contextNode, dynamicIndentation);
896893
}
897894
}
@@ -900,7 +897,7 @@ namespace ts.formatting {
900897
previousParent = parent;
901898
previousRangeStartLine = rangeStart.line;
902899

903-
return lineAdded;
900+
return lineAction;
904901
}
905902

906903
function processPair(currentItem: TextRangeWithKind,
@@ -910,27 +907,27 @@ namespace ts.formatting {
910907
previousStartLine: number,
911908
previousParent: Node,
912909
contextNode: Node,
913-
dynamicIndentation: DynamicIndentation): boolean {
910+
dynamicIndentation: DynamicIndentation): LineAction {
914911

915912
formattingContext.updateContext(previousItem, previousParent, currentItem, currentParent, contextNode);
916913

917914
const rule = getRule(formattingContext);
918915

919916
let trimTrailingWhitespaces: boolean;
920-
let lineAdded: boolean;
917+
let lineAction = LineAction.None;
921918
if (rule) {
922919
applyRuleEdits(rule, previousItem, previousStartLine, currentItem, currentStartLine);
923920

924921
if (rule.action & (RuleAction.Space | RuleAction.Delete) && currentStartLine !== previousStartLine) {
925-
lineAdded = false;
922+
lineAction = LineAction.LineRemoved;
926923
// Handle the case where the next line is moved to be the end of this line.
927924
// In this case we don't indent the next line in the next pass.
928925
if (currentParent.getStart(sourceFile) === currentItem.pos) {
929926
dynamicIndentation.recomputeIndentation(/*lineAddedByFormatting*/ false);
930927
}
931928
}
932929
else if (rule.action & RuleAction.NewLine && currentStartLine === previousStartLine) {
933-
lineAdded = true;
930+
lineAction = LineAction.LineAdded;
934931
// Handle the case where token2 is moved to the new line.
935932
// In this case we indent token2 in the next pass but we set
936933
// sameLineIndent flag to notify the indenter that the indentation is within the line.
@@ -951,7 +948,7 @@ namespace ts.formatting {
951948
trimTrailingWhitespacesForLines(previousStartLine, currentStartLine, previousItem);
952949
}
953950

954-
return lineAdded;
951+
return lineAction;
955952
}
956953

957954
function insertIndentation(pos: number, indentation: number, lineAdded: boolean): void {
@@ -1154,6 +1151,8 @@ namespace ts.formatting {
11541151
}
11551152
}
11561153

1154+
const enum LineAction { None, LineAdded, LineRemoved }
1155+
11571156
/**
11581157
* @param precedingToken pass `null` if preceding token was already computed and result was `undefined`.
11591158
*/

0 commit comments

Comments
 (0)