babel-generator: Ensure ASCII-safe output for string literals (#4478)
Ref. #4477.
This commit is contained in:
committed by
Henry Zhu
parent
e5b9c92d3d
commit
b9919bdc48
@@ -15,6 +15,7 @@
|
||||
"babel-runtime": "^6.9.0",
|
||||
"babel-types": "^6.14.0",
|
||||
"detect-indent": "^3.0.1",
|
||||
"jsesc": "^1.3.0",
|
||||
"lodash": "^4.2.0",
|
||||
"source-map": "^0.5.0"
|
||||
},
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
/* eslint quotes: 0 */
|
||||
|
||||
import * as t from "babel-types";
|
||||
import jsesc from "jsesc";
|
||||
|
||||
export function Identifier(node: Object) {
|
||||
// FIXME: We hang variance off Identifer to support Flow's def-site variance.
|
||||
@@ -135,26 +136,11 @@ export function StringLiteral(node: Object, parent: Object) {
|
||||
return;
|
||||
}
|
||||
|
||||
let val = JSON.stringify(node.value);
|
||||
|
||||
// escape illegal js but valid json unicode characters
|
||||
val = val.replace(/[\u000A\u000D\u2028\u2029]/g, function (c) {
|
||||
return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4);
|
||||
// ensure the output is ASCII-safe
|
||||
let val = jsesc(node.value, {
|
||||
quotes: t.isJSX(parent) ? "double" : this.format.quotes,
|
||||
wrap: true
|
||||
});
|
||||
|
||||
if (this.format.quotes === "single" && !t.isJSX(parent)) {
|
||||
// remove double quotes
|
||||
val = val.slice(1, -1);
|
||||
|
||||
// unescape double quotes
|
||||
val = val.replace(/\\"/g, '"');
|
||||
|
||||
// escape single quotes
|
||||
val = val.replace(/'/g, "\\'");
|
||||
|
||||
// add single quotes
|
||||
val = `'${val}'`;
|
||||
}
|
||||
|
||||
return this.token(val);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
'\x20';
|
||||
"\n\r";
|
||||
"😂";
|
||||
`😂`;
|
||||
/foobar/g;
|
||||
null;
|
||||
true;
|
||||
|
||||
@@ -1 +1 @@
|
||||
5;5;"foobar";" ";"\n\r";"😂";/foobar/g;null;true;false;5;2;56;31;
|
||||
5;5;"foobar";" ";"\n\r";"\uD83D\uDE02";`😂`;/foobar/g;null;true;false;5;2;56;31;
|
||||
|
||||
Reference in New Issue
Block a user