mirror of
https://github.com/zulip/zulip.git
synced 2025-11-15 11:22:04 +00:00
lint: Extract check_file_for_pattern().
This commit is contained in:
@@ -11,6 +11,7 @@ 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, Tuple, Iterable
|
||||||
|
|
||||||
|
Rule = Dict[str, Any]
|
||||||
RuleList = List[Dict[str, Any]]
|
RuleList = List[Dict[str, Any]]
|
||||||
LineTup = Tuple[int, str, str, str]
|
LineTup = Tuple[int, str, str, str]
|
||||||
|
|
||||||
@@ -95,18 +96,25 @@ def get_rules_applying_to_fn(fn: str, rules: RuleList) -> RuleList:
|
|||||||
|
|
||||||
return rules_to_apply
|
return rules_to_apply
|
||||||
|
|
||||||
def custom_check_file(fn: str,
|
def check_file_for_pattern(fn: str,
|
||||||
|
line_tups: List[LineTup],
|
||||||
identifier: str,
|
identifier: str,
|
||||||
rules: RuleList,
|
|
||||||
color: Optional[Iterable[str]],
|
color: Optional[Iterable[str]],
|
||||||
max_length: Optional[int]=None) -> bool:
|
rule: Rule) -> bool:
|
||||||
failed = False
|
|
||||||
|
|
||||||
line_tups = get_line_info_from_file(fn=fn)
|
'''
|
||||||
|
DO NOT MODIFY THIS FUNCTION WITHOUT PROFILING.
|
||||||
|
|
||||||
rules_to_apply = get_rules_applying_to_fn(fn=fn, rules=rules)
|
This function gets called ~40k times, once per file per regex.
|
||||||
|
|
||||||
for rule in rules_to_apply:
|
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.
|
||||||
|
'''
|
||||||
exclude_lines = {
|
exclude_lines = {
|
||||||
line for
|
line for
|
||||||
(exclude_fn, line) in rule.get('exclude_line', set())
|
(exclude_fn, line) in rule.get('exclude_line', set())
|
||||||
@@ -114,7 +122,9 @@ def custom_check_file(fn: str,
|
|||||||
}
|
}
|
||||||
|
|
||||||
pattern = re.compile(rule['pattern'])
|
pattern = re.compile(rule['pattern'])
|
||||||
strip_rule = rule.get('strip')
|
strip_rule = rule.get('strip') # type: Optional[str]
|
||||||
|
|
||||||
|
ok = True
|
||||||
for (i, line, line_newline_stripped, line_fully_stripped) in line_tups:
|
for (i, line, line_newline_stripped, line_fully_stripped) in line_tups:
|
||||||
if line_fully_stripped in exclude_lines:
|
if line_fully_stripped in exclude_lines:
|
||||||
exclude_lines.remove(line_fully_stripped)
|
exclude_lines.remove(line_fully_stripped)
|
||||||
@@ -133,7 +143,7 @@ def custom_check_file(fn: str,
|
|||||||
print_err(identifier, color, '{} at {} line {}:'.format(
|
print_err(identifier, color, '{} at {} line {}:'.format(
|
||||||
rule['description'], fn, i+1))
|
rule['description'], fn, i+1))
|
||||||
print_err(identifier, color, line)
|
print_err(identifier, color, line)
|
||||||
failed = True
|
ok = False
|
||||||
except Exception:
|
except Exception:
|
||||||
print("Exception with %s at %s line %s" % (rule['pattern'], fn, i+1))
|
print("Exception with %s at %s line %s" % (rule['pattern'], fn, i+1))
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
@@ -141,6 +151,30 @@ def custom_check_file(fn: str,
|
|||||||
if exclude_lines:
|
if exclude_lines:
|
||||||
print('Please remove exclusions for file %s: %s' % (fn, exclude_lines))
|
print('Please remove exclusions for file %s: %s' % (fn, exclude_lines))
|
||||||
|
|
||||||
|
return ok
|
||||||
|
|
||||||
|
def custom_check_file(fn: str,
|
||||||
|
identifier: str,
|
||||||
|
rules: RuleList,
|
||||||
|
color: Optional[Iterable[str]],
|
||||||
|
max_length: Optional[int]=None) -> bool:
|
||||||
|
failed = False
|
||||||
|
|
||||||
|
line_tups = get_line_info_from_file(fn=fn)
|
||||||
|
|
||||||
|
rules_to_apply = get_rules_applying_to_fn(fn=fn, rules=rules)
|
||||||
|
|
||||||
|
for rule in rules_to_apply:
|
||||||
|
ok = check_file_for_pattern(
|
||||||
|
fn=fn,
|
||||||
|
line_tups=line_tups,
|
||||||
|
identifier=identifier,
|
||||||
|
color=color,
|
||||||
|
rule=rule,
|
||||||
|
)
|
||||||
|
if not ok:
|
||||||
|
failed = True
|
||||||
|
|
||||||
# TODO: Move the below into more of a framework.
|
# TODO: Move the below into more of a framework.
|
||||||
firstline = None
|
firstline = None
|
||||||
lastLine = None
|
lastLine = None
|
||||||
|
|||||||
Reference in New Issue
Block a user