servo: Merge #12318 - [tidy] prevent duplicate json keys (from tallowen:tidy-features); r=frewsxcv

Throws a tidy error when duplicated keys are present.
![screenshot from 2016-07-07 16-54-17](https://cloud.githubusercontent.com/assets/557689/16658260/fd19cece-4465-11e6-89b1-a79296ba6a72.png)

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #12283 (github issue number if applicable).

<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

Source-Repo: https://github.com/servo/servo
Source-Revision: 638eb80110cf07ec2f07e43fd64a48042fe2925b
This commit is contained in:
Owen Coutts
2016-07-10 19:15:35 -07:00
parent e9cb41e689
commit 4b5b98d018
3 changed files with 25 additions and 1 deletions

View File

@@ -524,16 +524,28 @@ def check_webidl_spec(file_name, contents):
yield (0, "No specification link found.")
def check_for_possible_duplicate_json_keys(key_value_pairs):
keys = [x[0] for x in key_value_pairs]
seen_keys = set()
for key in keys:
if key in seen_keys:
raise KeyError(key)
seen_keys.add(key)
def check_json(filename, contents):
if not filename.endswith(".json"):
raise StopIteration
try:
json.loads(contents)
json.loads(contents, object_pairs_hook=check_for_possible_duplicate_json_keys)
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)
def check_spec(file_name, lines):