djLint icon indicating copy to clipboard operation
djLint copied to clipboard

[BUG] [Linter] False positive duplicate attribute in if … else statement

Open craiga opened this issue 2 years ago • 4 comments

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

craiga avatar May 12 '23 11:05 craiga

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 avatar May 18 '23 14:05 christopherpickering

@christopherpickering I'd say all of those should be reported as duplicates. Is that possible?

craiga avatar May 18 '23 14:05 craiga

That is, you should be able to have one in each of if and else, but not one inside if and another outside if.

craiga avatar May 18 '23 14:05 craiga

cool!

christopherpickering avatar May 18 '23 16:05 christopherpickering