added type body length violation

This commit is contained in:
JP Simard
2015-05-17 11:26:38 +02:00
parent 036087ada3
commit 7bbfa99e07
2 changed files with 43 additions and 8 deletions
+30
View File
@@ -237,11 +237,41 @@ extension File {
let kind = flatMap(kindString, { SwiftDeclarationKind(rawValue: $0) }) {
violations.extend(self.validateTypeName(kind, dict: subDict))
violations.extend(self.validateVariableName(kind, dict: subDict))
violations.extend(self.validateTypeBodyLength(kind, dict: subDict))
}
return violations
}, [], +)
}
func validateTypeBodyLength(kind: SwiftDeclarationKind, dict: XPCDictionary) ->
[StyleViolation] {
let typeKinds: [SwiftDeclarationKind] = [
.Class,
.Struct,
.Enum
]
if !contains(typeKinds, kind) {
return []
}
var violations = [StyleViolation]()
if let name = dict["key.name"] as? String,
let offset = flatMap(dict["key.offset"] as? Int64, { Int($0) }),
let bodyOffset = flatMap(dict["key.bodyoffset"] as? Int64, { Int($0) }),
let bodyLength = flatMap(dict["key.bodylength"] as? Int64, { Int($0) }) {
let location = Location(file: self, offset: offset)
let startLine = self.contents.lineAndCharacterForByteOffset(bodyOffset)
let endLine = self.contents.lineAndCharacterForByteOffset(bodyOffset + bodyLength)
if let startLine = startLine?.line, let endLine = endLine?.line
where endLine - startLine > 200 {
violations.append(StyleViolation(type: .Length,
location: location,
reason: "Type body should be span 200 lines or less: currently spans " +
"\(endLine - startLine) lines"))
}
}
return violations
}
func validateTypeName(kind: SwiftDeclarationKind, dict: XPCDictionary) -> [StyleViolation] {
let typeKinds: [SwiftDeclarationKind] = [
.Class,