Skip to content

Commit 99983f1

Browse files
committed
[HOTFIX] Fixing bug where Variable.hasSetter returned true for explicit getter only
1 parent bd92605 commit 99983f1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Sources/SyntaxSparrow/Internal/Resolvers/Declaration/VariableSemanticsResolver.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ struct VariableSemanticsResolver: SemanticsResolving {
116116
// Resolver accessors for assessment
117117
let accessors = resolveAccessors()
118118
let accessorKinds = accessors.compactMap(\.kind)
119+
// Only getter accessors mean that there is no setters
120+
guard accessorKinds != [.get] else { return false }
121+
// General setter check
119122
let hasSetterAccessor = accessorKinds.contains(where: { [.set, .willSet, .didSet].contains($0) })
120123
let hasEffectGetter = accessors.contains(where: {
121124
let hasSpecifier = ($0.effectSpecifiers?.throwsSpecifier != nil || $0.effectSpecifiers?.asyncSpecifier != nil)
@@ -125,7 +128,7 @@ struct VariableSemanticsResolver: SemanticsResolving {
125128
guard !hasEffectGetter else { return false }
126129
// If setter exists in accessors can return true (usually protocol context or manually written will/did/set accessor).
127130
if hasSetterAccessor { return true }
128-
// If no accessors, but a direct return/code block, can assume there is no setter
131+
// If no accessors, but a direct return/code block, can assume there is no setter. (computed var)
129132
if accessors.isEmpty, resolveHasCodeBlockItems() { return false }
130133
// Otherwise if the keyword is not `let` (immutable)
131134
guard resolveKeyword() != "let" else { return false }

Tests/SyntaxSparrowTests/Declarations/VariableTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ final class VariableTests: XCTestCase {
6666
XCTAssertFalse(variable.hasSetter)
6767
}
6868

69+
func test_variable_mutable_onlyGetter_willReturnFalseForHasSetter() {
70+
let source = #"""
71+
var name: String {
72+
get { "test" }
73+
}
74+
var other: String {
75+
"test"
76+
}
77+
"""#
78+
instanceUnderTest.updateToSource(source)
79+
XCTAssertTrue(instanceUnderTest.isStale)
80+
instanceUnderTest.collectChildren()
81+
XCTAssertFalse(instanceUnderTest.isStale)
82+
XCTAssertEqual(instanceUnderTest.variables.count, 2)
83+
XCTAssertFalse(instanceUnderTest.variables[0].hasSetter)
84+
XCTAssertFalse(instanceUnderTest.variables[1].hasSetter)
85+
}
86+
6987
func test_variable_multiplePatternBindings_willResolveExpectedValues() {
7088
let source = #"""
7189
var firstName, lastName: String

0 commit comments

Comments
 (0)