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

36 lines
1.3 KiB
Swift

//
// RedundantExtensionACL.swift
// SwiftFormat
//
// Created by Nick Lockwood on 2/3/19.
// Copyright © 2024 Nick Lockwood. All rights reserved.
//
import Foundation
public extension FormatRule {
/// Remove redundant access control level modifiers in extensions
static let redundantExtensionACL = FormatRule(
help: "Remove redundant access control modifiers."
) { formatter in
formatter.forEach(.keyword("extension")) { i, _ in
var acl = ""
guard formatter.modifiersForDeclaration(at: i, contains: {
acl = $1
return _FormatRules.aclModifiers.contains(acl)
}), let startIndex = formatter.index(of: .startOfScope("{"), after: i),
var endIndex = formatter.index(of: .endOfScope("}"), after: startIndex) else {
return
}
if acl == "private" { acl = "fileprivate" }
while let aclIndex = formatter.lastIndex(of: .keyword(acl), in: startIndex + 1 ..< endIndex) {
formatter.removeToken(at: aclIndex)
if formatter.token(at: aclIndex)?.isSpace == true {
formatter.removeToken(at: aclIndex)
}
endIndex = aclIndex
}
}
}
}