Skip to content

Commit

Permalink
Merge pull request #3281 from realm/marcelo/bugfix-3225
Browse files Browse the repository at this point in the history
Trigger closure_parameter_position in free closures and capture lists
  • Loading branch information
marcelofabri authored Aug 5, 2020
2 parents 5a817b0 + 6ec5806 commit 8638d23
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#3186](https://github.com/realm/SwiftLint/issues/3186)

* `closure_parameter_position` now triggers in closures that are not inside a
function call and also validates captured variables.
[Marcelo Fabri](https://github.com/marcelofabri)
[#3225](https://github.com/realm/SwiftLint/issues/3225)

## 0.39.2: Stay Home

This is the last release to support building with Swift 5.0.x.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,81 @@ public struct ClosureParameterPositionRule: ASTRule, ConfigurationProviderRule,
""")
],
triggeringExamples: [
Example("[1, 2].map {\n ↓number in\n number + 1 \n}\n"),
Example("[1, 2].map {\n ↓number -> Int in\n number + 1 \n}\n"),
Example("[1, 2].map {\n (↓number: Int) -> Int in\n number + 1 \n}\n"),
Example("[1, 2].map {\n [weak self] ↓number in\n number + 1 \n}\n"),
Example("[1, 2].map { [weak self]\n ↓number in\n number + 1 \n}\n"),
Example("[1, 2].map({\n ↓number in\n number + 1 \n})\n"),
Example("[1, 2].something(closure: {\n ↓number in\n number + 1 \n})\n"),
Example("[1, 2].reduce(0) {\n ↓sum, ↓number in\n number + sum \n}\n")
Example("""
[1, 2].map {
↓number in
number + 1
}
"""),
Example("""
[1, 2].map {
↓number -> Int in
number + 1
}
"""),
Example("""
[1, 2].map {
(↓number: Int) -> Int in
number + 1
}
"""),
Example("""
[1, 2].map {
[weak ↓self] ↓number in
number + 1
}
"""),
Example("""
[1, 2].map { [weak self]
↓number in
number + 1
}
"""),
Example("""
[1, 2].map({
↓number in
number + 1
})
"""),
Example("""
[1, 2].something(closure: {
↓number in
number + 1
})
"""),
Example("""
[1, 2].reduce(0) {
↓sum, ↓number in
number + sum
})
"""),
Example("""
f.completionHandler = {
↓thing in
doStuff()
}
"""),
Example("""
foo {
[weak ↓self] in
self?.bar()
}
""")
]
)

private static let openBraceRegex = regex("\\{")

public func validate(file: SwiftLintFile) -> [StyleViolation] {
return file.structureDictionary.traverseDepthFirst { subDict in
guard let kind = self.kind(from: subDict) else { return nil }
return validate(file: file, kind: kind, dictionary: subDict)
}.unique.sorted(by: { $0.location < $1.location })
}

public func validate(file: SwiftLintFile, kind: SwiftExpressionKind,
dictionary: SourceKittenDictionary) -> [StyleViolation] {
guard kind == .call else {
guard kind == .closure || kind == .call else {
return []
}

Expand All @@ -61,7 +120,8 @@ public struct ClosureParameterPositionRule: ASTRule, ConfigurationProviderRule,
return []
}

let parameters = dictionary.enclosedVarParameters
let parameters = dictionary.enclosedVarParameters +
dictionary.substructure.filter { $0.declarationKind == .varLocal } // capture lists
let rangeStart = nameOffset + nameLength
let regex = ClosureParameterPositionRule.openBraceRegex

Expand Down

0 comments on commit 8638d23

Please sign in to comment.