I am using SwiftLint and I need to have a new line after the "{" if there is a body, for example, this is correct
func foo() {
if true { }
}
but this doesn't seem right
func foo() {
if true { print("This print should be on the new line") }
}
like this
func foo() {
if true {
print("This print should be on the new line")
}
}
How to do this?
UPD
Thanks, @Bram, there is such a regex
custom_rules:
newline_block:
name: "Newline Block"
regex: 'if \(?\w+\)? \{[ ]*(.+)+[ ]*\}'
message: "Statement must be on its own line"
The problem is this regex catch all the conditions with one word after the if
, like this
if myCondition { }
and it doesn't matter if braces are on the next line or not, however, this regex doesn't catch conditions like this
if 0 < 1 { print() }
It appears SwiftLint is a bit flaky with the any single character (.
), so the closest I got was this
custom_rules:
newline_block:
name: "Newline Block"
regex: "\\{[ ]*(\\S|( +))+[ ]*\\}"
message: "Statement must be on its own line"
The better regex would be {[ ]*(.+)+[ ]*}
(unescaped), but for some reason that doens't work when I run it at Xcode.
This will work for your request with the prints, but the solution does have some drawbacks:
func foo() {
// No warning
if true {}
// Newline Block Violation: Statement must be on its own line (newline_block)
if true { }
}
And, but I'm not sure if that applies to you as well:
var connection: OpaquePointer? {
// Newline Block Violation: Statement must be on its own line
get { queue.sync { underlyingConnection } }
// Newline Block Violation: Statement must be on its own line
set { queue.sync { underlyingConnection = newValue } }
}