Files
SwiftLint/Source/SwiftLintFramework/Rules/RedundantSetAccessControlRule.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

57 lines
2.0 KiB
Swift

import Foundation
import SourceKittenFramework
public struct RedundantSetAccessControlRule: ASTRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "redundant_set_access_control",
name: "Redundant Set Access Control Rule",
description: "Property setter access level shouldn't be explicit if " +
"it's the same as the variable access level.",
kind: .idiomatic,
minSwiftVersion: .fourDotOne,
nonTriggeringExamples: [
"private(set) public var foo: Int",
"public let foo: Int",
"public var foo: Int",
"var foo: Int"
],
triggeringExamples: [
"↓private(set) private var foo: Int",
"↓fileprivate(set) fileprivate var foo: Int",
"↓internal(set) internal var foo: Int",
"↓public(set) public var foo: Int",
"""
open class Foo {
↓open(set) open var bar: Int
}
"""
]
)
public func validate(file: File, kind: SwiftDeclarationKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard SwiftDeclarationKind.variableKinds.contains(kind),
dictionary.setterAccessibility == dictionary.accessibility else {
return []
}
let explicitSetACL = dictionary.swiftAttributes.first { dict in
return dict.attribute?.hasPrefix("source.decl.attribute.setter_access") ?? false
}
guard let offset = explicitSetACL?.offset else {
return []
}
return [
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset))
]
}
}