swiftxcodeyamlswiftlint

Why are some custom swiftlint rules marked as having an "invalid configuration"?


When I run swiftlint I get a printout saying that some rules have an invalid configuration.

Invalid configuration for custom rule 'commented_out_code'.
Invalid configuration for custom rule 'avoid_multiline_comment_markers'.
Invalid configuration for custom rule 'avoid_background_color'.

No other custom rules are marked as such, and the rules themselves still work in the project. These are the configurations in question:

commented_out_code:
  regex: '(?<!:|\/)\/\/\h*[a-z.](?!wiftlint)'
  message: "Comment starting with lowercase letter - did you forget to delete old code?"

avoid_multiline_comment_markers:
  regex: '*/'
  message: "Avoid using multi-line comment markers like */ and /* - use // and /// instead."

avoid_background_color:
  regex: '.background(Color.'
  message: "Avoid using .background(Color), use .backgroundColor() instead."

The last one is my own, the other 2 are default custom rules however, that come in a basic swiftlint config file.

Here is a "valid" rule to show the formatting is the same:

use_int_zero_property_in_single_check:
  regex: ' == 0[^( {)]'
  message: "Avoid checking for '0' in single check, use '== .zero' instead."

What could be invalid about these?

Things I've tried that had no result:


Solution

  • The problem here is incorrect regex syntax, mainly not escaping characters properly.

    So for avoid_background_color it should be

    regex: '\.background\(Color\.'
    

    And for avoid_multiline_comment_markers

    regex: '\*/'
    

    commented_out_code is hard for me to understand but it looks like you missed a = at the beginning

    regex: '(?=<!:|\/)\/\/\h*[a-z.](?!wiftlint)'
    

    I would recommend the site https://regex101.com for testing and verifying your regex syntax.