Compare commits

..

57 Commits

Author SHA1 Message Date
Richard van Velzen
9676167aac v2.6.4 2016-06-22 12:24:31 +02:00
Mihai Bazon
1840a0b282 Merge pull request #1155 from kzc/issue_1154
Fix conditional expressions of form (x ? -1 : -1)
2016-06-21 23:14:05 +03:00
kzc
ace8aaa0f4 Fix conditional expressions of form (x ? -1 : -1)
Fixes #1154, #1153
2016-06-21 14:52:13 -04:00
kzc
0c003c92a8 Don't replace undefined, NaN and Infinity within with scope 2016-06-21 10:53:29 +02:00
Anthony Van de Gejuchte
85fbf86d7b Keep master in sync with harmony
* Do not mangle when no mangle is required
 * Improve use_asm reset while printing code
2016-06-20 18:42:17 +02:00
Richard van Velzen
aa82027a17 Don't assume DEBUG is defined when exporting --self
Potential fix for #1148
2016-06-20 08:40:45 +02:00
Richard van Velzen
55c592dd43 v2.6.3 2016-06-19 21:56:06 +02:00
Asia
fc1abd1c11 Document the except option to mangle
Added documentation for the `except` option to the `mangle` option in the API reference.
2016-06-19 21:17:31 +02:00
Shrey Banga
e645ba84cf Respect quote style in object literals
The option added in fbbaa42ee5 wasn't
being respected inside object literals, so quoted property names would
still be stripped out with this option.

This is mostly a corner-case, but useful when the output is passed to
something like the Closure compiler, where quoted property names can be
used to prevent mangling.
2016-06-19 21:13:31 +02:00
Anthony Van de Gejuchte
6c99816855 Normalize error messages 2016-06-19 21:08:34 +02:00
Anthony Van de Gejuchte
2149bfb707 Don't mix strings with directives in output
* Don't interpret strings with escaped content as directive
 * Don't interpret strings after empty statement as directive
 * Adapt output to prevent strings being represent as directive
 * Introduce UGLIFY_DEBUG to allow internal testing like EXPECT_DIRECTIVE
2016-06-19 20:59:17 +02:00
Anthony Van de Gejuchte
d7971ba0e4 Fix test262 failures related to <, <=, in and instanceof
Fixed-by: @kzc
2016-06-15 23:11:08 +02:00
Anthony Van de Gejuchte
5c4cfaa0a7 Re-add parens after new expression in beautify mode 2016-06-12 20:03:48 +02:00
Anthony Van de Gejuchte
bb9c9707aa Don't allow with statements in strict mode 2016-06-12 19:08:16 +02:00
Anthony Van de Gejuchte
6c8e001fee Stop dropping args in new expressions 2016-06-12 17:17:17 +02:00
Richard van Velzen
9c53c7ada7 Fix octal string strict mode tests 2016-06-12 14:35:43 +02:00
David Bau
f99b7b630d Escape null characters as \0 unless followed by 0-7. 2016-06-12 14:32:32 +02:00
Anthony Van de Gejuchte
ea31da2455 Don't drop unused if scope uses with statement
Fix provided by @kzc
2016-06-12 14:30:28 +02:00
Anthony Van de Gejuchte
4d7746baf3 Throw errors in strict mode for octal strings
Adds a directive tracker for the parser/tokenizer to
allow parsing depending on directive context.
2016-06-12 14:27:08 +02:00
Anthony Van de Gejuchte
31d5825a86 Catch errors when compression test fails to parse 2016-06-09 21:12:15 +02:00
Anthony Van de Gejuchte
8287ef6781 Fix uglify attempting to rewrite invalid new expressions 2016-06-08 19:45:21 +02:00
ChALkeR
5cb5305cf3 Export tokenizer function
In uglify-js@1, both parser and tokenizer methods were exported

This allows to use tokenizer() separately, e.g. to wrap or override it, as
parse() method accepts not only text, but also tokenized functions.
2016-06-07 12:25:16 +03:00
Anthony Van de Gejuchte
00ad57e393 Do not allow newlines in regex 2016-06-05 17:02:19 +02:00
kzc
09d5707a8a collapse_vars: Do not consider RegExp literals to be constants
Fixes #1100
2016-05-27 00:03:51 -04:00
kzc
1e390269d4 Optimize if_return for single if/return cases.
Fixes #1089
2016-05-24 17:54:08 +02:00
Richard van Velzen
bc49dfd27a Completely allow evaluating -0 2016-05-24 17:50:29 +02:00
Richard van Velzen
27eedbc302 Never produce -0 when evaluating expressions (like -"")
Fix for #1085. The major case was already there, but more expressions can result in -0.
2016-05-17 22:34:38 +02:00
kzc
5f464b41e2 Simplify iife new fix
as suggested by @rvanvelzen.

Added a test for IIFEs in nested contexts.
2016-05-15 19:12:17 -04:00
kzc
bcc1318d4b Do not apply negate_iife optimization to new expression 2016-05-09 03:19:28 -04:00
kzc
a0e03c9df4 Retain comments before AST_Constants during mangle. 2016-05-04 20:11:45 +02:00
Anthony Van de Gejuchte
6641dcafb6 Fix regression causing tests to fail on windows 2016-05-04 20:05:51 +02:00
kzc
d2945744f2 Workaround for process.exit() tty output truncation.
Fixes #1055
2016-05-04 20:04:48 +02:00
Anthony Van de Gejuchte
35bc716625 Add node 6 to travis 2016-05-04 20:03:39 +02:00
kzc
f39fd3d583 Handle CR line endings in comments.
Fixes #1050
2016-05-04 20:02:29 +02:00
Mihai Bazon
65887d9a56 Merge pull request #1053 from rvanvelzen/hoist_if_return_funs
Hoist functions when reversing if (x) return; ... vs. if (!x) ...
2016-04-26 22:09:52 +03:00
Richard van Velzen
e9224ab444 Add test cases for slightly more esoteric cases 2016-04-26 11:49:55 +02:00
Richard van Velzen
4d9a085687 Add test case for hoisting a single function 2016-04-26 11:43:03 +02:00
Richard van Velzen
4fe630431c Hoist functions when reversing if (x) return; ... vs. if (!x) ...
Fixes #1052
2016-04-23 23:48:33 +02:00
kzc
c55dd5ed74 Add passes compress option. Fix duplicate compress warnings. 2016-04-19 20:05:33 +02:00
kzc
e4fa4b109a Parse comments without recursion to avoid RangeError.
Fixes #993
2016-04-16 02:02:47 -04:00
Richard van Velzen
4b4528ee05 Prevent endless recursion when evaluating self-referencing consts
Fix #1041
2016-04-13 15:03:31 +02:00
Richard van Velzen
187a0caf9d Add base54.reset() to compress tests
Without this reset, char counts bleed to next tests. One test had a bad expect clause.
2016-04-12 20:08:09 +02:00
Mihai Bazon
b5a7a231f7 Actually limit sequence length.
Fix #1038
2016-04-12 14:17:24 +03:00
kzc
3907a5e3b2 Fix warnings for referenced non-hoisted functions.
Fixes #1034

Also added `expect_warnings` functionality to test framework.
2016-04-11 18:15:20 +02:00
Mihai Bazon
b434b75b36 Merge pull request #1032 from kzc/member
Simplify member(name, array) implementation.
2016-04-08 00:32:14 +03:00
kzc
c70d176f35 Simplify member(name, array) implementation. 2016-04-07 09:57:30 -04:00
Mihai Bazon
9317237372 Avoid using inherited hasOwnProperty
Fix #1031
2016-04-07 13:16:22 +03:00
kzc
98434258d0 Optimize ternaries with boolean consequent or alternative.
Fixes #511
2016-04-02 17:22:12 +02:00
kzc
45ddb9caeb Speedup unused compress option for already minified code
Fixes: #321 #917 #1022
2016-03-28 17:58:50 -04:00
Sebastien Daniel
9bcf702a6e added documentation on conditional compilation using API 2016-03-27 19:42:52 +02:00
Mihai Bazon
f68de86a17 Merge pull request #1011 from kzc/dont-produce-let-in-mangle
Do not produce `let` as a variable name in mangle.
2016-03-24 18:16:26 +02:00
Mihai Bazon
c3c7587796 Merge pull request #1019 from kzc/escape-ascii-only
Escape all ASCII control characters within strings for ascii_only
2016-03-24 18:08:57 +02:00
kzc
07bb7262d0 Escape all ASCII control characters within strings when using ascii_only.
Fixes #1017.

Tab characters within strings are now output as `\t` in all output modes.
2016-03-24 11:51:54 -04:00
kzc
21befe583f Attempt to increase timeout for mocha let test. 2016-03-15 11:44:09 -04:00
kzc
a9d4a6291b Do not produce let as a variable name in mangle.
Would previously occur in large generated functions with 21,000+ variables.
Fixes #986.
2016-03-15 11:20:32 -04:00
philippsimon
ee6c9fabb7 Fix: Uglified Number.prototype functions on big numbers 2016-03-14 12:41:06 +01:00
kzc
102d1b9137 #877 Ignore mangle sort option 2016-02-27 15:33:10 -05:00
41 changed files with 2476 additions and 216 deletions

View File

@@ -4,6 +4,7 @@ node_js:
- "0.12"
- "0.10"
- "4"
- "6"
matrix:
fast_finish: true
sudo: false

View File

@@ -133,7 +133,16 @@ The available options are:
--reserved-file File containing reserved names
--reserve-domprops Make (most?) DOM properties reserved for
--mangle-props
--mangle-props Mangle property names
--mangle-props Mangle property names (default `0`). Set to
`true` or `1` to mangle all property names. Set
to `unquoted` or `2` to only mangle unquoted
property names. Mode `2` also enables the
`keep_quoted_props` beautifier option to
preserve the quotes around property names and
disables the `properties` compressor option to
prevent rewriting quoted properties with dot
notation. You can override these by setting
them explicitly on the command line.
--mangle-regex Only mangle property names matching the regex
--name-cache File to hold mangled names mappings
--pure-funcs List of functions that can be safely removed if
@@ -192,11 +201,6 @@ input files from the command line.
To enable the mangler you need to pass `--mangle` (`-m`). The following
(comma-separated) options are supported:
- `sort` — to assign shorter names to most frequently used variables. This
saves a few hundred bytes on jQuery before gzip, but the output is
_bigger_ after gzip (and seems to happen for other libraries I tried it
on) therefore it's not enabled by default.
- `toplevel` — mangle names declared in the toplevel scope (disabled by
default).
@@ -296,12 +300,19 @@ to set `true`; it's effectively a shortcut for `foo=true`).
- `unsafe` (default: false) -- apply "unsafe" transformations (discussion below)
- `unsafe_comps` (default: false) -- Reverse `<` and `<=` to `>` and `>=` to
allow improved compression. This might be unsafe when an at least one of two
operands is an object with computed values due the use of methods like `get`,
or `valueOf`. This could cause change in execution order after operands in the
comparison are switching. Compression only works if both `comparisons` and
`unsafe_comps` are both set to true.
- `conditionals` -- apply optimizations for `if`-s and conditional
expressions
- `comparisons` -- apply certain optimizations to binary nodes, for example:
`!(a <= b) → a > b` (only when `unsafe`), attempts to negate binary nodes,
e.g. `a = !b && !c && !d && !e → a=!(b||c||d||e)` etc.
`!(a <= b) → a > b` (only when `unsafe_comps`), attempts to negate binary
nodes, e.g. `a = !b && !c && !d && !e → a=!(b||c||d||e)` etc.
- `evaluate` -- attempt to evaluate constant expressions
@@ -361,6 +372,9 @@ to set `true`; it's effectively a shortcut for `foo=true`).
compressor from mangling/discarding function names. Useful for code relying on
`Function.prototype.name`.
- `passes` -- default `1`. Number of times to run compress. Use an
integer argument larger than 1 to further reduce code size in some cases.
Note: raising the number of passes will increase uglify compress time.
### The `unsafe` option
@@ -415,6 +429,22 @@ code as usual. The build will contain the `const` declarations if you use
them. If you are targeting < ES6 environments, use `/** @const */ var`.
<a name="codegen-options"></a>
#### Conditional compilation, API
You can also use conditional compilation via the programmatic API. With the difference that the
property name is `global_defs` and is a compressor property:
```js
uglifyJS.minify([ "input.js"], {
compress: {
dead_code: true,
global_defs: {
DEBUG: false
}
}
});
```
## Beautifier options
The code generator tries to output shortest code possible by default. In
@@ -431,7 +461,7 @@ can pass additional arguments that control the code output:
objects
- `space-colon` (default `true`) -- insert a space after the colon signs
- `ascii-only` (default `false`) -- escape Unicode characters in strings and
regexps
regexps (affects directives with non-ascii characters becoming invalid)
- `inline-script` (default `false`) -- escape the slash in occurrences of
`</script` in strings
- `width` (default 80) -- only takes effect when beautification is on, this
@@ -458,6 +488,8 @@ can pass additional arguments that control the code output:
- `1` -- always use single quotes
- `2` -- always use double quotes
- `3` -- always use the original quotes
- `keep_quoted_props` (default `false`) -- when turned on, prevents stripping
quotes from property names in object literals.
### Keeping copyright notices or other comments
@@ -644,9 +676,14 @@ Other options:
- `parse` (default {}) — pass an object if you wish to specify some
additional [parser options][parser]. (not all options available... see below)
##### mangle
- `except` - pass an array of identifiers that should be excluded from mangling
##### mangleProperties options
- `regex` — Pass a RegExp to only mangle certain names (maps to the `--mange-regex` CLI arguments option)
- `regex` — Pass a RegExp to only mangle certain names (maps to the `--mangle-regex` CLI arguments option)
- `ignore_quoted` Only mangle unquoted property names (maps to the `--mangle-props 2` CLI arguments option)
We could add more options to `UglifyJS.minify` — if you need additional
functionality please suggest!

View File

@@ -69,7 +69,7 @@ You need to pass an argument to this option to specify the name that your module
.describe("quotes", "Quote style (0 - auto, 1 - single, 2 - double, 3 - original)")
.describe("reserved-file", "File containing reserved names")
.describe("reserve-domprops", "Make (most?) DOM properties reserved for --mangle-props")
.describe("mangle-props", "Mangle property names")
.describe("mangle-props", "Mangle property names (0 - disabled, 1 - mangle all properties, 2 - mangle unquoted properies)")
.describe("mangle-regex", "Only mangle property names matching the regex")
.describe("name-cache", "File to hold mangled names mappings")
.describe("pure-funcs", "List of functions that can be safely removed if their return value is not used")
@@ -125,7 +125,6 @@ You need to pass an argument to this option to specify the name that your module
.boolean("noerr")
.boolean("bare-returns")
.boolean("keep-fnames")
.boolean("mangle-props")
.boolean("reserve-domprops")
.wrap(80)
@@ -213,12 +212,24 @@ if (ARGS.quotes === true) {
ARGS.quotes = 3;
}
if (ARGS.mangle_props === true) {
ARGS.mangle_props = 1;
} else if (ARGS.mangle_props === "unquoted") {
ARGS.mangle_props = 2;
}
var OUTPUT_OPTIONS = {
beautify : BEAUTIFY ? true : false,
preamble : ARGS.preamble || null,
quote_style : ARGS.quotes != null ? ARGS.quotes : 0
};
if (ARGS.mangle_props == 2) {
OUTPUT_OPTIONS.keep_quoted_props = true;
if (COMPRESS && !("properties" in COMPRESS))
COMPRESS.properties = false;
}
if (ARGS.screw_ie8) {
if (COMPRESS) COMPRESS.screw_ie8 = true;
if (MANGLE) MANGLE.screw_ie8 = true;
@@ -401,10 +412,11 @@ async.eachLimit(files, 1, function (file, cb) {
}
TOPLEVEL = UglifyJS.mangle_properties(TOPLEVEL, {
reserved : reserved,
cache : cache,
only_cache : !ARGS.mangle_props,
regex : regex
reserved : reserved,
cache : cache,
only_cache : !ARGS.mangle_props,
regex : regex,
ignore_quoted : ARGS.mangle_props == 2
});
writeNameCache("props", cache);
})();
@@ -423,7 +435,7 @@ async.eachLimit(files, 1, function (file, cb) {
if (COMPRESS) {
time_it("squeeze", function(){
TOPLEVEL = TOPLEVEL.transform(compressor);
TOPLEVEL = compressor.compress(TOPLEVEL);
});
}

View File

@@ -71,7 +71,7 @@ function DEFNODE(type, props, methods, base) {
if (type) {
ctor.prototype.TYPE = ctor.TYPE = type;
}
if (methods) for (i in methods) if (methods.hasOwnProperty(i)) {
if (methods) for (i in methods) if (HOP(methods, i)) {
if (/^\$/.test(i)) {
ctor[i.substr(1)] = methods[i];
} else {
@@ -633,6 +633,13 @@ var AST_Seq = DEFNODE("Seq", "car cdr", {
p = p.cdr;
}
},
len: function() {
if (this.cdr instanceof AST_Seq) {
return this.cdr.len() + 1;
} else {
return 2;
}
},
_walk: function(visitor) {
return visitor._visit(this, function(){
this.car._walk(visitor);

View File

@@ -75,18 +75,36 @@ function Compressor(options, false_by_default) {
screw_ie8 : false,
drop_console : false,
angular : false,
warnings : true,
global_defs : {}
global_defs : {},
passes : 1,
}, true);
this.warnings_produced = {};
};
Compressor.prototype = new TreeTransformer;
merge(Compressor.prototype, {
option: function(key) { return this.options[key] },
warn: function() {
if (this.options.warnings)
AST_Node.warn.apply(AST_Node, arguments);
compress: function(node) {
var passes = +this.options.passes || 1;
for (var pass = 0; pass < passes && pass < 3; ++pass) {
if (pass > 0) node.clear_opt_flags();
node = node.transform(this);
}
return node;
},
warn: function(text, props) {
if (this.options.warnings) {
// only emit unique warnings
var message = string_template(text, props);
if (!(message in this.warnings_produced)) {
this.warnings_produced[message] = true;
AST_Node.warn.apply(AST_Node, arguments);
}
}
},
clear_warnings: function() {
this.warnings_produced = {};
},
before: function(node, descend, in_list) {
if (node._squeezed) return node;
@@ -129,6 +147,15 @@ merge(Compressor.prototype, {
return this.print_to_string() == node.print_to_string();
});
AST_Node.DEFMETHOD("clear_opt_flags", function(){
this.walk(new TreeWalker(function(node){
if (!(node instanceof AST_Directive || node instanceof AST_Constant)) {
node._squeezed = false;
node._optimized = false;
}
}));
});
function make_node(ctor, orig, props) {
if (!props) props = {};
if (orig) {
@@ -156,9 +183,18 @@ merge(Compressor.prototype, {
value: val
}).optimize(compressor);
case "number":
return make_node(isNaN(val) ? AST_NaN : AST_Number, orig, {
value: val
}).optimize(compressor);
if (isNaN(val)) {
return make_node(AST_NaN, orig);
}
if ((1 / val) < 0) {
return make_node(AST_UnaryPrefix, orig, {
operator: "-",
expression: make_node(AST_Number, null, { value: -val })
});
}
return make_node(AST_Number, orig, { value: val }).optimize(compressor);
case "boolean":
return make_node(val ? AST_True : AST_False, orig).optimize(compressor);
case "undefined":
@@ -302,7 +338,7 @@ merge(Compressor.prototype, {
if (ref.scope.uses_eval || ref.scope.uses_with) break;
// Constant single use vars can be replaced in any scope.
if (var_decl.value.is_constant(compressor)) {
if (!(var_decl.value instanceof AST_RegExp) && var_decl.value.is_constant(compressor)) {
var ctt = new TreeTransformer(function(node) {
if (node === ref)
return replace_var(node, ctt.parent(), true);
@@ -392,10 +428,7 @@ merge(Compressor.prototype, {
var_defs_removed = true;
}
// Further optimize statement after substitution.
stat.walk(new TreeWalker(function(node){
delete node._squeezed;
delete node._optimized;
}));
stat.clear_opt_flags();
compressor.warn("Replacing " + (is_constant ? "constant" : "variable") +
" " + var_name + " [{file}:{line},{col}]", node.start);
@@ -502,6 +535,7 @@ merge(Compressor.prototype, {
function handle_if_return(statements, compressor) {
var self = compressor.self();
var multiple_if_returns = has_multiple_if_returns(statements);
var in_lambda = self instanceof AST_Lambda;
var ret = [];
loop: for (var i = statements.length; --i >= 0;) {
@@ -539,7 +573,8 @@ merge(Compressor.prototype, {
}
//---
// if (foo()) return x; [ return ; ] ==> return foo() ? x : undefined;
if ((ret.length == 0 || ret[0] instanceof AST_Return) && stat.body.value && !stat.alternative && in_lambda) {
if (multiple_if_returns && (ret.length == 0 || ret[0] instanceof AST_Return)
&& stat.body.value && !stat.alternative && in_lambda) {
CHANGED = true;
stat = stat.clone();
stat.alternative = ret[0] || make_node(AST_Return, stat, {
@@ -554,11 +589,13 @@ merge(Compressor.prototype, {
CHANGED = true;
stat = stat.clone();
stat.condition = stat.condition.negate(compressor);
var body = as_statement_array(stat.alternative).concat(ret);
var funs = extract_functions_from_statement_array(body);
stat.body = make_node(AST_BlockStatement, stat, {
body: as_statement_array(stat.alternative).concat(ret)
body: body
});
stat.alternative = null;
ret = [ stat.transform(compressor) ];
ret = funs.concat([ stat.transform(compressor) ]);
continue loop;
}
//---
@@ -629,6 +666,17 @@ merge(Compressor.prototype, {
}
}
return ret;
function has_multiple_if_returns(statements) {
var n = 0;
for (var i = statements.length; --i >= 0;) {
var stat = statements[i];
if (stat instanceof AST_If && stat.body instanceof AST_Return) {
if (++n > 1) return true;
}
}
return false;
}
};
function eliminate_dead_code(statements, compressor) {
@@ -673,8 +721,12 @@ merge(Compressor.prototype, {
seq = [];
};
statements.forEach(function(stat){
if (stat instanceof AST_SimpleStatement && seq.length < 2000) seq.push(stat.body);
else push_seq(), ret.push(stat);
if (stat instanceof AST_SimpleStatement && seqLength(seq) < 2000) {
seq.push(stat.body);
} else {
push_seq();
ret.push(stat);
}
});
push_seq();
ret = sequencesize_2(ret, compressor);
@@ -682,6 +734,18 @@ merge(Compressor.prototype, {
return ret;
};
function seqLength(a) {
for (var len = 0, i = 0; i < a.length; ++i) {
var stat = a[i];
if (stat instanceof AST_Seq) {
len += stat.len();
} else {
len++;
}
}
return len;
};
function sequencesize_2(statements, compressor) {
function cons_seq(right) {
ret.pop();
@@ -769,6 +833,9 @@ merge(Compressor.prototype, {
if (stat instanceof AST_SimpleStatement) {
stat.body = (function transform(thing) {
return thing.transform(new TreeTransformer(function(node){
if (node instanceof AST_New) {
return node;
}
if (node instanceof AST_Call && node.expression instanceof AST_Function) {
return make_node(AST_UnaryPrefix, node, {
operator: "!",
@@ -800,8 +867,22 @@ merge(Compressor.prototype, {
};
function extract_functions_from_statement_array(statements) {
var funs = [];
for (var i = statements.length - 1; i >= 0; --i) {
var stat = statements[i];
if (stat instanceof AST_Defun) {
statements.splice(i, 1);
funs.unshift(stat);
}
}
return funs;
}
function extract_declarations_from_unreachable_code(compressor, stat, target) {
compressor.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start);
if (!(stat instanceof AST_Defun)) {
compressor.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start);
}
stat.walk(new TreeWalker(function(node){
if (node instanceof AST_Definitions) {
compressor.warn("Declarations in unreachable code! [{file}:{line},{col}]", node.start);
@@ -969,42 +1050,43 @@ merge(Compressor.prototype, {
return typeof e;
case "void": return void ev(e, compressor);
case "~": return ~ev(e, compressor);
case "-":
e = ev(e, compressor);
if (e === 0) throw def;
return -e;
case "-": return -ev(e, compressor);
case "+": return +ev(e, compressor);
}
throw def;
});
def(AST_Binary, function(c){
var left = this.left, right = this.right;
var left = this.left, right = this.right, result;
switch (this.operator) {
case "&&" : return ev(left, c) && ev(right, c);
case "||" : return ev(left, c) || ev(right, c);
case "|" : return ev(left, c) | ev(right, c);
case "&" : return ev(left, c) & ev(right, c);
case "^" : return ev(left, c) ^ ev(right, c);
case "+" : return ev(left, c) + ev(right, c);
case "*" : return ev(left, c) * ev(right, c);
case "/" : return ev(left, c) / ev(right, c);
case "%" : return ev(left, c) % ev(right, c);
case "-" : return ev(left, c) - ev(right, c);
case "<<" : return ev(left, c) << ev(right, c);
case ">>" : return ev(left, c) >> ev(right, c);
case ">>>" : return ev(left, c) >>> ev(right, c);
case "==" : return ev(left, c) == ev(right, c);
case "===" : return ev(left, c) === ev(right, c);
case "!=" : return ev(left, c) != ev(right, c);
case "!==" : return ev(left, c) !== ev(right, c);
case "<" : return ev(left, c) < ev(right, c);
case "<=" : return ev(left, c) <= ev(right, c);
case ">" : return ev(left, c) > ev(right, c);
case ">=" : return ev(left, c) >= ev(right, c);
case "in" : return ev(left, c) in ev(right, c);
case "instanceof" : return ev(left, c) instanceof ev(right, c);
case "&&" : result = ev(left, c) && ev(right, c); break;
case "||" : result = ev(left, c) || ev(right, c); break;
case "|" : result = ev(left, c) | ev(right, c); break;
case "&" : result = ev(left, c) & ev(right, c); break;
case "^" : result = ev(left, c) ^ ev(right, c); break;
case "+" : result = ev(left, c) + ev(right, c); break;
case "*" : result = ev(left, c) * ev(right, c); break;
case "/" : result = ev(left, c) / ev(right, c); break;
case "%" : result = ev(left, c) % ev(right, c); break;
case "-" : result = ev(left, c) - ev(right, c); break;
case "<<" : result = ev(left, c) << ev(right, c); break;
case ">>" : result = ev(left, c) >> ev(right, c); break;
case ">>>" : result = ev(left, c) >>> ev(right, c); break;
case "==" : result = ev(left, c) == ev(right, c); break;
case "===" : result = ev(left, c) === ev(right, c); break;
case "!=" : result = ev(left, c) != ev(right, c); break;
case "!==" : result = ev(left, c) !== ev(right, c); break;
case "<" : result = ev(left, c) < ev(right, c); break;
case "<=" : result = ev(left, c) <= ev(right, c); break;
case ">" : result = ev(left, c) > ev(right, c); break;
case ">=" : result = ev(left, c) >= ev(right, c); break;
default:
throw def;
}
throw def;
if (isNaN(result) && c.find_parent(AST_With)) {
// leave original expression as is
throw def;
}
return result;
});
def(AST_Conditional, function(compressor){
return ev(this.condition, compressor)
@@ -1012,8 +1094,16 @@ merge(Compressor.prototype, {
: ev(this.alternative, compressor);
});
def(AST_SymbolRef, function(compressor){
var d = this.definition();
if (d && d.constant && d.init) return ev(d.init, compressor);
if (this._evaluating) throw def;
this._evaluating = true;
try {
var d = this.definition();
if (d && d.constant && d.init) {
return ev(d.init, compressor);
}
} finally {
this._evaluating = false;
}
throw def;
});
def(AST_Dot, function(compressor){
@@ -1240,8 +1330,10 @@ merge(Compressor.prototype, {
if (compressor.option("unused")
&& !(self instanceof AST_Toplevel)
&& !self.uses_eval
&& !self.uses_with
) {
var in_use = [];
var in_use_ids = {}; // avoid expensive linear scans of in_use
var initializations = new Dictionary();
// pass 1: find out which symbols are directly used in
// this scope (not in nested scopes).
@@ -1264,7 +1356,11 @@ merge(Compressor.prototype, {
return true;
}
if (node instanceof AST_SymbolRef) {
push_uniq(in_use, node.definition());
var node_def = node.definition();
if (!(node_def.id in in_use_ids)) {
in_use_ids[node_def.id] = true;
in_use.push(node_def);
}
return true;
}
if (node instanceof AST_Scope) {
@@ -1287,7 +1383,11 @@ merge(Compressor.prototype, {
if (init) init.forEach(function(init){
var tw = new TreeWalker(function(node){
if (node instanceof AST_SymbolRef) {
push_uniq(in_use, node.definition());
var node_def = node.definition();
if (!(node_def.id in in_use_ids)) {
in_use_ids[node_def.id] = true;
in_use.push(node_def);
}
}
});
init.walk(tw);
@@ -1315,7 +1415,7 @@ merge(Compressor.prototype, {
}
}
if (node instanceof AST_Defun && node !== self) {
if (!member(node.name.definition(), in_use)) {
if (!(node.name.definition().id in in_use_ids)) {
compressor.warn("Dropping unused function {name} [{file}:{line},{col}]", {
name : node.name.name,
file : node.name.start.file,
@@ -1328,7 +1428,7 @@ merge(Compressor.prototype, {
}
if (node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn)) {
var def = node.definitions.filter(function(def){
if (member(def.name.definition(), in_use)) return true;
if (def.name.definition().id in in_use_ids) return true;
var w = {
name : def.name.name,
file : def.name.start.file,
@@ -2406,9 +2506,11 @@ merge(Compressor.prototype, {
});
self = best_of(self, negated);
}
switch (self.operator) {
case "<": reverse(">"); break;
case "<=": reverse(">="); break;
if (compressor.option("unsafe_comps")) {
switch (self.operator) {
case "<": reverse(">"); break;
case "<=": reverse(">="); break;
}
}
}
if (self.operator == "+" && self.right instanceof AST_String
@@ -2500,16 +2602,19 @@ merge(Compressor.prototype, {
if (self.undeclared() && !isLHS(self, compressor.parent())) {
var defines = compressor.option("global_defs");
if (defines && defines.hasOwnProperty(self.name)) {
if (defines && HOP(defines, self.name)) {
return make_node_from_constant(compressor, defines[self.name], self);
}
switch (self.name) {
case "undefined":
return make_node(AST_Undefined, self);
case "NaN":
return make_node(AST_NaN, self).transform(compressor);
case "Infinity":
return make_node(AST_Infinity, self).transform(compressor);
// testing against !self.scope.uses_with first is an optimization
if (!self.scope.uses_with || !compressor.find_parent(AST_With)) {
switch (self.name) {
case "undefined":
return make_node(AST_Undefined, self);
case "NaN":
return make_node(AST_NaN, self).transform(compressor);
case "Infinity":
return make_node(AST_Infinity, self).transform(compressor);
}
}
}
return self;
@@ -2641,7 +2746,7 @@ merge(Compressor.prototype, {
if (consequent.is_constant(compressor)
&& alternative.is_constant(compressor)
&& consequent.equivalent_to(alternative)) {
var consequent_value = consequent.constant_value();
var consequent_value = consequent.constant_value(compressor);
if (self.condition.has_side_effects(compressor)) {
return AST_Seq.from_array([self.condition, make_node_from_constant(compressor, consequent_value, self)]);
} else {
@@ -2649,24 +2754,58 @@ merge(Compressor.prototype, {
}
}
// y?true:false --> !!y
if (is_true(consequent) && is_false(alternative)) {
if (self.condition.is_boolean()) {
// boolean_expression ? true : false --> boolean_expression
return self.condition;
if (is_true(self.consequent)) {
if (is_false(self.alternative)) {
// c ? true : false ---> !!c
return booleanize(self.condition);
}
self.condition = self.condition.negate(compressor);
return make_node(AST_UnaryPrefix, self.condition, {
operator: "!",
expression: self.condition
// c ? true : x ---> !!c || x
return make_node(AST_Binary, self, {
operator: "||",
left: booleanize(self.condition),
right: self.alternative
});
}
// y?false:true --> !y
if (is_false(consequent) && is_true(alternative)) {
return self.condition.negate(compressor)
if (is_false(self.consequent)) {
if (is_true(self.alternative)) {
// c ? false : true ---> !c
return booleanize(self.condition.negate(compressor));
}
// c ? false : x ---> !c && x
return make_node(AST_Binary, self, {
operator: "&&",
left: booleanize(self.condition.negate(compressor)),
right: self.alternative
});
}
if (is_true(self.alternative)) {
// c ? x : true ---> !c || x
return make_node(AST_Binary, self, {
operator: "||",
left: booleanize(self.condition.negate(compressor)),
right: self.consequent
});
}
if (is_false(self.alternative)) {
// c ? x : false ---> !!c && x
return make_node(AST_Binary, self, {
operator: "&&",
left: booleanize(self.condition),
right: self.consequent
});
}
return self;
function booleanize(node) {
if (node.is_boolean()) return node;
// !!expression
return make_node(AST_UnaryPrefix, node, {
operator: "!",
expression: node.negate(compressor)
});
}
// AST_True or !0
function is_true(node) {
return node instanceof AST_True

View File

@@ -43,6 +43,8 @@
"use strict";
var EXPECT_DIRECTIVE = /^$|[;{][\s\n]*$/;
function OutputStream(options) {
options = defaults(options, {
@@ -64,7 +66,8 @@ function OutputStream(options) {
preserve_line : false,
screw_ie8 : false,
preamble : null,
quote_style : 0
quote_style : 0,
keep_quoted_props: false
}, true);
var indentation = 0;
@@ -74,7 +77,7 @@ function OutputStream(options) {
var OUTPUT = "";
function to_ascii(str, identifier) {
return str.replace(/[\u0080-\uffff]/g, function(ch) {
return str.replace(/[\u0000-\u001f\u007f-\uffff]/g, function(ch) {
var code = ch.charCodeAt(0).toString(16);
if (code.length <= 2 && !identifier) {
while (code.length < 2) code = "0" + code;
@@ -88,20 +91,23 @@ function OutputStream(options) {
function make_string(str, quote) {
var dq = 0, sq = 0;
str = str.replace(/[\\\b\f\n\r\v\t\x22\x27\u2028\u2029\0\ufeff]/g, function(s){
str = str.replace(/[\\\b\f\n\r\v\t\x22\x27\u2028\u2029\0\ufeff]/g,
function(s, i){
switch (s) {
case '"': ++dq; return '"';
case "'": ++sq; return "'";
case "\\": return "\\\\";
case "\b": return "\\b";
case "\f": return "\\f";
case "\n": return "\\n";
case "\r": return "\\r";
case "\t": return "\\t";
case "\b": return "\\b";
case "\f": return "\\f";
case "\x0B": return options.screw_ie8 ? "\\v" : "\\x0B";
case "\u2028": return "\\u2028";
case "\u2029": return "\\u2029";
case '"': ++dq; return '"';
case "'": ++sq; return "'";
case "\0": return "\\x00";
case "\ufeff": return "\\ufeff";
case "\0":
return /[0-7]/.test(str.charAt(i+1)) ? "\\x00" : "\\0";
}
return s;
});
@@ -351,7 +357,18 @@ function OutputStream(options) {
force_semicolon : force_semicolon,
to_ascii : to_ascii,
print_name : function(name) { print(make_name(name)) },
print_string : function(str, quote) { print(encode_string(str, quote)) },
print_string : function(str, quote, escape_directive) {
var encoded = encode_string(str, quote);
if (escape_directive === true && encoded.indexOf("\\") === -1) {
// Insert semicolons to break directive prologue
if (!EXPECT_DIRECTIVE.test(OUTPUT)) {
force_semicolon();
}
force_semicolon();
}
print(encoded);
},
encode_string : encode_string,
next_indent : next_indent,
with_indent : with_indent,
with_block : with_block,
@@ -383,10 +400,11 @@ function OutputStream(options) {
};
var use_asm = false;
var in_directive = false;
AST_Node.DEFMETHOD("print", function(stream, force_parens){
var self = this, generator = self._codegen, prev_use_asm = use_asm;
if (self instanceof AST_Directive && self.value == "use asm") {
if (self instanceof AST_Directive && self.value == "use asm" && stream.parent() instanceof AST_Scope) {
use_asm = true;
}
function doit() {
@@ -401,13 +419,14 @@ function OutputStream(options) {
doit();
}
stream.pop_node();
if (self instanceof AST_Lambda) {
if (self instanceof AST_Scope) {
use_asm = prev_use_asm;
}
});
AST_Node.DEFMETHOD("print_to_string", function(options){
var s = OutputStream(options);
if (!options) s._readonly = true;
this.print(s);
return s.get();
});
@@ -415,6 +434,7 @@ function OutputStream(options) {
/* -----[ comments ]----- */
AST_Node.DEFMETHOD("add_comments", function(output){
if (output._readonly) return;
var c = output.option("comments"), self = this;
var start = self.start;
if (start && !start._comments_dumped) {
@@ -512,7 +532,8 @@ function OutputStream(options) {
PARENS([ AST_Unary, AST_Undefined ], function(output){
var p = output.parent();
return p instanceof AST_PropAccess && p.expression === this;
return p instanceof AST_PropAccess && p.expression === this
|| p instanceof AST_New;
});
PARENS(AST_Seq, function(output){
@@ -588,7 +609,7 @@ function OutputStream(options) {
PARENS(AST_New, function(output){
var p = output.parent();
if (no_constructor_parens(this, output)
if (!need_constructor_parens(this, output)
&& (p instanceof AST_PropAccess // (new Date).getTime(), (new Date)["getTime"]()
|| p instanceof AST_Call && p.expression === this)) // (new foo)(bar)
return true;
@@ -596,8 +617,12 @@ function OutputStream(options) {
PARENS(AST_Number, function(output){
var p = output.parent();
if (this.getValue() < 0 && p instanceof AST_PropAccess && p.expression === this)
return true;
if (p instanceof AST_PropAccess && p.expression === this) {
var value = this.getValue();
if (value < 0 || /^0/.test(make_num(value))) {
return true;
}
}
});
PARENS([ AST_Assign, AST_Conditional ], function (output){
@@ -632,9 +657,16 @@ function OutputStream(options) {
/* -----[ statements ]----- */
function display_body(body, is_toplevel, output) {
function display_body(body, is_toplevel, output, allow_directives) {
var last = body.length - 1;
in_directive = allow_directives;
body.forEach(function(stmt, i){
if (in_directive === true && !(stmt instanceof AST_Directive ||
stmt instanceof AST_EmptyStatement ||
(stmt instanceof AST_SimpleStatement && stmt.body instanceof AST_String)
)) {
in_directive = false;
}
if (!(stmt instanceof AST_EmptyStatement)) {
output.indent();
stmt.print(output);
@@ -643,7 +675,14 @@ function OutputStream(options) {
if (is_toplevel) output.newline();
}
}
if (in_directive === true &&
stmt instanceof AST_SimpleStatement &&
stmt.body instanceof AST_String
) {
in_directive = false;
}
});
in_directive = false;
};
AST_StatementWithBody.DEFMETHOD("_do_print_body", function(output){
@@ -655,7 +694,7 @@ function OutputStream(options) {
output.semicolon();
});
DEFPRINT(AST_Toplevel, function(self, output){
display_body(self.body, true, output);
display_body(self.body, true, output, true);
output.print("");
});
DEFPRINT(AST_LabeledStatement, function(self, output){
@@ -667,9 +706,9 @@ function OutputStream(options) {
self.body.print(output);
output.semicolon();
});
function print_bracketed(body, output) {
function print_bracketed(body, output, allow_directives) {
if (body.length > 0) output.with_block(function(){
display_body(body, false, output);
display_body(body, false, output, allow_directives);
});
else output.print("{}");
};
@@ -769,7 +808,7 @@ function OutputStream(options) {
});
});
output.space();
print_bracketed(self.body, output);
print_bracketed(self.body, output, true);
});
DEFPRINT(AST_Lambda, function(self, output){
self._do_print(output);
@@ -985,7 +1024,7 @@ function OutputStream(options) {
/* -----[ other expressions ]----- */
DEFPRINT(AST_Call, function(self, output){
self.expression.print(output);
if (self instanceof AST_New && no_constructor_parens(self, output))
if (self instanceof AST_New && !need_constructor_parens(self, output))
return;
output.with_parens(function(){
self.args.forEach(function(expr, i){
@@ -1026,7 +1065,7 @@ function OutputStream(options) {
var expr = self.expression;
expr.print(output);
if (expr instanceof AST_Number && expr.getValue() >= 0) {
if (!/[xa-f.]/i.test(output.last())) {
if (!/[xa-f.)]/i.test(output.last())) {
output.print(".");
}
}
@@ -1135,7 +1174,11 @@ function OutputStream(options) {
&& parseFloat(key) >= 0) {
output.print(make_num(key));
} else if (RESERVED_WORDS(key) ? output.option("screw_ie8") : is_identifier_string(key)) {
output.print_name(key);
if (quote && output.option("keep_quoted_props")) {
output.print_string(key, quote);
} else {
output.print_name(key);
}
} else {
output.print_string(key, quote);
}
@@ -1175,10 +1218,10 @@ function OutputStream(options) {
output.print(self.getValue());
});
DEFPRINT(AST_String, function(self, output){
output.print_string(self.getValue(), self.quote);
output.print_string(self.getValue(), self.quote, in_directive);
});
DEFPRINT(AST_Number, function(self, output){
if (use_asm && self.start.raw != null) {
if (use_asm && self.start && self.start.raw != null) {
output.print(self.start.raw);
} else {
output.print(make_num(self.getValue()));
@@ -1275,8 +1318,11 @@ function OutputStream(options) {
};
// self should be AST_New. decide if we want to show parens or not.
function no_constructor_parens(self, output) {
return self.args.length == 0 && !output.option("beautify");
function need_constructor_parens(self, output) {
// Always print parentheses with arguments
if (self.args.length > 0) return true;
return output.option("beautify");
};
function best_of(a) {

View File

@@ -46,7 +46,7 @@
var KEYWORDS = 'break case catch const continue debugger default delete do else finally for function if in instanceof new return switch throw try typeof var void while with';
var KEYWORDS_ATOM = 'false null true';
var RESERVED_WORDS = 'abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized this throws transient volatile yield'
var RESERVED_WORDS = 'abstract boolean byte char class double enum export extends final float goto implements import int interface let long native package private protected public short static super synchronized this throws transient volatile yield'
+ " " + KEYWORDS_ATOM + " " + KEYWORDS;
var KEYWORDS_BEFORE_EXPRESSION = 'return new delete throw else case';
@@ -223,7 +223,9 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
tokcol : 0,
newline_before : false,
regex_allowed : false,
comments_before : []
comments_before : [],
directives : {},
directive_stack : []
};
function peek() { return S.text.charAt(S.pos); };
@@ -255,6 +257,16 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
return S.text.substr(S.pos, str.length) == str;
};
function find_eol() {
var text = S.text;
for (var i = S.pos, n = S.text.length; i < n; ++i) {
var ch = text[i];
if (ch == '\n' || ch == '\r')
return i;
}
return -1;
};
function find(what, signal_eof) {
var pos = S.text.indexOf(what, S.pos);
if (signal_eof && pos == -1) throw EX_EOF;
@@ -382,8 +394,6 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
for (;;) {
var ch = next(true, true);
if (ch == "\\") {
// read OctalEscapeSequence (XXX: deprecated if "strict mode")
// https://github.com/mishoo/UglifyJS/issues/178
var octal_len = 0, first = null;
ch = read_while(function(ch){
if (ch >= "0" && ch <= "7") {
@@ -396,8 +406,13 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
}
return false;
});
if (octal_len > 0) ch = String.fromCharCode(parseInt(ch, 8));
else ch = read_escaped_char(true);
if (octal_len > 0) {
if (ch !== "0" && next_token.has_directive("use strict"))
parse_error("Octal literals are not allowed in strict mode");
ch = String.fromCharCode(parseInt(ch, 8));
} else {
ch = read_escaped_char(true);
}
}
else if ("\r\n\u2028\u2029".indexOf(ch) >= 0) parse_error("Unterminated string constant");
else if (ch == quote) break;
@@ -410,7 +425,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
function skip_line_comment(type) {
var regex_allowed = S.regex_allowed;
var i = find("\n"), ret;
var i = find_eol(), ret;
if (i == -1) {
ret = S.text.substr(S.pos);
S.pos = S.text.length;
@@ -421,13 +436,13 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
S.col = S.tokcol + (S.pos - S.tokpos);
S.comments_before.push(token(type, ret, true));
S.regex_allowed = regex_allowed;
return next_token();
return next_token;
};
var skip_multiline_comment = with_eof_error("Unterminated multiline comment", function(){
var regex_allowed = S.regex_allowed;
var i = find("*/", true);
var text = S.text.substring(S.pos, i);
var text = S.text.substring(S.pos, i).replace(/\r\n|\r/g, '\n');
var a = text.split("\n"), n = a.length;
// update stream position
S.pos = i + 2;
@@ -439,7 +454,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
S.comments_before.push(token("comment2", text, true));
S.regex_allowed = regex_allowed;
S.newline_before = nlb;
return next_token();
return next_token;
});
function read_name() {
@@ -480,6 +495,8 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
break;
} else if (ch == "\\") {
prev_backslash = true;
} else if ("\r\n\u2028\u2029".indexOf(ch) >= 0) {
parse_error("Unexpected line terminator");
} else {
regexp += ch;
}
@@ -548,36 +565,45 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
function next_token(force_regexp) {
if (force_regexp != null)
return read_regexp(force_regexp);
skip_whitespace();
start_token();
if (html5_comments) {
if (looking_at("<!--")) {
forward(4);
return skip_line_comment("comment3");
for (;;) {
skip_whitespace();
start_token();
if (html5_comments) {
if (looking_at("<!--")) {
forward(4);
skip_line_comment("comment3");
continue;
}
if (looking_at("-->") && S.newline_before) {
forward(3);
skip_line_comment("comment4");
continue;
}
}
if (looking_at("-->") && S.newline_before) {
forward(3);
return skip_line_comment("comment4");
var ch = peek();
if (!ch) return token("eof");
var code = ch.charCodeAt(0);
switch (code) {
case 34: case 39: return read_string(ch);
case 46: return handle_dot();
case 47: {
var tok = handle_slash();
if (tok === next_token) continue;
return tok;
}
}
}
var ch = peek();
if (!ch) return token("eof");
var code = ch.charCodeAt(0);
switch (code) {
case 34: case 39: return read_string(ch);
case 46: return handle_dot();
case 47: return handle_slash();
}
if (is_digit(code)) return read_num();
if (PUNC_CHARS(ch)) return token("punc", next());
if (OPERATOR_CHARS(ch)) return read_operator();
if (code == 92 || is_identifier_start(code)) return read_word();
if (shebang) {
if (S.pos == 0 && looking_at("#!")) {
forward(2);
return skip_line_comment("comment5");
if (is_digit(code)) return read_num();
if (PUNC_CHARS(ch)) return token("punc", next());
if (OPERATOR_CHARS(ch)) return read_operator();
if (code == 92 || is_identifier_start(code)) return read_word();
if (shebang) {
if (S.pos == 0 && looking_at("#!")) {
forward(2);
skip_line_comment("comment5");
continue;
}
}
break;
}
parse_error("Unexpected character '" + ch + "'");
};
@@ -587,6 +613,35 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
return S;
};
next_token.add_directive = function(directive) {
S.directive_stack[S.directive_stack.length - 1].push(directive);
if (S.directives[directive] === undefined) {
S.directives[directive] = 1;
} else {
S.directives[directive]++;
}
}
next_token.push_directives_stack = function() {
S.directive_stack.push([]);
}
next_token.pop_directives_stack = function() {
var directives = S.directive_stack[S.directive_stack.length - 1];
for (var i = 0; i < directives.length; i++) {
S.directives[directives[i]]--;
}
S.directive_stack.pop();
}
next_token.has_directive = function(directive) {
return S.directives[directive] !== undefined &&
S.directives[directive] > 0;
}
return next_token;
};
@@ -707,14 +762,14 @@ function parse($TEXT, options) {
function unexpected(token) {
if (token == null)
token = S.token;
token_error(token, "Unexpected token: " + token.type + " (" + token.value + ")");
token_error(token, "SyntaxError: Unexpected token: " + token.type + " (" + token.value + ")");
};
function expect_token(type, val) {
if (is(type, val)) {
return next();
}
token_error(S.token, "Unexpected token " + S.token.type + " «" + S.token.value + "»" + ", expected " + type + " «" + val + "»");
token_error(S.token, "SyntaxError: Unexpected token " + S.token.type + " «" + S.token.value + "»" + ", expected " + type + " «" + val + "»");
};
function expect(punc) { return expect_token("punc", punc); };
@@ -760,9 +815,16 @@ function parse($TEXT, options) {
handle_regexp();
switch (S.token.type) {
case "string":
var dir = false;
if (S.in_directives === true) {
if ((is_token(peek(), "punc", ";") || peek().nlb) && S.token.raw.indexOf("\\") === -1) {
S.input.add_directive(S.token.value);
} else {
S.in_directives = false;
}
}
var dir = S.in_directives, stat = simple_statement();
// XXXv2: decide how to fix directives
if (dir && stat.body instanceof AST_String && !is("punc", ",")) {
if (dir) {
return new AST_Directive({
start : stat.body.start,
end : stat.body.end,
@@ -794,6 +856,7 @@ function parse($TEXT, options) {
case "(":
return simple_statement();
case ";":
S.in_directives = false;
next();
return new AST_EmptyStatement();
default:
@@ -835,7 +898,7 @@ function parse($TEXT, options) {
case "return":
if (S.in_function == 0 && !options.bare_returns)
croak("'return' outside of function");
croak("SyntaxError: 'return' outside of function");
return new AST_Return({
value: ( is("punc", ";")
? (next(), null)
@@ -852,7 +915,7 @@ function parse($TEXT, options) {
case "throw":
if (S.token.nlb)
croak("Illegal newline after 'throw'");
croak("SyntaxError: Illegal newline after 'throw'");
return new AST_Throw({
value: (tmp = expression(true), semicolon(), tmp)
});
@@ -867,6 +930,9 @@ function parse($TEXT, options) {
return tmp = const_(), semicolon(), tmp;
case "with":
if (S.input.has_directive("use strict")) {
croak("SyntaxError: Strict mode may not include a with statement");
}
return new AST_With({
expression : parenthesised(),
body : statement()
@@ -885,7 +951,7 @@ function parse($TEXT, options) {
// syntactically incorrect if it contains a
// LabelledStatement that is enclosed by a
// LabelledStatement with the same Identifier as label.
croak("Label " + label.name + " defined twice");
croak("SyntaxError: Label " + label.name + " defined twice");
}
expect(":");
S.labels.push(label);
@@ -898,7 +964,7 @@ function parse($TEXT, options) {
label.references.forEach(function(ref){
if (ref instanceof AST_Continue) {
ref = ref.label.start;
croak("Continue label `" + label.name + "` refers to non-IterationStatement.",
croak("SyntaxError: Continue label `" + label.name + "` refers to non-IterationStatement.",
ref.line, ref.col, ref.pos);
}
});
@@ -918,11 +984,11 @@ function parse($TEXT, options) {
if (label != null) {
ldef = find_if(function(l){ return l.name == label.name }, S.labels);
if (!ldef)
croak("Undefined label " + label.name);
croak("SyntaxError: Undefined label " + label.name);
label.thedef = ldef;
}
else if (S.in_loop == 0)
croak(type.TYPE + " not inside a loop or switch");
croak("SyntaxError: " + type.TYPE + " not inside a loop or switch");
semicolon();
var stat = new type({ label: label });
if (ldef) ldef.references.push(stat);
@@ -938,7 +1004,7 @@ function parse($TEXT, options) {
: expression(true, true);
if (is("operator", "in")) {
if (init instanceof AST_Var && init.definitions.length > 1)
croak("Only one variable declaration allowed in for..in loop");
croak("SyntaxError: Only one variable declaration allowed in for..in loop");
next();
return for_in(init);
}
@@ -991,9 +1057,11 @@ function parse($TEXT, options) {
body: (function(loop, labels){
++S.in_function;
S.in_directives = true;
S.input.push_directives_stack();
S.in_loop = 0;
S.labels = [];
var a = block_();
S.input.pop_directives_stack();
--S.in_function;
S.in_loop = loop;
S.labels = labels;
@@ -1086,7 +1154,7 @@ function parse($TEXT, options) {
});
}
if (!bcatch && !bfinally)
croak("Missing catch/finally blocks");
croak("SyntaxError: Missing catch/finally blocks");
return new AST_Try({
body : body,
bcatch : bcatch,
@@ -1180,8 +1248,8 @@ function parse($TEXT, options) {
break;
case "operator":
if (!is_identifier_string(tok.value)) {
throw new JS_Parse_Error("Invalid getter/setter name: " + tok.value,
tok.file, tok.line, tok.col, tok.pos);
croak("SyntaxError: Invalid getter/setter name: " + tok.value,
tok.line, tok.col, tok.pos);
}
ret = _make_symbol(AST_SymbolRef);
break;
@@ -1331,7 +1399,7 @@ function parse($TEXT, options) {
function as_symbol(type, noerror) {
if (!is("name")) {
if (!noerror) croak("Name expected");
if (!noerror) croak("SyntaxError: Name expected");
return null;
}
var sym = _make_symbol(type);
@@ -1395,7 +1463,7 @@ function parse($TEXT, options) {
function make_unary(ctor, op, expr) {
if ((op == "++" || op == "--") && !is_assignable(expr))
croak("Invalid use of " + op + " operator");
croak("SyntaxError: Invalid use of " + op + " operator");
return new ctor({ operator: op, expression: expr });
};
@@ -1459,7 +1527,7 @@ function parse($TEXT, options) {
end : prev()
});
}
croak("Invalid assignment");
croak("SyntaxError: Invalid assignment");
}
return left;
};
@@ -1493,8 +1561,10 @@ function parse($TEXT, options) {
return (function(){
var start = S.token;
var body = [];
S.input.push_directives_stack();
while (!is("eof"))
body.push(statement());
S.input.pop_directives_stack();
var end = prev();
var toplevel = options.toplevel;
if (toplevel) {

View File

@@ -65,7 +65,8 @@ function mangle_properties(ast, options) {
reserved : null,
cache : null,
only_cache : false,
regex : null
regex : null,
ignore_quoted : false
});
var reserved = options.reserved;
@@ -81,6 +82,7 @@ function mangle_properties(ast, options) {
}
var regex = options.regex;
var ignore_quoted = options.ignore_quoted;
var names_to_mangle = [];
var unmangleable = [];
@@ -88,7 +90,8 @@ function mangle_properties(ast, options) {
// step 1: find candidates to mangle
ast.walk(new TreeWalker(function(node){
if (node instanceof AST_ObjectKeyVal) {
add(node.key);
if (!(ignore_quoted && node.quote))
add(node.key);
}
else if (node instanceof AST_ObjectProperty) {
// setter or getter, since KeyVal is handled above
@@ -101,7 +104,8 @@ function mangle_properties(ast, options) {
}
else if (node instanceof AST_Sub) {
if (this.parent() instanceof AST_Assign) {
addStrings(node.property);
if (!ignore_quoted)
addStrings(node.property);
}
}
}));
@@ -109,7 +113,8 @@ function mangle_properties(ast, options) {
// step 2: transform the tree, renaming properties
return ast.transform(new TreeTransformer(function(node){
if (node instanceof AST_ObjectKeyVal) {
node.key = mangle(node.key);
if (!(ignore_quoted && node.quote))
node.key = mangle(node.key);
}
else if (node instanceof AST_ObjectProperty) {
// setter or getter
@@ -119,7 +124,8 @@ function mangle_properties(ast, options) {
node.property = mangle(node.property);
}
else if (node instanceof AST_Sub) {
node.property = mangleStrings(node.property);
if (!ignore_quoted)
node.property = mangleStrings(node.property);
}
// else if (node instanceof AST_String) {
// if (should_mangle(node.value)) {

View File

@@ -53,8 +53,11 @@ function SymbolDef(scope, index, orig) {
this.undeclared = false;
this.constant = false;
this.index = index;
this.id = SymbolDef.next_id++;
};
SymbolDef.next_id = 1;
SymbolDef.prototype = {
unmangleable: function(options) {
if (!options) options = {};
@@ -372,7 +375,7 @@ AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){
return defaults(options, {
except : [],
eval : false,
sort : false,
sort : false, // Ignored. Flag retained for backwards compatibility.
toplevel : false,
screw_ie8 : false,
keep_fnames : false
@@ -415,9 +418,6 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
a.push(symbol);
}
});
if (options.sort) a.sort(function(a, b){
return b.references.length - a.references.length;
});
to_mangle.push.apply(to_mangle, a);
return;
}

View File

@@ -59,10 +59,7 @@ function characters(str) {
};
function member(name, array) {
for (var i = array.length; --i >= 0;)
if (array[i] == name)
return true;
return false;
return array.indexOf(name) >= 0;
};
function find_if(func, array) {
@@ -97,17 +94,17 @@ function defaults(args, defs, croak) {
if (args === true)
args = {};
var ret = args || {};
if (croak) for (var i in ret) if (ret.hasOwnProperty(i) && !defs.hasOwnProperty(i))
if (croak) for (var i in ret) if (HOP(ret, i) && !HOP(defs, i))
DefaultsError.croak("`" + i + "` is not a supported option", defs);
for (var i in defs) if (defs.hasOwnProperty(i)) {
ret[i] = (args && args.hasOwnProperty(i)) ? args[i] : defs[i];
for (var i in defs) if (HOP(defs, i)) {
ret[i] = (args && HOP(args, i)) ? args[i] : defs[i];
}
return ret;
};
function merge(obj, ext) {
var count = 0;
for (var i in ext) if (ext.hasOwnProperty(i)) {
for (var i in ext) if (HOP(ext, i)) {
obj[i] = ext[i];
count++;
}
@@ -150,7 +147,7 @@ var MAP = (function(){
}
}
else {
for (i in a) if (a.hasOwnProperty(i)) if (doit()) break;
for (i in a) if (HOP(a, i)) if (doit()) break;
}
return top.concat(ret);
};
@@ -308,3 +305,7 @@ Dictionary.fromObject = function(obj) {
dict._size = merge(dict._values, obj);
return dict;
};
function HOP(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}

View File

@@ -4,7 +4,7 @@
"homepage": "http://lisperator.net/uglifyjs",
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
"license": "BSD-2-Clause",
"version": "2.6.2",
"version": "2.6.4",
"engines": {
"node": ">=0.8.0"
},

34
test/compress/ascii.js Normal file
View File

@@ -0,0 +1,34 @@
ascii_only_true: {
options = {}
beautify = {
ascii_only : true,
beautify : false,
}
input: {
function f() {
return "\x000\x001\x007\x008\x00" +
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" +
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
"\x20\x21\x22\x23 ... \x7d\x7e\x7f\x80\x81 ... \xfe\xff\u0fff\uffff";
}
}
expect_exact: 'function f(){return"\\x000\\x001\\x007\\08\\0"+"\\0\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\b\\t\\n\\x0B\\f\\r\\x0e\\x0f"+"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f"+\' !"# ... }~\\x7f\\x80\\x81 ... \\xfe\\xff\\u0fff\\uffff\'}'
}
ascii_only_false: {
options = {}
beautify = {
ascii_only : false,
beautify : false,
}
input: {
function f() {
return "\x000\x001\x007\x008\x00" +
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" +
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
"\x20\x21\x22\x23 ... \x7d\x7e\x7f\x80\x81 ... \xfe\xff\u0fff\uffff";
}
}
expect_exact: 'function f(){return"\\x000\\x001\\x007\\08\\0"+"\\0\x01\x02\x03\x04\x05\x06\x07\\b\\t\\n\\x0B\\f\\r\x0e\x0f"+"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"+\' !"# ... }~\x7f\x80\x81 ... \xfe\xff\u0fff\uffff\'}'
}

View File

@@ -92,7 +92,7 @@ asm_mixed: {
function logSum(start, end) {
start = 0 | start, end = 0 | end;
var sum = 0, p = 0, q = 0;
for (p = start << 3, q = end << 3; (0 | q) > (0 | p); p = p + 8 | 0) sum += +log(values[p >> 3]);
for (p = start << 3, q = end << 3; (0 | p) < (0 | q); p = p + 8 | 0) sum += +log(values[p >> 3]);
return +sum;
}
function geometricMean(start, end) {

View File

@@ -1153,3 +1153,59 @@ collapse_vars_short_circuited_conditions: {
}
}
collapse_vars_regexp: {
options = {
collapse_vars: true,
loops: false,
sequences: true,
dead_code: true,
conditionals: true,
comparisons: true,
evaluate: true,
booleans: true,
unused: true,
hoist_funs: true,
keep_fargs: true,
if_return: true,
join_vars: true,
cascade: true,
side_effects: true,
}
input: {
function f1() {
var k = 9;
var rx = /[A-Z]+/;
return [rx, k];
}
function f2() {
var rx = /[abc123]+/;
return function(s) {
return rx.exec(s);
};
}
(function(){
var result;
var s = 'acdabcdeabbb';
var rx = /ab*/g;
while (result = rx.exec(s)) {
console.log(result[0]);
}
})();
}
expect: {
function f1() {
return [/[A-Z]+/, 9];
}
function f2() {
var rx = /[abc123]+/;
return function(s) {
return rx.exec(s);
};
}
(function(){
var result, rx = /ab*/g;
while (result = rx.exec('acdabcdeabbb'))
console.log(result[0]);
})();
}
}

View File

@@ -0,0 +1,76 @@
keep_comparisons: {
options = {
comparisons: true,
unsafe_comps: false
}
input: {
var obj1 = {
valueOf: function() {triggeredFirst();}
}
var obj2 = {
valueOf: function() {triggeredSecond();}
}
var result1 = obj1 <= obj2;
var result2 = obj1 < obj2;
var result3 = obj1 >= obj2;
var result4 = obj1 > obj2;
}
expect: {
var obj1 = {
valueOf: function() {triggeredFirst();}
}
var obj2 = {
valueOf: function() {triggeredSecond();}
}
var result1 = obj1 <= obj2;
var result2 = obj1 < obj2;
var result3 = obj1 >= obj2;
var result4 = obj1 > obj2;
}
}
keep_comparisons_with_unsafe_comps: {
options = {
comparisons: true,
unsafe_comps: true
}
input: {
var obj1 = {
valueOf: function() {triggeredFirst();}
}
var obj2 = {
valueOf: function() {triggeredSecond();}
}
var result1 = obj1 <= obj2;
var result2 = obj1 < obj2;
var result3 = obj1 >= obj2;
var result4 = obj1 > obj2;
}
expect: {
var obj1 = {
valueOf: function() {triggeredFirst();}
}
var obj2 = {
valueOf: function() {triggeredSecond();}
}
var result1 = obj2 >= obj1;
var result2 = obj2 > obj1;
var result3 = obj1 >= obj2;
var result4 = obj1 > obj2;
}
}
dont_change_in_or_instanceof_expressions: {
input: {
1 in 1;
null in null;
1 instanceof 1;
null instanceof null;
}
expect: {
1 in 1;
null in null;
1 instanceof 1;
null instanceof null;
}
}

View File

@@ -11,6 +11,9 @@ concat_1: {
var d = 1 + x() + 2 + 3 + "boo";
var e = 1 + x() + 2 + "X" + 3 + "boo";
// be careful with concatentation with "\0" with octal-looking strings.
var f = "\0" + 360 + "\0" + 8 + "\0";
}
expect: {
var a = "foobar" + x() + "moofoo" + y() + "xyz" + q();
@@ -18,5 +21,6 @@ concat_1: {
var c = 1 + x() + 2 + "boo";
var d = 1 + x() + 2 + 3 + "boo";
var e = 1 + x() + 2 + "X3boo";
var f = "\x00360\08\0";
}
}

View File

@@ -407,8 +407,8 @@ cond_8: {
a = !condition;
a = !condition;
a = condition ? 1 : false;
a = condition ? 0 : true;
a = !!condition && 1;
a = !condition || 0;
a = condition ? 1 : 0;
}
}
@@ -490,8 +490,8 @@ cond_8b: {
a = !condition;
a = !condition;
a = condition ? 1 : !1;
a = condition ? 0 : !0;
a = !!condition && 1;
a = !condition || 0;
a = condition ? 1 : 0;
}
}
@@ -557,7 +557,7 @@ cond_8c: {
a = !!condition;
a = !condition;
a = condition() ? !0 : !-3.5;
a = !!condition() || !-3.5;
a = !!condition;
a = !!condition;
@@ -573,12 +573,68 @@ cond_8c: {
a = !condition;
a = !condition;
a = condition ? 1 : false;
a = condition ? 0 : true;
a = !!condition && 1;
a = !condition || 0;
a = condition ? 1 : 0;
}
}
ternary_boolean_consequent: {
options = {
collapse_vars:true, sequences:true, properties:true, dead_code:true, conditionals:true,
comparisons:true, evaluate:true, booleans:true, loops:true, unused:true, hoist_funs:true,
keep_fargs:true, if_return:true, join_vars:true, cascade:true, side_effects:true
}
input: {
function f1() { return a == b ? true : x; }
function f2() { return a == b ? false : x; }
function f3() { return a < b ? !0 : x; }
function f4() { return a < b ? !1 : x; }
function f5() { return c ? !0 : x; }
function f6() { return c ? false : x; }
function f7() { return !c ? true : x; }
function f8() { return !c ? !1 : x; }
}
expect: {
function f1() { return a == b || x; }
function f2() { return a != b && x; }
function f3() { return a < b || x; }
function f4() { return !(a < b) && x; }
function f5() { return !!c || x; }
function f6() { return !c && x; }
function f7() { return !c || x; }
function f8() { return !!c && x; }
}
}
ternary_boolean_alternative: {
options = {
collapse_vars:true, sequences:true, properties:true, dead_code:true, conditionals:true,
comparisons:true, evaluate:true, booleans:true, loops:true, unused:true, hoist_funs:true,
keep_fargs:true, if_return:true, join_vars:true, cascade:true, side_effects:true
}
input: {
function f1() { return a == b ? x : true; }
function f2() { return a == b ? x : false; }
function f3() { return a < b ? x : !0; }
function f4() { return a < b ? x : !1; }
function f5() { return c ? x : true; }
function f6() { return c ? x : !1; }
function f7() { return !c ? x : !0; }
function f8() { return !c ? x : false; }
}
expect: {
function f1() { return a != b || x; }
function f2() { return a == b && x; }
function f3() { return !(a < b) || x; }
function f4() { return a < b && x; }
function f5() { return !c || x; }
function f6() { return !!c && x; }
function f7() { return !!c || x; }
function f8() { return !c && x; }
}
}
conditional_and: {
options = {
conditionals: true,
@@ -812,3 +868,41 @@ trivial_boolean_ternary_expressions : {
f(!(x >= y));
}
}
issue_1154: {
options = {
conditionals: true,
evaluate : true,
booleans : true,
};
input: {
function f1(x) { return x ? -1 : -1; }
function f2(x) { return x ? +2 : +2; }
function f3(x) { return x ? ~3 : ~3; }
function f4(x) { return x ? !4 : !4; }
function f5(x) { return x ? void 5 : void 5; }
function f6(x) { return x ? typeof 6 : typeof 6; }
function g1() { return g() ? -1 : -1; }
function g2() { return g() ? +2 : +2; }
function g3() { return g() ? ~3 : ~3; }
function g4() { return g() ? !4 : !4; }
function g5() { return g() ? void 5 : void 5; }
function g6() { return g() ? typeof 6 : typeof 6; }
}
expect: {
function f1(x) { return -1; }
function f2(x) { return 2; }
function f3(x) { return -4; }
function f4(x) { return !1; }
function f5(x) { return; }
function f6(x) { return "number"; }
function g1() { return g(), -1; }
function g2() { return g(), 2; }
function g3() { return g(), -4; }
function g4() { return g(), !1; }
function g5() { return g(), void 0; }
function g6() { return g(), "number"; }
}
}

39
test/compress/evaluate.js Normal file
View File

@@ -0,0 +1,39 @@
negative_zero: {
options = { evaluate: true }
input: {
console.log(
-"",
- -"",
1 / (-0),
1 / (-"")
);
}
expect: {
console.log(
-0,
0,
1 / (-0),
1 / (-0)
);
}
}
positive_zero: {
options = { evaluate: true }
input: {
console.log(
+"",
+ -"",
1 / (+0),
1 / (+"")
);
}
expect: {
console.log(
0,
-0,
1 / (0),
1 / (0)
);
}
}

207
test/compress/if_return.js Normal file
View File

@@ -0,0 +1,207 @@
if_return_1: {
options = {
if_return : true,
sequences : true,
conditionals : true,
comparisons : true,
evaluate : true,
booleans : true,
unused : true,
side_effects : true,
dead_code : true,
}
input: {
function f(x) {
if (x) {
return true;
}
}
}
expect: {
function f(x){if(x)return!0}
}
}
if_return_2: {
options = {
if_return : true,
sequences : true,
conditionals : true,
comparisons : true,
evaluate : true,
booleans : true,
unused : true,
side_effects : true,
dead_code : true,
}
input: {
function f(x, y) {
if (x)
return 3;
if (y)
return c();
}
}
expect: {
function f(x,y){return x?3:y?c():void 0}
}
}
if_return_3: {
options = {
if_return : true,
sequences : true,
conditionals : true,
comparisons : true,
evaluate : true,
booleans : true,
unused : true,
side_effects : true,
dead_code : true,
}
input: {
function f(x) {
a();
if (x) {
b();
return false;
}
}
}
expect: {
function f(x){if(a(),x)return b(),!1}
}
}
if_return_4: {
options = {
if_return : true,
sequences : true,
conditionals : true,
comparisons : true,
evaluate : true,
booleans : true,
unused : true,
side_effects : true,
dead_code : true,
}
input: {
function f(x, y) {
a();
if (x) return 3;
b();
if (y) return c();
}
}
expect: {
function f(x,y){return a(),x?3:(b(),y?c():void 0)}
}
}
if_return_5: {
options = {
if_return : true,
sequences : true,
conditionals : true,
comparisons : true,
evaluate : true,
booleans : true,
unused : true,
side_effects : true,
dead_code : true,
}
input: {
function f() {
if (x)
return;
return 7;
if (y)
return j;
}
}
expect: {
function f(){if(!x)return 7}
}
}
if_return_6: {
options = {
if_return : true,
sequences : true,
conditionals : true,
comparisons : true,
evaluate : true,
booleans : true,
unused : true,
side_effects : true,
dead_code : true,
}
input: {
function f(x) {
return x ? true : void 0;
return y;
}
}
expect: {
// suboptimal
function f(x){return!!x||void 0}
}
}
if_return_7: {
options = {
if_return : true,
sequences : true,
conditionals : true,
comparisons : true,
evaluate : true,
booleans : true,
unused : true,
side_effects : true,
dead_code : true,
}
input: {
function f(x) {
if (x) {
return true;
}
foo();
bar();
}
}
expect: {
// suboptimal
function f(x){return!!x||(foo(),void bar())}
}
}
issue_1089: {
options = {
if_return : true,
sequences : true,
conditionals : true,
comparisons : true,
evaluate : true,
booleans : true,
unused : true,
side_effects : true,
dead_code : true,
}
input: {
function x() {
var f = document.getElementById("fname");
if (f.files[0].size > 12345) {
alert("alert");
f.focus();
return false;
}
}
}
expect: {
function x() {
var f = document.getElementById("fname");
if (f.files[0].size > 12345)
return alert("alert"), f.focus(), !1;
}
}
}

121
test/compress/issue-1034.js Normal file
View File

@@ -0,0 +1,121 @@
non_hoisted_function_after_return: {
options = {
hoist_funs: false, dead_code: true, conditionals: true, comparisons: true,
evaluate: true, booleans: true, loops: true, unused: true, keep_fargs: true,
if_return: true, join_vars: true, cascade: true, side_effects: true
}
input: {
function foo(x) {
if (x) {
return bar();
not_called1();
} else {
return baz();
not_called2();
}
function bar() { return 7; }
return not_reached;
function UnusedFunction() {}
function baz() { return 8; }
}
}
expect: {
function foo(x) {
return x ? bar() : baz();
function bar() { return 7 }
function baz() { return 8 }
}
}
expect_warnings: [
'WARN: Dropping unreachable code [test/compress/issue-1034.js:11,16]',
"WARN: Dropping unreachable code [test/compress/issue-1034.js:14,16]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:17,12]",
"WARN: Dropping unused function UnusedFunction [test/compress/issue-1034.js:18,21]"
]
}
non_hoisted_function_after_return_2a: {
options = {
hoist_funs: false, dead_code: true, conditionals: true, comparisons: true,
evaluate: true, booleans: true, loops: true, unused: true, keep_fargs: true,
if_return: true, join_vars: true, cascade: true, side_effects: true,
collapse_vars: false, passes: 2
}
input: {
function foo(x) {
if (x) {
return bar(1);
var a = not_called(1);
} else {
return bar(2);
var b = not_called(2);
}
var c = bar(3);
function bar(x) { return 7 - x; }
function nope() {}
return b || c;
}
}
expect: {
function foo(x) {
return bar(x ? 1 : 2);
function bar(x) {
return 7 - x;
}
}
}
expect_warnings: [
"WARN: Dropping unreachable code [test/compress/issue-1034.js:48,16]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:48,16]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:51,16]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:51,16]",
"WARN: Dropping unused variable a [test/compress/issue-1034.js:48,20]",
"WARN: Dropping unused function nope [test/compress/issue-1034.js:55,21]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:53,12]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:53,12]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:56,12]",
"WARN: Dropping unused variable b [test/compress/issue-1034.js:51,20]",
"WARN: Dropping unused variable c [test/compress/issue-1034.js:53,16]"
]
}
non_hoisted_function_after_return_2b: {
options = {
hoist_funs: false, dead_code: true, conditionals: true, comparisons: true,
evaluate: true, booleans: true, loops: true, unused: true, keep_fargs: true,
if_return: true, join_vars: true, cascade: true, side_effects: true,
collapse_vars: false
}
input: {
function foo(x) {
if (x) {
return bar(1);
} else {
return bar(2);
var b;
}
var c = bar(3);
function bar(x) {
return 7 - x;
}
return b || c;
}
}
expect: {
function foo(x) {
return bar(x ? 1 : 2);
function bar(x) { return 7 - x; }
}
}
expect_warnings: [
// duplicate warnings no longer emitted
"WARN: Dropping unreachable code [test/compress/issue-1034.js:95,16]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:95,16]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:97,12]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:97,12]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:101,12]",
"WARN: Dropping unused variable b [test/compress/issue-1034.js:95,20]",
"WARN: Dropping unused variable c [test/compress/issue-1034.js:97,16]"
]
}

View File

@@ -0,0 +1,39 @@
const_declaration: {
options = {
evaluate: true
};
input: {
const goog = goog || {};
}
expect: {
const goog = goog || {};
}
}
const_pragma: {
options = {
evaluate: true
};
input: {
/** @const */ var goog = goog || {};
}
expect: {
var goog = goog || {};
}
}
// for completeness' sake
not_const: {
options = {
evaluate: true
};
input: {
var goog = goog || {};
}
expect: {
var goog = goog || {};
}
}

View File

@@ -0,0 +1,96 @@
multiple_functions: {
options = { if_return: true, hoist_funs: false };
input: {
( function() {
if ( !window ) {
return;
}
function f() {}
function g() {}
} )();
}
expect: {
( function() {
function f() {}
function g() {}
// NOTE: other compression steps will reduce this
// down to just `window`.
if ( window );
} )();
}
}
single_function: {
options = { if_return: true, hoist_funs: false };
input: {
( function() {
if ( !window ) {
return;
}
function f() {}
} )();
}
expect: {
( function() {
function f() {}
if ( window );
} )();
}
}
deeply_nested: {
options = { if_return: true, hoist_funs: false };
input: {
( function() {
if ( !window ) {
return;
}
function f() {}
function g() {}
if ( !document ) {
return;
}
function h() {}
} )();
}
expect: {
( function() {
function f() {}
function g() {}
function h() {}
// NOTE: other compression steps will reduce this
// down to just `window`.
if ( window )
if (document);
} )();
}
}
not_hoisted_when_already_nested: {
options = { if_return: true, hoist_funs: false };
input: {
( function() {
if ( !window ) {
return;
}
if ( foo ) function f() {}
} )();
}
expect: {
( function() {
if ( window )
if ( foo ) function f() {}
} )();
}
}

240
test/compress/issue-1105.js Normal file
View File

@@ -0,0 +1,240 @@
with_in_global_scope: {
options = {
unused: true
}
input: {
var o = 42;
with(o) {
var foo = 'something'
}
doSomething(o);
}
expect: {
var o=42;
with(o)
var foo = "something";
doSomething(o);
}
}
with_in_function_scope: {
options = {
unused: true
}
input: {
function foo() {
var o = 42;
with(o) {
var foo = "something"
}
doSomething(o);
}
}
expect: {
function foo() {
var o=42;
with(o)
var foo = "something";
doSomething(o)
}
}
}
compress_with_with_in_other_scope: {
options = {
unused: true
}
input: {
function foo() {
var o = 42;
with(o) {
var foo = "something"
}
doSomething(o);
}
function bar() {
var unused = 42;
return something();
}
}
expect: {
function foo() {
var o = 42;
with(o)
var foo = "something";
doSomething(o)
}
function bar() {
return something()
}
}
}
with_using_existing_variable_outside_scope: {
options = {
unused: true
}
input: {
function f() {
var o = {};
var unused = {}; // Doesn't get removed because upper scope uses with
function foo() {
with(o) {
var foo = "something"
}
doSomething(o);
}
foo()
}
}
expect: {
function f() {
var o = {};
var unused = {};
function foo() {
with(o)
var foo = "something";
doSomething(o)
}
foo()
}
}
}
check_drop_unused_in_peer_function: {
options = {
unused: true
}
input: {
function outer() {
var o = {};
var unused = {}; // should be kept
function foo() { // should be kept
function not_in_use() {
var nested_unused = "foo"; // should be dropped
return 24;
}
var unused = {}; // should be kept
with (o) {
var foo = "something";
}
doSomething(o);
}
function bar() {
var unused = {}; // should be dropped
doSomethingElse();
}
foo();
bar();
}
}
expect: {
function outer() {
var o = {};
var unused = {}; // should be kept
function foo() { // should be kept
function not_in_use() {
return 24;
}
var unused = {}; // should be kept
with (o)
var foo = "something";
doSomething(o);
}
function bar() {
doSomethingElse();
}
foo();
bar();
}
}
}
Infinity_not_in_with_scope: {
options = {
unused: true
}
input: {
var o = { Infinity: 'oInfinity' };
var vInfinity = "Infinity";
vInfinity = Infinity;
}
expect: {
var o = { Infinity: 'oInfinity' }
var vInfinity = "Infinity"
vInfinity = 1/0
}
}
Infinity_in_with_scope: {
options = {
unused: true
}
input: {
var o = { Infinity: 'oInfinity' };
var vInfinity = "Infinity";
with (o) { vInfinity = Infinity; }
}
expect: {
var o = { Infinity: 'oInfinity' }
var vInfinity = "Infinity"
with (o) vInfinity = Infinity
}
}
assorted_Infinity_NaN_undefined_in_with_scope: {
options = {
unused: true,
evaluate: true,
dead_code: true,
conditionals: true,
comparisons: true,
booleans: true,
hoist_funs: true,
keep_fargs: true,
if_return: true,
join_vars: true,
cascade: true,
side_effects: true,
sequences: false,
}
input: {
var o = {
undefined : 3,
NaN : 4,
Infinity : 5,
}
if (o) {
f(undefined, void 0);
f(NaN, 0/0);
f(Infinity, 1/0);
f(-Infinity, -(1/0));
f(2 + 7 + undefined, 2 + 7 + void 0);
}
with (o) {
f(undefined, void 0);
f(NaN, 0/0);
f(Infinity, 1/0);
f(-Infinity, -(1/0));
f(2 + 7 + undefined, 2 + 7 + void 0);
}
}
expect: {
var o = {
undefined : 3,
NaN : 4,
Infinity : 5
}
if (o) {
f(void 0, void 0);
f(NaN, NaN);
f(1/0, 1/0);
f(-(1/0), -(1/0));
f(NaN, NaN);
}
with (o) {
f(undefined, void 0);
f(NaN, 0/0);
f(Infinity, 1/0);
f(-Infinity, -(1/0));
f(9 + undefined, 9 + void 0);
}
}
}

View File

@@ -84,5 +84,5 @@ eval_mangle: {
return a + eval('c');
}
}
expect_exact: 'function f1(n,c,e,a,o){return n("c")+c}function f2(a,b,c,d,e){return a+eval("c")}function f3(a,eval,c,d,e){return a+eval("c")}'
expect_exact: 'function f1(n,c,e,a,f){return n("c")+c}function f2(a,b,c,d,e){return a+eval("c")}function f3(a,eval,c,d,e){return a+eval("c")}'
}

View File

@@ -74,3 +74,59 @@ negate_iife_4: {
}();
}
}
negate_iife_nested: {
options = {
negate_iife: true,
sequences: true,
conditionals: true,
};
input: {
function Foo(f) {
this.f = f;
}
new Foo(function() {
(function(x) {
(function(y) {
console.log(y);
})(x);
})(7);
}).f();
}
expect: {
function Foo(f) {
this.f = f;
}
new Foo(function() {
!function(x) {
!function(y) {
console.log(y);
}(x);
}(7);
}).f();
}
}
negate_iife_issue_1073: {
options = {
negate_iife: true,
sequences: true,
conditionals: true,
};
input: {
new (function(a) {
return function Foo() {
this.x = a;
console.log(this);
};
}(7))();
}
expect: {
new (function(a) {
return function Foo() {
this.x = a,
console.log(this);
};
}(7))();
}
}

View File

@@ -10,3 +10,43 @@ new_statement: {
}
expect_exact: "new x(1);new x(1)(2);new x(1)(2)(3);new new x(1);new new x(1)(2);new new x(1)(2);(new new x(1))(2);"
}
new_statements_2: {
input: {
new x;
new new x;
new new new x;
new true;
new (0);
new (!0);
new (bar = function(foo) {this.foo=foo;})(123);
new (bar = function(foo) {this.foo=foo;})();
}
expect_exact: "new x;new(new x);new(new(new x));new true;new 0;new(!0);new(bar=function(foo){this.foo=foo})(123);new(bar=function(foo){this.foo=foo});"
}
new_statements_3: {
input: {
new (function(foo){this.foo=foo;})(1);
new (function(foo){this.foo=foo;})();
new (function test(foo){this.foo=foo;})(1);
new (function test(foo){this.foo=foo;})();
}
expect_exact: "new function(foo){this.foo=foo}(1);new function(foo){this.foo=foo};new function test(foo){this.foo=foo}(1);new function test(foo){this.foo=foo};"
}
new_with_rewritten_true_value: {
options = { booleans: true }
input: {
new true;
}
expect_exact: "new(!0);"
}
new_with_many_parameters: {
input: {
new foo.bar("baz");
new x(/123/, 456);
}
expect_exact: 'new foo.bar("baz");new x(/123/,456);'
}

19
test/compress/numbers.js Normal file
View File

@@ -0,0 +1,19 @@
hex_numbers_in_parentheses_for_prototype_functions: {
input: {
(-2);
(-2).toFixed(0);
(2);
(2).toFixed(0);
(0.2);
(0.2).toFixed(0);
(0.00000002);
(0.00000002).toFixed(0);
(1000000000000000128);
(1000000000000000128).toFixed(0);
}
expect_exact: "-2;(-2).toFixed(0);2;2..toFixed(0);.2;.2.toFixed(0);2e-8;2e-8.toFixed(0);0xde0b6b3a7640080;(0xde0b6b3a7640080).toFixed(0);"
}

View File

@@ -72,3 +72,54 @@ evaluate_length: {
a = ("foo" + b).length;
}
}
mangle_properties: {
mangle_props = {
ignore_quoted: false
};
input: {
a["foo"] = "bar";
a.color = "red";
x = {"bar": 10};
}
expect: {
a["a"] = "bar";
a.b = "red";
x = {c: 10};
}
}
mangle_unquoted_properties: {
mangle_props = {
ignore_quoted: true
}
beautify = {
beautify: false,
quote_style: 3,
keep_quoted_props: true,
}
input: {
function f1() {
a["foo"] = "bar";
a.color = "red";
x = {"bar": 10};
}
function f2() {
a.foo = "bar";
a['color'] = "red";
x = {bar: 10};
}
}
expect: {
function f1() {
a["foo"] = "bar";
a.a = "red";
x = {"bar": 10};
}
function f2() {
a.b = "bar";
a['color'] = "red";
x = {c: 10};
}
}
}

View File

@@ -0,0 +1,27 @@
var Uglify = require('../../');
var assert = require("assert");
describe("comment before constant", function() {
var js = 'function f() { /*c1*/ var /*c2*/ foo = /*c3*/ false; return foo; }';
it("Should test comment before constant is retained and output after mangle.", function() {
var result = Uglify.minify(js, {
fromString: true,
compress: { collapse_vars: false },
mangle: {},
output: { comments: true },
});
assert.strictEqual(result.code, 'function f(){/*c1*/var/*c2*/n=/*c3*/!1;return n}');
});
it("Should test code works when comments disabled.", function() {
var result = Uglify.minify(js, {
fromString: true,
compress: { collapse_vars: false },
mangle: {},
output: {},
});
assert.strictEqual(result.code, 'function f(){var n=!1;return n}');
});
});

370
test/mocha/directives.js Normal file
View File

@@ -0,0 +1,370 @@
var assert = require("assert");
var uglify = require("../../");
describe("Directives", function() {
it ("Should allow tokenizer to store directives state", function() {
var tokenizer = uglify.tokenizer("", "foo.js");
// Stack level 0
assert.strictEqual(tokenizer.has_directive("use strict"), false);
assert.strictEqual(tokenizer.has_directive("use asm"), false);
assert.strictEqual(tokenizer.has_directive("use thing"), false);
// Stack level 2
tokenizer.push_directives_stack();
tokenizer.push_directives_stack();
tokenizer.add_directive("use strict");
assert.strictEqual(tokenizer.has_directive("use strict"), true);
assert.strictEqual(tokenizer.has_directive("use asm"), false);
assert.strictEqual(tokenizer.has_directive("use thing"), false);
// Stack level 3
tokenizer.push_directives_stack();
tokenizer.add_directive("use strict");
tokenizer.add_directive("use asm");
assert.strictEqual(tokenizer.has_directive("use strict"), true);
assert.strictEqual(tokenizer.has_directive("use asm"), true);
assert.strictEqual(tokenizer.has_directive("use thing"), false);
// Stack level 2
tokenizer.pop_directives_stack();
assert.strictEqual(tokenizer.has_directive("use strict"), true);
assert.strictEqual(tokenizer.has_directive("use asm"), false);
assert.strictEqual(tokenizer.has_directive("use thing"), false);
// Stack level 3
tokenizer.push_directives_stack();
tokenizer.add_directive("use thing");
tokenizer.add_directive("use\\\nasm");
assert.strictEqual(tokenizer.has_directive("use strict"), true);
assert.strictEqual(tokenizer.has_directive("use asm"), false); // Directives are strict!
assert.strictEqual(tokenizer.has_directive("use thing"), true);
// Stack level 2
tokenizer.pop_directives_stack();
assert.strictEqual(tokenizer.has_directive("use strict"), true);
assert.strictEqual(tokenizer.has_directive("use asm"), false);
assert.strictEqual(tokenizer.has_directive("use thing"), false);
// Stack level 1
tokenizer.pop_directives_stack();
assert.strictEqual(tokenizer.has_directive("use strict"), false);
assert.strictEqual(tokenizer.has_directive("use asm"), false);
assert.strictEqual(tokenizer.has_directive("use thing"), false);
// Stack level 0
tokenizer.pop_directives_stack();
assert.strictEqual(tokenizer.has_directive("use strict"), false);
assert.strictEqual(tokenizer.has_directive("use asm"), false);
assert.strictEqual(tokenizer.has_directive("use thing"), false);
});
it("Should know which strings are directive and which ones are not", function() {
var test_directive = function(tokenizer, test) {
test.directives.map(function(directive) {
assert.strictEqual(tokenizer.has_directive(directive), true, directive + " in " + test.input);
});
test.non_directives.map(function(fake_directive) {
assert.strictEqual(tokenizer.has_directive(fake_directive), false, fake_directive + " in " + test.input);
});
}
var tests = [
{
input: '"use strict"\n',
directives: ["use strict"],
non_directives: ["use asm"]
},
{
input: '"use\\\nstrict";',
directives: [],
non_directives: ["use strict", "use\nstrict", "use \nstrict", "use asm"]
},
{
input: '"use strict"\n"use asm"\n"use bar"\n',
directives: ["use strict", "use asm", "use bar"],
non_directives: ["use foo", "use\\x20strict"]
},
{
input: '"use \\\nstrict";"use strict";',
directives: [],
non_directives: ["use strict", "use\nstrict", "use \nstrict", "use asm"]
},
{
input: '"\\76";',
directives: [],
non_directives: [">", "\\76"]
},
{
input: '"use strict"', // no ; or newline
directives: [],
non_directives: ["use strict", "use\nstrict", "use \nstrict", "use asm"]
},
{
input: ';"use strict"',
directives: [],
non_directives: ["use strict", "use\nstrict", "use \nstrict", "use asm"]
},
// Duplicate above code but put it in a function
{
input: 'function foo() {"use strict"\n',
directives: ["use strict"],
non_directives: ["use asm"]
},
{
input: 'function foo() {"use\\\nstrict";',
directives: [],
non_directives: ["use strict", "use\nstrict", "use \nstrict", "use asm"]
},
{
input: 'function foo() {"use strict"\n"use asm"\n"use bar"\n',
directives: ["use strict", "use asm", "use bar"],
non_directives: ["use foo", "use\\x20strict"]
},
{
input: 'function foo() {"use \\\nstrict";"use strict";',
directives: [],
non_directives: ["use strict", "use\nstrict", "use \nstrict", "use asm"]
},
{
input: 'var foo = function() {"\\76";',
directives: [],
non_directives: [">", "\\76"]
},
{
input: 'var foo = function() {"use strict"', // no ; or newline
directives: [],
non_directives: ["use strict", "use\nstrict", "use \nstrict", "use asm"]
},
{
input: 'var foo = function() {;"use strict"',
directives: [],
non_directives: ["use strict", "use\nstrict", "use \nstrict", "use asm"]
},
// Special cases
{
input: '"1";"2";"3";"4";;"5"',
directives: ["1", "2", "3", "4"],
non_directives: ["5", "6", "use strict", "use asm"]
},
{
input: 'if(1){"use strict";',
directives: [],
non_directives: ["use strict", "use\nstrict", "use \nstrict", "use asm"]
},
{
input: '"use strict";try{"use asm";',
directives: ["use strict"],
non_directives: ["use\nstrict", "use \nstrict", "use asm"]
}
];
for (var i = 0; i < tests.length; i++) {
// Fail parser deliberately to get state at failure
var tokenizer = uglify.tokenizer(tests[i].input + "]", "foo.js");
try {
var parser = uglify.parse(tokenizer);
throw new Error("Expected parser to fail");
} catch (e) {
assert.strictEqual(e instanceof uglify.JS_Parse_Error, true);
assert.strictEqual(e.message, "SyntaxError: Unexpected token: punc (])");
}
test_directive(tokenizer, tests[i]);
}
});
it("Should test EXPECT_DIRECTIVE RegExp", function() {
var tests = [
["", true],
["'test';", true],
["'test';;", true],
["'tests';\n", true],
["'tests'", false],
["'tests'; \n\t", true],
["'tests';\n\n", true],
["\n\n\"use strict\";\n\n", true]
];
for (var i = 0; i < tests.length; i++) {
assert.strictEqual(uglify.EXPECT_DIRECTIVE.test(tests[i][0]), tests[i][1], tests[i][0]);
}
});
it("Should only print 2 semicolons spread over 2 lines in beautify mode", function() {
assert.strictEqual(
uglify.minify(
'"use strict";\'use strict\';"use strict";"use strict";;\'use strict\';console.log(\'use strict\');',
{fromString: true, output: {beautify: true, quote_style: 3}, compress: false}
).code,
'"use strict";\n\n\'use strict\';\n\n"use strict";\n\n"use strict";\n\n;\'use strict\';\n\nconsole.log(\'use strict\');'
);
});
it("Should not add double semicolons in non-scoped block statements to avoid strings becoming directives", function() {
var tests = [
[
'{"use\x20strict"}',
'{"use strict"}'
],
[
'function foo(){"use\x20strict";}', // Valid place for directives
'function foo(){"use strict"}'
],
[
'try{"use\x20strict"}catch(e){}finally{"use\x20strict"}',
'try{"use strict"}catch(e){}finally{"use strict"}'
],
[
'if(1){"use\x20strict"} else {"use strict"}',
'if(1){"use strict"}else{"use strict"}'
]
];
for (var i = 0; i < tests.length; i++) {
assert.strictEqual(
uglify.minify(tests[i][0], {fromString: true, quote_style: 3, compress: false, mangle: false}).code,
tests[i][1],
tests[i][0]
);
}
});
it("Should add double semicolon when relying on automatic semicolon insertion", function() {
var code = uglify.minify('"use strict";"use\\x20strict";',
{fromString: true, output: {semicolons: false}, compress: false}
).code;
assert.strictEqual(code, '"use strict";;"use strict"\n');
});
it("Should check quote style of directives", function() {
var tests = [
// 0. Prefer double quotes, unless string contains more double quotes than single quotes
[
'"testing something";',
0,
'"testing something";'
],
[
"'use strict';",
0,
'"use strict";'
],
[
'"\\\'use strict\\\'";', // Not a directive as it contains quotes
0,
';"\'use strict\'";',
],
[
"'\"use strict\"';",
0,
"'\"use strict\"';",
],
// 1. Always use single quote
[
'"testing something";',
1,
"'testing something';"
],
[
"'use strict';",
1,
"'use strict';"
],
[
'"\'use strict\'";',
1,
// Intentionally causes directive breakage at cost of less logic, usage should be rare anyway
"'\\'use strict\\'';",
],
[
"'\\'use strict\\'';", // Not a valid directive
1,
"'\\'use strict\\'';" // But no ; necessary as directive stays invalid
],
[
"'\"use strict\"';",
1,
"'\"use strict\"';",
],
// 2. Always use double quote
[
'"testing something";',
2,
'"testing something";'
],
[
"'use strict';",
2,
'"use strict";'
],
[
'"\'use strict\'";',
2,
"\"'use strict'\";",
],
[
"'\"use strict\"';",
2,
// Intentionally causes directive breakage at cost of less logic, usage should be rare anyway
'"\\\"use strict\\\"";',
],
[
'"\\"use strict\\"";', // Not a valid directive
2,
'"\\"use strict\\"";' // But no ; necessary as directive stays invalid
],
// 3. Always use original
[
'"testing something";',
3,
'"testing something";'
],
[
"'use strict';",
3,
"'use strict';",
],
[
'"\'use strict\'";',
3,
'"\'use strict\'";',
],
[
"'\"use strict\"';",
3,
"'\"use strict\"';",
],
];
for (var i = 0; i < tests.length; i++) {
assert.strictEqual(
uglify.minify(tests[i][0], {fromString: true, output:{quote_style: tests[i][1]}, compress: false}).code,
tests[i][2],
tests[i][0] + " using mode " + tests[i][1]
);
}
});
it("Should be able to compress without side effects", function() {
// NOTE: the "use asm" directive disables any optimisation after being defined
var tests = [
[
'"use strict";"use strict";"use strict";"use foo";"use strict";;"use sloppy";doSomething("foo");',
'"use strict";"use foo";doSomething("foo");'
],
[
// Nothing gets optimised in the compressor because "use asm" is the first statement
'"use asm";"use\\x20strict";1+1;',
'"use asm";;"use strict";1+1;' // Yet, the parser noticed that "use strict" wasn't a directive
]
];
for (var i = 0; i < tests.length; i++) {
assert.strictEqual(
uglify.minify(tests[i][0], {fromString: true, compress: {collapse_vars: true, side_effects: true}}).code,
tests[i][1],
tests[i][0]
);
}
});
});

View File

@@ -71,7 +71,7 @@ describe("Getters and setters", function() {
var fail = function(data) {
return function (e) {
return e instanceof UglifyJS.JS_Parse_Error &&
e.message === "Invalid getter/setter name: " + data.operator;
e.message === "SyntaxError: Invalid getter/setter name: " + data.operator;
};
};

View File

@@ -0,0 +1,19 @@
var Uglify = require('../../');
var assert = require("assert");
describe("Huge number of comments.", function() {
it("Should parse and compress code with thousands of consecutive comments", function() {
var js = 'function lots_of_comments(x) { return 7 -';
var i;
for (i = 1; i <= 5000; ++i) { js += "// " + i + "\n"; }
for (; i <= 10000; ++i) { js += "/* " + i + " */ /**/"; }
js += "x; }";
var result = Uglify.minify(js, {
fromString: true,
mangle: false,
compress: {}
});
assert.strictEqual(result.code, "function lots_of_comments(x){return 7-x}");
});
});

30
test/mocha/let.js Normal file
View File

@@ -0,0 +1,30 @@
var Uglify = require('../../');
var assert = require("assert");
describe("let", function() {
it("Should not produce `let` as a variable name in mangle", function(done) {
this.timeout(10000);
// Produce a lot of variables in a function and run it through mangle.
var s = '"use strict"; function foo() {';
for (var i = 0; i < 21000; ++i) {
s += "var v" + i + "=0;";
}
s += '}';
var result = Uglify.minify(s, {fromString: true, compress: false});
// Verify that select keywords and reserved keywords not produced
assert.strictEqual(result.code.indexOf("var let="), -1);
assert.strictEqual(result.code.indexOf("var do="), -1);
assert.strictEqual(result.code.indexOf("var var="), -1);
// Verify that the variable names that appeared immediately before
// and after the erroneously generated `let` variable name still exist
// to show the test generated enough symbols.
assert(result.code.indexOf("var ket=") >= 0);
assert(result.code.indexOf("var met=") >= 0);
done();
});
});

View File

@@ -0,0 +1,56 @@
var Uglify = require('../../');
var assert = require("assert");
describe("line-endings", function() {
var options = {
fromString: true,
mangle: false,
compress: false,
output: {
beautify: false,
comments: /^!/,
}
};
var expected_code = '/*!one\n2\n3*/\nfunction f(x){if(x)return 3}';
it("Should parse LF line endings", function() {
var js = '/*!one\n2\n3*///comment\nfunction f(x) {\n if (x)\n//comment\n return 3;\n}\n';
var result = Uglify.minify(js, options);
assert.strictEqual(result.code, expected_code);
});
it("Should parse CR/LF line endings", function() {
var js = '/*!one\r\n2\r\n3*///comment\r\nfunction f(x) {\r\n if (x)\r\n//comment\r\n return 3;\r\n}\r\n';
var result = Uglify.minify(js, options);
assert.strictEqual(result.code, expected_code);
});
it("Should parse CR line endings", function() {
var js = '/*!one\r2\r3*///comment\rfunction f(x) {\r if (x)\r//comment\r return 3;\r}\r';
var result = Uglify.minify(js, options);
assert.strictEqual(result.code, expected_code);
});
it("Should not allow line terminators in regexp", function() {
var inputs = [
"/\n/",
"/\r/",
"/\u2028/",
"/\u2029/",
"/someRandomTextLike[]()*AndThen\n/"
]
var test = function(input) {
return function() {
Uglify.parse(input);
}
}
var fail = function(e) {
return e instanceof Uglify.JS_Parse_Error &&
e.message === "Unexpected line terminator";
}
for (var i = 0; i < inputs.length; i++) {
assert.throws(test(inputs[i]), fail);
}
});
});

62
test/mocha/minify.js Normal file
View File

@@ -0,0 +1,62 @@
var Uglify = require('../../');
var assert = require("assert");
describe("minify", function() {
it("Should test basic sanity of minify with default options", function() {
var js = 'function foo(bar) { if (bar) return 3; else return 7; var u = not_called(); }';
var result = Uglify.minify(js, {fromString: true});
assert.strictEqual(result.code, 'function foo(n){return n?3:7}');
});
describe("keep_quoted_props", function() {
it("Should preserve quotes in object literals", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
var result = Uglify.minify(js, {
fromString: true, output: {
keep_quoted_props: true
}});
assert.strictEqual(result.code, 'var foo={"x":1,y:2,"z":3};');
});
it("Should preserve quote styles when quote_style is 3", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
var result = Uglify.minify(js, {
fromString: true, output: {
keep_quoted_props: true,
quote_style: 3
}});
assert.strictEqual(result.code, 'var foo={"x":1,y:2,\'z\':3};');
});
it("Should not preserve quotes in object literals when disabled", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
var result = Uglify.minify(js, {
fromString: true, output: {
keep_quoted_props: false,
quote_style: 3
}});
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
});
});
describe("mangleProperties", function() {
it("Shouldn't mangle quoted properties", function() {
var js = 'a["foo"] = "bar"; a.color = "red"; x = {"bar": 10};';
var result = Uglify.minify(js, {
fromString: true,
compress: {
properties: false
},
mangleProperties: {
ignore_quoted: true
},
output: {
keep_quoted_props: true,
quote_style: 3
}
});
assert.strictEqual(result.code,
'a["foo"]="bar",a.a="red",x={"bar":10};');
});
});
});

88
test/mocha/new.js Normal file
View File

@@ -0,0 +1,88 @@
var assert = require("assert");
var uglify = require("../../");
describe("New", function() {
it("Should add trailing parentheses for new expressions with zero arguments in beautify mode", function() {
var tests = [
"new x(1);",
"new x;",
"new new x;",
"new (function(foo){this.foo=foo;})(1);",
"new (function(foo){this.foo=foo;})();",
"new (function test(foo){this.foo=foo;})(1);",
"new (function test(foo){this.foo=foo;})();",
"new true;",
"new (0);",
"new (!0);",
"new (bar = function(foo) {this.foo=foo;})(123);",
"new (bar = function(foo) {this.foo=foo;})();"
];
var expected = [
"new x(1);",
"new x();",
"new new x()();",
"new function(foo) {\n this.foo = foo;\n}(1);",
"new function(foo) {\n this.foo = foo;\n}();",
"new function test(foo) {\n this.foo = foo;\n}(1);",
"new function test(foo) {\n this.foo = foo;\n}();",
"new true();",
"new 0();",
"new (!0)();",
"new (bar = function(foo) {\n this.foo = foo;\n})(123);",
"new (bar = function(foo) {\n this.foo = foo;\n})();"
];
for (var i = 0; i < tests.length; i++) {
assert.strictEqual(
uglify.minify(tests[i], {
fromString: true,
output: {beautify: true},
compress: false,
mangle: false
}).code,
expected[i]
);
}
});
it("Should not add trailing parentheses for new expressions with zero arguments in non-beautify mode", function() {
var tests = [
"new x(1);",
"new x;",
"new new x;",
"new (function(foo){this.foo=foo;})(1);",
"new (function(foo){this.foo=foo;})();",
"new (function test(foo){this.foo=foo;})(1);",
"new (function test(foo){this.foo=foo;})();",
"new true;",
"new (0);",
"new (!0);",
"new (bar = function(foo) {this.foo=foo;})(123);",
"new (bar = function(foo) {this.foo=foo;})();"
];
var expected = [
"new x(1);",
"new x;",
"new(new x);",
"new function(foo){this.foo=foo}(1);",
"new function(foo){this.foo=foo};",
"new function test(foo){this.foo=foo}(1);",
"new function test(foo){this.foo=foo};",
"new true;",
"new 0;",
"new(!0);",
"new(bar=function(foo){this.foo=foo})(123);",
"new(bar=function(foo){this.foo=foo});"
];
for (var i = 0; i < tests.length; i++) {
assert.strictEqual(
uglify.minify(tests[i], {
fromString: true,
output: {beautify: false},
compress: false,
mangle: false
}).code,
expected[i]
);
}
});
});

View File

@@ -31,4 +31,51 @@ describe("String literals", function() {
var output = UglifyJS.parse('var a = "a\\\nb";').print_to_string();
assert.equal(output, 'var a="ab";');
});
});
it("Should throw error in strict mode if string contains escaped octalIntegerLiteral", function() {
var inputs = [
'"use strict";\n"\\76";',
'"use strict";\nvar foo = "\\76";',
'"use strict";\n"\\1";',
'"use strict";\n"\\07";',
'"use strict";\n"\\011"'
];
var test = function(input) {
return function() {
var output = UglifyJS.parse(input);
}
};
var error = function(e) {
return e instanceof UglifyJS.JS_Parse_Error &&
e.message === "Octal literals are not allowed in strict mode";
}
for (var input in inputs) {
assert.throws(test(inputs[input]), error);
}
});
it("Should not throw error outside strict mode if string contains escaped octalIntegerLiteral", function() {
var tests = [
['"\\76";', ';">";'],
['"\\0"', '"\\0";'],
['"\\08"', '"\\08";'],
['"\\008"', '"\\08";'],
['"\\0008"', '"\\08";'],
['"use strict" === "use strict";\n"\\76";', '"use strict"==="use strict";">";'],
['"use\\\n strict";\n"\\07";', ';"use strict";"\07";']
];
for (var test in tests) {
var output = UglifyJS.parse(tests[test][0]).print_to_string();
assert.equal(output, tests[test][1]);
}
});
it("Should not throw error when digit is 8 or 9", function() {
assert.equal(UglifyJS.parse('"use strict";"\\08"').print_to_string(), '"use strict";"\\08";');
assert.equal(UglifyJS.parse('"use strict";"\\09"').print_to_string(), '"use strict";"\\09";');
});
});

16
test/mocha/with.js Normal file
View File

@@ -0,0 +1,16 @@
var assert = require("assert");
var uglify = require("../../");
describe("With", function() {
it ("Should throw syntaxError when using with statement in strict mode", function() {
var code = '"use strict";\nthrow NotEarlyError;\nwith ({}) { }';
var test = function() {
uglify.parse(code);
}
var error = function(e) {
return e instanceof uglify.JS_Parse_Error &&
e.message === "SyntaxError: Strict mode may not include a with statement";
}
assert.throws(test, error);
});
});

View File

@@ -1,5 +1,7 @@
#! /usr/bin/env node
global.UGLIFY_DEBUG = true;
var U = require("../tools/node");
var path = require("path");
var fs = require("fs");
@@ -85,9 +87,18 @@ function run_compress_tests() {
log_start_file(file);
function test_case(test) {
log_test(test.name);
U.base54.reset();
var options = U.defaults(test.options, {
warnings: false
});
var warnings_emitted = [];
var original_warn_function = U.AST_Node.warn_function;
if (test.expect_warnings) {
U.AST_Node.warn_function = function(text) {
warnings_emitted.push("WARN: " + text);
};
options.warnings = true;
}
var cmp = new U.Compressor(options, true);
var output_options = test.beautify || {};
var expect;
@@ -97,11 +108,15 @@ function run_compress_tests() {
expect = test.expect_exact;
}
var input = as_toplevel(test.input);
var input_code = make_code(test.input, { beautify: true });
var input_code = make_code(test.input, {
beautify: true,
quote_style: 3,
keep_quoted_props: true
});
if (test.mangle_props) {
input = U.mangle_properties(input, test.mangle_props);
}
var output = input.transform(cmp);
var output = cmp.compress(input);
output.figure_out_scope();
if (test.mangle) {
output.compute_char_frequency(test.mangle);
@@ -117,6 +132,26 @@ function run_compress_tests() {
failures++;
failed_files[file] = 1;
}
else if (test.expect_warnings) {
U.AST_Node.warn_function = original_warn_function;
var expected_warnings = make_code(test.expect_warnings, {
beautify: false,
quote_style: 2, // force double quote to match JSON
});
warnings_emitted = warnings_emitted.map(function(input) {
return input.split(process.cwd() + path.sep).join("").split(path.sep).join("/");
});
var actual_warnings = JSON.stringify(warnings_emitted);
if (expected_warnings != actual_warnings) {
log("!!! failed\n---INPUT---\n{input}\n---EXPECTED WARNINGS---\n{expected_warnings}\n---ACTUAL WARNINGS---\n{actual_warnings}\n\n", {
input: input_code,
expected_warnings: expected_warnings,
actual_warnings: actual_warnings,
});
failures++;
failed_files[file] = 1;
}
}
}
var tests = parse_test(path.resolve(dir, file));
for (var i in tests) if (tests.hasOwnProperty(i)) {
@@ -130,9 +165,16 @@ function run_compress_tests() {
function parse_test(file) {
var script = fs.readFileSync(file, "utf8");
var ast = U.parse(script, {
filename: file
});
// TODO try/catch can be removed after fixing https://github.com/mishoo/UglifyJS2/issues/348
try {
var ast = U.parse(script, {
filename: file
});
} catch (e) {
console.log("Caught error while parsing tests in " + file + "\n");
console.log(e);
throw e;
}
var tests = {};
var tw = new U.TreeWalker(function(node, descend){
if (node instanceof U.AST_LabeledStatement
@@ -168,7 +210,7 @@ function parse_test(file) {
}
if (node instanceof U.AST_LabeledStatement) {
assert.ok(
node.label.name == "input" || node.label.name == "expect" || node.label.name == "expect_exact",
["input", "expect", "expect_exact", "expect_warnings"].indexOf(node.label.name) >= 0,
tmpl("Unsupported label {name} [{line},{col}]", {
name: node.label.name,
line: node.label.start.line,

View File

@@ -14,5 +14,10 @@ exports["merge"] = merge;
exports["parse"] = parse;
exports["push_uniq"] = push_uniq;
exports["string_template"] = string_template;
exports["tokenizer"] = tokenizer;
exports["is_identifier"] = is_identifier;
exports["SymbolDef"] = SymbolDef;
if (typeof DEBUG !== "undefined" && DEBUG) {
exports["EXPECT_DIRECTIVE"] = EXPECT_DIRECTIVE;
}

View File

@@ -1,3 +1,9 @@
// workaround for tty output truncation upon process.exit()
[process.stdout, process.stderr].forEach(function(stream){
if (stream._handle && stream._handle.setBlocking)
stream._handle.setBlocking(true);
});
var path = require("path");
var fs = require("fs");
@@ -19,11 +25,12 @@ var FILES = exports.FILES = [
var UglifyJS = exports;
new Function("MOZ_SourceMap", "exports", FILES.map(function(file){
new Function("MOZ_SourceMap", "exports", "DEBUG", FILES.map(function(file){
return fs.readFileSync(file, "utf8");
}).join("\n\n"))(
require("source-map"),
UglifyJS
UglifyJS,
!!global.UGLIFY_DEBUG
);
UglifyJS.AST_Node.warn_function = function(txt) {
@@ -78,7 +85,7 @@ exports.minify = function(files, options) {
UglifyJS.merge(compress, options.compress);
toplevel.figure_out_scope();
var sq = UglifyJS.Compressor(compress);
toplevel = toplevel.transform(sq);
toplevel = sq.compress(toplevel);
}
// 3. mangle properties