Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for multiple import blocks separated by whitespace #7662

Merged
merged 2 commits into from Sep 19, 2015
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

tidy.py: Check for import blocks separated by whitespace

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

Fixes #7381.
  • Loading branch information
nerith committed Sep 19, 2015
commit e924393be8cce6cb16d3af5a13ed9f634670f843
@@ -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.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.