diff options
author | coalman <tehcoalman@gmail.com> | 2017-04-17 03:45:30 -0400 |
---|---|---|
committer | coalman <tehcoalman@gmail.com> | 2017-04-18 14:56:03 -0400 |
commit | 57e74542ee0b33bf103064a194f4d7ba27ea5b49 (patch) | |
tree | bb92f0ff2aa4aa185e3f23b8ec02178f45b33a97 /python | |
parent | a754c10e79ef237b500bbf173eb7ba43529e51eb (diff) | |
download | servo-57e74542ee0b33bf103064a194f4d7ba27ea5b49.tar.gz servo-57e74542ee0b33bf103064a194f4d7ba27ea5b49.zip |
Make tidy check that opening and closing braces that begin a line do so with proper alignment.
Diffstat (limited to 'python')
-rw-r--r-- | python/tidy/servo_tidy/tidy.py | 15 | ||||
-rw-r--r-- | python/tidy/servo_tidy_tests/rust_tidy.rs | 9 | ||||
-rw-r--r-- | python/tidy/servo_tidy_tests/test_tidy.py | 2 |
3 files changed, 23 insertions, 3 deletions
diff --git a/python/tidy/servo_tidy/tidy.py b/python/tidy/servo_tidy/tidy.py index 1468d00bf42..ccca233db9c 100644 --- a/python/tidy/servo_tidy/tidy.py +++ b/python/tidy/servo_tidy/tidy.py @@ -450,6 +450,8 @@ def check_rust(file_name, lines): prev_crate = {} prev_mod = {} prev_feature_name = "" + indent = 0 + prev_indent = 0 decl_message = "{} is not in alphabetical order" decl_expected = "\n\t\033[93mexpected: {}\033[0m" @@ -458,6 +460,9 @@ def check_rust(file_name, lines): for idx, original_line in enumerate(lines): # simplify the analysis line = original_line.strip() + prev_indent = indent + indent = len(original_line) - len(line) + is_attribute = re.search(r"#\[.*\]", line) is_comment = re.search(r"^//|^/\*|^\*", line) @@ -577,11 +582,17 @@ def check_rust(file_name, lines): yield (idx + 1, "found an empty line following a {") prev_open_brace = line.endswith("{") + # ensure a line starting with { or } has a number of leading spaces that is a multiple of 4 + if line.startswith(("{", "}")): + match = re.match(r"(?: {4})* {1,3}([{}])", original_line) + if match: + if indent != prev_indent - 4: + yield (idx + 1, "space before {} is not a multiple of 4".format(match.group(1))) + # check alphabetical order of extern crates if line.startswith("extern crate "): # strip "extern crate " from the begin and ";" from the end crate_name = line[13:-1] - indent = len(original_line) - len(line) if indent not in prev_crate: prev_crate[indent] = "" if prev_crate[indent] > crate_name: @@ -616,7 +627,6 @@ def check_rust(file_name, lines): # into a single import block if line.startswith("use "): import_block = True - indent = len(original_line) - len(line) if not line.endswith(";") and '{' in line: yield (idx + 1, "use statement spans multiple lines") if '{ ' in line: @@ -645,7 +655,6 @@ def check_rust(file_name, lines): # modules must be in the same line and alphabetically sorted if line.startswith("mod ") or line.startswith("pub mod "): - indent = len(original_line) - len(line) # strip /(pub )?mod/ from the left and ";" from the right mod = line[4:-1] if line.startswith("mod ") else line[8:-1] diff --git a/python/tidy/servo_tidy_tests/rust_tidy.rs b/python/tidy/servo_tidy_tests/rust_tidy.rs index 7a65de0ffcc..bb1b4e00572 100644 --- a/python/tidy/servo_tidy_tests/rust_tidy.rs +++ b/python/tidy/servo_tidy_tests/rust_tidy.rs @@ -63,4 +63,13 @@ impl test { let var = "val"; + + fn test_fun4() + { + } + let var = if true { + "true" + } else { // Should not trigger + "false" + } // Should not trigger } diff --git a/python/tidy/servo_tidy_tests/test_tidy.py b/python/tidy/servo_tidy_tests/test_tidy.py index a50d92b241e..07dbaa020dc 100644 --- a/python/tidy/servo_tidy_tests/test_tidy.py +++ b/python/tidy/servo_tidy_tests/test_tidy.py @@ -131,6 +131,8 @@ class CheckTidiness(unittest.TestCase): self.assertEqual('extra space after (', errors.next()[2]) self.assertEqual('extra space after test_fun', errors.next()[2]) self.assertEqual('no = in the beginning of line', errors.next()[2]) + self.assertEqual('space before { is not a multiple of 4', errors.next()[2]) + self.assertEqual('space before } is not a multiple of 4', errors.next()[2]) self.assertNoMoreErrors(errors) feature_errors = tidy.collect_errors_for_files(iterFile('lib.rs'), [], [tidy.check_rust], print_text=False) |