Skip to content

Commit

Permalink
Merge branch 'master' into harmony-v3.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Sep 10, 2017
2 parents 2779a29 + cd27f4e commit 8891495
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 47 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,19 @@ debugging your compressed JavaScript. To get a source map, pass

Additional options:

- `--source-map filename=<NAME>` to specify the name of the source map.
- `--source-map "filename='<NAME>'"` to specify the name of the source map.

- `--source-map root=<URL>` to pass the URL where the original files can be found.
- `--source-map "root='<URL>'"` to pass the URL where the original files can be found.
Otherwise UglifyJS assumes HTTP `X-SourceMap` is being used and will omit the
`//# sourceMappingURL=` directive.

- `--source-map url=<URL>` to specify the URL where the source map can be found.
- `--source-map "url='<URL>'"` to specify the URL where the source map can be found.

For example:

uglifyjs js/file1.js js/file2.js \
-o foo.min.js -c -m \
--source-map root="http://foo.com/src",url=foo.min.js.map
--source-map "root='http://foo.com/src',url='foo.min.js.map'"

The above will compress and mangle `file1.js` and `file2.js`, will drop the
output in `foo.min.js` and the source map in `foo.min.js.map`. The source
Expand All @@ -179,8 +179,8 @@ CoffeeScript → compiled JS, UglifyJS can generate a map from CoffeeScript →
compressed JS by mapping every token in the compiled JS to its original
location.

To use this feature pass `--source-map content="/path/to/input/source.map"`
or `--source-map content=inline` if the source map is included inline with
To use this feature pass `--source-map "content='/path/to/input/source.map'"`
or `--source-map "content=inline"` if the source map is included inline with
the sources.

## CLI compress options
Expand Down Expand Up @@ -211,7 +211,7 @@ When mangling is enabled but you want to prevent certain names from being
mangled, you can declare those names with `--mangle reserved` — pass a
comma-separated list of names. For example:

uglifyjs ... -m reserved=[$,require,exports]
uglifyjs ... -m reserved=['$','require','exports']

to prevent the `require`, `exports` and `$` names from being changed.

Expand Down
35 changes: 24 additions & 11 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ merge(Compressor.prototype, {
return node instanceof AST_SymbolRef && node.definition().undeclared;
}

var global_names = makePredicate("Array Boolean console Error Function Math Number RegExp Object String");
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.definition().undeclared
|| compressor.option("unsafe") && global_names(this.name);
Expand Down Expand Up @@ -927,7 +927,7 @@ merge(Compressor.prototype, {
}
} else {
var arg = iife.args[i];
if (!arg) arg = make_node(AST_Undefined, sym);
if (!arg) arg = make_node(AST_Undefined, sym).transform(compressor);
else if (has_overlapping_symbol(fn, arg)) arg = null;
if (arg) candidates.unshift(make_node(AST_VarDef, sym, {
name: sym,
Expand Down Expand Up @@ -2081,16 +2081,27 @@ merge(Compressor.prototype, {
if (!compressor.option("side_effects")) return false;
if (this.pure !== undefined) return this.pure;
var pure = false;
var comments, last_comment;
var comments, pure_comment;
if (this.start
&& (comments = this.start.comments_before)
&& comments.length
&& /[@#]__PURE__/.test((last_comment = comments[comments.length - 1]).value)) {
pure = last_comment;
&& (pure_comment = find_if(function (comment) {
return /[@#]__PURE__/.test(comment.value);
}, comments))) {
pure = pure_comment;
}
return this.pure = pure;
});

var global_pure_fns = makePredicate("Boolean decodeURI decodeURIComponent Date encodeURI encodeURIComponent Error escape EvalError isFinite isNaN Number Object parseFloat parseInt RangeError ReferenceError String SyntaxError TypeError unescape URIError");
AST_Call.DEFMETHOD("is_expr_pure", function(compressor) {
if (compressor.option("unsafe")) {
var expr = this.expression;
if (is_undeclared_ref(expr) && global_pure_fns(expr.name)) return true;
}
return this.has_pure_annotation(compressor) || !compressor.pure_funcs(this);
});

// determine if expression has side effects
(function(def){
def(AST_Node, return_true);
Expand All @@ -2100,7 +2111,7 @@ merge(Compressor.prototype, {
def(AST_This, return_false);

def(AST_Call, function(compressor){
if (!this.has_pure_annotation(compressor) && compressor.pure_funcs(this)) return true;
if (!this.is_expr_pure(compressor)) return true;
for (var i = this.args.length; --i >= 0;) {
if (this.args[i].has_side_effects(compressor))
return true;
Expand Down Expand Up @@ -2570,17 +2581,18 @@ merge(Compressor.prototype, {
// We fix it at this stage by moving the `var` outside the `for`.
if (node instanceof AST_For) {
descend(node, this);
var block;
if (node.init instanceof AST_BlockStatement) {
var block = node.init;
block = node.init;
node.init = block.body.pop();
block.body.push(node);
return in_list ? MAP.splice(block.body) : block;
} else if (node.init instanceof AST_SimpleStatement) {
}
if (node.init instanceof AST_SimpleStatement) {
node.init = node.init.body;
} else if (is_empty(node.init)) {
node.init = null;
}
return node;
return !block ? node : in_list ? MAP.splice(block.body) : block;
}
if (node instanceof AST_LabeledStatement && node.body instanceof AST_For) {
descend(node, this);
Expand Down Expand Up @@ -2776,7 +2788,7 @@ merge(Compressor.prototype, {
def(AST_Constant, return_null);
def(AST_This, return_null);
def(AST_Call, function(compressor, first_in_statement){
if (!this.has_pure_annotation(compressor) && compressor.pure_funcs(this)) {
if (!this.is_expr_pure(compressor)) {
if (is_func_expr(this.expression)
&& (!this.expression.name || !this.expression.name.definition().references.length)) {
var node = this.clone();
Expand Down Expand Up @@ -3286,6 +3298,7 @@ merge(Compressor.prototype, {
});
a.push(var_);
}
remove(def.name.definition().orig, def.name);
return a;
}, []);
if (assignments.length == 0) return null;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"homepage": "https://github.com/mishoo/UglifyJS2/tree/harmony",
"author": "Mihai Bazon <[email protected]> (http://lisperator.net/)",
"license": "BSD-2-Clause",
"version": "3.0.28",
"version": "3.1.0",
"engines": {
"node": ">=0.8.0"
},
Expand All @@ -31,8 +31,8 @@
},
"devDependencies": {
"acorn": "~5.1.1",
"mocha": "~3.4.2",
"semver": "~5.3.0"
"mocha": "~3.5.1",
"semver": "~5.4.1"
},
"scripts": {
"test": "node test/run-tests.js"
Expand Down
42 changes: 42 additions & 0 deletions test/compress/collapse_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -2616,3 +2616,45 @@ issue_2250_2: {
bar();
}
}

issue_2298: {
options = {
collapse_vars: true,
reduce_vars: true,
unused: true,
}
input: {
!function() {
function f() {
var a = undefined;
var undefined = a++;
try {
!function g(b) {
b[1] = "foo";
}();
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
f();
}();
}
expect: {
!function() {
(function() {
var a = undefined;
var undefined = a++;
try {
!function(b) {
(void 0)[1] = "foo";
}();
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
})();
}();
}
expect_stdout: "PASS"
}
104 changes: 104 additions & 0 deletions test/compress/dead-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,18 +388,54 @@ issue_2233_1: {
Array.isArray;
Boolean;
console.log;
Date;
decodeURI;
decodeURIComponent;
encodeURI;
encodeURIComponent;
Error.name;
escape;
eval;
EvalError;
Function.length;
isFinite;
isNaN;
JSON;
Math.random;
Number.isNaN;
parseFloat;
parseInt;
RegExp;
Object.defineProperty;
String.fromCharCode;
RangeError;
ReferenceError;
SyntaxError;
TypeError;
unescape;
URIError;
}
expect: {}
expect_stdout: true
}

global_timeout_and_interval_symbols: {
options = {
pure_getters: "strict",
side_effects: true,
unsafe: true,
}
input: {
// These global symbols do not exist in the test sandbox
// and must be tested separately.
clearInterval;
clearTimeout;
setInterval;
setTimeout;
}
expect: {}
}

issue_2233_2: {
options = {
pure_getters: "strict",
Expand Down Expand Up @@ -456,3 +492,71 @@ issue_2233_3: {
UndeclaredGlobal;
}
}

global_fns: {
options = {
side_effects: true,
unsafe: true,
}
input: {
Boolean(1, 2);
decodeURI(1, 2);
decodeURIComponent(1, 2);
Date(1, 2);
encodeURI(1, 2);
encodeURIComponent(1, 2);
Error(1, 2);
escape(1, 2);
EvalError(1, 2);
isFinite(1, 2);
isNaN(1, 2);
Number(1, 2);
Object(1, 2);
parseFloat(1, 2);
parseInt(1, 2);
RangeError(1, 2);
ReferenceError(1, 2);
String(1, 2);
SyntaxError(1, 2);
TypeError(1, 2);
unescape(1, 2);
URIError(1, 2);
try {
Function(1, 2);
} catch (e) {
console.log(e.name);
}
try {
RegExp(1, 2);
} catch (e) {
console.log(e.name);
}
try {
Array(NaN);
} catch (e) {
console.log(e.name);
}
}
expect: {
try {
Function(1, 2);
} catch (e) {
console.log(e.name);
}
try {
RegExp(1, 2);
} catch (e) {
console.log(e.name);
}
try {
Array(NaN);
} catch (e) {
console.log(e.name);
}
}
expect_stdout: [
"SyntaxError",
"SyntaxError",
"RangeError",
]
}
22 changes: 22 additions & 0 deletions test/compress/drop-unused.js
Original file line number Diff line number Diff line change
Expand Up @@ -1513,3 +1513,25 @@ issue_2226_3: {
}
expect_stdout: "3"
}

issue_2288: {
options = {
unused: true,
}
beautify = {
beautify: true,
}
input: {
function foo(o) {
for (var j = o.a, i = 0; i < 0; i++);
for (var i = 0; i < 0; i++);
}
}
expect: {
function foo(o) {
o.a;
for (i = 0; i < 0; i++);
for (var i = 0; i < 0; i++);
}
}
}
21 changes: 21 additions & 0 deletions test/compress/hoist_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,24 @@ sequences_funs: {
}
}
}

issue_2295: {
options = {
collapse_vars: true,
hoist_vars: true,
}
input: {
function foo(o) {
var a = o.a;
if (a) return a;
var a = 1;
}
}
expect: {
function foo(o) {
var a = o.a;
if (a) return a;
a = 1;
}
}
}
Loading

0 comments on commit 8891495

Please sign in to comment.