Open LinusU opened 5 years ago
This is indeed a surprising and annoying feature of image literals! To correctly check for this, you would have to check that the asset catalog containing the image is available in the target(s) that use the image literal. But maybe some simplified heuristics would be enough for many users.
I would still recommend using SwiftGen instead for a more stable solution to this problem.
I would still recommend using SwiftGen instead for a more stable solution to this problem.
Personally, I prefer literals since they are statically analyzable, and doesn't require a separate tool for code generation. Each to their own though!
But maybe some simplified heuristics would be enough for many users.
Tried some very simple heuristics in my project and it works really well, but we only have a single asset catalog though...
I'm in favor of this, but it would need some design discussion first. Right now, SwiftLint completely ignores anything that isn't in a Swift file, which includes collecting which assets are defined, and in which modules.
New rule request
Referencing a non-existing asset with
#imageLiteral()
will result in a crash at runtime. As seen e.g. here and as experienced by me today, it's very surprising that#imageLiteral()
is not checked by the Swift compiler in the first place. I think the next best thing after that would be to have an error lint about it, which is why I created this suggestion ☺️The following code would trigger a violation:
The following code would not trigger a violation:
No.
I think the rule should be enabled by default (unless the implementation turns out to be slow) since it's a problem that will cause a crash at runtime, and I don't see that there will be any false positives.
I'd be willing to help contribute code for this ☺️