mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
Fix docCommentsBeforeModifiers bug
This commit is contained in:
@@ -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])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user