Preserve internal import

This commit is contained in:
Nick Lockwood
2024-01-26 00:20:26 +00:00
parent 630cdff508
commit d2148c730e
2 changed files with 11 additions and 4 deletions
+6 -4
View File
@@ -7310,17 +7310,19 @@ public struct _FormatRules {
help: "Remove redundant internal access control."
) { formatter in
formatter.forEach(.keyword("internal")) { internalKeywordIndex, _ in
let accessControlLevels = ["public", "package", "internal", "private", "fileprivate"]
// Don't remove import acl
if formatter.next(.nonSpaceOrComment, after: internalKeywordIndex) == .keyword("import") {
return
}
// If we're inside an extension, than `internal` is only redundant
// if the extension itself is `internal`.
// If we're inside an extension, then `internal` is only redundant if the extension itself is `internal`.
if let startOfScope = formatter.startOfScope(at: internalKeywordIndex),
let typeKeywordIndex = formatter.indexOfLastSignificantKeyword(at: startOfScope),
formatter.tokens[typeKeywordIndex] == .keyword("extension"),
// In the language grammar, the ACL level always directly precedes the
// `extension` keyword if present.
let previousToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: typeKeywordIndex),
accessControlLevels.contains(previousToken.string),
["public", "package", "internal", "private", "fileprivate"].contains(previousToken.string),
previousToken.string != "internal"
{
// The extension has an explicit ACL other than `internal`, so is not internal.
+5
View File
@@ -9165,6 +9165,11 @@ class RedundancyTests: RulesTests {
testFormatting(for: input, output, rule: FormatRules.redundantInternal, exclude: ["redundantExtensionACL"])
}
func testPreserveInternalImport() {
let input = "internal import MyPackage"
testFormatting(for: input, rule: FormatRules.redundantInternal)
}
// MARK: - noExplicitOwnership
func testRemovesOwnershipKeywordsFromFunc() {