Skip to content

Commit c2d7ede

Browse files
committed
Revert "OrderedImports should create separate group for @_implementationOnly … (#1013)"
This reverts commit ae522f2. Adding separate groups can impact a lot of code, let's skip for 6.2 for now.
1 parent adcbed1 commit c2d7ede

File tree

4 files changed

+24
-119
lines changed

4 files changed

+24
-119
lines changed

Documentation/RuleDocumentation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,9 @@ Lint: If a function call with a trailing closure also contains a non-trailing cl
407407
### OrderedImports
408408

409409
Imports must be lexicographically ordered and logically grouped at the top of each source file.
410-
The order of the import groups is 1) regular imports, 2) declaration imports, 3) @_implementationOnly
411-
imports, and 4) @testable imports. These groups are separated by a single blank line. Blank lines in
412-
between the import declarations are removed.
410+
The order of the import groups is 1) regular imports, 2) declaration imports, and 3) @testable
411+
imports. These groups are separated by a single blank line. Blank lines in between the import
412+
declarations are removed.
413413

414414
Lint: If an import appears anywhere other than the beginning of the file it resides in,
415415
not lexicographically ordered, or not in the appropriate import group, a lint error is

Sources/SwiftFormat/Rules/OrderedImports.swift

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
import SwiftSyntax
1414

1515
/// Imports must be lexicographically ordered and logically grouped at the top of each source file.
16-
/// The order of the import groups is 1) regular imports, 2) declaration imports, 3) @_implementationOnly
17-
/// imports, and 4) @testable imports. These groups are separated by a single blank line. Blank lines in
18-
/// between the import declarations are removed.
16+
/// The order of the import groups is 1) regular imports, 2) declaration imports, and 3) @testable
17+
/// imports. These groups are separated by a single blank line. Blank lines in between the import
18+
/// declarations are removed.
1919
///
2020
/// Lint: If an import appears anywhere other than the beginning of the file it resides in,
2121
/// not lexicographically ordered, or not in the appropriate import group, a lint error is
@@ -34,7 +34,6 @@ public final class OrderedImports: SyntaxFormatRule {
3434

3535
var regularImports: [Line] = []
3636
var declImports: [Line] = []
37-
var implementationOnlyImports: [Line] = []
3837
var testableImports: [Line] = []
3938
var codeBlocks: [Line] = []
4039
var fileHeader: [Line] = []
@@ -53,23 +52,14 @@ public final class OrderedImports: SyntaxFormatRule {
5352

5453
regularImports = formatImports(regularImports)
5554
declImports = formatImports(declImports)
56-
implementationOnlyImports = formatImports(implementationOnlyImports)
5755
testableImports = formatImports(testableImports)
5856
formatCodeblocks(&codeBlocks)
5957

60-
let joined = joinLines(
61-
fileHeader,
62-
regularImports,
63-
declImports,
64-
implementationOnlyImports,
65-
testableImports,
66-
codeBlocks
67-
)
58+
let joined = joinLines(fileHeader, regularImports, declImports, testableImports, codeBlocks)
6859
formattedLines.append(contentsOf: joined)
6960

7061
regularImports = []
7162
declImports = []
72-
implementationOnlyImports = []
7363
testableImports = []
7464
codeBlocks = []
7565
fileHeader = []
@@ -125,11 +115,6 @@ public final class OrderedImports: SyntaxFormatRule {
125115
regularImports.append(line)
126116
commentBuffer = []
127117

128-
case .implementationOnlyImport:
129-
implementationOnlyImports.append(contentsOf: commentBuffer)
130-
implementationOnlyImports.append(line)
131-
commentBuffer = []
132-
133118
case .testableImport:
134119
testableImports.append(contentsOf: commentBuffer)
135120
testableImports.append(line)
@@ -163,7 +148,6 @@ public final class OrderedImports: SyntaxFormatRule {
163148
/// statements do not appear at the top of the file.
164149
private func checkGrouping<C: Collection>(_ lines: C) where C.Element == Line {
165150
var declGroup = false
166-
var implementationOnlyGroup = false
167151
var testableGroup = false
168152
var codeGroup = false
169153

@@ -173,8 +157,6 @@ public final class OrderedImports: SyntaxFormatRule {
173157
switch lineType {
174158
case .declImport:
175159
declGroup = true
176-
case .implementationOnlyImport:
177-
implementationOnlyGroup = true
178160
case .testableImport:
179161
testableGroup = true
180162
case .codeBlock:
@@ -184,28 +166,17 @@ public final class OrderedImports: SyntaxFormatRule {
184166

185167
if codeGroup {
186168
switch lineType {
187-
case .regularImport, .declImport, .implementationOnlyImport, .testableImport:
169+
case .regularImport, .declImport, .testableImport:
188170
diagnose(.placeAtTopOfFile, on: line.firstToken)
189171
default: ()
190172
}
191173
}
192174

193175
if testableGroup {
194-
switch lineType {
195-
case .regularImport, .declImport, .implementationOnlyImport:
196-
diagnose(
197-
.groupImports(before: lineType, after: LineType.testableImport),
198-
on: line.firstToken
199-
)
200-
default: ()
201-
}
202-
}
203-
204-
if implementationOnlyGroup {
205176
switch lineType {
206177
case .regularImport, .declImport:
207178
diagnose(
208-
.groupImports(before: lineType, after: LineType.implementationOnlyImport),
179+
.groupImports(before: lineType, after: LineType.testableImport),
209180
on: line.firstToken
210181
)
211182
default: ()
@@ -237,7 +208,7 @@ public final class OrderedImports: SyntaxFormatRule {
237208

238209
for line in imports {
239210
switch line.type {
240-
case .regularImport, .declImport, .implementationOnlyImport, .testableImport:
211+
case .regularImport, .declImport, .testableImport:
241212
let fullyQualifiedImport = line.fullyQualifiedImport
242213
// Check for duplicate imports and potentially remove them.
243214
if let previousMatchingImportIndex = visitedImports[fullyQualifiedImport] {
@@ -419,7 +390,6 @@ fileprivate func convertToCodeBlockItems(lines: [Line]) -> [CodeBlockItemSyntax]
419390
public enum LineType: CustomStringConvertible {
420391
case regularImport
421392
case declImport
422-
case implementationOnlyImport
423393
case testableImport
424394
case codeBlock
425395
case comment
@@ -431,8 +401,6 @@ public enum LineType: CustomStringConvertible {
431401
return "regular"
432402
case .declImport:
433403
return "declaration"
434-
case .implementationOnlyImport:
435-
return "implementationOnly"
436404
case .testableImport:
437405
return "testable"
438406
case .codeBlock:
@@ -547,16 +515,12 @@ fileprivate class Line {
547515

548516
/// Returns a `LineType` the represents the type of import from the given import decl.
549517
private func importType(of importDecl: ImportDeclSyntax) -> LineType {
550-
551-
let importIdentifierTypes = importDecl.attributes.compactMap { $0.as(AttributeSyntax.self)?.attributeName }
552-
let importAttributeNames = importIdentifierTypes.compactMap { $0.as(IdentifierTypeSyntax.self)?.name.text }
553-
554-
if importAttributeNames.contains("testable") {
518+
if let attr = importDecl.attributes.firstToken(viewMode: .sourceAccurate),
519+
attr.tokenKind == .atSign,
520+
attr.nextToken(viewMode: .sourceAccurate)?.text == "testable"
521+
{
555522
return .testableImport
556523
}
557-
if importAttributeNames.contains("_implementationOnly") {
558-
return .implementationOnlyImport
559-
}
560524
if importDecl.importKindSpecifier != nil {
561525
return .declImport
562526
}

Tests/SwiftFormatTests/Rules/OrderedImportsTests.swift

Lines changed: 10 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,14 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
2727
import UIKit
2828
2929
@testable import SwiftFormat
30-
🔟import enum Darwin.D.isatty
30+
8️⃣import enum Darwin.D.isatty
3131
// Starts Test
3232
3️⃣@testable import MyModuleUnderTest
3333
// Starts Ind
34-
2️⃣8️⃣import func Darwin.C.isatty
35-
36-
// Starts ImplementationOnly
37-
9️⃣@_implementationOnly import InternalModule
34+
2️⃣7️⃣import func Darwin.C.isatty
3835
3936
let a = 3
40-
4️⃣5️⃣6️⃣7️⃣import SwiftSyntax
37+
4️⃣5️⃣6️⃣import SwiftSyntax
4138
""",
4239
expected: """
4340
// Starts Imports
@@ -51,9 +48,6 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
5148
import func Darwin.C.isatty
5249
import enum Darwin.D.isatty
5350
54-
// Starts ImplementationOnly
55-
@_implementationOnly import InternalModule
56-
5751
// Starts Test
5852
@testable import MyModuleUnderTest
5953
@testable import SwiftFormat
@@ -66,11 +60,9 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
6660
FindingSpec("3️⃣", message: "sort import statements lexicographically"),
6761
FindingSpec("4️⃣", message: "place imports at the top of the file"),
6862
FindingSpec("5️⃣", message: "place regular imports before testable imports"),
69-
FindingSpec("6️⃣", message: "place regular imports before implementationOnly imports"),
70-
FindingSpec("7️⃣", message: "place regular imports before declaration imports"),
71-
FindingSpec("8️⃣", message: "sort import statements lexicographically"),
72-
FindingSpec("9️⃣", message: "place implementationOnly imports before testable imports"),
73-
FindingSpec("🔟", message: "place declaration imports before testable imports"),
63+
FindingSpec("6️⃣", message: "place regular imports before declaration imports"),
64+
FindingSpec("7️⃣", message: "sort import statements lexicographically"),
65+
FindingSpec("8️⃣", message: "place declaration imports before testable imports"),
7466
]
7567
)
7668
}
@@ -104,50 +96,6 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
10496
)
10597
}
10698

107-
func testImportsWithAttributes() {
108-
assertFormatting(
109-
OrderedImports.self,
110-
input: """
111-
import Foundation
112-
1️⃣@preconcurrency import AVFoundation
113-
114-
@preconcurrency @_implementationOnly import InternalModuleC
115-
116-
2️⃣@_implementationOnly import InternalModuleA
117-
118-
3️⃣import Core
119-
120-
@testable @preconcurrency import TestServiceB
121-
4️⃣@preconcurrency @testable import TestServiceA
122-
123-
5️⃣@_implementationOnly @preconcurrency import InternalModuleB
124-
125-
let a = 3
126-
""",
127-
expected: """
128-
@preconcurrency import AVFoundation
129-
import Core
130-
import Foundation
131-
132-
@_implementationOnly import InternalModuleA
133-
@_implementationOnly @preconcurrency import InternalModuleB
134-
@preconcurrency @_implementationOnly import InternalModuleC
135-
136-
@preconcurrency @testable import TestServiceA
137-
@testable @preconcurrency import TestServiceB
138-
139-
let a = 3
140-
""",
141-
findings: [
142-
FindingSpec("1️⃣", message: "sort import statements lexicographically"),
143-
FindingSpec("2️⃣", message: "sort import statements lexicographically"),
144-
FindingSpec("3️⃣", message: "place regular imports before implementationOnly imports"),
145-
FindingSpec("4️⃣", message: "sort import statements lexicographically"),
146-
FindingSpec("5️⃣", message: "place implementationOnly imports before testable imports"),
147-
]
148-
)
149-
}
150-
15199
func testImportsOrderWithDocComment() {
152100
assertFormatting(
153101
OrderedImports.self,
@@ -198,9 +146,6 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
198146
199147
import func Darwin.C.isatty
200148
201-
@_implementationOnly import InternalModuleA
202-
@preconcurrency @_implementationOnly import InternalModuleB
203-
204149
@testable import MyModuleUnderTest
205150
""",
206151
expected: """
@@ -211,9 +156,6 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
211156
212157
import func Darwin.C.isatty
213158
214-
@_implementationOnly import InternalModuleA
215-
@preconcurrency @_implementationOnly import InternalModuleB
216-
217159
@testable import MyModuleUnderTest
218160
""",
219161
findings: []
@@ -382,7 +324,7 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
382324
@testable import testZ // trailing comment about testZ
383325
3️⃣@testable import testC
384326
// swift-format-ignore
385-
@_implementationOnly import testB
327+
@testable import testB
386328
// Comment about Bar
387329
import enum Bar
388330
@@ -408,7 +350,7 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
408350
@testable import testZ // trailing comment about testZ
409351
410352
// swift-format-ignore
411-
@_implementationOnly import testB
353+
@testable import testB
412354
413355
// Comment about Bar
414356
import enum Bar
@@ -571,7 +513,7 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
571513
input: """
572514
// exported import of bar
573515
@_exported import bar
574-
@preconcurrency import bar
516+
@_implementationOnly import bar
575517
import bar
576518
import foo
577519
// second import of foo
@@ -589,7 +531,7 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
589531
expected: """
590532
// exported import of bar
591533
@_exported import bar
592-
@preconcurrency import bar
534+
@_implementationOnly import bar
593535
import bar
594536
// second import of foo
595537
import foo

api-breakages.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ API breakage: func Configuration.MultilineStringReflowBehavior.hash(into:) has b
1818
API breakage: func Configuration.MultilineStringReflowBehavior.encode(to:) has been removed
1919
API breakage: var Configuration.MultilineStringReflowBehavior.hashValue has been removed
2020
API breakage: constructor Configuration.MultilineStringReflowBehavior.init(from:) has been removed
21-
API breakage: enumelement LineType.implementationOnlyImport has been added as a new enum case

0 commit comments

Comments
 (0)