forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProhibitedSuperRule.swift
77 lines (71 loc) · 2.88 KB
/
ProhibitedSuperRule.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
//
// ProhibitedSuperRule.swift
// SwiftLint
//
// Created by Aaron McTavish on 12/12/16.
// Copyright © 2016 Realm. All rights reserved.
//
import SourceKittenFramework
public struct ProhibitedSuperRule: ConfigurationProviderRule, ASTRule, OptInRule {
public var configuration = ProhibitedSuperConfiguration()
public init() {}
public static let description = RuleDescription(
identifier: "prohibited_super_call",
name: "Prohibited calls to super",
description: "Some methods should not call super",
kind: .lint,
nonTriggeringExamples: [
"class VC: UIViewController {\n" +
"\toverride func loadView() {\n" +
"\t}\n" +
"}\n",
"class NSView {\n" +
"\tfunc updateLayer() {\n" +
"\t\tself.method1()" +
"\t}\n" +
"}\n"
],
triggeringExamples: [
"class VC: UIViewController {\n" +
"\toverride func loadView() {↓\n" +
"\t\tsuper.loadView()\n" +
"\t}\n" +
"}\n",
"class VC: NSFileProviderExtension {\n" +
"\toverride func providePlaceholder(at url: URL," +
"completionHandler: @escaping (Error?) -> Void) {↓\n" +
"\t\tself.method1()\n" +
"\t\tsuper.providePlaceholder(at:url, completionHandler: completionHandler)\n" +
"\t}\n" +
"}\n",
"class VC: NSView {\n" +
"\toverride func updateLayer() {↓\n" +
"\t\tself.method1()\n" +
"\t\tsuper.updateLayer()\n" +
"\t\tself.method2()\n" +
"\t}\n" +
"}\n",
"class VC: NSView {\n" +
"\toverride func updateLayer() {↓\n" +
"\t\tdefer {\n" +
"\t\t\tsuper.updateLayer()\n" +
"\t\t}\n" +
"\t}\n" +
"}\n"
]
)
public func validate(file: File, kind: SwiftDeclarationKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard let offset = dictionary.bodyOffset,
let name = dictionary.name,
kind == .functionMethodInstance,
configuration.resolvedMethodNames.contains(name),
dictionary.enclosedSwiftAttributes.contains("source.decl.attribute.override"),
!dictionary.extractCallsToSuper(methodName: name).isEmpty
else { return [] }
return [StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset),
reason: "Method '\(name)' should not call to super function")]
}
}