Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cf45e2f79b | ||
|
|
8354758f30 | ||
|
|
9e6b128374 | ||
|
|
93cdb194f4 | ||
|
|
b633706ce4 | ||
|
|
e9920f7ca1 | ||
|
|
7e465d4a01 | ||
|
|
aa80ee349d | ||
|
|
80e81765cf | ||
|
|
711f88dcb4 |
7
.github/ISSUE_TEMPLATE.md
vendored
Normal file
7
.github/ISSUE_TEMPLATE.md
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
- Bug report or feature request?
|
||||||
|
- `uglify-js` version (`uglifyjs -V`)
|
||||||
|
- JavaScript input - ideally as small as possible.
|
||||||
|
- The `uglifyjs` CLI command executed or `minify()` options used.
|
||||||
|
- An example of JavaScript output produced and/or the error or warning.
|
||||||
|
|
||||||
|
Note: the release version of `uglify-js` only supports ES5. Those wishing to minify ES6 should use the experimental [`harmony`](https://github.com/mishoo/UglifyJS2#harmony) branch.
|
||||||
@@ -391,11 +391,11 @@ to set `true`; it's effectively a shortcut for `foo=true`).
|
|||||||
- `cascade` -- small optimization for sequences, transform `x, x` into `x`
|
- `cascade` -- small optimization for sequences, transform `x, x` into `x`
|
||||||
and `x = something(), x` into `x = something()`
|
and `x = something(), x` into `x = something()`
|
||||||
|
|
||||||
- `collapse_vars` -- default `false`. Collapse single-use `var` and `const`
|
- `collapse_vars` -- Collapse single-use `var` and `const` definitions
|
||||||
definitions when possible.
|
when possible.
|
||||||
|
|
||||||
- `reduce_vars` -- default `false`. Improve optimization on variables assigned
|
- `reduce_vars` -- Improve optimization on variables assigned with and
|
||||||
with and used as constant values.
|
used as constant values.
|
||||||
|
|
||||||
- `warnings` -- display warnings when dropping unreachable code or unused
|
- `warnings` -- display warnings when dropping unreachable code or unused
|
||||||
declarations etc.
|
declarations etc.
|
||||||
|
|||||||
@@ -281,6 +281,9 @@ merge(Compressor.prototype, {
|
|||||||
if (node instanceof AST_Function
|
if (node instanceof AST_Function
|
||||||
&& (iife = tw.parent()) instanceof AST_Call
|
&& (iife = tw.parent()) instanceof AST_Call
|
||||||
&& iife.expression === node) {
|
&& iife.expression === node) {
|
||||||
|
// Virtually turn IIFE parameters into variable definitions:
|
||||||
|
// (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})()
|
||||||
|
// So existing transformation rules can work on them.
|
||||||
node.argnames.forEach(function(arg, i) {
|
node.argnames.forEach(function(arg, i) {
|
||||||
var d = arg.definition();
|
var d = arg.definition();
|
||||||
d.fixed = iife.args[i] || make_node(AST_Undefined, iife);
|
d.fixed = iife.args[i] || make_node(AST_Undefined, iife);
|
||||||
@@ -1810,10 +1813,12 @@ merge(Compressor.prototype, {
|
|||||||
node.name = null;
|
node.name = null;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Lambda && !(node instanceof AST_Accessor)) {
|
if (node instanceof AST_Lambda && !(node instanceof AST_Accessor)) {
|
||||||
if (!compressor.option("keep_fargs")) {
|
var trim = !compressor.option("keep_fargs");
|
||||||
for (var a = node.argnames, i = a.length; --i >= 0;) {
|
for (var a = node.argnames, i = a.length; --i >= 0;) {
|
||||||
var sym = a[i];
|
var sym = a[i];
|
||||||
if (!(sym.definition().id in in_use_ids)) {
|
if (!(sym.definition().id in in_use_ids)) {
|
||||||
|
sym.__unused = true;
|
||||||
|
if (trim) {
|
||||||
a.pop();
|
a.pop();
|
||||||
compressor.warn("Dropping unused function argument {name} [{file}:{line},{col}]", {
|
compressor.warn("Dropping unused function argument {name} [{file}:{line},{col}]", {
|
||||||
name : sym.name,
|
name : sym.name,
|
||||||
@@ -1822,7 +1827,9 @@ merge(Compressor.prototype, {
|
|||||||
col : sym.start.col
|
col : sym.start.col
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else break;
|
}
|
||||||
|
else {
|
||||||
|
trim = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1840,6 +1847,7 @@ merge(Compressor.prototype, {
|
|||||||
}
|
}
|
||||||
if (drop_vars && node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn)) {
|
if (drop_vars && node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn)) {
|
||||||
var def = node.definitions.filter(function(def){
|
var def = node.definitions.filter(function(def){
|
||||||
|
if (def.value) def.value = def.value.transform(tt);
|
||||||
if (def.name.definition().id in in_use_ids) return true;
|
if (def.name.definition().id in in_use_ids) return true;
|
||||||
var w = {
|
var w = {
|
||||||
name : def.name.name,
|
name : def.name.name,
|
||||||
@@ -1900,17 +1908,15 @@ merge(Compressor.prototype, {
|
|||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
if (drop_vars && assign_as_unused) {
|
if (drop_vars && assign_as_unused
|
||||||
var n = node;
|
&& node instanceof AST_Assign
|
||||||
while (n instanceof AST_Assign
|
&& node.operator == "="
|
||||||
&& n.operator == "="
|
&& node.left instanceof AST_SymbolRef) {
|
||||||
&& n.left instanceof AST_SymbolRef) {
|
var def = node.left.definition();
|
||||||
var def = n.left.definition();
|
if (!(def.id in in_use_ids)
|
||||||
if (def.id in in_use_ids
|
&& self.variables.get(def.name) === def) {
|
||||||
|| self.variables.get(def.name) !== def) break;
|
return maintain_this_binding(tt.parent(), node, node.right.transform(tt));
|
||||||
n = n.right;
|
|
||||||
}
|
}
|
||||||
if (n !== node) return n;
|
|
||||||
}
|
}
|
||||||
if (node instanceof AST_For) {
|
if (node instanceof AST_For) {
|
||||||
descend(node, this);
|
descend(node, this);
|
||||||
@@ -2610,11 +2616,10 @@ merge(Compressor.prototype, {
|
|||||||
exp = def.fixed;
|
exp = def.fixed;
|
||||||
if (compressor.option("unused")
|
if (compressor.option("unused")
|
||||||
&& def.references.length == 1
|
&& def.references.length == 1
|
||||||
|
&& !(def.scope.uses_arguments
|
||||||
|
&& def.orig[0] instanceof AST_SymbolFunarg)
|
||||||
|
&& !def.scope.uses_eval
|
||||||
&& compressor.find_parent(AST_Scope) === def.scope) {
|
&& compressor.find_parent(AST_Scope) === def.scope) {
|
||||||
if (!compressor.option("keep_fnames")
|
|
||||||
&& exp.name && exp.name.definition() === def) {
|
|
||||||
exp.name = null;
|
|
||||||
}
|
|
||||||
self.expression = exp;
|
self.expression = exp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2622,16 +2627,26 @@ merge(Compressor.prototype, {
|
|||||||
if (compressor.option("unused")
|
if (compressor.option("unused")
|
||||||
&& exp instanceof AST_Function
|
&& exp instanceof AST_Function
|
||||||
&& !exp.uses_arguments
|
&& !exp.uses_arguments
|
||||||
&& !exp.uses_eval
|
&& !exp.uses_eval) {
|
||||||
&& self.args.length > exp.argnames.length) {
|
var pos = 0, last = 0;
|
||||||
var end = exp.argnames.length;
|
for (var i = 0, len = self.args.length; i < len; i++) {
|
||||||
for (var i = end, len = self.args.length; i < len; i++) {
|
var trim = i >= exp.argnames.length;
|
||||||
var node = self.args[i].drop_side_effect_free(compressor);
|
if (trim || exp.argnames[i].__unused) {
|
||||||
if (node) {
|
var node = self.args[i].drop_side_effect_free(compressor);
|
||||||
self.args[end++] = node;
|
if (node) {
|
||||||
|
self.args[pos++] = node;
|
||||||
|
} else if (!trim) {
|
||||||
|
self.args[pos++] = make_node(AST_Number, self.args[i], {
|
||||||
|
value: 0
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.args[pos++] = self.args[i];
|
||||||
}
|
}
|
||||||
|
last = pos;
|
||||||
}
|
}
|
||||||
self.args.length = end;
|
self.args.length = last;
|
||||||
}
|
}
|
||||||
if (compressor.option("unsafe")) {
|
if (compressor.option("unsafe")) {
|
||||||
if (exp instanceof AST_SymbolRef && exp.undeclared()) {
|
if (exp instanceof AST_SymbolRef && exp.undeclared()) {
|
||||||
|
|||||||
@@ -928,11 +928,9 @@ function parse($TEXT, options) {
|
|||||||
expression : parenthesised(),
|
expression : parenthesised(),
|
||||||
body : statement()
|
body : statement()
|
||||||
});
|
});
|
||||||
|
|
||||||
default:
|
|
||||||
unexpected();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
unexpected();
|
||||||
});
|
});
|
||||||
|
|
||||||
function labeled_statement() {
|
function labeled_statement() {
|
||||||
|
|||||||
@@ -212,9 +212,10 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
|
|||||||
self.walk(new TreeWalker(function(node, descend) {
|
self.walk(new TreeWalker(function(node, descend) {
|
||||||
if (node instanceof AST_SymbolCatch) {
|
if (node instanceof AST_SymbolCatch) {
|
||||||
var name = node.name;
|
var name = node.name;
|
||||||
|
var refs = node.thedef.references;
|
||||||
var scope = node.thedef.scope.parent_scope;
|
var scope = node.thedef.scope.parent_scope;
|
||||||
var def = scope.find_variable(name) || self.globals.get(name) || scope.def_variable(node);
|
var def = scope.find_variable(name) || self.globals.get(name) || scope.def_variable(node);
|
||||||
node.thedef.references.forEach(function(ref) {
|
refs.forEach(function(ref) {
|
||||||
ref.thedef = def;
|
ref.thedef = def;
|
||||||
ref.reference(options);
|
ref.reference(options);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
"homepage": "http://lisperator.net/uglifyjs",
|
"homepage": "http://lisperator.net/uglifyjs",
|
||||||
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
|
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
|
||||||
"license": "BSD-2-Clause",
|
"license": "BSD-2-Clause",
|
||||||
"version": "2.8.9",
|
"version": "2.8.11",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=0.8.0"
|
"node": ">=0.8.0"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -700,3 +700,94 @@ issue_1539: {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vardef_value: {
|
||||||
|
options = {
|
||||||
|
keep_fnames: false,
|
||||||
|
reduce_vars: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f() {
|
||||||
|
function g(){
|
||||||
|
return x();
|
||||||
|
}
|
||||||
|
var a = g();
|
||||||
|
return a(42);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f() {
|
||||||
|
var a = function(){
|
||||||
|
return x();
|
||||||
|
}();
|
||||||
|
return a(42);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assign_binding: {
|
||||||
|
options = {
|
||||||
|
cascade: true,
|
||||||
|
side_effects: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f() {
|
||||||
|
var a;
|
||||||
|
a = f.g, a();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f() {
|
||||||
|
(0, f.g)();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assign_chain: {
|
||||||
|
options = {
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f() {
|
||||||
|
var a, b;
|
||||||
|
x = a = y = b = 42;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f() {
|
||||||
|
x = y = 42;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
issue_1583: {
|
||||||
|
options = {
|
||||||
|
keep_fargs: true,
|
||||||
|
reduce_vars: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function m(t) {
|
||||||
|
(function(e) {
|
||||||
|
t = e();
|
||||||
|
})(function() {
|
||||||
|
return (function(a) {
|
||||||
|
return a;
|
||||||
|
})(function(a) {});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function m(t) {
|
||||||
|
(function(e) {
|
||||||
|
t = (function() {
|
||||||
|
return (function(a) {
|
||||||
|
return a;
|
||||||
|
})(function(a) {});
|
||||||
|
})();
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1144,3 +1144,111 @@ double_reference: {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
iife_arguments_1: {
|
||||||
|
options = {
|
||||||
|
reduce_vars: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
(function(x) {
|
||||||
|
console.log(x() === arguments[0]);
|
||||||
|
})(function f() {
|
||||||
|
return f;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
(function(x) {
|
||||||
|
console.log(x() === arguments[0]);
|
||||||
|
})(function f() {
|
||||||
|
return f;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
iife_arguments_2: {
|
||||||
|
options = {
|
||||||
|
reduce_vars: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
(function() {
|
||||||
|
var x = function f() {
|
||||||
|
return f;
|
||||||
|
};
|
||||||
|
console.log(x() === arguments[0]);
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
(function() {
|
||||||
|
console.log(function f() {
|
||||||
|
return f;
|
||||||
|
}() === arguments[0]);
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
iife_eval_1: {
|
||||||
|
options = {
|
||||||
|
reduce_vars: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
(function(x) {
|
||||||
|
console.log(x() === eval("x"));
|
||||||
|
})(function f() {
|
||||||
|
return f;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
(function(x) {
|
||||||
|
console.log(x() === eval("x"));
|
||||||
|
})(function f() {
|
||||||
|
return f;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
iife_eval_2: {
|
||||||
|
options = {
|
||||||
|
reduce_vars: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
(function() {
|
||||||
|
var x = function f() {
|
||||||
|
return f;
|
||||||
|
};
|
||||||
|
console.log(x() === eval("x"));
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
(function() {
|
||||||
|
var x = function f() {
|
||||||
|
return f;
|
||||||
|
};
|
||||||
|
console.log(x() === eval("x"));
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
iife_func_side_effects: {
|
||||||
|
options = {
|
||||||
|
reduce_vars: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
(function(a, b, c) {
|
||||||
|
return b();
|
||||||
|
})(x(), function() {
|
||||||
|
return y();
|
||||||
|
}, z());
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
(function(a, b, c) {
|
||||||
|
return function() {
|
||||||
|
return y();
|
||||||
|
}();
|
||||||
|
})(x(), 0, z());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -182,3 +182,39 @@ reduce_vars: {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
issue_1586_1: {
|
||||||
|
options = {
|
||||||
|
screw_ie8: false,
|
||||||
|
}
|
||||||
|
mangle = {
|
||||||
|
screw_ie8: false,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f() {
|
||||||
|
try {
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect_exact: "function f(){try{}catch(c){console.log(c.message)}}"
|
||||||
|
}
|
||||||
|
|
||||||
|
issue_1586_2: {
|
||||||
|
options = {
|
||||||
|
screw_ie8: true,
|
||||||
|
}
|
||||||
|
mangle = {
|
||||||
|
screw_ie8: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f() {
|
||||||
|
try {
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect_exact: "function f(){try{}catch(c){console.log(c.message)}}"
|
||||||
|
}
|
||||||
|
|||||||
1
test/input/invalid/loop-no-body.js
Normal file
1
test/input/invalid/loop-no-body.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
for (var i = 0; i < 1; i++)
|
||||||
@@ -238,4 +238,17 @@ describe("bin/uglifyjs", function () {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
it("Should fail with a missing loop body", function(done) {
|
||||||
|
var command = uglifyjscmd + ' test/input/invalid/loop-no-body.js';
|
||||||
|
|
||||||
|
exec(command, function (err, stdout, stderr) {
|
||||||
|
assert.ok(err);
|
||||||
|
var lines = stderr.split(/\n/);
|
||||||
|
assert.strictEqual(lines[0], "Parse error at test/input/invalid/loop-no-body.js:2,0");
|
||||||
|
assert.strictEqual(lines[1], "for (var i = 0; i < 1; i++) ");
|
||||||
|
assert.strictEqual(lines[2], " ^");
|
||||||
|
assert.strictEqual(lines[3], "SyntaxError: Unexpected token: eof (undefined)");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user