-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Split rule object_literal into image_literal and color_literal #1587
Comments
That could be said about imageLiterals too. I'd be in favor of making it configurable to just check one or another, but I still think it shouldn't be a default rule. |
I disagree with this one. Technically you're right, since we used the images feature from SwiftGen before Xcode 8 was released. But since Xcode 8 there's no reason to stick to the SwiftGen feature as there's a new "single file" with all images: The Image Assets. Actually what SwiftGen does for images is exactly what the imageLiterals now do – it's an exact replacement. Both search the project for images and provide them in a way the compiler can auto-complete and ensure the resources are actually there. But when it comes to colors, it's different. With SwiftGen you have to add an additional file, e.g. named And there's one more difference between colors and images: We usually try to have as few colors as possible in a project, which I think is the goal for most developers and a best practice. That's not true for images though: You usually add more and more images with every new feature. |
Not exactly, because if you rename/remove an image, compilation would fail with SwiftGen, but |
Making it configurable suits me, too, by the way. I just want to throw out my custom rule and use something more community-approved. So if I can turn the one on and the other off with the object_literal rule, I'd be okay with that, too. But I'm still not convinced why enabling imageLiterals would hurt any project when a rule like |
WTF – seems like you're right, I didn't know that and just wanted to prove it wrong, to see that it really doesn't show an error. 😄 So what's the point of imageLiteral then, really. Just auto-completion? How sad ... okay sorry, now I agree on not making it default. I'm shocked right now ... 😅 |
I guess the literal is resolved in the compiler, which doesn't know about asset catalogs? But, yeah, it'd be awesome if they were able to check it. Anyway, if you still think it'd be helpful to make it a configuration, that'd be welcome. |
Now that the shock is over, here's what I now think: It's true that imageLiteral does not have any compile time existence check – BUT: In my custom rule above I only expect the user to replace So, I'm taking back that you convinced me, I still think that as a replacement to Having that said, if you still really don't want this, I would also volunteer for implementing the option on the rule, as you suggested. At least that's what I read as an implication from your last sentence? |
@marcelofabri You just gave me another reason not to use @Dschee To me, files created by SwiftGen don't belong in the For example a folder structure like below, with
This request sounds more like trying to make My vote is that Though I don't really see much harm in adding configurable properties to the object_literal:
enable_image: false
# or
enable_color: false |
@starr0stealer About SwiftGen and the inclusion into the Source folder: I actually put it there, but that doesn't mean SwiftLint is running with the files. We do have a best practice to put all files generated by SwiftGen into a folder named "Constants" within the "Sources" folder (see here). This folder we ignore (see an example .swiftlint.yml). So, that's not a problem and it's not at all the reason for this request. I think there must be a misunderstanding on your side here somewhere. Also I think you see the #imageLiteral in a wrong way: So small as the images are within the code, I highly doubt that the rationale behind introducing it (for the images at least) was the fact that you can see them within code. I think the preview information is much more useful in the The rationale behind |
@Dschee No misunderstanding on my part, I seen you don't use the If I gave the impression that I felt Literals were useless, I do apologize, not my intention. I merely expressed my dislike of them, I use When Apple announced the Literals, the main keynote they talked about was about seeing the Asset inline within the code, seeing them quickly gave the author validation they selected the right asset. Other keynotes they suggested this feature added was brevity and speed. Literals take up less space, and have auto-complete. Another feature of the Literals is that you can drag/drop the asset onto a line of code. Testing the code via running the App is the main way of ensuring no typo was made, if you see the image you typed the name of the Asset correctly. Best Practices are a collection of opinions, sometimes held with greater merit depending on the origin, i.e. from a Languages maintainer/author, or from a well respected firm/group. In my opinion, storing files generated within "Source/Constants" is not best practice for a few reasons. Main reason, the most accepted consensus of generated code is that it belongs in a build task, not source control, it is not authored by a developer but by a tool. Another reason I see, is the placement into the "Constants" folder, as the project grows you may author, by hand, file(s) that have Constant variables, now you will need to maintain the But I digress, opinions above isn't the desired topic here. The subject at hand is about two main topics. Splitting To touch the first topic, splitting into multiple rules. Having more rules isn't always the best route, mostly from a maintainability perspective. Splitting these would open up a chance to violate Then the topic of defaulting some but not all. That enforces a major opinion in the toolkit, why should one be treated "more useful" than the others. Instead, introducing configuration into a single rule provides both standpoints an acceptable solution. Those that have zero interest in using Literals can continue to not opt-in, while those that would like to use some by not all can disable the ones they do not want to use. |
I still think the best solution here would be supporting a configuration and keeping the rule as one (and opt-in). I just don't think there's enough consensus in the community for making it a default rule. Also, this rule is more useful when a person/team is using Xcode, but there are people who just use other IDEs or text editors. |
This fixes realm#1587 by implementing the discussed option config.
I just implemented said option in #1605. |
This fixes realm#1587 by implementing the discussed option config.
This fixes realm#1587 by implementing the discussed option config.
I just implemented a custom rule to prevent that any of my coworkers use the
UIImage(named: "...")
initializer since there's the#imageLiteral
now. It looks like this:Then I naturally thought "why is this not part of SwiftLint anyways?" and looked it up to find the object_literal rule added via #1067. I also read the discussion there which was about adding the rule by default or not. The consensus was that
#imageLiteral
could be turned on by default but there's problems with#colorLiteral
since some people don't use it – like we do, too. We use SwiftGen instead, which we find better since we can define the set of colors to use in a single file.My question now is: Why not split the object_literal rule into two, namely color_literal and image_literal and turn on image_literal by default?
The text was updated successfully, but these errors were encountered: