Files
SwiftLint/Source/SwiftLintFramework/Rules/FunctionBodyLengthRule.swift
T
2016-01-14 20:24:47 -08:00

90 lines
3.5 KiB
Swift

//
// FunctionBodyLengthRule.swift
// SwiftLint
//
// Created by JP Simard on 2015-05-16.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
import SwiftXPC
public struct FunctionBodyLengthRule: ASTRule, ViolationLevelRule {
public var warning = RuleParameter(severity: .Warning, value: 40)
public var error = RuleParameter(severity: .Error, value: 100)
public init() {}
public static let description = RuleDescription(
identifier: "function_body_length",
name: "Function Body Length",
description: "Functions bodies should not span too many lines."
)
private func numberOfCommentOnlyLines(file: File, startLine: Int, endLine: Int) -> Int {
let commentKinds = Set(SyntaxKind.commentKinds())
return file.syntaxKindsByLine(startLine, endLine: endLine).filter { _, kinds -> Bool in
// skip blank lines
guard !kinds.isEmpty else {
return false
}
return kinds.filter { !commentKinds.contains($0) }.isEmpty
}.count
}
private func lineCount(file: File, startLine: Int, endLine: Int) -> Int {
let commentedLines = numberOfCommentOnlyLines(file, startLine: startLine, endLine: endLine)
return endLine - startLine - commentedLines
}
private func exceedsLineCountExcludingComments(file: File, _ start: Int, _ end: Int,
_ limit: Int) -> Bool {
return end - start > limit && lineCount(file, startLine: start, endLine: end) > limit
}
public func validateFile(file: File,
kind: SwiftDeclarationKind,
dictionary: XPCDictionary) -> [StyleViolation] {
let functionKinds: [SwiftDeclarationKind] = [
.FunctionAccessorAddress,
.FunctionAccessorDidset,
.FunctionAccessorGetter,
.FunctionAccessorMutableaddress,
.FunctionAccessorSetter,
.FunctionAccessorWillset,
.FunctionConstructor,
.FunctionDestructor,
.FunctionFree,
.FunctionMethodClass,
.FunctionMethodInstance,
.FunctionMethodStatic,
.FunctionOperator,
.FunctionSubscript
]
if !functionKinds.contains(kind) {
return []
}
if let offset = (dictionary["key.offset"] as? Int64).flatMap({ Int($0) }),
let bodyOffset = (dictionary["key.bodyoffset"] as? Int64).flatMap({ Int($0) }),
let bodyLength = (dictionary["key.bodylength"] as? Int64).flatMap({ Int($0) }) {
let location = Location(file: file, byteOffset: offset)
let startLine = file.contents.lineAndCharacterForByteOffset(bodyOffset)
let endLine = file.contents.lineAndCharacterForByteOffset(bodyOffset + bodyLength)
for parameter in [error, warning] {
let limit = parameter.value
if let startLine = startLine?.line, let endLine = endLine?.line
where exceedsLineCountExcludingComments(file, startLine, endLine, limit) {
return [StyleViolation(ruleDescription: self.dynamicType.description,
severity: parameter.severity,
location: location,
reason: "Function body should span \(warning.value) lines " +
"or less: currently spans \(endLine - startLine) lines")]
}
}
}
return []
}
}