mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
fcf848608e
* 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.
104 lines
3.6 KiB
Swift
104 lines
3.6 KiB
Swift
import SourceKittenFramework
|
|
|
|
public struct OverriddenSuperCallRule: ConfigurationProviderRule, ASTRule, OptInRule, AutomaticTestableRule {
|
|
public var configuration = OverridenSuperCallConfiguration()
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "overridden_super_call",
|
|
name: "Overridden methods call super",
|
|
description: "Some overridden methods should always call super",
|
|
kind: .lint,
|
|
nonTriggeringExamples: [
|
|
Example("""
|
|
class VC: UIViewController {
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class VC: UIViewController {
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
self.method1()
|
|
super.viewWillAppear(animated)
|
|
self.method2()
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class VC: UIViewController {
|
|
override func loadView() {
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Some {
|
|
func viewWillAppear(_ animated: Bool) {
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class VC: UIViewController {
|
|
override func viewDidLoad() {
|
|
defer {
|
|
super.viewDidLoad()
|
|
}
|
|
}
|
|
}
|
|
""")
|
|
],
|
|
triggeringExamples: [
|
|
Example("""
|
|
class VC: UIViewController {
|
|
override func viewWillAppear(_ animated: Bool) {↓
|
|
//Not calling to super
|
|
self.method()
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class VC: UIViewController {
|
|
override func viewWillAppear(_ animated: Bool) {↓
|
|
super.viewWillAppear(animated)
|
|
//Other code
|
|
super.viewWillAppear(animated)
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class VC: UIViewController {
|
|
override func didReceiveMemoryWarning() {↓
|
|
}
|
|
}
|
|
""")
|
|
]
|
|
)
|
|
|
|
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)
|
|
else { return [] }
|
|
|
|
let callsToSuper = dictionary.extractCallsToSuper(methodName: name)
|
|
|
|
if callsToSuper.isEmpty {
|
|
return [StyleViolation(ruleDescription: type(of: self).description,
|
|
severity: configuration.severity,
|
|
location: Location(file: file, byteOffset: offset),
|
|
reason: "Method '\(name)' should call to super function")]
|
|
} else if callsToSuper.count > 1 {
|
|
return [StyleViolation(ruleDescription: type(of: self).description,
|
|
severity: configuration.severity,
|
|
location: Location(file: file, byteOffset: offset),
|
|
reason: "Method '\(name)' should call to super only once")]
|
|
}
|
|
return []
|
|
}
|
|
}
|