added rule T028. closes #128

This commit is contained in:
Christopher Pickering 2021-11-16 11:17:51 +01:00
parent fee2c1c72b
commit 19acc02cda
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
3 changed files with 44 additions and 1 deletions

View file

@ -65,6 +65,9 @@ Codes
+--------+-------------------------------------------------------------------------+
| T027 | Unclosed string found in template syntax. |
+--------+-------------------------------------------------------------------------+
| T028 | Consider using spaceless tags inside attribute values. |
+--------+-------------------------------------------------------------------------+
Adding Rules

View file

@ -199,3 +199,9 @@
patterns:
- "{%(?:(?!'|\"|%}).)*((?:'|\")(?:(?!'|\"|%}).)*(?:'|\")(?:(?!'|\"|%}).)*)*(?:'|\")(?:(?!'|\"|%}).)*%}"
- "{{(?:(?!'|\"|}}).)*((?:'|\")(?:(?!'|\"|}}).)*(?:'|\")(?:(?!'|\"|}}).)*)*(?:'|\")(?:(?!'|\"|}}).)*}}"
- rule:
name: T028
message: Consider using spaceless tags inside attribute values.
patterns:
- <(?:/?(?:\w+)\b(?:\"[^\"]*\"|'[^']*'|{[^}]*}|[^'\">{}/])*=([\"'])(?:(?!\1).)*?({%|{{)[^-])
- <(?:/?(?:\w+)\b(?:\"[^\"]*\"|'[^']*'|{[^}]*}|[^'\">{}/])*=([\"'])(?:(?!\1).)*?[^-](%}|}}))

View file

@ -7,7 +7,7 @@ run::
# for a single test
pytest tests/test_linter.py::test_T027 --cov=src/djlint --cov-branch \
pytest tests/test_linter.py::test_T028 --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
"""
@ -449,6 +449,40 @@ def test_T027(runner: CliRunner, tmp_file: TextIO) -> None:
assert "T027" in result.output
def test_T028(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(tmp_file.name, b"<a href=\"{% blah 'asdf' -%}\">")
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 1
assert "T028" in result.output
write_to_file(tmp_file.name, b"<a href=\"{%- blah 'asdf' %}\">")
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 1
assert "T028" in result.output
write_to_file(tmp_file.name, b"<a href=\"{{- blah 'asdf' }}\">")
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 1
assert "T028" in result.output
write_to_file(tmp_file.name, b"<a href=\"{{ blah 'asdf' -}}\">")
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 1
assert "T028" in result.output
write_to_file(tmp_file.name, b"<a {{ blah 'asdf' }}>")
result = runner.invoke(djlint, [tmp_file.name])
assert "T028" not in result.output
write_to_file(tmp_file.name, b"<a {% blah 'asdf' %}>")
result = runner.invoke(djlint, [tmp_file.name])
assert "T028" not in result.output
write_to_file(tmp_file.name, b"{% blah 'asdf' %}")
result = runner.invoke(djlint, [tmp_file.name])
assert "T028" not in result.output
def test_rules_not_matched_in_ignored_block(
runner: CliRunner, tmp_file: TextIO
) -> None: