-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule that requires SwiftUI state properties to be private
Make private state property rule opt-in Update CHANGELOG.md
- Loading branch information
1 parent
393318d
commit dc7dfba
Showing
5 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
Source/SwiftLintFramework/Rules/Lint/PrivateSwiftUIStatePropertyRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import SwiftSyntax | ||
|
||
/// Rule to require that any state properties in SwiftUI be declared as private. | ||
/// State properties should only be accessible from inside the View's body, or from methods called by it | ||
struct PrivateSwiftUIStatePropertyRule: SwiftSyntaxRule, OptInRule, ConfigurationProviderRule { | ||
var configuration = SeverityConfiguration(.warning) | ||
|
||
static let description = RuleDescription( | ||
identifier: "private_swiftui_state", | ||
name: "Private SwiftUI @State Properties", | ||
description: "SwiftUI's state properties should be private", | ||
kind: .lint, | ||
nonTriggeringExamples: [ | ||
Example( | ||
""" | ||
struct ContentView: View { | ||
@State private var isPlaying: Bool = false | ||
} | ||
""" | ||
), | ||
Example( | ||
""" | ||
struct ContentView: View { | ||
@State fileprivate var isPlaying: Bool = false | ||
} | ||
""" | ||
), | ||
Example( | ||
""" | ||
struct ContentView: View { | ||
var isPlaying = false | ||
} | ||
""" | ||
), | ||
Example( | ||
""" | ||
struct ContentView: View { | ||
@StateObject var foo = Foo() | ||
} | ||
""" | ||
) | ||
], | ||
triggeringExamples: [ | ||
Example( | ||
""" | ||
struct ContentView: View { | ||
@State var isPlaying: Bool = false | ||
} | ||
""" | ||
) | ||
]) | ||
|
||
init() {} | ||
|
||
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor { | ||
Visitor(viewMode: .sourceAccurate) | ||
} | ||
} | ||
|
||
private extension PrivateSwiftUIStatePropertyRule { | ||
final class Visitor: ViolationsSyntaxVisitor { | ||
override func visitPost(_ node: MemberDeclListItemSyntax) { | ||
guard | ||
let decl = node.decl.as(VariableDeclSyntax.self), | ||
decl.attributes.hasStateAttribute, | ||
!decl.modifiers.isPrivateOrFileprivate | ||
else { | ||
return | ||
} | ||
|
||
violations.append(decl.letOrVarKeyword.positionAfterSkippingLeadingTrivia) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters