mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
With the binding of configurations to their associated rule types "unknown configuration" errors can be made more specific mentioning also the rule's identifier in the printed message.
64 lines
1.8 KiB
Swift
64 lines
1.8 KiB
Swift
import SwiftSyntax
|
|
|
|
struct FatalErrorMessageRule: SwiftSyntaxRule, ConfigurationProviderRule, OptInRule {
|
|
var configuration = SeverityConfiguration<Self>(.warning)
|
|
|
|
static let description = RuleDescription(
|
|
identifier: "fatal_error_message",
|
|
name: "Fatal Error Message",
|
|
description: "A fatalError call should have a message",
|
|
kind: .idiomatic,
|
|
nonTriggeringExamples: [
|
|
Example("""
|
|
func foo() {
|
|
fatalError("Foo")
|
|
}
|
|
"""),
|
|
Example("""
|
|
func foo() {
|
|
fatalError(x)
|
|
}
|
|
""")
|
|
],
|
|
triggeringExamples: [
|
|
Example("""
|
|
func foo() {
|
|
↓fatalError("")
|
|
}
|
|
"""),
|
|
Example("""
|
|
func foo() {
|
|
↓fatalError()
|
|
}
|
|
""")
|
|
]
|
|
)
|
|
|
|
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
|
Visitor(viewMode: .sourceAccurate)
|
|
}
|
|
}
|
|
|
|
private extension FatalErrorMessageRule {
|
|
final class Visitor: ViolationsSyntaxVisitor {
|
|
override func visitPost(_ node: FunctionCallExprSyntax) {
|
|
guard let expression = node.calledExpression.as(IdentifierExprSyntax.self),
|
|
expression.identifier.text == "fatalError",
|
|
node.argumentList.isEmptyOrEmptyString else {
|
|
return
|
|
}
|
|
|
|
violations.append(node.positionAfterSkippingLeadingTrivia)
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension TupleExprElementListSyntax {
|
|
var isEmptyOrEmptyString: Bool {
|
|
if isEmpty {
|
|
return true
|
|
}
|
|
return count == 1 && first?.expression.as(StringLiteralExprSyntax.self)?.isEmptyString == true
|
|
}
|
|
}
|