Files
SwiftLint/Source/SwiftLintFramework/Rules/Style/ColonRule+FunctionCall.swift
JP Simard fe5baca7cd Migrate to use SourceKitten's new ByteCount/ByteRange types (#3037)
New APIs were introduced in SourceKitten to allow for a more typesafe distinction between integers meaning NSString-based distances and byte-based distances.

* https://github.com/jpsim/SourceKitten/pull/639
* https://github.com/jpsim/SourceKitten/pull/642

This PR migrates SwiftLint's use of those APIs.
2020-01-16 15:18:37 -08:00

48 lines
1.7 KiB
Swift

import SourceKittenFramework
extension ColonRule {
internal func functionCallColonViolationRanges(in file: SwiftLintFile,
dictionary: SourceKittenDictionary) -> [ByteRange] {
return dictionary.traverseDepthFirst { subDict in
guard let kind = subDict.expressionKind else { return nil }
return functionCallColonViolationRanges(in: file, kind: kind, dictionary: subDict)
}
}
internal func functionCallColonViolationRanges(in file: SwiftLintFile, kind: SwiftExpressionKind,
dictionary: SourceKittenDictionary) -> [ByteRange] {
guard kind == .argument,
let ranges = functionCallColonRanges(dictionary: dictionary)
else {
return []
}
let contents = file.stringView
return ranges.filter {
guard let colon = contents.substringWithByteRange($0) else {
return false
}
if configuration.flexibleRightSpacing {
let isCorrect = colon.hasPrefix(": ") || colon.hasPrefix(":\n")
return !isCorrect
}
return colon != ": " && !colon.hasPrefix(":\n")
}
}
private func functionCallColonRanges(dictionary: SourceKittenDictionary) -> [ByteRange]? {
guard let nameOffset = dictionary.nameOffset,
let nameLength = dictionary.nameLength, nameLength > 0,
let bodyOffset = dictionary.bodyOffset,
case let location = nameOffset + nameLength,
bodyOffset > location
else {
return nil
}
return [ByteRange(location: location, length: bodyOffset - location)]
}
}