mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Fix nil init value removed with property wrappers
This commit is contained in:
1 parent
beee31bf07
commit
f0c56bf59b
3 files changed
+21
-2
No files matched your search
+4
-1
@@ -1901,7 +1901,10 @@ public struct _FormatRules {
|
||||
|
||||
// Check specifiers don't include `lazy`
|
||||
formatter.forEach(.keyword("var")) { i, _ in
|
||||
if formatter.specifiersForType(at: i, contains: "lazy") {
|
||||
if formatter.specifiersForType(at: i, contains: {
|
||||
let string = $1.string
|
||||
return string == "lazy" || (string != "@objc" && string.hasPrefix("@"))
|
||||
}) {
|
||||
return // Can't remove the init
|
||||
}
|
||||
// Check this isn't a Codable
|
||||
|
||||
@@ -5039,6 +5039,20 @@ class RulesTests: XCTestCase {
|
||||
XCTAssertEqual(try format(input + "\n", rules: FormatRules.all), output + "\n")
|
||||
}
|
||||
|
||||
func testNoRemoveNilInitWithCustomAttribute() {
|
||||
let input = "@Foo var foo: Int? = nil"
|
||||
let output = input
|
||||
XCTAssertEqual(try format(input, rules: [FormatRules.redundantNilInit]), output)
|
||||
XCTAssertEqual(try format(input + "\n", rules: FormatRules.all), output + "\n")
|
||||
}
|
||||
|
||||
func testRemoveNilInitWithObjcAttributes() {
|
||||
let input = "@objc var foo: Int? = nil"
|
||||
let output = "@objc var foo: Int?"
|
||||
XCTAssertEqual(try format(input, rules: [FormatRules.redundantNilInit]), output)
|
||||
XCTAssertEqual(try format(input + "\n", rules: FormatRules.all), output + "\n")
|
||||
}
|
||||
|
||||
// MARK: redundantLet
|
||||
|
||||
func testRemoveRedundantLet() {
|
||||
|
||||
@@ -490,6 +490,7 @@ extension RulesTests {
|
||||
("testEnumCaseIndentingCommas", testEnumCaseIndentingCommas),
|
||||
("testEnumCaseIndentingCommasWithXcodeStyle", testEnumCaseIndentingCommasWithXcodeStyle),
|
||||
("testEnumCaseNotEqualYodaCondition", testEnumCaseNotEqualYodaCondition),
|
||||
("testEnumCaseWrappedIfWithXcodeStyle", testEnumCaseWrappedIfWithXcodeStyle),
|
||||
("testEnumIfCaseEndifIndenting", testEnumIfCaseEndifIndenting),
|
||||
("testExponentialGrouping", testExponentialGrouping),
|
||||
("testExpressionCountEqualsZero", testExpressionCountEqualsZero),
|
||||
@@ -571,7 +572,6 @@ extension RulesTests {
|
||||
("testIfEndifInsideEnumWithTrailingCommentOutdenting", testIfEndifInsideEnumWithTrailingCommentOutdenting),
|
||||
("testIfEndifNoIndenting", testIfEndifNoIndenting),
|
||||
("testIfEndifOutdenting", testIfEndifOutdenting),
|
||||
("testEnumCaseWrappedIfWithXcodeStyle", testEnumCaseWrappedIfWithXcodeStyle),
|
||||
("testIfFunctionAndReplaced", testIfFunctionAndReplaced),
|
||||
("testIfUnknownCaseEndifIndenting", testIfUnknownCaseEndifIndenting),
|
||||
("testIfUnknownCaseEndifIndenting2", testIfUnknownCaseEndifIndenting2),
|
||||
@@ -828,6 +828,7 @@ extension RulesTests {
|
||||
("testNoRemoveLinebreakAfterCommentInElements", testNoRemoveLinebreakAfterCommentInElements),
|
||||
("testNoRemoveMethodSignature", testNoRemoveMethodSignature),
|
||||
("testNoRemoveMultilineGetSet", testNoRemoveMultilineGetSet),
|
||||
("testNoRemoveNilInitWithCustomAttribute", testNoRemoveNilInitWithCustomAttribute),
|
||||
("testNoRemoveNonNilInit", testNoRemoveNonNilInit),
|
||||
("testNoRemoveNonRedundantNestedFunctionSelf", testNoRemoveNonRedundantNestedFunctionSelf),
|
||||
("testNoRemoveNonRedundantNestedFunctionSelf2", testNoRemoveNonRedundantNestedFunctionSelf2),
|
||||
@@ -1114,6 +1115,7 @@ extension RulesTests {
|
||||
("testRemoveInternalFunctionArgumentName", testRemoveInternalFunctionArgumentName),
|
||||
("testRemoveMultilineIsolatedGet", testRemoveMultilineIsolatedGet),
|
||||
("testRemoveMultipleRedundantNilInitsInSameLine", testRemoveMultipleRedundantNilInitsInSameLine),
|
||||
("testRemoveNilInitWithObjcAttributes", testRemoveNilInitWithObjcAttributes),
|
||||
("testRemoveRedundantBackticksInLet", testRemoveRedundantBackticksInLet),
|
||||
("testRemoveRedundantEmptyReturnType", testRemoveRedundantEmptyReturnType),
|
||||
("testRemoveRedundantImplicitUnwrapInit", testRemoveRedundantImplicitUnwrapInit),
|
||||
|
||||
Reference in new issue
Block a user