Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove calls to console.* #293

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var options = {
squeeze: true,
make_seqs: true,
dead_code: true,
no_console: false,
verbose: false,
show_copyright: true,
out_same_file: false,
Expand Down Expand Up @@ -76,6 +77,9 @@ out: while (args.length > 0) {
case "--no-dead-code":
options.dead_code = false;
break;
case "--no-console":
options.no_console = true;
break;
case "--no-copyright":
case "-nc":
options.show_copyright = false;
Expand Down Expand Up @@ -281,6 +285,9 @@ function squeeze_it(code) {
if (options.lift_vars) {
ast = time_it("lift", function(){ return pro.ast_lift_variables(ast); });
}
if (options.no_console) {
ast = time_it("console", function(){ return pro.ast_squeeze_console(ast); });
}
if (options.mangle) ast = time_it("mangle", function(){
return pro.ast_mangle(ast, {
toplevel : options.mangle_toplevel,
Expand Down
4 changes: 3 additions & 1 deletion lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -2006,4 +2006,6 @@ exports.split_lines = split_lines;
exports.MAP = MAP;

// keep this last!
exports.ast_squeeze_more = require("./squeeze-more").ast_squeeze_more;
var more = require("./squeeze-more");
exports.ast_squeeze_more = more.ast_squeeze_more;
exports.ast_squeeze_console = more.ast_squeeze_console;
20 changes: 20 additions & 0 deletions lib/squeeze-more.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,24 @@ function ast_squeeze_more(ast) {
});
};

function ast_squeeze_console(ast) {
var w = pro.ast_walker(), walk = w.walk, scope;
return w.with_walkers({
"stat": function(stmt) {
if(stmt[0] === "call" && stmt[1][0] == "dot" && stmt[1][1] instanceof Array && stmt[1][1][0] == 'name' && stmt[1][1][1] == "console") {
return ["block"];
}
return ["stat", walk(stmt)];
},
"call": function(expr, args) {
if (expr[0] == "dot" && expr[1] instanceof Array && expr[1][0] == 'name' && expr[1][1] == "console") {
return ["atom", "0"];
}
}
}, function() {
return walk(ast);
});
};

exports.ast_squeeze_more = ast_squeeze_more;
exports.ast_squeeze_console = ast_squeeze_console;
1 change: 1 addition & 0 deletions test/unit/compress/expected/console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var f=function(a,b,c){return a+b+c}
3 changes: 3 additions & 0 deletions test/unit/compress/test/console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log("test")

var f = function(a, b, c){console.log(a,b,c);return a + b + c;};
11 changes: 7 additions & 4 deletions test/unit/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ var Script = process.binding('evals').Script;

var scriptsPath = __dirname;

function compress(code) {
function compress(code, squeezeConsole) {
var ast = jsp.parse(code);
ast = pro.ast_mangle(ast);
if(squeezeConsole) {
ast = pro.ast_squeeze_console(ast);
}
ast = pro.ast_squeeze(ast, { no_warnings: true });
ast = pro.ast_squeeze_more(ast);
ast = pro.ast_squeeze_more(ast);
return pro.gen_code(ast);
};

Expand All @@ -25,13 +28,13 @@ function getTester(script) {
var testPath = path.join(testDir, script);
var expectedPath = path.join(expectedDir, script);
var content = fs.readFileSync(testPath, 'utf-8');
var outputCompress = compress(content);
var outputCompress = compress(content, script==="console.js");

// Check if the noncompressdata is larger or same size as the compressed data
test.ok(content.length >= outputCompress.length);

// Check that a recompress gives the same result
var outputReCompress = compress(content);
var outputReCompress = compress(content, script==="console.js");
test.equal(outputCompress, outputReCompress);

// Check if the compressed output is what is expected
Expand Down