Skip to content

Commit

Permalink
Respect grapheme clusters in the collection_alignment rule (#5173)
Browse files Browse the repository at this point in the history
Do so by counting characters. Solves (to some extent) the problem of apparent character counts not matching the UTF-8-based offsets returned by SwiftSyntax.
  • Loading branch information
kishikawakatsumi authored Sep 11, 2023
1 parent f05fbf5 commit 05818b5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@

#### Bug Fixes

* Respect grapheme clusters in counting the number of characters in the `collection_alignment` rule.
[kishikawakatsumi](https://github.com/kishikawakatsumi)
[#4837](https://github.com/realm/SwiftLint/issues/4837)

* Fix false positive in `control_statement` rule that triggered on conditions
with trailing closures where parentheses are recommended by the compiler.
[SimplyDanny](https://github.com/SimplyDanny)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,24 @@ private extension CollectionAlignmentRule {
let locations = node.map { element in
let position = alignColons ? element.colon.positionAfterSkippingLeadingTrivia :
element.key.positionAfterSkippingLeadingTrivia
return locationConverter.location(for: position)
let location = locationConverter.location(for: position)

let graphemeColumn: Int
let graphemeClusters = String(
locationConverter.sourceLines[location.line - 1].utf8.prefix(location.column - 1)
)
if let graphemeClusters {
graphemeColumn = graphemeClusters.count + 1
} else {
graphemeColumn = location.column
}

return SourceLocation(
line: location.line,
column: graphemeColumn,
offset: location.offset,
file: location.file
)
}
violations.append(contentsOf: validate(keyLocations: locations))
}
Expand Down Expand Up @@ -141,6 +158,10 @@ extension CollectionAlignmentRule {
"b" :2,
"c" : 3
]
"""),
Example("""
NSAttributedString(string: "", attributes: [.font: UIFont.systemFont(ofSize: 12, weight: .regular),
.foregroundColor: UIColor(white: 0, alpha: 0.2)])
""")
]
}
Expand Down Expand Up @@ -199,6 +220,10 @@ extension CollectionAlignmentRule {
"lunch": "sandwich",
"dinner": "burger"
]
"""),
Example("""
NSAttributedString(string: "", attributes: [.font: UIFont.systemFont(ofSize: 12, weight: .regular),
.foregroundColor: UIColor(white: 0, alpha: 0.2)])
""")
]
}
Expand Down

0 comments on commit 05818b5

Please sign in to comment.