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

Add enforce_system_properties rule #55

Merged
merged 6 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))