Skip to content

Commit

Permalink
feat(linter): allow codes to be ignored for remainder of file
Browse files Browse the repository at this point in the history
closes #655
  • Loading branch information
christopherpickering committed May 22, 2023
1 parent 5f33d08 commit 4637a0f
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/djlint/settings.py
Expand Up @@ -790,14 +790,14 @@ def __init__(

self.ignored_rules: List[str] = [
# html comment
r"<!--\s*djlint\:off(.+?)-->.*?(?=<!--\s*djlint\:on\s*-->)",
r"<!--\s*djlint\:off(.+?)-->(?:(?!<!--\s*djlint\:on\s*-->).)*",
# 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"""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_linter/test_h005.py
Expand Up @@ -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])

Expand Down
4 changes: 2 additions & 2 deletions tests/test_linter/test_h006.py
Expand Up @@ -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])

Expand Down
4 changes: 2 additions & 2 deletions tests/test_linter/test_h007.py
Expand Up @@ -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])

Expand Down
4 changes: 2 additions & 2 deletions tests/test_linter/test_h008.py
Expand Up @@ -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])

Expand Down
4 changes: 2 additions & 2 deletions tests/test_linter/test_h009.py
Expand Up @@ -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])

Expand Down
4 changes: 2 additions & 2 deletions tests/test_linter/test_h010.py
Expand Up @@ -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])

Expand Down
4 changes: 2 additions & 2 deletions tests/test_linter/test_h037.py
Expand Up @@ -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])

Expand Down
45 changes: 45 additions & 0 deletions 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(
("<img>{# djlint:off H004,H006,H013 #}\n" "<img>\n"),
(
[
{
"code": "H006",
"line": "1:0",
"match": "<img>",
"message": "Img tag should have height and width attributes.",
},
{
"code": "H013",
"line": "1:0",
"match": "<img>",
"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

0 comments on commit 4637a0f

Please sign in to comment.