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:
Copilot
2026-02-16 10:29:19 -08:00
committed by GitHub
co-authored by calda
parent 9884549bae
commit 8e8d4e45d5
2 changed files with 47 additions and 0 deletions
+6
View File
@@ -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)
}
}