Files
SwiftLint/Source/SwiftLintBuiltInRules/Rules/Idiomatic/XCTFailMessageRule.swift
T
Danny Mösch 40bd97038a Support arbitrary configurations in @SwiftSyntaxRule (#5275)
Almost all rules based on SwiftSyntax can be set up now by just adding
`@SwiftSyntaxRule` to the rule struct.
2023-10-16 19:34:43 +02:00

63 lines
1.6 KiB
Swift

import SwiftSyntax
@SwiftSyntaxRule
struct XCTFailMessageRule: Rule {
var configuration = SeverityConfiguration<Self>(.warning)
static let description = RuleDescription(
identifier: "xctfail_message",
name: "XCTFail Message",
description: "An XCTFail call should include a description of the assertion",
kind: .idiomatic,
nonTriggeringExamples: [
Example("""
func testFoo() {
XCTFail("bar")
}
"""),
Example("""
func testFoo() {
XCTFail(bar)
}
""")
],
triggeringExamples: [
Example("""
func testFoo() {
↓XCTFail()
}
"""),
Example("""
func testFoo() {
↓XCTFail("")
}
""")
]
)
}
private extension XCTFailMessageRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override func visitPost(_ node: FunctionCallExprSyntax) {
guard
let expression = node.calledExpression.as(DeclReferenceExprSyntax.self),
expression.baseName.text == "XCTFail",
node.arguments.isEmptyOrEmptyString
else {
return
}
violations.append(node.positionAfterSkippingLeadingTrivia)
}
}
}
private extension LabeledExprListSyntax {
var isEmptyOrEmptyString: Bool {
if isEmpty {
return true
}
return count == 1 && first?.expression.as(StringLiteralExprSyntax.self)?.isEmptyString == true
}
}