mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
46 lines
1.3 KiB
Swift
46 lines
1.3 KiB
Swift
//
|
|
// EmptyExtensions.swift
|
|
// SwiftFormat
|
|
//
|
|
// Created by Manny Lopez on 7/30/24.
|
|
// Copyright © 2024 Nick Lockwood. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public extension FormatRule {
|
|
/// Remove empty, non-conforming, extensions.
|
|
static let emptyExtensions = FormatRule(
|
|
help: "Remove empty, non-protocol-conforming extensions.",
|
|
orderAfter: [.unusedPrivateDeclarations]
|
|
) { formatter in
|
|
var emptyExtensions = [TypeDeclaration]()
|
|
|
|
for declaration in formatter.parseDeclarations() {
|
|
guard declaration.keyword == "extension",
|
|
let extensionDeclaration = declaration.asTypeDeclaration,
|
|
extensionDeclaration.body.isEmpty,
|
|
// Ensure that it is not a macro
|
|
!extensionDeclaration.modifiers.contains(where: \.isAttribute)
|
|
else { continue }
|
|
|
|
// Ensure that the extension does not conform to any protocols
|
|
guard extensionDeclaration.conformances.isEmpty else { continue }
|
|
|
|
emptyExtensions.append(extensionDeclaration)
|
|
}
|
|
|
|
for emptyExtension in emptyExtensions {
|
|
emptyExtension.remove()
|
|
}
|
|
} examples: {
|
|
"""
|
|
```diff
|
|
- extension String {}
|
|
-
|
|
extension String: Equatable {}
|
|
```
|
|
"""
|
|
}
|
|
}
|