Files
SwiftLint/Source/SwiftLintBuiltInRules/Rules/Lint/QuickDiscouragedFocusedTestRule.swift
T
Danny Mösch 58928b7e40 Enforce any on existential types (#5273)
This makes syntactically clear which types are rather expensive.
2023-10-12 08:37:23 +02:00

63 lines
2.1 KiB
Swift

import SwiftSyntax
@SwiftSyntaxRule
struct QuickDiscouragedFocusedTestRule: OptInRule, ConfigurationProviderRule {
var configuration = SeverityConfiguration<Self>(.warning)
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
)
}
private extension QuickDiscouragedFocusedTestRule {
final class Visitor: ViolationsSyntaxVisitor {
override var skippableDeclarations: [any DeclSyntaxProtocol.Type] { .all }
override func visitPost(_ node: FunctionCallExprSyntax) {
if let identifierExpr = node.calledExpression.as(DeclReferenceExprSyntax.self),
case let name = identifierExpr.baseName.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?.inheritedTypes else {
return false
}
return inheritanceList.isNotEmpty
}
}
private extension FunctionDeclSyntax {
var isSpecFunction: Bool {
return name.tokenKind == .identifier("spec") &&
signature.parameterClause.parameters.isEmpty &&
modifiers.contains(keyword: .override)
}
}
private enum QuickFocusedCallKind: String {
case fdescribe
case fcontext
case fit
case fitBehavesLike
}