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

TrailingCommaRule only triggers when multi-line #912

Merged
merged 1 commit into from
Dec 1, 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
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