zulint: Add `--verbose` arg to print good/bad lines in error.

This argument probably still needs some iteration, but it's already
useful.
This commit is contained in:
Aman Agrawal 2019-06-23 17:11:06 +05:30 committed by Tim Abbott
parent 8eee19a160
commit 8fc7c89874
3 changed files with 17 additions and 5 deletions

View File

@ -88,7 +88,7 @@ def run():
def custom_py(): def custom_py():
# type: () -> int # type: () -> int
"""Runs custom checks for python files (config: tools/linter_lib/custom_check.py)""" """Runs custom checks for python files (config: tools/linter_lib/custom_check.py)"""
failed = python_rules.check(by_lang) failed = python_rules.check(by_lang, verbose=args.verbose)
return 1 if failed else 0 return 1 if failed else 0
@linter_config.lint @linter_config.lint
@ -97,7 +97,7 @@ def run():
"""Runs custom checks for non-python files (config: tools/linter_lib/custom_check.py)""" """Runs custom checks for non-python files (config: tools/linter_lib/custom_check.py)"""
failed = False failed = False
for rule in non_py_rules: for rule in non_py_rules:
failed = failed or rule.check(by_lang) failed = failed or rule.check(by_lang, verbose=args.verbose)
return 1 if failed else 0 return 1 if failed else 0
@linter_config.lint @linter_config.lint

View File

@ -43,6 +43,9 @@ def add_default_linter_arguments(parser):
type=split_arg_into_list, type=split_arg_into_list,
help='Only run linter for languages in the group(s), e.g.: ' help='Only run linter for languages in the group(s), e.g.: '
'--groups=backend,frontend') '--groups=backend,frontend')
parser.add_argument('--verbose', '-v',
action='store_true',
help='Print verbose output where available')
def split_arg_into_list(arg): def split_arg_into_list(arg):
# type: (str) -> List[str] # type: (str) -> List[str]

View File

@ -6,7 +6,7 @@ from __future__ import absolute_import
import re import re
import traceback import traceback
from zulint.printer import print_err, colors from zulint.printer import print_err, colors, GREEN, BOLDRED, ENDC, MAGENTA
if False: if False:
from typing import Any, Dict, List, Optional, Tuple, Iterable from typing import Any, Dict, List, Optional, Tuple, Iterable
@ -28,6 +28,7 @@ class RuleList:
self.shebang_rules = shebang_rules self.shebang_rules = shebang_rules
# Exclude the files in this folder from rules # Exclude the files in this folder from rules
self.exclude_files_in = "\\" self.exclude_files_in = "\\"
self.verbose = False
def get_line_info_from_file(self, fn): def get_line_info_from_file(self, fn):
# type: (str) -> List[LineTup] # type: (str) -> List[LineTup]
@ -125,6 +126,13 @@ class RuleList:
print_err(identifier, color, '{} at {} line {}:'.format( print_err(identifier, color, '{} at {} line {}:'.format(
rule['description'], fn, line_number)) rule['description'], fn, line_number))
print_err(identifier, color, line) print_err(identifier, color, line)
if self.verbose:
if rule.get('good_lines'):
print_err(identifier, color, GREEN + " Good code: {}{}".format(
(MAGENTA + " | " + GREEN).join(rule['good_lines']), ENDC))
if rule.get('bad_lines'):
print_err(identifier, color, BOLDRED + " Bad code: {}{}".format(
(MAGENTA + " | " + BOLDRED).join(rule['bad_lines']), ENDC))
def check_file_for_long_lines(self, def check_file_for_long_lines(self,
fn, fn,
@ -202,8 +210,8 @@ class RuleList:
return failed return failed
def check(self, by_lang): def check(self, by_lang, verbose=False):
# type: (Dict[str, List[str]]) -> bool # type: (Dict[str, List[str]], bool) -> bool
# By default, a rule applies to all files within the extension for # By default, a rule applies to all files within the extension for
# which it is specified (e.g. all .py files) # which it is specified (e.g. all .py files)
# There are three operators we can use to manually include or exclude files from linting for a rule: # There are three operators we can use to manually include or exclude files from linting for a rule:
@ -215,6 +223,7 @@ class RuleList:
# 'include_only': 'set([<path>, ...])' - includes only those files where <path> is a # 'include_only': 'set([<path>, ...])' - includes only those files where <path> is a
# substring of the filepath. # substring of the filepath.
failed = False failed = False
self.verbose = verbose
for lang in self.langs: for lang in self.langs:
color = next(colors) color = next(colors)
for fn in by_lang[lang]: for fn in by_lang[lang]: