From 9e243d80b00c025ea9581dc2b16f07567a46d1cc Mon Sep 17 00:00:00 2001 From: freak4pc Date: Tue, 26 Jul 2016 12:07:05 +0300 Subject: [PATCH] ColonRule allows setting flexible_right_spacing. Resolves issue realm/SwiftLint#730. --- CHANGELOG.md | 4 ++ .../SwiftLintFramework/Rules/ColonRule.swift | 41 +++++++++++++------ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d83550e71..a8ef41a5a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ ##### Enhancements +* Allow setting `flexible_right_spacing` configuration for the `colon` rule. + [Shai Mishali](https://github.com/freak4pc) + [#730](https://github.com/realm/SwiftLint/issues/730) + * Add Junit reporter. [Matthew Ellis](https://github.com/matthewellis) diff --git a/Source/SwiftLintFramework/Rules/ColonRule.swift b/Source/SwiftLintFramework/Rules/ColonRule.swift index b2e96ed0b3..7acb7b8637 100644 --- a/Source/SwiftLintFramework/Rules/ColonRule.swift +++ b/Source/SwiftLintFramework/Rules/ColonRule.swift @@ -15,6 +15,16 @@ public struct ColonRule: CorrectableRule, ConfigurationProviderRule { public init() {} + public var flexibleRightSpacing = false + + public init(configuration: AnyObject) throws { + self.flexibleRightSpacing = configuration["flexible_right_spacing"] as? Int == 1 + } + + public var configurationDescription: String { + return "flexible_right_spacing: \(self.flexibleRightSpacing)" + } + public static let description = RuleDescription( identifier: "colon", name: "Colon", @@ -110,19 +120,24 @@ public struct ColonRule: CorrectableRule, ConfigurationProviderRule { // MARK: - Private - private let pattern = - "(\\w)" + // Capture an identifier - "(?:" + // start group - "\\s+" + // followed by whitespace - ":" + // to the left of a colon - "\\s*" + // followed by any amount of whitespace. - "|" + // or - ":" + // immediately followed by a colon - "(?:\\s{0}|\\s{2,})" + // followed by 0 or 2+ whitespace characters. - ")" + // end group - "(" + // Capture a type identifier - "[\\[|\\(]*" + // which may begin with a series of nested parenthesis or brackets - "\\S)" // lazily to the first non-whitespace character. + private var pattern: String { + // If flexible_right_spacing is true, match only 0 whitespaces. + // If flexible_right_spacing is false or omitted, match 0 or 2+ whitespaces. + let spacingRegex = flexibleRightSpacing ? "(?:\\s{0})" : "(?:\\s{0}|\\s{2,})" + + return "(\\w)" + // Capture an identifier + "(?:" + // start group + "\\s+" + // followed by whitespace + ":" + // to the left of a colon + "\\s*" + // followed by any amount of whitespace. + "|" + // or + ":" + // immediately followed by a colon + spacingRegex + // followed by right spacing regex + ")" + // end group + "(" + // Capture a type identifier + "[\\[|\\(]*" + // which may begin with a series of nested parenthesis or brackets + "\\S)" // lazily to the first non-whitespace character. + } private func violationRangesInFile(file: File, withPattern pattern: String) -> [NSRange] { let nsstring = file.contents as NSString