Skip to content

Commit

Permalink
Fix --nilInit insert being applied to computed property (#1735)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakuyoMo committed Jun 17, 2024
1 parent 9a4258e commit 4433c16
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 14 deletions.
8 changes: 4 additions & 4 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2951,7 +2951,7 @@ public struct _FormatRules {
help: "Remove/insert redundant `nil` default value (Optional vars are nil by default).",
options: ["nilinit"]
) { formatter in
func search(from index: Int) {
func search(from index: Int, isStoredProperty: Bool) {
if let optionalIndex = formatter.index(of: .unwrapOperator, after: index) {
if formatter.index(of: .endOfStatement, in: index + 1 ..< optionalIndex) != nil {
return
Expand All @@ -2968,13 +2968,13 @@ public struct _FormatRules {
formatter.removeTokens(in: optionalIndex + 1 ... nilIndex)
}
case .insert:
if equalsIndex == nil {
if isStoredProperty && equalsIndex == nil {
let tokens: [Token] = [.space(" "), .operator("=", .infix), .space(" "), .identifier("nil")]
formatter.insert(tokens, at: optionalIndex + 1)
}
}
}
search(from: optionalIndex)
search(from: optionalIndex, isStoredProperty: isStoredProperty)
}
}
Expand Down Expand Up @@ -3006,7 +3006,7 @@ public struct _FormatRules {
}
}
// Find the nil
search(from: i)
search(from: i, isStoredProperty: formatter.isStoredProperty(atIntroducerIndex: i))
}
}
Expand Down
78 changes: 68 additions & 10 deletions Tests/RulesTests+Redundancy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2045,14 +2045,14 @@ class RedundancyTests: RulesTests {
}

func testNoInsertLazyVarNilInit() {
let input = "lazy var foo: Int? = nil"
let input = "lazy var foo: Int?"
let options = FormatOptions(nilInit: .insert)
testFormatting(for: input, rule: FormatRules.redundantNilInit,
options: options)
}

func testNoInsertLazyPublicPrivateSetVarNilInit() {
let input = "lazy private(set) public var foo: Int? = nil"
let input = "lazy private(set) public var foo: Int?"
let options = FormatOptions(nilInit: .insert)
testFormatting(for: input, rule: FormatRules.redundantNilInit, options: options,
exclude: ["modifierOrder"])
Expand All @@ -2066,28 +2066,28 @@ class RedundancyTests: RulesTests {
}

func testNoInsertNilInitWithPropertyWrapper() {
let input = "@Foo var foo: Int? = nil"
let input = "@Foo var foo: Int?"
let options = FormatOptions(nilInit: .insert)
testFormatting(for: input, rule: FormatRules.redundantNilInit,
options: options)
}

func testNoInsertNilInitWithLowercasePropertyWrapper() {
let input = "@foo var foo: Int? = nil"
let input = "@foo var foo: Int?"
let options = FormatOptions(nilInit: .insert)
testFormatting(for: input, rule: FormatRules.redundantNilInit,
options: options)
}

func testNoInsertNilInitWithPropertyWrapperWithArgument() {
let input = "@Foo(bar: baz) var foo: Int? = nil"
let input = "@Foo(bar: baz) var foo: Int?"
let options = FormatOptions(nilInit: .insert)
testFormatting(for: input, rule: FormatRules.redundantNilInit,
options: options)
}

func testNoInsertNilInitWithLowercasePropertyWrapperWithArgument() {
let input = "@foo(bar: baz) var foo: Int? = nil"
let input = "@foo(bar: baz) var foo: Int?"
let options = FormatOptions(nilInit: .insert)
testFormatting(for: input, rule: FormatRules.redundantNilInit,
options: options)
Expand Down Expand Up @@ -2157,6 +2157,7 @@ class RedundancyTests: RulesTests {
}

func testNoInsertNilInitInViewBuilder() {
// Not insert `nil` in result builder
let input = """
struct TestView: View {
var body: some View {
Expand All @@ -2170,7 +2171,8 @@ class RedundancyTests: RulesTests {
options: options)
}

func testInsertNilInitInIfStatementInViewBuilder() {
func testNoInsertNilInitInIfStatementInViewBuilder() {
// Not insert `nil` in result builder
let input = """
struct TestView: View {
var body: some View {
Expand All @@ -2183,12 +2185,13 @@ class RedundancyTests: RulesTests {
}
}
"""
let options = FormatOptions(nilInit: .remove)
let options = FormatOptions(nilInit: .insert)
testFormatting(for: input, rule: FormatRules.redundantNilInit,
options: options)
}

func testInsertNilInitInSwitchStatementInViewBuilder() {
func testNoInsertNilInitInSwitchStatementInViewBuilder() {
// Not insert `nil` in result builder
let input = """
struct TestView: View {
var body: some View {
Expand All @@ -2203,11 +2206,66 @@ class RedundancyTests: RulesTests {
}
}
"""
let options = FormatOptions(nilInit: .remove)
let options = FormatOptions(nilInit: .insert)
testFormatting(for: input, rule: FormatRules.redundantNilInit,
options: options)
}

func testNoInsertNilInitInSingleLineComputedProperty() {
let input = """
var bar: String? { "some string" }
var foo: String? { nil }
"""
let options = FormatOptions(nilInit: .insert)
testFormatting(for: input, rule: FormatRules.redundantNilInit,
options: options)
}

func testNoInsertNilInitInMultilineComputedProperty() {
let input = """
var foo: String? {
print("some")
}
var bar: String? {
nil
}
"""
let options = FormatOptions(nilInit: .insert)
testFormatting(for: input, rule: FormatRules.redundantNilInit,
options: options)
}

func testNoInsertNilInitInCustomGetterAndSetterProperty() {
let input = """
var _foo: String? = nil
var foo: String? {
set { _foo = newValue }
get { newValue }
}
"""
let options = FormatOptions(nilInit: .insert)
testFormatting(for: input, rule: FormatRules.redundantNilInit,
options: options)
}

func testInsertNilInitInInstancePropertyWithBody() {
let input = """
var foo: String? {
didSet { print(foo) }
}
"""

let output = """
var foo: String? = nil {
didSet { print(foo) }
}
"""
let options = FormatOptions(nilInit: .insert)
testFormatting(for: input, output, rule: FormatRules.redundantNilInit,
options: options)
}

// MARK: - redundantLet

func testRemoveRedundantLet() {
Expand Down

0 comments on commit 4433c16

Please sign in to comment.