Skip to content

Commit

Permalink
Merge pull request realm#734 from freak4pc/colon-flexible-right-spacing
Browse files Browse the repository at this point in the history
Allow setting flexible_right_spacing for colon rule.
  • Loading branch information
jpsim authored Jul 28, 2016
2 parents df1f1a8 + c963aad commit 20252c5
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
41 changes: 28 additions & 13 deletions Source/SwiftLintFramework/Rules/ColonRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public struct ColonRule: CorrectableRule, ConfigurationProviderRule {

public init() {}

public var flexibleRightSpacing = false

public init(configuration: AnyObject) throws {
flexibleRightSpacing = configuration["flexible_right_spacing"] as? Bool == true
}

public var configurationDescription: String {
return "flexible_right_spacing: \(flexibleRightSpacing)"
}

public static let description = RuleDescription(
identifier: "colon",
name: "Colon",
Expand Down Expand Up @@ -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
Expand Down
72 changes: 71 additions & 1 deletion Tests/SwiftLintFramework/RulesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,79 @@ class RulesTests: XCTestCase {
verifyRule(ClosingBraceRule.description)
}

// swiftlint:disable function_body_length
func testColon() {
// Verify Colon rule with test values for when flexible_right_spacing
// is false (default).
verifyRule(ColonRule.description)
}

// Verify Colon rule with test values for when flexible_right_spacing
// is true.
let description = RuleDescription(
identifier: "colon",
name: "Colon",
description: "Colons should be next to the identifier when specifying a type.",
nonTriggeringExamples: [
"let abc: Void\n",
"let abc: [Void: Void]\n",
"let abc: (Void, Void)\n",
"let abc: ([Void], String, Int)\n",
"let abc: [([Void], String, Int)]\n",
"let abc: String=\"def\"\n",
"let abc: Int=0\n",
"let abc: Enum=Enum.Value\n",
"func abc(def: Void) {}\n",
"func abc(def: Void, ghi: Void) {}\n",
"// 周斌佳年周斌佳\nlet abc: String = \"abc:\"",
"let abc: Void\n",
"let abc: (Void, String, Int)\n",
"let abc: ([Void], String, Int)\n",
"let abc: [([Void], String, Int)]\n",
"func abc(def: Void) {}\n"
],
triggeringExamples: [
"let ↓abc:Void\n",
"let ↓abc :Void\n",
"let ↓abc : Void\n",
"let ↓abc : [Void: Void]\n",
"let ↓abc : (Void, String, Int)\n",
"let ↓abc : ([Void], String, Int)\n",
"let ↓abc : [([Void], String, Int)]\n",
"let ↓abc :String=\"def\"\n",
"let ↓abc :Int=0\n",
"let ↓abc :Int = 0\n",
"let ↓abc:Int=0\n",
"let ↓abc:Int = 0\n",
"let ↓abc:Enum=Enum.Value\n",
"func abc(↓def:Void) {}\n",
"func abc(↓def :Void) {}\n",
"func abc(↓def : Void) {}\n",
"func abc(def: Void, ↓ghi :Void) {}\n"
],
corrections: [
"let abc:Void\n": "let abc: Void\n",
"let abc :Void\n": "let abc: Void\n",
"let abc : Void\n": "let abc: Void\n",
"let abc : [Void: Void]\n": "let abc: [Void: Void]\n",
"let abc : (Void, String, Int)\n": "let abc: (Void, String, Int)\n",
"let abc : ([Void], String, Int)\n": "let abc: ([Void], String, Int)\n",
"let abc : [([Void], String, Int)]\n": "let abc: [([Void], String, Int)]\n",
"let abc :String=\"def\"\n": "let abc: String=\"def\"\n",
"let abc :Int=0\n": "let abc: Int=0\n",
"let abc :Int = 0\n": "let abc: Int = 0\n",
"let abc:Int=0\n": "let abc: Int=0\n",
"let abc:Int = 0\n": "let abc: Int = 0\n",
"let abc:Enum=Enum.Value\n": "let abc: Enum=Enum.Value\n",
"func abc(def:Void) {}\n": "func abc(def: Void) {}\n",
"func abc(def :Void) {}\n": "func abc(def: Void) {}\n",
"func abc(def : Void) {}\n": "func abc(def: Void) {}\n",
"func abc(def: Void, ghi :Void) {}\n": "func abc(def: Void, ghi: Void) {}\n"
]
)

verifyRule(description, ruleConfiguration: ["flexible_right_spacing": true])
}
// swiftlint:enable function_body_length

func testComma() {
verifyRule(CommaRule.description)
Expand Down

0 comments on commit 20252c5

Please sign in to comment.