Skip to content

Commit

Permalink
Add Enforce Runtime Attributes rule (lyft#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriellanata committed Apr 3, 2020
1 parent bc00cc1 commit 4c90835
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ in the .xib or .storyboard file.
Checks for labels with outlets into a view controller that have no accessibility identifiers.
Labels with outlets might get dynamic text, and therefore should be accessible to UI testing.

- `enforce_runtime_attributes`

Ensures a runtime attribute is set to one of the allowed values. Configure `runtime_attributes` in a custom rule configuration using `rules_config` (see below). Use `null` as an option to allow no value.

- `enforce_system_properties`

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

- `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.
Expand Down Expand Up @@ -76,10 +84,6 @@ 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.13'
__version__ = '0.9.14'
54 changes: 54 additions & 0 deletions xiblint/rules/enforce_runtime_attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from xiblint.rules import Rule
from xiblint.xibcontext import XibContext


class EnforceRuntimeAttributes(Rule):
"""
Ensures a runtime attribute is set to one of the allowed values.
You must specify a module as part of the class name if it is a custom type.
Example configuration:
{
"runtime_attributes": {
"SomeModule.LegacyButton": {
"sizeName": ["small", "large"]
},
"button": {
"layer.cornerRadius": [null]
}
}
}
"""
def check(self, context): # type: (XibContext) -> None
runtime_attributes = self.config.get('runtime_attributes', {})

for full_class_name in runtime_attributes.keys():
enforced_attributes = runtime_attributes.get(full_class_name)

# Get system or custom class elements
full_class_name_split = full_class_name.split(".")
if len(full_class_name_split) == 1:
elements = context.tree.findall(".//{}".format(full_class_name))
elif len(full_class_name_split) == 2:
elements = context.tree.findall(".//*[@customClass='{}'][@customModule='{}']"
.format(full_class_name_split[1], full_class_name_split[0]))

for element in elements:
for attribute_keyPath in enforced_attributes.keys():
attribute_allowed_values = enforced_attributes.get(attribute_keyPath)
attribute_value = None

attribute_list = element.find("./userDefinedRuntimeAttributes")
if attribute_list is not None:
attribute_element = attribute_list.find("./userDefinedRuntimeAttribute/[@keyPath='{}']"
.format(attribute_keyPath))
if attribute_element is not None:
attribute_value = attribute_element.get("value")

if attribute_value in attribute_allowed_values:
continue

options_string = '`, `'.join(map(str, attribute_allowed_values))
context.error(element, '`<{}>` runtime attribute `{}` must use `{}` instead of `{}`.'
.format(full_class_name, attribute_keyPath, options_string, attribute_value))
2 changes: 1 addition & 1 deletion xiblint/rules/enforce_system_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class EnforceSystemProperties(Rule):
"""
Ensures unavailable system properties are not used.
Ensures a property in a system type is set to one of the allowed values.
Example configuration:
{
Expand Down

0 comments on commit 4c90835

Please sign in to comment.