mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
check-templates: Extract/improve report_problem.
We extract the function for modularity and to allow early-return. We also add checks for "else" and improve a few error messages.
This commit is contained in:
@@ -308,17 +308,22 @@ def validate(fn: Optional[str] = None, text: Optional[str] = None) -> None:
|
|||||||
else:
|
else:
|
||||||
max_lines = 1
|
max_lines = 1
|
||||||
|
|
||||||
problem = None
|
def report_problem() -> Optional[str]:
|
||||||
if (start_tag == "code") and (end_line == start_line + 1):
|
if (start_tag == "code") and (end_line == start_line + 1):
|
||||||
problem = "Code tag is split across two lines."
|
return "Code tag is split across two lines."
|
||||||
if is_else_tag:
|
|
||||||
pass
|
|
||||||
elif start_tag != end_tag:
|
|
||||||
problem = "Mismatched tag."
|
|
||||||
|
|
||||||
if not problem and (end_line > start_line + max_lines):
|
if is_else_tag:
|
||||||
|
# We are not completely rigorous about having a sensible
|
||||||
|
# order of if/elif/elif/else, but we catch obviously
|
||||||
|
# mismatching else tags.
|
||||||
|
if start_tag not in ("if", "else", "unless"):
|
||||||
|
return f"Unexpected else/elif tag encountered after {start_tag} tag."
|
||||||
|
elif start_tag != end_tag:
|
||||||
|
return f"Mismatched tags: ({start_tag} != {end_tag})"
|
||||||
|
|
||||||
|
if end_line > start_line + max_lines:
|
||||||
if end_col != start_col:
|
if end_col != start_col:
|
||||||
problem = "Bad indentation."
|
return "Indentation for start/end tags does not match."
|
||||||
|
|
||||||
if end_line >= start_line + 2:
|
if end_line >= start_line + 2:
|
||||||
# We have 3+ lines in the tag's block.
|
# We have 3+ lines in the tag's block.
|
||||||
@@ -326,8 +331,11 @@ def validate(fn: Optional[str] = None, text: Optional[str] = None) -> None:
|
|||||||
start_indent = indent_level(start_row_text)
|
start_indent = indent_level(start_row_text)
|
||||||
if start_indent != start_col - 1 and start_row_text[start_indent] not in "<{":
|
if start_indent != start_col - 1 and start_row_text[start_indent] not in "<{":
|
||||||
junk = start_row_text[start_indent : start_col - 1]
|
junk = start_row_text[start_indent : start_col - 1]
|
||||||
problem = f"There is junk before the start tag: {junk}"
|
return f"There is junk before the start tag: {junk}"
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
problem = report_problem()
|
||||||
if problem:
|
if problem:
|
||||||
raise TemplateParserException(
|
raise TemplateParserException(
|
||||||
f"""
|
f"""
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ class ParserTest(unittest.TestCase):
|
|||||||
my_html = """
|
my_html = """
|
||||||
<b>foo</i>
|
<b>foo</i>
|
||||||
"""
|
"""
|
||||||
self._assert_validate_error("Mismatched tag.", text=my_html)
|
self._assert_validate_error(r"Mismatched tags: \(b != i\)", text=my_html)
|
||||||
|
|
||||||
def test_validate_bad_indentation(self) -> None:
|
def test_validate_bad_indentation(self) -> None:
|
||||||
my_html = """
|
my_html = """
|
||||||
@@ -92,7 +92,7 @@ class ParserTest(unittest.TestCase):
|
|||||||
foo
|
foo
|
||||||
</p>
|
</p>
|
||||||
"""
|
"""
|
||||||
self._assert_validate_error("Bad indentation.", text=my_html)
|
self._assert_validate_error("Indentation for start/end tags does not match.", text=my_html)
|
||||||
|
|
||||||
def test_validate_state_depth(self) -> None:
|
def test_validate_state_depth(self) -> None:
|
||||||
my_html = """
|
my_html = """
|
||||||
|
|||||||
Reference in New Issue
Block a user