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

Support comments when removing spaces between chained functions #1723

Conversation

mannylopez
Copy link
Contributor

@mannylopez mannylopez commented Jun 11, 2024

Updating the blankLinesBetweenChainedFunctions rule to take comments into account when removing blank lines between chained functions.

So this:

[0, 1, 2]
    .map { $0 * 2 }

    // Multiplies by 3
    .map { $0 * 3 }

Will turn into:

[0, 1, 2]
    .map { $0 * 2 }
    // Multiplies by 3
    .map { $0 * 3 }

cc @calda

Comment on lines 1193 to 1195
let commentIndex = formatter.index(of: .nonSpaceOrLinebreak, after: endOfLine)
{
let startOfLine = formatter.startOfLine(at: commentIndex)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this name could be improved, since this next token isn't always a comment. This could also be a good place to mention why we're doing this:

Suggested change
let commentIndex = formatter.index(of: .nonSpaceOrLinebreak, after: endOfLine)
{
let startOfLine = formatter.startOfLine(at: commentIndex)
// Make sure to preserve any code comment between the two lines
let nextTokenOrComment = formatter.index(of: .nonSpaceOrLinebreak, after: endOfLine)
{
let startOfLine = formatter.startOfLine(at: nextTokenOrComment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added!

Copy link
Collaborator

@calda calda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link

codecov bot commented Jun 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.23%. Comparing base (b0d492f) to head (e8d8656).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1723      +/-   ##
===========================================
+ Coverage    95.19%   95.23%   +0.03%     
===========================================
  Files           20       20              
  Lines        23248    23250       +2     
===========================================
+ Hits         22131    22141      +10     
+ Misses        1117     1109       -8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nicklockwood nicklockwood merged commit 36af3c5 into nicklockwood:develop Jun 11, 2024
7 checks passed
@nicklockwood
Copy link
Owner

This looks good, thanks! I'm wondering if this should be put behind some sort of option though, in case there are scenarios where a developer deliberately wanted to leave a blank line above commented methods in the chain but remove them otherwise?

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.

3 participants