mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Fix issue where redundantViewBuilder would remove @ViewBuilder from protocol members (#2364)
Co-authored-by: calda <1811727+calda@users.noreply.github.com>
This commit is contained in:
@@ -20,6 +20,12 @@ public extension FormatRule {
|
||||
guard let viewBuilderIndex = formatter.indexOfViewBuilderAttribute(for: declaration)
|
||||
else { return }
|
||||
|
||||
// Never remove @ViewBuilder from protocol members, as conforming types
|
||||
// rely on the implicit result builder being added
|
||||
if formatter.isInsideProtocol(at: declaration.keywordIndex) {
|
||||
return
|
||||
}
|
||||
|
||||
let bodyScope: ClosedRange<Int>?
|
||||
let isBodyMember: Bool
|
||||
|
||||
|
||||
@@ -497,4 +497,45 @@ final class RedundantViewBuilderTests: XCTestCase {
|
||||
"""
|
||||
testFormatting(for: input, output, rule: .redundantViewBuilder)
|
||||
}
|
||||
|
||||
func testKeepViewBuilderOnProtocolMember() {
|
||||
// Protocol members with @ViewBuilder should not have it removed,
|
||||
// as conforming types rely on the implicit result builder
|
||||
let input = """
|
||||
protocol Foo {
|
||||
associatedtype MyFoo: View
|
||||
|
||||
@ViewBuilder
|
||||
var myBody: MyFoo { get }
|
||||
}
|
||||
"""
|
||||
testFormatting(for: input, rule: .redundantViewBuilder)
|
||||
}
|
||||
|
||||
func testKeepViewBuilderOnProtocolFunction() {
|
||||
let input = """
|
||||
protocol ViewProvider {
|
||||
@ViewBuilder
|
||||
func makeView() -> some View
|
||||
}
|
||||
"""
|
||||
testFormatting(for: input, rule: .redundantViewBuilder)
|
||||
}
|
||||
|
||||
func testKeepViewBuilderOnProtocolComputedProperty() {
|
||||
let input = """
|
||||
protocol ContentProvider {
|
||||
@ViewBuilder
|
||||
var content: some View { get }
|
||||
}
|
||||
|
||||
struct MyContent: ContentProvider {
|
||||
var content: some View {
|
||||
Text("Hello")
|
||||
Text("World")
|
||||
}
|
||||
}
|
||||
"""
|
||||
testFormatting(for: input, rule: .redundantViewBuilder)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user