From 07661d2bbc4df57754fd3470415fd5ec716ff17d Mon Sep 17 00:00:00 2001 From: Cal Stephens Date: Sun, 3 Nov 2024 10:13:18 -0800 Subject: [PATCH] Migrate emptyExtension rule to DeclarationV2 --- Sources/DeclarationV2.swift | 5 +++++ Sources/Rules/EmptyExtensions.swift | 27 ++++++++++----------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Sources/DeclarationV2.swift b/Sources/DeclarationV2.swift index d8d11f332..36d55e1f4 100644 --- a/Sources/DeclarationV2.swift +++ b/Sources/DeclarationV2.swift @@ -144,6 +144,11 @@ extension DeclarationV2 { return formatter.parsePropertyDeclaration(atIntroducerIndex: keywordIndex) } + /// The `TypeDeclaration` for this declaration, if it's a type with a body. + var asTypeDeclaration: TypeDeclaration? { + self as? TypeDeclaration + } + /// A list of all declarations that are a parent of this declaration var parentDeclarations: [DeclarationV2] { guard let parent = parent else { return [] } diff --git a/Sources/Rules/EmptyExtensions.swift b/Sources/Rules/EmptyExtensions.swift index 56dcb5d9b..893997f6e 100644 --- a/Sources/Rules/EmptyExtensions.swift +++ b/Sources/Rules/EmptyExtensions.swift @@ -14,31 +14,24 @@ public extension FormatRule { help: "Remove empty, non-conforming, extensions.", orderAfter: [.unusedPrivateDeclarations] ) { formatter in - var emptyExtensions = [Declaration]() + var emptyExtensions = [TypeDeclaration]() - formatter.forEachRecursiveDeclaration { declaration, _ in - let declarationModifiers = Set(declaration.modifiers) + for declaration in formatter.parseDeclarationsV2() { guard declaration.keyword == "extension", - let declarationBody = declaration.body, - declarationBody.isEmpty, + let extensionDeclaration = declaration.asTypeDeclaration, + extensionDeclaration.body.isEmpty, // Ensure that it is not a macro - !declarationModifiers.contains(where: { $0.first == "@" }) - else { return } + !extensionDeclaration.modifiers.contains(where: { $0.first == "@" }) + else { continue } // Ensure that the extension does not conform to any protocols - let parser = Formatter(declaration.openTokens) - guard let extensionIndex = parser.index(of: .keyword("extension"), after: -1), - let typeNameIndex = parser.index(of: .nonSpaceOrLinebreak, after: extensionIndex), - let type = parser.parseType(at: typeNameIndex), - let indexAfterType = parser.index(of: .nonSpaceOrCommentOrLinebreak, after: type.range.upperBound), - parser.tokens[indexAfterType] != .delimiter(":") - else { return } + guard extensionDeclaration.conformances.isEmpty else { continue } - emptyExtensions.append(declaration) + emptyExtensions.append(extensionDeclaration) } - for declaration in emptyExtensions.reversed() { - formatter.removeTokens(in: declaration.originalRange) + for emptyExtension in emptyExtensions { + emptyExtension.remove() } } examples: { """