lint: Move shebang_rules out of RuleList class to generalize it.

shebang_rules was moved to custom_check.py. Also add shebang_rules only
to those rules which need it.
This commit is contained in:
Aman Agrawal
2019-06-12 22:07:03 +05:30
committed by Tim Abbott
parent db25c0c2ca
commit dcd46f1c11
2 changed files with 20 additions and 15 deletions

View File

@@ -54,6 +54,20 @@ FILES_WITH_LEGACY_SUBJECT = {
'zerver/tests/test_narrow.py', 'zerver/tests/test_narrow.py',
} }
shebang_rules = [
{'pattern': '^#!',
'description': "zerver library code shouldn't have a shebang line.",
'include_only': set(['zerver/'])},
# /bin/sh and /usr/bin/env are the only two binaries
# that NixOS provides at a fixed path (outside a
# buildFHSUserEnv sandbox).
{'pattern': '^#!(?! *(?:/usr/bin/env|/bin/sh)(?: |$))',
'description': "Use `#!/usr/bin/env foo` instead of `#!/path/foo`"
" for interpreters other than sh."},
{'pattern': '^#!/usr/bin/env python$',
'description': "Use `#!/usr/bin/env python3` instead of `#!/usr/bin/env python`."}
] # type: Rule
trailing_whitespace_rule = { trailing_whitespace_rule = {
'pattern': r'\s+$', 'pattern': r'\s+$',
'strip': '\n', 'strip': '\n',
@@ -505,6 +519,7 @@ python_rules = RuleList(
}, },
]) + whitespace_rules + comma_whitespace_rule, ]) + whitespace_rules + comma_whitespace_rule,
max_length=110, max_length=110,
shebang_rules=shebang_rules,
) )
bash_rules = RuleList( bash_rules = RuleList(
@@ -521,6 +536,7 @@ bash_rules = RuleList(
'scripts/setup/configure-rabbitmq' 'scripts/setup/configure-rabbitmq'
]), }, ]), },
]) + whitespace_rules[0:1], ]) + whitespace_rules[0:1],
shebang_rules=shebang_rules,
) )
css_rules = RuleList( css_rules = RuleList(

View File

@@ -18,12 +18,14 @@ if False:
class RuleList: class RuleList:
"""Defines and runs custom linting rules for the specified language.""" """Defines and runs custom linting rules for the specified language."""
def __init__(self, langs, rules, max_length=None, length_exclude=[], exclude_files_in=None): def __init__(self, langs, rules, max_length=None, length_exclude=[], shebang_rules=[],
exclude_files_in=None):
# type: (List[str], Rule, Optional[int], List[str], Rule, Optional[str]) -> None # type: (List[str], Rule, Optional[int], List[str], Rule, Optional[str]) -> None
self.langs = langs self.langs = langs
self.rules = rules self.rules = rules
self.max_length = max_length self.max_length = max_length
self.length_exclude = length_exclude self.length_exclude = length_exclude
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 = "\\"
@@ -184,20 +186,7 @@ class RuleList:
failed = True failed = True
if firstline: if firstline:
shebang_rules = [ shebang_rules_to_apply = self.get_rules_applying_to_fn(fn=fn, rules=self.shebang_rules)
{'pattern': '^#!',
'description': "zerver library code shouldn't have a shebang line.",
'include_only': set(['zerver/'])},
# /bin/sh and /usr/bin/env are the only two binaries
# that NixOS provides at a fixed path (outside a
# buildFHSUserEnv sandbox).
{'pattern': '^#!(?! *(?:/usr/bin/env|/bin/sh)(?: |$))',
'description': "Use `#!/usr/bin/env foo` instead of `#!/path/foo`"
" for interpreters other than sh."},
{'pattern': '^#!/usr/bin/env python$',
'description': "Use `#!/usr/bin/env python3` instead of `#!/usr/bin/env python`."}
] # type: Rule
shebang_rules_to_apply = self.get_rules_applying_to_fn(fn=fn, rules=shebang_rules)
for rule in shebang_rules_to_apply: for rule in shebang_rules_to_apply:
if re.search(rule['pattern'], firstline): if re.search(rule['pattern'], firstline):
print_err(identifier, color, print_err(identifier, color,