forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommaRule.swift
42 lines (37 loc) · 1.36 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
//
// Comma.swift
// SwiftLint
//
// Created by Alex Culeva on 10/22/15.
// Copyright © 2015 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct CommaRule: Rule {
public init() {}
public static let description = RuleDescription(
identifier: "comma",
name: "Comma Spacing",
description: "One space before and no after must be present next to 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 }"
]
)
public func validateFile(file: File) -> [StyleViolation] {
let pattern = "(\\,[^\\s])|(\\s\\,)"
let excludingKinds: [SyntaxKind] = [.Comment, .CommentMark, .CommentURL,
.DocComment, .DocCommentField, .String]
return file.matchPattern(pattern, excludingSyntaxKinds: excludingKinds).flatMap { match in
return StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: match.location),
reason: "One space before and no after must be present next to commas")
}
}
}