forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTodoRule.swift
99 lines (86 loc) · 3.15 KB
/
TodoRule.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// TodoRule.swift
// SwiftLint
//
// Created by JP Simard on 5/16/15.
// Copyright © 2015 Realm. All rights reserved.
//
import SourceKittenFramework
extension SyntaxKind {
/// Returns if the syntax kind is comment-like.
public var isCommentLike: Bool {
return [
SyntaxKind.comment,
.commentMark,
.commentURL,
.docComment,
.docCommentField
].contains(self)
}
}
public struct TodoRule: ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "todo",
name: "Todo",
description: "TODOs and FIXMEs should be avoided.",
nonTriggeringExamples: [
"// notaTODO:\n",
"// notaFIXME:\n"
],
triggeringExamples: [
"// ↓TODO:\n",
"// ↓FIXME:\n",
"// ↓TODO(note)\n",
"// ↓FIXME(note)\n",
"/* ↓FIXME: */\n",
"/* ↓TODO: */\n",
"/** ↓FIXME: */\n",
"/** ↓TODO: */\n"
]
)
private func customMessage(lines: [Line], location: Location) -> String {
var reason = type(of: self).description.description
guard let lineIndex = location.line,
let currentLine = lines.first(where: { $0.index == lineIndex }) else {
return reason
}
// customizing the reason message to be specific to fixme or todo
var message = currentLine.content
if currentLine.content.contains("FIXME") {
reason = "FIXMEs should be avoided"
message = message.replacingOccurrences(of: "FIXME", with: "")
} else {
reason = "TODOs should be avoided"
message = message.replacingOccurrences(of: "TODO", with: "")
}
message = message.replacingOccurrences(of: "//", with: "")
// trim whitespace
message = message.trimmingCharacters(in: .whitespacesAndNewlines)
// limiting the output length of todo message
let maxLengthOfMessage = 30
if message.utf16.count > maxLengthOfMessage {
let index = message.index(message.startIndex,
offsetBy: maxLengthOfMessage,
limitedBy: message.endIndex) ?? message.endIndex
reason += message.substring(to: index) + "..."
} else {
reason += message
}
return reason
}
public func validate(file: File) -> [StyleViolation] {
return file.match(pattern: "\\b(TODO|FIXME)\\b").flatMap { range, syntaxKinds in
if !syntaxKinds.filter({ !$0.isCommentLike }).isEmpty {
return nil
}
let location = Location(file: file, characterOffset: range.location)
let reason = customMessage(lines: file.lines, location: location)
return StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: location,
reason: reason )
}
}
}