fix(linter): make attribute pattern on H025 more accepting. Prevent false positive

closes #447
This commit is contained in:
Christopher Pickering 2022-11-07 09:13:12 +01:00
parent 0d170aab6c
commit 1bd0bf7ef0
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
4 changed files with 28 additions and 8 deletions

View file

@ -71,26 +71,38 @@ def lint_file(config: Config, this_file: Path) -> Dict:
# rule H025 is a special case where the output must be an even number.
if rule["name"] == "H025":
open_tags: List[re.Match] = []
# for match in re.finditer(
# re.compile(
# pattern, flags=build_flags(rule.get("flags", "re.DOTALL"))
# ),
# html,
# ):
# print(r"<(/?(\w+))\s*" +config.attribute_pattern + r"\s*?>")
for match in re.finditer(
re.compile(
pattern, flags=build_flags(rule.get("flags", "re.DOTALL"))
r"<(/?(\w+))\s*(" + config.attribute_pattern + r"|\s*)*\s*?>",
re.VERBOSE,
),
html,
):
if match.group(2) and not re.search(
# print(match)
# print(match.group(2))
# print(match.group(1))
if match.group(1) and not re.search(
re.compile(
rf"^/?{config.always_self_closing_html_tags}\b", re.I | re.X
),
match.group(2),
match.group(1),
):
# close tags should equal open tags
if match.group(2)[0] != "/":
if match.group(1)[0] != "/":
open_tags.insert(0, match)
else:
for i, tag in enumerate(copy.deepcopy(open_tags)):
if tag.group(3) == match.group(2)[1:]:
if tag.group(2) == match.group(1)[1:]:
open_tags.pop(i)
break
else:

View file

@ -184,9 +184,8 @@
- rule:
name: H025
message: Tag seems to be an orphan.
flags: re.I|re.DOTALL
patterns:
- <((/?(\w+))\b(\"[^\"]*\"|'[^']*'|{{[^}]*}}|{%[^%]*%}|{#[^#]*#}|[^'\">{}])*)(?<!/)>
- None
- rule:
name: H026
message: Empty id and class tags can be removed.

View file

@ -507,7 +507,7 @@ class Config:
(?:
(
(?:\w|-|\.)+ | required | checked
) # attribute name
)? # attribute name
(?: [ ]*?=[ ]*? # followed by "="
(
\"[^\"]*? # double quoted attribute
@ -538,6 +538,7 @@ class Config:
"""
+ r"""
| {{.*?}}
| {\#.*?\#}
| {%.*?%})
"""
)

View file

@ -670,6 +670,14 @@ def test_H025(runner: CliRunner, tmp_file: TextIO) -> None:
result = runner.invoke(djlint, [tmp_file.name])
assert "H025" not in result.output
# issue #447
write_to_file(
tmp_file.name,
b"""<button title="{% trans "text with ONE single ' quote" %}">
</button>""",
)
assert "H025" not in result.output
def test_H026(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(tmp_file.name, b'<asdf id="" >')