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

Add new private_swiftui_state_property rule #4769

Merged
merged 16 commits into from
Jul 28, 2023

Conversation

mt00chikin
Copy link
Contributor

This creates a new opt-in rule that requires that SwiftUI state properties be declared as private. This is inspired by Apple's documentation for State:

To avoid this, always declare state as private, and place it in the highest view in the view hierarchy that needs access to the value

This only covers @State and not other SwiftUI property wrappers like @StateObject or @EnvironmentObject.

This MR attempts to address requests from issue: #3173

@SwiftLintBot
Copy link

SwiftLintBot commented Feb 16, 2023

168 Warnings
⚠️ This PR introduced a violation in Brave: /Sources/Brave/Frontend/Browser/Toolbars/BottomToolbar/Menu/PlaylistMenuButton.swift:86:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Brave: /Sources/BraveWallet/Crypto/BuySendSwap/SwapCryptoView.swift:170:10: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Brave: /Sources/BraveWallet/Crypto/FiltersDisplaySettingsView.swift:100:10: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Brave: /Sources/BraveWallet/Crypto/FiltersDisplaySettingsView.swift:102:10: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Brave: /Sources/BraveWallet/Crypto/FiltersDisplaySettingsView.swift:104:10: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Brave: /Sources/BraveWallet/Crypto/FiltersDisplaySettingsView.swift:106:10: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Brave: /Sources/BraveWallet/Crypto/FiltersDisplaySettingsView.swift:109:10: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Brave: /Sources/BraveWallet/Crypto/FiltersDisplaySettingsView.swift:111:10: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Brave: /Sources/BraveWallet/Crypto/FiltersDisplaySettingsView.swift:98:10: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Brave: /Sources/BraveWallet/Crypto/Market/MarketView.swift:21:10: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Brave: /Sources/BraveWallet/Crypto/NetworkFilterView.swift:20:10: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Brave: /Sources/BraveWallet/Crypto/Portfolio/EditUserAssetsView.swift:18:10: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Brave: /Sources/BraveWallet/Crypto/Search/SendTokenSearchView.swift:15:10: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/AppTPActivityView.swift:33:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/AppTPManageTrackerCell.swift:25:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/AppTPManageTrackersView.swift:29:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/AppTPManageTrackersView.swift:30:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/AppTPTrackerDetailView.swift:29:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/AppTPTrackerDetailView.swift:30:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/AutofillLoginPromptView.swift:25:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/CustomDaxDialog.swift:27:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/DownloadsList.swift:26:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/HomeMessageView.swift:46:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/OmniBarNotification.swift:26:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/OmniBarNotification.swift:28:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/OmniBarNotification.swift:29:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/OmniBarNotification.swift:31:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/OnboardingContainerView.swift:32:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/PasswordGenerationPromptView.swift:25:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /DuckDuckGo/SaveLoginView.swift:33:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /LocalPackages/SyncUI/Sources/SyncUI/Views/DeviceConnectedView.swift:31:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /LocalPackages/SyncUI/Sources/SyncUI/Views/Internal/ConnectModeView.swift:27:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /LocalPackages/SyncUI/Sources/SyncUI/Views/Internal/PasteCodeView.swift:30:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /LocalPackages/SyncUI/Sources/SyncUI/Views/Internal/UnderflowContainer.swift:35:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /LocalPackages/SyncUI/Sources/SyncUI/Views/Internal/UnderflowContainer.swift:36:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /LocalPackages/SyncUI/Sources/SyncUI/Views/Internal/UnderflowContainer.swift:41:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /LocalPackages/SyncUI/Sources/SyncUI/Views/Internal/UnderflowContainer.swift:46:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /LocalPackages/SyncUI/Sources/SyncUI/Views/ScanOrPasteCodeView.swift:31:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /LocalPackages/SyncUI/Sources/SyncUI/Views/SyncSettingsView.swift:65:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in DuckDuckGo: /LocalPackages/SyncUI/Sources/SyncUI/Views/TurnOnSyncView.swift:31:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardInputView.swift:18:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardInputView.swift:19:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardInputView.swift:20:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardInputView.swift:21:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardInputView.swift:22:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardItemRow.swift:21:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardItemRow.swift:22:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardItemRow.swift:23:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardItemRow.swift:24:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardItemRow.swift:25:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardItemRow.swift:26:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardSectionHeader.swift:13:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardSettingsEmptyView.swift:14:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardSettingsEmptyView.swift:15:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardSettingsEmptyView.swift:16:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardSettingsEmptyView.swift:17:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/ViewComponents/CreditCardAutofillToggle.swift:32:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/ViewComponents/CreditCardAutofillToggle.swift:33:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/ViewComponents/CreditCardAutofillToggle.swift:34:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/ViewComponents/CreditCardInputField.swift:29:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/ViewComponents/CreditCardInputField.swift:30:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/ViewComponents/CreditCardInputField.swift:41:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/ViewComponents/CreditCardInputField.swift:42:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/ViewComponents/CreditCardInputField.swift:43:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/ViewComponents/CreditCardInputField.swift:44:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/ViewComponents/RemoveCardButton.swift:26:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/ViewComponents/RemoveCardButton.swift:27:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/ViewComponents/RemoveCardButton.swift:28:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Firefox: /Client/Frontend/Autofill/CreditCard/ViewComponents/ToastView.swift:12:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in Kickstarter: /Kickstarter-iOS/SharedViews/LoadingBarButtonItem.swift:8:10: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in NetNewsWire: /iOS/Account/FeedbinAddAccountView.swift:20:9: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in NetNewsWire: /iOS/Account/NewsBlurAddAccountView.swift:18:9: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in NetNewsWire: /iOS/Account/ReaderAPIAddAccountView.swift:21:9: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /Pocket Casts Watch App Extension/PodcastsListView.swift:28:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /Pocket Casts Watch App Extension/PodcastsListView.swift:45:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/App Icon/AppIconWidget.swift:20:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/Common/ArtworkViews.swift:33:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/Common/ArtworkViews.swift:4:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/Now Playing/NowPlayingEntryView.swift:5:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/Now Playing/NowPlayingLockScreenWidget.swift:20:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/Up Next/EpisodeView.swift:5:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/Up Next/EpisodeView.swift:6:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/Up Next/UpNextLargeWidgetView.swift:5:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/Up Next/UpNextLargeWidgetView.swift:6:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/Up Next/UpNextLargeWidgetView.swift:7:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/Up Next/UpNextLockScreenWidget.swift:21:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/Up Next/UpNextMediumWidgetView.swift:5:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/Up Next/UpNextMediumWidgetView.swift:6:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/Up Next/UpNextMediumWidgetView.swift:7:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /WidgetExtension/Up Next/UpNextWidgetEntryView.swift:5:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/AboutView.swift:163:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/AboutView.swift:164:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/AboutView.swift:165:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/AboutView.swift:166:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/ChoosePodcastFolderView.swift:85:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/ColorPreviewFolderView.swift:82:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/ColorSelectCircle.swift:6:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/EditFolderView.swift:9:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/End of Year/Views/CircularProgressView.swift:49:16: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/End of Year/Views/CircularProgressView.swift:50:16: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/NameFolderView.swift:13:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/New Search/Views/Results/SearchResultsView.swift:11:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/New Search/Views/Results/SearchResultsView.swift:13:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/New Search/Views/Results/SearchResultsView.swift:14:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/Onboarding/Import/ImportDetailsView.swift:13:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/Onboarding/Import/ImportDetailsView.swift:14:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/Onboarding/Import/ImportDetailsView.swift:15:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/Onboarding/Import/ImportDetailsView.swift:16:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/Onboarding/Login/LoginLandingView.swift:290:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/Onboarding/Login/LoginLandingView.swift:58:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/Onboarding/Login/LoginLandingView.swift:61:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/Onboarding/Plus/PlusLandingView.swift:6:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/Onboarding/Plus/PlusPurchaseModal.swift:144:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/Onboarding/Plus/PlusPurchaseModal.swift:8:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/Onboarding/Plus/PlusPurchaseModal.swift:9:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/Onboarding/Plus/UpgradeLandingView.swift:192:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/Onboarding/Plus/UpgradeLandingView.swift:344:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/PodcastPickerRow.swift:10:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/PodcastPickerView.swift:83:12: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/SwiftUI/ActionBarOverlayView.swift:151:16: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/SwiftUI/ActionBarOverlayView.swift:152:16: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/SwiftUI/HighlightedText.swift:202:16: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/SwiftUI/HorizontalCarousel.swift:303:16: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/SwiftUI/HorizontalCarousel.swift:304:16: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/SwiftUI/HorizontalCarousel.swift:305:16: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ This PR introduced a violation in PocketCasts: /podcasts/SwiftUI/HorizontalCarousel.swift:306:16: warning: Private SwiftUI @State Properties Violation: SwiftUI state properties should be private (private_swiftui_state)
⚠️ Danger found 168 violations with this PR. Due to GitHub's max issue comment size, the number shown has been truncated to 126.
17 Messages
📖 Linting Aerial with this PR took 1.16s vs 1.15s on main (0% slower)
📖 Linting Alamofire with this PR took 1.48s vs 1.49s on main (0% faster)
📖 Linting Brave with this PR took 8.5s vs 8.5s on main (0% slower)
📖 Linting DuckDuckGo with this PR took 4.11s vs 4.1s on main (0% slower)
📖 Linting Firefox with this PR took 9.96s vs 9.91s on main (0% slower)
📖 Linting Kickstarter with this PR took 10.83s vs 10.84s on main (0% faster)
📖 Linting Moya with this PR took 0.61s vs 0.62s on main (1% faster)
📖 Linting NetNewsWire with this PR took 3.29s vs 3.26s on main (0% slower)
📖 Linting Nimble with this PR took 0.79s vs 0.79s on main (0% slower)
📖 Linting PocketCasts with this PR took 8.44s vs 8.42s on main (0% slower)
📖 Linting Quick with this PR took 0.4s vs 0.39s on main (2% slower)
📖 Linting Realm with this PR took 13.72s vs 13.8s on main (0% faster)
📖 Linting Sourcery with this PR took 2.42s vs 2.4s on main (0% slower)
📖 Linting Swift with this PR took 5.49s vs 5.45s on main (0% slower)
📖 Linting VLC with this PR took 1.45s vs 1.47s on main (1% faster)
📖 Linting Wire with this PR took 8.95s vs 8.96s on main (0% faster)
📖 Linting WordPress with this PR took 12.98s vs 12.93s on main (0% slower)

Generated by 🚫 Danger

Copy link
Collaborator

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution @mt00chikin! It's a good starting point.

Are you going to add more SwiftUI attributes to this rule?

It shouldn't be hard to make this rule correctable, that is offer a rewriter to add the private modifier automatically when running swiftlint --fix. What do you think about that?

CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@mt00chikin
Copy link
Contributor Author

I updated the branch based on your feedback and suggestions, they all looked good to me.
As far as making this correctable, that seems like a good addition to this rule. I'll have to dig into the Rewriter code a bit to figure out how it's implemented, but I'd be happy to try and get that working as well.

@mt00chikin mt00chikin force-pushed the private_swiftui_state_rule branch from 42f279d to dc7dfba Compare February 20, 2023 02:53
@mt00chikin
Copy link
Contributor Author

Right now this rule only requires that SwiftUI @State properties be private since this is explicitly recommended in Apple's documentation, but I could potentially see an argument for adding @StateObject as well.
I didn't want the scope of the rule to be too broad, so I left that one (along with @EnvironmentObject and @ObservedObject) out of this initial pass.

@DivineDominion
Copy link

@SimplyDanny With the advent of Swift macros and thus @StateObject being phased out, I believe this is a good-enough starting point, no more attributed needed 👍

@SimplyDanny
Copy link
Collaborator

@SimplyDanny With the advent of Swift macros and thus @StateObject being phased out, I believe this is a good-enough starting point, no more attributed needed 👍

Convinced. Are you willing to rebase the PR and resolve all conflicts, @mt00chikin?

@mt00chikin
Copy link
Contributor Author

@SimplyDanny absolutely, I'll try to get that addressed soon and get updates pushed up.

@mt00chikin mt00chikin force-pushed the private_swiftui_state_rule branch from dc7dfba to 6bc0a4f Compare July 17, 2023 15:15
Copy link
Collaborator

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid I still have some remarks.

@mt00chikin
Copy link
Contributor Author

No worries, @SimplyDanny! I appreciate the feedback. Let me know if there's anything else in my latest commit that might have been overlooked.

@mt00chikin mt00chikin force-pushed the private_swiftui_state_rule branch from e07e398 to 5bed5de Compare July 25, 2023 14:43
@SimplyDanny
Copy link
Collaborator

There are actually two more things that I've just thought of ...

When a declaration is nested inside another declaration implementing View like

struct ContentView: View {
    struct OtherStruct {
        @State var isPlaying: Bool = false   // rule doesn't trigger
    }
}

the rule doesn't trigger in the nested type. That's fine. However, if it's the other way around as in

struct OtherStruct {
    struct ContentView: View {
        @State var isPlaying: Bool = false    // rule doesn't trigger
    }
}

it also doesn't trigger and that'd be incorrect. It should trigger. We'd have to keep track of the scope we are currently in instead of skipping the type's body completely.

Other than that, Apple's documentation you referred to states

Create a state value in an App, Scene, or View by applying the @State attribute to a property declaration and providing an initial value.

which means the rule should not only check for conformance to View but to App and Scene as well.

@mt00chikin mt00chikin force-pushed the private_swiftui_state_rule branch from 807eae4 to a8ced28 Compare July 28, 2023 03:26
@SimplyDanny
Copy link
Collaborator

Let's see if the following example works:

struct MyStruct {
    struct ContentView: View {
        @State var isPlaying: Bool = false
    }
    @State var isPlaying: Bool = false
}

@mt00chikin
Copy link
Contributor Author

It doesn't. That's what I get for trying to do this late at night...
Is there a way to determine when a Visitor has exited a node? I don't see anything in the protocol that looks like it would tell me that. I'm guessing I'll have to use the parent to determine the current context when visiting a node?
I might have to put this on hold for a bit, work has been busy lately and I'll be on vacation next week.
I'll see if I can't get something actually working today, but it'll probably be the week after next before I can get this wrapped up.

@mt00chikin
Copy link
Contributor Author

Ok, I fundamentally misinterpreted visitPost(_:) before today. Needed to read the docs more thoroughly, that was my mistake. I believe I have your latest test case working correctly now, but I'm going to do some additional testing and try to clean the code up a bit where possible.

…s don't inadvertently affect the state of their parent
Copy link
Collaborator

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more tip ...

Apart from this little improvement, I think this looks good now. At least I cannot think of any more cases to be considered for the time being.

@SimplyDanny SimplyDanny enabled auto-merge (squash) July 28, 2023 19:58
@SimplyDanny
Copy link
Collaborator

This is going to be merged soon. Thank you for your patience!

@SimplyDanny SimplyDanny merged commit 0537f3a into realm:main Jul 28, 2023
@mt00chikin
Copy link
Contributor Author

Thank you! I appreciate the thorough and patient review feedback. I definitely learned a lot about SwiftSyntax from this experience.

@calebwilson706
Copy link

Has this rule been released? I'm trying to use it but I'm getting 'warning: 'private_swiftui_state' is not a valid rule identifier' and the response from 'swiftlint rules' doesn't contain it. Im on version '0.52.4'

@SimplyDanny
Copy link
Collaborator

Has this rule been released? I'm trying to use it but I'm getting 'warning: 'private_swiftui_state' is not a valid rule identifier' and the response from 'swiftlint rules' doesn't contain it. Im on version '0.52.4'

No, it's not yet been released.

@ObjectiveCesar
Copy link

ObjectiveCesar commented Aug 25, 2023

I personally think the same rule should apply to @StateObject.

@DivineDominion
Copy link

You and Apple both :)
https://developer.apple.com/documentation/swiftui/stateobject#

Just want to say that this is not a matter of preference whether a @StateObject should be changed from "outside" its owning view. One can try, but it's a source of problems.

@SimplyDanny
Copy link
Collaborator

I personally think the same rule should apply to @StateObject.

This is easy to achieve. Anyone willing to open a PR? 😊

@mt00chikin
Copy link
Contributor Author

If no one has already started on that work, I can probably find some time to add it this week or next! I actually had considered adding this from the outset, but it just got lost along the way. I'd be happy to add it, I think that it's a good addition to the rule

@ObjectiveCesar
Copy link

If no one has already started on that work, I can probably find some time to add it this week or next! I actually had considered adding this from the outset, but it just got lost along the way. I'd be happy to add it, I think that it's a good addition to the rule

Thank you for making the effort. I certainly appreciate it.

@mt00chikin
Copy link
Contributor Author

FYI, I've opened #5200 to address this feedback and apply the rule to StateObject as well. Feel free to comment on that MR with any additional concerns or requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants