Skip to content

Commit

Permalink
Fix check for trailing whitespace to return early
Browse files Browse the repository at this point in the history
  • Loading branch information
jaherhi committed Nov 4, 2016
1 parent 7107dfd commit 418b824
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ public struct TrailingWhitespaceRule: CorrectableRule, ConfigurationProviderRule

public func validateFile(file: File) -> [StyleViolation] {
let filteredLines = file.lines.filter {
guard $0.content.hasTrailingWhitespace() else { return false }

let commentKinds = SyntaxKind.commentKinds()
if $0.content.hasTrailingWhitespace() && configuration.ignoresComments,
if configuration.ignoresComments,
let lastSyntaxKind = file.syntaxKindsByLines[$0.index].last
where commentKinds.contains(lastSyntaxKind) {
return false
}

return $0.content.hasTrailingWhitespace() &&
(!configuration.ignoresEmptyLines ||
// If configured, ignore lines that contain nothing but whitespace (empty lines)
!$0.content.stringByTrimmingCharactersInSet(.whitespaceCharacterSet()).isEmpty)
return !configuration.ignoresEmptyLines ||
// If configured, ignore lines that contain nothing but whitespace (empty lines)
!$0.content.stringByTrimmingCharactersInSet(.whitespaceCharacterSet()).isEmpty
}

return filteredLines.map {
Expand All @@ -54,8 +55,13 @@ public struct TrailingWhitespaceRule: CorrectableRule, ConfigurationProviderRule
var correctedLines = [String]()
var corrections = [Correction]()
for line in file.lines {
guard line.content.hasTrailingWhitespace() else {
correctedLines.append(line.content)
continue
}

let commentKinds = SyntaxKind.commentKinds()
if line.content.hasTrailingWhitespace() && configuration.ignoresComments,
if configuration.ignoresComments,
let lastSyntaxKind = file.syntaxKindsByLines[line.index].last
where commentKinds.contains(lastSyntaxKind) {
correctedLines.append(line.content)
Expand Down

0 comments on commit 418b824

Please sign in to comment.