-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix false positive in multiline_parameters
rule when parameter is a closure has default value
#1913
Fix false positive in multiline_parameters
rule when parameter is a closure has default value
#1913
Conversation
Generated by 🚫 Danger |
@@ -40,6 +40,7 @@ public struct MultilineParametersRule: ASTRule, OptInRule, ConfigurationProvider | |||
for structure in dictionary.substructure { | |||
guard | |||
let structureOffset = structure.offset, | |||
structure.typeName != nil, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is enough to fix the issue, as you can explicit use a type for the closure parameter:
func foo(param1: Int,
param2: Bool,
param3: @escaping (Int) -> Void = { (x: Int) in }) { }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. You're right. :'-( Do you see a not-so-dirty way to filter out these parameters within parameters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can’t think about anything right now, but I vaguely remind dealing with this before. Maybe one or the rules has this implemented?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take a look. Thanks :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can check if the offset of the parameter isn't in the range of other parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. I've pushed another commit that checks the range of the parameter. If the parameter is in the bounds of another parameter, it rejects it. Something like this: a 1, a 2, and a 3 would be filtered out since they're inside parameter 3.
dictionary.substructure varParameters
----------------------------------------------------------------|
| |---------| |---------| |----------------------------| |
| | param 1 | | param 2 | | param 3: |a 1| |a 2| |a 3| | |
| |---------| |---------| |----------------------------| |
----------------------------------------------------------------|
8c95209
to
a4a1297
Compare
when parameter is a closure has default value. Fixes realm#1912.
For a function that takes a closure as default value, the array of parameter reported by sourcekit also includes the parameters of that closure. In other words, a function like func foo(param1: Int, param2: Bool, param3: (Int) -> Void = { (x: Int) in }) { } reports 4 parameters (param 1, param2, param3, and x). This commit uses the bounds of the method/function parameters to filter out parameters within parameters. Below, a 1, a 2, and a 3 would be filtered out since they're inside parameter 3. ----------------------------------------------------------------| | |---------| |---------| |----------------------------| | | | param 1 | | param 2 | | param 3: |a 1| |a 2| |a 3| | | | |---------| |---------| |----------------------------| | ----------------------------------------------------------------|
a4a1297
to
d387532
Compare
Fixes #1912.