Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Created February 7, 2023 09:59
Show Gist options
  • Save chriseidhof/d8c079ca97099a6122f37890a144e9b0 to your computer and use it in GitHub Desktop.
Save chriseidhof/d8c079ca97099a6122f37890a144e9b0 to your computer and use it in GitHub Desktop.
SwiftLint rules for state, state object, environment and scaled metric
disabled_rules:
- trailing_comma
opt_in_rules:
- file_header
file_header:
forbidden_pattern: /./
custom_rules:
state_private:
name: "Private SwiftUI State"
regex: "\\@State\\s*var"
message: "SwiftUI @State variables should always be marked private."
severity: warning
state_object_private:
name: "Private SwiftUI StateObject"
regex: "\\@StateObject\\s*var"
message: "SwiftUI @StateObject variables should always be marked private."
severity: warning
environment_private:
name: "Private SwiftUI Environment"
regex: "\\@Environment.*\\)\\s*var"
message: "SwiftUI @Environment variables should always be marked private."
severity: warning
environment_private:
name: "Private SwiftUI ScaledMetric"
regex: "\\@ScaledMetric.*\\)\\s*var"
message: "SwiftUI @ScaledMetric variables should always be marked private."
severity: warning
@miku1958
Copy link

make some improvements

# Make DynamicProperty always be private
# Using $value to get the Binding and modifying it in the external view will get this error:
# Accessing State's value outside of being installed on a View. This will result in a constant Binding of the initial value and will not update.
# Bingding and ObservedObject were removed because both can be held separately outside of the View
custom_rules:
  DynamicProperty_private:
      name: "Private SwiftUI DynamicProperty"
      regex: "@(AccessibilityFocusState|AppStorage|EnvironmentObject|FocusState|FocusedObject|FocusedObject|GestureState|Namespace|ScaledMetric|SceneStorage|State|StateObject)\\s*?\\n?(\\s|\\t)*?(var|public|internal|open)"
      message: "SwiftUI DynamicProperty variables should always be marked private."
      severity: warning
  DynamicProperty_private_argument:
      name: "Private SwiftUI DynamicProperty"
      regex: "@(Environment|FetchRequest|FocusedValue|GestureState|NSApplicationDelegateAdaptor|ScaledMetric|SectionedFetchRequest)\\(.*\\)\\s*?\\n?(\\s|\\t)*?(var|public|internal|open)"
      message: "SwiftUI DynamicProperty(with argument) variables should always be marked private."
      severity: warning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment