-
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
Excluding Multi-line if/guard/while Conditions From Opening Brace Rule #365
Conversation
This might be a slightly better regex: |
138750d
to
53bbabb
Compare
@realm/swiftlint Just rebased this. I don't want to leave this WIP PR out there much longer. Any thoughts on implementation? As a recap, I initially tried to implement this by modifying the original regex but was unsuccessful. I then switched to an implementation that finds matches for the original regex, then finds matches for a new regex that is designed to detect the multi-line if/while/guard and excludes those from the original matches. Primary areas of feedback are:
I plan on checking these again myself. Thanks! |
@@ -138,6 +138,22 @@ extension File { | |||
}.map { $0.0 } | |||
} | |||
|
|||
public func matchPattern(pattern: String, |
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.
internal
?
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. Thanks.
Seems quite reasonable to me, although this should really be benchmarked to see how expensive it is. |
Just benchmarked 5x vs. master on Carthage. Difference in performance was < 0.5% with ~ 1% stdev in the results. So no significant change in performance by my measure, though I wouldn't mind a second pair of eyes. |
Could you add some triggering examples? Multi-line variants of the existing ones should be fine. Have you looked into expanding the autocorrect scope to support multiline statements like these? It's fine if that's out of scope for this PR, but I'd like to make sure we're not leaving potentially low hanging fruit on the tree. |
Definitely. I'll add the triggering examples, and take a look at autocorrect. I guess now is as good a time as any to dive into that side of things. |
@jpsim, for autocorrect I decided to add This... if
let a = b,
let c = d{
// do something
} ...will be corrected to this... if
let a = b,
let c = d {
// do something
} ...as was the behavior before. Do you think we want to force this on people in autocorrect? if
let a = b,
let c = d
{
// do something
} Or do we want that to be a personal style decision? |
I think sticking with the default behavior is fine, but I'm happy it's now tested! |
Cool. I'll get this rebased and then good to go I think. |
…and NSRange extensions to support. Used this in OpeningBraceRule.
bd9f179
to
a723e14
Compare
👍 awesome, thanks @scottrhoyt! |
Excluding Multi-line if/guard/while Conditions From Opening Brace Rule
Crap, I missed all the notifications for this, sorry @jpsim. I will likely still look into making a PR to support syntax like the following: if let
foo = foo,
bar = foo.bar
{
} As well as guard statements like this: guard let
foo = foo,
bar = foo.bar
else {
} But this is a huge step in the right direction, so thanks for doing this! |
That should be a pretty easy modification of the exclusion regex to allow an optional |
Any updates on it? |
This was shipped with 0.8.0: https://github.com/realm/SwiftLint/releases/tag/0.8.0 |
@jpsim sorry, i was meant syntax, mentioned by Watt above: if let
foo = foo,
bar = bar
{
} Running 0.9.2 and Swiftlint complains about opening brace for similar syntax :) |
Judging by the git history for that rule, your request hasn't been implemented yet. |
Closes #355
/cc @watt
Here is my first naive take on how to accomplish this. I'm not sure this is the right way to do this. I would love some help testing for correctness and performance implications.
@jpsim I'm sure you can bring some functional goodness to this.