Skip to content

Commit

Permalink
Improve vertical_whitespace reason and autocorrect when max_empty_lin…
Browse files Browse the repository at this point in the history
…es > 1(#1774)
  • Loading branch information
hossamghareeb authored and marcelofabri committed Aug 17, 2017
1 parent 4609455 commit a1cb85f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
a configuration in a parent directory.
[Marcelo Fabri](https://github.com/marcelofabri)
[#1744](https://github.com/realm/SwiftLint/issues/1744)
* 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

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 {
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!")
}
}
}

0 comments on commit a1cb85f

Please sign in to comment.