Skip to content

Commit 4c90835

Browse files
Add Enforce Runtime Attributes rule (#56)
1 parent bc00cc1 commit 4c90835

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ in the .xib or .storyboard file.
4040
Checks for labels with outlets into a view controller that have no accessibility identifiers.
4141
Labels with outlets might get dynamic text, and therefore should be accessible to UI testing.
4242

43+
- `enforce_runtime_attributes`
44+
45+
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.
46+
47+
- `enforce_system_properties`
48+
49+
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.
50+
4351
- `named_colors`
4452

4553
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.
@@ -76,10 +84,6 @@ in the .xib or .storyboard file.
7684

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

79-
- `enforce_system_properties`
80-
81-
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.
82-
8387
## Usage
8488

8589
For a list of available rules, run `xiblint -h`.

xiblint/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.9.13'
1+
__version__ = '0.9.14'
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from xiblint.rules import Rule
2+
from xiblint.xibcontext import XibContext
3+
4+
5+
class EnforceRuntimeAttributes(Rule):
6+
"""
7+
Ensures a runtime attribute is set to one of the allowed values.
8+
9+
You must specify a module as part of the class name if it is a custom type.
10+
11+
Example configuration:
12+
{
13+
"runtime_attributes": {
14+
"SomeModule.LegacyButton": {
15+
"sizeName": ["small", "large"]
16+
},
17+
"button": {
18+
"layer.cornerRadius": [null]
19+
}
20+
}
21+
}
22+
"""
23+
def check(self, context): # type: (XibContext) -> None
24+
runtime_attributes = self.config.get('runtime_attributes', {})
25+
26+
for full_class_name in runtime_attributes.keys():
27+
enforced_attributes = runtime_attributes.get(full_class_name)
28+
29+
# Get system or custom class elements
30+
full_class_name_split = full_class_name.split(".")
31+
if len(full_class_name_split) == 1:
32+
elements = context.tree.findall(".//{}".format(full_class_name))
33+
elif len(full_class_name_split) == 2:
34+
elements = context.tree.findall(".//*[@customClass='{}'][@customModule='{}']"
35+
.format(full_class_name_split[1], full_class_name_split[0]))
36+
37+
for element in elements:
38+
for attribute_keyPath in enforced_attributes.keys():
39+
attribute_allowed_values = enforced_attributes.get(attribute_keyPath)
40+
attribute_value = None
41+
42+
attribute_list = element.find("./userDefinedRuntimeAttributes")
43+
if attribute_list is not None:
44+
attribute_element = attribute_list.find("./userDefinedRuntimeAttribute/[@keyPath='{}']"
45+
.format(attribute_keyPath))
46+
if attribute_element is not None:
47+
attribute_value = attribute_element.get("value")
48+
49+
if attribute_value in attribute_allowed_values:
50+
continue
51+
52+
options_string = '`, `'.join(map(str, attribute_allowed_values))
53+
context.error(element, '`<{}>` runtime attribute `{}` must use `{}` instead of `{}`.'
54+
.format(full_class_name, attribute_keyPath, options_string, attribute_value))

xiblint/rules/enforce_system_properties.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class EnforceSystemProperties(Rule):
66
"""
7-
Ensures unavailable system properties are not used.
7+
Ensures a property in a system type is set to one of the allowed values.
88
99
Example configuration:
1010
{

0 commit comments

Comments
 (0)