djLint
djLint copied to clipboard
[BUG] [Linter] False positive duplicate attribute in if … else statement
- [X] I'm on the latest version of djLint
- [X] I've searched the issues
- [X] I've read the docs
System Info
- OS: macOS 13.3.1 (a)
- Python Version 3.11.2
- djLint Version 1.27.2
- template language: Django
Issue
It looks like djLint is finding a duplicate attributes in the if and else clause of an if statement in a Django template.
How To Reproduce
Given this test.html:
<button {% if user in project.bookmark_users.all %}
class="bookmark bookmark--on" title="Bookmarked; click to remove bookmark"
{% else %}
class="bookmark bookmark--off" title="Not bookmarked; click to add bookmark"
{% endif %}
id="bookmark-toggle-{{ project.id }}"></button>
…djLint reports a duplicate class attribute:
❯ djlint test.html
Linting 1/1 files ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 00:00
test.html
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
H037 1:52 Duplicate attribute found. class
Linted 1 file, found 1 error.
Contents of .djlintrc/pyproject.toml [tool.djlint]
[tool.djlint]
format_attribute_template_tags = true
Thanks @craiga. Can you help me develop this a bit?
Would any of these be considered a duplicate?
<!-- one inside, one outside -->
<button {% if %} class="" {% endif %} class="">
</button>
<!-- one in else, one outside -->
<button {% if %} id="" {% else %} class="" {% endif %} class="">
</button>
<!-- one in if, one outside -->
<button {% if %} class="" {% else %} id="" {% endif %} class="">
</button>
@christopherpickering I'd say all of those should be reported as duplicates. Is that possible?
That is, you should be able to have one in each of if and else, but not one inside if and another outside if.
cool!