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:
Steve Howell 2021-12-01 12:06:16 +00:00 committed by Tim Abbott
parent 00e80b8c91
commit d197813b88
2 changed files with 27 additions and 19 deletions

View File

@ -308,26 +308,34 @@ def validate(fn: Optional[str] = None, text: Optional[str] = None) -> None:
else:
max_lines = 1
problem = None
if (start_tag == "code") and (end_line == start_line + 1):
problem = "Code tag is split across two lines."
if is_else_tag:
pass
elif start_tag != end_tag:
problem = "Mismatched tag."
def report_problem() -> Optional[str]:
if (start_tag == "code") and (end_line == start_line + 1):
return "Code tag is split across two lines."
if not problem and (end_line > start_line + max_lines):
if end_col != start_col:
problem = "Bad indentation."
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 + 2:
# We have 3+ lines in the tag's block.
start_row_text = lines[start_line - 1]
start_indent = indent_level(start_row_text)
if start_indent != start_col - 1 and start_row_text[start_indent] not in "<{":
junk = start_row_text[start_indent : start_col - 1]
problem = f"There is junk before the start tag: {junk}"
if end_line > start_line + max_lines:
if end_col != start_col:
return "Indentation for start/end tags does not match."
if end_line >= start_line + 2:
# We have 3+ lines in the tag's block.
start_row_text = lines[start_line - 1]
start_indent = indent_level(start_row_text)
if start_indent != start_col - 1 and start_row_text[start_indent] not in "<{":
junk = start_row_text[start_indent : start_col - 1]
return f"There is junk before the start tag: {junk}"
return None
problem = report_problem()
if problem:
raise TemplateParserException(
f"""

View File

@ -84,7 +84,7 @@ class ParserTest(unittest.TestCase):
my_html = """
<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:
my_html = """
@ -92,7 +92,7 @@ class ParserTest(unittest.TestCase):
foo
</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:
my_html = """