2012-08-22 18:28:59 +00:00
|
|
|
/***********************************************************************
|
|
|
|
|
|
|
|
|
|
A JavaScript tokenizer / parser / beautifier / compressor.
|
2020-05-05 13:07:33 +00:00
|
|
|
https://github.com/mishoo/UglifyJS
|
2012-08-22 18:28:59 +00:00
|
|
|
|
|
|
|
|
-------------------------------- (C) ---------------------------------
|
|
|
|
|
|
|
|
|
|
Author: Mihai Bazon
|
|
|
|
|
<mihai.bazon@gmail.com>
|
|
|
|
|
http://mihai.bazon.net/blog
|
|
|
|
|
|
|
|
|
|
Distributed under the BSD license:
|
|
|
|
|
|
2012-08-27 08:01:27 +00:00
|
|
|
Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
|
2012-08-22 18:28:59 +00:00
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
|
are met:
|
|
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
|
copyright notice, this list of conditions and the following
|
|
|
|
|
disclaimer.
|
|
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
|
copyright notice, this list of conditions and the following
|
|
|
|
|
disclaimer in the documentation and/or other materials
|
|
|
|
|
provided with the distribution.
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
|
|
|
|
|
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
|
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
|
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
|
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
|
|
|
|
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
|
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
2012-10-02 09:45:31 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
2012-05-27 11:09:01 +00:00
|
|
|
function characters(str) {
|
|
|
|
|
return str.split("");
|
2018-04-26 20:30:29 +00:00
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
|
|
|
|
|
function member(name, array) {
|
2016-04-07 13:57:30 +00:00
|
|
|
return array.indexOf(name) >= 0;
|
2018-04-26 20:30:29 +00:00
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
function find_if(func, array) {
|
2018-06-21 06:10:37 +00:00
|
|
|
for (var i = array.length; --i >= 0;) if (func(array[i])) return array[i];
|
2018-04-26 20:30:29 +00:00
|
|
|
}
|
2012-05-27 14:25:31 +00:00
|
|
|
|
|
|
|
|
function repeat_string(str, i) {
|
|
|
|
|
if (i <= 0) return "";
|
|
|
|
|
if (i == 1) return str;
|
|
|
|
|
var d = repeat_string(str, i >> 1);
|
|
|
|
|
d += d;
|
2018-04-26 20:30:29 +00:00
|
|
|
return i & 1 ? d + str : d;
|
|
|
|
|
}
|
2012-05-27 14:25:31 +00:00
|
|
|
|
2017-02-26 19:40:54 +00:00
|
|
|
function configure_error_stack(fn) {
|
|
|
|
|
Object.defineProperty(fn.prototype, "stack", {
|
|
|
|
|
get: function() {
|
|
|
|
|
var err = new Error(this.message);
|
|
|
|
|
err.name = this.name;
|
|
|
|
|
try {
|
|
|
|
|
throw err;
|
2019-04-29 22:32:24 +00:00
|
|
|
} catch (e) {
|
2017-02-26 19:40:54 +00:00
|
|
|
return e.stack;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-02 08:00:47 +00:00
|
|
|
function DefaultsError(msg, defs) {
|
2017-02-26 19:40:54 +00:00
|
|
|
this.message = msg;
|
2012-10-02 08:00:47 +00:00
|
|
|
this.defs = defs;
|
2018-04-26 20:30:29 +00:00
|
|
|
}
|
2013-12-10 18:24:27 +00:00
|
|
|
DefaultsError.prototype = Object.create(Error.prototype);
|
|
|
|
|
DefaultsError.prototype.constructor = DefaultsError;
|
2017-02-26 19:40:54 +00:00
|
|
|
DefaultsError.prototype.name = "DefaultsError";
|
|
|
|
|
configure_error_stack(DefaultsError);
|
2012-10-02 08:00:47 +00:00
|
|
|
|
|
|
|
|
function defaults(args, defs, croak) {
|
2020-04-18 10:03:06 +00:00
|
|
|
if (croak) for (var i in args) {
|
|
|
|
|
if (HOP(args, i) && !HOP(defs, i)) throw new DefaultsError("`" + i + "` is not a supported option", defs);
|
|
|
|
|
}
|
|
|
|
|
for (var i in args) {
|
|
|
|
|
if (HOP(args, i)) defs[i] = args[i];
|
2018-04-26 20:30:29 +00:00
|
|
|
}
|
2020-04-17 07:13:49 +00:00
|
|
|
return defs;
|
2018-04-26 20:30:29 +00:00
|
|
|
}
|
2012-08-19 12:57:50 +00:00
|
|
|
|
2012-09-26 10:04:54 +00:00
|
|
|
function merge(obj, ext) {
|
Add property name mangler
We only touch properties that are present in an object literal, or which are
assigned to. Example:
x = { foo: 1 };
x.bar = 2;
x["baz"] = 3;
x[cond ? "qwe" : "asd"] = 4;
console.log(x.stuff);
The names "foo", "bar", "baz", "qwe" and "asd" will be mangled, and the
resulting mangled names will be used for the same properties throughout the
code. The "stuff" will not be, since it's just referenced but never
assigned to.
This *will* break most of the code out there, but could work on carefully
written code: do not use eval, do not define methods or properties by
walking an array of names, etc. Also, a comprehensive list of exclusions
needs to be passed, to avoid mangling properties that are standard in
JavaScript, DOM, used in external libraries etc.
2015-03-14 09:22:28 +00:00
|
|
|
var count = 0;
|
2016-04-07 10:15:28 +00:00
|
|
|
for (var i in ext) if (HOP(ext, i)) {
|
2012-09-26 10:04:54 +00:00
|
|
|
obj[i] = ext[i];
|
Add property name mangler
We only touch properties that are present in an object literal, or which are
assigned to. Example:
x = { foo: 1 };
x.bar = 2;
x["baz"] = 3;
x[cond ? "qwe" : "asd"] = 4;
console.log(x.stuff);
The names "foo", "bar", "baz", "qwe" and "asd" will be mangled, and the
resulting mangled names will be used for the same properties throughout the
code. The "stuff" will not be, since it's just referenced but never
assigned to.
This *will* break most of the code out there, but could work on carefully
written code: do not use eval, do not define methods or properties by
walking an array of names, etc. Also, a comprehensive list of exclusions
needs to be passed, to avoid mangling properties that are standard in
JavaScript, DOM, used in external libraries etc.
2015-03-14 09:22:28 +00:00
|
|
|
count++;
|
2012-09-26 10:04:54 +00:00
|
|
|
}
|
Add property name mangler
We only touch properties that are present in an object literal, or which are
assigned to. Example:
x = { foo: 1 };
x.bar = 2;
x["baz"] = 3;
x[cond ? "qwe" : "asd"] = 4;
console.log(x.stuff);
The names "foo", "bar", "baz", "qwe" and "asd" will be mangled, and the
resulting mangled names will be used for the same properties throughout the
code. The "stuff" will not be, since it's just referenced but never
assigned to.
This *will* break most of the code out there, but could work on carefully
written code: do not use eval, do not define methods or properties by
walking an array of names, etc. Also, a comprehensive list of exclusions
needs to be passed, to avoid mangling properties that are standard in
JavaScript, DOM, used in external libraries etc.
2015-03-14 09:22:28 +00:00
|
|
|
return count;
|
2018-04-26 20:30:29 +00:00
|
|
|
}
|
2012-09-26 10:04:54 +00:00
|
|
|
|
2017-03-15 10:44:13 +00:00
|
|
|
function noop() {}
|
2016-10-26 15:34:30 +00:00
|
|
|
function return_false() { return false; }
|
|
|
|
|
function return_true() { return true; }
|
2017-03-15 10:44:13 +00:00
|
|
|
function return_this() { return this; }
|
|
|
|
|
function return_null() { return null; }
|
2012-08-19 12:57:50 +00:00
|
|
|
|
2020-02-06 18:46:25 +00:00
|
|
|
var List = (function() {
|
2020-08-25 02:10:56 +00:00
|
|
|
function List(a, f) {
|
|
|
|
|
var ret = [];
|
|
|
|
|
for (var i = 0; i < a.length; i++) {
|
2012-09-16 12:46:20 +00:00
|
|
|
var val = f(a[i], i);
|
2020-08-25 02:10:56 +00:00
|
|
|
if (val === skip) continue;
|
|
|
|
|
if (val instanceof Splice) {
|
|
|
|
|
ret.push.apply(ret, val.v);
|
2012-09-16 12:46:20 +00:00
|
|
|
} else {
|
2020-08-25 02:10:56 +00:00
|
|
|
ret.push(val);
|
2012-09-16 12:46:20 +00:00
|
|
|
}
|
2012-09-12 13:10:03 +00:00
|
|
|
}
|
2020-08-25 02:10:56 +00:00
|
|
|
return ret;
|
2018-06-06 09:50:56 +00:00
|
|
|
}
|
2020-05-09 01:58:03 +00:00
|
|
|
List.is_op = function(val) {
|
2020-08-25 02:10:56 +00:00
|
|
|
return val === skip || val instanceof Splice;
|
|
|
|
|
};
|
|
|
|
|
List.splice = function(val) {
|
|
|
|
|
return new Splice(val);
|
2020-05-09 01:58:03 +00:00
|
|
|
};
|
2020-02-06 18:46:25 +00:00
|
|
|
var skip = List.skip = {};
|
2020-08-25 02:10:56 +00:00
|
|
|
function Splice(val) {
|
|
|
|
|
this.v = val;
|
|
|
|
|
}
|
2020-02-06 18:46:25 +00:00
|
|
|
return List;
|
2012-08-19 12:57:50 +00:00
|
|
|
})();
|
2012-08-19 19:46:00 +00:00
|
|
|
|
2012-08-20 14:19:30 +00:00
|
|
|
function push_uniq(array, el) {
|
2018-07-12 17:51:10 +00:00
|
|
|
if (array.indexOf(el) < 0) return array.push(el);
|
2018-04-26 20:30:29 +00:00
|
|
|
}
|
2012-08-20 14:19:30 +00:00
|
|
|
|
|
|
|
|
function string_template(text, props) {
|
2020-10-27 09:39:33 +00:00
|
|
|
return text.replace(/\{([^}]+)\}/g, function(str, p) {
|
|
|
|
|
var value = props[p];
|
|
|
|
|
return value instanceof AST_Node ? value.print_to_string() : value;
|
2012-08-20 14:19:30 +00:00
|
|
|
});
|
2018-04-26 20:30:29 +00:00
|
|
|
}
|
2012-09-11 12:42:28 +00:00
|
|
|
|
2012-10-18 12:14:57 +00:00
|
|
|
function remove(array, el) {
|
2018-07-19 06:45:36 +00:00
|
|
|
var index = array.indexOf(el);
|
|
|
|
|
if (index >= 0) array.splice(index, 1);
|
2018-04-26 20:30:29 +00:00
|
|
|
}
|
2012-09-23 09:47:34 +00:00
|
|
|
|
2012-10-11 08:52:05 +00:00
|
|
|
function makePredicate(words) {
|
2018-04-03 07:15:01 +00:00
|
|
|
if (!Array.isArray(words)) words = words.split(" ");
|
|
|
|
|
var map = Object.create(null);
|
|
|
|
|
words.forEach(function(word) {
|
|
|
|
|
map[word] = true;
|
|
|
|
|
});
|
|
|
|
|
return map;
|
|
|
|
|
}
|
2012-11-02 08:58:45 +00:00
|
|
|
|
2013-05-14 15:36:31 +00:00
|
|
|
function all(array, predicate) {
|
|
|
|
|
for (var i = array.length; --i >= 0;)
|
2020-03-20 16:55:24 +00:00
|
|
|
if (!predicate(array[i], i))
|
2013-05-14 15:36:31 +00:00
|
|
|
return false;
|
|
|
|
|
return true;
|
2018-04-26 20:30:29 +00:00
|
|
|
}
|
2013-05-14 15:36:31 +00:00
|
|
|
|
2012-11-02 08:58:45 +00:00
|
|
|
function Dictionary() {
|
|
|
|
|
this._values = Object.create(null);
|
2012-11-06 09:39:41 +00:00
|
|
|
this._size = 0;
|
2018-04-26 20:30:29 +00:00
|
|
|
}
|
2012-11-02 08:58:45 +00:00
|
|
|
Dictionary.prototype = {
|
2012-11-06 09:39:41 +00:00
|
|
|
set: function(key, val) {
|
|
|
|
|
if (!this.has(key)) ++this._size;
|
|
|
|
|
this._values["$" + key] = val;
|
|
|
|
|
return this;
|
|
|
|
|
},
|
2012-12-05 10:30:25 +00:00
|
|
|
add: function(key, val) {
|
|
|
|
|
if (this.has(key)) {
|
|
|
|
|
this.get(key).push(val);
|
|
|
|
|
} else {
|
|
|
|
|
this.set(key, [ val ]);
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
},
|
2012-11-02 08:58:45 +00:00
|
|
|
get: function(key) { return this._values["$" + key] },
|
2012-11-06 09:39:41 +00:00
|
|
|
del: function(key) {
|
|
|
|
|
if (this.has(key)) {
|
|
|
|
|
--this._size;
|
|
|
|
|
delete this._values["$" + key];
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
},
|
2012-11-02 08:58:45 +00:00
|
|
|
has: function(key) { return ("$" + key) in this._values },
|
2020-04-11 11:54:26 +00:00
|
|
|
all: function(predicate) {
|
|
|
|
|
for (var i in this._values)
|
|
|
|
|
if (!predicate(this._values[i], i.substr(1)))
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
2012-11-02 08:58:45 +00:00
|
|
|
each: function(f) {
|
|
|
|
|
for (var i in this._values)
|
|
|
|
|
f(this._values[i], i.substr(1));
|
|
|
|
|
},
|
2012-11-06 09:39:41 +00:00
|
|
|
size: function() {
|
|
|
|
|
return this._size;
|
|
|
|
|
},
|
2012-11-02 08:58:45 +00:00
|
|
|
map: function(f) {
|
|
|
|
|
var ret = [];
|
|
|
|
|
for (var i in this._values)
|
|
|
|
|
ret.push(f(this._values[i], i.substr(1)));
|
|
|
|
|
return ret;
|
Add property name mangler
We only touch properties that are present in an object literal, or which are
assigned to. Example:
x = { foo: 1 };
x.bar = 2;
x["baz"] = 3;
x[cond ? "qwe" : "asd"] = 4;
console.log(x.stuff);
The names "foo", "bar", "baz", "qwe" and "asd" will be mangled, and the
resulting mangled names will be used for the same properties throughout the
code. The "stuff" will not be, since it's just referenced but never
assigned to.
This *will* break most of the code out there, but could work on carefully
written code: do not use eval, do not define methods or properties by
walking an array of names, etc. Also, a comprehensive list of exclusions
needs to be passed, to avoid mangling properties that are standard in
JavaScript, DOM, used in external libraries etc.
2015-03-14 09:22:28 +00:00
|
|
|
},
|
2018-01-17 13:33:13 +00:00
|
|
|
clone: function() {
|
|
|
|
|
var ret = new Dictionary();
|
|
|
|
|
for (var i in this._values)
|
|
|
|
|
ret._values[i] = this._values[i];
|
|
|
|
|
ret._size = this._size;
|
|
|
|
|
return ret;
|
|
|
|
|
},
|
Add property name mangler
We only touch properties that are present in an object literal, or which are
assigned to. Example:
x = { foo: 1 };
x.bar = 2;
x["baz"] = 3;
x[cond ? "qwe" : "asd"] = 4;
console.log(x.stuff);
The names "foo", "bar", "baz", "qwe" and "asd" will be mangled, and the
resulting mangled names will be used for the same properties throughout the
code. The "stuff" will not be, since it's just referenced but never
assigned to.
This *will* break most of the code out there, but could work on carefully
written code: do not use eval, do not define methods or properties by
walking an array of names, etc. Also, a comprehensive list of exclusions
needs to be passed, to avoid mangling properties that are standard in
JavaScript, DOM, used in external libraries etc.
2015-03-14 09:22:28 +00:00
|
|
|
toObject: function() { return this._values }
|
|
|
|
|
};
|
|
|
|
|
Dictionary.fromObject = function(obj) {
|
|
|
|
|
var dict = new Dictionary();
|
|
|
|
|
dict._size = merge(dict._values, obj);
|
|
|
|
|
return dict;
|
2012-11-02 08:58:45 +00:00
|
|
|
};
|
2016-04-07 10:15:28 +00:00
|
|
|
|
|
|
|
|
function HOP(obj, prop) {
|
|
|
|
|
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
|
|
|
}
|
2017-02-18 11:11:57 +00:00
|
|
|
|
|
|
|
|
// return true if the node at the top of the stack (that means the
|
|
|
|
|
// innermost node in the current output) is lexically the first in
|
|
|
|
|
// a statement.
|
|
|
|
|
function first_in_statement(stack) {
|
|
|
|
|
var node = stack.parent(-1);
|
2018-04-26 20:30:29 +00:00
|
|
|
for (var i = 0, p; p = stack.parent(i++); node = p) {
|
|
|
|
|
if (p.TYPE == "Call") {
|
|
|
|
|
if (p.expression === node) continue;
|
|
|
|
|
} else if (p instanceof AST_Binary) {
|
|
|
|
|
if (p.left === node) continue;
|
|
|
|
|
} else if (p instanceof AST_Conditional) {
|
|
|
|
|
if (p.condition === node) continue;
|
|
|
|
|
} else if (p instanceof AST_PropAccess) {
|
|
|
|
|
if (p.expression === node) continue;
|
|
|
|
|
} else if (p instanceof AST_Sequence) {
|
|
|
|
|
if (p.expressions[0] === node) continue;
|
|
|
|
|
} else if (p instanceof AST_Statement) {
|
|
|
|
|
return p.body === node;
|
|
|
|
|
} else if (p instanceof AST_UnaryPostfix) {
|
|
|
|
|
if (p.expression === node) continue;
|
2017-02-18 11:11:57 +00:00
|
|
|
}
|
2018-04-26 20:30:29 +00:00
|
|
|
return false;
|
2017-02-18 11:11:57 +00:00
|
|
|
}
|
|
|
|
|
}
|