Compare commits
69 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
572b97b0bb | ||
|
|
698705a820 | ||
|
|
debc525fa1 | ||
|
|
5576e2737a | ||
|
|
b40d5de69c | ||
|
|
b7ef7840f3 | ||
|
|
85924bb32e | ||
|
|
a97690fc72 | ||
|
|
02c638209e | ||
|
|
030611b729 | ||
|
|
335b72df03 | ||
|
|
3a7d53f3cf | ||
|
|
9676167aac | ||
|
|
1840a0b282 | ||
|
|
ace8aaa0f4 | ||
|
|
0c003c92a8 | ||
|
|
85fbf86d7b | ||
|
|
aa82027a17 | ||
|
|
55c592dd43 | ||
|
|
fc1abd1c11 | ||
|
|
e645ba84cf | ||
|
|
6c99816855 | ||
|
|
2149bfb707 | ||
|
|
d7971ba0e4 | ||
|
|
5c4cfaa0a7 | ||
|
|
bb9c9707aa | ||
|
|
6c8e001fee | ||
|
|
9c53c7ada7 | ||
|
|
f99b7b630d | ||
|
|
ea31da2455 | ||
|
|
4d7746baf3 | ||
|
|
31d5825a86 | ||
|
|
8287ef6781 | ||
|
|
5cb5305cf3 | ||
|
|
00ad57e393 | ||
|
|
09d5707a8a | ||
|
|
1e390269d4 | ||
|
|
bc49dfd27a | ||
|
|
27eedbc302 | ||
|
|
5f464b41e2 | ||
|
|
bcc1318d4b | ||
|
|
a0e03c9df4 | ||
|
|
6641dcafb6 | ||
|
|
d2945744f2 | ||
|
|
35bc716625 | ||
|
|
f39fd3d583 | ||
|
|
65887d9a56 | ||
|
|
e9224ab444 | ||
|
|
4d9a085687 | ||
|
|
4fe630431c | ||
|
|
c55dd5ed74 | ||
|
|
e4fa4b109a | ||
|
|
4b4528ee05 | ||
|
|
187a0caf9d | ||
|
|
b5a7a231f7 | ||
|
|
3907a5e3b2 | ||
|
|
b434b75b36 | ||
|
|
c70d176f35 | ||
|
|
9317237372 | ||
|
|
98434258d0 | ||
|
|
45ddb9caeb | ||
|
|
9bcf702a6e | ||
|
|
f68de86a17 | ||
|
|
c3c7587796 | ||
|
|
07bb7262d0 | ||
|
|
21befe583f | ||
|
|
a9d4a6291b | ||
|
|
ee6c9fabb7 | ||
|
|
102d1b9137 |
@@ -4,6 +4,7 @@ node_js:
|
||||
- "0.12"
|
||||
- "0.10"
|
||||
- "4"
|
||||
- "6"
|
||||
matrix:
|
||||
fast_finish: true
|
||||
sudo: false
|
||||
|
||||
103
README.md
103
README.md
@@ -65,9 +65,11 @@ The available options are:
|
||||
--in-source-map Input source map, useful if you're compressing
|
||||
JS that was generated from some other original
|
||||
code.
|
||||
--screw-ie8 Pass this flag if you don't care about full
|
||||
compliance with Internet Explorer 6-8 quirks
|
||||
(by default UglifyJS will try to be IE-proof).
|
||||
--screw-ie8 Use this flag if you don't wish to support
|
||||
Internet Explorer 6-8 quirks.
|
||||
By default UglifyJS will not try to be IE-proof.
|
||||
--support-ie8 Use this flag to support Internet Explorer 6-8 quirks.
|
||||
Note: may break standards compliant `catch` identifiers.
|
||||
--expr Parse a single expression, rather than a
|
||||
program (for parsing JSON)
|
||||
-p, --prefix Skip prefix for original filenames that appear
|
||||
@@ -133,7 +135,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 +203,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).
|
||||
|
||||
@@ -285,7 +291,14 @@ you can pass a comma-separated list of options. Options are in the form
|
||||
`foo=bar`, or just `foo` (the latter implies a boolean option that you want
|
||||
to set `true`; it's effectively a shortcut for `foo=true`).
|
||||
|
||||
- `sequences` -- join consecutive simple statements using the comma operator
|
||||
- `sequences` (default: true) -- join consecutive simple statements using the
|
||||
comma operator. May be set to a positive integer to specify the maximum number
|
||||
of consecutive comma sequences that will be generated. If this option is set to
|
||||
`true` then the default `sequences` limit is `200`. Set option to `false` or `0`
|
||||
to disable. The smallest `sequences` length is `2`. A `sequences` value of `1`
|
||||
is grandfathered to be equivalent to `true` and as such means `200`. On rare
|
||||
occasions the default sequences limit leads to very slow compress times in which
|
||||
case a value of `20` or less is recommended.
|
||||
|
||||
- `properties` -- rewrite property access using the dot notation, for
|
||||
example `foo["bar"] → foo.bar`
|
||||
@@ -296,12 +309,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 +381,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 +438,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 +470,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 +497,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
|
||||
|
||||
@@ -629,7 +670,8 @@ Other options:
|
||||
- `fromString` (default `false`) — if you pass `true` then you can pass
|
||||
JavaScript source code, rather than file names.
|
||||
|
||||
- `mangle` — pass `false` to skip mangling names.
|
||||
- `mangle` (default `true`) — pass `false` to skip mangling names, or pass
|
||||
an object to specify mangling options (see below).
|
||||
|
||||
- `mangleProperties` (default `false`) — pass an object to specify custom
|
||||
mangle property options.
|
||||
@@ -644,9 +686,40 @@ 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
|
||||
|
||||
- `toplevel` — mangle names declared in the toplevel scope (disabled by
|
||||
default).
|
||||
|
||||
- `eval` — mangle names visible in scopes where eval or with are used
|
||||
(disabled by default).
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
//tst.js
|
||||
var globalVar;
|
||||
function funcName(firstLongName, anotherLongName)
|
||||
{
|
||||
var myVariable = firstLongName + anotherLongName;
|
||||
}
|
||||
|
||||
UglifyJS.minify("tst.js").code;
|
||||
// 'function funcName(a,n){}var globalVar;'
|
||||
|
||||
UglifyJS.minify("tst.js", { mangle: { except: ['firstLongName'] }}).code;
|
||||
// 'function funcName(firstLongName,a){}var globalVar;'
|
||||
|
||||
UglifyJS.minify("tst.js", { mangle: toplevel: true }}).code;
|
||||
// 'function n(n,a){}var a;'
|
||||
```
|
||||
|
||||
##### 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!
|
||||
|
||||
46
bin/uglifyjs
46
bin/uglifyjs
@@ -10,6 +10,7 @@ var fs = require("fs");
|
||||
var path = require("path");
|
||||
var async = require("async");
|
||||
var acorn;
|
||||
var screw_ie8 = true;
|
||||
var ARGS = yargs
|
||||
.usage("$0 input1.js [input2.js ...] [options]\n\
|
||||
Use a single dash to read input from the standard input.\
|
||||
@@ -24,7 +25,8 @@ mangling you need to use `-c` and `-m`.\
|
||||
.describe("source-map-url", "The path to the source map to be added in //# sourceMappingURL. Defaults to the value passed with --source-map.")
|
||||
.describe("source-map-include-sources", "Pass this flag if you want to include the content of source files in the source map as sourcesContent property.")
|
||||
.describe("in-source-map", "Input source map, useful if you're compressing JS that was generated from some other original code.")
|
||||
.describe("screw-ie8", "Pass this flag if you don't care about full compliance with Internet Explorer 6-8 quirks (by default UglifyJS will try to be IE-proof).")
|
||||
.describe("screw-ie8", "Do not support Internet Explorer 6-8 quirks. This flag is enabled by default.")
|
||||
.describe("support-ie8", "Support non-standard Internet Explorer 6-8 javascript. Note: may break standards compliant `catch` identifiers.")
|
||||
.describe("expr", "Parse a single expression, rather than a program (for parsing JSON)")
|
||||
.describe("p", "Skip prefix for original filenames that appear in source maps. \
|
||||
For example -p 3 will drop 3 directories from file names and ensure they are relative paths. \
|
||||
@@ -69,7 +71,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")
|
||||
@@ -105,12 +107,14 @@ You need to pass an argument to this option to specify the name that your module
|
||||
.string("p")
|
||||
.string("prefix")
|
||||
.string("name-cache")
|
||||
|
||||
.array("reserved-file")
|
||||
.array("pure-funcs")
|
||||
|
||||
.boolean("expr")
|
||||
.boolean("source-map-include-sources")
|
||||
.boolean("screw-ie8")
|
||||
.boolean("support-ie8")
|
||||
.boolean("export-all")
|
||||
.boolean("self")
|
||||
.boolean("v")
|
||||
@@ -125,7 +129,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,18 +216,32 @@ 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.screw_ie8) {
|
||||
if (COMPRESS) COMPRESS.screw_ie8 = true;
|
||||
if (MANGLE) MANGLE.screw_ie8 = true;
|
||||
OUTPUT_OPTIONS.screw_ie8 = true;
|
||||
if (ARGS.mangle_props == 2) {
|
||||
OUTPUT_OPTIONS.keep_quoted_props = true;
|
||||
if (COMPRESS && !("properties" in COMPRESS))
|
||||
COMPRESS.properties = false;
|
||||
}
|
||||
|
||||
if (ARGS.support_ie8 === true && ARGS.screw_ie8 !== true) {
|
||||
screw_ie8 = false;
|
||||
}
|
||||
|
||||
if (COMPRESS) COMPRESS.screw_ie8 = screw_ie8;
|
||||
if (MANGLE) MANGLE.screw_ie8 = screw_ie8;
|
||||
OUTPUT_OPTIONS.screw_ie8 = screw_ie8;
|
||||
|
||||
if (ARGS.keep_fnames) {
|
||||
if (COMPRESS) COMPRESS.keep_fnames = true;
|
||||
if (MANGLE) MANGLE.keep_fnames = true;
|
||||
@@ -401,10 +418,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);
|
||||
})();
|
||||
@@ -414,7 +432,7 @@ async.eachLimit(files, 1, function (file, cb) {
|
||||
|
||||
if (SCOPE_IS_NEEDED) {
|
||||
time_it("scope", function(){
|
||||
TOPLEVEL.figure_out_scope({ screw_ie8: ARGS.screw_ie8, cache: TL_CACHE });
|
||||
TOPLEVEL.figure_out_scope({ screw_ie8: screw_ie8, cache: TL_CACHE });
|
||||
if (ARGS.lint) {
|
||||
TOPLEVEL.scope_warnings();
|
||||
}
|
||||
@@ -423,13 +441,13 @@ async.eachLimit(files, 1, function (file, cb) {
|
||||
|
||||
if (COMPRESS) {
|
||||
time_it("squeeze", function(){
|
||||
TOPLEVEL = TOPLEVEL.transform(compressor);
|
||||
TOPLEVEL = compressor.compress(TOPLEVEL);
|
||||
});
|
||||
}
|
||||
|
||||
if (SCOPE_IS_NEEDED) {
|
||||
time_it("scope", function(){
|
||||
TOPLEVEL.figure_out_scope({ screw_ie8: ARGS.screw_ie8, cache: TL_CACHE });
|
||||
TOPLEVEL.figure_out_scope({ screw_ie8: screw_ie8, cache: TL_CACHE });
|
||||
if (MANGLE && !TL_CACHE) {
|
||||
TOPLEVEL.compute_char_frequency(MANGLE);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
301
lib/compress.js
301
lib/compress.js
@@ -72,21 +72,41 @@ function Compressor(options, false_by_default) {
|
||||
pure_getters : false,
|
||||
pure_funcs : null,
|
||||
negate_iife : !false_by_default,
|
||||
screw_ie8 : false,
|
||||
screw_ie8 : true,
|
||||
drop_console : false,
|
||||
angular : false,
|
||||
|
||||
warnings : true,
|
||||
global_defs : {}
|
||||
global_defs : {},
|
||||
passes : 1,
|
||||
}, true);
|
||||
var sequences = this.options["sequences"];
|
||||
this.sequences_limit = sequences == 1 ? 200 : sequences | 0;
|
||||
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 +149,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 +185,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, orig, { 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":
|
||||
@@ -230,7 +268,7 @@ merge(Compressor.prototype, {
|
||||
if (compressor.option("if_return")) {
|
||||
statements = handle_if_return(statements, compressor);
|
||||
}
|
||||
if (compressor.option("sequences")) {
|
||||
if (compressor.sequences_limit > 0) {
|
||||
statements = sequencesize(statements, compressor);
|
||||
}
|
||||
if (compressor.option("join_vars")) {
|
||||
@@ -302,7 +340,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 +430,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 +537,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 +575,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 +591,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 +668,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 +723,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) < compressor.sequences_limit) {
|
||||
seq.push(stat.body);
|
||||
} else {
|
||||
push_seq();
|
||||
ret.push(stat);
|
||||
}
|
||||
});
|
||||
push_seq();
|
||||
ret = sequencesize_2(ret, compressor);
|
||||
@@ -682,6 +736,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 +835,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 +869,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 +1052,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 +1096,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 +1332,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 +1358,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 +1385,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 +1417,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 +1430,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 +2508,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 +2604,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 +2748,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 +2756,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
|
||||
|
||||
@@ -45,20 +45,55 @@
|
||||
|
||||
(function(){
|
||||
|
||||
var MOZ_TO_ME = {
|
||||
ExpressionStatement: function(M) {
|
||||
var expr = M.expression;
|
||||
if (expr.type === "Literal" && typeof expr.value === "string") {
|
||||
return new AST_Directive({
|
||||
start: my_start_token(M),
|
||||
end: my_end_token(M),
|
||||
value: expr.value
|
||||
var normalize_directives = function(body) {
|
||||
var in_directive = true;
|
||||
|
||||
for (var i = 0; i < body.length; i++) {
|
||||
if (in_directive && body[i] instanceof AST_Statement && body[i].body instanceof AST_String) {
|
||||
body[i] = new AST_Directive({
|
||||
start: body[i].start,
|
||||
end: body[i].end,
|
||||
value: body[i].body.value
|
||||
});
|
||||
} else if (in_directive && !(body[i] instanceof AST_Statement && body[i].body instanceof AST_String)) {
|
||||
in_directive = false;
|
||||
}
|
||||
}
|
||||
|
||||
return body;
|
||||
};
|
||||
|
||||
var MOZ_TO_ME = {
|
||||
Program: function(M) {
|
||||
return new AST_Toplevel({
|
||||
start: my_start_token(M),
|
||||
end: my_end_token(M),
|
||||
body: normalize_directives(M.body.map(from_moz))
|
||||
});
|
||||
},
|
||||
FunctionDeclaration: function(M) {
|
||||
return new AST_Defun({
|
||||
start: my_start_token(M),
|
||||
end: my_end_token(M),
|
||||
name: from_moz(M.id),
|
||||
argnames: M.params.map(from_moz),
|
||||
body: normalize_directives(from_moz(M.body).body)
|
||||
});
|
||||
},
|
||||
FunctionExpression: function(M) {
|
||||
return new AST_Function({
|
||||
start: my_start_token(M),
|
||||
end: my_end_token(M),
|
||||
name: from_moz(M.id),
|
||||
argnames: M.params.map(from_moz),
|
||||
body: normalize_directives(from_moz(M.body).body)
|
||||
});
|
||||
},
|
||||
ExpressionStatement: function(M) {
|
||||
return new AST_SimpleStatement({
|
||||
start: my_start_token(M),
|
||||
end: my_end_token(M),
|
||||
body: from_moz(expr)
|
||||
body: from_moz(M.expression)
|
||||
});
|
||||
},
|
||||
TryStatement: function(M) {
|
||||
@@ -94,6 +129,15 @@
|
||||
return new AST_ObjectGetter(args);
|
||||
}
|
||||
},
|
||||
ArrayExpression: function(M) {
|
||||
return new AST_Array({
|
||||
start : my_start_token(M),
|
||||
end : my_end_token(M),
|
||||
elements : M.elements.map(function(elem){
|
||||
return elem === null ? new AST_Hole() : from_moz(elem);
|
||||
})
|
||||
});
|
||||
},
|
||||
ObjectExpression: function(M) {
|
||||
return new AST_Object({
|
||||
start : my_start_token(M),
|
||||
@@ -185,7 +229,6 @@
|
||||
});
|
||||
};
|
||||
|
||||
map("Program", AST_Toplevel, "body@body");
|
||||
map("EmptyStatement", AST_EmptyStatement);
|
||||
map("BlockStatement", AST_BlockStatement, "body@body");
|
||||
map("IfStatement", AST_If, "test>condition, consequent>body, alternate>alternative");
|
||||
@@ -201,13 +244,10 @@
|
||||
map("ForStatement", AST_For, "init>init, test>condition, update>step, body>body");
|
||||
map("ForInStatement", AST_ForIn, "left>init, right>object, body>body");
|
||||
map("DebuggerStatement", AST_Debugger);
|
||||
map("FunctionDeclaration", AST_Defun, "id>name, params@argnames, body%body");
|
||||
map("VariableDeclarator", AST_VarDef, "id>name, init>value");
|
||||
map("CatchClause", AST_Catch, "param>argname, body%body");
|
||||
|
||||
map("ThisExpression", AST_This);
|
||||
map("ArrayExpression", AST_Array, "elements@elements");
|
||||
map("FunctionExpression", AST_Function, "id>name, params@argnames, body%body");
|
||||
map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right");
|
||||
map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right");
|
||||
map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right");
|
||||
@@ -215,6 +255,31 @@
|
||||
map("NewExpression", AST_New, "callee>expression, arguments@args");
|
||||
map("CallExpression", AST_Call, "callee>expression, arguments@args");
|
||||
|
||||
def_to_moz(AST_Toplevel, function To_Moz_Program(M) {
|
||||
return {
|
||||
type: "Program",
|
||||
body: M.body.map(to_moz)
|
||||
};
|
||||
});
|
||||
|
||||
def_to_moz(AST_Defun, function To_Moz_FunctionDeclaration(M) {
|
||||
return {
|
||||
type: "FunctionDeclaration",
|
||||
id: to_moz(M.name),
|
||||
params: M.argnames.map(to_moz),
|
||||
body: to_moz_block(M)
|
||||
}
|
||||
});
|
||||
|
||||
def_to_moz(AST_Function, function To_Moz_FunctionExpression(M) {
|
||||
return {
|
||||
type: "FunctionExpression",
|
||||
id: to_moz(M.name),
|
||||
params: M.argnames.map(to_moz),
|
||||
body: to_moz_block(M)
|
||||
}
|
||||
});
|
||||
|
||||
def_to_moz(AST_Directive, function To_Moz_Directive(M) {
|
||||
return {
|
||||
type: "ExpressionStatement",
|
||||
@@ -302,6 +367,13 @@
|
||||
};
|
||||
});
|
||||
|
||||
def_to_moz(AST_Array, function To_Moz_ArrayExpression(M) {
|
||||
return {
|
||||
type: "ArrayExpression",
|
||||
elements: M.elements.map(to_moz)
|
||||
};
|
||||
});
|
||||
|
||||
def_to_moz(AST_Object, function To_Moz_ObjectExpression(M) {
|
||||
return {
|
||||
type: "ObjectExpression",
|
||||
|
||||
106
lib/output.js
106
lib/output.js
@@ -43,6 +43,8 @@
|
||||
|
||||
"use strict";
|
||||
|
||||
var EXPECT_DIRECTIVE = /^$|[;{][\s\n]*$/;
|
||||
|
||||
function OutputStream(options) {
|
||||
|
||||
options = defaults(options, {
|
||||
@@ -62,9 +64,10 @@ function OutputStream(options) {
|
||||
comments : false,
|
||||
shebang : true,
|
||||
preserve_line : false,
|
||||
screw_ie8 : false,
|
||||
screw_ie8 : true,
|
||||
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);
|
||||
@@ -822,8 +861,8 @@ function OutputStream(options) {
|
||||
// adds the block brackets if needed.
|
||||
if (!self.body)
|
||||
return output.force_semicolon();
|
||||
if (self.body instanceof AST_Do
|
||||
&& !output.option("screw_ie8")) {
|
||||
if (self.body instanceof AST_Do) {
|
||||
// Unconditionally use the if/do-while workaround for all browsers.
|
||||
// https://github.com/mishoo/UglifyJS/issues/#issue/57 IE
|
||||
// croaks with "syntax error" on code like this: if (foo)
|
||||
// do ... while(cond); else ... we need block brackets
|
||||
@@ -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) {
|
||||
|
||||
243
lib/parse.js
243
lib/parse.js
@@ -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';
|
||||
|
||||
@@ -107,7 +107,9 @@ var OPERATORS = makePredicate([
|
||||
"||"
|
||||
]);
|
||||
|
||||
var WHITESPACE_CHARS = makePredicate(characters(" \u00a0\n\r\t\f\u000b\u200b\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\uFEFF"));
|
||||
var WHITESPACE_CHARS = makePredicate(characters(" \u00a0\n\r\t\f\u000b\u200b\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\uFEFF"));
|
||||
|
||||
var NEWLINE_CHARS = makePredicate(characters("\n\r\u2028\u2029"));
|
||||
|
||||
var PUNC_BEFORE_EXPRESSION = makePredicate(characters("[{(,.;:"));
|
||||
|
||||
@@ -223,7 +225,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); };
|
||||
@@ -232,7 +236,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
|
||||
var ch = S.text.charAt(S.pos++);
|
||||
if (signal_eof && !ch)
|
||||
throw EX_EOF;
|
||||
if ("\r\n\u2028\u2029".indexOf(ch) >= 0) {
|
||||
if (NEWLINE_CHARS(ch)) {
|
||||
S.newline_before = S.newline_before || !in_string;
|
||||
++S.line;
|
||||
S.col = 0;
|
||||
@@ -255,6 +259,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 (NEWLINE_CHARS(ch))
|
||||
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;
|
||||
@@ -301,8 +315,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
|
||||
};
|
||||
|
||||
function skip_whitespace() {
|
||||
var ch;
|
||||
while (WHITESPACE_CHARS(ch = peek()) || ch == "\u2028" || ch == "\u2029")
|
||||
while (WHITESPACE_CHARS(peek()))
|
||||
next();
|
||||
};
|
||||
|
||||
@@ -340,7 +353,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
|
||||
if (!isNaN(valid)) {
|
||||
return token("num", valid);
|
||||
} else {
|
||||
parse_error("Invalid syntax: " + num);
|
||||
parse_error("SyntaxError: Invalid syntax: " + num);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -353,7 +366,6 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
|
||||
case 98 : return "\b";
|
||||
case 118 : return "\u000b"; // \v
|
||||
case 102 : return "\f";
|
||||
case 48 : return "\0";
|
||||
case 120 : return String.fromCharCode(hex_bytes(2)); // \x
|
||||
case 117 : return String.fromCharCode(hex_bytes(4)); // \u
|
||||
case 10 : return ""; // newline
|
||||
@@ -363,43 +375,44 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
if (ch >= "0" && ch <= "7")
|
||||
return read_octal_escape_sequence(ch);
|
||||
return ch;
|
||||
};
|
||||
|
||||
function read_octal_escape_sequence(ch) {
|
||||
// Read
|
||||
var p = peek();
|
||||
if (p >= "0" && p <= "7") {
|
||||
ch += next(true);
|
||||
if (ch[0] <= "3" && (p = peek()) >= "0" && p <= "7")
|
||||
ch += next(true);
|
||||
}
|
||||
|
||||
// Parse
|
||||
if (ch === "0") return "\0";
|
||||
if (ch.length > 0 && next_token.has_directive("use strict"))
|
||||
parse_error("SyntaxError: Octal literals are not allowed in strict mode");
|
||||
return String.fromCharCode(parseInt(ch, 8));
|
||||
}
|
||||
|
||||
function hex_bytes(n) {
|
||||
var num = 0;
|
||||
for (; n > 0; --n) {
|
||||
var digit = parseInt(next(true), 16);
|
||||
if (isNaN(digit))
|
||||
parse_error("Invalid hex-character pattern in string");
|
||||
parse_error("SyntaxError: Invalid hex-character pattern in string");
|
||||
num = (num << 4) | digit;
|
||||
}
|
||||
return num;
|
||||
};
|
||||
|
||||
var read_string = with_eof_error("Unterminated string constant", function(quote_char){
|
||||
var read_string = with_eof_error("SyntaxError: Unterminated string constant", function(quote_char){
|
||||
var quote = next(), ret = "";
|
||||
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") {
|
||||
if (!first) {
|
||||
first = ch;
|
||||
return ++octal_len;
|
||||
}
|
||||
else if (first <= "3" && octal_len <= 2) return ++octal_len;
|
||||
else if (first >= "4" && octal_len <= 1) return ++octal_len;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
if (octal_len > 0) 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");
|
||||
if (ch == "\\") ch = read_escaped_char(true);
|
||||
else if (NEWLINE_CHARS(ch)) parse_error("SyntaxError: Unterminated string constant");
|
||||
else if (ch == quote) break;
|
||||
ret += ch;
|
||||
}
|
||||
@@ -410,7 +423,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,25 +434,18 @@ 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 skip_multiline_comment = with_eof_error("SyntaxError: Unterminated multiline comment", function(){
|
||||
var regex_allowed = S.regex_allowed;
|
||||
var i = find("*/", true);
|
||||
var text = S.text.substring(S.pos, i);
|
||||
var a = text.split("\n"), n = a.length;
|
||||
var text = S.text.substring(S.pos, i).replace(/\r\n|\r|\u2028|\u2029/g, '\n');
|
||||
// update stream position
|
||||
S.pos = i + 2;
|
||||
S.line += n - 1;
|
||||
if (n > 1) S.col = a[n - 1].length;
|
||||
else S.col += a[n - 1].length;
|
||||
S.col += 2;
|
||||
var nlb = S.newline_before = S.newline_before || text.indexOf("\n") >= 0;
|
||||
forward(text.length /* doesn't count \r\n as 2 char while S.pos - i does */ + 2);
|
||||
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() {
|
||||
@@ -451,9 +457,9 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
|
||||
else break;
|
||||
}
|
||||
else {
|
||||
if (ch != "u") parse_error("Expecting UnicodeEscapeSequence -- uXXXX");
|
||||
if (ch != "u") parse_error("SyntaxError: Expecting UnicodeEscapeSequence -- uXXXX");
|
||||
ch = read_escaped_char();
|
||||
if (!is_identifier_char(ch)) parse_error("Unicode char: " + ch.charCodeAt(0) + " is not valid in identifier");
|
||||
if (!is_identifier_char(ch)) parse_error("SyntaxError: Unicode char: " + ch.charCodeAt(0) + " is not valid in identifier");
|
||||
name += ch;
|
||||
backslash = false;
|
||||
}
|
||||
@@ -465,9 +471,11 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
|
||||
return name;
|
||||
};
|
||||
|
||||
var read_regexp = with_eof_error("Unterminated regular expression", function(regexp){
|
||||
var read_regexp = with_eof_error("SyntaxError: Unterminated regular expression", function(regexp){
|
||||
var prev_backslash = false, ch, in_class = false;
|
||||
while ((ch = next(true))) if (prev_backslash) {
|
||||
while ((ch = next(true))) if (NEWLINE_CHARS(ch)) {
|
||||
parse_error("SyntaxError: Unexpected line terminator");
|
||||
} else if (prev_backslash) {
|
||||
regexp += "\\" + ch;
|
||||
prev_backslash = false;
|
||||
} else if (ch == "[") {
|
||||
@@ -548,38 +556,47 @@ 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 + "'");
|
||||
parse_error("SyntaxError: Unexpected character '" + ch + "'");
|
||||
};
|
||||
|
||||
next_token.context = function(nc) {
|
||||
@@ -587,6 +604,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 +753,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 +806,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 +847,7 @@ function parse($TEXT, options) {
|
||||
case "(":
|
||||
return simple_statement();
|
||||
case ";":
|
||||
S.in_directives = false;
|
||||
next();
|
||||
return new AST_EmptyStatement();
|
||||
default:
|
||||
@@ -835,7 +889,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 +906,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 +921,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 +942,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 +955,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 +975,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 +995,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 +1048,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 +1145,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 +1239,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 +1390,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 +1454,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 +1518,7 @@ function parse($TEXT, options) {
|
||||
end : prev()
|
||||
});
|
||||
}
|
||||
croak("Invalid assignment");
|
||||
croak("SyntaxError: Invalid assignment");
|
||||
}
|
||||
return left;
|
||||
};
|
||||
@@ -1493,8 +1552,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) {
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
12
lib/scope.js
12
lib/scope.js
@@ -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 = {};
|
||||
@@ -85,7 +88,7 @@ SymbolDef.prototype = {
|
||||
|
||||
AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
|
||||
options = defaults(options, {
|
||||
screw_ie8: false,
|
||||
screw_ie8: true,
|
||||
cache: null
|
||||
});
|
||||
|
||||
@@ -372,9 +375,9 @@ 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,
|
||||
screw_ie8 : true,
|
||||
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;
|
||||
}
|
||||
|
||||
32
lib/utils.js
32
lib/utils.js
@@ -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);
|
||||
};
|
||||
@@ -230,10 +227,19 @@ function makePredicate(words) {
|
||||
}
|
||||
cats.push([words[i]]);
|
||||
}
|
||||
function quote(word) {
|
||||
return JSON.stringify(word).replace(/[\u2028\u2029]/g, function(s) {
|
||||
switch (s) {
|
||||
case "\u2028": return "\\u2028";
|
||||
case "\u2029": return "\\u2029";
|
||||
}
|
||||
return s;
|
||||
});
|
||||
}
|
||||
function compareTo(arr) {
|
||||
if (arr.length == 1) return f += "return str === " + JSON.stringify(arr[0]) + ";";
|
||||
if (arr.length == 1) return f += "return str === " + quote(arr[0]) + ";";
|
||||
f += "switch(str){";
|
||||
for (var i = 0; i < arr.length; ++i) f += "case " + JSON.stringify(arr[i]) + ":";
|
||||
for (var i = 0; i < arr.length; ++i) f += "case " + quote(arr[i]) + ":";
|
||||
f += "return true}return false;";
|
||||
}
|
||||
// When there are more than three length categories, an outer
|
||||
@@ -308,3 +314,7 @@ Dictionary.fromObject = function(obj) {
|
||||
dict._size = merge(dict._values, obj);
|
||||
return dict;
|
||||
};
|
||||
|
||||
function HOP(obj, prop) {
|
||||
return Object.prototype.hasOwnProperty.call(obj, prop);
|
||||
}
|
||||
|
||||
@@ -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.7.0",
|
||||
"engines": {
|
||||
"node": ">=0.8.0"
|
||||
},
|
||||
|
||||
36
test/compress/ascii.js
Normal file
36
test/compress/ascii.js
Normal file
@@ -0,0 +1,36 @@
|
||||
ascii_only_true: {
|
||||
options = {}
|
||||
beautify = {
|
||||
ascii_only : true,
|
||||
screw_ie8 : 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\\v\\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,
|
||||
screw_ie8 : 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\\v\\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\'}'
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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]);
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
76
test/compress/comparing.js
Normal file
76
test/compress/comparing.js
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
39
test/compress/evaluate.js
Normal 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
207
test/compress/if_return.js
Normal 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
121
test/compress/issue-1034.js
Normal 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]"
|
||||
]
|
||||
}
|
||||
|
||||
39
test/compress/issue-1041.js
Normal file
39
test/compress/issue-1041.js
Normal 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 || {};
|
||||
}
|
||||
}
|
||||
96
test/compress/issue-1052.js
Normal file
96
test/compress/issue-1052.js
Normal 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
240
test/compress/issue-1105.js
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")}'
|
||||
}
|
||||
|
||||
@@ -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))();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
19
test/compress/numbers.js
Normal 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);"
|
||||
}
|
||||
@@ -12,7 +12,8 @@ keep_properties: {
|
||||
|
||||
dot_properties: {
|
||||
options = {
|
||||
properties: true
|
||||
properties: true,
|
||||
screw_ie8: false
|
||||
};
|
||||
input: {
|
||||
a["foo"] = "bar";
|
||||
@@ -72,3 +73,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};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
10
test/compress/string-literal.js
Normal file
10
test/compress/string-literal.js
Normal file
@@ -0,0 +1,10 @@
|
||||
octal_escape_sequence: {
|
||||
input: {
|
||||
var boundaries = "\0\7\00\07\70\77\000\077\300\377";
|
||||
var border_check = "\400\700\0000\3000";
|
||||
}
|
||||
expect: {
|
||||
var boundaries = "\x00\x07\x00\x07\x38\x3f\x00\x3f\xc0\xff";
|
||||
var border_check = "\x20\x30\x38\x30\x00\x30\xc0\x30";
|
||||
}
|
||||
}
|
||||
22
test/mocha/cli.js
Normal file
22
test/mocha/cli.js
Normal file
@@ -0,0 +1,22 @@
|
||||
var assert = require("assert");
|
||||
var exec = require("child_process").exec;
|
||||
|
||||
describe("bin/uglifyjs", function () {
|
||||
it("should produce a functional build when using --self", function (done) {
|
||||
this.timeout(5000);
|
||||
|
||||
var uglifyjs = '"' + process.argv[0] + '" bin/uglifyjs';
|
||||
var command = uglifyjs + ' --self -cm --wrap WrappedUglifyJS';
|
||||
|
||||
exec(command, function (err, stdout) {
|
||||
if (err) throw err;
|
||||
|
||||
eval(stdout);
|
||||
|
||||
assert.strictEqual(typeof WrappedUglifyJS, 'object');
|
||||
assert.strictEqual(true, WrappedUglifyJS.parse('foo;') instanceof WrappedUglifyJS.AST_Node);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
50
test/mocha/comment.js
Normal file
50
test/mocha/comment.js
Normal file
@@ -0,0 +1,50 @@
|
||||
var assert = require("assert");
|
||||
var uglify = require("../../");
|
||||
|
||||
describe("Comment", function() {
|
||||
it("Should recognize eol of single line comments", function() {
|
||||
var tests = [
|
||||
"//Some comment 1\n>",
|
||||
"//Some comment 2\r>",
|
||||
"//Some comment 3\r\n>",
|
||||
"//Some comment 4\u2028>",
|
||||
"//Some comment 5\u2029>"
|
||||
];
|
||||
|
||||
var fail = function(e) {
|
||||
return e instanceof uglify.JS_Parse_Error &&
|
||||
e.message === "SyntaxError: Unexpected token: operator (>)" &&
|
||||
e.line === 2 &&
|
||||
e.col === 0;
|
||||
}
|
||||
|
||||
for (var i = 0; i < tests.length; i++) {
|
||||
assert.throws(function() {
|
||||
uglify.parse(tests[i], {fromString: true})
|
||||
}, fail, tests[i]);
|
||||
}
|
||||
});
|
||||
|
||||
it("Should update the position of a multiline comment correctly", function() {
|
||||
var tests = [
|
||||
"/*Some comment 1\n\n\n*/\n>\n\n\n\n\n\n",
|
||||
"/*Some comment 2\r\n\r\n\r\n*/\r\n>\n\n\n\n\n\n",
|
||||
"/*Some comment 3\r\r\r*/\r>\n\n\n\n\n\n",
|
||||
"/*Some comment 4\u2028\u2028\u2028*/\u2028>\n\n\n\n\n\n",
|
||||
"/*Some comment 5\u2029\u2029\u2029*/\u2029>\n\n\n\n\n\n"
|
||||
];
|
||||
|
||||
var fail = function(e) {
|
||||
return e instanceof uglify.JS_Parse_Error &&
|
||||
e.message === "SyntaxError: Unexpected token: operator (>)" &&
|
||||
e.line === 5 &&
|
||||
e.col === 0;
|
||||
}
|
||||
|
||||
for (var i = 0; i < tests.length; i++) {
|
||||
assert.throws(function() {
|
||||
uglify.parse(tests[i], {fromString: true})
|
||||
}, fail, tests[i]);
|
||||
}
|
||||
});
|
||||
});
|
||||
27
test/mocha/comment_before_constant.js
Normal file
27
test/mocha/comment_before_constant.js
Normal 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
370
test/mocha/directives.js
Normal 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]
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
19
test/mocha/huge-number-of-comments.js
Normal file
19
test/mocha/huge-number-of-comments.js
Normal 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
30
test/mocha/let.js
Normal 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();
|
||||
});
|
||||
});
|
||||
|
||||
60
test/mocha/line-endings.js
Normal file
60
test/mocha/line-endings.js
Normal file
@@ -0,0 +1,60 @@
|
||||
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/",
|
||||
"/\\\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 === "SyntaxError: Unexpected line terminator";
|
||||
}
|
||||
for (var i = 0; i < inputs.length; i++) {
|
||||
assert.throws(test(inputs[i]), fail);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
40
test/mocha/minify-file-map.js
Normal file
40
test/mocha/minify-file-map.js
Normal file
@@ -0,0 +1,40 @@
|
||||
var Uglify = require('../../');
|
||||
var assert = require("assert");
|
||||
|
||||
describe("Input file as map", function() {
|
||||
it("Should accept object", function() {
|
||||
var jsMap = {
|
||||
'/scripts/foo.js': 'var foo = {"x": 1, y: 2, \'z\': 3};'
|
||||
};
|
||||
var result = Uglify.minify(jsMap, {fromString: true, outSourceMap: true});
|
||||
|
||||
var map = JSON.parse(result.map);
|
||||
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
|
||||
assert.deepEqual(map.sources, ['/scripts/foo.js']);
|
||||
});
|
||||
|
||||
it("Should accept array of objects and strings", function() {
|
||||
var jsSeq = [
|
||||
{'/scripts/foo.js': 'var foo = {"x": 1, y: 2, \'z\': 3};'},
|
||||
'var bar = 15;'
|
||||
];
|
||||
var result = Uglify.minify(jsSeq, {fromString: true, outSourceMap: true});
|
||||
|
||||
var map = JSON.parse(result.map);
|
||||
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3},bar=15;');
|
||||
assert.strictEqual(map.sources[0], '/scripts/foo.js');
|
||||
});
|
||||
|
||||
it("Should correctly include source", function() {
|
||||
var jsSeq = [
|
||||
{'/scripts/foo.js': 'var foo = {"x": 1, y: 2, \'z\': 3};'},
|
||||
'var bar = 15;'
|
||||
];
|
||||
var result = Uglify.minify(jsSeq, {fromString: true, outSourceMap: true, sourceMapIncludeSources: true});
|
||||
|
||||
var map = JSON.parse(result.map);
|
||||
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3},bar=15;');
|
||||
assert.deepEqual(map.sourcesContent, ['var foo = {"x": 1, y: 2, \'z\': 3};', 'var bar = 15;']);
|
||||
});
|
||||
|
||||
});
|
||||
62
test/mocha/minify.js
Normal file
62
test/mocha/minify.js
Normal 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
88
test/mocha/new.js
Normal 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]
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
124
test/mocha/spidermonkey.js
Normal file
124
test/mocha/spidermonkey.js
Normal file
@@ -0,0 +1,124 @@
|
||||
var assert = require("assert");
|
||||
var exec = require("child_process").exec;
|
||||
var uglify = require("../../");
|
||||
|
||||
describe("spidermonkey export/import sanity test", function() {
|
||||
it("should produce a functional build when using --self with spidermonkey", function (done) {
|
||||
this.timeout(20000);
|
||||
|
||||
var uglifyjs = '"' + process.argv[0] + '" bin/uglifyjs';
|
||||
var command = uglifyjs + " --self -cm --wrap SpiderUglify --dump-spidermonkey-ast | " +
|
||||
uglifyjs + " --spidermonkey -cm";
|
||||
|
||||
exec(command, function (err, stdout) {
|
||||
if (err) throw err;
|
||||
|
||||
eval(stdout);
|
||||
assert.strictEqual(typeof SpiderUglify, "object");
|
||||
|
||||
var ast = SpiderUglify.parse("foo([true,,2+3]);");
|
||||
assert.strictEqual(true, ast instanceof SpiderUglify.AST_Node);
|
||||
|
||||
ast.figure_out_scope();
|
||||
ast = SpiderUglify.Compressor({}).compress(ast);
|
||||
assert.strictEqual(true, ast instanceof SpiderUglify.AST_Node);
|
||||
|
||||
var stream = SpiderUglify.OutputStream({});
|
||||
ast.print(stream);
|
||||
var code = stream.toString();
|
||||
assert.strictEqual(code, "foo([!0,,5]);");
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it("Should judge between directives and strings correctly on import", function() {
|
||||
var tests = [
|
||||
{
|
||||
input: '"use strict";;"use sloppy"',
|
||||
directives: 1,
|
||||
strings: 1
|
||||
},
|
||||
{
|
||||
input: ';"use strict"',
|
||||
directives: 0,
|
||||
strings: 1
|
||||
},
|
||||
{
|
||||
input: '"use strict"; "use something else";',
|
||||
directives: 2,
|
||||
strings: 0
|
||||
},
|
||||
{
|
||||
input: 'function foo() {"use strict";;"use sloppy" }',
|
||||
directives: 1,
|
||||
strings: 1
|
||||
},
|
||||
{
|
||||
input: 'function foo() {;"use strict" }',
|
||||
directives: 0,
|
||||
strings: 1
|
||||
},
|
||||
{
|
||||
input: 'function foo() {"use strict"; "use something else"; }',
|
||||
directives: 2,
|
||||
strings: 0
|
||||
},
|
||||
{
|
||||
input: 'var foo = function() {"use strict";;"use sloppy" }',
|
||||
directives: 1,
|
||||
strings: 1
|
||||
},
|
||||
{
|
||||
input: 'var foo = function() {;"use strict" }',
|
||||
directives: 0,
|
||||
strings: 1
|
||||
},
|
||||
{
|
||||
input: 'var foo = function() {"use strict"; "use something else"; }',
|
||||
directives: 2,
|
||||
strings: 0
|
||||
},
|
||||
{
|
||||
input: '{"use strict";;"use sloppy" }',
|
||||
directives: 0,
|
||||
strings: 2
|
||||
},
|
||||
{
|
||||
input: '{;"use strict" }',
|
||||
directives: 0,
|
||||
strings: 1
|
||||
},
|
||||
{
|
||||
input: '{"use strict"; "use something else"; }',
|
||||
directives: 0,
|
||||
strings: 2
|
||||
}
|
||||
];
|
||||
|
||||
var counter_directives;
|
||||
var counter_strings;
|
||||
|
||||
var checkWalker = new uglify.TreeWalker(function(node, descend) {
|
||||
if (node instanceof uglify.AST_String) {
|
||||
counter_strings++;
|
||||
} else if (node instanceof uglify.AST_Directive) {
|
||||
counter_directives++;
|
||||
}
|
||||
});
|
||||
|
||||
for (var i = 0; i < tests.length; i++) {
|
||||
counter_directives = 0;
|
||||
counter_strings = 0;
|
||||
|
||||
var ast = uglify.parse(tests[i].input);
|
||||
var moz_ast = ast.to_mozilla_ast();
|
||||
var from_moz_ast = uglify.AST_Node.from_mozilla_ast(moz_ast);
|
||||
|
||||
from_moz_ast.walk(checkWalker);
|
||||
|
||||
assert.strictEqual(counter_directives, tests[i].directives, "Directives count mismatch for test " + tests[i].input);
|
||||
assert.strictEqual(counter_strings, tests[i].strings, "String count mismatch for test " + tests[i].input);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -19,7 +19,7 @@ describe("String literals", function() {
|
||||
|
||||
var error = function(e) {
|
||||
return e instanceof UglifyJS.JS_Parse_Error &&
|
||||
e.message === "Unterminated string constant";
|
||||
e.message === "SyntaxError: Unterminated string constant";
|
||||
};
|
||||
|
||||
for (var input in inputs) {
|
||||
@@ -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 === "SyntaxError: 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
16
test/mocha/with.js
Normal 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);
|
||||
});
|
||||
});
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
@@ -54,18 +61,25 @@ exports.minify = function(files, options) {
|
||||
if (options.spidermonkey) {
|
||||
toplevel = UglifyJS.AST_Node.from_mozilla_ast(files);
|
||||
} else {
|
||||
if (typeof files == "string")
|
||||
files = [ files ];
|
||||
files.forEach(function(file, i){
|
||||
function addFile(file, fileUrl) {
|
||||
var code = options.fromString
|
||||
? file
|
||||
: fs.readFileSync(file, "utf8");
|
||||
sourcesContent[file] = code;
|
||||
sourcesContent[fileUrl] = code;
|
||||
toplevel = UglifyJS.parse(code, {
|
||||
filename: options.fromString ? i : file,
|
||||
filename: fileUrl,
|
||||
toplevel: toplevel,
|
||||
bare_returns: options.parse ? options.parse.bare_returns : undefined
|
||||
});
|
||||
}
|
||||
[].concat(files).forEach(function (files, i) {
|
||||
if (typeof files === 'string') {
|
||||
addFile(files, options.fromString ? i : files);
|
||||
} else {
|
||||
for (var fileUrl in files) {
|
||||
addFile(files[fileUrl], fileUrl);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if (options.wrap) {
|
||||
@@ -78,7 +92,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
|
||||
|
||||
Reference in New Issue
Block a user