Fix false positive on `private_subject` (#4646)

Fixes #4643
This commit is contained in:
Marcelo Fabri 2022-12-19 13:31:34 -03:00 committed by GitHub
parent e1cddb710d
commit 7bd8362dae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 6 deletions

View File

@ -14,7 +14,10 @@
#### Bug Fixes
* None.
* Fix false positives on `private_subject` rule when using
subjects inside functions.
[Marcelo Fabri](https://github.com/marcelofabri)
[#4643](https://github.com/realm/SwiftLint/issues/4643)
## 0.50.3: Bundle of Towels

View File

@ -154,6 +154,10 @@ open class ViolationsSyntaxVisitor: SyntaxVisitor {
skippableDeclarations.contains { $0 == FunctionDeclSyntax.self } ? .skipChildren : .visitChildren
}
override open func visit(_ node: SubscriptDeclSyntax) -> SyntaxVisitorContinueKind {
skippableDeclarations.contains { $0 == FunctionDeclSyntax.self } ? .skipChildren : .visitChildren
}
override open func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {
skippableDeclarations.contains { $0 == VariableDeclSyntax.self } ? .skipChildren : .visitChildren
}

View File

@ -29,6 +29,10 @@ private extension PrivateSubjectRule {
final class Visitor: ViolationsSyntaxVisitor {
private let subjectTypes: Set<String> = ["PassthroughSubject", "CurrentValueSubject"]
override var skippableDeclarations: [DeclSyntaxProtocol.Type] {
[FunctionDeclSyntax.self, VariableDeclSyntax.self, SubscriptDeclSyntax.self]
}
override func visitPost(_ node: VariableDeclSyntax) {
guard !node.modifiers.isPrivateOrFileprivate,
!node.modifiers.containsStaticOrClass else {

View File

@ -24,7 +24,7 @@ internal struct PrivateSubjectRuleExamples {
Example(
#"""
final class Foobar {
private let goodSubject: PassthroughSubject<Bool, Never> = .ini()
private let goodSubject: PassthroughSubject<Bool, Never> = .init()
}
"""#
),
@ -52,7 +52,7 @@ internal struct PrivateSubjectRuleExamples {
Example(
#"""
final class Foobar {
private let goodSubject: CurrentValueSubject<String, Never> = .ini("toto")
private let goodSubject: CurrentValueSubject<String, Never> = .init("toto")
}
"""#
),
@ -89,7 +89,7 @@ internal struct PrivateSubjectRuleExamples {
#"""
final class Foobar {
private let goodSubject:
PassthroughSubject<Bool, Never> = .ini()
PassthroughSubject<Bool, Never> = .init()
}
"""#
),
@ -100,6 +100,13 @@ internal struct PrivateSubjectRuleExamples {
CurrentValueSubject<Bool, Never>(true)
}
"""#
),
Example(
"""
func foo() {
let goodSubject = PassthroughSubject<Bool, Never>(true)
}
"""
)
]
@ -135,7 +142,7 @@ internal struct PrivateSubjectRuleExamples {
Example(
#"""
final class Foobar {
let goodSubject: PassthroughSubject<Bool, Never> = .ini()
let goodSubject: PassthroughSubject<Bool, Never> = .init()
}
"""#
),
@ -243,7 +250,7 @@ internal struct PrivateSubjectRuleExamples {
#"""
final class Foobar {
let badSubject:
PassthroughSubject<Bool, Never> = .ini()
PassthroughSubject<Bool, Never> = .init()
}
"""#
),