📜 ⬆️ ⬇️

String.raw: some features and limitations

I. Opportunities


When I read on MDN : “I’m happy to hear that I’ve often not been happy with it.” JavaScript lacked something like single quotes in Perl.

I immediately came up with several uses and began to actively use them in scripts.

1. Definition of paths to Windows files without double shielding.
')
 const r = String.raw; const test_module = require(r`e:\DOC\prg\js\node\-lib\test.js`); 

2. Identifying the paths to the Windows registry keys.

 const r = String.raw; const Winreg = require('winreg'); const regKey = new Winreg({ hive: Winreg.HKCU, key: r`\Software\MPC-HC\MPC-HC\Settings` }); 

3. Create complex regular expressions from composite literals.

See sample code in a recent article.

Ii. Restrictions


However, over time, I began to stumble upon unexpected limitations. Having written about one of them in the V8 bugtracker, I received a sobering explanation. It turns out that although String.raw produces a string without interpreting shielded literals, at the stage of parsing the code, the analyzer still requires that the literals conform to the rules. This implies non-obvious limitations for the mentioned applications.

1. After the backslash, x or u characters without corresponding hex sequences cannot follow.

The following code generates an Uncaught SyntaxError: Invalid hexadecimal escape sequence error Uncaught SyntaxError: Invalid hexadecimal escape sequence :

 console.log(String.raw`:\x.js`); 

The following code generates an Uncaught SyntaxError: Invalid Unicode escape sequence error Uncaught SyntaxError: Invalid Unicode escape sequence :

 console.log(String.raw`:\u.js`); 

Despite the fact that the right combinations are still serialized uninterpreted:

 console.log(String.raw`\x61`); //\x61 console.log(String.raw`\u0061`); //\u0061 

I did not find simple ways to solve the problem:

 console.log(String.raw`:\\x.js`); // :\\x.js console.log(String.raw`:\\x78.js`); // :\\x78.js console.log(String.raw`:\${'x'}.js`); // :\${'x'}.js console.log(String.raw`:\\${'x'}.js`); // :\\x.js console.log(String.raw`:\\u.js`); // :\\u.js console.log(String.raw`:\\x75.js`); // :\\x75.js console.log(String.raw`:\${'u'}.js`); // :\${'u'}.js console.log(String.raw`:\\${'u'}.js`); // :\\u.js 

Working solutions are so complex that it will be easier to return to the use of ordinary quotes with double shielding:

 console.log(String.raw`:${'\\'}x.js`); // :\x.js console.log(String.raw`:${'\\'}u.js`); // :\u.js 

The remaining matches with screened literals do not cause any difficulties and are displayed literally:

 console.log(String.raw`:\ab\0 cd`); console.log(String.raw`:\ab\' cd`); console.log(String.raw`:\ab\" cd`); console.log(String.raw`:\ab\\ cd`); console.log(String.raw`:\ab\n cd`); console.log(String.raw`:\ab\r cd`); console.log(String.raw`:\ab\v cd`); console.log(String.raw`:\ab\t cd`); console.log(String.raw`:\ab\b cd`); console.log(String.raw`:\ab\f cd`); 

2. There is no easy way to include ` .

This symbol sometimes replaces the English apostrophe, and is also used in some transliteration systems .

Expected error Uncaught SyntaxError: missing ) after argument list :

 console.log(String.raw`:\John`s.js`); 

Non-working solutions:

 console.log(String.raw`:\John\`s.js`); // :\John\`s.js console.log(String.raw`:\John\x60s.js`); // :\John\x60s.js 

Unjustified complexity:

 console.log(String.raw`:\John${'`'}s.js`); // :\John`s.js 

3. There is no easy way to create a line with a backslash at the very end.

Expected Uncaught SyntaxError: Unterminated template literal :

 console.log(String.raw`:\`); 

Non-working solutions:

 console.log(String.raw`:\\`); // :\\ console.log(String.raw`:\x5c`); // :\x5c 

Unjustified complexity:

 console.log(String.raw`:${'\\'}`); // :\ 

Iii. Comparison with other languages


The corresponding tools of the languages ​​mentioned at the beginning of the note work, as expected, which can be checked, for example, here .

C # :



Perl :



Python :



If you have found other interesting ways to use String.raw , come across other unexpected restrictions, or come up with elegant ways to circumvent them, please share.

PS Pending changes: www.2ality.com/2016/09/template-literal-revision.html

Source: https://habr.com/ru/post/300846/


All Articles