Update RegexConfig to use a Set for matchTokens. Fixed CustomRules.

This commit is contained in:
Scott Hoyt
2016-01-25 07:26:31 -08:00
parent 0a4602b3e6
commit 923b4c4e7e
2 changed files with 10 additions and 12 deletions
@@ -58,15 +58,13 @@ public struct CustomRules: Rule, ConfigProviderRule {
}
private func validate(file: File, withConfig config: RegexConfig) -> [StyleViolation] {
// We are not using the preconstucted regex due to the API available, but it is important
// to still construct it at configuration parsing time to catch errors.
let ranges = file.matchPattern(config.regex.pattern, withSyntaxKinds: config.matchTokens)
let violations = ranges.map {
StyleViolation(ruleDescription: config.description,
severity: config.severity,
location: Location(file: file, characterOffset: $0.location),
reason: config.message)
}
return violations
return file.matchPattern(config.regex).filter {
!config.matchTokens.intersect($0.1).isEmpty
}.map {
StyleViolation(ruleDescription: config.description,
severity: config.severity,
location: Location(file: file, characterOffset: $0.0.location),
reason: config.message)
}
}
}
@@ -13,7 +13,7 @@ public struct RegexConfig: RuleConfig, Equatable {
let identifier: String
var message = "Regex matched."
var regex = NSRegularExpression()
var matchTokens = SyntaxKind.allKinds()
var matchTokens = Set(SyntaxKind.allKinds())
var severityConfig = SeverityConfig(.Warning)
public var severity: ViolationSeverity {
@@ -42,7 +42,7 @@ public struct RegexConfig: RuleConfig, Equatable {
self.regex = try NSRegularExpression(pattern: regexString, options: [])
}
try [String].arrayOf(configDict["match_tokens"])?.forEach {
self.matchTokens.append(try SyntaxKind(shortName: $0))
self.matchTokens.insert(try SyntaxKind(shortName: $0))
}
if let severityString = configDict["severity"] as? String {
try severityConfig.setConfig(severityString)