SwiftLint/Source/SwiftLintFramework/Rules/Style/ColonRule+FunctionCall.swift

54 lines
2.2 KiB
Swift

import Foundation
import SourceKittenFramework
extension ColonRule {
internal func functionCallColonViolationRanges(in file: File,
dictionary: [String: SourceKitRepresentable]) -> [NSRange] {
return dictionary.substructure.flatMap { subDict -> [NSRange] in
var ranges: [NSRange] = []
if let kindString = subDict.kind,
let kind = KindType(rawValue: kindString) {
ranges += functionCallColonViolationRanges(in: file, kind: kind, dictionary: subDict)
}
ranges += functionCallColonViolationRanges(in: file, dictionary: subDict)
return ranges
}
}
internal func functionCallColonViolationRanges(in file: File, kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [NSRange] {
guard kind == .argument,
let ranges = functionCallColonRanges(dictionary: dictionary, file: file) else {
return []
}
let contents = file.contents.bridge()
return ranges.filter {
guard let colon = contents.substringWithByteRange(start: $0.location, length: $0.length) else {
return false
}
if configuration.flexibleRightSpacing {
let isCorrect = colon.hasPrefix(": ") || colon.hasPrefix(":\n")
return !isCorrect
}
return colon != ": " && !colon.hasPrefix(":\n")
}
}
private func functionCallColonRanges(dictionary: [String: SourceKitRepresentable], file: File) -> [NSRange]? {
guard let nameOffset = dictionary.nameOffset,
let nameLength = dictionary.nameLength, nameLength > 0,
let bodyOffset = dictionary.bodyOffset,
case let location = nameOffset + nameLength,
bodyOffset > location,
case let nameRange = NSRange(location: nameOffset, length: nameLength),
file.syntaxMap.kinds(inByteRange: nameRange) != [.objectLiteral] else {
return nil
}
return [NSRange(location: location, length: bodyOffset - location)]
}
}