@@ -16,19 +16,19 @@ import SyntaxSupport
16
16
import Utils
17
17
18
18
let basicFormatFile = SourceFileSyntax {
19
- ImportDeclSyntax (
20
- leadingTrivia : . docLineComment ( generateCopyrightHeader ( for: " generate-swiftbasicformat " ) ) ,
21
- path : [ AccessPathComponentSyntax ( name : " SwiftSyntax " ) ]
22
- )
19
+ DeclSyntax ( """
20
+ \( raw : generateCopyrightHeader ( for: " generate-swiftbasicformat " ) )
21
+ import SwiftSyntax
22
+ """ )
23
23
24
- ClassDeclSyntax ( " open class BasicFormat: SyntaxRewriter " ) {
25
- VariableDeclSyntax ( " public var indentationLevel: Int = 0 " )
26
- VariableDeclSyntax ( " open var indentation: TriviaPiece { .spaces(indentationLevel * 4) } " )
27
- VariableDeclSyntax ( " public var indentedNewline: Trivia { Trivia(pieces: [.newlines(1), indentation]) } " )
28
- VariableDeclSyntax ( " private var lastRewrittenToken: TokenSyntax? " )
29
- VariableDeclSyntax ( " private var putNextTokenOnNewLine: Bool = false " )
24
+ try ! ClassDeclSyntax ( " open class BasicFormat: SyntaxRewriter " ) {
25
+ DeclSyntax ( " public var indentationLevel: Int = 0 " )
26
+ DeclSyntax ( " open var indentation: TriviaPiece { .spaces(indentationLevel * 4) } " )
27
+ DeclSyntax ( " public var indentedNewline: Trivia { Trivia(pieces: [.newlines(1), indentation]) } " )
28
+ DeclSyntax ( " private var lastRewrittenToken: TokenSyntax? " )
29
+ DeclSyntax ( " private var putNextTokenOnNewLine: Bool = false " )
30
30
31
- FunctionDeclSyntax ( """
31
+ DeclSyntax ( """
32
32
open override func visitPre(_ node: Syntax) {
33
33
if let keyPath = getKeyPath(node), shouldIndent(keyPath) {
34
34
indentationLevel += 1
@@ -39,7 +39,7 @@ let basicFormatFile = SourceFileSyntax {
39
39
}
40
40
"""
41
41
)
42
- FunctionDeclSyntax ( """
42
+ DeclSyntax ( """
43
43
open override func visitPost(_ node: Syntax) {
44
44
if let keyPath = getKeyPath(node), shouldIndent(keyPath) {
45
45
indentationLevel -= 1
@@ -48,7 +48,7 @@ let basicFormatFile = SourceFileSyntax {
48
48
"""
49
49
)
50
50
51
- FunctionDeclSyntax ( """
51
+ DeclSyntax ( """
52
52
open override func visit(_ node: TokenSyntax) -> TokenSyntax {
53
53
var leadingTrivia = node.leadingTrivia
54
54
var trailingTrivia = node.trailingTrivia
@@ -76,53 +76,53 @@ let basicFormatFile = SourceFileSyntax {
76
76
"""
77
77
)
78
78
79
- FunctionDeclSyntax ( " open func shouldIndent(_ keyPath: AnyKeyPath) -> Bool " ) {
80
- SwitchStmtSyntax ( expression : ExprSyntax ( " keyPath " ) ) {
79
+ try ! FunctionDeclSyntax ( " open func shouldIndent(_ keyPath: AnyKeyPath) -> Bool " ) {
80
+ try ! SwitchStmtSyntax ( " switch keyPath" ) {
81
81
for node in SYNTAX_NODES where !node. isBase {
82
82
for child in node. children where child. isIndented {
83
83
SwitchCaseSyntax ( " case \\ \( raw: node. type. syntaxBaseName) . \( raw: child. swiftName) : " ) {
84
- ReturnStmtSyntax ( " return true " )
84
+ StmtSyntax ( " return true " )
85
85
}
86
86
}
87
87
}
88
88
SwitchCaseSyntax ( " default: " ) {
89
- ReturnStmtSyntax ( " return false " )
89
+ StmtSyntax ( " return false " )
90
90
}
91
91
}
92
92
}
93
93
94
- FunctionDeclSyntax ( " open func requiresLeadingNewline(_ keyPath: AnyKeyPath) -> Bool " ) {
95
- SwitchStmtSyntax ( expression : ExprSyntax ( " keyPath " ) ) {
94
+ try ! FunctionDeclSyntax ( " open func requiresLeadingNewline(_ keyPath: AnyKeyPath) -> Bool " ) {
95
+ try ! SwitchStmtSyntax ( " switch keyPath" ) {
96
96
for node in SYNTAX_NODES where !node. isBase {
97
97
for child in node. children where child. requiresLeadingNewline {
98
98
SwitchCaseSyntax ( " case \\ \( raw: node. type. syntaxBaseName) . \( raw: child. swiftName) : " ) {
99
- ReturnStmtSyntax ( " return true " )
99
+ StmtSyntax ( " return true " )
100
100
}
101
101
}
102
102
}
103
103
SwitchCaseSyntax ( " default: " ) {
104
- ReturnStmtSyntax ( " return putNextTokenOnNewLine " )
104
+ StmtSyntax ( " return putNextTokenOnNewLine " )
105
105
}
106
106
}
107
107
}
108
108
109
- FunctionDeclSyntax ( " open func childrenSeparatedByNewline(_ node: Syntax) -> Bool " ) {
110
- SwitchStmtSyntax ( expression : ExprSyntax ( " node.as(SyntaxEnum.self) " ) ) {
109
+ try ! FunctionDeclSyntax ( " open func childrenSeparatedByNewline(_ node: Syntax) -> Bool " ) {
110
+ try ! SwitchStmtSyntax ( " switch node.as(SyntaxEnum.self)" ) {
111
111
for node in SYNTAX_NODES where !node. isBase {
112
112
if node. elementsSeparatedByNewline {
113
113
SwitchCaseSyntax ( " case . \( raw: node. swiftSyntaxKind) : " ) {
114
- ReturnStmtSyntax ( " return true " )
114
+ StmtSyntax ( " return true " )
115
115
}
116
116
}
117
117
}
118
118
SwitchCaseSyntax ( " default: " ) {
119
- ReturnStmtSyntax ( " return false " )
119
+ StmtSyntax ( " return false " )
120
120
}
121
121
}
122
122
}
123
123
124
- FunctionDeclSyntax ( " open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool " ) {
125
- SwitchStmtSyntax ( """
124
+ try ! FunctionDeclSyntax ( " open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool " ) {
125
+ StmtSyntax ( """
126
126
switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) {
127
127
case (.leftParen, .binaryOperator): // Ensures there is no space in @available(*, deprecated)
128
128
return false
@@ -131,27 +131,27 @@ let basicFormatFile = SourceFileSyntax {
131
131
}
132
132
""" )
133
133
134
- SwitchStmtSyntax ( expression : ExprSyntax ( " token.tokenKind " ) ) {
134
+ try ! SwitchStmtSyntax ( " switch token.tokenKind" ) {
135
135
for token in SYNTAX_TOKENS {
136
136
if token. requiresLeadingSpace {
137
137
SwitchCaseSyntax ( " case . \( raw: token. swiftKind) : " ) {
138
- ReturnStmtSyntax ( " return true " )
138
+ StmtSyntax ( " return true " )
139
139
}
140
140
}
141
141
}
142
142
for keyword in KEYWORDS where keyword. requiresLeadingSpace {
143
143
SwitchCaseSyntax ( " case .keyword(. \( raw: keyword. escapedName) ): " ) {
144
- ReturnStmtSyntax ( " return true " )
144
+ StmtSyntax ( " return true " )
145
145
}
146
146
}
147
147
SwitchCaseSyntax ( " default: " ) {
148
- ReturnStmtSyntax ( " return false " )
148
+ StmtSyntax ( " return false " )
149
149
}
150
150
}
151
151
}
152
152
153
- FunctionDeclSyntax ( " open func requiresTrailingSpace(_ token: TokenSyntax) -> Bool " ) {
154
- SwitchStmtSyntax ( """
153
+ try ! FunctionDeclSyntax ( " open func requiresTrailingSpace(_ token: TokenSyntax) -> Bool " ) {
154
+ StmtSyntax ( """
155
155
switch (token.tokenKind, token.parent?.kind) {
156
156
case (.colon, .dictionaryExpr): // Ensures there is not space in `[:]`
157
157
return false
@@ -163,7 +163,7 @@ let basicFormatFile = SourceFileSyntax {
163
163
}
164
164
""" )
165
165
166
- SwitchStmtSyntax ( """
166
+ StmtSyntax ( """
167
167
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
168
168
case (.keyword(.as), .exclamationMark), // Ensures there is not space in `as!`
169
169
(.keyword(.as), .postfixQuestionMark), // Ensures there is not space in `as?`
@@ -183,26 +183,26 @@ let basicFormatFile = SourceFileSyntax {
183
183
}
184
184
""" )
185
185
186
- SwitchStmtSyntax ( expression : ExprSyntax ( " token.tokenKind " ) ) {
186
+ try ! SwitchStmtSyntax ( " switch token.tokenKind" ) {
187
187
for token in SYNTAX_TOKENS {
188
188
if token. requiresTrailingSpace {
189
189
SwitchCaseSyntax ( " case . \( raw: token. swiftKind) : " ) {
190
- ReturnStmtSyntax ( " return true " )
190
+ StmtSyntax ( " return true " )
191
191
}
192
192
}
193
193
}
194
194
for keyword in KEYWORDS where keyword. requiresTrailingSpace {
195
195
SwitchCaseSyntax ( " case .keyword(. \( raw: keyword. escapedName) ): " ) {
196
- ReturnStmtSyntax ( " return true " )
196
+ StmtSyntax ( " return true " )
197
197
}
198
198
}
199
199
SwitchCaseSyntax ( " default: " ) {
200
- ReturnStmtSyntax ( " return false " )
200
+ StmtSyntax ( " return false " )
201
201
}
202
202
}
203
203
}
204
204
205
- FunctionDeclSyntax ( """
205
+ DeclSyntax ( """
206
206
private func getKeyPath(_ node: Syntax) -> AnyKeyPath? {
207
207
guard let parent = node.parent else {
208
208
return nil
0 commit comments