Rule Request: Enforce private properties in SwiftUI `View`'s
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
New rule request
- 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
- 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 = ""
}
- 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.
- 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the requested examples and the opt-in-rules guidance linked in README.md. Review the rule-request requirements and existing project conventions before determining the rule’s scope and configuration; done should cover the specified SwiftUI property wrappers, preserve the non-violating example, and include appropriate tests and documentation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100