From d2148c730e8523df85d43168d7c17feb694151bf Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Tue, 23 Jan 2024 22:45:48 +0000 Subject: [PATCH] Preserve internal import --- Sources/Rules.swift | 10 ++++++---- Tests/RulesTests+Redundancy.swift | 5 +++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Sources/Rules.swift b/Sources/Rules.swift index e23a272a..493ea903 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -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. diff --git a/Tests/RulesTests+Redundancy.swift b/Tests/RulesTests+Redundancy.swift index a89eef33..76f8ff3a 100644 --- a/Tests/RulesTests+Redundancy.swift +++ b/Tests/RulesTests+Redundancy.swift @@ -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() {