Files
SwiftLint/Source/SwiftLintFramework/Rules/StatementPositionRule.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

44 lines
1.2 KiB
Swift

//
// StatementPositionRule.swift
// SwiftLint
//
// Created by Alex Culeva on 10/22/15.
// Copyright © 2015 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct StatementPositionRule: Rule {
public init() {}
public static let description = RuleDescription(
identifier: "statement_position",
name: "Statement Position",
description: "Else and catch should be on the same line, one space after the previous " +
"declaration.",
nonTriggeringExamples: [
"} else if {",
"} else {",
"} catch {",
"\"}else{\""
],
triggeringExamples: [
"}else if {",
"} else {",
"}\ncatch {",
"}\n\t catch {"
]
)
public func validateFile(file: File) -> [StyleViolation] {
let pattern = "((?:\\}|[\\s] |[\\n\\t\\r])(?:else|catch))"
let excludingKinds = SyntaxKind.commentAndStringKinds()
return file.matchPattern(pattern, excludingSyntaxKinds: excludingKinds).flatMap {
StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: $0.location))
}
}
}