forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultilineParametersRule.swift
More file actions
84 lines (69 loc) · 2.83 KB
/
MultilineParametersRule.swift
File metadata and controls
84 lines (69 loc) · 2.83 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// MultilineParametersRule.swift
// SwiftLint
//
// Created by Ornithologist Coder on 22/05/17.
// Copyright © 2017 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct MultilineParametersRule: ASTRule, OptInRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)
private typealias ParameterRange = (offset: Int, length: Int)
public init() {}
public static let description = RuleDescription(
identifier: "multiline_parameters",
name: "Multiline Parameters",
description: "Functions and methods parameters should be either on the same line, or one per line.",
kind: .style,
nonTriggeringExamples: MultilineParametersRuleExamples.nonTriggeringExamples,
triggeringExamples: MultilineParametersRuleExamples.triggeringExamples
)
public func validate(file: File,
kind: SwiftDeclarationKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard
SwiftDeclarationKind.functionKinds.contains(kind),
let offset = dictionary.nameOffset,
let length = dictionary.nameLength
else {
return []
}
let parameterRanges = dictionary.substructure.flatMap { subStructure -> ParameterRange? in
guard
let offset = subStructure.offset,
let length = subStructure.length,
let kind = subStructure.kind, SwiftDeclarationKind(rawValue: kind) == .varParameter
else {
return nil
}
return (offset, length)
}
var numberOfParameters = 0
var linesWithParameters = Set<Int>()
for range in parameterRanges {
guard
let (line, _) = file.contents.bridge().lineAndCharacter(forByteOffset: range.offset),
offset..<(offset + length) ~= range.offset,
isRange(range, withinRanges: parameterRanges)
else {
continue
}
linesWithParameters.insert(line)
numberOfParameters += 1
}
guard
linesWithParameters.count > 1,
numberOfParameters != linesWithParameters.count
else {
return []
}
return [StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset))]
}
// MARK: - Private
private func isRange(_ range: ParameterRange, withinRanges ranges: [ParameterRange]) -> Bool {
return ranges.filter { $0 != range && ($0.offset..<($0.offset + $0.length)).contains(range.offset) }.isEmpty
}
}