Skip to content

Commit 64edd9b

Browse files
committed
Only unindent leading semicolons when SemicolonPreference is set to "remove", add new comprehensive test covering many scenarios
1 parent d8a4264 commit 64edd9b

File tree

6 files changed

+387
-7
lines changed

6 files changed

+387
-7
lines changed

src/services/formatting/formatting.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import {
6767
rangeContainsStartEnd,
6868
rangeOverlapsWithStartEnd,
6969
repeatString,
70+
SemicolonPreference,
7071
SourceFile,
7172
SourceFileLike,
7273
startEndContainsRange,
@@ -980,7 +981,7 @@ function formatSpanWorker(
980981

981982
const lastTriviaWasNewLine = formattingScanner.lastTrailingTriviaWasNewLine();
982983
let indentToken = false;
983-
let isNewlineSemicolon = false;
984+
let unindentToken = false;
984985

985986
if (currentTokenInfo.leadingTrivia) {
986987
processTrivia(currentTokenInfo.leadingTrivia, parent, childContextNode, dynamicIndentation);
@@ -1002,7 +1003,7 @@ function formatSpanWorker(
10021003
const prevEndLine = savePreviousRange && sourceFile.getLineAndCharacterOfPosition(savePreviousRange.end).line;
10031004
indentToken = lastTriviaWasNewLine && tokenStart.line !== prevEndLine;
10041005

1005-
isNewlineSemicolon = currentTokenInfo.token.kind === SyntaxKind.SemicolonToken;
1006+
unindentToken = currentTokenInfo.token.kind === SyntaxKind.SemicolonToken && formattingContext.options.semicolons === SemicolonPreference.Remove;
10061007
}
10071008
else {
10081009
indentToken = lineAction === LineAction.LineAdded;
@@ -1017,7 +1018,7 @@ function formatSpanWorker(
10171018

10181019
if (indentToken) {
10191020
const tokenIndentation = (isTokenInRange && !rangeContainsError(currentTokenInfo.token)) ?
1020-
dynamicIndentation.getIndentationForToken(tokenStart.line, currentTokenInfo.token.kind, container, !!isListEndToken || isNewlineSemicolon) :
1021+
dynamicIndentation.getIndentationForToken(tokenStart.line, currentTokenInfo.token.kind, container, !!isListEndToken || unindentToken) :
10211022
Constants.Unknown;
10221023

10231024
let indentNextTokenOrTrivia = true;

tests/cases/fourslash/formatAddSemicolons1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class C {
4242
three: string;
4343
m() { }
4444
;["three"] = {}
45-
;["four"];
45+
;["four"];
4646
}
4747
enum E {
4848
C

0 commit comments

Comments
 (0)