Swiftlint: custom rules for colon and brace

Created on 7 Jan 2017  路  5Comments  路  Source: realm/SwiftLint

I'd like to know if I can configure my .swiftlint.yml for those custom behaviour checks:

  1. similar to rule colon, but with a space before the colon for literals. i.e.:
    var headers = [String : String]() would be accepted
    var headers = [String: String]() would trigger a warning for missing space

  2. similar to rule opening_brace, but with a newline before the brace for struct/class/enum/extension. i.e.:

  3. This would be accepted:

    extension String
    {

  4. This would trigger a warning for missing newline

    extension String {

enhancement wontfix

All 5 comments

for # 2 you can put this in your .swiftlint.yml:

custom_rules:
invalid_open_brace:
name: "Open brace should start on its own line"
regex: "S[ t]+{"
message: "Open brace should start on its own line"
severity: warning

@Trifusion It doesn't work.
I am actually also looking for this solution and I figured that we need do do 2 regex, like the open_brace build-in-rule but I have no idea how to do it inside the .swiftlint.yml

I don't think giving thumbs down will motivate anyone to implement this features. If @Ohad-Maor have nothing to contribute, Please be patient and don't sh*t someone who are trying to help.

@Trifusion 's regex was wrong. I tried to wrote one too ([\\t]*.+{(?!.*}\\n)) but it's not working appropriately. If there's a working Regex who can help, I will learn and try to make commits into the related rule file.

Cheers.

We ended up using:

k&r_style_opening_braces:
    name: "K&R style braces used"
    message: "Curly brace must be on it's own line (Allman style)"
    regex: '[a-zA-Z)] ?\{$'
    include: "*.swift"

It says: check for a letter or a round brace, then an optional space, afterwards the curly brace and then the end of the line. Probably not bulletproof but works OK so far.

This issue has been automatically marked as stale because it has not had any recent activity. Please comment to prevent this issue from being closed. Thank you for your contributions!

Was this page helpful?
0 / 5 - 0 ratings