swiftregexswiftlint

SwiftLint Rule to ask for pragma mark before every extension


I'm trying to implement custom swiftlint rule to track when before "extension" there is no "// MARK: -". Here is my rule:

custom_rules:
    pragma_mark:
        name: "Missing pagma mark"
        match_kinds:
            - comment
            - string
        regex: '(^(?!(\/\/ MARK: -.*\n))(.*\n.*extension.*\{))'
        message: "Please add // MARK:"
        severity: warning

I've created regex on regex101: https://regex101.com/r/0AKKmL/2

It works on regex101 but doesn't work in SwiftLint.

I'm sure that SwiftLint is configured correct as other rules work perfectly. Also I'm using disabled_rules - so I can't use whitelist and it is not a reason for custom_rules.

Maybe I collapse SwiftLint existing rules?


Solution

  • I was trying to have the same. Rule below functioned for me

    mark_before_protocol_extensions:
        regex: '^(?!// MARK: -)\nextension\s+\w+:\s+\w+'
        message: "Add a MARK comment before each protocol extension."
        severity: warning