@@ -26,59 +26,56 @@ public final class OmitReturns: SyntaxFormatRule {
26
26
let decl = super. visit ( node)
27
27
28
28
// func <name>() -> <Type> { return ... }
29
- if var funcDecl = decl. as ( FunctionDeclSyntax . self) ,
30
- let body = funcDecl. body,
31
- let `return` = containsSingleReturn ( body. statements) {
32
- funcDecl. body? . statements = unwrapReturnStmt ( `return`)
33
- diagnose ( . omitReturnStatement, on: `return`, severity: . refactoring)
34
- return DeclSyntax ( funcDecl)
29
+ guard var funcDecl = decl. as ( FunctionDeclSyntax . self) ,
30
+ let body = funcDecl. body,
31
+ let returnStmt = containsSingleReturn ( body. statements) else {
32
+ return decl
35
33
}
36
34
37
- return decl
35
+ funcDecl. body? . statements = unwrapReturnStmt ( returnStmt)
36
+ diagnose ( . omitReturnStatement, on: returnStmt, severity: . refactoring)
37
+ return DeclSyntax ( funcDecl)
38
38
}
39
39
40
40
public override func visit( _ node: SubscriptDeclSyntax ) -> DeclSyntax {
41
41
let decl = super. visit ( node)
42
42
43
- guard var `subscript` = decl. as ( SubscriptDeclSyntax . self) else {
43
+ guard var subscriptDecl = decl. as ( SubscriptDeclSyntax . self) ,
44
+ let accessorBlock = subscriptDecl. accessorBlock,
45
+ // We are assuming valid Swift code here where only
46
+ // one `get { ... }` is allowed.
47
+ let transformed = transformAccessorBlock ( accessorBlock) else {
44
48
return decl
45
49
}
46
50
47
- if let accessorBlock = `subscript`. accessorBlock,
48
- // We are assuming valid Swift code here where only
49
- // one `get { ... }` is allowed.
50
- let transformed = transformAccessorBlock ( accessorBlock) {
51
- `subscript`. accessorBlock = transformed
52
- return DeclSyntax ( `subscript`)
53
- }
54
-
55
- return decl
51
+ subscriptDecl. accessorBlock = transformed
52
+ return DeclSyntax ( subscriptDecl)
56
53
}
57
54
58
55
public override func visit( _ node: PatternBindingSyntax ) -> PatternBindingSyntax {
59
56
var binding = node
60
57
61
- if let accessorBlock = binding. accessorBlock,
62
- let transformed = transformAccessorBlock ( accessorBlock) {
63
- binding. accessorBlock = transformed
64
- return binding
58
+ guard let accessorBlock = binding. accessorBlock,
59
+ let transformed = transformAccessorBlock ( accessorBlock) else {
60
+ return node
65
61
}
66
62
67
- return node
63
+ binding. accessorBlock = transformed
64
+ return binding
68
65
}
69
66
70
67
public override func visit( _ node: ClosureExprSyntax ) -> ExprSyntax {
71
68
let expr = super. visit ( node)
72
69
73
70
// test { return ... }
74
- if var closure = expr. as ( ClosureExprSyntax . self) ,
75
- let `return` = containsSingleReturn ( closure. statements) {
76
- closure. statements = unwrapReturnStmt ( `return`)
77
- diagnose ( . omitReturnStatement, on: `return`, severity: . refactoring)
78
- return ExprSyntax ( closure)
71
+ guard var closureExpr = expr. as ( ClosureExprSyntax . self) ,
72
+ let returnStmt = containsSingleReturn ( closureExpr. statements) else {
73
+ return expr
79
74
}
80
75
81
- return expr
76
+ closureExpr. statements = unwrapReturnStmt ( returnStmt)
77
+ diagnose ( . omitReturnStatement, on: returnStmt, severity: . refactoring)
78
+ return ExprSyntax ( closureExpr)
82
79
}
83
80
84
81
private func transformAccessorBlock( _ accessorBlock: AccessorBlockSyntax ) -> AccessorBlockSyntax ? {
@@ -93,7 +90,7 @@ public final class OmitReturns: SyntaxFormatRule {
93
90
}
94
91
95
92
guard let body = getter. body,
96
- let `return` = containsSingleReturn ( body. statements) else {
93
+ let returnStmt = containsSingleReturn ( body. statements) else {
97
94
return nil
98
95
}
99
96
@@ -103,50 +100,44 @@ public final class OmitReturns: SyntaxFormatRule {
103
100
return nil
104
101
}
105
102
106
- getter. body? . statements = unwrapReturnStmt ( `return` )
103
+ getter. body? . statements = unwrapReturnStmt ( returnStmt )
107
104
108
- diagnose ( . omitReturnStatement, on: `return` , severity: . refactoring)
105
+ diagnose ( . omitReturnStatement, on: returnStmt , severity: . refactoring)
109
106
110
- return . init(
111
- leadingTrivia: accessorBlock. leadingTrivia,
112
- leftBrace: accessorBlock. leftBrace,
113
- accessors: . accessors( accessors. with ( \. [ getterAt] , getter) ) ,
114
- rightBrace: accessorBlock. rightBrace,
115
- trailingTrivia: accessorBlock. trailingTrivia)
107
+ var newBlock = accessorBlock
108
+ newBlock. accessors = . accessors( accessors. with ( \. [ getterAt] , getter) )
109
+ return newBlock
116
110
117
111
case . getter( let getter) :
118
- guard let `return` = containsSingleReturn ( getter) else {
112
+ guard let returnStmt = containsSingleReturn ( getter) else {
119
113
return nil
120
114
}
121
115
122
- diagnose ( . omitReturnStatement, on: `return` , severity: . refactoring)
116
+ diagnose ( . omitReturnStatement, on: returnStmt , severity: . refactoring)
123
117
124
- return . init(
125
- leadingTrivia: accessorBlock. leadingTrivia,
126
- leftBrace: accessorBlock. leftBrace,
127
- accessors: . getter( unwrapReturnStmt ( `return`) ) ,
128
- rightBrace: accessorBlock. rightBrace,
129
- trailingTrivia: accessorBlock. trailingTrivia)
118
+ var newBlock = accessorBlock
119
+ newBlock. accessors = . getter( unwrapReturnStmt ( returnStmt) )
120
+ return newBlock
130
121
}
131
122
}
132
123
133
124
private func containsSingleReturn( _ body: CodeBlockItemListSyntax ) -> ReturnStmtSyntax ? {
134
- if let element = body. firstAndOnly? . as ( CodeBlockItemSyntax . self) ,
135
- let ret = element. item. as ( ReturnStmtSyntax . self) ,
136
- !ret . children ( viewMode : . all ) . isEmpty , ret . expression != nil {
137
- return ret
125
+ guard let element = body. firstAndOnly? . as ( CodeBlockItemSyntax . self) ,
126
+ let returnStmt = element. item. as ( ReturnStmtSyntax . self) else
127
+ {
128
+ return nil
138
129
}
139
130
140
- return nil
131
+ return !returnStmt . children ( viewMode : . all ) . isEmpty && returnStmt . expression != nil ? returnStmt : nil
141
132
}
142
133
143
- private func unwrapReturnStmt( _ `return` : ReturnStmtSyntax ) -> CodeBlockItemListSyntax {
134
+ private func unwrapReturnStmt( _ returnStmt : ReturnStmtSyntax ) -> CodeBlockItemListSyntax {
144
135
CodeBlockItemListSyntax ( [
145
136
CodeBlockItemSyntax (
146
- leadingTrivia: `return` . leadingTrivia,
147
- item: . expr( `return` . expression!) ,
137
+ leadingTrivia: returnStmt . leadingTrivia,
138
+ item: . expr( returnStmt . expression!) ,
148
139
semicolon: nil ,
149
- trailingTrivia: `return` . trailingTrivia)
140
+ trailingTrivia: returnStmt . trailingTrivia)
150
141
] )
151
142
}
152
143
}
0 commit comments