Skip to content

Commit

Permalink
Additional MR feedback. Now the rule also can be applied to actors an…
Browse files Browse the repository at this point in the history
…d the function to check for the view protocol has been extracted to a function
  • Loading branch information
thompsonmatthew-heb committed Jul 24, 2023
1 parent e685a95 commit e07e398
Showing 1 changed file with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ struct PrivateSwiftUIStatePropertyRule: SwiftSyntaxRule, OptInRule, Configuratio
@State private var isPlaying: Bool = false
}
}
"""),
Example("""
actor ContentView: View {
@State private var isPlaying: Bool = false
}
""")
],
triggeringExamples: [
Expand All @@ -67,6 +72,11 @@ struct PrivateSwiftUIStatePropertyRule: SwiftSyntaxRule, OptInRule, Configuratio
@State ↓var isPlaying: Bool = false
}
}
"""),
Example("""
actor ContentView: View {
@State ↓var isPlaying: Bool = false
}
""")
]
)
Expand All @@ -79,25 +89,31 @@ struct PrivateSwiftUIStatePropertyRule: SwiftSyntaxRule, OptInRule, Configuratio
private extension PrivateSwiftUIStatePropertyRule {
final class Visitor: ViolationsSyntaxVisitor {
override var skippableDeclarations: [DeclSyntaxProtocol.Type] {
[EnumDeclSyntax.self, FunctionDeclSyntax.self, ProtocolDeclSyntax.self, VariableDeclSyntax.self]
[ProtocolDeclSyntax.self]
}

override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
guard let inheritedTypeCollection = node.inheritanceClause?.inheritedTypeCollection else {
return .skipChildren
}

let inheritedTypes = inheritedTypeCollection.typeNames
return inheritedTypes.contains("View") ? .visitChildren : .skipChildren
return inheritedTypeCollection.conformsToViewProtocol ? .visitChildren : .skipChildren
}

override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind {
guard let inheritedTypeCollection = node.inheritanceClause?.inheritedTypeCollection else {
return .skipChildren
}

let inheritedTypes = inheritedTypeCollection.typeNames
return inheritedTypes.contains("View") ? .visitChildren : .skipChildren
return inheritedTypeCollection.conformsToViewProtocol ? .visitChildren : .skipChildren
}

override func visit(_ node: ActorDeclSyntax) -> SyntaxVisitorContinueKind {
guard let inheritedTypeCollection = node.inheritanceClause?.inheritedTypeCollection else {
return .skipChildren
}

return inheritedTypeCollection.conformsToViewProtocol ? .visitChildren : .skipChildren
}

override func visitPost(_ node: MemberDeclListItemSyntax) {
Expand All @@ -118,6 +134,10 @@ private extension InheritedTypeListSyntax {
var typeNames: [String] {
compactMap { $0.typeName.as(SimpleTypeIdentifierSyntax.self) }.map(\.name.text)
}

var conformsToViewProtocol: Bool {
typeNames.contains("View")
}
}

private extension AttributeListSyntax? {
Expand Down

0 comments on commit e07e398

Please sign in to comment.