Search code examples
iosswiftswiftlint

Make Custom SwiftLint action regex ignore comments


I have a custom SwiftLint action to flag up print() statements:

custom_rules:
    disable_print:
      included: ".*\\.swift"
      name: "print usage"
      regex: "((\\bprint)|(Swift\\.print))\\s*\\("
      message: "Don't use print"
      severity: error

It works but it also flags whenever I have a print() statement specified in a documentation comment like this:

/// some comment mentioning print("hello") <- Error here
func myFunc() {}

How can I alter the regex so it ignores the print statement whenever it's in a documentation comment?


Solution

  • It seems that a custom rule can specify what type of code will match. The property is called match_kinds, example from Swiftlint Readme:

    match_kinds: # SyntaxKinds to match. optional.
       - comment
       - identifier
    

    Specifying identifier should be enough for your use case.