Multiline template strings that don't break indentation

Andy Earnshaw andyearnshaw at gmail.com
Tue Sep 9 01:18:25 PDT 2014


Hi everyone,

It's great to finally play around with template strings after noticing them
in the latest Firefox nightly.  However, I can't help but think we're
repeating mistakes of the past when it comes to multiline strings and code
tidiness.

Back when I had to write PHP code I used to feel sad that multiline strings
had to look like this:

    function my_function () {
        ... some levels of
            ... indentation
                $a = <<<EOD
    This is a multiline string.
    It makes my code look messy because I can't indent
    subsequent lines without the white space appearing
    in the resulting string. :-(
    EOD;

               ... more code
        ...
    }

I've always avoided the backslash newline escape in JavaScript for the same
reason.  As happy as I am to see multiline strings appear in JavaScript in
the form of template strings, I'm not looking forward to seeing the
untidiness that goes hand in hand with it.  I'm wondering if there's a
feasible solution that could allow us to keep our code indented across
multiple lines without the formatting whitespace being included in the
resulting string.  Ideally, I'd like my multiline strings to look like this:

    function myFunction () {
        var a = `This is a template string.
                 Even though each line is indented to keep the
                 code neat and tidy, the white space used to indent
                 is not in the resulting string`;

        ...
    }

As things stand, this wouldn't look great when output in a terminal window,
a <textarea> or pre-formatted element in the browser.  Also, minifiers
wouldn't know if it was safe to remove this white space when minifying and
would likely leave it in.  I realise I could write a tag function that
deals with it, but that comes with additional runtime complexity and
concatenation isn't really an option since I don't think this would make
good code:

    var a = tag`This is a template string.\n`
          + tag`Even though each line is indented to keep the\n`
          ...

I thought of a few potential solutions, but they come with problems of
their own.

Solution 1:  Ignore all space and tab characters following a newline until
the first non-space or tab character.  The problem with this is immediately
obvious – what if someone wanted to output a string formatted with
indentation at the start of a line?  Well, there's always `String.raw`,
which would return the string with the white space included, although I'll
admit maybe it's not ideal.

Solution 2:  Have the parser take note of the column where the delimiting `
character appears, and ignore white space on the next line only if the
length of it equals this column number + 1.  This would allow you to still
write template strings like this:

    var a = `This line is not indented.
                 > But this line is.`

I've never written a parser, so I'm not sure how easy this would be to
implement.   I think it could potentially be confusing to people.

It could be that I'm the only person who is this fussy and it doesn't
bother anyone else, but I hope that's not the case.  I wonder if developers
won't stick to the old methods of string concatenation and avoid template
strings mostly – I've seen that happen a lot with PHP code.  At least with
here documents you could include white space as part of the delimiter and
the content was consistently aligned (even if not indented), so it wasn't
quite as bad as template strings.


Best regards
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140909/2799c2eb/attachment-0001.html>


More information about the es-discuss mailing list