Skip to content

Commit

Permalink
lint: Extract check_file_for_pattern().
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Howell committed Nov 10, 2018
1 parent 9d1fcba commit b59a36e
Showing 1 changed file with 63 additions and 24 deletions.
87 changes: 63 additions & 24 deletions tools/linter_lib/custom_check.py
Expand Up @@ -9,8 +9,10 @@

from zulint.printer import print_err, colors

from typing import cast, Any, Callable, Dict, List, Optional, Tuple, Iterable
from typing import cast, Any, Callable, Dict, List, Optional, Set, Tuple, Iterable
from typing.re import Pattern

Rule = Dict[str, Any]
RuleList = List[Dict[str, Any]]
LineTup = Tuple[int, str, str, str]

Expand Down Expand Up @@ -96,6 +98,54 @@ def get_rules_applying_to_fn(fn: str, rules: RuleList) -> RuleList:

return rules_to_apply

def check_file_for_pattern(fn: str,
line_tups: List[LineTup],
pattern: Pattern,
identifier: str,
color: Optional[Iterable[str]],
rule: Rule,
strip_rule: Optional[str],
exclude_lines: Set[str]) -> bool:

'''
DO NOT MODIFY THIS FUNCTION WITHOUT PROFILING.
This function gets called ~40k times, once per file per regex.
Inside it's doing a regex check for every line in the file, so
it's important to do things like pre-compiling regexes.
DO NOT INLINE THIS FUNCTION.
We need to see it show up in profiles, and the function call
overhead will never be a bottleneck.
'''
ok = True
for (i, line, line_newline_stripped, line_fully_stripped) in line_tups:
if line_fully_stripped in exclude_lines:
exclude_lines.remove(line_fully_stripped)
continue
try:
line_to_check = line_fully_stripped
if strip_rule is not None:
if strip_rule == '\n':
line_to_check = line_newline_stripped
else:
raise Exception("Invalid strip rule")
if pattern.search(line_to_check):
if rule.get("exclude_pattern"):
if re.search(rule['exclude_pattern'], line_to_check):
continue
print_err(identifier, color, '{} at {} line {}:'.format(
rule['description'], fn, i+1))
print_err(identifier, color, line)
ok = False
except Exception:
print("Exception with %s at %s line %s" % (rule['pattern'], fn, i+1))
traceback.print_exc()

return ok

def custom_check_file(fn: str,
identifier: str,
rules: RuleList,
Expand All @@ -115,29 +165,18 @@ def custom_check_file(fn: str,
}

pattern = re.compile(rule['pattern'])
strip_rule = rule.get('strip')
for (i, line, line_newline_stripped, line_fully_stripped) in line_tups:
if line_fully_stripped in exclude_lines:
exclude_lines.remove(line_fully_stripped)
continue
try:
line_to_check = line_fully_stripped
if strip_rule is not None:
if strip_rule == '\n':
line_to_check = line_newline_stripped
else:
raise Exception("Invalid strip rule")
if pattern.search(line_to_check):
if rule.get("exclude_pattern"):
if re.search(rule['exclude_pattern'], line_to_check):
continue
print_err(identifier, color, '{} at {} line {}:'.format(
rule['description'], fn, i+1))
print_err(identifier, color, line)
failed = True
except Exception:
print("Exception with %s at %s line %s" % (rule['pattern'], fn, i+1))
traceback.print_exc()
strip_rule = rule.get('strip') # type: Optional[str]

ok = check_file_for_pattern(
fn=fn,
line_tups=line_tups,
pattern=pattern,
identifier=identifier,
color=color,
rule=rule,
strip_rule=strip_rule,
exclude_lines=exclude_lines,
)

if exclude_lines:
print('Please remove exclusions for file %s: %s' % (fn, exclude_lines))
Expand Down

0 comments on commit b59a36e

Please sign in to comment.