djlint / djLint

✨ HTML Template Linter and Formatter. Django - Jinja - Nunjucks - Handlebars - GoLang
https://djLint.com
GNU General Public License v3.0
673 stars 82 forks source link

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

Open craiga opened 1 year ago

craiga commented 1 year ago

System Info

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
christopherpickering commented 1 year ago

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>
craiga commented 1 year ago

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

craiga commented 1 year ago

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

christopherpickering commented 1 year ago

cool!