Files
SwiftLint/Source/SwiftLintFramework/Rules/Lint/ProhibitedInterfaceBuilderRule.swift
T
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

54 lines
1.9 KiB
Swift

import SourceKittenFramework
public struct ProhibitedInterfaceBuilderRule: ConfigurationProviderRule, ASTRule, OptInRule, AutomaticTestableRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "prohibited_interface_builder",
name: "Prohibited Interface Builder",
description: "Creating views using Interface Builder should be avoided.",
kind: .lint,
nonTriggeringExamples: [
wrapExample("var label: UILabel!"),
wrapExample("@objc func buttonTapped(_ sender: UIButton) {}")
],
triggeringExamples: [
wrapExample("@IBOutlet ↓var label: UILabel!"),
wrapExample("@IBAction ↓func buttonTapped(_ sender: UIButton) {}")
]
)
public func validate(file: SwiftLintFile, kind: SwiftDeclarationKind,
dictionary: SourceKittenDictionary) -> [StyleViolation] {
guard let offset = dictionary.offset else {
return []
}
func makeViolation() -> StyleViolation {
return StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset))
}
if kind == .varInstance && dictionary.enclosedSwiftAttributes.contains(.iboutlet) {
return [makeViolation()]
}
if kind == .functionMethodInstance && dictionary.enclosedSwiftAttributes.contains(.ibaction) {
return [makeViolation()]
}
return []
}
}
private func wrapExample(_ text: String, file: StaticString = #file, line: UInt = #line) -> Example {
return Example("""
class ViewController: UIViewController {
\(text)
}
""", file: file, line: line)
}