From d94aab7bf45ec0a911226fbe08b88b7712096fc2 Mon Sep 17 00:00:00 2001 From: John Mueller Date: Fri, 31 Jan 2020 11:37:46 -0600 Subject: [PATCH] Fix false positives when line ends with carriage return + line feed Fixes: #3060 --- CHANGELOG.md | 4 ++++ Source/SwiftLintFramework/Models/Command.swift | 2 +- Tests/LinuxMain.swift | 7 +++++++ Tests/SwiftLintFrameworkTests/LineEndingTests.swift | 12 ++++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 Tests/SwiftLintFrameworkTests/LineEndingTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 548670912d..5583fa549c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,10 @@ capturing observers into an array. [Petteri Huusko](https://github.com/PetteriHuusko) +* Fix false positives when line ends with carriage return + line feed + [John Mueller](https://github.com/john-mueller) + [#3060](https://github.com/realm/SwiftLint/issues/3060) + ## 0.38.2: Machine Repair Manual #### Breaking diff --git a/Source/SwiftLintFramework/Models/Command.swift b/Source/SwiftLintFramework/Models/Command.swift index ff008ebec9..81b6d2cf52 100644 --- a/Source/SwiftLintFramework/Models/Command.swift +++ b/Source/SwiftLintFramework/Models/Command.swift @@ -124,7 +124,7 @@ public struct Command: Equatable { let startOfCommentPastDelimiter = scanner.scanLocation + Command.commentDelimiter.count trailingComment = scanner.string.bridge().substring(from: startOfCommentPastDelimiter) } - let ruleTexts = rawRuleTexts.components(separatedBy: .whitespaces).filter { + let ruleTexts = rawRuleTexts.components(separatedBy: .whitespacesAndNewlines).filter { let component = $0.trimmingCharacters(in: .whitespaces) return !component.isEmpty && component != "*/" } diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 07d7186622..0bc447d127 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -787,6 +787,12 @@ extension LetVarWhitespaceRuleTests { ] } +extension LineEndingTests { + static var allTests: [(String, (LineEndingTests) -> () throws -> Void)] = [ + ("testCarriageReturnDoesNotCauseError", testCarriageReturnDoesNotCauseError) + ] +} + extension LineLengthConfigurationTests { static var allTests: [(String, (LineLengthConfigurationTests) -> () throws -> Void)] = [ ("testLineLengthConfigurationInitializerSetsLength", testLineLengthConfigurationInitializerSetsLength), @@ -1735,6 +1741,7 @@ XCTMain([ testCase(LegacyNSGeometryFunctionsRuleTests.allTests), testCase(LegacyRandomRuleTests.allTests), testCase(LetVarWhitespaceRuleTests.allTests), + testCase(LineEndingTests.allTests), testCase(LineLengthConfigurationTests.allTests), testCase(LineLengthRuleTests.allTests), testCase(LinterCacheTests.allTests), diff --git a/Tests/SwiftLintFrameworkTests/LineEndingTests.swift b/Tests/SwiftLintFrameworkTests/LineEndingTests.swift new file mode 100644 index 0000000000..b9357b3515 --- /dev/null +++ b/Tests/SwiftLintFrameworkTests/LineEndingTests.swift @@ -0,0 +1,12 @@ +@testable import SwiftLintFramework +import XCTest + +class LineEndingTests: XCTestCase { + func testCarriageReturnDoesNotCauseError() { + XCTAssert( + violations( + "//swiftlint:disable all\r\nprint(123)\r\n" + ).isEmpty + ) + } +}