From 485518faef04df4e80bfdedfeec01c586a4419f7 Mon Sep 17 00:00:00 2001 From: Marcelo Fabri Date: Wed, 30 Nov 2016 19:02:12 -0200 Subject: [PATCH] TrailingCommaRule only triggers when multi-line Fixes #910 and #911 --- CHANGELOG.md | 6 +++++- .../Rules/TrailingCommaRule.swift | 10 +++++++++- Tests/SwiftLintFramework/RulesTests.swift | 19 +++++++++++++------ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ae3881f39..3abb09208b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Source/SwiftLintFramework/Rules/TrailingCommaRule.swift b/Source/SwiftLintFramework/Rules/TrailingCommaRule.swift index 85770d3e03..2dacfea02d 100644 --- a/Source/SwiftLintFramework/Rules/TrailingCommaRule.swift +++ b/Source/SwiftLintFramework/Rules/TrailingCommaRule.swift @@ -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", @@ -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) ?? "" diff --git a/Tests/SwiftLintFramework/RulesTests.swift b/Tests/SwiftLintFramework/RulesTests.swift index a98c5a3128..56be491032 100644 --- a/Tests/SwiftLintFramework/RulesTests.swift +++ b/Tests/SwiftLintFramework/RulesTests.swift @@ -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" ] )