mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
fcf848608e
* Add Example wrapper in order to display test failures inline when running in Xcode. * Stop using Swift 5.1-only features so we can compile on Xcode 10.2. * Wrap strings in Example. * Add Changelog entry. * Wrap all examples in Example struct. * Better and more complete capturing of line numbers. * Fix broken test. * Better test traceability. * Address or disable linting warnings. * Add documentation comments. * Disable linter for a few cases. * Limit mutability and add copy-and-mutate utility functions. * Limit scope of mutability.
52 lines
2.0 KiB
Swift
52 lines
2.0 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: [
|
|
Example(repeatElement("print(\"swiftlint\")\n", count: 400).joined())
|
|
],
|
|
triggeringExamples: [
|
|
Example(repeatElement("print(\"swiftlint\")\n", count: 401).joined()),
|
|
Example((repeatElement("print(\"swiftlint\")\n", count: 400) + ["//\n"]).joined())
|
|
]
|
|
)
|
|
|
|
public func validate(file: SwiftLintFile) -> [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 []
|
|
}
|
|
}
|