Annotate tools/lint-all.

This commit is contained in:
Eklavya Sharma 2016-07-24 18:07:56 +05:30
parent ec6b630537
commit bd0fa3e77b
2 changed files with 29 additions and 15 deletions

View File

@ -9,6 +9,7 @@ import subprocess
import traceback import traceback
try: try:
import lister import lister
from typing import cast, Any, Callable, Dict, List, Optional
except ImportError as e: except ImportError as e:
print("ImportError: {}".format(e)) print("ImportError: {}".format(e))
print("You need to run the Zulip linters inside a Zulip dev environment.") print("You need to run the Zulip linters inside a Zulip dev environment.")
@ -50,9 +51,9 @@ puppet/apt/README.md
static/locale static/locale
""".split() """.split()
by_lang = lister.list_files(args, modified_only=options.modified, use_shebang=True, by_lang = cast(Dict[str, List[str]], lister.list_files(args, modified_only=options.modified,
ftypes=['py', 'sh', 'js', 'pp', 'css', 'handlebars', 'html', 'json', 'md', 'txt', 'text'], ftypes=['py', 'sh', 'js', 'pp', 'css', 'handlebars', 'html', 'json', 'md', 'txt', 'text'],
group_by_ftype=True, exclude=exclude) use_shebang=True, group_by_ftype=True, exclude=exclude))
# Invoke the appropriate lint checker for each language, # Invoke the appropriate lint checker for each language,
# and also check files for extra whitespace. # and also check files for extra whitespace.
@ -66,6 +67,7 @@ else:
logger.setLevel(logging.WARNING) logger.setLevel(logging.WARNING)
def check_pyflakes(): def check_pyflakes():
# type: () -> bool
if not by_lang['py']: if not by_lang['py']:
return False return False
failed = False failed = False
@ -92,7 +94,10 @@ def check_pyflakes():
failed = True failed = True
return failed return failed
RuleList = List[Dict[str, Any]]
def custom_check_file(fn, rules, skip_rules=None, max_length=None): def custom_check_file(fn, rules, skip_rules=None, max_length=None):
# type: (str, RuleList, Optional[Any], Optional[int]) -> bool
failed = False failed = False
lineFlag = False lineFlag = False
for i, line in enumerate(open(fn)): for i, line in enumerate(open(fn)):
@ -144,7 +149,7 @@ whitespace_rules = [
'strip': '\n', 'strip': '\n',
'exclude': set(['zerver/lib/bugdown/codehilite.py']), 'exclude': set(['zerver/lib/bugdown/codehilite.py']),
'description': 'Fix tab-based whitespace'}, 'description': 'Fix tab-based whitespace'},
] ] # type: RuleList
markdown_whitespace_rules = list([rule for rule in whitespace_rules if rule['pattern'] != '\s+$']) + [ markdown_whitespace_rules = list([rule for rule in whitespace_rules if rule['pattern'] != '\s+$']) + [
# Two spaces trailing a line with other content is okay--it's a markdown line break. # Two spaces trailing a line with other content is okay--it's a markdown line break.
# This rule finds one space trailing a non-space, three or more trailing spaces, and # This rule finds one space trailing a non-space, three or more trailing spaces, and
@ -152,8 +157,8 @@ markdown_whitespace_rules = list([rule for rule in whitespace_rules if rule['pat
{'pattern': '((?<!\s)\s$)|(\s\s\s+$)|(^\s+$)', {'pattern': '((?<!\s)\s$)|(\s\s\s+$)|(^\s+$)',
'strip': '\n', 'strip': '\n',
'description': 'Fix trailing whitespace'}, 'description': 'Fix trailing whitespace'},
] ] # type: RuleList
js_rules = [ js_rules = cast(RuleList, [
{'pattern': '[^_]function\(', {'pattern': '[^_]function\(',
'description': 'The keyword "function" should be followed by a space'}, 'description': 'The keyword "function" should be followed by a space'},
{'pattern': '.*blueslip.warning\(.*', {'pattern': '.*blueslip.warning\(.*',
@ -191,8 +196,8 @@ js_rules = [
'exclude': set(['tools/lint-all']), 'exclude': set(['tools/lint-all']),
'description': 'Argument to report_error should be a literal string enclosed ' 'description': 'Argument to report_error should be a literal string enclosed '
'by i18n.t()'}, 'by i18n.t()'},
] + whitespace_rules ]) + whitespace_rules
python_rules = [ python_rules = cast(RuleList, [
{'pattern': '^(?!#)@login_required', {'pattern': '^(?!#)@login_required',
'description': '@login_required is unsupported; use @zulip_login_required'}, 'description': '@login_required is unsupported; use @zulip_login_required'},
{'pattern': '".*"%\([a-z_].*\)?$', {'pattern': '".*"%\([a-z_].*\)?$',
@ -273,11 +278,11 @@ python_rules = [
'description': 'Argument to JsonableError should be a literal string enclosed by _()'}, 'description': 'Argument to JsonableError should be a literal string enclosed by _()'},
{'pattern': '([a-zA-Z0-9_]+)=REQ\([\'"]\\1[\'"]', {'pattern': '([a-zA-Z0-9_]+)=REQ\([\'"]\\1[\'"]',
'description': 'REQ\'s first argument already defaults to parameter name'}, 'description': 'REQ\'s first argument already defaults to parameter name'},
] + whitespace_rules ]) + whitespace_rules
bash_rules = [ bash_rules = [
{'pattern': '#!.*sh [-xe]', {'pattern': '#!.*sh [-xe]',
'description': 'Fix shebang line with proper call to /usr/bin/env for Bash path, change -x|-e switches to set -x|set -e'}, 'description': 'Fix shebang line with proper call to /usr/bin/env for Bash path, change -x|-e switches to set -x|set -e'},
] + whitespace_rules[0:1] ] + whitespace_rules[0:1] # type: RuleList
css_rules = [ css_rules = [
{'pattern': '^[^:]*:\S[^:]*;$', {'pattern': '^[^:]*:\S[^:]*;$',
'description': "Missing whitespace after : in CSS"}, 'description': "Missing whitespace after : in CSS"},
@ -285,23 +290,24 @@ css_rules = [
'description': "Missing whitespace before '{' in CSS."}, 'description': "Missing whitespace before '{' in CSS."},
{'pattern': '{\w', {'pattern': '{\w',
'description': "Missing whitespace after '{' in CSS (should be newline)."}, 'description': "Missing whitespace after '{' in CSS (should be newline)."},
] + whitespace_rules ] + whitespace_rules # type: RuleList
handlebars_rules = whitespace_rules handlebars_rules = whitespace_rules
html_rules = whitespace_rules + [ html_rules = whitespace_rules + [
{'pattern': 'placeholder="[^{]', {'pattern': 'placeholder="[^{]',
'description': "`placeholder` value should be translatable."}, 'description': "`placeholder` value should be translatable."},
{'pattern': "placeholder='[^{]", {'pattern': "placeholder='[^{]",
'description': "`placeholder` value should be translatable."}, 'description': "`placeholder` value should be translatable."},
] ] # type: RuleList
json_rules = [] # just fix newlines at ends of files json_rules = [] # type: RuleList # just fix newlines at ends of files
markdown_rules = markdown_whitespace_rules + [ markdown_rules = markdown_whitespace_rules + [
{'pattern': ' [jJ]avascript', {'pattern': ' [jJ]avascript',
'exclude': set(['README.dev.md']), # temporary exclusion to avoid merge conflicts 'exclude': set(['README.dev.md']), # temporary exclusion to avoid merge conflicts
'description': "javascript should be spelled JavaScript"}, 'description': "javascript should be spelled JavaScript"},
] ] # type: RuleList
txt_rules = whitespace_rules txt_rules = whitespace_rules
def check_custom_checks_py(): def check_custom_checks_py():
# type: () -> bool
failed = False failed = False
for fn in by_lang['py']: for fn in by_lang['py']:
@ -310,6 +316,7 @@ def check_custom_checks_py():
return failed return failed
def check_custom_checks_nonpy(): def check_custom_checks_nonpy():
# type: () -> bool
failed = False failed = False
for fn in by_lang['js']: for fn in by_lang['js']:
@ -346,9 +353,10 @@ def check_custom_checks_nonpy():
return failed return failed
lint_functions = {} lint_functions = {} # type: Dict[str, Callable[[], int]]
def run_parallel(): def run_parallel():
# type: () -> bool
pids = [] pids = []
for name, func in lint_functions.items(): for name, func in lint_functions.items():
pid = os.fork() pid = os.fork()
@ -367,6 +375,7 @@ def run_parallel():
return failed return failed
def lint(func): def lint(func):
# type: (Callable[[], int]) -> Callable[[], int]
lint_functions[func.__name__] = func lint_functions[func.__name__] = func
return func return func
@ -377,6 +386,7 @@ try:
@lint @lint
def templates(): def templates():
# type: () -> int
args = ['tools/check-templates'] args = ['tools/check-templates']
if options.modified: if options.modified:
args.append('-m') args.append('-m')
@ -385,12 +395,14 @@ try:
@lint @lint
def jslint(): def jslint():
# type: () -> int
result = subprocess.call(['tools/node', 'tools/jslint/check-all.js'] result = subprocess.call(['tools/node', 'tools/jslint/check-all.js']
+ by_lang['js']) + by_lang['js'])
return result return result
@lint @lint
def puppet(): def puppet():
# type: () -> int
if not by_lang['pp']: if not by_lang['pp']:
return 0 return 0
result = subprocess.call(['puppet', 'parser', 'validate'] + by_lang['pp']) result = subprocess.call(['puppet', 'parser', 'validate'] + by_lang['pp'])
@ -398,16 +410,19 @@ try:
@lint @lint
def custom_py(): def custom_py():
# type: () -> int
failed = check_custom_checks_py() failed = check_custom_checks_py()
return 1 if failed else 0 return 1 if failed else 0
@lint @lint
def custom_nonpy(): def custom_nonpy():
# type: () -> int
failed = check_custom_checks_nonpy() failed = check_custom_checks_nonpy()
return 1 if failed else 0 return 1 if failed else 0
@lint @lint
def pyflakes(): def pyflakes():
# type: () -> int
failed = check_pyflakes() failed = check_pyflakes()
return 1 if failed else 0 return 1 if failed else 0

View File

@ -64,7 +64,6 @@ tools/compile-handlebars-templates
tools/deprecated/inject-messages/inject-messages tools/deprecated/inject-messages/inject-messages
tools/deprecated/review tools/deprecated/review
tools/get-handlebar-vars tools/get-handlebar-vars
tools/lint-all
tools/minify-js tools/minify-js
tools/test-js-with-casper tools/test-js-with-casper
tools/test-run-dev tools/test-run-dev