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
Prev Previous commit
Next Next commit
Update code example
  • Loading branch information
mannylopez committed Jun 5, 2024
commit a33189d72bce9ed21c6c53a26312c5c305b47ed8
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2095,19 +2095,29 @@ _You can enable the following settings in Xcode by running [this script](resourc

<details>

```swift
// WRONG
[0, 1, 2]
.map { $0 * 2 }
#### Why?

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

```swift
// WRONG
let planets = ["Mercury", "Venus", "Earth", "Mars"]

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

.map { $0 + "!" } // Adding excitement with an exclamation mark
calda marked this conversation as resolved.
Show resolved Hide resolved
}

// RIGHT
[0, 1, 2]
.map { $0 * 2 }
.map { $0 * 3 }
let planets = ["Mercury", "Venus", "Earth", "Mars"]

var transformedPlanetNames: [String] {
planets
.map { $0.uppercased() } // Making names uppercase
.map { $0 + "!" } // Adding excitement with an exclamation mark
}
```

</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

Expand Down
Loading