Use SwiftDeclarationAttributeKind instead of string constants

This commit is contained in:
Marcelo Fabri
2018-04-12 16:43:58 -07:00
parent e6b7baf4c6
commit e2b195a4e2
20 changed files with 50 additions and 52 deletions
@@ -76,8 +76,9 @@ extension Dictionary where Key: ExpressibleByStringLiteral {
return self["key.attribute"] as? String
}
var enclosedSwiftAttributes: [String] {
var enclosedSwiftAttributes: [SwiftDeclarationAttributeKind] {
return swiftAttributes.compactMap { $0.attribute }
.compactMap(SwiftDeclarationAttributeKind.init(rawValue:))
}
var swiftAttributes: [[String: SourceKitRepresentable]] {
@@ -296,32 +296,31 @@ public struct AttributesRule: ASTRule, OptInRule, ConfigurationProviderRule {
return false
}
private func parseAttributes(dictionary: [String: SourceKitRepresentable]) -> [String] {
private func parseAttributes(dictionary: [String: SourceKitRepresentable]) -> [SwiftDeclarationAttributeKind] {
let attributes = dictionary.enclosedSwiftAttributes
let blacklist: Set<String> = [
"source.decl.attribute.__raw_doc_comment",
"source.decl.attribute.mutating",
"source.decl.attribute.nonmutating",
"source.decl.attribute.lazy",
"source.decl.attribute.dynamic",
"source.decl.attribute.final",
"source.decl.attribute.infix",
"source.decl.attribute.optional",
"source.decl.attribute.override",
"source.decl.attribute.postfix",
"source.decl.attribute.prefix",
"source.decl.attribute.required",
"source.decl.attribute.weak",
"source.decl.attribute.private",
"source.decl.attribute.fileprivate",
"source.decl.attribute.internal",
"source.decl.attribute.public",
"source.decl.attribute.open",
"source.decl.attribute.setter_access.private",
"source.decl.attribute.setter_access.fileprivate",
"source.decl.attribute.setter_access.internal",
"source.decl.attribute.setter_access.public",
"source.decl.attribute.setter_access.open"
let blacklist: Set<SwiftDeclarationAttributeKind> = [
.mutating,
.nonmutating,
.lazy,
.dynamic,
.final,
.infix,
.optional,
.override,
.postfix,
.prefix,
.required,
.weak,
.private,
.fileprivate,
.internal,
.public,
.open,
.setterPrivate,
.setterFilePrivate,
.setterInternal,
.setterPublic,
.setterOpen
]
return attributes.filter { !blacklist.contains($0) }
}
@@ -41,7 +41,7 @@ public struct BlockBasedKVORule: ASTRule, ConfigurationProviderRule {
public func validate(file: File, kind: SwiftDeclarationKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard SwiftVersion.current >= .four, kind == .functionMethodInstance,
dictionary.enclosedSwiftAttributes.contains("source.decl.attribute.override"),
dictionary.enclosedSwiftAttributes.contains(.override),
dictionary.name == "observeValue(forKeyPath:of:change:context:)",
hasExpectedParamTypes(types: dictionary.enclosedVarParameters.parameterTypes),
let offset = dictionary.offset else {
@@ -50,8 +50,7 @@ public struct ClassDelegateProtocolRule: ASTRule, ConfigurationProviderRule {
}
// Check if @objc
let objcAttributes: Set<String> = ["source.decl.attribute.objc",
"source.decl.attribute.objc.name"]
let objcAttributes: Set<SwiftDeclarationAttributeKind> = [.objc, .objcName]
let isObjc = !objcAttributes.isDisjoint(with: dictionary.enclosedSwiftAttributes)
guard !isObjc else {
return []
@@ -67,7 +67,7 @@ public struct DiscardedNotificationCenterObserverRule: ASTRule, ConfigurationPro
if let lastMatch = file.match(pattern: "\\breturn\\s+", with: [.keyword], range: range).last,
lastMatch.location == range.length - lastMatch.length,
let lastFunction = file.structure.functions(forByteOffset: offset).last,
!lastFunction.enclosedSwiftAttributes.contains("source.decl.attribute.discardableResult") {
!lastFunction.enclosedSwiftAttributes.contains(.discardableResult) {
return []
}
@@ -41,8 +41,8 @@ public struct DynamicInlineRule: ASTRule, ConfigurationProviderRule {
// the attribute we are interested in.
guard functionKinds.contains(kind),
case let attributes = dictionary.enclosedSwiftAttributes,
attributes.contains("source.decl.attribute.dynamic"),
attributes.contains("source.decl.attribute.inline"),
attributes.contains(.dynamic),
attributes.contains(.inline),
let funcByteOffset = dictionary.offset,
let funcOffset = file.contents.bridge()
.byteRangeToNSRange(start: funcByteOffset, length: 0)?.location,
@@ -83,8 +83,8 @@ public struct FunctionParameterCountRule: ASTRule, ConfigurationProviderRule {
return []
}
fileprivate func allFunctionParameterCount(structure: [[String: SourceKitRepresentable]],
offset: Int, length: Int) -> Int {
private func allFunctionParameterCount(structure: [[String: SourceKitRepresentable]],
offset: Int, length: Int) -> Int {
var parameterCount = 0
for subDict in structure {
guard let key = subDict.kind,
@@ -103,13 +103,13 @@ public struct FunctionParameterCountRule: ASTRule, ConfigurationProviderRule {
return parameterCount
}
fileprivate func defaultFunctionParameterCount(file: File, byteOffset: Int, byteLength: Int) -> Int {
private func defaultFunctionParameterCount(file: File, byteOffset: Int, byteLength: Int) -> Int {
let substring = file.contents.bridge().substringWithByteRange(start: byteOffset, length: byteLength)!
let equals = substring.filter { $0 == "=" }
return equals.count
}
fileprivate func functionIsInitializer(file: File, byteOffset: Int, byteLength: Int) -> Bool {
private func functionIsInitializer(file: File, byteOffset: Int, byteLength: Int) -> Bool {
guard let name = file.contents.bridge()
.substringWithByteRange(start: byteOffset, length: byteLength),
name.hasPrefix("init"),
@@ -124,7 +124,7 @@ public struct FunctionParameterCountRule: ASTRule, ConfigurationProviderRule {
return alphaNumericName == "init"
}
fileprivate func functionIsOverride(attributes: [String]) -> Bool {
return attributes.contains("source.decl.attribute.override")
private func functionIsOverride(attributes: [SwiftDeclarationAttributeKind]) -> Bool {
return attributes.contains(.override)
}
}
@@ -34,7 +34,7 @@ public struct IdentifierNameRule: ASTRule, ConfigurationProviderRule {
public func validate(file: File, kind: SwiftDeclarationKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard !dictionary.enclosedSwiftAttributes.contains("source.decl.attribute.override") else {
guard !dictionary.enclosedSwiftAttributes.contains(.override) else {
return []
}
@@ -57,7 +57,7 @@ public struct ImplicitlyUnwrappedOptionalRule: ASTRule, ConfigurationProviderRul
guard hasImplicitlyUnwrappedOptional(typeName) else { return [] }
if configuration.mode == .allExceptIBOutlets {
let isOutlet = dictionary.enclosedSwiftAttributes.contains("source.decl.attribute.iboutlet")
let isOutlet = dictionary.enclosedSwiftAttributes.contains(.iboutlet)
if isOutlet { return [] }
}
@@ -74,7 +74,7 @@ public struct OverriddenSuperCallRule: ConfigurationProviderRule, ASTRule, OptIn
let name = dictionary.name,
kind == .functionMethodInstance,
configuration.resolvedMethodNames.contains(name),
dictionary.enclosedSwiftAttributes.contains("source.decl.attribute.override")
dictionary.enclosedSwiftAttributes.contains(.override)
else { return [] }
let callsToSuper = dictionary.extractCallsToSuper(methodName: name)
@@ -51,7 +51,7 @@ public struct OverrideInExtensionRule: ConfigurationProviderRule, OptInRule {
.flatMap { element in
return element.dictionary.substructure.compactMap { element -> Int? in
guard element.kind.flatMap(SwiftDeclarationKind.init) != nil,
element.enclosedSwiftAttributes.contains("source.decl.attribute.override"),
element.enclosedSwiftAttributes.contains(.override),
let offset = element.offset else {
return nil
}
@@ -48,7 +48,7 @@ public struct PrivateActionRule: ASTRule, OptInRule, ConfigurationProviderRule {
guard
let offset = dictionary.offset,
kind == .functionMethodInstance,
dictionary.enclosedSwiftAttributes.contains("source.decl.attribute.ibaction"),
dictionary.enclosedSwiftAttributes.contains(.ibaction),
let controlLevel = dictionary.accessibility.flatMap(AccessControlLevel.init(identifier:)),
controlLevel.isPrivate == false
else {
@@ -39,7 +39,7 @@ public struct PrivateOutletRule: ASTRule, OptInRule, ConfigurationProviderRule {
}
// Check if IBOutlet
let isOutlet = dictionary.enclosedSwiftAttributes.contains("source.decl.attribute.iboutlet")
let isOutlet = dictionary.enclosedSwiftAttributes.contains(.iboutlet)
guard isOutlet else { return [] }
// Check if private
@@ -65,7 +65,7 @@ public struct ProhibitedSuperRule: ConfigurationProviderRule, ASTRule, OptInRule
let name = dictionary.name,
kind == .functionMethodInstance,
configuration.resolvedMethodNames.contains(name),
dictionary.enclosedSwiftAttributes.contains("source.decl.attribute.override"),
dictionary.enclosedSwiftAttributes.contains(.override),
!dictionary.extractCallsToSuper(methodName: name).isEmpty
else { return [] }
@@ -33,7 +33,7 @@ public struct QuickDiscouragedCallRule: OptInRule, ConfigurationProviderRule {
return classDict.substructure.filter {
return $0.name == "spec()" && $0.enclosedVarParameters.isEmpty &&
$0.kind.flatMap(SwiftDeclarationKind.init) == .functionMethodInstance &&
$0.enclosedSwiftAttributes.contains("source.decl.attribute.override")
$0.enclosedSwiftAttributes.contains(.override)
}
}
@@ -33,7 +33,7 @@ public struct QuickDiscouragedFocusedTestRule: OptInRule, ConfigurationProviderR
return classDict.substructure.filter {
return $0.name == "spec()" && $0.enclosedVarParameters.isEmpty &&
$0.kind.flatMap(SwiftDeclarationKind.init) == .functionMethodInstance &&
$0.enclosedSwiftAttributes.contains("source.decl.attribute.override")
$0.enclosedSwiftAttributes.contains(.override)
}
}
@@ -33,7 +33,7 @@ public struct QuickDiscouragedPendingTestRule: OptInRule, ConfigurationProviderR
return classDict.substructure.filter {
return $0.name == "spec()" && $0.enclosedVarParameters.isEmpty &&
$0.kind.flatMap(SwiftDeclarationKind.init) == .functionMethodInstance &&
$0.enclosedSwiftAttributes.contains("source.decl.attribute.override")
$0.enclosedSwiftAttributes.contains(.override)
}
}
@@ -72,7 +72,7 @@ public struct RedundantOptionalInitializationRule: ASTRule, CorrectableRule, Con
dictionary.setterAccessibility != nil,
let type = dictionary.typeName,
typeIsOptional(type),
!dictionary.enclosedSwiftAttributes.contains("source.decl.attribute.lazy"),
!dictionary.enclosedSwiftAttributes.contains(.lazy),
let range = range(for: dictionary, file: file),
let match = file.match(pattern: pattern, with: [.keyword], range: range).first,
match.location == range.location + range.length - match.length else {
@@ -51,8 +51,7 @@ public struct ValidIBInspectableRule: ASTRule, ConfigurationProviderRule {
}
// Check if IBInspectable
let isIBInspectable = dictionary.enclosedSwiftAttributes.contains(
"source.decl.attribute.ibinspectable")
let isIBInspectable = dictionary.enclosedSwiftAttributes.contains(.ibinspectable)
guard isIBInspectable else {
return []
}
@@ -53,7 +53,7 @@ public struct WeakDelegateRule: ASTRule, ConfigurationProviderRule {
}
// Check if non-weak
let isWeak = dictionary.enclosedSwiftAttributes.contains("source.decl.attribute.weak")
let isWeak = dictionary.enclosedSwiftAttributes.contains(.weak)
guard !isWeak else { return [] }
// if the declaration is inside a protocol