diff --git a/src/djlint/helpers.py b/src/djlint/helpers.py index 014f7a6..0bc82a2 100644 --- a/src/djlint/helpers.py +++ b/src/djlint/helpers.py @@ -148,8 +148,14 @@ def inside_ignored_rule(config: Config, html: str, match: re.Match, rule: str) - ): if ( rule in list(set(re.split(r"\s|,", ignored_match.group(1).strip()))) - and ignored_match.start(0) <= match.start() - and match.end(0) <= ignored_match.end() + and ( + ignored_match.start(0) <= match.start() + and match.start() <= ignored_match.end() + ) + or ( + ignored_match.start(0) <= match.end() + and match.end() <= ignored_match.end() + ) ): return True return False diff --git a/src/djlint/rules.yaml b/src/djlint/rules.yaml index 42fac5e..e240884 100644 --- a/src/djlint/rules.yaml +++ b/src/djlint/rules.yaml @@ -18,14 +18,14 @@ # close - '[^(\s|^|\-)]+[}|%|#]}' - '[^(\s|^)]+\-[}|%|#]}' - - \s{2,}[}|%|#]} - - '{[{|%|#]-?\s{2,}' + - '[^\s][ ]{2,}[}|%|#]}' + - '{[{|%|#]-?[ ]{2,}' - rule: name: T002 message: Double quotes should be used in tags. flags: re.DOTALL patterns: - - '{%.?extends\s+?[^\"]\w+' + - "{%[ \t]*?extends[ \t]+?'[^']*'" - rule: name: T003 message: 'Endblock should have name. Ex: {% endblock body %}.' @@ -96,8 +96,8 @@ message: There should be no spaces around attribute =. flags: re.DOTALL patterns: - - <\w+?(?:(?!\{[%|{|#])[^\n|>])*\s+= - - <\w+?(?:(?!\{[%|{|#])[^\n|>])*=\s + - <\w+?(\"[^\"]*\"|'[^']*'|{[^}]*}|[^'\">{}])*\s+= + - <\w+?(\"[^\"]*\"|'[^']*'|{[^}]*}|[^'\">{}])*=\s - rule: name: H013 message: Img tag should have an alt attribute. @@ -161,7 +161,7 @@ message: Inline styles should be avoided. flags: re.I patterns: - - <\w+\s(?:[^>]*\s)?style=(?=[^>]*>) + - <\w+\s(?:[^>]*\s)?style=(?=((?!>|{{|{%).)*>) - rule: name: H022 message: Use HTTPS for external links. diff --git a/tests/test_linter/test_linter.py b/tests/test_linter/test_linter.py index bab55b9..9e53cb3 100644 --- a/tests/test_linter/test_linter.py +++ b/tests/test_linter/test_linter.py @@ -7,7 +7,7 @@ run:: # for a single test - pytest tests/test_linter/test_linter.py::test_T001 + pytest tests/test_linter/test_linter.py::test_ignoring_rules """ # pylint: disable=C0116,C0103 @@ -50,6 +50,18 @@ def test_T001(runner: CliRunner, tmp_file: TextIO) -> None: result = runner.invoke(djlint, [tmp_file.name, "--profile", "jinja"]) assert result.exit_code == 0 + # test line break around tag + write_to_file( + tmp_file.name, + b"""
+ {% + ("SashaNose", "1"), + %} +
""", + ) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "jinja"]) + assert "T001" not in result.output + def test_T002(runner: CliRunner, tmp_file: TextIO) -> None: write_to_file(tmp_file.name, b"{% extends 'this' %}") @@ -57,6 +69,11 @@ def test_T002(runner: CliRunner, tmp_file: TextIO) -> None: assert result.exit_code == 1 assert "T002 1:" in result.output + # allow variable names (unquoted) + write_to_file(tmp_file.name, b"{% extends this %}") + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) + assert "T002" not in result.output + def test_T003(runner: CliRunner, tmp_file: TextIO) -> None: write_to_file(tmp_file.name, b"{% endblock %}") @@ -202,6 +219,16 @@ def test_H012(runner: CliRunner, tmp_file: TextIO) -> None: assert result.exit_code == 0 assert "H012 1:" not in result.output + # space allowed inside attributes. + write_to_file( + tmp_file.name, + b"""