forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFatalErrorMessageRule.swift
61 lines (52 loc) · 1.93 KB
/
FatalErrorMessageRule.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// FatalErrorMessageRule.swift
// SwiftLint
//
// Created by Kim de Vos on 03/18/17.
// Copyright © 2017 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct FatalErrorMessageRule: ASTRule, ConfigurationProviderRule, OptInRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "fatal_error_message",
name: "Fatal Error Message",
description: "A fatalError call should have a message.",
kind: .idiomatic,
nonTriggeringExamples: [
"func foo() {\n fatalError(\"Foo\")\n}\n",
"func foo() {\n fatalError(x)\n}\n"
],
triggeringExamples: [
"func foo() {\n ↓fatalError(\"\")\n}\n",
"func foo() {\n ↓fatalError()\n}\n"
]
)
public func validate(file: File, kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard kind == .call,
let offset = dictionary.offset,
dictionary.name == "fatalError",
hasEmptyBody(dictionary: dictionary, file: file) else {
return []
}
return [
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset))
]
}
private func hasEmptyBody(dictionary: [String: SourceKitRepresentable], file: File) -> Bool {
guard let bodyOffset = dictionary.bodyOffset,
let bodyLength = dictionary.bodyLength else {
return false
}
if bodyLength == 0 {
return true
}
let body = file.contents.bridge().substringWithByteRange(start: bodyOffset, length: bodyLength)
return body == "\"\""
}
}