// maintaining various internal state that might be useful for
// squeezing nodes.
-function Compressor(options) {
+function Compressor(options, false_by_default) {
options = defaults(options, {
- sequences : true,
- dead_code : true,
- keep_comps : true,
- drop_debugger : true,
- unsafe : true
+ sequences : !false_by_default,
+ properties : !false_by_default,
+ dead_code : !false_by_default,
+ keep_comps : !false_by_default,
+ drop_debugger : !false_by_default,
+ unsafe : !false_by_default,
+
+ warnings : true
});
var stack = [];
return {
stack : function() { return stack },
parent : function(n) {
return stack[stack.length - 2 - (n || 0)];
+ },
+ warn : function() {
+ if (options.warnings)
+ AST_Node.warn.apply(AST_Node, arguments);
}
};
};
return this;
});
+ function make_node(ctor, orig, props) {
+ if (!props.start) props.start = orig.start;
+ if (!props.end) props.end = orig.end;
+ return new ctor(props);
+ };
+
function SQUEEZE(nodetype, squeeze) {
nodetype.DEFMETHOD("squeeze", function(compressor){
compressor.push_node(this);
function do_list(array, compressor) {
return MAP(array, function(node){
- if (node instanceof Array) {
- sys.debug(node.map(function(node){
- return node.TYPE;
- }).join("\n"));
- }
return node.squeeze(compressor);
});
};
return self;
});
+ function tighten_body(statements, compressor) {
+ statements = do_list(statements, compressor);
+ statements = eliminate_spurious_blocks(statements);
+ if (compressor.option("dead_code")) {
+ statements = eliminate_dead_code(statements, compressor);
+ }
+ if (compressor.option("sequences")) {
+ statements = sequencesize(statements);
+ }
+ return statements;
+ };
+
+ function eliminate_spurious_blocks(statements) {
+ return statements.reduce(function(a, stat){
+ if (stat.TYPE == "BlockStatement") {
+ // XXX: no instanceof here because we would catch
+ // AST_Lambda-s and other blocks too. perhaps we
+ // should refine the hierarchy.
+ a.push.apply(a, stat.body);
+ } else {
+ a.push(stat);
+ }
+ return a;
+ }, []);
+ }
+
+ function eliminate_dead_code(statements, compressor) {
+ var has_quit = false;
+ return statements.reduce(function(a, stat){
+ if (has_quit) {
+ if (stat instanceof AST_Defun) {
+ a.push(stat);
+ }
+ else if (compressor.option("warnings")) {
+ stat.walk(new TreeWalker(function(node){
+ if (node instanceof AST_Definitions
+ || node instanceof AST_Defun) {
+ compressor.warn("Declarations in unreachable code! [{line},{col}]", node.start);
+ if (node instanceof AST_Definitions) {
+ node = node.clone();
+ node.remove_initializers();
+ a.push(node);
+ }
+ else if (node instanceof AST_Defun) {
+ a.push(node);
+ }
+ return true;
+ }
+ if (node instanceof AST_Scope)
+ return true;
+ }))
+ };
+ } else {
+ a.push(stat);
+ if (stat instanceof AST_Jump) {
+ has_quit = true;
+ }
+ }
+ return a;
+ }, []);
+ }
+
+ function sequencesize(statements) {
+ var prev = null, last = statements.length - 1;
+ if (last) statements = statements.reduce(function(a, cur, i){
+ if (prev instanceof AST_SimpleStatement
+ && cur instanceof AST_SimpleStatement) {
+ var seq = make_node(AST_Seq, prev, {
+ first: prev.body,
+ second: cur.body
+ });
+ prev.body = seq;
+ }
+ else if (i == last && cur instanceof AST_Exit
+ && cur.value && a.length == 1) {
+ // it only makes sense to do this transformation
+ // if the AST gets to a single statement.
+ var seq = make_node(AST_Seq, prev, {
+ first: prev.body,
+ second: cur.value
+ });
+ cur.value = seq;
+ return [ cur ];
+ }
+ else {
+ a.push(cur);
+ prev = cur;
+ }
+ return a;
+ }, []);
+ return statements;
+ }
+
SQUEEZE(AST_BlockStatement, function(self, compressor){
self = self.clone();
- self.body = do_list(self.body, compressor);
+ self.body = tighten_body(self.body, compressor);
+ if (self.body.length == 1 && !self.required)
+ return self.body[0];
return self;
});
SQUEEZE(AST_EmptyStatement, function(self, compressor){
- if (compressor.parent() instanceof AST_BlockStatement)
- return MAP.skip;
+ return self;
});
SQUEEZE(AST_DWLoop, function(self, compressor){
SQUEEZE(AST_Case, function(self, compressor){
self = self.clone();
self.expression = self.expression.squeeze(compressor);
- self.body = do_list(self.body, compressor);
+ self.body = tighten_body(self.body, compressor);
return self;
});
return self;
});
+ AST_Definitions.DEFMETHOD("remove_initializers", function(){
+ this.definitions = this.definitions.map(function(def){
+ var def = def.clone();
+ def.value = null;
+ return def;
+ });
+ });
+
SQUEEZE(AST_Definitions, function(self, compressor){
self = self.clone();
self.definitions = do_list(self.definitions, compressor);
SQUEEZE(AST_Sub, function(self, compressor){
self = self.clone();
self.expression = self.expression.squeeze(compressor);
- self.property = self.property.squeeze(compressor);
+ var prop = self.property = self.property.squeeze(compressor);
+ if (prop instanceof AST_String && compressor.option("properties")) {
+ prop = prop.getValue();
+ if (is_identifier(prop)) {
+ self = new AST_Dot(self);
+ self.property = prop;
+ }
+ }
return self;
});
--- /dev/null
+#! /usr/bin/env node
+
+var U = require("../tools/node");
+var path = require("path");
+var fs = require("fs");
+var assert = require("assert");
+var sys = require("util");
+
+var tests_dir = path.dirname(module.filename);
+
+run_compress_tests();
+
+/* -----[ utils ]----- */
+
+function tmpl() {
+ return U.string_template.apply(this, arguments);
+}
+
+function log() {
+ var txt = tmpl.apply(this, arguments);
+ sys.puts(txt);
+}
+
+function log_directory(dir) {
+ log("--- Entering [{dir}]", { dir: dir });
+}
+
+function log_start_file(file) {
+ log("*** {file}", { file: file });
+}
+
+function log_test(name) {
+ log(" Running test [{name}]", { name: name });
+}
+
+function find_test_files(dir) {
+ var files = fs.readdirSync(dir).filter(function(name){
+ return /\.js$/i.test(name);
+ });
+ return files;
+}
+
+function test_directory(dir) {
+ return path.resolve(tests_dir, dir);
+}
+
+function run_compress_tests() {
+ var dir = test_directory("compress");
+ log_directory("compress");
+ var files = find_test_files(dir);
+ function test_file(file) {
+ log_start_file(file);
+ function test_case(test) {
+ log_test(test.name);
+ var cmp = new U.Compressor(test.options || {}, true);
+ var expect = make_code(test.expect, false);
+ var output = make_code(test.input.squeeze(cmp), false);
+ if (expect != output) {
+ log("!!! failed\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n---EXPECTED---\n{expected}\n\n", {
+ input: make_code(test.input),
+ output: output,
+ expected: expect
+ });
+ }
+ }
+ var tests = parse_test(path.resolve(dir, file));
+ for (var i in tests) if (tests.hasOwnProperty(i)) {
+ test_case(tests[i]);
+ }
+ }
+ files.forEach(function(file){
+ test_file(file);
+ });
+}
+
+function parse_test(file) {
+ var script = fs.readFileSync(file, "utf8");
+ var ast = U.parse(script);
+ var tests = {};
+ var tw = new U.TreeWalker(function(node, descend){
+ if (node instanceof U.AST_LabeledStatement
+ && tw.parent() instanceof U.AST_Toplevel) {
+ var name = node.label.name;
+ tests[name] = get_one_test(name, node.statement);
+ return true;
+ }
+ if (!(node instanceof U.AST_Toplevel)) croak(node);
+ });
+ ast.walk(tw);
+ return tests;
+
+ function croak(node) {
+ throw new Error(tmpl("Can't understand test file {file} [{line},{col}]\n{code}", {
+ file: file,
+ line: node.start.line,
+ col: node.start.col,
+ code: make_code(node, false)
+ }));
+ }
+
+ function get_one_test(name, block) {
+ var test = { name: name, options: {} };
+ var tw = new U.TreeWalker(function(node, descend){
+ if (node instanceof U.AST_Assign) {
+ if (!(node.left instanceof U.AST_SymbolRef)) {
+ croak(node);
+ }
+ var name = node.left.name;
+ test[name] = evaluate(node.right);
+ return true;
+ }
+ if (node instanceof U.AST_LabeledStatement) {
+ assert.ok(
+ node.label.name == "input" || node.label.name == "expect",
+ tmpl("Unsupported label {name} [{line},{col}]", {
+ name: node.label.name,
+ line: node.label.start.line,
+ col: node.label.start.col
+ })
+ );
+ var stat = node.statement;
+ if (stat instanceof U.AST_BlockStatement)
+ stat.required = 1;
+ test[node.label.name] = stat;
+ return true;
+ }
+ });
+ block.walk(tw);
+ return test;
+ };
+}
+
+function make_code(ast, beautify) {
+ if (arguments.length == 1) beautify = true;
+ var stream = U.OutputStream({ beautify: beautify });
+ ast.print(stream);
+ return stream.get();
+}
+
+function evaluate(code) {
+ if (code instanceof U.AST_Node)
+ code = make_code(code);
+ return new Function("return(" + code + ")")();
+}