aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMihai Bazon <mihai@bazon.net>2012-10-02 11:00:47 +0300
committerMihai Bazon <mihai@bazon.net>2012-10-02 11:22:38 +0300
commit9e5dd81f1e98b5c77084e19345191d630576cc44 (patch)
tree73ea24334ac93a13911247a52a42ce3709040f80 /lib
parent896444482a4044c21b68a1eb58cfe8639b628279 (diff)
downloadtracifyjs-9e5dd81f1e98b5c77084e19345191d630576cc44.tar.gz
tracifyjs-9e5dd81f1e98b5c77084e19345191d630576cc44.zip
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
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js2
-rw-r--r--lib/output.js62
-rw-r--r--lib/scope.js9
-rw-r--r--lib/utils.js9
4 files changed, 63 insertions, 19 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 3054e1fe..14e722d3 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -64,7 +64,7 @@ function Compressor(options, false_by_default) {
cascade : !false_by_default,
warnings : true
- });
+ }, true);
};
Compressor.prototype = new TreeTransformer;
diff --git a/lib/output.js b/lib/output.js
index 69401d0d..615f7110 100644
--- a/lib/output.js
+++ b/lib/output.js
@@ -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;
diff --git a/lib/scope.js b/lib/scope.js
index 1f564431..9f8981f9 100644
--- a/lib/scope.js
+++ b/lib/scope.js
@@ -339,7 +339,10 @@ AST_LoopControl.DEFMETHOD("target", function(){
return this.loopcontrol_target;
});
-AST_Toplevel.DEFMETHOD("mangle_names", function(sort){
+AST_Toplevel.DEFMETHOD("mangle_names", function(options){
+ options = defaults(options, {
+ sort: false
+ });
// We only need to mangle declaration nodes. Special logic wired
// into the code generator will display the mangled name if it's
// present (and for AST_SymbolRef-s it'll use the mangled name of
@@ -374,11 +377,11 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(sort){
});
this.walk(tw);
- if (sort) to_mangle = mergeSort(to_mangle, function(a, b){
+ if (options.sort) to_mangle = mergeSort(to_mangle, function(a, b){
return b.references.length - a.references.length;
});
- to_mangle.forEach(function(def){ def.mangle() });
+ to_mangle.forEach(function(def){ def.mangle(options) });
});
AST_Toplevel.DEFMETHOD("compute_char_frequency", function(){
diff --git a/lib/utils.js b/lib/utils.js
index 5de83fbd..3b8aa00c 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -96,10 +96,17 @@ function repeat_string(str, i) {
return d;
};
-function defaults(args, defs) {
+function DefaultsError(msg, defs) {
+ this.msg = msg;
+ this.defs = defs;
+};
+
+function defaults(args, defs, croak) {
if (args === true)
args = {};
var ret = args || {};
+ if (croak) for (var i in ret) if (HOP(ret, i) && !HOP(defs, i))
+ throw new DefaultsError("`" + i + "` is not a supported option", defs);
for (var i in defs) if (HOP(defs, i)) {
ret[i] = (args && HOP(args, i)) ? args[i] : defs[i];
}