From 0e7b588d50f5bb4a25d2511053f0362585dcb062 Mon Sep 17 00:00:00 2001 From: Danny Moesch Date: Wed, 2 Mar 2022 16:52:12 +0100 Subject: [PATCH] Ignore meta class types in `prefer_self_in_static_references` rule (#3832) --- CHANGELOG.md | 4 +++ .../PreferSelfInStaticReferencesRule.swift | 27 ++++++++----------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03fec3667a..6bbbf6ebd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,10 @@ [Natan Rolnik](https://github.com/natanrolnik) [#3857](https://github.com/realm/SwiftLint/pull/3857) +* Ignore meta class types in `prefer_self_in_static_references` rule. + [SimplyDanny](https://github.com/SimplyDanny) + [#3804](https://github.com/realm/SwiftLint/issues/3804) + ## 0.46.2: Detergent Package #### Breaking diff --git a/Source/SwiftLintFramework/Rules/Style/PreferSelfInStaticReferencesRule.swift b/Source/SwiftLintFramework/Rules/Style/PreferSelfInStaticReferencesRule.swift index b994491b9b..5cf3ef5d48 100644 --- a/Source/SwiftLintFramework/Rules/Style/PreferSelfInStaticReferencesRule.swift +++ b/Source/SwiftLintFramework/Rules/Style/PreferSelfInStaticReferencesRule.swift @@ -21,12 +21,6 @@ public struct PreferSelfInStaticReferencesRule: SubstitutionCorrectableASTRule, func f() -> Int { Self.i } } """), - Example(""" - class S { - static func f() { Self.g(Self.f) } - static func g(f: () -> Void) { f() } - } - """), Example(""" struct T { static let i = 0 @@ -59,6 +53,7 @@ public struct PreferSelfInStaticReferencesRule: SubstitutionCorrectableASTRule, func g(@GreaterEqualThan(C.s) j: Int = C.s) -> Int { j } return i + Self.s } + func g() -> Any { C.self } } """), Example(""" @@ -82,6 +77,7 @@ public struct PreferSelfInStaticReferencesRule: SubstitutionCorrectableASTRule, struct S { static let i = 0 func f() -> Int { ↓S.i } + func g() -> Any { ↓S.self } } """), Example(""" @@ -123,12 +119,12 @@ public struct PreferSelfInStaticReferencesRule: SubstitutionCorrectableASTRule, } """), Example(""" - struct S { + class S { static func f() { ↓S.g(↓S.f) } static func g(f: () -> Void) { f() } } """): Example(""" - struct S { + class S { static func f() { Self.g(Self.f) } static func g(f: () -> Void) { f() } } @@ -136,7 +132,7 @@ public struct PreferSelfInStaticReferencesRule: SubstitutionCorrectableASTRule, ] ) - private static let nestedKindsToIgnore: Set = [ + private static let complexDeclarations: Set = [ SwiftDeclarationKind.class, SwiftDeclarationKind.enum, SwiftDeclarationKind.struct @@ -178,7 +174,9 @@ public struct PreferSelfInStaticReferencesRule: SubstitutionCorrectableASTRule, public func violationRanges(in file: SwiftLintFile, kind: SwiftDeclarationKind, dictionary: SourceKittenDictionary) -> [NSRange] { - guard isComplexDeclaration(kind), let name = dictionary.name, let bodyRange = dictionary.bodyByteRange else { + guard Self.complexDeclarations.contains(kind), + let name = dictionary.name, + let bodyRange = dictionary.bodyByteRange else { return [] } @@ -189,6 +187,7 @@ public struct PreferSelfInStaticReferencesRule: SubstitutionCorrectableASTRule, .sorted { $0.location < $1.location } rangesToIgnore.append(ByteRange(location: bodyRange.upperBound, length: 0)) // Marks the end of the search + let pattern = "(? [NSRange] in @@ -199,22 +198,18 @@ public struct PreferSelfInStaticReferencesRule: SubstitutionCorrectableASTRule, let searchRange = ByteRange(location: location, length: range.lowerBound - location) location = range.upperBound return file.match( - pattern: "(? Bool { - kind == .class || kind == .struct || kind == .enum - } - private func getSubstructuresToIgnore(in structure: SourceKittenDictionary, containedIn parentKind: SwiftDeclarationKind) -> [SourceKittenDictionary] { guard let kind = structure.kind, let declarationKind = SwiftDeclarationKind(rawValue: kind) else { return [] } - if Self.nestedKindsToIgnore.contains(declarationKind) { + if Self.complexDeclarations.contains(declarationKind) { return [structure] } if parentKind != .class {