Siorki
Siorki

Reputation: 183

changing string delimiters to backticks : possible impact?

ES6 introduced template strings delimited by backticks `.

In which cases would replacing single ' or double " quotes around a string by backticks yield a different result, or otherwise be unsafe ?

Escaping of existing backticks inside the code is performed as part of the operation.

// before
var message = "Display backtick ` on screen";

// after
var message = `Display backtick \` on screen`;

I understand that any string containing ${...} would fail as it would be (mis)interpreted as a placeholder. Are there any other relevant patterns ?


Context : this is for the development of a JS compression tool that automatically processes input code. The latter, and the strings it contains is user-provided, hence I have no control over its contents. The only assumption one can make is that it is valid Javascript.

Execution environment can by any recent browser or Node.js.

Upvotes: 7

Views: 1269

Answers (1)

Bergi
Bergi

Reputation: 665276

You can check the grammar of string literals and no-substitution-templates.
Apart from the ` vs. ' vs. " and the special meaning of ${ that you already mentioned, only line breaks differ between the two forms. We can ignore line continuations ("escaped linebreaks") as your minifier would strip them away anyway, but plain line breaks are valid inside templates while not inside string literals, so if you convert back and forth you'll have to care about those as well. You can even use them to save another byte if your string literal contains \n.

Upvotes: 2

Related Questions