Skip to content

Commit

Permalink
fix(linter): fixed false positive on h026 data-id
Browse files Browse the repository at this point in the history
closes #711
  • Loading branch information
christopherpickering committed Jul 17, 2023
1 parent 232f4bc commit 400882a
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/djlint/rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@
message: Empty id and class tags can be removed.
flags: re.I
patterns:
- <\w+\b[^(?:{(?:%|{|#))>]*?\b(class|id)\b=(\"\"|'')
- <\w+\b[^(?:{(?:%|{|#))>-]*?\b(class|id)\b[^=\"-]
- <\w+\b[^(?:{(?:%|{|#))>]*?\s(class|id)\b=(\"\"|'')
- <\w+\b[^(?:{(?:%|{|#))>-]*?\s(class|id)\b[^=\"-]
- rule:
name: T027
message: Unclosed string found in template syntax.
Expand Down
102 changes: 102 additions & 0 deletions tests/test_linter/test_h026.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
"""Test linter code H026.
poetry run pytest tests/test_linter/test_h026.py
"""
import pytest

from src.djlint.lint import linter
from tests.conftest import lint_printer

test_data = [
pytest.param(
('<asdf id="" >'),
(
[
{
"code": "H025",
"line": "1:0",
"match": '<asdf id="" >',
"message": "Tag seems to be an orphan.",
},
{
"code": "H026",
"line": "1:0",
"match": '<asdf id=""',
"message": "Empty id and class tags can be removed.",
},
]
),
id="empted quotes",
),
pytest.param(
("<asdf id >"),
(
[
{
"code": "H025",
"line": "1:0",
"match": "<asdf id >",
"message": "Tag seems to be an orphan.",
},
{
"code": "H026",
"line": "1:0",
"match": "<asdf id",
"message": "Empty id and class tags can be removed.",
},
]
),
id="no quotes",
),
pytest.param(
('<asdf class="" >'),
(
[
{
"code": "H025",
"line": "1:0",
"match": '<asdf class="" >',
"message": "Tag seems to be an orphan.",
},
{
"code": "H026",
"line": "1:0",
"match": '<asdf class=""',
"message": "Empty id and class tags can be removed.",
},
]
),
id="class",
),
pytest.param(
('<asdf {% class="" %}></asdf>'),
([]),
id="class in tag",
),
pytest.param(
("<div x-id-y></div><div id-y></div><div x-id></div>"),
([]),
id="prefix and suffix",
),
pytest.param(
(
'<div x-id-y=""></div><div id-y=""></div><div x-id=""></div><div data-id=""></div>'
),
([]),
id="prefix and suffix quoted",
),
]


@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
26 changes: 0 additions & 26 deletions tests/test_linter/test_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,32 +391,6 @@ def test_H025(runner: CliRunner, tmp_file: TextIO) -> None:
assert "H025" not in result.output


def test_H026(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(tmp_file.name, b'<asdf id="" >')
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 1
assert "H026" in result.output

write_to_file(tmp_file.name, b"<asdf id >")
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 1
assert "H026" in result.output

write_to_file(tmp_file.name, b'<asdf class="" >')
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 1
assert "H026" in result.output

write_to_file(tmp_file.name, b'<asdf {% class="" %}></asdf>')
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 0
assert "H026" not in result.output

write_to_file(tmp_file.name, b"<div x-id-y><div id-y><div x-id>")
result = runner.invoke(djlint, [tmp_file.name])
assert "H026" not in result.output


def test_T027(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(tmp_file.name, b"<a href=\"{{- blah 'asdf' }}\">")
result = runner.invoke(djlint, [tmp_file.name])
Expand Down

0 comments on commit 400882a

Please sign in to comment.