fix MarkRule edge cases

fixes #805
This commit is contained in:
JP Simard
2016-11-23 15:30:29 -08:00
parent 1b829091d5
commit a2a636d749
2 changed files with 23 additions and 7 deletions
+4 -1
View File
@@ -10,7 +10,10 @@
##### Bug Fixes
* None.
* Fix a few edge cases where malformed `MARK:` comments wouldn't trigger a
violation.
[JP Simard](https://github.com/jpsim)
[#805](https://github.com/realm/SwiftLint/issues/805)
## 0.13.0: MakeYourClothesCleanAgain
+19 -6
View File
@@ -19,22 +19,35 @@ public struct MarkRule: ConfigurationProviderRule {
name: "Mark",
description: "MARK comment should be in valid format.",
nonTriggeringExamples: [
"// MARK: good\n",
"// MARK: - good\n",
"// MARK: -\n"
"// 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",
"//MARK:-bad",
]
)
public func validateFile(file: File) -> [StyleViolation] {
let options = ["MARK:[^ ]", "[^ ]MARK: [^-]", "\\sMARK:[^ ]", "MARK:[ ][-][^\\s ]"]
let pattern = "(" + options.joinWithSeparator("|") + ")"
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,