Skip to content

Commit

Permalink
fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulTaykalo committed Oct 26, 2019
1 parent 3bd9c14 commit c687c22
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 49 deletions.
20 changes: 7 additions & 13 deletions Source/SwiftLintFramework/Extensions/Dictionary+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import SourceKittenFramework

public struct SourceKittenDictionary {
public let value: [String: SourceKitRepresentable]
private let _substructure: [SourceKittenDictionary]
public let substructure: [SourceKittenDictionary]

init(_ value: [String: SourceKitRepresentable]) {
self.value = value

let substructure = value["key.substructure"] as? [SourceKitRepresentable] ?? []
_substructure = substructure.compactMap { $0 as? [String: SourceKitRepresentable] }
self.substructure = substructure.compactMap { $0 as? [String: SourceKitRepresentable] }
.map(SourceKittenDictionary.init)
}

Expand Down Expand Up @@ -81,22 +82,15 @@ public struct SourceKittenDictionary {

var swiftAttributes: [SourceKittenDictionary] {
let array = value["key.attributes"] as? [SourceKitRepresentable] ?? []
let dictionaries = array.compactMap { ($0 as? SourceKittenDictionary) }
let dictionaries = array.compactMap { $0 as? [String: SourceKitRepresentable] }
.map(SourceKittenDictionary.init)
return dictionaries
}

var substructure: [SourceKittenDictionary] {
return _substructure
}

var elements: [SourceKittenDictionary] {
let elements = value["key.elements"] as? [SourceKitRepresentable] ?? []
return elements.compactMap { $0 as? SourceKittenDictionary }
}

var entities: [SourceKittenDictionary] {
let entities = value["key.entities"] as? [SourceKitRepresentable] ?? []
return entities.compactMap { $0 as? SourceKittenDictionary }
return elements.compactMap { $0 as? [String: SourceKitRepresentable] }
.map(SourceKittenDictionary.init)
}

var enclosedVarParameters: [SourceKittenDictionary] {
Expand Down
4 changes: 3 additions & 1 deletion Source/SwiftLintFramework/Extensions/File+Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ private var structureCache = Cache({ file -> Structure? in
return nil
})

private var structureDictionaryCache = Cache({ file in structureCache.get(file).map { SourceKittenDictionary($0.dictionary) } })
private var structureDictionaryCache = Cache({ file in
return structureCache.get(file).map { SourceKittenDictionary($0.dictionary) }
})

private var syntaxMapCache = Cache({ file in responseCache.get(file).map(SyntaxMap.init) })
private var syntaxKindsByLinesCache = Cache({ file in file.syntaxKindsByLine() })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ extension Structure {
/// that contains the byte offset. Returns all kinds if no parameter specified.
///
/// - Parameter byteOffset: Int?
internal func kinds(forByteOffset byteOffset: Int? = nil, in dictionary: SourceKittenDictionary) -> [(kind: String, byteRange: NSRange)] {
internal func kinds(forByteOffset byteOffset: Int? = nil, in dictionary: SourceKittenDictionary)
-> [(kind: String, byteRange: NSRange)] {
var results = [(kind: String, byteRange: NSRange)]()

func parse(_ dictionary: SourceKittenDictionary) {
Expand All @@ -26,7 +27,8 @@ extension Structure {
return results
}

internal func structures(forByteOffset byteOffset: Int, in dictionary: SourceKittenDictionary) -> [SourceKittenDictionary] {
internal func structures(forByteOffset byteOffset: Int, in dictionary: SourceKittenDictionary)
-> [SourceKittenDictionary] {
var results = [SourceKittenDictionary]()

func parse(_ dictionary: SourceKittenDictionary) {
Expand Down
3 changes: 2 additions & 1 deletion Source/SwiftLintFramework/Models/SwiftVersion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public extension SwiftVersion {
return token.type == SyntaxKind.string.rawValue
}
if !Request.disableSourceKit,
let decl = file.structure.kinds(in: file.structureDictionary).first(where: { $0.kind == SwiftDeclarationKind.varGlobal.rawValue }),
let decl = file.structure.kinds(in: file.structureDictionary)
.first(where: { $0.kind == SwiftDeclarationKind.varGlobal.rawValue }),
let token = file.syntaxMap.tokens(inByteRange: decl.byteRange).first(where: isString ) {
return .init(rawValue: file.contents.substring(from: token.offset + 1, length: token.length - 2))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ public struct NimbleOperatorRule: ConfigurationProviderRule, OptInRule, Correcta
return false
}

let containsCall = file.structure.structures(forByteOffset: byteRange.upperBound - 1, in: file.structureDictionary)
let containsCall = file.structure.structures(forByteOffset: byteRange.upperBound - 1,
in: file.structureDictionary)
.contains(where: { dict -> Bool in
return dict.kind.flatMap(SwiftExpressionKind.init) == .call &&
(dict.name ?? "").starts(with: "expect")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public struct UnneededBreakInSwitchRule: ConfigurationProviderRule, AutomaticTes
return file.match(pattern: "break", with: [.keyword]).compactMap { range in
let contents = file.contents.bridge()
guard let byteRange = contents.NSRangeToByteRange(start: range.location, length: range.length),
let innerStructure = file.structure.structures(forByteOffset: byteRange.location, in: file.structureDictionary).last,
let innerStructure = file.structure.structures(forByteOffset: byteRange.location,
in: file.structureDictionary).last,
innerStructure.kind.flatMap(StatementKind.init) == .case,
let caseOffset = innerStructure.offset,
let caseLength = innerStructure.length,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public struct DiscardedNotificationCenterObserverRule: ASTRule, ConfigurationPro
}
}

private func functions(forByteOffset byteOffset: Int, in dictionary: SourceKittenDictionary) -> [SourceKittenDictionary] {
private func functions(forByteOffset byteOffset: Int, in dictionary: SourceKittenDictionary)
-> [SourceKittenDictionary] {
var results = [SourceKittenDictionary]()

func parse(_ dictionary: SourceKittenDictionary) {
Expand Down
3 changes: 2 additions & 1 deletion Source/SwiftLintFramework/Rules/Lint/InertDeferRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public struct InertDeferRule: ConfigurationProviderRule, AutomaticTestableRule {
return defers.compactMap { range -> StyleViolation? in
let contents = file.contents.bridge()
guard let byteRange = contents.NSRangeToByteRange(start: range.location, length: range.length),
case let kinds = file.structure.kinds(forByteOffset: byteRange.upperBound, in: file.structureDictionary),
case let kinds = file.structure.kinds(forByteOffset: byteRange.upperBound,
in: file.structureDictionary),
let brace = kinds.enumerated().lazy.reversed().first(where: isBrace),
brace.offset > kinds.startIndex,
case let outerKindIndex = kinds.index(before: brace.offset),
Expand Down
20 changes: 0 additions & 20 deletions Source/SwiftLintFramework/Rules/Lint/UnusedImportRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,26 +334,6 @@ private extension Dictionary where Value == SourceKitRepresentable, Key == Strin
let entities = self["key.entities"] as? [SourceKitRepresentable] ?? []
return entities.compactMap { $0 as? [String: SourceKitRepresentable] }
}

var enclosedSwiftAttributes: [SwiftDeclarationAttributeKind] {
return swiftAttributes.compactMap { $0.attribute }
.compactMap(SwiftDeclarationAttributeKind.init(rawValue:))
}

var swiftAttributes: [[String: SourceKitRepresentable]] {
let array = self["key.attributes"] as? [SourceKitRepresentable] ?? []
let dictionaries = array.compactMap { ($0 as? [String: SourceKitRepresentable]) }
return dictionaries
}

var attribute: String? {
return self["key.attribute"] as? String
}

var substructure: [[String: SourceKitRepresentable]] {
let substructure = self["key.substructure"] as? [SourceKitRepresentable] ?? []
return substructure.compactMap { $0 as? [String: SourceKitRepresentable] }
}
}

private let syntaxKindsToSkip: Set<SyntaxKind> = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public struct UnusedSetterValueRule: ConfigurationProviderRule, AutomaticTestabl

let violatingLocations = setTokens.compactMap { setToken -> Int? in
// the last element is the deepest structure
guard let dict = declarations(forByteOffset: setToken.offset, structureDictionary: file.structureDictionary).last,
guard let dict = declarations(forByteOffset: setToken.offset,
structureDictionary: file.structureDictionary).last,
let bodyOffset = dict.bodyOffset, let bodyLength = dict.bodyLength,
case let contents = file.contents.bridge(),
let propertyRange = contents.byteRangeToNSRange(start: bodyOffset, length: bodyLength),
Expand Down Expand Up @@ -177,7 +178,8 @@ public struct UnusedSetterValueRule: ConfigurationProviderRule, AutomaticTestabl
let getTokens = file.rangesAndTokens(matching: "\\bget\\b", range: range).keywordTokens()
return getTokens.first(where: { token -> Bool in
// the last element is the deepest structure
guard let dict = declarations(forByteOffset: token.offset, structureDictionary: file.structureDictionary).last,
guard let dict = declarations(forByteOffset: token.offset,
structureDictionary: file.structureDictionary).last,
propertyStructure.value.isEqualTo(dict.value) else {
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public struct ControlStatementRule: ConfigurationProviderRule, AutomaticTestable
.filter { match, _ -> Bool in
let contents = file.contents.bridge()
guard let byteOffset = contents.NSRangeToByteRange(start: match.location, length: 1)?.location,
let outerKind = file.structure.kinds(forByteOffset: byteOffset, in: file.structureDictionary).last else {
let outerKind = file.structure.kinds(forByteOffset: byteOffset,
in: file.structureDictionary).last else {
return true
}

Expand Down
11 changes: 7 additions & 4 deletions Source/SwiftLintFramework/Rules/Style/ImplicitGetterRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ public struct ImplicitGetterRule: ConfigurationProviderRule, AutomaticTestableRu

let violatingLocations = getTokens.compactMap { token -> (Int, SwiftDeclarationKind?)? in
// the last element is the deepest structure
guard let dict = declarations(forByteOffset: token.offset, structureDictionary: file.structureDictionary).last else {
guard let dict = declarations(forByteOffset: token.offset,
structureDictionary: file.structureDictionary).last else {
return nil
}

Expand All @@ -216,12 +217,14 @@ public struct ImplicitGetterRule: ConfigurationProviderRule, AutomaticTestableRu
reason: reason)
}
}
}

private func declarations(forByteOffset byteOffset: Int,
structureDictionary: SourceKittenDictionary) -> [SourceKittenDictionary] {
private extension ImplicitGetterRule {
func declarations(forByteOffset byteOffset: Int,
structureDictionary: SourceKittenDictionary) -> [SourceKittenDictionary] {
var results = [SourceKittenDictionary]()
let allowedKinds = SwiftDeclarationKind.variableKinds.subtracting([.varParameter])
.union([.functionSubscript])
.union([.functionSubscript])

func parse(dictionary: SourceKittenDictionary, parentKind: SwiftDeclarationKind?) {
// Only accepts declarations which contains a body and contains the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public struct ImplicitReturnRule: ConfigurationProviderRule, SubstitutionCorrect
guard kinds == [.keyword, .keyword] || kinds == [.keyword],
let byteRange = contents.NSRangeToByteRange(start: range.location,
length: range.length),
let outerKindString = file.structure.kinds(forByteOffset: byteRange.location, in: file.structureDictionary).last?.kind,
let outerKindString = file.structure.kinds(forByteOffset: byteRange.location,
in: file.structureDictionary).last?.kind,
let outerKind = SwiftExpressionKind(rawValue: outerKindString),
[.call, .argument, .closure].contains(outerKind) else {
return nil
Expand Down

0 comments on commit c687c22

Please sign in to comment.