Files
SwiftLint/Source/SwiftLintFramework/Rules/Lint/ProhibitedSuperRule.swift
Zev Eisenberg fcf848608e Add Inline test failure messages (#3040)
* Add Example wrapper in order to display test failures inline when running in Xcode.
* Stop using Swift 5.1-only features so we can compile on Xcode 10.2.
* Wrap strings in Example.
* Add Changelog entry.
* Wrap all examples in Example struct.
* Better and more complete capturing of line numbers.
* Fix broken test.
* Better test traceability.
* Address or disable linting warnings.
* Add documentation comments.
* Disable linter for a few cases.
* Limit mutability and add copy-and-mutate utility functions.
* Limit scope of mutability.
2020-02-02 10:35:37 +02:00

91 lines
3.2 KiB
Swift

import SourceKittenFramework
public struct ProhibitedSuperRule: ConfigurationProviderRule, ASTRule, OptInRule, AutomaticTestableRule {
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: [
Example("""
class VC: UIViewController {
override func loadView() {
}
}
"""),
Example("""
class NSView {
func updateLayer() {
self.method1()
}
}
"""),
Example("""
public class FileProviderExtension: NSFileProviderExtension {
override func providePlaceholder(at url: URL, completionHandler: @escaping (Error?) -> Void) {
guard let identifier = persistentIdentifierForItem(at: url) else {
completionHandler(NSFileProviderError(.noSuchItem))
return
}
}
}
""")
],
triggeringExamples: [
Example("""
class VC: UIViewController {
override func loadView() {↓
super.loadView()
}
}
"""),
Example("""
class VC: NSFileProviderExtension {
override func providePlaceholder(at url: URL, completionHandler: @escaping (Error?) -> Void) {↓
self.method1()
super.providePlaceholder(at:url, completionHandler: completionHandler)
}
}
"""),
Example("""
class VC: NSView {
override func updateLayer() {↓
self.method1()
super.updateLayer()
self.method2()
}
}
"""),
Example("""
class VC: NSView {
override func updateLayer() {↓
defer {
super.updateLayer()
}
}
}
""")
]
)
public func validate(file: SwiftLintFile, kind: SwiftDeclarationKind,
dictionary: SourceKittenDictionary) -> [StyleViolation] {
guard let offset = dictionary.bodyOffset,
let name = dictionary.name,
kind == .functionMethodInstance,
configuration.resolvedMethodNames.contains(name),
dictionary.enclosedSwiftAttributes.contains(.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")]
}
}