68 lines
2.2 KiB
Swift
68 lines
2.2 KiB
Swift
import SwiftSyntax
|
|
|
|
struct QuickDiscouragedFocusedTestRule: OptInRule, ConfigurationProviderRule, SwiftSyntaxRule {
|
|
var configuration = SeverityConfiguration(.warning)
|
|
|
|
init() {}
|
|
|
|
static let description = RuleDescription(
|
|
identifier: "quick_discouraged_focused_test",
|
|
name: "Quick Discouraged Focused Test",
|
|
description: "Non-focused tests won't run as long as this test is focused",
|
|
kind: .lint,
|
|
nonTriggeringExamples: QuickDiscouragedFocusedTestRuleExamples.nonTriggeringExamples,
|
|
triggeringExamples: QuickDiscouragedFocusedTestRuleExamples.triggeringExamples
|
|
)
|
|
|
|
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
|
Visitor(viewMode: .sourceAccurate)
|
|
}
|
|
}
|
|
|
|
private extension QuickDiscouragedFocusedTestRule {
|
|
final class Visitor: ViolationsSyntaxVisitor {
|
|
override var skippableDeclarations: [DeclSyntaxProtocol.Type] { .all }
|
|
|
|
override func visitPost(_ node: FunctionCallExprSyntax) {
|
|
if let identifierExpr = node.calledExpression.as(IdentifierExprSyntax.self),
|
|
case let name = identifierExpr.identifier.withoutTrivia().text,
|
|
QuickFocusedCallKind(rawValue: name) != nil {
|
|
violations.append(node.positionAfterSkippingLeadingTrivia)
|
|
}
|
|
}
|
|
|
|
override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
|
|
node.containsInheritance ? .visitChildren : .skipChildren
|
|
}
|
|
|
|
override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
|
|
node.isSpecFunction ? .visitChildren : .skipChildren
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension ClassDeclSyntax {
|
|
var containsInheritance: Bool {
|
|
guard let inheritanceList = inheritanceClause?.inheritedTypeCollection else {
|
|
return false
|
|
}
|
|
|
|
return inheritanceList.isNotEmpty
|
|
}
|
|
}
|
|
|
|
private extension FunctionDeclSyntax {
|
|
var isSpecFunction: Bool {
|
|
return identifier.tokenKind == .identifier("spec") &&
|
|
signature.input.parameterList.isEmpty &&
|
|
modifiers.containsOverride
|
|
}
|
|
}
|
|
|
|
private enum QuickFocusedCallKind: String {
|
|
case fdescribe
|
|
case fcontext
|
|
case fit
|
|
case fitBehavesLike
|
|
}
|