Skip to content

Commit

Permalink
Preserve internal import
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Jan 26, 2024
1 parent 630cdff commit d2148c7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions Tests/RulesTests+Redundancy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit d2148c7

Please sign in to comment.