Fix docCommentsBeforeModifiers bug

This commit is contained in:
Nick Lockwood
2024-11-19 23:45:33 +00:00
parent 5fdd6a5707
commit dd1d2f6c5e
2 changed files with 37 additions and 4 deletions
+9 -3
View File
@@ -404,9 +404,15 @@ extension Formatter {
func modifiersForDeclaration(at index: Int, contains: (Int, String) -> Bool) -> Bool {
var index = index
while var prevIndex = self.index(of: .nonSpaceOrCommentOrLinebreak, before: index) {
let token = tokens[prevIndex]
switch token {
case _ where token.isModifierKeyword || token.isAttribute:
switch tokens[prevIndex] {
case let token where token.isModifierKeyword || token.isAttribute:
if case .identifier = token,
let nextToken = last(.nonSpaceOrCommentOrLinebreak, before: prevIndex),
nextToken == .keyword("case") || nextToken.isOperator(ofType: .infix) || nextToken.isOperator(ofType: .prefix)
{
// Part of previous declaration
return false
}
if contains(prevIndex, token.string) {
return true
}
@@ -271,10 +271,37 @@ class DocCommentsBeforeModifiersTests: XCTestCase {
case prefix(String)
/// A postfix operator
case postfix(String)
case postfix
/// Required
case required
/// Optional
case optional
/// Open
case open
/// Other
case other
}
"""
testFormatting(for: input, rule: .docCommentsBeforeModifiers)
}
func testDynamicFunctionName() {
let input = """
enum Colors {
/// Tint color
static let tintColor = UIColor.dynamic(light: .fullBlack, dark: .white)
/// Text color
static let textColor = UIColor.dynamic(light: .fullBlack, dark: .white)
/// Line color
static let lineColor: UIColor = .dynamic(light: .fullBlack, dark: .white)
}
"""
testFormatting(for: input, rule: .docCommentsBeforeModifiers, exclude: [.propertyTypes])
}
}