Comments in template literals `${/* comment */}`

Template literals serve a wide range of use-cases in string formatting.
This has led to situations where these strings span a larger number of lines and become more complicated as well.

Currently you cannot place documentation directly inside a template string, because a template literal variable requires an expression to be provided. For example, the following is illegal:

const query = `a & b ${/* comment */} & c & d`;

As a minimal workaround, you need can inject through ${'' /* comment after empty string */}.

Comments would allow you to document segments of a (large or complex) template string, without having to split up the string into multiple parts (e.g. variables). Which introduces naming problems, dislocates the string order and/or requires multiple separate template-concattenations to occur (which may not be possible if a specialized template tag function is used).

1 Like

Yeah, I think this might be a useful addition.

Pairing the comment with an empty string works for simple string substitution, but not if you're running a template function that does more complex context-specific handling of substitutions, like a SQL or HTML parser.

However, we still need to think about how it's reflected into a template function. If it's treated as an interpolation point, we still have the same issue where a template function might mess it up. I guess this would just be completely ignored, so in your example there would be one literal segment comprised of a & b & c & d and no interpolation segments at all?

Mentioned here too: Template Literals Juxtaposition and String.join - #7 by claudiameadows