-
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
Add multiline closure argument rule #218
Conversation
if at all possible, it might be preferable to make this single statement closures instead of single-line |
Lol, of course the rule would violate itself:
|
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.
bebd84a
to
8f5fbd5
Compare
I think we have a difference of stylistic opinion regarding when it's appropriate to use 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. |
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))
}
} |
@keith would you like to revive this now that we have opt-in rules? |
Sure. I'll try and get to the feedback shortly. |
FWIW I wouldn't enable it for this project. |
@keith do you have any updates here? Or should we close this PR out? Thanks. |
I'll reopen this when I have time to make the fixes! |
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?). |
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. |
Hmm, on second thought, I don't think I can do this as easily as I had expected... There's no |
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.