mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
449190d324
A rule must conform to ManuallyTestedExamplesRule to skip generation of a test for its examples.
44 lines
1.7 KiB
Swift
44 lines
1.7 KiB
Swift
import Foundation
|
|
import SourceKittenFramework
|
|
|
|
public struct ProtocolPropertyAccessorsOrderRule: ConfigurationProviderRule, SubstitutionCorrectableRule {
|
|
public var configuration = SeverityConfiguration(.warning)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "protocol_property_accessors_order",
|
|
name: "Protocol Property Accessors Order",
|
|
description: "When declaring properties in protocols, the order of accessors should be `get set`.",
|
|
kind: .style,
|
|
nonTriggeringExamples: [
|
|
Example("protocol Foo {\n var bar: String { get set }\n }"),
|
|
Example("protocol Foo {\n var bar: String { get }\n }"),
|
|
Example("protocol Foo {\n var bar: String { set }\n }")
|
|
],
|
|
triggeringExamples: [
|
|
Example("protocol Foo {\n var bar: String { ↓set get }\n }")
|
|
],
|
|
corrections: [
|
|
Example("protocol Foo {\n var bar: String { ↓set get }\n }"):
|
|
Example("protocol Foo {\n var bar: String { get set }\n }")
|
|
]
|
|
)
|
|
|
|
public func validate(file: SwiftLintFile) -> [StyleViolation] {
|
|
return violationRanges(in: file).map {
|
|
StyleViolation(ruleDescription: Self.description,
|
|
severity: configuration.severity,
|
|
location: Location(file: file, characterOffset: $0.location))
|
|
}
|
|
}
|
|
|
|
public func violationRanges(in file: SwiftLintFile) -> [NSRange] {
|
|
return file.match(pattern: "\\bset\\s*get\\b", with: [.keyword, .keyword])
|
|
}
|
|
|
|
public func substitution(for violationRange: NSRange, in file: SwiftLintFile) -> (NSRange, String)? {
|
|
return (violationRange, "get set")
|
|
}
|
|
}
|