forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnneededParenthesesInClosureArgumentRule.swift
96 lines (83 loc) · 4.03 KB
/
UnneededParenthesesInClosureArgumentRule.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
//
// UnneededParenthesesInClosureArgumentRule.swift
// SwiftLint
//
// Created by Marcelo Fabri on 07/17/17.
// Copyright © 2017 Realm. All rights reserved.
//
import Foundation
import Foundation
import SourceKittenFramework
public struct UnneededParenthesesInClosureArgumentRule: ConfigurationProviderRule, CorrectableRule, OptInRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "unneeded_parentheses_in_closure_argument",
name: "Unneeded Parentheses in Closure Argument",
description: "Parentheses are not needed when declaring closure arguments.",
kind: .style,
nonTriggeringExamples: [
"let foo = { (bar: Int) in }\n",
"let foo = { bar in }\n",
"let foo = { bar -> Bool in return true }\n"
],
triggeringExamples: [
"call(arg: { ↓(bar) in })\n",
"let foo = { ↓(bar) -> Bool in return true }\n"
],
corrections: [
"call(arg: { ↓(bar) in })\n": "call(arg: { bar in })\n",
"let foo = { ↓(bar) -> Bool in return true }\n": "let foo = { bar -> Bool in return true }\n",
"method { ↓(foo, bar) in }\n": "method { foo, bar in }\n"
]
)
public func validate(file: File) -> [StyleViolation] {
return violationRanges(file: file).map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
}
}
private func violationRanges(file: File) -> [NSRange] {
let pattern = "\\{\\s*(\\([^:]+\\))\\s*(in|->)"
let contents = file.contents.bridge()
let range = NSRange(location: 0, length: contents.length)
return regex(pattern).matches(in: file.contents, options: [], range: range).flatMap { match -> NSRange? in
let parametersRange = match.range(at: 1)
let inRange = match.range(at: 2)
guard let parametersByteRange = contents.NSRangeToByteRange(start: parametersRange.location,
length: parametersRange.length),
let inByteRange = contents.NSRangeToByteRange(start: inRange.location,
length: inRange.length) else {
return nil
}
let parametersKinds = Set(file.syntaxMap.kinds(inByteRange: parametersByteRange))
let inKinds = Set(file.syntaxMap.kinds(inByteRange: inByteRange))
guard parametersKinds == [.identifier],
inKinds.isEmpty || inKinds == [.keyword] else {
return nil
}
return parametersRange
}
}
public func correct(file: File) -> [Correction] {
let violatingRanges = file.ruleEnabled(violatingRanges: violationRanges(file: file), for: self)
var correctedContents = file.contents
var adjustedLocations = [Int]()
for violatingRange in violatingRanges.reversed() {
let correctingRange = NSRange(location: violatingRange.location + 1,
length: violatingRange.length - 2)
if let indexRange = correctedContents.nsrangeToIndexRange(violatingRange),
let updatedRange = correctedContents.nsrangeToIndexRange(correctingRange) {
let updatedArguments = correctedContents.substring(with: updatedRange)
correctedContents = correctedContents.replacingCharacters(in: indexRange, with: updatedArguments)
adjustedLocations.insert(violatingRange.location, at: 0)
}
}
file.write(correctedContents)
return adjustedLocations.map {
Correction(ruleDescription: type(of: self).description,
location: Location(file: file, characterOffset: $0))
}
}
}