Files
SwiftLint/Source/SwiftLintFramework/Rules/FileLengthRule.swift
T
JP Simard b83e0991b9 Remove all file headers
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.
2018-05-04 13:42:02 -07:00

52 lines
1.9 KiB
Swift

import SourceKittenFramework
public struct FileLengthRule: ConfigurationProviderRule {
public var configuration = FileLengthRuleConfiguration(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.",
kind: .metrics,
nonTriggeringExamples: [
repeatElement("print(\"swiftlint\")\n", count: 400).joined()
],
triggeringExamples: [
repeatElement("print(\"swiftlint\")\n", count: 401).joined(),
(repeatElement("print(\"swiftlint\")\n", count: 400) + ["//\n"]).joined()
]
)
public func validate(file: File) -> [StyleViolation] {
func lineCountWithoutComments() -> Int {
let commentKinds = SyntaxKind.commentKinds
let lineCount = file.syntaxKindsByLines.filter { kinds in
return !Set(kinds).isSubset(of: commentKinds)
}.count
return lineCount
}
var lineCount = file.lines.count
let hasViolation = configuration.severityConfiguration.params.contains {
$0.value < lineCount
}
if hasViolation && configuration.ignoreCommentOnlyLines {
lineCount = lineCountWithoutComments()
}
for parameter in configuration.severityConfiguration.params where lineCount > parameter.value {
let reason = "File should contain \(configuration.severityConfiguration.warning) lines or less: " +
"currently contains \(lineCount)"
return [StyleViolation(ruleDescription: type(of: self).description,
severity: parameter.severity,
location: Location(file: file.path, line: lineCount),
reason: reason)]
}
return []
}
}