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 remove blank lines between chained functions #272

Merged
merged 9 commits into from
Jun 14, 2024

Conversation

mannylopez
Copy link
Contributor

@mannylopez mannylopez commented Jun 5, 2024

Summary

This PR proposes adding a rule that removes spaces between chained functions (blankLinesBetweenChainedFunctions )

Reasoning

Improves readability and maintainability, making it easier to see the sequence of functions that are applied to the object.

Examples

// WRONG
var innerPlanetNames: [String] {
  planets
    .filter { $0.isInnerPlanet }

    .map { $0.name }
}

// RIGHT
 var innerPlanetNames: [String] {
  planets
    .filter { $0.isInnerPlanet }
    .map { $0.name }
}

Note

After nicklockwood/SwiftFormat#1723 merges, the rule will also apply to cases where there is a comment between chained functions.

// WRONG
var innerPlanetNames: [String] {
  planets
    .filter { $0.isInnerPlanet }

    // Gets the name of the inner planet
    .map { $0.name }
}

// RIGHT
var innerPlanetNames: [String] {
  planets
    .filter { $0.isInnerPlanet }
    // Gets the name of the inner planet
    .map { $0.name }
}

Test

Confirmed that running the linter with --rules blankLinesBetweenChainedFunctions turned on does remove the extra space

image

.vscode/launch.json Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
@mannylopez mannylopez marked this pull request as ready for review June 5, 2024 23:58
}
```

</details>
Copy link
Member

Choose a reason for hiding this comment

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

The rule does not remove this empty space if there is a comment in between chained functions. Running the linter will leave this code as-is.

let planets = ["Mercury", "Venus", "Earth", "Mars"]

var transformedPlanetNames: [String] {
   planets
     .map { $0.uppercased() } // Making names uppercase

     // Adding excitement with an exclamation mark
     .map { $0 + "!" }
}

Is this exception necessary? I'd prefer we not allow this, and for consistency remove the blank line here as well.

Looking at the PR that introduced this SwiftFormat rule, I don't see any indication that this behavior was implemented intentionally. It would be easy to update the SwiftFormat rule to handle this case and remove this blank line as well.

What do you think @mannylopez?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the help updating the rule. Here's the SwiftFormat PR to get rid of this exception: nicklockwood/SwiftFormat#1723

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@mannylopez
Copy link
Contributor Author

@calda nicklockwood/SwiftFormat#1723 merged overnight. Do you think this PR is ready to merge as well?

@calda
Copy link
Member

calda commented Jun 11, 2024

We currently wait to merge until about one week after announcing the proposal review so everyone has a chance to share their feedback (so, we can merge this on Thursday or Friday). We'll also want to update the SwiftFormat version in this repo to include your bug fix. I'm working on instructions for how to do that, which I'll share in a bit.

@mannylopez
Copy link
Contributor Author

We currently wait to merge until about one week after announcing the proposal review so everyone has a chance to share their feedback (so, we can merge this on Thursday or Friday). We'll also want to update the SwiftFormat version in this repo to include your bug fix. I'm working on instructions for how to do that, which I'll share in a bit.

ah, that's right! Thanks.

Copy link
Member

@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! Looks great, and very strong support on Slack.

@calda calda merged commit f37baa7 into master Jun 14, 2024
5 checks passed
@calda calda deleted the manny-lopez--blank-lines-between-chained-functions branch June 14, 2024 18:17
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