mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
37 lines
1.6 KiB
Swift
37 lines
1.6 KiB
Swift
import SourceKittenFramework
|
|
|
|
public struct DiscouragedObjectLiteralRule: ASTRule, OptInRule, ConfigurationProviderRule, AutomaticTestableRule {
|
|
public var configuration = SeverityConfiguration(.warning)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "discouraged_object_literal",
|
|
name: "Discouraged Object Literal",
|
|
description: "Prefer initializers over object literals.",
|
|
kind: .idiomatic,
|
|
nonTriggeringExamples: [
|
|
"let image = UIImage(named: aVariable)",
|
|
"let image = UIImage(named: \"interpolated \\(variable)\")",
|
|
"let color = UIColor(red: value, green: value, blue: value, alpha: 1)",
|
|
"let image = NSImage(named: aVariable)",
|
|
"let image = NSImage(named: \"interpolated \\(variable)\")",
|
|
"let color = NSColor(red: value, green: value, blue: value, alpha: 1)"
|
|
],
|
|
triggeringExamples: [
|
|
"let image = ↓#imageLiteral(resourceName: \"image.jpg\")",
|
|
"let color = ↓#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)"
|
|
]
|
|
)
|
|
|
|
public func validate(file: File,
|
|
kind: SwiftExpressionKind,
|
|
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
|
|
guard let offset = dictionary.offset, kind == .objectLiteral else { return [] }
|
|
|
|
return [StyleViolation(ruleDescription: type(of: self).description,
|
|
severity: configuration.severity,
|
|
location: Location(file: file, byteOffset: offset))]
|
|
}
|
|
}
|