Skip to content

Commit

Permalink
Add enforce_system_properties rule (lyft#55)
Browse files Browse the repository at this point in the history
* Add enforce_system_properties rule
  • Loading branch information
gabriellanata authored Mar 31, 2020
1 parent 2e4b2bf commit bc00cc1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ in the .xib or .storyboard file.

Ensures a system type uses a set of custom clases. Configure `system_classes` in a custom rule configuration using `rules_config` (see below).

- `enforce_system_properties`

Ensures a property in a system type is set to one of the allowed properties. Configure `system_properties` in a custom rule configuration using `rules_config` (see below). Use `null` as an option to allow default value.

## Usage

For a list of available rules, run `xiblint -h`.
Expand Down
2 changes: 1 addition & 1 deletion xiblint/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.9.12'
__version__ = '0.9.13'
37 changes: 37 additions & 0 deletions xiblint/rules/enforce_system_properties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from xiblint.rules import Rule
from xiblint.xibcontext import XibContext


class EnforceSystemProperties(Rule):
"""
Ensures unavailable system properties are not used.
Example configuration:
{
"system_properties": {
"label": {
"adjustsFontForContentSizeCategory": ["YES"]
},
"button": {
"reversesTitleShadowWhenHighlighted": [null, "NO"]
}
}
}
"""
def check(self, context): # type: (XibContext) -> None
system_properties = self.config.get('system_properties', {})

for tag_name in system_properties.keys():
for element in context.tree.findall('.//{}'.format(tag_name)):
tag_name = element.tag
enforced_properties = system_properties.get(tag_name)
for property_name in enforced_properties.keys():
property_allowed_values = enforced_properties.get(property_name)
property_value = element.get(property_name)

if property_value in property_allowed_values:
continue

options_string = '`, `'.join(map(str, property_allowed_values))
context.error(element, '`<{}>` property `{}` must use `{}` instead of `{}`.'
.format(tag_name, property_name, options_string, property_value))

0 comments on commit bc00cc1

Please sign in to comment.