Skip to content

Commit

Permalink
Convert strict_fileprivate rule to SwiftSyntax (realm#4319)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelofabri authored Oct 7, 2022
1 parent f174a55 commit 515587f
Showing 1 changed file with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation
import SourceKittenFramework
import SwiftSyntax

public struct StrictFilePrivateRule: OptInRule, ConfigurationProviderRule {
public struct StrictFilePrivateRule: OptInRule, ConfigurationProviderRule, SwiftSyntaxRule {
public var configuration = SeverityConfiguration(.warning)

public init() {}
Expand Down Expand Up @@ -59,12 +58,19 @@ public struct StrictFilePrivateRule: OptInRule, ConfigurationProviderRule {
]
)

public func validate(file: SwiftLintFile) -> [StyleViolation] {
// Mark all fileprivate occurrences as a violation
return file.match(pattern: "fileprivate", with: [.attributeBuiltin]).map {
StyleViolation(ruleDescription: Self.description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor? {
Visitor(viewMode: .sourceAccurate)
}
}

private extension StrictFilePrivateRule {
final class Visitor: SyntaxVisitor, ViolationsSyntaxVisitor {
private(set) var violationPositions: [AbsolutePosition] = []

override func visitPost(_ node: DeclModifierSyntax) {
if node.name.tokenKind == .fileprivateKeyword {
violationPositions.append(node.positionAfterSkippingLeadingTrivia)
}
}
}
}

0 comments on commit 515587f

Please sign in to comment.