Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
u-abyss committed Oct 12, 2024
1 parent df72a00 commit 5544376
Showing 1 changed file with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,44 @@ struct VoidFunctionInTernaryConditionRule: Rule {
func exampleNestedIfExpr() -> String {
test()
if true {
isTrue ? defaultValue() : defaultValue()
return isTrue ? defaultValue() : defaultValue()
} else {
"Default"
return "Default"
}
}
"""),
Example("""
func collectionView() -> CGSize {
switch indexPath.section {
case 0: isEditing ? CGSize(width: 150, height: 20) : CGSize(width: 100, height: 20)
case 0: return isEditing ? CGSize(width: 150, height: 20) : CGSize(width: 100, height: 20)
default: .zero
}
}
"""),
Example("""
func exampleFunction() -> String {
if true {
switch value {
case 1:
if flag {
return isTrue ? "1" : "2"
} else {
return "3"
}
case 2:
if true {
return "4"
} else {
return "5"
}
default:
return "6"
}
} else {
return "7"
}
}
""")
],
triggeringExamples: [
Example("success ↓? askQuestion() : exit()"),
Expand Down Expand Up @@ -345,26 +369,6 @@ private extension CodeBlockItemSyntax {
return parent.children(viewMode: .sourceAccurate).count == 1
}

func getFunctionDeclSyntax(parent: CodeBlockItemListSyntax) -> FunctionDeclSyntax? {
let targetSyntax = parent.parent?.parent
if let targetSyntax = targetSyntax?.as(FunctionDeclSyntax.self) {
return targetSyntax
}
if let ifExprSyntax = targetSyntax?.as(IfExprSyntax.self) {
guard let codeBlockItemListSyntax = ifExprSyntax.parent?.parent?.parent?.as(CodeBlockItemListSyntax.self) else {
return nil
}
return getFunctionDeclSyntax(parent: codeBlockItemListSyntax)
}
if let switchExpr = targetSyntax?.parent?.as(SwitchExprSyntax.self) {
guard let codeBlockItemListSyntax = switchExpr.parent?.parent?.parent?.as(CodeBlockItemListSyntax.self) else {
return nil
}
return getFunctionDeclSyntax(parent: codeBlockItemListSyntax)
}
return nil
}

// Recursively traverse the codeBlockItem to determine if it is a FunctionDeclSyntax
func getFunctionDeclSyntax(codeBlockItem: CodeBlockItemSyntax) -> FunctionDeclSyntax? {
let targetSyntax = codeBlockItem.parent?.parent?.parent?.parent
Expand Down

0 comments on commit 5544376

Please sign in to comment.