Skip to content

Commit

Permalink
Remove redundant Void from protocol definitions (#1824)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel-jimenez-0529 authored and nicklockwood committed Aug 24, 2024
1 parent bc39514 commit 16af7f5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
18 changes: 15 additions & 3 deletions Sources/Rules/RedundantVoidReturnType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ public extension FormatRule {
return
}

guard formatter.next(.nonSpaceOrCommentOrLinebreak, after: endIndex) == .startOfScope("{")
else { return }
guard let nextToken = formatter.next(.nonSpaceOrCommentOrLinebreak, after: endIndex) else { return }

let isInProtocol = nextToken == .endOfScope("}") || (nextToken.isKeywordOrAttribute && nextToken != .keyword("in"))

// After a `Void` we could see the start of a function's body, or if the function is inside a protocol declaration
// we can find a keyword related to other declarations or the end scope of the protocol definition.
guard nextToken == .startOfScope("{") || isInProtocol else { return }

guard let prevIndex = formatter.index(of: .endOfScope(")"), before: i),
let parenIndex = formatter.index(of: .startOfScope("("), before: prevIndex),
Expand All @@ -40,7 +45,14 @@ public extension FormatRule {
else {
return
}
formatter.removeTokens(in: i ..< formatter.index(of: .nonSpace, after: endIndex)!)

let startRemoveIndex: Int
if isInProtocol, formatter.token(at: i - 1)?.isSpace == true {
startRemoveIndex = i - 1
} else {
startRemoveIndex = i
}
formatter.removeTokens(in: startRemoveIndex ..< formatter.index(of: .nonSpace, after: endIndex)!)
}
}
}
2 changes: 1 addition & 1 deletion Tests/Rules/BlankLinesBetweenScopesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class BlankLinesBetweenScopesTests: XCTestCase {
}

func testNoBlankLineBetweenFunctionsInProtocol() {
let input = "protocol Foo {\n func bar() -> Void\n func baz() -> Int\n}"
let input = "protocol Foo {\n func bar()\n func baz() -> Int\n}"
testFormatting(for: input, rule: .blankLinesBetweenScopes)
}

Expand Down
21 changes: 21 additions & 0 deletions Tests/Rules/RedundantVoidReturnTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,25 @@ class RedundantVoidReturnTypeTests: XCTestCase {
let options = FormatOptions(closureVoidReturn: .preserve)
testFormatting(for: input, rule: .redundantVoidReturnType, options: options)
}

func testRemoveRedundantVoidInProtocolDeclaration() {
let input = """
protocol Foo {
func foo() -> Void
func bar() -> ()
var baz: Int { get }
func bazz() -> ( )
}
"""

let output = """
protocol Foo {
func foo()
func bar()
var baz: Int { get }
func bazz()
}
"""
testFormatting(for: input, [output], rules: [.void, .redundantVoidReturnType])
}
}

0 comments on commit 16af7f5

Please sign in to comment.