Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mishoo/UglifyJS2
Browse files Browse the repository at this point in the history
# Conflicts:
#	.travis.yml
#	README.md
#	package.json
#	test/compress/drop-unused.js
#	test/mozilla-ast.js
#	test/run-tests.js
  • Loading branch information
luis-pabon-tf committed Oct 30, 2015
2 parents 49147ea + bd0ae65 commit 9f5e9ca
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 148 deletions.
36 changes: 18 additions & 18 deletions bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ You need to pass an argument to this option to specify the name that your module
.describe("mangle-props", "Mangle property names")
.describe("mangle-regex", "Only mangle property names matching the regex")
.describe("name-cache", "File to hold mangled names mappings")
.describe("pure-funcs", "List of functions that can be safely removed if their return value is not used")

.alias("p", "prefix")
.alias("o", "output")
Expand Down Expand Up @@ -104,6 +105,7 @@ You need to pass an argument to this option to specify the name that your module
.string("prefix")
.string("name-cache")
.array("reserved-file")
.array("pure-funcs")

.boolean("expr")
.boolean("source-map-include-sources")
Expand Down Expand Up @@ -175,6 +177,10 @@ if (ARGS.d) {
if (COMPRESS) COMPRESS.global_defs = getOptions("d");
}

if (ARGS.pure_funcs) {
if (COMPRESS) COMPRESS.pure_funcs = ARGS.pure_funcs;
}

if (ARGS.r) {
if (MANGLE) MANGLE.except = ARGS.r.replace(/^\s+|\s+$/g).split(/\s*,+\s*/);
}
Expand Down Expand Up @@ -254,7 +260,6 @@ if (ARGS.self) {
}
files = UglifyJS.FILES;
if (!ARGS.wrap) ARGS.wrap = "UglifyJS";
ARGS.export_all = true;
}

var ORIG_MAP = ARGS.in_source_map;
Expand Down Expand Up @@ -402,32 +407,27 @@ async.eachLimit(files, 1, function (file, cb) {
writeNameCache("props", cache);
})();

var SCOPE_IS_NEEDED = COMPRESS || MANGLE || ARGS.lint;
var TL_CACHE = readNameCache("vars");

if (SCOPE_IS_NEEDED) {
time_it("scope", function(){
TOPLEVEL.figure_out_scope({ screw_ie8: ARGS.screw_ie8, cache: TL_CACHE });
if (ARGS.lint) {
TOPLEVEL.scope_warnings();
}
});
}
time_it("scope", function(){
TOPLEVEL.figure_out_scope({ screw_ie8: ARGS.screw_ie8, cache: TL_CACHE });
if (ARGS.lint) {
TOPLEVEL.scope_warnings();
}
});

if (COMPRESS) {
time_it("squeeze", function(){
TOPLEVEL = TOPLEVEL.transform(compressor);
});
}

if (SCOPE_IS_NEEDED) {
time_it("scope", function(){
TOPLEVEL.figure_out_scope({ screw_ie8: ARGS.screw_ie8, cache: TL_CACHE });
if (MANGLE && !TL_CACHE) {
TOPLEVEL.compute_char_frequency(MANGLE);
}
});
}
time_it("scope", function(){
TOPLEVEL.figure_out_scope({ screw_ie8: ARGS.screw_ie8, cache: TL_CACHE });
if (MANGLE && !TL_CACHE) {
TOPLEVEL.compute_char_frequency(MANGLE);
}
});

if (MANGLE) time_it("mangle", function(){
MANGLE.cache = TL_CACHE;
Expand Down
13 changes: 7 additions & 6 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function DEFNODE(type, props, methods, base) {
ctor.DEFMETHOD = function(name, method) {
this.prototype[name] = method;
};
exports["AST_" + type] = ctor;
return ctor;
};

Expand Down Expand Up @@ -329,12 +330,11 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
}
}));
}
var wrapped_tl = "(function(exports, global){ global['" + name + "'] = exports; '$ORIG'; '$EXPORTS'; }({}, (function(){return this}())))";
var wrapped_tl = "(function(exports, global){ '$ORIG'; '$EXPORTS'; global['" + name + "'] = exports; }({}, (function(){return this}())))";
wrapped_tl = parse(wrapped_tl);
wrapped_tl = wrapped_tl.transform(new TreeTransformer(function before(node){
if (node instanceof AST_SimpleStatement) {
node = node.body;
if (node instanceof AST_String) switch (node.getValue()) {
if (node instanceof AST_Directive) {
switch (node.value) {
case "$ORIG":
return MAP.splice(self.body);
case "$EXPORTS":
Expand Down Expand Up @@ -864,10 +864,11 @@ var AST_String = DEFNODE("String", "value quote", {
}
}, AST_Constant);

var AST_Number = DEFNODE("Number", "value", {
var AST_Number = DEFNODE("Number", "value literal", {
$documentation: "A number literal",
$propdoc: {
value: "[number] the numeric value"
value: "[number] the numeric value",
literal: "[string] numeric value as string (optional)"
}
}, AST_Constant);

Expand Down
51 changes: 28 additions & 23 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function Compressor(options, false_by_default) {
loops : !false_by_default,
unused : !false_by_default,
hoist_funs : !false_by_default,
keep_fargs : false,
keep_fargs : true,
keep_fnames : false,
hoist_vars : false,
if_return : !false_by_default,
Expand Down Expand Up @@ -111,6 +111,7 @@ merge(Compressor.prototype, {
node.DEFMETHOD("optimize", function(compressor){
var self = this;
if (self._optimized) return self;
if (compressor.has_directive("use asm")) return self;
var opt = optimizer(self, compressor);
opt._optimized = true;
if (opt === self) return opt;
Expand Down Expand Up @@ -1026,6 +1027,7 @@ merge(Compressor.prototype, {

AST_Scope.DEFMETHOD("drop_unused", function(compressor){
var self = this;
if (compressor.has_directive("use asm")) return self;
if (compressor.option("unused")
&& !(self instanceof AST_Toplevel)
&& !self.uses_eval
Expand Down Expand Up @@ -1087,7 +1089,7 @@ merge(Compressor.prototype, {
var tt = new TreeTransformer(
function before(node, descend, in_list) {
if (node instanceof AST_Lambda && !(node instanceof AST_Accessor)) {
if (compressor.option("unsafe") && !compressor.option("keep_fargs")) {
if (!compressor.option("keep_fargs")) {
for (var a = node.argnames, i = a.length; --i >= 0;) {
var sym = a[i];
if (sym.unreferenced()) {
Expand Down Expand Up @@ -1205,9 +1207,10 @@ merge(Compressor.prototype, {
});

AST_Scope.DEFMETHOD("hoist_declarations", function(compressor){
var self = this;
if (compressor.has_directive("use asm")) return self;
var hoist_funs = compressor.option("hoist_funs");
var hoist_vars = compressor.option("hoist_vars");
var self = this;
if (hoist_funs || hoist_vars) {
var dirs = [];
var hoisted = [];
Expand Down Expand Up @@ -1910,15 +1913,11 @@ merge(Compressor.prototype, {
if (!compressor.option("side_effects"))
return self;
if (!self.car.has_side_effects(compressor)) {
// we shouldn't compress (1,eval)(something) to
// eval(something) because that changes the meaning of
// eval (becomes lexical instead of global).
var p;
if (!(self.cdr instanceof AST_SymbolRef
&& self.cdr.name == "eval"
&& self.cdr.undeclared()
&& (p = compressor.parent()) instanceof AST_Call
&& p.expression === self)) {
// we shouldn't compress (1,func)(something) to
// func(something) because that changes the meaning of
// the func (becomes lexical instead of global).
var p = compressor.parent();
if (!(p instanceof AST_Call && p.expression === self)) {
return self.cdr;
}
}
Expand Down Expand Up @@ -2032,15 +2031,14 @@ merge(Compressor.prototype, {
var commutativeOperators = makePredicate("== === != !== * & | ^");

OPT(AST_Binary, function(self, compressor){
var reverse = compressor.has_directive("use asm") ? noop
: function(op, force) {
if (force || !(self.left.has_side_effects(compressor) || self.right.has_side_effects(compressor))) {
if (op) self.operator = op;
var tmp = self.left;
self.left = self.right;
self.right = tmp;
}
};
function reverse(op, force) {
if (force || !(self.left.has_side_effects(compressor) || self.right.has_side_effects(compressor))) {
if (op) self.operator = op;
var tmp = self.left;
self.left = self.right;
self.right = tmp;
}
}
if (commutativeOperators(self.operator)) {
if (self.right instanceof AST_Constant
&& !(self.left instanceof AST_Constant)) {
Expand Down Expand Up @@ -2108,10 +2106,10 @@ merge(Compressor.prototype, {
if (compressor.option("conditionals")) {
if (self.operator == "&&") {
var ll = self.left.evaluate(compressor);
var rr = self.right.evaluate(compressor);
if (ll.length > 1) {
if (ll[1]) {
compressor.warn("Condition left of && always true [{file}:{line},{col}]", self.start);
var rr = self.right.evaluate(compressor);
return rr[0];
} else {
compressor.warn("Condition left of && always false [{file}:{line},{col}]", self.start);
Expand All @@ -2121,13 +2119,13 @@ merge(Compressor.prototype, {
}
else if (self.operator == "||") {
var ll = self.left.evaluate(compressor);
var rr = self.right.evaluate(compressor);
if (ll.length > 1) {
if (ll[1]) {
compressor.warn("Condition left of || always true [{file}:{line},{col}]", self.start);
return ll[0];
} else {
compressor.warn("Condition left of || always false [{file}:{line},{col}]", self.start);
var rr = self.right.evaluate(compressor);
return rr[0];
}
}
Expand Down Expand Up @@ -2521,4 +2519,11 @@ merge(Compressor.prototype, {
OPT(AST_Object, literals_in_boolean_context);
OPT(AST_RegExp, literals_in_boolean_context);

OPT(AST_Return, function(self, compressor){
if (self.value instanceof AST_Undefined) {
self.value = null;
}
return self;
});

})();
6 changes: 3 additions & 3 deletions lib/mozilla-ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@

function map(moztype, mytype, propmap) {
var moz_to_me = "function From_Moz_" + moztype + "(M){\n";
moz_to_me += "return new " + mytype.name + "({\n" +
moz_to_me += "return new U2." + mytype.name + "({\n" +
"start: my_start_token(M),\n" +
"end: my_end_token(M)";

Expand Down Expand Up @@ -442,8 +442,8 @@
//me_to_moz = parse(me_to_moz).print_to_string({ beautify: true });
//console.log(moz_to_me);

moz_to_me = new Function("my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")(
my_start_token, my_end_token, from_moz
moz_to_me = new Function("U2", "my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")(
exports, my_start_token, my_end_token, from_moz
);
me_to_moz = new Function("to_moz", "to_moz_block", "return(" + me_to_moz + ")")(
to_moz, to_moz_block
Expand Down
Loading

0 comments on commit 9f5e9ca

Please sign in to comment.