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 prefer a generated EnvironmentValue property implementation when possible #293

Merged
merged 5 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ let package = Package(

.binaryTarget(
name: "swiftformat",
url: "https://github.com/calda/SwiftFormat/releases/download/0.55-beta-15/SwiftFormat.artifactbundle.zip",
checksum: "5da82a61d4a29e77acc5a2e8668c40168970e1d6b2078bdfcb377b6f6ef35039"),
url: "https://github.com/calda/SwiftFormat/releases/download/0.55-beta-16/SwiftFormat.artifactbundle.zip",
checksum: "3db287e8fec8dea6eb73e646c0e69285d0140e409e78644b22286d92e4ae573c"),

.binaryTarget(
name: "SwiftLintBinary",
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3745,6 +3745,35 @@ _You can enable the following settings in Xcode by running [this script](resourc

</details>

* <a id='redundant-environment-key-implementation'></a>(<a href='#redundant-environment-key-implementation'>link</a>) **Prefer using the `@Entry` macro to define properties inside `EnvironmentValues`**. When adding properties to SwiftUI `EnvironemtnValues`, prefer using the compiler-synthesized property implementation when possible. [![SwiftFormat: environmentEntry](https://img.shields.io/badge/SwiftFormat-environmentEntry-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/develop/Rules.md#environmentEntry)

<details>

### Why?

Manually-implemented environment keys are verbose and it is considered a legacy pattern. `@Entry` was specifically intended to be a replacement considering it was backported to iOS 13.

```swift
/// WRONG: The `EnvironmentValues` property depends on `IsSelectedEnvironmentKey`
struct IsSelectedEnvironmentKey: EnvironmentKey {
static var defaultValue: Bool { false }
}

extension EnvironmentValues {
var isSelected: Bool {
get { self[IsSelectedEnvironmentKey.self] }
set { self[IsSelectedEnvironmentKey.self] = newValue }
}
}

/// RIGHT: The `EnvironmentValues` property uses the @Entry macro
extension EnvironmentValues {
@Entry var isSelected: Bool = false
}
```

</details>

* <a id='void-type'></a>(<a href='#void-type'>link</a>) **Avoid using `()` as a type**. Prefer `Void`.

<details>
Expand Down
1 change: 1 addition & 0 deletions Sources/AirbnbSwiftFormatTool/airbnb.swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
--rules consecutiveBlankLines
--rules duplicateImports
--rules extensionAccessControl
--rules environmentEntry
--rules hoistPatternLet
--rules indent
--rules markTypes
Expand Down
Loading