diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c6f31a5a..1d7681c6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -260,6 +260,7 @@ * Trigger `prefer_self_in_static_references` rule on more type references like: * Key paths (e.g. `\MyType.myVar` -> `\Self.myVar`) + * Computed properties (e.g. `var i: Int { MyType.myVar )` -> `var i: Int { Self.myVar }`) [SimplyDanny](https://github.com/SimplyDanny) diff --git a/Source/SwiftLintFramework/Rules/Style/PreferSelfInStaticReferencesRule.swift b/Source/SwiftLintFramework/Rules/Style/PreferSelfInStaticReferencesRule.swift index 5ecbf7e70..975954ea7 100644 --- a/Source/SwiftLintFramework/Rules/Style/PreferSelfInStaticReferencesRule.swift +++ b/Source/SwiftLintFramework/Rules/Style/PreferSelfInStaticReferencesRule.swift @@ -80,14 +80,6 @@ public struct PreferSelfInStaticReferencesRule: SwiftSyntaxRule, CorrectableRule @objc var s = "" @objc func f() { _ = #keyPath(C.s) } } - """, excludeFromDocumentation: true), - Example(""" - extension E { - class C { - static let i = 2 - var j: Int { C.i } - } - } """, excludeFromDocumentation: true) ], triggeringExamples: [ @@ -99,7 +91,7 @@ public struct PreferSelfInStaticReferencesRule: SwiftSyntaxRule, CorrectableRule } static let i = 1 let h = C.i - var j: Int { C.i } + var j: Int { ↓C.i } func f() -> Int { ↓C.i + h } } """), @@ -131,7 +123,19 @@ public struct PreferSelfInStaticReferencesRule: SwiftSyntaxRule, CorrectableRule static func f() -> E { ↓E.A } static func g() -> E { ↓E.f() } } - """) + """), + Example(""" + extension E { + class C { + static var i = 2 + var j: Int { ↓C.i } + var k: Int { + get { ↓C.i } + set { ↓C.i = newValue } + } + } + } + """, excludeFromDocumentation: true) ], corrections: [ Example(""" @@ -321,6 +325,10 @@ private class Visitor: ViolationsSyntaxVisitor { } override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind { + if node.bindings.count == 1, node.bindings.first?.accessor != nil { + // Computed property + return .visitChildren + } if case .handleReferences = variableDeclScopes.last { return .visitChildren }