mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
b83e0991b9
The MIT license doesn't require that all files be prepended with this licensing or copyright information. Realm confirmed that they're ok with this change. This will enable some companies to contribute to SwiftLint and the date & authorship information will remain accessible via git source control.
42 lines
1.9 KiB
Swift
42 lines
1.9 KiB
Swift
import SourceKittenFramework
|
|
|
|
public struct FunctionBodyLengthRule: ASTRule, ConfigurationProviderRule {
|
|
public var configuration = SeverityLevelsConfiguration(warning: 40, error: 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.",
|
|
kind: .metrics
|
|
)
|
|
|
|
public func validate(file: File, kind: SwiftDeclarationKind,
|
|
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
|
|
guard SwiftDeclarationKind.functionKinds.contains(kind),
|
|
let offset = dictionary.offset,
|
|
let bodyOffset = dictionary.bodyOffset,
|
|
let bodyLength = dictionary.bodyLength,
|
|
case let contentsNSString = file.contents.bridge(),
|
|
let startLine = contentsNSString.lineAndCharacter(forByteOffset: bodyOffset)?.line,
|
|
let endLine = contentsNSString.lineAndCharacter(forByteOffset: bodyOffset + bodyLength)?.line
|
|
else {
|
|
return []
|
|
}
|
|
for parameter in configuration.params {
|
|
let (exceeds, lineCount) = file.exceedsLineCountExcludingCommentsAndWhitespace(
|
|
startLine, endLine, parameter.value
|
|
)
|
|
guard exceeds else { continue }
|
|
return [StyleViolation(ruleDescription: type(of: self).description,
|
|
severity: parameter.severity,
|
|
location: Location(file: file, byteOffset: offset),
|
|
reason: "Function body should span \(configuration.warning) lines or less " +
|
|
"excluding comments and whitespace: currently spans \(lineCount) " +
|
|
"lines")]
|
|
}
|
|
return []
|
|
}
|
|
}
|