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
Dschee opened this Issue May 16, 2017 · 5 comments

Comments

Projects
None yet
4 participants
@Dschee
Copy link
Collaborator

Dschee 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
}

@Dschee Dschee changed the title New Rule: Vertical Whitespace between Cases Rule Request: Vertical Whitespace between Cases May 16, 2017

@marcelofabri

This comment has been minimized.

Copy link
Collaborator

marcelofabri commented May 16, 2017

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

@Uncommon

This comment has been minimized.

Copy link
Contributor

Uncommon commented May 16, 2017

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

@Dschee

This comment has been minimized.

Copy link
Collaborator

Dschee 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

This comment has been minimized.

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
}
@Dschee

This comment has been minimized.

Copy link
Collaborator

Dschee 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.

@jpsim jpsim closed this in #2292 Nov 28, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment