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

Excluding Multi-line if/guard/while Conditions From Opening Brace Rule #365

Merged
merged 9 commits into from
Feb 3, 2016

Conversation

scottrhoyt
Copy link
Contributor

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.

@scottrhoyt
Copy link
Contributor Author

This might be a slightly better regex: ((?:if|guard|while)\n(?:[^\{]+\n)+\{).

@scottrhoyt scottrhoyt force-pushed the sh-opening-brace-exception branch from 138750d to 53bbabb Compare February 2, 2016 19:29
@scottrhoyt
Copy link
Contributor Author

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

  • exclusion regex correctness (mostly looking for false negatives, since it's function is one of subtraction)
  • implementation style
  • performance costs

I plan on checking these again myself. Thanks!

@@ -138,6 +138,22 @@ extension File {
}.map { $0.0 }
}

public func matchPattern(pattern: String,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

internal?

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

@jpsim
Copy link
Collaborator

jpsim commented Feb 2, 2016

Seems quite reasonable to me, although this should really be benchmarked to see how expensive it is.

@scottrhoyt
Copy link
Contributor Author

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.

@jpsim
Copy link
Collaborator

jpsim commented Feb 2, 2016

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.

@scottrhoyt
Copy link
Contributor Author

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.

@scottrhoyt
Copy link
Contributor Author

@jpsim, for autocorrect I decided to add corrections to test, but not to modify the existing behavior.

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?

@jpsim
Copy link
Collaborator

jpsim commented Feb 3, 2016

I think sticking with the default behavior is fine, but I'm happy it's now tested!

@scottrhoyt
Copy link
Contributor Author

Cool. I'll get this rebased and then good to go I think.

@scottrhoyt scottrhoyt changed the title [WIP] Excluding Multi-line if/guard/while Conditions From Opening Brace Rule Excluding Multi-line if/guard/while Conditions From Opening Brace Rule Feb 3, 2016
@scottrhoyt scottrhoyt force-pushed the sh-opening-brace-exception branch from bd9f179 to a723e14 Compare February 3, 2016 01:16
@jpsim
Copy link
Collaborator

jpsim commented Feb 3, 2016

👍 awesome, thanks @scottrhoyt!

jpsim added a commit that referenced this pull request Feb 3, 2016
Excluding Multi-line if/guard/while Conditions From Opening Brace Rule
@jpsim jpsim merged commit 491cf79 into master Feb 3, 2016
@jpsim jpsim deleted the sh-opening-brace-exception branch February 3, 2016 01:34
@watt
Copy link

watt commented Feb 11, 2016

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!

@scottrhoyt
Copy link
Contributor Author

That should be a pretty easy modification of the exclusion regex to allow an optional let between the (if|gaurd|while) and the newline, and an optional else before the {. But be sure to benchmark the performance on a couple of repos. I benchmarked the initial change on a single repo with little difference, but there was a huge regression on another repo.

@dimazen
Copy link

dimazen commented Apr 26, 2016

Any updates on it?

@jpsim
Copy link
Collaborator

jpsim commented Apr 26, 2016

This was shipped with 0.8.0: https://github.com/realm/SwiftLint/releases/tag/0.8.0

@dimazen
Copy link

dimazen commented Apr 26, 2016

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

@jpsim
Copy link
Collaborator

jpsim commented Apr 26, 2016

Judging by the git history for that rule, your request hasn't been implemented yet.

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.

4 participants