allow trailing semicolons in comments

This commit is contained in:
JP Simard
2015-11-17 11:41:02 -08:00
parent 0a2b9e8a54
commit d269bde1fa
6 changed files with 32 additions and 23 deletions
@@ -47,7 +47,7 @@ extension File {
}
public func matchPattern(pattern: String) -> [(NSRange, [SyntaxKind])] {
let regex = try! NSRegularExpression(pattern: pattern, options: [])
let regex = try! NSRegularExpression(pattern: pattern, options: [.AnchorsMatchLines])
let range = NSRange(location: 0, length: contents.utf16.count)
let syntax = syntaxMap
let matches = regex.matchesInString(contents, options: [], range: range)
@@ -78,7 +78,7 @@ extension File {
*/
public func matchPattern(pattern: String,
excludingSyntaxKinds syntaxKinds: [SyntaxKind]) -> [NSRange] {
let regex = try! NSRegularExpression(pattern: pattern, options: [])
let regex = try! NSRegularExpression(pattern: pattern, options: [.AnchorsMatchLines])
let range = NSRange(location: 0, length: contents.utf16.count)
let syntax = syntaxMap
let matches = regex.matchesInString(contents, options: [], range: range)
@@ -18,15 +18,11 @@ public struct Linter {
public var styleViolations: [StyleViolation] {
let regions = file.regions()
return rules.flatMap { rule in
return rule.validateFile(self.file).filter { styleViolation in
guard let violationRegion = regions.filter({
$0.start <= styleViolation.location && $0.end >= styleViolation.location
}).first else {
return rule.validateFile(self.file).filter { violation in
guard let violationRegion = regions.filter({ $0.contains(violation) }).first else {
return true
}
return !violationRegion.disabledRuleIdentifiers.contains(
rule.dynamicType.description.identifier
)
return violationRegion.isRuleEnabled(rule)
}
}
}
@@ -19,4 +19,16 @@ public struct Region {
self.end = end
self.disabledRuleIdentifiers = disabledRuleIdentifiers
}
public func contains(violation: StyleViolation) -> Bool {
return start <= violation.location && end >= violation.location
}
public func isRuleEnabled(rule: Rule) -> Bool {
return !isRuleDisabled(rule)
}
public func isRuleDisabled(rule: Rule) -> Bool {
return disabledRuleIdentifiers.contains(rule.dynamicType.description.identifier)
}
}
@@ -16,14 +16,17 @@ public struct TrailingSemicolonRule: Rule {
name: "Trailing Semicolon",
description: "Lines should not have trailing semicolons.",
nonTriggeringExamples: [ "let a = 0\n" ],
triggeringExamples: [ "let a = 0;\n" ]
triggeringExamples: [
"let a = 0;\n",
"let a = 0;\nlet b = 1\n"
]
)
public func validateFile(file: File) -> [StyleViolation] {
return file.lines.filter { $0.content.hasSuffix(";") }.map {
let excludingKinds = SyntaxKind.commentAndStringKinds()
return file.matchPattern(";$", excludingSyntaxKinds: excludingKinds).flatMap {
StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file.path, line: $0.index),
reason: "Line #\($0.index) should have no trailing semicolon")
location: Location(file: file, offset: $0.location))
}
}
}
@@ -90,6 +90,6 @@ class StringRuleTests: XCTestCase {
}
func testTrailingSemicolon() {
verifyRule(TrailingSemicolonRule.description, commentDoesntViolate: false)
verifyRule(TrailingSemicolonRule.description)
}
}
@@ -19,8 +19,7 @@ private func violations(string: String, _ description: RuleDescription) -> [Styl
}
extension XCTestCase {
func verifyRule(ruleDescription: RuleDescription,
commentDoesntViolate: Bool = true) {
func verifyRule(ruleDescription: RuleDescription, commentDoesntViolate: Bool = true) {
XCTAssertEqual(
ruleDescription.nonTriggeringExamples.flatMap({violations($0, ruleDescription)}),
[]
@@ -29,15 +28,14 @@ extension XCTestCase {
ruleDescription.triggeringExamples.flatMap({
violations($0, ruleDescription).map({$0.ruleDescription})
}),
Array(count: ruleDescription.triggeringExamples.count, repeatedValue: ruleDescription))
Array(count: ruleDescription.triggeringExamples.count, repeatedValue: ruleDescription)
)
if commentDoesntViolate {
XCTAssertEqual(
ruleDescription.triggeringExamples.flatMap({
violations("/** " + $0, ruleDescription)
}),
[]
)
let commentedViolations = ruleDescription.triggeringExamples.flatMap {
violations("/** " + $0, ruleDescription)
}
XCTAssertEqual(commentedViolations, [])
}
let command = "// swiftlint:disable \(ruleDescription.identifier)\n"