Skip to content

Commit

Permalink
Merge pull request #68 from w3c/lint-whitelist
Browse files Browse the repository at this point in the history
Refactor the lint whitelist handling.
  • Loading branch information
jgraham committed May 9, 2016
2 parents 8adcfae + 9a8cbfc commit eff67d5
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions lint/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def set_type(error_type, errors):
return [(error_type,) + error for error in errors]

def parse_whitelist_file(filename):
"""
Parse the whitelist file at `filename`, and return the parsed structure.
"""

data = defaultdict(lambda:defaultdict(set))

with open(filename) as f:
Expand All @@ -69,29 +73,27 @@ def parse_whitelist_file(filename):
error_type, file_match, line_number = parts
data[file_match][error_type].add(line_number)

def inner(path, errors):
whitelisted = [False for item in xrange(len(errors))]
return data

for file_match, whitelist_errors in data.iteritems():
if fnmatch.fnmatch(path, file_match):
for i, (error_type, msg, line) in enumerate(errors):
if "*" in whitelist_errors:
whitelisted[i] = True
elif error_type in whitelist_errors:
allowed_lines = whitelist_errors[error_type]
if None in allowed_lines or line in allowed_lines:
whitelisted[i] = True

return [item for i, item in enumerate(errors) if not whitelisted[i]]
return inner
def filter_whitelist_errors(data, path, errors):
"""
Filter out those errors that are whitelisted in `data`.
"""

_whitelist_fn = None
def whitelist_errors(path, errors):
global _whitelist_fn
whitelisted = [False for item in xrange(len(errors))]

if _whitelist_fn is None:
_whitelist_fn = parse_whitelist_file(os.path.join(repo_root, "lint.whitelist"))
return _whitelist_fn(path, errors)
for file_match, whitelist_errors in data.iteritems():
if fnmatch.fnmatch(path, file_match):
for i, (error_type, msg, line) in enumerate(errors):
if "*" in whitelist_errors:
whitelisted[i] = True
elif error_type in whitelist_errors:
allowed_lines = whitelist_errors[error_type]
if None in allowed_lines or line in allowed_lines:
whitelisted[i] = True

return [item for i, item in enumerate(errors) if not whitelisted[i]]

class Regexp(object):
pattern = None
Expand Down Expand Up @@ -265,8 +267,10 @@ def lint(paths):
error_count = defaultdict(int)
last = None

whitelist = parse_whitelist_file(os.path.join(repo_root, "lint.whitelist"))

def run_lint(path, fn, last, *args):
errors = whitelist_errors(path, fn(path, *args))
errors = filter_whitelist_errors(whitelist, path, fn(path, *args))
if errors:
last = (errors[-1][0], path)

Expand Down

0 comments on commit eff67d5

Please sign in to comment.