mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
With the binding of configurations to their associated rule types "unknown configuration" errors can be made more specific mentioning also the rule's identifier in the printed message.
49 lines
1.6 KiB
Swift
49 lines
1.6 KiB
Swift
import SwiftSyntax
|
|
|
|
struct ProhibitedInterfaceBuilderRule: ConfigurationProviderRule, SwiftSyntaxRule, OptInRule {
|
|
var configuration = SeverityConfiguration<Self>(.warning)
|
|
|
|
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) {}")
|
|
]
|
|
)
|
|
|
|
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
|
Visitor(viewMode: .sourceAccurate)
|
|
}
|
|
}
|
|
|
|
private extension ProhibitedInterfaceBuilderRule {
|
|
final class Visitor: ViolationsSyntaxVisitor {
|
|
override func visitPost(_ node: VariableDeclSyntax) {
|
|
if node.isIBOutlet {
|
|
violations.append(node.bindingKeyword.positionAfterSkippingLeadingTrivia)
|
|
}
|
|
}
|
|
|
|
override func visitPost(_ node: FunctionDeclSyntax) {
|
|
if node.isIBAction {
|
|
violations.append(node.funcKeyword.positionAfterSkippingLeadingTrivia)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func wrapExample(_ text: String, file: StaticString = #file, line: UInt = #line) -> Example {
|
|
return Example("""
|
|
class ViewController: UIViewController {
|
|
\(text)
|
|
}
|
|
""", file: file, line: line)
|
|
}
|