mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
b83e0991b9
The MIT license doesn't require that all files be prepended with this licensing or copyright information. Realm confirmed that they're ok with this change. This will enable some companies to contribute to SwiftLint and the date & authorship information will remain accessible via git source control.
60 lines
1.9 KiB
Swift
60 lines
1.9 KiB
Swift
import Foundation
|
|
import SourceKittenFramework
|
|
|
|
public struct XCTFailMessageRule: ASTRule, ConfigurationProviderRule {
|
|
public var configuration = SeverityConfiguration(.warning)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "xctfail_message",
|
|
name: "XCTFail Message",
|
|
description: "An XCTFail call should include a description of the assertion.",
|
|
kind: .idiomatic,
|
|
nonTriggeringExamples: [
|
|
"func testFoo() {\n" +
|
|
" XCTFail(\"bar\")\n" +
|
|
"}",
|
|
"func testFoo() {\n" +
|
|
" XCTFail(bar)\n" +
|
|
"}"
|
|
],
|
|
triggeringExamples: [
|
|
"func testFoo() {\n" +
|
|
" ↓XCTFail()\n" +
|
|
"}",
|
|
"func testFoo() {\n" +
|
|
" ↓XCTFail(\"\")\n" +
|
|
"}"
|
|
]
|
|
)
|
|
|
|
public func validate(file: File,
|
|
kind: SwiftExpressionKind,
|
|
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
|
|
guard
|
|
kind == .call,
|
|
let offset = dictionary.offset,
|
|
dictionary.name == "XCTFail",
|
|
hasEmptyMessage(dictionary: dictionary, file: file)
|
|
else {
|
|
return []
|
|
}
|
|
|
|
return [StyleViolation(ruleDescription: type(of: self).description,
|
|
severity: configuration.severity,
|
|
location: Location(file: file, byteOffset: offset))]
|
|
}
|
|
|
|
private func hasEmptyMessage(dictionary: [String: SourceKitRepresentable], file: File) -> Bool {
|
|
guard
|
|
let bodyOffset = dictionary.bodyOffset,
|
|
let bodyLength = dictionary.bodyLength else { return false }
|
|
|
|
guard bodyLength > 0 else { return true }
|
|
|
|
let body = file.contents.bridge().substringWithByteRange(start: bodyOffset, length: bodyLength)
|
|
return body == "\"\""
|
|
}
|
|
}
|