Skip to content
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

Merged
merged 3 commits into from
Oct 23, 2017

Conversation

ornithocoder
Copy link
Contributor

Fixes #1912.

@SwiftLintBot
Copy link

SwiftLintBot commented Oct 18, 2017

1 Warning
⚠️ This PR may need tests.
12 Messages
📖 Linting Aerial with this PR took 0.44s vs 0.39s on master (12% slower)
📖 Linting Alamofire with this PR took 3.25s vs 3.26s on master (0% faster)
📖 Linting Firefox with this PR took 12.68s vs 12.99s on master (2% faster)
📖 Linting Kickstarter with this PR took 20.42s vs 20.61s on master (0% faster)
📖 Linting Moya with this PR took 1.53s vs 1.58s on master (3% faster)
📖 Linting Nimble with this PR took 1.89s vs 1.94s on master (2% faster)
📖 Linting Quick with this PR took 0.54s vs 0.57s on master (5% faster)
📖 Linting Realm with this PR took 3.45s vs 3.5s on master (1% faster)
📖 Linting SourceKitten with this PR took 1.08s vs 1.09s on master (0% faster)
📖 Linting Sourcery with this PR took 4.6s vs 4.62s on master (0% faster)
📖 Linting Swift with this PR took 14.6s vs 14.84s on master (1% faster)
📖 Linting WordPress with this PR took 12.59s vs 12.71s on master (0% faster)

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,
Copy link
Collaborator

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 }) { }

Copy link
Contributor Author

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?

Copy link
Collaborator

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?

Copy link
Contributor Author

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 :-)

Copy link
Contributor Author

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.

Copy link
Contributor Author

@ornithocoder ornithocoder Oct 19, 2017

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| | |
|        |---------| |---------| |----------------------------| |
----------------------------------------------------------------|

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| | |
|        |---------| |---------| |----------------------------| |
----------------------------------------------------------------|
@marcelofabri marcelofabri merged commit 03f1a2f into realm:master Oct 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants