Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comma rule does not get triggered in all situations #876

Merged
merged 3 commits into from
Nov 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@
[Norio Nomura](https://github.com/norio-nomura)
[#813](https://github.com/realm/SwiftLint/issues/813)

* Fixed regex bug in Comma Rule. Some violations were not being triggered
when there were consecutive violations in the same expression.
[Savio Figueiredo](https://github.com/sadefigu)
[#872](https://github.com/realm/SwiftLint/issues/872)

## 0.12.0: Vertical Laundry

##### Breaking
Expand Down
22 changes: 17 additions & 5 deletions Source/SwiftLintFramework/Rules/CommaRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public struct CommaRule: CorrectableRule, ConfigurationProviderRule {
],
triggeringExamples: [
"func abc(a: String↓ ,b: String) { }",
"func abc(a: String↓ ,b: String↓ ,c: String↓ ,d: String) { }",
"abc(a: \"string\"↓,b: \"string\"",
"enum a { case a↓ ,b }",
"let result = plus(\n first: 3↓ , // #683\n second: 4\n)\n",
Expand All @@ -38,6 +39,7 @@ public struct CommaRule: CorrectableRule, ConfigurationProviderRule {
"abc(a: \"string\" , b: \"string\"\n": "abc(a: \"string\", b: \"string\"\n",
"enum a { case a ,b }\n": "enum a { case a, b }\n",
"let a = [1,1]\nlet b = 1\nf(1, b)\n": "let a = [1, 1]\nlet b = 1\nf(1, b)\n",
"let a = [1↓,1↓,1↓,1]\n": "let a = [1, 1, 1, 1]\n",
]
)

Expand Down Expand Up @@ -68,8 +70,8 @@ public struct CommaRule: CorrectableRule, ConfigurationProviderRule {

// captures spaces and comma only
// http://userguide.icu-project.org/strings/regexp
private static let pattern =
"\\S" + // not whitespace

private static let mainPatternGroups =
"(" + // start first capure
"\\s+" + // followed by whitespace
"," + // to the left of a comma
Expand All @@ -81,6 +83,11 @@ public struct CommaRule: CorrectableRule, ConfigurationProviderRule {
")" + // end capture
"(\\S)" // second capture is not whitespace.

private static let pattern =
"\\S\(mainPatternGroups)" + // Regexp will match if expression not begin with comma
"|" + // or
"\(mainPatternGroups)" // Regexp will match if expression begins with comma

// swiftlint:disable:next force_try
private static let regularExpression = try! NSRegularExpression(pattern: pattern, options: [])
private static let excludingSyntaxKindsForFirstCapture = SyntaxKind.commentAndStringKinds()
Expand All @@ -95,10 +102,15 @@ public struct CommaRule: CorrectableRule, ConfigurationProviderRule {
return CommaRule.regularExpression
.matchesInString(contents, options: [], range: range)
.flatMap { match -> NSRange? in
if match.numberOfRanges != 3 { return nil }
if match.numberOfRanges != 5 { return nil } // Number of Groups in regexp

var indexStartRange = 1
if match.rangeAtIndex(indexStartRange).location == NSNotFound {
indexStartRange += 2
}

// check first captured range
let firstRange = match.rangeAtIndex(1)
let firstRange = match.rangeAtIndex(indexStartRange)
guard let matchByteFirstRange = contents
.NSRangeToByteRange(start: firstRange.location, length: firstRange.length)
else { return nil }
Expand All @@ -119,7 +131,7 @@ public struct CommaRule: CorrectableRule, ConfigurationProviderRule {
}

// check second captured range
let secondRange = match.rangeAtIndex(2)
let secondRange = match.rangeAtIndex(indexStartRange + 1)
guard let matchByteSecondRange = contents
.NSRangeToByteRange(start: secondRange.location, length: secondRange.length)
else { return nil }
Expand Down