Reputation: 51
In my Swift codebase, I put brackets on newlines in some specific cases due to my poor vision. I want to enforce this rule using SwiftLint.
My requirements are simple:
Show a warning when the character {
is on the same line as the following strings:
func
, if
, else
, class
, struct
, catch
, try
, init
, guard
, switch
I'm far from a REGEX expert, but I came up with this REGEX string: ^(?=.*\{)(?=.*(?:func|if|else|class|struct|catch|try|init|guard|switch)).*$
The rule ends up looking like this:
custom_rules:
brackets_newline:
name: "Opening Brackets not on Next Line"
message: "Opening brackets should be placed on their own lines"
include: "*.swift"
regex: '^(?=.*\{)(?=.*(?:func|if|else|class|struct|catch|try|init|guard|switch)).*$'
severity: warning
Now, in a REGEX playground, this works well with no problems. Try this file, for example: https://pastebin.ai/urmb1ejoly
However, when I run SwiftLint on this file in Xcode, it produces the brackets_newline
warning on top of any Swift file, even if it has no violation of the brackets_newline
rule:
I tried excluding lines starting with the characters //
, but that just shifts the error to the first empty line.
I'm expecting the rule violation warning to show up only on the relevant lines.
What am I doing wrong?
Upvotes: 0
Views: 83