Uglifyjs: string template

Created on 28 Jun 2017  路  12Comments  路  Source: mishoo/UglifyJS

When string templates syntax exists in code, a bug has been found in the following part.

aaa bbb cccc;

The expected results isaaa\nbbb\nccc, however after compressing aaa\r\nbbb\r\nccc
as results, Two line breaks will occur.

thank you.

bug harmony

All 12 comments

Please provide all the information to reproduce the issue at hand, as detailed in the template when you open this report.
https://github.com/mishoo/UglifyJS2/blob/master/.github/ISSUE_TEMPLATE.md

repro:

$ cat abc.js
console.log(`a
b
c\nw\n\rx\r\ny\rz`);
$ cat abc.js | node | od -c
0000000    a  \n   b  \n   c  \n   w  \n  \r   x  \r  \n   y  \r   z  \n
0000020



md5-e9a98e66ef811e619d4a432a2a7e4483



$ cat abc.js | unix2dos | node | od -c
0000000    a  \n   b  \n   c  \n   w  \n  \r   x  \r  \n   y  \r   z  \n
0000020



md5-e9a98e66ef811e619d4a432a2a7e4483



$ cat abc.js | bin/uglifyjs -c | node | od -c
0000000    a  \n   b  \n   c  \n   w  \n  \r   x  \r  \n   y  \r   z  \n
0000020



md5-e9a98e66ef811e619d4a432a2a7e4483



$ cat abc.js | unix2dos | bin/uglifyjs -c | node | od -c
0000000    a  \r  \n   b  \r  \n   c  \n   w  \n  \r   x  \r  \n   y  \r
0000020    z  \n                                                        
0000022



md5-b8333ca2099f41ec408ebf8f9e53fcde



*.js    text eol=lf

Fix:

--- a/lib/parse.js
+++ b/lib/parse.js
@@ -519,7 +519,11 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
                 tok.end = false;
                 return tok;
             }
-
+            if (ch == "\r" && peek() == "\n") {
+                // treat a \r\n sequence as a single \n
+                ++S.pos;
+                ch = "\n";
+            }
             raw += ch;
             if (ch === "\\") {
                 var tmp = S.pos;

which is similar to:
https://github.com/mishoo/UglifyJS2/blob/harmony-v3.0.20/lib/parse.js#L295-L299

@kzc thanks for the test case.

So is this line break conversion behaviour part of ES6 specification or just specific to Node.js?

It's an ES6 thing. acorn has the same behavior. As does babel:

$ cat abc.js | babel
"use strict";

console.log("a\nb\nc\nw\n\rx\r\ny\rz");
$ cat abc.js | unix2dos | babel
"use strict";

console.log("a\nb\nc\nw\n\rx\r\ny\rz");

@kzc cool - I'll put that PR together later on today :+1:

Some supporting text:
http://exploringjs.com/es6/ch_template-literals.html#_line-terminators-in-template-literals-are-always-lf-n

But in that case, even \r alone will need to be converted to \n

First, I'm sorry about late reply.
Thank you very much for your corrections.
If so, what should I do to enjoy the changes?
Reinstall or Edit the sources directly?
Note that, I am not familiar with node js and github here.
Thank you.

@kenlaslo You'll have to wait for a new 3.0.21 release for the fix. Perhaps in a couple of days.

@kenlaslo as @kzc pointed out, 3.0.21 has not been released yet - I shall do so in a couple of hours.

As for how you may get this, it depends on how uglify-es is specified under your package.json. But in many cases npm update should get the job done.

@kzc , @alexlamsl thank you so much :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kzc picture kzc  路  5Comments

utdrmac picture utdrmac  路  4Comments

hacdias picture hacdias  路  5Comments

neverfox picture neverfox  路  4Comments

Jimbly picture Jimbly  路  4Comments