Skip to content

Commit

Permalink
feature(strict_color_names): Added a rule for color validation. (lyft#60
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dirtyhabits97 committed Mar 3, 2021
1 parent af66750 commit 2e14b59
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ in the .xib or .storyboard file.

- `named_colors`

Ensures all colors are using named colors from an asset catalog. Configure `ignore_alpha` (default is `false`) in a custom rule configuration using `rules_config` (see below) if you’d like to ignore colors with alpha.
Ensures all colors are using named colors from an asset catalog. Configure `allowed_colors` to limit the colors to a subset (default is all named colors are allowed), `allow_system_colors` (default is `false`) and `ignore_alpha` (default is `false`) in a custom rule configuration using `rules_config` (see below).

- `no_trait_variations`

Expand Down
31 changes: 25 additions & 6 deletions xiblint/rules/named_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ class NamedColors(Rule):
Example configuration:
{
"allowed_colors": ["CustomRed", "CustomGreen"],
"allow_system_colors": true,
"ignore_alpha": true
}
"""
def check(self, context): # type: (XibContext) -> None
allowed_colors = self.config.get('allowed_colors', [])
allow_system_colors = self.config.get('allow_system_colors', False)
ignore_alpha = self.config.get('ignore_alpha', False)

for element in context.tree.findall(".//color"):
Expand All @@ -24,16 +28,31 @@ def check(self, context): # type: (XibContext) -> None
if element.parent.tag == 'namedColor':
continue

# Skip <color> tags part of a system color definition
if element.parent.tag == 'systemColor':
continue

# Skip colors with alpha (if configured)
if ignore_alpha and element.get('alpha') != '1':
if ignore_alpha and element.get('alpha') is not None and element.get('alpha') != '1':
continue

# If `systemColor` or `catalog` is present, it's a named system color
if (
element.get('systemColor') is not None or
element.get('cocoaTouchSystemColor') is not None or
element.get('catalog') is not None
):
if not allow_system_colors:
context.error(element, "Use of named system colors is not allowed. Use a named color instead.")
continue

# Require a name
if element.get('name') is None:
color_name = element.get('name')
if color_name is None:
context.error(element, "Use of custom colors is not allowed. Use a named color instead.")
continue

# If `catalog` is present, it's a named system color
if element.get('catalog') is not None:
context.error(element, "Use of named system colors is not allowed. Use a named color instead.")
continue
# If allowed_colors is set, verify that color_name is included
if allowed_colors and color_name not in allowed_colors:
context.error(element, '"{}" is not one of the allowed colors: "{}".'
.format(color_name, allowed_colors))

0 comments on commit 2e14b59

Please sign in to comment.