forked from realm/SwiftLint
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds opt-in discouraged_object_literal rule
Implements realm#1987.
- Loading branch information
1 parent
e574f1f
commit 690340f
Showing
7 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Source/SwiftLintFramework/Rules/DiscouragedObjectLiteralRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// DiscouragedObjectLiteralRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Ornithologist Coder on 1/3/18. | ||
// Copyright © 2018 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct DiscouragedObjectLiteralRule: ASTRule, OptInRule, ConfigurationProviderRule { | ||
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))] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters