Skip to content

Commit 7a77247

Browse files
committed
Don't apply FileprivateAtFileScope rule to extensions.
This is a subtlety of SE-0169. For most decls, `private` at file scope is equivalent to `fileprivate`. But for extensions, since that visibility is distributed throughout the extension's members, changing `private` to `fileprivate` would increase the visibility of those members. When `private`, those members would only be visible within the same extension scope and within other extensions to the same type and to the main type decl itself *that occur within the same file.* If they were made `fileprivate`, then this would make them visible to *all* declarations within the same file.
1 parent 27a800f commit 7a77247

File tree

3 files changed

+13
-18
lines changed

3 files changed

+13
-18
lines changed

Sources/SwiftFormatRules/FileprivateAtFileScope.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,6 @@ public final class FileprivateAtFileScope: SyntaxFormatRule {
9292
modifiers: typealiasDecl.modifiers,
9393
factory: typealiasDecl.withModifiers)))
9494

95-
case .extensionDecl(let extensionDecl):
96-
return codeBlockItem.withItem(
97-
Syntax(rewrittenDecl(
98-
extensionDecl,
99-
modifiers: extensionDecl.modifiers,
100-
factory: extensionDecl.withModifiers)))
101-
10295
default:
10396
return codeBlockItem
10497
}

Tests/SwiftFormatRulesTests/FileprivateAtFileScopeTests.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ final class FileprivateAtFileScopeTests: LintOrFormatRuleTestCase {
1010
private enum Foo {}
1111
private protocol Foo {}
1212
private typealias Foo = Bar
13-
private extension Foo {}
1413
private func foo() {}
1514
private var foo: Bar
1615
""",
@@ -20,7 +19,6 @@ final class FileprivateAtFileScopeTests: LintOrFormatRuleTestCase {
2019
fileprivate enum Foo {}
2120
fileprivate protocol Foo {}
2221
fileprivate typealias Foo = Bar
23-
fileprivate extension Foo {}
2422
fileprivate func foo() {}
2523
fileprivate var foo: Bar
2624
""")
@@ -31,7 +29,18 @@ final class FileprivateAtFileScopeTests: LintOrFormatRuleTestCase {
3129
XCTAssertDiagnosed(.replacePrivateWithFileprivate)
3230
XCTAssertDiagnosed(.replacePrivateWithFileprivate)
3331
XCTAssertDiagnosed(.replacePrivateWithFileprivate)
34-
XCTAssertDiagnosed(.replacePrivateWithFileprivate)
32+
}
33+
34+
func testFileScopeExtensionsAreNotChanged() {
35+
XCTAssertFormatting(
36+
FileprivateAtFileScope.self,
37+
input: """
38+
private extension Foo {}
39+
""",
40+
expected: """
41+
private extension Foo {}
42+
""")
43+
XCTAssertNotDiagnosed(.replacePrivateWithFileprivate)
3544
}
3645

3746
func testNonFileScopeDeclsAreNotChanged() {
@@ -70,7 +79,6 @@ final class FileprivateAtFileScopeTests: LintOrFormatRuleTestCase {
7079
private enum Foo {}
7180
private protocol Foo {}
7281
private typealias Foo = Bar
73-
private extension Foo {}
7482
private func foo() {}
7583
private var foo: Bar
7684
#elseif BAR
@@ -79,7 +87,6 @@ final class FileprivateAtFileScopeTests: LintOrFormatRuleTestCase {
7987
private enum Foo {}
8088
private protocol Foo {}
8189
private typealias Foo = Bar
82-
private extension Foo {}
8390
private func foo() {}
8491
private var foo: Bar
8592
#else
@@ -88,7 +95,6 @@ final class FileprivateAtFileScopeTests: LintOrFormatRuleTestCase {
8895
private enum Foo {}
8996
private protocol Foo {}
9097
private typealias Foo = Bar
91-
private extension Foo {}
9298
private func foo() {}
9399
private var foo: Bar
94100
#endif
@@ -100,7 +106,6 @@ final class FileprivateAtFileScopeTests: LintOrFormatRuleTestCase {
100106
fileprivate enum Foo {}
101107
fileprivate protocol Foo {}
102108
fileprivate typealias Foo = Bar
103-
fileprivate extension Foo {}
104109
fileprivate func foo() {}
105110
fileprivate var foo: Bar
106111
#elseif BAR
@@ -109,7 +114,6 @@ final class FileprivateAtFileScopeTests: LintOrFormatRuleTestCase {
109114
fileprivate enum Foo {}
110115
fileprivate protocol Foo {}
111116
fileprivate typealias Foo = Bar
112-
fileprivate extension Foo {}
113117
fileprivate func foo() {}
114118
fileprivate var foo: Bar
115119
#else
@@ -118,7 +122,6 @@ final class FileprivateAtFileScopeTests: LintOrFormatRuleTestCase {
118122
fileprivate enum Foo {}
119123
fileprivate protocol Foo {}
120124
fileprivate typealias Foo = Bar
121-
fileprivate extension Foo {}
122125
fileprivate func foo() {}
123126
fileprivate var foo: Bar
124127
#endif
@@ -136,7 +139,6 @@ final class FileprivateAtFileScopeTests: LintOrFormatRuleTestCase {
136139
private enum Foo {}
137140
private protocol Foo {}
138141
private typealias Foo = Bar
139-
private extension Foo {}
140142
private func foo() {}
141143
private var foo: Bar
142144
#endif
@@ -150,7 +152,6 @@ final class FileprivateAtFileScopeTests: LintOrFormatRuleTestCase {
150152
fileprivate enum Foo {}
151153
fileprivate protocol Foo {}
152154
fileprivate typealias Foo = Bar
153-
fileprivate extension Foo {}
154155
fileprivate func foo() {}
155156
fileprivate var foo: Bar
156157
#endif

Tests/SwiftFormatRulesTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ extension FileprivateAtFileScopeTests {
7070
("testFileScopeDecls", testFileScopeDecls),
7171
("testFileScopeDeclsInsideConditionals", testFileScopeDeclsInsideConditionals),
7272
("testFileScopeDeclsInsideNestedConditionals", testFileScopeDeclsInsideNestedConditionals),
73+
("testFileScopeExtensionsAreNotChanged", testFileScopeExtensionsAreNotChanged),
7374
("testLeadingTriviaIsPreserved", testLeadingTriviaIsPreserved),
7475
("testModifierDetailIsPreserved", testModifierDetailIsPreserved),
7576
("testNonFileScopeDeclsAreNotChanged", testNonFileScopeDeclsAreNotChanged),

0 commit comments

Comments
 (0)