-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlint
executable file
·135 lines (113 loc) · 3.83 KB
/
lint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
from __future__ import absolute_import, print_function
import argparse
import re
from lib.custom_check import non_py_rules
from zulint.command import LinterConfig, add_default_linter_arguments
from zulint.custom_rules import RuleList
from zulint.linters import run_pyflakes
MYPY = False
if MYPY:
from typing import List, Tuple
def run():
# type: () -> None
parser = argparse.ArgumentParser()
# Add custom parser arguments here.
add_default_linter_arguments(parser)
args = parser.parse_args()
linter_config = LinterConfig(args)
# Linters will be run on these file types.
# eg: file_types = ['py', 'html', 'css', 'js']
file_types = ["py", "sh", "md", "yaml"]
EXCLUDED_FILES = [
# No linters will be run on files in this list.
# eg: 'path/to/file.py'
] # type: List[str]
by_lang = linter_config.list_files(file_types, exclude=EXCLUDED_FILES)
command = ["tools/run-mypy", "--quiet"]
linter_config.external_linter(
"mypy",
command,
["py"],
pass_targets=False,
description="Static type checker for Python",
)
linter_config.external_linter(
"gitlint",
["tools/commit-message-lint"],
description="Checks commit messages for common formatting errors (config: .gitlint)",
)
linter_config.external_linter(
"isort",
["isort"],
["py"],
description="Sorts Python import statements",
check_arg=["--check-only", "--diff"],
)
linter_config.external_linter(
"black",
["black"],
["py"],
description="Reformats Python code",
check_arg=["--check"],
suppress_line=lambda line: line == "All done! ✨ 🍰 ✨\n"
or re.fullmatch(r"\d+ files? would be left unchanged\.\n", line) is not None
or re.fullmatch(r"\d+ file? left unchanged\.\n", line) is not None,
)
linter_config.external_linter(
"prettier",
["node_modules/.bin/prettier", "--check", "--loglevel=warn"],
["json", "yaml", "yml"],
fix_arg=["--write"],
description="Formats Json, YAML",
)
linter_config.external_linter(
"shellcheck",
["shellcheck", "-x", "-P", "SCRIPTDIR"],
["sh"],
description="Standard shell script linter",
)
linter_config.external_linter(
"shfmt",
["shfmt"],
["sh"],
check_arg="-d",
fix_arg="-w",
description="Formats shell scripts",
)
@linter_config.lint
def check_custom_rules():
# type: () -> int
"""Check trailing whitespace for specified files"""
trailing_whitespace_rule = RuleList(
langs=file_types,
rules=[
{
"pattern": r"\s+$",
"strip": "\n",
"description": "Fix trailing whitespace",
}
],
)
failed = trailing_whitespace_rule.check(by_lang, verbose=args.verbose)
return 1 if failed else 0
@linter_config.lint
def custom_nonpy() -> int:
"""Runs custom checks for non-python files (config: tools/lib/custom_check.py)"""
failed = False
for rule in non_py_rules:
failed = failed or rule.check(by_lang, verbose=args.verbose)
return 1 if failed else 0
@linter_config.lint
def pyflakes():
# type: () -> int
suppress_patterns = [
# Error patters in this list will be will not be reported by the linter.
# syntax: ('File Path', 'Error message')
# eg: ('path/to/file.py', 'imported but unused')
] # type: List[Tuple[str, str]]
failed = run_pyflakes(by_lang["py"], args, suppress_patterns)
return 1 if failed else 0
linter_config.do_lint()
if __name__ == "__main__":
run()