Skip to content

Commit

Permalink
improve max_line_len (#3095)
Browse files Browse the repository at this point in the history
fixes #304
  • Loading branch information
alexlamsl committed Apr 24, 2018
1 parent c71ed91 commit b5ce199
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 43 deletions.
67 changes: 37 additions & 30 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function OutputStream(options) {
default:
return dq > sq ? quote_single() : quote_double();
}
};
}

function encode_string(str, quote) {
var ret = make_string(str, quote);
Expand All @@ -183,17 +183,17 @@ function OutputStream(options) {
ret = ret.replace(/--\x3e/g, "--\\x3e");
}
return ret;
};
}

function make_name(name) {
name = name.toString();
name = to_utf8(name, true);
return name;
};
}

function make_indent(back) {
return repeat_string(" ", options.indent_start + indentation - back * options.indent_level);
};
}

/* -----[ beautification/minification ]----- */

Expand Down Expand Up @@ -351,7 +351,7 @@ function OutputStream(options) {
current_col = a[n].length;
}
last = str;
};
}

var space = options.beautify ? function() {
print(" ");
Expand All @@ -374,6 +374,11 @@ function OutputStream(options) {
return ret;
} : function(col, cont) { return cont() };

var may_add_newline = options.max_line_len ? function() {
ensure_line_len();
might_add_newline = OUTPUT.length;
} : noop;

var newline = options.beautify ? function() {
if (newline_insert < 0) return print("\n");
if (OUTPUT[newline_insert] != "\n") {
Expand All @@ -382,10 +387,7 @@ function OutputStream(options) {
current_line++;
}
newline_insert++;
} : options.max_line_len ? function() {
ensure_line_len();
might_add_newline = OUTPUT.length;
} : noop;
} : may_add_newline;

var semicolon = options.beautify ? function() {
print(";");
Expand All @@ -396,11 +398,11 @@ function OutputStream(options) {
function force_semicolon() {
might_need_semicolon = false;
print(";");
};
}

function next_indent() {
return indentation + options.indent_level;
};
}

function with_block(cont) {
var ret;
Expand All @@ -412,34 +414,40 @@ function OutputStream(options) {
indent();
print("}");
return ret;
};
}

function with_parens(cont) {
print("(");
may_add_newline();
//XXX: still nice to have that for argument lists
//var ret = with_indent(current_col, cont);
var ret = cont();
may_add_newline();
print(")");
return ret;
};
}

function with_square(cont) {
print("[");
may_add_newline();
//var ret = with_indent(current_col, cont);
var ret = cont();
may_add_newline();
print("]");
return ret;
};
}

function comma() {
may_add_newline();
print(",");
may_add_newline();
space();
};
}

function colon() {
print(":");
space();
};
}

var add_mapping = mappings ? function(token, name) {
mapping_token = token;
Expand All @@ -451,7 +459,7 @@ function OutputStream(options) {
ensure_line_len();
}
return OUTPUT;
};
}

function has_nlb() {
var index = OUTPUT.lastIndexOf("\n");
Expand Down Expand Up @@ -619,8 +627,7 @@ function OutputStream(options) {
return stack[stack.length - 2 - (n || 0)];
}
};

};
}

/* -----[ code generators ]----- */

Expand All @@ -630,7 +637,7 @@ function OutputStream(options) {

function DEFPRINT(nodetype, generator) {
nodetype.DEFMETHOD("_codegen", generator);
};
}

var in_directive = false;
var active_scope = null;
Expand Down Expand Up @@ -679,7 +686,7 @@ function OutputStream(options) {
} else {
nodetype.DEFMETHOD("needs_parens", func);
}
};
}

PARENS(AST_Node, return_false);

Expand Down Expand Up @@ -865,7 +872,7 @@ function OutputStream(options) {
}
});
in_directive = false;
};
}

AST_StatementWithBody.DEFMETHOD("_do_print_body", function(output){
force_statement(this.body, output);
Expand Down Expand Up @@ -901,7 +908,7 @@ function OutputStream(options) {
display_body(self.body, false, output, allow_directives);
});
} else print_braced_empty(self, output);
};
}
DEFPRINT(AST_BlockStatement, function(self, output){
print_braced(self, output);
});
Expand Down Expand Up @@ -1064,7 +1071,7 @@ function OutputStream(options) {
else break;
}
force_statement(self.body, output);
};
}
DEFPRINT(AST_If, function(self, output){
output.print("if");
output.space();
Expand Down Expand Up @@ -1184,7 +1191,7 @@ function OutputStream(options) {
}
}));
node.print(output, parens);
};
}

DEFPRINT(AST_VarDef, function(self, output){
self.name.print(output);
Expand Down Expand Up @@ -1430,15 +1437,15 @@ function OutputStream(options) {
else
stat.print(output);
}
};
}

// self should be AST_New. decide if we want to show parens or not.
function need_constructor_parens(self, output) {
// Always print parentheses with arguments
if (self.args.length > 0) return true;

return output.option("beautify");
};
}

function best_of(a) {
var best = a[0], len = best.length;
Expand All @@ -1449,7 +1456,7 @@ function OutputStream(options) {
}
}
return best;
};
}

function make_num(num) {
var str = num.toString(10), a = [ str.replace(/^0\./, ".").replace('e+', 'e') ], m;
Expand All @@ -1469,7 +1476,7 @@ function OutputStream(options) {
str.substr(str.indexOf(".")));
}
return best_of(a);
};
}

function make_block(stmt, output) {
if (!stmt || stmt instanceof AST_EmptyStatement)
Expand All @@ -1481,7 +1488,7 @@ function OutputStream(options) {
stmt.print(output);
output.newline();
});
};
}

/* -----[ source map generators ]----- */

Expand Down
34 changes: 24 additions & 10 deletions test/compress/max_line_len.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ too_short: {
}
}
expect_exact: [
'function f(a){',
'return{',
'c:42,',
'd:a(),',
'e:"foo"}}',
"function f(",
"a){return{",
"c:42,d:a(",
'),e:"foo"}',
"}",
]
expect_warnings: [
"WARN: Output exceeds 10 characters"
"WARN: Output exceeds 10 characters",
]
}

Expand All @@ -29,11 +29,25 @@ just_enough: {
}
}
expect_exact: [
'function f(a){',
'return{c:42,',
"function f(a){",
"return{c:42,",
'd:a(),e:"foo"}',
'}',
"}",
]
expect_warnings: [
expect_warnings: []
}

issue_304: {
beautify = {
max_line_len: 10,
}
input: {
var a = 0, b = 0, c = 0, d = 0, e = 0;
}
expect_exact: [
"var a=0,",
"b=0,c=0,",
"d=0,e=0;",
]
expect_warnings: []
}
6 changes: 3 additions & 3 deletions test/input/issue-505/output.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b5ce199

Please sign in to comment.