13
13
import SwiftSyntax
14
14
15
15
/// 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.
19
19
///
20
20
/// Lint: If an import appears anywhere other than the beginning of the file it resides in,
21
21
/// not lexicographically ordered, or not in the appropriate import group, a lint error is
@@ -34,7 +34,6 @@ public final class OrderedImports: SyntaxFormatRule {
34
34
35
35
var regularImports : [ Line ] = [ ]
36
36
var declImports : [ Line ] = [ ]
37
- var implementationOnlyImports : [ Line ] = [ ]
38
37
var testableImports : [ Line ] = [ ]
39
38
var codeBlocks : [ Line ] = [ ]
40
39
var fileHeader : [ Line ] = [ ]
@@ -53,23 +52,14 @@ public final class OrderedImports: SyntaxFormatRule {
53
52
54
53
regularImports = formatImports ( regularImports)
55
54
declImports = formatImports ( declImports)
56
- implementationOnlyImports = formatImports ( implementationOnlyImports)
57
55
testableImports = formatImports ( testableImports)
58
56
formatCodeblocks ( & codeBlocks)
59
57
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)
68
59
formattedLines. append ( contentsOf: joined)
69
60
70
61
regularImports = [ ]
71
62
declImports = [ ]
72
- implementationOnlyImports = [ ]
73
63
testableImports = [ ]
74
64
codeBlocks = [ ]
75
65
fileHeader = [ ]
@@ -125,11 +115,6 @@ public final class OrderedImports: SyntaxFormatRule {
125
115
regularImports. append ( line)
126
116
commentBuffer = [ ]
127
117
128
- case . implementationOnlyImport:
129
- implementationOnlyImports. append ( contentsOf: commentBuffer)
130
- implementationOnlyImports. append ( line)
131
- commentBuffer = [ ]
132
-
133
118
case . testableImport:
134
119
testableImports. append ( contentsOf: commentBuffer)
135
120
testableImports. append ( line)
@@ -163,7 +148,6 @@ public final class OrderedImports: SyntaxFormatRule {
163
148
/// statements do not appear at the top of the file.
164
149
private func checkGrouping< C: Collection > ( _ lines: C ) where C. Element == Line {
165
150
var declGroup = false
166
- var implementationOnlyGroup = false
167
151
var testableGroup = false
168
152
var codeGroup = false
169
153
@@ -173,8 +157,6 @@ public final class OrderedImports: SyntaxFormatRule {
173
157
switch lineType {
174
158
case . declImport:
175
159
declGroup = true
176
- case . implementationOnlyImport:
177
- implementationOnlyGroup = true
178
160
case . testableImport:
179
161
testableGroup = true
180
162
case . codeBlock:
@@ -184,28 +166,17 @@ public final class OrderedImports: SyntaxFormatRule {
184
166
185
167
if codeGroup {
186
168
switch lineType {
187
- case . regularImport, . declImport, . implementationOnlyImport , . testableImport:
169
+ case . regularImport, . declImport, . testableImport:
188
170
diagnose ( . placeAtTopOfFile, on: line. firstToken)
189
171
default : ( )
190
172
}
191
173
}
192
174
193
175
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 {
205
176
switch lineType {
206
177
case . regularImport, . declImport:
207
178
diagnose (
208
- . groupImports( before: lineType, after: LineType . implementationOnlyImport ) ,
179
+ . groupImports( before: lineType, after: LineType . testableImport ) ,
209
180
on: line. firstToken
210
181
)
211
182
default : ( )
@@ -237,7 +208,7 @@ public final class OrderedImports: SyntaxFormatRule {
237
208
238
209
for line in imports {
239
210
switch line. type {
240
- case . regularImport, . declImport, . implementationOnlyImport , . testableImport:
211
+ case . regularImport, . declImport, . testableImport:
241
212
let fullyQualifiedImport = line. fullyQualifiedImport
242
213
// Check for duplicate imports and potentially remove them.
243
214
if let previousMatchingImportIndex = visitedImports [ fullyQualifiedImport] {
@@ -419,7 +390,6 @@ fileprivate func convertToCodeBlockItems(lines: [Line]) -> [CodeBlockItemSyntax]
419
390
public enum LineType : CustomStringConvertible {
420
391
case regularImport
421
392
case declImport
422
- case implementationOnlyImport
423
393
case testableImport
424
394
case codeBlock
425
395
case comment
@@ -431,8 +401,6 @@ public enum LineType: CustomStringConvertible {
431
401
return " regular "
432
402
case . declImport:
433
403
return " declaration "
434
- case . implementationOnlyImport:
435
- return " implementationOnly "
436
404
case . testableImport:
437
405
return " testable "
438
406
case . codeBlock:
@@ -547,16 +515,12 @@ fileprivate class Line {
547
515
548
516
/// Returns a `LineType` the represents the type of import from the given import decl.
549
517
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
+ {
555
522
return . testableImport
556
523
}
557
- if importAttributeNames. contains ( " _implementationOnly " ) {
558
- return . implementationOnlyImport
559
- }
560
524
if importDecl. importKindSpecifier != nil {
561
525
return . declImport
562
526
}
0 commit comments