From 4637a0f6192b49100bdc772bba64265d74d568f2 Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Mon, 22 May 2023 10:16:46 -0500 Subject: [PATCH] feat(linter): allow codes to be ignored for remainder of file closes #655 --- src/djlint/settings.py | 10 +++--- tests/test_linter/test_h005.py | 4 +-- tests/test_linter/test_h006.py | 4 +-- tests/test_linter/test_h007.py | 4 +-- tests/test_linter/test_h008.py | 4 +-- tests/test_linter/test_h009.py | 4 +-- tests/test_linter/test_h010.py | 4 +-- tests/test_linter/test_h037.py | 4 +-- tests/test_linter/test_ignore_rules.py | 45 ++++++++++++++++++++++++++ 9 files changed, 64 insertions(+), 19 deletions(-) create mode 100644 tests/test_linter/test_ignore_rules.py diff --git a/src/djlint/settings.py b/src/djlint/settings.py index 74ac04fd..cd424907 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -790,14 +790,14 @@ def __init__( self.ignored_rules: List[str] = [ # html comment - r".*?(?=)", + r"(?:(?!).)*", # django/jinja/nunjucks - r"{\#\s*djlint\:\s*off(.+?)\#}.*?(?={\#\s*djlint\:\s*on\s*\#})", - r"{%\s*comment\s*%\}\s*djlint\:off(.*?)\{%\s*endcomment\s*%\}.*?(?={%\s*comment\s*%\}\s*djlint\:on\s*\{%\s*endcomment\s*%\})", + r"{\#\s*djlint\:\s*off(.+?)\#}(?:(?!{\#\s*djlint\:\s*on\s*\#}).)*", + r"{%\s*comment\s*%\}\s*djlint\:off(.*?)\{%\s*endcomment\s*%\}(?:(?!{%\s*comment\s*%\}\s*djlint\:on\s*\{%\s*endcomment\s*%\}).)*", # handlebars - r"{{!--\s*djlint\:off(.*?)--}}.*?(?={{!--\s*djlint\:on\s*--}})", + r"{{!--\s*djlint\:off(.*?)--}}(?:(?!{{!--\s*djlint\:on\s*--}}).)*", # golang - r"{{-?\s*/\*\s*djlint\:off(.*?)\*/\s*-?}}.*?(?={{-?\s*/\*\s*djlint\:on\s*\*/\s*-?}})", + r"{{-?\s*/\*\s*djlint\:off(.*?)\*/\s*-?}}(?:(?!{{-?\s*/\*\s*djlint\:on\s*\*/\s*-?}}).)*", ] self.ignored_trans_blocks: str = r""" diff --git a/tests/test_linter/test_h005.py b/tests/test_linter/test_h005.py index d968600b..257cccaf 100644 --- a/tests/test_linter/test_h005.py +++ b/tests/test_linter/test_h005.py @@ -32,9 +32,9 @@ @pytest.mark.parametrize(("source", "expected"), test_data) -def test_base(source, expected, nunjucks_config): +def test_base(source, expected, basic_config): filename = "test.html" - output = linter(nunjucks_config, source, filename, filename) + output = linter(basic_config, source, filename, filename) lint_printer(source, expected, output[filename]) diff --git a/tests/test_linter/test_h006.py b/tests/test_linter/test_h006.py index 67ddb2ea..a65aa263 100644 --- a/tests/test_linter/test_h006.py +++ b/tests/test_linter/test_h006.py @@ -68,9 +68,9 @@ @pytest.mark.parametrize(("source", "expected"), test_data) -def test_base(source, expected, nunjucks_config): +def test_base(source, expected, basic_config): filename = "test.html" - output = linter(nunjucks_config, source, filename, filename) + output = linter(basic_config, source, filename, filename) lint_printer(source, expected, output[filename]) diff --git a/tests/test_linter/test_h007.py b/tests/test_linter/test_h007.py index 745f4cb0..662f4668 100644 --- a/tests/test_linter/test_h007.py +++ b/tests/test_linter/test_h007.py @@ -32,9 +32,9 @@ @pytest.mark.parametrize(("source", "expected"), test_data) -def test_base(source, expected, nunjucks_config): +def test_base(source, expected, basic_config): filename = "test.html" - output = linter(nunjucks_config, source, filename, filename) + output = linter(basic_config, source, filename, filename) lint_printer(source, expected, output[filename]) diff --git a/tests/test_linter/test_h008.py b/tests/test_linter/test_h008.py index f171b446..4ce1a26f 100644 --- a/tests/test_linter/test_h008.py +++ b/tests/test_linter/test_h008.py @@ -47,9 +47,9 @@ @pytest.mark.parametrize(("source", "expected"), test_data) -def test_base(source, expected, nunjucks_config): +def test_base(source, expected, basic_config): filename = "test.html" - output = linter(nunjucks_config, source, filename, filename) + output = linter(basic_config, source, filename, filename) lint_printer(source, expected, output[filename]) diff --git a/tests/test_linter/test_h009.py b/tests/test_linter/test_h009.py index fcff56c8..8abb33aa 100644 --- a/tests/test_linter/test_h009.py +++ b/tests/test_linter/test_h009.py @@ -32,9 +32,9 @@ @pytest.mark.parametrize(("source", "expected"), test_data) -def test_base(source, expected, nunjucks_config): +def test_base(source, expected, basic_config): filename = "test.html" - output = linter(nunjucks_config, source, filename, filename) + output = linter(basic_config, source, filename, filename) lint_printer(source, expected, output[filename]) diff --git a/tests/test_linter/test_h010.py b/tests/test_linter/test_h010.py index b0e132c0..701733a2 100644 --- a/tests/test_linter/test_h010.py +++ b/tests/test_linter/test_h010.py @@ -31,9 +31,9 @@ @pytest.mark.parametrize(("source", "expected"), test_data) -def test_base(source, expected, nunjucks_config): +def test_base(source, expected, basic_config): filename = "test.html" - output = linter(nunjucks_config, source, filename, filename) + output = linter(basic_config, source, filename, filename) lint_printer(source, expected, output[filename]) diff --git a/tests/test_linter/test_h037.py b/tests/test_linter/test_h037.py index 69b30036..205a7aec 100644 --- a/tests/test_linter/test_h037.py +++ b/tests/test_linter/test_h037.py @@ -137,9 +137,9 @@ @pytest.mark.parametrize(("source", "expected"), test_data) -def test_base(source, expected, nunjucks_config): +def test_base(source, expected, basic_config): filename = "test.html" - output = linter(nunjucks_config, source, filename, filename) + output = linter(basic_config, source, filename, filename) lint_printer(source, expected, output[filename]) diff --git a/tests/test_linter/test_ignore_rules.py b/tests/test_linter/test_ignore_rules.py new file mode 100644 index 00000000..bd5b8833 --- /dev/null +++ b/tests/test_linter/test_ignore_rules.py @@ -0,0 +1,45 @@ +"""Test linter code H005. + +poetry run pytest tests/test_linter/test_ignore_rules.py +""" +import pytest + +from src.djlint.lint import linter +from tests.conftest import lint_printer + +test_data = [ + pytest.param( + ("{# djlint:off H004,H006,H013 #}\n" "\n"), + ( + [ + { + "code": "H006", + "line": "1:0", + "match": "", + "message": "Img tag should have height and width attributes.", + }, + { + "code": "H013", + "line": "1:0", + "match": "", + "message": "Img tag should have an alt attribute.", + }, + ] + ), + id="one", + ), +] + + +@pytest.mark.parametrize(("source", "expected"), test_data) +def test_base(source, expected, basic_config): + filename = "test.html" + output = linter(basic_config, source, filename, filename) + + lint_printer(source, expected, output[filename]) + + mismatch = list(filter(lambda x: x not in expected, output[filename])) + list( + filter(lambda x: x not in output[filename], expected) + ) + + assert len(mismatch) == 0