Jax
Jax

Reputation: 210

SwiftLint Custom Spacing Rule After slass/struct/enum/extension

I've got the following regex that is working via https://regex101.com/r/7D6fAL/1 but is not working when added as a swiftlint custom rule. I would like for the rule to trigger under the following conditions (working as expected in regex101):

// should trigger
public final class Aaaaaaa {
var cat: Bool
}

// should not trigger
class Aaa {

var cat: Bool
}

// should not trigger
func bbb {
}

// should trigger
struct Ccc {
}

// should not trigger
struct Ccc {

}

// should trigger
enum Ddd {
}

// should not trigger
enum Ddd {

}

// should trigger
extension Eee {
}

// should not trigger
extension Eee {

}

But when added as a swiftlint custom rule as such no warnings are triggered:

custom_rules:
  space_after_main_type:
    name: "No space after main type"
    regex: '(class|struct|enum|extension)[\h\S]*{$\n(?!\s)' 
    message: "Empty line required after main declarations"
    severity: warning

Upvotes: 0

Views: 488

Answers (1)

Jax
Jax

Reputation: 210

Figured it out thanks to this comment to an issue on the swiftlint repo. Posting my answer here in case other folks go searching for a similar solution and don't want to spend hours on it like I did:

(class|struct|enum|extension)((?-s)\s.*\{$\n)(?!^\s*$)

Are there other ways to solve this, almost certainly, but I will let people smarter than me post about why I'm a dummy for not figuring those out. ;)

Upvotes: 1

Related Questions