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.
This commit is contained in:
Mihai Bazon
2015-01-27 22:26:27 +02:00
parent 099992ecae
commit fbbaa42ee5
5 changed files with 71 additions and 22 deletions

View File

@@ -367,7 +367,7 @@ function tokenizer($TEXT, filename, html5_comments) {
return num;
};
var read_string = with_eof_error("Unterminated string constant", function(){
var read_string = with_eof_error("Unterminated string constant", function(quote_char){
var quote = next(), ret = "";
for (;;) {
var ch = next(true);
@@ -392,7 +392,9 @@ function tokenizer($TEXT, filename, html5_comments) {
else if (ch == quote) break;
ret += ch;
}
return token("string", ret);
var tok = token("string", ret);
tok.quote = quote_char;
return tok;
});
function skip_line_comment(type) {
@@ -547,7 +549,7 @@ function tokenizer($TEXT, filename, html5_comments) {
if (!ch) return token("eof");
var code = ch.charCodeAt(0);
switch (code) {
case 34: case 39: return read_string();
case 34: case 39: return read_string(ch);
case 46: return handle_dot();
case 47: return handle_slash();
}
@@ -737,8 +739,14 @@ function parse($TEXT, options) {
case "string":
var dir = S.in_directives, stat = simple_statement();
// XXXv2: decide how to fix directives
if (dir && stat.body instanceof AST_String && !is("punc", ","))
return new AST_Directive({ value: stat.body.value });
if (dir && stat.body instanceof AST_String && !is("punc", ",")) {
return new AST_Directive({
start : stat.body.start,
end : stat.body.end,
quote : stat.body.quote,
value : stat.body.value,
});
}
return stat;
case "num":
case "regexp":
@@ -1124,7 +1132,12 @@ function parse($TEXT, options) {
ret = new AST_Number({ start: tok, end: tok, value: tok.value });
break;
case "string":
ret = new AST_String({ start: tok, end: tok, value: tok.value });
ret = new AST_String({
start : tok,
end : tok,
value : tok.value,
quote : tok.quote
});
break;
case "regexp":
ret = new AST_RegExp({ start: tok, end: tok, value: tok.value });
@@ -1237,6 +1250,7 @@ function parse($TEXT, options) {
expect(":");
a.push(new AST_ObjectKeyVal({
start : start,
quote : start.quote,
key : name,
value : expression(false),
end : prev()