backquote strings (template literals) not supported
Opened this issue · 0 comments
Hi,
javascript-beautifier doesn't support JavaScript's third quoting mechanism, 'template literals' (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals). Presence of a template literal seems to abort parsing for the rest of the line, causing it to be emitted as-is without further beautification. Since web JS files are frequently uglified down to one gigantic line, this is problematic.
Template literals have some other complexities: inline variable expansions and 'tags'. Inline expansion looks like:
var msg = `the value of fred is: ${fred}`;
-- where the expression could be arbitrarily complex, and therefore possibly worthy of beautification itself. But this probably isn't important. Just passing the contents of `` through as a literal string would be a huge step forward. The one place it might cause a problem is if the expression contains template literals itself; then the inner template literals would end up getting parsed as code. But this doesn't seem to be common and seems to me like a clever way to shoot oneself in the foot, so I'm not worried about it.
And tagged template literals look like:
var foo = my_str_processor`some input string`;
-- which is just a weird way of calling my_str_processor(string). I'm not sure it needs any special treatment beyond `` itself; lexically it's just like `` with an identifier pasted onto the beginning.
I'm surprised this doesn't seem to have been raised before. I didn't see mention here on github nor on CPAN.
A trivial patch to Beautifier.pm seems adequate:
$c eq '"' || # string
+ $c eq '`' || # template literal string
($c eq '/' &&