mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
a2a636d749
fixes #805
59 lines
1.8 KiB
Swift
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))
|
|
}
|
|
}
|
|
}
|