Compare commits

...

40 Commits

Author SHA1 Message Date
Mihai Bazon
642ba2e92c rename the npm package to "uglify-js" and cli tool to "uglifyjs" 2012-11-21 13:27:03 +02:00
Mihai Bazon
089ac908b7 fix #51 2012-11-18 17:37:45 +02:00
Mihai Bazon
0d3fd2ef30 retain (1,eval) as is when it's the expression of an AST_Call
otherwise we change the meaning of eval from global to lexical.
2012-11-17 12:05:31 +02:00
Richard van Velzen
e98119496a Add support for somewhat preserving line numbers.
Usage: uglifyjs2 -b "beautify=0,preserve_line=1" /path/to/js

ref #46
2012-11-14 15:30:18 +02:00
Mihai Bazon
bdfcbf496b better solution for the last test in constant switch folding 2012-11-14 12:21:43 +02:00
Mihai Bazon
dba8da4800 optimize constant switch blocks
ref. mishoo/UglifyJS#441
2012-11-14 12:06:07 +02:00
Mihai Bazon
60c0f40250 Merge branch 'optimize_concat' of https://github.com/rvanvelzen/UglifyJS2 into rvanvelzen-optimize_concat 2012-11-13 14:38:55 +02:00
Mihai Bazon
e02771a5f2 don't change order in binary expressions if both operands have side effects 2012-11-13 14:32:07 +02:00
Richard van Velzen
f96f796f71 Add simple optimization for empty-string concats.
ref. #43
2012-11-12 15:41:03 +01:00
Mihai Bazon
a9fa178f86 v2.1.11 2012-11-12 13:24:52 +02:00
Mihai Bazon
53355bdb24 fix invalid AST produced by dropping unused variable
close #44
2012-11-12 13:23:57 +02:00
Mihai Bazon
f05c99d89f Merge pull request #41 from Skalman/toString-patch
Convert x.toString() to ""+x instead of x+""
2012-11-12 00:47:56 -08:00
Dan Wolff
b49230ab8d convert x.toString() to ""+x instead of x+""
In some places this can save one byte in whitespace, e.g. after return.
Example:

function f(arg) {
        // return""+arg - no space between return and ""
        return arg.toString();
}
2012-11-11 15:53:34 +02:00
Mihai Bazon
78856a3dab declare dependency versions
close #40
2012-11-09 16:43:49 +02:00
Mihai Bazon
1e5e13ed81 AST_LabelRef no longer inherits from AST_SymbolRef 2012-11-08 15:39:14 +02:00
Mihai Bazon
64270b9778 v2.1.10 2012-11-08 12:33:27 +02:00
Mihai Bazon
e312c5c2a7 fix API breakage
close #36, #38
2012-11-08 12:31:28 +02:00
Mihai Bazon
1a5fd3e052 optimization for if/break as first statement in a loop body
for(...; x; ...) if (y) break; → for(...; x&&!y; ...);

similarly for `while` and some combinations (i.e. the `break` appears in the
`else` clause, etc.)
2012-11-08 11:43:14 +02:00
Mihai Bazon
5a7e54cf72 ignore node_modules/ 2012-11-07 15:27:12 +02:00
Mihai Bazon
39f8a62703 v2.1.9 2012-11-07 13:31:58 +02:00
Mihai Bazon
46be3f2bf1 fix another small regression
we do need parens here: `new (foo.bar().baz)`, but not here: `new foo.bar.baz`
2012-11-07 13:31:43 +02:00
Mihai Bazon
258b46f4dc v2.1.8 2012-11-07 13:03:11 +02:00
Mihai Bazon
80da21dab4 fix regression from 5346fb94 (shouldn't parenthesize i++ in x[i++]) 2012-11-07 13:02:51 +02:00
Mihai Bazon
bb0e4d7126 v2.1.7 2012-11-07 12:45:23 +02:00
Mihai Bazon
5276a4a873 add AST_Accessor and AST_SymbolAccessor node types
AST_Accessor will represent the function for a setter or getter.  Since they
are not mangleable, and they should not introduce a name in scope, we have a
new node for their name (AST_SymbolAccessor) which doesn't inherit from
AST_SymbolDeclaration.

fix #37
2012-11-07 12:43:27 +02:00
Mihai Bazon
a1ae0c8609 parenthesize property access when it's the expression in New
refs #35
2012-11-07 12:26:33 +02:00
Mihai Bazon
a90c1aeafe further fix for parens around New (refs #35) 2012-11-07 11:49:06 +02:00
Mihai Bazon
ff388a8d2d parenthesize a Call expression when its parent is New
fix #35
2012-11-07 11:36:15 +02:00
Mihai Bazon
5346fb94bb add proper parens around unary expressions
fix #34
2012-11-07 11:23:50 +02:00
Mihai Bazon
a4f6d46118 add option to mangle names even if eval/with is in use
(for more fair comparison to Closure compiler)
2012-11-06 18:19:51 +02:00
Mihai Bazon
7f5f4d60b7 discard the hack that worked around the deprecation warning
(since the source-map module no longer uses require.js)

refs #9
2012-11-05 22:23:51 +02:00
Mihai Bazon
ffccb233e5 convert while into for 2012-11-05 16:01:20 +02:00
Mihai Bazon
fba0c1aafe minor 2012-11-05 16:01:09 +02:00
Mihai Bazon
774f2ded94 minor optimization
for `==` or `!=` against a constant, prefer to display the constant first.
should help a bit after gzip, i.e.:

    typeof foo=="undefined"
    ^^^^^^    ^^^^^^^^^^^^^

vs:

    "undefined"==typeof foo
    ^^^^^^^^^^^^^^^^^^^     (longer sequence that could repeat)

idea stolen from closure.
2012-11-05 13:13:06 +02:00
Mihai Bazon
85af942d64 print final semicolon
close #28
2012-11-05 13:09:39 +02:00
Mihai Bazon
8413787efc use a Dictionary object instead of plain object for hashes
to mitigate the `__proto__` issue

related to #30
2012-11-02 10:58:45 +02:00
Mihai Bazon
dde57452aa v2.1.6 2012-11-01 16:55:10 +02:00
Mihai Bazon
cf409800be it's safe to negate expression in !EXP only in boolean context
#kendo
2012-11-01 15:49:05 +02:00
Mihai Bazon
18270dd9f3 added unsafe_comps for negating <= with >
since it has the potential to break code, let's keep it disabled by default
2012-11-01 15:14:56 +02:00
Mihai Bazon
d4c25c571b fix compressing UnaryPrefix
only try negating the expression if the operator is `!`

#kendo
2012-11-01 13:35:08 +02:00
15 changed files with 754 additions and 128 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
tmp/ tmp/
node_modules/

View File

@@ -14,7 +14,7 @@ Install
From NPM: From NPM:
npm install uglify-js2 npm install uglify-js
From Git: From Git:
@@ -25,7 +25,7 @@ From Git:
Usage Usage
----- -----
uglifyjs2 [input files] [options] uglifyjs [input files] [options]
UglifyJS2 can take multiple input files. It's recommended that you pass the UglifyJS2 can take multiple input files. It's recommended that you pass the
input files first, then pass the options. UglifyJS will parse input files input files first, then pass the options. UglifyJS will parse input files
@@ -98,7 +98,7 @@ map.
For example: For example:
uglifyjs2 /home/doe/work/foo/src/js/file1.js \ uglifyjs /home/doe/work/foo/src/js/file1.js \
/home/doe/work/foo/src/js/file2.js \ /home/doe/work/foo/src/js/file2.js \
-o foo.min.js \ -o foo.min.js \
--source-map foo.min.js.map \ --source-map foo.min.js.map \
@@ -140,7 +140,7 @@ When mangling is enabled but you want to prevent certain names from being
mangled, you can declare those names with `--reserved` (`-r`) — pass a mangled, you can declare those names with `--reserved` (`-r`) — pass a
comma-separated list of names. For example: comma-separated list of names. For example:
uglifyjs2 ... -m -r '$,require,exports' uglifyjs ... -m -r '$,require,exports'
to prevent the `require`, `exports` and `$` names from being changed. to prevent the `require`, `exports` and `$` names from being changed.
@@ -206,7 +206,7 @@ separate file and include it into the build. For example you can have a
and build your code like this: and build your code like this:
uglifyjs2 build/defines.js js/foo.js js/bar.js... -c uglifyjs build/defines.js js/foo.js js/bar.js... -c
UglifyJS will notice the constants and, since they cannot be altered, it UglifyJS will notice the constants and, since they cannot be altered, it
will evaluate references to them to the value itself and drop unreachable will evaluate references to them to the value itself and drop unreachable
@@ -288,7 +288,7 @@ SpiderMonkey AST. It has a small CLI utility that parses one file and dumps
the AST in JSON on the standard output. To use UglifyJS to mangle and the AST in JSON on the standard output. To use UglifyJS to mangle and
compress that: compress that:
acorn file.js | uglifyjs2 --spidermonkey -m -c acorn file.js | uglifyjs --spidermonkey -m -c
The `--spidermonkey` option tells UglifyJS that all input files are not The `--spidermonkey` option tells UglifyJS that all input files are not
JavaScript, but JS code described in SpiderMonkey AST in JSON. Therefore we JavaScript, but JS code described in SpiderMonkey AST in JSON. Therefore we
@@ -310,7 +310,7 @@ API Reference
Assuming installation via NPM, you can load UglifyJS in your application Assuming installation via NPM, you can load UglifyJS in your application
like this: like this:
var UglifyJS = require("uglify-js2"); var UglifyJS = require("uglify-js");
It exports a lot of names, but I'll discuss here the basics that are needed It exports a lot of names, but I'll discuss here the basics that are needed
for parsing, mangling and compressing a piece of code. The sequence is (1) for parsing, mangling and compressing a piece of code. The sequence is (1)

View File

@@ -252,7 +252,7 @@ if (SCOPE_IS_NEEDED) {
time_it("scope", function(){ time_it("scope", function(){
TOPLEVEL.figure_out_scope(); TOPLEVEL.figure_out_scope();
if (MANGLE) { if (MANGLE) {
TOPLEVEL.compute_char_frequency(); TOPLEVEL.compute_char_frequency(MANGLE);
} }
}); });
} }

View File

@@ -345,6 +345,10 @@ var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments", {
} }
}, AST_Scope); }, AST_Scope);
var AST_Accessor = DEFNODE("Accessor", null, {
$documentation: "A setter/getter function"
}, AST_Lambda);
var AST_Function = DEFNODE("Function", null, { var AST_Function = DEFNODE("Function", null, {
$documentation: "A function expression" $documentation: "A function expression"
}, AST_Lambda); }, AST_Lambda);
@@ -758,6 +762,10 @@ var AST_Symbol = DEFNODE("Symbol", "scope name thedef", {
$documentation: "Base class for all symbols", $documentation: "Base class for all symbols",
}); });
var AST_SymbolAccessor = DEFNODE("SymbolAccessor", null, {
$documentation: "The name of a property accessor (setter/getter function)"
}, AST_Symbol);
var AST_SymbolDeclaration = DEFNODE("SymbolDeclaration", "init", { var AST_SymbolDeclaration = DEFNODE("SymbolDeclaration", "init", {
$documentation: "A declaration symbol (symbol in var/const, function name or argument, symbol in catch)", $documentation: "A declaration symbol (symbol in var/const, function name or argument, symbol in catch)",
$propdoc: { $propdoc: {
@@ -802,7 +810,7 @@ var AST_SymbolRef = DEFNODE("SymbolRef", null, {
var AST_LabelRef = DEFNODE("LabelRef", null, { var AST_LabelRef = DEFNODE("LabelRef", null, {
$documentation: "Reference to a label symbol", $documentation: "Reference to a label symbol",
}, AST_SymbolRef); }, AST_Symbol);
var AST_This = DEFNODE("This", null, { var AST_This = DEFNODE("This", null, {
$documentation: "The `this` symbol", $documentation: "The `this` symbol",

View File

@@ -53,6 +53,7 @@ function Compressor(options, false_by_default) {
dead_code : !false_by_default, dead_code : !false_by_default,
drop_debugger : !false_by_default, drop_debugger : !false_by_default,
unsafe : !false_by_default, unsafe : !false_by_default,
unsafe_comps : false,
conditionals : !false_by_default, conditionals : !false_by_default,
comparisons : !false_by_default, comparisons : !false_by_default,
evaluate : !false_by_default, evaluate : !false_by_default,
@@ -553,12 +554,24 @@ merge(Compressor.prototype, {
def(AST_UnaryPrefix, function(){ def(AST_UnaryPrefix, function(){
return this.operator == "typeof"; return this.operator == "typeof";
}); });
def(AST_Binary, function(){ def(AST_Binary, function(compressor){
return this.operator == "+" && return this.operator == "+" &&
(this.left.is_string() || this.right.is_string()); (this.left.is_string(compressor) || this.right.is_string(compressor));
}); });
def(AST_Assign, function(){ def(AST_Assign, function(compressor){
return this.operator == "=" && this.right.is_string(); return (this.operator == "=" || this.operator == "+=") && this.right.is_string(compressor);
});
def(AST_Seq, function(compressor){
return this.cdr.is_string(compressor);
});
def(AST_Conditional, function(compressor){
return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);
});
def(AST_Call, function(compressor){
return compressor.option("unsafe")
&& this.expression instanceof AST_SymbolRef
&& this.expression.name == "String"
&& this.expression.undeclared();
}); });
})(function(node, func){ })(function(node, func){
node.DEFMETHOD("is_string", func); node.DEFMETHOD("is_string", func);
@@ -703,7 +716,7 @@ merge(Compressor.prototype, {
}); });
def(AST_Binary, function(compressor){ def(AST_Binary, function(compressor){
var self = this.clone(), op = this.operator; var self = this.clone(), op = this.operator;
if (compressor.option("comparisons") && compressor.option("unsafe")) { if (compressor.option("unsafe_comps")) {
switch (op) { switch (op) {
case "<=" : self.operator = ">" ; return self; case "<=" : self.operator = ">" ; return self;
case "<" : self.operator = ">=" ; return self; case "<" : self.operator = ">=" ; return self;
@@ -813,10 +826,12 @@ merge(Compressor.prototype, {
(function(def){ (function(def){
def(AST_Statement, function(){ return null }); def(AST_Statement, function(){ return null });
def(AST_Jump, function(){ return this }); def(AST_Jump, function(){ return this });
def(AST_BlockStatement, function(){ function block_aborts(){
var n = this.body.length; var n = this.body.length;
return n > 0 && aborts(this.body[n - 1]); return n > 0 && aborts(this.body[n - 1]);
}); };
def(AST_BlockStatement, block_aborts);
def(AST_SwitchBranch, block_aborts);
def(AST_If, function(){ def(AST_If, function(){
return this.alternative && aborts(this.body) && aborts(this.alternative); return this.alternative && aborts(this.body) && aborts(this.alternative);
}); });
@@ -884,7 +899,7 @@ merge(Compressor.prototype, {
}); });
return true; return true;
} }
if (node instanceof AST_SymbolRef && !(node instanceof AST_LabelRef)) { if (node instanceof AST_SymbolRef) {
push_uniq(in_use, node.definition()); push_uniq(in_use, node.definition());
return true; return true;
} }
@@ -907,8 +922,7 @@ merge(Compressor.prototype, {
if (decl instanceof AST_SymbolDeclaration) { if (decl instanceof AST_SymbolDeclaration) {
decl.init.forEach(function(init){ decl.init.forEach(function(init){
var tw = new TreeWalker(function(node){ var tw = new TreeWalker(function(node){
if (node instanceof AST_SymbolRef if (node instanceof AST_SymbolRef) {
&& !(node instanceof AST_LabelRef)) {
push_uniq(in_use, node.definition()); push_uniq(in_use, node.definition());
} }
}); });
@@ -919,7 +933,7 @@ merge(Compressor.prototype, {
} }
// pass 3: we should drop declarations not in_use // pass 3: we should drop declarations not in_use
var tt = new TreeTransformer( var tt = new TreeTransformer(
function before(node, descend) { function before(node, descend, in_list) {
if (node instanceof AST_Lambda) { if (node instanceof AST_Lambda) {
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];
@@ -1010,6 +1024,19 @@ merge(Compressor.prototype, {
} }
return node; return node;
} }
if (node instanceof AST_For && node.init instanceof AST_BlockStatement) {
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 instanceof AST_Scope && node !== self) if (node instanceof AST_Scope && node !== self)
return node; return node;
} }
@@ -1025,7 +1052,7 @@ merge(Compressor.prototype, {
if (hoist_funs || hoist_vars) { if (hoist_funs || hoist_vars) {
var dirs = []; var dirs = [];
var hoisted = []; var hoisted = [];
var vars = {}, vars_found = 0, var_decl = 0; var vars = new Dictionary(), vars_found = 0, var_decl = 0;
// let's count var_decl first, we seem to waste a lot of // let's count var_decl first, we seem to waste a lot of
// space if we hoist `var` when there's only one. // space if we hoist `var` when there's only one.
self.walk(new TreeWalker(function(node){ self.walk(new TreeWalker(function(node){
@@ -1050,7 +1077,7 @@ merge(Compressor.prototype, {
} }
if (node instanceof AST_Var && hoist_vars) { if (node instanceof AST_Var && hoist_vars) {
node.definitions.forEach(function(def){ node.definitions.forEach(function(def){
vars[def.name.name] = def; vars.set(def.name.name, def);
++vars_found; ++vars_found;
}); });
var seq = node.to_assignments(); var seq = node.to_assignments();
@@ -1074,8 +1101,8 @@ merge(Compressor.prototype, {
); );
self = self.transform(tt); self = self.transform(tt);
if (vars_found > 0) hoisted.unshift(make_node(AST_Var, self, { if (vars_found > 0) hoisted.unshift(make_node(AST_Var, self, {
definitions: Object.keys(vars).map(function(name){ definitions: vars.map(function(def){
var def = vars[name].clone(); def = def.clone();
def.value = null; def.value = null;
return def; return def;
}) })
@@ -1117,6 +1144,61 @@ merge(Compressor.prototype, {
return self; return self;
}); });
function if_break_in_loop(self, compressor) {
function drop_it(rest) {
rest = as_statement_array(rest);
if (self.body instanceof AST_BlockStatement) {
self.body = self.body.clone();
self.body.body = rest.concat(self.body.body.slice(1));
self.body = self.body.transform(compressor);
} else {
self.body = make_node(AST_BlockStatement, self.body, {
body: rest
}).transform(compressor);
}
if_break_in_loop(self, compressor);
}
var first = self.body instanceof AST_BlockStatement ? self.body.body[0] : self.body;
if (first instanceof AST_If) {
if (first.body instanceof AST_Break
&& compressor.loopcontrol_target(first.body.label) === self) {
if (self.condition) {
self.condition = make_node(AST_Binary, self.condition, {
left: self.condition,
operator: "&&",
right: first.condition.negate(compressor),
});
} else {
self.condition = first.condition.negate(compressor);
}
drop_it(first.alternative);
}
else if (first.alternative instanceof AST_Break
&& compressor.loopcontrol_target(first.alternative.label) === self) {
if (self.condition) {
self.condition = make_node(AST_Binary, self.condition, {
left: self.condition,
operator: "&&",
right: first.condition,
});
} else {
self.condition = first.condition;
}
drop_it(first.body);
}
}
};
OPT(AST_While, function(self, compressor) {
if (!compressor.option("loops")) return self;
self = AST_DWLoop.prototype.optimize.call(self, compressor);
if (self instanceof AST_While) {
if_break_in_loop(self, compressor);
self = make_node(AST_For, self, self).transform(compressor);
}
return self;
});
OPT(AST_For, function(self, compressor){ OPT(AST_For, function(self, compressor){
var cond = self.condition; var cond = self.condition;
if (cond) { if (cond) {
@@ -1141,6 +1223,7 @@ merge(Compressor.prototype, {
} }
} }
} }
if_break_in_loop(self, compressor);
return self; return self;
}); });
@@ -1279,6 +1362,79 @@ merge(Compressor.prototype, {
if (stat instanceof AST_Break && loop_body(compressor.loopcontrol_target(stat.label)) === self) if (stat instanceof AST_Break && loop_body(compressor.loopcontrol_target(stat.label)) === self)
last_branch.body.pop(); last_branch.body.pop();
} }
var exp = self.expression.evaluate(compressor);
out: if (exp.length == 2) try {
// constant expression
self.expression = exp[0];
if (!compressor.option("dead_code")) break out;
var value = exp[1];
var in_if = false;
var in_block = false;
var started = false;
var stopped = false;
var ruined = false;
var tt = new TreeTransformer(function(node, descend, in_list){
if (node instanceof AST_Lambda || node instanceof AST_SimpleStatement) {
// no need to descend these node types
return node;
}
else if (node instanceof AST_Switch && node === self) {
node = node.clone();
descend(node, this);
return ruined ? node : make_node(AST_BlockStatement, node, {
body: node.body.reduce(function(a, branch){
return a.concat(branch.body);
}, [])
}).transform(compressor);
}
else if (node instanceof AST_If || node instanceof AST_Try) {
var save = in_if;
in_if = !in_block;
descend(node, this);
in_if = save;
return node;
}
else if (node instanceof AST_StatementWithBody || node instanceof AST_Switch) {
var save = in_block;
in_block = true;
descend(node, this);
in_block = save;
return node;
}
else if (node instanceof AST_Break && this.loopcontrol_target(node.label) === self) {
if (in_if) {
ruined = true;
return node;
}
if (in_block) return node;
stopped = true;
return in_list ? MAP.skip : make_node(AST_EmptyStatement, node);
}
else if (node instanceof AST_SwitchBranch && this.parent() === self) {
if (stopped) return MAP.skip;
if (node instanceof AST_Case) {
var exp = node.expression.evaluate(compressor);
if (exp.length < 2) {
// got a case with non-constant expression, baling out
throw self;
}
if (exp[1] === value || started) {
started = true;
if (aborts(node)) stopped = true;
descend(node, this);
return node;
}
return MAP.skip;
}
descend(node, this);
return node;
}
});
tt.stack = compressor.stack; // so that's able to see parent nodes
self = self.transform(tt);
} catch(ex) {
if (ex !== self) throw ex;
}
return self; return self;
}); });
@@ -1357,9 +1513,9 @@ merge(Compressor.prototype, {
} }
else if (exp instanceof AST_Dot && exp.property == "toString" && self.args.length == 0) { else if (exp instanceof AST_Dot && exp.property == "toString" && self.args.length == 0) {
return make_node(AST_Binary, self, { return make_node(AST_Binary, self, {
left: exp.expression, left: make_node(AST_String, self, { value: "" }),
operator: "+", operator: "+",
right: make_node(AST_String, self, { value: "" }) right: exp.expression
}).transform(compressor); }).transform(compressor);
} }
} }
@@ -1393,8 +1549,19 @@ merge(Compressor.prototype, {
OPT(AST_Seq, function(self, compressor){ OPT(AST_Seq, function(self, compressor){
if (!compressor.option("side_effects")) if (!compressor.option("side_effects"))
return self; return self;
if (!self.car.has_side_effects()) if (!self.car.has_side_effects()) {
// 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)) {
return self.cdr; return self.cdr;
}
}
if (compressor.option("cascade")) { if (compressor.option("cascade")) {
if (self.car instanceof AST_Assign if (self.car instanceof AST_Assign
&& !self.car.left.has_side_effects() && !self.car.left.has_side_effects()
@@ -1445,10 +1612,10 @@ merge(Compressor.prototype, {
compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start); compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
return make_node(AST_True, self); return make_node(AST_True, self);
} }
} if (e instanceof AST_Binary && self.operator == "!") {
if (e instanceof AST_Binary) {
self = best_of(self, e.negate(compressor)); self = best_of(self, e.negate(compressor));
} }
}
return self.evaluate(compressor)[0]; return self.evaluate(compressor)[0];
}); });
@@ -1476,30 +1643,35 @@ merge(Compressor.prototype, {
return this; return this;
}); });
var commutativeOperators = makePredicate("== === != !== * & | ^");
OPT(AST_Binary, function(self, compressor){ OPT(AST_Binary, function(self, compressor){
function reverse(op) {
if (!(self.left.has_side_effects() && self.right.has_side_effects())) {
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)) {
reverse();
}
}
self = self.lift_sequences(compressor); self = self.lift_sequences(compressor);
if (compressor.option("comparisons")) switch (self.operator) { if (compressor.option("comparisons")) switch (self.operator) {
case "===": case "===":
case "!==": case "!==":
if ((self.left.is_string() && self.right.is_string()) || if ((self.left.is_string(compressor) && self.right.is_string(compressor)) ||
(self.left.is_boolean() && self.right.is_boolean())) { (self.left.is_boolean() && self.right.is_boolean())) {
self.operator = self.operator.substr(0, 2); self.operator = self.operator.substr(0, 2);
} }
// XXX: intentionally falling down to the next case // XXX: intentionally falling down to the next case
case "==": case "==":
case "!=": case "!=":
if (self.left instanceof AST_UnaryPrefix if (self.left instanceof AST_String
&& self.left.operator == "typeof"
&& self.right instanceof AST_String
&& self.right.value == "undefined") {
if (!(self.left.expression instanceof AST_SymbolRef)
|| !self.left.expression.undeclared()) {
self.left = self.left.expression;
self.right = make_node(AST_Undefined, self.right).optimize(compressor);
if (self.operator.length == 2) self.operator += "=";
}
}
else if (self.left instanceof AST_String
&& self.left.value == "undefined" && self.left.value == "undefined"
&& self.right instanceof AST_UnaryPrefix && self.right instanceof AST_UnaryPrefix
&& self.right.operator == "typeof") { && self.right.operator == "typeof") {
@@ -1565,17 +1737,16 @@ merge(Compressor.prototype, {
}); });
self = best_of(self, negated); self = best_of(self, negated);
} }
var reverse = function(op) {
self.operator = op;
var tmp = self.left;
self.left = self.right;
self.right = tmp;
};
switch (self.operator) { switch (self.operator) {
case "<": reverse(">"); break; case "<": reverse(">"); break;
case "<=": reverse(">="); break; case "<=": reverse(">="); break;
} }
} }
if (self.operator == "+" && self.right instanceof AST_String
&& self.right.getValue() === "" && self.left instanceof AST_Binary
&& self.left.operator == "+" && self.left.is_string()) {
return self.left;
}
return self; return self;
}); });

View File

@@ -59,7 +59,8 @@ function OutputStream(options) {
source_map : null, source_map : null,
bracketize : false, bracketize : false,
semicolons : true, semicolons : true,
comments : false comments : false,
preserve_line : false
}, true); }, true);
var indentation = 0; var indentation = 0;
@@ -137,7 +138,7 @@ function OutputStream(options) {
str = String(str); str = String(str);
var ch = str.charAt(0); var ch = str.charAt(0);
if (might_need_semicolon) { if (might_need_semicolon) {
if (";}".indexOf(ch) < 0 && !/[;]$/.test(last)) { if ((!ch || ";}".indexOf(ch) < 0) && !/[;]$/.test(last)) {
if (options.semicolons || requireSemicolonChars(ch)) { if (options.semicolons || requireSemicolonChars(ch)) {
OUTPUT += ";"; OUTPUT += ";";
current_col++; current_col++;
@@ -154,6 +155,18 @@ function OutputStream(options) {
might_need_semicolon = false; might_need_semicolon = false;
maybe_newline(); maybe_newline();
} }
if (!options.beautify && options.preserve_line && stack[stack.length - 1]) {
var target_line = stack[stack.length - 1].start.line;
while (current_line < target_line) {
OUTPUT += "\n";
current_pos++;
current_line++;
current_col = 0;
might_need_space = false;
}
}
if (might_need_space) { if (might_need_space) {
var prev = last_char(); var prev = last_char();
if ((is_identifier_char(prev) if ((is_identifier_char(prev)
@@ -410,6 +423,11 @@ function OutputStream(options) {
return first_in_statement(output); return first_in_statement(output);
}); });
PARENS(AST_Unary, function(output){
var p = output.parent();
return p instanceof AST_PropAccess && p.expression === this;
});
PARENS(AST_Seq, function(output){ PARENS(AST_Seq, function(output){
var p = output.parent(); var p = output.parent();
return p instanceof AST_Call // (foo, bar)() or foo(1, (2, 3), 4) return p instanceof AST_Call // (foo, bar)() or foo(1, (2, 3), 4)
@@ -463,10 +481,36 @@ function OutputStream(options) {
} }
}); });
PARENS(AST_PropAccess, function(output){
var p = output.parent();
if (p instanceof AST_New && p.expression === this) {
// i.e. new (foo.bar().baz)
//
// if there's one call into this subtree, then we need
// parens around it too, otherwise the call will be
// interpreted as passing the arguments to the upper New
// expression.
try {
this.walk(new TreeWalker(function(node){
if (node instanceof AST_Call) throw p;
}));
} catch(ex) {
if (ex !== p) throw ex;
return true;
}
}
});
PARENS(AST_Call, function(output){
var p = output.parent();
return p instanceof AST_New && p.expression === this;
});
PARENS(AST_New, function(output){ PARENS(AST_New, function(output){
var p = output.parent(); var p = output.parent();
// (new Date).getTime(); if (no_constructor_parens(this, output)
if (p instanceof AST_Dot && no_constructor_parens(this, output)) && (p instanceof AST_Dot // (new Date).getTime()
|| p instanceof AST_Call && p.expression === this)) // (new foo)(bar)
return true; return true;
}); });
@@ -529,6 +573,7 @@ function OutputStream(options) {
}); });
DEFPRINT(AST_Toplevel, function(self, output){ DEFPRINT(AST_Toplevel, function(self, output){
display_body(self.body, true, output); display_body(self.body, true, output);
output.print("");
}); });
DEFPRINT(AST_LabeledStatement, function(self, output){ DEFPRINT(AST_LabeledStatement, function(self, output){
self.label.print(output); self.label.print(output);

View File

@@ -883,6 +883,8 @@ function parse($TEXT, options) {
var function_ = function(in_statement, ctor) { var function_ = function(in_statement, ctor) {
var name = is("name") ? as_symbol(in_statement var name = is("name") ? as_symbol(in_statement
? AST_SymbolDefun ? AST_SymbolDefun
: ctor === AST_Accessor
? AST_SymbolAccessor
: AST_SymbolLambda) : null; : AST_SymbolLambda) : null;
if (in_statement && !name) if (in_statement && !name)
unexpected(); unexpected();
@@ -1158,7 +1160,7 @@ function parse($TEXT, options) {
a.push(new AST_ObjectGetter({ a.push(new AST_ObjectGetter({
start : start, start : start,
key : name, key : name,
value : function_(false, AST_Lambda), value : function_(false, AST_Accessor),
end : prev() end : prev()
})); }));
continue; continue;
@@ -1167,7 +1169,7 @@ function parse($TEXT, options) {
a.push(new AST_ObjectSetter({ a.push(new AST_ObjectSetter({
start : start, start : start,
key : name, key : name,
value : function_(false, AST_Lambda), value : function_(false, AST_Accessor),
end : prev() end : prev()
})); }));
continue; continue;

View File

@@ -43,7 +43,7 @@
"use strict"; "use strict";
function SymbolDef(scope, orig) { function SymbolDef(scope, index, orig) {
this.name = orig.name; this.name = orig.name;
this.orig = [ orig ]; this.orig = [ orig ];
this.scope = scope; this.scope = scope;
@@ -52,15 +52,18 @@ function SymbolDef(scope, orig) {
this.mangled_name = null; this.mangled_name = null;
this.undeclared = false; this.undeclared = false;
this.constant = false; this.constant = false;
this.index = index;
}; };
SymbolDef.prototype = { SymbolDef.prototype = {
unmangleable: function() { unmangleable: function(options) {
return this.global || this.undeclared || this.scope.uses_eval || this.scope.uses_with; return this.global
|| this.undeclared
|| (!(options && options.eval) && (this.scope.uses_eval || this.scope.uses_with));
}, },
mangle: function() { mangle: function(options) {
if (!this.mangled_name && !this.unmangleable()) if (!this.mangled_name && !this.unmangleable(options))
this.mangled_name = this.scope.next_mangled(); this.mangled_name = this.scope.next_mangled(options);
} }
}; };
@@ -75,14 +78,17 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
// pass 1: setup scope chaining and handle definitions // pass 1: setup scope chaining and handle definitions
var self = this; var self = this;
var scope = self.parent_scope = null; var scope = self.parent_scope = null;
var labels = Object.create(null); var labels = new Dictionary();
var nesting = 0;
var tw = new TreeWalker(function(node, descend){ var tw = new TreeWalker(function(node, descend){
if (node instanceof AST_Scope) { if (node instanceof AST_Scope) {
node.init_scope_vars(); node.init_scope_vars(nesting);
var save_scope = node.parent_scope = scope; var save_scope = node.parent_scope = scope;
++nesting;
scope = node; scope = node;
descend(); descend();
scope = save_scope; scope = save_scope;
--nesting;
return true; // don't descend again in TreeWalker return true; // don't descend again in TreeWalker
} }
if (node instanceof AST_Directive) { if (node instanceof AST_Directive) {
@@ -97,11 +103,11 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
} }
if (node instanceof AST_LabeledStatement) { if (node instanceof AST_LabeledStatement) {
var l = node.label; var l = node.label;
if (labels[l.name]) if (labels.has(l.name))
throw new Error(string_template("Label {name} defined twice", l)); throw new Error(string_template("Label {name} defined twice", l));
labels[l.name] = l; labels.set(l.name, l);
descend(); descend();
delete labels[l.name]; labels.del(l.name);
return true; // no descend again return true; // no descend again
} }
if (node instanceof AST_SymbolDeclaration) { if (node instanceof AST_SymbolDeclaration) {
@@ -151,7 +157,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
scope.def_variable(node); scope.def_variable(node);
} }
if (node instanceof AST_LabelRef) { if (node instanceof AST_LabelRef) {
var sym = labels[node.name]; var sym = labels.get(node.name);
if (!sym) throw new Error(string_template("Undefined label {name} [{line},{col}]", { if (!sym) throw new Error(string_template("Undefined label {name} [{line},{col}]", {
name: node.name, name: node.name,
line: node.start.line, line: node.start.line,
@@ -164,7 +170,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
// pass 2: find back references and eval // pass 2: find back references and eval
var func = null; var func = null;
var globals = self.globals = Object.create(null); var globals = self.globals = new Dictionary();
var tw = new TreeWalker(function(node, descend){ var tw = new TreeWalker(function(node, descend){
if (node instanceof AST_Lambda) { if (node instanceof AST_Lambda) {
var prev_func = func; var prev_func = func;
@@ -182,12 +188,12 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
var sym = node.scope.find_variable(name); var sym = node.scope.find_variable(name);
if (!sym) { if (!sym) {
var g; var g;
if (globals[name]) { if (globals.has(name)) {
g = globals[name]; g = globals.get(name);
} else { } else {
g = new SymbolDef(self, node); g = new SymbolDef(self, globals.size(), node);
g.undeclared = true; g.undeclared = true;
globals[name] = g; globals.set(name, g);
} }
node.thedef = g; node.thedef = g;
if (name == "eval" && tw.parent() instanceof AST_Call) { if (name == "eval" && tw.parent() instanceof AST_Call) {
@@ -207,15 +213,16 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
self.walk(tw); self.walk(tw);
}); });
AST_Scope.DEFMETHOD("init_scope_vars", function(){ AST_Scope.DEFMETHOD("init_scope_vars", function(nesting){
this.directives = []; // contains the directives defined in this scope, i.e. "use strict" this.directives = []; // contains the directives defined in this scope, i.e. "use strict"
this.variables = Object.create(null); // map name to AST_SymbolVar (variables defined in this scope; includes functions) this.variables = new Dictionary(); // map name to AST_SymbolVar (variables defined in this scope; includes functions)
this.functions = Object.create(null); // map name to AST_SymbolDefun (functions defined in this scope) this.functions = new Dictionary(); // map name to AST_SymbolDefun (functions defined in this scope)
this.uses_with = false; // will be set to true if this or some nested scope uses the `with` statement this.uses_with = false; // will be set to true if this or some nested scope uses the `with` statement
this.uses_eval = false; // will be set to true if this or nested scope uses the global `eval` this.uses_eval = false; // will be set to true if this or nested scope uses the global `eval`
this.parent_scope = null; // the parent scope this.parent_scope = null; // the parent scope
this.enclosed = []; // a list of variables from this or outer scope(s) that are referenced from this or inner scopes this.enclosed = []; // a list of variables from this or outer scope(s) that are referenced from this or inner scopes
this.cname = -1; // the current index for mangling functions/variables this.cname = -1; // the current index for mangling functions/variables
this.nesting = nesting; // the nesting level of this scope (0 means toplevel)
}); });
AST_Scope.DEFMETHOD("strict", function(){ AST_Scope.DEFMETHOD("strict", function(){
@@ -223,7 +230,7 @@ AST_Scope.DEFMETHOD("strict", function(){
}); });
AST_Lambda.DEFMETHOD("init_scope_vars", function(){ AST_Lambda.DEFMETHOD("init_scope_vars", function(){
AST_Scope.prototype.init_scope_vars.call(this); AST_Scope.prototype.init_scope_vars.apply(this, arguments);
this.uses_arguments = false; this.uses_arguments = false;
}); });
@@ -236,6 +243,7 @@ AST_SymbolRef.DEFMETHOD("reference", function() {
if (s === def.scope) break; if (s === def.scope) break;
s = s.parent_scope; s = s.parent_scope;
} }
this.frame = this.scope.nesting - def.scope.nesting;
}); });
AST_SymbolDeclaration.DEFMETHOD("init_scope_vars", function(){ AST_SymbolDeclaration.DEFMETHOD("init_scope_vars", function(){
@@ -252,7 +260,7 @@ AST_LabelRef.DEFMETHOD("reference", function(){
AST_Scope.DEFMETHOD("find_variable", function(name){ AST_Scope.DEFMETHOD("find_variable", function(name){
if (name instanceof AST_Symbol) name = name.name; if (name instanceof AST_Symbol) name = name.name;
return this.variables[name] return this.variables.get(name)
|| (this.parent_scope && this.parent_scope.find_variable(name)); || (this.parent_scope && this.parent_scope.find_variable(name));
}); });
@@ -262,23 +270,23 @@ AST_Scope.DEFMETHOD("has_directive", function(value){
}); });
AST_Scope.DEFMETHOD("def_function", function(symbol){ AST_Scope.DEFMETHOD("def_function", function(symbol){
this.functions[symbol.name] = this.def_variable(symbol); this.functions.set(symbol.name, this.def_variable(symbol));
}); });
AST_Scope.DEFMETHOD("def_variable", function(symbol){ AST_Scope.DEFMETHOD("def_variable", function(symbol){
var def; var def;
if (!this.variables[symbol.name]) { if (!this.variables.has(symbol.name)) {
def = new SymbolDef(this, symbol); def = new SymbolDef(this, this.variables.size(), symbol);
this.variables[symbol.name] = def; this.variables.set(symbol.name, def);
def.global = !this.parent_scope; def.global = !this.parent_scope;
} else { } else {
def = this.variables[symbol.name]; def = this.variables.get(symbol.name);
def.orig.push(symbol); def.orig.push(symbol);
} }
return symbol.thedef = def; return symbol.thedef = def;
}); });
AST_Scope.DEFMETHOD("next_mangled", function(){ AST_Scope.DEFMETHOD("next_mangled", function(options){
var ext = this.enclosed, n = ext.length; var ext = this.enclosed, n = ext.length;
out: while (true) { out: while (true) {
var m = base54(++this.cname); var m = base54(++this.cname);
@@ -288,7 +296,7 @@ AST_Scope.DEFMETHOD("next_mangled", function(){
// inner scopes. // inner scopes.
for (var i = n; --i >= 0;) { for (var i = n; --i >= 0;) {
var sym = ext[i]; var sym = ext[i];
var name = sym.mangled_name || (sym.unmangleable() && sym.name); var name = sym.mangled_name || (sym.unmangleable(options) && sym.name);
if (m == name) continue out; if (m == name) continue out;
} }
return m; return m;
@@ -300,8 +308,13 @@ AST_Scope.DEFMETHOD("references", function(sym){
return this.enclosed.indexOf(sym) < 0 ? null : sym; return this.enclosed.indexOf(sym) < 0 ? null : sym;
}); });
AST_Symbol.DEFMETHOD("unmangleable", function(){ AST_Symbol.DEFMETHOD("unmangleable", function(options){
return this.definition().unmangleable(); return this.definition().unmangleable(options);
});
// property accessors are not mangleable
AST_SymbolAccessor.DEFMETHOD("unmangleable", function(){
return true;
}); });
// labels are always mangleable // labels are always mangleable
@@ -334,10 +347,15 @@ AST_Symbol.DEFMETHOD("global", function(){
return this.definition().global; return this.definition().global;
}); });
AST_Toplevel.DEFMETHOD("mangle_names", function(options){ AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){
options = defaults(options, { return defaults(options, {
except : [] except : [],
eval : false,
}); });
});
AST_Toplevel.DEFMETHOD("mangle_names", function(options){
options = this._default_mangler_options(options);
// We only need to mangle declaration nodes. Special logic wired // We only need to mangle declaration nodes. Special logic wired
// into the code generator will display the mangled name if it's // into the code generator will display the mangled name if it's
// present (and for AST_SymbolRef-s it'll use the mangled name of // present (and for AST_SymbolRef-s it'll use the mangled name of
@@ -354,16 +372,11 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
} }
if (node instanceof AST_Scope) { if (node instanceof AST_Scope) {
var p = tw.parent(); var p = tw.parent();
var is_setget = p instanceof AST_ObjectSetter || p instanceof AST_ObjectGetter; node.variables.each(function(symbol){
var a = node.variables;
for (var i in a) {
var symbol = a[i];
if (!(is_setget && symbol instanceof AST_SymbolLambda)) {
if (options.except.indexOf(symbol.name) < 0) { if (options.except.indexOf(symbol.name) < 0) {
to_mangle.push(symbol); to_mangle.push(symbol);
} }
} });
}
return; return;
} }
if (node instanceof AST_Label) { if (node instanceof AST_Label) {
@@ -377,7 +390,8 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
to_mangle.forEach(function(def){ def.mangle(options) }); to_mangle.forEach(function(def){ def.mangle(options) });
}); });
AST_Toplevel.DEFMETHOD("compute_char_frequency", function(){ AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options){
options = this._default_mangler_options(options);
var tw = new TreeWalker(function(node){ var tw = new TreeWalker(function(node){
if (node instanceof AST_Constant) if (node instanceof AST_Constant)
base54.consider(node.print_to_string()); base54.consider(node.print_to_string());
@@ -435,7 +449,7 @@ AST_Toplevel.DEFMETHOD("compute_char_frequency", function(){
base54.consider("catch"); base54.consider("catch");
else if (node instanceof AST_Finally) else if (node instanceof AST_Finally)
base54.consider("finally"); base54.consider("finally");
else if (node instanceof AST_Symbol && node.unmangleable()) else if (node instanceof AST_Symbol && node.unmangleable(options))
base54.consider(node.name); base54.consider(node.name);
else if (node instanceof AST_Unary || node instanceof AST_Binary) else if (node instanceof AST_Unary || node instanceof AST_Binary)
base54.consider(node.operator); base54.consider(node.operator);

View File

@@ -65,7 +65,7 @@ TreeTransformer.prototype = new TreeWalker;
x = this; x = this;
descend(x, tw); descend(x, tw);
} else { } else {
tw.stack[tw.stack - 1] = x = this.clone(); tw.stack[tw.stack.length - 1] = x = this.clone();
descend(x, tw); descend(x, tw);
y = tw.after(x, in_list); y = tw.after(x, in_list);
if (y !== undefined) x = y; if (y !== undefined) x = y;

View File

@@ -244,3 +244,37 @@ function makePredicate(words) {
} }
return new Function("str", f); return new Function("str", f);
}; };
function Dictionary() {
this._values = Object.create(null);
this._size = 0;
};
Dictionary.prototype = {
set: function(key, val) {
if (!this.has(key)) ++this._size;
this._values["$" + key] = val;
return this;
},
get: function(key) { return this._values["$" + key] },
del: function(key) {
if (this.has(key)) {
--this._size;
delete this._values["$" + key];
}
return this;
},
has: function(key) { return ("$" + key) in this._values },
each: function(f) {
for (var i in this._values)
f(this._values[i], i.substr(1));
},
size: function() {
return this._size;
},
map: function(f) {
var ret = [];
for (var i in this._values)
ret.push(f(this._values[i], i.substr(1)));
return ret;
}
};

View File

@@ -1,9 +1,9 @@
{ {
"name": "uglify-js2", "name": "uglify-js",
"description": "JavaScript parser, mangler/compressor and beautifier toolkit", "description": "JavaScript parser, mangler/compressor and beautifier toolkit",
"homepage": "http://lisperator.net/uglifyjs", "homepage": "http://lisperator.net/uglifyjs",
"main": "tools/node.js", "main": "tools/node.js",
"version": "2.1.5", "version": "2.2.0",
"engines": { "node" : ">=0.4.0" }, "engines": { "node" : ">=0.4.0" },
"maintainers": [{ "maintainers": [{
"name": "Mihai Bazon", "name": "Mihai Bazon",
@@ -15,11 +15,11 @@
"url": "https://github.com/mishoo/UglifyJS2.git" "url": "https://github.com/mishoo/UglifyJS2.git"
}], }],
"dependencies": { "dependencies": {
"source-map" : "*", "source-map" : "~0.1.7",
"optimist" : "*" "optimist" : "~0.3.5"
}, },
"bin": { "bin": {
"uglifyjs2" : "bin/uglifyjs2" "uglifyjs" : "bin/uglifyjs"
}, },
"scripts": {"test": "node test/run-tests.js"} "scripts": {"test": "node test/run-tests.js"}
} }

31
test/compress/issue-44.js Normal file
View File

@@ -0,0 +1,31 @@
issue_44_valid_ast_1: {
options = { unused: true };
input: {
function a(b) {
for (var i = 0, e = b.qoo(); ; i++) {}
}
}
expect: {
function a(b) {
var i = 0;
for (b.qoo(); ; i++);
}
}
}
issue_44_valid_ast_2: {
options = { unused: true };
input: {
function a(b) {
if (foo) for (var i = 0, e = b.qoo(); ; i++) {}
}
}
expect: {
function a(b) {
if (foo) {
var i = 0;
for (b.qoo(); ; i++);
}
}
}
}

123
test/compress/loops.js Normal file
View File

@@ -0,0 +1,123 @@
while_becomes_for: {
options = { loops: true };
input: {
while (foo()) bar();
}
expect: {
for (; foo(); ) bar();
}
}
drop_if_break_1: {
options = { loops: true };
input: {
for (;;)
if (foo()) break;
}
expect: {
for (; !foo(););
}
}
drop_if_break_2: {
options = { loops: true };
input: {
for (;bar();)
if (foo()) break;
}
expect: {
for (; bar() && !foo(););
}
}
drop_if_break_3: {
options = { loops: true };
input: {
for (;bar();) {
if (foo()) break;
stuff1();
stuff2();
}
}
expect: {
for (; bar() && !foo();) {
stuff1();
stuff2();
}
}
}
drop_if_break_4: {
options = { loops: true, sequences: true };
input: {
for (;bar();) {
x();
y();
if (foo()) break;
z();
k();
}
}
expect: {
for (; bar() && (x(), y(), !foo());) z(), k();
}
}
drop_if_else_break_1: {
options = { loops: true };
input: {
for (;;) if (foo()) bar(); else break;
}
expect: {
for (; foo(); ) bar();
}
}
drop_if_else_break_2: {
options = { loops: true };
input: {
for (;bar();) {
if (foo()) baz();
else break;
}
}
expect: {
for (; bar() && foo();) baz();
}
}
drop_if_else_break_3: {
options = { loops: true };
input: {
for (;bar();) {
if (foo()) baz();
else break;
stuff1();
stuff2();
}
}
expect: {
for (; bar() && foo();) {
baz();
stuff1();
stuff2();
}
}
}
drop_if_else_break_4: {
options = { loops: true, sequences: true };
input: {
for (;bar();) {
x();
y();
if (foo()) baz();
else break;
z();
k();
}
}
expect: {
for (; bar() && (x(), y(), foo());) baz(), z(), k();
}
}

210
test/compress/switch.js Normal file
View File

@@ -0,0 +1,210 @@
constant_switch_1: {
options = { dead_code: true, evaluate: true };
input: {
switch (1+1) {
case 1: foo(); break;
case 1+1: bar(); break;
case 1+1+1: baz(); break;
}
}
expect: {
bar();
}
}
constant_switch_2: {
options = { dead_code: true, evaluate: true };
input: {
switch (1) {
case 1: foo();
case 1+1: bar(); break;
case 1+1+1: baz();
}
}
expect: {
foo();
bar();
}
}
constant_switch_3: {
options = { dead_code: true, evaluate: true };
input: {
switch (10) {
case 1: foo();
case 1+1: bar(); break;
case 1+1+1: baz();
default:
def();
}
}
expect: {
def();
}
}
constant_switch_4: {
options = { dead_code: true, evaluate: true };
input: {
switch (2) {
case 1:
x();
if (foo) break;
y();
break;
case 1+1:
bar();
default:
def();
}
}
expect: {
bar();
def();
}
}
constant_switch_5: {
options = { dead_code: true, evaluate: true };
input: {
switch (1) {
case 1:
x();
if (foo) break;
y();
break;
case 1+1:
bar();
default:
def();
}
}
expect: {
// the break inside the if ruins our job
// we can still get rid of irrelevant cases.
switch (1) {
case 1:
x();
if (foo) break;
y();
}
// XXX: we could optimize this better by inventing an outer
// labeled block, but that's kinda tricky.
}
}
constant_switch_6: {
options = { dead_code: true, evaluate: true };
input: {
OUT: {
foo();
switch (1) {
case 1:
x();
if (foo) break OUT;
y();
case 1+1:
bar();
break;
default:
def();
}
}
}
expect: {
OUT: {
foo();
x();
if (foo) break OUT;
y();
bar();
}
}
}
constant_switch_7: {
options = { dead_code: true, evaluate: true };
input: {
OUT: {
foo();
switch (1) {
case 1:
x();
if (foo) break OUT;
for (var x = 0; x < 10; x++) {
if (x > 5) break; // this break refers to the for, not to the switch; thus it
// shouldn't ruin our optimization
console.log(x);
}
y();
case 1+1:
bar();
break;
default:
def();
}
}
}
expect: {
OUT: {
foo();
x();
if (foo) break OUT;
for (var x = 0; x < 10; x++) {
if (x > 5) break;
console.log(x);
}
y();
bar();
}
}
}
constant_switch_8: {
options = { dead_code: true, evaluate: true };
input: {
OUT: switch (1) {
case 1:
x();
for (;;) break OUT;
y();
break;
case 1+1:
bar();
default:
def();
}
}
expect: {
OUT: {
x();
for (;;) break OUT;
y();
}
}
}
constant_switch_9: {
options = { dead_code: true, evaluate: true };
input: {
OUT: switch (1) {
case 1:
x();
for (;;) if (foo) break OUT;
y();
case 1+1:
bar();
default:
def();
}
}
expect: {
OUT: {
x();
for (;;) if (foo) break OUT;
y();
bar();
def();
}
}
}

View File

@@ -1,18 +1,5 @@
var path = require("path"); var path = require("path");
var fs = require("fs"); var fs = require("fs");
// Avoid NodeJS warning.
//
// There's a --no-deprecation command line argument supported by
// NodeJS, but that's tricky to use, so I'd like to set it from the
// program itself. Turns out you need to set `process.noDeprecation`,
// but by the time you can set that the `path` module is already
// loaded and `path.existsSync` is already changed to display that
// warning, therefore here's the poor solution:
if (fs.existsSync) {
path.existsSync = fs.existsSync;
}
var vm = require("vm"); var vm = require("vm");
var sys = require("util"); var sys = require("util");