Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting violation. Types should be nested at most 1 level deep

struct Email {
    struct Constraints {
        static let top = 20.asConstraint()
    }

    static let placeholder = "login_email"
}

enter image description here

How to fix that issue?

like image 315
Bartłomiej Semańczyk Avatar asked Oct 16 '17 09:10

Bartłomiej Semańczyk


2 Answers

This particular rule of SwiftLint, nesting, has priorly been discussed in the following Q&A:

  • Nested types in Swift - what is the good practice?

With somewhat of an consensus that this rule is not based on a technical reason, but rather seem to be an opinion-based "best practice" rule, noticeably not one followed e.g. by the Swift standard libs (which use nested type frequently). Moreover, the Swift language guide, specifically the Nested Types Section, contains no warning for/recommendation against nesting type deeper than depth 1.

... Types can be nested to as many levels as are required.

One motivation for this (opinion-based rule) can be found in SwiftLint issue 1450:

jpsim:

The nesting rule exists for two reasons:

  • to avoid overly-nested types in an API
  • to avoid overly-indented statements (aka pyramid of doom)

...

Based on the above, unless you find your code turning into unreadable pyramid of doom spaghetti (which I don't believe to be the case in your example), you could simply considering disabling the nesting rule:

// swiftlint:disable nesting
like image 149
dfrib Avatar answered Sep 27 '22 19:09

dfrib


You can create another struct:

struct Email {
    let constraints = EmailConstraints()
    let placeholder = "login_email"
}

struct EmailConstraints {
    let top = 20.asConstraint()
}

As you mentioned later on in comments, since this is only a linter error, you can disable this rule in swiftlint.yml file.

like image 37
KlimczakM Avatar answered Sep 27 '22 17:09

KlimczakM