Skip to content

Commit 2558d7c

Browse files
authored
Merge pull request swiftlang#231 from dabelknap/no-leading-newlines
Don't allow leading blank lines in a file
2 parents d7e0ae6 + 4aedaf8 commit 2558d7c

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
15741574
}
15751575

15761576
private func extractLeadingTrivia(_ token: TokenSyntax) {
1577-
let isStartOfFile = token.previousToken == nil
1577+
var isStartOfFile = token.previousToken == nil
15781578
let trivia = token.leadingTrivia
15791579

15801580
for (index, piece) in trivia.enumerated() {
@@ -1583,6 +1583,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
15831583
if index > 0 || isStartOfFile {
15841584
appendToken(.comment(Comment(kind: .line, text: text), wasEndOfLine: false))
15851585
appendToken(.newline)
1586+
isStartOfFile = false
15861587
}
15871588

15881589
case .blockComment(let text):
@@ -1592,17 +1593,21 @@ private final class TokenStreamCreator: SyntaxVisitor {
15921593
// comment if the user places one here but the comment is otherwise adjacent to a text
15931594
// token.
15941595
appendToken(.break(.same, size: 0))
1596+
isStartOfFile = false
15951597
}
15961598

15971599
case .docLineComment(let text):
15981600
appendToken(.comment(Comment(kind: .docLine, text: text), wasEndOfLine: false))
15991601
appendToken(.newline)
1602+
isStartOfFile = false
16001603

16011604
case .docBlockComment(let text):
16021605
appendToken(.comment(Comment(kind: .docBlock, text: text), wasEndOfLine: false))
16031606
appendToken(.newline)
1607+
isStartOfFile = false
16041608

16051609
case .newlines(let count), .carriageReturns(let count), .carriageReturnLineFeeds(let count):
1610+
guard !isStartOfFile else { break }
16061611
if config.respectsExistingLineBreaks && isDiscretionaryNewlineAllowed(before: token) {
16071612
appendToken(.newlines(count, discretionary: true))
16081613
}

0 commit comments

Comments
 (0)