fix line terminators in template literals (#2173)

fixes #2172
This commit is contained in:
Alex Lam S.L
2017-06-28 22:52:29 +08:00
committed by GitHub
parent 4d5aeeddfb
commit d052394621
3 changed files with 16 additions and 4 deletions

View File

@@ -446,7 +446,7 @@ var AST_PrefixedTemplateString = DEFNODE("PrefixedTemplateString", "template_str
var AST_TemplateString = DEFNODE("TemplateString", "segments", {
$documentation: "A template string literal",
$propdoc: {
segments: "[AST_TemplateSegment|AST_Expression]* One or more segments, starting with AST_TemplateSegment. AST_Expression may follow AST_TemplateSegment, but each AST_Expression must be followed by AST_TemplateSegment."
segments: "[AST_Node*] One or more segments, starting with AST_TemplateSegment. AST_Node may follow AST_TemplateSegment, but each AST_Node must be followed by AST_TemplateSegment."
},
_walk: function(visitor) {
return visitor._visit(this, function(){

View File

@@ -509,8 +509,11 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
}
var content = "", raw = "", ch, tok;
next(true, true);
while ((ch = next(true, true)) !== "`") {
if (ch === "$" && peek() === "{") {
while ((ch = next(true, true)) != "`") {
if (ch == "\r") {
if (peek() == "\n") ++S.pos;
ch = "\n";
} else if (ch == "$" && peek() == "{") {
next(true, true);
S.brace_counter++;
tok = token(begin ? "template_head" : "template_substitution", content);
@@ -521,7 +524,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
}
raw += ch;
if (ch === "\\") {
if (ch == "\\") {
var tmp = S.pos;
ch = read_escaped_char();
raw += S.text.substr(tmp, S.pos - tmp);

View File

@@ -30,4 +30,13 @@ describe("Template string", function() {
assert.throws(exec(tests[i]), fail, tests[i]);
}
});
it("Should process all line terminators as LF", function() {
[
"`a\rb`",
"`a\nb`",
"`a\r\nb`",
].forEach(function(code) {
assert.strictEqual(uglify.parse(code).print_to_string(), "`a\\nb`;");
});
});
});