a shy attempt to obey width in the beautifier; added bracketize option to always print brackets around if/do/while/for statements; export more options via the CLI

This commit is contained in:
Mihai Bazon
2012-10-02 11:00:47 +03:00
parent 896444482a
commit 9e5dd81f1e
5 changed files with 117 additions and 49 deletions

View File

@@ -53,9 +53,10 @@ function OutputStream(options) {
width : 80,
max_line_len : 32000,
ie_proof : true,
beautify : true,
beautify : false,
source_map : null,
});
bracketize : false,
}, true);
var indentation = 0;
var current_col = 0;
@@ -261,6 +262,9 @@ function OutputStream(options) {
get : get,
toString : get,
indent : indent,
indentation : function() { return indentation },
current_width : function() { return current_col - indentation },
should_break : function() { return options.width && this.current_width() >= options.width },
newline : newline,
print : print,
space : space,
@@ -271,6 +275,7 @@ function OutputStream(options) {
force_semicolon : force_semicolon,
print_name : function(name) { print(make_name(name)) },
print_string : function(str) { print(encode_string(str)) },
next_indent : next_indent,
with_indent : with_indent,
with_block : with_block,
with_parens : with_parens,
@@ -314,9 +319,6 @@ function OutputStream(options) {
};
AST_Node.DEFMETHOD("print_to_string", function(options){
options = defaults(options, {
beautify: false
});
var s = OutputStream(options);
this.print(s);
return s.get();
@@ -610,6 +612,10 @@ function OutputStream(options) {
/* -----[ if ]----- */
function make_then(self, output) {
if (output.option("bracketize")) {
make_block(self.body, output);
return;
}
// The squeezer replaces "block"-s that contain only a single
// statement with the statement itself; technically, the AST
// is correct, but this can create problems when we output an
@@ -780,13 +786,29 @@ function OutputStream(options) {
output.space();
AST_Call.prototype.print.call(self, output);
});
DEFPRINT(AST_Seq, function(self, output){
self.car.print(output);
if (self.cdr) {
AST_Seq.DEFMETHOD("_do_print", function(output){
this.car.print(output);
if (this.cdr) {
output.comma();
self.cdr.print(output);
if (output.should_break()) {
output.newline();
output.indent();
}
this.cdr.print(output);
}
});
DEFPRINT(AST_Seq, function(self, output){
self._do_print(output);
// var p = output.parent();
// if (p instanceof AST_Statement) {
// output.with_indent(output.next_indent(), function(){
// self._do_print(output);
// });
// } else {
// self._do_print(output);
// }
});
DEFPRINT(AST_Dot, function(self, output){
var expr = self.expression;
expr.print(output);
@@ -919,10 +941,22 @@ function OutputStream(options) {
});
function force_statement(stat, output) {
if (stat instanceof AST_EmptyStatement)
output.force_semicolon();
else
stat.print(output);
if (output.option("bracketize")) {
if (!stat || stat instanceof AST_EmptyStatement)
output.print("{}");
else if (stat instanceof AST_BlockStatement)
stat.print(output);
else output.with_block(function(){
output.indent();
stat.print(output);
output.newline();
});
} else {
if (!stat || stat instanceof AST_EmptyStatement)
output.force_semicolon();
else
stat.print(output);
}
};
// return true if the node at the top of the stack (that means the
@@ -938,7 +972,7 @@ function OutputStream(options) {
(p instanceof AST_Dot && p.expression === node ) ||
(p instanceof AST_Sub && p.expression === node ) ||
(p instanceof AST_Conditional && p.condition === node ) ||
(p instanceof AST_Binary && p.left === node ) ||
(p instanceof AST_Binary && p.left === node ) ||
(p instanceof AST_UnaryPostfix && p.expression === node ))
{
node = p;