mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 13:33:24 +00:00
lint: Strengthen lint checks for string % non-tuple.
This is really a job for an AST parser rather than a pile of regexes; among other issues, these will still miss violations that span multiple lines. But, you know, I tried. Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
committed by
Tim Abbott
parent
643bd18b9f
commit
44dd7c2d95
@@ -216,6 +216,17 @@ def check_file_for_long_lines(fn: str,
|
|||||||
ok = False
|
ok = False
|
||||||
return ok
|
return ok
|
||||||
|
|
||||||
|
PYDELIMS = r'''"'()\[\]{}#\\'''
|
||||||
|
PYREG = r"[^{}]".format(PYDELIMS)
|
||||||
|
PYSQ = r'"(?:[^"\\]|\\.)*"'
|
||||||
|
PYDQ = r"'(?:[^'\\]|\\.)*'"
|
||||||
|
PYLEFT = r"[(\[{]"
|
||||||
|
PYRIGHT = r"[)\]}]"
|
||||||
|
PYCODE = PYREG
|
||||||
|
for depth in range(5):
|
||||||
|
PYGROUP = r"""(?:{}|{}|{}{}*{})""".format(PYSQ, PYDQ, PYLEFT, PYCODE, PYRIGHT)
|
||||||
|
PYCODE = r"""(?:{}|{})""".format(PYREG, PYGROUP)
|
||||||
|
|
||||||
def build_custom_checkers(by_lang):
|
def build_custom_checkers(by_lang):
|
||||||
# type: (Dict[str, List[str]]) -> Tuple[Callable[[], bool], Callable[[], bool]]
|
# type: (Dict[str, List[str]]) -> Tuple[Callable[[], bool], Callable[[], bool]]
|
||||||
|
|
||||||
@@ -465,15 +476,14 @@ def build_custom_checkers(by_lang):
|
|||||||
# This next check could have false positives, but it seems pretty
|
# This next check could have false positives, but it seems pretty
|
||||||
# rare; if we find any, they can be added to the exclude list for
|
# rare; if we find any, they can be added to the exclude list for
|
||||||
# this rule.
|
# this rule.
|
||||||
{'pattern': r''' % [a-zA-Z0-9_."']*\)?$''',
|
{'pattern': r"""^(?:[^'"#\\]|{}|{})*(?:{}|{})\s*%\s*(?![\s({{\\]|dict\(|tuple\()(?:[^,{}]|{})+(?:$|[,#\\]|{})""".format(
|
||||||
'exclude_line': set([
|
PYSQ, PYDQ, PYSQ, PYDQ, PYDELIMS, PYGROUP, PYRIGHT),
|
||||||
('tools/tests/test_template_parser.py', '{% foo'),
|
'description': 'Used % formatting without a tuple',
|
||||||
]),
|
|
||||||
'description': 'Used % comprehension without a tuple',
|
|
||||||
'good_lines': ['"foo %s bar" % ("baz",)'],
|
'good_lines': ['"foo %s bar" % ("baz",)'],
|
||||||
'bad_lines': ['"foo %s bar" % "baz"']},
|
'bad_lines': ['"foo %s bar" % "baz"']},
|
||||||
{'pattern': r'''.*%s.* % \([a-zA-Z0-9_."']*\)$''',
|
{'pattern': r"""^(?:[^'"#\\]|{}|{})*(?:{}|{})\s*%\s*\((?:[^,{}]|{})*\)""".format(
|
||||||
'description': 'Used % comprehension without a tuple',
|
PYSQ, PYDQ, PYSQ, PYDQ, PYDELIMS, PYGROUP),
|
||||||
|
'description': 'Used % formatting with parentheses that do not form a tuple',
|
||||||
'good_lines': ['"foo %s bar" % ("baz",)"'],
|
'good_lines': ['"foo %s bar" % ("baz",)"'],
|
||||||
'bad_lines': ['"foo %s bar" % ("baz")']},
|
'bad_lines': ['"foo %s bar" % ("baz")']},
|
||||||
{'pattern': 'sudo',
|
{'pattern': 'sudo',
|
||||||
|
|||||||
Reference in New Issue
Block a user