Skip to content

Add rule: private_property_wrappers #39

@kj6dev

Description

@kj6dev

Overview

Enforce private access control for SwiftUI property wrappers that represent view-internal state.

Rule Identifier

private_property_wrappers

Rationale

SwiftUI property wrappers like @State, @StateObject, and @EnvironmentObject represent view-internal state that should not be accessed directly from outside the view. Making them private enforces proper encapsulation.

Property Wrappers That Should Be Private

  • @State - View's internal mutable state
  • @StateObject - View owns and manages the lifecycle
  • @EnvironmentObject - Injected from environment
  • @Environment - Reading from environment
  • @AppStorage - Persistent storage
  • @SceneStorage - Scene-specific storage
  • @FetchRequest - Core Data fetch
  • @GestureState - Gesture tracking state
  • @FocusState - Focus management
  • @ScaledMetric - Dynamic type scaling

Sometimes Non-Private (configurable)

  • @Binding - Often passed from parent view (public API)
  • @ObservedObject - Often passed from parent

Example

// ❌ Bad
@State var counter: Int = 0

// ✅ Good
private @State var counter: Int = 0

Configuration Options

Consider strictness levels:

  • Strict: All property wrappers must be private
  • Moderate: Allow @binding and @ObservedObject to be non-private
  • Relaxed: Only enforce for state-owning wrappers

Priority

Medium - High value, low risk, moderate effort

Reference

Full spec in docs/TODO-private-property-wrappers.md (to be archived after issue creation)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions