Skip to content

Commit d68cb1b

Browse files
committed
Create typealias for closure bounds
It's easier to read than a long tuple.
1 parent e526bff commit d68cb1b

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
`redundant_void_return` rule.
3131
[Ryan Booker](https://github.com/ryanbooker)
3232
[#1761](https://github.com/realm/SwiftLint/issues/1761)
33+
3334
* Add `closure_body_length` rule to enforce the maximum number of lines
3435
a closure should have.
3536
[Ornithologist Coder](https://github.com/ornithocoder)

Source/SwiftLintFramework/Rules/ClosureBodyLengthRule.swift

+8-6
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ public struct ClosureBodyLengthRule: ASTRule, OptInRule, ConfigurationProviderRu
2323
triggeringExamples: ClosureBodyLengthRuleExamples.triggeringExamples
2424
)
2525

26+
private typealias ClosureBounds = (offset: Int, startLine: Int, endLine: Int)
27+
2628
public func validate(file: File,
2729
kind: SwiftExpressionKind,
2830
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
2931
guard kind == .call else { return [] }
3032

3133
return findClosures(in: dictionary)
32-
.flatMap { closureDictionary -> (Int, Int, Int)? in
34+
.flatMap { closureDictionary -> ClosureBounds? in
3335
guard
3436
let bodyOffset = closureDictionary.bodyOffset,
3537
let bodyLength = closureDictionary.bodyLength,
@@ -38,12 +40,12 @@ public struct ClosureBodyLengthRule: ASTRule, OptInRule, ConfigurationProviderRu
3840
let endLine = contents.lineAndCharacter(forByteOffset: bodyOffset + bodyLength)?.line
3941
else { return nil }
4042

41-
return (offset: bodyOffset, startLine: startLine, endLine: endLine)
43+
return ClosureBounds(offset: bodyOffset, startLine: startLine, endLine: endLine)
4244
}
43-
.flatMap { offset, startLine, endLine -> [StyleViolation] in
45+
.flatMap { closureBounds -> [StyleViolation] in
4446
return configuration.params.flatMap { parameter -> StyleViolation? in
45-
let (exceeds, count) = file.exceedsLineCountExcludingCommentsAndWhitespace(startLine,
46-
endLine,
47+
let (exceeds, count) = file.exceedsLineCountExcludingCommentsAndWhitespace(closureBounds.startLine,
48+
closureBounds.endLine,
4749
parameter.value)
4850
guard exceeds else { return nil }
4951

@@ -52,7 +54,7 @@ public struct ClosureBodyLengthRule: ASTRule, OptInRule, ConfigurationProviderRu
5254

5355
return StyleViolation(ruleDescription: type(of: self).description,
5456
severity: parameter.severity,
55-
location: Location(file: file, byteOffset: offset),
57+
location: Location(file: file, byteOffset: closureBounds.offset),
5658
reason: reason)
5759
}
5860
}

0 commit comments

Comments
 (0)