Skip to content
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

SwiftLint rules to encourage usage of SwiftGen #446

Open
AliSoftware opened this issue Jul 5, 2018 · 4 comments
Open

SwiftLint rules to encourage usage of SwiftGen #446

AliSoftware opened this issue Jul 5, 2018 · 4 comments
Milestone

Comments

@AliSoftware
Copy link
Collaborator

AliSoftware commented Jul 5, 2018

To encourage users to use SwiftGen instead of the String-based APIs, it would be nice to have SwiftLint rules that users can enable for their project.

  1. Here's what I have for now, using custom rules and RegEx:
custom_rules:
  swiftgen_assets:
    name: "SwiftGen Assets"
    regex: '(UIImage|UIColor)(\.init)?\(named: ?"?.+"?(, ?in:.+?, ?compatibleWith:.+?)?\)|#imageLiteral\(resourceName: ?".+"\)'
    message: "Use Asset.<asset> instead"
    severity: error
  swiftgen_colors:
    name: "SwiftGen Colors"
    regex: '(UIColor(\.init)?|#colorLiteral)\(((red|displayP3Red):.+?,green:.+?,blue:.+?,alpha:.+?)|(white:.+?,alpha:.+?)|(hue:.+?,saturation:.+?,brightness:.+?,alpha:.+?)\)'
    message: "Use ColorName.<color> instead"
    severity: error
  swiftgen_fonts:
    name: "SwiftGen Fonts"
    regex: 'UIFont(\.init)?\(name: ?"?.+"?, ?size:.+?\)'
    message: "Use FontFamily.<family>.<variant>.size(<size>) instead"
    severity: error
  swiftgen_storyboards:
    name: "SwiftGen Storyboards"
    regex: '(UIStoryboard\(name: ?"?.+"?, ?bundle:.+\))|(instantiateViewController\(withIdentifier:.+?\))|(instantiateInitialViewController\(\))'
    message: "Use StoryboardScene.<storyboad>.<scene>.instantiate() instead"
    severity: error
  swiftgen_strings:
    name: "SwiftGen Strings"
    regex: 'NSLocalizedString'
    message: "Use L10n.key instead"
    severity: error
  1. I haven't tested them all (that's just a first draft), I can see in another project that it detects assets and Fonts, but didn't test the other ones just yet), so it's quite open for improvement and for adding rules for other stuff

  2. Note that these are custom_rules, which use Regular Expressions to do the matching. It would be even better to create a PR in SwiftLint to add proper AST-based rules, so that it would analyse the semantics and not the text (allowing the rules to match even if there are some comments in-between the parameters, etc)


Next steps:

  • Feel free to improve those custom_rules and add or fix the regular expressions used above, so that we end up with a nice set of custom rules that we could share with all users in SwiftGen's wiki
  • Create a wiki page to host those custom rules for people wanting to use them in their project, until we have time to make an AST-based rule in SwiftLint via a PR
  • Create a PR in SwiftLint to add the AST-based rules for each type of asset covered by SwiftGen, making those rules more resilient and robust than the RegEx-based ones above.
@djbe
Copy link
Member

djbe commented Jul 5, 2018

The nice thing about (AST) SwiftLint rules is that we could provide autocorrect functionality, which would be 🔥

@AliSoftware
Copy link
Collaborator Author

mindblown

@AliSoftware AliSoftware changed the title SwiftLint rules SwiftLint rules to encourage usage of SwiftGen Jul 5, 2018
@djbe djbe added this to the Nice to Have milestone Sep 3, 2018
@djbe
Copy link
Member

djbe commented Oct 2, 2018

Storyboard segues:

  swiftgen_storyboard_segue_perform:
    name: "SwiftGen Storyboard Segue Identifier"
    regex: 'performSegue\(withIdentifier: ?"?.+"?, ?sender:.+\)'
    message: "Use perform(segue: StoryboardSegue.<storyboard>.<segue>, sender: <sender>) instead"
    severity: error
  swiftgen_storyboard_segue_prepare:
    name: "SwiftGen Storyboard Segue Prepare"
    regex: 'segue.identifier'
    message: "Use StoryboardSegue.<storyboard>(segue) instead"
    severity: error

Thoughts?

@AliSoftware
Copy link
Collaborator Author

Didn't check the regexes, but from the looks of it, LGTM👌

You could also omit the "sender:" pay for the first rule so that it works even if that sender is specified eg on the next line after a CRLF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants