Files
SwiftLint/Source/SwiftLintFramework/Rules/Style/ProtocolPropertyAccessorsOrderRule.swift
T
Danny Mösch 449190d324 Verify examples in rules by default and enforce explicit exclusion (#4065)
A rule must conform to ManuallyTestedExamplesRule to skip generation of a test for its examples.
2022-08-09 22:32:09 +02:00

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")
}
}