Files
SwiftLint/Source/SwiftLintFramework/Rules/Style/ProtocolPropertyAccessorsOrderRule.swift
T
Zev Eisenberg fcf848608e Add Inline test failure messages (#3040)
* Add Example wrapper in order to display test failures inline when running in Xcode.
* Stop using Swift 5.1-only features so we can compile on Xcode 10.2.
* Wrap strings in Example.
* Add Changelog entry.
* Wrap all examples in Example struct.
* Better and more complete capturing of line numbers.
* Fix broken test.
* Better test traceability.
* Address or disable linting warnings.
* Add documentation comments.
* Disable linter for a few cases.
* Limit mutability and add copy-and-mutate utility functions.
* Limit scope of mutability.
2020-02-02 10:35:37 +02:00

45 lines
1.8 KiB
Swift

import Foundation
import SourceKittenFramework
public struct ProtocolPropertyAccessorsOrderRule: ConfigurationProviderRule, SubstitutionCorrectableRule,
AutomaticTestableRule {
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: type(of: 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")
}
}