Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftLint Rule for Braces on next line but with some exceptions

I want to modify .swiftlint.yml to add some custom rules for enforcing braces on next line. This works for me ...

  opening_braces:
    name: "Opening Braces not on Next Line"
    message: "Opening braces should be placed on the next line."
    include: "*.swift"
    regex: '\S[ \t]*\{'
    severity: warning

However there are some cases where I want to allow braces on the same line, e.g. something like this:

override var cornerRadius: CGFloat
{
    get { return layer.cornerRadius }
    set { layer.cornerRadius = newValue }
}

How do I change my regexp to allow for same line for one-line getters/setters?

like image 213
BadmintonCat Avatar asked Dec 07 '25 09:12

BadmintonCat


1 Answers

I suggest using

regex: '^(?![ \t]*[sg]et[ \t]+\{.*\}).*\S[ \t]*\{'

Or, its alternative with \h matching horizontal whitespace:

regex: '^(?!\h*[sg]et\h+\{.*\}).*\S\h*\{'

See the regex demo (or this one).

Details

  • ^ - start of string
  • (?!\h*[sg]et\h+\{.*\}) - a location in string that should not be immediately followed with
    • \h* - 0+ horizontal whitespaces
    • [sg]et - set or get
    • \h+ - 1+ horizontal whitespaces
    • \{.*\} - {, any 0+ chars, as many as possible, and }
  • .* - any 0+ chars, as many as possible
  • \S - a non-whitespace char
  • \h* - 0+ horizontal whitespaces
  • \{ - a { char.
like image 190
Wiktor Stribiżew Avatar answered Dec 10 '25 00:12

Wiktor Stribiżew