tidy.py: Check for import blocks separated by whitespace

This enables flagging multiple import blocks separated by whitespace
as errors.

Fixes #7381.
This commit is contained in:
Brandon Fairchild 2015-09-17 17:42:01 -04:00
parent 7f2d81933a
commit e924393be8

View file

@ -201,6 +201,9 @@ def check_rust(file_name, contents):
comment_depth = 0
merged_lines = ''
import_block = False
whitespace = False
uses = []
mods = []
@ -224,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)
@ -291,22 +304,28 @@ 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 = ""
@ -345,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.