Skip to content

Commit

Permalink
Keep linting even when seeing mixed indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
fredpi committed Jul 24, 2019
1 parent 8247665 commit da58c89
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
15 changes: 10 additions & 5 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
case let .tabs(tabs): return tabs & indentationWidth // Fix
case let .spaces(spaces): return spaces
}
}
Expand Down Expand Up @@ -67,8 +67,10 @@ public struct IndentationWidthRule: ConfigurationProviderRule, OptInRule {
let tabCount = prefix.filter { $0 == "\t" }.count
let spaceCount = prefix.filter { $0 == " " }.count

// Catch mixed indentation
// Determine indentation
let indentation: Indentation
if tabCount != 0 && spaceCount != 0 {
// Catch mixed indentation
violations.append(
StyleViolation(
ruleDescription: IndentationWidthRule.description,
Expand All @@ -79,12 +81,15 @@ public struct IndentationWidthRule: ConfigurationProviderRule, OptInRule {
)
)

// Break as next line cannot be parsed without knowing this line's exact indentation
break
// Model this line's indentation using spaces (although it's tabs & spaces) to let parsing continue
indentation = .spaces(spaceCount + tabCount * configuration.indentationWidth)
} else if tabCount != 0 {
indentation = .tabs(tabCount)
} else {
indentation = .spaces(spaceCount)
}

// Catch indented first line
let indentation: Indentation = tabCount != 0 ? .tabs(tabCount) : .spaces(spaceCount)
guard !previousLineIndentations.isEmpty else {
previousLineIndentations = [indentation]

Expand Down
5 changes: 3 additions & 2 deletions Tests/SwiftLintFrameworkTests/IndentationWidthRuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ class IndentationWidthRuleTests: XCTestCase {

/// It's not okay to indent using both tabs and spaces in one line.
func testMixedTabSpaceIndentation() {
assert1Violation(in: "firstLine\n\t secondLine")
assert1Violation(in: "firstLine\n \tsecondLine")
// Expect 2 violations as secondLine is also indented by 8 spaces (which isn't valid)
assertViolations(in: "firstLine\n\t secondLine", equals: 2)
assertViolations(in: "firstLine\n \tsecondLine", equals: 2)
}

/// It's okay to keep the same indentation.
Expand Down

0 comments on commit da58c89

Please # to comment.