Files
SwiftLint/Source/SwiftLintFramework/Rules/FileLengthRule.swift
T
Norio Nomura 5cc6d3243a On SourceKitd failed, continue linting the file by limited rules that does not use SourceKitd
Add `RuleDescription.needsSourceKit` indicating the rule needs SourceKit.
If `ASTRule` or `needsSourceKit` is true, skip linting if SourceKitd fails.
2016-04-13 18:40:25 +09:00

41 lines
1.3 KiB
Swift

//
// FileLengthRule.swift
// SwiftLint
//
// Created by JP Simard on 2015-05-16.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
public struct FileLengthRule: ConfigurationProviderRule {
public var configuration = SeverityLevelsConfiguration(warning: 400, error: 1000)
public init() {}
public static let description = RuleDescription(
identifier: "file_length",
name: "File Line Length",
description: "Files should not span too many lines.",
nonTriggeringExamples: [
Repeat(count: 400, repeatedValue: "//\n").joinWithSeparator("")
],
triggeringExamples: [
Repeat(count: 401, repeatedValue: "//\n").joinWithSeparator("")
],
needsSourceKit: false
)
public func validateFile(file: File) -> [StyleViolation] {
let lineCount = file.lines.count
for parameter in configuration.params where lineCount > parameter.value {
return [StyleViolation(ruleDescription: self.dynamicType.description,
severity: parameter.severity,
location: Location(file: file.path, line: lineCount),
reason: "File should contain \(configuration.warning) lines or less: " +
"currently contains \(lineCount)")]
}
return []
}
}