Search code examples
swiftswiftlint

Mandatory empty line after function definition SwiftLint


I am integrating swiftlint in my xcode project and setting it's configuration. But I am not able to get the rule which enforces an empty line after function definition

func test1() {
}
func test2() {
}

If I add multiple spaces between two functions there is a rule for that.

Is there any rule which can be used to enable empty line between two functions or I need to write custom one


Solution

  • Doing swiftlint rules will give you a list of rules that are available. What I usually do to find the rule applicable is change different rules in the yaml file until i find the one that does what I want.

    If your rule does not exist, I suggest writing a custom rule using regex such as:

     custom_rules:
      pirates_beat_ninjas: # rule identifier
        included: ".*\\.swift" # regex that defines paths to include during linting. optional.
        excluded: ".*Test\\.swift" # regex that defines paths to exclude during linting. optional
        name: "Pirates Beat Ninjas" # rule name. optional.
        regex: "([nN]inja)" # matching pattern
        capture_group: 0 # number of regex capture group to highlight the rule violation at. optional.
        match_kinds: # SyntaxKinds to match. optional.
          - comment
          - identifier
        message: "Pirates are better than ninjas." # violation message. optional.
        severity: error # violation severity. optional.
      no_hiding_in_strings:
        regex: "([nN]inja)"
        match_kinds: string
    

    This rule makes it so that every time you see the word Ninja, it causes a violation