Skip to content
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
[#6798](https://github.com/realm/SwiftLint/issues/6798)
[#6799](https://github.com/realm/SwiftLint/issues/6799)

* Fix `closure_end_indentation` moving closing braces to column 1 in files
with CRLF line endings when running `swiftlint --fix`.
[sjh9714](https://github.com/sjh9714)
[#6598](https://github.com/realm/SwiftLint/issues/6598)

## 0.65.0: Fresh Folded Fixtures

### Breaking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,13 @@ private extension ClosureEndIndentationRule {

/// Calculates the column of the first non-whitespace character on a given line.
private func getFirstNonWhitespaceColumn(onLine lineNumber: Int) -> Int {
guard lineNumber > 0, lineNumber <= file.lines.count else {
// Use the lines seen by `locationConverter`, which produced `lineNumber`.
// `file.lines` counts CRLF line endings differently, corrupting files.
let sourceLines = locationConverter.sourceLines
guard lineNumber > 0, lineNumber <= sourceLines.count else {
return 1 // Should not happen
}
let lineContent = file.lines[lineNumber - 1].content
let lineContent = sourceLines[lineNumber - 1]

if let firstCharIndex = lineContent.firstIndex(where: { !$0.isWhitespace }) {
return lineContent.distance(from: lineContent.startIndex, to: firstCharIndex) + 1
Expand Down
53 changes: 53 additions & 0 deletions Tests/BuiltInRulesTests/ClosureEndIndentationRuleTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import TestHelpers
import Testing

@testable import SwiftLintBuiltInRules

@Suite(.rulesRegistered)
struct ClosureEndIndentationRuleTests {
@Test
func crlfLineEndings() {
let correctlyIndented = [
"struct TestView: View {",
" var body: some View {",
" HStack {",
" VStack {",
" Text(\"Hello\")",
" Spacer()",
" }",
" }",
" }",
"}",
"",
].joined(separator: "\r\n")
let misindented = [
"struct S {",
" func f() {",
" run {",
" print(\"hi\")",
" ↓}",
" }",
"}",
"",
].joined(separator: "\r\n")
let corrected = [
"struct S {",
" func f() {",
" run {",
" print(\"hi\")",
" }",
" }",
"}",
"",
].joined(separator: "\r\n")

// testWrappingInString is disabled because the test helper's string
// wrapping escapes only "\n", leaving bare "\r" bytes in the literal.
let description = ClosureEndIndentationRule.description
.with(nonTriggeringExamples: [Example(code: correctlyIndented)])
.with(triggeringExamples: [Example(code: misindented, testWrappingInString: false)])
.with(corrections: [Example(code: misindented): Example(code: corrected)])

verifyRule(description)
}
}