Skip to content

Commit

Permalink
Fix wrong spacesEquivalent implementation; simplify validate method
Browse files Browse the repository at this point in the history
  • Loading branch information
fredpi committed Jul 24, 2019
1 parent da58c89 commit 2407a14
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
38 changes: 12 additions & 26 deletions Source/SwiftLintFramework/Rules/Style/IndentationWidthRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public struct IndentationWidthRule: ConfigurationProviderRule, OptInRule {

func spacesEquivalent(indentationWidth: Int) -> Int {
switch self {
case let .tabs(tabs): return tabs & indentationWidth // Fix
case let .tabs(tabs): return tabs * indentationWidth
case let .spaces(spaces): return spaces
}
}
Expand Down Expand Up @@ -153,30 +153,16 @@ public struct IndentationWidthRule: ConfigurationProviderRule, OptInRule {
///
/// Returns a Bool determining the validity of the indentation.
private func validate(indentation: Indentation, comparingTo lastIndentation: Indentation) -> Bool {
switch indentation {
case let .spaces(currentSpaceCount):
let previousSpacesCount = lastIndentation.spacesEquivalent(indentationWidth: configuration.indentationWidth)
guard
currentSpaceCount == previousSpacesCount + configuration.indentationWidth ||
(
(previousSpacesCount - currentSpaceCount) >= 0 &&
(previousSpacesCount - currentSpaceCount) % configuration.indentationWidth == 0
)
else { return false }

case let .tabs(currentTabCount):
switch lastIndentation {
case let .spaces(previousSpacesCount):
guard
currentTabCount * configuration.indentationWidth - previousSpacesCount
<= configuration.indentationWidth
else { return false }

case let .tabs(previousTabCount):
guard currentTabCount - previousTabCount <= 1 else { return false }
}
}

return true
let currentSpaceEquivalent = indentation.spacesEquivalent(indentationWidth: configuration.indentationWidth)
let lastSpaceEquivalent = lastIndentation.spacesEquivalent(indentationWidth: configuration.indentationWidth)

return (
// Allow indent by indentationWidth
currentSpaceEquivalent == lastSpaceEquivalent + configuration.indentationWidth ||
(
(lastSpaceEquivalent - currentSpaceEquivalent) >= 0 &&
(lastSpaceEquivalent - currentSpaceEquivalent) % configuration.indentationWidth == 0
) // Allow unindent if it stays in the grid
)
}
}
7 changes: 7 additions & 0 deletions Tests/SwiftLintFrameworkTests/IndentationWidthRuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class IndentationWidthRuleTests: XCTestCase {
assertViolations(in: "firstLine\n \tsecondLine", equals: 2)
}

/// It's okay to indent using either tabs or spaces in different lines.
func testMixedTabsAndSpacesIndentation() {
assertNoViolation(in: "firstLine\n\tsecondLine\n thirdLine")
assertNoViolation(in: "firstLine\n secondLine\n\t\tthirdLine")
assertNoViolation(in: "firstLine\n\tsecondLine\n thirdLine\n\t\t\tfourthLine")
}

/// It's okay to keep the same indentation.
func testKeepingIndentation() {
assertNoViolation(in: "firstLine\nsecondLine")
Expand Down

0 comments on commit 2407a14

Please # to comment.