Skip to content

Commit

Permalink
Fix false positive in function_default_parameter_at_end
Browse files Browse the repository at this point in the history
Fixes #2788
  • Loading branch information
marcelofabri committed Jul 5, 2019
1 parent bb1a012 commit 94c6009
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#2792](https://github.com/realm/SwiftLint/issues/2792)

* Fix false positive in `function_default_parameter_at_end` rule when using
a closure parameter with default value.
[Marcelo Fabri](https://github.com/marcelofabri)
[#2788](https://github.com/realm/SwiftLint/issues/2788)

## 0.33.0: Worldwide Dryers Conference

#### Breaking
Expand Down
5 changes: 5 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -8399,6 +8399,11 @@ func foo(a: Int, b: CGFloat = 0) {
}
```

```swift
func foo(a: String, b: String? = nil,
c: String? = nil, d: @escaping AlertActionHandler = { _ in }) {}
```

</details>
<details>
<summary>Triggering Examples</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public struct FunctionDefaultParameterAtEndRule: ASTRule, ConfigurationProviderR
func foo(a: Int, b: CGFloat = 0) {
let block = { (error: Error?) in }
}
""",
"""
func foo(a: String, b: String? = nil,
c: String? = nil, d: @escaping AlertActionHandler = { _ in }) {}
"""
],
triggeringExamples: [
Expand All @@ -43,13 +47,22 @@ public struct FunctionDefaultParameterAtEndRule: ASTRule, ConfigurationProviderR
}

let isNotClosure = { !self.isClosureParameter(dictionary: $0) }
let params = dictionary.enclosedVarParameters.filter(isNotClosure).filter { param in
guard let paramOffset = param.offset else {
return false
let params = dictionary.substructure
.flatMap { subDict -> [[String: SourceKitRepresentable]] in
guard subDict.kind.flatMap(SwiftDeclarationKind.init) == .varParameter else {
return []
}

return [subDict]
}
.filter(isNotClosure)
.filter { param in
guard let paramOffset = param.offset else {
return false
}

return paramOffset < bodyOffset
}
return paramOffset < bodyOffset
}

guard !params.isEmpty else {
return []
Expand Down

0 comments on commit 94c6009

Please sign in to comment.