Skip to content

Commit

Permalink
fix(linter): prevent linter matches that overlap ignored blocks of co…
Browse files Browse the repository at this point in the history
…de such as comments

closes #343
  • Loading branch information
christopherpickering committed Aug 22, 2022
1 parent 79618a5 commit 90cf576
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/djlint/helpers.py
Expand Up @@ -107,6 +107,36 @@ def inside_ignored_block(config: Config, html: str, match: re.Match) -> bool:
)


def overlaps_ignored_block(config: Config, html: str, match: re.Match) -> bool:
"""Do not add whitespace if the tag is in a non indent block."""
return any(
# don't require the match to be fully inside the ignored block.
# poorly build html will probably span ignored blocks and should be ignored.
(
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()
)
for ignored_match in list(
re.finditer(
re.compile(
config.ignored_blocks,
re.DOTALL | re.IGNORECASE | re.VERBOSE | re.MULTILINE | re.DOTALL,
),
html,
)
)
+ list(
re.finditer(
re.compile(config.ignored_inline_blocks, re.IGNORECASE | re.VERBOSE),
html,
)
)
)


def inside_ignored_rule(config: Config, html: str, match: re.Match, rule: str) -> bool:
"""Check if match is inside an ignored pattern."""
for rule_regex in config.ignored_rules:
Expand Down
6 changes: 3 additions & 3 deletions src/djlint/lint.py
Expand Up @@ -5,7 +5,7 @@

import regex as re

from .helpers import inside_ignored_block, inside_ignored_rule
from .helpers import inside_ignored_rule, overlaps_ignored_block
from .settings import Config

flags = {
Expand Down Expand Up @@ -99,7 +99,7 @@ def lint_file(config: Config, this_file: Path) -> Dict:

for match in open_tags:
if (
inside_ignored_block(config, html, match) is False
overlaps_ignored_block(config, html, match) is False
and inside_ignored_rule(config, html, match, rule["name"])
is False
):
Expand All @@ -119,7 +119,7 @@ def lint_file(config: Config, this_file: Path) -> Dict:
html,
):
if (
inside_ignored_block(config, html, match) is False
overlaps_ignored_block(config, html, match) is False
and inside_ignored_rule(config, html, match, rule["name"])
is False
):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_linter/test_linter.py
Expand Up @@ -91,6 +91,18 @@ def test_H006(runner: CliRunner, tmp_file: TextIO) -> None:
assert "H006 1:" in result.output
assert "found 1 error" in result.output

# check that we don't partial match in an ignored block
write_to_file(
tmp_file.name,
b"""{# [INFO][JINJA] I use syntax "{% if <img alt=\""",
if I want that something happened solely if "img" exists in the content of my articles #}
<script src="KiraJS.js" defer></script>
""",
)
result = runner.invoke(djlint, [tmp_file.name])
assert "H006" not in result.output


def test_H007(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(tmp_file.name, b'<html lang="en">')
Expand Down

0 comments on commit 90cf576

Please sign in to comment.