mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Fix redundantNilInit removing required nil inside ViewBuilder
This commit is contained in:
+1
-1
@@ -2852,7 +2852,7 @@ public struct _FormatRules {
|
||||
formatter.forEach(.keyword("var")) { i, _ in
|
||||
if formatter.modifiersForDeclaration(at: i, contains: {
|
||||
$1 == "lazy" || ($1 != "@objc" && $1.hasPrefix("@"))
|
||||
}) {
|
||||
}) || formatter.isInResultBuilder(at: i) {
|
||||
return // Can't remove the init
|
||||
}
|
||||
// Check this isn't a Codable
|
||||
|
||||
@@ -1772,6 +1772,51 @@ class RedundancyTests: RulesTests {
|
||||
testFormatting(for: input, output, rule: FormatRules.redundantNilInit)
|
||||
}
|
||||
|
||||
func testNoRemoveNilInitInViewBuilder() {
|
||||
let input = """
|
||||
struct TestView: View {
|
||||
var body: some View {
|
||||
var foo: String? = nil
|
||||
Text(foo ?? "")
|
||||
}
|
||||
}
|
||||
"""
|
||||
testFormatting(for: input, rule: FormatRules.redundantNilInit)
|
||||
}
|
||||
|
||||
func testNoRemoveNilInitInIfStatementInViewBuilder() {
|
||||
let input = """
|
||||
struct TestView: View {
|
||||
var body: some View {
|
||||
if true {
|
||||
var foo: String?
|
||||
Text(foo ?? "")
|
||||
} else {
|
||||
EmptyView()
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
testFormatting(for: input, rule: FormatRules.redundantNilInit)
|
||||
}
|
||||
|
||||
func testNoRemoveNilInitInSwitchStatementInViewBuilder() {
|
||||
let input = """
|
||||
struct TestView: View {
|
||||
var body: some View {
|
||||
switch foo {
|
||||
case .bar:
|
||||
var foo: String?
|
||||
Text(foo ?? "")
|
||||
default:
|
||||
EmptyView()
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
testFormatting(for: input, rule: FormatRules.redundantNilInit)
|
||||
}
|
||||
|
||||
// MARK: - redundantLet
|
||||
|
||||
func testRemoveRedundantLet() {
|
||||
|
||||
Reference in New Issue
Block a user