-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMissingCommentRule.swift
More file actions
76 lines (62 loc) · 2.46 KB
/
MissingCommentRule.swift
File metadata and controls
76 lines (62 loc) · 2.46 KB
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
//
// MissingCommentRule.swift
// StringsLintFramework
//
// Created by Eidinger, Marco on 6/25/21.
//
import Foundation
public class MissingCommentRule: LintRule {
public static var description = RuleDescription(
identifier: "missing_comment",
name: "MissingComment",
description: "Comemnt for Localized string is missing"
)
private var declaredStrings = [LocalizedString]()
var severity: ViolationSeverity
private let declareParser: LocalizableParser
public required convenience init(configuration: Any) throws {
var config = MissingCommentRuleConfiguration()
do {
try config.apply(defaultDictionaryValue(configuration, for: MissingCommentRule.self.description.identifier))
} catch {}
self.init(declareParser: ComposedParser(parsers: [
try StringsParser.self.init(configuration: configuration)
]),
usageParser: ComposedParser(parsers: []),
severity: config.severity
)
}
public required convenience init() {
let config = MissingRuleConfiguration()
self.init(declareParser: StringsParser(),
usageParser: ComposedParser(parsers: []),
severity: config.severity)
}
public init(declareParser: LocalizableParser,
usageParser: LocalizableParser,
severity: ViolationSeverity) {
self.declareParser = declareParser
self.severity = severity
}
public func processFile(_ file: File) {
if self.declareParser.support(file: file) {
self.declaredStrings += self.processDeclarationFile(file)
}
}
public var violations: [Violation] {
return self.declaredStrings.filter({$0.comment == nil}).compactMap({ (string) -> Violation? in
return self.buildViolation(key: string.key, location: string.location)
})
}
private func processDeclarationFile(_ file: File) -> [LocalizedString] {
do {
return try self.declareParser.parse(file: file)
} catch let error {
print("Unable to parse file \(file): \(error)")
return []
}
}
private func buildViolation(key: String, location: Location) -> Violation {
return Violation(ruleDescription: MissingCommentRule.description, severity: self.severity, location: location, reason: "Comment for Localized string \"\(key)\" is missing")
}
}