Files
SwiftFormat/Sources/Rules/AnyObjectProtocol.swift
T
2024-11-09 11:26:22 +00:00

32 lines
1.1 KiB
Swift

//
// AnyObjectProtocol.swift
// SwiftFormat
//
// Created by Nick Lockwood on 1/23/19.
// Copyright © 2024 Nick Lockwood. All rights reserved.
//
import Foundation
public extension FormatRule {
/// Prefer `AnyObject` over `class` for class-based protocols
static let anyObjectProtocol = FormatRule(
help: "Prefer `AnyObject` over `class` in protocol definitions."
) { formatter in
formatter.forEach(.keyword("protocol")) { i, _ in
guard formatter.options.swiftVersion >= "4.1",
let nameIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i, if: {
$0.isIdentifier
}), let colonIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: nameIndex, if: {
$0 == .delimiter(":")
}), let classIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: colonIndex, if: {
$0 == .keyword("class")
})
else {
return
}
formatter.replaceToken(at: classIndex, with: .identifier("AnyObject"))
}
}
}