rtts / djhtml

Django/Jinja template indenter
GNU General Public License v3.0
582 stars 32 forks source link

Indentation of multiline method chains in script tags #59

Closed thibaudcolas closed 1 year ago

thibaudcolas commented 2 years ago

The following JS within HTML:

<script>
window.fetch('/test.html')
    .then((html) => {
        document.body.innerHTML = html;
    });
</script>

gets indented as:

<script>
    window.fetch('/test.html')
        .then((html) => {
        document.body.innerHTML = html;
    });
</script>

I would have expected everything within the script tag to get one extra level of indentation. I’m not sure what is causing this, but I at least tried with both a function and the arrow function as above, so I suspect it’s the method chaining that might be the problem.

JaapJoris commented 2 years ago

Thank you for reporting this bug. DjHTML's handling of chained methods is arguably sloppy. This is what happens:

First, the JavaScript is indented "normally" without regards for chained methods:

<script>
    window.fetch('/test.html')
    .then((html) => {
        document.body.innerHTML = html;
    });
</script>

Then, any line that starts with a . is shifted to the right:

<script>
    window.fetch('/test.html')
        .then((html) => {
        document.body.innerHTML = html;
    });
</script>

I find it hard to wrap my head around how chained methods could be handled otherwise. However, they definitely should, because the current way introduces all kinds of indentation errors like this.

JaapJoris commented 1 year ago

Thanks again for reporting this bug. I'm happy to inform you DjHTML 3.0.0 has just been released with improved handling of JavaScript method chains, switch/case statements, multi-line variable assignments, and many more JS improvements!