forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VariableNameRule.swift
82 lines (75 loc) · 3.19 KB
/
VariableNameRule.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
//
// VariableNameRule.swift
// SwiftLint
//
// Created by JP Simard on 2015-05-16.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
import SwiftXPC
public struct VariableNameRule: ASTRule {
public init() {}
public let identifier = "variable_name"
public func validateFile(file: File) -> [StyleViolation] {
return validateFile(file, dictionary: file.structure.dictionary)
}
public func validateFile(file: File, dictionary: XPCDictionary) -> [StyleViolation] {
return (dictionary["key.substructure"] as? XPCArray ?? []).flatMap { subItem in
var violations = [StyleViolation]()
if let subDict = subItem as? XPCDictionary,
let kindString = subDict["key.kind"] as? String,
let kind = flatMap(kindString, { SwiftDeclarationKind(rawValue: $0) }) {
violations.extend(validateFile(file, dictionary: subDict))
violations.extend(validateFile(file, kind: kind, dictionary: subDict))
}
return violations
}
}
public func validateFile(file: File,
kind: SwiftDeclarationKind,
dictionary: XPCDictionary) -> [StyleViolation] {
let variableKinds: [SwiftDeclarationKind] = [
.VarClass,
.VarGlobal,
.VarInstance,
.VarLocal,
.VarParameter,
.VarStatic
]
if !contains(variableKinds, kind) {
return []
}
var violations = [StyleViolation]()
if let name = dictionary["key.name"] as? String,
let offset = flatMap(dictionary["key.offset"] as? Int64, { Int($0) }) {
let location = Location(file: file, offset: offset)
let nameCharacterSet = NSCharacterSet(charactersInString: name)
if !NSCharacterSet.alphanumericCharacterSet().isSupersetOfSet(nameCharacterSet) {
violations.append(StyleViolation(type: .NameFormat,
location: location,
severity: .High,
reason: "Variable name should only contain alphanumeric characters: '\(name)'"))
} else if name.substringToIndex(name.startIndex.successor()).isUppercase() {
violations.append(StyleViolation(type: .NameFormat,
location: location,
severity: .High,
reason: "Variable name should start with a lowercase character: '\(name)'"))
} else if count(name) < 3 || count(name) > 40 {
violations.append(StyleViolation(type: .NameFormat,
location: location,
severity: .Medium,
reason: "Variable name should be between 3 and 40 characters in length: " +
"'\(name)'"))
}
}
return violations
}
public let example = RuleExample(
ruleName: "Variable Name Rule",
ruleDescription: "Variable name should only contain alphanumeric characters, " +
"start with a a lowercase character and be between 3 and 40 characters in length.",
nonTriggeringExamples: [],
triggeringExamples: [],
showExamples: false
)
}