@@ -168,6 +168,20 @@ struct VoidFunctionInTernaryConditionRule: Rule {
168
168
return hoge
169
169
}
170
170
""" ) ,
171
+ Example ( """
172
+ func exampleNestedIfExpr() -> String {
173
+ if true {
174
+ if true {
175
+ isTrue ↓? defaultValue() : defaultValue()
176
+ } else {
177
+ return " False "
178
+ }
179
+ } else {
180
+ return " Default "
181
+ }
182
+ return hoge
183
+ }
184
+ """ ) ,
171
185
]
172
186
)
173
187
}
@@ -263,23 +277,52 @@ private extension CodeBlockItemSyntax {
263
277
264
278
return parent. children ( viewMode: . sourceAccurate) . count == 1
265
279
}
266
-
267
- var isIfExprImplicitReturn : Bool {
268
- guard let parent = parent? . as ( CodeBlockItemListSyntax . self) ,
269
- let ifExprSytax = parent. parent? . parent? . as ( IfExprSyntax . self) else {
270
- return false
280
+
281
+
282
+
283
+ func getFunctionDeclSyntax( parent: CodeBlockItemListSyntax ) -> FunctionDeclSyntax ? {
284
+ let targetSyntax = parent. parent? . parent
285
+ if let targetSyntax = targetSyntax? . as ( FunctionDeclSyntax . self) {
286
+ return targetSyntax
287
+ }
288
+ if let ifExprSyntax = targetSyntax? . as ( IfExprSyntax . self) {
289
+ guard let codeBlockItemListSyntax = ifExprSyntax. parent? . parent? . parent? . as ( CodeBlockItemListSyntax . self) else {
290
+ return nil
271
291
}
272
- guard let funcDecl = ifExprSytax. parent? . parent? . parent? . parent? . parent? . as ( FunctionDeclSyntax . self) else {
273
- return false
292
+ return getFunctionDeclSyntax ( parent: codeBlockItemListSyntax)
293
+ }
294
+ if let swichExprSyntax = targetSyntax? . parent? . as ( SwitchExprSyntax . self) {
295
+ guard let codeBlockItemListSyntax = swichExprSyntax. parent? . parent? . parent? . as ( CodeBlockItemListSyntax . self) else {
296
+ return nil
274
297
}
275
- if let codeBlockItemListSyntax = ifExprSytax. parent? . parent? . parent? . as ( CodeBlockItemListSyntax . self) ,
298
+ return getFunctionDeclSyntax ( parent: codeBlockItemListSyntax)
299
+ }
300
+ return nil
301
+ }
302
+
303
+ // if文もしくはswitch文のとき
304
+ var isIfExprImplicitReturn : Bool {
305
+
306
+ guard let parent = parent? . as ( CodeBlockItemListSyntax . self) else { return false }
307
+ guard let result = getFunctionDeclSyntax ( parent: parent) else { return false }
308
+ if let codeBlockItemListSyntax = result. body? . statements,
276
309
let expressionStmtSyntax = codeBlockItemListSyntax. last? . item. as ( ExpressionStmtSyntax . self) {
277
310
return parent. children ( viewMode: . sourceAccurate) . count == 1 &&
278
311
( codeBlockItemListSyntax. count == 1 || expressionStmtSyntax. expression. is ( IfExprSyntax . self) ) &&
279
- funcDecl . signature. allowsImplicitReturns
312
+ result . signature. allowsImplicitReturns
280
313
}
281
314
return false
315
+ //
316
+ // if let codeBlockItemListSyntax = ifExprSytax.parent?.parent?.parent?.as(CodeBlockItemListSyntax.self),
317
+ // let expressionStmtSyntax = codeBlockItemListSyntax.last?.item.as(ExpressionStmtSyntax.self) {
318
+ // return parent.children(viewMode: .sourceAccurate).count == 1 &&
319
+ // ( codeBlockItemListSyntax.count == 1 || expressionStmtSyntax.expression.is(IfExprSyntax.self)) &&
320
+ // funcDecl.signature.allowsImplicitReturns
321
+ // }
322
+ // return false
282
323
}
324
+
325
+
283
326
284
327
var isSwitchExprImplicitReturn : Bool {
285
328
guard let parent = parent? . as ( CodeBlockItemListSyntax . self) ,
0 commit comments