|
| 1 | +// |
| 2 | +// ClosureBodyLengthRule.swift |
| 3 | +// SwiftLint |
| 4 | +// |
| 5 | +// Created by Ornithologist Coder on 8/3/17. |
| 6 | +// Copyright © 2017 Realm. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import SourceKittenFramework |
| 11 | + |
| 12 | +public struct ClosureBodyLengthRule: ASTRule, OptInRule, ConfigurationProviderRule { |
| 13 | + public var configuration = SeverityLevelsConfiguration(warning: 20, error: 100) |
| 14 | + |
| 15 | + public init() {} |
| 16 | + |
| 17 | + public static let description = RuleDescription( |
| 18 | + identifier: "closure_body_length", |
| 19 | + name: "Closure Body Length", |
| 20 | + description: "Closure bodies should not span too many lines.", |
| 21 | + kind: .metrics, |
| 22 | + nonTriggeringExamples: [ |
| 23 | + "foo.bar { $0 }", |
| 24 | + "foo.bar { value in\n" + repeatElement("\tprint(\"toto\")\n", count: 19).joined() + "\treturn value\n}", |
| 25 | + "foo.bar { value in\n\n" + repeatElement("\tprint(\"toto\")\n", count: 19).joined() + "\n\treturn value\n}" |
| 26 | + ], |
| 27 | + triggeringExamples: [ |
| 28 | + "foo.bar {↓ value in\n" + repeatElement("\tprint(\"toto\")\n", count: 20).joined() + "\treturn value\n}", |
| 29 | + "foo.bar {↓ value in\n\n" + repeatElement("\tprint(\"toto\")\n", count: 20).joined() + "\n\treturn value\n}" |
| 30 | + ] |
| 31 | + ) |
| 32 | + |
| 33 | + public func validate(file: File, |
| 34 | + kind: SwiftExpressionKind, |
| 35 | + dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { |
| 36 | + guard |
| 37 | + kind == .call, |
| 38 | + let bodyOffset = dictionary.bodyOffset, |
| 39 | + let bodyLength = dictionary.bodyLength, |
| 40 | + case let contents = file.contents.bridge(), |
| 41 | + contents.substringWithByteRange(start: bodyOffset - 1, length: 1) == "{", |
| 42 | + let startLine = contents.lineAndCharacter(forByteOffset: bodyOffset)?.line, |
| 43 | + let endLine = contents.lineAndCharacter(forByteOffset: bodyOffset + bodyLength)?.line |
| 44 | + else { |
| 45 | + return [] |
| 46 | + } |
| 47 | + |
| 48 | + return configuration.params.flatMap { parameter in |
| 49 | + let (exceeds, lineCount) = file.exceedsLineCountExcludingCommentsAndWhitespace(startLine, |
| 50 | + endLine, |
| 51 | + parameter.value) |
| 52 | + guard exceeds else { return nil } |
| 53 | + |
| 54 | + return StyleViolation(ruleDescription: type(of: self).description, |
| 55 | + severity: parameter.severity, |
| 56 | + location: Location(file: file, byteOffset: bodyOffset), |
| 57 | + reason: "Closure body should span \(configuration.warning) lines or less " + |
| 58 | + "excluding comments and whitespace: currently spans \(lineCount) " + "lines") |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments