From e2b195a4e22d75f1244d43bbddb742522fa4d23f Mon Sep 17 00:00:00 2001 From: Marcelo Fabri Date: Thu, 12 Apr 2018 16:43:58 -0700 Subject: [PATCH 1/2] Use SwiftDeclarationAttributeKind instead of string constants --- .../Extensions/Dictionary+SwiftLint.swift | 3 +- .../Rules/AttributesRule.swift | 49 +++++++++---------- .../Rules/BlockBasedKVORule.swift | 2 +- .../Rules/ClassDelegateProtocolRule.swift | 3 +- ...cardedNotificationCenterObserverRule.swift | 2 +- .../Rules/DynamicInlineRule.swift | 4 +- .../Rules/FunctionParameterCountRule.swift | 12 ++--- .../Rules/IdentifierNameRule.swift | 2 +- .../ImplicitlyUnwrappedOptionalRule.swift | 2 +- .../Rules/OverriddenSuperCallRule.swift | 2 +- .../Rules/OverrideInExtensionRule.swift | 2 +- .../Rules/PrivateActionRule.swift | 2 +- .../Rules/PrivateOutletRule.swift | 2 +- .../Rules/ProhibitedSuperRule.swift | 2 +- .../Rules/QuickDiscouragedCallRule.swift | 2 +- .../QuickDiscouragedFocusedTestRule.swift | 2 +- .../QuickDiscouragedPendingTestRule.swift | 2 +- .../RedundantOptionalInitializationRule.swift | 2 +- .../Rules/ValidIBInspectableRule.swift | 3 +- .../Rules/WeakDelegateRule.swift | 2 +- 20 files changed, 50 insertions(+), 52 deletions(-) diff --git a/Source/SwiftLintFramework/Extensions/Dictionary+SwiftLint.swift b/Source/SwiftLintFramework/Extensions/Dictionary+SwiftLint.swift index afae80bc5c..c23c46936c 100644 --- a/Source/SwiftLintFramework/Extensions/Dictionary+SwiftLint.swift +++ b/Source/SwiftLintFramework/Extensions/Dictionary+SwiftLint.swift @@ -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]] { diff --git a/Source/SwiftLintFramework/Rules/AttributesRule.swift b/Source/SwiftLintFramework/Rules/AttributesRule.swift index 6e2980b190..e76e2b86ce 100644 --- a/Source/SwiftLintFramework/Rules/AttributesRule.swift +++ b/Source/SwiftLintFramework/Rules/AttributesRule.swift @@ -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 = [ - "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 = [ + .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) } } diff --git a/Source/SwiftLintFramework/Rules/BlockBasedKVORule.swift b/Source/SwiftLintFramework/Rules/BlockBasedKVORule.swift index 901ad4cf77..9f9a127a23 100644 --- a/Source/SwiftLintFramework/Rules/BlockBasedKVORule.swift +++ b/Source/SwiftLintFramework/Rules/BlockBasedKVORule.swift @@ -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 { diff --git a/Source/SwiftLintFramework/Rules/ClassDelegateProtocolRule.swift b/Source/SwiftLintFramework/Rules/ClassDelegateProtocolRule.swift index 18062e5c28..7dcc6fe704 100644 --- a/Source/SwiftLintFramework/Rules/ClassDelegateProtocolRule.swift +++ b/Source/SwiftLintFramework/Rules/ClassDelegateProtocolRule.swift @@ -50,8 +50,7 @@ public struct ClassDelegateProtocolRule: ASTRule, ConfigurationProviderRule { } // Check if @objc - let objcAttributes: Set = ["source.decl.attribute.objc", - "source.decl.attribute.objc.name"] + let objcAttributes: Set = [.objc, .objcName] let isObjc = !objcAttributes.isDisjoint(with: dictionary.enclosedSwiftAttributes) guard !isObjc else { return [] diff --git a/Source/SwiftLintFramework/Rules/DiscardedNotificationCenterObserverRule.swift b/Source/SwiftLintFramework/Rules/DiscardedNotificationCenterObserverRule.swift index eba26cd534..4a3de4b5ea 100644 --- a/Source/SwiftLintFramework/Rules/DiscardedNotificationCenterObserverRule.swift +++ b/Source/SwiftLintFramework/Rules/DiscardedNotificationCenterObserverRule.swift @@ -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 [] } diff --git a/Source/SwiftLintFramework/Rules/DynamicInlineRule.swift b/Source/SwiftLintFramework/Rules/DynamicInlineRule.swift index b1fd543329..4a17ab73d2 100644 --- a/Source/SwiftLintFramework/Rules/DynamicInlineRule.swift +++ b/Source/SwiftLintFramework/Rules/DynamicInlineRule.swift @@ -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, diff --git a/Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift b/Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift index f585f51a96..ed2a3457bd 100644 --- a/Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift +++ b/Source/SwiftLintFramework/Rules/FunctionParameterCountRule.swift @@ -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) } } diff --git a/Source/SwiftLintFramework/Rules/IdentifierNameRule.swift b/Source/SwiftLintFramework/Rules/IdentifierNameRule.swift index 9c365208cc..9d3cffecbc 100644 --- a/Source/SwiftLintFramework/Rules/IdentifierNameRule.swift +++ b/Source/SwiftLintFramework/Rules/IdentifierNameRule.swift @@ -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 [] } diff --git a/Source/SwiftLintFramework/Rules/ImplicitlyUnwrappedOptionalRule.swift b/Source/SwiftLintFramework/Rules/ImplicitlyUnwrappedOptionalRule.swift index b17625bce5..d6ca237c30 100644 --- a/Source/SwiftLintFramework/Rules/ImplicitlyUnwrappedOptionalRule.swift +++ b/Source/SwiftLintFramework/Rules/ImplicitlyUnwrappedOptionalRule.swift @@ -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 [] } } diff --git a/Source/SwiftLintFramework/Rules/OverriddenSuperCallRule.swift b/Source/SwiftLintFramework/Rules/OverriddenSuperCallRule.swift index 8e86d58449..a02e222017 100644 --- a/Source/SwiftLintFramework/Rules/OverriddenSuperCallRule.swift +++ b/Source/SwiftLintFramework/Rules/OverriddenSuperCallRule.swift @@ -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) diff --git a/Source/SwiftLintFramework/Rules/OverrideInExtensionRule.swift b/Source/SwiftLintFramework/Rules/OverrideInExtensionRule.swift index ec703158be..3e1e7043b9 100644 --- a/Source/SwiftLintFramework/Rules/OverrideInExtensionRule.swift +++ b/Source/SwiftLintFramework/Rules/OverrideInExtensionRule.swift @@ -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 } diff --git a/Source/SwiftLintFramework/Rules/PrivateActionRule.swift b/Source/SwiftLintFramework/Rules/PrivateActionRule.swift index 022397287f..3f8463883c 100644 --- a/Source/SwiftLintFramework/Rules/PrivateActionRule.swift +++ b/Source/SwiftLintFramework/Rules/PrivateActionRule.swift @@ -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 { diff --git a/Source/SwiftLintFramework/Rules/PrivateOutletRule.swift b/Source/SwiftLintFramework/Rules/PrivateOutletRule.swift index 2531a64c6a..ff4a129ef9 100644 --- a/Source/SwiftLintFramework/Rules/PrivateOutletRule.swift +++ b/Source/SwiftLintFramework/Rules/PrivateOutletRule.swift @@ -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 diff --git a/Source/SwiftLintFramework/Rules/ProhibitedSuperRule.swift b/Source/SwiftLintFramework/Rules/ProhibitedSuperRule.swift index 477f8ab367..fd11c9ce1d 100644 --- a/Source/SwiftLintFramework/Rules/ProhibitedSuperRule.swift +++ b/Source/SwiftLintFramework/Rules/ProhibitedSuperRule.swift @@ -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 [] } diff --git a/Source/SwiftLintFramework/Rules/QuickDiscouragedCallRule.swift b/Source/SwiftLintFramework/Rules/QuickDiscouragedCallRule.swift index 635ed40777..bb54f56a13 100644 --- a/Source/SwiftLintFramework/Rules/QuickDiscouragedCallRule.swift +++ b/Source/SwiftLintFramework/Rules/QuickDiscouragedCallRule.swift @@ -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) } } diff --git a/Source/SwiftLintFramework/Rules/QuickDiscouragedFocusedTestRule.swift b/Source/SwiftLintFramework/Rules/QuickDiscouragedFocusedTestRule.swift index 78f1355ac3..a7bb5b5031 100644 --- a/Source/SwiftLintFramework/Rules/QuickDiscouragedFocusedTestRule.swift +++ b/Source/SwiftLintFramework/Rules/QuickDiscouragedFocusedTestRule.swift @@ -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) } } diff --git a/Source/SwiftLintFramework/Rules/QuickDiscouragedPendingTestRule.swift b/Source/SwiftLintFramework/Rules/QuickDiscouragedPendingTestRule.swift index 8f4a706760..f68a3178f5 100644 --- a/Source/SwiftLintFramework/Rules/QuickDiscouragedPendingTestRule.swift +++ b/Source/SwiftLintFramework/Rules/QuickDiscouragedPendingTestRule.swift @@ -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) } } diff --git a/Source/SwiftLintFramework/Rules/RedundantOptionalInitializationRule.swift b/Source/SwiftLintFramework/Rules/RedundantOptionalInitializationRule.swift index b8a1700501..ebe5e89b13 100644 --- a/Source/SwiftLintFramework/Rules/RedundantOptionalInitializationRule.swift +++ b/Source/SwiftLintFramework/Rules/RedundantOptionalInitializationRule.swift @@ -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 { diff --git a/Source/SwiftLintFramework/Rules/ValidIBInspectableRule.swift b/Source/SwiftLintFramework/Rules/ValidIBInspectableRule.swift index c9e8171fa9..e60fee6874 100644 --- a/Source/SwiftLintFramework/Rules/ValidIBInspectableRule.swift +++ b/Source/SwiftLintFramework/Rules/ValidIBInspectableRule.swift @@ -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 [] } diff --git a/Source/SwiftLintFramework/Rules/WeakDelegateRule.swift b/Source/SwiftLintFramework/Rules/WeakDelegateRule.swift index 628a3c4b79..a9aebc09b8 100644 --- a/Source/SwiftLintFramework/Rules/WeakDelegateRule.swift +++ b/Source/SwiftLintFramework/Rules/WeakDelegateRule.swift @@ -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 From 8d90dc706f5232b0b54aa31bc88a765be882309f Mon Sep 17 00:00:00 2001 From: Marcelo Fabri Date: Tue, 24 Apr 2018 22:32:44 -0700 Subject: [PATCH 2/2] Update SourceKitten on SPM --- Package.resolved | 4 ++-- Package.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Package.resolved b/Package.resolved index 948be2ebd8..bdce1a34c6 100644 --- a/Package.resolved +++ b/Package.resolved @@ -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" } }, { diff --git a/Package.swift b/Package.swift index ff29b7b986..b246c87a92 100644 --- a/Package.swift +++ b/Package.swift @@ -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"), ],