mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
Check for JSON key non-duplication and order
This commit is contained in:
parent
c62ce53efa
commit
d47aca18c4
4 changed files with 35 additions and 3 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue