forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VerticalParameterAlignmentOnCallRule.swift
151 lines (134 loc) · 5.55 KB
/
VerticalParameterAlignmentOnCallRule.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// VerticalParameterAlignmentOnCallRule.swift
// SwiftLint
//
// Created by Marcelo Fabri on 02/05/17.
// Copyright © 2017 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct VerticalParameterAlignmentOnCallRule: ASTRule, ConfigurationProviderRule, OptInRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "vertical_parameter_alignment_on_call",
name: "Vertical Parameter Alignment On Call",
description: "Function parameters should be aligned vertically if they're in multiple lines in a method call.",
kind: .style,
nonTriggeringExamples: [
"foo(param1: 1, param2: bar\n" +
" param3: false, param4: true)",
"foo(param1: 1, param2: bar)",
"foo(param1: 1, param2: bar\n" +
" param3: false,\n" +
" param4: true)",
"foo(\n" +
" param1: 1\n" +
") { _ in }",
"UIView.animate(withDuration: 0.4, animations: {\n" +
" blurredImageView.alpha = 1\n" +
"}, completion: { _ in\n" +
" self.hideLoading()\n" +
"})",
"UIView.animate(withDuration: 0.4, animations: {\n" +
" blurredImageView.alpha = 1\n" +
"},\n" +
"completion: { _ in\n" +
" self.hideLoading()\n" +
"})",
"foo(param1: 1, param2: { _ in },\n" +
" param3: false, param4: true)",
"foo({ _ in\n" +
" bar()\n" +
" },\n" +
" completion: { _ in\n" +
" baz()\n" +
" }\n" +
")",
"foo(param1: 1, param2: [\n" +
" 0,\n" +
" 1\n" +
"], param3: 0)"
],
triggeringExamples: [
"foo(param1: 1, param2: bar\n" +
" ↓param3: false, param4: true)",
"foo(param1: 1, param2: bar\n" +
" ↓param3: false, param4: true)",
"foo(param1: 1, param2: bar\n" +
" ↓param3: false,\n" +
" ↓param4: true)",
"foo(param1: 1,\n" +
" ↓param2: { _ in })",
"foo(param1: 1,\n" +
" param2: { _ in\n" +
"}, param3: 2,\n" +
" ↓param4: 0)",
"foo(param1: 1, param2: { _ in },\n" +
" ↓param3: false, param4: true)"
]
)
public func validate(file: File, kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard kind == .call,
case let arguments = dictionary.enclosedArguments,
arguments.count > 1,
let firstArgumentOffset = arguments.first?.offset,
case let contents = file.contents.bridge(),
var firstArgumentPosition = contents.lineAndCharacter(forByteOffset: firstArgumentOffset) else {
return []
}
var visitedLines = Set<Int>()
var previousArgumentWasMultiline = false
let lastIndex = arguments.count - 1
let violatingOffsets: [Int] = arguments.enumerated().flatMap { idx, argument in
defer {
previousArgumentWasMultiline = isMultiline(argument: argument, file: file)
}
guard let offset = argument.offset,
let (line, character) = contents.lineAndCharacter(forByteOffset: offset),
line > firstArgumentPosition.line else {
return nil
}
let (firstVisit, _) = visitedLines.insert(line)
guard character != firstArgumentPosition.character && firstVisit else {
return nil
}
// if this is the first element on a new line after a closure with multiple lines,
// we reset the reference position
if previousArgumentWasMultiline && firstVisit {
firstArgumentPosition = (line, character)
return nil
}
// never trigger on a trailing closure
if idx == lastIndex, isTrailingClosure(dictionary: dictionary, file: file) {
return nil
}
return offset
}
return violatingOffsets.map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: $0))
}
}
private func isMultiline(argument: [String: SourceKitRepresentable], file: File) -> Bool {
guard let offset = argument.bodyOffset,
let length = argument.bodyLength,
case let contents = file.contents.bridge(),
let (startLine, _) = contents.lineAndCharacter(forByteOffset: offset),
let (endLine, _) = contents.lineAndCharacter(forByteOffset: offset + length) else {
return false
}
return endLine > startLine
}
private func isTrailingClosure(dictionary: [String: SourceKitRepresentable], file: File) -> Bool {
guard let offset = dictionary.offset,
let length = dictionary.length,
case let start = min(offset, offset + length - 1),
let text = file.contents.bridge().substringWithByteRange(start: start, length: length) else {
return false
}
return !text.hasSuffix(")")
}
}