mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
Simplift DiscouragedNoneNameRule implementation (#4407)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import SwiftSyntax
|
||||
|
||||
public struct DiscouragedNoneNameRule: SourceKitFreeRule, OptInRule, ConfigurationProviderRule {
|
||||
public struct DiscouragedNoneNameRule: SwiftSyntaxRule, OptInRule, ConfigurationProviderRule {
|
||||
public var configuration = SeverityConfiguration(.warning)
|
||||
|
||||
public static var description = RuleDescription(
|
||||
@@ -178,29 +178,20 @@ public struct DiscouragedNoneNameRule: SourceKitFreeRule, OptInRule, Configurati
|
||||
|
||||
public init() {}
|
||||
|
||||
public func validate(file: SwiftLintFile) -> [StyleViolation] {
|
||||
public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
||||
Visitor(viewMode: .sourceAccurate)
|
||||
.walk(file: file, handler: \.violationPositions)
|
||||
.sorted { $0.position < $1.position }
|
||||
.map { position, reason in
|
||||
StyleViolation(
|
||||
ruleDescription: Self.description,
|
||||
severity: configuration.severity,
|
||||
location: Location(file: file, position: position),
|
||||
reason: reason
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension DiscouragedNoneNameRule {
|
||||
final class Visitor: SyntaxVisitor {
|
||||
private(set) var violationPositions: [(position: AbsolutePosition, reason: String)] = []
|
||||
|
||||
final class Visitor: ViolationsSyntaxVisitor {
|
||||
override func visitPost(_ node: EnumCaseElementSyntax) {
|
||||
let emptyParams = node.associatedValue?.parameterList.isEmpty ?? true
|
||||
if node.identifier.isNone, emptyParams {
|
||||
violationPositions.append((node.positionAfterSkippingLeadingTrivia, reason(type: "`case`")))
|
||||
if emptyParams, node.identifier.isNone {
|
||||
violations.append(ReasonedRuleViolation(
|
||||
position: node.positionAfterSkippingLeadingTrivia,
|
||||
reason: reason(type: "`case`")
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,7 +215,10 @@ private extension DiscouragedNoneNameRule {
|
||||
continue
|
||||
}
|
||||
|
||||
violationPositions.append((node.positionAfterSkippingLeadingTrivia, reason(type: type)))
|
||||
violations.append(ReasonedRuleViolation(
|
||||
position: node.positionAfterSkippingLeadingTrivia,
|
||||
reason: reason(type: type)
|
||||
))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user