mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
40828dff03
* master: (41 commits) Fix formatting in CHANGELOG.md release 0.13.0 Update CHANGELOG.md Fix check for trailing whitespace to return early Fix checks for some inline comments Replace check for comments to use SyntaxKind Add configuration for trailing_whitespace to ignore comments Unwanted space removed - Lint issues fixed Updated HTML Reporter PR feedback Add check on autocorrect for disabled range Use `utf8.count` instead of `utf16.count` to byte range Re-write `ExplicitInitRule` to `ASTRule` added ExplicitInitRule Updated CHANGELOG HTML Reporter added HTML Reporter added Adds information about SwiftLint plugin for AppCode into README.md added reasons why a new rule should be opt in ... # Conflicts: # Source/SwiftLintFramework/Extensions/File+SwiftLint.swift # Source/SwiftLintFramework/Extensions/Structure+SwiftLint.swift # Source/SwiftLintFramework/Rules/ColonRule.swift # Source/SwiftLintFramework/Rules/CommaRule.swift # Source/SwiftLintFramework/Rules/LegacyCGGeometryFunctionsRule.swift # Source/SwiftLintFramework/Rules/LegacyConstantRule.swift # Source/SwiftLintFramework/Rules/LegacyConstructorRule.swift # Source/SwiftLintFramework/Rules/LegacyNSGeometryFunctionsRule.swift # Source/SwiftLintFramework/Rules/LineLengthRule.swift # Source/SwiftLintFramework/Rules/OperatorFunctionWhitespaceRule.swift # Source/SwiftLintFramework/Rules/ReturnArrowWhitespaceRule.swift # Source/SwiftLintFramework/Rules/RuleConfigurations/StatementPositionConfiguration.swift # Source/SwiftLintFramework/Rules/StatementPositionRule.swift # Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift # Tests/SwiftLintFramework/RuleConfigurationTests.swift
99 lines
3.7 KiB
Swift
99 lines
3.7 KiB
Swift
//
|
|
// TrailingWhitespaceRule.swift
|
|
// SwiftLint
|
|
//
|
|
// Created by JP Simard on 2015-05-16.
|
|
// Copyright (c) 2015 Realm. All rights reserved.
|
|
//
|
|
|
|
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.",
|
|
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 validateFile(_ 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 correctFile(_ 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 as NSString)
|
|
.trimmingTrailingCharacters(in: whitespaceCharacterSet)
|
|
|
|
if configuration.ignoresEmptyLines && correctedLine.characters.isEmpty {
|
|
correctedLines.append(line.content)
|
|
continue
|
|
}
|
|
|
|
if file.ruleEnabledViolatingRanges([line.range], forRule: 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 []
|
|
}
|
|
}
|