Skip to content

Commit fcae9a6

Browse files
committed
add: recursive function to find FunctionDeclSyntax
1 parent 337358f commit fcae9a6

File tree

1 file changed

+52
-9
lines changed

1 file changed

+52
-9
lines changed

Source/SwiftLintBuiltInRules/Rules/Idiomatic/VoidFunctionInTernaryConditionRule.swift

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,20 @@ struct VoidFunctionInTernaryConditionRule: Rule {
168168
return hoge
169169
}
170170
"""),
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+
"""),
171185
]
172186
)
173187
}
@@ -263,23 +277,52 @@ private extension CodeBlockItemSyntax {
263277

264278
return parent.children(viewMode: .sourceAccurate).count == 1
265279
}
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
271291
}
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
274297
}
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,
276309
let expressionStmtSyntax = codeBlockItemListSyntax.last?.item.as(ExpressionStmtSyntax.self) {
277310
return parent.children(viewMode: .sourceAccurate).count == 1 &&
278311
( codeBlockItemListSyntax.count == 1 || expressionStmtSyntax.expression.is(IfExprSyntax.self)) &&
279-
funcDecl.signature.allowsImplicitReturns
312+
result.signature.allowsImplicitReturns
280313
}
281314
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
282323
}
324+
325+
283326

284327
var isSwitchExprImplicitReturn: Bool {
285328
guard let parent = parent?.as(CodeBlockItemListSyntax.self),

0 commit comments

Comments
 (0)