diff --git a/src/djlint/helpers.py b/src/djlint/helpers.py index 2570382..014f7a6 100644 --- a/src/djlint/helpers.py +++ b/src/djlint/helpers.py @@ -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: diff --git a/src/djlint/lint.py b/src/djlint/lint.py index 815260f..69709be 100644 --- a/src/djlint/lint.py +++ b/src/djlint/lint.py @@ -5,7 +5,7 @@ from typing import Dict, List 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 = { @@ -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 ): @@ -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 ): diff --git a/tests/test_linter/test_linter.py b/tests/test_linter/test_linter.py index b9dbeb1..bab55b9 100644 --- a/tests/test_linter/test_linter.py +++ b/tests/test_linter/test_linter.py @@ -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 \""", +""", + ) + 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'')