Files
SwiftLint/Source/SwiftLintFramework/Rules/TrailingSemicolonRule.swift
T
JP Simard b83e0991b9 Remove all file headers
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.
2018-05-04 13:42:02 -07:00

71 lines
2.6 KiB
Swift

import Foundation
import SourceKittenFramework
private extension File {
func violatingTrailingSemicolonRanges() -> [NSRange] {
return match(pattern: "(;+([^\\S\\n]?)*)+;?$",
excludingSyntaxKinds: SyntaxKind.commentAndStringKinds)
}
}
public struct TrailingSemicolonRule: CorrectableRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "trailing_semicolon",
name: "Trailing Semicolon",
description: "Lines should not have trailing semicolons.",
kind: .idiomatic,
nonTriggeringExamples: [ "let a = 0\n" ],
triggeringExamples: [
"let a = 0↓;\n",
"let a = 0↓;\nlet b = 1\n",
"let a = 0↓;;\n",
"let a = 0↓; ;;\n",
"let a = 0↓; ; ;\n"
],
corrections: [
"let a = 0↓;\n": "let a = 0\n",
"let a = 0↓;\nlet b = 1\n": "let a = 0\nlet b = 1\n",
"let a = 0↓;;\n": "let a = 0\n",
"let a = 0↓; ;;\n": "let a = 0\n",
"let a = 0↓; ; ;\n": "let a = 0\n"
]
)
public func validate(file: File) -> [StyleViolation] {
return file.violatingTrailingSemicolonRanges().map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
}
}
public func correct(file: File) -> [Correction] {
let violatingRanges = file.ruleEnabled(violatingRanges: file.violatingTrailingSemicolonRanges(), for: self)
let adjustedRanges = violatingRanges.reduce([NSRange]()) { adjustedRanges, element in
let adjustedLocation = element.location - adjustedRanges.count
let adjustedRange = NSRange(location: adjustedLocation, length: element.length)
return adjustedRanges + [adjustedRange]
}
if adjustedRanges.isEmpty {
return []
}
var correctedContents = file.contents
for range in adjustedRanges {
if let indexRange = correctedContents.nsrangeToIndexRange(range) {
correctedContents = correctedContents
.replacingCharacters(in: indexRange, with: "")
}
}
file.write(correctedContents)
return adjustedRanges.map {
Correction(ruleDescription: type(of: self).description,
location: Location(file: file, characterOffset: $0.location))
}
}
}