From 2f4eff60ea5db7eb09f45f4de43f265db5b875b9 Mon Sep 17 00:00:00 2001 From: Miguel Jimenez Date: Tue, 22 Oct 2024 15:03:20 -0400 Subject: [PATCH] Add environment entry swift format rule --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index a3e8439..e3fac8a 100644 --- a/README.md +++ b/README.md @@ -3745,6 +3745,35 @@ _You can enable the following settings in Xcode by running [this script](resourc +* (link) **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) + +
+ + ### Why? + + Manually-implemented environment keys are verbose, and keeping them up-to-date is error-prone. + + ```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 + } + ``` + +
+ * (link) **Avoid using `()` as a type**. Prefer `Void`.