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 rule to add a linebreak before the else keyword in a multi-line guard statement #228

Merged
merged 5 commits into from
May 15, 2023

Conversation

calda
Copy link
Member

@calda calda commented May 8, 2023

Summary

This PR proposes a new rule to add a linebreak before the else keyword in a multi-line guard statement.

// WRONG -- else should be on its own line for multi-line guard statements
guard
  let galaxy,
  galaxy.name == "Milky Way" else
{  }

// WRONG -- else should be on the same line for single-line guard statements
guard let galaxy
else {  }

// RIGHT
guard
  let galaxy,
  galaxy.name == "Milky Way"
else {  }

// RIGHT
guard let galaxy else {
  
}

Reasoning

This was first suggested back in #108 (comment). This is also how the examples in the "wrap multi-line conditionals" rule description are formatted.

Placing the else on its own line makes it more visually distinct, and prevents it from blending in with the last statement.

We previously didn't enforce this through the formatter since the SwiftFormat rule for this didn't match our requirements very well. As of nicklockwood/SwiftFormat#1447 the SwiftFormat rule implementation works well for us.

Please react with 👍/👎 if you agree or disagree with this proposal.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@calda calda enabled auto-merge (squash) May 15, 2023 17:43
@calda calda merged commit 836e054 into master May 15, 2023
3 checks passed
@calda calda deleted the cal--guard-wrap-else branch May 15, 2023 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants