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

Vertical whitespace msg fix #1774

Merged
merged 7 commits into from
Aug 14, 2017
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
[Otávio Lima](https://github.com/otaviolima)
[#1710](https://github.com/realm/SwiftLint/issues/1710)

* Fix the warning message and autocorrection of `vertical-whitespace` rule to display the maximum empty lines allowed if `maxEmptyLines` is greater than 1.
[Hossam Ghareeb](https://github.com/hossamghareeb)
[#1763](https://github.com/realm/SwiftLint/issues/1763)

## 0.21.0: Vintage Washboard

##### Breaking
Expand Down
22 changes: 15 additions & 7 deletions Source/SwiftLintFramework/Rules/VerticalWhitespaceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation
import SourceKittenFramework

private let descriptionReason = "Limit vertical whitespace to a single empty line."
private let defaultDescriptionReason = "Limit vertical whitespace to a single empty line."

public struct VerticalWhitespaceRule: CorrectableRule, ConfigurationProviderRule {

Expand All @@ -20,7 +20,7 @@ public struct VerticalWhitespaceRule: CorrectableRule, ConfigurationProviderRule
public static let description = RuleDescription(
identifier: "vertical_whitespace",
name: "Vertical Whitespace",
description: descriptionReason,
description: defaultDescriptionReason,
kind: .style,
nonTriggeringExamples: [
"let abc = 0\n",
Expand All @@ -40,6 +40,13 @@ public struct VerticalWhitespaceRule: CorrectableRule, ConfigurationProviderRule
] // End of line autocorrections are handled by Trailing Newline Rule.
)

private var configuredDescriptionReason: String {
guard configuration.maxEmptyLines == 1 else {
return "Limit vertical whitespace to maximum \(configuration.maxEmptyLines) empty lines."
}
return defaultDescriptionReason
}

public func validate(file: File) -> [StyleViolation] {
let linesSections = violatingLineSections(in: file)
guard !linesSections.isEmpty else {
Expand All @@ -54,7 +61,7 @@ public struct VerticalWhitespaceRule: CorrectableRule, ConfigurationProviderRule
ruleDescription: type(of: self).description,
severity: configuration.severityConfiguration.severity,
location: Location(file: file.path, line: eachLastLine.index),
reason: descriptionReason + " Currently \(eachSectionCount + 1)."
reason: configuredDescriptionReason + " Currently \(eachSectionCount + 1)."
))
}
}
Expand Down Expand Up @@ -120,9 +127,10 @@ public struct VerticalWhitespaceRule: CorrectableRule, ConfigurationProviderRule

var indexOfLinesToDelete = [Int]()

for eachLine in linesSections {
let start = eachLine.lastLine.index - eachLine.linesToRemove
indexOfLinesToDelete.append(contentsOf: start..<eachLine.lastLine.index)
for section in linesSections {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add tests for this change as well.

let linesToRemove = section.linesToRemove - configuration.maxEmptyLines + 1
let start = section.lastLine.index - linesToRemove
indexOfLinesToDelete.append(contentsOf: start..<section.lastLine.index)
}

var correctedLines = [String]()
Expand All @@ -138,7 +146,7 @@ public struct VerticalWhitespaceRule: CorrectableRule, ConfigurationProviderRule
// removes lines by skipping them from correctedLines
if Set(indexOfLinesToDelete).contains(currentLine.index) {
let description = type(of: self).description
let location = Location(file: file.path, line: currentLine.index)
let location = Location(file: file, characterOffset: currentLine.range.location)

//reports every line that is being deleted
corrections.append(Correction(ruleDescription: description, location: location))
Expand Down
40 changes: 40 additions & 0 deletions Tests/SwiftLintFrameworkTests/VerticalWhitespaceRuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import XCTest

class VerticalWhitespaceRuleTests: XCTestCase {

private let ruleID = VerticalWhitespaceRule.description.identifier

func testVerticalWhitespaceWithDefaultConfiguration() {
// Test with default parameters
verifyRule(VerticalWhitespaceRule.description)
Expand All @@ -26,4 +28,42 @@ class VerticalWhitespaceRuleTests: XCTestCase {
verifyRule(maxEmptyLinesDescription,
ruleConfiguration: ["max_empty_lines": 2])
}

func testAutoCorrection() {
let maxEmptyLinesDescription = VerticalWhitespaceRule.description
.with(nonTriggeringExamples: [])
.with(triggeringExamples: [])
.with(corrections: [
"let b = 0\n\n↓\n↓\n↓\n\nclass AAA {}\n": "let b = 0\n\n\nclass AAA {}\n",
"let b = 0\n\n\nclass AAA {}\n": "let b = 0\n\n\nclass AAA {}\n"
])

verifyRule(maxEmptyLinesDescription,
ruleConfiguration: ["max_empty_lines": 2])
}

func testViolationMessageWithMaxEmptyLines() {
guard let config = makeConfig(["max_empty_lines": 2], ruleID) else {
XCTFail("Failed to create configuration")
return
}
let allViolations = violations("let aaaa = 0\n\n\n\nlet bbb = 2\n", config: config)

let verticalWhiteSpaceViolation = allViolations.first { $0.ruleDescription.identifier == ruleID }
if let violation = verticalWhiteSpaceViolation {
XCTAssertEqual(violation.reason, "Limit vertical whitespace to maximum 2 empty lines. Currently 3.")
} else {
XCTFail("A vertical white space violation should have been triggered!")
}
}

func testViolationMessageWithDefaultConfiguration() {
let allViolations = violations("let aaaa = 0\n\n\n\nlet bbb = 2\n")
let verticalWhiteSpaceViolation = allViolations.first(where: { $0.ruleDescription.identifier == ruleID })
if let violation = verticalWhiteSpaceViolation {
XCTAssertEqual(violation.reason, "Limit vertical whitespace to a single empty line. Currently 3.")
} else {
XCTFail("A vertical white space violation should have been triggered!")
}
}
}