Rule Request: Setting CALayer's .contents to a UIImage
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
New rule request
(This request is of course predicated on having type information during linting, which I'm not sure swiftlint has.)
Please describe the rule idea, format
this issue's title as Rule Request: [Rule Name] and describe:
- Why should this rule be added? Share links to existing discussion about what
the community thinks about this.
Setting a CALayer's contents to a UIImage results in the layer being transparent, which makes it hard to notice. I have personally encountered this a number of times and I can think of no reason one would purposely set a CALayer's contents to a UIImage.
- Provide several examples of what would and wouldn't trigger violations.
What would trigger violations:
class Foo: UIViewController {
@IBOutlet weak var aView: UIView!
let someLayer = CALayer()
let coolImage = UIImage(named: "So_Cool")!
init {
someLayer.contents = UIImage(named: "Some_Image")! // violation
someLayer.contents = #imageLiteral(resourceName: "Some_Image") // violation
}
override func viewDidLoad() {
someLayer.contents = coolImage // violation
let nonExistant: UIImage? = UIImage(named: "I_Dont_Exist") // notice this is an optional var
someLayer.contents = nonExistant // violation
// Even though nonExistant may be nil and that is a reasonable value for a layer's contents
// chances are that isn't what you're intending
someLayer.contents = getAUIImage() // violation
}
func getAUIImage() -> UIImage {
return #imageLiteral(resourceName: "Its_Images_All_The_Way_Down")
}
}
What would not trigger violations:
class Foo: UIViewController {
@IBOutlet weak var aView: UIView!
let someLayer = CALayer()
let coolCGImage = UIImage(named: "So_Cool")!.cgImage
init {
someLayer.contents = UIImage(named: "Some_Image")?.cgImage // no violation
someLayer.contents = getTheCGImage(#imageLiteral(resourceName: "Some_Image") // no violation
}
private func getTheCGImage(image: UIImage) -> CGImage {
return image.cgImage
}
override func viewDidLoad() {
someLayer.contents = coolCGImage // no violation
let nonExistant: CGImage? = UIImage(named: "I_Dont_Exist")?.cgImage
someLayer.contents = nonExistant // no violation
someLayer.contents = getACGImage() // no violation
}
func getACGImage() -> CGImage {
return #imageLiteral(resourceName: "Its_Images_All_The_Way_Down").cgImage!
}
}
- Should the rule be configurable, if so what parameters should be configurable?
I believe being able to configure white/black lists for layer contents would be useful. If it is possible it would also be hella cool to be able to define white/black lists for any property (or if you want to go crazy, even function parameters). For example:
allowed_property_types:
- whitelists:
- CALayer.contents:
- CGImage
- CGColor
- blacklists:
- SomeLibraryType.anAnyVar
- ThisTypeDoesntWork
- Should the rule be opt-in or enabled by default? Why?
See README.md for guidelines on when to mark a rule as opt-in.
I can imagine it being pretty slow, and if so I would say it should be opt-in. However, if it is reasonably fast it should be enabled by default because it shouldn't have many false positives and would be incredibly helpful for flagging almost assured errors.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with README.md's opt-in rules guidance and investigate whether SwiftLint currently has the type information needed for these examples. Compare the proposed CALayer.contents assignments with the UIImage and CGImage cases, then determine the rule's scope, configurability, and default status.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100