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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.build
.swiftpm
.DS_Store
.DS_Store
.vscode
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2191,6 +2191,50 @@ _You can enable the following settings in Xcode by running [this script](resourc

</details>

* <a id='remove-blank-lines-between-chained-functions'></a>(<a href='#remove-blank-lines-between-chained-functions'>link</a>) **Remove blank lines between chained functions.** [![SwiftFormat: blanklinesbetweenchainedfunctions](https://img.shields.io/badge/SwiftFormat-blankLinesBetweenChainedFunctions-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#blanklinesbetweenchainedfunctions)
mannylopez marked this conversation as resolved.
Show resolved Hide resolved
mannylopez marked this conversation as resolved.
Show resolved Hide resolved

<details>

#### Why?

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

```swift
// WRONG
mannylopez marked this conversation as resolved.
Show resolved Hide resolved
var innerPlanetNames: [String] {
planets
.filter { $0.isInnerPlanet }

.map { $0.name }
}

// 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 }
.map { $0.name }
}

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

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


### Closures

* <a id='favor-void-closure-return'></a>(<a href='#favor-void-closure-return'>link</a>) **Favor `Void` return types over `()` in closure declarations.** If you must specify a `Void` return type in a function declaration, use `Void` rather than `()` to improve readability. [![SwiftLint: void_return](https://img.shields.io/badge/SwiftLint-void__return-007A87.svg)](https://realm.github.io/SwiftLint/void_return)
Expand Down
1 change: 1 addition & 0 deletions Sources/AirbnbSwiftFormatTool/airbnb.swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@
--rules consistentSwitchStatementSpacing
--rules semicolons
--rules propertyType
--rules blankLinesBetweenChainedFunctions
mannylopez marked this conversation as resolved.
Show resolved Hide resolved
Loading