forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscouragedDirectInitRule.swift
61 lines (55 loc) · 2.09 KB
/
DiscouragedDirectInitRule.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
//
// DiscourageInitRule.swift
// SwiftLint
//
// Created by Ornithologist Coder on 8/1/17.
// Copyright © 2017 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct DiscouragedDirectInitRule: ASTRule, ConfigurationProviderRule {
public var configuration = DiscouragedDirectInitConfiguration()
public init() {}
public static let description = RuleDescription(
identifier: "discouraged_direct_init",
name: "Discouraged Direct Initialization",
description: "Discouraged direct initialization of types that can be harmful.",
kind: .lint,
nonTriggeringExamples: [
"let foo = UIDevice.current",
"let foo = Bundle.main",
"let foo = Bundle(path: \"bar\")",
"let foo = Bundle(identifier: \"bar\")",
"let foo = Bundle.init(path: \"bar\")",
"let foo = Bundle.init(identifier: \"bar\")"
],
triggeringExamples: [
"↓UIDevice()",
"↓Bundle()",
"let foo = ↓UIDevice()",
"let foo = ↓Bundle()",
"let foo = bar(bundle: ↓Bundle(), device: ↓UIDevice())",
"↓UIDevice.init()",
"↓Bundle.init()",
"let foo = ↓UIDevice.init()",
"let foo = ↓Bundle.init()",
"let foo = bar(bundle: ↓Bundle.init(), device: ↓UIDevice.init())"
]
)
public func validate(file: File,
kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard
kind == .call,
let offset = dictionary.nameOffset,
let name = dictionary.name,
dictionary.bodyLength == 0,
configuration.discouragedInits.contains(name)
else {
return []
}
return [StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset))]
}
}