forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoGroupingExtensionRule.swift
48 lines (41 loc) · 1.76 KB
/
NoGroupingExtensionRule.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
//
// NoGroupingExtensionRule.swift
// SwiftLint
//
// Created by Mazyad Alabduljaleel on 8/20/17.
// Copyright © 2017 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct NoGroupingExtensionRule: OptInRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "no_grouping_extension",
name: "No Grouping Extension",
description: "Extensions shouldn't be used to group code within the same source file.",
kind: .idiomatic,
nonTriggeringExamples: [
"protocol Food {}\nextension Food {}\n",
"class Apples {}\nextension Oranges {}\n"
],
triggeringExamples: [
"enum Fruit {}\n↓extension Fruit {}\n",
"↓extension Tea: Error {}\nstruct Tea {}\n",
"class Ham { class Spam {}}\n↓extension Ham.Spam {}\n",
"extension External { struct Gotcha {}}\n↓extension External.Gotcha {}\n"
]
)
public func validate(file: File) -> [StyleViolation] {
let collector = NamespaceCollector(dictionary: file.structure.dictionary)
let elements = collector.findAllElements(of: [.class, .enum, .struct, .extension])
let susceptibleNames = Set(elements.flatMap { $0.kind != .extension ? $0.name : nil })
return elements
.filter { $0.kind == .extension && susceptibleNames.contains($0.name) }
.map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: $0.offset))
}
}
}