mirror of
https://github.com/Hopiu/djLint.git
synced 2026-03-17 05:50:24 +00:00
fix(linter): prevent linter matches that overlap ignored blocks of code such as comments
closes #343
This commit is contained in:
parent
79618a5bfb
commit
90cf5764ac
3 changed files with 45 additions and 3 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
):
|
||||
|
|
|
|||
|
|
@ -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">')
|
||||
|
|
|
|||
Loading…
Reference in a new issue