Skip to content

Commit

Permalink
(fix) address integration tests issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mredig committed Feb 23, 2024
1 parent 9490124 commit e78e18d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ struct IndentationStyleConfiguration: SeverityBasedRuleConfiguration, Equatable
@ConfigurationElement(key: "tab_width")
private(set) var tabWidth: Int?

static let testTabWidth: [String: Any] = ["tab_width": 4]
static let testMultilineString: [String: Any] = ["include_multiline_strings": true]
static let testMultilineComment: [String: Any] = ["include_multiline_comments": false]
static let testTabWidth: [String: any Sendable] = ["tab_width": 4]
static let testMultilineString: [String: any Sendable] = ["include_multiline_strings": true]
static let testMultilineComment: [String: any Sendable] = ["include_multiline_comments": false]

@MakeAcceptableByConfigurationElement
enum PreferredStyle: String {
Expand Down
42 changes: 18 additions & 24 deletions Source/SwiftLintBuiltInRules/Rules/Style/IndentationStyleRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,20 @@ struct IndentationStyleRule: Rule, OptInRule {

guard indentationCharacterCount > 0 else { continue }

if
ignoreMultilineStrings(line: line, in: file) ||
ignoreMultilineComments(line: line, in: file)
{ continue }
if ignoreMultilineStrings(line: line, in: file) || ignoreMultilineComments(line: line, in: file) {
continue
}

guard let firstLineIndentation = indentationForThisLine.first else { continue }

func createViolation(at location: Int, reason: String) -> StyleViolation {
StyleViolation(
ruleDescription: Self.description,
severity: configuration.severity,
location: Location(file: file, characterOffset: location),
reason: reason)
}

let confirmedFileStyle: ConfigurationType.PreferredStyle
if let fileStyle {
confirmedFileStyle = fileStyle
Expand All @@ -224,14 +231,9 @@ struct IndentationStyleRule: Rule, OptInRule {
fileStyle = .tabs
confirmedFileStyle = .tabs
default:
let violation = StyleViolation(
ruleDescription: Self.description,
severity: configuration.severity,
location: Location(file: file, characterOffset: line.range.location),
reason: "Somehow a non tab or space made it into indentation: '\(firstLineIndentation)'" +
" aka \(firstLineIndentation.unicodeScalars)")
violations.append(violation)
return violations
let reason = "Somehow a non tab or space made it into indentation: '\(firstLineIndentation)'" +
" aka \(firstLineIndentation.unicodeScalars)"
return [createViolation(at: line.range.location, reason: reason)]
}
}

Expand All @@ -240,12 +242,8 @@ struct IndentationStyleRule: Rule, OptInRule {
if let offset = line.content.firstIndex(of: "\t") {
let intOffset = line.content.distance(from: line.content.startIndex, to: offset)

let violation = StyleViolation(
ruleDescription: Self.description,
severity: configuration.severity,
location: Location(file: file, characterOffset: line.range.location + intOffset),
reason: "Code should be indented with spaces\(configuration.perFile ? " (In this file)" : "")")
violations.append(violation)
let reason = "Code should be indented with spaces\(configuration.perFile ? " (In this file)" : "")"
violations.append(createViolation(at: line.range.location + intOffset, reason: reason))
}
case .tabs:
if let offset = indentationForThisLine.firstIndex(of: " ") {
Expand All @@ -256,12 +254,8 @@ struct IndentationStyleRule: Rule, OptInRule {
}

let intOffset = line.content.distance(from: line.content.startIndex, to: offset)
let violation = StyleViolation(
ruleDescription: Self.description,
severity: configuration.severity,
location: Location(file: file, characterOffset: line.range.location + intOffset),
reason: "Code should be indented with tabs\(configuration.perFile ? " (In this file)" : "")")
violations.append(violation)
let reason = "Code should be indented with tabs\(configuration.perFile ? " (In this file)" : "")"
violations.append(createViolation(at: line.range.location + intOffset, reason: reason))
}
}
}
Expand Down

0 comments on commit e78e18d

Please sign in to comment.