Skip to content

Commit

Permalink
Refactoring shared code
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelofabri committed Dec 15, 2016
1 parent cbccb0e commit e854701
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 49 deletions.
20 changes: 20 additions & 0 deletions Source/SwiftLintFramework/Extensions/Dictionary+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,24 @@ extension Dictionary where Key: ExpressibleByStringLiteral {
let array = self["key.attributes"] as? [SourceKitRepresentable] ?? []
return array.flatMap { ($0 as? [String: String])?["key.attribute"] }
}

var enclosedVarParameters: [[String: SourceKitRepresentable]] {
let substructure = self["key.substructure"] as? [SourceKitRepresentable] ?? []
return substructure.flatMap { subItem -> [[String: SourceKitRepresentable]] in
guard let subDict = subItem as? [String: SourceKitRepresentable],
let kindString = subDict["key.kind"] as? String else {
return []
}

if SwiftDeclarationKind(rawValue: kindString) == .varParameter {
return [subDict]
}

if SwiftExpressionKind(rawValue: kindString) == .argument {
return subDict.enclosedVarParameters
}

return []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public struct ClosureParameterPositionRule: ASTRule, ConfigurationProviderRule {
return []
}

let parameters = filterByKind(dictionary: dictionary, kind: .varParameter)
let parameters = dictionary.enclosedVarParameters
let rangeStart = nameOffset + nameLength
let regex = ClosureParameterPositionRule.openBraceRegex

Expand Down Expand Up @@ -92,26 +92,4 @@ public struct ClosureParameterPositionRule: ASTRule, ConfigurationProviderRule {
location: Location(file: file, byteOffset: paramOffset))
}
}

private func filterByKind(dictionary: [String: SourceKitRepresentable],
kind: SwiftDeclarationKind) -> [[String: SourceKitRepresentable]] {

let substructure = dictionary["key.substructure"] as? [SourceKitRepresentable] ?? []
return substructure.flatMap { subItem -> [[String: SourceKitRepresentable]] in
guard let subDict = subItem as? [String: SourceKitRepresentable],
let kindString = subDict["key.kind"] as? String else {
return []
}

if SwiftDeclarationKind(rawValue: kindString) == kind {
return [subDict]
}

if SwiftExpressionKind(rawValue: kindString) == .argument {
return filterByKind(dictionary: subDict, kind: kind)
}

return []
}
}
}
28 changes: 2 additions & 26 deletions Source/SwiftLintFramework/Rules/UnusedClosureParameterRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// UnusedClosureParameterRule.swift
// SwiftLint
//
// Created by Marcelo Fabri on 15/12/16.
// Created by Marcelo Fabri on 12/15/16.
// Copyright © 2016 Realm. All rights reserved.
//

Expand Down Expand Up @@ -41,8 +41,6 @@ public struct UnusedClosureParameterRule: ASTRule, ConfigurationProviderRule {
]
)

private static let emptyParenthesesRegex = regex("^\\s*\\(\\s*\\)")

public func validateFile(_ file: File,
kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
Expand All @@ -61,7 +59,7 @@ public struct UnusedClosureParameterRule: ASTRule, ConfigurationProviderRule {

let rangeStart = nameOffset + nameLength
let rangeLength = (offset + length) - (nameOffset + nameLength)
let parameters = filterByKind(dictionary: dictionary, kind: .varParameter)
let parameters = dictionary.enclosedVarParameters
let contents = file.contents.bridge()

return parameters.flatMap { param -> StyleViolation? in
Expand Down Expand Up @@ -109,26 +107,4 @@ public struct UnusedClosureParameterRule: ASTRule, ConfigurationProviderRule {
reason: reason)
}
}

private func filterByKind(dictionary: [String: SourceKitRepresentable],
kind: SwiftDeclarationKind) -> [[String: SourceKitRepresentable]] {

let substructure = dictionary["key.substructure"] as? [SourceKitRepresentable] ?? []
return substructure.flatMap { subItem -> [[String: SourceKitRepresentable]] in
guard let subDict = subItem as? [String: SourceKitRepresentable],
let kindString = subDict["key.kind"] as? String else {
return []
}

if SwiftDeclarationKind(rawValue: kindString) == kind {
return [subDict]
}

if SwiftExpressionKind(rawValue: kindString) == .argument {
return filterByKind(dictionary: subDict, kind: kind)
}

return []
}
}
}

0 comments on commit e854701

Please sign in to comment.