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

Closure end indentation rule #1004

Merged
merged 7 commits into from
Dec 20, 2016
Merged

Closure end indentation rule #1004

merged 7 commits into from
Dec 20, 2016

Conversation

marcelofabri
Copy link
Collaborator

@marcelofabri marcelofabri commented Dec 19, 2016

Fixes #326

This still have some issues:

  • It should be opt-in. It's not currently to make it easier to test.
  • Some false positives are being trigged, mainly related to opening brace on different line than the method call. For example:
return file.matchPattern(pattern, excludingSyntaxKinds: kinds,
                                          excludingPattern: excludingPattern).map {
    return StyleViolation(ruleDescription: type(of: self).description,
                          severity: configuration.severity,
                          location: Location(file: file, characterOffset: $0.location))
}
  • It should check (and skip) if the closure begins and ends on the same line, not the body offset. This should prevent this false violation:
 return file.matchPattern(pattern, excludingSyntaxKinds: kinds,
                                 excludingPattern: excludingPattern) { $0.rangeAt(1) }.flatMap {
            let parensRegex = NSRegularExpression.forcePattern(parensPattern)
            return parensRegex.firstMatch(in: file.contents, options: [], range: $0)?.range
}

The main issue here is that I'd need the starting point of the method related to the closure, not the full chained one (which is what SourceKits gives me). Any ideas on how to get it?

@jpsim
Copy link
Collaborator

jpsim commented Dec 19, 2016

I'm not sure I stylistically agree with the current semantics (the style changes you've already made). Would need to write down more explicit rules just in sentences/point-form explaining when to place closing braces and where.

@marcelofabri
Copy link
Collaborator Author

So, my interpretation of the comments in #326 was:

  • If the method call is chained breaking lines on each method (e.g. . is on a new line), the closing brace should be in the same indentation as the ..
  • Else, the closing brace should be in the same indentation as the beginning of the statement (in the first line).

@jpsim
Copy link
Collaborator

jpsim commented Dec 19, 2016

Yeah, that sounds good. It was hurting my head a bit to try to think of when you'd apply which rules... this is why we need machines to decide this for us! 😅

@marcelofabri
Copy link
Collaborator Author

🤖 > 👨‍💻

@jpsim
Copy link
Collaborator

jpsim commented Dec 19, 2016

🤖 > 👨‍💻

Is it too late to rename SwiftLint to this? 😂

But seriously, this is the exact opposite of the point I was trying to make in my SwiftLint presentation from last week (slides), that you shouldn't bend to the will of the machines and humans know better...

But there are times when the machines pay closer attention to details than we can.

@marcelofabri
Copy link
Collaborator Author

Maybe the issue is that it's harder to come up with logic rules to describe what "feels right". Our intuition is much more complex than I thought! I've learned the hard way when developing #847.

@jpsim
Copy link
Collaborator

jpsim commented Dec 19, 2016

Our intuition is much more complex than I thought

This. Though it is definable.

@marcelofabri marcelofabri changed the title [WIP] Closure end indentation rule Closure end indentation rule Dec 19, 2016
@codecov-io
Copy link

codecov-io commented Dec 20, 2016

Current coverage is 82.28% (diff: 96.87%)

Merging #1004 into master will increase coverage by 0.12%

@@             master      #1004   diff @@
==========================================
  Files           137        138     +1   
  Lines          6530       6587    +57   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits           5365       5420    +55   
- Misses         1165       1167     +2   
  Partials          0          0          

Powered by Codecov. Last update 7abbd84...ed5fdd8

@jpsim jpsim merged commit 990f1d1 into realm:master Dec 20, 2016
@jpsim
Copy link
Collaborator

jpsim commented Dec 20, 2016

🎉 thanks Marcelo!

@marcelofabri marcelofabri deleted the closure_end_indentation branch December 20, 2016 02:49
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