diff options
author | Yuki Izumi <kivikakk@github.com> | 2016-11-07 19:02:22 +1100 |
---|---|---|
committer | Yuki Izumi <kivikakk@github.com> | 2016-11-07 19:02:22 +1100 |
commit | d47aca18c4bb199feb1cd4674defaa27ce71ee85 (patch) | |
tree | 3c78a320e385252ad6eeef7e9c5d1cfe8b79fa32 /python/tidy/servo_tidy/tidy.py | |
parent | c62ce53efa75c1d817669253036c2621caac88bf (diff) | |
download | servo-d47aca18c4bb199feb1cd4674defaa27ce71ee85.tar.gz servo-d47aca18c4bb199feb1cd4674defaa27ce71ee85.zip |
Check for JSON key non-duplication and order
Diffstat (limited to 'python/tidy/servo_tidy/tidy.py')
-rw-r--r-- | python/tidy/servo_tidy/tidy.py | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/python/tidy/servo_tidy/tidy.py b/python/tidy/servo_tidy/tidy.py index 98eb8fa2d94..45ad2f44638 100644 --- a/python/tidy/servo_tidy/tidy.py +++ b/python/tidy/servo_tidy/tidy.py @@ -27,6 +27,7 @@ CONFIG_FILE_PATH = os.path.join(".", "servo-tidy.toml") config = { "skip-check-length": False, "skip-check-licenses": False, + "check-ordered-json-keys": [], "ignore": { "files": [ "./.", # ignore hidden files @@ -636,23 +637,37 @@ def check_for_possible_duplicate_json_keys(key_value_pairs): seen_keys = set() for key in keys: if key in seen_keys: - raise KeyError(key) + raise KeyError("Duplicated Key (%s)" % key) seen_keys.add(key) +def check_for_alphabetical_sorted_json_keys(key_value_pairs): + for a, b in zip(key_value_pairs[:-1], key_value_pairs[1:]): + if a[0] > b[0]: + raise KeyError("Unordered key (found %s before %s)" % (a[0], b[0])) + + +def check_json_requirements(filename): + def check_fn(key_value_pairs): + check_for_possible_duplicate_json_keys(key_value_pairs) + if filename in config["check-ordered-json-keys"]: + check_for_alphabetical_sorted_json_keys(key_value_pairs) + return check_fn + + def check_json(filename, contents): if not filename.endswith(".json"): raise StopIteration try: - json.loads(contents, object_pairs_hook=check_for_possible_duplicate_json_keys) + json.loads(contents, object_pairs_hook=check_json_requirements(filename)) except ValueError as e: match = re.search(r"line (\d+) ", e.message) line_no = match and match.group(1) yield (line_no, e.message) except KeyError as e: - yield (None, "Duplicated Key (%s)" % e.message) + yield (None, e.message) def check_spec(file_name, lines): |