IBDecodable / IBLinter

A linter tool for Interface Builder
MIT License
950 stars 40 forks source link

Use Base Class Rule #84

Closed masamichiueta closed 5 years ago

masamichiueta commented 5 years ago

This PR is a proposal of a new rule, UseBaseClass.

What is UseBaseClass rule?

UseBaseClass rule is a rule to check whether an UI element uses custom class which is set as base class in config.

For example, if we have some base classes of UILabel like PrimaryLabel and SecondaryLabel that should be applied to all labels through the project, this rule checks whether labels in storyboards and xibs are set to base classes.

Why is this rule required?

As I describe above, many apps commonly have their own base UI components. But we often forget to set the custom class to the base classes in storyboard or xib. And new members often overlook the base components.

This rule can prevent the problem and this is the reason why I developed this rule.

How does it work?

Set element_class and base_classes in config like

use_base_class_rule:
  - element_class: UILabel
    base_classes:
      - PrimaryLabel
      - SecondaryLabel 

This enables to check UILabel uses PrimaryLabel or SecondaryLabel. If UILabel does not use the base classes, Xcode shows warnings like below.

Screen Shot 2019-03-11 at 18 53 35
masamichiueta commented 5 years ago

@kateinoigakukun Thank you for your quick review!

this rule can't work for inherited class because IB doesn't know about Swift type information

[Q] What do you mean about inherited class?

kateinoigakukun commented 5 years ago

@masamichiueta Oh, I misunderstood 😅. I mean PrimaryLabel can be inherited. Do you think that CustomizedPrimaryLabel should be invalid? If so, it's my misunderstanding and you don't need to change README

class PrimaryLabel: UILabel {
    ...
}

class CustomizedPrimaryLabel: PrimaryLabel {
    ... 
}
masamichiueta commented 5 years ago

I understand! As you said, this rule does not work for inherited classes. A developer needs to add the inherited classes to base_classes in yml.

I add caution on rule description👍

masamichiueta commented 5 years ago

Updated. Please review again🙏