Files
SwiftLint/Source/SwiftLintBuiltInRules/Rules/Lint/IBInspectableInExtensionRule.swift
Danny Mösch 40bd97038a Support arbitrary configurations in @SwiftSyntaxRule (#5275)
Almost all rules based on SwiftSyntax can be set up now by just adding
`@SwiftSyntaxRule` to the rule struct.
2023-10-16 19:34:43 +02:00

42 lines
1.2 KiB
Swift

import SwiftSyntax
@SwiftSyntaxRule
struct IBInspectableInExtensionRule: OptInRule {
var configuration = SeverityConfiguration<Self>(.warning)
static let description = RuleDescription(
identifier: "ibinspectable_in_extension",
name: "IBInspectable in Extension",
description: "Extensions shouldn't add @IBInspectable properties",
kind: .lint,
nonTriggeringExamples: [
Example("""
class Foo {
@IBInspectable private var x: Int
}
""")
],
triggeringExamples: [
Example("""
extension Foo {
↓@IBInspectable private var x: Int
}
""")
]
)
}
private extension IBInspectableInExtensionRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override var skippableDeclarations: [any DeclSyntaxProtocol.Type] {
.allExcept(ExtensionDeclSyntax.self, VariableDeclSyntax.self)
}
override func visitPost(_ node: AttributeSyntax) {
if node.attributeNameText == "IBInspectable" {
violations.append(node.positionAfterSkippingLeadingTrivia)
}
}
}
}