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

Rule Request: Vertical Whitespace between Cases #1517

Closed
Jeehut opened this issue May 16, 2017 · 5 comments
Closed

Rule Request: Vertical Whitespace between Cases #1517

Jeehut opened this issue May 16, 2017 · 5 comments
Labels
rule-request Requests for a new rules.

Comments

@Jeehut
Copy link
Collaborator

Jeehut commented May 16, 2017

This is a rule request. I'm willing to implement this myself and send a PR, but before doing that I'd like to know if the rule is wanted or not. To explain the desired rule, I'd like to first post the custom rule we are already using in our projects:

  vertical_whitespace_between_cases:
    included: ".*.swift"
    regex: '[^\n){][ \t]*\n[ \t]*(?:case[^:(\n]+|default):(?:\\s*\/(\/\*).*)?'
    name: "Vertical Whitespace Between Cases"
    message: "Include a vertical whitespace (empty line) between cases in switch statements."
    severity: warning

I felt like this rule might be useful for other projects as well, which is why I'm posting it. What do you think about it? For us it helps a lot to prevent common issues and enforce a shared style.

Rationale

We had this discussion a few times, the common ground we were all happy with is this: When using switch-case there should be a newline before a new case begins except for the last case, because there's already a closing parentheses and indentation there as visual clues.

A few examples:

let x = 5

// ❌ not acceptable
switch x {
case 0..<5:
    // some code
case 5..<10:
    // some code
default:
    // some code
}

// ❌not acceptable
switch x {
case 0..<5:
    // some code

case 5..<10:
    // some code

default:
    // some code

}

// ✅ acceptable
switch x {
case 0..<5:
    // some code

case 5..<10:
    // some code

default:
    // some code
}
@Jeehut Jeehut changed the title New Rule: Vertical Whitespace between Cases Rule Request: Vertical Whitespace between Cases May 16, 2017
@marcelofabri marcelofabri added the rule-request Requests for a new rules. label May 16, 2017
@marcelofabri
Copy link
Collaborator

I think this would be useful, but I'm not sure if it should be a default rule.

@Uncommon
Copy link
Contributor

I personally prefer a line before the first case as well, for consistency, so it would be nice to have that option.

@Jeehut
Copy link
Collaborator Author

Jeehut commented May 29, 2017

Okay, this sounds to me like the rule would be merged, if it had enough configuration options. Sure, I'll try to add them, too then. Let's see when I will have the time to tackle this. Probably end of this week ...

@gobetti
Copy link

gobetti commented Jul 12, 2018

Hey guys, not entirely related but very close, so I was thinking if this could be part of this rule as a configuration? I can open a new issue if you prefer. The idea is not to have vertical whitespace after a case:

// ❌not acceptable
switch x {
case 0..<5:

    // some code

case 5..<10:

    // some code

default:

    // some code
}

// ✅ acceptable
switch x {
case 0..<5:
    // some code

case 5..<10:
    // some code

default:
    // some code
}

@Jeehut
Copy link
Collaborator Author

Jeehut commented Jul 13, 2018

@gobetti I like the idea, though I think it should be part of a new rule which could be named something like VerticalWhitespacePrecedingCaseBody. Otherwise the description of this rule wouldn't apply any longer and things would get too complicated IMHO.

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

4 participants