realm / realm/SwiftLint

Rule Request: Enforce private properties in SwiftUI `View`'s

Open
#3,173 6 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

rule-request
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.