mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
b83e0991b9
The MIT license doesn't require that all files be prepended with this licensing or copyright information. Realm confirmed that they're ok with this change. This will enable some companies to contribute to SwiftLint and the date & authorship information will remain accessible via git source control.
92 lines
3.6 KiB
Swift
92 lines
3.6 KiB
Swift
import Foundation
|
|
import SourceKittenFramework
|
|
|
|
public struct TrailingWhitespaceRule: CorrectableRule, ConfigurationProviderRule {
|
|
|
|
public var configuration = TrailingWhitespaceConfiguration(ignoresEmptyLines: false,
|
|
ignoresComments: true)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "trailing_whitespace",
|
|
name: "Trailing Whitespace",
|
|
description: "Lines should not have trailing whitespace.",
|
|
kind: .style,
|
|
nonTriggeringExamples: [ "let name: String\n", "//\n", "// \n",
|
|
"let name: String //\n", "let name: String // \n" ],
|
|
triggeringExamples: [ "let name: String \n", "/* */ let name: String \n" ],
|
|
corrections: [ "let name: String \n": "let name: String\n",
|
|
"/* */ let name: String \n": "/* */ let name: String\n"]
|
|
)
|
|
|
|
public func validate(file: File) -> [StyleViolation] {
|
|
let filteredLines = file.lines.filter {
|
|
guard $0.content.hasTrailingWhitespace() else { return false }
|
|
|
|
let commentKinds = SyntaxKind.commentKinds
|
|
if configuration.ignoresComments,
|
|
let lastSyntaxKind = file.syntaxKindsByLines[$0.index].last,
|
|
commentKinds.contains(lastSyntaxKind) {
|
|
return false
|
|
}
|
|
|
|
return !configuration.ignoresEmptyLines ||
|
|
// If configured, ignore lines that contain nothing but whitespace (empty lines)
|
|
!$0.content.trimmingCharacters(in: .whitespaces).isEmpty
|
|
}
|
|
|
|
return filteredLines.map {
|
|
StyleViolation(ruleDescription: type(of: self).description,
|
|
severity: configuration.severityConfiguration.severity,
|
|
location: Location(file: file.path, line: $0.index))
|
|
}
|
|
}
|
|
|
|
public func correct(file: File) -> [Correction] {
|
|
let whitespaceCharacterSet = CharacterSet.whitespaces
|
|
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 configuration.ignoresComments,
|
|
let lastSyntaxKind = file.syntaxKindsByLines[line.index].last,
|
|
commentKinds.contains(lastSyntaxKind) {
|
|
correctedLines.append(line.content)
|
|
continue
|
|
}
|
|
|
|
let correctedLine = line.content.bridge()
|
|
.trimmingTrailingCharacters(in: whitespaceCharacterSet)
|
|
|
|
if configuration.ignoresEmptyLines && correctedLine.isEmpty {
|
|
correctedLines.append(line.content)
|
|
continue
|
|
}
|
|
|
|
if file.ruleEnabled(violatingRanges: [line.range], for: self).isEmpty {
|
|
correctedLines.append(line.content)
|
|
continue
|
|
}
|
|
|
|
if line.content != correctedLine {
|
|
let description = type(of: self).description
|
|
let location = Location(file: file.path, line: line.index)
|
|
corrections.append(Correction(ruleDescription: description, location: location))
|
|
}
|
|
correctedLines.append(correctedLine)
|
|
}
|
|
if !corrections.isEmpty {
|
|
// join and re-add trailing newline
|
|
file.write(correctedLines.joined(separator: "\n") + "\n")
|
|
return corrections
|
|
}
|
|
return []
|
|
}
|
|
}
|