forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLengthRule.swift
More file actions
59 lines (50 loc) · 2.07 KB
/
FileLengthRule.swift
File metadata and controls
59 lines (50 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// FileLengthRule.swift
// SwiftLint
//
// Created by JP Simard on 5/16/15.
// Copyright © 2015 Realm. All rights reserved.
//
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 = Set(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 []
}
}