Swiftformat: Rule to force position of else statement in multiline guard statements

Created on 28 Dec 2017  路  6Comments  路  Source: nicklockwood/SwiftFormat

Would it be possible to have a rule that enforces else position in multi-line guard statements?
E.g.

guard
  let a = a,
  let b = b else {
  return
}

would be formatted into

guard
  let a = a,
  let b = b
else {
  return
}

At the moment SwiftFormat leaves code like this untouched.

I believe this is related to #37.

Most helpful comment

@nicklockwood Please don't tie it into the elseOnSameLine rule. We use else on same line for everything except multiline guards

All 6 comments

Another useful formatting rule would be to enforce uniform formatting for multiline guards.

For example, when guard is multiline, but there is no newline after guard itself:

guard let a = a,
  let b = b
else {
 return
}

Could be formatted into

guard
  let a = a,
  let b = b
else {
  return
}

Since it's clear that guard is multiline here.

@mgrebenets do you think it would make sense to tie this to the existing elseOnSameLine rule configuration? It seems related.

@nicklockwood Please don't tie it into the elseOnSameLine rule. We use else on same line for everything except multiline guards

A rule to enforce the multiline guard formatting would be greatly appreciated, though. With our current setup, swiftformat seems to respect/infer the current multiline guard format and leave it as is; but the reason we're using swiftformat is to enforce a certain formatting style, so it kind of defeats the purpose :)

Same here.

@mgrebenets @rafcabezas @hartbit this is now implemented in 0.45.1. It's controlled by a new --guardelse option.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MaxDesiatov picture MaxDesiatov  路  4Comments

AniHovhannisyanAni picture AniHovhannisyanAni  路  3Comments

MaxDesiatov picture MaxDesiatov  路  3Comments

soranoba picture soranoba  路  3Comments

PompeiaPaetenari picture PompeiaPaetenari  路  4Comments