Skip to content

Commit

Permalink
Migrate EmptyParametersRule
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelofabri committed Jan 28, 2019
1 parent c1002d7 commit b3c399b
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions Source/SwiftLintFramework/Rules/Style/EmptyParametersRule.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import SourceKittenFramework

public struct EmptyParametersRule: ConfigurationProviderRule, CorrectableRule, AutomaticTestableRule {
public struct EmptyParametersRule: ConfigurationProviderRule, SubstitutionCorrectableRule, AutomaticTestableRule {
public var configuration = SeverityConfiguration(.warning)

public init() {}
Expand Down Expand Up @@ -35,14 +35,14 @@ public struct EmptyParametersRule: ConfigurationProviderRule, CorrectableRule, A
)

public func validate(file: File) -> [StyleViolation] {
return violationRanges(file: file).map {
return violationRanges(in: file).map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
}
}

private func violationRanges(file: File) -> [NSRange] {
public func violationRanges(in file: File) -> [NSRange] {
let voidPattern = "\\(Void\\)"
let pattern = voidPattern + "\\s*(throws\\s+)?->"
let excludingPattern = "->\\s*" + pattern // excludes curried functions
Expand All @@ -55,24 +55,7 @@ public struct EmptyParametersRule: ConfigurationProviderRule, CorrectableRule, A
}
}

public func correct(file: File) -> [Correction] {
let violatingRanges = file.ruleEnabled(violatingRanges: violationRanges(file: file), for: self)
var correctedContents = file.contents
var adjustedLocations = [Int]()

for violatingRange in violatingRanges.reversed() {
if let indexRange = correctedContents.nsrangeToIndexRange(violatingRange) {
correctedContents = correctedContents
.replacingCharacters(in: indexRange, with: "()")
adjustedLocations.insert(violatingRange.location, at: 0)
}
}

file.write(correctedContents)

return adjustedLocations.map {
Correction(ruleDescription: type(of: self).description,
location: Location(file: file, characterOffset: $0))
}
public func substitution(for violationRange: NSRange, in file: File) -> (NSRange, String) {
return (violationRange, "()")
}
}

0 comments on commit b3c399b

Please sign in to comment.