Compare commits
47 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f46281e2b7 | ||
|
|
25a18883f5 | ||
|
|
5b4b07e9a7 | ||
|
|
a8aa28a7a6 | ||
|
|
fe5a68f9d5 | ||
|
|
71e61153b1 | ||
|
|
c8b6f4733d | ||
|
|
a48f87abf2 | ||
|
|
2fd927a7cc | ||
|
|
8428326ea1 | ||
|
|
31f8209193 | ||
|
|
9b0f86f5a1 | ||
|
|
ee082ace1b | ||
|
|
ae67a49850 | ||
|
|
4178289c38 | ||
|
|
74ae16f9f8 | ||
|
|
1968203d83 | ||
|
|
86ea38a259 | ||
|
|
8a713e449f | ||
|
|
24aa07855b | ||
|
|
5fd723f143 | ||
|
|
516eaef50c | ||
|
|
4ae1fb3ed8 | ||
|
|
011123223b | ||
|
|
96439ca246 | ||
|
|
c927cea632 | ||
|
|
9f4b98f8e4 | ||
|
|
0f2ef3367c | ||
|
|
7e5b5cac97 | ||
|
|
c1346e06b7 | ||
|
|
0d2fe8e3ef | ||
|
|
f2b9c11e2a | ||
|
|
fe647b083e | ||
|
|
dfe4f6c6de | ||
|
|
a09c8ad666 | ||
|
|
ec598c351b | ||
|
|
eba0f93bc0 | ||
|
|
99800d4aa9 | ||
|
|
70d56c951a | ||
|
|
b810e2f8da | ||
|
|
1abe14296e | ||
|
|
6920e898d1 | ||
|
|
dd71639264 | ||
|
|
2dcc552ce0 | ||
|
|
55387e8fd0 | ||
|
|
7e3e9da860 | ||
|
|
00f509405b |
275
README.md
275
README.md
@@ -153,10 +153,10 @@ Additional options:
|
||||
- `--source-map "filename='<NAME>'"` to specify the name of the source map.
|
||||
|
||||
- `--source-map "root='<URL>'"` to pass the URL where the original files can be found.
|
||||
Otherwise UglifyJS assumes HTTP `X-SourceMap` is being used and will omit the
|
||||
`//# sourceMappingURL=` directive.
|
||||
|
||||
- `--source-map "url='<URL>'"` to specify the URL where the source map can be found.
|
||||
Otherwise UglifyJS assumes HTTP `X-SourceMap` is being used and will omit the
|
||||
`//# sourceMappingURL=` directive.
|
||||
|
||||
For example:
|
||||
|
||||
@@ -203,11 +203,9 @@ Example:
|
||||
To enable the mangler you need to pass `--mangle` (`-m`). The following
|
||||
(comma-separated) options are supported:
|
||||
|
||||
- `toplevel` — mangle names declared in the top level scope (disabled by
|
||||
default).
|
||||
- `toplevel` (default `false`) -- mangle names declared in the top level scope.
|
||||
|
||||
- `eval` — mangle names visible in scopes where `eval` or `with` are used
|
||||
(disabled by default).
|
||||
- `eval` (default `false`) -- mangle names visible in scopes where `eval` or `with` are used.
|
||||
|
||||
When mangling is enabled but you want to prevent certain names from being
|
||||
mangled, you can declare those names with `--mangle reserved` — pass a
|
||||
@@ -223,7 +221,7 @@ to prevent the `require`, `exports` and `$` names from being changed.
|
||||
is a separate step, different from variable name mangling. Pass
|
||||
`--mangle-props` to enable it. It will mangle all properties in the
|
||||
input code with the exception of built in DOM properties and properties
|
||||
in core javascript classes. For example:
|
||||
in core JavaScript classes. For example:
|
||||
|
||||
```javascript
|
||||
// example.js
|
||||
@@ -238,7 +236,7 @@ x.bar_ = 2;
|
||||
x["baz_"] = 3;
|
||||
console.log(x.calc());
|
||||
```
|
||||
Mangle all properties (except for javascript `builtins`):
|
||||
Mangle all properties (except for JavaScript `builtins`):
|
||||
```bash
|
||||
$ uglifyjs example.js -c -m --mangle-props
|
||||
```
|
||||
@@ -510,11 +508,13 @@ if (result.error) throw result.error;
|
||||
|
||||
- `ie8` (default `false`) - set to `true` to support IE8.
|
||||
|
||||
- `keep_fnames` (default: `false`) - pass `true` to prevent discarding or mangling
|
||||
of function names. Useful for code relying on `Function.prototype.name`.
|
||||
|
||||
## Minify options structure
|
||||
|
||||
```javascript
|
||||
{
|
||||
warnings: false,
|
||||
parse: {
|
||||
// parse options
|
||||
},
|
||||
@@ -537,6 +537,7 @@ if (result.error) throw result.error;
|
||||
nameCache: null, // or specify a name cache object
|
||||
toplevel: false,
|
||||
ie8: false,
|
||||
warnings: false,
|
||||
}
|
||||
```
|
||||
|
||||
@@ -590,111 +591,88 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u
|
||||
## Parse options
|
||||
|
||||
- `bare_returns` (default `false`) -- support top level `return` statements
|
||||
|
||||
- `html5_comments` (default `true`)
|
||||
|
||||
- `shebang` (default `true`) -- support `#!command` as the first line
|
||||
|
||||
## Compress options
|
||||
|
||||
- `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.
|
||||
- `booleans` (default: `true`) -- various optimizations for boolean context, for example `!!a
|
||||
? b : c → a ? b : c`
|
||||
|
||||
- `properties` -- rewrite property access using the dot notation, for
|
||||
example `foo["bar"] → foo.bar`
|
||||
- `cascade` (default: `true`) -- small optimization for sequences, transform `x, x` into `x`
|
||||
and `x = something(), x` into `x = something()`
|
||||
|
||||
- `dead_code` -- remove unreachable code
|
||||
- `collapse_vars` (default: `true`) -- Collapse single-use non-constant variables - side
|
||||
effects permitting.
|
||||
|
||||
- `drop_debugger` -- remove `debugger;` statements
|
||||
|
||||
- `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.
|
||||
|
||||
- `unsafe_Func` (default: false) -- compress and mangle `Function(args, code)`
|
||||
when both `args` and `code` are string literals.
|
||||
|
||||
- `unsafe_math` (default: false) -- optimize numerical expressions like
|
||||
`2 * x * 3` into `6 * x`, which may give imprecise floating point results.
|
||||
|
||||
- `unsafe_proto` (default: false) -- optimize expressions like
|
||||
`Array.prototype.slice.call(a)` into `[].slice.call(a)`
|
||||
|
||||
- `unsafe_regexp` (default: false) -- enable substitutions of variables with
|
||||
`RegExp` values the same way as if they are constants.
|
||||
|
||||
- `conditionals` -- apply optimizations for `if`-s and conditional
|
||||
expressions
|
||||
|
||||
- `comparisons` -- apply certain optimizations to binary nodes, for example:
|
||||
- `comparisons` (default: `true`) -- apply certain optimizations to binary nodes, for example:
|
||||
`!(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
|
||||
- `conditionals` (default: `true`) -- apply optimizations for `if`-s and conditional
|
||||
expressions
|
||||
|
||||
- `booleans` -- various optimizations for boolean context, for example `!!a
|
||||
? b : c → a ? b : c`
|
||||
- `dead_code` (default: `true`) -- remove unreachable code
|
||||
|
||||
- `typeofs` -- default `true`. Transforms `typeof foo == "undefined"` into
|
||||
`foo === void 0`. Note: recommend to set this value to `false` for IE10 and
|
||||
earlier versions due to known issues.
|
||||
- `drop_console` (default: `false`) -- default `false`. Pass `true` to discard calls to
|
||||
`console.*` functions. If you wish to drop a specific function call
|
||||
such as `console.info` and/or retain side effects from function arguments
|
||||
after dropping the function call then use `pure_funcs` instead.
|
||||
|
||||
- `loops` -- optimizations for `do`, `while` and `for` loops when we can
|
||||
statically determine the condition
|
||||
- `drop_debugger` (default: `true`) -- remove `debugger;` statements
|
||||
|
||||
- `unused` -- drop unreferenced functions and variables (simple direct variable
|
||||
assignments do not count as references unless set to `"keep_assign"`)
|
||||
- `evaluate` (default: `true`) -- attempt to evaluate constant expressions
|
||||
|
||||
- `toplevel` -- drop unreferenced functions (`"funcs"`) and/or variables (`"vars"`)
|
||||
in the top level scope (`false` by default, `true` to drop both unreferenced
|
||||
functions and variables)
|
||||
- `expression` (default: `false`) -- default `false`. Pass `true` to preserve completion values
|
||||
from terminal statements without `return`, e.g. in bookmarklets.
|
||||
|
||||
- `top_retain` -- prevent specific toplevel functions and variables from `unused`
|
||||
removal (can be array, comma-separated, RegExp or function. Implies `toplevel`)
|
||||
- `global_defs` (default: `{}`) -- see [conditional compilation](#conditional-compilation)
|
||||
|
||||
- `hoist_funs` -- hoist function declarations
|
||||
- `hoist_funs` (default: `true`) -- hoist function declarations
|
||||
|
||||
- `hoist_vars` (default: false) -- hoist `var` declarations (this is `false`
|
||||
- `hoist_props` (default: `false`) -- hoist properties from constant object and
|
||||
array literals into regular variables subject to a set of constraints. For example:
|
||||
`var o={p:1, q:2}; f(o.p, o.q);` is converted to `f(1, 2);`. Note: `hoist_props`
|
||||
works best with `mangle` enabled, the `compress` option `passes` set to `2` or higher,
|
||||
and the `compress` option `toplevel` enabled.
|
||||
|
||||
- `hoist_vars` (default: `false`) -- hoist `var` declarations (this is `false`
|
||||
by default because it seems to increase the size of the output in general)
|
||||
|
||||
- `if_return` -- optimizations for if/return and if/continue
|
||||
- `if_return` (default: `true`) -- optimizations for if/return and if/continue
|
||||
|
||||
- `inline` -- embed simple functions
|
||||
- `inline` (default: `true`) -- embed simple functions
|
||||
|
||||
- `join_vars` -- join consecutive `var` statements
|
||||
- `join_vars` (default: `true`) -- join consecutive `var` statements
|
||||
|
||||
- `cascade` -- small optimization for sequences, transform `x, x` into `x`
|
||||
and `x = something(), x` into `x = something()`
|
||||
- `keep_fargs` (default: `true`) -- default `true`. Prevents the
|
||||
compressor from discarding unused function arguments. You need this
|
||||
for code which relies on `Function.length`.
|
||||
|
||||
- `collapse_vars` -- Collapse single-use non-constant variables - side
|
||||
effects permitting.
|
||||
- `keep_fnames` (default: `false`) -- Pass `true` to prevent the
|
||||
compressor from discarding function names. Useful for code relying on
|
||||
`Function.prototype.name`. See also: the `keep_fnames` [mangle option](#mangle).
|
||||
|
||||
- `reduce_vars` -- Improve optimization on variables assigned with and
|
||||
used as constant values.
|
||||
- `keep_infinity` (default: `false`) -- default `false`. Pass `true` to prevent `Infinity` from
|
||||
being compressed into `1/0`, which may cause performance issues on Chrome.
|
||||
|
||||
- `warnings` -- display warnings when dropping unreachable code or unused
|
||||
declarations etc.
|
||||
- `loops` (default: `true`) -- optimizations for `do`, `while` and `for` loops when we can
|
||||
statically determine the condition
|
||||
|
||||
- `negate_iife` -- negate "Immediately-Called Function Expressions"
|
||||
- `negate_iife` (default: `true`) -- negate "Immediately-Called Function Expressions"
|
||||
where the return value is discarded, to avoid the parens that the
|
||||
code generator would insert.
|
||||
|
||||
- `pure_getters` -- the default is `false`. If you pass `true` for
|
||||
this, UglifyJS will assume that object property access
|
||||
(e.g. `foo.bar` or `foo["bar"]`) doesn't have any side effects.
|
||||
Specify `"strict"` to treat `foo.bar` as side-effect-free only when
|
||||
`foo` is certain to not throw, i.e. not `null` or `undefined`.
|
||||
- `passes` (default: `1`) -- The maximum number of times to run compress.
|
||||
In some cases more than one pass leads to further compressed code. Keep in
|
||||
mind more passes will take more time.
|
||||
|
||||
- `pure_funcs` -- default `null`. You can pass an array of names and
|
||||
- `properties` (default: `true`) -- rewrite property access using the dot notation, for
|
||||
example `foo["bar"] → foo.bar`
|
||||
|
||||
- `pure_funcs` (default: `null`) -- You can pass an array of names and
|
||||
UglifyJS will assume that those functions do not produce side
|
||||
effects. DANGER: will not check if the name is redefined in scope.
|
||||
An example case here, for instance `var q = Math.floor(a/b)`. If
|
||||
@@ -705,49 +683,84 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u
|
||||
statement would get discarded. The current implementation adds some
|
||||
overhead (compression will be slower).
|
||||
|
||||
- `drop_console` -- default `false`. Pass `true` to discard calls to
|
||||
`console.*` functions. If you wish to drop a specific function call
|
||||
such as `console.info` and/or retain side effects from function arguments
|
||||
after dropping the function call then use `pure_funcs` instead.
|
||||
- `pure_getters` (default: `"strict"`) -- If you pass `true` for
|
||||
this, UglifyJS will assume that object property access
|
||||
(e.g. `foo.bar` or `foo["bar"]`) doesn't have any side effects.
|
||||
Specify `"strict"` to treat `foo.bar` as side-effect-free only when
|
||||
`foo` is certain to not throw, i.e. not `null` or `undefined`.
|
||||
|
||||
- `expression` -- default `false`. Pass `true` to preserve completion values
|
||||
from terminal statements without `return`, e.g. in bookmarklets.
|
||||
- `reduce_vars` (default: `true`) -- Improve optimization on variables assigned with and
|
||||
used as constant values.
|
||||
|
||||
- `keep_fargs` -- default `true`. Prevents the
|
||||
compressor from discarding unused function arguments. You need this
|
||||
for code which relies on `Function.length`.
|
||||
- `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.
|
||||
|
||||
- `keep_fnames` -- default `false`. Pass `true` to prevent the
|
||||
compressor from discarding function names. Useful for code relying on
|
||||
`Function.prototype.name`. See also: the `keep_fnames` [mangle option](#mangle).
|
||||
|
||||
- `passes` -- default `1`. The maximum number of times to run compress.
|
||||
In some cases more than one pass leads to further compressed code. Keep in
|
||||
mind more passes will take more time.
|
||||
|
||||
- `keep_infinity` -- default `false`. Pass `true` to prevent `Infinity` from
|
||||
being compressed into `1/0`, which may cause performance issues on Chrome.
|
||||
|
||||
- `side_effects` -- default `true`. Pass `false` to disable potentially dropping
|
||||
- `side_effects` (default: `true`) -- default `true`. Pass `false` to disable potentially dropping
|
||||
functions marked as "pure". A function call is marked as "pure" if a comment
|
||||
annotation `/*@__PURE__*/` or `/*#__PURE__*/` immediately precedes the call. For
|
||||
example: `/*@__PURE__*/foo();`
|
||||
|
||||
- `switches` (default: `true`) -- de-duplicate and remove unreachable `switch` branches
|
||||
|
||||
- `toplevel` (default: `false`) -- drop unreferenced functions (`"funcs"`) and/or variables (`"vars"`)
|
||||
in the top level scope (`false` by default, `true` to drop both unreferenced
|
||||
functions and variables)
|
||||
|
||||
- `top_retain` (default: `null`) -- prevent specific toplevel functions and variables from `unused`
|
||||
removal (can be array, comma-separated, RegExp or function. Implies `toplevel`)
|
||||
|
||||
- `typeofs` (default: `true`) -- default `true`. Transforms `typeof foo == "undefined"` into
|
||||
`foo === void 0`. Note: recommend to set this value to `false` for IE10 and
|
||||
earlier versions due to known issues.
|
||||
|
||||
- `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.
|
||||
|
||||
- `unsafe_Func` (default: `false`) -- compress and mangle `Function(args, code)`
|
||||
when both `args` and `code` are string literals.
|
||||
|
||||
- `unsafe_math` (default: `false`) -- optimize numerical expressions like
|
||||
`2 * x * 3` into `6 * x`, which may give imprecise floating point results.
|
||||
|
||||
- `unsafe_proto` (default: `false`) -- optimize expressions like
|
||||
`Array.prototype.slice.call(a)` into `[].slice.call(a)`
|
||||
|
||||
- `unsafe_regexp` (default: `false`) -- enable substitutions of variables with
|
||||
`RegExp` values the same way as if they are constants.
|
||||
|
||||
- `unused` (default: `true`) -- drop unreferenced functions and variables (simple direct variable
|
||||
assignments do not count as references unless set to `"keep_assign"`)
|
||||
|
||||
- `warnings` (default: `false`) -- display warnings when dropping unreachable code or unused
|
||||
declarations etc.
|
||||
|
||||
## Mangle options
|
||||
|
||||
- `eval` (default `false`). Pass `true` to mangle names visible in scopes
|
||||
where `eval` or `with` are used.
|
||||
|
||||
- `keep_fnames` (default `false`). Pass `true` to not mangle function names.
|
||||
Useful for code relying on `Function.prototype.name`. See also: the `keep_fnames`
|
||||
[compress option](#compress-options).
|
||||
|
||||
- `reserved` (default `[]`). Pass an array of identifiers that should be
|
||||
excluded from mangling. Example: `["foo", "bar"]`.
|
||||
|
||||
- `toplevel` (default `false`). Pass `true` to mangle names declared in the
|
||||
top level scope.
|
||||
|
||||
- `keep_fnames` (default `false`). Pass `true` to not mangle function names.
|
||||
Useful for code relying on `Function.prototype.name`. See also: the `keep_fnames`
|
||||
[compress option](#compress-options).
|
||||
|
||||
- `eval` (default `false`). Pass `true` to mangle names visible in scopes
|
||||
where `eval` or `with` are used.
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
@@ -772,16 +785,20 @@ UglifyJS.minify(code, { mangle: { toplevel: true } }).code;
|
||||
|
||||
### Mangle properties options
|
||||
|
||||
- `reserved` (default: `[]`) -- Do not mangle property names listed in the
|
||||
`reserved` array.
|
||||
- `regex` (default: `null`) -— Pass a RegExp literal to only mangle property
|
||||
names matching the regular expression.
|
||||
- `keep_quoted` (default: `false`) -— Only mangle unquoted property names.
|
||||
- `debug` (default: `false`) -— Mangle names with the original name still present.
|
||||
Pass an empty string `""` to enable, or a non-empty string to set the debug suffix.
|
||||
- `builtins` (default: `false`) -- Use `true` to allow the mangling of builtin
|
||||
DOM properties. Not recommended to override this setting.
|
||||
|
||||
- `debug` (default: `false`) -— Mangle names with the original name still present.
|
||||
Pass an empty string `""` to enable, or a non-empty string to set the debug suffix.
|
||||
|
||||
- `keep_quoted` (default: `false`) -— Only mangle unquoted property names.
|
||||
|
||||
- `regex` (default: `null`) -— Pass a RegExp literal to only mangle property
|
||||
names matching the regular expression.
|
||||
|
||||
- `reserved` (default: `[]`) -- Do not mangle property names listed in the
|
||||
`reserved` array.
|
||||
|
||||
## Output options
|
||||
|
||||
The code generator tries to output shortest code possible by default. In
|
||||
@@ -790,31 +807,43 @@ can pass additional arguments that control the code output:
|
||||
|
||||
- `ascii_only` (default `false`) -- escape Unicode characters in strings and
|
||||
regexps (affects directives with non-ascii characters becoming invalid)
|
||||
|
||||
- `beautify` (default `true`) -- whether to actually beautify the output.
|
||||
Passing `-b` will set this to true, but you might need to pass `-b` even
|
||||
when you want to generate minified code, in order to specify additional
|
||||
arguments, so you can use `-b beautify=false` to override it.
|
||||
|
||||
- `bracketize` (default `false`) -- always insert brackets in `if`, `for`,
|
||||
`do`, `while` or `with` statements, even if their body is a single
|
||||
statement.
|
||||
|
||||
- `comments` (default `false`) -- pass `true` or `"all"` to preserve all
|
||||
comments, `"some"` to preserve some comments, a regular expression string
|
||||
(e.g. `/^!/`) or a function.
|
||||
- `indent_level` (default 4)
|
||||
- `indent_start` (default 0) -- prefix all lines by that many spaces
|
||||
|
||||
- `indent_level` (default `4`)
|
||||
|
||||
- `indent_start` (default `0`) -- prefix all lines by that many spaces
|
||||
|
||||
- `inline_script` (default `false`) -- escape the slash in occurrences of
|
||||
`</script` in strings
|
||||
|
||||
- `keep_quoted_props` (default `false`) -- when turned on, prevents stripping
|
||||
quotes from property names in object literals.
|
||||
|
||||
- `max_line_len` (default `false`) -- maximum line length (for uglified code)
|
||||
|
||||
- `preamble` (default `null`) -- when passed it must be a string and
|
||||
it will be prepended to the output literally. The source map will
|
||||
adjust for this text. Can be used to insert a comment containing
|
||||
licensing information, for example.
|
||||
|
||||
- `preserve_line` (default `false`) -- pass `true` to preserve lines, but it
|
||||
only works if `beautify` is set to `false`.
|
||||
|
||||
- `quote_keys` (default `false`) -- pass `true` to quote all keys in literal
|
||||
objects
|
||||
|
||||
- `quote_style` (default `0`) -- preferred quote style for strings (affects
|
||||
quoted property names and directives as well):
|
||||
- `0` -- prefers double quotes, switches to single quotes when there are
|
||||
@@ -822,16 +851,20 @@ 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
|
||||
|
||||
- `semicolons` (default `true`) -- separate statements with semicolons. If
|
||||
you pass `false` then whenever possible we will use a newline instead of a
|
||||
semicolon, leading to more readable output of uglified code (size before
|
||||
gzip could be smaller; size after gzip insignificantly larger).
|
||||
|
||||
- `shebang` (default `true`) -- preserve shebang `#!` in preamble (bash scripts)
|
||||
- `width` (default 80) -- only takes effect when beautification is on, this
|
||||
|
||||
- `width` (default `80`) -- only takes effect when beautification is on, this
|
||||
specifies an (orientative) line width that the beautifier will try to
|
||||
obey. It refers to the width of the line text (excluding indentation).
|
||||
It doesn't work very well currently, but it does make the code generated
|
||||
by UglifyJS more readable.
|
||||
|
||||
- `wrap_iife` (default `false`) -- pass `true` to wrap immediately invoked
|
||||
function expressions. See
|
||||
[#640](https://github.com/mishoo/UglifyJS2/issues/640) for more details.
|
||||
@@ -1033,7 +1066,7 @@ in total it's a bit more than just using UglifyJS's own parser.
|
||||
### Uglify Fast Minify Mode
|
||||
|
||||
It's not well known, but whitespace removal and symbol mangling accounts
|
||||
for 95% of the size reduction in minified code for most javascript - not
|
||||
for 95% of the size reduction in minified code for most JavaScript - not
|
||||
elaborate code transforms. One can simply disable `compress` to speed up
|
||||
Uglify builds by 3 to 4 times. In this fast `mangle`-only mode Uglify has
|
||||
comparable minify speeds and gzip sizes to
|
||||
|
||||
@@ -134,11 +134,10 @@ var AST_Debugger = DEFNODE("Debugger", null, {
|
||||
$documentation: "Represents a debugger statement",
|
||||
}, AST_Statement);
|
||||
|
||||
var AST_Directive = DEFNODE("Directive", "value scope quote", {
|
||||
var AST_Directive = DEFNODE("Directive", "value quote", {
|
||||
$documentation: "Represents a directive, like \"use strict\";",
|
||||
$propdoc: {
|
||||
value: "[string] The value of this directive as a plain string (it's not an AST_String!)",
|
||||
scope: "[AST_Scope/S] The scope that this directive affects",
|
||||
quote: "[string] the original quote character"
|
||||
},
|
||||
}, AST_Statement);
|
||||
@@ -299,10 +298,9 @@ var AST_With = DEFNODE("With", "expression", {
|
||||
|
||||
/* -----[ scope and functions ]----- */
|
||||
|
||||
var AST_Scope = DEFNODE("Scope", "directives variables functions uses_with uses_eval parent_scope enclosed cname", {
|
||||
var AST_Scope = DEFNODE("Scope", "variables functions uses_with uses_eval parent_scope enclosed cname", {
|
||||
$documentation: "Base class for all statements introducing a lexical scope",
|
||||
$propdoc: {
|
||||
directives: "[string*/S] an array of directives declared in this scope",
|
||||
variables: "[Object/S] a map of name -> SymbolDef for all variables/functions defined in this scope",
|
||||
functions: "[Object/S] like `variables`, but only lists function declarations",
|
||||
uses_with: "[boolean/S] tells whether this scope uses the `with` statement",
|
||||
|
||||
585
lib/compress.js
585
lib/compress.js
@@ -60,6 +60,7 @@ function Compressor(options, false_by_default) {
|
||||
expression : false,
|
||||
global_defs : {},
|
||||
hoist_funs : !false_by_default,
|
||||
hoist_props : false,
|
||||
hoist_vars : false,
|
||||
ie8 : false,
|
||||
if_return : !false_by_default,
|
||||
@@ -151,7 +152,7 @@ merge(Compressor.prototype, {
|
||||
var last_count = 1 / 0;
|
||||
for (var pass = 0; pass < passes; pass++) {
|
||||
if (pass > 0 || this.option("reduce_vars"))
|
||||
node.reset_opt_flags(this, true);
|
||||
node.reset_opt_flags(this);
|
||||
node = node.transform(this);
|
||||
if (passes > 1) {
|
||||
var count = 0;
|
||||
@@ -190,6 +191,7 @@ merge(Compressor.prototype, {
|
||||
if (node._squeezed) return node;
|
||||
var was_scope = false;
|
||||
if (node instanceof AST_Scope) {
|
||||
node = node.hoist_properties(this);
|
||||
node = node.hoist_declarations(this);
|
||||
was_scope = true;
|
||||
}
|
||||
@@ -283,8 +285,13 @@ merge(Compressor.prototype, {
|
||||
self.transform(tt);
|
||||
});
|
||||
|
||||
AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan) {
|
||||
var reduce_vars = rescan && compressor.option("reduce_vars");
|
||||
AST_Toplevel.DEFMETHOD("reset_opt_flags", function(compressor) {
|
||||
var reduce_vars = compressor.option("reduce_vars");
|
||||
var unused = compressor.option("unused");
|
||||
// Stack of look-up tables to keep track of whether a `SymbolDef` has been
|
||||
// properly assigned before use:
|
||||
// - `push()` & `pop()` when visiting conditional branches
|
||||
// - backup & restore via `save_ids` when visiting out-of-order sections
|
||||
var safe_ids = Object.create(null);
|
||||
var suppressor = new TreeWalker(function(node) {
|
||||
if (!(node instanceof AST_Symbol)) return;
|
||||
@@ -293,6 +300,8 @@ merge(Compressor.prototype, {
|
||||
if (node instanceof AST_SymbolRef) d.references.push(node);
|
||||
d.fixed = false;
|
||||
});
|
||||
var in_loop = null;
|
||||
var loop_ids = Object.create(null);
|
||||
var tw = new TreeWalker(function(node, descend) {
|
||||
node._squeezed = false;
|
||||
node._optimized = false;
|
||||
@@ -302,16 +311,31 @@ merge(Compressor.prototype, {
|
||||
if (node instanceof AST_SymbolRef) {
|
||||
var d = node.definition();
|
||||
d.references.push(node);
|
||||
if (d.fixed === undefined || !safe_to_read(d)
|
||||
|| is_modified(node, 0, is_immutable(node.fixed_value()))) {
|
||||
if (d.fixed === undefined || !safe_to_read(d) || d.single_use == "m") {
|
||||
d.fixed = false;
|
||||
} else {
|
||||
var parent = tw.parent();
|
||||
if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right
|
||||
|| parent instanceof AST_Call && node !== parent.expression
|
||||
|| parent instanceof AST_Return && node === parent.value && node.scope !== d.scope
|
||||
|| parent instanceof AST_VarDef && node === parent.value) {
|
||||
d.escaped = true;
|
||||
} else if (d.fixed) {
|
||||
var value = node.fixed_value();
|
||||
if (unused && value && d.references.length == 1) {
|
||||
if (value instanceof AST_Lambda) {
|
||||
d.single_use = d.scope === node.scope
|
||||
&& !(d.orig[0] instanceof AST_SymbolFunarg)
|
||||
|| value.is_constant_expression(node.scope);
|
||||
} else {
|
||||
d.single_use = d.scope === node.scope
|
||||
&& loop_ids[d.id] === in_loop
|
||||
&& value.is_constant_expression();
|
||||
}
|
||||
} else {
|
||||
d.single_use = false;
|
||||
}
|
||||
if (is_modified(node, value, 0, is_immutable(value))) {
|
||||
if (d.single_use) {
|
||||
d.single_use = "m";
|
||||
} else {
|
||||
d.fixed = false;
|
||||
}
|
||||
} else {
|
||||
mark_escaped(d, node, value, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -325,6 +349,7 @@ merge(Compressor.prototype, {
|
||||
d.fixed = function() {
|
||||
return node.value;
|
||||
};
|
||||
loop_ids[d.id] = in_loop;
|
||||
mark(d, false);
|
||||
descend();
|
||||
} else {
|
||||
@@ -358,6 +383,10 @@ merge(Compressor.prototype, {
|
||||
} else {
|
||||
d.fixed = node;
|
||||
mark(d, true);
|
||||
if (unused && d.references.length == 1) {
|
||||
d.single_use = d.scope === d.references[0].scope
|
||||
|| node.is_constant_expression(d.references[0].scope);
|
||||
}
|
||||
}
|
||||
var save_ids = safe_ids;
|
||||
safe_ids = Object.create(null);
|
||||
@@ -380,6 +409,7 @@ merge(Compressor.prototype, {
|
||||
d.fixed = function() {
|
||||
return iife.args[i] || make_node(AST_Undefined, iife);
|
||||
};
|
||||
loop_ids[d.id] = in_loop;
|
||||
mark(d, true);
|
||||
} else {
|
||||
d.fixed = false;
|
||||
@@ -391,14 +421,12 @@ merge(Compressor.prototype, {
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Accessor) {
|
||||
var save_ids = safe_ids;
|
||||
safe_ids = Object.create(null);
|
||||
push();
|
||||
descend();
|
||||
safe_ids = save_ids;
|
||||
pop();
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Binary
|
||||
&& (node.operator == "&&" || node.operator == "||")) {
|
||||
if (node instanceof AST_Binary && lazy_op(node.operator)) {
|
||||
node.left.walk(tw);
|
||||
push();
|
||||
node.right.walk(tw);
|
||||
@@ -428,10 +456,13 @@ merge(Compressor.prototype, {
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_DWLoop) {
|
||||
var saved_loop = in_loop;
|
||||
in_loop = node;
|
||||
push();
|
||||
node.condition.walk(tw);
|
||||
node.body.walk(tw);
|
||||
pop();
|
||||
in_loop = saved_loop;
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_LabeledStatement) {
|
||||
@@ -442,6 +473,8 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
if (node instanceof AST_For) {
|
||||
if (node.init) node.init.walk(tw);
|
||||
var saved_loop = in_loop;
|
||||
in_loop = node;
|
||||
if (node.condition) {
|
||||
push();
|
||||
node.condition.walk(tw);
|
||||
@@ -455,14 +488,18 @@ merge(Compressor.prototype, {
|
||||
node.step.walk(tw);
|
||||
pop();
|
||||
}
|
||||
in_loop = saved_loop;
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_ForIn) {
|
||||
node.init.walk(suppressor);
|
||||
node.object.walk(tw);
|
||||
var saved_loop = in_loop;
|
||||
in_loop = node;
|
||||
push();
|
||||
node.body.walk(tw);
|
||||
pop();
|
||||
in_loop = saved_loop;
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Try) {
|
||||
@@ -500,6 +537,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return def.fixed instanceof AST_Defun;
|
||||
}
|
||||
|
||||
function safe_to_assign(def, value) {
|
||||
@@ -522,6 +560,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
|
||||
function reset_def(def) {
|
||||
def.direct_access = false;
|
||||
def.escaped = false;
|
||||
if (def.scope.uses_eval) {
|
||||
def.fixed = false;
|
||||
@@ -532,24 +571,71 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
def.references = [];
|
||||
def.should_replace = undefined;
|
||||
def.single_use = undefined;
|
||||
}
|
||||
|
||||
function is_immutable(value) {
|
||||
return value && value.is_constant() || value instanceof AST_Lambda;
|
||||
if (!value) return false;
|
||||
return value.is_constant()
|
||||
|| value instanceof AST_Lambda
|
||||
|| value instanceof AST_This;
|
||||
}
|
||||
|
||||
function is_modified(node, level, immutable) {
|
||||
function read_property(obj, key) {
|
||||
if (key instanceof AST_Constant) key = key.getValue();
|
||||
if (key instanceof AST_Node) return null;
|
||||
var value;
|
||||
if (obj instanceof AST_Array) {
|
||||
var elements = obj.elements;
|
||||
if (key == "length") return make_node_from_constant(elements.length, obj);
|
||||
if (typeof key == "number" && key in elements) value = elements[key];
|
||||
} else if (obj instanceof AST_Object) {
|
||||
var props = obj.properties;
|
||||
for (var i = props.length; --i >= 0;) {
|
||||
var prop = props[i];
|
||||
if (!(prop instanceof AST_ObjectKeyVal)) return;
|
||||
if (!value && props[i].key === key) value = props[i].value;
|
||||
}
|
||||
}
|
||||
return value instanceof AST_SymbolRef && value.fixed_value() || value;
|
||||
}
|
||||
|
||||
function is_modified(node, value, level, immutable) {
|
||||
var parent = tw.parent(level);
|
||||
if (is_lhs(node, parent)
|
||||
|| !immutable && parent instanceof AST_Call && parent.expression === node) {
|
||||
|| !immutable
|
||||
&& parent instanceof AST_Call
|
||||
&& parent.expression === node
|
||||
&& (!(value instanceof AST_Function) || value.contains_this(parent))) {
|
||||
return true;
|
||||
} else if (parent instanceof AST_Array || parent instanceof AST_Object) {
|
||||
return is_modified(parent, parent, level + 1);
|
||||
} else if (parent instanceof AST_PropAccess && parent.expression === node) {
|
||||
return !immutable && is_modified(parent, level + 1);
|
||||
return !immutable && is_modified(parent, read_property(value, parent.property), level + 1);
|
||||
}
|
||||
}
|
||||
|
||||
function mark_escaped(d, node, value, level) {
|
||||
var parent = tw.parent(level);
|
||||
if (value instanceof AST_Constant || value instanceof AST_Function) return;
|
||||
if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right
|
||||
|| parent instanceof AST_Call && node !== parent.expression
|
||||
|| parent instanceof AST_Return && node === parent.value && node.scope !== d.scope
|
||||
|| parent instanceof AST_VarDef && node === parent.value) {
|
||||
d.escaped = true;
|
||||
return;
|
||||
} else if (parent instanceof AST_Array || parent instanceof AST_Object) {
|
||||
mark_escaped(d, parent, parent, level + 1);
|
||||
} else if (parent instanceof AST_PropAccess && node === parent.expression) {
|
||||
value = read_property(value, parent.property);
|
||||
mark_escaped(d, parent, value, level + 1);
|
||||
if (value) return;
|
||||
}
|
||||
if (level == 0) d.direct_access = true;
|
||||
}
|
||||
});
|
||||
|
||||
AST_SymbolRef.DEFMETHOD("fixed_value", function() {
|
||||
AST_Symbol.DEFMETHOD("fixed_value", function() {
|
||||
var fixed = this.definition().fixed;
|
||||
if (!fixed || fixed instanceof AST_Node) return fixed;
|
||||
return fixed();
|
||||
@@ -733,6 +819,7 @@ merge(Compressor.prototype, {
|
||||
function collapse(statements, compressor) {
|
||||
var scope = compressor.find_parent(AST_Scope);
|
||||
if (scope.uses_eval || scope.uses_with) return statements;
|
||||
var args;
|
||||
var candidates = [];
|
||||
var stat_index = statements.length;
|
||||
while (--stat_index >= 0) {
|
||||
@@ -750,9 +837,10 @@ merge(Compressor.prototype, {
|
||||
// Locate symbols which may execute code outside of scanning range
|
||||
var lvalues = get_lvalues(candidate);
|
||||
if (lhs instanceof AST_SymbolRef) lvalues[lhs.name] = false;
|
||||
var one_off = lhs instanceof AST_Symbol && lhs.definition().references.length == 1;
|
||||
var side_effects = value_has_side_effects(candidate);
|
||||
var hit = candidate.name instanceof AST_SymbolFunarg;
|
||||
var abort = false, replaced = false;
|
||||
var abort = false, replaced = false, can_replace = !args || !hit;
|
||||
var tt = new TreeTransformer(function(node, descend) {
|
||||
if (abort) return node;
|
||||
// Skip nodes before `candidate` as quickly as possible
|
||||
@@ -777,7 +865,8 @@ merge(Compressor.prototype, {
|
||||
return node;
|
||||
}
|
||||
// Replace variable with assignment when found
|
||||
if (!(node instanceof AST_SymbolDeclaration)
|
||||
if (can_replace
|
||||
&& !(node instanceof AST_SymbolDeclaration)
|
||||
&& !is_lhs(node, parent)
|
||||
&& lhs.equivalent_to(node)) {
|
||||
CHANGED = replaced = abort = true;
|
||||
@@ -810,16 +899,17 @@ merge(Compressor.prototype, {
|
||||
if (node instanceof AST_Call
|
||||
|| node instanceof AST_Exit
|
||||
|| node instanceof AST_PropAccess
|
||||
&& (side_effects || node.expression.may_throw_on_access(compressor))
|
||||
|| node instanceof AST_SymbolRef
|
||||
&& (lvalues[node.name]
|
||||
|| side_effects && !references_in_scope(node.definition()))
|
||||
|| (sym = lhs_or_def(node)) && get_symbol(sym).name in lvalues
|
||||
|| parent instanceof AST_Binary
|
||||
&& (parent.operator == "&&" || parent.operator == "||")
|
||||
|| parent instanceof AST_Case
|
||||
|| parent instanceof AST_Conditional
|
||||
|| parent instanceof AST_For
|
||||
|| parent instanceof AST_If) {
|
||||
|| (sym = lhs_or_def(node))
|
||||
&& (sym instanceof AST_PropAccess || sym.name in lvalues)
|
||||
|| (side_effects || !one_off)
|
||||
&& (parent instanceof AST_Binary && lazy_op(parent.operator)
|
||||
|| parent instanceof AST_Case
|
||||
|| parent instanceof AST_Conditional
|
||||
|| parent instanceof AST_If)) {
|
||||
if (!(node instanceof AST_Scope)) descend(node, tt);
|
||||
abort = true;
|
||||
return node;
|
||||
@@ -827,6 +917,12 @@ merge(Compressor.prototype, {
|
||||
// Skip (non-executed) functions and (leading) default case in switch statements
|
||||
if (node instanceof AST_Default || node instanceof AST_Scope) return node;
|
||||
});
|
||||
if (!can_replace) {
|
||||
for (var j = compressor.self().argnames.lastIndexOf(candidate.name) + 1; j < args.length; j++) {
|
||||
args[j].transform(tt);
|
||||
}
|
||||
can_replace = true;
|
||||
}
|
||||
for (var i = stat_index; !abort && i < statements.length; i++) {
|
||||
statements[i].transform(tt);
|
||||
}
|
||||
@@ -842,12 +938,20 @@ merge(Compressor.prototype, {
|
||||
&& !fn.uses_eval
|
||||
&& (iife = compressor.parent()) instanceof AST_Call
|
||||
&& iife.expression === fn) {
|
||||
var fn_strict = compressor.has_directive("use strict");
|
||||
if (fn_strict && fn.body.indexOf(fn_strict) < 0) fn_strict = false;
|
||||
var len = fn.argnames.length;
|
||||
args = iife.args.slice(len);
|
||||
var names = Object.create(null);
|
||||
for (var i = fn.argnames.length; --i >= 0;) {
|
||||
for (var i = len; --i >= 0;) {
|
||||
var sym = fn.argnames[i];
|
||||
var arg = iife.args[i];
|
||||
args.unshift(make_node(AST_VarDef, sym, {
|
||||
name: sym,
|
||||
value: arg
|
||||
}));
|
||||
if (sym.name in names) continue;
|
||||
names[sym.name] = true;
|
||||
var arg = iife.args[i];
|
||||
if (!arg) arg = make_node(AST_Undefined, sym).transform(compressor);
|
||||
else {
|
||||
var tw = new TreeWalker(function(node) {
|
||||
@@ -859,7 +963,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
arg = null;
|
||||
}
|
||||
if (node instanceof AST_This && !tw.find_parent(AST_Scope)) {
|
||||
if (node instanceof AST_This && (fn_strict || !tw.find_parent(AST_Scope))) {
|
||||
arg = null;
|
||||
return true;
|
||||
}
|
||||
@@ -903,27 +1007,14 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
|
||||
function get_symbol(node) {
|
||||
while (node instanceof AST_PropAccess) node = node.expression;
|
||||
return node;
|
||||
}
|
||||
|
||||
function get_lvalues(expr) {
|
||||
var lvalues = Object.create(null);
|
||||
if (expr instanceof AST_Unary) return lvalues;
|
||||
var scope;
|
||||
var tw = new TreeWalker(function(node, descend) {
|
||||
if (node instanceof AST_Scope) {
|
||||
var save_scope = scope;
|
||||
descend();
|
||||
scope = save_scope;
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_SymbolRef || node instanceof AST_PropAccess) {
|
||||
var sym = get_symbol(node);
|
||||
if (sym instanceof AST_SymbolRef) {
|
||||
lvalues[sym.name] = lvalues[sym.name] || is_lhs(node, tw.parent());
|
||||
}
|
||||
var sym = node;
|
||||
while (sym instanceof AST_PropAccess) sym = sym.expression;
|
||||
if (sym instanceof AST_SymbolRef || sym instanceof AST_This) {
|
||||
lvalues[sym.name] = lvalues[sym.name] || is_lhs(node, tw.parent());
|
||||
}
|
||||
});
|
||||
expr[expr instanceof AST_Assign ? "right" : "value"].walk(tw);
|
||||
@@ -1400,9 +1491,10 @@ merge(Compressor.prototype, {
|
||||
return member(this.operator, unary_bool);
|
||||
});
|
||||
def(AST_Binary, function(){
|
||||
return member(this.operator, binary_bool) ||
|
||||
( (this.operator == "&&" || this.operator == "||") &&
|
||||
this.left.is_boolean() && this.right.is_boolean() );
|
||||
return member(this.operator, binary_bool)
|
||||
|| lazy_op(this.operator)
|
||||
&& this.left.is_boolean()
|
||||
&& this.right.is_boolean();
|
||||
});
|
||||
def(AST_Conditional, function(){
|
||||
return this.consequent.is_boolean() && this.alternative.is_boolean();
|
||||
@@ -1471,6 +1563,7 @@ merge(Compressor.prototype, {
|
||||
node.DEFMETHOD("is_string", func);
|
||||
});
|
||||
|
||||
var lazy_op = makePredicate("&& ||");
|
||||
var unary_side_effects = makePredicate("delete ++ --");
|
||||
|
||||
function is_lhs(node, parent) {
|
||||
@@ -1582,35 +1675,6 @@ merge(Compressor.prototype, {
|
||||
&& unaryPrefix(this.operator);
|
||||
}
|
||||
});
|
||||
// Obtain the constant value of an expression already known to be constant.
|
||||
// Result only valid iff this.is_constant() is true.
|
||||
AST_Node.DEFMETHOD("constant_value", function(compressor){
|
||||
// Accomodate when option evaluate=false.
|
||||
if (this instanceof AST_Constant && !(this instanceof AST_RegExp)) {
|
||||
return this.value;
|
||||
}
|
||||
// Accomodate the common constant expressions !0 and -1 when option evaluate=false.
|
||||
if (this instanceof AST_UnaryPrefix
|
||||
&& this.expression instanceof AST_Constant) switch (this.operator) {
|
||||
case "!":
|
||||
return !this.expression.value;
|
||||
case "~":
|
||||
return ~this.expression.value;
|
||||
case "-":
|
||||
return -this.expression.value;
|
||||
case "+":
|
||||
return +this.expression.value;
|
||||
default:
|
||||
throw new Error(string_template("Cannot evaluate unary expression {value}", {
|
||||
value: this.print_to_string()
|
||||
}));
|
||||
}
|
||||
var result = this.evaluate(compressor);
|
||||
if (result !== this) {
|
||||
return result;
|
||||
}
|
||||
throw new Error(string_template("Cannot evaluate constant [{file}:{line},{col}]", this.start));
|
||||
});
|
||||
def(AST_Statement, function(){
|
||||
throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));
|
||||
});
|
||||
@@ -1629,6 +1693,7 @@ merge(Compressor.prototype, {
|
||||
var elements = [];
|
||||
for (var i = 0, len = this.elements.length; i < len; i++) {
|
||||
var element = this.elements[i];
|
||||
if (element instanceof AST_Function) continue;
|
||||
var value = ev(element, compressor);
|
||||
if (element === value) return this;
|
||||
elements.push(value);
|
||||
@@ -1652,6 +1717,7 @@ merge(Compressor.prototype, {
|
||||
if (typeof Object.prototype[key] === 'function') {
|
||||
return this;
|
||||
}
|
||||
if (prop.value instanceof AST_Function) continue;
|
||||
val[key] = ev(prop.value, compressor);
|
||||
if (val[key] === prop.value) return this;
|
||||
}
|
||||
@@ -2110,6 +2176,26 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
def(AST_Node, return_false);
|
||||
def(AST_Constant, return_true);
|
||||
def(AST_Lambda, function(scope){
|
||||
var self = this;
|
||||
var result = true;
|
||||
self.walk(new TreeWalker(function(node) {
|
||||
if (!result) return true;
|
||||
if (node instanceof AST_SymbolRef) {
|
||||
var def = node.definition();
|
||||
if (member(def, self.enclosed)
|
||||
&& !self.variables.has(def.name)) {
|
||||
if (scope) {
|
||||
var scope_def = scope.find_variable(node);
|
||||
if (def.undeclared ? !scope_def : scope_def === def) return true;
|
||||
}
|
||||
result = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}));
|
||||
return result;
|
||||
});
|
||||
def(AST_Unary, function(){
|
||||
return this.expression.is_constant_expression();
|
||||
});
|
||||
@@ -2193,7 +2279,6 @@ merge(Compressor.prototype, {
|
||||
if (self.uses_eval || self.uses_with) return;
|
||||
var drop_funcs = !(self instanceof AST_Toplevel) || compressor.toplevel.funcs;
|
||||
var drop_vars = !(self instanceof AST_Toplevel) || compressor.toplevel.vars;
|
||||
if (!drop_funcs && !drop_vars) return;
|
||||
var assign_as_unused = /keep_assign/.test(compressor.option("unused")) ? return_false : function(node) {
|
||||
if (node instanceof AST_Assign && (node.write_only || node.operator == "=")) {
|
||||
return node.left;
|
||||
@@ -2249,7 +2334,10 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
return true;
|
||||
}
|
||||
if (assign_as_unused(node) instanceof AST_SymbolRef && scope === self) {
|
||||
var sym;
|
||||
if (scope === self
|
||||
&& (sym = assign_as_unused(node)) instanceof AST_SymbolRef
|
||||
&& self.variables.get(sym.name) === sym.definition()) {
|
||||
if (node instanceof AST_Assign) node.right.walk(tw);
|
||||
return true;
|
||||
}
|
||||
@@ -2322,13 +2410,16 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
if (drop_funcs && node instanceof AST_Defun && node !== self) {
|
||||
if (!(node.name.definition().id in in_use_ids)) {
|
||||
var def = node.name.definition();
|
||||
if (!(def.id in in_use_ids)) {
|
||||
compressor[node.name.unreferenced() ? "warn" : "info"]("Dropping unused function {name} [{file}:{line},{col}]", template(node.name));
|
||||
drop_decl(def, node.name);
|
||||
return make_node(AST_EmptyStatement, node);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
if (drop_vars && node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn && tt.parent().init === node)) {
|
||||
var parent = tt.parent();
|
||||
if (node instanceof AST_Definitions && !(parent instanceof AST_ForIn && parent.init === node)) {
|
||||
// place uninitialized names at the start
|
||||
var body = [], head = [], tail = [];
|
||||
// for unused names whose initialization has
|
||||
@@ -2338,13 +2429,13 @@ merge(Compressor.prototype, {
|
||||
node.definitions.forEach(function(def) {
|
||||
if (def.value) def.value = def.value.transform(tt);
|
||||
var sym = def.name.definition();
|
||||
if (sym.id in in_use_ids) {
|
||||
if (!drop_vars || sym.id in in_use_ids) {
|
||||
if (def.name instanceof AST_SymbolVar) {
|
||||
var var_defs = var_defs_by_id.get(sym.id);
|
||||
if (var_defs.length > 1 && !def.value) {
|
||||
compressor.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name));
|
||||
remove(var_defs, def);
|
||||
remove(sym.orig, def.name);
|
||||
drop_decl(sym, def.name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -2377,7 +2468,7 @@ merge(Compressor.prototype, {
|
||||
} else {
|
||||
compressor[def.name.unreferenced() ? "warn" : "info"]("Dropping unused variable {name} [{file}:{line},{col}]", template(def.name));
|
||||
}
|
||||
remove(sym.orig, def.name);
|
||||
drop_decl(sym, def.name);
|
||||
}
|
||||
});
|
||||
if (head.length == 0 && tail.length == 1 && tail[0].name instanceof AST_SymbolVar) {
|
||||
@@ -2386,7 +2477,7 @@ merge(Compressor.prototype, {
|
||||
var def = tail.pop();
|
||||
compressor.warn("Converting duplicated definition of variable {name} to assignment [{file}:{line},{col}]", template(def.name));
|
||||
remove(var_defs, def);
|
||||
remove(def.name.definition().orig, def.name);
|
||||
drop_decl(def.name.definition(), def.name);
|
||||
side_effects.unshift(make_node(AST_Assign, def, {
|
||||
operator: "=",
|
||||
left: make_node(AST_SymbolRef, def.name, def.name),
|
||||
@@ -2404,23 +2495,22 @@ merge(Compressor.prototype, {
|
||||
}));
|
||||
}
|
||||
switch (body.length) {
|
||||
case 0:
|
||||
case 0:
|
||||
return in_list ? MAP.skip : make_node(AST_EmptyStatement, node);
|
||||
case 1:
|
||||
case 1:
|
||||
return body[0];
|
||||
default:
|
||||
default:
|
||||
return in_list ? MAP.splice(body) : make_node(AST_BlockStatement, node, {
|
||||
body: body
|
||||
});
|
||||
}
|
||||
}
|
||||
if (drop_vars) {
|
||||
var def = assign_as_unused(node);
|
||||
if (def instanceof AST_SymbolRef
|
||||
&& !((def = def.definition()).id in in_use_ids)
|
||||
&& self.variables.get(def.name) === def) {
|
||||
var sym = assign_as_unused(node);
|
||||
if (sym instanceof AST_SymbolRef
|
||||
&& !(sym.definition().id in in_use_ids)) {
|
||||
if (node instanceof AST_Assign) {
|
||||
return maintain_this_binding(tt.parent(), node, node.right.transform(tt));
|
||||
return maintain_this_binding(parent, node, node.right.transform(tt));
|
||||
}
|
||||
return make_node(AST_Number, node, {
|
||||
value: 0
|
||||
@@ -2469,6 +2559,14 @@ merge(Compressor.prototype, {
|
||||
col : sym.start.col
|
||||
};
|
||||
}
|
||||
|
||||
function drop_decl(def, decl) {
|
||||
remove(def.orig, decl);
|
||||
if (!def.orig.length) {
|
||||
def.scope.functions.del(def.name);
|
||||
def.scope.variables.del(def.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
self.transform(tt);
|
||||
@@ -2604,6 +2702,71 @@ merge(Compressor.prototype, {
|
||||
return self;
|
||||
});
|
||||
|
||||
AST_Scope.DEFMETHOD("hoist_properties", function(compressor){
|
||||
var self = this;
|
||||
if (!compressor.option("hoist_props") || compressor.has_directive("use asm")) return self;
|
||||
var defs_by_id = Object.create(null);
|
||||
var var_names = Object.create(null);
|
||||
self.enclosed.forEach(function(def) {
|
||||
var_names[def.name] = true;
|
||||
});
|
||||
self.variables.each(function(def, name) {
|
||||
var_names[name] = true;
|
||||
});
|
||||
return self.transform(new TreeTransformer(function(node) {
|
||||
if (node instanceof AST_VarDef) {
|
||||
var sym = node.name, def, value;
|
||||
if (sym.scope === self
|
||||
&& !(def = sym.definition()).escaped
|
||||
&& !def.single_use
|
||||
&& !def.direct_access
|
||||
&& (value = sym.fixed_value()) === node.value
|
||||
&& value instanceof AST_Object) {
|
||||
var defs = new Dictionary();
|
||||
var assignments = [];
|
||||
value.properties.forEach(function(prop) {
|
||||
assignments.push(make_node(AST_VarDef, node, {
|
||||
name: make_sym(prop.key),
|
||||
value: prop.value
|
||||
}));
|
||||
});
|
||||
defs_by_id[def.id] = defs;
|
||||
return MAP.splice(assignments);
|
||||
}
|
||||
}
|
||||
if (node instanceof AST_PropAccess && node.expression instanceof AST_SymbolRef) {
|
||||
var defs = defs_by_id[node.expression.definition().id];
|
||||
if (defs) {
|
||||
var key = node.property;
|
||||
if (key instanceof AST_Node) key = key.getValue();
|
||||
var def = defs.get(key);
|
||||
var sym = make_node(AST_SymbolRef, node, {
|
||||
name: def.name,
|
||||
scope: node.expression.scope,
|
||||
thedef: def
|
||||
});
|
||||
sym.reference({});
|
||||
return sym;
|
||||
}
|
||||
}
|
||||
|
||||
function make_sym(key) {
|
||||
var prefix = sym.name + "_" + key.toString().replace(/[^a-z_$]+/ig, "_");
|
||||
var name = prefix;
|
||||
for (var i = 0; var_names[name]; i++) name = prefix + "$" + i;
|
||||
var new_var = make_node(sym.CTOR, sym, {
|
||||
name: name,
|
||||
scope: self
|
||||
});
|
||||
var def = self.def_variable(new_var);
|
||||
defs.set(key, def);
|
||||
self.enclosed.push(def);
|
||||
var_names[name] = true;
|
||||
return new_var;
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
// drop_side_effect_free()
|
||||
// remove side-effect-free parts which only affects return value
|
||||
(function(def){
|
||||
@@ -2651,14 +2814,12 @@ merge(Compressor.prototype, {
|
||||
def(AST_Binary, function(compressor, first_in_statement){
|
||||
var right = this.right.drop_side_effect_free(compressor);
|
||||
if (!right) return this.left.drop_side_effect_free(compressor, first_in_statement);
|
||||
switch (this.operator) {
|
||||
case "&&":
|
||||
case "||":
|
||||
if (lazy_op(this.operator)) {
|
||||
if (right === this.right) return this;
|
||||
var node = this.clone();
|
||||
node.right = right;
|
||||
return node;
|
||||
default:
|
||||
} else {
|
||||
var left = this.left.drop_side_effect_free(compressor, first_in_statement);
|
||||
if (!left) return this.right.drop_side_effect_free(compressor, first_in_statement);
|
||||
return make_sequence(this, [ left, right ]);
|
||||
@@ -3524,7 +3685,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
if (cdr instanceof AST_Binary && !(cdr instanceof AST_Assign)) {
|
||||
if (cdr.left.is_constant()) {
|
||||
if (cdr.operator == "||" || cdr.operator == "&&") {
|
||||
if (lazy_op(cdr.operator)) {
|
||||
expressions[++i] = expressions[j];
|
||||
break;
|
||||
}
|
||||
@@ -3680,6 +3841,11 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
|
||||
var commutativeOperators = makePredicate("== === != !== * & | ^");
|
||||
function is_object(node) {
|
||||
return node instanceof AST_Array
|
||||
|| node instanceof AST_Lambda
|
||||
|| node instanceof AST_Object;
|
||||
}
|
||||
|
||||
OPT(AST_Binary, function(self, compressor){
|
||||
function reversible() {
|
||||
@@ -3715,7 +3881,8 @@ merge(Compressor.prototype, {
|
||||
case "!==":
|
||||
if ((self.left.is_string(compressor) && self.right.is_string(compressor)) ||
|
||||
(self.left.is_number(compressor) && self.right.is_number(compressor)) ||
|
||||
(self.left.is_boolean() && self.right.is_boolean())) {
|
||||
(self.left.is_boolean() && self.right.is_boolean()) ||
|
||||
self.left.equivalent_to(self.right)) {
|
||||
self.operator = self.operator.substr(0, 2);
|
||||
}
|
||||
// XXX: intentionally falling down to the next case
|
||||
@@ -3735,6 +3902,13 @@ merge(Compressor.prototype, {
|
||||
if (self.operator.length == 2) self.operator += "=";
|
||||
}
|
||||
}
|
||||
// obj !== obj => false
|
||||
else if (self.left instanceof AST_SymbolRef
|
||||
&& self.right instanceof AST_SymbolRef
|
||||
&& self.left.definition() === self.right.definition()
|
||||
&& is_object(self.left.fixed_value())) {
|
||||
return make_node(self.operator[0] == "=" ? AST_True : AST_False, self);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (compressor.option("booleans") && self.operator == "+" && compressor.in_boolean_context()) {
|
||||
@@ -4024,8 +4198,7 @@ merge(Compressor.prototype, {
|
||||
// "x" + (y + "z")==> "x" + y + "z"
|
||||
if (self.right instanceof AST_Binary
|
||||
&& self.right.operator == self.operator
|
||||
&& (self.operator == "&&"
|
||||
|| self.operator == "||"
|
||||
&& (lazy_op(self.operator)
|
||||
|| (self.operator == "+"
|
||||
&& (self.right.left.is_string(compressor)
|
||||
|| (self.left.is_string(compressor)
|
||||
@@ -4073,46 +4246,55 @@ merge(Compressor.prototype, {
|
||||
d.fixed = fixed = make_node(AST_Function, fixed, fixed);
|
||||
}
|
||||
if (compressor.option("unused")
|
||||
&& fixed instanceof AST_Function
|
||||
&& fixed
|
||||
&& d.references.length == 1
|
||||
&& !(d.scope.uses_arguments && d.orig[0] instanceof AST_SymbolFunarg)
|
||||
&& !d.scope.uses_eval
|
||||
&& compressor.find_parent(AST_Scope) === fixed.parent_scope) {
|
||||
return fixed.clone(true);
|
||||
&& d.single_use) {
|
||||
var value = fixed.optimize(compressor);
|
||||
return value === fixed ? fixed.clone(true) : value;
|
||||
}
|
||||
if (compressor.option("evaluate") && fixed) {
|
||||
if (d.should_replace === undefined) {
|
||||
var init = fixed.evaluate(compressor);
|
||||
if (init !== fixed && (compressor.option("unsafe_regexp") || !(init instanceof RegExp))) {
|
||||
init = make_node_from_constant(init, fixed);
|
||||
var value_length = init.optimize(compressor).print_to_string().length;
|
||||
var fn;
|
||||
if (has_symbol_ref(fixed)) {
|
||||
fn = function() {
|
||||
var result = init.optimize(compressor);
|
||||
return result === init ? result.clone(true) : result;
|
||||
};
|
||||
} else {
|
||||
value_length = Math.min(value_length, fixed.print_to_string().length);
|
||||
fn = function() {
|
||||
var result = best_of_expression(init.optimize(compressor), fixed);
|
||||
return result === init || result === fixed ? result.clone(true) : result;
|
||||
};
|
||||
}
|
||||
var name_length = d.name.length;
|
||||
var overhead = 0;
|
||||
if (compressor.option("unused") && !compressor.exposed(d)) {
|
||||
overhead = (name_length + 2 + value_length) / d.references.length;
|
||||
}
|
||||
d.should_replace = value_length <= name_length + overhead ? fn : false;
|
||||
} else {
|
||||
d.should_replace = false;
|
||||
if (fixed && d.should_replace === undefined) {
|
||||
var init;
|
||||
if (fixed instanceof AST_This) {
|
||||
if (!(d.orig[0] instanceof AST_SymbolFunarg)
|
||||
&& all(d.references, function(ref) {
|
||||
return d.scope === ref.scope;
|
||||
})) {
|
||||
init = fixed;
|
||||
}
|
||||
} else {
|
||||
var ev = fixed.evaluate(compressor);
|
||||
if (ev !== fixed && (compressor.option("unsafe_regexp") || !(ev instanceof RegExp))) {
|
||||
init = make_node_from_constant(ev, fixed);
|
||||
}
|
||||
}
|
||||
if (d.should_replace) {
|
||||
return d.should_replace();
|
||||
if (init) {
|
||||
var value_length = init.optimize(compressor).print_to_string().length;
|
||||
var fn;
|
||||
if (has_symbol_ref(fixed)) {
|
||||
fn = function() {
|
||||
var result = init.optimize(compressor);
|
||||
return result === init ? result.clone(true) : result;
|
||||
};
|
||||
} else {
|
||||
value_length = Math.min(value_length, fixed.print_to_string().length);
|
||||
fn = function() {
|
||||
var result = best_of_expression(init.optimize(compressor), fixed);
|
||||
return result === init || result === fixed ? result.clone(true) : result;
|
||||
};
|
||||
}
|
||||
var name_length = d.name.length;
|
||||
var overhead = 0;
|
||||
if (compressor.option("unused") && !compressor.exposed(d)) {
|
||||
overhead = (name_length + 2 + value_length) / d.references.length;
|
||||
}
|
||||
d.should_replace = value_length <= name_length + overhead ? fn : false;
|
||||
} else {
|
||||
d.should_replace = false;
|
||||
}
|
||||
}
|
||||
if (d.should_replace) {
|
||||
return d.should_replace();
|
||||
}
|
||||
}
|
||||
return self;
|
||||
|
||||
@@ -4406,19 +4588,69 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
|
||||
OPT(AST_Sub, function(self, compressor){
|
||||
var expr = self.expression;
|
||||
var prop = self.property;
|
||||
if (prop instanceof AST_String && compressor.option("properties")) {
|
||||
prop = prop.getValue();
|
||||
if (is_identifier_string(prop)) {
|
||||
return make_node(AST_Dot, self, {
|
||||
expression : self.expression,
|
||||
property : prop
|
||||
}).optimize(compressor);
|
||||
if (compressor.option("properties")) {
|
||||
var key = prop.evaluate(compressor);
|
||||
if (key !== prop) {
|
||||
var property = "" + key;
|
||||
if (is_identifier_string(property)
|
||||
&& property.length <= prop.print_to_string().length + 1) {
|
||||
return make_node(AST_Dot, self, {
|
||||
expression: expr,
|
||||
property: property
|
||||
}).optimize(compressor);
|
||||
}
|
||||
if (!(prop instanceof AST_Number)) {
|
||||
var value = parseFloat(property);
|
||||
if (value.toString() == property) {
|
||||
prop = self.property = make_node(AST_Number, prop, {
|
||||
value: value
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
var v = parseFloat(prop);
|
||||
if (!isNaN(v) && v.toString() == prop) {
|
||||
self.property = make_node(AST_Number, self.property, {
|
||||
value: v
|
||||
}
|
||||
if (is_lhs(self, compressor.parent())) return self;
|
||||
if (key !== prop) {
|
||||
var sub = self.flatten_object(property, compressor);
|
||||
if (sub) {
|
||||
expr = self.expression = sub.expression;
|
||||
prop = self.property = sub.property;
|
||||
}
|
||||
}
|
||||
if (compressor.option("properties") && compressor.option("side_effects")
|
||||
&& prop instanceof AST_Number && expr instanceof AST_Array) {
|
||||
var index = prop.getValue();
|
||||
var elements = expr.elements;
|
||||
if (index in elements) {
|
||||
var flatten = true;
|
||||
var values = [];
|
||||
for (var i = elements.length; --i > index;) {
|
||||
var value = elements[i].drop_side_effect_free(compressor);
|
||||
if (value) {
|
||||
values.unshift(value);
|
||||
if (flatten && value.has_side_effects(compressor)) flatten = false;
|
||||
}
|
||||
}
|
||||
var retValue = elements[index];
|
||||
retValue = retValue instanceof AST_Hole ? make_node(AST_Undefined, retValue) : retValue;
|
||||
if (!flatten) values.unshift(retValue);
|
||||
while (--i >= 0) {
|
||||
var value = elements[i].drop_side_effect_free(compressor);
|
||||
if (value) values.unshift(value);
|
||||
else index--;
|
||||
}
|
||||
if (flatten) {
|
||||
values.push(retValue);
|
||||
return make_sequence(self, values).optimize(compressor);
|
||||
} else return make_node(AST_Sub, self, {
|
||||
expression: make_node(AST_Array, expr, {
|
||||
elements: values
|
||||
}),
|
||||
property: make_node(AST_Number, prop, {
|
||||
value: index
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -4430,7 +4662,8 @@ merge(Compressor.prototype, {
|
||||
return self;
|
||||
});
|
||||
|
||||
AST_Lambda.DEFMETHOD("contains_this", function() {
|
||||
AST_Lambda.DEFMETHOD("contains_this", function(grandparent) {
|
||||
if (grandparent instanceof AST_New) return false;
|
||||
var result;
|
||||
var self = this;
|
||||
self.walk(new TreeWalker(function(node) {
|
||||
@@ -4441,25 +4674,40 @@ merge(Compressor.prototype, {
|
||||
return result;
|
||||
});
|
||||
|
||||
AST_PropAccess.DEFMETHOD("flatten_object", function(key, compressor) {
|
||||
if (!compressor.option("properties")) return;
|
||||
var expr = this.expression;
|
||||
if (expr instanceof AST_Object) {
|
||||
var props = expr.properties;
|
||||
for (var i = props.length; --i >= 0;) {
|
||||
var prop = props[i];
|
||||
if ("" + prop.key == key) {
|
||||
if (!all(props, function(prop) {
|
||||
return prop instanceof AST_ObjectKeyVal;
|
||||
})) break;
|
||||
var value = prop.value;
|
||||
if (value instanceof AST_Function
|
||||
&& value.contains_this(compressor.parent())) break;
|
||||
return make_node(AST_Sub, this, {
|
||||
expression: make_node(AST_Array, expr, {
|
||||
elements: props.map(function(prop) {
|
||||
return prop.value;
|
||||
})
|
||||
}),
|
||||
property: make_node(AST_Number, this, {
|
||||
value: i
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
OPT(AST_Dot, function(self, compressor){
|
||||
var def = self.resolve_defines(compressor);
|
||||
if (def) {
|
||||
return def.optimize(compressor);
|
||||
}
|
||||
if (compressor.option("unsafe") && self.expression instanceof AST_Object) {
|
||||
var values = self.expression.properties;
|
||||
for (var i = values.length; --i >= 0;) {
|
||||
if (values[i].key === self.property) {
|
||||
var value = values[i].value;
|
||||
if (value instanceof AST_Function ? !value.contains_this() : !value.has_side_effects(compressor)) {
|
||||
var obj = self.expression.clone();
|
||||
obj.properties = obj.properties.slice();
|
||||
obj.properties.splice(i, 1);
|
||||
return make_sequence(self, [ obj, value ]).optimize(compressor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (compressor.option("unsafe_proto")
|
||||
&& self.expression instanceof AST_Dot
|
||||
&& self.expression.property == "prototype") {
|
||||
@@ -4482,6 +4730,9 @@ merge(Compressor.prototype, {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (is_lhs(self, compressor.parent())) return self;
|
||||
var sub = self.flatten_object(self.property, compressor);
|
||||
if (sub) return sub.optimize(compressor);
|
||||
var ev = self.evaluate(compressor);
|
||||
if (ev !== self) {
|
||||
ev = make_node_from_constant(ev, self).optimize(compressor);
|
||||
|
||||
@@ -482,13 +482,17 @@ function OutputStream(options) {
|
||||
nodetype.DEFMETHOD("_codegen", generator);
|
||||
};
|
||||
|
||||
var use_asm = false;
|
||||
var in_directive = false;
|
||||
var active_scope = null;
|
||||
var use_asm = null;
|
||||
|
||||
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" && stream.parent() instanceof AST_Scope) {
|
||||
use_asm = true;
|
||||
var self = this, generator = self._codegen;
|
||||
if (self instanceof AST_Scope) {
|
||||
active_scope = self;
|
||||
}
|
||||
else if (!use_asm && self instanceof AST_Directive && self.value == "use asm") {
|
||||
use_asm = active_scope;
|
||||
}
|
||||
function doit() {
|
||||
self.add_comments(stream);
|
||||
@@ -502,8 +506,8 @@ function OutputStream(options) {
|
||||
doit();
|
||||
}
|
||||
stream.pop_node();
|
||||
if (self instanceof AST_Scope) {
|
||||
use_asm = prev_use_asm;
|
||||
if (self === use_asm) {
|
||||
use_asm = null;
|
||||
}
|
||||
});
|
||||
AST_Node.DEFMETHOD("_print", AST_Node.prototype.print);
|
||||
|
||||
@@ -1054,6 +1054,8 @@ function parse($TEXT, options) {
|
||||
var name = is("name") ? as_symbol(in_statement ? AST_SymbolDefun : AST_SymbolLambda) : null;
|
||||
if (in_statement && !name)
|
||||
unexpected();
|
||||
if (name && ctor !== AST_Accessor && !(name instanceof AST_SymbolDeclaration))
|
||||
unexpected(prev());
|
||||
expect("(");
|
||||
var argnames = [];
|
||||
for (var first = true; !is("punc", ")");) {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"homepage": "http://lisperator.net/uglifyjs",
|
||||
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
|
||||
"license": "BSD-2-Clause",
|
||||
"version": "3.1.1",
|
||||
"version": "3.1.7",
|
||||
"engines": {
|
||||
"node": ">=0.8.0"
|
||||
},
|
||||
@@ -30,7 +30,7 @@
|
||||
],
|
||||
"dependencies": {
|
||||
"commander": "~2.11.0",
|
||||
"source-map": "~0.5.1"
|
||||
"source-map": "~0.6.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"acorn": "~5.1.1",
|
||||
|
||||
@@ -128,50 +128,110 @@ constant_join_3: {
|
||||
|
||||
for_loop: {
|
||||
options = {
|
||||
unsafe : true,
|
||||
unused : true,
|
||||
evaluate : true,
|
||||
reduce_vars : true
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
unsafe: true,
|
||||
unused: true,
|
||||
};
|
||||
input: {
|
||||
function f0() {
|
||||
var a = [1, 2, 3];
|
||||
for (var i = 0; i < a.length; i++) {
|
||||
console.log(a[i]);
|
||||
}
|
||||
var b = 0;
|
||||
for (var i = 0; i < a.length; i++)
|
||||
b += a[i];
|
||||
return b;
|
||||
}
|
||||
|
||||
function f1() {
|
||||
var a = [1, 2, 3];
|
||||
for (var i = 0, len = a.length; i < len; i++) {
|
||||
console.log(a[i]);
|
||||
}
|
||||
var b = 0;
|
||||
for (var i = 0, len = a.length; i < len; i++)
|
||||
b += a[i];
|
||||
return b;
|
||||
}
|
||||
|
||||
function f2() {
|
||||
var a = [1, 2, 3];
|
||||
for (var i = 0; i < a.length; i++) {
|
||||
a[i]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
function f0() {
|
||||
var a = [1, 2, 3];
|
||||
for (var i = 0; i < 3; i++)
|
||||
console.log(a[i]);
|
||||
}
|
||||
|
||||
function f1() {
|
||||
var a = [1, 2, 3];
|
||||
for (var i = 0; i < 3; i++)
|
||||
console.log(a[i]);
|
||||
}
|
||||
|
||||
function f2() {
|
||||
var a = [1, 2, 3];
|
||||
for (var i = 0; i < a.length; i++)
|
||||
a[i]++;
|
||||
return a[2];
|
||||
}
|
||||
console.log(f0(), f1(), f2());
|
||||
}
|
||||
expect: {
|
||||
function f0() {
|
||||
var a = [1, 2, 3];
|
||||
var b = 0;
|
||||
for (var i = 0; i < 3; i++)
|
||||
b += a[i];
|
||||
return b;
|
||||
}
|
||||
function f1() {
|
||||
var a = [1, 2, 3];
|
||||
var b = 0;
|
||||
for (var i = 0; i < 3; i++)
|
||||
b += a[i];
|
||||
return b;
|
||||
}
|
||||
function f2() {
|
||||
var a = [1, 2, 3];
|
||||
for (var i = 0; i < a.length; i++)
|
||||
a[i]++;
|
||||
return a[2];
|
||||
}
|
||||
console.log(f0(), f1(), f2());
|
||||
}
|
||||
expect_stdout: "6 6 4"
|
||||
}
|
||||
|
||||
index: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unsafe: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = [ 1, 2 ];
|
||||
console.log(a[0], a[1]);
|
||||
}
|
||||
expect: {
|
||||
console.log(1, 2);
|
||||
}
|
||||
expect_stdout: "1 2"
|
||||
}
|
||||
|
||||
length: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unsafe: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = [ 1, 2 ];
|
||||
console.log(a.length);
|
||||
}
|
||||
expect: {
|
||||
console.log(2);
|
||||
}
|
||||
expect_stdout: "2"
|
||||
}
|
||||
|
||||
index_length: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unsafe: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = [ 1, 2 ];
|
||||
console.log(a[0], a.length);
|
||||
}
|
||||
expect: {
|
||||
console.log(1, 2);
|
||||
}
|
||||
expect_stdout: "1 2"
|
||||
}
|
||||
|
||||
@@ -104,3 +104,65 @@ asm_mixed: {
|
||||
}
|
||||
}
|
||||
|
||||
asm_toplevel: {
|
||||
options = {}
|
||||
input: {
|
||||
"use asm";
|
||||
0.0;
|
||||
function f() {
|
||||
0.0;
|
||||
(function(){
|
||||
0.0;
|
||||
});
|
||||
}
|
||||
0.0;
|
||||
}
|
||||
expect_exact: '"use asm";0.0;function f(){0.0;(function(){0.0})}0.0;'
|
||||
}
|
||||
|
||||
asm_function_expression: {
|
||||
options = {}
|
||||
input: {
|
||||
0.0;
|
||||
var a = function() {
|
||||
"use asm";
|
||||
0.0;
|
||||
}
|
||||
function f() {
|
||||
0.0;
|
||||
return function(){
|
||||
"use asm";
|
||||
0.0;
|
||||
}
|
||||
0.0;
|
||||
}
|
||||
0.0;
|
||||
}
|
||||
expect_exact: '0;var a=function(){"use asm";0.0};function f(){0;return function(){"use asm";0.0};0}0;'
|
||||
}
|
||||
|
||||
asm_nested_functions: {
|
||||
options = {}
|
||||
input: {
|
||||
0.0;
|
||||
function a() {
|
||||
"use asm";
|
||||
0.0;
|
||||
}
|
||||
0.0;
|
||||
function b() {
|
||||
0.0;
|
||||
function c(){
|
||||
"use asm";
|
||||
0.0;
|
||||
}
|
||||
0.0;
|
||||
function d(){
|
||||
0.0;
|
||||
}
|
||||
0.0;
|
||||
}
|
||||
0.0;
|
||||
}
|
||||
expect_exact: '0;function a(){"use asm";0.0}0;function b(){0;function c(){"use asm";0.0}0;function d(){0}0}0;'
|
||||
}
|
||||
|
||||
@@ -1268,22 +1268,21 @@ 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,
|
||||
cascade: true,
|
||||
collapse_vars: true,
|
||||
comparisons: true,
|
||||
conditionals: true,
|
||||
dead_code: true,
|
||||
evaluate: true,
|
||||
if_return: true,
|
||||
join_vars: true,
|
||||
cascade: true,
|
||||
side_effects: true,
|
||||
hoist_funs: true,
|
||||
keep_fargs: true,
|
||||
loops: false,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f1() {
|
||||
@@ -1292,12 +1291,12 @@ collapse_vars_regexp: {
|
||||
return [rx, k];
|
||||
}
|
||||
function f2() {
|
||||
var rx = /[abc123]+/;
|
||||
var rx = /ab*/g;
|
||||
return function(s) {
|
||||
return rx.exec(s);
|
||||
};
|
||||
}
|
||||
(function(){
|
||||
(function() {
|
||||
var result;
|
||||
var s = 'acdabcdeabbb';
|
||||
var rx = /ab*/g;
|
||||
@@ -1305,22 +1304,35 @@ collapse_vars_regexp: {
|
||||
console.log(result[0]);
|
||||
}
|
||||
})();
|
||||
(function() {
|
||||
var result;
|
||||
var s = 'acdabcdeabbb';
|
||||
var rx = f2();
|
||||
while (result = rx(s)) {
|
||||
console.log(result[0]);
|
||||
}
|
||||
})();
|
||||
}
|
||||
expect: {
|
||||
function f1() {
|
||||
return [/[A-Z]+/, 9];
|
||||
}
|
||||
function f2() {
|
||||
var rx = /[abc123]+/;
|
||||
var rx = /ab*/g;
|
||||
return function(s) {
|
||||
return rx.exec(s);
|
||||
};
|
||||
}
|
||||
(function(){
|
||||
(function() {
|
||||
var result, rx = /ab*/g;
|
||||
while (result = rx.exec("acdabcdeabbb"))
|
||||
console.log(result[0]);
|
||||
})();
|
||||
(function() {
|
||||
var result, rx = f2();
|
||||
while (result = rx("acdabcdeabbb"))
|
||||
console.log(result[0]);
|
||||
})();
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
@@ -1834,9 +1846,9 @@ issue_1858: {
|
||||
}
|
||||
expect: {
|
||||
console.log(function(x) {
|
||||
var a = {}, b = a.b = x;
|
||||
var a = {}, b = a.b = 1;
|
||||
return a.b + b;
|
||||
}(1));
|
||||
}());
|
||||
}
|
||||
expect_stdout: "2"
|
||||
}
|
||||
@@ -2039,7 +2051,7 @@ inner_lvalues: {
|
||||
console.log(null, a, b);
|
||||
}
|
||||
expect: {
|
||||
var a, b = 10;
|
||||
var b = 10;
|
||||
var a = (--b || a || 3).toString(), c = --b + -a;
|
||||
console.log(null, a, b);
|
||||
}
|
||||
@@ -2451,3 +2463,555 @@ issue_2313_2: {
|
||||
}
|
||||
expect_stdout: "0"
|
||||
}
|
||||
|
||||
issue_2319_1: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
console.log(function(a) {
|
||||
return a;
|
||||
}(!function() {
|
||||
return this;
|
||||
}()));
|
||||
}
|
||||
expect: {
|
||||
console.log(function(a) {
|
||||
return !function() {
|
||||
return this;
|
||||
}();
|
||||
}());
|
||||
}
|
||||
expect_stdout: "false"
|
||||
}
|
||||
|
||||
issue_2319_2: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
console.log(function(a) {
|
||||
"use strict";
|
||||
return a;
|
||||
}(!function() {
|
||||
return this;
|
||||
}()));
|
||||
}
|
||||
expect: {
|
||||
console.log(function(a) {
|
||||
"use strict";
|
||||
return a;
|
||||
}(!function() {
|
||||
return this;
|
||||
}()));
|
||||
}
|
||||
expect_stdout: "false"
|
||||
}
|
||||
|
||||
issue_2319_3: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
"use strict";
|
||||
console.log(function(a) {
|
||||
return a;
|
||||
}(!function() {
|
||||
return this;
|
||||
}()));
|
||||
}
|
||||
expect: {
|
||||
"use strict";
|
||||
console.log(function(a) {
|
||||
return !function() {
|
||||
return this;
|
||||
}();
|
||||
}());
|
||||
}
|
||||
expect_stdout: "true"
|
||||
}
|
||||
|
||||
issue_2365: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
pure_getters: true,
|
||||
}
|
||||
input: {
|
||||
console.log(function(a) {
|
||||
var b = a.f;
|
||||
a.f++;
|
||||
return b;
|
||||
}({ f: 1 }));
|
||||
console.log(function() {
|
||||
var a = { f: 1 }, b = a.f;
|
||||
a.f++;
|
||||
return b;
|
||||
}());
|
||||
console.log({
|
||||
f: 1,
|
||||
g: function() {
|
||||
var b = this.f;
|
||||
this.f++;
|
||||
return b;
|
||||
}
|
||||
}.g());
|
||||
}
|
||||
expect: {
|
||||
console.log(function(a) {
|
||||
var b = a.f;
|
||||
a.f++;
|
||||
return b;
|
||||
}({ f: 1 }));
|
||||
console.log(function() {
|
||||
var a = { f: 1 }, b = a.f;
|
||||
a.f++;
|
||||
return b;
|
||||
}());
|
||||
console.log({
|
||||
f: 1,
|
||||
g: function() {
|
||||
var b = this.f;
|
||||
this.f++;
|
||||
return b;
|
||||
}
|
||||
}.g());
|
||||
}
|
||||
expect_stdout: [
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
]
|
||||
}
|
||||
|
||||
issue_2364_1: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
pure_getters: true,
|
||||
}
|
||||
input: {
|
||||
function inc(obj) {
|
||||
return obj.count++;
|
||||
}
|
||||
function foo() {
|
||||
var first = arguments[0];
|
||||
var result = inc(first);
|
||||
return foo.amount = first.count, result;
|
||||
}
|
||||
var data = {
|
||||
count: 0,
|
||||
};
|
||||
var answer = foo(data);
|
||||
console.log(foo.amount, answer);
|
||||
}
|
||||
expect: {
|
||||
function inc(obj) {
|
||||
return obj.count++;
|
||||
}
|
||||
function foo() {
|
||||
var first = arguments[0];
|
||||
var result = inc(first);
|
||||
return foo.amount = first.count, result;
|
||||
}
|
||||
var data = {
|
||||
count: 0
|
||||
};
|
||||
var answer = foo(data);
|
||||
console.log(foo.amount, answer);
|
||||
}
|
||||
expect_stdout: "1 0"
|
||||
}
|
||||
|
||||
issue_2364_2: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
pure_getters: true,
|
||||
}
|
||||
input: {
|
||||
function callValidate() {
|
||||
var validate = compilation.validate;
|
||||
var result = validate.apply(null, arguments);
|
||||
return callValidate.errors = validate.errors, result;
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
function callValidate() {
|
||||
var validate = compilation.validate;
|
||||
var result = validate.apply(null, arguments);
|
||||
return callValidate.errors = validate.errors, result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
issue_2364_3: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
pure_getters: true,
|
||||
}
|
||||
input: {
|
||||
function inc(obj) {
|
||||
return obj.count++;
|
||||
}
|
||||
function foo(bar) {
|
||||
var result = inc(bar);
|
||||
return foo.amount = bar.count, result;
|
||||
}
|
||||
var data = {
|
||||
count: 0,
|
||||
};
|
||||
var answer = foo(data);
|
||||
console.log(foo.amount, answer);
|
||||
}
|
||||
expect: {
|
||||
function inc(obj) {
|
||||
return obj.count++;
|
||||
}
|
||||
function foo(bar) {
|
||||
var result = inc(bar);
|
||||
return foo.amount = bar.count, result;
|
||||
}
|
||||
var data = {
|
||||
count: 0,
|
||||
};
|
||||
var answer = foo(data);
|
||||
console.log(foo.amount, answer);
|
||||
}
|
||||
expect_stdout: "1 0"
|
||||
}
|
||||
|
||||
issue_2364_4: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
pure_getters: true,
|
||||
}
|
||||
input: {
|
||||
function inc(obj) {
|
||||
return obj.count++;
|
||||
}
|
||||
function foo(bar, baz) {
|
||||
var result = inc(bar);
|
||||
return foo.amount = baz.count, result;
|
||||
}
|
||||
var data = {
|
||||
count: 0,
|
||||
};
|
||||
var answer = foo(data, data);
|
||||
console.log(foo.amount, answer);
|
||||
}
|
||||
expect: {
|
||||
function inc(obj) {
|
||||
return obj.count++;
|
||||
}
|
||||
function foo(bar, baz) {
|
||||
var result = inc(bar);
|
||||
return foo.amount = baz.count, result;
|
||||
}
|
||||
var data = {
|
||||
count: 0,
|
||||
};
|
||||
var answer = foo(data, data);
|
||||
console.log(foo.amount, answer);
|
||||
}
|
||||
expect_stdout: "1 0"
|
||||
}
|
||||
|
||||
issue_2364_5: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
evaluate: true,
|
||||
pure_getters: true,
|
||||
properties: true,
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f0(o, a, h) {
|
||||
var b = 3 - a;
|
||||
var obj = o;
|
||||
var seven = 7;
|
||||
var prop = 'run';
|
||||
var t = obj[prop](b)[seven] = h;
|
||||
return t;
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
function f0(o, a, h) {
|
||||
return o.run(3 - a)[7] = h;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
issue_2364_6: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
pure_getters: true,
|
||||
}
|
||||
input: {
|
||||
function f(a, b) {
|
||||
var c = a.p;
|
||||
b.p = "FAIL";
|
||||
return c;
|
||||
}
|
||||
var o = {
|
||||
p: "PASS"
|
||||
}
|
||||
console.log(f(o, o));
|
||||
}
|
||||
expect: {
|
||||
function f(a, b) {
|
||||
var c = a.p;
|
||||
b.p = "FAIL";
|
||||
return c;
|
||||
}
|
||||
var o = {
|
||||
p: "PASS"
|
||||
}
|
||||
console.log(f(o, o));
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_2364_7: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
pure_getters: true,
|
||||
}
|
||||
input: {
|
||||
function f(a, b) {
|
||||
var c = a.p;
|
||||
b.f();
|
||||
return c;
|
||||
}
|
||||
var o = {
|
||||
p: "PASS",
|
||||
f: function() {
|
||||
this.p = "FAIL";
|
||||
}
|
||||
}
|
||||
console.log(f(o, o));
|
||||
}
|
||||
expect: {
|
||||
function f(a, b) {
|
||||
var c = a.p;
|
||||
b.f();
|
||||
return c;
|
||||
}
|
||||
var o = {
|
||||
p: "PASS",
|
||||
f: function() {
|
||||
this.p = "FAIL";
|
||||
}
|
||||
}
|
||||
console.log(f(o, o));
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_2364_8: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
pure_getters: true,
|
||||
}
|
||||
input: {
|
||||
function f(a, b, c) {
|
||||
var d = a[b.f = function() {
|
||||
return "PASS";
|
||||
}];
|
||||
return c.f(d);
|
||||
}
|
||||
var o = {
|
||||
f: function() {
|
||||
return "FAIL";
|
||||
}
|
||||
};
|
||||
console.log(f({}, o, o));
|
||||
}
|
||||
expect: {
|
||||
function f(a, b, c) {
|
||||
var d = a[b.f = function() {
|
||||
return "PASS";
|
||||
}];
|
||||
return c.f(d);
|
||||
}
|
||||
var o = {
|
||||
f: function() {
|
||||
return "FAIL";
|
||||
}
|
||||
};
|
||||
console.log(f({}, o, o));
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_2364_9: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
pure_getters: true,
|
||||
}
|
||||
input: {
|
||||
function f(a, b) {
|
||||
var d = a();
|
||||
return b.f(d);
|
||||
}
|
||||
var o = {
|
||||
f: function() {
|
||||
return "FAIL";
|
||||
}
|
||||
};
|
||||
console.log(f(function() {
|
||||
o.f = function() {
|
||||
return "PASS";
|
||||
};
|
||||
}, o));
|
||||
}
|
||||
expect: {
|
||||
function f(a, b) {
|
||||
var d = a();
|
||||
return b.f(d);
|
||||
}
|
||||
var o = {
|
||||
f: function() {
|
||||
return "FAIL";
|
||||
}
|
||||
};
|
||||
console.log(f(function() {
|
||||
o.f = function() {
|
||||
return "PASS";
|
||||
};
|
||||
}, o));
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
pure_getters_chain: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
pure_getters: true,
|
||||
}
|
||||
input: {
|
||||
function o(t, r) {
|
||||
var a = t[1], s = t[2], o = t[3], i = t[5];
|
||||
return a <= 23 && s <= 59 && o <= 59 && (!r || i);
|
||||
}
|
||||
console.log(o([ , 23, 59, 59, , 42], 1));
|
||||
}
|
||||
expect: {
|
||||
function o(t, r) {
|
||||
return t[1] <= 23 && t[2] <= 59 && t[3] <= 59 && (!r || t[5]);
|
||||
}
|
||||
console.log(o([ , 23, 59, 59, , 42], 1));
|
||||
}
|
||||
expect_stdout: "42"
|
||||
}
|
||||
|
||||
conditional_1: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
}
|
||||
input: {
|
||||
function f(a, b) {
|
||||
var c = "";
|
||||
var d = b ? ">" : "<";
|
||||
if (a) c += "=";
|
||||
return c += d;
|
||||
}
|
||||
console.log(f(0, 0), f(0, 1), f(1, 0), f(1, 1));
|
||||
}
|
||||
expect: {
|
||||
function f(a, b) {
|
||||
var c = "";
|
||||
if (a) c += "=";
|
||||
return c += b ? ">" : "<";
|
||||
}
|
||||
console.log(f(0, 0), f(0, 1), f(1, 0), f(1, 1));
|
||||
}
|
||||
expect_stdout: "< > =< =>"
|
||||
}
|
||||
|
||||
conditional_2: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
}
|
||||
input: {
|
||||
function f(a, b) {
|
||||
var c = a + 1, d = a + 2;
|
||||
return b ? c : d;
|
||||
}
|
||||
console.log(f(3, 0), f(4, 1));
|
||||
}
|
||||
expect: {
|
||||
function f(a, b) {
|
||||
return b ? a + 1 : a + 2;
|
||||
}
|
||||
console.log(f(3, 0), f(4, 1));
|
||||
}
|
||||
expect_stdout: "5 5"
|
||||
}
|
||||
|
||||
issue_2425_1: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 8;
|
||||
(function(b) {
|
||||
b.toString();
|
||||
})(--a, a |= 10);
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var a = 8;
|
||||
(function(b) {
|
||||
b.toString();
|
||||
})(--a, a |= 10);
|
||||
console.log(a);
|
||||
}
|
||||
expect_stdout: "15"
|
||||
}
|
||||
|
||||
issue_2425_2: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 8;
|
||||
(function(b, c) {
|
||||
b.toString();
|
||||
})(--a, a |= 10);
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var a = 8;
|
||||
(function(b, c) {
|
||||
b.toString();
|
||||
})(--a, a |= 10);
|
||||
console.log(a);
|
||||
}
|
||||
expect_stdout: "15"
|
||||
}
|
||||
|
||||
issue_2425_3: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 8;
|
||||
(function(b, b) {
|
||||
b.toString();
|
||||
})(--a, a |= 10);
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var a = 8;
|
||||
(function(b, b) {
|
||||
(a |= 10).toString();
|
||||
})(--a);
|
||||
console.log(a);
|
||||
}
|
||||
expect_stdout: "15"
|
||||
}
|
||||
|
||||
@@ -73,4 +73,41 @@ dont_change_in_or_instanceof_expressions: {
|
||||
1 instanceof 1;
|
||||
null instanceof null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self_comparison_1: {
|
||||
options = {
|
||||
comparisons: true,
|
||||
}
|
||||
input: {
|
||||
a === a;
|
||||
a !== b;
|
||||
b.c === a.c;
|
||||
b.c !== b.c;
|
||||
}
|
||||
expect: {
|
||||
a == a;
|
||||
a !== b;
|
||||
b.c === a.c;
|
||||
b.c != b.c;
|
||||
}
|
||||
}
|
||||
|
||||
self_comparison_2: {
|
||||
options = {
|
||||
comparisons: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
}
|
||||
input: {
|
||||
function f() {}
|
||||
var o = {};
|
||||
console.log(f != f, o === o);
|
||||
}
|
||||
expect: {
|
||||
function f() {}
|
||||
var o = {};
|
||||
console.log(false, true);
|
||||
}
|
||||
expect_stdout: "false true"
|
||||
}
|
||||
|
||||
@@ -751,12 +751,12 @@ issue_1583: {
|
||||
expect: {
|
||||
function m(t) {
|
||||
(function(e) {
|
||||
t = e();
|
||||
})(function() {
|
||||
return (function(a) {
|
||||
return a;
|
||||
})(function(a) {});
|
||||
});
|
||||
t = function() {
|
||||
return (function(a) {
|
||||
return function(a) {};
|
||||
})();
|
||||
}();
|
||||
})();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1109,11 +1109,11 @@ var_catch_toplevel: {
|
||||
}
|
||||
}
|
||||
|
||||
issue_2105: {
|
||||
issue_2105_1: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
inline: true,
|
||||
passes: 3,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
unused: true,
|
||||
@@ -1139,17 +1139,50 @@ issue_2105: {
|
||||
});
|
||||
}
|
||||
expect: {
|
||||
(function() {
|
||||
var quux = function() {
|
||||
({
|
||||
prop: function() {
|
||||
console.log;
|
||||
console.log("PASS");
|
||||
};
|
||||
return {
|
||||
prop: function() {
|
||||
console.log;
|
||||
quux();
|
||||
}
|
||||
}).prop();
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_2105_2: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
inline: true,
|
||||
passes: 2,
|
||||
properties: true,
|
||||
pure_getters: "strict",
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
unsafe: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
!function(factory) {
|
||||
factory();
|
||||
}( function() {
|
||||
return function(fn) {
|
||||
fn()().prop();
|
||||
}( function() {
|
||||
function bar() {
|
||||
var quux = function() {
|
||||
console.log("PASS");
|
||||
}, foo = function() {
|
||||
console.log;
|
||||
quux();
|
||||
};
|
||||
return { prop: foo };
|
||||
}
|
||||
};
|
||||
})().prop();
|
||||
return bar;
|
||||
} );
|
||||
});
|
||||
}
|
||||
expect: {
|
||||
console.log("PASS");
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
@@ -386,10 +386,11 @@ unsafe_object_accessor: {
|
||||
}
|
||||
}
|
||||
|
||||
unsafe_function: {
|
||||
prop_function: {
|
||||
options = {
|
||||
evaluate : true,
|
||||
unsafe : true
|
||||
evaluate: true,
|
||||
properties: true,
|
||||
side_effects: true,
|
||||
}
|
||||
input: {
|
||||
console.log(
|
||||
@@ -402,9 +403,9 @@ unsafe_function: {
|
||||
expect: {
|
||||
console.log(
|
||||
({a:{b:1},b:function(){}}) + 1,
|
||||
({b:function(){}}, {b:1}) + 1,
|
||||
({a:{b:1}}, function(){}) + 1,
|
||||
({b:function(){}}, {b:1}).b + 1
|
||||
({b:1}) + 1,
|
||||
function(){} + 1,
|
||||
2
|
||||
);
|
||||
}
|
||||
expect_stdout: true
|
||||
@@ -630,10 +631,11 @@ unsafe_string_bad_index: {
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
unsafe_prototype_function: {
|
||||
prototype_function: {
|
||||
options = {
|
||||
evaluate : true,
|
||||
unsafe : true
|
||||
evaluate: true,
|
||||
properties: true,
|
||||
side_effects: true,
|
||||
}
|
||||
input: {
|
||||
var a = ({valueOf: 0}) < 1;
|
||||
@@ -652,8 +654,8 @@ unsafe_prototype_function: {
|
||||
var d = ({toString: 0}) + "";
|
||||
var e = (({valueOf: 0}) + "")[2];
|
||||
var f = (({toString: 0}) + "")[2];
|
||||
var g = ({}, 0)();
|
||||
var h = ({}, 0)();
|
||||
var g = 0();
|
||||
var h = 0();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1021,6 +1023,7 @@ issue_1964_1: {
|
||||
input: {
|
||||
function f() {
|
||||
var long_variable_name = /\s/;
|
||||
console.log(long_variable_name.source);
|
||||
return "a b c".split(long_variable_name)[1];
|
||||
}
|
||||
console.log(f());
|
||||
@@ -1028,11 +1031,15 @@ issue_1964_1: {
|
||||
expect: {
|
||||
function f() {
|
||||
var long_variable_name = /\s/;
|
||||
console.log(long_variable_name.source);
|
||||
return "a b c".split(long_variable_name)[1];
|
||||
}
|
||||
console.log(f());
|
||||
}
|
||||
expect_stdout: "b"
|
||||
expect_stdout: [
|
||||
"\\s",
|
||||
"b",
|
||||
]
|
||||
}
|
||||
|
||||
issue_1964_2: {
|
||||
@@ -1045,17 +1052,22 @@ issue_1964_2: {
|
||||
input: {
|
||||
function f() {
|
||||
var long_variable_name = /\s/;
|
||||
console.log(long_variable_name.source);
|
||||
return "a b c".split(long_variable_name)[1];
|
||||
}
|
||||
console.log(f());
|
||||
}
|
||||
expect: {
|
||||
function f() {
|
||||
console.log(/\s/.source);
|
||||
return "a b c".split(/\s/)[1];
|
||||
}
|
||||
console.log(f());
|
||||
}
|
||||
expect_stdout: "b"
|
||||
expect_stdout: [
|
||||
"\\s",
|
||||
"b",
|
||||
]
|
||||
}
|
||||
|
||||
array_slice_index: {
|
||||
@@ -1185,3 +1197,40 @@ issue_2231_2: {
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
self_comparison_1: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unsafe: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var o = { n: NaN };
|
||||
console.log(o.n == o.n, o.n === o.n, o.n != o.n, o.n !== o.n, typeof o.n);
|
||||
}
|
||||
expect: {
|
||||
console.log(false, false, true, true, "number");
|
||||
}
|
||||
expect_stdout: "false false true true 'number'"
|
||||
}
|
||||
|
||||
self_comparison_2: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
hoist_props: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var o = { n: NaN };
|
||||
console.log(o.n == o.n, o.n === o.n, o.n != o.n, o.n !== o.n, typeof o.n);
|
||||
}
|
||||
expect: {
|
||||
console.log(false, false, true, true, "number");
|
||||
}
|
||||
expect_stdout: "false false true true 'number'"
|
||||
}
|
||||
|
||||
@@ -151,13 +151,13 @@ issue_1841_2: {
|
||||
|
||||
function_returning_constant_literal: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
unsafe: true,
|
||||
toplevel: true,
|
||||
evaluate: true,
|
||||
cascade: true,
|
||||
unused: true,
|
||||
inline: true,
|
||||
passes: 2,
|
||||
properties: true,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function greeter() {
|
||||
|
||||
399
test/compress/hoist_props.js
Normal file
399
test/compress/hoist_props.js
Normal file
@@ -0,0 +1,399 @@
|
||||
issue_2377_1: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
inline: true,
|
||||
hoist_props: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var obj = {
|
||||
foo: 1,
|
||||
bar: 2,
|
||||
square: function(x) {
|
||||
return x * x;
|
||||
},
|
||||
cube: function(x) {
|
||||
return x * x * x;
|
||||
},
|
||||
};
|
||||
console.log(obj.foo, obj.cube(3));
|
||||
}
|
||||
expect: {
|
||||
var obj_foo = 1, obj_cube = function(x) {
|
||||
return x * x * x;
|
||||
};
|
||||
console.log(obj_foo, obj_cube(3));
|
||||
}
|
||||
expect_stdout: "1 27"
|
||||
}
|
||||
|
||||
issue_2377_2: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
inline: true,
|
||||
hoist_props: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var obj = {
|
||||
foo: 1,
|
||||
bar: 2,
|
||||
square: function(x) {
|
||||
return x * x;
|
||||
},
|
||||
cube: function(x) {
|
||||
return x * x * x;
|
||||
},
|
||||
};
|
||||
console.log(obj.foo, obj.cube(3));
|
||||
}
|
||||
expect: {
|
||||
console.log(1, function(x) {
|
||||
return x * x * x;
|
||||
}(3));
|
||||
}
|
||||
expect_stdout: "1 27"
|
||||
}
|
||||
|
||||
issue_2377_3: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
inline: true,
|
||||
hoist_props: true,
|
||||
passes: 3,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var obj = {
|
||||
foo: 1,
|
||||
bar: 2,
|
||||
square: function(x) {
|
||||
return x * x;
|
||||
},
|
||||
cube: function(x) {
|
||||
return x * x * x;
|
||||
},
|
||||
};
|
||||
console.log(obj.foo, obj.cube(3));
|
||||
}
|
||||
expect: {
|
||||
console.log(1, 27);
|
||||
}
|
||||
expect_stdout: "1 27"
|
||||
}
|
||||
|
||||
direct_access_1: {
|
||||
options = {
|
||||
hoist_props: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 0;
|
||||
var obj = {
|
||||
a: 1,
|
||||
b: 2,
|
||||
};
|
||||
for (var k in obj) a++;
|
||||
console.log(a, obj.a);
|
||||
}
|
||||
expect: {
|
||||
var a = 0;
|
||||
var obj = {
|
||||
a: 1,
|
||||
b: 2,
|
||||
};
|
||||
for (var k in obj) a++;
|
||||
console.log(a, obj.a);
|
||||
}
|
||||
expect_stdout: "2 1"
|
||||
}
|
||||
|
||||
direct_access_2: {
|
||||
options = {
|
||||
hoist_props: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var o = { a: 1 };
|
||||
var f = function(k) {
|
||||
if (o[k]) return "PASS";
|
||||
};
|
||||
console.log(f("a"));
|
||||
}
|
||||
expect: {
|
||||
var o = { a: 1 };
|
||||
console.log(function(k) {
|
||||
if (o[k]) return "PASS";
|
||||
}("a"));
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
direct_access_3: {
|
||||
options = {
|
||||
hoist_props: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var o = { a: 1 };
|
||||
o.b;
|
||||
console.log(o.a);
|
||||
}
|
||||
expect: {
|
||||
var o = { a: 1 };
|
||||
o.b;
|
||||
console.log(o.a);
|
||||
}
|
||||
expect_stdout: "1"
|
||||
}
|
||||
|
||||
single_use: {
|
||||
options = {
|
||||
hoist_props: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var obj = {
|
||||
bar: function() {
|
||||
return 42;
|
||||
},
|
||||
};
|
||||
console.log(obj.bar());
|
||||
}
|
||||
expect: {
|
||||
console.log({
|
||||
bar: function() {
|
||||
return 42;
|
||||
},
|
||||
}.bar());
|
||||
}
|
||||
}
|
||||
|
||||
name_collision_1: {
|
||||
options = {
|
||||
hoist_props: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
}
|
||||
input: {
|
||||
var obj_foo = 1;
|
||||
var obj_bar = 2;
|
||||
function f() {
|
||||
var obj = {
|
||||
foo: 3,
|
||||
bar: 4,
|
||||
"b-r": 5,
|
||||
"b+r": 6,
|
||||
"b!r": 7,
|
||||
};
|
||||
console.log(obj_foo, obj.foo, obj.bar, obj["b-r"], obj["b+r"], obj["b!r"]);
|
||||
}
|
||||
f();
|
||||
}
|
||||
expect: {
|
||||
var obj_foo = 1;
|
||||
var obj_bar = 2;
|
||||
function f() {
|
||||
var obj_foo$0 = 3,
|
||||
obj_bar = 4,
|
||||
obj_b_r = 5,
|
||||
obj_b_r$0 = 6,
|
||||
obj_b_r$1 = 7;
|
||||
console.log(obj_foo, obj_foo$0, obj_bar, obj_b_r, obj_b_r$0, obj_b_r$1);
|
||||
}
|
||||
f();
|
||||
}
|
||||
expect_stdout: "1 3 4 5 6 7"
|
||||
}
|
||||
|
||||
name_collision_2: {
|
||||
options = {
|
||||
hoist_props: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
}
|
||||
input: {
|
||||
var o = {
|
||||
p: 1,
|
||||
0: function(x) {
|
||||
return x;
|
||||
},
|
||||
1: function(x) {
|
||||
return x + 1;
|
||||
}
|
||||
}, o__$0 = 2, o__$1 = 3;
|
||||
console.log(o.p === o.p, o[0](4), o[1](5), o__$0, o__$1);
|
||||
}
|
||||
expect: {
|
||||
var o_p = 1,
|
||||
o__ = function(x) {
|
||||
return x;
|
||||
},
|
||||
o__$2 = function(x) {
|
||||
return x + 1;
|
||||
},
|
||||
o__$0 = 2,
|
||||
o__$1 = 3;
|
||||
console.log(o_p === o_p, o__(4), o__$2(5), o__$0, o__$1);
|
||||
}
|
||||
expect_stdout: "true 4 6 2 3"
|
||||
}
|
||||
|
||||
name_collision_3: {
|
||||
options = {
|
||||
hoist_props: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
}
|
||||
input: {
|
||||
var o = {
|
||||
p: 1,
|
||||
0: function(x) {
|
||||
return x;
|
||||
},
|
||||
1: function(x) {
|
||||
return x + 1;
|
||||
}
|
||||
}, o__$0 = 2, o__$1 = 3;
|
||||
console.log(o.p === o.p, o[0](4), o[1](5));
|
||||
}
|
||||
expect: {
|
||||
var o_p = 1,
|
||||
o__ = function(x) {
|
||||
return x;
|
||||
},
|
||||
o__$2 = function(x) {
|
||||
return x + 1;
|
||||
},
|
||||
o__$0 = 2,
|
||||
o__$1 = 3;
|
||||
console.log(o_p === o_p, o__(4), o__$2(5));
|
||||
}
|
||||
expect_stdout: "true 4 6"
|
||||
}
|
||||
|
||||
contains_this_1: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
hoist_props: true,
|
||||
inline: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var o = {
|
||||
u: function() {
|
||||
return this === this;
|
||||
},
|
||||
p: 1
|
||||
};
|
||||
console.log(o.p, o.p);
|
||||
}
|
||||
expect: {
|
||||
console.log(1, 1);
|
||||
}
|
||||
expect_stdout: "1 1"
|
||||
}
|
||||
|
||||
contains_this_2: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
hoist_props: true,
|
||||
inline: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var o = {
|
||||
u: function() {
|
||||
return this === this;
|
||||
},
|
||||
p: 1
|
||||
};
|
||||
console.log(o.p, o.p, o.u);
|
||||
}
|
||||
expect: {
|
||||
console.log(1, 1, function() {
|
||||
return this === this;
|
||||
});
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
contains_this_3: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
hoist_props: true,
|
||||
inline: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var o = {
|
||||
u: function() {
|
||||
return this === this;
|
||||
},
|
||||
p: 1
|
||||
};
|
||||
console.log(o.p, o.p, o.u());
|
||||
}
|
||||
expect: {
|
||||
var o = {
|
||||
u: function() {
|
||||
return this === this;
|
||||
},
|
||||
p: 1
|
||||
};
|
||||
console.log(o.p, o.p, o.u());
|
||||
}
|
||||
expect_stdout: "1 1 true"
|
||||
}
|
||||
|
||||
new_this: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
hoist_props: true,
|
||||
inline: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var o = {
|
||||
a: 1,
|
||||
b: 2,
|
||||
f: function(a) {
|
||||
this.b = a;
|
||||
}
|
||||
};
|
||||
console.log(new o.f(o.a).b, o.b);
|
||||
}
|
||||
expect: {
|
||||
console.log(new function(a) {
|
||||
this.b = a;
|
||||
}(1).b, 2);
|
||||
}
|
||||
expect_stdout: "1 2"
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
keep_properties: {
|
||||
options = {
|
||||
properties: false
|
||||
};
|
||||
evaluate: true,
|
||||
properties: false,
|
||||
}
|
||||
input: {
|
||||
a["foo"] = "bar";
|
||||
}
|
||||
@@ -12,6 +13,7 @@ keep_properties: {
|
||||
|
||||
dot_properties: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
properties: true,
|
||||
}
|
||||
beautify = {
|
||||
@@ -37,6 +39,7 @@ dot_properties: {
|
||||
|
||||
dot_properties_es5: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
properties: true,
|
||||
}
|
||||
beautify = {
|
||||
@@ -61,8 +64,8 @@ dot_properties_es5: {
|
||||
sub_properties: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
properties: true
|
||||
};
|
||||
properties: true,
|
||||
}
|
||||
input: {
|
||||
a[0] = 0;
|
||||
a["0"] = 1;
|
||||
@@ -81,18 +84,18 @@ sub_properties: {
|
||||
a[3.14] = 3;
|
||||
a.if = 4;
|
||||
a["foo bar"] = 5;
|
||||
a[NaN] = 6;
|
||||
a[null] = 7;
|
||||
a.NaN = 6;
|
||||
a.null = 7;
|
||||
a[void 0] = 8;
|
||||
}
|
||||
}
|
||||
|
||||
evaluate_array_length: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
properties: true,
|
||||
unsafe: true,
|
||||
evaluate: true
|
||||
};
|
||||
}
|
||||
input: {
|
||||
a = [1, 2, 3].length;
|
||||
a = [1, 2, 3].join()["len" + "gth"];
|
||||
@@ -109,10 +112,10 @@ evaluate_array_length: {
|
||||
|
||||
evaluate_string_length: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
properties: true,
|
||||
unsafe: true,
|
||||
evaluate: true
|
||||
};
|
||||
}
|
||||
input: {
|
||||
a = "foo".length;
|
||||
a = ("foo" + "bar")["len" + "gth"];
|
||||
@@ -151,7 +154,8 @@ mangle_properties: {
|
||||
|
||||
mangle_unquoted_properties: {
|
||||
options = {
|
||||
properties: false
|
||||
evaluate: true,
|
||||
properties: false,
|
||||
}
|
||||
mangle = {
|
||||
properties: {
|
||||
@@ -249,7 +253,8 @@ mangle_debug_suffix: {
|
||||
|
||||
mangle_debug_suffix_keep_quoted: {
|
||||
options = {
|
||||
properties: false
|
||||
evaluate: true,
|
||||
properties: false,
|
||||
}
|
||||
mangle = {
|
||||
properties: {
|
||||
@@ -677,8 +682,8 @@ accessor_this: {
|
||||
issue_2208_1: {
|
||||
options = {
|
||||
inline: true,
|
||||
properties: true,
|
||||
side_effects: true,
|
||||
unsafe: true,
|
||||
}
|
||||
input: {
|
||||
console.log({
|
||||
@@ -696,8 +701,8 @@ issue_2208_1: {
|
||||
issue_2208_2: {
|
||||
options = {
|
||||
inline: true,
|
||||
properties: true,
|
||||
side_effects: true,
|
||||
unsafe: true,
|
||||
}
|
||||
input: {
|
||||
console.log({
|
||||
@@ -721,8 +726,8 @@ issue_2208_2: {
|
||||
issue_2208_3: {
|
||||
options = {
|
||||
inline: true,
|
||||
properties: true,
|
||||
side_effects: true,
|
||||
unsafe: true,
|
||||
}
|
||||
input: {
|
||||
a = 42;
|
||||
@@ -746,8 +751,8 @@ issue_2208_3: {
|
||||
issue_2208_4: {
|
||||
options = {
|
||||
inline: true,
|
||||
properties: true,
|
||||
side_effects: true,
|
||||
unsafe: true,
|
||||
}
|
||||
input: {
|
||||
function foo() {}
|
||||
@@ -770,8 +775,8 @@ issue_2208_4: {
|
||||
issue_2208_5: {
|
||||
options = {
|
||||
inline: true,
|
||||
properties: true,
|
||||
side_effects: true,
|
||||
unsafe: true,
|
||||
}
|
||||
input: {
|
||||
console.log({
|
||||
@@ -804,3 +809,219 @@ issue_2256: {
|
||||
g.keep = g.g;
|
||||
}
|
||||
}
|
||||
|
||||
lhs_prop_1: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
properties: true,
|
||||
}
|
||||
input: {
|
||||
console.log(++{
|
||||
a: 1
|
||||
}.a);
|
||||
}
|
||||
expect: {
|
||||
console.log(++{
|
||||
a: 1
|
||||
}.a);
|
||||
}
|
||||
expect_stdout: "2"
|
||||
}
|
||||
|
||||
lhs_prop_2: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
inline: true,
|
||||
properties: true,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
[1][0] = 42;
|
||||
(function(a) {
|
||||
a.b = "g";
|
||||
})("abc");
|
||||
(function(a) {
|
||||
a[2] = "g";
|
||||
})("def");
|
||||
(function(a) {
|
||||
a[""] = "g";
|
||||
})("ghi");
|
||||
}
|
||||
expect: {
|
||||
[1][0] = 42;
|
||||
"abc".b = "g";
|
||||
"def"[2] = "g";
|
||||
"ghi"[""] = "g";
|
||||
}
|
||||
}
|
||||
|
||||
literal_duplicate_key_side_effects: {
|
||||
options = {
|
||||
properties: true,
|
||||
side_effects: true,
|
||||
}
|
||||
input: {
|
||||
console.log({
|
||||
a: "FAIL",
|
||||
a: console.log ? "PASS" : "FAIL"
|
||||
}.a);
|
||||
}
|
||||
expect: {
|
||||
console.log(console.log ? "PASS" : "FAIL");
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
prop_side_effects_1: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
inline: true,
|
||||
properties: true,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var C = 1;
|
||||
console.log(C);
|
||||
var obj = {
|
||||
bar: function() {
|
||||
return C + C;
|
||||
}
|
||||
};
|
||||
console.log(obj.bar());
|
||||
}
|
||||
expect: {
|
||||
console.log(1);
|
||||
var obj = {
|
||||
bar: function() {
|
||||
return 2;
|
||||
}
|
||||
};
|
||||
console.log(obj.bar());
|
||||
}
|
||||
expect_stdout: [
|
||||
"1",
|
||||
"2",
|
||||
]
|
||||
}
|
||||
|
||||
prop_side_effects_2: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
inline: true,
|
||||
passes: 2,
|
||||
properties: true,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var C = 1;
|
||||
console.log(C);
|
||||
var obj = {
|
||||
"": function() {
|
||||
return C + C;
|
||||
}
|
||||
};
|
||||
console.log(obj[""]());
|
||||
}
|
||||
expect: {
|
||||
console.log(1);
|
||||
console.log(2);
|
||||
}
|
||||
expect_stdout: [
|
||||
"1",
|
||||
"2",
|
||||
]
|
||||
}
|
||||
|
||||
accessor_1: {
|
||||
options = {
|
||||
properties: true,
|
||||
}
|
||||
input: {
|
||||
console.log({
|
||||
a: "FAIL",
|
||||
get a() {
|
||||
return "PASS";
|
||||
}
|
||||
}.a);
|
||||
}
|
||||
expect: {
|
||||
console.log({
|
||||
a: "FAIL",
|
||||
get a() {
|
||||
return "PASS";
|
||||
}
|
||||
}.a);
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
node_version: ">=4"
|
||||
}
|
||||
|
||||
accessor_2: {
|
||||
options = {
|
||||
properties: true,
|
||||
}
|
||||
input: {
|
||||
console.log({
|
||||
get a() {
|
||||
return "PASS";
|
||||
},
|
||||
set a(v) {},
|
||||
a: "FAIL"
|
||||
}.a);
|
||||
}
|
||||
expect: {
|
||||
console.log({
|
||||
get a() {
|
||||
return "PASS";
|
||||
},
|
||||
set a(v) {},
|
||||
a: "FAIL"
|
||||
}.a);
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
array_hole: {
|
||||
options = {
|
||||
properties: true,
|
||||
side_effects: true,
|
||||
}
|
||||
input: {
|
||||
console.log(
|
||||
[ 1, 2, , 3][1],
|
||||
[ 1, 2, , 3][2],
|
||||
[ 1, 2, , 3][3]
|
||||
);
|
||||
}
|
||||
expect: {
|
||||
console.log(2, void 0, 3);
|
||||
}
|
||||
expect_stdout: "2 undefined 3"
|
||||
}
|
||||
|
||||
new_this: {
|
||||
options = {
|
||||
properties: true,
|
||||
side_effects: true,
|
||||
}
|
||||
input: {
|
||||
new {
|
||||
f: function(a) {
|
||||
this.a = a;
|
||||
}
|
||||
}.f(42);
|
||||
}
|
||||
expect: {
|
||||
new function(a) {
|
||||
this.a = a;
|
||||
}(42);
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -73,6 +73,12 @@ describe("minify", function() {
|
||||
assert.strictEqual(run_code(compressed), run_code(original));
|
||||
});
|
||||
|
||||
it("should not parse invalid use of reserved words", function() {
|
||||
assert.strictEqual(Uglify.minify("function enum(){}").error, undefined);
|
||||
assert.strictEqual(Uglify.minify("function static(){}").error, undefined);
|
||||
assert.strictEqual(Uglify.minify("function this(){}").error.message, "Unexpected token: name (this)");
|
||||
});
|
||||
|
||||
describe("keep_quoted_props", function() {
|
||||
it("Should preserve quotes in object literals", function() {
|
||||
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
|
||||
|
||||
@@ -162,6 +162,7 @@ var VALUES = [
|
||||
'"object"',
|
||||
'"number"',
|
||||
'"function"',
|
||||
'this',
|
||||
];
|
||||
|
||||
var BINARY_OPS_NO_COMMA = [
|
||||
@@ -349,10 +350,10 @@ function createParams() {
|
||||
return params.join(', ');
|
||||
}
|
||||
|
||||
function createArgs() {
|
||||
function createArgs(recurmax, stmtDepth, canThrow) {
|
||||
var args = [];
|
||||
for (var n = rng(4); --n >= 0;) {
|
||||
args.push(createValue());
|
||||
args.push(rng(2) ? createValue() : createExpression(recurmax - 1, COMMA_OK, stmtDepth, canThrow));
|
||||
}
|
||||
return args.join(', ');
|
||||
}
|
||||
@@ -390,9 +391,10 @@ function createFunction(recurmax, inGlobal, noDecl, canThrow, stmtDepth) {
|
||||
|
||||
VAR_NAMES.length = namesLenBefore;
|
||||
|
||||
if (noDecl) s = 'var ' + createVarName(MANDATORY) + ' = ' + s + '(' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ');';
|
||||
if (noDecl) s = 'var ' + createVarName(MANDATORY) + ' = ' + s;
|
||||
// avoid "function statements" (decl inside statements)
|
||||
else if (inGlobal || rng(10) > 0) s += 'var ' + createVarName(MANDATORY) + ' = ' + name + '(' + createArgs() + ');';
|
||||
else if (inGlobal || rng(10) > 0) s += 'var ' + createVarName(MANDATORY) + ' = ' + name;
|
||||
s += '(' + createArgs(recurmax, stmtDepth, canThrow) + ');';
|
||||
|
||||
return s;
|
||||
}
|
||||
@@ -626,6 +628,9 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
|
||||
case p++:
|
||||
case p++:
|
||||
return createValue();
|
||||
case p++:
|
||||
case p++:
|
||||
return getVarName();
|
||||
case p++:
|
||||
return createExpression(recurmax, COMMA_OK, stmtDepth, canThrow);
|
||||
case p++:
|
||||
|
||||
@@ -16,11 +16,9 @@
|
||||
{},
|
||||
{
|
||||
"compress": {
|
||||
"toplevel": true
|
||||
"hoist_props": true
|
||||
},
|
||||
"mangle": {
|
||||
"toplevel": true
|
||||
}
|
||||
"toplevel": true
|
||||
},
|
||||
{
|
||||
"compress": {
|
||||
|
||||
Reference in New Issue
Block a user