Skip to content

Commit 069a90a

Browse files
committed
Fix attribute misidentified as accessor in computed property
1 parent 0d1b914 commit 069a90a

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Sources/FormattingHelpers.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3356,6 +3356,18 @@ extension Formatter {
33563356
return false
33573357
}
33583358
}), let startIndex = self.index(of: .startOfScope("{"), after: nextIndex) {
3359+
if tokens[nextIndex].isAttribute {
3360+
guard let startIndex = self.index(of: .nonSpaceOrComment, after: nextIndex),
3361+
tokens[startIndex] == .startOfScope("("),
3362+
let endIndex = endOfScope(at: startIndex)
3363+
else {
3364+
// Not an accessor
3365+
break
3366+
}
3367+
// Could be an attribute on an accessor
3368+
index = endIndex
3369+
continue
3370+
}
33593371
foundAccessors = true
33603372
index = startIndex + 1
33613373
if let parenStart = self.index(of: .nonSpaceOrCommentOrLinebreak, after: nextIndex, if: {

Tests/RulesTests+Redundancy.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6291,6 +6291,25 @@ class RedundancyTests: RulesTests {
62916291
testFormatting(for: input, rule: FormatRules.redundantSelf, options: options)
62926292
}
62936293

6294+
func testNoInsertSelfBeforeBinding() {
6295+
let input = """
6296+
struct MyView: View {
6297+
@Environment(ViewModel.self) var viewModel
6298+
6299+
var body: some View {
6300+
@Bindable var viewModel = self.viewModel
6301+
ZStack {
6302+
MySubview(
6303+
navigationPath: $viewModel.navigationPath
6304+
)
6305+
}
6306+
}
6307+
}
6308+
"""
6309+
let options = FormatOptions(explicitSelf: .insert, swiftVersion: "5.10")
6310+
testFormatting(for: input, rule: FormatRules.redundantSelf, options: options)
6311+
}
6312+
62946313
// explicitSelf = .initOnly
62956314

62966315
func testPreserveSelfInsideClassInit() {

0 commit comments

Comments
 (0)