-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathVariableNameMinLengthRule.swift
86 lines (77 loc) · 3.11 KB
/
VariableNameMinLengthRule.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
//
// VariableNameMinLengthRule.swift
// SwiftLint
//
// Created by Mickaël Morier on 04/11/2015.
// Copyright © 2015 Realm. All rights reserved.
//
import SourceKittenFramework
import SwiftXPC
public struct VariableNameMinLengthRule: ASTRule, ConfigurableRule {
public init() { }
public init?(config: AnyObject) {
self.init()
if let config = [Int].arrayOf(config) where config.count > 0 {
warning = RuleParameter(severity: .Warning, value: config[0])
if config.count > 1 {
error = RuleParameter(severity: .Error, value: config[1])
}
} else if let config = config as? [String: AnyObject] {
if let warningNumber = config["warning"] as? Int {
warning = RuleParameter(severity: .Warning, value: warningNumber)
}
if let errorNumber = config["error"] as? Int {
error = RuleParameter(severity: .Error, value: errorNumber)
}
if let excluded = config["excluded"] as? [String] {
self.excluded = excluded
}
} else {
return nil
}
}
public var excluded = [String]()
private var warning = RuleParameter(severity: .Warning, value: 3)
private var error = RuleParameter(severity: .Error, value: 2)
public static let description = RuleDescription(
identifier: "variable_name_min_length",
name: "Variable Name Min Length Rule",
description: "Variable name should not be too short.",
nonTriggeringExamples: [
"let myLet = 0",
"var myVar = 0",
"private let _myLet = 0"
],
triggeringExamples: [
"↓let i = 0",
"↓var id = 0",
"private ↓let _i = 0"
]
)
public func validateFile(file: File, kind: SwiftDeclarationKind,
dictionary: XPCDictionary) -> [StyleViolation] {
return file.validateVariableName(dictionary, kind: kind).map { name, offset in
if !excluded.contains(name) {
let charCount = name.characters.count
for parameter in [error, warning] where charCount < parameter.value {
return [StyleViolation(ruleDescription: self.dynamicType.description,
severity: parameter.severity,
location: Location(file: file, byteOffset: offset),
reason: "Variable name should be \(parameter.value) characters " +
"or more: currently \(charCount) characters")]
}
}
return []
} ?? []
}
public func isEqualTo(rule: ConfigurableRule) -> Bool {
guard let rule = rule as? VariableNameMinLengthRule else {
return false
}
// Need to use alternate method to compare excluded due to apparent bug in
// the way that SwiftXPC compares [String]
return self.error == rule.error &&
self.warning == rule.warning &&
zip(self.excluded, rule.excluded).reduce(true) { $0 && ($1.0 == $1.1) }
}
}