djhtml icon indicating copy to clipboard operation
djhtml copied to clipboard

Indentation of multiline method chains in script tags

Open thibaudcolas opened this issue 3 years ago • 1 comments

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.

thibaudcolas avatar Feb 26 '22 15:02 thibaudcolas

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 avatar Mar 03 '22 09:03 JaapJoris

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!

JaapJoris avatar Feb 23 '23 01:02 JaapJoris