since SwiftLint has custom regex rule, could anyone share several of your regex rules? just curious how you guys use it effectively. thanks
I don't use any, so I'm afraid I can't help you here too much. But if you do search GitHub for custom_rules language:yaml you get quite a few examples:
custom_rules:
# From https://github.com/Twigz/Game
force_https:
name: "Force HTTPS over HTTP"
regex: "((?i)http(?!s))"
match_kinds: string
message: "HTTPS should be favored over HTTP"
severity: warning
# From https://github.com/onelittlefish/ObjectiveSet
trailing_whitespace_permissive:
name: Trailing Whitespace (excluding whitespace-only lines)
# This will only catch some trailing whitespace due to swiftlint's custom regex
# implementation and how it filters for match_kinds.
# This alternative regex will catch more (but still not all) cases:
# "\S+[\S\t\f\p{Z}]+[\t\f\p{Z}]+\n" but it is much less performant.
# Examples that won't be caught:
# closing brace by itself followed by space (not caught by either regex),
# func declaration followed by space after the opening brace (would be caught by
# less performant regex)
regex: "\S+[\t\f\p{Z}]+\n"
message: "Lines should not have trailing whitespace."
# From https://github.com/brandenr/swiftlintconfig
comments_space:
name: "Space After Comment"
regex: "(^ *//\w+)"
message: "There should be a space after //"
severity: error
comments_capitalized_ignore_possible_code:
name: "Capitalize First Word In Comment"
regex: "(^ +// +(?!swiftlint)[a-z]+)"
message: "The first word of a comment should be capitalized"
severity: error
comments_capitalized_find_possible_code:
name: "Catch Commented Out Code"
regex: "(^ *// +(?!swiftlint)[a-z]+)"
message: "The first word of a comment should be capitalized"
severity: warning
empty_first_line:
name: "Empty First Line"
regex: "(^[ a-zA-Z ]*(?:protocol|extension|class|struct) (?!(?:var|let))[ a-zA-Z:]*\{\n *\S+)"
message: "There should be an empty line after a declaration"
severity: error
empty_line_after_guard:
name: "Empty Line After Guard"
regex: "(^ *guard[ a-zA-Z0-9=?.\(\),><!]*\{[ a-zA-Z0-9=?.\(\),><!]*\}\n *(?!(?:return|guard))\S+)"
message: "There should be an empty line after a guard"
severity: error
empty_line_after_super:
name: "Empty Line After Super"
regex: "(^ *super\.[ a-zA-Z0-9=?.\(\)\{\}:,><!]*\n *(?!(?:\}|return))\S+)"
message: "There should be an empty line after super"
severity: error
multiple_empty_lines:
name: "Multiple Empty Lines"
regex: "((?:\s*\n){3,})"
message: "There are too many line breaks"
severity: error
unnecessary_type:
name: "Unnecessary Type"
regex: "[ a-zA-Z0-9]*(?:let|var) [ a-zA-Z0-9]*: ([a-zA-Z0-9]*)[\? ]*= \1"
message: "Type Definition Not Needed"
severity: error
thanks for the tips.
How to use custom rule to prevent "fatal error: Array index out of range". This usually the case where accessing array index without using count
How to use custom rule to prevent "fatal error: Array index out of range". This usually the case where accessing array index without using count
How would you propose such a rule even work? If this were possible, the Swift compiler would do it.
not sure how to make it work, probably parsing the array variable and check if it is enclosure by array count checking
To build something like that, you'd need to integrate it as part of the semantic analysis phase of the Swift compiler, and even then it's impossible to do in the general case, you'd need dependent types that encode their possible value in the type system, etc.
I have an improvement for the Unnecessary Type RegExp.
The current version
[ a-zA-Z0-9]*(?:let|var) [ a-zA-Z0-9]*: ([a-zA-Z0-9]*)[\? ]*= \1
can flag some false positive. For example this is marked as unnecessary_type
let user: MyUser = MyUserDecorator()
to fix is enough to change the regular expression in this way:
[ a-zA-Z0-9]*(?:let|var) [ a-zA-Z0-9]*: ([a-zA-Z0-9]*)[ ]*= \1\(
Most helpful comment
I don't use any, so I'm afraid I can't help you here too much. But if you do search GitHub for custom_rules language:yaml you get quite a few examples: