Skip to content

Commit

Permalink
Ignore meta class types in prefer_self_in_static_references rule (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Mar 2, 2022
1 parent e6c92f5 commit 0e7b588
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("""
Expand All @@ -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("""
Expand Down Expand Up @@ -123,20 +119,20 @@ 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() }
}
""")
]
)

private static let nestedKindsToIgnore: Set = [
private static let complexDeclarations: Set = [
SwiftDeclarationKind.class,
SwiftDeclarationKind.enum,
SwiftDeclarationKind.struct
Expand Down Expand Up @@ -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 []
}

Expand All @@ -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 = "(?<!\\.)\\b\(name)\\.\(kind == .class ? "(?!self)" : "")"
var location = bodyRange.location
return rangesToIgnore
.flatMap { (range: ByteRange) -> [NSRange] in
Expand All @@ -199,22 +198,18 @@ public struct PreferSelfInStaticReferencesRule: SubstitutionCorrectableASTRule,
let searchRange = ByteRange(location: location, length: range.lowerBound - location)
location = range.upperBound
return file.match(
pattern: "(?<!\\.)\\b\(name)\\.",
pattern: pattern,
with: [.identifier],
range: file.stringView.byteRangeToNSRange(searchRange))
}
}

private func isComplexDeclaration(_ kind: SwiftDeclarationKind) -> 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 {
Expand Down

0 comments on commit 0e7b588

Please sign in to comment.