Escape strings using jsesc (#229)
The current implementation with `JSON.stringify()` is clever but unfortunately generates incorrect JavaScript. Using `jsesc` seems like a better and safer option. https://github.com/mathiasbynens/jsesc It doesn't have any dependencies and is pretty small. I opted for escaping all the non ascii characters, so we don't display emojis anymore. I don't think that the world is ready yet for having random unicode characters inside of source files, there still are so many parts of the toolchain that breaks with them. If we want to revert back on this decision, there's a `minimal` option on jsesc which only escapes values that need to in order to generate valid JavaScript file (assuming the encoding of the file is set to utf8). Also, while working on React Native, we've seen that there is an optimization inside of jsc for js files that are all ascii: it doesn't do a copy for the conversion to ucs16. Fixes #163
This commit is contained in:
1 parent
b1138bdfd9
commit
c9af5a6c3b
4 files changed
+12
-17
No files matched your search
@@ -20,6 +20,7 @@
|
||||
"esutils": "^2.0.2",
|
||||
"flow-parser": "^0.37.0",
|
||||
"get-stdin": "^5.0.1",
|
||||
"jsesc": "^2.4.0",
|
||||
"minimist": "^1.2.0",
|
||||
"private": "^0.1.6"
|
||||
},
|
||||
|
||||
+5
-11
@@ -25,6 +25,7 @@ var isObject = types.builtInTypes.object;
|
||||
var FastPath = require("./fast-path");
|
||||
var util = require("./util");
|
||||
var isIdentifierName = require("esutils").keyword.isIdentifierNameES6;
|
||||
var jsesc = require("jsesc");
|
||||
|
||||
function Printer(originalOptions) {
|
||||
assert.ok(this instanceof Printer);
|
||||
@@ -2127,12 +2128,6 @@ function lastNonSpaceCharacter(lines) {
|
||||
} while (lines.prevPos(pos));
|
||||
}
|
||||
|
||||
function swapQuotes(str) {
|
||||
return str.replace(/['"]/g, function(m) {
|
||||
return m === '"' ? "'" : '"';
|
||||
});
|
||||
}
|
||||
|
||||
function nodeStr(str, options) {
|
||||
isString.assert(str);
|
||||
|
||||
@@ -2147,11 +2142,10 @@ function nodeStr(str, options) {
|
||||
shouldUseSingleQuote = true;
|
||||
}
|
||||
|
||||
if (shouldUseSingleQuote) {
|
||||
return swapQuotes(JSON.stringify(swapQuotes(str)));
|
||||
} else {
|
||||
return JSON.stringify(str);
|
||||
}
|
||||
return jsesc(str, {
|
||||
quotes: shouldUseSingleQuote ? 'single' : 'double',
|
||||
wrap: true
|
||||
});
|
||||
}
|
||||
|
||||
function isFirstStatement(path) {
|
||||
|
||||
@@ -70,8 +70,8 @@ exports[`test strings.js 1`] = `
|
||||
\"\\\\\";
|
||||
\"\\\\\";
|
||||
|
||||
\"\\u0000\";
|
||||
\"🐶\";
|
||||
\"\\0\";
|
||||
\"\\uD83D\\uDC36\";
|
||||
"
|
||||
`;
|
||||
|
||||
@@ -117,7 +117,7 @@ exports[`test strings.js 2`] = `
|
||||
\'\\\\\';
|
||||
\'\\\\\';
|
||||
|
||||
\'\\u0000\';
|
||||
\'🐶\';
|
||||
\'\\0\';
|
||||
\'\\uD83D\\uDC36\';
|
||||
"
|
||||
`;
|
||||
@@ -37,8 +37,8 @@ exports[`test strings.js 1`] = `
|
||||
\"\'\\\"\",
|
||||
\"\\\\\",
|
||||
\"\\\\\",
|
||||
\"\\u0000\",
|
||||
\"🐶\"
|
||||
\"\\0\",
|
||||
\"\\uD83D\\uDC36\"
|
||||
];
|
||||
"
|
||||
`;
|
||||
Reference in new issue
Block a user