Skip to content

Commit

Permalink
Merge pull request #912 from marcelofabri/mandatory-comma-multiline
Browse files Browse the repository at this point in the history
TrailingCommaRule only triggers when multi-line
  • Loading branch information
jpsim authored Dec 1, 2016
2 parents 5473c69 + 485518f commit 52c055d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

##### Enhancements

* None.
* `TrailingCommaRule` now only triggers when a declaration is multi-line
when using `mandatory_comma: true`.
[Marcelo Fabri](https://github.com/marcelofabri)
[#910](https://github.com/realm/SwiftLint/issues/910)
[#911](https://github.com/realm/SwiftLint/issues/911)

##### Bug Fixes

Expand Down
10 changes: 9 additions & 1 deletion Source/SwiftLintFramework/Rules/TrailingCommaRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public struct TrailingCommaRule: ASTRule, ConfigurationProviderRule {
"let foo = [1, 2, 3]\n",
"let foo = []\n",
"let foo = [:]\n",
"let foo = [1: 2, 2: 3]\n"
"let foo = [1: 2, 2: 3]\n",
"let foo = [Void]()\n"
],
triggeringExamples: [
"let foo = [1, 2, 3↓,]\n",
Expand Down Expand Up @@ -61,6 +62,13 @@ public struct TrailingCommaRule: ASTRule, ConfigurationProviderRule {
return []
}

if let (startLine, _) = file.contents.lineAndCharacterForByteOffset(bodyOffset),
(endLine, _) = file.contents.lineAndCharacterForByteOffset(lastPosition)
where configuration.mandatoryComma && startLine == endLine {
// shouldn't trigger if mandatory comma style and is a single-line declaration
return []
}

let length = bodyLength + bodyOffset - lastPosition
let contentsAfterLastElement = file.contents
.substringWithByteRange(start: lastPosition, length: length) ?? ""
Expand Down
19 changes: 13 additions & 6 deletions Tests/SwiftLintFramework/RulesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,21 @@ class RulesTests: XCTestCase {
"let foo = [1, 2, 3, ]\n",
"let foo = [1, 2, 3 ,]\n",
"let foo = [1: 2, 2: 3, ]\n",
"struct Bar {\n let foo = [1: 2, 2: 3,]\n}\n"
"struct Bar {\n let foo = [1: 2, 2: 3,]\n}\n",
"let foo = [Void]()\n",
"let foo = [(Void, Void)]()\n",
"let foo = [1, 2, 3]\n",
"let foo = [1: 2, 2: 3]\n",
"let foo = [1: 2, 2: 3 ]\n",
"struct Bar {\n let foo = [1: 2, 2: 3]\n}\n",
"let foo = [1, 2, 3] + [4, 5, 6]\n"
],
triggeringExamples: [
"let foo = [1, 2, 3↓]\n",
"let foo = [1: 2, 2: 3↓]\n",
"let foo = [1: 2, 2: 3↓ ]\n",
"struct Bar {\n let foo = [1: 2, 2: 3↓]\n}\n",
"let foo = [1, 2, 3↓] + [4, 5, 6↓]\n"
"let foo = [1, 2,\n 3↓]\n",
"let foo = [1: 2,\n 2: 3↓]\n",
"let foo = [1: 2,\n 2: 3↓ ]\n",
"struct Bar {\n let foo = [1: 2,\n 2: 3↓]\n}\n",
"let foo = [1, 2,\n 3↓] + [4,\n 5, 6↓]\n"
]
)

Expand Down

0 comments on commit 52c055d

Please sign in to comment.