From 923b4c4e7e87f486b8d8add699eeac8367a1100e Mon Sep 17 00:00:00 2001 From: Scott Hoyt Date: Thu, 21 Jan 2016 16:47:38 -0800 Subject: [PATCH] Update RegexConfig to use a Set for matchTokens. Fixed CustomRules. --- .../SwiftLintFramework/Rules/CustomRules.swift | 18 ++++++++---------- .../Rules/RuleConfigs/RegexConfig.swift | 4 ++-- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/Source/SwiftLintFramework/Rules/CustomRules.swift b/Source/SwiftLintFramework/Rules/CustomRules.swift index d11a013cb..e731568c6 100644 --- a/Source/SwiftLintFramework/Rules/CustomRules.swift +++ b/Source/SwiftLintFramework/Rules/CustomRules.swift @@ -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) + } } } diff --git a/Source/SwiftLintFramework/Rules/RuleConfigs/RegexConfig.swift b/Source/SwiftLintFramework/Rules/RuleConfigs/RegexConfig.swift index e51cc8006..709948901 100644 --- a/Source/SwiftLintFramework/Rules/RuleConfigs/RegexConfig.swift +++ b/Source/SwiftLintFramework/Rules/RuleConfigs/RegexConfig.swift @@ -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)