aboutsummaryrefslogtreecommitdiffstats
path: root/python/tidy.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/tidy.py')
-rw-r--r--python/tidy.py62
1 files changed, 55 insertions, 7 deletions
diff --git a/python/tidy.py b/python/tidy.py
index d637c9178f6..87e1cf142a7 100644
--- a/python/tidy.py
+++ b/python/tidy.py
@@ -201,8 +201,13 @@ def check_rust(file_name, contents):
comment_depth = 0
merged_lines = ''
+ import_block = False
+ whitespace = False
+
uses = []
+ mods = []
+
for idx, line in enumerate(contents):
# simplify the analysis
line = line.strip()
@@ -222,6 +227,16 @@ def check_rust(file_name, contents):
line = merged_lines + line
merged_lines = ''
+ # Keep track of whitespace to enable checking for a merged import block
+ #
+ # Ignore attributes, comments, and imports
+ if import_block:
+ if not (line_is_comment(line) or line_is_attribute(line) or line.startswith("use ")):
+ whitespace = line == ""
+
+ if not whitespace:
+ import_block = False
+
# get rid of strings and chars because cases like regex expression, keep attributes
if not line_is_attribute(line):
line = re.sub('".*?"|\'.*?\'', '', line)
@@ -289,22 +304,51 @@ def check_rust(file_name, contents):
if match and not (line.startswith("use") or line.startswith("pub use")):
yield (idx + 1, "missing space after {")
- # imports must be in the same line and alphabetically sorted
+ # imports must be in the same line, alphabetically sorted, and merged
+ # into a single import block
if line.startswith("use "):
+ import_block = True
use = line[4:]
if not use.endswith(";"):
yield (idx + 1, "use statement spans multiple lines")
- uses.append(use[:len(use) - 1])
- elif len(uses) > 0:
+ uses.append((use[:len(use) - 1], idx + 1))
+ elif len(uses) > 0 and whitespace or not import_block:
sorted_uses = sorted(uses)
for i in range(len(uses)):
- if sorted_uses[i] != uses[i]:
+ if sorted_uses[i][0] != uses[i][0]:
message = "use statement is not in alphabetical order"
- expected = "\n\t\033[93mexpected: {}\033[0m".format(sorted_uses[i])
- found = "\n\t\033[91mfound: {}\033[0m".format(uses[i])
- yield (idx + 1 - len(uses) + i, message + expected + found)
+ expected = "\n\t\033[93mexpected: {}\033[0m".format(sorted_uses[i][0])
+ found = "\n\t\033[91mfound: {}\033[0m".format(uses[i][0])
+ yield(uses[i][1], message + expected + found)
uses = []
+ if import_block and whitespace and line.startswith("use "):
+ whitespace = False
+ yield(idx, "encountered whitespace following a use statement")
+
+ # modules must be in the same line and alphabetically sorted
+ if line.startswith("mod ") or line.startswith("pub mod "):
+ mod = ""
+ if line.startswith("mod "):
+ mod = line[4:]
+ else:
+ mod = line[8:]
+
+ match = line.find(" {")
+ if match == -1:
+ if not mod.endswith(";"):
+ yield (idx + 1, "mod statement spans multiple lines")
+ mods.append(mod[:len(mod) - 1])
+ elif len(mods) > 0:
+ sorted_mods = sorted(mods)
+ for i in range(len(mods)):
+ if sorted_mods[i] != mods[i]:
+ message = "mod statement is not in alphabetical order"
+ expected = "\n\t\033[93mexpected: {}\033[0m".format(sorted_mods[i])
+ found = "\n\t\033[91mfound: {}\033[0m".format(mods[i])
+ yield (idx + 1 - len(mods) + i, message + expected + found)
+ mods = []
+
# Avoid flagging <Item=Foo> constructs
def is_associated_type(match, line, index):
@@ -320,6 +364,10 @@ def line_is_attribute(line):
return re.search(r"#\[.*\]", line)
+def line_is_comment(line):
+ return re.search(r"^//|^/\*|^\*", line)
+
+
def check_webidl_spec(file_name, contents):
# Sorted by this function (in pseudo-Rust). The idea is to group the same
# organization together.