Compare commits
57 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
49ea629f3f | ||
|
|
13c72a986c | ||
|
|
08af3eae44 | ||
|
|
27bdcbbd83 | ||
|
|
2c4d7d66ef | ||
|
|
d1cc5270a3 | ||
|
|
75c5b6029b | ||
|
|
fa14a9cfcd | ||
|
|
aeb9ea5ac2 | ||
|
|
798841be82 | ||
|
|
cc6eb4b15f | ||
|
|
14eee81dc6 | ||
|
|
55ebb27878 | ||
|
|
87046410ef | ||
|
|
f9b3198714 | ||
|
|
48b62393a4 | ||
|
|
a00f8dade7 | ||
|
|
9daa2fb6f5 | ||
|
|
8d81d264f4 | ||
|
|
5ef7060098 | ||
|
|
938368ba21 | ||
|
|
fe2f1965d6 | ||
|
|
30ed8f5580 | ||
|
|
dc9e7cd1fe | ||
|
|
76f40e2528 | ||
|
|
8024f7f7a8 | ||
|
|
eb7fa25270 | ||
|
|
ee7647dc67 | ||
|
|
bd2f53bc8b | ||
|
|
e8a7956b6f | ||
|
|
2b24dc25fb | ||
|
|
35cc5aa06f | ||
|
|
c1dd49e075 | ||
|
|
c76ee4b868 | ||
|
|
e23bf48052 | ||
|
|
7e0ad232b0 | ||
|
|
63adfb1590 | ||
|
|
f9806b43c3 | ||
|
|
c4c9c6d37d | ||
|
|
33f3b0c1d9 | ||
|
|
abb8ae02a5 | ||
|
|
97728c4f0b | ||
|
|
f74b7f7401 | ||
|
|
b06fd8a933 | ||
|
|
1bb0804d60 | ||
|
|
998245ffd6 | ||
|
|
7a033bb825 | ||
|
|
a441b00951 | ||
|
|
88985a46ed | ||
|
|
34ead0430b | ||
|
|
66ab2df97f | ||
|
|
b656f7c083 | ||
|
|
873db281e8 | ||
|
|
6bf1486935 | ||
|
|
ffa1943177 | ||
|
|
ac429dc8e1 | ||
|
|
3766d5c962 |
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@@ -8,6 +8,8 @@ jobs:
|
||||
os: [ ubuntu-latest, windows-latest ]
|
||||
script: [ compress, mocha, release/benchmark, release/jetstream ]
|
||||
exclude:
|
||||
- node: "0.8"
|
||||
script: release/benchmark
|
||||
- node: "0.8"
|
||||
script: release/jetstream
|
||||
name: ${{ matrix.node }} ${{ matrix.os }} ${{ matrix.script }}
|
||||
@@ -27,7 +29,7 @@ jobs:
|
||||
git clone --branch v1.5.4 --depth 1 https://github.com/jasongin/nvs.git ~/.nvs
|
||||
while ! timeout 60 bash -c '. ~/.nvs/nvs.sh add $NODE && nvs use $NODE'; do
|
||||
cd ~/.nvs
|
||||
git clean -xdf
|
||||
while !(git clean -xdf); do echo "'git clean' failed - retrying..."; done
|
||||
cd -
|
||||
done
|
||||
. ~/.nvs/nvs.sh --version
|
||||
|
||||
3
.github/workflows/ufuzz.yml
vendored
3
.github/workflows/ufuzz.yml
vendored
@@ -1,5 +1,6 @@
|
||||
name: Fuzzing
|
||||
on:
|
||||
pull_request:
|
||||
schedule:
|
||||
- cron: "*/8 * * * *"
|
||||
jobs:
|
||||
@@ -18,7 +19,7 @@ jobs:
|
||||
git clone --branch v1.5.4 --depth 1 https://github.com/jasongin/nvs.git ~/.nvs
|
||||
while ! timeout 60 bash -c '. ~/.nvs/nvs.sh add 10 && nvs use 10'; do
|
||||
cd ~/.nvs
|
||||
git clean -xdf
|
||||
while !(git clean -xdf); do echo "'git clean' failed - retrying..."; done
|
||||
cd -
|
||||
done
|
||||
. ~/.nvs/nvs.sh --version
|
||||
|
||||
10
README.md
10
README.md
@@ -4,8 +4,8 @@ UglifyJS 3
|
||||
UglifyJS is a JavaScript parser, minifier, compressor and beautifier toolkit.
|
||||
|
||||
#### Note:
|
||||
- **`uglify-js@3` has a simplified [API](#api-reference) and [CLI](#command-line-usage) that is not backwards compatible with [`uglify-js@2`](https://github.com/mishoo/UglifyJS2/tree/v2.x)**.
|
||||
- **Documentation for UglifyJS `2.x` releases can be found [here](https://github.com/mishoo/UglifyJS2/tree/v2.x)**.
|
||||
- **`uglify-js@3` has a simplified [API](#api-reference) and [CLI](#command-line-usage) that is not backwards compatible with [`uglify-js@2`](https://github.com/mishoo/UglifyJS/tree/v2.x)**.
|
||||
- **Documentation for UglifyJS `2.x` releases can be found [here](https://github.com/mishoo/UglifyJS/tree/v2.x)**.
|
||||
- `uglify-js` only supports JavaScript (ECMAScript 5).
|
||||
- To minify ECMAScript 2015 or above, transpile using tools like [Babel](https://babeljs.io/).
|
||||
|
||||
@@ -913,7 +913,7 @@ can pass additional arguments that control the code output:
|
||||
|
||||
- `wrap_iife` (default `false`) -- pass `true` to wrap immediately invoked
|
||||
function expressions. See
|
||||
[#640](https://github.com/mishoo/UglifyJS2/issues/640) for more details.
|
||||
[#640](https://github.com/mishoo/UglifyJS/issues/640) for more details.
|
||||
|
||||
# Miscellaneous
|
||||
|
||||
@@ -1072,8 +1072,8 @@ var result = UglifyJS.minify(ast, {
|
||||
### Working with Uglify AST
|
||||
|
||||
Transversal and transformation of the native AST can be performed through
|
||||
[`TreeWalker`](https://github.com/mishoo/UglifyJS2/blob/master/lib/ast.js) and
|
||||
[`TreeTransformer`](https://github.com/mishoo/UglifyJS2/blob/master/lib/transform.js)
|
||||
[`TreeWalker`](https://github.com/mishoo/UglifyJS/blob/master/lib/ast.js) and
|
||||
[`TreeTransformer`](https://github.com/mishoo/UglifyJS/blob/master/lib/transform.js)
|
||||
respectively.
|
||||
|
||||
### ESTree / SpiderMonkey AST
|
||||
|
||||
@@ -68,6 +68,7 @@ program.option("--self", "Build UglifyJS as a library (implies --wrap UglifyJS)"
|
||||
program.option("--source-map [options]", "Enable source map/specify source map options.", parse_js());
|
||||
program.option("--timings", "Display operations run time on STDERR.");
|
||||
program.option("--toplevel", "Compress and/or mangle variables in toplevel scope.");
|
||||
program.option("--validate", "Perform validation during AST manipulations.");
|
||||
program.option("--verbose", "Print diagnostic messages.");
|
||||
program.option("--warn", "Print warning messages.");
|
||||
program.option("--wrap <name>", "Embed everything as a function with “exports” corresponding to “name” globally.");
|
||||
@@ -91,6 +92,7 @@ if (!program.output && program.sourceMap && program.sourceMap.url != "inline") {
|
||||
"mangle",
|
||||
"sourceMap",
|
||||
"toplevel",
|
||||
"validate",
|
||||
"wrap"
|
||||
].forEach(function(name) {
|
||||
if (name in program) {
|
||||
|
||||
585
lib/ast.js
585
lib/ast.js
@@ -1,7 +1,7 @@
|
||||
/***********************************************************************
|
||||
|
||||
A JavaScript tokenizer / parser / beautifier / compressor.
|
||||
https://github.com/mishoo/UglifyJS2
|
||||
https://github.com/mishoo/UglifyJS
|
||||
|
||||
-------------------------------- (C) ---------------------------------
|
||||
|
||||
@@ -110,12 +110,16 @@ var AST_Node = DEFNODE("Node", "start end", {
|
||||
start: "[AST_Token] The first token of this node",
|
||||
end: "[AST_Token] The last token of this node"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this);
|
||||
},
|
||||
walk: function(visitor) {
|
||||
return this._walk(visitor); // not sure the indirection will be any help
|
||||
}
|
||||
visitor.visit(this);
|
||||
},
|
||||
_validate: noop,
|
||||
validate: function() {
|
||||
var ctor = this.CTOR;
|
||||
do {
|
||||
ctor.prototype._validate.call(this);
|
||||
} while (ctor = ctor.BASE);
|
||||
},
|
||||
}, null);
|
||||
|
||||
(AST_Node.log_function = function(fn, verbose) {
|
||||
@@ -138,6 +142,32 @@ var AST_Node = DEFNODE("Node", "start end", {
|
||||
}
|
||||
})();
|
||||
|
||||
var restore_transforms = [];
|
||||
AST_Node.enable_validation = function() {
|
||||
AST_Node.disable_validation();
|
||||
(function validate_transform(ctor) {
|
||||
var transform = ctor.prototype.transform;
|
||||
ctor.prototype.transform = function(tw, in_list) {
|
||||
var node = transform.call(this, tw, in_list);
|
||||
if (node instanceof AST_Node) {
|
||||
node.validate();
|
||||
} else if (!(node === null || in_list && List.is_op(node))) {
|
||||
throw new Error("invalid transformed value: " + node);
|
||||
}
|
||||
return node;
|
||||
};
|
||||
restore_transforms.push(function() {
|
||||
ctor.prototype.transform = transform;
|
||||
});
|
||||
ctor.SUBCLASSES.forEach(validate_transform);
|
||||
})(this);
|
||||
};
|
||||
|
||||
AST_Node.disable_validation = function() {
|
||||
var restore;
|
||||
while (restore = restore_transforms.pop()) restore();
|
||||
};
|
||||
|
||||
/* -----[ statements ]----- */
|
||||
|
||||
var AST_Statement = DEFNODE("Statement", null, {
|
||||
@@ -154,23 +184,37 @@ var AST_Directive = DEFNODE("Directive", "value quote", {
|
||||
value: "[string] The value of this directive as a plain string (it's not an AST_String!)",
|
||||
quote: "[string] the original quote character"
|
||||
},
|
||||
_validate: function() {
|
||||
if (typeof this.value != "string") throw new Error("value must be string");
|
||||
},
|
||||
}, AST_Statement);
|
||||
|
||||
function must_be_expression(node, prop) {
|
||||
if (!(node[prop] instanceof AST_Node)) throw new Error(prop + " must be AST_Node");
|
||||
if (node[prop] instanceof AST_Statement && !(node[prop] instanceof AST_Function)) {
|
||||
throw new Error(prop + " cannot be AST_Statement");
|
||||
}
|
||||
}
|
||||
|
||||
var AST_SimpleStatement = DEFNODE("SimpleStatement", "body", {
|
||||
$documentation: "A statement consisting of an expression, i.e. a = 1 + 2",
|
||||
$propdoc: {
|
||||
body: "[AST_Node] an expression node (should not be instanceof AST_Statement)"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.body._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.body.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "body");
|
||||
},
|
||||
}, AST_Statement);
|
||||
|
||||
function walk_body(node, visitor) {
|
||||
node.body.forEach(function(node) {
|
||||
node._walk(visitor);
|
||||
node.walk(visitor);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -179,11 +223,18 @@ var AST_Block = DEFNODE("Block", "body", {
|
||||
$propdoc: {
|
||||
body: "[AST_Statement*] an array of statements"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
walk_body(this, visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
walk_body(node, visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
this.body.forEach(function(node) {
|
||||
if (!(node instanceof AST_Statement)) throw new Error("body must be AST_Statement[]");
|
||||
if (node instanceof AST_Function) throw new Error("body cannot contain AST_Function");
|
||||
});
|
||||
},
|
||||
}, AST_Statement);
|
||||
|
||||
var AST_BlockStatement = DEFNODE("BlockStatement", null, {
|
||||
@@ -198,7 +249,11 @@ var AST_StatementWithBody = DEFNODE("StatementWithBody", "body", {
|
||||
$documentation: "Base class for all statements that contain one nested body: `For`, `ForIn`, `Do`, `While`, `With`",
|
||||
$propdoc: {
|
||||
body: "[AST_Statement] the body; this should always be present, even if it's an AST_EmptyStatement"
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (!(this.body instanceof AST_Statement)) throw new Error("body must be AST_Statement");
|
||||
if (this.body instanceof AST_Function) throw new Error("body cannot be AST_Function");
|
||||
},
|
||||
}, AST_Statement);
|
||||
|
||||
var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
|
||||
@@ -206,10 +261,11 @@ var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
|
||||
$propdoc: {
|
||||
label: "[AST_Label] a label definition"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.label._walk(visitor);
|
||||
this.body._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.label.walk(visitor);
|
||||
node.body.walk(visitor);
|
||||
});
|
||||
},
|
||||
clone: function(deep) {
|
||||
@@ -225,7 +281,10 @@ var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
|
||||
}));
|
||||
}
|
||||
return node;
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (!(this.label instanceof AST_Label)) throw new Error("label must be AST_Label");
|
||||
},
|
||||
}, AST_StatementWithBody);
|
||||
|
||||
var AST_IterationStatement = DEFNODE("IterationStatement", null, {
|
||||
@@ -236,25 +295,30 @@ var AST_DWLoop = DEFNODE("DWLoop", "condition", {
|
||||
$documentation: "Base class for do/while statements",
|
||||
$propdoc: {
|
||||
condition: "[AST_Node] the loop condition. Should not be instanceof AST_Statement"
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "condition");
|
||||
},
|
||||
}, AST_IterationStatement);
|
||||
|
||||
var AST_Do = DEFNODE("Do", null, {
|
||||
$documentation: "A `do` statement",
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.body._walk(visitor);
|
||||
this.condition._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.body.walk(visitor);
|
||||
node.condition.walk(visitor);
|
||||
});
|
||||
}
|
||||
}, AST_DWLoop);
|
||||
|
||||
var AST_While = DEFNODE("While", null, {
|
||||
$documentation: "A `while` statement",
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.condition._walk(visitor);
|
||||
this.body._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.condition.walk(visitor);
|
||||
node.body.walk(visitor);
|
||||
});
|
||||
}
|
||||
}, AST_DWLoop);
|
||||
@@ -266,14 +330,26 @@ var AST_For = DEFNODE("For", "init condition step", {
|
||||
condition: "[AST_Node?] the `for` termination clause, or null if empty",
|
||||
step: "[AST_Node?] the `for` update clause, or null if empty"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
if (this.init) this.init._walk(visitor);
|
||||
if (this.condition) this.condition._walk(visitor);
|
||||
if (this.step) this.step._walk(visitor);
|
||||
this.body._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
if (node.init) node.init.walk(visitor);
|
||||
if (node.condition) node.condition.walk(visitor);
|
||||
if (node.step) node.step.walk(visitor);
|
||||
node.body.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (this.init != null) {
|
||||
if (!(this.init instanceof AST_Node)) throw new Error("init must be AST_Node");
|
||||
if (this.init instanceof AST_Statement
|
||||
&& !(this.init instanceof AST_Definitions || this.init instanceof AST_Function)) {
|
||||
throw new Error("init cannot be AST_Statement");
|
||||
}
|
||||
}
|
||||
if (this.condition != null) must_be_expression(this, "condition");
|
||||
if (this.step != null) must_be_expression(this, "step");
|
||||
},
|
||||
}, AST_IterationStatement);
|
||||
|
||||
var AST_ForIn = DEFNODE("ForIn", "init object", {
|
||||
@@ -282,13 +358,22 @@ var AST_ForIn = DEFNODE("ForIn", "init object", {
|
||||
init: "[AST_Node] the `for/in` initialization code",
|
||||
object: "[AST_Node] the object that we're looping through"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.init._walk(visitor);
|
||||
this.object._walk(visitor);
|
||||
this.body._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.init.walk(visitor);
|
||||
node.object.walk(visitor);
|
||||
node.body.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (this.init instanceof AST_Definitions) {
|
||||
if (this.init.definitions.length != 1) throw new Error("init must have single declaration");
|
||||
} else if (!(this.init instanceof AST_PropAccess || this.init instanceof AST_SymbolRef)) {
|
||||
throw new Error("init must be assignable");
|
||||
}
|
||||
must_be_expression(this, "object");
|
||||
},
|
||||
}, AST_IterationStatement);
|
||||
|
||||
var AST_With = DEFNODE("With", "expression", {
|
||||
@@ -296,12 +381,16 @@ var AST_With = DEFNODE("With", "expression", {
|
||||
$propdoc: {
|
||||
expression: "[AST_Node] the `with` expression"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expression._walk(visitor);
|
||||
this.body._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expression.walk(visitor);
|
||||
node.body.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "expression");
|
||||
},
|
||||
}, AST_StatementWithBody);
|
||||
|
||||
/* -----[ scope and functions ]----- */
|
||||
@@ -326,7 +415,12 @@ var AST_Scope = DEFNODE("Scope", "variables functions uses_with uses_eval parent
|
||||
},
|
||||
pinned: function() {
|
||||
return this.uses_eval || this.uses_with;
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (this.parent_scope != null) {
|
||||
if (!(this.parent_scope instanceof AST_Scope)) throw new Error("parent_scope must be AST_Scope");
|
||||
}
|
||||
},
|
||||
}, AST_Block);
|
||||
|
||||
var AST_Toplevel = DEFNODE("Toplevel", "globals", {
|
||||
@@ -380,27 +474,44 @@ var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments length_read", {
|
||||
argnames: "[AST_SymbolFunarg*] array of function arguments",
|
||||
uses_arguments: "[boolean/S] tells whether this function accesses the arguments array"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
if (this.name) this.name._walk(visitor);
|
||||
this.argnames.forEach(function(argname) {
|
||||
argname._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
if (node.name) node.name.walk(visitor);
|
||||
node.argnames.forEach(function(argname) {
|
||||
argname.walk(visitor);
|
||||
});
|
||||
walk_body(this, visitor);
|
||||
walk_body(node, visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
this.argnames.forEach(function(node) {
|
||||
if (!(node instanceof AST_SymbolFunarg)) throw new Error("argnames must be AST_SymbolFunarg[]");
|
||||
});
|
||||
},
|
||||
}, AST_Scope);
|
||||
|
||||
var AST_Accessor = DEFNODE("Accessor", null, {
|
||||
$documentation: "A setter/getter function. The `name` property is always null."
|
||||
$documentation: "A setter/getter function. The `name` property is always null.",
|
||||
_validate: function() {
|
||||
if (this.name != null) throw new Error("name must be null");
|
||||
},
|
||||
}, AST_Lambda);
|
||||
|
||||
var AST_Function = DEFNODE("Function", "inlined", {
|
||||
$documentation: "A function expression"
|
||||
$documentation: "A function expression",
|
||||
_validate: function() {
|
||||
if (this.name != null) {
|
||||
if (!(this.name instanceof AST_SymbolLambda)) throw new Error("name must be AST_SymbolLambda");
|
||||
}
|
||||
},
|
||||
}, AST_Lambda);
|
||||
|
||||
var AST_Defun = DEFNODE("Defun", "inlined", {
|
||||
$documentation: "A function definition"
|
||||
$documentation: "A function definition",
|
||||
_validate: function() {
|
||||
if (!(this.name instanceof AST_SymbolDefun)) throw new Error("name must be AST_SymbolDefun");
|
||||
},
|
||||
}, AST_Lambda);
|
||||
|
||||
/* -----[ JUMPS ]----- */
|
||||
@@ -414,19 +525,26 @@ var AST_Exit = DEFNODE("Exit", "value", {
|
||||
$propdoc: {
|
||||
value: "[AST_Node?] the value returned or thrown by this statement; could be null for AST_Return"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, this.value && function() {
|
||||
this.value._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
if (node.value) node.value.walk(visitor);
|
||||
});
|
||||
}
|
||||
}, AST_Jump);
|
||||
|
||||
var AST_Return = DEFNODE("Return", null, {
|
||||
$documentation: "A `return` statement"
|
||||
$documentation: "A `return` statement",
|
||||
_validate: function() {
|
||||
if (this.value != null) must_be_expression(this, "value");
|
||||
},
|
||||
}, AST_Exit);
|
||||
|
||||
var AST_Throw = DEFNODE("Throw", null, {
|
||||
$documentation: "A `throw` statement"
|
||||
$documentation: "A `throw` statement",
|
||||
_validate: function() {
|
||||
must_be_expression(this, "value");
|
||||
},
|
||||
}, AST_Exit);
|
||||
|
||||
var AST_LoopControl = DEFNODE("LoopControl", "label", {
|
||||
@@ -434,11 +552,17 @@ var AST_LoopControl = DEFNODE("LoopControl", "label", {
|
||||
$propdoc: {
|
||||
label: "[AST_LabelRef?] the label, or null if none",
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, this.label && function() {
|
||||
this.label._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
if (node.label) node.label.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (this.label != null) {
|
||||
if (!(this.label instanceof AST_LabelRef)) throw new Error("label must be AST_LabelRef");
|
||||
}
|
||||
},
|
||||
}, AST_Jump);
|
||||
|
||||
var AST_Break = DEFNODE("Break", null, {
|
||||
@@ -457,13 +581,21 @@ var AST_If = DEFNODE("If", "condition alternative", {
|
||||
condition: "[AST_Node] the `if` condition",
|
||||
alternative: "[AST_Statement?] the `else` part, or null if not present"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.condition._walk(visitor);
|
||||
this.body._walk(visitor);
|
||||
if (this.alternative) this.alternative._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.condition.walk(visitor);
|
||||
node.body.walk(visitor);
|
||||
if (node.alternative) node.alternative.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "condition");
|
||||
if (this.alternative != null) {
|
||||
if (!(this.alternative instanceof AST_Statement)) throw new Error("alternative must be AST_Statement");
|
||||
if (this.alternative instanceof AST_Function) throw new error("alternative cannot be AST_Function");
|
||||
}
|
||||
},
|
||||
}, AST_StatementWithBody);
|
||||
|
||||
/* -----[ SWITCH ]----- */
|
||||
@@ -473,12 +605,16 @@ var AST_Switch = DEFNODE("Switch", "expression", {
|
||||
$propdoc: {
|
||||
expression: "[AST_Node] the `switch` “discriminant”"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expression._walk(visitor);
|
||||
walk_body(this, visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expression.walk(visitor);
|
||||
walk_body(node, visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "expression");
|
||||
},
|
||||
}, AST_Block);
|
||||
|
||||
var AST_SwitchBranch = DEFNODE("SwitchBranch", null, {
|
||||
@@ -494,12 +630,16 @@ var AST_Case = DEFNODE("Case", "expression", {
|
||||
$propdoc: {
|
||||
expression: "[AST_Node] the `case` expression"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expression._walk(visitor);
|
||||
walk_body(this, visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expression.walk(visitor);
|
||||
walk_body(node, visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "expression");
|
||||
},
|
||||
}, AST_SwitchBranch);
|
||||
|
||||
/* -----[ EXCEPTIONS ]----- */
|
||||
@@ -510,13 +650,22 @@ var AST_Try = DEFNODE("Try", "bcatch bfinally", {
|
||||
bcatch: "[AST_Catch?] the catch block, or null if not present",
|
||||
bfinally: "[AST_Finally?] the finally block, or null if not present"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
walk_body(this, visitor);
|
||||
if (this.bcatch) this.bcatch._walk(visitor);
|
||||
if (this.bfinally) this.bfinally._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
walk_body(node, visitor);
|
||||
if (node.bcatch) node.bcatch.walk(visitor);
|
||||
if (node.bfinally) node.bfinally.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (this.bcatch != null) {
|
||||
if (!(this.bcatch instanceof AST_Catch)) throw new Error("bcatch must be AST_Catch");
|
||||
}
|
||||
if (this.bfinally != null) {
|
||||
if (!(this.bfinally instanceof AST_Finally)) throw new Error("bfinally must be AST_Finally");
|
||||
}
|
||||
},
|
||||
}, AST_Block);
|
||||
|
||||
var AST_Catch = DEFNODE("Catch", "argname", {
|
||||
@@ -524,12 +673,16 @@ var AST_Catch = DEFNODE("Catch", "argname", {
|
||||
$propdoc: {
|
||||
argname: "[AST_SymbolCatch] symbol for the exception"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.argname._walk(visitor);
|
||||
walk_body(this, visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.argname.walk(visitor);
|
||||
walk_body(node, visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (!(this.argname instanceof AST_SymbolCatch)) throw new Error("argname must be AST_SymbolCatch");
|
||||
},
|
||||
}, AST_Block);
|
||||
|
||||
var AST_Finally = DEFNODE("Finally", null, {
|
||||
@@ -543,17 +696,23 @@ var AST_Definitions = DEFNODE("Definitions", "definitions", {
|
||||
$propdoc: {
|
||||
definitions: "[AST_VarDef*] array of variable definitions"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.definitions.forEach(function(defn) {
|
||||
defn._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.definitions.forEach(function(defn) {
|
||||
defn.walk(visitor);
|
||||
});
|
||||
});
|
||||
}
|
||||
}, AST_Statement);
|
||||
|
||||
var AST_Var = DEFNODE("Var", null, {
|
||||
$documentation: "A `var` statement"
|
||||
$documentation: "A `var` statement",
|
||||
_validate: function() {
|
||||
this.definitions.forEach(function(node) {
|
||||
if (!(node instanceof AST_VarDef)) throw new Error("definitions must be AST_VarDef[]");
|
||||
});
|
||||
},
|
||||
}, AST_Definitions);
|
||||
|
||||
var AST_VarDef = DEFNODE("VarDef", "name value", {
|
||||
@@ -562,30 +721,49 @@ var AST_VarDef = DEFNODE("VarDef", "name value", {
|
||||
name: "[AST_SymbolVar] name of the variable",
|
||||
value: "[AST_Node?] initializer, or null of there's no initializer"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.name._walk(visitor);
|
||||
if (this.value) this.value._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.name.walk(visitor);
|
||||
if (node.value) node.value.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (!(this.name instanceof AST_SymbolVar)) throw new Error("name must be AST_SymbolVar");
|
||||
if (this.value != null) must_be_expression(this, "value");
|
||||
},
|
||||
});
|
||||
|
||||
/* -----[ OTHER ]----- */
|
||||
|
||||
var AST_Call = DEFNODE("Call", "expression args", {
|
||||
function must_be_expressions(node, prop) {
|
||||
node[prop].forEach(function(node) {
|
||||
if (!(node instanceof AST_Node)) throw new Error(prop + " must be AST_Node[]");
|
||||
if (node instanceof AST_Statement && !(node instanceof AST_Function)) {
|
||||
throw new Error(prop + " cannot contain AST_Statement");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var AST_Call = DEFNODE("Call", "expression args pure", {
|
||||
$documentation: "A function call expression",
|
||||
$propdoc: {
|
||||
expression: "[AST_Node] expression to invoke as function",
|
||||
args: "[AST_Node*] array of arguments"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expression._walk(visitor);
|
||||
this.args.forEach(function(node) {
|
||||
node._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expression.walk(visitor);
|
||||
node.args.forEach(function(arg) {
|
||||
arg.walk(visitor);
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "expression");
|
||||
must_be_expressions(this, "args");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_New = DEFNODE("New", null, {
|
||||
@@ -597,13 +775,18 @@ var AST_Sequence = DEFNODE("Sequence", "expressions", {
|
||||
$propdoc: {
|
||||
expressions: "[AST_Node*] array of expressions (at least two)"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expressions.forEach(function(node) {
|
||||
node._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expressions.forEach(function(expr) {
|
||||
expr.walk(visitor);
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (this.expressions.length < 2) throw new Error("expressions must contain multiple elements");
|
||||
must_be_expressions(this, "expressions");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
|
||||
@@ -623,26 +806,37 @@ var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
|
||||
return;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "expression");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_Dot = DEFNODE("Dot", null, {
|
||||
$documentation: "A dotted property access expression",
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expression._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expression.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (typeof this.property != "string") throw new Error("property must be string");
|
||||
},
|
||||
}, AST_PropAccess);
|
||||
|
||||
var AST_Sub = DEFNODE("Sub", null, {
|
||||
$documentation: "Index-style property access, i.e. `a[\"foo\"]`",
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expression._walk(visitor);
|
||||
this.property._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expression.walk(visitor);
|
||||
node.property.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "property");
|
||||
},
|
||||
}, AST_PropAccess);
|
||||
|
||||
var AST_Unary = DEFNODE("Unary", "operator expression", {
|
||||
@@ -651,11 +845,16 @@ var AST_Unary = DEFNODE("Unary", "operator expression", {
|
||||
operator: "[string] the operator",
|
||||
expression: "[AST_Node] expression that this unary operator applies to"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expression._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expression.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (typeof this.operator != "string") throw new Error("operator must be string");
|
||||
must_be_expression(this, "expression");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_UnaryPrefix = DEFNODE("UnaryPrefix", null, {
|
||||
@@ -673,12 +872,18 @@ var AST_Binary = DEFNODE("Binary", "operator left right", {
|
||||
operator: "[string] the operator",
|
||||
right: "[AST_Node] right-hand side expression"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.left._walk(visitor);
|
||||
this.right._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.left.walk(visitor);
|
||||
node.right.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "left");
|
||||
if (typeof this.operator != "string") throw new Error("operator must be string");
|
||||
must_be_expression(this, "right");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_Conditional = DEFNODE("Conditional", "condition consequent alternative", {
|
||||
@@ -688,17 +893,26 @@ var AST_Conditional = DEFNODE("Conditional", "condition consequent alternative",
|
||||
consequent: "[AST_Node]",
|
||||
alternative: "[AST_Node]"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.condition._walk(visitor);
|
||||
this.consequent._walk(visitor);
|
||||
this.alternative._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.condition.walk(visitor);
|
||||
node.consequent.walk(visitor);
|
||||
node.alternative.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "condition");
|
||||
must_be_expression(this, "consequent");
|
||||
must_be_expression(this, "alternative");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_Assign = DEFNODE("Assign", null, {
|
||||
$documentation: "An assignment expression — `a = b + 5`",
|
||||
_validate: function() {
|
||||
if (this.operator.indexOf("=") < 0) throw new Error('operator must contain "="');
|
||||
},
|
||||
}, AST_Binary);
|
||||
|
||||
/* -----[ LITERALS ]----- */
|
||||
@@ -708,13 +922,17 @@ var AST_Array = DEFNODE("Array", "elements", {
|
||||
$propdoc: {
|
||||
elements: "[AST_Node*] array of elements"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.elements.forEach(function(element) {
|
||||
element._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.elements.forEach(function(element) {
|
||||
element.walk(visitor);
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expressions(this, "elements");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_Object = DEFNODE("Object", "properties", {
|
||||
@@ -722,13 +940,19 @@ var AST_Object = DEFNODE("Object", "properties", {
|
||||
$propdoc: {
|
||||
properties: "[AST_ObjectProperty*] array of properties"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.properties.forEach(function(prop) {
|
||||
prop._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.properties.forEach(function(prop) {
|
||||
prop.walk(visitor);
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
this.properties.forEach(function(node) {
|
||||
if (!(node instanceof AST_ObjectProperty)) throw new Error("properties must be AST_ObjectProperty[]");
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", {
|
||||
@@ -737,9 +961,10 @@ var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", {
|
||||
key: "[string|AST_SymbolAccessor] property name. For ObjectKeyVal this is a string. For getters and setters this is an AST_SymbolAccessor.",
|
||||
value: "[AST_Node] property value. For getters and setters this is an AST_Accessor."
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.value._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.value.walk(visitor);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -748,15 +973,27 @@ var AST_ObjectKeyVal = DEFNODE("ObjectKeyVal", "quote", {
|
||||
$documentation: "A key: value object property",
|
||||
$propdoc: {
|
||||
quote: "[string] the original quote character"
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (typeof this.key != "string") throw new Error("key must be string");
|
||||
must_be_expression(this, "value");
|
||||
},
|
||||
}, AST_ObjectProperty);
|
||||
|
||||
var AST_ObjectSetter = DEFNODE("ObjectSetter", null, {
|
||||
$documentation: "An object setter property",
|
||||
_validate: function() {
|
||||
if (!(this.key instanceof AST_SymbolAccessor)) throw new Error("key must be AST_SymbolAccessor");
|
||||
if (!(this.value instanceof AST_Accessor)) throw new Error("value must be AST_Accessor");
|
||||
},
|
||||
}, AST_ObjectProperty);
|
||||
|
||||
var AST_ObjectGetter = DEFNODE("ObjectGetter", null, {
|
||||
$documentation: "An object getter property",
|
||||
_validate: function() {
|
||||
if (!(this.key instanceof AST_SymbolAccessor)) throw new Error("key must be AST_SymbolAccessor");
|
||||
if (!(this.value instanceof AST_Accessor)) throw new Error("value must be AST_Accessor");
|
||||
},
|
||||
}, AST_ObjectProperty);
|
||||
|
||||
var AST_Symbol = DEFNODE("Symbol", "scope name thedef", {
|
||||
@@ -766,6 +1003,9 @@ var AST_Symbol = DEFNODE("Symbol", "scope name thedef", {
|
||||
thedef: "[SymbolDef/S] the definition of this symbol"
|
||||
},
|
||||
$documentation: "Base class for all symbols",
|
||||
_validate: function() {
|
||||
if (typeof this.name != "string") throw new Error("name must be string");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_SymbolAccessor = DEFNODE("SymbolAccessor", null, {
|
||||
@@ -817,6 +1057,9 @@ var AST_LabelRef = DEFNODE("LabelRef", null, {
|
||||
|
||||
var AST_This = DEFNODE("This", null, {
|
||||
$documentation: "The `this` symbol",
|
||||
_validate: function() {
|
||||
if (this.name !== "this") throw new Error('name must be "this"');
|
||||
},
|
||||
}, AST_Symbol);
|
||||
|
||||
var AST_Constant = DEFNODE("Constant", null, {
|
||||
@@ -828,21 +1071,30 @@ var AST_String = DEFNODE("String", "value quote", {
|
||||
$propdoc: {
|
||||
value: "[string] the contents of this string",
|
||||
quote: "[string] the original quote character"
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (typeof this.value != "string") throw new Error("value must be string");
|
||||
},
|
||||
}, AST_Constant);
|
||||
|
||||
var AST_Number = DEFNODE("Number", "value", {
|
||||
$documentation: "A number literal",
|
||||
$propdoc: {
|
||||
value: "[number] the numeric value",
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (typeof this.value != "number") throw new Error("value must be number");
|
||||
},
|
||||
}, AST_Constant);
|
||||
|
||||
var AST_RegExp = DEFNODE("RegExp", "value", {
|
||||
$documentation: "A regexp literal",
|
||||
$propdoc: {
|
||||
value: "[RegExp] the actual regexp"
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (!(this.value instanceof RegExp)) throw new Error("value must be RegExp");
|
||||
},
|
||||
}, AST_Constant);
|
||||
|
||||
var AST_Atom = DEFNODE("Atom", null, {
|
||||
@@ -891,21 +1143,16 @@ var AST_True = DEFNODE("True", null, {
|
||||
/* -----[ TreeWalker ]----- */
|
||||
|
||||
function TreeWalker(callback) {
|
||||
this.visit = callback;
|
||||
this.stack = [];
|
||||
this.callback = callback;
|
||||
this.directives = Object.create(null);
|
||||
this.stack = [];
|
||||
}
|
||||
TreeWalker.prototype = {
|
||||
_visit: function(node, descend) {
|
||||
visit: function(node, descend) {
|
||||
this.push(node);
|
||||
var ret = this.visit(node, descend ? function() {
|
||||
descend.call(node);
|
||||
} : noop);
|
||||
if (!ret && descend) {
|
||||
descend.call(node);
|
||||
}
|
||||
var done = this.callback(node, descend || noop);
|
||||
if (!done && descend) descend();
|
||||
this.pop();
|
||||
return ret;
|
||||
},
|
||||
parent: function(n) {
|
||||
return this.stack[this.stack.length - 2 - (n || 0)];
|
||||
|
||||
444
lib/compress.js
444
lib/compress.js
@@ -1,7 +1,7 @@
|
||||
/***********************************************************************
|
||||
|
||||
A JavaScript tokenizer / parser / beautifier / compressor.
|
||||
https://github.com/mishoo/UglifyJS2
|
||||
https://github.com/mishoo/UglifyJS
|
||||
|
||||
-------------------------------- (C) ---------------------------------
|
||||
|
||||
@@ -212,7 +212,7 @@ merge(Compressor.prototype, {
|
||||
node.hoist_declarations(this);
|
||||
node.process_boolean_returns(this);
|
||||
}
|
||||
// Before https://github.com/mishoo/UglifyJS2/pull/1602 AST_Node.optimize()
|
||||
// Before https://github.com/mishoo/UglifyJS/pull/1602 AST_Node.optimize()
|
||||
// would call AST_Node.transform() if a different instance of AST_Node is
|
||||
// produced after OPT().
|
||||
// This corrupts TreeWalker.stack, which cause AST look-ups to malfunction.
|
||||
@@ -358,7 +358,6 @@ merge(Compressor.prototype, {
|
||||
function reset_def(tw, compressor, def) {
|
||||
def.assignments = 0;
|
||||
def.bool_fn = 0;
|
||||
def.chained = false;
|
||||
def.cross_loop = false;
|
||||
def.direct_access = false;
|
||||
def.escaped = [];
|
||||
@@ -400,6 +399,10 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
});
|
||||
};
|
||||
if (compressor.option("ie8")) scope.variables.each(function(def) {
|
||||
var d = def.orig[0].definition();
|
||||
if (d !== def) d.fixed = false;
|
||||
});
|
||||
}
|
||||
|
||||
function mark_defun(tw, def) {
|
||||
@@ -447,6 +450,20 @@ merge(Compressor.prototype, {
|
||||
tw.safe_ids[def.id] = safe;
|
||||
}
|
||||
|
||||
function add_assign(tw, def, node) {
|
||||
if (def.fixed === false) return;
|
||||
tw.assigns.add(def.id, node);
|
||||
}
|
||||
|
||||
function set_assign(tw, def, node) {
|
||||
if (def.fixed === false) return;
|
||||
var assigns = tw.assigns.get(def.id);
|
||||
if (assigns) assigns.forEach(function(node) {
|
||||
node.assigns = assigns;
|
||||
});
|
||||
tw.assigns.set(def.id, def.assigns = [ node ]);
|
||||
}
|
||||
|
||||
function safe_to_read(tw, def) {
|
||||
if (def.single_use == "m") return false;
|
||||
if (tw.safe_ids[def.id]) {
|
||||
@@ -479,8 +496,7 @@ merge(Compressor.prototype, {
|
||||
function ref_once(tw, compressor, def) {
|
||||
return compressor.option("unused")
|
||||
&& !def.scope.pinned()
|
||||
&& def.references.length - def.recursive_refs == 1
|
||||
&& tw.loop_ids[def.id] === tw.in_loop;
|
||||
&& def.references.length - def.recursive_refs == 1;
|
||||
}
|
||||
|
||||
function is_immutable(value) {
|
||||
@@ -561,13 +577,13 @@ merge(Compressor.prototype, {
|
||||
var eq = node.operator == "=";
|
||||
var value = eq ? node.right : node;
|
||||
if (is_modified(compressor, tw, node, value, 0)) return;
|
||||
if (!eq) d.chained = true;
|
||||
sym.fixed = d.fixed = eq ? function() {
|
||||
return node.right;
|
||||
} : function() {
|
||||
return make_node(AST_Binary, node, {
|
||||
var value = fixed instanceof AST_Node ? fixed : fixed();
|
||||
return value && make_node(AST_Binary, node, {
|
||||
operator: node.operator.slice(0, -1),
|
||||
left: fixed instanceof AST_Node ? fixed : fixed(),
|
||||
left: value,
|
||||
right: node.right
|
||||
});
|
||||
};
|
||||
@@ -576,7 +592,12 @@ merge(Compressor.prototype, {
|
||||
mark(tw, d, false);
|
||||
node.right.walk(tw);
|
||||
mark(tw, d, true);
|
||||
if (eq) mark_escaped(tw, d, sym.scope, node, value, 0, 1);
|
||||
if (eq) {
|
||||
mark_escaped(tw, d, sym.scope, node, value, 0, 1);
|
||||
set_assign(tw, d, node);
|
||||
} else {
|
||||
add_assign(tw, d, node);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
def(AST_Binary, function(tw) {
|
||||
@@ -590,7 +611,13 @@ merge(Compressor.prototype, {
|
||||
def(AST_Call, function(tw, descend) {
|
||||
tw.find_parent(AST_Scope).may_call_this();
|
||||
var exp = this.expression;
|
||||
if (exp instanceof AST_SymbolRef) {
|
||||
if (exp instanceof AST_Function) {
|
||||
this.args.forEach(function(arg) {
|
||||
arg.walk(tw);
|
||||
});
|
||||
exp.walk(tw);
|
||||
return true;
|
||||
} else if (exp instanceof AST_SymbolRef) {
|
||||
var def = exp.definition();
|
||||
if (this.TYPE == "Call" && tw.in_boolean_context()) def.bool_fn++;
|
||||
if (!(def.fixed instanceof AST_Defun)) return;
|
||||
@@ -689,25 +716,29 @@ merge(Compressor.prototype, {
|
||||
return true;
|
||||
});
|
||||
def(AST_Function, function(tw, descend, compressor) {
|
||||
var node = this;
|
||||
node.inlined = false;
|
||||
push(tw);
|
||||
reset_variables(tw, compressor, node);
|
||||
var fn = this;
|
||||
fn.inlined = false;
|
||||
var iife;
|
||||
if (!node.name
|
||||
&& (iife = tw.parent()) instanceof AST_Call
|
||||
&& iife.expression === node) {
|
||||
if (!fn.name && (iife = tw.parent()) instanceof AST_Call && iife.expression === fn) {
|
||||
var hit = false;
|
||||
var aborts = false;
|
||||
fn.walk(new TreeWalker(function(node) {
|
||||
if (hit) return aborts = true;
|
||||
if (node instanceof AST_Return) return hit = true;
|
||||
if (node instanceof AST_Scope && node !== fn) return true;
|
||||
}));
|
||||
if (aborts) push(tw);
|
||||
reset_variables(tw, compressor, fn);
|
||||
// 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) {
|
||||
fn.argnames.forEach(function(arg, i) {
|
||||
var d = arg.definition();
|
||||
if (d.fixed === undefined && (!node.uses_arguments || tw.has_directive("use strict"))) {
|
||||
if (d.fixed === undefined && (!fn.uses_arguments || tw.has_directive("use strict"))) {
|
||||
var value = iife.args[i];
|
||||
d.fixed = function() {
|
||||
var j = node.argnames.indexOf(arg);
|
||||
if (j < 0) return value;
|
||||
return iife.args[j] || make_node(AST_Undefined, iife);
|
||||
var j = fn.argnames.indexOf(arg);
|
||||
return (j < 0 ? value : iife.args[j]) || make_node(AST_Undefined, iife);
|
||||
};
|
||||
tw.loop_ids[d.id] = tw.in_loop;
|
||||
mark(tw, d, true);
|
||||
@@ -715,10 +746,18 @@ merge(Compressor.prototype, {
|
||||
d.fixed = false;
|
||||
}
|
||||
});
|
||||
descend();
|
||||
var safe_ids = tw.safe_ids;
|
||||
pop(tw);
|
||||
walk_defuns(tw, fn);
|
||||
if (!aborts) tw.safe_ids = safe_ids;
|
||||
} else {
|
||||
push(tw);
|
||||
reset_variables(tw, compressor, fn);
|
||||
descend();
|
||||
pop(tw);
|
||||
walk_defuns(tw, fn);
|
||||
}
|
||||
descend();
|
||||
pop(tw);
|
||||
walk_defuns(tw, node);
|
||||
return true;
|
||||
});
|
||||
def(AST_If, function(tw) {
|
||||
@@ -760,8 +799,13 @@ merge(Compressor.prototype, {
|
||||
if (recursive) {
|
||||
d.recursive_refs++;
|
||||
} else if (value && ref_once(tw, compressor, d)) {
|
||||
d.single_use = value instanceof AST_Lambda && !value.pinned()
|
||||
|| d.scope === this.scope && value.is_constant_expression();
|
||||
d.in_loop = tw.loop_ids[d.id] !== tw.in_loop;
|
||||
d.single_use = value instanceof AST_Lambda
|
||||
&& !value.pinned()
|
||||
&& (!d.in_loop || tw.parent() instanceof AST_Call)
|
||||
|| !d.in_loop
|
||||
&& d.scope === this.scope
|
||||
&& value.is_constant_expression();
|
||||
} else {
|
||||
d.single_use = false;
|
||||
}
|
||||
@@ -821,22 +865,30 @@ merge(Compressor.prototype, {
|
||||
d.assignments++;
|
||||
var fixed = d.fixed;
|
||||
if (!fixed) return;
|
||||
d.chained = true;
|
||||
exp.fixed = d.fixed = function() {
|
||||
return make_node(AST_Binary, node, {
|
||||
d.fixed = function() {
|
||||
var value = fixed instanceof AST_Node ? fixed : fixed();
|
||||
return value && make_node(AST_Binary, node, {
|
||||
operator: node.operator.slice(0, -1),
|
||||
left: make_node(AST_UnaryPrefix, node, {
|
||||
operator: "+",
|
||||
expression: fixed instanceof AST_Node ? fixed : fixed()
|
||||
expression: value
|
||||
}),
|
||||
right: make_node(AST_Number, node, {
|
||||
value: 1
|
||||
})
|
||||
});
|
||||
};
|
||||
exp.fixed = node instanceof AST_UnaryPrefix ? d.fixed : function() {
|
||||
var value = fixed instanceof AST_Node ? fixed : fixed();
|
||||
return value && make_node(AST_UnaryPrefix, node, {
|
||||
operator: "+",
|
||||
expression: value
|
||||
});
|
||||
};
|
||||
if (!safe) return;
|
||||
d.references.push(exp);
|
||||
mark(tw, d, true);
|
||||
add_assign(tw, d, node);
|
||||
return true;
|
||||
});
|
||||
def(AST_VarDef, function(tw, descend) {
|
||||
@@ -851,6 +903,7 @@ merge(Compressor.prototype, {
|
||||
mark(tw, d, false);
|
||||
descend();
|
||||
mark(tw, d, true);
|
||||
set_assign(tw, d, node);
|
||||
return true;
|
||||
} else {
|
||||
d.fixed = false;
|
||||
@@ -875,6 +928,8 @@ merge(Compressor.prototype, {
|
||||
reset_flags(node);
|
||||
return node.reduce_vars(tw, descend, compressor);
|
||||
} : reset_flags);
|
||||
// Assignment chains
|
||||
tw.assigns = new Dictionary();
|
||||
// Flow control for visiting `AST_Defun`s
|
||||
tw.defun_ids = Object.create(null);
|
||||
tw.defun_visited = Object.create(null);
|
||||
@@ -895,10 +950,10 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
});
|
||||
|
||||
AST_Symbol.DEFMETHOD("fixed_value", function(final) {
|
||||
AST_Symbol.DEFMETHOD("fixed_value", function() {
|
||||
var fixed = this.definition().fixed;
|
||||
if (!fixed) return fixed;
|
||||
if (!final && this.fixed) fixed = this.fixed;
|
||||
if (this.fixed) fixed = this.fixed;
|
||||
return fixed instanceof AST_Node ? fixed : fixed();
|
||||
});
|
||||
|
||||
@@ -1062,6 +1117,10 @@ merge(Compressor.prototype, {
|
||||
return node instanceof AST_SymbolRef && node.definition().undeclared;
|
||||
}
|
||||
|
||||
function get_rvalue(expr) {
|
||||
return expr[expr instanceof AST_Assign ? "right" : "value"];
|
||||
}
|
||||
|
||||
var global_names = makePredicate("Array Boolean clearInterval clearTimeout console Date decodeURI decodeURIComponent encodeURI encodeURIComponent Error escape eval EvalError Function isFinite isNaN JSON Math Number parseFloat parseInt RangeError ReferenceError RegExp Object setInterval setTimeout String SyntaxError TypeError unescape URIError");
|
||||
AST_SymbolRef.DEFMETHOD("is_declared", function(compressor) {
|
||||
return this.defined
|
||||
@@ -1140,7 +1199,7 @@ merge(Compressor.prototype, {
|
||||
if (!hit) {
|
||||
if (node !== hit_stack[hit_index]) return node;
|
||||
hit_index++;
|
||||
if (hit_index < hit_stack.length) return handle_custom_scan_order(node);
|
||||
if (hit_index < hit_stack.length) return handle_custom_scan_order(node, scanner);
|
||||
hit = true;
|
||||
stop_after = (value_def ? find_stop_value : find_stop)(node, 0);
|
||||
if (stop_after === node) abort = true;
|
||||
@@ -1187,6 +1246,7 @@ merge(Compressor.prototype, {
|
||||
col: node.start.col
|
||||
});
|
||||
if (candidate instanceof AST_UnaryPostfix) {
|
||||
delete candidate.expression.fixed;
|
||||
return make_node(AST_UnaryPrefix, candidate, candidate);
|
||||
}
|
||||
if (candidate instanceof AST_VarDef) {
|
||||
@@ -1218,7 +1278,7 @@ merge(Compressor.prototype, {
|
||||
can_replace = replace;
|
||||
return node;
|
||||
}
|
||||
return handle_custom_scan_order(node);
|
||||
return handle_custom_scan_order(node, scanner);
|
||||
}, function(node) {
|
||||
if (abort) return;
|
||||
if (stop_after === node) abort = true;
|
||||
@@ -1241,7 +1301,7 @@ merge(Compressor.prototype, {
|
||||
value_def.replaced++;
|
||||
return List.skip;
|
||||
}
|
||||
return get_rvalue(candidate);
|
||||
return rvalue;
|
||||
case 1:
|
||||
if (!assign_used && node.body === candidate) {
|
||||
hit = true;
|
||||
@@ -1260,7 +1320,7 @@ merge(Compressor.prototype, {
|
||||
if (is_lhs(node, multi_replacer.parent())) return node;
|
||||
def.replaced++;
|
||||
value_def.replaced--;
|
||||
return get_rvalue(candidate).clone();
|
||||
return rvalue.clone();
|
||||
}
|
||||
// Skip (non-executed) functions and (leading) default case in switch statements
|
||||
if (node instanceof AST_Default || node instanceof AST_Scope) return node;
|
||||
@@ -1284,13 +1344,14 @@ merge(Compressor.prototype, {
|
||||
var lhs = get_lhs(candidate);
|
||||
var side_effects = lhs && lhs.has_side_effects(compressor);
|
||||
var scan_lhs = lhs && !side_effects && !is_lhs_read_only(lhs, compressor);
|
||||
var scan_rhs = foldable(get_rhs(candidate));
|
||||
var scan_rhs = foldable(candidate);
|
||||
if (!scan_lhs && !scan_rhs) continue;
|
||||
var modify_toplevel = false;
|
||||
// Locate symbols which may execute code outside of scanning range
|
||||
var lvalues = get_lvalues(candidate);
|
||||
var lhs_local = is_lhs_local(lhs);
|
||||
if (!side_effects) side_effects = value_has_side_effects(candidate);
|
||||
var rvalue = get_rvalue(candidate);
|
||||
if (!side_effects) side_effects = value_has_side_effects();
|
||||
var replace_all = replace_all_symbols(candidate);
|
||||
var may_throw = candidate.may_throw(compressor) ? in_try ? function(node) {
|
||||
return node.has_side_effects(compressor);
|
||||
@@ -1337,12 +1398,12 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
|
||||
function handle_custom_scan_order(node) {
|
||||
function handle_custom_scan_order(node, tt) {
|
||||
// Skip (non-executed) functions
|
||||
if (node instanceof AST_Scope) return node;
|
||||
// Scan case expressions first in a switch statement
|
||||
if (node instanceof AST_Switch) {
|
||||
node.expression = node.expression.transform(scanner);
|
||||
node.expression = node.expression.transform(tt);
|
||||
for (var i = 0; !abort && i < node.body.length; i++) {
|
||||
var branch = node.body[i];
|
||||
if (branch instanceof AST_Case) {
|
||||
@@ -1350,7 +1411,7 @@ merge(Compressor.prototype, {
|
||||
if (branch !== hit_stack[hit_index]) continue;
|
||||
hit_index++;
|
||||
}
|
||||
branch.expression = branch.expression.transform(scanner);
|
||||
branch.expression = branch.expression.transform(tt);
|
||||
if (!replace_all) break;
|
||||
scan_rhs = false;
|
||||
}
|
||||
@@ -1361,6 +1422,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
|
||||
function should_stop(node, parent) {
|
||||
if (node === rvalue) return true;
|
||||
if (parent instanceof AST_For) return node !== parent.init;
|
||||
if (node instanceof AST_Assign) {
|
||||
return node.operator != "=" && lhs.equivalent_to(node.left);
|
||||
@@ -1368,8 +1430,7 @@ merge(Compressor.prototype, {
|
||||
if (node instanceof AST_Call) {
|
||||
if (!(lhs instanceof AST_PropAccess)) return false;
|
||||
if (!lhs.equivalent_to(node.expression)) return false;
|
||||
var rhs = get_rvalue(candidate);
|
||||
return !(rhs instanceof AST_Function && !rhs.contains_this());
|
||||
return !(rvalue instanceof AST_Function && !rvalue.contains_this());
|
||||
}
|
||||
if (node instanceof AST_Debugger) return true;
|
||||
if (node instanceof AST_Defun) return funarg && lhs.name === node.name.name;
|
||||
@@ -1775,14 +1836,6 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
|
||||
function get_rhs(expr) {
|
||||
return candidate instanceof AST_Assign && candidate.operator == "=" && candidate.right;
|
||||
}
|
||||
|
||||
function get_rvalue(expr) {
|
||||
return expr[expr instanceof AST_Assign ? "right" : "value"];
|
||||
}
|
||||
|
||||
function invariant(expr) {
|
||||
if (expr instanceof AST_Array) return false;
|
||||
if (expr instanceof AST_Binary && lazy_op[expr.operator]) {
|
||||
@@ -1797,7 +1850,14 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
|
||||
function foldable(expr) {
|
||||
if (!expr) return false;
|
||||
var lhs_ids = Object.create(null);
|
||||
var marker = new TreeWalker(function(node) {
|
||||
if (node instanceof AST_SymbolRef) lhs_ids[node.definition().id] = true;
|
||||
});
|
||||
while (expr instanceof AST_Assign && expr.operator == "=") {
|
||||
expr.left.walk(marker);
|
||||
expr = expr.right;
|
||||
}
|
||||
if (expr instanceof AST_SymbolRef) {
|
||||
var value = expr.evaluate(compressor);
|
||||
if (value === expr) return rhs_exact_match;
|
||||
@@ -1811,12 +1871,9 @@ merge(Compressor.prototype, {
|
||||
if (!(lhs instanceof AST_SymbolRef)) return false;
|
||||
if (!invariant(expr)) return false;
|
||||
var circular;
|
||||
var def = lhs.definition();
|
||||
expr.walk(new TreeWalker(function(node) {
|
||||
if (circular) return true;
|
||||
if (node instanceof AST_SymbolRef && node.definition() === def) {
|
||||
circular = true;
|
||||
}
|
||||
if (node instanceof AST_SymbolRef && lhs_ids[node.definition().id]) circular = true;
|
||||
}));
|
||||
return !circular && rhs_exact_match;
|
||||
|
||||
@@ -1873,12 +1930,14 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
return true;
|
||||
}
|
||||
var found = false;
|
||||
return statements[stat_index].transform(new TreeTransformer(function(node, descend, in_list) {
|
||||
if (found) return node;
|
||||
if (node instanceof AST_Scope) return node;
|
||||
if (node !== expr && node.body !== expr) return;
|
||||
found = true;
|
||||
var end = hit_stack.length - 1;
|
||||
if (hit_stack[end - 1].body === hit_stack[end]) end--;
|
||||
var tt = new TreeTransformer(function(node, descend, in_list) {
|
||||
if (hit) return node;
|
||||
if (node !== hit_stack[hit_index]) return node;
|
||||
hit_index++;
|
||||
if (hit_index <= end) return handle_custom_scan_order(node, tt);
|
||||
hit = true;
|
||||
if (node instanceof AST_VarDef) {
|
||||
node.value = null;
|
||||
declare_only[node.name.name] = (declare_only[node.name.name] || 0) + 1;
|
||||
@@ -1886,7 +1945,11 @@ merge(Compressor.prototype, {
|
||||
return node;
|
||||
}
|
||||
return in_list ? List.skip : null;
|
||||
}, patch_sequence));
|
||||
}, patch_sequence);
|
||||
abort = false;
|
||||
hit = false;
|
||||
hit_index = 0;
|
||||
return statements[stat_index].transform(tt);
|
||||
}
|
||||
|
||||
function patch_sequence(node) {
|
||||
@@ -1906,9 +1969,9 @@ merge(Compressor.prototype, {
|
||||
|| candidate instanceof AST_Assign && candidate.operator != "="));
|
||||
}
|
||||
|
||||
function value_has_side_effects(expr) {
|
||||
if (expr instanceof AST_Unary) return false;
|
||||
return get_rvalue(expr).has_side_effects(compressor);
|
||||
function value_has_side_effects() {
|
||||
if (candidate instanceof AST_Unary) return false;
|
||||
return rvalue.has_side_effects(compressor);
|
||||
}
|
||||
|
||||
function replace_all_symbols(expr) {
|
||||
@@ -1986,8 +2049,10 @@ merge(Compressor.prototype, {
|
||||
|
||||
function handle_if_return(statements, compressor) {
|
||||
var self = compressor.self();
|
||||
var multiple_if_returns = has_multiple_if_returns(statements);
|
||||
var parent = compressor.parent();
|
||||
var in_lambda = self instanceof AST_Lambda;
|
||||
var in_iife = in_lambda && parent && parent.TYPE == "Call";
|
||||
var multiple_if_returns = has_multiple_if_returns(statements);
|
||||
for (var i = statements.length; --i >= 0;) {
|
||||
var stat = statements[i];
|
||||
var j = next_index(i);
|
||||
@@ -2115,7 +2180,7 @@ merge(Compressor.prototype, {
|
||||
// the example code.
|
||||
var prev = statements[prev_index(i)];
|
||||
if (compressor.option("sequences") && in_lambda && !stat.alternative
|
||||
&& prev instanceof AST_If && prev.body instanceof AST_Return
|
||||
&& (!prev && in_iife || prev instanceof AST_If && prev.body instanceof AST_Return)
|
||||
&& next_index(j) == statements.length && next instanceof AST_SimpleStatement) {
|
||||
CHANGED = true;
|
||||
stat = stat.clone();
|
||||
@@ -2442,7 +2507,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
if (prop instanceof AST_Node) break;
|
||||
prop = "" + prop;
|
||||
var diff = compressor.has_directive("use strict") ? function(node) {
|
||||
var diff = prop == "__proto__" || compressor.has_directive("use strict") ? function(node) {
|
||||
return node.key != prop && node.key.name != prop;
|
||||
} : function(node) {
|
||||
return node.key.name != prop;
|
||||
@@ -2536,6 +2601,7 @@ merge(Compressor.prototype, {
|
||||
if (!declarations_only(node)) return node;
|
||||
defs.definitions = defs.definitions.concat(node.definitions);
|
||||
CHANGED = true;
|
||||
if (parent instanceof AST_For && parent.init === node) return null;
|
||||
return in_list ? List.skip : make_node(AST_EmptyStatement, node);
|
||||
}
|
||||
if (node instanceof AST_Scope) return node;
|
||||
@@ -3189,6 +3255,22 @@ merge(Compressor.prototype, {
|
||||
&& unaryPrefix[this.operator];
|
||||
}
|
||||
});
|
||||
var scan_modified = new TreeWalker(function(node) {
|
||||
if (node instanceof AST_Assign) modified(node.left);
|
||||
if (node instanceof AST_Unary && unary_arithmetic[node.operator]) modified(node.expression);
|
||||
});
|
||||
function modified(node) {
|
||||
if (node instanceof AST_Dot) {
|
||||
modified(node.expression);
|
||||
} else if (node instanceof AST_Sub) {
|
||||
modified(node.expression);
|
||||
node.property.walk(scan_modified);
|
||||
} else if (node instanceof AST_SymbolRef) {
|
||||
node.definition().references.forEach(function(ref) {
|
||||
delete ref._eval;
|
||||
});
|
||||
}
|
||||
}
|
||||
def(AST_Statement, function() {
|
||||
throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));
|
||||
});
|
||||
@@ -3202,13 +3284,18 @@ merge(Compressor.prototype, {
|
||||
if (this.operator != "=") return this;
|
||||
var node = this.right;
|
||||
var value = node._eval(compressor, ignore_side_effects, cached, depth);
|
||||
modified(this.left);
|
||||
return value === node ? this : value;
|
||||
});
|
||||
def(AST_Sequence, function(compressor, ignore_side_effects, cached, depth) {
|
||||
if (!ignore_side_effects) return this;
|
||||
var node = this.tail_node();
|
||||
var value = node._eval(compressor, ignore_side_effects, cached, depth);
|
||||
return value === node ? this : value;
|
||||
var exprs = this.expressions;
|
||||
for (var i = 0, last = exprs.length - 1; i < last; i++) {
|
||||
exprs[i].walk(scan_modified);
|
||||
}
|
||||
var tail = exprs[last];
|
||||
var value = tail._eval(compressor, ignore_side_effects, cached, depth);
|
||||
return value === tail ? this : value;
|
||||
});
|
||||
def(AST_Lambda, function(compressor) {
|
||||
if (compressor.option("unsafe")) {
|
||||
@@ -3260,26 +3347,30 @@ merge(Compressor.prototype, {
|
||||
var non_converting_unary = makePredicate("! typeof void");
|
||||
def(AST_UnaryPrefix, function(compressor, ignore_side_effects, cached, depth) {
|
||||
var e = this.expression;
|
||||
var op = this.operator;
|
||||
// Function would be evaluated to an array and so typeof would
|
||||
// incorrectly return 'object'. Hence making is a special case.
|
||||
if (compressor.option("typeofs")
|
||||
&& this.operator == "typeof"
|
||||
&& op == "typeof"
|
||||
&& (e instanceof AST_Lambda
|
||||
|| e instanceof AST_SymbolRef
|
||||
&& e.fixed_value() instanceof AST_Lambda)) {
|
||||
return typeof function(){};
|
||||
}
|
||||
if (!non_converting_unary[this.operator]) depth++;
|
||||
if (!non_converting_unary[op]) depth++;
|
||||
var v = e._eval(compressor, ignore_side_effects, cached, depth);
|
||||
if (v === this.expression) return this;
|
||||
switch (this.operator) {
|
||||
if (v === e) {
|
||||
if (ignore_side_effects && op == "void") return;
|
||||
return this;
|
||||
}
|
||||
switch (op) {
|
||||
case "!": return !v;
|
||||
case "typeof":
|
||||
// typeof <RegExp> returns "object" or "function" on different platforms
|
||||
// so cannot evaluate reliably
|
||||
if (v instanceof RegExp) return this;
|
||||
return typeof v;
|
||||
case "void": return void v;
|
||||
case "void": return;
|
||||
case "~": return ~v;
|
||||
case "-": return -v;
|
||||
case "+": return +v;
|
||||
@@ -3287,11 +3378,22 @@ merge(Compressor.prototype, {
|
||||
case "--":
|
||||
if (!(e instanceof AST_SymbolRef)) return this;
|
||||
var refs = e.definition().references;
|
||||
if (refs[refs.length - 1] !== e) return this;
|
||||
return HOP(e, "_eval") ? +(this.operator[0] + 1) + +v : v;
|
||||
if (!ignore_side_effects && refs[refs.length - 1] !== e) return this;
|
||||
if (HOP(e, "_eval")) v = +(op[0] + 1) + +v;
|
||||
modified(e);
|
||||
return v;
|
||||
}
|
||||
return this;
|
||||
});
|
||||
def(AST_UnaryPostfix, function(compressor, ignore_side_effects, cached, depth) {
|
||||
var e = this.expression;
|
||||
if (!e.fixed) return this;
|
||||
var refs = e.definition().references;
|
||||
if (!ignore_side_effects && refs[refs.length - 1] !== e) return this;
|
||||
var v = e._eval(compressor, ignore_side_effects, cached, depth + 1);
|
||||
modified(e);
|
||||
return v === e ? this : +v;
|
||||
});
|
||||
var non_converting_binary = makePredicate("&& || === !==");
|
||||
def(AST_Binary, function(compressor, ignore_side_effects, cached, depth) {
|
||||
if (!non_converting_binary[this.operator]) depth++;
|
||||
@@ -3453,12 +3555,29 @@ merge(Compressor.prototype, {
|
||||
if (fn instanceof AST_Lambda) {
|
||||
if (fn.evaluating) return this;
|
||||
if (fn.name && fn.name.definition().recursive_refs > 0) return this;
|
||||
if (this.is_expr_pure(compressor)) return this;
|
||||
var stat = fn.first_statement();
|
||||
if (!(stat instanceof AST_Return)) return this;
|
||||
if (!(stat instanceof AST_Return)) {
|
||||
if (ignore_side_effects) {
|
||||
var found = false;
|
||||
fn.walk(new TreeWalker(function(node) {
|
||||
if (found) return true;
|
||||
if (node instanceof AST_Return) {
|
||||
if (node.value && node.value.evaluate(compressor, true) !== undefined) found = true;
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Scope && node !== fn) return true;
|
||||
}));
|
||||
if (!found) return;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
var args = eval_args(this.args);
|
||||
if (!args) return this;
|
||||
if (!stat.value) return undefined;
|
||||
if (!all(fn.argnames, function(sym, i) {
|
||||
if (!args && !ignore_side_effects) return this;
|
||||
var val = stat.value;
|
||||
if (!val) return;
|
||||
var cached_args = [];
|
||||
if (!args || all(fn.argnames, function(sym, i) {
|
||||
var value = args[i];
|
||||
var def = sym.definition();
|
||||
if (def.orig[def.orig.length - 1] !== sym) return false;
|
||||
@@ -3466,15 +3585,18 @@ merge(Compressor.prototype, {
|
||||
node._eval = function() {
|
||||
return value;
|
||||
};
|
||||
cached.push(node);
|
||||
cached_args.push(node);
|
||||
});
|
||||
return true;
|
||||
})) return this;
|
||||
fn.evaluating = true;
|
||||
var val = stat.value._eval(compressor, ignore_side_effects, cached, depth);
|
||||
delete fn.evaluating;
|
||||
if (val === stat.value) return this;
|
||||
return val;
|
||||
}) || ignore_side_effects) {
|
||||
fn.evaluating = true;
|
||||
val = val._eval(compressor, ignore_side_effects, cached, depth);
|
||||
delete fn.evaluating;
|
||||
}
|
||||
cached_args.forEach(function(node) {
|
||||
delete node._eval;
|
||||
});
|
||||
return val === stat.value ? this : val;
|
||||
} else if (compressor.option("unsafe") && exp instanceof AST_PropAccess) {
|
||||
var key = exp.property;
|
||||
if (key instanceof AST_Node) {
|
||||
@@ -3989,6 +4111,7 @@ merge(Compressor.prototype, {
|
||||
if (stat instanceof AST_Return) {
|
||||
var call = stat.value;
|
||||
if (!call || call.TYPE != "Call") break;
|
||||
if (call.is_expr_pure(compressor)) break;
|
||||
var fn = call.expression;
|
||||
if (fn instanceof AST_SymbolRef) {
|
||||
if (self.name && self.name.definition() === fn.definition()) break;
|
||||
@@ -4061,9 +4184,9 @@ merge(Compressor.prototype, {
|
||||
})) return;
|
||||
return sym;
|
||||
};
|
||||
var chained = Object.create(null);
|
||||
var in_use = [];
|
||||
var in_use_ids = Object.create(null); // avoid expensive linear scans of in_use
|
||||
var fixed_ids = Object.create(null);
|
||||
var value_read = Object.create(null);
|
||||
var value_modified = Object.create(null);
|
||||
if (self instanceof AST_Toplevel && compressor.top_retain) {
|
||||
@@ -4074,6 +4197,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
});
|
||||
}
|
||||
var assign_in_use = new Dictionary();
|
||||
var var_defs_by_id = new Dictionary();
|
||||
var initializations = new Dictionary();
|
||||
// pass 1: find out which symbols are directly used in
|
||||
@@ -4115,13 +4239,12 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
if (def.value) {
|
||||
initializations.add(node_def.id, def.value);
|
||||
if (def.value.has_side_effects(compressor)) {
|
||||
def.value.walk(tw);
|
||||
} else {
|
||||
initializations.add(node_def.id, def.value);
|
||||
}
|
||||
if (!node_def.chained && def.name.fixed_value(true) === def.value) {
|
||||
fixed_ids[node_def.id] = def;
|
||||
}
|
||||
match_assigns(node_def, def);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
@@ -4140,7 +4263,7 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
}
|
||||
var drop_fn_name = compressor.option("keep_fnames") ? return_false : compressor.option("ie8") ? function(def) {
|
||||
return !compressor.exposed(def) && !def.references.length;
|
||||
return !compressor.exposed(def) && def.references.length == def.replaced;
|
||||
} : function(def) {
|
||||
// any declarations with same name will overshadow
|
||||
// name of this anonymous function and can therefore
|
||||
@@ -4160,7 +4283,7 @@ merge(Compressor.prototype, {
|
||||
var in_use = def.id in in_use_ids;
|
||||
var value;
|
||||
if (node instanceof AST_Assign) {
|
||||
if (!in_use || node.left === sym && def.id in fixed_ids && fixed_ids[def.id] !== node) {
|
||||
if (!in_use || node.left === sym && indexOf_assign(def, node) < 0) {
|
||||
value = get_rhs(node);
|
||||
if (node.write_only === true) {
|
||||
value = value.drop_side_effect_free(compressor) || make_node(AST_Number, node, {
|
||||
@@ -4168,7 +4291,7 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
}
|
||||
}
|
||||
} else if (!in_use) {
|
||||
} else if (!in_use || node.expression === sym && indexOf_assign(def, node) < 0) {
|
||||
value = make_node(AST_Number, node, {
|
||||
value: 0
|
||||
});
|
||||
@@ -4234,7 +4357,7 @@ merge(Compressor.prototype, {
|
||||
if (def.value) def.value = def.value.transform(tt);
|
||||
var sym = def.name.definition();
|
||||
if (!drop_vars || sym.id in in_use_ids) {
|
||||
if (def.value && sym.id in fixed_ids && fixed_ids[sym.id] !== def) {
|
||||
if (def.value && indexOf_assign(sym, def) < 0) {
|
||||
def.value = def.value.drop_side_effect_free(compressor);
|
||||
}
|
||||
var var_defs = var_defs_by_id.get(sym.id);
|
||||
@@ -4249,6 +4372,7 @@ merge(Compressor.prototype, {
|
||||
} else if (compressor.option("functions")
|
||||
&& !compressor.option("ie8")
|
||||
&& var_defs.length == 1
|
||||
&& sym.assignments == 0
|
||||
&& def.value === def.name.fixed_value()
|
||||
&& def.value instanceof AST_Function
|
||||
&& !(def.value.name && def.value.name.definition().assignments)
|
||||
@@ -4326,7 +4450,8 @@ merge(Compressor.prototype, {
|
||||
left: ref,
|
||||
right: def.value
|
||||
});
|
||||
if (fixed_ids[sym.id] === def) fixed_ids[sym.id] = assign;
|
||||
var index = indexOf_assign(sym, def);
|
||||
if (index >= 0) assign_in_use.get(sym.id)[index] = assign;
|
||||
sym.eliminated++;
|
||||
return assign.transform(tt);
|
||||
}));
|
||||
@@ -4352,7 +4477,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
if (node instanceof AST_LabeledStatement && node.body instanceof AST_For) {
|
||||
// Certain combination of unused name + side effect leads to invalid AST:
|
||||
// https://github.com/mishoo/UglifyJS2/issues/1830
|
||||
// https://github.com/mishoo/UglifyJS/issues/1830
|
||||
// We fix it at this stage by moving the label inwards, back to the `for`.
|
||||
descend(node, tt);
|
||||
if (node.body instanceof AST_BlockStatement) {
|
||||
@@ -4373,9 +4498,9 @@ merge(Compressor.prototype, {
|
||||
}, function(node, in_list) {
|
||||
if (node instanceof AST_For) {
|
||||
// Certain combination of unused name + side effect leads to invalid AST:
|
||||
// https://github.com/mishoo/UglifyJS2/issues/44
|
||||
// https://github.com/mishoo/UglifyJS2/issues/1838
|
||||
// https://github.com/mishoo/UglifyJS2/issues/3371
|
||||
// https://github.com/mishoo/UglifyJS/issues/44
|
||||
// https://github.com/mishoo/UglifyJS/issues/1838
|
||||
// https://github.com/mishoo/UglifyJS/issues/3371
|
||||
// We fix it at this stage by moving the `var` outside the `for`.
|
||||
var block;
|
||||
if (node.init instanceof AST_BlockStatement) {
|
||||
@@ -4444,6 +4569,32 @@ merge(Compressor.prototype, {
|
||||
};
|
||||
}
|
||||
|
||||
function match_assigns(def, node) {
|
||||
if (!def.fixed) return;
|
||||
if (!def.assigns) return;
|
||||
if (find_if(node instanceof AST_Unary ? function(assign) {
|
||||
return assign === node;
|
||||
} : function(assign) {
|
||||
if (assign === node) return true;
|
||||
if (assign instanceof AST_Unary) return false;
|
||||
return get_rvalue(assign) === get_rvalue(node);
|
||||
}, def.assigns)) {
|
||||
assign_in_use.add(def.id, node);
|
||||
}
|
||||
}
|
||||
|
||||
function indexOf_assign(def, node) {
|
||||
if (!def.fixed) return;
|
||||
if (!def.assigns) return;
|
||||
var assigns = assign_in_use.get(def.id);
|
||||
if (!assigns) return;
|
||||
if (assigns.length != def.assigns.length) return;
|
||||
var index = assigns.indexOf(node);
|
||||
if (index >= 0 || !chained[def.id] || node.assigns && all(node.assigns, function(assign) {
|
||||
return assign.write_only || assign.operator == "=" || assign instanceof AST_VarDef;
|
||||
})) return index;
|
||||
}
|
||||
|
||||
function verify_safe_usage(def, read, modified) {
|
||||
if (def.id in in_use_ids) return;
|
||||
if (read && modified) {
|
||||
@@ -4465,6 +4616,16 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
|
||||
function scan_ref_scoped(node, descend, init) {
|
||||
if (node instanceof AST_Assign && node.left instanceof AST_SymbolRef) {
|
||||
var node_def = node.left.definition();
|
||||
if (node.operator != "=") chained[node_def.id] = true;
|
||||
match_assigns(node_def, node);
|
||||
}
|
||||
if (node instanceof AST_Unary && node.expression instanceof AST_SymbolRef) {
|
||||
var node_def = node.expression.definition();
|
||||
chained[node_def.id] = true;
|
||||
match_assigns(node_def, node);
|
||||
}
|
||||
var node_def, props = [], sym = assign_as_unused(node, props);
|
||||
if (sym && self.variables.get(sym.name) === (node_def = sym.definition())) {
|
||||
props.forEach(function(prop) {
|
||||
@@ -4479,9 +4640,6 @@ merge(Compressor.prototype, {
|
||||
right.walk(tw);
|
||||
}
|
||||
if (node.left === sym) {
|
||||
if (!node_def.chained && sym.fixed_value(true) === right) {
|
||||
fixed_ids[node_def.id] = node;
|
||||
}
|
||||
if (!node.write_only) {
|
||||
verify_safe_usage(node_def, true, value_modified[node_def.id]);
|
||||
}
|
||||
@@ -4660,7 +4818,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
|
||||
function all_bool(def, bool_returns, compressor) {
|
||||
return def.bool_fn + (bool_returns[def.id] || 0) === def.references.length
|
||||
return def.bool_fn + (bool_returns[def.id] || 0) === def.references.length - def.replaced
|
||||
&& !compressor.exposed(def);
|
||||
}
|
||||
|
||||
@@ -4846,11 +5004,15 @@ merge(Compressor.prototype, {
|
||||
if (def.assignments != count) return;
|
||||
if (def.direct_access) return;
|
||||
if (def.escaped.depth == 1) return;
|
||||
if (def.references.length == count) return;
|
||||
if (def.references.length - def.replaced == count) return;
|
||||
if (def.single_use) return;
|
||||
if (top_retain(def)) return;
|
||||
if (sym.fixed_value() !== right) return;
|
||||
return right instanceof AST_Object;
|
||||
return right instanceof AST_Object
|
||||
&& right.properties.length > 0
|
||||
&& all(right.properties, function(prop) {
|
||||
return prop instanceof AST_ObjectKeyVal;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -4936,7 +5098,11 @@ merge(Compressor.prototype, {
|
||||
exprs = trim(exprs, compressor, first_in_statement);
|
||||
return exprs && make_sequence(this, exprs);
|
||||
}
|
||||
if (exp instanceof AST_Function && (!exp.name || !exp.name.definition().references.length)) {
|
||||
if (exp instanceof AST_Function) {
|
||||
if (exp.name) {
|
||||
var def = exp.name.definition();
|
||||
if (def.references.length > def.replaced) return this;
|
||||
}
|
||||
exp.process_expression(false, function(node) {
|
||||
var value = node.value && node.value.drop_side_effect_free(compressor, true);
|
||||
return value ? make_node(AST_SimpleStatement, node, {
|
||||
@@ -5395,7 +5561,7 @@ merge(Compressor.prototype, {
|
||||
&& self.condition instanceof AST_Binary && self.condition.operator == "||") {
|
||||
// although the code length of self.condition and negated are the same,
|
||||
// negated does not require additional surrounding parentheses.
|
||||
// see https://github.com/mishoo/UglifyJS2/issues/979
|
||||
// see https://github.com/mishoo/UglifyJS/issues/979
|
||||
negated_is_best = true;
|
||||
}
|
||||
body.push(make_node(AST_SimpleStatement, self, {
|
||||
@@ -6014,7 +6180,7 @@ merge(Compressor.prototype, {
|
||||
return x instanceof AST_String;
|
||||
})) {
|
||||
// quite a corner-case, but we can handle it:
|
||||
// https://github.com/mishoo/UglifyJS2/issues/203
|
||||
// https://github.com/mishoo/UglifyJS/issues/203
|
||||
// if the code argument is a constant, then we can minify it.
|
||||
try {
|
||||
var code = "n(function(" + self.args.slice(0, -1).map(function(arg) {
|
||||
@@ -6076,9 +6242,9 @@ merge(Compressor.prototype, {
|
||||
&& !fn.uses_arguments
|
||||
&& !fn.pinned()
|
||||
&& !(fn.name && fn instanceof AST_Function)
|
||||
&& (value = can_flatten_body(stat))
|
||||
&& (exp === fn
|
||||
|| !recursive_ref(compressor, def = exp.definition()) && fn.is_constant_expression(exp.scope))
|
||||
&& (value = can_flatten_body(stat))
|
||||
&& !fn.contains_this()) {
|
||||
if (can_substitute_directly()) {
|
||||
var args = self.args.slice();
|
||||
@@ -6104,7 +6270,7 @@ merge(Compressor.prototype, {
|
||||
if (best_of(compressor, self, node) === node) return node;
|
||||
}
|
||||
var scope, in_loop, level = -1;
|
||||
if ((exp === fn || compressor.option("unused") && exp.definition().references.length == 1)
|
||||
if ((exp === fn || compressor.option("unused") && def.references.length - def.replaced == 1)
|
||||
&& can_inject_symbols()) {
|
||||
fn._squeezed = true;
|
||||
if (exp !== fn) fn.parent_scope = exp.scope;
|
||||
@@ -6188,7 +6354,7 @@ merge(Compressor.prototype, {
|
||||
if (var_assigned) return;
|
||||
if (compressor.option("inline") < 2 && fn.argnames.length) return;
|
||||
if (!fn.variables.all(function(def) {
|
||||
return def.references.length < 2 && def.orig[0] instanceof AST_SymbolFunarg;
|
||||
return def.references.length - def.replaced < 2 && def.orig[0] instanceof AST_SymbolFunarg;
|
||||
})) return;
|
||||
var abort = false;
|
||||
var begin;
|
||||
@@ -6713,7 +6879,7 @@ merge(Compressor.prototype, {
|
||||
if ((self.left.is_string(compressor) && self.right.is_string(compressor)) ||
|
||||
(self.left.is_number(compressor) && self.right.is_number(compressor)) ||
|
||||
(self.left.is_boolean(compressor) && self.right.is_boolean(compressor)) ||
|
||||
self.left.equivalent_to(self.right)) {
|
||||
can_self_compare(self.left) && self.left.equivalent_to(self.right)) {
|
||||
self.operator = self.operator.slice(0, 2);
|
||||
}
|
||||
// XXX: intentionally falling down to the next case
|
||||
@@ -7236,6 +7402,13 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
return try_evaluate(compressor, self);
|
||||
|
||||
function can_self_compare(node) {
|
||||
if (node instanceof AST_Dot) return can_self_compare(node.expression);
|
||||
if (node instanceof AST_Sub) return can_self_compare(node.expression) && can_self_compare(node.property);
|
||||
if (node instanceof AST_Symbol) return true;
|
||||
return !node.has_side_effects(compressor);
|
||||
}
|
||||
|
||||
function align(ref, op) {
|
||||
switch (ref) {
|
||||
case "-":
|
||||
@@ -7337,7 +7510,7 @@ merge(Compressor.prototype, {
|
||||
var single_use = def.single_use && !(parent instanceof AST_Call && parent.is_expr_pure(compressor));
|
||||
if (single_use) {
|
||||
if (fixed instanceof AST_Lambda) {
|
||||
if (def.scope !== self.scope
|
||||
if ((def.scope !== self.scope || def.in_loop)
|
||||
&& (!compressor.option("reduce_funcs") || def.escaped.depth == 1 || fixed.inlined)) {
|
||||
single_use = false;
|
||||
} else if (recursive_ref(compressor, def)) {
|
||||
@@ -7350,6 +7523,8 @@ merge(Compressor.prototype, {
|
||||
scope.inlined = true;
|
||||
} while (scope = scope.parent_scope);
|
||||
}
|
||||
} else if (compressor.option("ie8") && fixed.name && def !== fixed.name.definition()) {
|
||||
single_use = false;
|
||||
}
|
||||
if (single_use) fixed.parent_scope = self.scope;
|
||||
} else if (!fixed || !fixed.is_constant_expression()) {
|
||||
@@ -7408,6 +7583,12 @@ merge(Compressor.prototype, {
|
||||
}));
|
||||
} else {
|
||||
value = fixed.optimize(compressor);
|
||||
if (value === fixed) value = value.transform(new TreeTransformer(function(node, descend) {
|
||||
if (node instanceof AST_Scope) return node;
|
||||
node = node.clone();
|
||||
descend(node, this);
|
||||
return node;
|
||||
}));
|
||||
}
|
||||
def.replaced++;
|
||||
return value;
|
||||
@@ -7419,9 +7600,13 @@ merge(Compressor.prototype, {
|
||||
init = fixed;
|
||||
}
|
||||
} else {
|
||||
var ev = fixed.evaluate(compressor);
|
||||
if (ev !== fixed && (!(ev instanceof RegExp)
|
||||
|| compressor.option("unsafe_regexp") && !def.cross_loop && same_scope(def))) {
|
||||
var ev = fixed.evaluate(compressor, true);
|
||||
if (ev !== fixed
|
||||
&& typeof ev != "function"
|
||||
&& (typeof ev != "object"
|
||||
|| ev instanceof RegExp
|
||||
&& compressor.option("unsafe_regexp")
|
||||
&& !def.cross_loop && same_scope(def))) {
|
||||
init = make_node_from_constant(ev, fixed);
|
||||
}
|
||||
}
|
||||
@@ -7442,7 +7627,8 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
var name_length = def.name.length;
|
||||
if (compressor.option("unused") && !compressor.exposed(def)) {
|
||||
name_length += (name_length + 2 + value_length) / (def.references.length - def.assignments);
|
||||
var referenced = def.references.length - def.replaced;
|
||||
name_length += (name_length + 2 + value_length) / (referenced - def.assignments);
|
||||
}
|
||||
var delta = value_length - Math.floor(name_length);
|
||||
def.should_replace = delta < compressor.eval_threshold ? fn : false;
|
||||
@@ -7564,6 +7750,10 @@ merge(Compressor.prototype, {
|
||||
if (compressor.option("dead_code")) {
|
||||
if (self.left instanceof AST_PropAccess) {
|
||||
if (self.operator == "=") {
|
||||
if (self.left.equivalent_to(self.right)
|
||||
&& (self.left instanceof AST_Dot || !self.left.property.has_side_effects(compressor))) {
|
||||
return self.right;
|
||||
}
|
||||
var exp = self.left.expression;
|
||||
if (exp instanceof AST_Lambda
|
||||
|| !compressor.has_directive("use strict")
|
||||
@@ -7576,6 +7766,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
} else if (self.left instanceof AST_SymbolRef) {
|
||||
if (self.operator == "=" && self.left.equivalent_to(self.right)) return self.right;
|
||||
if (self.left.is_immutable()) return strip_assignment();
|
||||
var def = self.left.definition();
|
||||
var scope = def.scope.resolve();
|
||||
@@ -7597,6 +7788,11 @@ merge(Compressor.prototype, {
|
||||
if (is_reachable(scope, [ def ])) break;
|
||||
def.fixed = false;
|
||||
return strip_assignment();
|
||||
} else if (parent instanceof AST_VarDef) {
|
||||
if (parent.name.definition() !== def) continue;
|
||||
if (in_try(level, parent)) break;
|
||||
def.fixed = false;
|
||||
return strip_assignment();
|
||||
}
|
||||
} while (parent instanceof AST_Binary && parent.right === node
|
||||
|| parent instanceof AST_Sequence && parent.tail_node() === node
|
||||
@@ -7671,7 +7867,7 @@ merge(Compressor.prototype, {
|
||||
expressions.push(self);
|
||||
return make_sequence(self, expressions);
|
||||
}
|
||||
var condition = self.condition.is_truthy() || self.condition.evaluate(compressor);
|
||||
var condition = self.condition.is_truthy() || self.condition.evaluate(compressor, true);
|
||||
if (!condition) {
|
||||
AST_Node.warn("Condition always false [{file}:{line},{col}]", self.start);
|
||||
return make_sequence(self, [ self.condition, self.alternative ]).optimize(compressor);
|
||||
|
||||
@@ -85,9 +85,11 @@ function minify(files, options) {
|
||||
sourceMap: false,
|
||||
timings: false,
|
||||
toplevel: false,
|
||||
validate: false,
|
||||
warnings: false,
|
||||
wrap: false,
|
||||
}, true);
|
||||
if (options.validate) AST_Node.enable_validation();
|
||||
var timings = options.timings && {
|
||||
start: Date.now()
|
||||
};
|
||||
@@ -253,5 +255,7 @@ function minify(files, options) {
|
||||
return result;
|
||||
} catch (ex) {
|
||||
return { error: ex };
|
||||
} finally {
|
||||
AST_Node.disable_validation();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/***********************************************************************
|
||||
|
||||
A JavaScript tokenizer / parser / beautifier / compressor.
|
||||
https://github.com/mishoo/UglifyJS2
|
||||
https://github.com/mishoo/UglifyJS
|
||||
|
||||
-------------------------------- (C) ---------------------------------
|
||||
|
||||
@@ -111,7 +111,7 @@
|
||||
var args = {
|
||||
start : my_start_token(key),
|
||||
end : my_end_token(M.value),
|
||||
key : key.type == "Identifier" ? key.name : key.value,
|
||||
key : "" + key[key.type == "Identifier" ? "name" : "value"],
|
||||
value : from_moz(M.value)
|
||||
};
|
||||
if (M.kind == "init") return new AST_ObjectKeyVal(args);
|
||||
@@ -212,7 +212,14 @@
|
||||
end : my_end_token(M),
|
||||
name : M.name
|
||||
});
|
||||
}
|
||||
},
|
||||
ThisExpression: function(M) {
|
||||
return new AST_This({
|
||||
start : my_start_token(M),
|
||||
end : my_end_token(M),
|
||||
name : "this",
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
MOZ_TO_ME.UpdateExpression =
|
||||
@@ -245,7 +252,6 @@
|
||||
map("VariableDeclarator", AST_VarDef, "id>name, init>value");
|
||||
map("CatchClause", AST_Catch, "param>argname, body%body");
|
||||
|
||||
map("ThisExpression", AST_This);
|
||||
map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right");
|
||||
map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right");
|
||||
map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right");
|
||||
@@ -407,6 +413,10 @@
|
||||
};
|
||||
});
|
||||
|
||||
def_to_moz(AST_This, function To_Moz_ThisExpression() {
|
||||
return { type: "ThisExpression" };
|
||||
});
|
||||
|
||||
def_to_moz(AST_RegExp, function To_Moz_RegExpLiteral(M) {
|
||||
var flags = M.value.toString().match(/[gimuy]*$/)[0];
|
||||
var value = "/" + M.value.raw_source + "/" + flags;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/***********************************************************************
|
||||
|
||||
A JavaScript tokenizer / parser / beautifier / compressor.
|
||||
https://github.com/mishoo/UglifyJS2
|
||||
https://github.com/mishoo/UglifyJS
|
||||
|
||||
-------------------------------- (C) ---------------------------------
|
||||
|
||||
@@ -783,8 +783,8 @@ function OutputStream(options) {
|
||||
var p = output.parent();
|
||||
if (p instanceof AST_PropAccess && p.expression === this) {
|
||||
var value = this.value;
|
||||
// https://github.com/mishoo/UglifyJS2/issues/115
|
||||
// https://github.com/mishoo/UglifyJS2/pull/1009
|
||||
// https://github.com/mishoo/UglifyJS/issues/115
|
||||
// https://github.com/mishoo/UglifyJS/pull/1009
|
||||
if (value < 0 || /^0/.test(make_num(value))) {
|
||||
return true;
|
||||
}
|
||||
@@ -1149,7 +1149,7 @@ function OutputStream(options) {
|
||||
function parenthesize_for_noin(node, output, noin) {
|
||||
var parens = false;
|
||||
// need to take some precautions here:
|
||||
// https://github.com/mishoo/UglifyJS2/issues/60
|
||||
// https://github.com/mishoo/UglifyJS/issues/60
|
||||
if (noin) node.walk(new TreeWalker(function(node) {
|
||||
if (parens || node instanceof AST_Scope) return true;
|
||||
if (node instanceof AST_Binary && node.operator == "in") {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/***********************************************************************
|
||||
|
||||
A JavaScript tokenizer / parser / beautifier / compressor.
|
||||
https://github.com/mishoo/UglifyJS2
|
||||
https://github.com/mishoo/UglifyJS
|
||||
|
||||
-------------------------------- (C) ---------------------------------
|
||||
|
||||
@@ -948,7 +948,7 @@ function parse($TEXT, options) {
|
||||
if (!(stat instanceof AST_IterationStatement)) {
|
||||
// check for `continue` that refers to this label.
|
||||
// those should be reported as syntax errors.
|
||||
// https://github.com/mishoo/UglifyJS2/issues/287
|
||||
// https://github.com/mishoo/UglifyJS/issues/287
|
||||
label.references.forEach(function(ref) {
|
||||
if (ref instanceof AST_Continue) {
|
||||
ref = ref.label.start;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/***********************************************************************
|
||||
|
||||
A JavaScript tokenizer / parser / beautifier / compressor.
|
||||
https://github.com/mishoo/UglifyJS2
|
||||
https://github.com/mishoo/UglifyJS
|
||||
|
||||
-------------------------------- (C) ---------------------------------
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/***********************************************************************
|
||||
|
||||
A JavaScript tokenizer / parser / beautifier / compressor.
|
||||
https://github.com/mishoo/UglifyJS2
|
||||
https://github.com/mishoo/UglifyJS
|
||||
|
||||
-------------------------------- (C) ---------------------------------
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/***********************************************************************
|
||||
|
||||
A JavaScript tokenizer / parser / beautifier / compressor.
|
||||
https://github.com/mishoo/UglifyJS2
|
||||
https://github.com/mishoo/UglifyJS
|
||||
|
||||
-------------------------------- (C) ---------------------------------
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/***********************************************************************
|
||||
|
||||
A JavaScript tokenizer / parser / beautifier / compressor.
|
||||
https://github.com/mishoo/UglifyJS2
|
||||
https://github.com/mishoo/UglifyJS
|
||||
|
||||
-------------------------------- (C) ---------------------------------
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/***********************************************************************
|
||||
|
||||
A JavaScript tokenizer / parser / beautifier / compressor.
|
||||
https://github.com/mishoo/UglifyJS2
|
||||
https://github.com/mishoo/UglifyJS
|
||||
|
||||
-------------------------------- (C) ---------------------------------
|
||||
|
||||
@@ -147,6 +147,9 @@ var List = (function() {
|
||||
}
|
||||
return top.concat(ret);
|
||||
}
|
||||
List.is_op = function(val) {
|
||||
return val === skip || val instanceof AtTop || val instanceof Last || val instanceof Splice;
|
||||
};
|
||||
List.at_top = function(val) { return new AtTop(val); };
|
||||
List.splice = function(val) { return new Splice(val); };
|
||||
List.last = function(val) { return new Last(val); };
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"description": "JavaScript parser, mangler/compressor and beautifier toolkit",
|
||||
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
|
||||
"license": "BSD-2-Clause",
|
||||
"version": "3.9.2",
|
||||
"version": "3.9.4",
|
||||
"engines": {
|
||||
"node": ">=0.8.0"
|
||||
},
|
||||
@@ -11,7 +11,7 @@
|
||||
"Alex Lam <alexlamsl@gmail.com>",
|
||||
"Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)"
|
||||
],
|
||||
"repository": "mishoo/UglifyJS2",
|
||||
"repository": "mishoo/UglifyJS",
|
||||
"main": "tools/node.js",
|
||||
"bin": {
|
||||
"uglifyjs": "bin/uglifyjs"
|
||||
|
||||
@@ -8,8 +8,8 @@ var fetch = require("./fetch");
|
||||
var spawn = require("child_process").spawn;
|
||||
var zlib = require("zlib");
|
||||
var args = process.argv.slice(2);
|
||||
args.unshift("bin/uglifyjs");
|
||||
if (!args.length) args.push("-mc");
|
||||
args.unshift("bin/uglifyjs");
|
||||
args.push("--timings");
|
||||
var urls = [
|
||||
"https://code.jquery.com/jquery-3.4.1.js",
|
||||
|
||||
@@ -63,7 +63,7 @@ function make_code(ast, options) {
|
||||
|
||||
function parse_test(file) {
|
||||
var script = fs.readFileSync(file, "utf8");
|
||||
// TODO try/catch can be removed after fixing https://github.com/mishoo/UglifyJS2/issues/348
|
||||
// TODO try/catch can be removed after fixing https://github.com/mishoo/UglifyJS/issues/348
|
||||
try {
|
||||
var ast = U.parse(script, {
|
||||
filename: file
|
||||
@@ -188,6 +188,7 @@ function reminify(orig_options, input_code, input_formatted, stdout) {
|
||||
}
|
||||
});
|
||||
var options_formatted = JSON.stringify(options, null, 4);
|
||||
options.validate = true;
|
||||
var result = U.minify(input_code, options);
|
||||
if (result.error) {
|
||||
log([
|
||||
@@ -251,6 +252,7 @@ function run_code(code, toplevel) {
|
||||
|
||||
function test_case(test) {
|
||||
log(" Running test [{name}]", { name: test.name });
|
||||
U.AST_Node.enable_validation();
|
||||
var output_options = test.beautify || {};
|
||||
var expect;
|
||||
if (test.expect) {
|
||||
|
||||
@@ -7954,3 +7954,157 @@ mangleable_var: {
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3884: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 100, b = 1;
|
||||
{
|
||||
a++ + a || a;
|
||||
b <<= a;
|
||||
}
|
||||
console.log(a, b);
|
||||
}
|
||||
expect: {
|
||||
var a = 100;
|
||||
++a;
|
||||
console.log(a, 32);
|
||||
}
|
||||
expect_stdout: "101 32"
|
||||
}
|
||||
|
||||
issue_3891: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function log(a) {
|
||||
console.log(typeof a);
|
||||
}
|
||||
log(function f() {
|
||||
try {
|
||||
do {
|
||||
var b = function() {}();
|
||||
} while (f = 0, b.p);
|
||||
} catch (e) {
|
||||
var f;
|
||||
b;
|
||||
}
|
||||
});
|
||||
}
|
||||
expect: {
|
||||
function log(a) {
|
||||
console.log(typeof a);
|
||||
}
|
||||
log(function() {
|
||||
try {
|
||||
do {} while ((void 0).p);
|
||||
} catch (e) {}
|
||||
});
|
||||
}
|
||||
expect_stdout: "function"
|
||||
}
|
||||
|
||||
issue_3894: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
inline: true,
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function log(msg) {
|
||||
console.log(msg ? "FAIL" : "PASS");
|
||||
}
|
||||
var a, c;
|
||||
(function(b) {
|
||||
a = c = 0,
|
||||
log(b);
|
||||
})(-0);
|
||||
log(a);
|
||||
log(c);
|
||||
}
|
||||
expect: {
|
||||
function log(msg) {
|
||||
console.log(msg ? "FAIL" : "PASS");
|
||||
}
|
||||
var a, c;
|
||||
void log(-(a = c = 0));
|
||||
log(a);
|
||||
log(c);
|
||||
}
|
||||
expect_stdout: [
|
||||
"PASS",
|
||||
"PASS",
|
||||
"PASS",
|
||||
]
|
||||
}
|
||||
|
||||
issue_3897: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
}
|
||||
input: {
|
||||
var a = 0;
|
||||
(function() {
|
||||
function f(b) {
|
||||
b = a = 1 + a;
|
||||
a = 1 + a;
|
||||
console.log(b);
|
||||
}
|
||||
f();
|
||||
})();
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var a = 0;
|
||||
(function() {
|
||||
function f(b) {
|
||||
b = a = 1 + a;
|
||||
a = 1 + a;
|
||||
console.log(b);
|
||||
}
|
||||
f();
|
||||
})();
|
||||
console.log(a);
|
||||
}
|
||||
expect_stdout: [
|
||||
"1",
|
||||
"2",
|
||||
]
|
||||
}
|
||||
|
||||
issue_3908: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
conditionals: true,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
if (console) {
|
||||
var o = {
|
||||
p: !1
|
||||
}, a = o;
|
||||
}
|
||||
console.log("PASS");
|
||||
}
|
||||
expect: {
|
||||
console && 0;
|
||||
console.log("PASS");
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
@@ -93,6 +93,36 @@ self_comparison_2: {
|
||||
expect_stdout: "false true"
|
||||
}
|
||||
|
||||
self_comparison_3: {
|
||||
options = {
|
||||
comparisons: true,
|
||||
}
|
||||
input: {
|
||||
var a;
|
||||
function f() {
|
||||
var b = a;
|
||||
a = null;
|
||||
return b;
|
||||
}
|
||||
for (var i = 0; i < 2; i++)
|
||||
console.log(f() === f());
|
||||
}
|
||||
expect: {
|
||||
var a;
|
||||
function f() {
|
||||
var b = a;
|
||||
a = null;
|
||||
return b;
|
||||
}
|
||||
for (var i = 0; i < 2; i++)
|
||||
console.log(f() === f());
|
||||
}
|
||||
expect_stdout: [
|
||||
"false",
|
||||
"true",
|
||||
]
|
||||
}
|
||||
|
||||
issue_2857_1: {
|
||||
options = {
|
||||
comparisons: true,
|
||||
|
||||
@@ -1151,3 +1151,45 @@ issue_3830_6: {
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
redundant_assignments: {
|
||||
options = {
|
||||
dead_code: true,
|
||||
}
|
||||
input: {
|
||||
var a = a = "PASS", b = "FAIL";
|
||||
b = b = "PASS";
|
||||
console.log(a, b);
|
||||
}
|
||||
expect: {
|
||||
var a = "PASS", b = "FAIL";
|
||||
b = "PASS";
|
||||
console.log(a, b);
|
||||
}
|
||||
expect_stdout: "PASS PASS"
|
||||
}
|
||||
|
||||
self_assignments: {
|
||||
options = {
|
||||
dead_code: true,
|
||||
}
|
||||
input: {
|
||||
var a = "PASS", b = 0, l = [ "FAIL", "PASS" ], o = { p: "PASS" };
|
||||
a = a;
|
||||
l[0] = l[0];
|
||||
l[b] = l[b];
|
||||
l[b++] = l[b++];
|
||||
o.p = o.p;
|
||||
console.log(a, b, l[0], o.p);
|
||||
}
|
||||
expect: {
|
||||
var a = "PASS", b = 0, l = [ "FAIL", "PASS" ], o = { p: "PASS" };
|
||||
a;
|
||||
l[0];
|
||||
l[b];
|
||||
l[b++] = l[b++];
|
||||
o.p;
|
||||
console.log(a, b, l[0], o.p);
|
||||
}
|
||||
expect_stdout: "PASS 2 PASS PASS"
|
||||
}
|
||||
|
||||
@@ -2366,7 +2366,7 @@ function_parameter_ie8: {
|
||||
}
|
||||
expect: {
|
||||
(function() {
|
||||
(function f() {
|
||||
(function() {
|
||||
console.log("PASS");
|
||||
})();
|
||||
})();
|
||||
@@ -2489,7 +2489,7 @@ drop_duplicated_var_catch: {
|
||||
}
|
||||
}
|
||||
|
||||
issue_3802: {
|
||||
issue_3802_1: {
|
||||
options = {
|
||||
functions: true,
|
||||
reduce_vars: true,
|
||||
@@ -2510,3 +2510,58 @@ issue_3802: {
|
||||
}
|
||||
expect_stdout: "function"
|
||||
}
|
||||
|
||||
issue_3802_2: {
|
||||
options = {
|
||||
functions: true,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 0;
|
||||
a += 0;
|
||||
var a = function() {};
|
||||
console.log(typeof a);
|
||||
}
|
||||
expect: {
|
||||
0;
|
||||
var a = function() {};
|
||||
console.log(typeof a);
|
||||
}
|
||||
expect_stdout: "function"
|
||||
}
|
||||
|
||||
issue_3899: {
|
||||
options = {
|
||||
assignments: true,
|
||||
evaluate: true,
|
||||
functions: true,
|
||||
inline: true,
|
||||
join_vars: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 0;
|
||||
a = a + 1;
|
||||
var a = function f(b) {
|
||||
return function() {
|
||||
return b;
|
||||
};
|
||||
}(2);
|
||||
console.log(typeof a);
|
||||
}
|
||||
expect: {
|
||||
++a;
|
||||
var a = function() {
|
||||
return 2;
|
||||
};
|
||||
console.log(typeof a);
|
||||
}
|
||||
expect_stdout: "function"
|
||||
}
|
||||
|
||||
@@ -2190,3 +2190,298 @@ issue_3755: {
|
||||
}
|
||||
expect_stdout: "undefined"
|
||||
}
|
||||
|
||||
void_side_effects: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = void console.log("PASS");
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
console.log("PASS");
|
||||
console.log(void 0);
|
||||
}
|
||||
expect_stdout: [
|
||||
"PASS",
|
||||
"undefined",
|
||||
]
|
||||
}
|
||||
|
||||
no_returns: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = function() {
|
||||
console.log("PASS");
|
||||
}();
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
(function() {
|
||||
console.log("PASS");
|
||||
})();
|
||||
console.log(void 0);
|
||||
}
|
||||
expect_stdout: [
|
||||
"PASS",
|
||||
"undefined",
|
||||
]
|
||||
}
|
||||
|
||||
void_returns: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = function f() {
|
||||
function g(b) {
|
||||
if (b) console.log("FAIL");
|
||||
}
|
||||
while (1) {
|
||||
console.log("PASS");
|
||||
try {
|
||||
if (console) return;
|
||||
} catch (e) {
|
||||
return g(e);
|
||||
}
|
||||
}
|
||||
}();
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
(function() {
|
||||
function g(b) {
|
||||
if (b) console.log("FAIL");
|
||||
}
|
||||
while (1) {
|
||||
console.log("PASS");
|
||||
try {
|
||||
if (console) return;
|
||||
} catch (e) {
|
||||
return g(e);
|
||||
}
|
||||
}
|
||||
})();
|
||||
console.log(void 0);
|
||||
}
|
||||
expect_stdout: [
|
||||
"PASS",
|
||||
"undefined",
|
||||
]
|
||||
}
|
||||
|
||||
void_returns_recursive: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = function f() {
|
||||
function g(b) {
|
||||
return f();
|
||||
}
|
||||
while (1) {
|
||||
console.log("PASS");
|
||||
try {
|
||||
if (console) return;
|
||||
} catch (e) {
|
||||
return g(e);
|
||||
}
|
||||
}
|
||||
}();
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var a = function f() {
|
||||
function g(b) {
|
||||
return f();
|
||||
}
|
||||
while (1) {
|
||||
console.log("PASS");
|
||||
try {
|
||||
if (console) return;
|
||||
} catch (e) {
|
||||
return g();
|
||||
}
|
||||
}
|
||||
}();
|
||||
console.log(a);
|
||||
}
|
||||
expect_stdout: [
|
||||
"PASS",
|
||||
"undefined",
|
||||
]
|
||||
}
|
||||
|
||||
issue_3878_1: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var b = function(a) {
|
||||
return (a = 0) == (a && this > (a += 0));
|
||||
}();
|
||||
console.log(b ? "PASS" : "FAIL");
|
||||
}
|
||||
expect: {
|
||||
var b = function(a) {
|
||||
return (a = 0) == (a && this > (a += 0));
|
||||
}();
|
||||
console.log(b ? "PASS" : "FAIL");
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3878_2: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
}
|
||||
input: {
|
||||
var a = "foo";
|
||||
a++ + a;
|
||||
a && a;
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var a = "foo";
|
||||
a++ + a;
|
||||
a;
|
||||
console.log(a);
|
||||
}
|
||||
expect_stdout: "NaN"
|
||||
}
|
||||
|
||||
issue_3882: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f(a) {
|
||||
return console.log(a++), a && this;
|
||||
}
|
||||
var b = f();
|
||||
console.log(b);
|
||||
}
|
||||
expect: {
|
||||
var b = function(a) {
|
||||
return console.log(a++), a && this;
|
||||
}();
|
||||
console.log(b);
|
||||
}
|
||||
expect_stdout: [
|
||||
"NaN",
|
||||
"NaN",
|
||||
]
|
||||
}
|
||||
|
||||
issue_3887: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
(function(b) {
|
||||
try {
|
||||
b-- && console.log("PASS");
|
||||
} catch (a_2) {}
|
||||
})(1);
|
||||
}
|
||||
expect: {
|
||||
(function(b) {
|
||||
try {
|
||||
b-- && console.log("PASS");
|
||||
} catch (a_2) {}
|
||||
})(1);
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3903: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = "PASS";
|
||||
function f(b, c) {
|
||||
return console, c;
|
||||
}
|
||||
var d = f(f(), a = a);
|
||||
console.log(d);
|
||||
}
|
||||
expect: {
|
||||
var a = "PASS";
|
||||
function f(b, c) {
|
||||
return console, c;
|
||||
}
|
||||
var d = f(f(), a = a);
|
||||
console.log(d);
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3905: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
passes: 2,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
(function(a, a) {
|
||||
return console.log(a = 0), a && console.log("FAIL");
|
||||
})("foo", 42);
|
||||
}
|
||||
expect: {
|
||||
(function(a, a) {
|
||||
return console.log(a = 0), a && console.log("FAIL");
|
||||
})("foo", 42);
|
||||
}
|
||||
expect_stdout: "0"
|
||||
}
|
||||
|
||||
issue_3920: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = function(b) {
|
||||
return (b[b = 0] = 0) >= (b ? 0 : 1);
|
||||
}("foo");
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var a = function(b) {
|
||||
return (b[b = 0] = 0) >= (b ? 0 : 1);
|
||||
}("foo");
|
||||
console.log(a);
|
||||
}
|
||||
expect_stdout: "false"
|
||||
}
|
||||
|
||||
@@ -3514,10 +3514,9 @@ hoisted_single_use: {
|
||||
}
|
||||
expect: {
|
||||
function f(a) {
|
||||
for (var r in a) g(r);
|
||||
}
|
||||
function g(a) {
|
||||
console.log(a);
|
||||
for (var r in a) (function(a) {
|
||||
console.log(a);
|
||||
})(r);
|
||||
}
|
||||
(function(a) {
|
||||
var g = a.bar;
|
||||
@@ -4660,3 +4659,49 @@ issue_3836: {
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3852: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
inline: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
console.log(function(a) {
|
||||
return function(b) {
|
||||
return b && (b[0] = 0), "PASS";
|
||||
}(a);
|
||||
}(42));
|
||||
}
|
||||
expect: {
|
||||
console.log(function(a) {
|
||||
return a && (a[0] = 0), "PASS";
|
||||
}(42));
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3911: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
conditionals: true,
|
||||
inline: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f() {
|
||||
return function() {
|
||||
if (a) (a++, b += a);
|
||||
f();
|
||||
};
|
||||
}
|
||||
var a = f, b;
|
||||
console.log("PASS");
|
||||
}
|
||||
expect: {
|
||||
console.log("PASS");
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
@@ -912,3 +912,63 @@ issue_3440: {
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3868: {
|
||||
options = {
|
||||
hoist_props: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
}
|
||||
input: {
|
||||
(function(t) {
|
||||
t = {};
|
||||
({
|
||||
get p() {},
|
||||
q: (console.log("PASS"), +t),
|
||||
}).r;
|
||||
})();
|
||||
}
|
||||
expect: {
|
||||
(function(t) {
|
||||
t = {};
|
||||
({
|
||||
get p() {},
|
||||
q: (console.log("PASS"), +t),
|
||||
}).r;
|
||||
})();
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3871: {
|
||||
options = {
|
||||
hoist_props: true,
|
||||
reduce_vars: true,
|
||||
}
|
||||
input: {
|
||||
console.log(function() {
|
||||
do {
|
||||
var b = {
|
||||
get null() {
|
||||
c;
|
||||
}
|
||||
};
|
||||
} while (!b);
|
||||
return "PASS";
|
||||
}());
|
||||
}
|
||||
expect: {
|
||||
console.log(function() {
|
||||
do {
|
||||
var b = {
|
||||
get null() {
|
||||
c;
|
||||
}
|
||||
};
|
||||
} while (!b);
|
||||
return "PASS";
|
||||
}());
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
@@ -2389,7 +2389,7 @@ issue_3703: {
|
||||
var a = "PASS";
|
||||
(function() {
|
||||
var b;
|
||||
var c = function g() {
|
||||
var c = function() {
|
||||
a = "FAIL";
|
||||
};
|
||||
a ? b |= c : b.p;
|
||||
@@ -2460,3 +2460,65 @@ issue_3825: {
|
||||
}
|
||||
expect_stdout: "undefined"
|
||||
}
|
||||
|
||||
issue_3889: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
ie8: true,
|
||||
reduce_vars: true,
|
||||
}
|
||||
input: {
|
||||
function f(a) {
|
||||
a = 0;
|
||||
(function a() {
|
||||
var a;
|
||||
console.log(a);
|
||||
})();
|
||||
}
|
||||
f();
|
||||
}
|
||||
expect: {
|
||||
function f(a) {
|
||||
a = 0;
|
||||
(function a() {
|
||||
var a;
|
||||
console.log(a);
|
||||
})();
|
||||
}
|
||||
f();
|
||||
}
|
||||
expect_stdout: "undefined"
|
||||
}
|
||||
|
||||
issue_3918: {
|
||||
options = {
|
||||
conditionals: true,
|
||||
ie8: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
mangle = {
|
||||
ie8: true,
|
||||
}
|
||||
input: {
|
||||
if (console.log("PASS")) {
|
||||
var a = function f() {
|
||||
f.p;
|
||||
try {
|
||||
console.log("FAIL");
|
||||
} catch (e) {}
|
||||
}, b = a;
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
var a;
|
||||
console.log("PASS") && (a = function f() {
|
||||
f.p;
|
||||
try {
|
||||
console.log("FAIL");
|
||||
} catch (o) {}
|
||||
}, a);
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
@@ -573,3 +573,24 @@ issue_3600: {
|
||||
}
|
||||
expect_stdout: "1"
|
||||
}
|
||||
|
||||
iife_if_return_simple: {
|
||||
options = {
|
||||
conditionals: true,
|
||||
if_return: true,
|
||||
inline: true,
|
||||
sequences: true,
|
||||
side_effects: true,
|
||||
}
|
||||
input: {
|
||||
(function() {
|
||||
if (console)
|
||||
return console.log("PASS");
|
||||
console.log("FAIL");
|
||||
})();
|
||||
}
|
||||
expect: {
|
||||
console ? console.log("PASS") : console.log("FAIL");
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
@@ -35,11 +35,7 @@ f7: {
|
||||
console.log(a, b);
|
||||
}
|
||||
expect_exact: [
|
||||
"var b = 10;",
|
||||
"",
|
||||
"!function() {",
|
||||
" b = 100;",
|
||||
"}(), console.log(100, b);",
|
||||
"console.log(100, 100);",
|
||||
]
|
||||
expect_stdout: true
|
||||
expect_stdout: "100 100"
|
||||
}
|
||||
|
||||
@@ -785,11 +785,12 @@ issue_3791_2: {
|
||||
|
||||
issue_3795: {
|
||||
options = {
|
||||
booleans: true,
|
||||
collapse_vars: true,
|
||||
conditionals: true,
|
||||
dead_code: true,
|
||||
evaluate: true,
|
||||
join_vars: true,
|
||||
keep_fargs: "strict",
|
||||
loops: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
@@ -798,22 +799,21 @@ issue_3795: {
|
||||
}
|
||||
input: {
|
||||
var a = "FAIL";
|
||||
function f(b) {
|
||||
for (var i = 1; b && i; --i) return 0;
|
||||
function f(b, c) {
|
||||
for (var i = 5; c && i; --i) return -1;
|
||||
a = "PASS";
|
||||
}
|
||||
var c = f(a = "");
|
||||
console.log(a);
|
||||
var d = f(a = 42, d);
|
||||
console.log(a, d);
|
||||
}
|
||||
expect: {
|
||||
var a = "FAIL";
|
||||
(function(b) {
|
||||
a = "";
|
||||
var a = "FAIL", d = function() {
|
||||
if (a = 42, d) return -1;
|
||||
a = "PASS";
|
||||
})();
|
||||
console.log(a);
|
||||
}();
|
||||
console.log(a, d);
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
expect_stdout: "PASS undefined"
|
||||
}
|
||||
|
||||
if_body: {
|
||||
@@ -989,3 +989,69 @@ conditional_assignments_3: {
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3856: {
|
||||
options = {
|
||||
booleans: true,
|
||||
conditionals: true,
|
||||
if_return: true,
|
||||
join_vars: true,
|
||||
sequences: true,
|
||||
}
|
||||
input: {
|
||||
console.log(function() {
|
||||
(function() {
|
||||
var a;
|
||||
if (!a) {
|
||||
a = 0;
|
||||
for (var b; !console;);
|
||||
return 0;
|
||||
}
|
||||
if (a) return 1;
|
||||
})();
|
||||
}());
|
||||
}
|
||||
expect: {
|
||||
console.log(function() {
|
||||
(function() {
|
||||
var a, b;
|
||||
if (a) return !!a;
|
||||
for (a = 0; !console;);
|
||||
return 0;
|
||||
})();
|
||||
}());
|
||||
}
|
||||
expect_stdout: "undefined"
|
||||
}
|
||||
|
||||
issue_3916: {
|
||||
options = {
|
||||
join_vars: true,
|
||||
}
|
||||
input: {
|
||||
var o = {};
|
||||
o.p = "PASS";
|
||||
o.__proto__ = 42;
|
||||
o.q = "FAIL";
|
||||
o.__proto__ = {
|
||||
p: "FAIL",
|
||||
q: "PASS",
|
||||
};
|
||||
o.__proto__ = "foo";
|
||||
console.log(typeof o.__proto__, o.p, delete o.q, o.q);
|
||||
}
|
||||
expect: {
|
||||
var o = {
|
||||
p: "PASS",
|
||||
__proto__: 42,
|
||||
q: "FAIL",
|
||||
};
|
||||
o.__proto__ = {
|
||||
p: "FAIL",
|
||||
q: "PASS",
|
||||
};
|
||||
o.__proto__ = "foo";
|
||||
console.log(typeof o.__proto__, o.p, delete o.q, o.q);
|
||||
}
|
||||
expect_stdout: "object PASS true PASS"
|
||||
}
|
||||
|
||||
@@ -680,3 +680,130 @@ issue_3325_2: {
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3858: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
inline: true,
|
||||
keep_fargs: "strict",
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var f = function(a) {
|
||||
return /*@__PURE__*/ function(b) {
|
||||
console.log(b);
|
||||
}(a);
|
||||
};
|
||||
f("PASS");
|
||||
}
|
||||
expect: {
|
||||
var f = function(a) {
|
||||
return function() {
|
||||
console.log(a);
|
||||
}();
|
||||
};
|
||||
f("PASS");
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
inline_pure_call_1: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
inline: true,
|
||||
keep_fargs: "strict",
|
||||
reduce_vars: true,
|
||||
sequences: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var f = function(a) {
|
||||
return /*@__PURE__*/ function(b) {
|
||||
console.log(b);
|
||||
}(a);
|
||||
};
|
||||
f("PASS");
|
||||
}
|
||||
expect: {}
|
||||
}
|
||||
|
||||
inline_pure_call_2: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
inline: true,
|
||||
keep_fargs: "strict",
|
||||
reduce_vars: true,
|
||||
sequences: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var f = function(a) {
|
||||
return /*@__PURE__*/ function(b) {
|
||||
console.log(b);
|
||||
}(a);
|
||||
};
|
||||
var a = f("PASS");
|
||||
}
|
||||
expect: {}
|
||||
}
|
||||
|
||||
inline_pure_call_3: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
evaluate: true,
|
||||
inline: true,
|
||||
keep_fargs: "strict",
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var f = function(a) {
|
||||
return /*@__PURE__*/ function(b) {
|
||||
console.log(b);
|
||||
}(a);
|
||||
};
|
||||
var a = f("PASS");
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var a = function() {
|
||||
console.log("PASS");
|
||||
}();
|
||||
console.log(a);
|
||||
}
|
||||
expect_stdout: [
|
||||
"PASS",
|
||||
"undefined",
|
||||
]
|
||||
}
|
||||
|
||||
inline_pure_call_4: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = /*@__PURE__*/ function() {
|
||||
return console.log("PASS"), 42;
|
||||
}();
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var a = function() {
|
||||
return console.log("PASS"), 42;
|
||||
}();
|
||||
console.log(a);
|
||||
}
|
||||
expect_stdout: [
|
||||
"PASS",
|
||||
"42",
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1197,11 +1197,10 @@ toplevel_on_loops_1: {
|
||||
while (x);
|
||||
}
|
||||
expect: {
|
||||
function bar() {
|
||||
console.log("bar:", --x);
|
||||
}
|
||||
var x = 3;
|
||||
for (;bar(), x;);
|
||||
for (;function() {
|
||||
console.log("bar:", --x);
|
||||
}(), x;);
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
@@ -1254,10 +1253,9 @@ toplevel_on_loops_2: {
|
||||
while (x);
|
||||
}
|
||||
expect: {
|
||||
function bar() {
|
||||
for (;;) (function() {
|
||||
console.log("bar:");
|
||||
}
|
||||
for (;;) bar();
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4231,13 +4229,12 @@ issue_2450_4: {
|
||||
}
|
||||
expect: {
|
||||
var a;
|
||||
function f(b) {
|
||||
console.log(a === b);
|
||||
a = b;
|
||||
}
|
||||
function g() {}
|
||||
for (var i = 3; --i >= 0;)
|
||||
f(g);
|
||||
(function(b) {
|
||||
console.log(a === b);
|
||||
a = b;
|
||||
})(g);
|
||||
}
|
||||
expect_stdout: [
|
||||
"false",
|
||||
@@ -4338,14 +4335,13 @@ perf_1: {
|
||||
console.log(sum);
|
||||
}
|
||||
expect: {
|
||||
function indirect_foo(x, y, z) {
|
||||
return function(x, y, z) {
|
||||
return x < y ? x * y + z : x * z - y;
|
||||
}(x, y, z);
|
||||
}
|
||||
var sum = 0;
|
||||
for (var i = 0; i < 100; ++i)
|
||||
sum += indirect_foo(i, i + 1, 3 * i);
|
||||
sum += function(x, y, z) {
|
||||
return function(x, y, z) {
|
||||
return x < y ? x * y + z : x * z - y;
|
||||
}(x, y, z);
|
||||
}(i, i + 1, 3 * i);
|
||||
console.log(sum);
|
||||
}
|
||||
expect_stdout: "348150"
|
||||
@@ -4406,14 +4402,13 @@ perf_3: {
|
||||
console.log(sum);
|
||||
}
|
||||
expect: {
|
||||
var indirect_foo = function(x, y, z) {
|
||||
return function(x, y, z) {
|
||||
return x < y ? x * y + z : x * z - y;
|
||||
}(x, y, z);
|
||||
}
|
||||
var sum = 0;
|
||||
for (var i = 0; i < 100; ++i)
|
||||
sum += indirect_foo(i, i + 1, 3 * i);
|
||||
sum += function(x, y, z) {
|
||||
return function(x, y, z) {
|
||||
return x < y ? x * y + z : x * z - y;
|
||||
}(x, y, z);
|
||||
}(i, i + 1, 3 * i);
|
||||
console.log(sum);
|
||||
}
|
||||
expect_stdout: "348150"
|
||||
@@ -4475,14 +4470,13 @@ perf_5: {
|
||||
console.log(sum);
|
||||
}
|
||||
expect: {
|
||||
function indirect_foo(x, y, z) {
|
||||
return function(x, y, z) {
|
||||
return x < y ? x * y + z : x * z - y;
|
||||
}(x, y, z);
|
||||
}
|
||||
var sum = 0;
|
||||
for (var i = 0; i < 100; ++i)
|
||||
sum += indirect_foo(i, i + 1, 3 * i);
|
||||
sum += function(x, y, z) {
|
||||
return function(x, y, z) {
|
||||
return x < y ? x * y + z : x * z - y;
|
||||
}(x, y, z);
|
||||
}(i, i + 1, 3 * i);
|
||||
console.log(sum);
|
||||
}
|
||||
expect_stdout: "348150"
|
||||
@@ -4543,14 +4537,13 @@ perf_7: {
|
||||
console.log(sum);
|
||||
}
|
||||
expect: {
|
||||
var indirect_foo = function(x, y, z) {
|
||||
return function(x, y, z) {
|
||||
return x < y ? x * y + z : x * z - y;
|
||||
}(x, y, z);
|
||||
}
|
||||
var sum = 0;
|
||||
for (var i = 0; i < 100; ++i)
|
||||
sum += indirect_foo(i, i + 1, 3 * i);
|
||||
sum += function(x, y, z) {
|
||||
return function(x, y, z) {
|
||||
return x < y ? x * y + z : x * z - y;
|
||||
}(x, y, z);
|
||||
}(i, i + 1, 3 * i);
|
||||
console.log(sum);
|
||||
}
|
||||
expect_stdout: "348150"
|
||||
@@ -6976,3 +6969,156 @@ issue_3774: {
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
flatten_iife: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = "FAIL";
|
||||
while (!console);
|
||||
a++;
|
||||
(function() {
|
||||
while (!console);
|
||||
a = "PASS";
|
||||
})();
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var a;
|
||||
while (!console);
|
||||
0;
|
||||
(function() {
|
||||
while (!console);
|
||||
a = "PASS";
|
||||
})();
|
||||
console.log(a);
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3844: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
}
|
||||
input: {
|
||||
console.log(function() {
|
||||
if (!console) switch (A = 0) {
|
||||
case console.log("FAIL"):
|
||||
return;
|
||||
}
|
||||
return typeof A;
|
||||
}());
|
||||
}
|
||||
expect: {
|
||||
console.log(function() {
|
||||
if (!console) switch (A = 0) {
|
||||
case console.log("FAIL"):
|
||||
return;
|
||||
}
|
||||
return typeof A;
|
||||
}());
|
||||
}
|
||||
expect_stdout: "undefined"
|
||||
}
|
||||
|
||||
issue_3866: {
|
||||
options = {
|
||||
dead_code: true,
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
}
|
||||
input: {
|
||||
console.log(function() {
|
||||
{
|
||||
return "PASS";
|
||||
var a = 0;
|
||||
}
|
||||
return --a;
|
||||
}());
|
||||
}
|
||||
expect: {
|
||||
console.log("PASS");
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3880: {
|
||||
options = {
|
||||
reduce_funcs: true,
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
(function(a) {
|
||||
while (a.var ^= 1);
|
||||
console.log("PASS");
|
||||
})(function() {});
|
||||
}
|
||||
expect: {
|
||||
(function(a) {
|
||||
while (a.var ^= 1);
|
||||
console.log("PASS");
|
||||
})(function() {});
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3894: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
inline: true,
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function log(msg) {
|
||||
console.log(msg ? "FAIL" : "PASS");
|
||||
}
|
||||
var a;
|
||||
(function(b) {
|
||||
a = 0,
|
||||
log(b);
|
||||
})(-0);
|
||||
}
|
||||
expect: {
|
||||
function log(msg) {
|
||||
console.log(msg ? "FAIL" : "PASS");
|
||||
}
|
||||
var a;
|
||||
void log(-(a = 0));
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3922: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
keep_fargs: "strict",
|
||||
pure_getters: "strict",
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
(function(a) {
|
||||
var b;
|
||||
b && b[c];
|
||||
a |= this;
|
||||
console.log("PASS");
|
||||
var c = a.undefined;
|
||||
})();
|
||||
}
|
||||
expect: {
|
||||
(function() {
|
||||
0;
|
||||
console.log("PASS");
|
||||
})();
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
@@ -14,9 +14,9 @@ if (typeof phantom == "undefined") {
|
||||
args.splice(debug, 1);
|
||||
debug = true;
|
||||
}
|
||||
args.unshift("bin/uglifyjs");
|
||||
if (!args.length) args.push("-mcb", "beautify=false,webkit");
|
||||
args.push("--timings");
|
||||
args.unshift("bin/uglifyjs");
|
||||
args.push("--validate", "--timings");
|
||||
var child_process = require("child_process");
|
||||
var fetch = require("./fetch");
|
||||
var http = require("http");
|
||||
|
||||
@@ -24,11 +24,22 @@ function try_beautify(code) {
|
||||
}
|
||||
}
|
||||
|
||||
function test(original, estree, description) {
|
||||
var transformed = UglifyJS.minify(UglifyJS.AST_Node.from_mozilla_ast(estree), {
|
||||
function validate(ast) {
|
||||
try {
|
||||
ast.walk(new UglifyJS.TreeWalker(function(node) {
|
||||
node.validate();
|
||||
}));
|
||||
} catch (e) {
|
||||
return { error: e };
|
||||
}
|
||||
return UglifyJS.minify(ast, {
|
||||
compress: false,
|
||||
mangle: false
|
||||
mangle: false,
|
||||
});
|
||||
}
|
||||
|
||||
function test(original, estree, description) {
|
||||
var transformed = validate(UglifyJS.AST_Node.from_mozilla_ast(estree));
|
||||
if (transformed.error || original !== transformed.code) {
|
||||
console.log("//=============================================================");
|
||||
console.log("// !!!!!! Failed... round", round);
|
||||
|
||||
@@ -4,20 +4,19 @@ var List = U.List;
|
||||
var os = require("os");
|
||||
var sandbox = require("./sandbox");
|
||||
|
||||
// Reduce a ufuzz-style `console.log` based test case by iteratively replacing
|
||||
// AST nodes with various permutations. Each AST_Statement in the tree is also
|
||||
// speculatively dropped to determine whether it is needed. If the altered
|
||||
// tree and the last known good tree produce the same non-nil error-free output
|
||||
// after being run, then the permutation survives to the next generation and
|
||||
// is the basis for subsequent iterations. The test case is reduced as a
|
||||
// consequence of complex expressions being replaced with simpler ones.
|
||||
// This function assumes that the testcase will not result in a parse or
|
||||
// runtime Error. Note that a reduced test case will have different runtime
|
||||
// output - it is not functionally equivalent to the original. The only criteria
|
||||
// is that once the generated reduced test case is run without minification, it
|
||||
// will produce different output from the code minified with `minify_options`.
|
||||
// Returns a `minify` result object with an additonal boolean property `reduced`.
|
||||
// Reduce a test case by iteratively replacing AST nodes with various
|
||||
// permutations. Each AST_Statement in the tree is also speculatively dropped
|
||||
// to determine whether it is needed. If the altered tree and the last known
|
||||
// good tree produce the same output after being run, then the permutation
|
||||
// survives to the next generation and is the basis for subsequent iterations.
|
||||
// The test case is reduced as a consequence of complex expressions being
|
||||
// replaced with simpler ones. Note that a reduced test case will have
|
||||
// different runtime output - it is not functionally equivalent to the
|
||||
// original. The only criteria is that once the generated reduced test case is
|
||||
// run without minification, it will produce different output from the code
|
||||
// minified with `minify_options`. Returns a `minify` result object.
|
||||
|
||||
Error.stackTraceLimit = Infinity;
|
||||
module.exports = function reduce_test(testcase, minify_options, reduce_options) {
|
||||
if (testcase instanceof U.AST_Node) testcase = testcase.print_to_string();
|
||||
minify_options = minify_options || {};
|
||||
@@ -31,12 +30,17 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
|
||||
var verbose = reduce_options.verbose;
|
||||
var minify_options_json = JSON.stringify(minify_options, null, 2);
|
||||
var result_cache = Object.create(null);
|
||||
var test_for_diff = compare_run_code;
|
||||
// the initial timeout to assess the viability of the test case must be large
|
||||
var differs = producesDifferentResultWhenMinified(result_cache, testcase, minify_options, max_timeout);
|
||||
var differs = test_for_diff(testcase, minify_options, result_cache, max_timeout);
|
||||
|
||||
if (verbose) {
|
||||
log("// Node.js " + process.version + " on " + os.platform() + " " + os.arch());
|
||||
}
|
||||
if (differs.error && [ "DefaultsError", "SyntaxError" ].indexOf(differs.error.name) < 0) {
|
||||
test_for_diff = test_minify;
|
||||
differs = test_for_diff(testcase, minify_options, result_cache, max_timeout);
|
||||
}
|
||||
if (!differs) {
|
||||
// same stdout result produced when minified
|
||||
return {
|
||||
@@ -434,7 +438,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
|
||||
}
|
||||
}));
|
||||
var code = testcase_ast.print_to_string();
|
||||
if (diff = producesDifferentResultWhenMinified(result_cache, code, minify_options, max_timeout)) {
|
||||
if (diff = test_for_diff(code, minify_options, result_cache, max_timeout)) {
|
||||
testcase = code;
|
||||
differs = diff;
|
||||
} else {
|
||||
@@ -464,7 +468,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
|
||||
log("*** Discarding permutation and continuing.");
|
||||
continue;
|
||||
}
|
||||
var diff = producesDifferentResultWhenMinified(result_cache, code, minify_options, max_timeout);
|
||||
var diff = test_for_diff(code, minify_options, result_cache, max_timeout);
|
||||
if (diff) {
|
||||
if (diff.timed_out) {
|
||||
// can't trust the validity of `code_ast` and `code` when timed out.
|
||||
@@ -494,21 +498,25 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
|
||||
log("// reduce test pass " + pass + ": " + testcase.length + " bytes");
|
||||
}
|
||||
}
|
||||
testcase = try_beautify(result_cache, testcase, minify_options, differs.unminified_result, max_timeout);
|
||||
testcase = try_beautify(testcase, minify_options, differs.unminified_result, result_cache, max_timeout);
|
||||
var lines = [ "" ];
|
||||
var unminified_result = strip_color_codes(differs.unminified_result);
|
||||
var minified_result = strip_color_codes(differs.minified_result);
|
||||
if (trim_trailing_whitespace(unminified_result) == trim_trailing_whitespace(minified_result)) {
|
||||
lines.push(
|
||||
"// (stringified)",
|
||||
"// output: " + JSON.stringify(unminified_result),
|
||||
"// minify: " + JSON.stringify(minified_result)
|
||||
);
|
||||
if (isNaN(max_timeout)) {
|
||||
lines.push("// minify error: " + to_comment(strip_color_codes(differs.minified_result.stack)));
|
||||
} else {
|
||||
lines.push(
|
||||
"// output: " + to_comment(unminified_result),
|
||||
"// minify: " + to_comment(minified_result)
|
||||
);
|
||||
var unminified_result = strip_color_codes(differs.unminified_result);
|
||||
var minified_result = strip_color_codes(differs.minified_result);
|
||||
if (trim_trailing_whitespace(unminified_result) == trim_trailing_whitespace(minified_result)) {
|
||||
lines.push(
|
||||
"// (stringified)",
|
||||
"// output: " + JSON.stringify(unminified_result),
|
||||
"// minify: " + JSON.stringify(minified_result)
|
||||
);
|
||||
} else {
|
||||
lines.push(
|
||||
"// output: " + to_comment(unminified_result),
|
||||
"// minify: " + to_comment(minified_result)
|
||||
);
|
||||
}
|
||||
}
|
||||
lines.push("// options: " + to_comment(minify_options_json));
|
||||
testcase.code += lines.join("\n");
|
||||
@@ -529,7 +537,7 @@ function trim_trailing_whitespace(value) {
|
||||
return ("" + value).replace(/\s+$/, "");
|
||||
}
|
||||
|
||||
function try_beautify(result_cache, testcase, minify_options, expected, timeout) {
|
||||
function try_beautify(testcase, minify_options, expected, result_cache, timeout) {
|
||||
var result = U.minify(testcase, {
|
||||
compress: false,
|
||||
mangle: false,
|
||||
@@ -543,10 +551,16 @@ function try_beautify(result_cache, testcase, minify_options, expected, timeout)
|
||||
code: testcase,
|
||||
};
|
||||
var toplevel = sandbox.has_toplevel(minify_options);
|
||||
var actual = run_code(result_cache, result.code, toplevel, timeout);
|
||||
if (!sandbox.same_stdout(expected, actual)) return {
|
||||
code: testcase,
|
||||
};
|
||||
if (isNaN(timeout)) {
|
||||
if (!U.minify(result.code, minify_options).error) return {
|
||||
code: testcase,
|
||||
};
|
||||
} else {
|
||||
var actual = run_code(result.code, toplevel, result_cache, timeout);
|
||||
if (!sandbox.same_stdout(expected, actual)) return {
|
||||
code: testcase,
|
||||
};
|
||||
}
|
||||
result.code = "// (beautified)\n" + result.code;
|
||||
return result;
|
||||
}
|
||||
@@ -633,21 +647,21 @@ function wrap_with_console_log(node) {
|
||||
});
|
||||
}
|
||||
|
||||
function run_code(result_cache, code, toplevel, timeout) {
|
||||
function run_code(code, toplevel, result_cache, timeout) {
|
||||
var key = crypto.createHash("sha1").update(code).digest("base64");
|
||||
return result_cache[key] || (result_cache[key] = sandbox.run_code(code, toplevel, timeout));
|
||||
}
|
||||
|
||||
function producesDifferentResultWhenMinified(result_cache, code, minify_options, max_timeout) {
|
||||
function compare_run_code(code, minify_options, result_cache, max_timeout) {
|
||||
var minified = U.minify(code, minify_options);
|
||||
if (minified.error) return minified;
|
||||
|
||||
var toplevel = sandbox.has_toplevel(minify_options);
|
||||
var elapsed = Date.now();
|
||||
var unminified_result = run_code(result_cache, code, toplevel, max_timeout);
|
||||
var unminified_result = run_code(code, toplevel, result_cache, max_timeout);
|
||||
elapsed = Date.now() - elapsed;
|
||||
var timeout = Math.min(100 * elapsed, max_timeout);
|
||||
var minified_result = run_code(result_cache, minified.code, toplevel, timeout);
|
||||
var minified_result = run_code(minified.code, toplevel, result_cache, timeout);
|
||||
|
||||
if (sandbox.same_stdout(unminified_result, minified_result)) {
|
||||
return is_timed_out(unminified_result) && is_timed_out(minified_result) && {
|
||||
@@ -660,4 +674,10 @@ function producesDifferentResultWhenMinified(result_cache, code, minify_options,
|
||||
elapsed: elapsed,
|
||||
};
|
||||
}
|
||||
Error.stackTraceLimit = Infinity;
|
||||
|
||||
function test_minify(code, minify_options) {
|
||||
var minified = U.minify(code, minify_options);
|
||||
return minified.error && {
|
||||
minified_result: minified.error,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -9,6 +9,6 @@ require("./run")([
|
||||
"-mc keep_fargs=false,passes=3,pure_getters,unsafe,unsafe_comps,unsafe_math,unsafe_proto",
|
||||
].map(function(options) {
|
||||
var args = options.split(/ /);
|
||||
args.unshift("test/benchmark.js");
|
||||
args.unshift("test/benchmark.js", "--validate");
|
||||
return args;
|
||||
}));
|
||||
|
||||
@@ -1018,19 +1018,23 @@ function log_suspects(minify_options, component) {
|
||||
if (!options) return;
|
||||
if (typeof options != "object") options = {};
|
||||
var defs = default_options[component];
|
||||
var toplevel = sandbox.has_toplevel(minify_options);
|
||||
var suspects = Object.keys(defs).filter(function(name) {
|
||||
var flip = name == "keep_fargs";
|
||||
if (flip === !(name in options ? options : defs)[name]) {
|
||||
if (flip !== (name in options ? options : defs)[name]) {
|
||||
var m = JSON.parse(JSON.stringify(minify_options));
|
||||
var o = JSON.parse(JSON.stringify(options));
|
||||
o[name] = flip;
|
||||
m[component] = o;
|
||||
m.validate = true;
|
||||
var result = UglifyJS.minify(original_code, m);
|
||||
if (result.error) {
|
||||
if (typeof uglify_code != "string") {
|
||||
return !sandbox.same_stdout(uglify_code, result.error);
|
||||
} else if (result.error) {
|
||||
errorln("Error testing options." + component + "." + name);
|
||||
errorln(result.error);
|
||||
} else {
|
||||
var r = sandbox.run_code(result.code, sandbox.has_toplevel(m));
|
||||
var r = sandbox.run_code(result.code, toplevel);
|
||||
return !sandbox.same_stdout(uglify_result, r);
|
||||
}
|
||||
}
|
||||
@@ -1044,18 +1048,21 @@ function log_suspects(minify_options, component) {
|
||||
}
|
||||
}
|
||||
|
||||
function log_suspects_global(options) {
|
||||
function log_suspects_global(options, toplevel) {
|
||||
var suspects = Object.keys(default_options).filter(function(component) {
|
||||
return typeof default_options[component] != "object";
|
||||
}).filter(function(component) {
|
||||
var m = JSON.parse(options);
|
||||
m[component] = false;
|
||||
m.validate = true;
|
||||
var result = UglifyJS.minify(original_code, m);
|
||||
if (result.error) {
|
||||
if (typeof uglify_code != "string") {
|
||||
return !sandbox.same_stdout(uglify_code, result.error);
|
||||
} else if (result.error) {
|
||||
errorln("Error testing options." + component);
|
||||
errorln(result.error);
|
||||
} else {
|
||||
var r = sandbox.run_code(result.code, sandbox.has_toplevel(m));
|
||||
var r = sandbox.run_code(result.code, toplevel);
|
||||
return !sandbox.same_stdout(uglify_result, r);
|
||||
}
|
||||
});
|
||||
@@ -1074,7 +1081,37 @@ function log(options) {
|
||||
errorln("//=============================================================");
|
||||
if (!ok) errorln("// !!!!!! Failed... round " + round);
|
||||
errorln("// original code");
|
||||
try_beautify(original_code, toplevel, original_result, errorln);
|
||||
var beautified = UglifyJS.minify(original_code, {
|
||||
compress: false,
|
||||
mangle: false,
|
||||
output: {
|
||||
beautify: true,
|
||||
braces: true,
|
||||
},
|
||||
});
|
||||
if (beautified.error) {
|
||||
errorln("// !!! beautify failed !!!");
|
||||
errorln(beautified.error);
|
||||
errorln("//");
|
||||
errorln(original_code);
|
||||
} else {
|
||||
var uglified = UglifyJS.minify(beautified.code, JSON.parse(options));
|
||||
var expected, actual;
|
||||
if (typeof uglify_code != "string" || uglified.error) {
|
||||
expected = uglify_code;
|
||||
actual = uglified.error;
|
||||
} else {
|
||||
expected = uglify_result;
|
||||
actual = sandbox.run_code(uglified.code, toplevel);
|
||||
}
|
||||
if (sandbox.same_stdout(expected, actual)) {
|
||||
errorln("// (beautified)");
|
||||
errorln(beautified.code);
|
||||
} else {
|
||||
errorln("//");
|
||||
errorln(original_code);
|
||||
}
|
||||
}
|
||||
errorln();
|
||||
errorln();
|
||||
errorln("//-------------------------------------------------------------");
|
||||
@@ -1112,12 +1149,12 @@ function log(options) {
|
||||
errorln("minify(options):");
|
||||
errorln(JSON.stringify(JSON.parse(options), null, 2));
|
||||
errorln();
|
||||
if (!ok && typeof uglify_code == "string") {
|
||||
if (!ok) {
|
||||
Object.keys(default_options).filter(function(component) {
|
||||
var defs = default_options[component];
|
||||
return defs && typeof defs == "object";
|
||||
}).forEach(log_suspects.bind(null, JSON.parse(options)));
|
||||
log_suspects_global(options);
|
||||
log_suspects_global(options, toplevel);
|
||||
errorln("!!!!!! Failed... round " + round);
|
||||
}
|
||||
}
|
||||
@@ -1136,11 +1173,50 @@ function fuzzy_match(original, uglified) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function skip_infinite_recursion(orig, toplevel) {
|
||||
var code = orig;
|
||||
var tries = [];
|
||||
var offset = 0;
|
||||
var re = /(?:(?:^|[\s{}):;])try|}\s*catch\s*\(([^)]+)\)|}\s*finally)\s*(?={)/g;
|
||||
var match;
|
||||
while (match = re.exec(code)) {
|
||||
if (/}\s*finally\s*$/.test(match[0])) {
|
||||
tries.shift();
|
||||
continue;
|
||||
}
|
||||
var index = match.index + match[0].length + 1;
|
||||
if (/(?:^|[\s{}):;])try\s*$/.test(match[0])) {
|
||||
tries.unshift({ try: index - offset });
|
||||
continue;
|
||||
}
|
||||
while (tries.length && tries[0].catch) tries.shift();
|
||||
tries[0].catch = index;
|
||||
var insert = "throw " + match[1] + ".ufuzz_skip || (" + match[1] + ".ufuzz_skip = " + tries[0].try + "), " + match[1] + ";";
|
||||
var new_code = code.slice(0, index) + insert + code.slice(index);
|
||||
var result = sandbox.run_code(new_code, toplevel);
|
||||
if (typeof result != "object" || typeof result.name != "string" || typeof result.message != "string") {
|
||||
offset += insert.length;
|
||||
code = new_code;
|
||||
} else if (result.name == "RangeError" && result.message == "Maximum call stack size exceeded") {
|
||||
index = result.ufuzz_skip;
|
||||
return orig.slice(0, index) + 'throw new Error("skipping infinite recursion");' + orig.slice(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var fallback_options = [ JSON.stringify({
|
||||
compress: false,
|
||||
mangle: false
|
||||
}) ];
|
||||
var minify_options = require("./options.json").map(JSON.stringify);
|
||||
var sort_globals = [
|
||||
"Object.keys(this).sort().forEach(function(name) {",
|
||||
" var value = this[name];",
|
||||
" delete this[name];",
|
||||
" this[name] = value;",
|
||||
"});",
|
||||
"",
|
||||
].join("\n");
|
||||
var original_code, original_result, errored;
|
||||
var uglify_code, uglify_result, ok;
|
||||
for (var round = 1; round <= num_iterations; round++) {
|
||||
@@ -1153,12 +1229,18 @@ for (var round = 1; round <= num_iterations; round++) {
|
||||
(errored ? fallback_options : minify_options).forEach(function(options) {
|
||||
var o = JSON.parse(options);
|
||||
var toplevel = sandbox.has_toplevel(o);
|
||||
o.validate = true;
|
||||
uglify_code = UglifyJS.minify(original_code, o);
|
||||
original_result = orig_result[toplevel ? 1 : 0];
|
||||
if (!uglify_code.error) {
|
||||
uglify_code = uglify_code.code;
|
||||
uglify_result = sandbox.run_code(uglify_code, toplevel);
|
||||
ok = sandbox.same_stdout(original_result, uglify_result);
|
||||
// ignore declaration order of global variables
|
||||
if (!ok && !toplevel) {
|
||||
ok = sandbox.same_stdout(sandbox.run_code(sort_globals + original_code), sandbox.run_code(sort_globals + uglify_code));
|
||||
}
|
||||
// ignore numerical imprecision caused by `unsafe_math`
|
||||
if (!ok && typeof uglify_result == "string" && o.compress && o.compress.unsafe_math) {
|
||||
ok = fuzzy_match(original_result, uglify_result);
|
||||
if (!ok) {
|
||||
@@ -1166,11 +1248,17 @@ for (var round = 1; round <= num_iterations; round++) {
|
||||
ok = sandbox.same_stdout(fuzzy_result, uglify_result);
|
||||
}
|
||||
}
|
||||
// ignore difference in depth of termination caused by infinite recursion
|
||||
if (!ok) {
|
||||
var orig_skipped = skip_infinite_recursion(original_code, toplevel);
|
||||
var uglify_skipped = skip_infinite_recursion(uglify_code, toplevel);
|
||||
if (orig_skipped && uglify_skipped) {
|
||||
ok = sandbox.same_stdout(sandbox.run_code(orig_skipped, toplevel), sandbox.run_code(uglify_skipped, toplevel));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
uglify_code = uglify_code.error;
|
||||
if (errored) {
|
||||
ok = uglify_code.name == original_result.name;
|
||||
}
|
||||
ok = errored && uglify_code.name == original_result.name;
|
||||
}
|
||||
if (verbose || (verbose_interval && !(round % INTERVAL_COUNT)) || !ok) log(options);
|
||||
else if (errored) {
|
||||
|
||||
Reference in New Issue
Block a user