forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColonRule+Dictionary.swift
79 lines (65 loc) · 2.69 KB
/
ColonRule+Dictionary.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// ColonRule+Dictionary.swift
// SwiftLint
//
// Created by Marcelo Fabri on 09/13/17.
// Copyright © 2017 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
extension ColonRule {
internal func dictionaryColonViolationRanges(in file: File,
dictionary: [String: SourceKitRepresentable]) -> [NSRange] {
guard configuration.applyToDictionaries else {
return []
}
return dictionary.substructure.flatMap { subDict -> [NSRange] in
guard let kindString = subDict.kind,
let kind = KindType(rawValue: kindString) else {
return []
}
return dictionaryColonViolationRanges(in: file, dictionary: subDict) +
dictionaryColonViolationRanges(in: file, kind: kind, dictionary: subDict)
}
}
internal func dictionaryColonViolationRanges(in file: File, kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [NSRange] {
guard kind == .dictionary,
let ranges = dictionaryColonRanges(dictionary: dictionary) 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 dictionaryColonRanges(dictionary: [String: SourceKitRepresentable]) -> [NSRange]? {
let elements = dictionary.elements
guard elements.count % 2 == 0 else {
return nil
}
let expectedKind = "source.lang.swift.structure.elem.expr"
let ranges: [NSRange] = elements.flatMap { subDict in
guard subDict.kind == expectedKind,
let offset = subDict.offset,
let length = subDict.length else {
return nil
}
return NSRange(location: offset, length: length)
}
let even = ranges.enumerated().flatMap { $0 % 2 == 0 ? $1 : nil }
let odd = ranges.enumerated().flatMap { $0 % 2 != 0 ? $1 : nil }
return zip(even, odd).map { evenRange, oddRange -> NSRange in
let location = NSMaxRange(evenRange)
let length = oddRange.location - location
return NSRange(location: location, length: length)
}
}
}