Browse Source

Extend lint checks to reject duplication of defaults (#25149)

Joel Challis 1 year ago
parent
commit
8244659b44
2 changed files with 104 additions and 0 deletions
  1. 73 0
      data/mappings/info_defaults.hjson
  2. 31 0
      lib/python/qmk/cli/lint.py

+ 73 - 0
data/mappings/info_defaults.hjson

@@ -0,0 +1,73 @@
+{
+    "bootmagic": {
+        "matrix": [0, 0]
+    },
+    "backlight": {
+        "default": {
+            "on": true
+        },
+        "breathing_period": 6,
+        "levels": 3,
+        "on_state": 1
+    },
+    "features": {
+        "command": false,
+        "console": false
+    },
+    "indicators": {
+        "on_state": 1
+    },
+    "led_matrix": {
+        "default": {
+            "animation": "solid",
+            "on": true,
+            "val": 255,
+            "speed": 128
+        },
+        "led_flush_limit": 16,
+        "max_brightness": 255,
+        "sleep": false,
+        "speed_steps": 16,
+        "val_steps": 16
+    },
+    "rgblight": {
+        "default": {
+            "animation": "static_light",
+            "on": true,
+            "hue": 0,
+            "sat": 255,
+            "val": 255,
+            "speed": 0
+        },
+        "brightness_steps": 17,
+        "hue_steps": 8,
+        "max_brightness": 255,
+        "saturation_steps": 17,
+        "sleep": false
+    },
+    "rgb_matrix": {
+        "default": {
+            "animation": "cycle_left_right",
+            "on": true,
+            "hue": 0,
+            "sat": 255,
+            "val": 255,
+            "speed": 128
+        },
+        "hue_steps": 8,
+        "led_flush_limit": 16,
+        "max_brightness": 255,
+        "sat_steps": 16,
+        "sleep": false,
+        "speed_steps": 16,
+        "val_steps": 16
+    },
+    "split": {
+        "serial": {
+            "driver": "bitbang"
+        }
+    },
+    "ws2812": {
+        "driver": "bitbang"
+    }
+}

+ 31 - 0
lib/python/qmk/cli/lint.py

@@ -1,5 +1,6 @@
 """Command to look over a keyboard/keymap and check for common mistakes.
 """
+from dotty_dict import dotty
 from pathlib import Path
 
 from milc import cli
@@ -11,6 +12,7 @@ from qmk.keymap import locate_keymap, list_keymaps
 from qmk.path import keyboard
 from qmk.git import git_get_ignored_files
 from qmk.c_parse import c_source_files, preprocess_c_file
+from qmk.json_schema import json_load
 
 CHIBIOS_CONF_CHECKS = ['chconf.h', 'halconf.h', 'mcuconf.h', 'board.h']
 INVALID_KB_FEATURES = set(['encoder_map', 'dip_switch_map', 'combo', 'tap_dance', 'via'])
@@ -214,6 +216,32 @@ def _rules_mk_assignment_only(rules_mk):
     return errors
 
 
+def _handle_duplicating_code_defaults(kb, info):
+    def _collect_dotted_output(kb_info_json, prefix=''):
+        """Print the info.json in a plain text format with dot-joined keys.
+        """
+        for key in sorted(kb_info_json):
+            new_prefix = f'{prefix}.{key}' if prefix else key
+
+            if isinstance(kb_info_json[key], dict):
+                yield from _collect_dotted_output(kb_info_json[key], new_prefix)
+            elif isinstance(kb_info_json[key], list):
+                # TODO: handle non primitives?
+                yield (new_prefix, kb_info_json[key])
+            else:
+                yield (new_prefix, kb_info_json[key])
+
+    defaults_map = json_load(Path('data/mappings/info_defaults.hjson'))
+    dotty_info = dotty(info)
+
+    for key, v_default in _collect_dotted_output(defaults_map):
+        v_info = dotty_info.get(key)
+        if v_default == v_info:
+            cli.log.warning(f'{kb}: Option "{key}" duplicates default value of "{v_default}"')
+
+    return True
+
+
 def keymap_check(kb, km):
     """Perform the keymap level checks.
     """
@@ -266,6 +294,9 @@ def keyboard_check(kb):  # noqa C901
     if not _handle_invalid_config(kb, kb_info):
         ok = False
 
+    if not _handle_duplicating_code_defaults(kb, kb_info):
+        ok = False
+
     invalid_files = git_get_ignored_files(f'keyboards/{kb}/')
     for file in invalid_files:
         if 'keymap' in file: