@@ -38,68 +38,70 @@ public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {
38
38
-> CodeBlockItemListSyntax
39
39
{
40
40
let newCodeBlockItems = codeBlockItems. map { codeBlockItem -> CodeBlockItemSyntax in
41
- switch codeBlockItem. item. as ( SyntaxEnum . self) {
42
- case . ifConfigDecl( let ifConfigDecl) :
43
- // We need to look through `#if/#elseif/#else` blocks because the decls directly inside
44
- // them are still considered file-scope for our purposes.
45
- return codeBlockItem. withItem ( Syntax ( rewrittenIfConfigDecl ( ifConfigDecl) ) )
46
-
47
- case . functionDecl( let functionDecl) :
48
- return codeBlockItem. withItem (
49
- Syntax ( rewrittenDecl (
50
- functionDecl,
51
- modifiers: functionDecl. modifiers,
52
- factory: functionDecl. withModifiers) ) )
53
-
54
- case . variableDecl( let variableDecl) :
55
- return codeBlockItem. withItem (
56
- Syntax ( rewrittenDecl (
57
- variableDecl,
58
- modifiers: variableDecl. modifiers,
59
- factory: variableDecl. withModifiers) ) )
60
-
61
- case . classDecl( let classDecl) :
62
- return codeBlockItem. withItem (
63
- Syntax ( rewrittenDecl (
64
- classDecl,
65
- modifiers: classDecl. modifiers,
66
- factory: classDecl. withModifiers) ) )
67
-
68
- case . structDecl( let structDecl) :
69
- return codeBlockItem. withItem (
70
- Syntax ( rewrittenDecl (
71
- structDecl,
72
- modifiers: structDecl. modifiers,
73
- factory: structDecl. withModifiers) ) )
74
-
75
- case . enumDecl( let enumDecl) :
76
- return codeBlockItem. withItem (
77
- Syntax ( rewrittenDecl (
78
- enumDecl,
79
- modifiers: enumDecl. modifiers,
80
- factory: enumDecl. withModifiers) ) )
81
-
82
- case . protocolDecl( let protocolDecl) :
83
- return codeBlockItem. withItem (
84
- Syntax ( rewrittenDecl (
85
- protocolDecl,
86
- modifiers: protocolDecl. modifiers,
87
- factory: protocolDecl. withModifiers) ) )
88
-
89
- case . typealiasDecl( let typealiasDecl) :
90
- return codeBlockItem. withItem (
91
- Syntax ( rewrittenDecl (
92
- typealiasDecl,
93
- modifiers: typealiasDecl. modifiers,
94
- factory: typealiasDecl. withModifiers) ) )
95
-
41
+ switch codeBlockItem. item {
42
+ case . decl( let decl) :
43
+ return codeBlockItem. withItem ( . decl( rewrittenDecl ( decl) ) )
96
44
default :
97
45
return codeBlockItem
98
46
}
99
47
}
100
48
return CodeBlockItemListSyntax ( newCodeBlockItems)
101
49
}
102
50
51
+ private func rewrittenDecl( _ decl: DeclSyntax ) -> DeclSyntax {
52
+ switch Syntax ( decl) . as ( SyntaxEnum . self) {
53
+ case . ifConfigDecl( let ifConfigDecl) :
54
+ // We need to look through `#if/#elseif/#else` blocks because the decls directly inside
55
+ // them are still considered file-scope for our purposes.
56
+ return DeclSyntax ( rewrittenIfConfigDecl ( ifConfigDecl) )
57
+
58
+ case . functionDecl( let functionDecl) :
59
+ return DeclSyntax ( rewrittenDecl (
60
+ functionDecl,
61
+ modifiers: functionDecl. modifiers,
62
+ factory: functionDecl. withModifiers) )
63
+
64
+ case . variableDecl( let variableDecl) :
65
+ return DeclSyntax ( rewrittenDecl (
66
+ variableDecl,
67
+ modifiers: variableDecl. modifiers,
68
+ factory: variableDecl. withModifiers) )
69
+
70
+ case . classDecl( let classDecl) :
71
+ return DeclSyntax ( rewrittenDecl (
72
+ classDecl,
73
+ modifiers: classDecl. modifiers,
74
+ factory: classDecl. withModifiers) )
75
+
76
+ case . structDecl( let structDecl) :
77
+ return DeclSyntax ( rewrittenDecl (
78
+ structDecl,
79
+ modifiers: structDecl. modifiers,
80
+ factory: structDecl. withModifiers) )
81
+
82
+ case . enumDecl( let enumDecl) :
83
+ return DeclSyntax ( rewrittenDecl (
84
+ enumDecl,
85
+ modifiers: enumDecl. modifiers,
86
+ factory: enumDecl. withModifiers) )
87
+
88
+ case . protocolDecl( let protocolDecl) :
89
+ return DeclSyntax ( rewrittenDecl (
90
+ protocolDecl,
91
+ modifiers: protocolDecl. modifiers,
92
+ factory: protocolDecl. withModifiers) )
93
+
94
+ case . typealiasDecl( let typealiasDecl) :
95
+ return DeclSyntax ( rewrittenDecl (
96
+ typealiasDecl,
97
+ modifiers: typealiasDecl. modifiers,
98
+ factory: typealiasDecl. withModifiers) )
99
+
100
+ default :
101
+ return decl
102
+ }
103
+ }
104
+
103
105
/// Returns a new `IfConfigDeclSyntax` equivalent to the given node, but where any file-scoped
104
106
/// declarations with effective private access have had their formal access level rewritten, if
105
107
/// necessary, to be either `private` or `fileprivate`, as determined by the formatter
@@ -109,9 +111,9 @@ public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {
109
111
/// - Returns: A new `IfConfigDeclSyntax` that has possibly been rewritten.
110
112
private func rewrittenIfConfigDecl( _ ifConfigDecl: IfConfigDeclSyntax ) -> IfConfigDeclSyntax {
111
113
let newClauses = ifConfigDecl. clauses. map { clause -> IfConfigClauseSyntax in
112
- switch clause. elements? . as ( SyntaxEnum . self ) {
113
- case . codeBlockItemList ( let codeBlockItemList) ? :
114
- return clause. withElements ( Syntax ( rewrittenCodeBlockItems ( codeBlockItemList) ) )
114
+ switch clause. elements {
115
+ case . statements ( let codeBlockItemList) ? :
116
+ return clause. withElements ( . statements ( rewrittenCodeBlockItems ( codeBlockItemList) ) )
115
117
default :
116
118
return clause
117
119
}
0 commit comments