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

Rule Request: Enforce private properties in SwiftUI View's #3173

Open
2 tasks done
sindresorhus opened this issue Apr 6, 2020 · 6 comments
Open
2 tasks done

Rule Request: Enforce private properties in SwiftUI View's #3173

sindresorhus opened this issue Apr 6, 2020 · 6 comments
Labels
rule-request Requests for a new rules.

Comments

@sindresorhus
Copy link

sindresorhus commented Apr 6, 2020

New Issue Checklist

New rule request

  1. Why should this rule be added? Share links to existing discussion about what
    the community thinks about this.

Apple recommends that state properties in SwiftUI View's are private:

You should only access a state property from inside the view’s body, or from methods called by it. For this reason, declare your state properties as private, to prevent clients of your view from accessing it. - https://developer.apple.com/documentation/swiftui/state

  1. Provide several examples of what would and wouldn't trigger violations.

Would trigger:

struct ContentView: View {
	@State var foo = ""
}
struct ContentView: View {
	@ObservedObject var foo = Foo()
}
struct ContentView: View {
	@EnvironmentObject var foo
}
struct ContentView: View {
	@StateObject var foo = Foo()
}
struct ContentView: View {
	@Environment(\.colorScheme) var colorScheme
}

Would NOT trigger:

struct ContentView: View {
	var foo = ""
}
  1. Should the rule be configurable, if so what parameters should be configurable?

I would like to see an option to enforce that all properties that are not initializers to be private.

  1. Should the rule be opt-in or enabled by default? Why?
    See README.md for guidelines on when to mark a rule as opt-in.

Yes, since it's what Apple recommends.

@christiankm
Copy link
Contributor

I created a simple custom rule for this, as a starting point. I will try to make a PR for an official rule.

state_private:
    name: "Private SwiftUI State"
    regex: "\\@State\\s*var"
    message: "SwiftUI @State variables should always be marked private."
    severity: warning

@stale
Copy link

stale bot commented Nov 8, 2020

This issue has been automatically marked as stale because it has not had any recent activity. Please comment to prevent this issue from being closed. Thank you for your contributions!

@stale stale bot added the wontfix Issues that became stale and were auto-closed by a bot. label Nov 8, 2020
@sindresorhus
Copy link
Author

Please keep it open.

@stale stale bot removed the wontfix Issues that became stale and were auto-closed by a bot. label Nov 10, 2020
@stale
Copy link

stale bot commented Jan 10, 2021

This issue has been automatically marked as stale because it has not had any recent activity. Please comment to prevent this issue from being closed. Thank you for your contributions!

@stale stale bot added the wontfix Issues that became stale and were auto-closed by a bot. label Jan 10, 2021
@sindresorhus
Copy link
Author

Please keep it open.

@stale stale bot removed the wontfix Issues that became stale and were auto-closed by a bot. label Jan 10, 2021
@SimplyDanny SimplyDanny added the rule-request Requests for a new rules. label May 1, 2022
@mt00chikin
Copy link
Contributor

FYI, I created #4769 as a starting point to try and address a subset of these concerns. My team was also looking at a custom rule to detect @State properties and require them to be private. It doesn't cover every concern addressed in here, but it could definitely be updated or configured to cover additional cases if desired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule-request Requests for a new rules.
Projects
None yet
Development

No branches or pull requests

4 participants