mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
53 lines
1.8 KiB
Swift
53 lines
1.8 KiB
Swift
import SourceKittenFramework
|
|
|
|
public struct IBInspectableInExtensionRule: ConfigurationProviderRule, OptInRule, AutomaticTestableRule {
|
|
public var configuration = SeverityConfiguration(.warning)
|
|
|
|
public init() {}
|
|
|
|
public 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
|
|
}
|
|
""")
|
|
]
|
|
)
|
|
|
|
public func validate(file: SwiftLintFile) -> [StyleViolation] {
|
|
let collector = NamespaceCollector(dictionary: file.structureDictionary)
|
|
let elements = collector.findAllElements(of: [.extension])
|
|
|
|
return elements
|
|
.flatMap { element in
|
|
return element.dictionary.substructure.compactMap { element -> ByteCount? in
|
|
guard element.declarationKind == .varInstance,
|
|
element.enclosedSwiftAttributes.contains(.ibinspectable),
|
|
let offset = element.offset
|
|
else {
|
|
return nil
|
|
}
|
|
|
|
return offset
|
|
}
|
|
}
|
|
.map {
|
|
StyleViolation(ruleDescription: type(of: self).description,
|
|
severity: configuration.severity,
|
|
location: Location(file: file, byteOffset: $0))
|
|
}
|
|
}
|
|
}
|