fixed #10 for lint and format. added tests

This commit is contained in:
Christopher Pickering 2021-09-01 11:55:28 +02:00
parent 54d8a1269f
commit af97bbcb32
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
4 changed files with 44 additions and 6 deletions

View file

@ -127,5 +127,5 @@ def compress_html(html):
html,
re.IGNORECASE | re.MULTILINE,
)
print(html)
return html

View file

@ -121,9 +121,10 @@ def indent_html(rawcode):
else:
tmp = (indent * indent_level) + item + "\n"
# we can try to fix django tags
tmp = re.sub(r"({[{|%])(\w[^}].+?)([}|%]})", r"\1 \2\3", tmp)
tmp = re.sub(r"({[{|%])([^}].+?[^ ])([}|%]})", r"\1\2 \3", tmp)
# we can try to fix template tags
tmp = re.sub(r"({[{|%]\-?)(\w[^}].+?)([}|%]})", r"\1 \2\3", tmp)
tmp = re.sub(r"({[{|%])([^}].+?[^(?: |\-)])([}|%]})", r"\1\2 \3", tmp)
tmp = re.sub(r"({[{|%])([^}].+?[^ ])(\-[}|%]})", r"\1\2 \3", tmp)
# handlebars templates
tmp = re.sub(r"({{#(?:each|if).+?[^ ])(}})", r"\1 \2", tmp)

View file

@ -5,13 +5,15 @@
patterns:
# open
- '{{[^\s#/@^]+'
- '{%[^\s]+'
- '{%-[^\s]+'
- '{%[^\s|\-]+'
# handlebars
- '[^{]{#[^\s]+|^{#[^\s]+'
- '[^{]{\/[^\s]+|^{\/[^\s]+'
- '[^{]{\@[^\s]+|^{\@[^\s]+'
# close
- '[^(\s|^)]+[}|%|#]}'
- '[^(\s|^|\-)]+[}|%|#]}'
- '[^(\s|^)]+\-[}|%|#]}'
- \s{2,}[}|%|#]}
- '{[{|%|#]\s{2,}'
- rule:

View file

@ -90,6 +90,20 @@ def test_E001(runner, tmp_file):
assert "E001 1:" in result.output
assert "E001 2:" in result.output
write_to_file(tmp_file.name, b"{%- test-%}")
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 1
assert "E001 1:" in result.output
write_to_file(tmp_file.name, b"{%-test -%}")
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 1
assert "E001 1:" in result.output
write_to_file(tmp_file.name, b"{%- test -%}")
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 0
def test_E002(runner, tmp_file):
write_to_file(tmp_file.name, b"{% extends 'this' %}")
@ -367,3 +381,24 @@ def test_dj_comments_tag(runner, tmp_file):
open(tmp_file.name).read()
== """{# comment #}\n{% if this %}<div></div>{% endif %}\n"""
)
def test_template_tags(runner, tmp_file):
# njk tag
write_to_file(
tmp_file.name,
b"""{%- set posts = collections.docs -%}""",
)
runner.invoke(djlint, [tmp_file.name, "--reformat"])
assert open(tmp_file.name).read() == """{%- set posts = collections.docs -%}\n"""
# ensure spaces are added
write_to_file(
tmp_file.name,
b"""{%-set posts = collections.docs-%}\n{%asdf%}""",
)
runner.invoke(djlint, [tmp_file.name, "--reformat"])
assert (
open(tmp_file.name).read()
== """{%- set posts = collections.docs -%}\n{% asdf %}\n"""
)