Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional rule to prevent empty lines at the start of methods or closures #1659

Open
Bersaelor opened this issue Jul 5, 2017 · 2 comments
Labels
rule-request Requests for a new rules.

Comments

@Bersaelor
Copy link

Bersaelor commented Jul 5, 2017

So far we have vertical_whitespace with the max_empty_lines parameter.

There are plenty of places where empty lines are good to increase grouping.

One thing we have observed though and would like to prevent in our projects are empty lines at the beginning, like in

func startDoingStuff() -> Result {

    print("Now doing stuff")
    // ...

    return someResult
}

or

[0...1].map { number in

    print("number: \(number)")
    // ...
    return number* number
}

we don't think there is any reason to allow empty lines at the start of methods so I would like to propose a new rule disallowing this.

I'm about to make a PR too, but since I haven't used sourcery yet I might need a few minutes to get it right (and make all tests pass).

@marcelofabri
Copy link
Collaborator

Related to #1345 and #640

@marcelofabri marcelofabri added the rule-request Requests for a new rules. label Jul 5, 2017
@Bersaelor
Copy link
Author

Bersaelor commented Jul 5, 2017

Thank you for linking those issues @marcelofabri .
I agree for type declarations a padding at the start or end is something many people do.
What we wanted to prevent in our team is code being needlessly bloated by empty lines at the beginning of func or trailing closures.
Not 100% sure how to separate the type declarations from the closure implementations with regex, will try my best.
Example:

        nonTriggeringExamples: [
            "guard true else { return true }",
            "guard true else {\n return true\n}",
            "if true else {\n return true\n}",
            "method(name: String) {\n return name\n}",
            "method(name: String) { return name }",
            "[1, 2].map { $0 + 1 }",
            "[1, 2].map {\n $0 + 1\n}",
            "[1, 2].map { number in\n return number + 1 \n}"
            "struct AwesomeStruct {\n\n let awesomeName: String",
            "enum AmazingFood: String {\n\n case cheese",
            "class CoolClass {\n\n var lastOpened: Date"
        ],
        triggeringExamples: [
            "guard true else {\n↓\n return true\n}",
            "if true else {\n↓\n return true\n}",
            "method(name: String) {\n↓\n return name\n}",
            "[1, 2].map {\n↓\n $0 + 1\n}",
            "[1, 2].map { number in\n↓\n return number + 1 \n}"
        ]

WiP branch: https://github.com/Bersaelor/SwiftLint/tree/feature/NoBlankFirstClosureLineRule

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule-request Requests for a new rules.
Projects
None yet
Development

No branches or pull requests

2 participants