mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
added type body length violation
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user