forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NestingRule.swift
96 lines (88 loc) · 3.79 KB
/
NestingRule.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// NestingRule.swift
// SwiftLint
//
// Created by JP Simard on 2015-05-16.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
import SwiftXPC
public struct NestingRule: ASTRule {
public init() {}
public let identifier = "nesting"
public func validateFile(file: File) -> [StyleViolation] {
return validateFile(file, dictionary: file.structure.dictionary)
}
public func validateFile(file: File, dictionary: XPCDictionary) -> [StyleViolation] {
return (dictionary["key.substructure"] as? XPCArray ?? []).flatMap { subItem in
var violations = [StyleViolation]()
if let subDict = subItem as? XPCDictionary,
let kindString = subDict["key.kind"] as? String,
let kind = flatMap(kindString, { SwiftDeclarationKind(rawValue: $0) }) {
violations.extend(validateFile(file, dictionary: subDict))
violations.extend(validateFile(file, kind: kind, dictionary: subDict))
}
return violations
}
}
public func validateFile(file: File,
kind: SwiftDeclarationKind,
dictionary: XPCDictionary) -> [StyleViolation] {
return self.validateFile(file, kind: kind, dictionary: dictionary, level: 0)
}
func validateFile(file: File,
kind: SwiftDeclarationKind,
dictionary: XPCDictionary,
level: Int) -> [StyleViolation] {
var violations = [StyleViolation]()
let typeKinds: [SwiftDeclarationKind] = [
.Class,
.Struct,
.Typealias,
.Enum,
.Enumelement
]
if let offset = flatMap(dictionary["key.offset"] as? Int64, { Int($0) }) {
if level > 1 && contains(typeKinds, kind) {
violations.append(StyleViolation(type: .Nesting,
location: Location(file: file, offset: offset),
reason: "Types should be nested at most 1 level deep"))
} else if level > 5 {
violations.append(StyleViolation(type: .Nesting,
location: Location(file: file, offset: offset),
reason: "Statements should be nested at most 5 levels deep"))
}
}
let substructure = dictionary["key.substructure"] as? XPCArray ?? []
violations.extend(compact(substructure.map { subItem in
let subDict = subItem as? XPCDictionary
let kindString = subDict?["key.kind"] as? String
let kind = flatMap(kindString) { kindString in
return SwiftDeclarationKind(rawValue: kindString)
}
if let kind = kind, subDict = subDict {
return (kind, subDict)
}
return nil
} as [(SwiftDeclarationKind, XPCDictionary)?]).flatMap { (kind, dict) in
return validateFile(file, kind: kind, dictionary: dict, level: level + 1)
})
return violations
}
public let example = RuleExample(
ruleName: "Nesting Rule",
ruleDescription: "Types should be nested at most 1 level deep, " +
"and statements should be nested at most 5 levels deep.",
nonTriggeringExamples: ["class", "struct", "enum"].flatMap { kind in
["\(kind) Class0 { \(kind) Class1 {} }\n",
"func func0() {\nfunc func1() {\nfunc func2() {\nfunc func3() {\nfunc func4() { " +
"func func5() {\n}\n}\n}\n}\n}\n}\n"]
},
triggeringExamples: ["class", "struct", "enum"].map { kind in
"\(kind) Class0 { \(kind) Class1 { \(kind) Class2 {} } }\n"
} + [
"func func0() {\nfunc func1() {\nfunc func2() {\nfunc func3() {\nfunc func4() { " +
"func func5() {\nfunc func6() {\n}\n}\n}\n}\n}\n}\n}\n"
]
)
}