aboutsummaryrefslogtreecommitdiff
path: root/lib/output.js
diff options
context:
space:
mode:
authorMihai Bazon <mihai.bazon@gmail.com>2015-01-27 22:26:27 +0200
committerMihai Bazon <mihai.bazon@gmail.com>2015-01-27 22:26:27 +0200
commitfbbaa42ee55a7f753f7cab9b1a905ccf73cf26d5 (patch)
tree6db252b7cc4acb1835b6bf9644890a6f275cbf91 /lib/output.js
parent099992ecae5ed51332f3928b9e1c06e7ad8147b8 (diff)
downloadtracifyjs-fbbaa42ee55a7f753f7cab9b1a905ccf73cf26d5.tar.gz
tracifyjs-fbbaa42ee55a7f753f7cab9b1a905ccf73cf26d5.zip
Add option to preserve/enforce string quote style
`-q 0` (default) use single or double quotes such as to minimize the number of bytes (prefers double quotes when both will do); this is the previous behavior. `-q 1` -- always use single quotes `-q 2` -- always use double quotes `-q 3` or just `-q` -- always use the original quotes. Related codegen option: `quote_style`. Close #495 Close #460 Some `yargs` guru please tell me why `uglifyjs --help` doesn't display the help string for `-q` / `--quotes`, and why it doesn't output the expected argument types anymore, like good old `optimist` did.
Diffstat (limited to 'lib/output.js')
-rw-r--r--lib/output.js34
1 files changed, 25 insertions, 9 deletions
diff --git a/lib/output.js b/lib/output.js
index bfe6242a..135636b9 100644
--- a/lib/output.js
+++ b/lib/output.js
@@ -63,6 +63,7 @@ function OutputStream(options) {
preserve_line : false,
screw_ie8 : false,
preamble : null,
+ quote_style : 0
}, true);
var indentation = 0;
@@ -84,7 +85,7 @@ function OutputStream(options) {
});
};
- function make_string(str) {
+ function make_string(str, quote) {
var dq = 0, sq = 0;
str = str.replace(/[\\\b\f\n\r\t\x22\x27\u2028\u2029\0\ufeff]/g, function(s){
switch (s) {
@@ -102,13 +103,27 @@ function OutputStream(options) {
}
return s;
});
+ function quote_single() {
+ return "'" + str.replace(/\x27/g, "\\'") + "'";
+ }
+ function quote_double() {
+ return '"' + str.replace(/\x22/g, '\\"') + '"';
+ }
if (options.ascii_only) str = to_ascii(str);
- if (dq > sq) return "'" + str.replace(/\x27/g, "\\'") + "'";
- else return '"' + str.replace(/\x22/g, '\\"') + '"';
+ switch (options.quote_style) {
+ case 1:
+ return quote_single();
+ case 2:
+ return quote_double();
+ case 3:
+ return quote == "'" ? quote_single() : quote_double();
+ default:
+ return dq > sq ? quote_single() : quote_double();
+ }
};
- function encode_string(str) {
- var ret = make_string(str);
+ function encode_string(str, quote) {
+ var ret = make_string(str, quote);
if (options.inline_script)
ret = ret.replace(/<\x2fscript([>\/\t\n\f\r ])/gi, "<\\/script$1");
return ret;
@@ -324,7 +339,7 @@ function OutputStream(options) {
force_semicolon : force_semicolon,
to_ascii : to_ascii,
print_name : function(name) { print(make_name(name)) },
- print_string : function(str) { print(encode_string(str)) },
+ print_string : function(str, quote) { print(encode_string(str, quote)) },
next_indent : next_indent,
with_indent : with_indent,
with_block : with_block,
@@ -581,7 +596,7 @@ function OutputStream(options) {
/* -----[ PRINTERS ]----- */
DEFPRINT(AST_Directive, function(self, output){
- output.print_string(self.value);
+ output.print_string(self.value, self.quote);
output.semicolon();
});
DEFPRINT(AST_Debugger, function(self, output){
@@ -1077,6 +1092,7 @@ function OutputStream(options) {
});
DEFPRINT(AST_ObjectKeyVal, function(self, output){
var key = self.key;
+ var quote = self.quote;
if (output.option("quote_keys")) {
output.print_string(key + "");
} else if ((typeof key == "number"
@@ -1087,7 +1103,7 @@ function OutputStream(options) {
} else if (RESERVED_WORDS(key) ? output.option("screw_ie8") : is_identifier_string(key)) {
output.print_name(key);
} else {
- output.print_string(key);
+ output.print_string(key, quote);
}
output.colon();
self.value.print(output);
@@ -1125,7 +1141,7 @@ function OutputStream(options) {
output.print(self.getValue());
});
DEFPRINT(AST_String, function(self, output){
- output.print_string(self.getValue());
+ output.print_string(self.getValue(), self.quote);
});
DEFPRINT(AST_Number, function(self, output){
output.print(make_num(self.getValue()));