realm / SwiftLint

A tool to enforce Swift style and conventions.
https://realm.github.io/SwiftLint
MIT License
18.66k stars 2.22k forks source link

Restrict opening_brace to single-line declarations #1921

Closed lilyball closed 3 years ago

lilyball commented 7 years ago

New Issue Checklist

Rule Request

I'd like to be able to configure the opening_brace rule so it only applies to single-line declarations. If I'm splitting a method across multiple lines then it's usually a good idea to put the opening brace on a new line as well, but single-line declarations should still always have the brace on the same line.

lilyball commented 7 years ago

For example, the following looks weird:

func foo<T>(_ arg1: T)
    where T: Equatable {
    // bar
}

This reads much better when I can put the brace on a new line:

func foo<T>(_ arg1: T)
    where T: Equatable
{
    // bar
}
georgejlee commented 6 years ago

I agree, I have some functions with many long arguments so I find it easier to read when I split the arguments across multiple lines and put the opening brace on its own line, like this:

    open class func request(
        _ URLRequest: URLRequestConvertible,
        success: (((NSDictionary) -> Void)?),
        failure: (failureCallback?),
        isSuccess: @escaping ((DataResponse<Any>) -> Bool) = WebCom.defaultIsSuccess)
    {
        ...
    }
exevil commented 6 years ago

+1 here.

hartbit commented 6 years ago

+1 here too

petereddy commented 6 years ago

+1

I'm hoping to use SwiftLint to enforce just the thing kballard describes. I still want it to be possible to write:

func something() -> String {
    return "hi"
}

But in the case of functions with multi-line parameters/declarations, I'd like the opening brace to be allowed on a new line for clarity.

marcelofabri commented 6 years ago

PRs adding this as a configuration are welcome!

kubatruhlar commented 6 years ago

This is a good idea. If we go vertically, it's usually better to put the { on its own line to optically separate the parameters from the body of a function.

lilyball commented 6 years ago

This is becoming more of an issue on my team. I'm not willing to re-enable the opening_brace rule without the ability to restrict to single-line declarations, but my coworkers are starting to grumble about PRs being submitted that incorrectly put the opening brace on a new line for single-line declarations.

I am not at all familiar with the implementation of SwiftLint, so I can't even estimate how much work it would be to implement this (for example, does the rule have easy access to information about what line the statement started on, or is it going purely off of the token stream?). But I would really like to see it implemented. What can I do to try and push this forward?

kek-dev commented 5 years ago

+1

same for multiline class declarations (e.g. google swift style guide)

This is much more readable

class MyClass: MySuperclass, MyProtocol, SomeoneElsesProtocol, SomeFrameworkProtocol { // ... }

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had any recent activity. Please comment to prevent this issue from being closed. Thank you for your contributions!