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

Rule Request: Discouraged Object Literal (Opt-In) #1987

Closed
ornithocoder opened this Issue Jan 2, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@ornithocoder
Contributor

ornithocoder commented Jan 2, 2018

SwiftLint provides a rule to enforce the use of literals over UIImage and UIColor initializer, but while object_literal makes sense for some projects and users, it doesn't for others.

Some of the reasons not to use literals:

  1. Icons and colors (but specially icons) can be really hard to identify
    1. because they're teeny-tiny rendered, and/or
    2. against dark background of some Xcode color schemes
  2. When reading the code, it's hard to know which color or image is being used. There's no context, no asset or color name, no immediate information about it.
    • To learn more about the color or image being used, one has to stop reading the code and browse through additional code to find more about these.
  3. etc...

Below, the same image literal example with four different Xcode templates.

Default:

screen shot 2018-01-02 at 9 19 31 pm

Civic:

screen shot 2018-01-02 at 11 09 43 pm

Nord:

screen shot 2018-01-02 at 9 19 07 pm

Ayu 2:

screen shot 2018-01-02 at 9 19 23 pm

As shown above, the icon is hard to visualize when using dark background color and there's no information about the asset used (which asset is being used?, what's its name?).

A custom rule can be used as workaround to disable image literals while the new opt-in rule isn't implemented and merged into master.

  discouraged_image_literal:
    name: "Discouraged Image Literal"
    regex: "#imageLiteral"
    message: "Please use UIImage(named:) instead"
    severity: warning

A similar custom rule can be easily implemented for color literals.

@ornithocoder

This comment has been minimized.

Show comment
Hide comment
@ornithocoder

ornithocoder Jan 2, 2018

Contributor

@marcelofabri @jpsim please let me know if the rule makes sense for SwiftLint. If it does, I'll work on it.

Contributor

ornithocoder commented Jan 2, 2018

@marcelofabri @jpsim please let me know if the rule makes sense for SwiftLint. If it does, I'll work on it.

@ornithocoder ornithocoder changed the title from Rule Request: Discouraged Object Literal to Rule Request: Discouraged Object Literal (Opt-In) Jan 2, 2018

ornithocoder added a commit to ornithocoder/personal-fork-swiftlint that referenced this issue Jan 2, 2018

jpsim added a commit to ornithocoder/personal-fork-swiftlint that referenced this issue Jan 4, 2018

@jpsim jpsim closed this Jan 4, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment