mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
74 lines
2.7 KiB
Swift
74 lines
2.7 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, ParameterizedRule {
|
|
public init() {
|
|
self.init(parameters: [
|
|
RuleParameter(severity: .Warning, value: 40),
|
|
RuleParameter(severity: .Error, value: 100)
|
|
])
|
|
}
|
|
|
|
public init(parameters: [RuleParameter<Int>]) {
|
|
self.parameters = parameters
|
|
}
|
|
|
|
public let parameters: [RuleParameter<Int>]
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "function_body_length",
|
|
name: "Function Body Length",
|
|
description: "Functions bodies should not span too many lines."
|
|
)
|
|
|
|
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, offset: offset)
|
|
let startLine = file.contents.lineAndCharacterForByteOffset(bodyOffset)
|
|
let endLine = file.contents.lineAndCharacterForByteOffset(bodyOffset + bodyLength)
|
|
for parameter in parameters.reverse() {
|
|
if let startLine = startLine?.line, let endLine = endLine?.line
|
|
where endLine - startLine > parameter.value {
|
|
return [StyleViolation(ruleDescription: self.dynamicType.description,
|
|
severity: parameter.severity,
|
|
location: location,
|
|
reason: "Function body should be span \(parameters.first!.value) lines " +
|
|
"or less: currently spans \(endLine - startLine) lines")]
|
|
}
|
|
}
|
|
}
|
|
return []
|
|
}
|
|
}
|