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

Add multiline closure argument rule #218

Closed
wants to merge 2 commits into from
Closed

Add multiline closure argument rule #218

wants to merge 2 commits into from

Conversation

keith
Copy link
Collaborator

@keith keith commented Nov 18, 2015

This rule forces users to name arguments for multiline closures.

This is the first draft of #70

See the first commit for the rule. The second is just fixes for it.

@segiddins
Copy link
Contributor

if at all possible, it might be preferable to make this single statement closures instead of single-line

@jpsim
Copy link
Collaborator

jpsim commented Nov 18, 2015

Lol, of course the rule would violate itself:

/Users/travis/build/realm/SwiftLint/Source/SwiftLintFramework/Rules/MultilineClosureArgumentRule.swift:14:32: error: Force Try Violation: Force tries should be avoided. (force_try)
/Users/travis/build/realm/SwiftLint/Source/SwiftLintFramework/Rules/MultilineClosureArgumentRule.swift:25:1: warning: Multi-line closure argument Violation: Multi-line closures should not use $0 (closure_argument)
/Users/travis/build/realm/SwiftLint/Source/SwiftLintFramework/Rules/MultilineClosureArgumentRule.swift:32:1: warning: Multi-line closure argument Violation: Multi-line closures should not use $0 (closure_argument)
/Users/travis/build/realm/SwiftLint/Source/SwiftLintFramework/Rules/MultilineClosureArgumentRule.swift:41:1: warning: Multi-line closure argument Violation: Multi-line closures should not use $0 (closure_argument)
/Users/travis/build/realm/SwiftLint/Source/SwiftLintFramework/Rules/MultilineClosureArgumentRule.swift:42:1: warning: Multi-line closure argument Violation: Multi-line closures should not use $0 (closure_argument)

@keith
Copy link
Collaborator Author

keith commented Nov 18, 2015

Yea I can disable those if we want to move forward with this. And fix the description.

This rule forces users to name arguments for multiline closures.
@jpsim
Copy link
Collaborator

jpsim commented Nov 18, 2015

I think we have a difference of stylistic opinion regarding when it's appropriate to use $0.

I'd be much more inclined to say it's for single statements like @segiddins suggested rather than requiring the closure's braces to be on the same line.

I actually consider all of SwiftLint's current uses of the dollar sign shorthand to be appropriate in that sense.

Unfortunately, I have no idea how to detect statements.

@jpsim
Copy link
Collaborator

jpsim commented Nov 26, 2015

By the way, it'd be trivial to update this rule to only apply in the appropriate context:

-    private static let regex = try! NSRegularExpression(pattern:
-        "(^[^\\{\\n]*\\$0|\\$0[^\\}\\n]*$)", options: .AnchorsMatchLines)
-
     public func validateFile(file: File) -> [StyleViolation] {
-        let range = NSRange(location: 0, length: file.contents.utf16.count)
-        let matches = MultilineClosureArgumentRule.regex.matchesInString(file.contents,
-            options: [], range: range)
-
-        return matches.map { match in
-            return StyleViolation(ruleDescription: self.dynamicType.description,
-                location: Location(file: file, offset: match.range.location))
+        let pattern = "(^[^\\{\\n]*(\\$0)|(\\$0)[^\\}\\n]*$)"
+        return file.matchPattern(pattern, withSyntaxKinds: [.Identifier]).map { match in
+            StyleViolation(ruleDescription: self.dynamicType.description,
+                           location: Location(file: file, offset: match.location))
         }
     }

@jpsim
Copy link
Collaborator

jpsim commented Jan 14, 2016

@keith would you like to revive this now that we have opt-in rules?

@keith
Copy link
Collaborator Author

keith commented Jan 14, 2016

Sure. I'll try and get to the feedback shortly.

@jpsim
Copy link
Collaborator

jpsim commented Jan 14, 2016

FWIW I wouldn't enable it for this project.

@scottrhoyt
Copy link
Contributor

@keith do you have any updates here? Or should we close this PR out? Thanks.

@keith
Copy link
Collaborator Author

keith commented Feb 3, 2016

I'll reopen this when I have time to make the fixes!

@keith keith closed this Feb 3, 2016
@jpsim
Copy link
Collaborator

jpsim commented Feb 3, 2016

I'll actually take a stab at reviving this if you don't mind @keith. I think it's a great rule to have, even on by default, but keeping the default number of lines a bit higher (4-5?).

@keith
Copy link
Collaborator Author

keith commented Feb 3, 2016

Have at it! I think that a single line is a nice rule, but maybe that could be customizable?

@jpsim
Copy link
Collaborator

jpsim commented Feb 3, 2016

Have at it! I think that a single line is a nice rule, but maybe that could be customizable?

Yeah, if this goes in, it should certainly be customizable.

@jpsim
Copy link
Collaborator

jpsim commented Feb 3, 2016

Hmm, on second thought, I don't think I can do this as easily as I had expected... There's no SwiftDeclarationKind for closures, so they're a bit hard to detect.

@keith keith deleted the ks-closure-rule branch March 18, 2017 20:08
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.

None yet

4 participants