Skip to content

Match call-site paren on multi-line function calls #308

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

Merged

Conversation

mike-at-home
Copy link
Contributor

@mike-at-home mike-at-home commented May 12, 2025

Summary

This PR updates our Swift formatting rules to enable callsiteparen = balanced, which ensures consistent placement of parentheses and arguments in multiline function calls. With this option, the opening and closing parentheses are always balanced across lines, improving readability and making edits and diffs more predictable. It also works seamlessly with trailing commas to simplify future changes.


Reasoning

Currently, multiline function calls can inconsistently close the parenthesis on the same line as the last argument:

let result = executeRequest(
  request,
  onSuccess: { ... },
  onFailure: { ... })

With callsiteparen = balanced, this becomes:

let result = executeRequest(
  request,
  onSuccess: { ... },
  onFailure: { ... }
)

Benefits

  • Predictable structure
    The closing parenthesis always appears vertically aligned with the opening parenthesis, reducing visual clutter and ambiguity - particularly with SwiftUI chains.

Before - setBehaviors is aligned with the arguments.

AttributedLabel.groupItem(
  dataID: DataID.subtitleLabel,
  content: .stylableText(subtitle),
  style: style.subtitleFont)
  .setBehaviors { [weak self] context in
    guard let self else { return }
    context.constrainable.numberOfLines = style.subtitleNumberOfLines
  }

After - setBehaviors is aligned with what it is modifying.

AttributedLabel.groupItem(
  dataID: DataID.subtitleLabel,
  content: .stylableText(subtitle),
  style: style.subtitleFont
)
.setBehaviors { [weak self] context in
  guard let self else { return }
  context.constrainable.numberOfLines = style.subtitleNumberOfLines
}
  • Simplifies use of trailing commas
    Trailing commas make adding or removing parameters much cleaner and reduce diff noise:
let result = executeRequest(
  request,
  onSuccess: { ... },
  onFailure: { ... },
)

Adding another argument:

  onFailure: { ... },
+ completion: { ... },
)

Removing an argument:

- onFailure: { ... },
)
  • Easier visual diffing
    The uniform style prevents mismatched or ambiguous closings, which is especially useful in large nested calls.

  • Community alignment
    This approach is common in modern Swift style guides and is fully supported by swift-format and swiftlint.


Conclusion

Enabling callsiteparen = balanced gives us consistent, clear multiline call formatting that reduces code review noise and makes refactoring safer.

@mike-at-home mike-at-home marked this pull request as ready for review May 12, 2025 15:50
@calda
Copy link
Member

calda commented May 15, 2025

@mike-at-home, can you make the corresponding changes to the readme? This PR only changes the config file.

@calda
Copy link
Member

calda commented May 15, 2025

Feel free to make all of the changes in #306 since we are landing both of these changes together

@mike-at-home mike-at-home force-pushed the mkaz--match_closing_paren_on_calls branch from da16c1c to a87e445 Compare May 16, 2025 20:09
@mike-at-home mike-at-home merged commit 0f7a388 into airbnb:master May 16, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants