Files
SwiftLint/Source/SwiftLintFramework/Rules/MarkRule.swift
T
2016-11-23 15:30:29 -08:00

59 lines
1.8 KiB
Swift

//
// MarkRule.swift
// SwiftLint
//
// Created by Krzysztof Rodak on 22/08/16.
// Copyright © 2016 Realm. All rights reserved.
//
import SourceKittenFramework
public struct MarkRule: ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.Warning)
public init() {}
public static let description = RuleDescription(
identifier: "mark",
name: "Mark",
description: "MARK comment should be in valid format.",
nonTriggeringExamples: [
"// MARK: good",
"// MARK: - good",
"// MARK: -"
],
triggeringExamples: [
"//MARK: bad",
"// MARK:bad",
"//MARK:bad",
"// MARK: bad",
"// MARK: bad",
"// MARK: -bad",
"// MARK:- bad",
"// MARK:-bad",
"//MARK: - bad",
"//MARK:- bad",
"//MARK: -bad",
"//MARK:-bad",
]
)
public func validateFile(file: File) -> [StyleViolation] {
let nonSpace = "[^ ]"
let twoOrMoreSpace = " {2,}"
let nonSpaceOrTwoOrMoreSpace = "(\(nonSpace)|\(twoOrMoreSpace))"
let mark = "MARK:"
let badSpaceStart = "(\(nonSpaceOrTwoOrMoreSpace)?\(mark)\(nonSpaceOrTwoOrMoreSpace))"
let badSpaceEnd = "(\(nonSpaceOrTwoOrMoreSpace)\(mark)\(nonSpaceOrTwoOrMoreSpace)?)"
let badSpaceAfterHyphen = "(\(mark) -([^ \\n]|\(twoOrMoreSpace)))"
let pattern = [badSpaceStart, badSpaceEnd, badSpaceAfterHyphen].joinWithSeparator("|")
return file.matchPattern(pattern, withSyntaxKinds: [.Comment]).flatMap { range in
return StyleViolation(ruleDescription: self.dynamicType.description,
severity: configuration.severity,
location: Location(file: file, characterOffset: range.location))
}
}
}