diff --git a/lib/6to5/generation/generators/types.js b/lib/6to5/generation/generators/types.js index 7a5e368835..80c72b1a70 100644 --- a/lib/6to5/generation/generators/types.js +++ b/lib/6to5/generation/generators/types.js @@ -74,7 +74,16 @@ exports.Literal = function (node) { var val = node.value; var type = typeof val; - if (type === "boolean" || type === "number" || type === "string") { + if (type === "string") { + val = JSON.stringify(val); + + // escape unicode characters + val = val.replace(/[\u007f-\uffff]/g, function(c) { + return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4); + }); + + this.push(val); + } else if (type === "boolean" || type === "number") { this.push(JSON.stringify(val)); } else if (node.regex) { this.push("/" + node.regex.pattern + "/" + node.regex.flags); diff --git a/test/fixtures/generation/types/Literal/actual.js b/test/fixtures/generation/types/Literal/actual.js index 936cce21e5..89db0341a3 100644 --- a/test/fixtures/generation/types/Literal/actual.js +++ b/test/fixtures/generation/types/Literal/actual.js @@ -1,5 +1,6 @@ 5; "foobar"; +"\n\r\u2028\u2029"; /foobar/g; null; true; diff --git a/test/fixtures/generation/types/Literal/expected.js b/test/fixtures/generation/types/Literal/expected.js index 936cce21e5..89db0341a3 100644 --- a/test/fixtures/generation/types/Literal/expected.js +++ b/test/fixtures/generation/types/Literal/expected.js @@ -1,5 +1,6 @@ 5; "foobar"; +"\n\r\u2028\u2029"; /foobar/g; null; true;