forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultilineParametersRule.swift
65 lines (55 loc) · 2.23 KB
/
MultilineParametersRule.swift
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
//
// 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)
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 []
}
var numberOfParameters = 0
var linesWithParameters = Set<Int>()
for structure in dictionary.substructure {
guard
let structureOffset = structure.offset,
let structureKind = structure.kind, SwiftDeclarationKind(rawValue: structureKind) == .varParameter,
let (line, _) = file.contents.bridge().lineAndCharacter(forByteOffset: structureOffset),
offset..<(offset + length) ~= structureOffset
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))]
}
}