Swiftformat: isStartOfClosure incorrectly treats some functions as closures

Created on 19 Jun 2020  路  3Comments  路  Source: nicklockwood/SwiftFormat

I'm working on a new rule and have come across an issue with Formatter's isStartOfClosure function.

A simple test case:

func testFuncNotTreatedAsClosure() {
    let formatter = Formatter(tokenize("func test<T: Equatable>(e: T) {}"))
    XCTAssertFalse(formatter.isStartOfClosure(at: formatter.tokens.count - 2)) //fails

    let formatter2 = Formatter(tokenize("func test(e: T) {}"))
    XCTAssertFalse(formatter2.isStartOfClosure(at: formatter2.tokens.count - 2)) //passes
}

It seems to be falling into the default case here: https://github.com/nicklockwood/SwiftFormat/blob/master/Sources/ParsingHelpers.swift#L305-L326

I'm not sure if this is the only case where a function would trigger this bug, but it's the only one I've come across so far.

I am working on fixing this, but thought I may let you know in case you have some ideas of how it would be fixable.

bug fixed in develop

All 3 comments

Actually I think this is a pretty simple fix after checking a bit more. The switch is on tokens[prev], which is the end of the generic scope, >. Adding a case .endOfScope(">"): return false has my test passing now and no regressions in the ParsingHelpersTests

@dcramps your suggested fix seems to work - thanks!

@dcramps fixed in 0.44.15. Thanks again!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PompeiaPaetenari picture PompeiaPaetenari  路  3Comments

zizi4n5 picture zizi4n5  路  3Comments

Cyberbeni picture Cyberbeni  路  4Comments

shripada picture shripada  路  3Comments

mdiep picture mdiep  路  3Comments