From c39693c7df8c5f782d46de8cfeb123a31b5c6708 Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Mon, 15 Nov 2021 16:47:36 +0100 Subject: [PATCH] added rule T027. closes #134 --- docs/djlint/changelog.rst | 4 ++++ docs/djlint/rules.rst | 3 +++ src/djlint/rules.yaml | 7 +++++++ src/djlint/settings.py | 2 +- tests/test_linter.py | 18 +++++++++++++++++- 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/docs/djlint/changelog.rst b/docs/djlint/changelog.rst index cb0c61a..00d45ea 100644 --- a/docs/djlint/changelog.rst +++ b/docs/djlint/changelog.rst @@ -1,6 +1,10 @@ Changelog ========= +next_release +------------ +- Added rule T027 to check for unclosed in template syntax + 0.5.9a ------ - Added test support for python 3.10 diff --git a/docs/djlint/rules.rst b/docs/djlint/rules.rst index ed1d6a5..89c13d2 100644 --- a/docs/djlint/rules.rst +++ b/docs/djlint/rules.rst @@ -63,6 +63,9 @@ Codes +--------+-------------------------------------------------------------------------+ | H026 | Emtpy id and class tags can be removed. | +--------+-------------------------------------------------------------------------+ +| T027 | Unclosed string found in template syntax. | ++--------+-------------------------------------------------------------------------+ + Adding Rules ------------ diff --git a/src/djlint/rules.yaml b/src/djlint/rules.yaml index 7d4f7d3..6a483bc 100644 --- a/src/djlint/rules.yaml +++ b/src/djlint/rules.yaml @@ -192,3 +192,10 @@ patterns: - <\w+\b[^(?:{(?:%|{|#))>]*?\b(class|id)\b=(\"\"|'') - <\w+\b[^(?:{(?:%|{|#))>]*?\b(class|id)\b[^=\"] +- rule: + name: T027 + message: Unclosed string found in template syntax. + flags: re.I + patterns: + - "{%(?:(?!'|\"|%}).)*((?:'|\")(?:(?!'|\"|%}).)*(?:'|\")(?:(?!'|\"|%}).)*)*(?:'|\")(?:(?!'|\"|%}).)*%}" + - "{{(?:(?!'|\"|}}).)*((?:'|\")(?:(?!'|\"|}}).)*(?:'|\")(?:(?!'|\"|}}).)*)*(?:'|\")(?:(?!'|\"|}}).)*}}" diff --git a/src/djlint/settings.py b/src/djlint/settings.py index a573fcc..8edd919 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -636,7 +636,7 @@ class Config: | {{-?\s*/\*\s*djlint\:off\s*\*/\s*-?}}.*?{{-?\s*/\*\s*djlint\:on\s*\*/\s*-?}} | | <\?php.*?\?> - | {\%[ ]trans[ ][^}]*?\%} + # | {\%[ ]trans[ ][^}]*?\%} | {%[ ]+?comment[ ]+?[^(?:%})]*?%}.*?{%[ ]+?endcomment[ ]+?%} """ diff --git a/tests/test_linter.py b/tests/test_linter.py index d6ad145..0df0f41 100644 --- a/tests/test_linter.py +++ b/tests/test_linter.py @@ -7,7 +7,7 @@ run:: # for a single test - pytest tests/test_linter.py::test_output_order --cov=src/djlint --cov-branch \ + pytest tests/test_linter.py::test_T027 --cov=src/djlint --cov-branch \ --cov-report xml:coverage.xml --cov-report term-missing """ @@ -433,6 +433,22 @@ def test_H026(runner: CliRunner, tmp_file: TextIO) -> None: assert "H026" not in result.output +def test_T027(runner: CliRunner, tmp_file: TextIO) -> None: + write_to_file(tmp_file.name, b"{% blah 'asdf %}") + result = runner.invoke(djlint, [tmp_file.name]) + assert result.exit_code == 1 + assert "T027" in result.output + + write_to_file(tmp_file.name, b"{% blah 'asdf' %}{{ blah \"asdf\" }}") + result = runner.invoke(djlint, [tmp_file.name]) + assert "T027" not in result.output + + write_to_file(tmp_file.name, b"{% blah 'asdf' 'blah %}") + result = runner.invoke(djlint, [tmp_file.name]) + assert result.exit_code == 1 + assert "T027" in result.output + + def test_rules_not_matched_in_ignored_block( runner: CliRunner, tmp_file: TextIO ) -> None: