Compare commits
34 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
547561a568 | ||
|
|
c16d538ce7 | ||
|
|
73d082df2e | ||
|
|
50b8d7272c | ||
|
|
7d11b96f48 | ||
|
|
eab99a1c3d | ||
|
|
19e2fb134d | ||
|
|
f4919e3a25 | ||
|
|
bb700daa4c | ||
|
|
263577d5eb | ||
|
|
63287c0e68 | ||
|
|
c5ed2292bf | ||
|
|
b70670b69f | ||
|
|
9dd97605bc | ||
|
|
e4c5302406 | ||
|
|
bea3d90771 | ||
|
|
785c6064cc | ||
|
|
b214d3786f | ||
|
|
7cf79c302b | ||
|
|
a14c6b6574 | ||
|
|
f1b7094a57 | ||
|
|
0358e376f0 | ||
|
|
b47f7b76b9 | ||
|
|
582cc55cff | ||
|
|
8979579e55 | ||
|
|
0d6e08c541 | ||
|
|
e2daee9a65 | ||
|
|
9cd118ca3d | ||
|
|
cfd5c6155c | ||
|
|
1a5a4bd631 | ||
|
|
63e1a8e1fd | ||
|
|
7055af8221 | ||
|
|
aafe2e1db3 | ||
|
|
118105db43 |
10
README.md
10
README.md
@@ -87,6 +87,10 @@ The available options are:
|
||||
Note that currently not *all* comments can be kept when
|
||||
compression is on, because of dead code removal or
|
||||
cascading statements into sequences. [string]
|
||||
--preamble Preamble to prepend to the output. You can use this to
|
||||
insert a comment, for example for licensing information.
|
||||
This will not be parsed, but the source map will adjust
|
||||
for its presence.
|
||||
--stats Display operations run time on STDERR. [boolean]
|
||||
--acorn Use Acorn for parsing. [boolean]
|
||||
--spidermonkey Assume input files are SpiderMonkey AST format (as JSON).
|
||||
@@ -257,7 +261,7 @@ when this flag is on:
|
||||
- `String(exp)` or `exp.toString()` → `"" + exp`
|
||||
- `new Object/RegExp/Function/Error/Array (...)` → we discard the `new`
|
||||
- `typeof foo == "undefined"` → `foo === void 0`
|
||||
- `void 0` → `"undefined"` (if there is a variable named "undefined" in
|
||||
- `void 0` → `undefined` (if there is a variable named "undefined" in
|
||||
scope; we do it because the variable name will be mangled, typically
|
||||
reduced to a single character).
|
||||
|
||||
@@ -328,6 +332,10 @@ can pass additional arguments that control the code output:
|
||||
you pass `false` then whenever possible we will use a newline instead of a
|
||||
semicolon, leading to more readable output of uglified code (size before
|
||||
gzip could be smaller; size after gzip insignificantly larger).
|
||||
- `preamble` (default `null`) -- when passed it must be a string and
|
||||
it will be prepended to the output literally. The source map will
|
||||
adjust for this text. Can be used to insert a comment containing
|
||||
licensing information, for example.
|
||||
|
||||
### Keeping copyright notices or other comments
|
||||
|
||||
|
||||
25
bin/uglifyjs
25
bin/uglifyjs
@@ -48,6 +48,10 @@ You can optionally pass one of the following arguments to this flag:\n\
|
||||
Note that currently not *all* comments can be kept when compression is on, \
|
||||
because of dead code removal or cascading statements into sequences.")
|
||||
|
||||
.describe("preamble", "Preamble to prepend to the output. You can use this to insert a \
|
||||
comment, for example for licensing information. This will not be \
|
||||
parsed, but the source map will adjust for its presence.")
|
||||
|
||||
.describe("stats", "Display operations run time on STDERR.")
|
||||
.describe("acorn", "Use Acorn for parsing.")
|
||||
.describe("spidermonkey", "Assume input files are SpiderMonkey AST format (as JSON).")
|
||||
@@ -58,6 +62,7 @@ You need to pass an argument to this option to specify the name that your module
|
||||
.describe("lint", "Display some scope warnings")
|
||||
.describe("v", "Verbose")
|
||||
.describe("V", "Print version number and exit.")
|
||||
.describe("noerr", "Don't throw an error for unknown options in -c, -b or -m.")
|
||||
|
||||
.alias("p", "prefix")
|
||||
.alias("o", "output")
|
||||
@@ -92,6 +97,7 @@ You need to pass an argument to this option to specify the name that your module
|
||||
.boolean("spidermonkey")
|
||||
.boolean("lint")
|
||||
.boolean("V")
|
||||
.boolean("noerr")
|
||||
|
||||
.wrap(80)
|
||||
|
||||
@@ -100,6 +106,12 @@ You need to pass an argument to this option to specify the name that your module
|
||||
|
||||
normalize(ARGS);
|
||||
|
||||
if (ARGS.noerr) {
|
||||
UglifyJS.DefaultsError.croak = function(msg, defs) {
|
||||
sys.error("WARN: " + msg);
|
||||
};
|
||||
}
|
||||
|
||||
if (ARGS.version || ARGS.V) {
|
||||
var json = require("../package.json");
|
||||
sys.puts(json.name + ' ' + json.version);
|
||||
@@ -134,7 +146,8 @@ if (ARGS.r) {
|
||||
}
|
||||
|
||||
var OUTPUT_OPTIONS = {
|
||||
beautify: BEAUTIFY ? true : false
|
||||
beautify: BEAUTIFY ? true : false,
|
||||
preamble: ARGS.preamble || null,
|
||||
};
|
||||
|
||||
if (ARGS.screw_ie8) {
|
||||
@@ -251,7 +264,6 @@ async.eachLimit(files, 1, function (file, cb) {
|
||||
else if (ARGS.acorn) {
|
||||
TOPLEVEL = acorn.parse(code, {
|
||||
locations : true,
|
||||
trackComments : true,
|
||||
sourceFile : file,
|
||||
program : TOPLEVEL
|
||||
});
|
||||
@@ -377,7 +389,7 @@ function getOptions(x, constants) {
|
||||
if (x !== true) {
|
||||
var ast;
|
||||
try {
|
||||
ast = UglifyJS.parse(x);
|
||||
ast = UglifyJS.parse(x, { expression: true });
|
||||
} catch(ex) {
|
||||
if (ex instanceof UglifyJS.JS_Parse_Error) {
|
||||
sys.error("Error parsing arguments in: " + x);
|
||||
@@ -385,8 +397,6 @@ function getOptions(x, constants) {
|
||||
}
|
||||
}
|
||||
ast.walk(new UglifyJS.TreeWalker(function(node){
|
||||
if (node instanceof UglifyJS.AST_Toplevel) return; // descend
|
||||
if (node instanceof UglifyJS.AST_SimpleStatement) return; // descend
|
||||
if (node instanceof UglifyJS.AST_Seq) return; // descend
|
||||
if (node instanceof UglifyJS.AST_Assign) {
|
||||
var name = node.left.print_to_string({ beautify: false }).replace(/-/g, "_");
|
||||
@@ -396,6 +406,11 @@ function getOptions(x, constants) {
|
||||
ret[name] = value;
|
||||
return true; // no descend
|
||||
}
|
||||
if (node instanceof UglifyJS.AST_Symbol || node instanceof UglifyJS.AST_Binary) {
|
||||
var name = node.print_to_string({ beautify: false }).replace(/-/g, "_");
|
||||
ret[name] = true;
|
||||
return true; // no descend
|
||||
}
|
||||
sys.error(node.TYPE)
|
||||
sys.error("Error parsing arguments in: " + x);
|
||||
process.exit(1);
|
||||
|
||||
@@ -371,7 +371,7 @@ var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments", {
|
||||
}, AST_Scope);
|
||||
|
||||
var AST_Accessor = DEFNODE("Accessor", null, {
|
||||
$documentation: "A setter/getter function"
|
||||
$documentation: "A setter/getter function. The `name` property is always null."
|
||||
}, AST_Lambda);
|
||||
|
||||
var AST_Function = DEFNODE("Function", null, {
|
||||
@@ -756,7 +756,7 @@ var AST_Object = DEFNODE("Object", "properties", {
|
||||
var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", {
|
||||
$documentation: "Base class for literal object properties",
|
||||
$propdoc: {
|
||||
key: "[string] the property name; it's always a plain string in our AST, no matter if it was a string, number or identifier in original code",
|
||||
key: "[string] the property name converted to a string for ObjectKeyVal. For setters and getters this is an arbitrary AST_Node.",
|
||||
value: "[AST_Node] property value. For setters and getters this is an AST_Function."
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
|
||||
@@ -85,14 +85,16 @@ merge(Compressor.prototype, {
|
||||
},
|
||||
before: function(node, descend, in_list) {
|
||||
if (node._squeezed) return node;
|
||||
var was_scope = false;
|
||||
if (node instanceof AST_Scope) {
|
||||
node.drop_unused(this);
|
||||
node = node.hoist_declarations(this);
|
||||
was_scope = true;
|
||||
}
|
||||
descend(node, this);
|
||||
node = node.optimize(this);
|
||||
if (node instanceof AST_Scope) {
|
||||
if (was_scope && node instanceof AST_Scope) {
|
||||
node.drop_unused(this);
|
||||
descend(node, this);
|
||||
}
|
||||
node._squeezed = true;
|
||||
return node;
|
||||
@@ -1082,18 +1084,23 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
return node;
|
||||
}
|
||||
if (node instanceof AST_For && node.init instanceof AST_BlockStatement) {
|
||||
if (node instanceof AST_For) {
|
||||
descend(node, this);
|
||||
// certain combination of unused name + side effect leads to:
|
||||
// https://github.com/mishoo/UglifyJS2/issues/44
|
||||
// that's an invalid AST.
|
||||
// We fix it at this stage by moving the `var` outside the `for`.
|
||||
var body = node.init.body.slice(0, -1);
|
||||
node.init = node.init.body.slice(-1)[0].body;
|
||||
body.push(node);
|
||||
return in_list ? MAP.splice(body) : make_node(AST_BlockStatement, node, {
|
||||
body: body
|
||||
});
|
||||
|
||||
if (node.init instanceof AST_BlockStatement) {
|
||||
// certain combination of unused name + side effect leads to:
|
||||
// https://github.com/mishoo/UglifyJS2/issues/44
|
||||
// that's an invalid AST.
|
||||
// We fix it at this stage by moving the `var` outside the `for`.
|
||||
|
||||
var body = node.init.body.slice(0, -1);
|
||||
node.init = node.init.body.slice(-1)[0].body;
|
||||
body.push(node);
|
||||
|
||||
return in_list ? MAP.splice(body) : make_node(AST_BlockStatement, node, {
|
||||
body: body
|
||||
});
|
||||
}
|
||||
}
|
||||
if (node instanceof AST_Scope && node !== self)
|
||||
return node;
|
||||
@@ -1614,7 +1621,7 @@ merge(Compressor.prototype, {
|
||||
if (self.args.length != 1) {
|
||||
return make_node(AST_Array, self, {
|
||||
elements: self.args
|
||||
});
|
||||
}).transform(compressor);
|
||||
}
|
||||
break;
|
||||
case "Object":
|
||||
@@ -1628,11 +1635,30 @@ merge(Compressor.prototype, {
|
||||
if (self.args.length == 0) return make_node(AST_String, self, {
|
||||
value: ""
|
||||
});
|
||||
return make_node(AST_Binary, self, {
|
||||
if (self.args.length <= 1) return make_node(AST_Binary, self, {
|
||||
left: self.args[0],
|
||||
operator: "+",
|
||||
right: make_node(AST_String, self, { value: "" })
|
||||
}).transform(compressor);
|
||||
break;
|
||||
case "Number":
|
||||
if (self.args.length == 0) return make_node(AST_Number, self, {
|
||||
value: 0
|
||||
});
|
||||
if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
|
||||
expression: self.args[0],
|
||||
operator: "+"
|
||||
}).transform(compressor);
|
||||
case "Boolean":
|
||||
if (self.args.length == 0) return make_node(AST_False, self);
|
||||
if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
|
||||
expression: make_node(AST_UnaryPrefix, null, {
|
||||
expression: self.args[0],
|
||||
operator: "!"
|
||||
}),
|
||||
operator: "!"
|
||||
}).transform(compressor);
|
||||
break;
|
||||
case "Function":
|
||||
if (all(self.args, function(x){ return x instanceof AST_String })) {
|
||||
// quite a corner-case, but we can handle it:
|
||||
@@ -1840,6 +1866,14 @@ merge(Compressor.prototype, {
|
||||
return self.evaluate(compressor)[0];
|
||||
});
|
||||
|
||||
function has_side_effects_or_prop_access(node, compressor) {
|
||||
var save_pure_getters = compressor.option("pure_getters");
|
||||
compressor.options.pure_getters = false;
|
||||
var ret = node.has_side_effects(compressor);
|
||||
compressor.options.pure_getters = save_pure_getters;
|
||||
return ret;
|
||||
}
|
||||
|
||||
AST_Binary.DEFMETHOD("lift_sequences", function(compressor){
|
||||
if (compressor.option("sequences")) {
|
||||
if (this.left instanceof AST_Seq) {
|
||||
@@ -1851,8 +1885,8 @@ merge(Compressor.prototype, {
|
||||
return seq;
|
||||
}
|
||||
if (this.right instanceof AST_Seq
|
||||
&& !(this.operator == "||" || this.operator == "&&")
|
||||
&& !this.left.has_side_effects(compressor)) {
|
||||
&& this instanceof AST_Assign
|
||||
&& !has_side_effects_or_prop_access(this.left, compressor)) {
|
||||
var seq = this.right;
|
||||
var x = seq.to_array();
|
||||
this.right = x.pop();
|
||||
@@ -1882,7 +1916,11 @@ merge(Compressor.prototype, {
|
||||
// if right is a constant, whatever side effects the
|
||||
// left side might have could not influence the
|
||||
// result. hence, force switch.
|
||||
reverse(null, true);
|
||||
|
||||
if (!(self.left instanceof AST_Binary
|
||||
&& PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) {
|
||||
reverse(null, true);
|
||||
}
|
||||
}
|
||||
if (/^[!=]==?$/.test(self.operator)) {
|
||||
if (self.left instanceof AST_SymbolRef && self.right instanceof AST_Conditional) {
|
||||
@@ -2032,7 +2070,8 @@ merge(Compressor.prototype, {
|
||||
&& self.left.right instanceof AST_Constant
|
||||
&& self.right instanceof AST_Binary
|
||||
&& self.right.operator == "+"
|
||||
&& self.right.left instanceof AST_Constant) {
|
||||
&& self.right.left instanceof AST_Constant
|
||||
&& self.right.is_string(compressor)) {
|
||||
self = make_node(AST_Binary, self, {
|
||||
operator: "+",
|
||||
left: make_node(AST_Binary, self.left, {
|
||||
@@ -2049,6 +2088,19 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
}
|
||||
// x * (y * z) ==> x * y * z
|
||||
if (self.right instanceof AST_Binary
|
||||
&& self.right.operator == self.operator
|
||||
&& (self.operator == "*" || self.operator == "&&" || self.operator == "||"))
|
||||
{
|
||||
self.left = make_node(AST_Binary, self.left, {
|
||||
operator : self.operator,
|
||||
left : self.left,
|
||||
right : self.right.left
|
||||
});
|
||||
self.right = self.right.right;
|
||||
return self.transform(compressor);
|
||||
}
|
||||
return self.evaluate(compressor)[0];
|
||||
});
|
||||
|
||||
|
||||
@@ -61,6 +61,7 @@ function OutputStream(options) {
|
||||
comments : false,
|
||||
preserve_line : false,
|
||||
screw_ie8 : false,
|
||||
preamble : null,
|
||||
}, true);
|
||||
|
||||
var indentation = 0;
|
||||
@@ -299,6 +300,10 @@ function OutputStream(options) {
|
||||
return OUTPUT;
|
||||
};
|
||||
|
||||
if (options.preamble) {
|
||||
print(options.preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, "\n"));
|
||||
}
|
||||
|
||||
var stack = [];
|
||||
return {
|
||||
get : get,
|
||||
@@ -378,14 +383,15 @@ function OutputStream(options) {
|
||||
var start = self.start;
|
||||
if (start && !start._comments_dumped) {
|
||||
start._comments_dumped = true;
|
||||
var comments = start.comments_before;
|
||||
var comments = start.comments_before || [];
|
||||
|
||||
// XXX: ugly fix for https://github.com/mishoo/UglifyJS2/issues/112
|
||||
// if this node is `return` or `throw`, we cannot allow comments before
|
||||
// the returned or thrown value.
|
||||
if (self instanceof AST_Exit &&
|
||||
self.value && self.value.start.comments_before.length > 0) {
|
||||
comments = (comments || []).concat(self.value.start.comments_before);
|
||||
if (self instanceof AST_Exit && self.value
|
||||
&& self.value.start.comments_before
|
||||
&& self.value.start.comments_before.length > 0) {
|
||||
comments = comments.concat(self.value.start.comments_before);
|
||||
self.value.start.comments_before = [];
|
||||
}
|
||||
|
||||
@@ -475,11 +481,7 @@ function OutputStream(options) {
|
||||
var so = this.operator, sp = PRECEDENCE[so];
|
||||
if (pp > sp
|
||||
|| (pp == sp
|
||||
&& this === p.right
|
||||
&& !(so == po &&
|
||||
(so == "*" ||
|
||||
so == "&&" ||
|
||||
so == "||")))) {
|
||||
&& this === p.right)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -506,8 +508,17 @@ function OutputStream(options) {
|
||||
});
|
||||
|
||||
PARENS(AST_Call, function(output){
|
||||
var p = output.parent();
|
||||
return p instanceof AST_New && p.expression === this;
|
||||
var p = output.parent(), p1;
|
||||
if (p instanceof AST_New && p.expression === this)
|
||||
return true;
|
||||
|
||||
// workaround for Safari bug.
|
||||
// https://bugs.webkit.org/show_bug.cgi?id=123506
|
||||
return this.expression instanceof AST_Function
|
||||
&& p instanceof AST_PropAccess
|
||||
&& p.expression === this
|
||||
&& (p1 = output.parent(1)) instanceof AST_Assign
|
||||
&& p1.left === p;
|
||||
});
|
||||
|
||||
PARENS(AST_New, function(output){
|
||||
@@ -1064,10 +1075,14 @@ function OutputStream(options) {
|
||||
});
|
||||
DEFPRINT(AST_ObjectSetter, function(self, output){
|
||||
output.print("set");
|
||||
output.space();
|
||||
self.key.print(output);
|
||||
self.value._do_print(output, true);
|
||||
});
|
||||
DEFPRINT(AST_ObjectGetter, function(self, output){
|
||||
output.print("get");
|
||||
output.space();
|
||||
self.key.print(output);
|
||||
self.value._do_print(output, true);
|
||||
});
|
||||
DEFPRINT(AST_Symbol, function(self, output){
|
||||
|
||||
49
lib/parse.js
49
lib/parse.js
@@ -579,10 +579,10 @@ var UNARY_POSTFIX = makePredicate([ "--", "++" ]);
|
||||
var ASSIGNMENT = makePredicate([ "=", "+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&=" ]);
|
||||
|
||||
var PRECEDENCE = (function(a, ret){
|
||||
for (var i = 0, n = 1; i < a.length; ++i, ++n) {
|
||||
for (var i = 0; i < a.length; ++i) {
|
||||
var b = a[i];
|
||||
for (var j = 0; j < b.length; ++j) {
|
||||
ret[b[j]] = n;
|
||||
ret[b[j]] = i + 1;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
@@ -789,7 +789,7 @@ function parse($TEXT, options) {
|
||||
return for_();
|
||||
|
||||
case "function":
|
||||
return function_(true);
|
||||
return function_(AST_Defun);
|
||||
|
||||
case "if":
|
||||
return if_();
|
||||
@@ -933,19 +933,12 @@ function parse($TEXT, options) {
|
||||
});
|
||||
};
|
||||
|
||||
var function_ = function(in_statement, ctor) {
|
||||
var is_accessor = ctor === AST_Accessor;
|
||||
var name = (is("name") ? as_symbol(in_statement
|
||||
? AST_SymbolDefun
|
||||
: is_accessor
|
||||
? AST_SymbolAccessor
|
||||
: AST_SymbolLambda)
|
||||
: is_accessor && (is("string") || is("num")) ? as_atom_node()
|
||||
: null);
|
||||
var function_ = function(ctor) {
|
||||
var in_statement = ctor === AST_Defun;
|
||||
var name = is("name") ? as_symbol(in_statement ? AST_SymbolDefun : AST_SymbolLambda) : null;
|
||||
if (in_statement && !name)
|
||||
unexpected();
|
||||
expect("(");
|
||||
if (!ctor) ctor = in_statement ? AST_Defun : AST_Function;
|
||||
return new ctor({
|
||||
name: name,
|
||||
argnames: (function(first, a){
|
||||
@@ -1116,7 +1109,9 @@ function parse($TEXT, options) {
|
||||
var tok = S.token, ret;
|
||||
switch (tok.type) {
|
||||
case "name":
|
||||
return as_symbol(AST_SymbolRef);
|
||||
case "keyword":
|
||||
ret = _make_symbol(AST_SymbolRef);
|
||||
break;
|
||||
case "num":
|
||||
ret = new AST_Number({ start: tok, end: tok, value: tok.value });
|
||||
break;
|
||||
@@ -1167,7 +1162,7 @@ function parse($TEXT, options) {
|
||||
}
|
||||
if (is("keyword", "function")) {
|
||||
next();
|
||||
var func = function_(false);
|
||||
var func = function_(AST_Function);
|
||||
func.start = start;
|
||||
func.end = prev();
|
||||
return subscripts(func, allow_calls);
|
||||
@@ -1215,8 +1210,8 @@ function parse($TEXT, options) {
|
||||
if (name == "get") {
|
||||
a.push(new AST_ObjectGetter({
|
||||
start : start,
|
||||
key : name,
|
||||
value : function_(false, AST_Accessor),
|
||||
key : as_atom_node(),
|
||||
value : function_(AST_Accessor),
|
||||
end : prev()
|
||||
}));
|
||||
continue;
|
||||
@@ -1224,8 +1219,8 @@ function parse($TEXT, options) {
|
||||
if (name == "set") {
|
||||
a.push(new AST_ObjectSetter({
|
||||
start : start,
|
||||
key : name,
|
||||
value : function_(false, AST_Accessor),
|
||||
key : as_atom_node(),
|
||||
value : function_(AST_Accessor),
|
||||
end : prev()
|
||||
}));
|
||||
continue;
|
||||
@@ -1273,17 +1268,21 @@ function parse($TEXT, options) {
|
||||
}
|
||||
};
|
||||
|
||||
function _make_symbol(type) {
|
||||
var name = S.token.value;
|
||||
return new (name == "this" ? AST_This : type)({
|
||||
name : String(name),
|
||||
start : S.token,
|
||||
end : S.token
|
||||
});
|
||||
};
|
||||
|
||||
function as_symbol(type, noerror) {
|
||||
if (!is("name")) {
|
||||
if (!noerror) croak("Name expected");
|
||||
return null;
|
||||
}
|
||||
var name = S.token.value;
|
||||
var sym = new (name == "this" ? AST_This : type)({
|
||||
name : String(S.token.value),
|
||||
start : S.token,
|
||||
end : S.token
|
||||
});
|
||||
var sym = _make_symbol(type);
|
||||
next();
|
||||
return sym;
|
||||
};
|
||||
|
||||
17
lib/scope.js
17
lib/scope.js
@@ -64,9 +64,9 @@ SymbolDef.prototype = {
|
||||
mangle: function(options) {
|
||||
if (!this.mangled_name && !this.unmangleable(options)) {
|
||||
var s = this.scope;
|
||||
if (this.orig[0] instanceof AST_SymbolLambda && !options.screw_ie8)
|
||||
if (!options.screw_ie8 && this.orig[0] instanceof AST_SymbolLambda)
|
||||
s = s.parent_scope;
|
||||
this.mangled_name = s.next_mangled(options);
|
||||
this.mangled_name = s.next_mangled(options, this);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -256,6 +256,19 @@ AST_Scope.DEFMETHOD("next_mangled", function(options){
|
||||
}
|
||||
});
|
||||
|
||||
AST_Function.DEFMETHOD("next_mangled", function(options, def){
|
||||
// #179, #326
|
||||
// in Safari strict mode, something like (function x(x){...}) is a syntax error;
|
||||
// a function expression's argument cannot shadow the function expression's name
|
||||
|
||||
var tricky_def = def.orig[0] instanceof AST_SymbolFunarg && this.name && this.name.definition();
|
||||
while (true) {
|
||||
var name = AST_Lambda.prototype.next_mangled.call(this, options, def);
|
||||
if (!(tricky_def && tricky_def.mangled_name == name))
|
||||
return name;
|
||||
}
|
||||
});
|
||||
|
||||
AST_Scope.DEFMETHOD("references", function(sym){
|
||||
if (sym instanceof AST_Symbol) sym = sym.definition();
|
||||
return this.enclosed.indexOf(sym) < 0 ? null : sym;
|
||||
|
||||
@@ -86,12 +86,16 @@ function DefaultsError(msg, defs) {
|
||||
this.defs = defs;
|
||||
};
|
||||
|
||||
DefaultsError.croak = function(msg, defs) {
|
||||
throw new DefaultsError(msg, defs);
|
||||
};
|
||||
|
||||
function defaults(args, defs, croak) {
|
||||
if (args === true)
|
||||
args = {};
|
||||
var ret = args || {};
|
||||
if (croak) for (var i in ret) if (ret.hasOwnProperty(i) && !defs.hasOwnProperty(i))
|
||||
throw new DefaultsError("`" + i + "` is not a supported option", defs);
|
||||
DefaultsError.croak("`" + i + "` is not a supported option", defs);
|
||||
for (var i in defs) if (defs.hasOwnProperty(i)) {
|
||||
ret[i] = (args && args.hasOwnProperty(i)) ? args[i] : defs[i];
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"description": "JavaScript parser, mangler/compressor and beautifier toolkit",
|
||||
"homepage": "http://lisperator.net/uglifyjs",
|
||||
"main": "tools/node.js",
|
||||
"version": "2.4.1",
|
||||
"version": "2.4.5",
|
||||
"engines": { "node" : ">=0.4.0" },
|
||||
"maintainers": [{
|
||||
"name": "Mihai Bazon",
|
||||
|
||||
@@ -10,6 +10,7 @@ concatenate_rhs_strings: {
|
||||
foo(bar() + 123 + "Hello" + "World" + ("Foo" + "Bar"));
|
||||
foo("Foo" + "Bar" + bar() + 123 + "Hello" + "World" + ("Foo" + "Bar"));
|
||||
foo("Hello" + bar() + 123 + "World");
|
||||
foo(bar() + 'Foo' + (10 + parseInt('10')));
|
||||
}
|
||||
expect: {
|
||||
foo(bar() + 123 + "HelloWorld");
|
||||
@@ -18,5 +19,6 @@ concatenate_rhs_strings: {
|
||||
foo(bar() + 123 + "HelloWorldFooBar");
|
||||
foo("FooBar" + bar() + "123HelloWorldFooBar");
|
||||
foo("Hello" + bar() + "123World");
|
||||
foo(bar() + 'Foo' + (10 + parseInt('10')));
|
||||
}
|
||||
}
|
||||
|
||||
11
test/compress/issue-267.js
Normal file
11
test/compress/issue-267.js
Normal file
@@ -0,0 +1,11 @@
|
||||
issue_267: {
|
||||
options = { comparisons: true };
|
||||
input: {
|
||||
x = a % b / b * c * 2;
|
||||
x = a % b * 2
|
||||
}
|
||||
expect: {
|
||||
x = a % b / b * c * 2;
|
||||
x = a % b * 2;
|
||||
}
|
||||
}
|
||||
66
test/compress/issue-269.js
Normal file
66
test/compress/issue-269.js
Normal file
@@ -0,0 +1,66 @@
|
||||
issue_269_1: {
|
||||
options = {unsafe: true};
|
||||
input: {
|
||||
f(
|
||||
String(x),
|
||||
Number(x),
|
||||
Boolean(x),
|
||||
|
||||
String(),
|
||||
Number(),
|
||||
Boolean()
|
||||
);
|
||||
}
|
||||
expect: {
|
||||
f(
|
||||
x + '', +x, !!x,
|
||||
'', 0, false
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
issue_269_dangers: {
|
||||
options = {unsafe: true};
|
||||
input: {
|
||||
f(
|
||||
String(x, x),
|
||||
Number(x, x),
|
||||
Boolean(x, x)
|
||||
);
|
||||
}
|
||||
expect: {
|
||||
f(String(x, x), Number(x, x), Boolean(x, x));
|
||||
}
|
||||
}
|
||||
|
||||
issue_269_in_scope: {
|
||||
options = {unsafe: true};
|
||||
input: {
|
||||
var String, Number, Boolean;
|
||||
f(
|
||||
String(x),
|
||||
Number(x, x),
|
||||
Boolean(x)
|
||||
);
|
||||
}
|
||||
expect: {
|
||||
var String, Number, Boolean;
|
||||
f(String(x), Number(x, x), Boolean(x));
|
||||
}
|
||||
}
|
||||
|
||||
strings_concat: {
|
||||
options = {unsafe: true};
|
||||
input: {
|
||||
f(
|
||||
String(x + 'str'),
|
||||
String('str' + x)
|
||||
);
|
||||
}
|
||||
expect: {
|
||||
f(
|
||||
x + 'str',
|
||||
'str' + x
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -101,10 +101,12 @@ lift_sequences_1: {
|
||||
lift_sequences_2: {
|
||||
options = { sequences: true, evaluate: true };
|
||||
input: {
|
||||
q = 1 + (foo(), bar(), 5) + 7 * (5 / (3 - (a(), (QW=ER), c(), 2))) - (x(), y(), 5);
|
||||
foo.x = (foo = {}, 10);
|
||||
bar = (bar = {}, 10);
|
||||
}
|
||||
expect: {
|
||||
foo(), bar(), a(), QW = ER, c(), x(), y(), q = 36
|
||||
foo.x = (foo = {}, 10),
|
||||
bar = {}, bar = 10;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,8 +7,15 @@ var assert = require("assert");
|
||||
var sys = require("util");
|
||||
|
||||
var tests_dir = path.dirname(module.filename);
|
||||
var failures = 0;
|
||||
var failed_files = {};
|
||||
|
||||
run_compress_tests();
|
||||
if (failures) {
|
||||
sys.error("\n!!! Failed " + failures + " test cases.");
|
||||
sys.error("!!! " + Object.keys(failed_files).join(", "));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
/* -----[ utils ]----- */
|
||||
|
||||
@@ -83,6 +90,8 @@ function run_compress_tests() {
|
||||
output: output,
|
||||
expected: expect
|
||||
});
|
||||
failures++;
|
||||
failed_files[file] = 1;
|
||||
}
|
||||
}
|
||||
var tests = parse_test(path.resolve(dir, file));
|
||||
|
||||
Reference in New Issue
Block a user