mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Preserve internal import
This commit is contained in:
+6
-4
@@ -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.
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user