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

Conversation

miguel-jimenez-0529
Copy link
Contributor

@miguel-jimenez-0529 miguel-jimenez-0529 commented Oct 22, 2024

Summary

This PR adds a new rule to prefer using a generated EnvironmentValues property implementation.

This rule is implemented by the new environmentEntry SwiftFormat rule added in nicklockwood/SwiftFormat#1908.

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

Reasoning

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

@miguel-jimenez-0529 miguel-jimenez-0529 marked this pull request as draft October 22, 2024 19:07
README.md Outdated Show resolved Hide resolved
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.

Remember to update airbnb.swiftformat and the SwiftFormat build before we merge this!

@miguel-jimenez-0529 miguel-jimenez-0529 marked this pull request as ready for review October 22, 2024 19:19
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.

Love it! Nice work!

@calda calda merged commit 25cc395 into master Nov 4, 2024
5 checks passed
@calda calda deleted the mj-environment-entry branch November 4, 2024 22:08
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