-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
CommaRule.swift
110 lines (94 loc) · 4.46 KB
/
CommaRule.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
//
// Comma.swift
// SwiftLint
//
// Created by Alex Culeva on 10/22/15.
// Copyright © 2015 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct CommaRule: CorrectableRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.Warning)
public init() {}
public static let description = RuleDescription(
identifier: "comma",
name: "Comma Spacing",
description: "There should be no space before and one after any comma.",
nonTriggeringExamples: [
"func abc(a: String, b: String) { }",
"abc(a: \"string\", b: \"string\"",
"enum a { case a, b, c }"
],
triggeringExamples: [
"func abc(a: String↓ ,b: String) { }",
"abc(a: \"string\"↓,b: \"string\"",
"enum a { case a↓ ,b }"
],
corrections: [
"func abc(a: String,b: String) {}\n": "func abc(a: String, b: String) {}\n",
"abc(a: \"string\",b: \"string\"\n": "abc(a: \"string\", b: \"string\"\n",
"abc(a: \"string\" , b: \"string\"\n": "abc(a: \"string\", b: \"string\"\n",
"enum a { case a ,b }\n": "enum a { case a, b }\n",
"let a = [1,1]\nlet b = 1\nf(1, b)\n": "let a = [1, 1]\nlet b = 1\nf(1, b)\n",
]
)
public func validateFile(file: File) -> [StyleViolation] {
let pattern = "(\\,[^\\s])|(\\s\\,)"
let excludingKinds = SyntaxKind.commentAndStringKinds()
return file.matchPattern(pattern, excludingSyntaxKinds: excludingKinds).map {
StyleViolation(ruleDescription: self.dynamicType.description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
}
}
public func correctFile(file: File) -> [Correction] {
if validateFile(file).isEmpty { return [] }
// captures spaces and comma only
let pattern =
"\\S" + // not whitespace
"(" + // start capure
"\\s+" + // followed by whitespace
"," + // to the left of a comma
"\\s*" + // followed by any amount of whitespace.
"|" + // or
"," + // immediately followed by a comma
"(?:\\s{0}|\\s{2,})" + // followed by 0 or 2+ whitespace characters.
")" + // end capture
"\\S" // not whitespace
var contents = file.contents as NSString
let excludingSyntaxKinds = SyntaxKind.commentAndStringKinds().map { $0.rawValue }
let tokens = file.syntaxMap.tokens
let regularExpression = regex(pattern)
let range = NSRange(location: 0, length: contents.length)
let matches = regularExpression
.matchesInString(contents as String, options: [], range: range)
.flatMap { match -> NSRange? in
if match.numberOfRanges != 2 { return nil }
// use captured range
let range1 = match.rangeAtIndex(1)
guard let matchByteRange = contents
.NSRangeToByteRange(start: range1.location, length: range1.length)
else { return nil }
// captured range won't match tokens if it is not comment neither string.
let tokensInRange = tokens.filter { token in
let tokenByteRange = NSRange(location: token.offset, length: token.length)
return NSIntersectionRange(matchByteRange, tokenByteRange).length > 0
}.filter { excludingSyntaxKinds.contains($0.type) }
// If not empty, captured range is comment or string
if !tokensInRange.isEmpty {
return nil
}
// return captured range
return range1
}
let description = self.dynamicType.description
var corrections = [Correction]()
for range in matches.reverse() {
contents = contents.stringByReplacingCharactersInRange(range, withString: ", ")
let location = Location(file: file, characterOffset: range.location)
corrections.append(Correction(ruleDescription: description, location: location))
}
file.write(contents as String)
return corrections
}
}