Skip to content

Commit

Permalink
Merge pull request #2152 from realm/mf-SwiftDeclarationAttributeKind
Browse files Browse the repository at this point in the history
Use SwiftDeclarationAttributeKind instead of string constants
  • Loading branch information
marcelofabri authored Apr 25, 2018
2 parents e6b7baf + 8d90dc7 commit f61b1cf
Show file tree
Hide file tree
Showing 22 changed files with 53 additions and 55 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
"repositoryURL": "https://github.com/jpsim/SourceKitten.git",
"state": {
"branch": null,
"revision": "b6b10419ee439167ba6f7bd928ad30754ff5eb5d",
"version": "0.20.0"
"revision": "7c09176766d4bbc5da377ad857953fb49510a6aa",
"version": "0.21.0"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/Carthage/Commandant.git", from: "0.13.0"),
.package(url: "https://github.com/jpsim/SourceKitten.git", from: "0.20.0"),
.package(url: "https://github.com/jpsim/SourceKitten.git", from: "0.21.0"),
.package(url: "https://github.com/jpsim/Yams.git", from: "0.7.0"),
.package(url: "https://github.com/scottrhoyt/SwiftyTextTable.git", from: "0.8.0"),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]] {
Expand Down
49 changes: 24 additions & 25 deletions Source/SwiftLintFramework/Rules/AttributesRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/BlockBasedKVORule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
}

Expand Down
4 changes: 2 additions & 2 deletions Source/SwiftLintFramework/Rules/DynamicInlineRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"),
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/IdentifierNameRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 [] }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/PrivateActionRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/PrivateOutletRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/ProhibitedSuperRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 [] }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions Source/SwiftLintFramework/Rules/ValidIBInspectableRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
}
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/WeakDelegateRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f61b1cf

Please sign in to comment.