Files
SwiftLint/Source/SwiftLintFramework/Rules/ColonRule.swift
T
JP Simard 989127cbe0 [StyleViolation] use RuleDescription's description when reason is nil
Many cases just used a static string that was nearly identical to the rule
description as the `reason` parameter when initializing a StyleViolation.
2015-11-17 10:25:57 -08:00

61 lines
1.9 KiB
Swift

//
// ColonRule.swift
// SwiftLint
//
// Created by JP Simard on 2015-05-16.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
public struct ColonRule: Rule {
public init() {}
public static let description = RuleDescription(
identifier: "colon",
name: "Colon",
description: "Colons should be next to the identifier when specifying a type.",
nonTriggeringExamples: [
"let abc: Void\n",
"let abc: [Void: Void]\n",
"let abc: (Void, Void)\n",
"let abc: String=\"def\"\n",
"let abc: Int=0\n",
"let abc: Enum=Enum.Value\n",
"func abc(def: Void) {}\n",
"func abc(def: Void, ghi: Void) {}\n"
],
triggeringExamples: [
"let abc:Void\n",
"let abc: Void\n",
"let abc :Void\n",
"let abc : Void\n",
"let abc : [Void: Void]\n",
"let abc :String=\"def\"\n",
"let abc :Int=0\n",
"let abc :Int = 0\n",
"let abc:Int=0\n",
"let abc:Int = 0\n",
"let abc:Enum=Enum.Value\n",
"func abc(def:Void) {}\n",
"func abc(def: Void) {}\n",
"func abc(def :Void) {}\n",
"func abc(def : Void) {}\n",
"func abc(def: Void, ghi :Void) {}\n"
]
)
public func validateFile(file: File) -> [StyleViolation] {
let pattern = "\\w+\\s+:\\s*\\S+|\\w+:(?:\\s{0}|\\s{2,})\\S+"
return file.matchPattern(pattern).flatMap { range, syntaxKinds in
if !syntaxKinds.startsWith([.Identifier, .Typeidentifier]) {
return nil
}
return StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: range.location))
}
}
}