forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberSeparatorRule.swift
157 lines (127 loc) · 6.03 KB
/
NumberSeparatorRule.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// NumberSeparatorRule.swift
// SwiftLint
//
// Created by Marcelo Fabri on 12/05/16.
// Copyright © 2016 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct NumberSeparatorRule: OptInRule, CorrectableRule, ConfigurationProviderRule {
public var configuration = NumberSeparatorConfiguration(minimumLength: 0, minimumFractionLength: nil)
public init() {}
public static let description = RuleDescription(
identifier: "number_separator",
name: "Number Separator",
description: "Underscores should be used as thousand separator in large decimal numbers.",
kind: .style,
nonTriggeringExamples: NumberSeparatorRuleExamples.nonTriggeringExamples,
triggeringExamples: NumberSeparatorRuleExamples.triggeringExamples,
corrections: NumberSeparatorRuleExamples.corrections
)
public func validate(file: File) -> [StyleViolation] {
return violatingRanges(in: file).map { range, _ in
return StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severityConfiguration.severity,
location: Location(file: file, characterOffset: range.location))
}
}
private func violatingRanges(in file: File) -> [(NSRange, String)] {
let numberTokens = file.syntaxMap.tokens.filter { SyntaxKind(rawValue: $0.type) == .number }
return numberTokens.flatMap { token in
guard let content = contentFrom(file: file, token: token),
isDecimal(number: content) else {
return nil
}
let signs = CharacterSet(charactersIn: "+-")
let exponential = CharacterSet(charactersIn: "eE")
guard let nonSign = content.components(separatedBy: signs).last,
case let exponentialComponents = nonSign.components(separatedBy: exponential),
let nonExponential = exponentialComponents.first else {
return nil
}
let components = nonExponential.components(separatedBy: ".")
var validFraction = true
var expectedFraction: String?
if components.count == 2, let fractionSubstring = components.last {
let result = isValid(number: fractionSubstring, isFraction: true)
validFraction = result.0
expectedFraction = result.1
}
guard let integerSubstring = components.first,
case let (valid, expected) = isValid(number: integerSubstring, isFraction: false),
!valid || !validFraction,
let range = file.contents.bridge().byteRangeToNSRange(start: token.offset,
length: token.length) else {
return nil
}
var corrected = ""
let hasSign = content.countOfLeadingCharacters(in: signs) == 1
if hasSign {
corrected += String(content.characters.prefix(1))
}
corrected += expected
if let fraction = expectedFraction {
corrected += "." + fraction
}
if exponentialComponents.count == 2, let exponential = exponentialComponents.last {
let exponentialSymbol = content.contains("e") ? "e" : "E"
corrected += exponentialSymbol + exponential
}
return (range, corrected)
}
}
public func correct(file: File) -> [Correction] {
let violatingRanges = self.violatingRanges(in: file).filter { range, _ in
return !file.ruleEnabled(violatingRanges: [range], for: self).isEmpty
}
var correctedContents = file.contents
var adjustedLocations = [Int]()
for (violatingRange, correction) in violatingRanges.reversed() {
if let indexRange = correctedContents.nsrangeToIndexRange(violatingRange) {
correctedContents = correctedContents.replacingCharacters(in: indexRange, with: correction)
adjustedLocations.insert(violatingRange.location, at: 0)
}
}
file.write(correctedContents)
return adjustedLocations.map {
Correction(ruleDescription: type(of: self).description,
location: Location(file: file, characterOffset: $0))
}
}
private func isDecimal(number: String) -> Bool {
let lowercased = number.lowercased()
let prefixes = ["0x", "0o", "0b"].flatMap { [$0, "-" + $0, "+" + $0] }
return prefixes.filter { lowercased.hasPrefix($0) }.isEmpty
}
private func isValid(number: String, isFraction: Bool) -> (Bool, String) {
var correctComponents = [String]()
let clean = number.replacingOccurrences(of: "_", with: "")
let minimumLength: Int
if isFraction {
minimumLength = configuration.minimumFractionLength ?? configuration.minimumLength
} else {
minimumLength = configuration.minimumLength
}
let shouldAddSeparators = clean.characters.count >= minimumLength
for (idx, char) in reversedIfNeeded(Array(clean.characters),
reversed: !isFraction).enumerated() {
if idx % 3 == 0 && idx > 0 && shouldAddSeparators {
correctComponents.append("_")
}
correctComponents.append(String(char))
}
let expected = reversedIfNeeded(correctComponents, reversed: !isFraction).joined()
return (expected == number, expected)
}
private func reversedIfNeeded<T>(_ array: [T], reversed: Bool) -> [T] {
if reversed {
return array.reversed()
}
return array
}
private func contentFrom(file: File, token: SyntaxToken) -> String? {
return file.contents.bridge().substringWithByteRange(start: token.offset,
length: token.length)
}
}