forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariableNameMaxLengthRule.swift
85 lines (78 loc) · 2.97 KB
/
VariableNameMaxLengthRule.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
//
// VariableNameMaxLengthRule.swift
// SwiftLint
//
// Created by Mickaël Morier on 05/11/2015.
// Copyright © 2015 Realm. All rights reserved.
//
import SourceKittenFramework
import SwiftXPC
public struct VariableNameMaxLengthRule: ASTRule, ParameterizedRule {
public init() {
self.init(parameters: [
RuleParameter(severity: .Warning, value: 40),
RuleParameter(severity: .Error, value: 60)
])
}
public init(parameters: [RuleParameter<Int>]) {
self.parameters = parameters
}
public let parameters: [RuleParameter<Int>]
public static let description = RuleDescription(
identifier: "variable_name_max_length",
name: "Variable Name Max Length Rule",
description: "Variable name should be 40 characters or less.",
nonTriggeringExamples: [
"let myLet = 0",
"var myVar = 0",
"private let _myLet = 0"
],
triggeringExamples: [
"let myExtremelyVeryVeryVeryVeryVeryVeryLongLet = 0",
"var myExtremelyVeryVeryVeryVeryVeryVeryLongVar = 0",
"private let _myExtremelyVeryVeryVeryVeryVeryVeryLongLet = 0"
]
)
public func validateFile(file: File,
kind: SwiftDeclarationKind,
dictionary: XPCDictionary) -> [StyleViolation] {
let variableKinds: [SwiftDeclarationKind] = [
.VarClass,
.VarGlobal,
.VarInstance,
.VarLocal,
.VarParameter,
.VarStatic
]
if !variableKinds.contains(kind) {
return []
}
guard let name = dictionary["key.name"] as? String,
let offset = (dictionary["key.offset"] as? Int64).flatMap({ Int($0) }) else {
return []
}
return name.violationsForNameAtLocation(Location(file: file, offset: offset),
dictionary: dictionary, ruleDescription: self.dynamicType.description,
parameters: self.parameters)
}
}
extension String {
private func violationsForNameAtLocation(location: Location, dictionary: XPCDictionary,
ruleDescription: RuleDescription, parameters: [RuleParameter<Int>]) -> [StyleViolation] {
if characters.first == "$" {
// skip block variables
return []
}
let name = nameStrippingLeadingUnderscoreIfPrivate(dictionary)
for parameter in parameters.reverse() {
if name.characters.count > parameter.value {
return [StyleViolation(ruleDescription: ruleDescription,
severity: parameter.severity,
location: location,
reason: "Variable name should be \(parameter.value) characters " +
"or less: currently \(name.characters.count) characters")]
}
}
return []
}
}