pax_global_header00006660000000000000000000000064135525263730014525gustar00rootroot0000000000000052 comment=6b4886c908c6415a397539701117cd493545d21b UglifyJS2-3.6.3/000077500000000000000000000000001355252637300133145ustar00rootroot00000000000000UglifyJS2-3.6.3/.gitattributes000066400000000000000000000000241355252637300162030ustar00rootroot00000000000000*.js text eol=lf UglifyJS2-3.6.3/.github/000077500000000000000000000000001355252637300146545ustar00rootroot00000000000000UglifyJS2-3.6.3/.github/ISSUE_TEMPLATE.md000066400000000000000000000012301355252637300173550ustar00rootroot00000000000000**Bug report or feature request?** **Uglify version (`uglifyjs -V`)** **JavaScript input** **The `uglifyjs` CLI command executed or `minify()` options used.** **JavaScript output or error produced.** UglifyJS2-3.6.3/.gitignore000066400000000000000000000000431355252637300153010ustar00rootroot00000000000000/node_modules/ /npm-debug.log tmp/ UglifyJS2-3.6.3/.travis.yml000066400000000000000000000025321355252637300154270ustar00rootroot00000000000000cache: directories: tmp language: shell matrix: fast_finish: true env: - NODE=0.10 TYPE=compress - NODE=0.10 TYPE=mocha - NODE=0.10 TYPE=release/benchmark - NODE=0.10 TYPE=release/jetstream - NODE=0.12 TYPE=compress - NODE=0.12 TYPE=mocha - NODE=0.12 TYPE=release/benchmark - NODE=0.12 TYPE=release/jetstream - NODE=4 TYPE=compress - NODE=4 TYPE=mocha - NODE=4 TYPE=release/benchmark - NODE=4 TYPE=release/jetstream - NODE=6 TYPE=compress - NODE=6 TYPE=mocha - NODE=6 TYPE=release/benchmark - NODE=6 TYPE=release/jetstream - NODE=8 TYPE=compress - NODE=8 TYPE=mocha - NODE=8 TYPE=release/benchmark - NODE=8 TYPE=release/jetstream - NODE=10 TYPE=compress - NODE=10 TYPE=mocha - NODE=10 TYPE=release/benchmark - NODE=10 TYPE=release/jetstream - NODE=latest TYPE=compress - NODE=latest TYPE=mocha - NODE=latest TYPE=release/benchmark - NODE=latest TYPE=release/jetstream before_install: - git clone --branch v1.5.2 --depth 1 https://github.com/jasongin/nvs.git ~/.nvs - . ~/.nvs/nvs.sh - nvs --version install: - nvs add node/$NODE - nvs use node/$NODE - node --version - npm --version --no-update-notifier - npm install --no-audit --no-optional --no-save --no-update-notifier script: - node test/$TYPE UglifyJS2-3.6.3/CONTRIBUTING.md000066400000000000000000000030201355252637300155400ustar00rootroot00000000000000Contributing ============ ## Documentation Every new feature and API change should be accompanied by a README additon. ## Testing All features and bugs should have tests that verify the fix. You can run all tests using `npm test`. The most common type of test are tests that verify input and output of the Uglify transforms. These tests exist in `test/compress`. New tests can be added either to an existing file or in a new file `issue-xxx.js`. Tests that cannot be expressed as a simple AST can be found in `test/mocha`. ## Code style - File encoding must be `UTF-8`. - `LF` is always used as a line ending. - Statements end with semicolons. - Indentation uses 4 spaces, switch `case` 2 spaces. - Identifiers use `snake_case`. - Strings use double quotes (`"`). - Use a trailing comma for multiline array and object literals to minimize diffs. - The Uglify code only uses ES5, even in the `harmony` branch. - Line length should be at most 80 cols, except when it is easier to read a longer line. - If both sides of a comparison are of the same type, `==` and `!=` are used. - Multiline conditions place `&&` and `||` first on the line. **Example feature** ```js OPT(AST_Debugger, function(self, compressor) { if (compressor.option("drop_debugger")) return make_node(AST_EmptyStatement, self); return self; }); ``` **Example test case** ```js drop_debugger: { options = { drop_debugger: true, } input: { debugger; if (foo) debugger; } expect: { if (foo); } } ``` UglifyJS2-3.6.3/LICENSE000066400000000000000000000025041355252637300143220ustar00rootroot00000000000000UglifyJS is released under the BSD license: Copyright 2012-2019 (c) Mihai Bazon Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. UglifyJS2-3.6.3/README.md000066400000000000000000001312431355252637300145770ustar00rootroot00000000000000UglifyJS 3 ========== UglifyJS is a JavaScript parser, minifier, compressor and beautifier toolkit. #### Note: - **`uglify-js@3` has a simplified [API](#api-reference) and [CLI](#command-line-usage) that is not backwards compatible with [`uglify-js@2`](https://github.com/mishoo/UglifyJS2/tree/v2.x)**. - **Documentation for UglifyJS `2.x` releases can be found [here](https://github.com/mishoo/UglifyJS2/tree/v2.x)**. - `uglify-js` only supports JavaScript (ECMAScript 5). - To minify ECMAScript 2015 or above, transpile using tools like [Babel](https://babeljs.io/). Install ------- First make sure you have installed the latest version of [node.js](http://nodejs.org/) (You may need to restart your computer after this step). From NPM for use as a command line app: npm install uglify-js -g From NPM for programmatic use: npm install uglify-js # Command line usage uglifyjs [input files] [options] UglifyJS can take multiple input files. It's recommended that you pass the input files first, then pass the options. UglifyJS will parse input files in sequence and apply any compression options. The files are parsed in the same global scope, that is, a reference from a file to some variable/function declared in another file will be matched properly. If no input file is specified, UglifyJS will read from STDIN. If you wish to pass your options before the input files, separate the two with a double dash to prevent input files being used as option arguments: uglifyjs --compress --mangle -- input.js ### Command line options ``` -h, --help Print usage information. `--help options` for details on available options. -V, --version Print version number. -p, --parse Specify parser options: `acorn` Use Acorn for parsing. `bare_returns` Allow return outside of functions. Useful when minifying CommonJS modules and Userscripts that may be anonymous function wrapped (IIFE) by the .user.js engine `caller`. `expression` Parse a single expression, rather than a program (for parsing JSON). `spidermonkey` Assume input files are SpiderMonkey AST format (as JSON). -c, --compress [options] Enable compressor/specify compressor options: `pure_funcs` List of functions that can be safely removed when their return values are not used. -m, --mangle [options] Mangle names/specify mangler options: `reserved` List of names that should not be mangled. --mangle-props [options] Mangle properties/specify mangler options: `builtins` Mangle property names that overlaps with standard JavaScript globals. `debug` Add debug prefix and suffix. `domprops` Mangle property names that overlaps with DOM properties. `keep_quoted` Only mangle unquoted properties. `regex` Only mangle matched property names. `reserved` List of names that should not be mangled. -b, --beautify [options] Beautify output/specify output options: `beautify` Enabled with `--beautify` by default. `preamble` Preamble to prepend to the output. You can use this to insert a comment, for example for licensing information. This will not be parsed, but the source map will adjust for its presence. `quote_style` Quote style: 0 - auto 1 - single 2 - double 3 - original `wrap_iife` Wrap IIFEs in parenthesis. Note: you may want to disable `negate_iife` under compressor options. -o, --output Output file path (default STDOUT). Specify `ast` or `spidermonkey` to write UglifyJS or SpiderMonkey AST as JSON to STDOUT respectively. --comments [filter] Preserve copyright comments in the output. By default this works like Google Closure, keeping JSDoc-style comments that contain "@license" or "@preserve". You can optionally pass one of the following arguments to this flag: - "all" to keep all comments - a valid JS RegExp like `/foo/` or `/^!/` to keep only matching comments. Note that currently not *all* comments can be kept when compression is on, because of dead code removal or cascading statements into sequences. --config-file Read `minify()` options from JSON file. -d, --define [=value] Global definitions. -e, --enclose [arg[:value]] Embed everything in a big function, with configurable argument(s) & value(s). --ie8 Support non-standard Internet Explorer 8. Equivalent to setting `ie8: true` in `minify()` for `compress`, `mangle` and `output` options. By default UglifyJS will not try to be IE-proof. --keep-fnames Do not mangle/drop function names. Useful for code relying on Function.prototype.name. --name-cache File to hold mangled name mappings. --self Build UglifyJS as a library (implies --wrap UglifyJS) --source-map [options] Enable source map/specify source map options: `base` Path to compute relative paths from input files. `content` Input source map, useful if you're compressing JS that was generated from some other original code. Specify "inline" if the source map is included within the sources. `filename` Filename and/or location of the output source (sets `file` attribute in source map). `includeSources` Pass this flag if you want to include the content of source files in the source map as sourcesContent property. `root` Path to the original source to be included in the source map. `url` If specified, path to the source map to append in `//# sourceMappingURL`. --timings Display operations run time on STDERR. --toplevel Compress and/or mangle variables in top level scope. --verbose Print diagnostic messages. --warn Print warning messages. --wrap Embed everything in a big function, making the “exports” and “global” variables available. You need to pass an argument to this option to specify the name that your module will take when included in, say, a browser. ``` Specify `--output` (`-o`) to declare the output file. Otherwise the output goes to STDOUT. ## CLI source map options UglifyJS can generate a source map file, which is highly useful for debugging your compressed JavaScript. To get a source map, pass `--source-map --output output.js` (source map will be written out to `output.js.map`). Additional options: - `--source-map "filename=''"` to specify the name of the source map. The value of `filename` is only used to set `file` attribute (see [the spec][sm-spec]) in source map file. - `--source-map "root=''"` to pass the URL where the original files can be found. - `--source-map "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: uglifyjs js/file1.js js/file2.js \ -o foo.min.js -c -m \ --source-map "root='http://foo.com/src',url='foo.min.js.map'" The above will compress and mangle `file1.js` and `file2.js`, will drop the output in `foo.min.js` and the source map in `foo.min.js.map`. The source mapping will refer to `http://foo.com/src/js/file1.js` and `http://foo.com/src/js/file2.js` (in fact it will list `http://foo.com/src` as the source map root, and the original files as `js/file1.js` and `js/file2.js`). ### Composed source map When you're compressing JS code that was output by a compiler such as CoffeeScript, mapping to the JS code won't be too helpful. Instead, you'd like to map back to the original code (i.e. CoffeeScript). UglifyJS has an option to take an input source map. Assuming you have a mapping from CoffeeScript → compiled JS, UglifyJS can generate a map from CoffeeScript → compressed JS by mapping every token in the compiled JS to its original location. To use this feature pass `--source-map "content='/path/to/input/source.map'"` or `--source-map "content=inline"` if the source map is included inline with the sources. ## CLI compress options You need to pass `--compress` (`-c`) to enable the compressor. Optionally you can pass a comma-separated list of [compress options](#compress-options). Options are in the form `foo=bar`, or just `foo` (the latter implies a boolean option that you want to set `true`; it's effectively a shortcut for `foo=true`). Example: uglifyjs file.js -c toplevel,sequences=false ## CLI mangle options To enable the mangler you need to pass `--mangle` (`-m`). The following (comma-separated) options are supported: - `toplevel` (default `false`) -- mangle names declared in the top level scope. - `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 comma-separated list of names. For example: uglifyjs ... -m reserved=['$','require','exports'] to prevent the `require`, `exports` and `$` names from being changed. ### CLI mangling property names (`--mangle-props`) **Note:** THIS WILL PROBABLY BREAK YOUR CODE. Mangling property names 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: ```javascript // example.js var x = { baz_: 0, foo_: 1, calc: function() { return this.foo_ + this.baz_; } }; x.bar_ = 2; x["baz_"] = 3; console.log(x.calc()); ``` Mangle all properties (except for JavaScript `builtins`): ```bash $ uglifyjs example.js -c -m --mangle-props ``` ```javascript var x={o:0,_:1,l:function(){return this._+this.o}};x.t=2,x.o=3,console.log(x.l()); ``` Mangle all properties except for `reserved` properties: ```bash $ uglifyjs example.js -c -m --mangle-props reserved=[foo_,bar_] ``` ```javascript var x={o:0,foo_:1,_:function(){return this.foo_+this.o}};x.bar_=2,x.o=3,console.log(x._()); ``` Mangle all properties matching a `regex`: ```bash $ uglifyjs example.js -c -m --mangle-props regex=/_$/ ``` ```javascript var x={o:0,_:1,calc:function(){return this._+this.o}};x.l=2,x.o=3,console.log(x.calc()); ``` Combining mangle properties options: ```bash $ uglifyjs example.js -c -m --mangle-props regex=/_$/,reserved=[bar_] ``` ```javascript var x={o:0,_:1,calc:function(){return this._+this.o}};x.bar_=2,x.o=3,console.log(x.calc()); ``` In order for this to be of any use, we avoid mangling standard JS names by default (`--mangle-props builtins` to override). A default exclusion file is provided in `tools/domprops.json` which should cover most standard JS and DOM properties defined in various browsers. Pass `--mangle-props domprops` to disable this feature. A regular expression can be used to define which property names should be mangled. For example, `--mangle-props regex=/^_/` will only mangle property names that start with an underscore. When you compress multiple files using this option, in order for them to work together in the end we need to ensure somehow that one property gets mangled to the same name in all of them. For this, pass `--name-cache filename.json` and UglifyJS will maintain these mappings in a file which can then be reused. It should be initially empty. Example: ```bash $ rm -f /tmp/cache.json # start fresh $ uglifyjs file1.js file2.js --mangle-props --name-cache /tmp/cache.json -o part1.js $ uglifyjs file3.js file4.js --mangle-props --name-cache /tmp/cache.json -o part2.js ``` Now, `part1.js` and `part2.js` will be consistent with each other in terms of mangled property names. Using the name cache is not necessary if you compress all your files in a single call to UglifyJS. ### Mangling unquoted names (`--mangle-props keep_quoted`) Using quoted property name (`o["foo"]`) reserves the property name (`foo`) so that it is not mangled throughout the entire script even when used in an unquoted style (`o.foo`). Example: ```javascript // stuff.js var o = { "foo": 1, bar: 3 }; o.foo += o.bar; console.log(o.foo); ``` ```bash $ uglifyjs stuff.js --mangle-props keep_quoted -c -m ``` ```javascript var o={foo:1,o:3};o.foo+=o.o,console.log(o.foo); ``` ### Debugging property name mangling You can also pass `--mangle-props debug` in order to mangle property names without completely obscuring them. For example the property `o.foo` would mangle to `o._$foo$_` with this option. This allows property mangling of a large codebase while still being able to debug the code and identify where mangling is breaking things. ```bash $ uglifyjs stuff.js --mangle-props debug -c -m ``` ```javascript var o={_$foo$_:1,_$bar$_:3};o._$foo$_+=o._$bar$_,console.log(o._$foo$_); ``` You can also pass a custom suffix using `--mangle-props debug=XYZ`. This would then mangle `o.foo` to `o._$foo$XYZ_`. You can change this each time you compile a script to identify how a property got mangled. One technique is to pass a random number on every compile to simulate mangling changing with different inputs (e.g. as you update the input script with new properties), and to help identify mistakes like writing mangled keys to storage. # API Reference Assuming installation via NPM, you can load UglifyJS in your application like this: ```javascript var UglifyJS = require("uglify-js"); ``` There is a single high level function, **`minify(code, options)`**, which will perform all minification [phases](#minify-options) in a configurable manner. By default `minify()` will enable the options [`compress`](#compress-options) and [`mangle`](#mangle-options). Example: ```javascript var code = "function add(first, second) { return first + second; }"; var result = UglifyJS.minify(code); console.log(result.error); // runtime error, or `undefined` if no error console.log(result.code); // minified output: function add(n,d){return n+d} ``` You can `minify` more than one JavaScript file at a time by using an object for the first argument where the keys are file names and the values are source code: ```javascript var code = { "file1.js": "function add(first, second) { return first + second; }", "file2.js": "console.log(add(1 + 2, 3 + 4));" }; var result = UglifyJS.minify(code); console.log(result.code); // function add(d,n){return d+n}console.log(add(3,7)); ``` The `toplevel` option: ```javascript var code = { "file1.js": "function add(first, second) { return first + second; }", "file2.js": "console.log(add(1 + 2, 3 + 4));" }; var options = { toplevel: true }; var result = UglifyJS.minify(code, options); console.log(result.code); // console.log(3+7); ``` The `nameCache` option: ```javascript var options = { mangle: { toplevel: true, }, nameCache: {} }; var result1 = UglifyJS.minify({ "file1.js": "function add(first, second) { return first + second; }" }, options); var result2 = UglifyJS.minify({ "file2.js": "console.log(add(1 + 2, 3 + 4));" }, options); console.log(result1.code); // function n(n,r){return n+r} console.log(result2.code); // console.log(n(3,7)); ``` You may persist the name cache to the file system in the following way: ```javascript var cacheFileName = "/tmp/cache.json"; var options = { mangle: { properties: true, }, nameCache: JSON.parse(fs.readFileSync(cacheFileName, "utf8")) }; fs.writeFileSync("part1.js", UglifyJS.minify({ "file1.js": fs.readFileSync("file1.js", "utf8"), "file2.js": fs.readFileSync("file2.js", "utf8") }, options).code, "utf8"); fs.writeFileSync("part2.js", UglifyJS.minify({ "file3.js": fs.readFileSync("file3.js", "utf8"), "file4.js": fs.readFileSync("file4.js", "utf8") }, options).code, "utf8"); fs.writeFileSync(cacheFileName, JSON.stringify(options.nameCache), "utf8"); ``` An example of a combination of `minify()` options: ```javascript var code = { "file1.js": "function add(first, second) { return first + second; }", "file2.js": "console.log(add(1 + 2, 3 + 4));" }; var options = { toplevel: true, compress: { global_defs: { "@console.log": "alert" }, passes: 2 }, output: { beautify: false, preamble: "/* uglified */" } }; var result = UglifyJS.minify(code, options); console.log(result.code); // /* uglified */ // alert(10);" ``` To produce warnings: ```javascript var code = "function f(){ var u; return 2 + 3; }"; var options = { warnings: true }; var result = UglifyJS.minify(code, options); console.log(result.error); // runtime error, `undefined` in this case console.log(result.warnings); // [ 'Dropping unused variable u [0:1,18]' ] console.log(result.code); // function f(){return 5} ``` An error example: ```javascript var result = UglifyJS.minify({"foo.js" : "if (0) else console.log(1);"}); console.log(JSON.stringify(result.error)); // {"message":"Unexpected token: keyword (else)","filename":"foo.js","line":1,"col":7,"pos":7} ``` Note: unlike `uglify-js@2.x`, the `3.x` API does not throw errors. To achieve a similar effect one could do the following: ```javascript var result = UglifyJS.minify(code, options); if (result.error) throw result.error; ``` ## Minify options - `warnings` (default `false`) — pass `true` to return compressor warnings in `result.warnings`. Use the value `"verbose"` for more detailed warnings. - `parse` (default `{}`) — pass an object if you wish to specify some additional [parse options](#parse-options). - `compress` (default `{}`) — pass `false` to skip compressing entirely. Pass an object to specify custom [compress options](#compress-options). - `mangle` (default `true`) — pass `false` to skip mangling names, or pass an object to specify [mangle options](#mangle-options) (see below). - `mangle.properties` (default `false`) — a subcategory of the mangle option. Pass an object to specify custom [mangle property options](#mangle-properties-options). - `output` (default `null`) — pass an object if you wish to specify additional [output options](#output-options). The defaults are optimized for best compression. - `sourceMap` (default `false`) - pass an object if you wish to specify [source map options](#source-map-options). - `toplevel` (default `false`) - set to `true` if you wish to enable top level variable and function name mangling and to drop unused variables and functions. - `nameCache` (default `null`) - pass an empty object `{}` or a previously used `nameCache` object if you wish to cache mangled variable and property names across multiple invocations of `minify()`. Note: this is a read/write property. `minify()` will read the name cache state of this object and update it during minification so that it may be reused or externally persisted by the user. - `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 { parse: { // parse options }, compress: { // compress options }, mangle: { // mangle options properties: { // mangle property options } }, output: { // output options }, sourceMap: { // source map options }, nameCache: null, // or specify a name cache object toplevel: false, ie8: false, warnings: false, } ``` ### Source map options To generate a source map: ```javascript var result = UglifyJS.minify({"file1.js": "var a = function() {};"}, { sourceMap: { filename: "out.js", url: "out.js.map" } }); console.log(result.code); // minified output console.log(result.map); // source map ``` Note that the source map is not saved in a file, it's just returned in `result.map`. The value passed for `sourceMap.url` is only used to set `//# sourceMappingURL=out.js.map` in `result.code`. The value of `filename` is only used to set `file` attribute (see [the spec][sm-spec]) in source map file. You can set option `sourceMap.url` to be `"inline"` and source map will be appended to code. You can also specify sourceRoot property to be included in source map: ```javascript var result = UglifyJS.minify({"file1.js": "var a = function() {};"}, { sourceMap: { root: "http://example.com/src", url: "out.js.map" } }); ``` If you're compressing compiled JavaScript and have a source map for it, you can use `sourceMap.content`: ```javascript var result = UglifyJS.minify({"compiled.js": "compiled code"}, { sourceMap: { content: "content from compiled.js.map", url: "minified.js.map" } }); // same as before, it returns `code` and `map` ``` If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.url`. ## 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 - `arguments` (default: `true`) -- replace `arguments[index]` with function parameter name whenever possible. - `assignments` (default: `true`) -- apply optimizations to assignment expressions. - `booleans` (default: `true`) -- various optimizations for boolean context, for example `!!a ? b : c → a ? b : c` - `collapse_vars` (default: `true`) -- Collapse single-use non-constant variables, side effects permitting. - `comparisons` (default: `true`) -- apply certain optimizations to binary nodes, e.g. `!(a <= b) → a > b`, attempts to negate binary nodes, e.g. `a = !b && !c && !d && !e → a=!(b||c||d||e)` etc. - `conditionals` (default: `true`) -- apply optimizations for `if`-s and conditional expressions - `dead_code` (default: `true`) -- remove unreachable code - `directives` (default: `true`) -- remove redundant or non-standard directives - `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. - `drop_debugger` (default: `true`) -- remove `debugger;` statements - `evaluate` (default: `true`) -- attempt to evaluate constant expressions - `expression` (default: `false`) -- Pass `true` to preserve completion values from terminal statements without `return`, e.g. in bookmarklets. - `functions` (default: `true`) -- convert declarations from `var`to `function` whenever possible. - `global_defs` (default: `{}`) -- see [conditional compilation](#conditional-compilation) - `hoist_funs` (default: `false`) -- hoist function declarations - `hoist_props` (default: `true`) -- 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` (default: `true`) -- optimizations for if/return and if/continue - `inline` (default: `true`) -- inline calls to function with simple/`return` statement: - `false` -- same as `0` - `0` -- disabled inlining - `1` -- inline simple functions - `2` -- inline functions with arguments - `3` -- inline functions with arguments and variables - `true` -- same as `3` - `join_vars` (default: `true`) -- join consecutive `var` statements - `keep_fargs` (default: `strict`) -- Discard unused function arguments. Code which relies on `Function.length` will break if this is done indiscriminately, i.e. when passing `true`. Pass `false` to always retain function arguments. - `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-options). - `keep_infinity` (default: `false`) -- Pass `true` to prevent `Infinity` from being compressed into `1/0`, which may cause performance issues on Chrome. - `loops` (default: `true`) -- optimizations for `do`, `while` and `for` loops when we can statically determine the condition. - `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. - `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. - `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 variable `q` is not used elsewhere, UglifyJS will drop it, but will still keep the `Math.floor(a/b)`, not knowing what it does. You can pass `pure_funcs: [ 'Math.floor' ]` to let it know that this function won't produce any side effect, in which case the whole statement would get discarded. The current implementation adds some overhead (compression will be slower). Make sure symbols under `pure_funcs` are also under `mangle.reserved` to avoid mangling. - `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`. - `reduce_funcs` (default: `true`) -- Allows single-use functions to be inlined as function expressions when permissible allowing further optimization. Enabled by default. Option depends on `reduce_vars` being enabled. Some code runs faster in the Chrome V8 engine if this option is disabled. Does not negatively impact other major browsers. - `reduce_vars` (default: `true`) -- Improve optimization on variables assigned with and used as constant values. - `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. - `side_effects` (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`) -- 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`) -- compress expressions like `a <= b` assuming none of the operands can be (coerced to) `NaN`. - `unsafe_Function` (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. - `unsafe_undefined` (default: `false`) -- substitute `void 0` if there is a variable named `undefined` in scope (variable name will be mangled, typically reduced to a single character) - `unused` (default: `true`) -- drop unreferenced functions and variables (simple direct variable assignments do not count as references unless set to `"keep_assign"`) ## 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. Examples: ```javascript // test.js var globalVar; function funcName(firstLongName, anotherLongName) { var myVariable = firstLongName + anotherLongName; } ``` ```javascript var code = fs.readFileSync("test.js", "utf8"); UglifyJS.minify(code).code; // 'function funcName(a,n){}var globalVar;' UglifyJS.minify(code, { mangle: { reserved: ['firstLongName'] } }).code; // 'function funcName(firstLongName,a){}var globalVar;' UglifyJS.minify(code, { mangle: { toplevel: true } }).code; // 'function n(n,a){}var a;' ``` ### Mangle properties options - `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 case you want beautified output, pass `--beautify` (`-b`). Optionally you 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. - `braces` (default `false`) -- always insert braces 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 - `inline_script` (default `true`) -- escape HTML comments and the slash in occurrences of `` 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 retain line numbering on a best effort basis. - `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 more double quotes in the string itself. `0` is best for gzip size. - `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) - `webkit` (default `false`) -- enable workarounds for WebKit bugs. PhantomJS users should set this option to `true`. - `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. # Miscellaneous ### Keeping copyright notices or other comments You can pass `--comments` to retain certain comments in the output. By default it will keep JSDoc-style comments that contain "@preserve", "@license" or "@cc_on" (conditional compilation for IE). You can pass `--comments all` to keep all the comments, or a valid JavaScript regexp to keep only comments that match this regexp. For example `--comments /^!/` will keep comments like `/*! Copyright Notice */`. Note, however, that there might be situations where comments are lost. For example: ```javascript function f() { /** @preserve Foo Bar */ function g() { // this function is never called } return something(); } ``` Even though it has "@preserve", the comment will be lost because the inner function `g` (which is the AST node to which the comment is attached to) is discarded by the compressor as not referenced. The safest comments where to place copyright information (or other info that needs to be kept in the output) are comments attached to toplevel nodes. ### The `unsafe` `compress` option It enables some transformations that *might* break code logic in certain contrived cases, but should be fine for most code. You might want to try it on your own code, it should reduce the minified size. Here's what happens when this flag is on: - `new Array(1, 2, 3)` or `Array(1, 2, 3)` → `[ 1, 2, 3 ]` - `new Object()` → `{}` - `String(exp)` or `exp.toString()` → `"" + exp` - `new Object/RegExp/Function/Error/Array (...)` → we discard the `new` ### Conditional compilation You can use the `--define` (`-d`) switch in order to declare global variables that UglifyJS will assume to be constants (unless defined in scope). For example if you pass `--define DEBUG=false` then, coupled with dead code removal UglifyJS will discard the following from the output: ```javascript if (DEBUG) { console.log("debug stuff"); } ``` You can specify nested constants in the form of `--define env.DEBUG=false`. UglifyJS will warn about the condition being always false and about dropping unreachable code; for now there is no option to turn off only this specific warning, you can pass `warnings=false` to turn off *all* warnings. Another way of doing that is to declare your globals as constants in a separate file and include it into the build. For example you can have a `build/defines.js` file with the following: ```javascript var DEBUG = false; var PRODUCTION = true; // etc. ``` and build your code like this: uglifyjs build/defines.js js/foo.js js/bar.js... -c UglifyJS will notice the constants and, since they cannot be altered, it will evaluate references to them to the value itself and drop unreachable code as usual. The build will contain the `const` declarations if you use them. If you are targeting < ES6 environments which does not support `const`, using `var` with `reduce_vars` (enabled by default) should suffice. ### Conditional compilation API You can also use conditional compilation via the programmatic API. With the difference that the property name is `global_defs` and is a compressor property: ```javascript var result = UglifyJS.minify(fs.readFileSync("input.js", "utf8"), { compress: { dead_code: true, global_defs: { DEBUG: false } } }); ``` To replace an identifier with an arbitrary non-constant expression it is necessary to prefix the `global_defs` key with `"@"` to instruct UglifyJS to parse the value as an expression: ```javascript UglifyJS.minify("alert('hello');", { compress: { global_defs: { "@alert": "console.log" } } }).code; // returns: 'console.log("hello");' ``` Otherwise it would be replaced as string literal: ```javascript UglifyJS.minify("alert('hello');", { compress: { global_defs: { "alert": "console.log" } } }).code; // returns: '"console.log"("hello");' ``` ### Using native Uglify AST with `minify()` ```javascript // example: parse only, produce native Uglify AST var result = UglifyJS.minify(code, { parse: {}, compress: false, mangle: false, output: { ast: true, code: false // optional - faster if false } }); // result.ast contains native Uglify AST ``` ```javascript // example: accept native Uglify AST input and then compress and mangle // to produce both code and native AST. var result = UglifyJS.minify(ast, { compress: {}, mangle: {}, output: { ast: true, code: true // optional - faster if false } }); // result.ast contains native Uglify AST // result.code contains the minified code in string form. ``` ### Working with Uglify AST Transversal and transformation of the native AST can be performed through [`TreeWalker`](https://github.com/mishoo/UglifyJS2/blob/master/lib/ast.js) and [`TreeTransformer`](https://github.com/mishoo/UglifyJS2/blob/master/lib/transform.js) respectively. ### ESTree / SpiderMonkey AST UglifyJS has its own abstract syntax tree format; for [practical reasons](http://lisperator.net/blog/uglifyjs-why-not-switching-to-spidermonkey-ast/) we can't easily change to using the SpiderMonkey AST internally. However, UglifyJS now has a converter which can import a SpiderMonkey AST. For example [Acorn][acorn] is a super-fast parser that produces a SpiderMonkey AST. It has a small CLI utility that parses one file and dumps the AST in JSON on the standard output. To use UglifyJS to mangle and compress that: acorn file.js | uglifyjs -p spidermonkey -m -c The `-p spidermonkey` option tells UglifyJS that all input files are not JavaScript, but JS code described in SpiderMonkey AST in JSON. Therefore we don't use our own parser in this case, but just transform that AST into our internal AST. ### Use Acorn for parsing More for fun, I added the `-p acorn` option which will use Acorn to do all the parsing. If you pass this option, UglifyJS will `require("acorn")`. Acorn is really fast (e.g. 250ms instead of 380ms on some 650K code), but converting the SpiderMonkey tree that Acorn produces takes another 150ms so in total it's a bit more than just using UglifyJS's own parser. [acorn]: https://github.com/ternjs/acorn [sm-spec]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k ### 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 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 [`butternut`](https://www.npmjs.com/package/butternut): | d3.js | minify size | gzip size | minify time (seconds) | | --- | ---: | ---: | ---: | | original | 451,131 | 108,733 | - | | uglify-js@3.0.24 mangle=false, compress=false | 316,600 | 85,245 | 0.70 | | uglify-js@3.0.24 mangle=true, compress=false | 220,216 | 72,730 | 1.13 | | butternut@0.4.6 | 217,568 | 72,738 | 1.41 | | uglify-js@3.0.24 mangle=true, compress=true | 212,511 | 71,560 | 3.36 | | babili@0.1.4 | 210,713 | 72,140 | 12.64 | To enable fast minify mode from the CLI use: ``` uglifyjs file.js -m ``` To enable fast minify mode with the API use: ```js UglifyJS.minify(code, { compress: false, mangle: true }); ``` #### Source maps and debugging Various `compress` transforms that simplify, rearrange, inline and remove code are known to have an adverse effect on debugging with source maps. This is expected as code is optimized and mappings are often simply not possible as some code no longer exists. For highest fidelity in source map debugging disable the Uglify `compress` option and just use `mangle`. ### Compiler assumptions To allow for better optimizations, the compiler makes various assumptions: - `.toString()` and `.valueOf()` don't have side effects, and for built-in objects they have not been overridden. - `undefined`, `NaN` and `Infinity` have not been externally redefined. - `arguments.callee`, `arguments.caller` and `Function.prototype.caller` are not used. - The code doesn't expect the contents of `Function.prototype.toString()` or `Error.prototype.stack` to be anything in particular. - Getting and setting properties on a plain object does not cause other side effects (using `.watch()` or `Proxy`). - Object properties can be added, removed and modified (not prevented with `Object.defineProperty()`, `Object.defineProperties()`, `Object.freeze()`, `Object.preventExtensions()` or `Object.seal()`). UglifyJS2-3.6.3/appveyor.yml000066400000000000000000000030571355252637300157110ustar00rootroot00000000000000build: off cache: - tmp matrix: fast_finish: true environment: matrix: - NODE: 0.10 TYPE: compress - NODE: 0.10 TYPE: mocha - NODE: 0.10 TYPE: release/benchmark - NODE: 0.10 TYPE: release/jetstream - NODE: 0.12 TYPE: compress - NODE: 0.12 TYPE: mocha - NODE: 0.12 TYPE: release/benchmark - NODE: 0.12 TYPE: release/jetstream - NODE: 4 TYPE: compress - NODE: 4 TYPE: mocha - NODE: 4 TYPE: release/benchmark - NODE: 4 TYPE: release/jetstream - NODE: 6 TYPE: compress - NODE: 6 TYPE: mocha - NODE: 6 TYPE: release/benchmark - NODE: 6 TYPE: release/jetstream - NODE: 8 TYPE: compress - NODE: 8 TYPE: mocha - NODE: 8 TYPE: release/benchmark - NODE: 8 TYPE: release/jetstream - NODE: 10 TYPE: compress - NODE: 10 TYPE: mocha - NODE: 10 TYPE: release/benchmark - NODE: 10 TYPE: release/jetstream - NODE: latest TYPE: compress - NODE: latest TYPE: mocha - NODE: latest TYPE: release/benchmark - NODE: latest TYPE: release/jetstream install: - git clone --branch v1.5.2 --depth 1 https://github.com/jasongin/nvs.git %LOCALAPPDATA%\nvs - set PATH=%LOCALAPPDATA%\nvs;%PATH% - nvs --version - nvs add node/%NODE% - nvs use node/%NODE% - node --version - npm --version --no-update-notifier - npm install --no-audit --no-optional --no-save --no-update-notifier test_script: - node test/%TYPE% UglifyJS2-3.6.3/bin/000077500000000000000000000000001355252637300140645ustar00rootroot00000000000000UglifyJS2-3.6.3/bin/uglifyjs000077500000000000000000000354471355252637300156630ustar00rootroot00000000000000#! /usr/bin/env node // -*- js -*- "use strict"; require("../tools/exit"); var fs = require("fs"); var info = require("../package.json"); var path = require("path"); var program = require("commander"); var UglifyJS = require("../tools/node"); var skip_keys = [ "cname", "inlined", "parent_scope", "scope", "uses_eval", "uses_with" ]; var files = {}; var options = { compress: false, mangle: false }; program.version(info.name + " " + info.version); program.parseArgv = program.parse; program.parse = undefined; if (process.argv.indexOf("ast") >= 0) program.helpInformation = UglifyJS.describe_ast; else if (process.argv.indexOf("options") >= 0) program.helpInformation = function() { var text = []; var options = UglifyJS.default_options(); for (var option in options) { text.push("--" + (option == "output" ? "beautify" : option == "sourceMap" ? "source-map" : option) + " options:"); text.push(format_object(options[option])); text.push(""); } return text.join("\n"); }; program.option("-p, --parse ", "Specify parser options.", parse_js()); program.option("-c, --compress [options]", "Enable compressor/specify compressor options.", parse_js()); program.option("-m, --mangle [options]", "Mangle names/specify mangler options.", parse_js()); program.option("--mangle-props [options]", "Mangle properties/specify mangler options.", parse_js()); program.option("-b, --beautify [options]", "Beautify output/specify output options.", parse_js()); program.option("-o, --output ", "Output file (default STDOUT)."); program.option("--comments [filter]", "Preserve copyright comments in the output."); program.option("--config-file ", "Read minify() options from JSON file."); program.option("-d, --define [=value]", "Global definitions.", parse_js("define")); program.option("-e, --enclose [arg[,...][:value[,...]]]", "Embed everything in a big function, with configurable argument(s) & value(s)."); program.option("--ie8", "Support non-standard Internet Explorer 8."); program.option("--keep-fnames", "Do not mangle/drop function names. Useful for code relying on Function.prototype.name."); program.option("--name-cache ", "File to hold mangled name mappings."); program.option("--rename", "Force symbol expansion."); program.option("--no-rename", "Disable symbol expansion."); program.option("--self", "Build UglifyJS as a library (implies --wrap UglifyJS)"); program.option("--source-map [options]", "Enable source map/specify source map options.", parse_js()); program.option("--timings", "Display operations run time on STDERR."); program.option("--toplevel", "Compress and/or mangle variables in toplevel scope."); program.option("--verbose", "Print diagnostic messages."); program.option("--warn", "Print warning messages."); program.option("--wrap ", "Embed everything as a function with “exports” corresponding to “name” globally."); program.arguments("[files...]").parseArgv(process.argv); if (program.configFile) { options = JSON.parse(read_file(program.configFile)); if (options.mangle && options.mangle.properties && options.mangle.properties.regex) { options.mangle.properties.regex = UglifyJS.parse(options.mangle.properties.regex, { expression: true }).getValue(); } } if (!program.output && program.sourceMap && program.sourceMap.url != "inline") { fatal("cannot write source map to STDOUT"); } [ "compress", "enclose", "ie8", "mangle", "sourceMap", "toplevel", "wrap" ].forEach(function(name) { if (name in program) { options[name] = program[name]; } }); if (program.verbose) { options.warnings = "verbose"; } else if (program.warn) { options.warnings = true; } if (options.warnings) { UglifyJS.AST_Node.log_function(print_error, options.warnings == "verbose"); delete options.warnings; } if (program.beautify) { options.output = typeof program.beautify == "object" ? program.beautify : {}; if (!("beautify" in options.output)) { options.output.beautify = true; } } if (program.comments) { if (typeof options.output != "object") options.output = {}; options.output.comments = typeof program.comments == "string" ? program.comments : "some"; } if (program.define) { if (typeof options.compress != "object") options.compress = {}; if (typeof options.compress.global_defs != "object") options.compress.global_defs = {}; for (var expr in program.define) { options.compress.global_defs[expr] = program.define[expr]; } } if (program.keepFnames) { options.keep_fnames = true; } if (program.mangleProps) { if (program.mangleProps.domprops) { delete program.mangleProps.domprops; } else { if (typeof program.mangleProps != "object") program.mangleProps = {}; if (!Array.isArray(program.mangleProps.reserved)) program.mangleProps.reserved = []; require("../tools/domprops").forEach(function(name) { UglifyJS.push_uniq(program.mangleProps.reserved, name); }); } if (typeof options.mangle != "object") options.mangle = {}; options.mangle.properties = program.mangleProps; } if (program.nameCache) { options.nameCache = JSON.parse(read_file(program.nameCache, "{}")); } if (program.output == "ast") { options.output = { ast: true, code: false }; } if (program.parse) { if (!program.parse.acorn && !program.parse.spidermonkey) { options.parse = program.parse; } else if (program.sourceMap && program.sourceMap.content == "inline") { fatal("inline source map only works with built-in parser"); } } if (~program.rawArgs.indexOf("--rename")) { options.rename = true; } else if (!program.rename) { options.rename = false; } var convert_path = function(name) { return name; }; if (typeof program.sourceMap == "object" && "base" in program.sourceMap) { convert_path = function() { var base = program.sourceMap.base; delete options.sourceMap.base; return function(name) { return path.relative(base, name); }; }(); } if (program.self) { if (program.args.length) UglifyJS.AST_Node.warn("Ignoring input files since --self was passed"); if (!options.wrap) options.wrap = "UglifyJS"; simple_glob(UglifyJS.FILES).forEach(function(name) { files[convert_path(name)] = read_file(name); }); run(); } else if (program.args.length) { simple_glob(program.args).forEach(function(name) { files[convert_path(name)] = read_file(name); }); run(); } else { var chunks = []; process.stdin.setEncoding("utf8"); process.stdin.on("data", function(chunk) { chunks.push(chunk); }).on("end", function() { files = [ chunks.join("") ]; run(); }); process.stdin.resume(); } function convert_ast(fn) { return UglifyJS.AST_Node.from_mozilla_ast(Object.keys(files).reduce(fn, null)); } function run() { var content = program.sourceMap && program.sourceMap.content; if (content && content != "inline") { UglifyJS.AST_Node.info("Using input source map: " + content); options.sourceMap.content = read_file(content, content); } if (program.timings) options.timings = true; try { if (program.parse) { if (program.parse.acorn) { files = convert_ast(function(toplevel, name) { return require("acorn").parse(files[name], { locations: true, program: toplevel, sourceFile: name }); }); } else if (program.parse.spidermonkey) { files = convert_ast(function(toplevel, name) { var obj = JSON.parse(files[name]); if (!toplevel) return obj; toplevel.body = toplevel.body.concat(obj.body); return toplevel; }); } } } catch (ex) { fatal(ex); } var result = UglifyJS.minify(files, options); if (result.error) { var ex = result.error; if (ex.name == "SyntaxError") { print_error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col); var file = files[ex.filename]; if (file) { var col = ex.col; var lines = file.split(/\r?\n/); var line = lines[ex.line - 1]; if (!line && !col) { line = lines[ex.line - 2]; col = line.length; } if (line) { var limit = 70; if (col > limit) { line = line.slice(col - limit); col = limit; } print_error(line.slice(0, 80)); print_error(line.slice(0, col).replace(/\S/g, " ") + "^"); } } } else if (ex.defs) { print_error("Supported options:"); print_error(format_object(ex.defs)); } fatal(ex); } else if (program.output == "ast") { if (!options.compress && !options.mangle) { result.ast.figure_out_scope({}); } print(JSON.stringify(result.ast, function(key, value) { if (value) switch (key) { case "thedef": return symdef(value); case "enclosed": return value.length ? value.map(symdef) : undefined; case "variables": case "functions": case "globals": return value.size() ? value.map(symdef) : undefined; } if (skip_key(key)) return; if (value instanceof UglifyJS.AST_Token) return; if (value instanceof UglifyJS.Dictionary) return; if (value instanceof UglifyJS.AST_Node) { var result = { _class: "AST_" + value.TYPE }; value.CTOR.PROPS.forEach(function(prop) { result[prop] = value[prop]; }); return result; } return value; }, 2)); } else if (program.output == "spidermonkey") { print(JSON.stringify(UglifyJS.minify(result.code, { compress: false, mangle: false, output: { ast: true, code: false } }).ast.to_mozilla_ast(), null, 2)); } else if (program.output) { fs.writeFileSync(program.output, result.code); if (result.map) { fs.writeFileSync(program.output + ".map", result.map); } } else { print(result.code); } if (program.nameCache) { fs.writeFileSync(program.nameCache, JSON.stringify(options.nameCache)); } if (result.timings) for (var phase in result.timings) { print_error("- " + phase + ": " + result.timings[phase].toFixed(3) + "s"); } } function fatal(message) { if (message instanceof Error) { message = message.stack.replace(/^\S*?Error:/, "ERROR:") } else { message = "ERROR: " + message; } print_error(message); process.exit(1); } // A file glob function that only supports "*" and "?" wildcards in the basename. // Example: "foo/bar/*baz??.*.js" // Argument `glob` may be a string or an array of strings. // Returns an array of strings. Garbage in, garbage out. function simple_glob(glob) { if (Array.isArray(glob)) { return [].concat.apply([], glob.map(simple_glob)); } if (glob.match(/\*|\?/)) { var dir = path.dirname(glob); try { var entries = fs.readdirSync(dir); } catch (ex) {} if (entries) { var pattern = "^" + path.basename(glob) .replace(/[.+^$[\]\\(){}]/g, "\\$&") .replace(/\*/g, "[^/\\\\]*") .replace(/\?/g, "[^/\\\\]") + "$"; var mod = process.platform === "win32" ? "i" : ""; var rx = new RegExp(pattern, mod); var results = entries.filter(function(name) { return rx.test(name); }).map(function(name) { return path.join(dir, name); }); if (results.length) return results; } } return [ glob ]; } function read_file(path, default_value) { try { return fs.readFileSync(path, "utf8"); } catch (ex) { if (ex.code == "ENOENT" && default_value != null) return default_value; fatal(ex); } } function parse_js(flag) { return function(value, options) { options = options || {}; try { UglifyJS.parse(value, { expression: true }).walk(new UglifyJS.TreeWalker(function(node) { if (node instanceof UglifyJS.AST_Assign) { var name = node.left.print_to_string(); var value = node.right; if (flag) { options[name] = value; } else if (value instanceof UglifyJS.AST_Array) { options[name] = value.elements.map(to_string); } else { options[name] = to_string(value); } return true; } if (node instanceof UglifyJS.AST_Symbol || node instanceof UglifyJS.AST_PropAccess) { var name = node.print_to_string(); options[name] = true; return true; } if (!(node instanceof UglifyJS.AST_Sequence)) throw node; function to_string(value) { return value instanceof UglifyJS.AST_Constant ? value.getValue() : value.print_to_string({ quote_keys: true }); } })); } catch (ex) { if (flag) { fatal("cannot parse arguments for '" + flag + "': " + value); } else { options[value] = null; } } return options; } } function skip_key(key) { return skip_keys.indexOf(key) >= 0; } function symdef(def) { var ret = (1e6 + def.id) + " " + def.name; if (def.mangled_name) ret += " " + def.mangled_name; return ret; } function format_object(obj) { var lines = []; var padding = ""; Object.keys(obj).map(function(name) { if (padding.length < name.length) padding = Array(name.length + 1).join(" "); return [ name, JSON.stringify(obj[name]) ]; }).forEach(function(tokens) { lines.push(" " + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]); }); return lines.join("\n"); } function print_error(msg) { process.stderr.write(msg); process.stderr.write("\n"); } function print(txt) { process.stdout.write(txt); process.stdout.write("\n"); } UglifyJS2-3.6.3/lib/000077500000000000000000000000001355252637300140625ustar00rootroot00000000000000UglifyJS2-3.6.3/lib/ast.js000066400000000000000000001000051355252637300152030ustar00rootroot00000000000000/*********************************************************************** A JavaScript tokenizer / parser / beautifier / compressor. https://github.com/mishoo/UglifyJS2 -------------------------------- (C) --------------------------------- Author: Mihai Bazon http://mihai.bazon.net/blog Distributed under the BSD license: Copyright 2012 (c) Mihai Bazon Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***********************************************************************/ "use strict"; function DEFNODE(type, props, methods, base) { if (typeof base === "undefined") base = AST_Node; props = props ? props.split(/\s+/) : []; var self_props = props; if (base && base.PROPS) props = props.concat(base.PROPS); var code = [ "return function AST_", type, "(props){", "if(props){", ]; props.forEach(function(prop) { code.push("this.", prop, "=props.", prop, ";"); }); var proto = base && new base; if (proto && proto.initialize || methods && methods.initialize) code.push("this.initialize();"); code.push("}}"); var ctor = new Function(code.join(""))(); if (proto) { ctor.prototype = proto; ctor.BASE = base; } if (base) base.SUBCLASSES.push(ctor); ctor.prototype.CTOR = ctor; ctor.PROPS = props || null; ctor.SELF_PROPS = self_props; ctor.SUBCLASSES = []; if (type) { ctor.prototype.TYPE = ctor.TYPE = type; } if (methods) for (var name in methods) if (HOP(methods, name)) { if (/^\$/.test(name)) { ctor[name.substr(1)] = methods[name]; } else { ctor.prototype[name] = methods[name]; } } ctor.DEFMETHOD = function(name, method) { this.prototype[name] = method; }; if (typeof exports !== "undefined") { exports["AST_" + type] = ctor; } return ctor; } var AST_Token = DEFNODE("Token", "type value line col pos endline endcol endpos nlb comments_before comments_after file raw", { }, null); var AST_Node = DEFNODE("Node", "start end", { _clone: function(deep) { if (deep) { var self = this.clone(); return self.transform(new TreeTransformer(function(node) { if (node !== self) { return node.clone(true); } })); } return new this.CTOR(this); }, clone: function(deep) { return this._clone(deep); }, $documentation: "Base class of all AST nodes", $propdoc: { start: "[AST_Token] The first token of this node", end: "[AST_Token] The last token of this node" }, _walk: function(visitor) { return visitor._visit(this); }, walk: function(visitor) { return this._walk(visitor); // not sure the indirection will be any help } }, null); (AST_Node.log_function = function(fn, verbose) { var printed = Object.create(null); if (fn) { AST_Node.info = verbose ? function(text, props) { log("INFO: " + string_template(text, props)); } : noop; AST_Node.warn = function(text, props) { log("WARN: " + string_template(text, props)); }; } else { AST_Node.info = AST_Node.warn = noop; } function log(msg) { if (printed[msg]) return; printed[msg] = true; fn(msg); } })(); /* -----[ statements ]----- */ var AST_Statement = DEFNODE("Statement", null, { $documentation: "Base class of all statements", }); var AST_Debugger = DEFNODE("Debugger", null, { $documentation: "Represents a debugger statement", }, AST_Statement); 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!)", quote: "[string] the original quote character" }, }, AST_Statement); var AST_SimpleStatement = DEFNODE("SimpleStatement", "body", { $documentation: "A statement consisting of an expression, i.e. a = 1 + 2", $propdoc: { body: "[AST_Node] an expression node (should not be instanceof AST_Statement)" }, _walk: function(visitor) { return visitor._visit(this, function() { this.body._walk(visitor); }); } }, AST_Statement); function walk_body(node, visitor) { var body = node.body; if (body instanceof AST_Statement) { body._walk(visitor); } else body.forEach(function(node) { node._walk(visitor); }); } var AST_Block = DEFNODE("Block", "body", { $documentation: "A body of statements (usually braced)", $propdoc: { body: "[AST_Statement*] an array of statements" }, _walk: function(visitor) { return visitor._visit(this, function() { walk_body(this, visitor); }); } }, AST_Statement); var AST_BlockStatement = DEFNODE("BlockStatement", null, { $documentation: "A block statement", }, AST_Block); var AST_EmptyStatement = DEFNODE("EmptyStatement", null, { $documentation: "The empty statement (empty block or simply a semicolon)" }, AST_Statement); var AST_StatementWithBody = DEFNODE("StatementWithBody", "body", { $documentation: "Base class for all statements that contain one nested body: `For`, `ForIn`, `Do`, `While`, `With`", $propdoc: { body: "[AST_Statement] the body; this should always be present, even if it's an AST_EmptyStatement" } }, AST_Statement); var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", { $documentation: "Statement with a label", $propdoc: { label: "[AST_Label] a label definition" }, _walk: function(visitor) { return visitor._visit(this, function() { this.label._walk(visitor); this.body._walk(visitor); }); }, clone: function(deep) { var node = this._clone(deep); if (deep) { var label = node.label; var def = this.label; node.walk(new TreeWalker(function(node) { if (node instanceof AST_LoopControl && node.label && node.label.thedef === def) { node.label.thedef = label; label.references.push(node); } })); } return node; } }, AST_StatementWithBody); var AST_IterationStatement = DEFNODE("IterationStatement", null, { $documentation: "Internal class. All loops inherit from it." }, AST_StatementWithBody); var AST_DWLoop = DEFNODE("DWLoop", "condition", { $documentation: "Base class for do/while statements", $propdoc: { condition: "[AST_Node] the loop condition. Should not be instanceof AST_Statement" } }, AST_IterationStatement); var AST_Do = DEFNODE("Do", null, { $documentation: "A `do` statement", _walk: function(visitor) { return visitor._visit(this, function() { this.body._walk(visitor); this.condition._walk(visitor); }); } }, AST_DWLoop); var AST_While = DEFNODE("While", null, { $documentation: "A `while` statement", _walk: function(visitor) { return visitor._visit(this, function() { this.condition._walk(visitor); this.body._walk(visitor); }); } }, AST_DWLoop); var AST_For = DEFNODE("For", "init condition step", { $documentation: "A `for` statement", $propdoc: { init: "[AST_Node?] the `for` initialization code, or null if empty", condition: "[AST_Node?] the `for` termination clause, or null if empty", step: "[AST_Node?] the `for` update clause, or null if empty" }, _walk: function(visitor) { return visitor._visit(this, function() { if (this.init) this.init._walk(visitor); if (this.condition) this.condition._walk(visitor); if (this.step) this.step._walk(visitor); this.body._walk(visitor); }); } }, AST_IterationStatement); var AST_ForIn = DEFNODE("ForIn", "init object", { $documentation: "A `for ... in` statement", $propdoc: { init: "[AST_Node] the `for/in` initialization code", object: "[AST_Node] the object that we're looping through" }, _walk: function(visitor) { return visitor._visit(this, function() { this.init._walk(visitor); this.object._walk(visitor); this.body._walk(visitor); }); } }, AST_IterationStatement); var AST_With = DEFNODE("With", "expression", { $documentation: "A `with` statement", $propdoc: { expression: "[AST_Node] the `with` expression" }, _walk: function(visitor) { return visitor._visit(this, function() { this.expression._walk(visitor); this.body._walk(visitor); }); } }, AST_StatementWithBody); /* -----[ scope and functions ]----- */ 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: { 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", uses_eval: "[boolean/S] tells whether this scope contains a direct call to the global `eval`", parent_scope: "[AST_Scope?/S] link to the parent scope", enclosed: "[SymbolDef*/S] a list of all symbol definitions that are accessed from this scope or any subscopes", cname: "[integer/S] current index for mangling variables (used internally by the mangler)", }, clone: function(deep) { var node = this._clone(deep); if (this.variables) node.variables = this.variables.clone(); if (this.functions) node.functions = this.functions.clone(); if (this.enclosed) node.enclosed = this.enclosed.slice(); return node; }, pinned: function() { return this.uses_eval || this.uses_with; } }, AST_Block); var AST_Toplevel = DEFNODE("Toplevel", "globals", { $documentation: "The toplevel scope", $propdoc: { globals: "[Object/S] a map of name -> SymbolDef for all undeclared names", }, wrap: function(name) { var body = this.body; return parse([ "(function(exports){'$ORIG';})(typeof ", name, "=='undefined'?(", name, "={}):", name, ");" ].join(""), { filename: "wrap=" + JSON.stringify(name) }).transform(new TreeTransformer(function(node) { if (node instanceof AST_Directive && node.value == "$ORIG") { return MAP.splice(body); } })); }, enclose: function(args_values) { if (typeof args_values != "string") args_values = ""; var index = args_values.indexOf(":"); if (index < 0) index = args_values.length; var body = this.body; return parse([ "(function(", args_values.slice(0, index), '){"$ORIG"})(', args_values.slice(index + 1), ")" ].join(""), { filename: "enclose=" + JSON.stringify(args_values) }).transform(new TreeTransformer(function(node) { if (node instanceof AST_Directive && node.value == "$ORIG") { return MAP.splice(body); } })); } }, AST_Scope); var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments length_read", { $documentation: "Base class for functions", $propdoc: { name: "[AST_SymbolDeclaration?] the name of this function", argnames: "[AST_SymbolFunarg*] array of function arguments", uses_arguments: "[boolean/S] tells whether this function accesses the arguments array" }, _walk: function(visitor) { return visitor._visit(this, function() { if (this.name) this.name._walk(visitor); this.argnames.forEach(function(argname) { argname._walk(visitor); }); walk_body(this, visitor); }); } }, AST_Scope); var AST_Accessor = DEFNODE("Accessor", null, { $documentation: "A setter/getter function. The `name` property is always null." }, AST_Lambda); var AST_Function = DEFNODE("Function", "inlined", { $documentation: "A function expression" }, AST_Lambda); var AST_Defun = DEFNODE("Defun", "inlined", { $documentation: "A function definition" }, AST_Lambda); /* -----[ JUMPS ]----- */ var AST_Jump = DEFNODE("Jump", null, { $documentation: "Base class for “jumps” (for now that's `return`, `throw`, `break` and `continue`)" }, AST_Statement); var AST_Exit = DEFNODE("Exit", "value", { $documentation: "Base class for “exits” (`return` and `throw`)", $propdoc: { value: "[AST_Node?] the value returned or thrown by this statement; could be null for AST_Return" }, _walk: function(visitor) { return visitor._visit(this, this.value && function() { this.value._walk(visitor); }); } }, AST_Jump); var AST_Return = DEFNODE("Return", null, { $documentation: "A `return` statement" }, AST_Exit); var AST_Throw = DEFNODE("Throw", null, { $documentation: "A `throw` statement" }, AST_Exit); var AST_LoopControl = DEFNODE("LoopControl", "label", { $documentation: "Base class for loop control statements (`break` and `continue`)", $propdoc: { label: "[AST_LabelRef?] the label, or null if none", }, _walk: function(visitor) { return visitor._visit(this, this.label && function() { this.label._walk(visitor); }); } }, AST_Jump); var AST_Break = DEFNODE("Break", null, { $documentation: "A `break` statement" }, AST_LoopControl); var AST_Continue = DEFNODE("Continue", null, { $documentation: "A `continue` statement" }, AST_LoopControl); /* -----[ IF ]----- */ var AST_If = DEFNODE("If", "condition alternative", { $documentation: "A `if` statement", $propdoc: { condition: "[AST_Node] the `if` condition", alternative: "[AST_Statement?] the `else` part, or null if not present" }, _walk: function(visitor) { return visitor._visit(this, function() { this.condition._walk(visitor); this.body._walk(visitor); if (this.alternative) this.alternative._walk(visitor); }); } }, AST_StatementWithBody); /* -----[ SWITCH ]----- */ var AST_Switch = DEFNODE("Switch", "expression", { $documentation: "A `switch` statement", $propdoc: { expression: "[AST_Node] the `switch` “discriminant”" }, _walk: function(visitor) { return visitor._visit(this, function() { this.expression._walk(visitor); walk_body(this, visitor); }); } }, AST_Block); var AST_SwitchBranch = DEFNODE("SwitchBranch", null, { $documentation: "Base class for `switch` branches", }, AST_Block); var AST_Default = DEFNODE("Default", null, { $documentation: "A `default` switch branch", }, AST_SwitchBranch); var AST_Case = DEFNODE("Case", "expression", { $documentation: "A `case` switch branch", $propdoc: { expression: "[AST_Node] the `case` expression" }, _walk: function(visitor) { return visitor._visit(this, function() { this.expression._walk(visitor); walk_body(this, visitor); }); } }, AST_SwitchBranch); /* -----[ EXCEPTIONS ]----- */ var AST_Try = DEFNODE("Try", "bcatch bfinally", { $documentation: "A `try` statement", $propdoc: { bcatch: "[AST_Catch?] the catch block, or null if not present", bfinally: "[AST_Finally?] the finally block, or null if not present" }, _walk: function(visitor) { return visitor._visit(this, function() { walk_body(this, visitor); if (this.bcatch) this.bcatch._walk(visitor); if (this.bfinally) this.bfinally._walk(visitor); }); } }, AST_Block); var AST_Catch = DEFNODE("Catch", "argname", { $documentation: "A `catch` node; only makes sense as part of a `try` statement", $propdoc: { argname: "[AST_SymbolCatch] symbol for the exception" }, _walk: function(visitor) { return visitor._visit(this, function() { this.argname._walk(visitor); walk_body(this, visitor); }); } }, AST_Block); var AST_Finally = DEFNODE("Finally", null, { $documentation: "A `finally` node; only makes sense as part of a `try` statement" }, AST_Block); /* -----[ VAR ]----- */ var AST_Definitions = DEFNODE("Definitions", "definitions", { $documentation: "Base class for `var` nodes (variable declarations/initializations)", $propdoc: { definitions: "[AST_VarDef*] array of variable definitions" }, _walk: function(visitor) { return visitor._visit(this, function() { this.definitions.forEach(function(defn) { defn._walk(visitor); }); }); } }, AST_Statement); var AST_Var = DEFNODE("Var", null, { $documentation: "A `var` statement" }, AST_Definitions); var AST_VarDef = DEFNODE("VarDef", "name value", { $documentation: "A variable declaration; only appears in a AST_Definitions node", $propdoc: { name: "[AST_SymbolVar] name of the variable", value: "[AST_Node?] initializer, or null of there's no initializer" }, _walk: function(visitor) { return visitor._visit(this, function() { this.name._walk(visitor); if (this.value) this.value._walk(visitor); }); } }); /* -----[ OTHER ]----- */ var AST_Call = DEFNODE("Call", "expression args", { $documentation: "A function call expression", $propdoc: { expression: "[AST_Node] expression to invoke as function", args: "[AST_Node*] array of arguments" }, _walk: function(visitor) { return visitor._visit(this, function() { this.expression._walk(visitor); this.args.forEach(function(node) { node._walk(visitor); }); }); } }); var AST_New = DEFNODE("New", null, { $documentation: "An object instantiation. Derives from a function call since it has exactly the same properties" }, AST_Call); var AST_Sequence = DEFNODE("Sequence", "expressions", { $documentation: "A sequence expression (comma-separated expressions)", $propdoc: { expressions: "[AST_Node*] array of expressions (at least two)" }, _walk: function(visitor) { return visitor._visit(this, function() { this.expressions.forEach(function(node) { node._walk(visitor); }); }); } }); var AST_PropAccess = DEFNODE("PropAccess", "expression property", { $documentation: "Base class for property access expressions, i.e. `a.foo` or `a[\"foo\"]`", $propdoc: { expression: "[AST_Node] the “container” expression", property: "[AST_Node|string] the property to access. For AST_Dot this is always a plain string, while for AST_Sub it's an arbitrary AST_Node" }, getProperty: function() { var p = this.property; if (p instanceof AST_Constant) { return p.getValue(); } if (p instanceof AST_UnaryPrefix && p.operator == "void" && p.expression instanceof AST_Constant) { return; } return p; } }); var AST_Dot = DEFNODE("Dot", null, { $documentation: "A dotted property access expression", _walk: function(visitor) { return visitor._visit(this, function() { this.expression._walk(visitor); }); } }, AST_PropAccess); var AST_Sub = DEFNODE("Sub", null, { $documentation: "Index-style property access, i.e. `a[\"foo\"]`", _walk: function(visitor) { return visitor._visit(this, function() { this.expression._walk(visitor); this.property._walk(visitor); }); } }, AST_PropAccess); var AST_Unary = DEFNODE("Unary", "operator expression", { $documentation: "Base class for unary expressions", $propdoc: { operator: "[string] the operator", expression: "[AST_Node] expression that this unary operator applies to" }, _walk: function(visitor) { return visitor._visit(this, function() { this.expression._walk(visitor); }); } }); var AST_UnaryPrefix = DEFNODE("UnaryPrefix", null, { $documentation: "Unary prefix expression, i.e. `typeof i` or `++i`" }, AST_Unary); var AST_UnaryPostfix = DEFNODE("UnaryPostfix", null, { $documentation: "Unary postfix expression, i.e. `i++`" }, AST_Unary); var AST_Binary = DEFNODE("Binary", "operator left right", { $documentation: "Binary expression, i.e. `a + b`", $propdoc: { left: "[AST_Node] left-hand side expression", operator: "[string] the operator", right: "[AST_Node] right-hand side expression" }, _walk: function(visitor) { return visitor._visit(this, function() { this.left._walk(visitor); this.right._walk(visitor); }); } }); var AST_Conditional = DEFNODE("Conditional", "condition consequent alternative", { $documentation: "Conditional expression using the ternary operator, i.e. `a ? b : c`", $propdoc: { condition: "[AST_Node]", consequent: "[AST_Node]", alternative: "[AST_Node]" }, _walk: function(visitor) { return visitor._visit(this, function() { this.condition._walk(visitor); this.consequent._walk(visitor); this.alternative._walk(visitor); }); } }); var AST_Assign = DEFNODE("Assign", null, { $documentation: "An assignment expression — `a = b + 5`", }, AST_Binary); /* -----[ LITERALS ]----- */ var AST_Array = DEFNODE("Array", "elements", { $documentation: "An array literal", $propdoc: { elements: "[AST_Node*] array of elements" }, _walk: function(visitor) { return visitor._visit(this, function() { this.elements.forEach(function(element) { element._walk(visitor); }); }); } }); var AST_Object = DEFNODE("Object", "properties", { $documentation: "An object literal", $propdoc: { properties: "[AST_ObjectProperty*] array of properties" }, _walk: function(visitor) { return visitor._visit(this, function() { this.properties.forEach(function(prop) { prop._walk(visitor); }); }); } }); var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", { $documentation: "Base class for literal object properties", $propdoc: { key: "[string|AST_SymbolAccessor] property name. For ObjectKeyVal this is a string. For getters and setters this is an AST_SymbolAccessor.", value: "[AST_Node] property value. For getters and setters this is an AST_Accessor." }, _walk: function(visitor) { return visitor._visit(this, function() { this.value._walk(visitor); }); } }); var AST_ObjectKeyVal = DEFNODE("ObjectKeyVal", "quote", { $documentation: "A key: value object property", $propdoc: { quote: "[string] the original quote character" } }, AST_ObjectProperty); var AST_ObjectSetter = DEFNODE("ObjectSetter", null, { $documentation: "An object setter property", }, AST_ObjectProperty); var AST_ObjectGetter = DEFNODE("ObjectGetter", null, { $documentation: "An object getter property", }, AST_ObjectProperty); var AST_Symbol = DEFNODE("Symbol", "scope name thedef", { $propdoc: { name: "[string] name of this symbol", scope: "[AST_Scope/S] the current scope (not necessarily the definition scope)", thedef: "[SymbolDef/S] the definition of this symbol" }, $documentation: "Base class for all symbols", }); var AST_SymbolAccessor = DEFNODE("SymbolAccessor", null, { $documentation: "The name of a property accessor (setter/getter function)" }, AST_Symbol); var AST_SymbolDeclaration = DEFNODE("SymbolDeclaration", "init", { $documentation: "A declaration symbol (symbol in var, function name or argument, symbol in catch)", }, AST_Symbol); var AST_SymbolVar = DEFNODE("SymbolVar", null, { $documentation: "Symbol defining a variable", }, AST_SymbolDeclaration); var AST_SymbolFunarg = DEFNODE("SymbolFunarg", null, { $documentation: "Symbol naming a function argument", }, AST_SymbolVar); var AST_SymbolDefun = DEFNODE("SymbolDefun", null, { $documentation: "Symbol defining a function", }, AST_SymbolDeclaration); var AST_SymbolLambda = DEFNODE("SymbolLambda", null, { $documentation: "Symbol naming a function expression", }, AST_SymbolDeclaration); var AST_SymbolCatch = DEFNODE("SymbolCatch", null, { $documentation: "Symbol naming the exception in catch", }, AST_SymbolDeclaration); var AST_Label = DEFNODE("Label", "references", { $documentation: "Symbol naming a label (declaration)", $propdoc: { references: "[AST_LoopControl*] a list of nodes referring to this label" }, initialize: function() { this.references = []; this.thedef = this; } }, AST_Symbol); var AST_SymbolRef = DEFNODE("SymbolRef", "fixed", { $documentation: "Reference to some symbol (not definition/declaration)", }, AST_Symbol); var AST_LabelRef = DEFNODE("LabelRef", null, { $documentation: "Reference to a label symbol", }, AST_Symbol); var AST_This = DEFNODE("This", null, { $documentation: "The `this` symbol", }, AST_Symbol); var AST_Constant = DEFNODE("Constant", null, { $documentation: "Base class for all constants", getValue: function() { return this.value; } }); var AST_String = DEFNODE("String", "value quote", { $documentation: "A string literal", $propdoc: { value: "[string] the contents of this string", quote: "[string] the original quote character" } }, AST_Constant); var AST_Number = DEFNODE("Number", "value", { $documentation: "A number literal", $propdoc: { value: "[number] the numeric value", } }, AST_Constant); var AST_RegExp = DEFNODE("RegExp", "value", { $documentation: "A regexp literal", $propdoc: { value: "[RegExp] the actual regexp" } }, AST_Constant); var AST_Atom = DEFNODE("Atom", null, { $documentation: "Base class for atoms", }, AST_Constant); var AST_Null = DEFNODE("Null", null, { $documentation: "The `null` atom", value: null }, AST_Atom); var AST_NaN = DEFNODE("NaN", null, { $documentation: "The impossible value", value: 0/0 }, AST_Atom); var AST_Undefined = DEFNODE("Undefined", null, { $documentation: "The `undefined` value", value: function(){}() }, AST_Atom); var AST_Hole = DEFNODE("Hole", null, { $documentation: "A hole in an array", value: function(){}() }, AST_Atom); var AST_Infinity = DEFNODE("Infinity", null, { $documentation: "The `Infinity` value", value: 1/0 }, AST_Atom); var AST_Boolean = DEFNODE("Boolean", null, { $documentation: "Base class for booleans", }, AST_Atom); var AST_False = DEFNODE("False", null, { $documentation: "The `false` atom", value: false }, AST_Boolean); var AST_True = DEFNODE("True", null, { $documentation: "The `true` atom", value: true }, AST_Boolean); /* -----[ TreeWalker ]----- */ function TreeWalker(callback) { this.visit = callback; this.stack = []; this.directives = Object.create(null); } TreeWalker.prototype = { _visit: function(node, descend) { this.push(node); var ret = this.visit(node, descend ? function() { descend.call(node); } : noop); if (!ret && descend) { descend.call(node); } this.pop(); return ret; }, parent: function(n) { return this.stack[this.stack.length - 2 - (n || 0)]; }, push: function(node) { if (node instanceof AST_Lambda) { this.directives = Object.create(this.directives); } else if (node instanceof AST_Directive && !this.directives[node.value]) { this.directives[node.value] = node; } this.stack.push(node); }, pop: function() { if (this.stack.pop() instanceof AST_Lambda) { this.directives = Object.getPrototypeOf(this.directives); } }, self: function() { return this.stack[this.stack.length - 1]; }, find_parent: function(type) { var stack = this.stack; for (var i = stack.length; --i >= 0;) { var x = stack[i]; if (x instanceof type) return x; } }, has_directive: function(type) { var dir = this.directives[type]; if (dir) return dir; var node = this.stack[this.stack.length - 1]; if (node instanceof AST_Scope) { for (var i = 0; i < node.body.length; ++i) { var st = node.body[i]; if (!(st instanceof AST_Directive)) break; if (st.value == type) return st; } } }, loopcontrol_target: function(node) { var stack = this.stack; if (node.label) for (var i = stack.length; --i >= 0;) { var x = stack[i]; if (x instanceof AST_LabeledStatement && x.label.name == node.label.name) return x.body; } else for (var i = stack.length; --i >= 0;) { var x = stack[i]; if (x instanceof AST_IterationStatement || node instanceof AST_Break && x instanceof AST_Switch) return x; } }, in_boolean_context: function() { var self = this.self(); for (var i = 0, p; p = this.parent(i); i++) { if (p instanceof AST_SimpleStatement || p instanceof AST_Conditional && p.condition === self || p instanceof AST_DWLoop && p.condition === self || p instanceof AST_For && p.condition === self || p instanceof AST_If && p.condition === self || p instanceof AST_UnaryPrefix && p.operator == "!" && p.expression === self) { return true; } if (p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||") || p instanceof AST_Conditional || p.tail_node() === self) { self = p; } else if (p instanceof AST_Return) { var fn; do { fn = this.parent(++i); if (!fn) return false; } while (!(fn instanceof AST_Lambda)); if (fn.name) return false; self = this.parent(++i); if (!self || self.TYPE != "Call" || self.expression !== fn) return false; } else { return false; } } } }; UglifyJS2-3.6.3/lib/compress.js000066400000000000000000011070431355252637300162610ustar00rootroot00000000000000/*********************************************************************** A JavaScript tokenizer / parser / beautifier / compressor. https://github.com/mishoo/UglifyJS2 -------------------------------- (C) --------------------------------- Author: Mihai Bazon http://mihai.bazon.net/blog Distributed under the BSD license: Copyright 2012 (c) Mihai Bazon Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***********************************************************************/ "use strict"; function Compressor(options, false_by_default) { if (!(this instanceof Compressor)) return new Compressor(options, false_by_default); TreeTransformer.call(this, this.before, this.after); this.options = defaults(options, { arguments : !false_by_default, assignments : !false_by_default, booleans : !false_by_default, collapse_vars : !false_by_default, comparisons : !false_by_default, conditionals : !false_by_default, dead_code : !false_by_default, directives : !false_by_default, drop_console : false, drop_debugger : !false_by_default, evaluate : !false_by_default, expression : false, functions : !false_by_default, global_defs : false, hoist_funs : false, hoist_props : !false_by_default, hoist_vars : false, ie8 : false, if_return : !false_by_default, inline : !false_by_default, join_vars : !false_by_default, keep_fargs : false_by_default || "strict", keep_fnames : false, keep_infinity : false, loops : !false_by_default, negate_iife : !false_by_default, passes : 1, properties : !false_by_default, pure_getters : !false_by_default && "strict", pure_funcs : null, reduce_funcs : !false_by_default, reduce_vars : !false_by_default, sequences : !false_by_default, side_effects : !false_by_default, switches : !false_by_default, top_retain : null, toplevel : !!(options && options["top_retain"]), typeofs : !false_by_default, unsafe : false, unsafe_comps : false, unsafe_Function : false, unsafe_math : false, unsafe_proto : false, unsafe_regexp : false, unsafe_undefined: false, unused : !false_by_default, }, true); var global_defs = this.options["global_defs"]; if (typeof global_defs == "object") for (var key in global_defs) { if (/^@/.test(key) && HOP(global_defs, key)) { global_defs[key.slice(1)] = parse(global_defs[key], { expression: true }); } } if (this.options["inline"] === true) this.options["inline"] = 3; var keep_fargs = this.options["keep_fargs"]; this.drop_fargs = keep_fargs == "strict" ? function(lambda, parent) { if (lambda.length_read) return false; var name = lambda.name; if (!name) return parent && parent.TYPE == "Call" && parent.expression === lambda; if (name.fixed_value() !== lambda) return false; var def = name.definition(); if (def.direct_access) return false; var escaped = def.escaped; return escaped && escaped.depth != 1; } : keep_fargs ? return_false : return_true; var pure_funcs = this.options["pure_funcs"]; if (typeof pure_funcs == "function") { this.pure_funcs = pure_funcs; } else if (typeof pure_funcs == "string") { this.pure_funcs = function(node) { return pure_funcs !== node.expression.print_to_string(); }; } else if (Array.isArray(pure_funcs)) { this.pure_funcs = function(node) { return !member(node.expression.print_to_string(), pure_funcs); }; } else { this.pure_funcs = return_true; } var sequences = this.options["sequences"]; this.sequences_limit = sequences == 1 ? 800 : sequences | 0; var top_retain = this.options["top_retain"]; if (top_retain instanceof RegExp) { this.top_retain = function(def) { return top_retain.test(def.name); }; } else if (typeof top_retain == "function") { this.top_retain = top_retain; } else if (top_retain) { if (typeof top_retain == "string") { top_retain = top_retain.split(/,/); } this.top_retain = function(def) { return member(def.name, top_retain); }; } var toplevel = this.options["toplevel"]; this.toplevel = typeof toplevel == "string" ? { funcs: /funcs/.test(toplevel), vars: /vars/.test(toplevel) } : { funcs: toplevel, vars: toplevel }; } Compressor.prototype = new TreeTransformer; merge(Compressor.prototype, { option: function(key) { return this.options[key] }, exposed: function(def) { if (def.global) for (var i = 0; i < def.orig.length; i++) if (!this.toplevel[def.orig[i] instanceof AST_SymbolDefun ? "funcs" : "vars"]) return true; return false; }, compress: function(node) { node = node.resolve_defines(this); if (this.option("expression")) { node.process_expression(true); } var passes = +this.options.passes || 1; var min_count = 1 / 0; var stopping = false; var mangle = { ie8: this.option("ie8") }; for (var pass = 0; pass < passes; pass++) { node.figure_out_scope(mangle); if (pass > 0 || this.option("reduce_vars")) node.reset_opt_flags(this); node = node.transform(this); if (passes > 1) { var count = 0; node.walk(new TreeWalker(function() { count++; })); AST_Node.info("pass " + pass + ": last_count: " + min_count + ", count: " + count); if (count < min_count) { min_count = count; stopping = false; } else if (stopping) { break; } else { stopping = true; } } } if (this.option("expression")) { node.process_expression(false); } return node; }, before: function(node, descend, in_list) { if (node._squeezed) return node; var is_scope = node instanceof AST_Scope; if (is_scope) { node.hoist_properties(this); node.hoist_declarations(this); } // Before https://github.com/mishoo/UglifyJS2/pull/1602 AST_Node.optimize() // would call AST_Node.transform() if a different instance of AST_Node is // produced after OPT(). // This corrupts TreeWalker.stack, which cause AST look-ups to malfunction. // Migrate and defer all children's AST_Node.transform() to below, which // will now happen after this parent AST_Node has been properly substituted // thus gives a consistent AST snapshot. descend(node, this); // Existing code relies on how AST_Node.optimize() worked, and omitting the // following replacement call would result in degraded efficiency of both // output and performance. descend(node, this); var opt = node.optimize(this); if (is_scope) { opt.drop_unused(this); descend(opt, this); } if (opt === node) opt._squeezed = true; return opt; } }); (function(OPT) { OPT(AST_Node, function(self, compressor) { return self; }); AST_Node.DEFMETHOD("equivalent_to", function(node) { return this.TYPE == node.TYPE && this.print_to_string() == node.print_to_string(); }); AST_Scope.DEFMETHOD("process_expression", function(insert, compressor) { var self = this; var tt = new TreeTransformer(function(node) { if (insert && node instanceof AST_SimpleStatement) { return make_node(AST_Return, node, { value: node.body }); } if (!insert && node instanceof AST_Return) { if (compressor) { var value = node.value && node.value.drop_side_effect_free(compressor, true); return value ? make_node(AST_SimpleStatement, node, { body: value }) : make_node(AST_EmptyStatement, node); } return make_node(AST_SimpleStatement, node, { body: node.value || make_node(AST_UnaryPrefix, node, { operator: "void", expression: make_node(AST_Number, node, { value: 0 }) }) }); } if (node instanceof AST_Lambda && node !== self) { return node; } if (node instanceof AST_Block) { var index = node.body.length - 1; if (index >= 0) { node.body[index] = node.body[index].transform(tt); } } else if (node instanceof AST_If) { node.body = node.body.transform(tt); if (node.alternative) { node.alternative = node.alternative.transform(tt); } } else if (node instanceof AST_With) { node.body = node.body.transform(tt); } return node; }); self.transform(tt); }); function read_property(obj, node) { var key = node.getProperty(); if (key instanceof AST_Node) return; 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_Lambda) { if (key == "length") { obj.length_read = true; return make_node_from_constant(obj.argnames.length, obj); } } else if (obj instanceof AST_Object) { key = "" + key; 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_read_only_fn(value, name) { if (value instanceof AST_Boolean) return native_fns.Boolean[name]; if (value instanceof AST_Number) return native_fns.Number[name]; if (value instanceof AST_String) return native_fns.String[name]; if (name == "valueOf") return false; if (value instanceof AST_Array) return native_fns.Array[name]; if (value instanceof AST_Function) return native_fns.Function[name]; if (value instanceof AST_Object) return native_fns.Object[name]; if (value instanceof AST_RegExp) return native_fns.RegExp[name]; } function is_modified(compressor, tw, node, value, level, immutable) { var parent = tw.parent(level); if (compressor.option("unsafe") && parent instanceof AST_Dot && is_read_only_fn(value, parent.property)) { return; } var lhs = is_lhs(node, parent); if (lhs) return lhs; if (!immutable && parent instanceof AST_Call && parent.expression === node && !parent.is_expr_pure(compressor) && (!(value instanceof AST_Function) || !(parent instanceof AST_New) && value.contains_this())) { return true; } if (parent instanceof AST_Array) { return is_modified(compressor, tw, parent, parent, level + 1); } if (parent instanceof AST_ObjectKeyVal && node === parent.value) { var obj = tw.parent(level + 1); return is_modified(compressor, tw, obj, obj, level + 2); } if (parent instanceof AST_PropAccess && parent.expression === node) { var prop = read_property(value, parent); return !immutable && is_modified(compressor, tw, parent, prop, level + 1); } } function is_arguments(def) { if (def.name != "arguments") return false; var orig = def.orig; return orig.length == 1 && orig[0] instanceof AST_SymbolFunarg; } (function(def) { def(AST_Node, noop); function reset_def(tw, compressor, def) { def.assignments = 0; def.chained = false; def.direct_access = false; def.escaped = []; def.fixed = !def.scope.pinned() && !compressor.exposed(def) && !(def.init instanceof AST_Function && def.init !== def.scope) && def.init; if (def.fixed instanceof AST_Defun && !all(def.references, function(ref) { var scope = ref.scope; do { if (def.scope === scope) return true; } while (scope instanceof AST_Function && (scope = scope.parent_scope)); })) { tw.defun_ids[def.id] = false; } def.recursive_refs = 0; def.references = []; def.should_replace = undefined; def.single_use = undefined; } function reset_variables(tw, compressor, scope) { scope.variables.each(function(def) { reset_def(tw, compressor, def); if (def.fixed === null) { def.safe_ids = tw.safe_ids; mark(tw, def, true); } else if (def.fixed) { tw.loop_ids[def.id] = tw.in_loop; mark(tw, def, true); } }); scope.may_call_this = function() { scope.may_call_this = noop; if (!scope.contains_this()) return; scope.functions.each(function(def) { if (def.init instanceof AST_Defun && !(def.id in tw.defun_ids)) { tw.defun_ids[def.id] = false; } }); }; } function mark_defun(tw, def) { if (def.id in tw.defun_ids) { var marker = tw.defun_ids[def.id]; if (!marker) return; var visited = tw.defun_visited[def.id]; if (marker === tw.safe_ids) { if (!visited) return def.fixed; } else if (visited) { def.init.enclosed.forEach(function(d) { if (def.init.variables.get(d.name) === d) return; if (!safe_to_read(tw, d)) d.fixed = false; }); } else { tw.defun_ids[def.id] = false; } } else { if (!tw.in_loop) { tw.defun_ids[def.id] = tw.safe_ids; return def.fixed; } tw.defun_ids[def.id] = false; } } function walk_defuns(tw, scope) { scope.functions.each(function(def) { if (def.init instanceof AST_Defun && !tw.defun_visited[def.id]) { tw.defun_ids[def.id] = tw.safe_ids; def.init.walk(tw); } }); } function push(tw) { tw.safe_ids = Object.create(tw.safe_ids); } function pop(tw) { tw.safe_ids = Object.getPrototypeOf(tw.safe_ids); } function mark(tw, def, safe) { tw.safe_ids[def.id] = safe; } function safe_to_read(tw, def) { if (def.single_use == "m") return false; if (tw.safe_ids[def.id]) { if (def.fixed == null) { if (is_arguments(def)) return false; if (def.global && def.name == "arguments") return false; def.fixed = make_node(AST_Undefined, def.orig); } return true; } return def.fixed instanceof AST_Defun; } function safe_to_assign(tw, def, scope, value) { if (def.fixed === undefined) return true; if (def.fixed === null && def.safe_ids) { def.safe_ids[def.id] = false; delete def.safe_ids; return true; } if (!HOP(tw.safe_ids, def.id)) return false; if (!safe_to_read(tw, def)) return false; if (def.fixed === false) return false; if (def.fixed != null && (!value || def.references.length > def.assignments)) return false; if (def.fixed instanceof AST_Defun) { return value instanceof AST_Node && def.fixed.parent_scope === scope; } return all(def.orig, function(sym) { return !(sym instanceof AST_SymbolDefun || sym instanceof AST_SymbolLambda); }); } function ref_once(tw, compressor, def) { return compressor.option("unused") && !def.scope.pinned() && def.references.length - def.recursive_refs == 1 && tw.loop_ids[def.id] === tw.in_loop; } function is_immutable(value) { if (!value) return false; return value.is_constant() || value instanceof AST_Lambda || value instanceof AST_This; } function mark_escaped(tw, d, scope, node, value, level, depth) { var parent = tw.parent(level); if (value && value.is_constant()) return; if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right || parent instanceof AST_Call && (node !== parent.expression || parent instanceof AST_New) || parent instanceof AST_Exit && node === parent.value && node.scope !== d.scope || parent instanceof AST_VarDef && node === parent.value) { d.escaped.push(parent); if (depth > 1 && !(value && value.is_constant_expression(scope))) depth = 1; if (!d.escaped.depth || d.escaped.depth > depth) d.escaped.depth = depth; return; } else if (parent instanceof AST_Array || parent instanceof AST_Binary && lazy_op[parent.operator] || parent instanceof AST_Conditional && node !== parent.condition || parent instanceof AST_Sequence && node === parent.tail_node()) { mark_escaped(tw, d, scope, parent, parent, level + 1, depth); } else if (parent instanceof AST_ObjectKeyVal && node === parent.value) { var obj = tw.parent(level + 1); mark_escaped(tw, d, scope, obj, obj, level + 2, depth); } else if (parent instanceof AST_PropAccess && node === parent.expression) { value = read_property(value, parent); mark_escaped(tw, d, scope, parent, value, level + 1, depth + 1); if (value) return; } if (level > 0) return; if (parent instanceof AST_Call && node === parent.expression) return; if (parent instanceof AST_Sequence && node !== parent.tail_node()) return; if (parent instanceof AST_SimpleStatement) return; if (parent instanceof AST_Unary && !unary_side_effects[parent.operator]) return; d.direct_access = true; } function mark_assignment_to_arguments(node) { if (!(node instanceof AST_Sub)) return; var expr = node.expression; if (!(expr instanceof AST_SymbolRef)) return; var def = expr.definition(); if (is_arguments(def) && node.property instanceof AST_Number) def.reassigned = true; } var suppressor = new TreeWalker(function(node) { if (!(node instanceof AST_Symbol)) return; var d = node.definition(); if (!d) return; if (node instanceof AST_SymbolRef) d.references.push(node); d.fixed = false; }); def(AST_Accessor, function(tw, descend, compressor) { push(tw); reset_variables(tw, compressor, this); descend(); pop(tw); walk_defuns(tw, this); return true; }); def(AST_Assign, function(tw, descend, compressor) { var node = this; var sym = node.left; if (!(sym instanceof AST_SymbolRef)) { mark_assignment_to_arguments(sym); return; } if (sym.fixed) delete sym.fixed; var d = sym.definition(); var safe = safe_to_assign(tw, d, sym.scope, node.right); d.assignments++; var fixed = d.fixed; if (!fixed && node.operator != "=") return; var eq = node.operator == "="; var value = eq ? node.right : node; if (is_modified(compressor, tw, node, value, 0)) return; if (!eq) d.chained = true; sym.fixed = d.fixed = eq ? function() { return node.right; } : function() { return make_node(AST_Binary, node, { operator: node.operator.slice(0, -1), left: fixed instanceof AST_Node ? fixed : fixed(), right: node.right }); }; if (!safe) return; d.references.push(sym); mark(tw, d, false); node.right.walk(tw); mark(tw, d, true); if (eq) mark_escaped(tw, d, sym.scope, node, value, 0, 1); return true; }); def(AST_Binary, function(tw) { if (!lazy_op[this.operator]) return; this.left.walk(tw); push(tw); this.right.walk(tw); pop(tw); return true; }); def(AST_Call, function(tw, descend) { tw.find_parent(AST_Scope).may_call_this(); var exp = this.expression; if (!(exp instanceof AST_SymbolRef)) return; var def = exp.definition(); if (!(def.fixed instanceof AST_Defun)) return; var defun = mark_defun(tw, def); if (!defun) return; descend(); defun.walk(tw); return true; }); def(AST_Case, function(tw) { push(tw); this.expression.walk(tw); pop(tw); push(tw); walk_body(this, tw); pop(tw); return true; }); def(AST_Conditional, function(tw) { this.condition.walk(tw); push(tw); this.consequent.walk(tw); pop(tw); push(tw); this.alternative.walk(tw); pop(tw); return true; }); def(AST_Default, function(tw, descend) { push(tw); descend(); pop(tw); return true; }); def(AST_Defun, function(tw, descend, compressor) { var id = this.name.definition().id; if (tw.defun_visited[id]) return true; if (tw.defun_ids[id] !== tw.safe_ids) return true; tw.defun_visited[id] = true; this.inlined = false; push(tw); reset_variables(tw, compressor, this); descend(); pop(tw); walk_defuns(tw, this); return true; }); def(AST_Do, function(tw) { var saved_loop = tw.in_loop; tw.in_loop = this; push(tw); this.body.walk(tw); if (has_break_or_continue(this)) { pop(tw); push(tw); } this.condition.walk(tw); pop(tw); tw.in_loop = saved_loop; return true; }); def(AST_For, function(tw) { if (this.init) this.init.walk(tw); var saved_loop = tw.in_loop; tw.in_loop = this; push(tw); if (this.condition) this.condition.walk(tw); this.body.walk(tw); if (this.step) { if (has_break_or_continue(this)) { pop(tw); push(tw); } this.step.walk(tw); } pop(tw); tw.in_loop = saved_loop; return true; }); def(AST_ForIn, function(tw) { this.init.walk(suppressor); this.object.walk(tw); var saved_loop = tw.in_loop; tw.in_loop = this; push(tw); this.body.walk(tw); pop(tw); tw.in_loop = saved_loop; return true; }); def(AST_Function, function(tw, descend, compressor) { var node = this; node.inlined = false; push(tw); reset_variables(tw, compressor, node); var iife; if (!node.name && (iife = tw.parent()) instanceof AST_Call && iife.expression === node) { // Virtually turn IIFE parameters into variable definitions: // (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})() // So existing transformation rules can work on them. node.argnames.forEach(function(arg, i) { var d = arg.definition(); if (d.fixed === undefined && (!node.uses_arguments || tw.has_directive("use strict"))) { d.fixed = function() { return iife.args[i] || make_node(AST_Undefined, iife); }; tw.loop_ids[d.id] = tw.in_loop; mark(tw, d, true); } else { d.fixed = false; } }); } descend(); pop(tw); walk_defuns(tw, node); return true; }); def(AST_If, function(tw) { this.condition.walk(tw); push(tw); this.body.walk(tw); pop(tw); if (this.alternative) { push(tw); this.alternative.walk(tw); pop(tw); } return true; }); def(AST_LabeledStatement, function(tw) { push(tw); this.body.walk(tw); pop(tw); return true; }); def(AST_SymbolCatch, function() { this.definition().fixed = false; }); def(AST_SymbolRef, function(tw, descend, compressor) { if (this.fixed) delete this.fixed; var d = this.definition(); d.references.push(this); if (d.references.length == 1 && !d.fixed && d.orig[0] instanceof AST_SymbolDefun) { tw.loop_ids[d.id] = tw.in_loop; } var value; if (d.fixed === undefined || !safe_to_read(tw, d)) { d.fixed = false; } else if (d.fixed) { value = this.fixed_value(); if (recursive_ref(tw, d)) { d.recursive_refs++; } else if (value && ref_once(tw, compressor, d)) { d.single_use = value instanceof AST_Lambda && !value.pinned() || d.scope === this.scope && value.is_constant_expression(); } else { d.single_use = false; } if (is_modified(compressor, tw, this, value, 0, is_immutable(value))) { if (d.single_use) { d.single_use = "m"; } else { d.fixed = false; } } mark_escaped(tw, d, this.scope, this, value, 0, 1); } var parent; if (d.fixed instanceof AST_Defun && !((parent = tw.parent()) instanceof AST_Call && parent.expression === this)) { var defun = mark_defun(tw, d); if (defun) defun.walk(tw); } }); def(AST_Toplevel, function(tw, descend, compressor) { this.globals.each(function(def) { reset_def(tw, compressor, def); }); push(tw); reset_variables(tw, compressor, this); descend(); pop(tw); walk_defuns(tw, this); return true; }); def(AST_Try, function(tw) { push(tw); walk_body(this, tw); pop(tw); if (this.bcatch) { push(tw); this.bcatch.walk(tw); pop(tw); } if (this.bfinally) this.bfinally.walk(tw); return true; }); def(AST_Unary, function(tw, descend) { var node = this; if (!unary_arithmetic[node.operator]) return; var exp = node.expression; if (!(exp instanceof AST_SymbolRef)) { mark_assignment_to_arguments(exp); return; } if (exp.fixed) delete exp.fixed; var d = exp.definition(); var safe = safe_to_assign(tw, d, exp.scope, true); d.assignments++; var fixed = d.fixed; if (!fixed) return; d.chained = true; exp.fixed = d.fixed = function() { return make_node(AST_Binary, node, { operator: node.operator.slice(0, -1), left: make_node(AST_UnaryPrefix, node, { operator: "+", expression: fixed instanceof AST_Node ? fixed : fixed() }), right: make_node(AST_Number, node, { value: 1 }) }); }; if (!safe) return; d.references.push(exp); mark(tw, d, true); return true; }); def(AST_VarDef, function(tw, descend) { var node = this; var d = node.name.definition(); if (node.value) { if (safe_to_assign(tw, d, node.name.scope, node.value)) { d.fixed = function() { return node.value; }; tw.loop_ids[d.id] = tw.in_loop; mark(tw, d, false); descend(); mark(tw, d, true); return true; } else { d.fixed = false; } } }); def(AST_While, function(tw, descend) { var saved_loop = tw.in_loop; tw.in_loop = this; push(tw); descend(); pop(tw); tw.in_loop = saved_loop; return true; }); })(function(node, func) { node.DEFMETHOD("reduce_vars", func); }); AST_Toplevel.DEFMETHOD("reset_opt_flags", function(compressor) { var tw = new TreeWalker(compressor.option("reduce_vars") ? function(node, descend) { node._squeezed = false; node._optimized = false; return node.reduce_vars(tw, descend, compressor); } : function(node) { node._squeezed = false; node._optimized = false; }); // Flow control for visiting `AST_Defun`s tw.defun_ids = Object.create(null); tw.defun_visited = Object.create(null); // Record the loop body in which `AST_SymbolDeclaration` is first encountered tw.in_loop = null; tw.loop_ids = Object.create(null); // 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 tw.safe_ids = Object.create(null); this.walk(tw); }); AST_Symbol.DEFMETHOD("fixed_value", function(final) { var fixed = this.definition().fixed; if (!fixed) return fixed; if (!final && this.fixed) fixed = this.fixed; return fixed instanceof AST_Node ? fixed : fixed(); }); AST_SymbolRef.DEFMETHOD("is_immutable", function() { var def = this.definition(); if (def.orig.length != 1) return false; var sym = def.orig[0]; return sym instanceof AST_SymbolLambda && def.scope.name === sym; }); function is_lhs_read_only(lhs, compressor) { if (lhs instanceof AST_This) return true; if (lhs instanceof AST_SymbolRef) { var def = lhs.definition(); return def.lambda || compressor.exposed(def) && identifier_atom[def.name]; } if (lhs instanceof AST_PropAccess) { lhs = lhs.expression; if (lhs instanceof AST_SymbolRef) { if (lhs.is_immutable()) return false; lhs = lhs.fixed_value(); } if (!lhs) return true; if (lhs.is_constant()) return true; return is_lhs_read_only(lhs, compressor); } return false; } function find_variable(compressor, name) { var scope, i = 0; while (scope = compressor.parent(i++)) { if (scope instanceof AST_Scope) break; if (scope instanceof AST_Catch) { scope = scope.argname.definition().scope; break; } } return scope.find_variable(name); } function make_node(ctor, orig, props) { if (!props) props = {}; if (orig) { if (!props.start) props.start = orig.start; if (!props.end) props.end = orig.end; } return new ctor(props); } function make_sequence(orig, expressions) { if (expressions.length == 1) return expressions[0]; return make_node(AST_Sequence, orig, { expressions: expressions.reduce(merge_sequence, []) }); } function make_node_from_constant(val, orig) { switch (typeof val) { case "string": return make_node(AST_String, orig, { value: val }); case "number": if (isNaN(val)) return make_node(AST_NaN, orig); if (isFinite(val)) { return 1 / val < 0 ? make_node(AST_UnaryPrefix, orig, { operator: "-", expression: make_node(AST_Number, orig, { value: -val }) }) : make_node(AST_Number, orig, { value: val }); } return val < 0 ? make_node(AST_UnaryPrefix, orig, { operator: "-", expression: make_node(AST_Infinity, orig) }) : make_node(AST_Infinity, orig); case "boolean": return make_node(val ? AST_True : AST_False, orig); case "undefined": return make_node(AST_Undefined, orig); default: if (val === null) { return make_node(AST_Null, orig, { value: null }); } if (val instanceof RegExp) { return make_node(AST_RegExp, orig, { value: val }); } throw new Error(string_template("Can't handle constant of type: {type}", { type: typeof val })); } } function needs_unbinding(compressor, val) { return val instanceof AST_PropAccess || compressor.has_directive("use strict") && is_undeclared_ref(val) && val.name == "eval"; } // we shouldn't compress (1,func)(something) to // func(something) because that changes the meaning of // the func (becomes lexical instead of global). function maintain_this_binding(compressor, parent, orig, val) { if (parent instanceof AST_UnaryPrefix && parent.operator == "delete" || parent.TYPE == "Call" && parent.expression === orig && needs_unbinding(compressor, val)) { return make_sequence(orig, [ make_node(AST_Number, orig, { value: 0 }), val ]); } return val; } function merge_sequence(array, node) { if (node instanceof AST_Sequence) { array.push.apply(array, node.expressions); } else { array.push(node); } return array; } function as_statement_array(thing) { if (thing === null) return []; if (thing instanceof AST_BlockStatement) return thing.body; if (thing instanceof AST_EmptyStatement) return []; if (thing instanceof AST_Statement) return [ thing ]; throw new Error("Can't convert thing to statement array"); } function is_empty(thing) { if (thing === null) return true; if (thing instanceof AST_EmptyStatement) return true; if (thing instanceof AST_BlockStatement) return thing.body.length == 0; return false; } function loop_body(x) { if (x instanceof AST_IterationStatement) { return x.body instanceof AST_BlockStatement ? x.body : x; } return x; } function root_expr(prop) { while (prop instanceof AST_PropAccess) prop = prop.expression; return prop; } function is_iife_call(node) { if (node.TYPE != "Call") return false; return node.expression instanceof AST_Function || is_iife_call(node.expression); } function is_undeclared_ref(node) { return node instanceof AST_SymbolRef && node.definition().undeclared; } var global_names = makePredicate("Array Boolean clearInterval clearTimeout console Date decodeURI decodeURIComponent encodeURI encodeURIComponent Error escape eval EvalError Function isFinite isNaN JSON Math Number parseFloat parseInt RangeError ReferenceError RegExp Object setInterval setTimeout String SyntaxError TypeError unescape URIError"); AST_SymbolRef.DEFMETHOD("is_declared", function(compressor) { return !this.definition().undeclared || compressor.option("unsafe") && global_names[this.name]; }); var identifier_atom = makePredicate("Infinity NaN undefined"); function is_identifier_atom(node) { return node instanceof AST_Infinity || node instanceof AST_NaN || node instanceof AST_Undefined; } function tighten_body(statements, compressor) { var in_loop, in_try, scope; find_loop_scope_try(); var CHANGED, max_iter = 10; do { CHANGED = false; eliminate_spurious_blocks(statements); if (compressor.option("dead_code")) { eliminate_dead_code(statements, compressor); } if (compressor.option("if_return")) { handle_if_return(statements, compressor); } if (compressor.sequences_limit > 0) { sequencesize(statements, compressor); sequencesize_2(statements, compressor); } if (compressor.option("join_vars")) { join_consecutive_vars(statements); } if (compressor.option("collapse_vars")) { collapse(statements, compressor); } } while (CHANGED && max_iter-- > 0); function find_loop_scope_try() { var node = compressor.self(), level = 0; do { if (node instanceof AST_Catch || node instanceof AST_Finally) { level++; } else if (node instanceof AST_IterationStatement) { in_loop = true; } else if (node instanceof AST_Scope) { scope = node; break; } else if (node instanceof AST_Try) { in_try = true; } } while (node = compressor.parent(level++)); } // Search from right to left for assignment-like expressions: // - `var a = x;` // - `a = x;` // - `++a` // For each candidate, scan from left to right for first usage, then try // to fold assignment into the site for compression. // Will not attempt to collapse assignments into or past code blocks // which are not sequentially executed, e.g. loops and conditionals. function collapse(statements, compressor) { if (scope.pinned()) return statements; var args; var candidates = []; var stat_index = statements.length; var scanner = new TreeTransformer(function(node) { if (abort) return node; // Skip nodes before `candidate` as quickly as possible if (!hit) { if (node !== hit_stack[hit_index]) return node; hit_index++; if (hit_index < hit_stack.length) return handle_custom_scan_order(node); hit = true; stop_after = find_stop(node, 0); if (stop_after === node) abort = true; return node; } // Stop immediately if these node types are encountered var parent = scanner.parent(); if (should_stop(node, parent)) { abort = true; return node; } // Stop only if candidate is found within conditional branches if (!stop_if_hit && in_conditional(node, parent)) { stop_if_hit = parent; } // Replace variable with assignment when found var hit_rhs; if (can_replace && !(node instanceof AST_SymbolDeclaration) && (scan_lhs && lhs.equivalent_to(node) || scan_rhs && (hit_rhs = scan_rhs(node, this)))) { if (stop_if_hit && (hit_rhs || !lhs_local || !replace_all)) { abort = true; return node; } if (is_lhs(node, parent)) { if (value_def) replaced++; return node; } else { replaced++; if (value_def && candidate instanceof AST_VarDef) return node; } CHANGED = abort = true; AST_Node.info("Collapsing {name} [{file}:{line},{col}]", { name: node.print_to_string(), file: node.start.file, line: node.start.line, col: node.start.col }); if (candidate instanceof AST_UnaryPostfix) { return make_node(AST_UnaryPrefix, candidate, candidate); } if (candidate instanceof AST_VarDef) { var def = candidate.name.definition(); if (def.references.length - def.replaced == 1 && !compressor.exposed(def)) { def.replaced++; return maintain_this_binding(compressor, parent, node, candidate.value); } return make_node(AST_Assign, candidate, { operator: "=", left: make_node(AST_SymbolRef, candidate.name, candidate.name), right: candidate.value }); } candidate.write_only = false; return candidate; } // These node types have child nodes that execute sequentially, // but are otherwise not safe to scan into or beyond them. var sym; if (is_last_node(node, parent) || may_throw(node)) { stop_after = node; if (node instanceof AST_Scope) abort = true; } return handle_custom_scan_order(node); }, function(node) { if (abort) return; if (stop_after === node) abort = true; if (stop_if_hit === node) stop_if_hit = null; }); var multi_replacer = new TreeTransformer(function(node) { if (abort) return node; // Skip nodes before `candidate` as quickly as possible if (!hit) { if (node !== hit_stack[hit_index]) return node; hit_index++; if (hit_index < hit_stack.length) return; hit = true; return node; } // Replace variable when found if (node instanceof AST_SymbolRef && node.name == def.name) { if (!--replaced) abort = true; if (is_lhs(node, multi_replacer.parent())) return node; def.replaced++; value_def.replaced--; return candidate.value.clone(); } // Skip (non-executed) functions and (leading) default case in switch statements if (node instanceof AST_Default || node instanceof AST_Scope) return node; }); while (--stat_index >= 0) { // Treat parameters as collapsible in IIFE, i.e. // function(a, b){ ... }(x()); // would be translated into equivalent assignments: // var a = x(), b = undefined; if (stat_index == 0 && compressor.option("unused")) extract_args(); // Find collapsible assignments var hit_stack = []; extract_candidates(statements[stat_index]); while (candidates.length > 0) { hit_stack = candidates.pop(); var hit_index = 0; var candidate = hit_stack[hit_stack.length - 1]; var value_def = null; var stop_after = null; var stop_if_hit = null; var lhs = get_lhs(candidate); var side_effects = lhs && lhs.has_side_effects(compressor); var scan_lhs = lhs && !side_effects && !is_lhs_read_only(lhs, compressor); var scan_rhs = foldable(get_rhs(candidate)); if (!scan_lhs && !scan_rhs) continue; // Locate symbols which may execute code outside of scanning range var lvalues = get_lvalues(candidate); var lhs_local = is_lhs_local(lhs); if (!side_effects) side_effects = value_has_side_effects(candidate); var replace_all = replace_all_symbols(); var may_throw = candidate.may_throw(compressor) ? in_try ? function(node) { return node.has_side_effects(compressor); } : side_effects_external : return_false; var funarg = candidate.name instanceof AST_SymbolFunarg; var hit = funarg; var abort = false, replaced = 0, can_replace = !args || !hit; if (!can_replace) { for (var j = compressor.self().argnames.lastIndexOf(candidate.name) + 1; !abort && j < args.length; j++) { args[j].transform(scanner); } can_replace = true; } for (var i = stat_index; !abort && i < statements.length; i++) { statements[i].transform(scanner); } if (value_def) { var def = candidate.name.definition(); if (abort && def.references.length - def.replaced > replaced) replaced = false; else { abort = false; hit_index = 0; hit = funarg; for (var i = stat_index; !abort && i < statements.length; i++) { statements[i].transform(multi_replacer); } value_def.single_use = false; } } if (replaced && !remove_candidate(candidate)) statements.splice(stat_index, 1); } } function handle_custom_scan_order(node) { // Skip (non-executed) functions if (node instanceof AST_Scope) return node; // Scan case expressions first in a switch statement if (node instanceof AST_Switch) { node.expression = node.expression.transform(scanner); for (var i = 0; !abort && i < node.body.length; i++) { var branch = node.body[i]; if (branch instanceof AST_Case) { if (!hit) { if (branch !== hit_stack[hit_index]) continue; hit_index++; } branch.expression = branch.expression.transform(scanner); if (!replace_all) break; } } abort = true; return node; } } function should_stop(node, parent) { if (parent instanceof AST_For) return node !== parent.init; if (node instanceof AST_Assign) { return node.operator != "=" && lhs.equivalent_to(node.left); } if (node instanceof AST_Call) { return lhs instanceof AST_PropAccess && lhs.equivalent_to(node.expression); } if (node instanceof AST_Debugger) return true; if (node instanceof AST_Defun) return funarg && lhs.name === node.name.name; if (node instanceof AST_IterationStatement) return !(node instanceof AST_For); if (node instanceof AST_LoopControl) return true; if (node instanceof AST_Try) return true; if (node instanceof AST_With) return true; if (replace_all) return false; return node instanceof AST_SymbolRef && !node.is_declared(compressor) && !(parent instanceof AST_Assign && parent.left === node); } function in_conditional(node, parent) { if (parent instanceof AST_Binary) return lazy_op[parent.operator] && parent.left !== node; if (parent instanceof AST_Conditional) return parent.condition !== node; return parent instanceof AST_If && parent.condition !== node; } function is_last_node(node, parent) { if (node instanceof AST_Call) return true; if (node instanceof AST_Exit) { return side_effects || lhs instanceof AST_PropAccess || may_modify(lhs); } if (node instanceof AST_Function) { return compressor.option("ie8") && node.name && node.name.name in lvalues; } if (node instanceof AST_PropAccess) { return side_effects || node.expression.may_throw_on_access(compressor); } if (node instanceof AST_SymbolRef) { if (symbol_in_lvalues(node, parent)) { return !parent || parent.operator != "=" || parent.left !== node; } return side_effects && may_modify(node); } if (node instanceof AST_This) return symbol_in_lvalues(node, parent); if (node instanceof AST_VarDef) { if (!node.value) return false; return node.name.name in lvalues || side_effects && may_modify(node.name); } var sym = is_lhs(node.left, node); if (sym && sym.name in lvalues) return true; if (sym instanceof AST_PropAccess) return true; } function extract_args() { var iife, fn = compressor.self(); if (fn instanceof AST_Function && !fn.name && !fn.uses_arguments && !fn.pinned() && (iife = compressor.parent()) instanceof AST_Call && iife.expression === fn) { var fn_strict = compressor.has_directive("use strict"); if (fn_strict && !member(fn_strict, fn.body)) fn_strict = false; var len = fn.argnames.length; args = iife.args.slice(len); var names = Object.create(null); 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; if (!arg) { arg = make_node(AST_Undefined, sym).transform(compressor); } else if (arg instanceof AST_Lambda && arg.pinned()) { arg = null; } else { arg.walk(new TreeWalker(function(node) { if (!arg) return true; if (node instanceof AST_SymbolRef && fn.variables.has(node.name)) { var s = node.definition().scope; if (s !== scope) while (s = s.parent_scope) { if (s === scope) return true; } arg = null; } if (node instanceof AST_This && (fn_strict || !this.find_parent(AST_Scope))) { arg = null; return true; } })); } if (arg) candidates.unshift([ make_node(AST_VarDef, sym, { name: sym, value: arg }) ]); } } } function extract_candidates(expr) { hit_stack.push(expr); if (expr instanceof AST_Assign) { candidates.push(hit_stack.slice()); extract_candidates(expr.left); extract_candidates(expr.right); } else if (expr instanceof AST_Binary) { extract_candidates(expr.left); extract_candidates(expr.right); } else if (expr instanceof AST_Call) { extract_candidates(expr.expression); expr.args.forEach(extract_candidates); } else if (expr instanceof AST_Case) { extract_candidates(expr.expression); } else if (expr instanceof AST_Conditional) { extract_candidates(expr.condition); extract_candidates(expr.consequent); extract_candidates(expr.alternative); } else if (expr instanceof AST_Definitions) { expr.definitions.forEach(extract_candidates); } else if (expr instanceof AST_Dot) { extract_candidates(expr.expression); } else if (expr instanceof AST_DWLoop) { extract_candidates(expr.condition); if (!(expr.body instanceof AST_Block)) { extract_candidates(expr.body); } } else if (expr instanceof AST_Exit) { if (expr.value) extract_candidates(expr.value); } else if (expr instanceof AST_For) { if (expr.init) extract_candidates(expr.init); if (expr.condition) extract_candidates(expr.condition); if (expr.step) extract_candidates(expr.step); if (!(expr.body instanceof AST_Block)) { extract_candidates(expr.body); } } else if (expr instanceof AST_ForIn) { extract_candidates(expr.object); if (!(expr.body instanceof AST_Block)) { extract_candidates(expr.body); } } else if (expr instanceof AST_If) { extract_candidates(expr.condition); if (!(expr.body instanceof AST_Block)) { extract_candidates(expr.body); } if (expr.alternative && !(expr.alternative instanceof AST_Block)) { extract_candidates(expr.alternative); } } else if (expr instanceof AST_Sequence) { expr.expressions.forEach(extract_candidates); } else if (expr instanceof AST_SimpleStatement) { extract_candidates(expr.body); } else if (expr instanceof AST_Sub) { extract_candidates(expr.expression); extract_candidates(expr.property); } else if (expr instanceof AST_Switch) { extract_candidates(expr.expression); expr.body.forEach(extract_candidates); } else if (expr instanceof AST_Unary) { if (unary_arithmetic[expr.operator]) { candidates.push(hit_stack.slice()); } else { extract_candidates(expr.expression); } } else if (expr instanceof AST_VarDef) { if (expr.value) { var def = expr.name.definition(); if (def.references.length > def.replaced) { candidates.push(hit_stack.slice()); } extract_candidates(expr.value); } } hit_stack.pop(); } function find_stop(node, level, write_only) { var parent = scanner.parent(level); if (parent instanceof AST_Assign) { if (write_only && !(parent.left instanceof AST_PropAccess || parent.left.name in lvalues)) { return find_stop(parent, level + 1, write_only); } return node; } if (parent instanceof AST_Binary) { if (write_only && (!lazy_op[parent.operator] || parent.left === node)) { return find_stop(parent, level + 1, write_only); } return node; } if (parent instanceof AST_Call) return node; if (parent instanceof AST_Case) return node; if (parent instanceof AST_Conditional) { if (write_only && parent.condition === node) { return find_stop(parent, level + 1, write_only); } return node; } if (parent instanceof AST_Definitions) { return find_stop(parent, level + 1, true); } if (parent instanceof AST_Exit) { return write_only ? find_stop(parent, level + 1, write_only) : node; } if (parent instanceof AST_If) { if (write_only && parent.condition === node) { return find_stop(parent, level + 1, write_only); } return node; } if (parent instanceof AST_IterationStatement) return node; if (parent instanceof AST_PropAccess) return node; if (parent instanceof AST_Sequence) { return find_stop(parent, level + 1, parent.tail_node() !== node); } if (parent instanceof AST_SimpleStatement) { return find_stop(parent, level + 1, true); } if (parent instanceof AST_Switch) return node; if (parent instanceof AST_Unary) return node; if (parent instanceof AST_VarDef) return node; return null; } function mangleable_var(var_def) { var value = var_def.value; if (!(value instanceof AST_SymbolRef)) return; var def = value.definition(); if (def.undeclared) return; if (is_arguments(def)) return; return value_def = def; } function get_lhs(expr) { if (expr instanceof AST_VarDef) { var def = expr.name.definition(); if (!member(expr.name, def.orig)) return; var referenced = def.references.length - def.replaced; var declared = def.orig.length - def.eliminated; if (declared > 1 && !(expr.name instanceof AST_SymbolFunarg) || (referenced > 1 ? mangleable_var(expr) : !compressor.exposed(def))) { return make_node(AST_SymbolRef, expr.name, expr.name); } } else { return expr[expr instanceof AST_Assign ? "left" : "expression"]; } } function get_rhs(expr) { return candidate instanceof AST_Assign && candidate.operator == "=" && candidate.right; } function get_rvalue(expr) { return expr[expr instanceof AST_Assign ? "right" : "value"]; } function invariant(expr) { if (expr instanceof AST_Array) return false; if (expr instanceof AST_Binary && lazy_op[expr.operator]) { return invariant(expr.left) && invariant(expr.right); } if (expr instanceof AST_Call) return false; if (expr instanceof AST_Conditional) { return invariant(expr.consequent) && invariant(expr.alternative); } if (expr instanceof AST_Object) return false; return !expr.has_side_effects(compressor); } function foldable(expr) { if (!expr) return false; if (expr instanceof AST_SymbolRef) { var value = expr.evaluate(compressor); if (value === expr) return rhs_exact_match; return rhs_fuzzy_match(value, rhs_exact_match); } if (expr instanceof AST_This) return rhs_exact_match; if (expr.is_truthy()) return rhs_fuzzy_match(true, return_false); if (expr.is_constant()) { return rhs_fuzzy_match(expr.evaluate(compressor), rhs_exact_match); } if (!(lhs instanceof AST_SymbolRef)) return false; if (!invariant(expr)) return false; var circular; var def = lhs.definition(); expr.walk(new TreeWalker(function(node) { if (circular) return true; if (node instanceof AST_SymbolRef && node.definition() === def) { circular = true; } })); return !circular && rhs_exact_match; function rhs_exact_match(node) { return expr.equivalent_to(node); } } function rhs_fuzzy_match(value, fallback) { return function(node, tw) { if (tw.in_boolean_context()) { if (value && node.is_truthy() && !node.has_side_effects(compressor)) { return true; } if (node.is_constant()) { return !node.evaluate(compressor) == !value; } } return fallback(node); }; } function get_lvalues(expr) { var lvalues = Object.create(null); if (candidate instanceof AST_VarDef) { lvalues[candidate.name.name] = lhs; } var tw = new TreeWalker(function(node) { var value; if (node instanceof AST_SymbolRef) { value = node.fixed_value() || node; } else if (node instanceof AST_This) { value = node; } if (value && !lvalues[node.name]) { lvalues[node.name] = is_modified(compressor, tw, node, value, 0); } }); expr.walk(tw); return lvalues; } function remove_candidate(expr) { if (expr.name instanceof AST_SymbolFunarg) { var index = compressor.self().argnames.indexOf(expr.name); var args = compressor.parent().args; if (args[index]) args[index] = make_node(AST_Number, args[index], { value: 0 }); return true; } var found = false; return statements[stat_index].transform(new TreeTransformer(function(node, descend, in_list) { if (found) return node; if (node !== expr && node.body !== expr) return; if (node instanceof AST_VarDef) { found = true; node.value = null; return node; } if (in_list) { found = true; return MAP.skip; } if (!this.parent()) { found = true; return null; } }, function(node) { if (node instanceof AST_Sequence) switch (node.expressions.length) { case 0: return null; case 1: return node.expressions[0]; } })); } function is_lhs_local(lhs) { var sym = root_expr(lhs); return sym instanceof AST_SymbolRef && sym.definition().scope === scope && !(in_loop && (sym.name in lvalues && lvalues[sym.name] !== lhs || candidate instanceof AST_Unary || candidate instanceof AST_Assign && candidate.operator != "=")); } function value_has_side_effects(expr) { if (expr instanceof AST_Unary) return false; return get_rvalue(expr).has_side_effects(compressor); } function replace_all_symbols() { if (side_effects) return false; if (value_def) return true; if (lhs instanceof AST_SymbolRef) { var def = lhs.definition(); if (def.references.length - def.replaced == (candidate instanceof AST_VarDef ? 1 : 2)) { return true; } } return false; } function symbol_in_lvalues(sym, parent) { var lvalue = lvalues[sym.name]; if (!lvalue) return; if (lvalue !== lhs) return !(parent instanceof AST_Call && parent.expression === sym); scan_rhs = false; } function may_modify(sym) { var def = sym.definition(); if (def.orig.length == 1 && def.orig[0] instanceof AST_SymbolDefun) return false; if (def.scope !== scope) return true; return !all(def.references, function(ref) { return ref.scope.resolve() === scope; }); } function side_effects_external(node, lhs) { if (node instanceof AST_Assign) return side_effects_external(node.left, true); if (node instanceof AST_Unary) return side_effects_external(node.expression, true); if (node instanceof AST_VarDef) return node.value && side_effects_external(node.value); if (lhs) { if (node instanceof AST_Dot) return side_effects_external(node.expression, true); if (node instanceof AST_Sub) return side_effects_external(node.expression, true); if (node instanceof AST_SymbolRef) return node.definition().scope !== scope; } return false; } } function eliminate_spurious_blocks(statements) { var seen_dirs = []; for (var i = 0; i < statements.length;) { var stat = statements[i]; if (stat instanceof AST_BlockStatement) { CHANGED = true; eliminate_spurious_blocks(stat.body); [].splice.apply(statements, [i, 1].concat(stat.body)); i += stat.body.length; } else if (stat instanceof AST_EmptyStatement) { CHANGED = true; statements.splice(i, 1); } else if (stat instanceof AST_Directive) { if (!member(stat.value, seen_dirs)) { i++; seen_dirs.push(stat.value); } else { CHANGED = true; statements.splice(i, 1); } } else i++; } } function handle_if_return(statements, compressor) { var self = compressor.self(); var multiple_if_returns = has_multiple_if_returns(statements); var in_lambda = self instanceof AST_Lambda; for (var i = statements.length; --i >= 0;) { var stat = statements[i]; var j = next_index(i); var next = statements[j]; if (in_lambda && !next && stat instanceof AST_Return) { if (!stat.value) { CHANGED = true; statements.splice(i, 1); continue; } if (stat.value instanceof AST_UnaryPrefix && stat.value.operator == "void") { CHANGED = true; statements[i] = make_node(AST_SimpleStatement, stat, { body: stat.value.expression }); continue; } } if (stat instanceof AST_If) { var ab = aborts(stat.body); if (can_merge_flow(ab)) { if (ab.label) { remove(ab.label.thedef.references, ab); } CHANGED = true; stat = stat.clone(); stat.condition = stat.condition.negate(compressor); var body = as_statement_array_with_return(stat.body, ab); stat.body = make_node(AST_BlockStatement, stat, { body: as_statement_array(stat.alternative).concat(extract_functions()) }); stat.alternative = make_node(AST_BlockStatement, stat, { body: body }); statements[i] = stat.transform(compressor); continue; } if (ab && !stat.alternative && stat.body instanceof AST_BlockStatement && next instanceof AST_Jump) { var negated = stat.condition.negate(compressor); if (negated.print_to_string().length <= stat.condition.print_to_string().length) { CHANGED = true; stat = stat.clone(); stat.condition = negated; statements[j] = stat.body; stat.body = next; statements[i] = stat.transform(compressor); continue; } } var ab = aborts(stat.alternative); if (can_merge_flow(ab)) { if (ab.label) { remove(ab.label.thedef.references, ab); } CHANGED = true; stat = stat.clone(); stat.body = make_node(AST_BlockStatement, stat.body, { body: as_statement_array(stat.body).concat(extract_functions()) }); var body = as_statement_array_with_return(stat.alternative, ab); stat.alternative = make_node(AST_BlockStatement, stat.alternative, { body: body }); statements[i] = stat.transform(compressor); continue; } } if (stat instanceof AST_If && stat.body instanceof AST_Return) { var value = stat.body.value; //--- // pretty silly case, but: // if (foo()) return; return; ==> foo(); return; if (!value && !stat.alternative && (in_lambda && !next || next instanceof AST_Return && !next.value)) { CHANGED = true; statements[i] = make_node(AST_SimpleStatement, stat.condition, { body: stat.condition }); continue; } //--- // if (foo()) return x; return y; ==> return foo() ? x : y; if (value && !stat.alternative && next instanceof AST_Return && next.value) { CHANGED = true; stat = stat.clone(); stat.alternative = next; statements.splice(i, 1, stat.transform(compressor)); statements.splice(j, 1); continue; } //--- // if (foo()) return x; [ return ; ] ==> return foo() ? x : undefined; if (value && !stat.alternative && (!next && in_lambda && multiple_if_returns || next instanceof AST_Return)) { CHANGED = true; stat = stat.clone(); stat.alternative = next || make_node(AST_Return, stat, { value: null }); statements.splice(i, 1, stat.transform(compressor)); if (next) statements.splice(j, 1); continue; } //--- // if (a) return b; if (c) return d; e; ==> return a ? b : c ? d : void e; // // if sequences is not enabled, this can lead to an endless loop (issue #866). // however, with sequences on this helps producing slightly better output for // the example code. var prev = statements[prev_index(i)]; if (compressor.option("sequences") && in_lambda && !stat.alternative && prev instanceof AST_If && prev.body instanceof AST_Return && next_index(j) == statements.length && next instanceof AST_SimpleStatement) { CHANGED = true; stat = stat.clone(); stat.alternative = make_node(AST_BlockStatement, next, { body: [ next, make_node(AST_Return, next, { value: null }) ] }); statements.splice(i, 1, stat.transform(compressor)); statements.splice(j, 1); continue; } } } function has_multiple_if_returns(statements) { var n = 0; for (var i = statements.length; --i >= 0;) { var stat = statements[i]; if (stat instanceof AST_If && stat.body instanceof AST_Return) { if (++n > 1) return true; } } return false; } function is_return_void(value) { return !value || value instanceof AST_UnaryPrefix && value.operator == "void"; } function can_merge_flow(ab) { if (!ab) return false; var lct = ab instanceof AST_LoopControl ? compressor.loopcontrol_target(ab) : null; return ab instanceof AST_Return && in_lambda && is_return_void(ab.value) || ab instanceof AST_Continue && self === loop_body(lct) || ab instanceof AST_Break && lct instanceof AST_BlockStatement && self === lct; } function extract_functions() { var tail = statements.slice(i + 1); statements.length = i + 1; return tail.filter(function(stat) { if (stat instanceof AST_Defun) { statements.push(stat); return false; } return true; }); } function as_statement_array_with_return(node, ab) { var body = as_statement_array(node).slice(0, -1); if (ab.value) { body.push(make_node(AST_SimpleStatement, ab.value, { body: ab.value.expression })); } return body; } function next_index(i) { for (var j = i + 1; j < statements.length; j++) { var stat = statements[j]; if (!(stat instanceof AST_Var && declarations_only(stat))) { break; } } return j; } function prev_index(i) { for (var j = i; --j >= 0;) { var stat = statements[j]; if (!(stat instanceof AST_Var && declarations_only(stat))) { break; } } return j; } } function eliminate_dead_code(statements, compressor) { var has_quit; var self = compressor.self(); for (var i = 0, n = 0, len = statements.length; i < len; i++) { var stat = statements[i]; if (stat instanceof AST_LoopControl) { var lct = compressor.loopcontrol_target(stat); if (stat instanceof AST_Break && !(lct instanceof AST_IterationStatement) && loop_body(lct) === self || stat instanceof AST_Continue && loop_body(lct) === self) { if (stat.label) { remove(stat.label.thedef.references, stat); } } else { statements[n++] = stat; } } else { statements[n++] = stat; } if (aborts(stat)) { has_quit = statements.slice(i + 1); break; } } statements.length = n; CHANGED = n != len; if (has_quit) has_quit.forEach(function(stat) { extract_declarations_from_unreachable_code(stat, statements); }); } function declarations_only(node) { return all(node.definitions, function(var_def) { return !var_def.value; }); } function sequencesize(statements, compressor) { if (statements.length < 2) return; var seq = [], n = 0; function push_seq() { if (!seq.length) return; var body = make_sequence(seq[0], seq); statements[n++] = make_node(AST_SimpleStatement, body, { body: body }); seq = []; } for (var i = 0, len = statements.length; i < len; i++) { var stat = statements[i]; if (stat instanceof AST_SimpleStatement) { if (seq.length >= compressor.sequences_limit) push_seq(); var body = stat.body; if (seq.length > 0) body = body.drop_side_effect_free(compressor); if (body) merge_sequence(seq, body); } else if (stat instanceof AST_Definitions && declarations_only(stat) || stat instanceof AST_Defun) { statements[n++] = stat; } else { push_seq(); statements[n++] = stat; } } push_seq(); statements.length = n; if (n != len) CHANGED = true; } function to_simple_statement(block, decls) { if (!(block instanceof AST_BlockStatement)) return block; var stat = null; for (var i = 0; i < block.body.length; i++) { var line = block.body[i]; if (line instanceof AST_Var && declarations_only(line)) { decls.push(line); } else if (stat) { return false; } else { stat = line; } } return stat; } function sequencesize_2(statements, compressor) { function cons_seq(right) { n--; CHANGED = true; var left = prev.body; return make_sequence(left, [ left, right ]); } var n = 0, prev; for (var i = 0; i < statements.length; i++) { var stat = statements[i]; if (prev) { if (stat instanceof AST_Exit) { stat.value = cons_seq(stat.value || make_node(AST_Undefined, stat)).transform(compressor); } else if (stat instanceof AST_For) { if (!(stat.init instanceof AST_Definitions)) { var abort = false; prev.body.walk(new TreeWalker(function(node) { if (abort || node instanceof AST_Scope) return true; if (node instanceof AST_Binary && node.operator == "in") { abort = true; return true; } })); if (!abort) { if (stat.init) stat.init = cons_seq(stat.init); else { stat.init = prev.body; n--; CHANGED = true; } } } } else if (stat instanceof AST_ForIn) { stat.object = cons_seq(stat.object); } else if (stat instanceof AST_If) { stat.condition = cons_seq(stat.condition); } else if (stat instanceof AST_Switch) { stat.expression = cons_seq(stat.expression); } else if (stat instanceof AST_With) { stat.expression = cons_seq(stat.expression); } } if (compressor.option("conditionals") && stat instanceof AST_If) { var decls = []; var body = to_simple_statement(stat.body, decls); var alt = to_simple_statement(stat.alternative, decls); if (body !== false && alt !== false && decls.length > 0) { var len = decls.length; decls.push(make_node(AST_If, stat, { condition: stat.condition, body: body || make_node(AST_EmptyStatement, stat.body), alternative: alt })); decls.unshift(n, 1); [].splice.apply(statements, decls); i += len; n += len + 1; prev = null; CHANGED = true; continue; } } statements[n++] = stat; prev = stat instanceof AST_SimpleStatement ? stat : null; } statements.length = n; } function join_assigns(defn, body) { var exprs; if (body instanceof AST_Assign) { exprs = [ body ]; } else if (body instanceof AST_Sequence) { exprs = body.expressions.slice(); } if (!exprs) return; if (defn instanceof AST_Definitions) { var def = defn.definitions[defn.definitions.length - 1]; if (trim_assigns(def.name, def.value, exprs)) return exprs; } for (var i = exprs.length - 1; --i >= 0;) { var expr = exprs[i]; if (!(expr instanceof AST_Assign)) continue; if (expr.operator != "=") continue; if (!(expr.left instanceof AST_SymbolRef)) continue; var tail = exprs.slice(i + 1); if (!trim_assigns(expr.left, expr.right, tail)) continue; return exprs.slice(0, i + 1).concat(tail); } } function trim_assigns(name, value, exprs) { if (!(value instanceof AST_Object)) return; var trimmed = false; do { var node = exprs[0]; if (!(node instanceof AST_Assign)) break; if (node.operator != "=") break; if (!(node.left instanceof AST_PropAccess)) break; var sym = node.left.expression; if (!(sym instanceof AST_SymbolRef)) break; if (name.name != sym.name) break; if (!node.right.is_constant_expression(scope)) break; var prop = node.left.property; if (prop instanceof AST_Node) { prop = prop.evaluate(compressor); } if (prop instanceof AST_Node) break; prop = "" + prop; var diff = compressor.has_directive("use strict") ? function(node) { return node.key != prop && node.key.name != prop; } : function(node) { return node.key.name != prop; }; if (!all(value.properties, diff)) break; value.properties.push(make_node(AST_ObjectKeyVal, node, { key: prop, value: node.right })); exprs.shift(); trimmed = true; } while (exprs.length); return trimmed; } function join_consecutive_vars(statements) { var defs; for (var i = 0, j = -1; i < statements.length; i++) { var stat = statements[i]; var prev = statements[j]; if (stat instanceof AST_Definitions) { if (prev && prev.TYPE == stat.TYPE) { prev.definitions = prev.definitions.concat(stat.definitions); CHANGED = true; } else if (defs && defs.TYPE == stat.TYPE && declarations_only(stat)) { defs.definitions = defs.definitions.concat(stat.definitions); CHANGED = true; } else { statements[++j] = stat; defs = stat; } } else if (stat instanceof AST_Exit) { stat.value = join_assigns_expr(stat.value); } else if (stat instanceof AST_For) { var exprs = join_assigns(prev, stat.init); if (exprs) { CHANGED = true; stat.init = exprs.length ? make_sequence(stat.init, exprs) : null; statements[++j] = stat; } else if (prev instanceof AST_Var && (!stat.init || stat.init.TYPE == prev.TYPE)) { if (stat.init) { prev.definitions = prev.definitions.concat(stat.init.definitions); } stat.init = prev; statements[j] = stat; CHANGED = true; } else if (defs && stat.init && defs.TYPE == stat.init.TYPE && declarations_only(stat.init)) { defs.definitions = defs.definitions.concat(stat.init.definitions); stat.init = null; statements[++j] = stat; CHANGED = true; } else { statements[++j] = stat; } } else if (stat instanceof AST_ForIn) { stat.object = join_assigns_expr(stat.object); } else if (stat instanceof AST_If) { stat.condition = join_assigns_expr(stat.condition); } else if (stat instanceof AST_SimpleStatement) { var exprs = join_assigns(prev, stat.body); if (exprs) { CHANGED = true; if (!exprs.length) continue; stat.body = make_sequence(stat.body, exprs); } statements[++j] = stat; } else if (stat instanceof AST_Switch) { stat.expression = join_assigns_expr(stat.expression); } else if (stat instanceof AST_With) { stat.expression = join_assigns_expr(stat.expression); } else { statements[++j] = stat; } } statements.length = j + 1; function join_assigns_expr(value) { statements[++j] = stat; var exprs = join_assigns(prev, value); if (!exprs) return value; CHANGED = true; var tail = value.tail_node(); if (exprs[exprs.length - 1] !== tail) exprs.push(tail.left); return make_sequence(value, exprs); } } } function extract_declarations_from_unreachable_code(stat, target) { if (!(stat instanceof AST_Defun)) { AST_Node.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start); } stat.walk(new TreeWalker(function(node) { if (node instanceof AST_Definitions) { AST_Node.warn("Declarations in unreachable code! [{file}:{line},{col}]", node.start); node.remove_initializers(); target.push(node); return true; } if (node instanceof AST_Defun) { target.push(node); return true; } if (node instanceof AST_Scope) { return true; } })); } function is_undefined(node, compressor) { return node.is_undefined || node instanceof AST_Undefined || node instanceof AST_UnaryPrefix && node.operator == "void" && !node.expression.has_side_effects(compressor); } // is_truthy() // return true if `!!node === true` (function(def) { def(AST_Node, return_false); def(AST_Array, return_true); def(AST_Assign, function() { return this.operator == "=" && this.right.is_truthy(); }); def(AST_Lambda, return_true); def(AST_Object, return_true); def(AST_RegExp, return_true); def(AST_Sequence, function() { return this.tail_node().is_truthy(); }); def(AST_SymbolRef, function() { var fixed = this.fixed_value(); if (!fixed) return false; this.is_truthy = return_false; var result = fixed.is_truthy(); delete this.is_truthy; return result; }); })(function(node, func) { node.DEFMETHOD("is_truthy", func); }); // may_throw_on_access() // returns true if this node may be null, undefined or contain `AST_Accessor` (function(def) { AST_Node.DEFMETHOD("may_throw_on_access", function(compressor) { return !compressor.option("pure_getters") || this._dot_throw(compressor); }); function is_strict(compressor) { return /strict/.test(compressor.option("pure_getters")); } def(AST_Node, is_strict); def(AST_Array, return_false); def(AST_Assign, function(compressor) { if (this.operator != "=") return false; var rhs = this.right; if (!rhs._dot_throw(compressor)) return false; var sym = this.left; if (!(sym instanceof AST_SymbolRef)) return true; if (rhs instanceof AST_Binary && rhs.operator == "||" && sym.name == rhs.left.name) { return rhs.right._dot_throw(compressor); } return true; }); def(AST_Binary, function(compressor) { switch (this.operator) { case "&&": return this.left._dot_throw(compressor) || this.right._dot_throw(compressor); case "||": return this.right._dot_throw(compressor); default: return false; } }); def(AST_Conditional, function(compressor) { return this.consequent._dot_throw(compressor) || this.alternative._dot_throw(compressor); }); def(AST_Constant, return_false); def(AST_Dot, function(compressor) { if (!is_strict(compressor)) return false; var exp = this.expression; if (exp instanceof AST_SymbolRef) exp = exp.fixed_value(); return !(exp instanceof AST_Lambda && this.property == "prototype"); }); def(AST_Lambda, return_false); def(AST_Null, return_true); def(AST_Object, function(compressor) { if (!is_strict(compressor)) return false; for (var i = this.properties.length; --i >=0;) if (this.properties[i].value instanceof AST_Accessor) return true; return false; }); def(AST_Sequence, function(compressor) { return this.tail_node()._dot_throw(compressor); }); def(AST_SymbolRef, function(compressor) { if (this.is_undefined) return true; if (!is_strict(compressor)) return false; if (is_undeclared_ref(this) && this.is_declared(compressor)) return false; if (this.is_immutable()) return false; if (is_arguments(this.definition())) return false; var fixed = this.fixed_value(); if (!fixed) return true; this._dot_throw = return_true; var result = fixed._dot_throw(compressor); delete this._dot_throw; return result; }); def(AST_UnaryPrefix, function() { return this.operator == "void"; }); def(AST_UnaryPostfix, return_false); def(AST_Undefined, return_true); })(function(node, func) { node.DEFMETHOD("_dot_throw", func); }); (function(def) { def(AST_Node, return_false); def(AST_Array, return_true); def(AST_Assign, function(compressor) { return this.operator != "=" || this.right.is_defined(compressor); }); def(AST_Binary, function(compressor) { switch (this.operator) { case "&&": return this.left.is_defined(compressor) && this.right.is_defined(compressor); case "||": return this.left.is_truthy() || this.right.is_defined(compressor); default: return true; } }); def(AST_Conditional, function(compressor) { return this.consequent.is_defined(compressor) && this.alternative.is_defined(compressor); }); def(AST_Constant, return_true); def(AST_Lambda, return_true); def(AST_Object, return_true); def(AST_Sequence, function(compressor) { return this.tail_node().is_defined(compressor); }); def(AST_SymbolRef, function(compressor) { if (this.is_undefined) return false; if (is_undeclared_ref(this) && this.is_declared(compressor)) return true; if (this.is_immutable()) return true; var fixed = this.fixed_value(); if (!fixed) return false; this.is_defined = return_false; var result = fixed.is_defined(compressor); delete this.is_defined; return result; }); def(AST_UnaryPrefix, function() { return this.operator != "void"; }); def(AST_UnaryPostfix, return_true); def(AST_Undefined, return_false); })(function(node, func) { node.DEFMETHOD("is_defined", func); }); /* -----[ boolean/negation helpers ]----- */ // methods to determine whether an expression has a boolean result type (function(def) { def(AST_Node, return_false); def(AST_Assign, function(compressor) { return this.operator == "=" && this.right.is_boolean(compressor); }); var binary = makePredicate("in instanceof == != === !== < <= >= >"); def(AST_Binary, function(compressor) { return binary[this.operator] || lazy_op[this.operator] && this.left.is_boolean(compressor) && this.right.is_boolean(compressor); }); def(AST_Boolean, return_true); var fn = makePredicate("every hasOwnProperty isPrototypeOf propertyIsEnumerable some"); def(AST_Call, function(compressor) { if (!compressor.option("unsafe")) return false; var exp = this.expression; return exp instanceof AST_Dot && (fn[exp.property] || exp.property == "test" && exp.expression instanceof AST_RegExp); }); def(AST_Conditional, function(compressor) { return this.consequent.is_boolean(compressor) && this.alternative.is_boolean(compressor); }); def(AST_New, return_false); def(AST_Sequence, function(compressor) { return this.tail_node().is_boolean(compressor); }); def(AST_SymbolRef, function(compressor) { var fixed = this.fixed_value(); if (!fixed) return false; this.is_boolean = return_false; var result = fixed.is_boolean(compressor); delete this.is_boolean; return result; }); var unary = makePredicate("! delete"); def(AST_UnaryPrefix, function() { return unary[this.operator]; }); })(function(node, func) { node.DEFMETHOD("is_boolean", func); }); // methods to determine if an expression has a numeric result type (function(def) { def(AST_Node, return_false); var binary = makePredicate("- * / % & | ^ << >> >>>"); def(AST_Assign, function(compressor) { return binary[this.operator.slice(0, -1)] || this.operator == "=" && this.right.is_number(compressor); }); def(AST_Binary, function(compressor) { return binary[this.operator] || this.operator == "+" && this.left.is_number(compressor) && this.right.is_number(compressor); }); var fn = makePredicate([ "charCodeAt", "getDate", "getDay", "getFullYear", "getHours", "getMilliseconds", "getMinutes", "getMonth", "getSeconds", "getTime", "getTimezoneOffset", "getUTCDate", "getUTCDay", "getUTCFullYear", "getUTCHours", "getUTCMilliseconds", "getUTCMinutes", "getUTCMonth", "getUTCSeconds", "getYear", "indexOf", "lastIndexOf", "localeCompare", "push", "search", "setDate", "setFullYear", "setHours", "setMilliseconds", "setMinutes", "setMonth", "setSeconds", "setTime", "setUTCDate", "setUTCFullYear", "setUTCHours", "setUTCMilliseconds", "setUTCMinutes", "setUTCMonth", "setUTCSeconds", "setYear", "toExponential", "toFixed", "toPrecision", ]); def(AST_Call, function(compressor) { if (!compressor.option("unsafe")) return false; var exp = this.expression; return exp instanceof AST_Dot && (fn[exp.property] || is_undeclared_ref(exp.expression) && exp.expression.name == "Math"); }); def(AST_Conditional, function(compressor) { return this.consequent.is_number(compressor) && this.alternative.is_number(compressor); }); def(AST_New, return_false); def(AST_Number, return_true); def(AST_Sequence, function(compressor) { return this.tail_node().is_number(compressor); }); def(AST_SymbolRef, function(compressor) { var fixed = this.fixed_value(); if (!fixed) return false; this.is_number = return_false; var result = fixed.is_number(compressor); delete this.is_number; return result; }); var unary = makePredicate("+ - ~ ++ --"); def(AST_Unary, function() { return unary[this.operator]; }); })(function(node, func) { node.DEFMETHOD("is_number", func); }); // methods to determine if an expression has a string result type (function(def) { def(AST_Node, return_false); def(AST_Assign, function(compressor) { return (this.operator == "=" || this.operator == "+=") && this.right.is_string(compressor); }); def(AST_Binary, function(compressor) { return this.operator == "+" && (this.left.is_string(compressor) || this.right.is_string(compressor)); }); var fn = makePredicate([ "charAt", "substr", "substring", "toLowerCase", "toString", "toUpperCase", "trim", ]); def(AST_Call, function(compressor) { if (!compressor.option("unsafe")) return false; var exp = this.expression; return exp instanceof AST_Dot && fn[exp.property]; }); def(AST_Conditional, function(compressor) { return this.consequent.is_string(compressor) && this.alternative.is_string(compressor); }); def(AST_Sequence, function(compressor) { return this.tail_node().is_string(compressor); }); def(AST_String, return_true); def(AST_SymbolRef, function(compressor) { var fixed = this.fixed_value(); if (!fixed) return false; this.is_string = return_false; var result = fixed.is_string(compressor); delete this.is_string; return result; }); def(AST_UnaryPrefix, function() { return this.operator == "typeof"; }); })(function(node, func) { node.DEFMETHOD("is_string", func); }); var lazy_op = makePredicate("&& ||"); var unary_arithmetic = makePredicate("++ --"); var unary_side_effects = makePredicate("delete ++ --"); function is_lhs(node, parent) { if (parent instanceof AST_Unary && unary_side_effects[parent.operator]) return parent.expression; if (parent instanceof AST_Assign && parent.left === node) return node; } (function(def) { function to_node(value, orig) { if (value instanceof AST_Node) return make_node(value.CTOR, orig, value); if (Array.isArray(value)) return make_node(AST_Array, orig, { elements: value.map(function(value) { return to_node(value, orig); }) }); if (value && typeof value == "object") { var props = []; for (var key in value) if (HOP(value, key)) { props.push(make_node(AST_ObjectKeyVal, orig, { key: key, value: to_node(value[key], orig) })); } return make_node(AST_Object, orig, { properties: props }); } return make_node_from_constant(value, orig); } function warn(node) { AST_Node.warn("global_defs " + node.print_to_string() + " redefined [{file}:{line},{col}]", node.start); } AST_Toplevel.DEFMETHOD("resolve_defines", function(compressor) { if (!compressor.option("global_defs")) return this; this.figure_out_scope({ ie8: compressor.option("ie8") }); return this.transform(new TreeTransformer(function(node) { var def = node._find_defs(compressor, ""); if (!def) return; var level = 0, child = node, parent; while (parent = this.parent(level++)) { if (!(parent instanceof AST_PropAccess)) break; if (parent.expression !== child) break; child = parent; } if (is_lhs(child, parent)) { warn(node); return; } return def; })); }); def(AST_Node, noop); def(AST_Dot, function(compressor, suffix) { return this.expression._find_defs(compressor, "." + this.property + suffix); }); def(AST_SymbolDeclaration, function(compressor) { if (!this.global()) return; if (HOP(compressor.option("global_defs"), this.name)) warn(this); }); def(AST_SymbolRef, function(compressor, suffix) { if (!this.global()) return; var defines = compressor.option("global_defs"); var name = this.name + suffix; if (HOP(defines, name)) return to_node(defines[name], this); }); })(function(node, func) { node.DEFMETHOD("_find_defs", func); }); function best_of_expression(ast1, ast2) { return ast1.print_to_string().length > ast2.print_to_string().length ? ast2 : ast1; } function best_of_statement(ast1, ast2) { return best_of_expression(make_node(AST_SimpleStatement, ast1, { body: ast1 }), make_node(AST_SimpleStatement, ast2, { body: ast2 })).body; } function best_of(compressor, ast1, ast2) { return (first_in_statement(compressor) ? best_of_statement : best_of_expression)(ast1, ast2); } function convert_to_predicate(obj) { Object.keys(obj).forEach(function(key) { obj[key] = makePredicate(obj[key]); }); } var object_fns = [ "constructor", "toString", "valueOf", ]; var native_fns = { Array: [ "indexOf", "join", "lastIndexOf", "slice", ].concat(object_fns), Boolean: object_fns, Function: object_fns, Number: [ "toExponential", "toFixed", "toPrecision", ].concat(object_fns), Object: object_fns, RegExp: [ "test", ].concat(object_fns), String: [ "charAt", "charCodeAt", "concat", "indexOf", "italics", "lastIndexOf", "match", "replace", "search", "slice", "split", "substr", "substring", "toLowerCase", "toUpperCase", "trim", ].concat(object_fns), }; convert_to_predicate(native_fns); var static_fns = { Array: [ "isArray", ], Math: [ "abs", "acos", "asin", "atan", "ceil", "cos", "exp", "floor", "log", "round", "sin", "sqrt", "tan", "atan2", "pow", "max", "min", ], Number: [ "isFinite", "isNaN", ], Object: [ "create", "getOwnPropertyDescriptor", "getOwnPropertyNames", "getPrototypeOf", "isExtensible", "isFrozen", "isSealed", "keys", ], String: [ "fromCharCode", ], }; convert_to_predicate(static_fns); // methods to evaluate a constant expression (function(def) { // If the node has been successfully reduced to a constant, // then its value is returned; otherwise the element itself // is returned. // They can be distinguished as constant value is never a // descendant of AST_Node. AST_Node.DEFMETHOD("evaluate", function(compressor) { if (!compressor.option("evaluate")) return this; var cached = []; var val = this._eval(compressor, cached, 1); cached.forEach(function(node) { delete node._eval; }); if (!val || val instanceof RegExp) return val; if (typeof val == "function" || typeof val == "object") return this; return val; }); var unaryPrefix = makePredicate("! ~ - + void"); AST_Node.DEFMETHOD("is_constant", function() { // Accomodate when compress option evaluate=false // as well as the common constant expressions !0 and -1 if (this instanceof AST_Constant) { return !(this instanceof AST_RegExp); } else { return this instanceof AST_UnaryPrefix && this.expression instanceof AST_Constant && unaryPrefix[this.operator]; } }); def(AST_Statement, function() { throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start)); }); def(AST_Lambda, return_this); def(AST_Node, return_this); def(AST_Constant, function() { return this.getValue(); }); def(AST_Function, function(compressor) { if (compressor.option("unsafe")) { var fn = function() {}; fn.node = this; fn.toString = function() { return "function(){}"; }; return fn; } return this; }); def(AST_Array, function(compressor, cached, depth) { if (compressor.option("unsafe")) { var elements = []; for (var i = 0; i < this.elements.length; i++) { var element = this.elements[i]; var value = element._eval(compressor, cached, depth); if (element === value) return this; elements.push(value); } return elements; } return this; }); def(AST_Object, function(compressor, cached, depth) { if (compressor.option("unsafe")) { var val = {}; for (var i = 0; i < this.properties.length; i++) { var prop = this.properties[i]; var key = prop.key; if (key instanceof AST_Symbol) { key = key.name; } else if (key instanceof AST_Node) { key = key._eval(compressor, cached, depth); if (key === prop.key) return this; } if (typeof Object.prototype[key] === 'function') { return this; } if (prop.value instanceof AST_Function) continue; val[key] = prop.value._eval(compressor, cached, depth); if (val[key] === prop.value) return this; } return val; } return this; }); var non_converting_unary = makePredicate("! typeof void"); def(AST_UnaryPrefix, function(compressor, cached, depth) { var e = this.expression; // Function would be evaluated to an array and so typeof would // incorrectly return 'object'. Hence making is a special case. if (compressor.option("typeofs") && this.operator == "typeof" && (e instanceof AST_Lambda || e instanceof AST_SymbolRef && e.fixed_value() instanceof AST_Lambda)) { return typeof function(){}; } if (!non_converting_unary[this.operator]) depth++; var v = e._eval(compressor, cached, depth); if (v === this.expression) return this; switch (this.operator) { case "!": return !v; case "typeof": // typeof returns "object" or "function" on different platforms // so cannot evaluate reliably if (v instanceof RegExp) return this; return typeof v; case "void": return void v; case "~": return ~v; case "-": return -v; case "+": return +v; case "++": case "--": if (e instanceof AST_SymbolRef) { var refs = e.definition().references; if (refs[refs.length - 1] === e) return v; } } return this; }); var non_converting_binary = makePredicate("&& || === !=="); def(AST_Binary, function(compressor, cached, depth) { if (!non_converting_binary[this.operator]) depth++; var left = this.left._eval(compressor, cached, depth); if (left === this.left) return this; var right = this.right._eval(compressor, cached, depth); if (right === this.right) return this; var result; switch (this.operator) { case "&&" : result = left && right; break; case "||" : result = left || right; break; case "|" : result = left | right; break; case "&" : result = left & right; break; case "^" : result = left ^ right; break; case "+" : result = left + right; break; case "*" : result = left * right; break; case "/" : result = left / right; break; case "%" : result = left % right; break; case "-" : result = left - right; break; case "<<" : result = left << right; break; case ">>" : result = left >> right; break; case ">>>": result = left >>> right; break; case "==" : result = left == right; break; case "===": result = left === right; break; case "!=" : result = left != right; break; case "!==": result = left !== right; break; case "<" : result = left < right; break; case "<=" : result = left <= right; break; case ">" : result = left > right; break; case ">=" : result = left >= right; break; default : return this; } return isNaN(result) && compressor.find_parent(AST_With) ? this : result; }); def(AST_Conditional, function(compressor, cached, depth) { var condition = this.condition._eval(compressor, cached, depth); if (condition === this.condition) return this; var node = condition ? this.consequent : this.alternative; var value = node._eval(compressor, cached, depth); return value === node ? this : value; }); def(AST_SymbolRef, function(compressor, cached, depth) { var fixed = this.fixed_value(); if (!fixed) return this; var value; if (member(fixed, cached)) { value = fixed._eval(); } else { this._eval = return_this; value = fixed._eval(compressor, cached, depth); delete this._eval; if (value === fixed) return this; fixed._eval = function() { return value; }; cached.push(fixed); } if (value && typeof value == "object") { var escaped = this.definition().escaped; switch (escaped.length) { case 0: break; case 1: if (contains_ref(escaped[0], this)) break; default: if (depth > escaped.depth) return this; } } return value; function contains_ref(expr, ref) { var found = false; expr.walk(new TreeWalker(function(node) { if (found) return true; if (node === ref) return found = true; })); return found; } }); var global_objs = { Array: Array, Math: Math, Number: Number, Object: Object, String: String, }; var static_values = { Math: [ "E", "LN10", "LN2", "LOG2E", "LOG10E", "PI", "SQRT1_2", "SQRT2", ], Number: [ "MAX_VALUE", "MIN_VALUE", "NaN", "NEGATIVE_INFINITY", "POSITIVE_INFINITY", ], }; convert_to_predicate(static_values); def(AST_PropAccess, function(compressor, cached, depth) { if (compressor.option("unsafe")) { var key = this.property; if (key instanceof AST_Node) { key = key._eval(compressor, cached, depth); if (key === this.property) return this; } var exp = this.expression; var val; if (is_undeclared_ref(exp)) { var static_value = static_values[exp.name]; if (!static_value || !static_value[key]) return this; val = global_objs[exp.name]; } else { val = exp._eval(compressor, cached, depth + 1); if (!val || val === exp) return this; if (typeof val == "object" && !HOP(val, key)) return this; if (typeof val == "function") switch (key) { case "name": return val.node.name ? val.node.name.name : ""; case "length": return val.node.argnames.length; default: return this; } } return val[key]; } return this; }); def(AST_Call, function(compressor, cached, depth) { var exp = this.expression; if (compressor.option("unsafe") && exp instanceof AST_PropAccess) { var key = exp.property; if (key instanceof AST_Node) { key = key._eval(compressor, cached, depth); if (key === exp.property) return this; } var val; var e = exp.expression; if (is_undeclared_ref(e)) { var static_fn = static_fns[e.name]; if (!static_fn || !static_fn[key]) return this; val = global_objs[e.name]; } else { val = e._eval(compressor, cached, depth + 1); if (val === e || !val) return this; var native_fn = native_fns[val.constructor.name]; if (!native_fn || !native_fn[key]) return this; } var args = []; for (var i = 0; i < this.args.length; i++) { var arg = this.args[i]; var value = arg._eval(compressor, cached, depth); if (arg === value) return this; args.push(value); } if (key == "replace" && typeof args[1] == "function") return this; try { return val[key].apply(val, args); } catch (ex) { AST_Node.warn("Error evaluating {code} [{file}:{line},{col}]", { code: this.print_to_string(), file: this.start.file, line: this.start.line, col: this.start.col }); } } return this; }); def(AST_New, return_this); })(function(node, func) { node.DEFMETHOD("_eval", func); }); // method to negate an expression (function(def) { function basic_negation(exp) { return make_node(AST_UnaryPrefix, exp, { operator: "!", expression: exp }); } function best(orig, alt, first_in_statement) { var negated = basic_negation(orig); if (first_in_statement) { var stat = make_node(AST_SimpleStatement, alt, { body: alt }); return best_of_expression(negated, stat) === stat ? alt : negated; } return best_of_expression(negated, alt); } def(AST_Node, function() { return basic_negation(this); }); def(AST_Statement, function() { throw new Error("Cannot negate a statement"); }); def(AST_Function, function() { return basic_negation(this); }); def(AST_UnaryPrefix, function() { if (this.operator == "!") return this.expression; return basic_negation(this); }); def(AST_Sequence, function(compressor) { var expressions = this.expressions.slice(); expressions.push(expressions.pop().negate(compressor)); return make_sequence(this, expressions); }); def(AST_Conditional, function(compressor, first_in_statement) { var self = this.clone(); self.consequent = self.consequent.negate(compressor); self.alternative = self.alternative.negate(compressor); return best(this, self, first_in_statement); }); def(AST_Binary, function(compressor, first_in_statement) { var self = this.clone(), op = this.operator; if (compressor.option("unsafe_comps")) { switch (op) { case "<=" : self.operator = ">" ; return self; case "<" : self.operator = ">=" ; return self; case ">=" : self.operator = "<" ; return self; case ">" : self.operator = "<=" ; return self; } } switch (op) { case "==" : self.operator = "!="; return self; case "!=" : self.operator = "=="; return self; case "===": self.operator = "!=="; return self; case "!==": self.operator = "==="; return self; case "&&": self.operator = "||"; self.left = self.left.negate(compressor, first_in_statement); self.right = self.right.negate(compressor); return best(this, self, first_in_statement); case "||": self.operator = "&&"; self.left = self.left.negate(compressor, first_in_statement); self.right = self.right.negate(compressor); return best(this, self, first_in_statement); } return basic_negation(this); }); })(function(node, func) { node.DEFMETHOD("negate", function(compressor, first_in_statement) { return func.call(this, compressor, first_in_statement); }); }); var global_pure_fns = makePredicate("Boolean decodeURI decodeURIComponent Date encodeURI encodeURIComponent Error escape EvalError isFinite isNaN Number Object parseFloat parseInt RangeError ReferenceError String SyntaxError TypeError unescape URIError"); AST_Call.DEFMETHOD("is_expr_pure", function(compressor) { if (compressor.option("unsafe")) { var expr = this.expression; if (is_undeclared_ref(expr) && global_pure_fns[expr.name]) return true; if (expr instanceof AST_Dot && is_undeclared_ref(expr.expression)) { var static_fn = static_fns[expr.expression.name]; return static_fn && static_fn[expr.property]; } } return this.pure || !compressor.pure_funcs(this); }); AST_Node.DEFMETHOD("is_call_pure", return_false); AST_Call.DEFMETHOD("is_call_pure", function(compressor) { if (!compressor.option("unsafe")) return false; var dot = this.expression; if (!(dot instanceof AST_Dot)) return false; var exp = dot.expression; var map; var prop = dot.property; if (exp instanceof AST_Array) { map = native_fns.Array; } else if (exp.is_boolean(compressor)) { map = native_fns.Boolean; } else if (exp.is_number(compressor)) { map = native_fns.Number; } else if (exp instanceof AST_RegExp) { map = native_fns.RegExp; } else if (exp.is_string(compressor)) { map = native_fns.String; if (prop == "replace") { var arg = this.args[1]; if (arg && !arg.is_string(compressor)) return false; } } else if (!dot.may_throw_on_access(compressor)) { map = native_fns.Object; } return map && map[prop]; }); // determine if expression has side effects (function(def) { function any(list, compressor) { for (var i = list.length; --i >= 0;) if (list[i].has_side_effects(compressor)) return true; return false; } def(AST_Node, return_true); def(AST_Array, function(compressor) { return any(this.elements, compressor); }); def(AST_Assign, return_true); def(AST_Binary, function(compressor) { return this.left.has_side_effects(compressor) || this.right.has_side_effects(compressor); }); def(AST_Block, function(compressor) { return any(this.body, compressor); }); def(AST_Call, function(compressor) { if (!this.is_expr_pure(compressor) && (!this.is_call_pure(compressor) || this.expression.has_side_effects(compressor))) { return true; } return any(this.args, compressor); }); def(AST_Case, function(compressor) { return this.expression.has_side_effects(compressor) || any(this.body, compressor); }); def(AST_Conditional, function(compressor) { return this.condition.has_side_effects(compressor) || this.consequent.has_side_effects(compressor) || this.alternative.has_side_effects(compressor); }); def(AST_Constant, return_false); def(AST_Definitions, function(compressor) { return any(this.definitions, compressor); }); def(AST_Dot, function(compressor) { return this.expression.may_throw_on_access(compressor) || this.expression.has_side_effects(compressor); }); def(AST_EmptyStatement, return_false); def(AST_If, function(compressor) { return this.condition.has_side_effects(compressor) || this.body && this.body.has_side_effects(compressor) || this.alternative && this.alternative.has_side_effects(compressor); }); def(AST_LabeledStatement, function(compressor) { return this.body.has_side_effects(compressor); }); def(AST_Lambda, return_false); def(AST_Object, function(compressor) { return any(this.properties, compressor); }); def(AST_ObjectProperty, function(compressor) { return this.value.has_side_effects(compressor); }); def(AST_Sub, function(compressor) { return this.expression.may_throw_on_access(compressor) || this.expression.has_side_effects(compressor) || this.property.has_side_effects(compressor); }); def(AST_Sequence, function(compressor) { return any(this.expressions, compressor); }); def(AST_SimpleStatement, function(compressor) { return this.body.has_side_effects(compressor); }); def(AST_Switch, function(compressor) { return this.expression.has_side_effects(compressor) || any(this.body, compressor); }); def(AST_SymbolDeclaration, return_false); def(AST_SymbolRef, function(compressor) { return !this.is_declared(compressor); }); def(AST_This, return_false); def(AST_Try, function(compressor) { return any(this.body, compressor) || this.bcatch && this.bcatch.has_side_effects(compressor) || this.bfinally && this.bfinally.has_side_effects(compressor); }); def(AST_Unary, function(compressor) { return unary_side_effects[this.operator] || this.expression.has_side_effects(compressor); }); def(AST_VarDef, function(compressor) { return this.value; }); })(function(node, func) { node.DEFMETHOD("has_side_effects", func); }); // determine if expression may throw (function(def) { def(AST_Node, return_true); def(AST_Constant, return_false); def(AST_EmptyStatement, return_false); def(AST_Lambda, return_false); def(AST_SymbolDeclaration, return_false); def(AST_This, return_false); function any(list, compressor) { for (var i = list.length; --i >= 0;) if (list[i].may_throw(compressor)) return true; return false; } def(AST_Array, function(compressor) { return any(this.elements, compressor); }); def(AST_Assign, function(compressor) { if (this.right.may_throw(compressor)) return true; if (!compressor.has_directive("use strict") && this.operator == "=" && this.left instanceof AST_SymbolRef) { return false; } return this.left.may_throw(compressor); }); def(AST_Binary, function(compressor) { return this.left.may_throw(compressor) || this.right.may_throw(compressor); }); def(AST_Block, function(compressor) { return any(this.body, compressor); }); def(AST_Call, function(compressor) { if (any(this.args, compressor)) return true; if (this.is_expr_pure(compressor)) return false; if (this.expression.may_throw(compressor)) return true; return !(this.expression instanceof AST_Lambda) || any(this.expression.body, compressor); }); def(AST_Case, function(compressor) { return this.expression.may_throw(compressor) || any(this.body, compressor); }); def(AST_Conditional, function(compressor) { return this.condition.may_throw(compressor) || this.consequent.may_throw(compressor) || this.alternative.may_throw(compressor); }); def(AST_Definitions, function(compressor) { return any(this.definitions, compressor); }); def(AST_Dot, function(compressor) { return this.expression.may_throw_on_access(compressor) || this.expression.may_throw(compressor); }); def(AST_If, function(compressor) { return this.condition.may_throw(compressor) || this.body && this.body.may_throw(compressor) || this.alternative && this.alternative.may_throw(compressor); }); def(AST_LabeledStatement, function(compressor) { return this.body.may_throw(compressor); }); def(AST_Object, function(compressor) { return any(this.properties, compressor); }); def(AST_ObjectProperty, function(compressor) { return this.value.may_throw(compressor); }); def(AST_Return, function(compressor) { return this.value && this.value.may_throw(compressor); }); def(AST_Sequence, function(compressor) { return any(this.expressions, compressor); }); def(AST_SimpleStatement, function(compressor) { return this.body.may_throw(compressor); }); def(AST_Sub, function(compressor) { return this.expression.may_throw_on_access(compressor) || this.expression.may_throw(compressor) || this.property.may_throw(compressor); }); def(AST_Switch, function(compressor) { return this.expression.may_throw(compressor) || any(this.body, compressor); }); def(AST_SymbolRef, function(compressor) { return !this.is_declared(compressor); }); def(AST_Try, function(compressor) { return this.bcatch ? this.bcatch.may_throw(compressor) : any(this.body, compressor) || this.bfinally && this.bfinally.may_throw(compressor); }); def(AST_Unary, function(compressor) { if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef) return false; return this.expression.may_throw(compressor); }); def(AST_VarDef, function(compressor) { if (!this.value) return false; return this.value.may_throw(compressor); }); })(function(node, func) { node.DEFMETHOD("may_throw", func); }); // determine if expression is constant (function(def) { function all(list) { for (var i = list.length; --i >= 0;) if (!list[i].is_constant_expression()) return false; return true; } def(AST_Node, return_false); def(AST_Array, function() { return all(this.elements); }); def(AST_Binary, function() { return this.left.is_constant_expression() && this.right.is_constant_expression(); }); def(AST_Constant, return_true); def(AST_Lambda, function(scope) { var self = this; var result = true; var inner_scopes = []; self.walk(new TreeWalker(function(node, descend) { if (!result) return true; if (node instanceof AST_Catch) { inner_scopes.push(node.argname.scope); descend(); inner_scopes.pop(); return true; } if (node instanceof AST_Scope && node !== self) { inner_scopes.push(node); descend(); inner_scopes.pop(); return true; } if (node instanceof AST_SymbolRef) { if (self.inlined) { result = false; return true; } var def = node.definition(); if (!self.variables.has(def.name) && !member(def.scope, inner_scopes)) { if (scope) { var scope_def = scope.find_variable(node); if (def.undeclared ? !scope_def : scope_def === def) { result = "f"; return true; } } result = false; } return true; } })); return result; }); def(AST_Object, function() { return all(this.properties); }); def(AST_ObjectProperty, function() { return this.value.is_constant_expression(); }); def(AST_Unary, function() { return this.expression.is_constant_expression(); }); })(function(node, func) { node.DEFMETHOD("is_constant_expression", func); }); // tell me if a statement aborts function aborts(thing) { return thing && thing.aborts(); } (function(def) { def(AST_Statement, return_null); def(AST_Jump, return_this); function block_aborts() { var n = this.body.length; return n > 0 && aborts(this.body[n - 1]); } def(AST_BlockStatement, block_aborts); def(AST_SwitchBranch, block_aborts); def(AST_If, function() { return this.alternative && aborts(this.body) && aborts(this.alternative) && this; }); })(function(node, func) { node.DEFMETHOD("aborts", func); }); /* -----[ optimizers ]----- */ var directives = makePredicate(["use asm", "use strict"]); OPT(AST_Directive, function(self, compressor) { if (compressor.option("directives") && (!directives[self.value] || compressor.has_directive(self.value) !== self)) { return make_node(AST_EmptyStatement, self); } return self; }); OPT(AST_Debugger, function(self, compressor) { if (compressor.option("drop_debugger")) return make_node(AST_EmptyStatement, self); return self; }); OPT(AST_LabeledStatement, function(self, compressor) { if (self.body instanceof AST_Break && compressor.loopcontrol_target(self.body) === self.body) { return make_node(AST_EmptyStatement, self); } return self.label.references.length == 0 ? self.body : self; }); OPT(AST_Block, function(self, compressor) { tighten_body(self.body, compressor); return self; }); OPT(AST_BlockStatement, function(self, compressor) { tighten_body(self.body, compressor); switch (self.body.length) { case 1: return self.body[0]; case 0: return make_node(AST_EmptyStatement, self); } return self; }); OPT(AST_Lambda, function(self, compressor) { tighten_body(self.body, compressor); if (compressor.option("side_effects") && self.body.length == 1 && self.body[0] === compressor.has_directive("use strict")) { self.body.length = 0; } return self; }); AST_Scope.DEFMETHOD("drop_unused", function(compressor) { if (!compressor.option("unused")) return; if (compressor.has_directive("use asm")) return; var self = this; if (self.pinned()) return; var drop_funcs = !(self instanceof AST_Toplevel) || compressor.toplevel.funcs; var drop_vars = !(self instanceof AST_Toplevel) || compressor.toplevel.vars; var assign_as_unused = /keep_assign/.test(compressor.option("unused")) ? return_false : function(node, props) { var sym; if (node instanceof AST_Assign) { if (node.write_only || node.operator == "=") sym = node.left; } else if (node instanceof AST_Unary) { if (node.write_only) sym = node.expression; } if (!/strict/.test(compressor.option("pure_getters"))) return sym instanceof AST_SymbolRef && sym; while (sym instanceof AST_PropAccess && !sym.expression.may_throw_on_access(compressor)) { if (sym instanceof AST_Sub) props.unshift(sym.property); sym = sym.expression; } return sym instanceof AST_SymbolRef && all(sym.definition().orig, function(sym) { return !(sym instanceof AST_SymbolLambda); }) && sym; }; var in_use = []; var in_use_ids = Object.create(null); // avoid expensive linear scans of in_use var fixed_ids = Object.create(null); var value_read = Object.create(null); var value_modified = Object.create(null); if (self instanceof AST_Toplevel && compressor.top_retain) { self.variables.each(function(def) { if (compressor.top_retain(def) && !(def.id in in_use_ids)) { in_use_ids[def.id] = true; in_use.push(def); } }); } var var_defs_by_id = new Dictionary(); var initializations = new Dictionary(); // pass 1: find out which symbols are directly used in // this scope (not in nested scopes). var scope = this; var tw = new TreeWalker(function(node, descend) { if (node instanceof AST_Lambda && node.uses_arguments && !tw.has_directive("use strict")) { node.argnames.forEach(function(argname) { var def = argname.definition(); if (!(def.id in in_use_ids)) { in_use_ids[def.id] = true; in_use.push(def); } }); } if (node === self) return; if (node instanceof AST_Defun) { var node_def = node.name.definition(); if (!drop_funcs && scope === self) { if (!(node_def.id in in_use_ids)) { in_use_ids[node_def.id] = true; in_use.push(node_def); } } initializations.add(node_def.id, node); return true; // don't go in nested scopes } if (node instanceof AST_SymbolFunarg && scope === self) { var_defs_by_id.add(node.definition().id, node); } if (node instanceof AST_Definitions && scope === self) { node.definitions.forEach(function(def) { var node_def = def.name.definition(); var_defs_by_id.add(node_def.id, def); if (!drop_vars) { if (!(node_def.id in in_use_ids)) { in_use_ids[node_def.id] = true; in_use.push(node_def); } } if (def.value) { initializations.add(node_def.id, def.value); if (def.value.has_side_effects(compressor)) { def.value.walk(tw); } if (!node_def.chained && def.name.fixed_value(true) === def.value) { fixed_ids[node_def.id] = def; } } }); return true; } return scan_ref_scoped(node, descend); }); self.walk(tw); // pass 2: for every used symbol we need to walk its // initialization code to figure out if it uses other // symbols (that may not be in_use). tw = new TreeWalker(scan_ref_scoped); for (var i = 0; i < in_use.length; i++) { var init = initializations.get(in_use[i].id); if (init) init.forEach(function(init) { init.walk(tw); }); } var drop_fn_name = compressor.option("keep_fnames") ? return_false : compressor.option("ie8") ? function(def) { return !compressor.exposed(def) && !def.references.length; } : function(def) { // any declarations with same name will overshadow // name of this anonymous function and can therefore // never be used anywhere return !(def.id in in_use_ids) || def.orig.length > 1; }; // pass 3: we should drop declarations not in_use var unused_fn_names = []; var tt = new TreeTransformer(function(node, descend, in_list) { var parent = tt.parent(); if (drop_vars) { var props = [], sym = assign_as_unused(node, props); if (sym) { var def = sym.definition(); var in_use = def.id in in_use_ids; var value; if (node instanceof AST_Assign) { if (!in_use || node.left === sym && def.id in fixed_ids && fixed_ids[def.id] !== node) { value = get_rhs(node); } } else if (!in_use) { value = make_node(AST_Number, node, { value: 0 }); } if (value) { props.push(value); return maintain_this_binding(compressor, parent, node, make_sequence(node, props.map(function(prop) { return prop.transform(tt); }))); } } } if (scope !== self) return; if (node instanceof AST_Function && node.name && drop_fn_name(node.name.definition())) { unused_fn_names.push(node); } if (node instanceof AST_Lambda && !(node instanceof AST_Accessor)) { var trim = compressor.drop_fargs(node, parent); for (var a = node.argnames, i = a.length; --i >= 0;) { var sym = a[i]; if (!(sym.definition().id in in_use_ids)) { sym.__unused = true; if (trim) { a.pop(); AST_Node[sym.unreferenced() ? "warn" : "info"]("Dropping unused function argument {name} [{file}:{line},{col}]", template(sym)); } } else { trim = false; } } } if (drop_funcs && node instanceof AST_Defun && node !== self) { var def = node.name.definition(); if (!(def.id in in_use_ids)) { AST_Node[node.name.unreferenced() ? "warn" : "info"]("Dropping unused function {name} [{file}:{line},{col}]", template(node.name)); def.eliminated++; return make_node(AST_EmptyStatement, node); } } 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 // side effects, we can cascade the init. code // into the next one, or next statement. var side_effects = []; node.definitions.forEach(function(def) { if (def.value) def.value = def.value.transform(tt); var sym = def.name.definition(); if (!drop_vars || sym.id in in_use_ids) { if (def.value && sym.id in fixed_ids && fixed_ids[sym.id] !== def) { def.value = def.value.drop_side_effect_free(compressor); } var var_defs = var_defs_by_id.get(sym.id); if (var_defs.length > 1 && (!def.value || sym.orig.indexOf(def.name) > sym.eliminated)) { AST_Node.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name)); if (def.value) { var ref = make_node(AST_SymbolRef, def.name, def.name); sym.references.push(ref); var assign = make_node(AST_Assign, def, { operator: "=", left: ref, right: def.value }); if (fixed_ids[sym.id] === def) { fixed_ids[sym.id] = assign; } side_effects.push(assign.transform(tt)); } remove(var_defs, def); sym.eliminated++; return; } if (!def.value) { head.push(def); } else if (compressor.option("functions") && !compressor.option("ie8") && def.value === def.name.fixed_value() && def.value instanceof AST_Function && !(def.value.name && def.value.name.definition().assignments) && can_rename(def.value, def.name.name) && (!compressor.has_directive("use strict") || parent instanceof AST_Scope)) { AST_Node.warn("Declaring {name} as function [{file}:{line},{col}]", template(def.name)); var defun = make_node(AST_Defun, def, def.value); defun.name = make_node(AST_SymbolDefun, def.name, def.name); var name_def = def.name.scope.resolve().def_function(defun.name); if (def.value.name) { var old_def = def.value.name.definition(); def.value.walk(new TreeWalker(function(node) { if (node instanceof AST_SymbolRef && node.definition() === old_def) { node.name = name_def.name; node.thedef = name_def; node.reference({}); } })); } body.push(defun); } else { if (side_effects.length > 0) { if (tail.length > 0) { side_effects.push(def.value); def.value = make_sequence(def.value, side_effects); } else { body.push(make_node(AST_SimpleStatement, node, { body: make_sequence(node, side_effects) })); } side_effects = []; } tail.push(def); } } else if (sym.orig[0] instanceof AST_SymbolCatch) { var value = def.value && def.value.drop_side_effect_free(compressor); if (value) side_effects.push(value); def.value = null; head.push(def); } else { var value = def.value && def.value.drop_side_effect_free(compressor); if (value) { AST_Node.warn("Side effects in initialization of unused variable {name} [{file}:{line},{col}]", template(def.name)); side_effects.push(value); } else { AST_Node[def.name.unreferenced() ? "warn" : "info"]("Dropping unused variable {name} [{file}:{line},{col}]", template(def.name)); } sym.eliminated++; } function can_rename(fn, name) { var def = fn.variables.get(name); return !def || fn.name && def === fn.name.definition(); } }); if (head.length > 0 || tail.length > 0) { node.definitions = head.concat(tail); body.push(node); } if (side_effects.length > 0) { body.push(make_node(AST_SimpleStatement, node, { body: make_sequence(node, side_effects) })); } switch (body.length) { case 0: return in_list ? MAP.skip : make_node(AST_EmptyStatement, node); case 1: return body[0]; default: return in_list ? MAP.splice(body) : make_node(AST_BlockStatement, node, { body: body }); } } // certain combination of unused name + side effect leads to: // https://github.com/mishoo/UglifyJS2/issues/44 // https://github.com/mishoo/UglifyJS2/issues/1830 // https://github.com/mishoo/UglifyJS2/issues/1838 // https://github.com/mishoo/UglifyJS2/issues/3371 // that's an invalid AST. // We fix it at this stage by moving the `var` outside the `for`. if (node instanceof AST_For) { descend(node, this); var block; if (node.init instanceof AST_BlockStatement) { block = node.init; node.init = block.body.pop(); block.body.push(node); } if (node.init instanceof AST_Defun) { if (!block) { block = make_node(AST_BlockStatement, node, { body: [ node ] }); } block.body.splice(-1, 0, node.init); node.init = null; } else if (node.init instanceof AST_SimpleStatement) { node.init = node.init.body; } else if (is_empty(node.init)) { node.init = null; } return !block ? node : in_list ? MAP.splice(block.body) : block; } if (node instanceof AST_LabeledStatement && node.body instanceof AST_For) { descend(node, this); if (node.body instanceof AST_BlockStatement) { var block = node.body; node.body = block.body.pop(); block.body.push(node); return in_list ? MAP.splice(block.body) : block; } return node; } if (node instanceof AST_Scope) { var save_scope = scope; scope = node; descend(node, this); scope = save_scope; return node; } function template(sym) { return { name : sym.name, file : sym.start.file, line : sym.start.line, col : sym.start.col }; } }); tt.push(compressor.parent()); self.transform(tt); unused_fn_names.forEach(function(fn) { fn.name = null; }); function verify_safe_usage(def, read, modified) { if (def.id in in_use_ids) return; if (read && modified) { in_use_ids[def.id] = true; in_use.push(def); } else { value_read[def.id] = read; value_modified[def.id] = modified; } } function get_rhs(assign) { var rhs = assign.right; if (!assign.write_only) return rhs; if (!(rhs instanceof AST_Binary && lazy_op[rhs.operator])) return rhs; var sym = assign.left; if (!(sym instanceof AST_SymbolRef) || sym.name != rhs.left.name) return rhs; return rhs.right.has_side_effects(compressor) ? rhs : rhs.right; } function scan_ref_scoped(node, descend) { var node_def, props = [], sym = assign_as_unused(node, props); if (sym && self.variables.get(sym.name) === (node_def = sym.definition())) { props.forEach(function(prop) { prop.walk(tw); }); if (node instanceof AST_Assign) { if (node.write_only === "p" && node.right.may_throw_on_access(compressor)) return; var right = get_rhs(node); right.walk(tw); if (node.left === sym) { if (!node_def.chained && sym.fixed_value(true) === right) { fixed_ids[node_def.id] = node; } if (!node.write_only) { verify_safe_usage(node_def, true, value_modified[node_def.id]); } } else { var fixed = sym.fixed_value(); if (!fixed || !fixed.is_constant()) { verify_safe_usage(node_def, value_read[node_def.id], true); } } } return true; } if (node instanceof AST_SymbolRef) { node_def = node.definition(); if (!(node_def.id in in_use_ids)) { in_use_ids[node_def.id] = true; in_use.push(node_def); } return true; } if (node instanceof AST_Scope) { var save_scope = scope; scope = node; descend(); scope = save_scope; return true; } } }); AST_Scope.DEFMETHOD("hoist_declarations", function(compressor) { if (compressor.has_directive("use asm")) return; var hoist_funs = compressor.option("hoist_funs"); var hoist_vars = compressor.option("hoist_vars"); var self = this; if (hoist_vars) { // let's count var_decl first, we seem to waste a lot of // space if we hoist `var` when there's only one. var var_decl = 0; self.walk(new TreeWalker(function(node) { if (var_decl > 1) return true; if (node instanceof AST_Scope && node !== self) return true; if (node instanceof AST_Var) { var_decl++; return true; } })); if (var_decl <= 1) hoist_vars = false; } if (!hoist_funs && !hoist_vars) return; var dirs = []; var hoisted = []; var vars = new Dictionary(), vars_found = 0; var tt = new TreeTransformer(function(node) { if (node === self) return; if (node instanceof AST_Directive) { dirs.push(node); return make_node(AST_EmptyStatement, node); } if (hoist_funs && node instanceof AST_Defun && (tt.parent() === self || !compressor.has_directive("use strict"))) { hoisted.push(node); return make_node(AST_EmptyStatement, node); } if (hoist_vars && node instanceof AST_Var) { node.definitions.forEach(function(def) { vars.set(def.name.name, def); ++vars_found; }); var seq = node.to_assignments(compressor); var p = tt.parent(); if (p instanceof AST_ForIn && p.init === node) { if (seq) return seq; var def = node.definitions[0].name; return make_node(AST_SymbolRef, def, def); } if (p instanceof AST_For && p.init === node) return seq; if (!seq) return make_node(AST_EmptyStatement, node); return make_node(AST_SimpleStatement, node, { body: seq }); } if (node instanceof AST_Scope) return node; }); self.transform(tt); if (vars_found > 0) { // collect only vars which don't show up in self's arguments list var defs = []; vars.each(function(def, name) { if (self instanceof AST_Lambda && !all(self.argnames, function(argname) { return argname.name != name; })) { vars.del(name); } else { def = def.clone(); def.value = null; defs.push(def); vars.set(name, def); } }); if (defs.length > 0) { // try to merge in assignments for (var i = 0; i < self.body.length;) { if (self.body[i] instanceof AST_SimpleStatement) { var expr = self.body[i].body, sym, assign; if (expr instanceof AST_Assign && expr.operator == "=" && (sym = expr.left) instanceof AST_Symbol && vars.has(sym.name)) { var def = vars.get(sym.name); if (def.value) break; def.value = expr.right; remove(defs, def); defs.push(def); self.body.splice(i, 1); continue; } if (expr instanceof AST_Sequence && (assign = expr.expressions[0]) instanceof AST_Assign && assign.operator == "=" && (sym = assign.left) instanceof AST_Symbol && vars.has(sym.name)) { var def = vars.get(sym.name); if (def.value) break; def.value = assign.right; remove(defs, def); defs.push(def); self.body[i].body = make_sequence(expr, expr.expressions.slice(1)); continue; } } if (self.body[i] instanceof AST_EmptyStatement) { self.body.splice(i, 1); continue; } if (self.body[i] instanceof AST_BlockStatement) { var tmp = [ i, 1 ].concat(self.body[i].body); self.body.splice.apply(self.body, tmp); continue; } break; } defs = make_node(AST_Var, self, { definitions: defs }); hoisted.push(defs); } } self.body = dirs.concat(hoisted, self.body); }); AST_Scope.DEFMETHOD("var_names", function() { var var_names = this._var_names; if (!var_names) { this._var_names = var_names = Object.create(null); this.enclosed.forEach(function(def) { var_names[def.name] = true; }); this.variables.each(function(def, name) { var_names[name] = true; }); } return var_names; }); AST_Scope.DEFMETHOD("make_var_name", function(prefix) { var var_names = this.var_names(); prefix = prefix.replace(/(?:^[^a-z_$]|[^a-z0-9_$])/ig, "_"); var name = prefix; for (var i = 0; var_names[name]; i++) name = prefix + "$" + i; var_names[name] = true; return name; }); AST_Scope.DEFMETHOD("hoist_properties", function(compressor) { if (!compressor.option("hoist_props") || compressor.has_directive("use asm")) return; var self = this; var top_retain = self instanceof AST_Toplevel && compressor.top_retain || return_false; var defs_by_id = Object.create(null); self.transform(new TreeTransformer(function(node, descend) { if (node instanceof AST_Assign) { if (node.operator != "=") return; if (!node.write_only) return; if (node.left.scope !== self) return; if (!can_hoist(node.left, node.right, 1)) return; descend(node, this); var defs = new Dictionary(); var assignments = []; var decls = []; node.right.properties.forEach(function(prop) { var decl = make_sym(node.left, prop.key); decls.push(make_node(AST_VarDef, node, { name: decl, value: null })); var sym = make_node(AST_SymbolRef, node, { name: decl.name, scope: self, thedef: decl.definition() }); sym.reference({}); assignments.push(make_node(AST_Assign, node, { operator: "=", left: sym, right: prop.value })); }); defs_by_id[node.left.definition().id] = defs; self.body.splice(self.body.indexOf(this.stack[1]) + 1, 0, make_node(AST_Var, node, { definitions: decls })); return make_sequence(node, assignments); } if (node instanceof AST_Scope) return node === self ? undefined : node; if (node instanceof AST_VarDef) { if (!can_hoist(node.name, node.value, 0)) return; descend(node, this); var defs = new Dictionary(); var var_defs = []; node.value.properties.forEach(function(prop) { var_defs.push(make_node(AST_VarDef, node, { name: make_sym(node.name, prop.key), value: prop.value })); }); defs_by_id[node.name.definition().id] = defs; return MAP.splice(var_defs); } function make_sym(sym, key) { var new_var = make_node(AST_SymbolVar, sym, { name: self.make_var_name(sym.name + "_" + key), scope: self }); var def = self.def_variable(new_var); defs.set(key, def); self.enclosed.push(def); return new_var; } })); self.transform(new TreeTransformer(function(node, descend) { if (node instanceof AST_PropAccess) { if (!(node.expression instanceof AST_SymbolRef)) return; var defs = defs_by_id[node.expression.definition().id]; if (!defs) return; var def = defs.get(node.getProperty()); var sym = make_node(AST_SymbolRef, node, { name: def.name, scope: node.expression.scope, thedef: def }); sym.reference({}); return sym; } if (node instanceof AST_Unary) { if (unary_side_effects[node.operator]) return; if (!(node.expression instanceof AST_SymbolRef)) return; if (!(node.expression.definition().id in defs_by_id)) return; var opt = node.clone(); opt.expression = make_node(AST_Object, node, { properties: [] }); return opt; } })); function can_hoist(sym, right, count) { var def = sym.definition(); if (def.assignments != count) return; if (def.direct_access) return; if (def.escaped.depth == 1) return; if (def.references.length == count) return; if (def.single_use) return; if (top_retain(def)) return; if (sym.fixed_value() !== right) return; return right instanceof AST_Object; } }); function safe_to_drop(fn, compressor) { if (!fn.name || !compressor.option("ie8")) return true; var def = fn.name.definition(); if (compressor.exposed(def)) return false; return all(def.references, function(sym) { return !(sym instanceof AST_SymbolRef); }); } // drop_side_effect_free() // remove side-effect-free parts which only affects return value (function(def) { // Drop side-effect-free elements from an array of expressions. // Returns an array of expressions with side-effects or null // if all elements were dropped. Note: original array may be // returned if nothing changed. function trim(nodes, compressor, first_in_statement) { var len = nodes.length; if (!len) return null; var ret = [], changed = false; for (var i = 0; i < len; i++) { var node = nodes[i].drop_side_effect_free(compressor, first_in_statement); changed |= node !== nodes[i]; if (node) { ret.push(node); first_in_statement = false; } } return changed ? ret.length ? ret : null : nodes; } def(AST_Node, return_this); def(AST_Accessor, return_null); def(AST_Array, function(compressor, first_in_statement) { var values = trim(this.elements, compressor, first_in_statement); return values && make_sequence(this, values); }); def(AST_Assign, function(compressor) { var left = this.left; if (left instanceof AST_PropAccess) { var expr = left.expression; if (expr instanceof AST_Assign && !expr.may_throw_on_access(compressor)) { expr.write_only = "p"; } if (compressor.has_directive("use strict") && expr.is_constant()) return this; } if (left.has_side_effects(compressor)) return this; this.write_only = true; if (root_expr(left).is_constant_expression(compressor.find_parent(AST_Scope))) { return this.right.drop_side_effect_free(compressor); } return this; }); def(AST_Binary, function(compressor, first_in_statement) { var right = this.right.drop_side_effect_free(compressor, first_in_statement); if (!right) return this.left.drop_side_effect_free(compressor, first_in_statement); if (lazy_op[this.operator]) { var node = this; if (right !== node.right) { node = this.clone(); node.right = right.drop_side_effect_free(compressor); } return (first_in_statement ? best_of_statement : best_of_expression)(node, make_node(AST_Binary, this, { operator: node.operator == "&&" ? "||" : "&&", left: node.left.negate(compressor, first_in_statement), right: node.right })); } else { var left = this.left.drop_side_effect_free(compressor, first_in_statement); if (!left) return right; return make_sequence(this, [ left, right.drop_side_effect_free(compressor) ]); } }); def(AST_Call, function(compressor, first_in_statement) { if (!this.is_expr_pure(compressor)) { var exp = this.expression; if (this.is_call_pure(compressor)) { var exprs = this.args.slice(); exprs.unshift(exp.expression); exprs = trim(exprs, compressor, first_in_statement); return exprs && make_sequence(this, exprs); } if (exp instanceof AST_Function && (!exp.name || !exp.name.definition().references.length)) { var node = this.clone(); exp.process_expression(false, compressor); exp.walk(new TreeWalker(function(node) { if (node instanceof AST_Return && node.value) { node.value = node.value.drop_side_effect_free(compressor); return true; } if (node instanceof AST_Scope && node !== exp) return true; })); return node; } return this; } if (this.pure) AST_Node.warn("Dropping __PURE__ call [{file}:{line},{col}]", this.start); var args = trim(this.args, compressor, first_in_statement); return args && make_sequence(this, args); }); def(AST_Conditional, function(compressor) { var consequent = this.consequent.drop_side_effect_free(compressor); var alternative = this.alternative.drop_side_effect_free(compressor); if (consequent === this.consequent && alternative === this.alternative) return this; if (!consequent) return alternative ? make_node(AST_Binary, this, { operator: "||", left: this.condition, right: alternative }) : this.condition.drop_side_effect_free(compressor); if (!alternative) return make_node(AST_Binary, this, { operator: "&&", left: this.condition, right: consequent }); var node = this.clone(); node.consequent = consequent; node.alternative = alternative; return node; }); def(AST_Constant, return_null); def(AST_Dot, function(compressor, first_in_statement) { var expr = this.expression; if (expr.may_throw_on_access(compressor)) return this; return expr.drop_side_effect_free(compressor, first_in_statement); }); def(AST_Function, function(compressor) { return safe_to_drop(this, compressor) ? null : this; }); def(AST_Object, function(compressor, first_in_statement) { var values = trim(this.properties, compressor, first_in_statement); return values && make_sequence(this, values); }); def(AST_ObjectProperty, function(compressor, first_in_statement) { return this.value.drop_side_effect_free(compressor, first_in_statement); }); def(AST_Sequence, function(compressor, first_in_statement) { var expressions = trim(this.expressions, compressor, first_in_statement); if (expressions === this.expressions) return this; if (!expressions) return null; return make_sequence(this, expressions); }); def(AST_Sub, function(compressor, first_in_statement) { if (this.expression.may_throw_on_access(compressor)) return this; var expression = this.expression.drop_side_effect_free(compressor, first_in_statement); if (!expression) return this.property.drop_side_effect_free(compressor, first_in_statement); var property = this.property.drop_side_effect_free(compressor); if (!property) return expression; return make_sequence(this, [ expression, property ]); }); def(AST_SymbolRef, function(compressor) { if (!this.is_declared(compressor)) return this; this.definition().replaced++; return null; }); def(AST_This, return_null); def(AST_Unary, function(compressor, first_in_statement) { if (unary_side_effects[this.operator]) { this.write_only = !this.expression.has_side_effects(compressor); return this; } if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef) { this.expression.definition().replaced++; return null; } var expression = this.expression.drop_side_effect_free(compressor, first_in_statement); if (first_in_statement && expression && is_iife_call(expression)) { if (expression === this.expression && this.operator == "!") return this; return expression.negate(compressor, first_in_statement); } return expression; }); })(function(node, func) { node.DEFMETHOD("drop_side_effect_free", func); }); OPT(AST_SimpleStatement, function(self, compressor) { if (compressor.option("side_effects")) { var body = self.body; var node = body.drop_side_effect_free(compressor, true); if (!node) { AST_Node.warn("Dropping side-effect-free statement [{file}:{line},{col}]", self.start); return make_node(AST_EmptyStatement, self); } if (node !== body) { return make_node(AST_SimpleStatement, self, { body: node }); } } return self; }); OPT(AST_While, function(self, compressor) { return compressor.option("loops") ? make_node(AST_For, self, self).optimize(compressor) : self; }); function has_break_or_continue(loop, parent) { var found = false; var tw = new TreeWalker(function(node) { if (found || node instanceof AST_Scope) return true; if (node instanceof AST_LoopControl && tw.loopcontrol_target(node) === loop) { return found = true; } }); if (parent instanceof AST_LabeledStatement) tw.push(parent); tw.push(loop); loop.body.walk(tw); return found; } OPT(AST_Do, function(self, compressor) { if (!compressor.option("loops")) return self; var cond = self.condition.is_truthy() || self.condition.tail_node().evaluate(compressor); if (!(cond instanceof AST_Node)) { if (cond) return make_node(AST_For, self, { body: make_node(AST_BlockStatement, self.body, { body: [ self.body, make_node(AST_SimpleStatement, self.condition, { body: self.condition }) ] }) }).optimize(compressor); if (!has_break_or_continue(self, compressor.parent())) { return make_node(AST_BlockStatement, self.body, { body: [ self.body, make_node(AST_SimpleStatement, self.condition, { body: self.condition }) ] }).optimize(compressor); } } if (self.body instanceof AST_SimpleStatement) return make_node(AST_For, self, { condition: make_sequence(self.condition, [ self.body.body, self.condition ]), body: make_node(AST_EmptyStatement, self) }).optimize(compressor); return self; }); function if_break_in_loop(self, compressor) { var first = self.body instanceof AST_BlockStatement ? self.body.body[0] : self.body; if (compressor.option("dead_code") && is_break(first)) { var body = []; if (self.init instanceof AST_Statement) { body.push(self.init); } else if (self.init) { body.push(make_node(AST_SimpleStatement, self.init, { body: self.init })); } if (self.condition) { body.push(make_node(AST_SimpleStatement, self.condition, { body: self.condition })); } extract_declarations_from_unreachable_code(self.body, body); return make_node(AST_BlockStatement, self, { body: body }); } if (first instanceof AST_If) { if (is_break(first.body)) { if (self.condition) { self.condition = make_node(AST_Binary, self.condition, { left: self.condition, operator: "&&", right: first.condition.negate(compressor), }); } else { self.condition = first.condition.negate(compressor); } drop_it(first.alternative); } else if (is_break(first.alternative)) { if (self.condition) { self.condition = make_node(AST_Binary, self.condition, { left: self.condition, operator: "&&", right: first.condition, }); } else { self.condition = first.condition; } drop_it(first.body); } } return self; function is_break(node) { return node instanceof AST_Break && compressor.loopcontrol_target(node) === compressor.self(); } function drop_it(rest) { rest = as_statement_array(rest); if (self.body instanceof AST_BlockStatement) { self.body = self.body.clone(); self.body.body = rest.concat(self.body.body.slice(1)); self.body = self.body.transform(compressor); } else { self.body = make_node(AST_BlockStatement, self.body, { body: rest }).transform(compressor); } self = if_break_in_loop(self, compressor); } } OPT(AST_For, function(self, compressor) { if (!compressor.option("loops")) return self; if (compressor.option("side_effects")) { if (self.init) self.init = self.init.drop_side_effect_free(compressor); if (self.step) self.step = self.step.drop_side_effect_free(compressor); } if (self.condition) { var cond = self.condition.evaluate(compressor); if (!(cond instanceof AST_Node)) { if (cond) self.condition = null; else if (!compressor.option("dead_code")) { var orig = self.condition; self.condition = make_node_from_constant(cond, self.condition); self.condition = best_of_expression(self.condition.transform(compressor), orig); } } if (cond instanceof AST_Node) { cond = self.condition.is_truthy() || self.condition.tail_node().evaluate(compressor); } if (!cond) { if (compressor.option("dead_code")) { var body = []; extract_declarations_from_unreachable_code(self.body, body); if (self.init instanceof AST_Statement) { body.push(self.init); } else if (self.init) { body.push(make_node(AST_SimpleStatement, self.init, { body: self.init })); } body.push(make_node(AST_SimpleStatement, self.condition, { body: self.condition })); return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor); } } else if (self.condition && !(cond instanceof AST_Node)) { self.body = make_node(AST_BlockStatement, self.body, { body: [ make_node(AST_SimpleStatement, self.condition, { body: self.condition }), self.body ] }); self.condition = null; } } return if_break_in_loop(self, compressor); }); OPT(AST_If, function(self, compressor) { if (is_empty(self.alternative)) self.alternative = null; if (!compressor.option("conditionals")) return self; // if condition can be statically determined, warn and drop // one of the blocks. note, statically determined implies // “has no side effects”; also it doesn't work for cases like // `x && true`, though it probably should. var cond = self.condition.evaluate(compressor); if (!compressor.option("dead_code") && !(cond instanceof AST_Node)) { var orig = self.condition; self.condition = make_node_from_constant(cond, orig); self.condition = best_of_expression(self.condition.transform(compressor), orig); } if (compressor.option("dead_code")) { if (cond instanceof AST_Node) { cond = self.condition.is_truthy() || self.condition.tail_node().evaluate(compressor); } if (!cond) { AST_Node.warn("Condition always false [{file}:{line},{col}]", self.condition.start); var body = []; extract_declarations_from_unreachable_code(self.body, body); body.push(make_node(AST_SimpleStatement, self.condition, { body: self.condition })); if (self.alternative) body.push(self.alternative); return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor); } else if (!(cond instanceof AST_Node)) { AST_Node.warn("Condition always true [{file}:{line},{col}]", self.condition.start); var body = []; if (self.alternative) extract_declarations_from_unreachable_code(self.alternative, body); body.push(make_node(AST_SimpleStatement, self.condition, { body: self.condition })); body.push(self.body); return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor); } } var negated = self.condition.negate(compressor); var self_condition_length = self.condition.print_to_string().length; var negated_length = negated.print_to_string().length; var negated_is_best = negated_length < self_condition_length; if (self.alternative && negated_is_best) { negated_is_best = false; // because we already do the switch here. // no need to swap values of self_condition_length and negated_length // here because they are only used in an equality comparison later on. self.condition = negated; var tmp = self.body; self.body = self.alternative || make_node(AST_EmptyStatement, self); self.alternative = tmp; } if (is_empty(self.body) && is_empty(self.alternative)) { return make_node(AST_SimpleStatement, self.condition, { body: self.condition.clone() }).optimize(compressor); } if (self.body instanceof AST_SimpleStatement && self.alternative instanceof AST_SimpleStatement) { return make_node(AST_SimpleStatement, self, { body: make_node(AST_Conditional, self, { condition : self.condition, consequent : self.body.body, alternative : self.alternative.body }) }).optimize(compressor); } if (is_empty(self.alternative) && self.body instanceof AST_SimpleStatement) { if (self_condition_length === negated_length && !negated_is_best && self.condition instanceof AST_Binary && self.condition.operator == "||") { // although the code length of self.condition and negated are the same, // negated does not require additional surrounding parentheses. // see https://github.com/mishoo/UglifyJS2/issues/979 negated_is_best = true; } if (negated_is_best) return make_node(AST_SimpleStatement, self, { body: make_node(AST_Binary, self, { operator : "||", left : negated, right : self.body.body }).transform(compressor) }).optimize(compressor); return make_node(AST_SimpleStatement, self, { body: make_node(AST_Binary, self, { operator : "&&", left : self.condition, right : self.body.body }).transform(compressor) }).optimize(compressor); } if (self.body instanceof AST_EmptyStatement && self.alternative instanceof AST_SimpleStatement) { return make_node(AST_SimpleStatement, self, { body: make_node(AST_Binary, self, { operator : "||", left : self.condition, right : self.alternative.body }).transform(compressor) }).optimize(compressor); } if (self.body instanceof AST_Exit && self.alternative instanceof AST_Exit && self.body.TYPE == self.alternative.TYPE) { return make_node(self.body.CTOR, self, { value: make_node(AST_Conditional, self, { condition : self.condition, consequent : self.body.value || make_node(AST_Undefined, self.body), alternative : self.alternative.value || make_node(AST_Undefined, self.alternative) }).transform(compressor) }).optimize(compressor); } if (self.body instanceof AST_If && !self.body.alternative && !self.alternative) { self = make_node(AST_If, self, { condition: make_node(AST_Binary, self.condition, { operator: "&&", left: self.condition, right: self.body.condition }), body: self.body.body, alternative: null }); } if (aborts(self.body)) { if (self.alternative) { var alt = self.alternative; self.alternative = null; return make_node(AST_BlockStatement, self, { body: [ self, alt ] }).optimize(compressor); } } if (aborts(self.alternative)) { var body = self.body; self.body = self.alternative; self.condition = negated_is_best ? negated : self.condition.negate(compressor); self.alternative = null; return make_node(AST_BlockStatement, self, { body: [ self, body ] }).optimize(compressor); } return self; }); OPT(AST_Switch, function(self, compressor) { if (!compressor.option("switches")) return self; var branch; var value = self.expression.evaluate(compressor); if (!(value instanceof AST_Node)) { var orig = self.expression; self.expression = make_node_from_constant(value, orig); self.expression = best_of_expression(self.expression.transform(compressor), orig); } if (!compressor.option("dead_code")) return self; if (value instanceof AST_Node) { value = self.expression.tail_node().evaluate(compressor); } var decl = []; var body = []; var default_branch; var exact_match; for (var i = 0, len = self.body.length; i < len && !exact_match; i++) { branch = self.body[i]; if (branch instanceof AST_Default) { if (!default_branch) { default_branch = branch; } else { eliminate_branch(branch, body[body.length - 1]); } } else if (!(value instanceof AST_Node)) { var exp = branch.expression.evaluate(compressor); if (!(exp instanceof AST_Node) && exp !== value) { eliminate_branch(branch, body[body.length - 1]); continue; } if (exp instanceof AST_Node) exp = branch.expression.tail_node().evaluate(compressor); if (exp === value) { exact_match = branch; if (default_branch) { var default_index = body.indexOf(default_branch); body.splice(default_index, 1); eliminate_branch(default_branch, body[default_index - 1]); default_branch = null; } } } if (aborts(branch)) { var prev = body[body.length - 1]; if (aborts(prev) && prev.body.length == branch.body.length && make_node(AST_BlockStatement, prev, prev).equivalent_to(make_node(AST_BlockStatement, branch, branch))) { prev.body = []; } } body.push(branch); } while (i < len) eliminate_branch(self.body[i++], body[body.length - 1]); if (body.length > 0) { body[0].body = decl.concat(body[0].body); } self.body = body; while (branch = body[body.length - 1]) { var stat = branch.body[branch.body.length - 1]; if (stat instanceof AST_Break && compressor.loopcontrol_target(stat) === self) branch.body.pop(); if (branch.body.length || branch instanceof AST_Case && (default_branch || branch.expression.has_side_effects(compressor))) break; if (body.pop() === default_branch) default_branch = null; } if (body.length == 0) { return make_node(AST_BlockStatement, self, { body: decl.concat(make_node(AST_SimpleStatement, self.expression, { body: self.expression })) }).optimize(compressor); } if (body.length == 1 && (body[0] === exact_match || body[0] === default_branch)) { var has_break = false; var tw = new TreeWalker(function(node) { if (has_break || node instanceof AST_Lambda || node instanceof AST_SimpleStatement) return true; if (node instanceof AST_Break && tw.loopcontrol_target(node) === self) has_break = true; }); self.walk(tw); if (!has_break) { var statements = body[0].body.slice(); var exp = body[0].expression; if (exp) statements.unshift(make_node(AST_SimpleStatement, exp, { body: exp })); statements.unshift(make_node(AST_SimpleStatement, self.expression, { body:self.expression })); return make_node(AST_BlockStatement, self, { body: statements }).optimize(compressor); } } return self; function eliminate_branch(branch, prev) { if (prev && !aborts(prev)) { prev.body = prev.body.concat(branch.body); } else { extract_declarations_from_unreachable_code(branch, decl); } } }); OPT(AST_Try, function(self, compressor) { tighten_body(self.body, compressor); if (self.bcatch && self.bfinally && all(self.bfinally.body, is_empty)) self.bfinally = null; if (compressor.option("dead_code") && all(self.body, is_empty)) { var body = []; if (self.bcatch) { extract_declarations_from_unreachable_code(self.bcatch, body); body.forEach(function(stat) { if (!(stat instanceof AST_Definitions)) return; stat.definitions.forEach(function(var_def) { var def = var_def.name.definition().redefined(); if (!def) return; var_def.name = var_def.name.clone(); var_def.name.thedef = def; }); }); } if (self.bfinally) body = body.concat(self.bfinally.body); return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor); } return self; }); AST_Definitions.DEFMETHOD("remove_initializers", function() { this.definitions.forEach(function(def) { def.value = null; }); }); AST_Definitions.DEFMETHOD("to_assignments", function(compressor) { var reduce_vars = compressor.option("reduce_vars"); var assignments = this.definitions.reduce(function(a, def) { if (def.value) { var name = make_node(AST_SymbolRef, def.name, def.name); a.push(make_node(AST_Assign, def, { operator : "=", left : name, right : def.value })); if (reduce_vars) name.definition().fixed = false; } def = def.name.definition(); def.eliminated++; def.replaced--; return a; }, []); if (assignments.length == 0) return null; return make_sequence(this, assignments); }); OPT(AST_Definitions, function(self, compressor) { return self.definitions.length ? self : make_node(AST_EmptyStatement, self); }); AST_Call.DEFMETHOD("lift_sequences", function(compressor) { if (!compressor.option("sequences")) return this; var exp = this.expression; if (!(exp instanceof AST_Sequence)) return this; var tail = exp.tail_node(); if (needs_unbinding(compressor, tail) && !(this instanceof AST_New)) return this; var expressions = exp.expressions.slice(0, -1); var node = this.clone(); node.expression = tail; expressions.push(node); return make_sequence(this, expressions).optimize(compressor); }); OPT(AST_Call, function(self, compressor) { var seq = self.lift_sequences(compressor); if (seq !== self) { return seq; } var exp = self.expression; var fn = exp; if (compressor.option("reduce_vars") && fn instanceof AST_SymbolRef) { fn = fn.fixed_value(); } var is_func = fn instanceof AST_Lambda; if (compressor.option("unused") && is_func && !fn.uses_arguments && !fn.pinned()) { var pos = 0, last = 0; for (var i = 0; i < self.args.length; i++) { var trim = i >= fn.argnames.length; if (trim || fn.argnames[i].__unused) { var node = self.args[i].drop_side_effect_free(compressor); if (node) { self.args[pos++] = node; } else if (!trim) { self.args[pos++] = make_node(AST_Number, self.args[i], { value: 0 }); continue; } } else { self.args[pos++] = self.args[i]; } last = pos; } self.args.length = last; } if (compressor.option("unsafe")) { if (is_undeclared_ref(exp)) switch (exp.name) { case "Array": if (self.args.length == 1) { var first = self.args[0]; if (first instanceof AST_Number) try { var length = first.getValue(); if (length > 6) break; var elements = Array(length); for (var i = 0; i < length; i++) elements[i] = make_node(AST_Hole, self); return make_node(AST_Array, self, { elements: elements }); } catch (ex) { AST_Node.warn("Invalid array length: {length} [{file}:{line},{col}]", { length: length, file: self.start.file, line: self.start.line, col: self.start.col }); break; } if (!first.is_boolean(compressor) && !first.is_string(compressor)) break; } return make_node(AST_Array, self, { elements: self.args }); case "Object": if (self.args.length == 0) { return make_node(AST_Object, self, { properties: [] }); } break; case "String": if (self.args.length == 0) return make_node(AST_String, self, { value: "" }); if (self.args.length <= 1) return make_node(AST_Binary, self, { left: self.args[0], operator: "+", right: make_node(AST_String, self, { value: "" }) }).optimize(compressor); break; case "Number": if (self.args.length == 0) return make_node(AST_Number, self, { value: 0 }); if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, { expression: self.args[0], operator: "+" }).optimize(compressor); case "Boolean": if (self.args.length == 0) return make_node(AST_False, self); if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, { expression: make_node(AST_UnaryPrefix, self, { expression: self.args[0], operator: "!" }), operator: "!" }).optimize(compressor); break; case "RegExp": var params = []; if (all(self.args, function(arg) { var value = arg.evaluate(compressor); params.unshift(value); return arg !== value; })) { try { return best_of(compressor, self, make_node(AST_RegExp, self, { value: RegExp.apply(RegExp, params), })); } catch (ex) { AST_Node.warn("Error converting {expr} [{file}:{line},{col}]", { expr: self.print_to_string(), file: self.start.file, line: self.start.line, col: self.start.col }); } } break; } else if (exp instanceof AST_Dot) switch(exp.property) { case "toString": if (self.args.length == 0 && !exp.expression.may_throw_on_access(compressor)) { return make_node(AST_Binary, self, { left: make_node(AST_String, self, { value: "" }), operator: "+", right: exp.expression }).optimize(compressor); } break; case "join": if (exp.expression instanceof AST_Array) EXIT: { var separator; if (self.args.length > 0) { separator = self.args[0].evaluate(compressor); if (separator === self.args[0]) break EXIT; // not a constant } var elements = []; var consts = []; exp.expression.elements.forEach(function(el) { var value = el.evaluate(compressor); if (value !== el) { consts.push(value); } else { if (consts.length > 0) { elements.push(make_node(AST_String, self, { value: consts.join(separator) })); consts.length = 0; } elements.push(el); } }); if (consts.length > 0) { elements.push(make_node(AST_String, self, { value: consts.join(separator) })); } if (elements.length == 0) return make_node(AST_String, self, { value: "" }); if (elements.length == 1) { if (elements[0].is_string(compressor)) { return elements[0]; } return make_node(AST_Binary, elements[0], { operator : "+", left : make_node(AST_String, self, { value: "" }), right : elements[0] }); } if (separator == "") { var first; if (elements[0].is_string(compressor) || elements[1].is_string(compressor)) { first = elements.shift(); } else { first = make_node(AST_String, self, { value: "" }); } return elements.reduce(function(prev, el) { return make_node(AST_Binary, el, { operator : "+", left : prev, right : el }); }, first).optimize(compressor); } // need this awkward cloning to not affect original element // best_of will decide which one to get through. var node = self.clone(); node.expression = node.expression.clone(); node.expression.expression = node.expression.expression.clone(); node.expression.expression.elements = elements; return best_of(compressor, self, node); } break; case "charAt": if (self.args.length < 2) { var node = make_node(AST_Sub, self, { expression: exp.expression, property: self.args.length ? make_node(AST_Binary, self.args[0], { operator: "|", left: make_node(AST_Number, self, { value: 0 }), right: self.args[0] }) : make_node(AST_Number, self, { value: 0 }) }); node.is_string = return_true; return node.optimize(compressor); } break; case "apply": if (self.args.length == 2 && self.args[1] instanceof AST_Array) { var args = self.args[1].elements.slice(); args.unshift(self.args[0]); return make_node(AST_Call, self, { expression: make_node(AST_Dot, exp, { expression: exp.expression, property: "call" }), args: args }).optimize(compressor); } break; case "call": var func = exp.expression; if (func instanceof AST_SymbolRef) { func = func.fixed_value(); } if (func instanceof AST_Lambda && !func.contains_this()) { return (self.args.length ? make_sequence(this, [ self.args[0], make_node(AST_Call, self, { expression: exp.expression, args: self.args.slice(1) }) ]) : make_node(AST_Call, self, { expression: exp.expression, args: [] })).optimize(compressor); } break; } } if (compressor.option("unsafe_Function") && is_undeclared_ref(exp) && exp.name == "Function") { // new Function() => function(){} if (self.args.length == 0) return make_node(AST_Function, self, { argnames: [], body: [] }); if (all(self.args, function(x) { return x instanceof AST_String; })) { // quite a corner-case, but we can handle it: // https://github.com/mishoo/UglifyJS2/issues/203 // if the code argument is a constant, then we can minify it. try { var code = "n(function(" + self.args.slice(0, -1).map(function(arg) { return arg.value; }).join(",") + "){" + self.args[self.args.length - 1].value + "})"; var ast = parse(code); var mangle = { ie8: compressor.option("ie8") }; ast.figure_out_scope(mangle); var comp = new Compressor(compressor.options); ast = ast.transform(comp); ast.figure_out_scope(mangle); ast.compute_char_frequency(mangle); ast.mangle_names(mangle); var fun; ast.walk(new TreeWalker(function(node) { if (fun) return true; if (node instanceof AST_Lambda) { fun = node; return true; } })); var code = OutputStream(); AST_BlockStatement.prototype._codegen.call(fun, fun, code); self.args = [ make_node(AST_String, self, { value: fun.argnames.map(function(arg) { return arg.print_to_string(); }).join(",") }), make_node(AST_String, self.args[self.args.length - 1], { value: code.get().replace(/^\{|\}$/g, "") }) ]; return self; } catch (ex) { if (ex instanceof JS_Parse_Error) { AST_Node.warn("Error parsing code passed to new Function [{file}:{line},{col}]", self.args[self.args.length - 1].start); AST_Node.warn(ex.toString()); } else { throw ex; } } } } var stat = is_func && fn.body[0]; var can_inline = compressor.option("inline") && !self.is_expr_pure(compressor); if (can_inline && stat instanceof AST_Return) { var value = stat.value; if (!value || value.is_constant_expression()) { if (value) { value = value.clone(true); } else { value = make_node(AST_Undefined, self); } var args = self.args.concat(value); return make_sequence(self, args).optimize(compressor); } } if (is_func) { var def, value, scope, in_loop, level = -1; if (can_inline && !fn.uses_arguments && !fn.pinned() && !(fn.name && fn instanceof AST_Function) && (value = can_flatten_body(stat)) && (exp === fn || compressor.option("unused") && (def = exp.definition()).references.length == 1 && !recursive_ref(compressor, def) && fn.is_constant_expression(exp.scope)) && !self.pure && !fn.contains_this() && can_inject_symbols()) { fn._squeezed = true; return make_sequence(self, flatten_fn()).optimize(compressor); } if (compressor.option("side_effects") && all(fn.body, is_empty) && (fn !== exp || safe_to_drop(fn, compressor))) { var args = self.args.concat(make_node(AST_Undefined, self)); return make_sequence(self, args).optimize(compressor); } } if (compressor.option("drop_console")) { if (exp instanceof AST_PropAccess) { var name = exp.expression; while (name.expression) { name = name.expression; } if (is_undeclared_ref(name) && name.name == "console") { return make_node(AST_Undefined, self).optimize(compressor); } } } if (compressor.option("negate_iife") && compressor.parent() instanceof AST_SimpleStatement && is_iife_call(self)) { return self.negate(compressor, true); } var ev = self.evaluate(compressor); if (ev !== self) { ev = make_node_from_constant(ev, self).optimize(compressor); return best_of(compressor, ev, self); } return self; function return_value(stat) { if (!stat) return make_node(AST_Undefined, self); if (stat instanceof AST_Return) { if (!stat.value) return make_node(AST_Undefined, self); return stat.value.clone(true); } if (stat instanceof AST_SimpleStatement) { return make_node(AST_UnaryPrefix, stat, { operator: "void", expression: stat.body.clone(true) }); } } function can_flatten_body(stat) { var len = fn.body.length; if (compressor.option("inline") < 3) { return len == 1 && return_value(stat); } stat = null; for (var i = 0; i < len; i++) { var line = fn.body[i]; if (line instanceof AST_Var) { if (stat && !all(line.definitions, function(var_def) { return !var_def.value; })) { return false; } } else if (line instanceof AST_Defun || line instanceof AST_EmptyStatement) { continue; } else if (stat) { return false; } else { stat = line; } } return return_value(stat); } function var_exists(defined, name) { return defined[name] || identifier_atom[name] || scope.var_names()[name]; } function can_inject_args(catches, used, safe_to_inject) { for (var i = 0; i < fn.argnames.length; i++) { var arg = fn.argnames[i]; if (arg.__unused) continue; if (!safe_to_inject || var_exists(catches, arg.name)) return false; used[arg.name] = true; if (in_loop) in_loop.push(arg.definition()); } return true; } function can_inject_vars(catches, used, safe_to_inject) { for (var i = 0; i < fn.body.length; i++) { var stat = fn.body[i]; if (stat instanceof AST_Defun) { if (!safe_to_inject || var_exists(used, stat.name.name)) return false; continue; } if (!(stat instanceof AST_Var)) continue; if (!safe_to_inject) return false; for (var j = stat.definitions.length; --j >= 0;) { var name = stat.definitions[j].name; if (var_exists(catches, name.name)) return false; if (in_loop) in_loop.push(name.definition()); } } return true; } function can_inject_symbols() { var catches = Object.create(null); do { scope = compressor.parent(++level); if (scope instanceof AST_Catch) { catches[scope.argname.name] = true; } else if (scope instanceof AST_IterationStatement) { in_loop = []; } else if (scope instanceof AST_SymbolRef) { if (scope.fixed_value() instanceof AST_Scope) return false; } } while (!(scope instanceof AST_Scope)); var safe_to_inject = (!(scope instanceof AST_Toplevel) || compressor.toplevel.vars) && (exp !== fn || fn.parent_scope === compressor.find_parent(AST_Scope)); var inline = compressor.option("inline"); var used = Object.create(catches); if (!can_inject_args(catches, used, inline >= 2 && safe_to_inject)) return false; if (!can_inject_vars(catches, used, inline >= 3 && safe_to_inject)) return false; return !in_loop || in_loop.length == 0 || !is_reachable(fn, in_loop); } function append_var(decls, expressions, name, value) { var def = name.definition(); scope.variables.set(name.name, def); scope.enclosed.push(def); if (!scope.var_names()[name.name]) { scope.var_names()[name.name] = true; decls.push(make_node(AST_VarDef, name, { name: name, value: null })); } var sym = make_node(AST_SymbolRef, name, name); def.references.push(sym); if (value) expressions.push(make_node(AST_Assign, self, { operator: "=", left: sym, right: value })); } function flatten_args(decls, expressions) { var len = fn.argnames.length; for (var i = self.args.length; --i >= len;) { expressions.push(self.args[i]); } for (i = len; --i >= 0;) { var name = fn.argnames[i]; var value = self.args[i]; if (name.__unused || scope.var_names()[name.name]) { if (value) expressions.push(value); } else { var symbol = make_node(AST_SymbolVar, name, name); name.definition().orig.push(symbol); if (!value && in_loop) value = make_node(AST_Undefined, self); append_var(decls, expressions, symbol, value); } } decls.reverse(); expressions.reverse(); } function flatten_vars(decls, expressions) { var pos = expressions.length; for (var i = 0; i < fn.body.length; i++) { var stat = fn.body[i]; if (!(stat instanceof AST_Var)) continue; for (var j = 0; j < stat.definitions.length; j++) { var var_def = stat.definitions[j]; var name = var_def.name; var redef = name.definition().redefined(); if (redef) { name = name.clone(); name.thedef = redef; } append_var(decls, expressions, name, var_def.value); if (in_loop && all(fn.argnames, function(argname) { return argname.name != name.name; })) { var def = fn.variables.get(name.name); var sym = make_node(AST_SymbolRef, name, name); def.references.push(sym); expressions.splice(pos++, 0, make_node(AST_Assign, var_def, { operator: "=", left: sym, right: make_node(AST_Undefined, name) })); } } } } function flatten_fn() { var decls = []; var expressions = []; flatten_args(decls, expressions); flatten_vars(decls, expressions); expressions.push(value); var args = fn.body.filter(function(stat) { if (stat instanceof AST_Defun) { var def = stat.name.definition(); scope.functions.set(def.name, def); scope.variables.set(def.name, def); scope.enclosed.push(def); scope.var_names()[def.name] = true; return true; } }); args.unshift(scope.body.indexOf(compressor.parent(level - 1)) + 1, 0); if (decls.length) args.push(make_node(AST_Var, fn, { definitions: decls })); [].splice.apply(scope.body, args); return expressions; } }); OPT(AST_New, function(self, compressor) { var seq = self.lift_sequences(compressor); if (seq !== self) { return seq; } if (compressor.option("unsafe")) { var exp = self.expression; if (is_undeclared_ref(exp)) { switch (exp.name) { case "Object": case "RegExp": case "Function": case "Error": case "Array": return make_node(AST_Call, self, self).transform(compressor); } } } return self; }); OPT(AST_Sequence, function(self, compressor) { if (!compressor.option("side_effects")) return self; var expressions = []; filter_for_side_effects(); var end = expressions.length - 1; trim_right_for_undefined(); if (end == 0) { self = maintain_this_binding(compressor, compressor.parent(), compressor.self(), expressions[0]); if (!(self instanceof AST_Sequence)) self = self.optimize(compressor); return self; } self.expressions = expressions; return self; function filter_for_side_effects() { var first = first_in_statement(compressor); var last = self.expressions.length - 1; self.expressions.forEach(function(expr, index) { if (index < last) expr = expr.drop_side_effect_free(compressor, first); if (expr) { merge_sequence(expressions, expr); first = false; } }); } function trim_right_for_undefined() { while (end > 0 && is_undefined(expressions[end], compressor)) end--; if (end < expressions.length - 1) { expressions[end] = make_node(AST_UnaryPrefix, self, { operator : "void", expression : expressions[end] }); expressions.length = end + 1; } } }); AST_Unary.DEFMETHOD("lift_sequences", function(compressor) { if (compressor.option("sequences") && this.expression instanceof AST_Sequence) { var x = this.expression.expressions.slice(); var e = this.clone(); e.expression = x.pop(); x.push(e); return make_sequence(this, x).optimize(compressor); } return this; }); OPT(AST_UnaryPostfix, function(self, compressor) { return self.lift_sequences(compressor); }); OPT(AST_UnaryPrefix, function(self, compressor) { var e = self.expression; if (compressor.option("evaluate") && self.operator == "delete" && !(e instanceof AST_SymbolRef || e instanceof AST_PropAccess || is_identifier_atom(e))) { if (e instanceof AST_Sequence) { e = e.expressions.slice(); e.push(make_node(AST_True, self)); return make_sequence(self, e).optimize(compressor); } return make_sequence(self, [ e, make_node(AST_True, self) ]).optimize(compressor); } var seq = self.lift_sequences(compressor); if (seq !== self) { return seq; } if (compressor.option("side_effects") && self.operator == "void") { e = e.drop_side_effect_free(compressor); if (e) { self.expression = e; return self; } else { return make_node(AST_Undefined, self).optimize(compressor); } } if (compressor.option("booleans")) { if (self.operator == "!" && e.is_truthy()) { return make_sequence(self, [ e, make_node(AST_False, self) ]).optimize(compressor); } else if (compressor.in_boolean_context()) switch (self.operator) { case "!": if (e instanceof AST_UnaryPrefix && e.operator == "!") { // !!foo ==> foo, if we're in boolean context return e.expression; } if (e instanceof AST_Binary) { self = best_of(compressor, self, e.negate(compressor, first_in_statement(compressor))); } break; case "typeof": // typeof always returns a non-empty string, thus it's // always true in booleans AST_Node.warn("Boolean expression always true [{file}:{line},{col}]", self.start); return (e instanceof AST_SymbolRef ? make_node(AST_True, self) : make_sequence(self, [ e, make_node(AST_True, self) ])).optimize(compressor); } } if (self.operator == "-" && e instanceof AST_Infinity) { e = e.transform(compressor); } if (e instanceof AST_Binary && (self.operator == "+" || self.operator == "-") && (e.operator == "*" || e.operator == "/" || e.operator == "%")) { return make_node(AST_Binary, self, { operator: e.operator, left: make_node(AST_UnaryPrefix, e.left, { operator: self.operator, expression: e.left }), right: e.right }); } // avoids infinite recursion of numerals if (self.operator != "-" || !(e instanceof AST_Number || e instanceof AST_Infinity)) { var ev = self.evaluate(compressor); if (ev !== self) { ev = make_node_from_constant(ev, self).optimize(compressor); return best_of(compressor, ev, self); } } return self; }); AST_Binary.DEFMETHOD("lift_sequences", function(compressor) { if (compressor.option("sequences")) { if (this.left instanceof AST_Sequence) { var x = this.left.expressions.slice(); var e = this.clone(); e.left = x.pop(); x.push(e); return make_sequence(this, x).optimize(compressor); } if (this.right instanceof AST_Sequence && !this.left.has_side_effects(compressor)) { var assign = this.operator == "=" && this.left instanceof AST_SymbolRef; var x = this.right.expressions; var last = x.length - 1; for (var i = 0; i < last; i++) { if (!assign && x[i].has_side_effects(compressor)) break; } if (i == last) { x = x.slice(); var e = this.clone(); e.right = x.pop(); x.push(e); return make_sequence(this, x).optimize(compressor); } else if (i > 0) { var e = this.clone(); e.right = make_sequence(this.right, x.slice(i)); x = x.slice(0, i); x.push(e); return make_sequence(this, x).optimize(compressor); } } } return this; }); var indexFns = makePredicate("indexOf lastIndexOf"); 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() { return self.left.is_constant() || self.right.is_constant() || !self.left.has_side_effects(compressor) && !self.right.has_side_effects(compressor); } function reverse(op) { if (reversible()) { if (op) self.operator = op; var tmp = self.left; self.left = self.right; self.right = tmp; } } if (commutativeOperators[self.operator] && self.right.is_constant() && !self.left.is_constant()) { // if right is a constant, whatever side effects the // left side might have could not influence the // result. hence, force switch. if (!(self.left instanceof AST_Binary && PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) { reverse(); } } self = self.lift_sequences(compressor); if (compressor.option("assignments") && lazy_op[self.operator]) { var assign = self.right; // a || (a = x) => a = a || x // a && (a = x) => a = a && x if (self.left instanceof AST_SymbolRef && assign instanceof AST_Assign && assign.operator == "=" && self.left.equivalent_to(assign.left)) { self.right = assign.right; assign.right = self; return assign; } } if (compressor.option("comparisons")) switch (self.operator) { case "===": case "!==": if (is_undefined(self.left, compressor) && self.right.is_defined(compressor)) { AST_Node.warn("Expression always defined [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.right, make_node(self.operator == "===" ? AST_False : AST_True, self) ]).optimize(compressor); } var is_strict_comparison = true; 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(compressor) && self.right.is_boolean(compressor)) || self.left.equivalent_to(self.right)) { self.operator = self.operator.substr(0, 2); } // XXX: intentionally falling down to the next case case "==": case "!=": // void 0 == x => null == x if (!is_strict_comparison && is_undefined(self.left, compressor)) { self.left = make_node(AST_Null, self.left); } // "undefined" == typeof x => undefined === x else if (compressor.option("typeofs") && self.left instanceof AST_String && self.left.value == "undefined" && self.right instanceof AST_UnaryPrefix && self.right.operator == "typeof") { var expr = self.right.expression; if (expr instanceof AST_SymbolRef ? expr.is_declared(compressor) : !(expr instanceof AST_PropAccess && compressor.option("ie8"))) { self.right = expr; self.left = make_node(AST_Undefined, self.left).optimize(compressor); 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; case "&&": case "||": // void 0 !== x && null !== x => null != x // void 0 === x || null === x => null == x var lhs = self.left; if (lhs.operator == self.operator) { lhs = lhs.right; } if (lhs instanceof AST_Binary && lhs.operator == (self.operator == "&&" ? "!==" : "===") && self.right instanceof AST_Binary && lhs.operator == self.right.operator && (is_undefined(lhs.left, compressor) && self.right.left instanceof AST_Null || lhs.left instanceof AST_Null && is_undefined(self.right.left, compressor)) && !lhs.right.has_side_effects(compressor) && lhs.right.equivalent_to(self.right.right)) { var combined = make_node(AST_Binary, self, { operator: lhs.operator.slice(0, -1), left: make_node(AST_Null, self), right: lhs.right }); if (lhs !== self.left) { combined = make_node(AST_Binary, self, { operator: self.operator, left: self.left.left, right: combined }); } return combined; } break; } var in_bool = compressor.option("booleans") && compressor.in_boolean_context(); if (in_bool) switch (self.operator) { case "+": var ll = self.left.evaluate(compressor); var rr = self.right.evaluate(compressor); if (ll && typeof ll == "string") { AST_Node.warn("+ in boolean context always true [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.right, make_node(AST_True, self) ]).optimize(compressor); } if (rr && typeof rr == "string") { AST_Node.warn("+ in boolean context always true [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.left, make_node(AST_True, self) ]).optimize(compressor); } break; case "==": if (self.left instanceof AST_String && self.left.getValue() == "" && self.right.is_string(compressor)) { return make_node(AST_UnaryPrefix, self, { operator: "!", expression: self.right }).optimize(compressor); } break; case "!=": if (self.left instanceof AST_String && self.left.getValue() == "" && self.right.is_string(compressor)) { return self.right.optimize(compressor); } break; } var parent = compressor.parent(); if (compressor.option("comparisons") && self.is_boolean(compressor)) { if (!(parent instanceof AST_Binary) || parent instanceof AST_Assign) { var negated = make_node(AST_UnaryPrefix, self, { operator: "!", expression: self.negate(compressor, first_in_statement(compressor)) }); self = best_of(compressor, self, negated); } switch (self.operator) { case ">": reverse("<"); break; case ">=": reverse("<="); break; } } if (self.operator == "+") { if (self.right instanceof AST_String && self.right.getValue() == "" && self.left.is_string(compressor)) { return self.left.optimize(compressor); } if (self.left instanceof AST_String && self.left.getValue() == "" && self.right.is_string(compressor)) { return self.right.optimize(compressor); } if (self.left instanceof AST_Binary && self.left.operator == "+" && self.left.left instanceof AST_String && self.left.left.getValue() == "" && self.right.is_string(compressor)) { self.left = self.left.right; return self.optimize(compressor); } } if (compressor.option("evaluate")) { switch (self.operator) { case "&&": var ll = fuzzy_eval(self.left); if (!ll) { AST_Node.warn("Condition left of && always false [{file}:{line},{col}]", self.start); return maintain_this_binding(compressor, parent, compressor.self(), self.left).optimize(compressor); } else if (!(ll instanceof AST_Node)) { AST_Node.warn("Condition left of && always true [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.left, self.right ]).optimize(compressor); } var rr = self.right.evaluate(compressor); if (!rr) { if (in_bool) { AST_Node.warn("Boolean && always false [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.left, make_node(AST_False, self) ]).optimize(compressor); } else self.falsy = true; } else if (!(rr instanceof AST_Node)) { if (in_bool || parent.operator == "&&" && parent.left === compressor.self()) { AST_Node.warn("Dropping side-effect-free && [{file}:{line},{col}]", self.start); return self.left.optimize(compressor); } } // x || false && y ---> x ? y : false if (self.left.operator == "||") { var lr = self.left.right.evaluate(compressor); if (!lr) return make_node(AST_Conditional, self, { condition: self.left.left, consequent: self.right, alternative: self.left.right }).optimize(compressor); } break; case "||": var ll = fuzzy_eval(self.left); if (!ll) { AST_Node.warn("Condition left of || always false [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.left, self.right ]).optimize(compressor); } else if (!(ll instanceof AST_Node)) { AST_Node.warn("Condition left of || always true [{file}:{line},{col}]", self.start); return maintain_this_binding(compressor, parent, compressor.self(), self.left).optimize(compressor); } var rr = self.right.evaluate(compressor); if (!rr) { if (in_bool || parent.operator == "||" && parent.left === compressor.self()) { AST_Node.warn("Dropping side-effect-free || [{file}:{line},{col}]", self.start); return self.left.optimize(compressor); } } else if (!(rr instanceof AST_Node)) { if (in_bool) { AST_Node.warn("Boolean || always true [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.left, make_node(AST_True, self) ]).optimize(compressor); } else self.truthy = true; } if (self.left.operator == "&&") { var lr = self.left.right.evaluate(compressor); if (lr && !(lr instanceof AST_Node)) return make_node(AST_Conditional, self, { condition: self.left.left, consequent: self.left.right, alternative: self.right }).optimize(compressor); } break; } var associative = true; switch (self.operator) { case "+": // "foo" + ("bar" + x) => "foobar" + x if (self.left instanceof AST_Constant && self.right instanceof AST_Binary && self.right.operator == "+" && self.right.left instanceof AST_Constant && self.right.is_string(compressor)) { self = make_node(AST_Binary, self, { operator: "+", left: make_node(AST_String, self.left, { value: "" + self.left.getValue() + self.right.left.getValue(), start: self.left.start, end: self.right.left.end }), right: self.right.right }); } // (x + "foo") + "bar" => x + "foobar" if (self.right instanceof AST_Constant && self.left instanceof AST_Binary && self.left.operator == "+" && self.left.right instanceof AST_Constant && self.left.is_string(compressor)) { self = make_node(AST_Binary, self, { operator: "+", left: self.left.left, right: make_node(AST_String, self.right, { value: "" + self.left.right.getValue() + self.right.getValue(), start: self.left.right.start, end: self.right.end }) }); } // (x + "foo") + ("bar" + y) => (x + "foobar") + y if (self.left instanceof AST_Binary && self.left.operator == "+" && self.left.is_string(compressor) && self.left.right instanceof AST_Constant && self.right instanceof AST_Binary && self.right.operator == "+" && self.right.left instanceof AST_Constant && self.right.is_string(compressor)) { self = make_node(AST_Binary, self, { operator: "+", left: make_node(AST_Binary, self.left, { operator: "+", left: self.left.left, right: make_node(AST_String, self.left.right, { value: "" + self.left.right.getValue() + self.right.left.getValue(), start: self.left.right.start, end: self.right.left.end }) }), right: self.right.right }); } // a + -b => a - b if (self.right instanceof AST_UnaryPrefix && self.right.operator == "-" && self.left.is_number(compressor)) { self = make_node(AST_Binary, self, { operator: "-", left: self.left, right: self.right.expression }); break; } // -a + b => b - a if (self.left instanceof AST_UnaryPrefix && self.left.operator == "-" && reversible() && self.right.is_number(compressor)) { self = make_node(AST_Binary, self, { operator: "-", left: self.right, right: self.left.expression }); break; } case "*": associative = compressor.option("unsafe_math"); case "&": case "|": case "^": // a + +b => +b + a if (self.left.is_number(compressor) && self.right.is_number(compressor) && reversible() && !(self.left instanceof AST_Binary && self.left.operator != self.operator && PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) { var reversed = make_node(AST_Binary, self, { operator: self.operator, left: self.right, right: self.left }); if (self.right instanceof AST_Constant && !(self.left instanceof AST_Constant)) { self = best_of(compressor, reversed, self); } else { self = best_of(compressor, self, reversed); } } if (associative && self.is_number(compressor)) { // a + (b + c) => (a + b) + c if (self.right instanceof AST_Binary && self.right.operator == self.operator) { self = make_node(AST_Binary, self, { operator: self.operator, left: make_node(AST_Binary, self.left, { operator: self.operator, left: self.left, right: self.right.left, start: self.left.start, end: self.right.left.end }), right: self.right.right }); } // (n + 2) + 3 => 5 + n // (2 * n) * 3 => 6 + n if (self.right instanceof AST_Constant && self.left instanceof AST_Binary && self.left.operator == self.operator) { if (self.left.left instanceof AST_Constant) { self = make_node(AST_Binary, self, { operator: self.operator, left: make_node(AST_Binary, self.left, { operator: self.operator, left: self.left.left, right: self.right, start: self.left.left.start, end: self.right.end }), right: self.left.right }); } else if (self.left.right instanceof AST_Constant) { self = make_node(AST_Binary, self, { operator: self.operator, left: make_node(AST_Binary, self.left, { operator: self.operator, left: self.left.right, right: self.right, start: self.left.right.start, end: self.right.end }), right: self.left.left }); } } // (a | 1) | (2 | d) => (3 | a) | b if (self.left instanceof AST_Binary && self.left.operator == self.operator && self.left.right instanceof AST_Constant && self.right instanceof AST_Binary && self.right.operator == self.operator && self.right.left instanceof AST_Constant) { self = make_node(AST_Binary, self, { operator: self.operator, left: make_node(AST_Binary, self.left, { operator: self.operator, left: make_node(AST_Binary, self.left.left, { operator: self.operator, left: self.left.right, right: self.right.left, start: self.left.right.start, end: self.right.left.end }), right: self.left.left }), right: self.right.right }); } } } } if (compressor.option("unsafe")) { var indexRight = is_indexFn(self.right); if (in_bool && indexRight && (self.operator == "==" || self.operator == "!=") && self.left instanceof AST_Number && self.left.getValue() == 0) { return (self.operator == "==" ? make_node(AST_UnaryPrefix, self, { operator: "!", expression: self.right }) : self.right).optimize(compressor); } var indexLeft = is_indexFn(self.left); if (compressor.option("comparisons") && is_indexOf_match_pattern()) { var node = make_node(AST_UnaryPrefix, self, { operator: "!", expression: make_node(AST_UnaryPrefix, self, { operator: "~", expression: indexLeft ? self.left : self.right }) }); switch (self.operator) { case "<": if (indexLeft) break; case "<=": case "!=": node = make_node(AST_UnaryPrefix, self, { operator: "!", expression: node }); break; } return node.optimize(compressor); } } // x && (y && z) ==> x && y && z // x || (y || z) ==> x || y || z // x + ("y" + z) ==> x + "y" + z // "x" + (y + "z")==> "x" + y + "z" if (self.right instanceof AST_Binary && self.right.operator == self.operator && (lazy_op[self.operator] || (self.operator == "+" && (self.right.left.is_string(compressor) || (self.left.is_string(compressor) && self.right.right.is_string(compressor)))))) { self.left = make_node(AST_Binary, self.left, { operator : self.operator, left : self.left, right : self.right.left }); self.right = self.right.right; return self.transform(compressor); } var ev = self.evaluate(compressor); if (ev !== self) { ev = make_node_from_constant(ev, self).optimize(compressor); return best_of(compressor, ev, self); } return self; function fuzzy_eval(node) { if (node.truthy) return true; if (node.falsy) return false; if (node.is_truthy()) return true; return node.evaluate(compressor); } function is_indexFn(node) { return node instanceof AST_Call && node.expression instanceof AST_Dot && indexFns[node.expression.property]; } function is_indexOf_match_pattern() { switch (self.operator) { case "<=": // 0 <= array.indexOf(string) => !!~array.indexOf(string) return indexRight && self.left instanceof AST_Number && self.left.getValue() == 0; case "<": // array.indexOf(string) < 0 => !~array.indexOf(string) if (indexLeft && self.right instanceof AST_Number && self.right.getValue() == 0) return true; // -1 < array.indexOf(string) => !!~array.indexOf(string) case "==": case "!=": // -1 == array.indexOf(string) => !~array.indexOf(string) // -1 != array.indexOf(string) => !!~array.indexOf(string) if (!indexRight) return false; return self.left instanceof AST_Number && self.left.getValue() == -1 || self.left instanceof AST_UnaryPrefix && self.left.operator == "-" && self.left.expression instanceof AST_Number && self.left.expression.getValue() == 1; } } }); function recursive_ref(compressor, def) { var node; for (var i = 0; node = compressor.parent(i); i++) { if (node instanceof AST_Lambda) { var name = node.name; if (name && name.definition() === def) break; } } return node; } OPT(AST_SymbolRef, function(self, compressor) { if (!compressor.option("ie8") && is_undeclared_ref(self) // testing against `self.scope.uses_with` is an optimization && !(self.scope.uses_with && compressor.find_parent(AST_With))) { switch (self.name) { case "undefined": return make_node(AST_Undefined, self).optimize(compressor); case "NaN": return make_node(AST_NaN, self).optimize(compressor); case "Infinity": return make_node(AST_Infinity, self).optimize(compressor); } } var parent = compressor.parent(); if (compressor.option("reduce_vars") && is_lhs(compressor.self(), parent) !== compressor.self()) { var def = self.definition(); var fixed = self.fixed_value(); var single_use = def.single_use && !(parent instanceof AST_Call && parent.is_expr_pure(compressor)); if (single_use && fixed instanceof AST_Lambda) { if (def.scope !== self.scope && (!compressor.option("reduce_funcs") || def.escaped.depth == 1 || fixed.inlined)) { single_use = false; } else if (recursive_ref(compressor, def)) { single_use = false; } else if (def.scope !== self.scope || def.orig[0] instanceof AST_SymbolFunarg) { single_use = fixed.is_constant_expression(self.scope); if (single_use == "f") { var scope = self.scope; do if (scope instanceof AST_Defun || scope instanceof AST_Function) { scope.inlined = true; } while (scope = scope.parent_scope); } } } if (single_use && fixed) { def.single_use = false; fixed._squeezed = true; if (fixed instanceof AST_Defun) { fixed = make_node(AST_Function, fixed, fixed); fixed.name = make_node(AST_SymbolLambda, fixed.name, fixed.name); } var value; if (def.recursive_refs > 0) { value = fixed.clone(true); var defun_def = value.name.definition(); var lambda_def = value.variables.get(value.name.name); var name = lambda_def && lambda_def.orig[0]; if (!(name instanceof AST_SymbolLambda)) { name = make_node(AST_SymbolLambda, value.name, value.name); name.scope = value; value.name = name; lambda_def = value.def_function(name); } value.walk(new TreeWalker(function(node) { if (!(node instanceof AST_SymbolRef)) return; var def = node.definition(); if (def === defun_def) { node.thedef = lambda_def; lambda_def.references.push(node); } else { def.single_use = false; var fn = node.fixed_value(); if (!(fn instanceof AST_Lambda)) return; if (!fn.name) return; var fn_def = fn.name.definition(); if (fn_def.scope !== fn.name.scope) return; if (fixed.variables.get(fn.name.name) !== fn_def) return; fn.name = fn.name.clone(); var value_def = value.variables.get(fn.name.name) || value.def_function(fn.name); node.thedef = value_def; value_def.references.push(node); } })); } else { value = fixed.optimize(compressor); } def.replaced++; return value; } if (fixed && def.should_replace === undefined) { var init; if (fixed instanceof AST_This) { if (!(def.orig[0] instanceof AST_SymbolFunarg) && all(def.references, function(ref) { return def.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 (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 = def.name.length; var overhead = 0; if (compressor.option("unused") && !compressor.exposed(def)) { overhead = (name_length + 2 + value_length) / (def.references.length - def.assignments); } def.should_replace = value_length <= name_length + overhead ? fn : false; } else { def.should_replace = false; } } if (def.should_replace) { var value = def.should_replace(); def.replaced++; return value; } } return self; function has_symbol_ref(value) { var found; value.walk(new TreeWalker(function(node) { if (node instanceof AST_SymbolRef) found = true; if (found) return true; })); return found; } }); function is_atomic(lhs, self) { return lhs instanceof AST_SymbolRef || lhs.TYPE === self.TYPE; } OPT(AST_Undefined, function(self, compressor) { if (compressor.option("unsafe_undefined")) { var undef = find_variable(compressor, "undefined"); if (undef) { var ref = make_node(AST_SymbolRef, self, { name : "undefined", scope : undef.scope, thedef : undef }); ref.is_undefined = true; return ref; } } var lhs = is_lhs(compressor.self(), compressor.parent()); if (lhs && is_atomic(lhs, self)) return self; return make_node(AST_UnaryPrefix, self, { operator: "void", expression: make_node(AST_Number, self, { value: 0 }) }); }); OPT(AST_Infinity, function(self, compressor) { var lhs = is_lhs(compressor.self(), compressor.parent()); if (lhs && is_atomic(lhs, self)) return self; if (compressor.option("keep_infinity") && !(lhs && !is_atomic(lhs, self)) && !find_variable(compressor, "Infinity")) return self; return make_node(AST_Binary, self, { operator: "/", left: make_node(AST_Number, self, { value: 1 }), right: make_node(AST_Number, self, { value: 0 }) }); }); OPT(AST_NaN, function(self, compressor) { var lhs = is_lhs(compressor.self(), compressor.parent()); if (lhs && !is_atomic(lhs, self) || find_variable(compressor, "NaN")) { return make_node(AST_Binary, self, { operator: "/", left: make_node(AST_Number, self, { value: 0 }), right: make_node(AST_Number, self, { value: 0 }) }); } return self; }); function is_reachable(self, defs) { var reachable = false; var find_ref = new TreeWalker(function(node) { if (reachable) return true; if (node instanceof AST_SymbolRef && member(node.definition(), defs)) { return reachable = true; } }); var scan_scope = new TreeWalker(function(node) { if (reachable) return true; if (node instanceof AST_Scope && node !== self) { var parent = scan_scope.parent(); if (parent instanceof AST_Call && parent.expression === node) return; node.walk(find_ref); return true; } }); self.walk(scan_scope); return reachable; } var ASSIGN_OPS = makePredicate("+ - / * % >> << >>> | ^ &"); var ASSIGN_OPS_COMMUTATIVE = makePredicate("* | ^ &"); OPT(AST_Assign, function(self, compressor) { var def; if (compressor.option("dead_code") && self.left instanceof AST_SymbolRef && (def = self.left.definition()).scope === compressor.find_parent(AST_Lambda)) { if (self.left.is_immutable()) return strip_assignment(); var level = 0, node, parent = self; do { node = parent; parent = compressor.parent(level++); if (parent instanceof AST_Exit) { if (in_try(level, parent)) break; if (is_reachable(def.scope, [ def ])) break; def.fixed = false; return strip_assignment(); } } while (parent instanceof AST_Binary && parent.right === node || parent instanceof AST_Sequence && parent.tail_node() === node || parent instanceof AST_UnaryPrefix); } self = self.lift_sequences(compressor); if (!compressor.option("assignments")) return self; if (self.operator == "=" && self.left instanceof AST_SymbolRef && self.right instanceof AST_Binary) { // x = expr1 OP expr2 if (self.right.left instanceof AST_SymbolRef && self.right.left.name == self.left.name && ASSIGN_OPS[self.right.operator]) { // x = x - 2 ---> x -= 2 self.operator = self.right.operator + "="; self.right = self.right.right; } else if (self.right.right instanceof AST_SymbolRef && self.right.right.name == self.left.name && ASSIGN_OPS_COMMUTATIVE[self.right.operator] && !self.right.left.has_side_effects(compressor)) { // x = 2 & x ---> x &= 2 self.operator = self.right.operator + "="; self.right = self.right.left; } } if ((self.operator == "+=" || self.operator == "-=") && self.left.is_number(compressor) && self.right instanceof AST_Number && self.right.getValue() === 1) { var op = self.operator.slice(0, -1); return make_node(AST_UnaryPrefix, self, { operator: op + op, expression: self.left }); } return self; function in_try(level, node) { var right = self.right; self.right = make_node(AST_Null, right); var may_throw = node.may_throw(compressor); self.right = right; var scope = self.left.definition().scope; var parent; while ((parent = compressor.parent(level++)) !== scope) { if (parent instanceof AST_Try) { if (parent.bfinally) return true; if (may_throw && parent.bcatch) return true; } } } function strip_assignment() { return (self.operator != "=" ? make_node(AST_Binary, self, { operator: self.operator.slice(0, -1), left: self.left, right: self.right }) : maintain_this_binding(compressor, compressor.parent(), self, self.right)).optimize(compressor); } }); OPT(AST_Conditional, function(self, compressor) { if (!compressor.option("conditionals")) return self; // This looks like lift_sequences(), should probably be under "sequences" if (self.condition instanceof AST_Sequence) { var expressions = self.condition.expressions.slice(); self.condition = expressions.pop(); expressions.push(self); return make_sequence(self, expressions); } var cond = self.condition.is_truthy() || self.condition.tail_node().evaluate(compressor); if (!cond) { AST_Node.warn("Condition always false [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.condition, self.alternative ]).optimize(compressor); } else if (!(cond instanceof AST_Node)) { AST_Node.warn("Condition always true [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.condition, self.consequent ]).optimize(compressor); } var negated = cond.negate(compressor, first_in_statement(compressor)); if (best_of(compressor, cond, negated) === negated) { self = make_node(AST_Conditional, self, { condition: negated, consequent: self.alternative, alternative: self.consequent }); } var condition = self.condition; var consequent = self.consequent; var alternative = self.alternative; // x?x:y --> x||y if (condition instanceof AST_SymbolRef && consequent instanceof AST_SymbolRef && condition.definition() === consequent.definition()) { return make_node(AST_Binary, self, { operator: "||", left: condition, right: alternative }); } // if (foo) exp = something; else exp = something_else; // | // v // exp = foo ? something : something_else; var seq_tail = consequent.tail_node(); if (seq_tail instanceof AST_Assign) { var is_eq = seq_tail.operator == "="; var alt_tail = is_eq ? alternative.tail_node() : alternative; if ((is_eq || consequent === seq_tail) && alt_tail instanceof AST_Assign && seq_tail.operator == alt_tail.operator && seq_tail.left.equivalent_to(alt_tail.left) && (is_eq && !seq_tail.left.has_side_effects(compressor) || !condition.has_side_effects(compressor) && can_shift_lhs_of_tail(consequent) && can_shift_lhs_of_tail(alternative))) { return make_node(AST_Assign, self, { operator: seq_tail.operator, left: seq_tail.left, right: make_node(AST_Conditional, self, { condition: condition, consequent: pop_lhs(consequent), alternative: pop_lhs(alternative) }) }); } } // x ? y(a) : y(b) --> y(x ? a : b) var arg_index; if (consequent instanceof AST_Call && alternative.TYPE === consequent.TYPE && consequent.args.length > 0 && consequent.args.length == alternative.args.length && consequent.expression.equivalent_to(alternative.expression) && !condition.has_side_effects(compressor) && !consequent.expression.has_side_effects(compressor) && typeof (arg_index = single_arg_diff()) == "number") { var node = consequent.clone(); node.args[arg_index] = make_node(AST_Conditional, self, { condition: condition, consequent: consequent.args[arg_index], alternative: alternative.args[arg_index] }); return node; } // x?y?z:a:a --> x&&y?z:a if (consequent instanceof AST_Conditional && consequent.alternative.equivalent_to(alternative)) { return make_node(AST_Conditional, self, { condition: make_node(AST_Binary, self, { left: condition, operator: "&&", right: consequent.condition }), consequent: consequent.consequent, alternative: alternative }); } // x ? y : y --> x, y if (consequent.equivalent_to(alternative)) { return make_sequence(self, [ condition, consequent ]).optimize(compressor); } // x ? (y, w) : (z, w) --> x ? y : z, w if ((consequent instanceof AST_Sequence || alternative instanceof AST_Sequence) && consequent.tail_node().equivalent_to(alternative.tail_node())) { return make_sequence(self, [ make_node(AST_Conditional, self, { condition: condition, consequent: pop_seq(consequent), alternative: pop_seq(alternative) }), consequent.tail_node() ]).optimize(compressor); } // x ? y || z : z --> x && y || z if (consequent instanceof AST_Binary && consequent.operator == "||" && consequent.right.equivalent_to(alternative)) { return make_node(AST_Binary, self, { operator: "||", left: make_node(AST_Binary, self, { operator: "&&", left: condition, right: consequent.left }), right: alternative }).optimize(compressor); } var in_bool = compressor.option("booleans") && compressor.in_boolean_context(); if (is_true(self.consequent)) { if (is_false(self.alternative)) { // c ? true : false ---> !!c return booleanize(condition); } // c ? true : x ---> !!c || x return make_node(AST_Binary, self, { operator: "||", left: booleanize(condition), right: self.alternative }); } if (is_false(self.consequent)) { if (is_true(self.alternative)) { // c ? false : true ---> !c return booleanize(condition.negate(compressor)); } // c ? false : x ---> !c && x return make_node(AST_Binary, self, { operator: "&&", left: booleanize(condition.negate(compressor)), right: self.alternative }); } if (is_true(self.alternative)) { // c ? x : true ---> !c || x return make_node(AST_Binary, self, { operator: "||", left: booleanize(condition.negate(compressor)), right: self.consequent }); } if (is_false(self.alternative)) { // c ? x : false ---> !!c && x return make_node(AST_Binary, self, { operator: "&&", left: booleanize(condition), right: self.consequent }); } return self; function booleanize(node) { if (node.is_boolean(compressor)) return node; // !!expression return make_node(AST_UnaryPrefix, node, { operator: "!", expression: node.negate(compressor) }); } // AST_True or !0 function is_true(node) { return node instanceof AST_True || in_bool && node instanceof AST_Constant && node.getValue() || (node instanceof AST_UnaryPrefix && node.operator == "!" && node.expression instanceof AST_Constant && !node.expression.getValue()); } // AST_False or !1 function is_false(node) { return node instanceof AST_False || in_bool && node instanceof AST_Constant && !node.getValue() || (node instanceof AST_UnaryPrefix && node.operator == "!" && node.expression instanceof AST_Constant && node.expression.getValue()); } function single_arg_diff() { var a = consequent.args; var b = alternative.args; for (var i = 0, len = a.length; i < len; i++) { if (!a[i].equivalent_to(b[i])) { for (var j = i + 1; j < len; j++) { if (!a[j].equivalent_to(b[j])) return; } return i; } } } function can_shift_lhs_of_tail(node) { if (node === node.tail_node()) return true; var exprs = node.expressions; for (var i = exprs.length - 1; --i >= 0;) { var expr = exprs[i]; if (!(expr instanceof AST_Assign) && expr.has_side_effects(compressor) || expr.operator != "=" || expr.left.has_side_effects(compressor) || expr.right.has_side_effects(compressor)) return false; } return true; } function pop_lhs(node) { if (!(node instanceof AST_Sequence)) return node.right; var exprs = node.expressions.slice(); exprs.push(exprs.pop().right); return make_sequence(node, exprs); } function pop_seq(node) { if (!(node instanceof AST_Sequence)) return make_node(AST_Number, node, { value: 0 }); return make_sequence(node, node.expressions.slice(0, -1)); } }); OPT(AST_Boolean, function(self, compressor) { if (!compressor.option("booleans")) return self; if (compressor.in_boolean_context()) return make_node(AST_Number, self, { value: +self.value }); var p = compressor.parent(); if (p instanceof AST_Binary && (p.operator == "==" || p.operator == "!=")) { AST_Node.warn("Non-strict equality against boolean: {operator} {value} [{file}:{line},{col}]", { operator : p.operator, value : self.value, file : p.start.file, line : p.start.line, col : p.start.col, }); return make_node(AST_Number, self, { value: +self.value }); } return make_node(AST_UnaryPrefix, self, { operator: "!", expression: make_node(AST_Number, self, { value: 1 - self.value }) }); }); function safe_to_flatten(value, compressor) { if (value instanceof AST_SymbolRef) { value = value.fixed_value(); } if (!value) return false; return !(value instanceof AST_Lambda) || compressor.parent() instanceof AST_New || !value.contains_this(); } OPT(AST_Sub, function(self, compressor) { var expr = self.expression; var prop = self.property; if (compressor.option("properties")) { var key = prop.evaluate(compressor); if (key !== prop) { if (typeof key == "string") { if (key == "undefined") { key = undefined; } else { var value = parseFloat(key); if (value.toString() == key) { key = value; } } } prop = self.property = best_of_expression(prop, make_node_from_constant(key, prop).transform(compressor)); 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); } } } var parent = compressor.parent(); var def, fn, fn_parent; if (compressor.option("arguments") && expr instanceof AST_SymbolRef && is_arguments(def = expr.definition()) && prop instanceof AST_Number && (fn = expr.scope) === find_lambda()) { var index = prop.getValue(); if (parent instanceof AST_UnaryPrefix && parent.operator == "delete") { if (!def.deleted) def.deleted = []; def.deleted[index] = true; } var argname = fn.argnames[index]; if (def.deleted && def.deleted[index]) { argname = null; } else if (argname && compressor.has_directive("use strict")) { var arg_def = argname.definition(); if (!compressor.option("reduce_vars") || def.reassigned || arg_def.assignments || arg_def.orig.length > 1) { argname = null; } } else if (!argname && index < fn.argnames.length + 5 && compressor.drop_fargs(fn, fn_parent)) { while (index >= fn.argnames.length) { argname = make_node(AST_SymbolFunarg, fn, { name: fn.make_var_name("argument_" + fn.argnames.length), scope: fn }); fn.argnames.push(argname); fn.enclosed.push(fn.def_variable(argname)); } } if (argname && find_if(function(node) { return node.name === argname.name; }, fn.argnames) === argname) { def.reassigned = false; var sym = make_node(AST_SymbolRef, self, argname); sym.reference({}); delete argname.__unused; return sym; } } if (is_lhs(compressor.self(), 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; var retValue = elements[index]; if (safe_to_flatten(retValue, compressor)) { 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; } } 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 }) }); } } var ev = self.evaluate(compressor); if (ev !== self) { ev = make_node_from_constant(ev, self).optimize(compressor); return best_of(compressor, ev, self); } return self; function find_lambda() { var i = 0, p; while (p = compressor.parent(i++)) { if (p instanceof AST_Lambda) { fn_parent = compressor.parent(i); return p; } } } }); AST_Scope.DEFMETHOD("contains_this", function() { var result; var self = this; self.walk(new TreeWalker(function(node) { if (result) return true; if (node instanceof AST_This) return result = true; if (node !== self && node instanceof AST_Scope) return true; })); 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; if (!safe_to_flatten(prop.value, compressor)) 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) { if (self.property == "arguments" || self.property == "caller") { AST_Node.warn("Function.prototype.{prop} not supported [{file}:{line},{col}]", { prop: self.property, file: self.start.file, line: self.start.line, col: self.start.col }); } if (is_lhs(compressor.self(), compressor.parent())) return self; if (compressor.option("unsafe_proto") && self.expression instanceof AST_Dot && self.expression.property == "prototype") { var exp = self.expression.expression; if (is_undeclared_ref(exp)) switch (exp.name) { case "Array": self.expression = make_node(AST_Array, self.expression, { elements: [] }); break; case "Function": self.expression = make_node(AST_Function, self.expression, { argnames: [], body: [] }); break; case "Number": self.expression = make_node(AST_Number, self.expression, { value: 0 }); break; case "Object": self.expression = make_node(AST_Object, self.expression, { properties: [] }); break; case "RegExp": self.expression = make_node(AST_RegExp, self.expression, { value: /t/ }); break; case "String": self.expression = make_node(AST_String, self.expression, { value: "" }); break; } } 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); return best_of(compressor, ev, self); } return self; }); OPT(AST_Return, function(self, compressor) { if (self.value && is_undefined(self.value, compressor)) { self.value = null; } return self; }); })(function(node, optimizer) { node.DEFMETHOD("optimize", function(compressor) { var self = this; if (self._optimized) return self; if (compressor.has_directive("use asm")) return self; var opt = optimizer(self, compressor); opt._optimized = true; return opt; }); }); UglifyJS2-3.6.3/lib/minify.js000066400000000000000000000236701355252637300157230ustar00rootroot00000000000000"use strict"; var to_ascii, to_base64; if (typeof Buffer == "undefined") { to_ascii = atob; to_base64 = btoa; } else if (typeof Buffer.alloc == "undefined") { to_ascii = function(b64) { return new Buffer(b64, "base64").toString(); }; to_base64 = function(str) { return new Buffer(str).toString("base64"); }; } else { to_ascii = function(b64) { return Buffer.from(b64, "base64").toString(); }; to_base64 = function(str) { return Buffer.from(str).toString("base64"); }; } function read_source_map(name, toplevel) { var comments = toplevel.end.comments_after; for (var i = comments.length; --i >= 0;) { var comment = comments[i]; if (comment.type != "comment1") break; var match = /^# ([^\s=]+)=(\S+)\s*$/.exec(comment.value); if (!match) break; if (match[1] == "sourceMappingURL") { match = /^data:application\/json(;.*?)?;base64,(\S+)$/.exec(match[2]); if (!match) break; return to_ascii(match[2]); } } AST_Node.warn("inline source map not found: " + name); } function parse_source_map(content) { try { return JSON.parse(content); } catch (ex) { throw new Error("invalid input source map: " + content); } } function set_shorthand(name, options, keys) { if (options[name]) { keys.forEach(function(key) { if (options[key]) { if (typeof options[key] != "object") options[key] = {}; if (!(name in options[key])) options[key][name] = options[name]; } }); } } function init_cache(cache) { if (!cache) return; if (!("props" in cache)) { cache.props = new Dictionary(); } else if (!(cache.props instanceof Dictionary)) { cache.props = Dictionary.fromObject(cache.props); } } function to_json(cache) { return { props: cache.props.toObject() }; } function minify(files, options) { try { options = defaults(options, { compress: {}, enclose: false, ie8: false, keep_fnames: false, mangle: {}, nameCache: null, output: {}, parse: {}, rename: undefined, sourceMap: false, timings: false, toplevel: false, warnings: false, wrap: false, }, true); var timings = options.timings && { start: Date.now() }; if (options.rename === undefined) { options.rename = options.compress && options.mangle; } set_shorthand("ie8", options, [ "compress", "mangle", "output" ]); set_shorthand("keep_fnames", options, [ "compress", "mangle" ]); set_shorthand("toplevel", options, [ "compress", "mangle" ]); var quoted_props; if (options.mangle) { options.mangle = defaults(options.mangle, { cache: options.nameCache && (options.nameCache.vars || {}), eval: false, ie8: false, keep_fnames: false, properties: false, reserved: [], toplevel: false, }, true); if (options.mangle.properties) { if (typeof options.mangle.properties != "object") { options.mangle.properties = {}; } if (options.mangle.properties.keep_quoted) { quoted_props = options.mangle.properties.reserved; if (!Array.isArray(quoted_props)) quoted_props = []; options.mangle.properties.reserved = quoted_props; } if (options.nameCache && !("cache" in options.mangle.properties)) { options.mangle.properties.cache = options.nameCache.props || {}; } } init_cache(options.mangle.cache); init_cache(options.mangle.properties.cache); } if (options.sourceMap) { options.sourceMap = defaults(options.sourceMap, { content: null, filename: null, includeSources: false, root: null, url: null, }, true); } var warnings = []; if (options.warnings) AST_Node.log_function(function(warning) { warnings.push(warning); }, options.warnings == "verbose"); if (timings) timings.parse = Date.now(); var source_maps, toplevel; if (files instanceof AST_Toplevel) { toplevel = files; } else { if (typeof files == "string") { files = [ files ]; } options.parse = options.parse || {}; options.parse.toplevel = null; var source_map_content = options.sourceMap && options.sourceMap.content; if (typeof source_map_content == "string" && source_map_content != "inline") { source_map_content = parse_source_map(source_map_content); } source_maps = source_map_content && Object.create(null); for (var name in files) if (HOP(files, name)) { options.parse.filename = name; options.parse.toplevel = toplevel = parse(files[name], options.parse); if (source_maps) { if (source_map_content == "inline") { var inlined_content = read_source_map(name, toplevel); if (inlined_content) { source_maps[name] = parse_source_map(inlined_content); } } else { source_maps[name] = source_map_content; } } } } if (quoted_props) { reserve_quoted_keys(toplevel, quoted_props); } [ "enclose", "wrap" ].forEach(function(action) { var option = options[action]; if (!option) return; var orig = toplevel.print_to_string().slice(0, -1); toplevel = toplevel[action](option); files[toplevel.start.file] = toplevel.print_to_string().replace(orig, ""); }); if (timings) timings.rename = Date.now(); if (options.rename) { toplevel.figure_out_scope(options.mangle); toplevel.expand_names(options.mangle); } if (timings) timings.compress = Date.now(); if (options.compress) toplevel = new Compressor(options.compress).compress(toplevel); if (timings) timings.scope = Date.now(); if (options.mangle) toplevel.figure_out_scope(options.mangle); if (timings) timings.mangle = Date.now(); if (options.mangle) { toplevel.compute_char_frequency(options.mangle); toplevel.mangle_names(options.mangle); } if (timings) timings.properties = Date.now(); if (options.mangle && options.mangle.properties) { toplevel = mangle_properties(toplevel, options.mangle.properties); } if (timings) timings.output = Date.now(); var result = {}; if (options.output.ast) { result.ast = toplevel; } if (!HOP(options.output, "code") || options.output.code) { if (options.sourceMap) { options.output.source_map = SourceMap({ file: options.sourceMap.filename, orig: source_maps, root: options.sourceMap.root }); if (options.sourceMap.includeSources) { if (files instanceof AST_Toplevel) { throw new Error("original source content unavailable"); } else for (var name in files) if (HOP(files, name)) { options.output.source_map.get().setSourceContent(name, files[name]); } } else { options.output.source_map.get()._sourcesContents = null; } } delete options.output.ast; delete options.output.code; var stream = OutputStream(options.output); toplevel.print(stream); result.code = stream.get(); if (options.sourceMap) { result.map = options.output.source_map.toString(); var url = options.sourceMap.url; if (url) { result.code = result.code.replace(/\n\/\/# sourceMappingURL=\S+\s*$/, ""); if (url == "inline") { result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(result.map); } else { result.code += "\n//# sourceMappingURL=" + url; } } } } if (options.nameCache && options.mangle) { if (options.mangle.cache) options.nameCache.vars = to_json(options.mangle.cache); if (options.mangle.properties && options.mangle.properties.cache) { options.nameCache.props = to_json(options.mangle.properties.cache); } } if (timings) { timings.end = Date.now(); result.timings = { parse: 1e-3 * (timings.rename - timings.parse), rename: 1e-3 * (timings.compress - timings.rename), compress: 1e-3 * (timings.scope - timings.compress), scope: 1e-3 * (timings.mangle - timings.scope), mangle: 1e-3 * (timings.properties - timings.mangle), properties: 1e-3 * (timings.output - timings.properties), output: 1e-3 * (timings.end - timings.output), total: 1e-3 * (timings.end - timings.start) } } if (warnings.length) { result.warnings = warnings; } return result; } catch (ex) { return { error: ex }; } } UglifyJS2-3.6.3/lib/mozilla-ast.js000066400000000000000000000550051355252637300166610ustar00rootroot00000000000000/*********************************************************************** A JavaScript tokenizer / parser / beautifier / compressor. https://github.com/mishoo/UglifyJS2 -------------------------------- (C) --------------------------------- Author: Mihai Bazon http://mihai.bazon.net/blog Distributed under the BSD license: Copyright 2012 (c) Mihai Bazon Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***********************************************************************/ "use strict"; (function() { function normalize_directives(body) { var in_directive = true; for (var i = 0; i < body.length; i++) { if (in_directive && body[i] instanceof AST_Statement && body[i].body instanceof AST_String) { body[i] = new AST_Directive({ start: body[i].start, end: body[i].end, value: body[i].body.value }); } else if (in_directive && !(body[i] instanceof AST_Statement && body[i].body instanceof AST_String)) { in_directive = false; } } return body; } var MOZ_TO_ME = { Program: function(M) { return new AST_Toplevel({ start: my_start_token(M), end: my_end_token(M), body: normalize_directives(M.body.map(from_moz)) }); }, FunctionDeclaration: function(M) { return new AST_Defun({ start: my_start_token(M), end: my_end_token(M), name: from_moz(M.id), argnames: M.params.map(from_moz), body: normalize_directives(from_moz(M.body).body) }); }, FunctionExpression: function(M) { return new AST_Function({ start: my_start_token(M), end: my_end_token(M), name: from_moz(M.id), argnames: M.params.map(from_moz), body: normalize_directives(from_moz(M.body).body) }); }, ExpressionStatement: function(M) { return new AST_SimpleStatement({ start: my_start_token(M), end: my_end_token(M), body: from_moz(M.expression) }); }, TryStatement: function(M) { var handlers = M.handlers || [M.handler]; if (handlers.length > 1 || M.guardedHandlers && M.guardedHandlers.length) { throw new Error("Multiple catch clauses are not supported."); } return new AST_Try({ start : my_start_token(M), end : my_end_token(M), body : from_moz(M.block).body, bcatch : from_moz(handlers[0]), bfinally : M.finalizer ? new AST_Finally(from_moz(M.finalizer)) : null }); }, Property: function(M) { var key = M.key; var args = { start : my_start_token(key), end : my_end_token(M.value), key : key.type == "Identifier" ? key.name : key.value, value : from_moz(M.value) }; if (M.kind == "init") return new AST_ObjectKeyVal(args); args.key = new AST_SymbolAccessor({ name: args.key }); args.value = new AST_Accessor(args.value); if (M.kind == "get") return new AST_ObjectGetter(args); if (M.kind == "set") return new AST_ObjectSetter(args); }, ArrayExpression: function(M) { return new AST_Array({ start : my_start_token(M), end : my_end_token(M), elements : M.elements.map(function(elem) { return elem === null ? new AST_Hole() : from_moz(elem); }) }); }, ObjectExpression: function(M) { return new AST_Object({ start : my_start_token(M), end : my_end_token(M), properties : M.properties.map(function(prop) { prop.type = "Property"; return from_moz(prop) }) }); }, SequenceExpression: function(M) { return new AST_Sequence({ start : my_start_token(M), end : my_end_token(M), expressions: M.expressions.map(from_moz) }); }, MemberExpression: function(M) { return new (M.computed ? AST_Sub : AST_Dot)({ start : my_start_token(M), end : my_end_token(M), property : M.computed ? from_moz(M.property) : M.property.name, expression : from_moz(M.object) }); }, SwitchCase: function(M) { return new (M.test ? AST_Case : AST_Default)({ start : my_start_token(M), end : my_end_token(M), expression : from_moz(M.test), body : M.consequent.map(from_moz) }); }, VariableDeclaration: function(M) { return new AST_Var({ start : my_start_token(M), end : my_end_token(M), definitions : M.declarations.map(from_moz) }); }, Literal: function(M) { var val = M.value, args = { start : my_start_token(M), end : my_end_token(M) }; if (val === null) return new AST_Null(args); var rx = M.regex; if (rx && rx.pattern) { // RegExpLiteral as per ESTree AST spec args.value = new RegExp(rx.pattern, rx.flags); args.value.raw_source = rx.pattern; return new AST_RegExp(args); } else if (rx) { // support legacy RegExp args.value = M.regex && M.raw ? M.raw : val; return new AST_RegExp(args); } switch (typeof val) { case "string": args.value = val; return new AST_String(args); case "number": args.value = val; return new AST_Number(args); case "boolean": return new (val ? AST_True : AST_False)(args); } }, Identifier: function(M) { var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2]; return new ( p.type == "LabeledStatement" ? AST_Label : p.type == "VariableDeclarator" && p.id === M ? AST_SymbolVar : p.type == "FunctionExpression" ? (p.id === M ? AST_SymbolLambda : AST_SymbolFunarg) : p.type == "FunctionDeclaration" ? (p.id === M ? AST_SymbolDefun : AST_SymbolFunarg) : p.type == "CatchClause" ? AST_SymbolCatch : p.type == "BreakStatement" || p.type == "ContinueStatement" ? AST_LabelRef : AST_SymbolRef)({ start : my_start_token(M), end : my_end_token(M), name : M.name }); } }; MOZ_TO_ME.UpdateExpression = MOZ_TO_ME.UnaryExpression = function To_Moz_Unary(M) { var prefix = "prefix" in M ? M.prefix : M.type == "UnaryExpression" ? true : false; return new (prefix ? AST_UnaryPrefix : AST_UnaryPostfix)({ start : my_start_token(M), end : my_end_token(M), operator : M.operator, expression : from_moz(M.argument) }); }; map("EmptyStatement", AST_EmptyStatement); map("BlockStatement", AST_BlockStatement, "body@body"); map("IfStatement", AST_If, "test>condition, consequent>body, alternate>alternative"); map("LabeledStatement", AST_LabeledStatement, "label>label, body>body"); map("BreakStatement", AST_Break, "label>label"); map("ContinueStatement", AST_Continue, "label>label"); map("WithStatement", AST_With, "object>expression, body>body"); map("SwitchStatement", AST_Switch, "discriminant>expression, cases@body"); map("ReturnStatement", AST_Return, "argument>value"); map("ThrowStatement", AST_Throw, "argument>value"); map("WhileStatement", AST_While, "test>condition, body>body"); map("DoWhileStatement", AST_Do, "test>condition, body>body"); map("ForStatement", AST_For, "init>init, test>condition, update>step, body>body"); map("ForInStatement", AST_ForIn, "left>init, right>object, body>body"); map("DebuggerStatement", AST_Debugger); map("VariableDeclarator", AST_VarDef, "id>name, init>value"); map("CatchClause", AST_Catch, "param>argname, body%body"); map("ThisExpression", AST_This); map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right"); map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right"); map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right"); map("ConditionalExpression", AST_Conditional, "test>condition, consequent>consequent, alternate>alternative"); map("NewExpression", AST_New, "callee>expression, arguments@args"); map("CallExpression", AST_Call, "callee>expression, arguments@args"); def_to_moz(AST_Toplevel, function To_Moz_Program(M) { return to_moz_scope("Program", M); }); def_to_moz(AST_Defun, function To_Moz_FunctionDeclaration(M) { return { type: "FunctionDeclaration", id: to_moz(M.name), params: M.argnames.map(to_moz), body: to_moz_scope("BlockStatement", M) } }); def_to_moz(AST_Function, function To_Moz_FunctionExpression(M) { return { type: "FunctionExpression", id: to_moz(M.name), params: M.argnames.map(to_moz), body: to_moz_scope("BlockStatement", M) } }); def_to_moz(AST_Directive, function To_Moz_Directive(M) { return { type: "ExpressionStatement", expression: { type: "Literal", value: M.value } }; }); def_to_moz(AST_SimpleStatement, function To_Moz_ExpressionStatement(M) { return { type: "ExpressionStatement", expression: to_moz(M.body) }; }); def_to_moz(AST_SwitchBranch, function To_Moz_SwitchCase(M) { return { type: "SwitchCase", test: to_moz(M.expression), consequent: M.body.map(to_moz) }; }); def_to_moz(AST_Try, function To_Moz_TryStatement(M) { return { type: "TryStatement", block: to_moz_block(M), handler: to_moz(M.bcatch), guardedHandlers: [], finalizer: to_moz(M.bfinally) }; }); def_to_moz(AST_Catch, function To_Moz_CatchClause(M) { return { type: "CatchClause", param: to_moz(M.argname), guard: null, body: to_moz_block(M) }; }); def_to_moz(AST_Definitions, function To_Moz_VariableDeclaration(M) { return { type: "VariableDeclaration", kind: "var", declarations: M.definitions.map(to_moz) }; }); def_to_moz(AST_Sequence, function To_Moz_SequenceExpression(M) { return { type: "SequenceExpression", expressions: M.expressions.map(to_moz) }; }); def_to_moz(AST_PropAccess, function To_Moz_MemberExpression(M) { var isComputed = M instanceof AST_Sub; return { type: "MemberExpression", object: to_moz(M.expression), computed: isComputed, property: isComputed ? to_moz(M.property) : {type: "Identifier", name: M.property} }; }); def_to_moz(AST_Unary, function To_Moz_Unary(M) { return { type: M.operator == "++" || M.operator == "--" ? "UpdateExpression" : "UnaryExpression", operator: M.operator, prefix: M instanceof AST_UnaryPrefix, argument: to_moz(M.expression) }; }); def_to_moz(AST_Binary, function To_Moz_BinaryExpression(M) { return { type: M.operator == "&&" || M.operator == "||" ? "LogicalExpression" : "BinaryExpression", left: to_moz(M.left), operator: M.operator, right: to_moz(M.right) }; }); def_to_moz(AST_Array, function To_Moz_ArrayExpression(M) { return { type: "ArrayExpression", elements: M.elements.map(to_moz) }; }); def_to_moz(AST_Object, function To_Moz_ObjectExpression(M) { return { type: "ObjectExpression", properties: M.properties.map(to_moz) }; }); def_to_moz(AST_ObjectProperty, function To_Moz_Property(M) { var key = { type: "Literal", value: M.key instanceof AST_SymbolAccessor ? M.key.name : M.key }; var kind; if (M instanceof AST_ObjectKeyVal) { kind = "init"; } else if (M instanceof AST_ObjectGetter) { kind = "get"; } else if (M instanceof AST_ObjectSetter) { kind = "set"; } return { type: "Property", kind: kind, key: key, value: to_moz(M.value) }; }); def_to_moz(AST_Symbol, function To_Moz_Identifier(M) { var def = M.definition(); return { type: "Identifier", name: def && def.mangled_name || M.name }; }); def_to_moz(AST_RegExp, function To_Moz_RegExpLiteral(M) { var flags = M.value.toString().match(/[gimuy]*$/)[0]; var value = "/" + M.value.raw_source + "/" + flags; return { type: "Literal", value: value, raw: value, regex: { pattern: M.value.raw_source, flags: flags } }; }); def_to_moz(AST_Constant, function To_Moz_Literal(M) { var value = M.value; if (typeof value === 'number' && (value < 0 || (value === 0 && 1 / value < 0))) { return { type: "UnaryExpression", operator: "-", prefix: true, argument: { type: "Literal", value: -value, raw: M.start.raw } }; } return { type: "Literal", value: value, raw: M.start.raw }; }); def_to_moz(AST_Atom, function To_Moz_Atom(M) { return { type: "Identifier", name: String(M.value) }; }); AST_Boolean.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast); AST_Null.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast); AST_Hole.DEFMETHOD("to_mozilla_ast", function To_Moz_ArrayHole() { return null }); AST_Block.DEFMETHOD("to_mozilla_ast", AST_BlockStatement.prototype.to_mozilla_ast); AST_Lambda.DEFMETHOD("to_mozilla_ast", AST_Function.prototype.to_mozilla_ast); /* -----[ tools ]----- */ function raw_token(moznode) { if (moznode.type == "Literal") { return moznode.raw != null ? moznode.raw : moznode.value + ""; } } function my_start_token(moznode) { var loc = moznode.loc, start = loc && loc.start; var range = moznode.range; return new AST_Token({ file : loc && loc.source, line : start && start.line, col : start && start.column, pos : range ? range[0] : moznode.start, endline : start && start.line, endcol : start && start.column, endpos : range ? range[0] : moznode.start, raw : raw_token(moznode), }); } function my_end_token(moznode) { var loc = moznode.loc, end = loc && loc.end; var range = moznode.range; return new AST_Token({ file : loc && loc.source, line : end && end.line, col : end && end.column, pos : range ? range[1] : moznode.end, endline : end && end.line, endcol : end && end.column, endpos : range ? range[1] : moznode.end, raw : raw_token(moznode), }); } function map(moztype, mytype, propmap) { var moz_to_me = "function From_Moz_" + moztype + "(M){\n"; moz_to_me += "return new U2." + mytype.name + "({\n" + "start: my_start_token(M),\n" + "end: my_end_token(M)"; var me_to_moz = "function To_Moz_" + moztype + "(M){\n"; me_to_moz += "return {\n" + "type: " + JSON.stringify(moztype); if (propmap) propmap.split(/\s*,\s*/).forEach(function(prop) { var m = /([a-z0-9$_]+)(=|@|>|%)([a-z0-9$_]+)/i.exec(prop); if (!m) throw new Error("Can't understand property map: " + prop); var moz = m[1], how = m[2], my = m[3]; moz_to_me += ",\n" + my + ": "; me_to_moz += ",\n" + moz + ": "; switch (how) { case "@": moz_to_me += "M." + moz + ".map(from_moz)"; me_to_moz += "M." + my + ".map(to_moz)"; break; case ">": moz_to_me += "from_moz(M." + moz + ")"; me_to_moz += "to_moz(M." + my + ")"; break; case "=": moz_to_me += "M." + moz; me_to_moz += "M." + my; break; case "%": moz_to_me += "from_moz(M." + moz + ").body"; me_to_moz += "to_moz_block(M)"; break; default: throw new Error("Can't understand operator in propmap: " + prop); } }); moz_to_me += "\n})\n}"; me_to_moz += "\n}\n}"; //moz_to_me = parse(moz_to_me).print_to_string({ beautify: true }); //me_to_moz = parse(me_to_moz).print_to_string({ beautify: true }); //console.log(moz_to_me); moz_to_me = new Function("U2", "my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")( exports, my_start_token, my_end_token, from_moz ); me_to_moz = new Function("to_moz", "to_moz_block", "to_moz_scope", "return(" + me_to_moz + ")")( to_moz, to_moz_block, to_moz_scope ); MOZ_TO_ME[moztype] = moz_to_me; def_to_moz(mytype, me_to_moz); } var FROM_MOZ_STACK = null; function from_moz(node) { FROM_MOZ_STACK.push(node); var ret = node != null ? MOZ_TO_ME[node.type](node) : null; FROM_MOZ_STACK.pop(); return ret; } AST_Node.from_mozilla_ast = function(node) { var save_stack = FROM_MOZ_STACK; FROM_MOZ_STACK = []; var ast = from_moz(node); FROM_MOZ_STACK = save_stack; ast.walk(new TreeWalker(function(node) { if (node instanceof AST_LabelRef) { for (var level = 0, parent; parent = this.parent(level); level++) { if (parent instanceof AST_Scope) break; if (parent instanceof AST_LabeledStatement && parent.label.name == node.name) { node.thedef = parent.label; break; } } if (!node.thedef) { var s = node.start; js_error("Undefined label " + node.name, s.file, s.line, s.col, s.pos); } } })); return ast; }; function set_moz_loc(mynode, moznode, myparent) { var start = mynode.start; var end = mynode.end; if (start.pos != null && end.endpos != null) { moznode.range = [start.pos, end.endpos]; } if (start.line) { moznode.loc = { start: {line: start.line, column: start.col}, end: end.endline ? {line: end.endline, column: end.endcol} : null }; if (start.file) { moznode.loc.source = start.file; } } return moznode; } function def_to_moz(mytype, handler) { mytype.DEFMETHOD("to_mozilla_ast", function() { return set_moz_loc(this, handler(this)); }); } function to_moz(node) { return node != null ? node.to_mozilla_ast() : null; } function to_moz_block(node) { return { type: "BlockStatement", body: node.body.map(to_moz) }; } function to_moz_scope(type, node) { var body = node.body.map(to_moz); if (node.body[0] instanceof AST_SimpleStatement && node.body[0].body instanceof AST_String) { body.unshift(to_moz(new AST_EmptyStatement(node.body[0]))); } return { type: type, body: body }; } })(); UglifyJS2-3.6.3/lib/output.js000066400000000000000000001442451355252637300157720ustar00rootroot00000000000000/*********************************************************************** A JavaScript tokenizer / parser / beautifier / compressor. https://github.com/mishoo/UglifyJS2 -------------------------------- (C) --------------------------------- Author: Mihai Bazon http://mihai.bazon.net/blog Distributed under the BSD license: Copyright 2012 (c) Mihai Bazon Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***********************************************************************/ "use strict"; var EXPECT_DIRECTIVE = /^$|[;{][\s\n]*$/; function is_some_comments(comment) { // multiline comment return comment.type == "comment2" && /@preserve|@license|@cc_on/i.test(comment.value); } function OutputStream(options) { var readonly = !options; options = defaults(options, { ascii_only : false, beautify : false, braces : false, comments : false, ie8 : false, indent_level : 4, indent_start : 0, inline_script : true, keep_quoted_props: false, max_line_len : false, preamble : null, preserve_line : false, quote_keys : false, quote_style : 0, semicolons : true, shebang : true, source_map : null, webkit : false, width : 80, wrap_iife : false, }, true); // Convert comment option to RegExp if neccessary and set up comments filter var comment_filter = return_false; // Default case, throw all comments away if (options.comments) { var comments = options.comments; if (typeof options.comments === "string" && /^\/.*\/[a-zA-Z]*$/.test(options.comments)) { var regex_pos = options.comments.lastIndexOf("/"); comments = new RegExp( options.comments.substr(1, regex_pos - 1), options.comments.substr(regex_pos + 1) ); } if (comments instanceof RegExp) { comment_filter = function(comment) { return comment.type != "comment5" && comments.test(comment.value); }; } else if (typeof comments === "function") { comment_filter = function(comment) { return comment.type != "comment5" && comments(this, comment); }; } else if (comments === "some") { comment_filter = is_some_comments; } else { // NOTE includes "all" option comment_filter = return_true; } } var indentation = 0; var current_col = 0; var current_line = 1; var current_pos = 0; var OUTPUT = ""; var to_utf8 = options.ascii_only ? function(str, identifier) { return str.replace(/[\u0000-\u001f\u007f-\uffff]/g, function(ch) { var code = ch.charCodeAt(0).toString(16); if (code.length <= 2 && !identifier) { while (code.length < 2) code = "0" + code; return "\\x" + code; } else { while (code.length < 4) code = "0" + code; return "\\u" + code; } }); } : function(str) { var s = ""; for (var i = 0; i < str.length; i++) { if (is_surrogate_pair_head(str[i]) && !is_surrogate_pair_tail(str[i + 1]) || is_surrogate_pair_tail(str[i]) && !is_surrogate_pair_head(str[i - 1])) { s += "\\u" + str.charCodeAt(i).toString(16); } else { s += str[i]; } } return s; }; function make_string(str, quote) { var dq = 0, sq = 0; str = str.replace(/[\\\b\f\n\r\v\t\x22\x27\u2028\u2029\0\ufeff]/g, function(s, i) { switch (s) { case '"': ++dq; return '"'; case "'": ++sq; return "'"; case "\\": return "\\\\"; case "\n": return "\\n"; case "\r": return "\\r"; case "\t": return "\\t"; case "\b": return "\\b"; case "\f": return "\\f"; case "\x0B": return options.ie8 ? "\\x0B" : "\\v"; case "\u2028": return "\\u2028"; case "\u2029": return "\\u2029"; case "\ufeff": return "\\ufeff"; case "\0": return /[0-9]/.test(str.charAt(i+1)) ? "\\x00" : "\\0"; } return s; }); function quote_single() { return "'" + str.replace(/\x27/g, "\\'") + "'"; } function quote_double() { return '"' + str.replace(/\x22/g, '\\"') + '"'; } str = to_utf8(str); switch (options.quote_style) { case 1: return quote_single(); case 2: return quote_double(); case 3: return quote == "'" ? quote_single() : quote_double(); default: return dq > sq ? quote_single() : quote_double(); } } function encode_string(str, quote) { var ret = make_string(str, quote); if (options.inline_script) { ret = ret.replace(/<\x2f(script)([>\/\t\n\f\r ])/gi, "<\\/$1$2"); ret = ret.replace(/\x3c!--/g, "\\x3c!--"); ret = ret.replace(/--\x3e/g, "--\\x3e"); } return ret; } function make_name(name) { name = name.toString(); name = to_utf8(name, true); return name; } function make_indent(back) { return repeat_string(" ", options.indent_start + indentation - back * options.indent_level); } /* -----[ beautification/minification ]----- */ var has_parens = false; var line_end = 0; var line_fixed = true; var might_need_space = false; var might_need_semicolon = false; var need_newline_indented = false; var need_space = false; var newline_insert = -1; var last = ""; var mapping_token, mapping_name, mappings = options.source_map && []; var adjust_mappings = mappings ? function(line, col) { mappings.forEach(function(mapping) { mapping.line += line; mapping.col += col; }); } : noop; var flush_mappings = mappings ? function() { mappings.forEach(function(mapping) { options.source_map.add( mapping.token.file, mapping.line, mapping.col, mapping.token.line, mapping.token.col, !mapping.name && mapping.token.type == "name" ? mapping.token.value : mapping.name ); }); mappings = []; } : noop; function insert_newlines(count) { var index = OUTPUT.lastIndexOf("\n"); if (line_end < index) line_end = index; var left = OUTPUT.slice(0, line_end); var right = OUTPUT.slice(line_end); adjust_mappings(count, right.length - current_col); current_line += count; current_pos += count; current_col = right.length; OUTPUT = left; while (count--) OUTPUT += "\n"; OUTPUT += right; } var fix_line = options.max_line_len ? function() { if (line_fixed) { if (current_col > options.max_line_len) { AST_Node.warn("Output exceeds {max_line_len} characters", options); } return; } if (current_col > options.max_line_len) insert_newlines(1); line_fixed = true; flush_mappings(); } : noop; var requireSemicolonChars = makePredicate("( [ + * / - , ."); function print(str) { str = String(str); var ch = str.charAt(0); if (need_newline_indented && ch) { need_newline_indented = false; if (ch != "\n") { print("\n"); indent(); } } if (need_space && ch) { need_space = false; if (!/[\s;})]/.test(ch)) { space(); } } newline_insert = -1; var prev = last.charAt(last.length - 1); if (might_need_semicolon) { might_need_semicolon = false; if (prev == ":" && ch == "}" || (!ch || ";}".indexOf(ch) < 0) && prev != ";") { if (options.semicolons || requireSemicolonChars[ch]) { OUTPUT += ";"; current_col++; current_pos++; } else { fix_line(); OUTPUT += "\n"; current_pos++; current_line++; current_col = 0; if (/^\s+$/.test(str)) { // reset the semicolon flag, since we didn't print one // now and might still have to later might_need_semicolon = true; } } if (!options.beautify) might_need_space = false; } } if (might_need_space) { if ((is_identifier_char(prev) && (is_identifier_char(ch) || ch == "\\")) || (ch == "/" && ch == prev) || ((ch == "+" || ch == "-") && ch == last)) { OUTPUT += " "; current_col++; current_pos++; } might_need_space = false; } if (mapping_token) { mappings.push({ token: mapping_token, name: mapping_name, line: current_line, col: current_col }); mapping_token = false; if (line_fixed) flush_mappings(); } OUTPUT += str; has_parens = str[str.length - 1] == "("; current_pos += str.length; var a = str.split(/\r?\n/), n = a.length - 1; current_line += n; current_col += a[0].length; if (n > 0) { fix_line(); current_col = a[n].length; } last = str; } var space = options.beautify ? function() { print(" "); } : function() { might_need_space = true; }; var indent = options.beautify ? function(half) { if (options.beautify) { print(make_indent(half ? 0.5 : 0)); } } : noop; var with_indent = options.beautify ? function(col, cont) { if (col === true) col = next_indent(); var save_indentation = indentation; indentation = col; var ret = cont(); indentation = save_indentation; return ret; } : function(col, cont) { return cont() }; var may_add_newline = options.max_line_len || options.preserve_line ? function() { fix_line(); line_end = OUTPUT.length; line_fixed = false; } : noop; var newline = options.beautify ? function() { if (newline_insert < 0) return print("\n"); if (OUTPUT[newline_insert] != "\n") { OUTPUT = OUTPUT.slice(0, newline_insert) + "\n" + OUTPUT.slice(newline_insert); current_pos++; current_line++; } newline_insert++; } : may_add_newline; var semicolon = options.beautify ? function() { print(";"); } : function() { might_need_semicolon = true; }; function force_semicolon() { might_need_semicolon = false; print(";"); } function next_indent() { return indentation + options.indent_level; } function with_block(cont) { var ret; print("{"); newline(); with_indent(next_indent(), function() { ret = cont(); }); indent(); print("}"); return ret; } function with_parens(cont) { print("("); may_add_newline(); //XXX: still nice to have that for argument lists //var ret = with_indent(current_col, cont); var ret = cont(); may_add_newline(); print(")"); return ret; } function with_square(cont) { print("["); may_add_newline(); //var ret = with_indent(current_col, cont); var ret = cont(); may_add_newline(); print("]"); return ret; } function comma() { may_add_newline(); print(","); may_add_newline(); space(); } function colon() { print(":"); space(); } var add_mapping = mappings ? function(token, name) { mapping_token = token; mapping_name = name; } : noop; function get() { if (!line_fixed) fix_line(); return OUTPUT; } function has_nlb() { var index = OUTPUT.lastIndexOf("\n"); return /^ *$/.test(OUTPUT.slice(index + 1)); } function prepend_comments(node) { var self = this; var scan = node instanceof AST_Exit && node.value; var comments = dump(node); if (!comments) return; if (scan) { var tw = new TreeWalker(function(node) { var parent = tw.parent(); if (parent instanceof AST_Exit || parent instanceof AST_Binary && parent.left === node || parent.TYPE == "Call" && parent.expression === node || parent instanceof AST_Conditional && parent.condition === node || parent instanceof AST_Dot && parent.expression === node || parent instanceof AST_Sequence && parent.expressions[0] === node || parent instanceof AST_Sub && parent.expression === node || parent instanceof AST_UnaryPostfix) { var before = dump(node); if (before) comments = comments.concat(before); } else { return true; } }); tw.push(node); node.value.walk(tw); } if (current_pos == 0) { if (comments.length > 0 && options.shebang && comments[0].type == "comment5") { print("#!" + comments.shift().value + "\n"); indent(); } var preamble = options.preamble; if (preamble) { print(preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, "\n")); } } comments = comments.filter(comment_filter, node); if (comments.length == 0) return; var last_nlb = has_nlb(); comments.forEach(function(c, i) { if (!last_nlb) { if (c.nlb) { print("\n"); indent(); last_nlb = true; } else if (i > 0) { space(); } } if (/comment[134]/.test(c.type)) { print("//" + c.value.replace(/[@#]__PURE__/g, ' ') + "\n"); indent(); last_nlb = true; } else if (c.type == "comment2") { print("/*" + c.value.replace(/[@#]__PURE__/g, ' ') + "*/"); last_nlb = false; } }); if (!last_nlb) { if (node.start.nlb) { print("\n"); indent(); } else { space(); } } function dump(node) { var token = node.start; if (!token) { if (!scan) return; node.start = token = new AST_Token(); } var comments = token.comments_before; if (!comments) { if (!scan) return; token.comments_before = comments = []; } if (comments._dumped === self) return; comments._dumped = self; return comments; } } function append_comments(node, tail) { var self = this; var token = node.end; if (!token) return; var comments = token[tail ? "comments_before" : "comments_after"]; if (!comments || comments._dumped === self) return; if (!(node instanceof AST_Statement || all(comments, function(c) { return !/comment[134]/.test(c.type); }))) return; comments._dumped = self; var insert = OUTPUT.length; comments.filter(comment_filter, node).forEach(function(c, i) { need_space = false; if (need_newline_indented) { print("\n"); indent(); need_newline_indented = false; } else if (c.nlb && (i > 0 || !has_nlb())) { print("\n"); indent(); } else if (i > 0 || !tail) { space(); } if (/comment[134]/.test(c.type)) { print("//" + c.value.replace(/[@#]__PURE__/g, ' ')); need_newline_indented = true; } else if (c.type == "comment2") { print("/*" + c.value.replace(/[@#]__PURE__/g, ' ') + "*/"); need_space = true; } }); if (OUTPUT.length > insert) newline_insert = insert; } var stack = []; return { get : get, toString : get, indent : indent, indentation : function() { return indentation }, current_width : function() { return current_col - indentation }, should_break : function() { return options.width && this.current_width() >= options.width }, has_parens : function() { return has_parens }, newline : newline, print : print, space : space, comma : comma, colon : colon, last : function() { return last }, semicolon : semicolon, force_semicolon : force_semicolon, to_utf8 : to_utf8, print_name : function(name) { print(make_name(name)) }, print_string : function(str, quote, escape_directive) { var encoded = encode_string(str, quote); if (escape_directive === true && encoded.indexOf("\\") === -1) { // Insert semicolons to break directive prologue if (!EXPECT_DIRECTIVE.test(OUTPUT)) { force_semicolon(); } force_semicolon(); } print(encoded); }, next_indent : next_indent, with_indent : with_indent, with_block : with_block, with_parens : with_parens, with_square : with_square, add_mapping : add_mapping, option : function(opt) { return options[opt] }, prepend_comments: readonly ? noop : prepend_comments, append_comments : readonly || comment_filter === return_false ? noop : append_comments, line : function() { return current_line }, col : function() { return current_col }, pos : function() { return current_pos }, push_node : function(node) { stack.push(node) }, pop_node : options.preserve_line ? function() { var node = stack.pop(); if (node.start && node.start.line > current_line) { insert_newlines(node.start.line - current_line); } } : function() { stack.pop(); }, parent : function(n) { return stack[stack.length - 2 - (n || 0)]; } }; } /* -----[ code generators ]----- */ (function() { /* -----[ utils ]----- */ function DEFPRINT(nodetype, generator) { nodetype.DEFMETHOD("_codegen", generator); } 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; 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() { stream.prepend_comments(self); self.add_source_map(stream); generator(self, stream); stream.append_comments(self); } stream.push_node(self); if (force_parens || self.needs_parens(stream)) { stream.with_parens(doit); } else { doit(); } stream.pop_node(); if (self === use_asm) { use_asm = null; } }); AST_Node.DEFMETHOD("_print", AST_Node.prototype.print); AST_Node.DEFMETHOD("print_to_string", function(options) { var s = OutputStream(options); this.print(s); return s.get(); }); /* -----[ PARENTHESES ]----- */ function PARENS(nodetype, func) { if (Array.isArray(nodetype)) { nodetype.forEach(function(nodetype) { PARENS(nodetype, func); }); } else { nodetype.DEFMETHOD("needs_parens", func); } } PARENS(AST_Node, return_false); // a function expression needs parens around it when it's provably // the first token to appear in a statement. PARENS(AST_Function, function(output) { if (!output.has_parens() && first_in_statement(output)) return true; if (output.option('webkit')) { var p = output.parent(); if (p instanceof AST_PropAccess && p.expression === this) return true; } if (output.option('wrap_iife')) { var p = output.parent(); if (p instanceof AST_Call && p.expression === this) return true; } }); // same goes for an object literal, because otherwise it would be // interpreted as a block of code. PARENS(AST_Object, function(output) { return !output.has_parens() && first_in_statement(output); }); PARENS(AST_Unary, function(output) { var p = output.parent(); return p instanceof AST_PropAccess && p.expression === this || p instanceof AST_Call && p.expression === this; }); PARENS(AST_Sequence, function(output) { var p = output.parent(); return p instanceof AST_Call // (foo, bar)() or foo(1, (2, 3), 4) || p instanceof AST_Unary // !(foo, bar, baz) || p instanceof AST_Binary // 1 + (2, 3) + 4 ==> 8 || p instanceof AST_VarDef // var a = (1, 2), b = a + a; ==> b == 4 || p instanceof AST_PropAccess // (1, {foo:2}).foo or (1, {foo:2})["foo"] ==> 2 || p instanceof AST_Array // [ 1, (2, 3), 4 ] ==> [ 1, 3, 4 ] || p instanceof AST_ObjectProperty // { foo: (1, 2) }.foo ==> 2 || p instanceof AST_Conditional /* (false, true) ? (a = 10, b = 20) : (c = 30) * ==> 20 (side effect, set a := 10 and b := 20) */ ; }); PARENS(AST_Binary, function(output) { var p = output.parent(); // (foo && bar)() if (p instanceof AST_Call && p.expression === this) return true; // typeof (foo && bar) if (p instanceof AST_Unary) return true; // (foo && bar)["prop"], (foo && bar).prop if (p instanceof AST_PropAccess && p.expression === this) return true; // this deals with precedence: 3 * (2 + 1) if (p instanceof AST_Binary) { var po = p.operator, pp = PRECEDENCE[po]; var so = this.operator, sp = PRECEDENCE[so]; if (pp > sp || (pp == sp && this === p.right)) { return true; } } }); PARENS(AST_PropAccess, function(output) { var p = output.parent(); if (p instanceof AST_New && p.expression === this) { // i.e. new (foo.bar().baz) // // if there's one call into this subtree, then we need // parens around it too, otherwise the call will be // interpreted as passing the arguments to the upper New // expression. var parens = false; this.walk(new TreeWalker(function(node) { if (parens || node instanceof AST_Scope) return true; if (node instanceof AST_Call) { parens = true; return true; } })); return parens; } }); PARENS(AST_Call, function(output) { var p = output.parent(); if (p instanceof AST_New && p.expression === this) return true; // https://bugs.webkit.org/show_bug.cgi?id=123506 if (output.option('webkit')) { var g = output.parent(1); return this.expression instanceof AST_Function && p instanceof AST_PropAccess && p.expression === this && g instanceof AST_Assign && g.left === p; } }); PARENS(AST_New, function(output) { var p = output.parent(); if (!need_constructor_parens(this, output) && (p instanceof AST_PropAccess // (new Date).getTime(), (new Date)["getTime"]() || p instanceof AST_Call && p.expression === this)) // (new foo)(bar) return true; }); PARENS(AST_Number, function(output) { var p = output.parent(); if (p instanceof AST_PropAccess && p.expression === this) { var value = this.getValue(); if (value < 0 || /^0/.test(make_num(value))) { return true; } } }); PARENS([ AST_Assign, AST_Conditional ], function(output) { var p = output.parent(); // !(a = false) → true if (p instanceof AST_Unary) return true; // 1 + (a = 2) + 3 → 6, side effect setting a = 2 if (p instanceof AST_Binary && !(p instanceof AST_Assign)) return true; // (a = func)() —or— new (a = Object)() if (p instanceof AST_Call && p.expression === this) return true; // (a = foo) ? bar : baz if (p instanceof AST_Conditional && p.condition === this) return true; // (a = foo)["prop"] —or— (a = foo).prop if (p instanceof AST_PropAccess && p.expression === this) return true; }); /* -----[ PRINTERS ]----- */ DEFPRINT(AST_Directive, function(self, output) { output.print_string(self.value, self.quote); output.semicolon(); }); DEFPRINT(AST_Debugger, function(self, output) { output.print("debugger"); output.semicolon(); }); /* -----[ statements ]----- */ function display_body(body, is_toplevel, output, allow_directives) { var last = body.length - 1; in_directive = allow_directives; body.forEach(function(stmt, i) { if (in_directive === true && !(stmt instanceof AST_Directive || stmt instanceof AST_EmptyStatement || (stmt instanceof AST_SimpleStatement && stmt.body instanceof AST_String) )) { in_directive = false; } if (!(stmt instanceof AST_EmptyStatement)) { output.indent(); stmt.print(output); if (!(i == last && is_toplevel)) { output.newline(); if (is_toplevel) output.newline(); } } if (in_directive === true && stmt instanceof AST_SimpleStatement && stmt.body instanceof AST_String ) { in_directive = false; } }); in_directive = false; } AST_StatementWithBody.DEFMETHOD("_do_print_body", function(output) { force_statement(this.body, output); }); DEFPRINT(AST_Statement, function(self, output) { self.body.print(output); output.semicolon(); }); DEFPRINT(AST_Toplevel, function(self, output) { display_body(self.body, true, output, true); output.print(""); }); DEFPRINT(AST_LabeledStatement, function(self, output) { self.label.print(output); output.colon(); self.body.print(output); }); DEFPRINT(AST_SimpleStatement, function(self, output) { self.body.print(output); output.semicolon(); }); function print_braced_empty(self, output) { output.print("{"); output.with_indent(output.next_indent(), function() { output.append_comments(self, true); }); output.print("}"); } function print_braced(self, output, allow_directives) { if (self.body.length > 0) { output.with_block(function() { display_body(self.body, false, output, allow_directives); }); } else print_braced_empty(self, output); } DEFPRINT(AST_BlockStatement, function(self, output) { print_braced(self, output); }); DEFPRINT(AST_EmptyStatement, function(self, output) { output.semicolon(); }); DEFPRINT(AST_Do, function(self, output) { output.print("do"); output.space(); make_block(self.body, output); output.space(); output.print("while"); output.space(); output.with_parens(function() { self.condition.print(output); }); output.semicolon(); }); DEFPRINT(AST_While, function(self, output) { output.print("while"); output.space(); output.with_parens(function() { self.condition.print(output); }); output.space(); self._do_print_body(output); }); DEFPRINT(AST_For, function(self, output) { output.print("for"); output.space(); output.with_parens(function() { if (self.init) { if (self.init instanceof AST_Definitions) { self.init.print(output); } else { parenthesize_for_noin(self.init, output, true); } output.print(";"); output.space(); } else { output.print(";"); } if (self.condition) { self.condition.print(output); output.print(";"); output.space(); } else { output.print(";"); } if (self.step) { self.step.print(output); } }); output.space(); self._do_print_body(output); }); DEFPRINT(AST_ForIn, function(self, output) { output.print("for"); output.space(); output.with_parens(function() { self.init.print(output); output.space(); output.print("in"); output.space(); self.object.print(output); }); output.space(); self._do_print_body(output); }); DEFPRINT(AST_With, function(self, output) { output.print("with"); output.space(); output.with_parens(function() { self.expression.print(output); }); output.space(); self._do_print_body(output); }); /* -----[ functions ]----- */ AST_Lambda.DEFMETHOD("_do_print", function(output, nokeyword) { var self = this; if (!nokeyword) { output.print("function"); } if (self.name) { output.space(); self.name.print(output); } output.with_parens(function() { self.argnames.forEach(function(arg, i) { if (i) output.comma(); arg.print(output); }); }); output.space(); print_braced(self, output, true); }); DEFPRINT(AST_Lambda, function(self, output) { self._do_print(output); }); /* -----[ jumps ]----- */ function print_jump(output, kind, target) { output.print(kind); if (target) { output.space(); target.print(output); } output.semicolon(); } DEFPRINT(AST_Return, function(self, output) { print_jump(output, "return", self.value); }); DEFPRINT(AST_Throw, function(self, output) { print_jump(output, "throw", self.value); }); DEFPRINT(AST_Break, function(self, output) { print_jump(output, "break", self.label); }); DEFPRINT(AST_Continue, function(self, output) { print_jump(output, "continue", self.label); }); /* -----[ if ]----- */ function make_then(self, output) { var b = self.body; if (output.option("braces") || output.option("ie8") && b instanceof AST_Do) return make_block(b, output); // The squeezer replaces "block"-s that contain only a single // statement with the statement itself; technically, the AST // is correct, but this can create problems when we output an // IF having an ELSE clause where the THEN clause ends in an // IF *without* an ELSE block (then the outer ELSE would refer // to the inner IF). This function checks for this case and // adds the block braces if needed. if (!b) return output.force_semicolon(); while (true) { if (b instanceof AST_If) { if (!b.alternative) { make_block(self.body, output); return; } b = b.alternative; } else if (b instanceof AST_StatementWithBody) { b = b.body; } else break; } force_statement(self.body, output); } DEFPRINT(AST_If, function(self, output) { output.print("if"); output.space(); output.with_parens(function() { self.condition.print(output); }); output.space(); if (self.alternative) { make_then(self, output); output.space(); output.print("else"); output.space(); if (self.alternative instanceof AST_If) self.alternative.print(output); else force_statement(self.alternative, output); } else { self._do_print_body(output); } }); /* -----[ switch ]----- */ DEFPRINT(AST_Switch, function(self, output) { output.print("switch"); output.space(); output.with_parens(function() { self.expression.print(output); }); output.space(); var last = self.body.length - 1; if (last < 0) print_braced_empty(self, output); else output.with_block(function() { self.body.forEach(function(branch, i) { output.indent(true); branch.print(output); if (i < last && branch.body.length > 0) output.newline(); }); }); }); AST_SwitchBranch.DEFMETHOD("_do_print_body", function(output) { output.newline(); this.body.forEach(function(stmt) { output.indent(); stmt.print(output); output.newline(); }); }); DEFPRINT(AST_Default, function(self, output) { output.print("default:"); self._do_print_body(output); }); DEFPRINT(AST_Case, function(self, output) { output.print("case"); output.space(); self.expression.print(output); output.print(":"); self._do_print_body(output); }); /* -----[ exceptions ]----- */ DEFPRINT(AST_Try, function(self, output) { output.print("try"); output.space(); print_braced(self, output); if (self.bcatch) { output.space(); self.bcatch.print(output); } if (self.bfinally) { output.space(); self.bfinally.print(output); } }); DEFPRINT(AST_Catch, function(self, output) { output.print("catch"); output.space(); output.with_parens(function() { self.argname.print(output); }); output.space(); print_braced(self, output); }); DEFPRINT(AST_Finally, function(self, output) { output.print("finally"); output.space(); print_braced(self, output); }); DEFPRINT(AST_Var, function(self, output) { output.print("var"); output.space(); self.definitions.forEach(function(def, i) { if (i) output.comma(); def.print(output); }); var p = output.parent(); if (p && p.init !== self || !(p instanceof AST_For || p instanceof AST_ForIn)) output.semicolon(); }); function parenthesize_for_noin(node, output, noin) { var parens = false; // need to take some precautions here: // https://github.com/mishoo/UglifyJS2/issues/60 if (noin) node.walk(new TreeWalker(function(node) { if (parens || node instanceof AST_Scope) return true; if (node instanceof AST_Binary && node.operator == "in") { parens = true; return true; } })); node.print(output, parens); } DEFPRINT(AST_VarDef, function(self, output) { self.name.print(output); if (self.value) { output.space(); output.print("="); output.space(); var p = output.parent(1); var noin = p instanceof AST_For || p instanceof AST_ForIn; parenthesize_for_noin(self.value, output, noin); } }); /* -----[ other expressions ]----- */ DEFPRINT(AST_Call, function(self, output) { self.expression.print(output); if (self instanceof AST_New && !need_constructor_parens(self, output)) return; if (self.expression instanceof AST_Call || self.expression instanceof AST_Lambda) { output.add_mapping(self.start); } output.with_parens(function() { self.args.forEach(function(expr, i) { if (i) output.comma(); expr.print(output); }); }); }); DEFPRINT(AST_New, function(self, output) { output.print("new"); output.space(); AST_Call.prototype._codegen(self, output); }); DEFPRINT(AST_Sequence, function(self, output) { self.expressions.forEach(function(node, index) { if (index > 0) { output.comma(); if (output.should_break()) { output.newline(); output.indent(); } } node.print(output); }); }); DEFPRINT(AST_Dot, function(self, output) { var expr = self.expression; expr.print(output); var prop = self.property; if (output.option("ie8") && RESERVED_WORDS[prop]) { output.print("["); output.add_mapping(self.end); output.print_string(prop); output.print("]"); } else { if (expr instanceof AST_Number && expr.getValue() >= 0) { if (!/[xa-f.)]/i.test(output.last())) { output.print("."); } } output.print("."); // the name after dot would be mapped about here. output.add_mapping(self.end); output.print_name(prop); } }); DEFPRINT(AST_Sub, function(self, output) { self.expression.print(output); output.print("["); self.property.print(output); output.print("]"); }); DEFPRINT(AST_UnaryPrefix, function(self, output) { var op = self.operator; output.print(op); if (/^[a-z]/i.test(op) || (/[+-]$/.test(op) && self.expression instanceof AST_UnaryPrefix && /^[+-]/.test(self.expression.operator))) { output.space(); } self.expression.print(output); }); DEFPRINT(AST_UnaryPostfix, function(self, output) { self.expression.print(output); output.print(self.operator); }); DEFPRINT(AST_Binary, function(self, output) { var op = self.operator; self.left.print(output); if (op[0] == ">" /* ">>" ">>>" ">" ">=" */ && self.left instanceof AST_UnaryPostfix && self.left.operator == "--") { // space is mandatory to avoid outputting --> output.print(" "); } else { // the space is optional depending on "beautify" output.space(); } output.print(op); if ((op == "<" || op == "<<") && self.right instanceof AST_UnaryPrefix && self.right.operator == "!" && self.right.expression instanceof AST_UnaryPrefix && self.right.expression.operator == "--") { // space is mandatory to avoid outputting ") && S.newline_before) { forward(3); skip_line_comment("comment4"); continue; } } var ch = peek(); if (!ch) return token("eof"); var code = ch.charCodeAt(0); switch (code) { case 34: case 39: return read_string(ch); case 46: return handle_dot(); case 47: { var tok = handle_slash(); if (tok === next_token) continue; return tok; } } if (is_digit(code)) return read_num(); if (PUNC_CHARS[ch]) return token("punc", next()); if (OPERATOR_CHARS[ch]) return read_operator(); if (code == 92 || is_identifier_start(code)) return read_word(); break; } parse_error("Unexpected character '" + ch + "'"); } next_token.context = function(nc) { if (nc) S = nc; return S; }; next_token.add_directive = function(directive) { S.directive_stack[S.directive_stack.length - 1].push(directive); if (S.directives[directive] === undefined) { S.directives[directive] = 1; } else { S.directives[directive]++; } } next_token.push_directives_stack = function() { S.directive_stack.push([]); } next_token.pop_directives_stack = function() { var directives = S.directive_stack[S.directive_stack.length - 1]; for (var i = 0; i < directives.length; i++) { S.directives[directives[i]]--; } S.directive_stack.pop(); } next_token.has_directive = function(directive) { return S.directives[directive] > 0; } return next_token; } /* -----[ Parser (constants) ]----- */ var UNARY_PREFIX = makePredicate([ "typeof", "void", "delete", "--", "++", "!", "~", "-", "+" ]); var UNARY_POSTFIX = makePredicate([ "--", "++" ]); var ASSIGNMENT = makePredicate([ "=", "+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&=" ]); var PRECEDENCE = function(a, ret) { for (var i = 0; i < a.length; ++i) { var b = a[i]; for (var j = 0; j < b.length; ++j) { ret[b[j]] = i + 1; } } return ret; }([ ["||"], ["&&"], ["|"], ["^"], ["&"], ["==", "===", "!=", "!=="], ["<", ">", "<=", ">=", "in", "instanceof"], [">>", "<<", ">>>"], ["+", "-"], ["*", "/", "%"] ], {}); var ATOMIC_START_TOKEN = makePredicate([ "atom", "num", "string", "regexp", "name" ]); /* -----[ Parser ]----- */ function parse($TEXT, options) { options = defaults(options, { bare_returns : false, expression : false, filename : null, html5_comments : true, shebang : true, strict : false, toplevel : null, }, true); var S = { input : (typeof $TEXT == "string" ? tokenizer($TEXT, options.filename, options.html5_comments, options.shebang) : $TEXT), token : null, prev : null, peeked : null, in_function : 0, in_directives : true, in_loop : 0, labels : [] }; S.token = next(); function is(type, value) { return is_token(S.token, type, value); } function peek() { return S.peeked || (S.peeked = S.input()); } function next() { S.prev = S.token; if (S.peeked) { S.token = S.peeked; S.peeked = null; } else { S.token = S.input(); } S.in_directives = S.in_directives && ( S.token.type == "string" || is("punc", ";") ); return S.token; } function prev() { return S.prev; } function croak(msg, line, col, pos) { var ctx = S.input.context(); js_error(msg, ctx.filename, line != null ? line : ctx.tokline, col != null ? col : ctx.tokcol, pos != null ? pos : ctx.tokpos); } function token_error(token, msg) { croak(msg, token.line, token.col); } function token_to_string(type, value) { return type + (value === undefined ? "" : " «" + value + "»"); } function unexpected(token) { if (token == null) token = S.token; token_error(token, "Unexpected token: " + token_to_string(token.type, token.value)); } function expect_token(type, val) { if (is(type, val)) { return next(); } token_error(S.token, "Unexpected token: " + token_to_string(S.token.type, S.token.value) + ", expected: " + token_to_string(type, val)); } function expect(punc) { return expect_token("punc", punc); } function has_newline_before(token) { return token.nlb || !all(token.comments_before, function(comment) { return !comment.nlb; }); } function can_insert_semicolon() { return !options.strict && (is("eof") || is("punc", "}") || has_newline_before(S.token)); } function semicolon(optional) { if (is("punc", ";")) next(); else if (!optional && !can_insert_semicolon()) expect_token("punc", ";"); } function parenthesised() { expect("("); var exp = expression(true); expect(")"); return exp; } function embed_tokens(parser) { return function() { var start = S.token; var expr = parser.apply(null, arguments); var end = prev(); expr.start = start; expr.end = end; return expr; }; } function handle_regexp() { if (is("operator", "/") || is("operator", "/=")) { S.peeked = null; S.token = S.input(S.token.value.substr(1)); // force regexp } } var statement = embed_tokens(function(strict_defun) { handle_regexp(); switch (S.token.type) { case "string": if (S.in_directives) { var token = peek(); if (S.token.raw.indexOf("\\") == -1 && (is_token(token, "punc", ";") || is_token(token, "punc", "}") || has_newline_before(token) || is_token(token, "eof"))) { S.input.add_directive(S.token.value); } else { S.in_directives = false; } } var dir = S.in_directives, stat = simple_statement(); return dir ? new AST_Directive(stat.body) : stat; case "num": case "regexp": case "operator": case "atom": return simple_statement(); case "name": return is_token(peek(), "punc", ":") ? labeled_statement() : simple_statement(); case "punc": switch (S.token.value) { case "{": return new AST_BlockStatement({ start : S.token, body : block_(), end : prev() }); case "[": case "(": return simple_statement(); case ";": S.in_directives = false; next(); return new AST_EmptyStatement(); default: unexpected(); } case "keyword": switch (S.token.value) { case "break": next(); return break_cont(AST_Break); case "continue": next(); return break_cont(AST_Continue); case "debugger": next(); semicolon(); return new AST_Debugger(); case "do": next(); var body = in_loop(statement); expect_token("keyword", "while"); var condition = parenthesised(); semicolon(true); return new AST_Do({ body : body, condition : condition }); case "while": next(); return new AST_While({ condition : parenthesised(), body : in_loop(statement) }); case "for": next(); return for_(); case "function": if (!strict_defun && S.input.has_directive("use strict")) { croak("In strict mode code, functions can only be declared at top level or immediately within another function."); } next(); return function_(AST_Defun); case "if": next(); return if_(); case "return": if (S.in_function == 0 && !options.bare_returns) croak("'return' outside of function"); next(); var value = null; if (is("punc", ";")) { next(); } else if (!can_insert_semicolon()) { value = expression(true); semicolon(); } return new AST_Return({ value: value }); case "switch": next(); return new AST_Switch({ expression : parenthesised(), body : in_loop(switch_body_) }); case "throw": next(); if (has_newline_before(S.token)) croak("Illegal newline after 'throw'"); var value = expression(true); semicolon(); return new AST_Throw({ value: value }); case "try": next(); return try_(); case "var": next(); var node = var_(); semicolon(); return node; case "with": if (S.input.has_directive("use strict")) { croak("Strict mode may not include a with statement"); } next(); return new AST_With({ expression : parenthesised(), body : statement() }); } } unexpected(); }); function labeled_statement() { var label = as_symbol(AST_Label); if (!all(S.labels, function(l) { return l.name != label.name; })) { // ECMA-262, 12.12: An ECMAScript program is considered // syntactically incorrect if it contains a // LabelledStatement that is enclosed by a // LabelledStatement with the same Identifier as label. croak("Label " + label.name + " defined twice"); } expect(":"); S.labels.push(label); var stat = statement(); S.labels.pop(); if (!(stat instanceof AST_IterationStatement)) { // check for `continue` that refers to this label. // those should be reported as syntax errors. // https://github.com/mishoo/UglifyJS2/issues/287 label.references.forEach(function(ref) { if (ref instanceof AST_Continue) { ref = ref.label.start; croak("Continue label `" + label.name + "` refers to non-IterationStatement.", ref.line, ref.col, ref.pos); } }); } return new AST_LabeledStatement({ body: stat, label: label }); } function simple_statement(tmp) { return new AST_SimpleStatement({ body: (tmp = expression(true), semicolon(), tmp) }); } function break_cont(type) { var label = null, ldef; if (!can_insert_semicolon()) { label = as_symbol(AST_LabelRef, true); } if (label != null) { ldef = find_if(function(l) { return l.name == label.name; }, S.labels); if (!ldef) croak("Undefined label " + label.name); label.thedef = ldef; } else if (S.in_loop == 0) croak(type.TYPE + " not inside a loop or switch"); semicolon(); var stat = new type({ label: label }); if (ldef) ldef.references.push(stat); return stat; } function for_() { expect("("); var init = null; if (!is("punc", ";")) { init = is("keyword", "var") ? (next(), var_(true)) : expression(true, true); if (is("operator", "in")) { if (init instanceof AST_Var) { if (init.definitions.length > 1) croak("Only one variable declaration allowed in for..in loop", init.start.line, init.start.col, init.start.pos); } else if (!is_assignable(init)) { croak("Invalid left-hand side in for..in loop", init.start.line, init.start.col, init.start.pos); } next(); return for_in(init); } } return regular_for(init); } function regular_for(init) { expect(";"); var test = is("punc", ";") ? null : expression(true); expect(";"); var step = is("punc", ")") ? null : expression(true); expect(")"); return new AST_For({ init : init, condition : test, step : step, body : in_loop(statement) }); } function for_in(init) { var obj = expression(true); expect(")"); return new AST_ForIn({ init : init, object : obj, body : in_loop(statement) }); } var function_ = function(ctor) { var in_statement = ctor === AST_Defun; var name = is("name") ? as_symbol(in_statement ? AST_SymbolDefun : AST_SymbolLambda) : null; if (in_statement && !name) expect_token("name"); if (name && ctor !== AST_Accessor && !(name instanceof AST_SymbolDeclaration)) unexpected(prev()); expect("("); var argnames = []; for (var first = true; !is("punc", ")");) { if (first) first = false; else expect(","); argnames.push(as_symbol(AST_SymbolFunarg)); } next(); var loop = S.in_loop; var labels = S.labels; ++S.in_function; S.in_directives = true; S.input.push_directives_stack(); S.in_loop = 0; S.labels = []; var body = block_(true); if (S.input.has_directive("use strict")) { if (name) strict_verify_symbol(name); argnames.forEach(strict_verify_symbol); } S.input.pop_directives_stack(); --S.in_function; S.in_loop = loop; S.labels = labels; return new ctor({ name: name, argnames: argnames, body: body }); }; function if_() { var cond = parenthesised(), body = statement(), belse = null; if (is("keyword", "else")) { next(); belse = statement(); } return new AST_If({ condition : cond, body : body, alternative : belse }); } function block_(strict_defun) { expect("{"); var a = []; while (!is("punc", "}")) { if (is("eof")) expect_token("punc", "}"); a.push(statement(strict_defun)); } next(); return a; } function switch_body_() { expect("{"); var a = [], cur = null, branch = null, tmp; while (!is("punc", "}")) { if (is("eof")) expect_token("punc", "}"); if (is("keyword", "case")) { if (branch) branch.end = prev(); cur = []; branch = new AST_Case({ start : (tmp = S.token, next(), tmp), expression : expression(true), body : cur }); a.push(branch); expect(":"); } else if (is("keyword", "default")) { if (branch) branch.end = prev(); cur = []; branch = new AST_Default({ start : (tmp = S.token, next(), expect(":"), tmp), body : cur }); a.push(branch); } else { if (!cur) unexpected(); cur.push(statement()); } } if (branch) branch.end = prev(); next(); return a; } function try_() { var body = block_(), bcatch = null, bfinally = null; if (is("keyword", "catch")) { var start = S.token; next(); expect("("); var name = as_symbol(AST_SymbolCatch); expect(")"); bcatch = new AST_Catch({ start : start, argname : name, body : block_(), end : prev() }); } if (is("keyword", "finally")) { var start = S.token; next(); bfinally = new AST_Finally({ start : start, body : block_(), end : prev() }); } if (!bcatch && !bfinally) croak("Missing catch/finally blocks"); return new AST_Try({ body : body, bcatch : bcatch, bfinally : bfinally }); } function vardefs(no_in) { var a = []; for (;;) { a.push(new AST_VarDef({ start : S.token, name : as_symbol(AST_SymbolVar), value : is("operator", "=") ? (next(), expression(false, no_in)) : null, end : prev() })); if (!is("punc", ",")) break; next(); } return a; } var var_ = function(no_in) { return new AST_Var({ start : prev(), definitions : vardefs(no_in), end : prev() }); }; var new_ = function(allow_calls) { var start = S.token; expect_token("operator", "new"); var newexp = expr_atom(false), args; if (is("punc", "(")) { next(); args = expr_list(")"); } else { args = []; } var call = new AST_New({ start : start, expression : newexp, args : args, end : prev() }); mark_pure(call); return subscripts(call, allow_calls); }; function as_atom_node() { var tok = S.token, ret; switch (tok.type) { case "name": ret = _make_symbol(AST_SymbolRef); break; case "num": ret = new AST_Number({ start: tok, end: tok, value: tok.value }); break; case "string": ret = new AST_String({ start : tok, end : tok, value : tok.value, quote : tok.quote }); break; case "regexp": ret = new AST_RegExp({ start: tok, end: tok, value: tok.value }); break; case "atom": switch (tok.value) { case "false": ret = new AST_False({ start: tok, end: tok }); break; case "true": ret = new AST_True({ start: tok, end: tok }); break; case "null": ret = new AST_Null({ start: tok, end: tok }); break; } break; } next(); return ret; } var expr_atom = function(allow_calls) { if (is("operator", "new")) { return new_(allow_calls); } var start = S.token; if (is("punc")) { switch (start.value) { case "(": next(); var ex = expression(true); var len = start.comments_before.length; [].unshift.apply(ex.start.comments_before, start.comments_before); start.comments_before = ex.start.comments_before; start.comments_before_length = len; if (len == 0 && start.comments_before.length > 0) { var comment = start.comments_before[0]; if (!comment.nlb) { comment.nlb = start.nlb; start.nlb = false; } } start.comments_after = ex.start.comments_after; ex.start = start; expect(")"); var end = prev(); end.comments_before = ex.end.comments_before; [].push.apply(ex.end.comments_after, end.comments_after); end.comments_after = ex.end.comments_after; ex.end = end; if (ex instanceof AST_Call) mark_pure(ex); return subscripts(ex, allow_calls); case "[": return subscripts(array_(), allow_calls); case "{": return subscripts(object_(), allow_calls); } unexpected(); } if (is("keyword", "function")) { next(); var func = function_(AST_Function); func.start = start; func.end = prev(); return subscripts(func, allow_calls); } if (ATOMIC_START_TOKEN[S.token.type]) { return subscripts(as_atom_node(), allow_calls); } unexpected(); }; function expr_list(closing, allow_trailing_comma, allow_empty) { var first = true, a = []; while (!is("punc", closing)) { if (first) first = false; else expect(","); if (allow_trailing_comma && is("punc", closing)) break; if (is("punc", ",") && allow_empty) { a.push(new AST_Hole({ start: S.token, end: S.token })); } else { a.push(expression(false)); } } next(); return a; } var array_ = embed_tokens(function() { expect("["); return new AST_Array({ elements: expr_list("]", !options.strict, true) }); }); var create_accessor = embed_tokens(function() { return function_(AST_Accessor); }); var object_ = embed_tokens(function() { expect("{"); var first = true, a = []; while (!is("punc", "}")) { if (first) first = false; else expect(","); if (!options.strict && is("punc", "}")) // allow trailing comma break; var start = S.token; var type = start.type; var name = as_property_name(); if (type == "name" && !is("punc", ":")) { var key = new AST_SymbolAccessor({ start: S.token, name: "" + as_property_name(), end: prev() }); if (name == "get") { a.push(new AST_ObjectGetter({ start : start, key : key, value : create_accessor(), end : prev() })); continue; } if (name == "set") { a.push(new AST_ObjectSetter({ start : start, key : key, value : create_accessor(), end : prev() })); continue; } } expect(":"); a.push(new AST_ObjectKeyVal({ start : start, quote : start.quote, key : "" + name, value : expression(false), end : prev() })); } next(); return new AST_Object({ properties: a }); }); function as_property_name() { var tmp = S.token; switch (tmp.type) { case "operator": if (!KEYWORDS[tmp.value]) unexpected(); case "num": case "string": case "name": case "keyword": case "atom": next(); return tmp.value; default: unexpected(); } } function as_name() { if (!is("name")) expect_token("name"); var name = S.token.value; next(); return name; } function _make_symbol(type) { var name = S.token.value; return new (name == "this" ? AST_This : type)({ name : String(name), start : S.token, end : S.token }); } function strict_verify_symbol(sym) { if (sym.name == "arguments" || sym.name == "eval") croak("Unexpected " + sym.name + " in strict mode", sym.start.line, sym.start.col, sym.start.pos); } function as_symbol(type, noerror) { if (!is("name")) { if (!noerror) croak("Name expected"); return null; } var sym = _make_symbol(type); if (S.input.has_directive("use strict") && sym instanceof AST_SymbolDeclaration) { strict_verify_symbol(sym); } next(); return sym; } function mark_pure(call) { var start = call.start; var comments = start.comments_before; var i = HOP(start, "comments_before_length") ? start.comments_before_length : comments.length; while (--i >= 0) { var comment = comments[i]; if (/[@#]__PURE__/.test(comment.value)) { call.pure = comment; break; } } } var subscripts = function(expr, allow_calls) { var start = expr.start; if (is("punc", ".")) { next(); return subscripts(new AST_Dot({ start : start, expression : expr, property : as_name(), end : prev() }), allow_calls); } if (is("punc", "[")) { next(); var prop = expression(true); expect("]"); return subscripts(new AST_Sub({ start : start, expression : expr, property : prop, end : prev() }), allow_calls); } if (allow_calls && is("punc", "(")) { next(); var call = new AST_Call({ start : start, expression : expr, args : expr_list(")"), end : prev() }); mark_pure(call); return subscripts(call, true); } return expr; }; var maybe_unary = function(allow_calls) { var start = S.token; if (is("operator") && UNARY_PREFIX[start.value]) { next(); handle_regexp(); var ex = make_unary(AST_UnaryPrefix, start, maybe_unary(allow_calls)); ex.start = start; ex.end = prev(); return ex; } var val = expr_atom(allow_calls); while (is("operator") && UNARY_POSTFIX[S.token.value] && !has_newline_before(S.token)) { val = make_unary(AST_UnaryPostfix, S.token, val); val.start = start; val.end = S.token; next(); } return val; }; function make_unary(ctor, token, expr) { var op = token.value; switch (op) { case "++": case "--": if (!is_assignable(expr)) croak("Invalid use of " + op + " operator", token.line, token.col, token.pos); break; case "delete": if (expr instanceof AST_SymbolRef && S.input.has_directive("use strict")) croak("Calling delete on expression not allowed in strict mode", expr.start.line, expr.start.col, expr.start.pos); break; } return new ctor({ operator: op, expression: expr }); } var expr_op = function(left, min_prec, no_in) { var op = is("operator") ? S.token.value : null; if (op == "in" && no_in) op = null; var prec = op != null ? PRECEDENCE[op] : null; if (prec != null && prec > min_prec) { next(); var right = expr_op(maybe_unary(true), prec, no_in); return expr_op(new AST_Binary({ start : left.start, left : left, operator : op, right : right, end : right.end }), min_prec, no_in); } return left; }; function expr_ops(no_in) { return expr_op(maybe_unary(true), 0, no_in); } var maybe_conditional = function(no_in) { var start = S.token; var expr = expr_ops(no_in); if (is("operator", "?")) { next(); var yes = expression(false); expect(":"); return new AST_Conditional({ start : start, condition : expr, consequent : yes, alternative : expression(false, no_in), end : prev() }); } return expr; }; function is_assignable(expr) { return expr instanceof AST_PropAccess || expr instanceof AST_SymbolRef; } var maybe_assign = function(no_in) { var start = S.token; var left = maybe_conditional(no_in), val = S.token.value; if (is("operator") && ASSIGNMENT[val]) { if (is_assignable(left)) { next(); return new AST_Assign({ start : start, left : left, operator : val, right : maybe_assign(no_in), end : prev() }); } croak("Invalid assignment"); } return left; }; var expression = function(commas, no_in) { var start = S.token; var exprs = []; while (true) { exprs.push(maybe_assign(no_in)); if (!commas || !is("punc", ",")) break; next(); commas = true; } return exprs.length == 1 ? exprs[0] : new AST_Sequence({ start : start, expressions : exprs, end : peek() }); }; function in_loop(cont) { ++S.in_loop; var ret = cont(); --S.in_loop; return ret; } if (options.expression) { handle_regexp(); return expression(true); } return function() { var start = S.token; var body = []; S.input.push_directives_stack(); while (!is("eof")) body.push(statement(true)); S.input.pop_directives_stack(); var end = prev(); var toplevel = options.toplevel; if (toplevel) { toplevel.body = toplevel.body.concat(body); toplevel.end = end; } else { toplevel = new AST_Toplevel({ start: start, body: body, end: end }); } return toplevel; }(); } UglifyJS2-3.6.3/lib/propmangle.js000066400000000000000000000175451355252637300166000ustar00rootroot00000000000000/*********************************************************************** A JavaScript tokenizer / parser / beautifier / compressor. https://github.com/mishoo/UglifyJS2 -------------------------------- (C) --------------------------------- Author: Mihai Bazon http://mihai.bazon.net/blog Distributed under the BSD license: Copyright 2012 (c) Mihai Bazon Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***********************************************************************/ "use strict"; function find_builtins(reserved) { // NaN will be included due to Number.NaN [ "null", "true", "false", "Infinity", "-Infinity", "undefined", ].forEach(add); [ Array, Boolean, Date, Error, Function, Math, Number, Object, RegExp, String, ].forEach(function(ctor) { Object.getOwnPropertyNames(ctor).map(add); if (ctor.prototype) { Object.getOwnPropertyNames(ctor.prototype).map(add); } }); function add(name) { push_uniq(reserved, name); } } function reserve_quoted_keys(ast, reserved) { ast.walk(new TreeWalker(function(node) { if (node instanceof AST_ObjectKeyVal && node.quote) { add(node.key); } else if (node instanceof AST_Sub) { addStrings(node.property, add); } })); function add(name) { push_uniq(reserved, name); } } function addStrings(node, add) { node.walk(new TreeWalker(function(node) { if (node instanceof AST_Sequence) { addStrings(node.tail_node(), add); } else if (node instanceof AST_String) { add(node.value); } else if (node instanceof AST_Conditional) { addStrings(node.consequent, add); addStrings(node.alternative, add); } return true; })); } function mangle_properties(ast, options) { options = defaults(options, { builtins: false, cache: null, debug: false, keep_quoted: false, only_cache: false, regex: null, reserved: null, }, true); var reserved = options.reserved; if (!Array.isArray(reserved)) reserved = []; if (!options.builtins) find_builtins(reserved); var cname = -1; var cache; if (options.cache) { cache = options.cache.props; cache.each(function(mangled_name) { push_uniq(reserved, mangled_name); }); } else { cache = new Dictionary(); } var regex = options.regex; // note debug is either false (disabled), or a string of the debug suffix to use (enabled). // note debug may be enabled as an empty string, which is falsey. Also treat passing 'true' // the same as passing an empty string. var debug = options.debug !== false; var debug_suffix; if (debug) debug_suffix = options.debug === true ? "" : options.debug; var names_to_mangle = []; var unmangleable = []; // step 1: find candidates to mangle ast.walk(new TreeWalker(function(node) { if (node instanceof AST_ObjectKeyVal) { add(node.key); } else if (node instanceof AST_ObjectProperty) { // setter or getter, since KeyVal is handled above add(node.key.name); } else if (node instanceof AST_Dot) { add(node.property); } else if (node instanceof AST_Sub) { addStrings(node.property, add); } else if (node instanceof AST_Call && node.expression.print_to_string() == "Object.defineProperty") { addStrings(node.args[1], add); } })); // step 2: transform the tree, renaming properties return ast.transform(new TreeTransformer(function(node) { if (node instanceof AST_ObjectKeyVal) { node.key = mangle(node.key); } else if (node instanceof AST_ObjectProperty) { // setter or getter node.key.name = mangle(node.key.name); } else if (node instanceof AST_Dot) { node.property = mangle(node.property); } else if (!options.keep_quoted && node instanceof AST_Sub) { node.property = mangleStrings(node.property); } else if (node instanceof AST_Call && node.expression.print_to_string() == "Object.defineProperty") { node.args[1] = mangleStrings(node.args[1]); } })); // only function declarations after this line function can_mangle(name) { if (unmangleable.indexOf(name) >= 0) return false; if (reserved.indexOf(name) >= 0) return false; if (options.only_cache) return cache.has(name); if (/^-?[0-9]+(\.[0-9]+)?(e[+-][0-9]+)?$/.test(name)) return false; return true; } function should_mangle(name) { if (regex && !regex.test(name)) return false; if (reserved.indexOf(name) >= 0) return false; return cache.has(name) || names_to_mangle.indexOf(name) >= 0; } function add(name) { if (can_mangle(name)) push_uniq(names_to_mangle, name); if (!should_mangle(name)) push_uniq(unmangleable, name); } function mangle(name) { if (!should_mangle(name)) { return name; } var mangled = cache.get(name); if (!mangled) { if (debug) { // debug mode: use a prefix and suffix to preserve readability, e.g. o.foo -> o._$foo$NNN_. var debug_mangled = "_$" + name + "$" + debug_suffix + "_"; if (can_mangle(debug_mangled)) mangled = debug_mangled; } // either debug mode is off, or it is on and we could not use the mangled name if (!mangled) do { mangled = base54(++cname); } while (!can_mangle(mangled)); cache.set(name, mangled); } return mangled; } function mangleStrings(node) { return node.transform(new TreeTransformer(function(node) { if (node instanceof AST_Sequence) { var last = node.expressions.length - 1; node.expressions[last] = mangleStrings(node.expressions[last]); } else if (node instanceof AST_String) { node.value = mangle(node.value); } else if (node instanceof AST_Conditional) { node.consequent = mangleStrings(node.consequent); node.alternative = mangleStrings(node.alternative); } return node; })); } } UglifyJS2-3.6.3/lib/scope.js000066400000000000000000000505141355252637300155360ustar00rootroot00000000000000/*********************************************************************** A JavaScript tokenizer / parser / beautifier / compressor. https://github.com/mishoo/UglifyJS2 -------------------------------- (C) --------------------------------- Author: Mihai Bazon http://mihai.bazon.net/blog Distributed under the BSD license: Copyright 2012 (c) Mihai Bazon Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***********************************************************************/ "use strict"; function SymbolDef(scope, orig, init) { this.name = orig.name; this.orig = [ orig ]; this.init = init; this.eliminated = 0; this.scope = scope; this.references = []; this.replaced = 0; this.global = false; this.mangled_name = null; this.undeclared = false; this.id = SymbolDef.next_id++; this.lambda = orig instanceof AST_SymbolLambda; } SymbolDef.next_id = 1; SymbolDef.prototype = { unmangleable: function(options) { return this.global && !options.toplevel || this.undeclared || !options.eval && this.scope.pinned() || options.keep_fnames && (this.orig[0] instanceof AST_SymbolLambda || this.orig[0] instanceof AST_SymbolDefun); }, mangle: function(options) { var cache = options.cache && options.cache.props; if (this.global && cache && cache.has(this.name)) { this.mangled_name = cache.get(this.name); } else if (!this.mangled_name && !this.unmangleable(options)) { var def; if (def = this.redefined()) { this.mangled_name = def.mangled_name || def.name; } else { this.mangled_name = next_mangled_name(this.scope, options, this); } if (this.global && cache) { cache.set(this.name, this.mangled_name); } } }, redefined: function() { return this.defun && this.defun.variables.get(this.name); } }; AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) { options = defaults(options, { cache: null, ie8: false, }); // pass 1: setup scope chaining and handle definitions var self = this; var scope = self.parent_scope = null; var defun = null; var tw = new TreeWalker(function(node, descend) { if (node instanceof AST_Catch) { var save_scope = scope; scope = new AST_Scope(node); scope.init_scope_vars(save_scope); descend(); scope = save_scope; return true; } if (node instanceof AST_Scope) { node.init_scope_vars(scope); var save_scope = scope; var save_defun = defun; defun = scope = node; descend(); scope = save_scope; defun = save_defun; return true; } if (node instanceof AST_With) { for (var s = scope; s; s = s.parent_scope) s.uses_with = true; return; } if (node instanceof AST_Symbol) { node.scope = scope; } if (node instanceof AST_Label) { node.thedef = node; node.references = []; } if (node instanceof AST_SymbolDefun) { // This should be defined in the parent scope, as we encounter the // AST_Defun node before getting to its AST_Symbol. (node.scope = defun.parent_scope.resolve()).def_function(node, defun); } else if (node instanceof AST_SymbolLambda) { var def = defun.def_function(node, node.name == "arguments" ? undefined : defun); if (options.ie8) def.defun = defun.parent_scope.resolve(); } else if (node instanceof AST_SymbolVar) { defun.def_variable(node, node.TYPE == "SymbolVar" ? null : undefined); if (defun !== scope) { node.mark_enclosed(options); var def = scope.find_variable(node); if (node.thedef !== def) { node.thedef = def; } node.reference(options); } } else if (node instanceof AST_SymbolCatch) { scope.def_variable(node).defun = defun; } }); self.walk(tw); // pass 2: find back references and eval self.globals = new Dictionary(); var tw = new TreeWalker(function(node) { if (node instanceof AST_LoopControl) { if (node.label) node.label.thedef.references.push(node); return true; } if (node instanceof AST_SymbolRef) { var name = node.name; if (name == "eval" && tw.parent() instanceof AST_Call) { for (var s = node.scope; s && !s.uses_eval; s = s.parent_scope) { s.uses_eval = true; } } var sym = node.scope.find_variable(name); if (!sym) { sym = self.def_global(node); } else if (sym.scope instanceof AST_Lambda && name == "arguments") { sym.scope.uses_arguments = true; } node.thedef = sym; node.reference(options); return true; } // ensure mangling works if catch reuses a scope variable if (node instanceof AST_SymbolCatch) { var def = node.definition().redefined(); if (def) for (var s = node.scope; s; s = s.parent_scope) { push_uniq(s.enclosed, def); if (s === def.scope) break; } return true; } }); self.walk(tw); // pass 3: fix up any scoping issue with IE8 if (options.ie8) self.walk(new TreeWalker(function(node) { if (node instanceof AST_SymbolCatch) { var scope = node.thedef.defun; if (scope.name instanceof AST_SymbolLambda && scope.name.name == node.name) { scope = scope.parent_scope.resolve(); } redefine(node, scope); return true; } if (node instanceof AST_SymbolLambda) { var def = node.thedef; redefine(node, node.scope.parent_scope.resolve()); if (typeof node.thedef.init !== "undefined") { node.thedef.init = false; } else if (def.init) { node.thedef.init = def.init; } return true; } })); function redefine(node, scope) { var name = node.name; var old_def = node.thedef; var new_def = scope.find_variable(name) || self.globals.get(name) || scope.def_variable(node); old_def.orig.concat(old_def.references).forEach(function(node) { node.thedef = new_def; node.reference(options); }); if (old_def.lambda) new_def.lambda = true; if (new_def.undeclared) self.variables.set(name, new_def); } }); AST_Toplevel.DEFMETHOD("def_global", function(node) { var globals = this.globals, name = node.name; if (globals.has(name)) { return globals.get(name); } else { var g = new SymbolDef(this, node); g.undeclared = true; g.global = true; globals.set(name, g); return g; } }); AST_Scope.DEFMETHOD("init_scope_vars", function(parent_scope) { this.variables = new Dictionary(); // map name to AST_SymbolVar (variables defined in this scope; includes functions) this.functions = new Dictionary(); // map name to AST_SymbolDefun (functions defined in this scope) this.uses_with = false; // will be set to true if this or some nested scope uses the `with` statement this.uses_eval = false; // will be set to true if this or nested scope uses the global `eval` this.parent_scope = parent_scope; // the parent scope this.enclosed = []; // a list of variables from this or outer scope(s) that are referenced from this or inner scopes this.cname = -1; // the current index for mangling functions/variables }); AST_Lambda.DEFMETHOD("init_scope_vars", function() { AST_Scope.prototype.init_scope_vars.apply(this, arguments); this.uses_arguments = false; this.def_variable(new AST_SymbolFunarg({ name: "arguments", start: this.start, end: this.end })); }); AST_Symbol.DEFMETHOD("mark_enclosed", function(options) { var def = this.definition(); for (var s = this.scope; s; s = s.parent_scope) { push_uniq(s.enclosed, def); if (options.keep_fnames) { s.functions.each(function(d) { push_uniq(def.scope.enclosed, d); }); } if (s === def.scope) break; } }); AST_Symbol.DEFMETHOD("reference", function(options) { this.definition().references.push(this); this.mark_enclosed(options); }); AST_Scope.DEFMETHOD("find_variable", function(name) { if (name instanceof AST_Symbol) name = name.name; return this.variables.get(name) || (this.parent_scope && this.parent_scope.find_variable(name)); }); AST_Scope.DEFMETHOD("def_function", function(symbol, init) { var def = this.def_variable(symbol, init); if (!def.init || def.init instanceof AST_Defun) def.init = init; this.functions.set(symbol.name, def); return def; }); AST_Scope.DEFMETHOD("def_variable", function(symbol, init) { var def = this.variables.get(symbol.name); if (def) { def.orig.push(symbol); if (def.init && (def.scope !== symbol.scope || def.init instanceof AST_Function)) { def.init = init; } } else { def = new SymbolDef(this, symbol, init); this.variables.set(symbol.name, def); def.global = !this.parent_scope; } return symbol.thedef = def; }); AST_Lambda.DEFMETHOD("resolve", return_this); AST_Scope.DEFMETHOD("resolve", function() { return this.parent_scope.resolve(); }); AST_Toplevel.DEFMETHOD("resolve", return_this); function names_in_use(scope, options) { var names = scope.names_in_use; if (!names) { scope.names_in_use = names = Object.create(scope.mangled_names || null); scope.cname_holes = []; var cache = options.cache && options.cache.props; scope.enclosed.forEach(function(def) { if (def.unmangleable(options)) names[def.name] = true; if (def.global && cache && cache.has(def.name)) { names[cache.get(def.name)] = true; } }); } return names; } function next_mangled_name(scope, options, def) { var in_use = names_in_use(scope, options); var holes = scope.cname_holes; var names = Object.create(null); var scopes = [ scope ]; def.references.forEach(function(sym) { var scope = sym.scope; do { if (scopes.indexOf(scope) < 0) { for (var name in names_in_use(scope, options)) { names[name] = true; } scopes.push(scope); } else break; } while (scope = scope.parent_scope); }); var name; for (var i = 0; i < holes.length; i++) { name = base54(holes[i]); if (names[name]) continue; holes.splice(i, 1); scope.names_in_use[name] = true; return name; } while (true) { name = base54(++scope.cname); if (in_use[name] || RESERVED_WORDS[name] || options.reserved.has[name]) continue; if (!names[name]) break; holes.push(scope.cname); } scope.names_in_use[name] = true; return name; } AST_Symbol.DEFMETHOD("unmangleable", function(options) { var def = this.definition(); return !def || def.unmangleable(options); }); // labels are always mangleable AST_Label.DEFMETHOD("unmangleable", return_false); AST_Symbol.DEFMETHOD("unreferenced", function() { return !this.definition().references.length && !this.scope.pinned(); }); AST_Symbol.DEFMETHOD("definition", function() { return this.thedef; }); AST_Symbol.DEFMETHOD("global", function() { return this.definition().global; }); function _default_mangler_options(options) { options = defaults(options, { eval : false, ie8 : false, keep_fnames : false, reserved : [], toplevel : false, }); if (!Array.isArray(options.reserved)) options.reserved = []; // Never mangle arguments push_uniq(options.reserved, "arguments"); options.reserved.has = makePredicate(options.reserved); return options; } AST_Toplevel.DEFMETHOD("mangle_names", function(options) { options = _default_mangler_options(options); // We only need to mangle declaration nodes. Special logic wired // into the code generator will display the mangled name if it's // present (and for AST_SymbolRef-s it'll use the mangled name of // the AST_SymbolDeclaration that it points to). var lname = -1; if (options.cache && options.cache.props) { var mangled_names = this.mangled_names = Object.create(null); options.cache.props.each(function(mangled_name) { mangled_names[mangled_name] = true; }); } var redefined = []; var tw = new TreeWalker(function(node, descend) { if (node instanceof AST_LabeledStatement) { // lname is incremented when we get to the AST_Label var save_nesting = lname; descend(); lname = save_nesting; return true; } if (node instanceof AST_Scope) { descend(); if (options.cache && node instanceof AST_Toplevel) { node.globals.each(mangle); } node.variables.each(function(def) { if (!defer_redef(def)) mangle(def); }); return true; } if (node instanceof AST_Label) { var name; do { name = base54(++lname); } while (RESERVED_WORDS[name]); node.mangled_name = name; return true; } if (!options.ie8 && node instanceof AST_Catch) { var def = node.argname.definition(); var redef = defer_redef(def, node.argname); descend(); if (!redef) mangle(def); return true; } }); this.walk(tw); redefined.forEach(mangle); function mangle(def) { if (options.reserved.has[def.name]) return; def.mangle(options); } function defer_redef(def, node) { var redef = def.redefined(); if (!redef) return false; redefined.push(def); def.references.forEach(reference); if (node) reference(node); return true; function reference(sym) { sym.thedef = redef; sym.reference(options); sym.thedef = def; } } }); AST_Toplevel.DEFMETHOD("find_colliding_names", function(options) { var cache = options.cache && options.cache.props; var avoid = Object.create(null); options.reserved.forEach(to_avoid); this.globals.each(add_def); this.walk(new TreeWalker(function(node) { if (node instanceof AST_Scope) node.variables.each(add_def); if (node instanceof AST_SymbolCatch) add_def(node.definition()); })); return avoid; function to_avoid(name) { avoid[name] = true; } function add_def(def) { var name = def.name; if (def.global && cache && cache.has(name)) name = cache.get(name); else if (!def.unmangleable(options)) return; to_avoid(name); } }); AST_Toplevel.DEFMETHOD("expand_names", function(options) { base54.reset(); base54.sort(); options = _default_mangler_options(options); var avoid = this.find_colliding_names(options); var cname = 0; this.globals.each(rename); this.walk(new TreeWalker(function(node) { if (node instanceof AST_Scope) node.variables.each(rename); if (node instanceof AST_SymbolCatch) rename(node.definition()); })); function next_name() { var name; do { name = base54(cname++); } while (avoid[name] || RESERVED_WORDS[name]); return name; } function rename(def) { if (def.global && options.cache) return; if (def.unmangleable(options)) return; if (options.reserved.has[def.name]) return; var redef = def.redefined(); var name = redef ? redef.rename || redef.name : next_name(); def.rename = name; def.orig.forEach(function(sym) { sym.name = name; }); def.references.forEach(function(sym) { sym.name = name; }); } }); AST_Node.DEFMETHOD("tail_node", return_this); AST_Sequence.DEFMETHOD("tail_node", function() { return this.expressions[this.expressions.length - 1]; }); AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options) { options = _default_mangler_options(options); base54.reset(); try { AST_Node.prototype.print = function(stream, force_parens) { this._print(stream, force_parens); if (this instanceof AST_Symbol && !this.unmangleable(options)) { base54.consider(this.name, -1); } else if (options.properties) { if (this instanceof AST_Dot) { base54.consider(this.property, -1); } else if (this instanceof AST_Sub) { skip_string(this.property); } } }; base54.consider(this.print_to_string(), 1); } finally { AST_Node.prototype.print = AST_Node.prototype._print; } base54.sort(); function skip_string(node) { if (node instanceof AST_String) { base54.consider(node.value, -1); } else if (node instanceof AST_Conditional) { skip_string(node.consequent); skip_string(node.alternative); } else if (node instanceof AST_Sequence) { skip_string(node.tail_node()); } } }); var base54 = (function() { var freq = Object.create(null); function init(chars) { var array = []; for (var i = 0; i < chars.length; i++) { var ch = chars[i]; array.push(ch); freq[ch] = -1e-2 * i; } return array; } var digits = init("0123456789"); var leading = init("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"); var chars, frequency; function reset() { frequency = Object.create(freq); } base54.consider = function(str, delta) { for (var i = str.length; --i >= 0;) { frequency[str[i]] += delta; } }; function compare(a, b) { return frequency[b] - frequency[a]; } base54.sort = function() { chars = leading.sort(compare).concat(digits.sort(compare)); }; base54.reset = reset; reset(); function base54(num) { var ret = "", base = 54; num++; do { num--; ret += chars[num % base]; num = Math.floor(num / base); base = 64; } while (num > 0); return ret; } return base54; })(); UglifyJS2-3.6.3/lib/sourcemap.js000066400000000000000000000075171355252637300164300ustar00rootroot00000000000000/*********************************************************************** A JavaScript tokenizer / parser / beautifier / compressor. https://github.com/mishoo/UglifyJS2 -------------------------------- (C) --------------------------------- Author: Mihai Bazon http://mihai.bazon.net/blog Distributed under the BSD license: Copyright 2012 (c) Mihai Bazon Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***********************************************************************/ "use strict"; // a small wrapper around fitzgen's source-map library function SourceMap(options) { options = defaults(options, { file: null, root: null, orig: null, orig_line_diff: 0, dest_line_diff: 0, }, true); var generator = new MOZ_SourceMap.SourceMapGenerator({ file: options.file, sourceRoot: options.root }); var maps = options.orig && Object.create(null); if (maps) for (var source in options.orig) { var map = new MOZ_SourceMap.SourceMapConsumer(options.orig[source]); if (Array.isArray(options.orig[source].sources)) { map._sources.toArray().forEach(function(source) { var sourceContent = map.sourceContentFor(source, true); if (sourceContent) generator.setSourceContent(source, sourceContent); }); } maps[source] = map; } return { add: function(source, gen_line, gen_col, orig_line, orig_col, name) { var map = maps && maps[source]; if (map) { var info = map.originalPositionFor({ line: orig_line, column: orig_col }); if (info.source === null) return; source = info.source; orig_line = info.line; orig_col = info.column; name = info.name || name; } generator.addMapping({ name: name, source: source, generated: { line: gen_line + options.dest_line_diff, column: gen_col }, original: { line: orig_line + options.orig_line_diff, column: orig_col } }); }, get: function() { return generator; }, toString: function() { return JSON.stringify(generator.toJSON()); } }; } UglifyJS2-3.6.3/lib/transform.js000066400000000000000000000153501355252637300164370ustar00rootroot00000000000000/*********************************************************************** A JavaScript tokenizer / parser / beautifier / compressor. https://github.com/mishoo/UglifyJS2 -------------------------------- (C) --------------------------------- Author: Mihai Bazon http://mihai.bazon.net/blog Distributed under the BSD license: Copyright 2012 (c) Mihai Bazon Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***********************************************************************/ "use strict"; function TreeTransformer(before, after) { TreeWalker.call(this); this.before = before; this.after = after; } TreeTransformer.prototype = new TreeWalker; (function(DEF) { function do_list(list, tw) { return MAP(list, function(node) { return node.transform(tw, true); }); } DEF(AST_Node, noop); DEF(AST_LabeledStatement, function(self, tw) { self.label = self.label.transform(tw); self.body = self.body.transform(tw); }); DEF(AST_SimpleStatement, function(self, tw) { self.body = self.body.transform(tw); }); DEF(AST_Block, function(self, tw) { self.body = do_list(self.body, tw); }); DEF(AST_Do, function(self, tw) { self.body = self.body.transform(tw); self.condition = self.condition.transform(tw); }); DEF(AST_While, function(self, tw) { self.condition = self.condition.transform(tw); self.body = self.body.transform(tw); }); DEF(AST_For, function(self, tw) { if (self.init) self.init = self.init.transform(tw); if (self.condition) self.condition = self.condition.transform(tw); if (self.step) self.step = self.step.transform(tw); self.body = self.body.transform(tw); }); DEF(AST_ForIn, function(self, tw) { self.init = self.init.transform(tw); self.object = self.object.transform(tw); self.body = self.body.transform(tw); }); DEF(AST_With, function(self, tw) { self.expression = self.expression.transform(tw); self.body = self.body.transform(tw); }); DEF(AST_Exit, function(self, tw) { if (self.value) self.value = self.value.transform(tw); }); DEF(AST_LoopControl, function(self, tw) { if (self.label) self.label = self.label.transform(tw); }); DEF(AST_If, function(self, tw) { self.condition = self.condition.transform(tw); self.body = self.body.transform(tw); if (self.alternative) self.alternative = self.alternative.transform(tw); }); DEF(AST_Switch, function(self, tw) { self.expression = self.expression.transform(tw); self.body = do_list(self.body, tw); }); DEF(AST_Case, function(self, tw) { self.expression = self.expression.transform(tw); self.body = do_list(self.body, tw); }); DEF(AST_Try, function(self, tw) { self.body = do_list(self.body, tw); if (self.bcatch) self.bcatch = self.bcatch.transform(tw); if (self.bfinally) self.bfinally = self.bfinally.transform(tw); }); DEF(AST_Catch, function(self, tw) { self.argname = self.argname.transform(tw); self.body = do_list(self.body, tw); }); DEF(AST_Definitions, function(self, tw) { self.definitions = do_list(self.definitions, tw); }); DEF(AST_VarDef, function(self, tw) { self.name = self.name.transform(tw); if (self.value) self.value = self.value.transform(tw); }); DEF(AST_Lambda, function(self, tw) { if (self.name) self.name = self.name.transform(tw); self.argnames = do_list(self.argnames, tw); self.body = do_list(self.body, tw); }); DEF(AST_Call, function(self, tw) { self.expression = self.expression.transform(tw); self.args = do_list(self.args, tw); }); DEF(AST_Sequence, function(self, tw) { self.expressions = do_list(self.expressions, tw); }); DEF(AST_Dot, function(self, tw) { self.expression = self.expression.transform(tw); }); DEF(AST_Sub, function(self, tw) { self.expression = self.expression.transform(tw); self.property = self.property.transform(tw); }); DEF(AST_Unary, function(self, tw) { self.expression = self.expression.transform(tw); }); DEF(AST_Binary, function(self, tw) { self.left = self.left.transform(tw); self.right = self.right.transform(tw); }); DEF(AST_Conditional, function(self, tw) { self.condition = self.condition.transform(tw); self.consequent = self.consequent.transform(tw); self.alternative = self.alternative.transform(tw); }); DEF(AST_Array, function(self, tw) { self.elements = do_list(self.elements, tw); }); DEF(AST_Object, function(self, tw) { self.properties = do_list(self.properties, tw); }); DEF(AST_ObjectProperty, function(self, tw) { self.value = self.value.transform(tw); }); })(function(node, descend) { node.DEFMETHOD("transform", function(tw, in_list) { var x, y; tw.push(this); if (tw.before) x = tw.before(this, descend, in_list); if (typeof x === "undefined") { x = this; descend(x, tw); if (tw.after) { y = tw.after(x, in_list); if (typeof y !== "undefined") x = y; } } tw.pop(); return x; }); }); UglifyJS2-3.6.3/lib/utils.js000066400000000000000000000204431355252637300155630ustar00rootroot00000000000000/*********************************************************************** A JavaScript tokenizer / parser / beautifier / compressor. https://github.com/mishoo/UglifyJS2 -------------------------------- (C) --------------------------------- Author: Mihai Bazon http://mihai.bazon.net/blog Distributed under the BSD license: Copyright 2012 (c) Mihai Bazon Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***********************************************************************/ "use strict"; function characters(str) { return str.split(""); } function member(name, array) { return array.indexOf(name) >= 0; } function find_if(func, array) { for (var i = array.length; --i >= 0;) if (func(array[i])) return array[i]; } function repeat_string(str, i) { if (i <= 0) return ""; if (i == 1) return str; var d = repeat_string(str, i >> 1); d += d; return i & 1 ? d + str : d; } function configure_error_stack(fn) { Object.defineProperty(fn.prototype, "stack", { get: function() { var err = new Error(this.message); err.name = this.name; try { throw err; } catch (e) { return e.stack; } } }); } function DefaultsError(msg, defs) { this.message = msg; this.defs = defs; } DefaultsError.prototype = Object.create(Error.prototype); DefaultsError.prototype.constructor = DefaultsError; DefaultsError.prototype.name = "DefaultsError"; configure_error_stack(DefaultsError); function defaults(args, defs, croak) { if (args === true) args = {}; var ret = args || {}; if (croak) for (var i in ret) if (HOP(ret, i) && !HOP(defs, i)) { throw new DefaultsError("`" + i + "` is not a supported option", defs); } for (var i in defs) if (HOP(defs, i)) { ret[i] = (args && HOP(args, i)) ? args[i] : defs[i]; } return ret; } function merge(obj, ext) { var count = 0; for (var i in ext) if (HOP(ext, i)) { obj[i] = ext[i]; count++; } return count; } function noop() {} function return_false() { return false; } function return_true() { return true; } function return_this() { return this; } function return_null() { return null; } var MAP = (function() { function MAP(a, f, backwards) { var ret = [], top = [], i; function doit() { var val = f(a[i], i); var is_last = val instanceof Last; if (is_last) val = val.v; if (val instanceof AtTop) { val = val.v; if (val instanceof Splice) { top.push.apply(top, backwards ? val.v.slice().reverse() : val.v); } else { top.push(val); } } else if (val !== skip) { if (val instanceof Splice) { ret.push.apply(ret, backwards ? val.v.slice().reverse() : val.v); } else { ret.push(val); } } return is_last; } if (Array.isArray(a)) { if (backwards) { for (i = a.length; --i >= 0;) if (doit()) break; ret.reverse(); top.reverse(); } else { for (i = 0; i < a.length; ++i) if (doit()) break; } } else { for (i in a) if (HOP(a, i)) if (doit()) break; } return top.concat(ret); } MAP.at_top = function(val) { return new AtTop(val) }; MAP.splice = function(val) { return new Splice(val) }; MAP.last = function(val) { return new Last(val) }; var skip = MAP.skip = {}; function AtTop(val) { this.v = val } function Splice(val) { this.v = val } function Last(val) { this.v = val } return MAP; })(); function push_uniq(array, el) { if (array.indexOf(el) < 0) return array.push(el); } function string_template(text, props) { return text.replace(/\{(.+?)\}/g, function(str, p) { return props && props[p]; }); } function remove(array, el) { var index = array.indexOf(el); if (index >= 0) array.splice(index, 1); } function makePredicate(words) { if (!Array.isArray(words)) words = words.split(" "); var map = Object.create(null); words.forEach(function(word) { map[word] = true; }); return map; } function all(array, predicate) { for (var i = array.length; --i >= 0;) if (!predicate(array[i])) return false; return true; } function Dictionary() { this._values = Object.create(null); this._size = 0; } Dictionary.prototype = { set: function(key, val) { if (!this.has(key)) ++this._size; this._values["$" + key] = val; return this; }, add: function(key, val) { if (this.has(key)) { this.get(key).push(val); } else { this.set(key, [ val ]); } return this; }, get: function(key) { return this._values["$" + key] }, del: function(key) { if (this.has(key)) { --this._size; delete this._values["$" + key]; } return this; }, has: function(key) { return ("$" + key) in this._values }, each: function(f) { for (var i in this._values) f(this._values[i], i.substr(1)); }, size: function() { return this._size; }, map: function(f) { var ret = []; for (var i in this._values) ret.push(f(this._values[i], i.substr(1))); return ret; }, clone: function() { var ret = new Dictionary(); for (var i in this._values) ret._values[i] = this._values[i]; ret._size = this._size; return ret; }, toObject: function() { return this._values } }; Dictionary.fromObject = function(obj) { var dict = new Dictionary(); dict._size = merge(dict._values, obj); return dict; }; function HOP(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); } // return true if the node at the top of the stack (that means the // innermost node in the current output) is lexically the first in // a statement. function first_in_statement(stack) { var node = stack.parent(-1); for (var i = 0, p; p = stack.parent(i++); node = p) { if (p.TYPE == "Call") { if (p.expression === node) continue; } else if (p instanceof AST_Binary) { if (p.left === node) continue; } else if (p instanceof AST_Conditional) { if (p.condition === node) continue; } else if (p instanceof AST_PropAccess) { if (p.expression === node) continue; } else if (p instanceof AST_Sequence) { if (p.expressions[0] === node) continue; } else if (p instanceof AST_Statement) { return p.body === node; } else if (p instanceof AST_UnaryPostfix) { if (p.expression === node) continue; } return false; } } UglifyJS2-3.6.3/package.json000066400000000000000000000021741355252637300156060ustar00rootroot00000000000000{ "name": "uglify-js", "description": "JavaScript parser, mangler/compressor and beautifier toolkit", "author": "Mihai Bazon (http://lisperator.net/)", "license": "BSD-2-Clause", "version": "3.6.3", "engines": { "node": ">=0.8.0" }, "maintainers": [ "Alex Lam ", "Mihai Bazon (http://lisperator.net/)" ], "repository": "mishoo/UglifyJS2", "main": "tools/node.js", "bin": { "uglifyjs": "bin/uglifyjs" }, "files": [ "bin", "lib", "tools", "LICENSE" ], "dependencies": { "commander": "~2.20.3", "source-map": "~0.6.1" }, "devDependencies": { "acorn": "~7.1.0", "semver": "~6.3.0" }, "scripts": { "test": "node test/compress.js && node test/mocha.js" }, "keywords": [ "cli", "compress", "compressor", "ecma", "ecmascript", "es", "es5", "javascript", "js", "jsmin", "min", "minification", "minifier", "minify", "optimize", "optimizer", "pack", "packer", "parse", "parser", "uglifier", "uglify" ] } UglifyJS2-3.6.3/test/000077500000000000000000000000001355252637300142735ustar00rootroot00000000000000UglifyJS2-3.6.3/test/benchmark.js000066400000000000000000000063621355252637300165720ustar00rootroot00000000000000#! /usr/bin/env node // -*- js -*- "use strict"; var createHash = require("crypto").createHash; var fetch = require("./fetch"); var fork = require("child_process").fork; var zlib = require("zlib"); var args = process.argv.slice(2); if (!args.length) { args.push("-mc"); } args.push("--timings"); var urls = [ "https://code.jquery.com/jquery-3.4.1.js", "https://code.angularjs.org/1.7.8/angular.js", "https://unpkg.com/mathjs@6.2.3/dist/math.js", "https://unpkg.com/react@15.3.2/dist/react.js", "https://cdnjs.cloudflare.com/ajax/libs/d3/5.12.0/d3.js", "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.js", "https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js", "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.12.2/ember.prod.js", "https://raw.githubusercontent.com/kangax/html-minifier/v4.0.0/dist/htmlminifier.js", ]; var results = {}; var remaining = 2 * urls.length; function done() { if (!--remaining) { var failures = []; var sum = { input: 0, output: 0, gzip: 0 }; urls.forEach(function(url) { var info = results[url]; console.log(); console.log(url); console.log(info.log); console.log("Original:", info.input, "bytes"); console.log("Uglified:", info.output, "bytes"); console.log("GZipped: ", info.gzip, "bytes"); console.log("SHA1 sum:", info.sha1); if (info.code) { failures.push(url); } sum.input += info.input; sum.output += info.output; sum.gzip += info.gzip; }); if (failures.length) { console.error("Benchmark failed:"); failures.forEach(function(url) { console.error(url); }); process.exit(1); } else { console.log(); console.log("Subtotal"); console.log(); console.log("Original:", sum.input, "bytes"); console.log("Uglified:", sum.output, "bytes"); console.log("GZipped: ", sum.gzip, "bytes"); } } } urls.forEach(function(url) { results[url] = { input: 0, output: 0, gzip: 0, log: "" }; fetch(url, function(err, res) { if (err) throw err; var uglifyjs = fork("bin/uglifyjs", args, { silent: true }); res.on("data", function(data) { results[url].input += data.length; }).pipe(uglifyjs.stdin); uglifyjs.stdout.on("data", function(data) { results[url].output += data.length; }).pipe(zlib.createGzip({ level: zlib.Z_BEST_COMPRESSION })).on("data", function(data) { results[url].gzip += data.length; }).pipe(createHash("sha1")).on("data", function(data) { results[url].sha1 = data.toString("hex"); done(); }); uglifyjs.stderr.setEncoding("utf8"); uglifyjs.stderr.on("data", function(data) { results[url].log += data; }); uglifyjs.on("exit", function(code) { results[url].code = code; done(); }); }); }); setInterval(function() { process.stderr.write("\0"); }, 5 * 60 * 1000).unref(); UglifyJS2-3.6.3/test/compress.js000066400000000000000000000365131355252637300164740ustar00rootroot00000000000000var assert = require("assert"); var child_process = require("child_process"); var fs = require("fs"); var path = require("path"); var sandbox = require("./sandbox"); var semver = require("semver"); var U = require("./node"); var file = process.argv[2]; var dir = path.resolve(path.dirname(module.filename), "compress"); if (file) { var minify_options = require("./ufuzz.json").map(JSON.stringify); log("--- {file}", { file: file }); var tests = parse_test(path.resolve(dir, file)); process.exit(Object.keys(tests).filter(function(name) { return !test_case(tests[name]); }).length); } else { var files = fs.readdirSync(dir).filter(function(name) { return /\.js$/i.test(name); }); var failures = 0; var failed_files = Object.create(null); (function next() { var file = files.shift(); if (file) { child_process.spawn(process.argv[0], [ process.argv[1], file ], { stdio: [ "ignore", 1, 2 ] }).on("exit", function(code) { if (code) { failures += code; failed_files[file] = code; } next(); }); } else if (failures) { console.error(); console.error("!!! Failed " + failures + " test case(s)."); console.error("!!! " + Object.keys(failed_files).join(", ")); process.exit(1); } })(); } function evaluate(code) { if (code instanceof U.AST_Node) code = make_code(code, { beautify: true }); return new Function("return(" + code + ")")(); } function log() { console.log("%s", tmpl.apply(null, arguments)); } function make_code(ast, options) { var stream = U.OutputStream(options); ast.print(stream); return stream.get(); } function parse_test(file) { var script = fs.readFileSync(file, "utf8"); // TODO try/catch can be removed after fixing https://github.com/mishoo/UglifyJS2/issues/348 try { var ast = U.parse(script, { filename: file }); } catch (e) { console.error("Caught error while parsing tests in " + file); console.error(e); process.exit(1); } var tests = Object.create(null); var tw = new U.TreeWalker(function(node, descend) { if (node instanceof U.AST_LabeledStatement && tw.parent() instanceof U.AST_Toplevel) { var name = node.label.name; if (name in tests) { throw new Error('Duplicated test name "' + name + '" in ' + file); } tests[name] = get_one_test(name, node.body); return true; } if (!(node instanceof U.AST_Toplevel)) croak(node); }); ast.walk(tw); return tests; function croak(node) { throw new Error(tmpl("Can't understand test file {file} [{line},{col}]\n{code}", { file: file, line: node.start.line, col: node.start.col, code: make_code(node, { beautify: false }) })); } function read_string(stat) { if (stat.TYPE == "SimpleStatement") { var body = stat.body; switch(body.TYPE) { case "String": return body.value; case "Array": return body.elements.map(function(element) { if (element.TYPE !== "String") throw new Error("Should be array of strings"); return element.value; }).join("\n"); } } throw new Error("Should be string or array of strings"); } function get_one_test(name, block) { var test = { name: name, options: {} }; var tw = new U.TreeWalker(function(node, descend) { if (node instanceof U.AST_Assign) { if (!(node.left instanceof U.AST_SymbolRef)) { croak(node); } var name = node.left.name; test[name] = evaluate(node.right); return true; } if (node instanceof U.AST_LabeledStatement) { var label = node.label; assert.ok([ "input", "expect", "expect_exact", "expect_warnings", "expect_stdout", "node_version", ].indexOf(label.name) >= 0, tmpl("Unsupported label {name} [{line},{col}]", { name: label.name, line: label.start.line, col: label.start.col })); var stat = node.body; if (label.name == "expect_exact" || label.name == "node_version") { test[label.name] = read_string(stat); } else if (label.name == "expect_stdout") { var body = stat.body; if (body instanceof U.AST_Boolean) { test[label.name] = body.value; } else if (body instanceof U.AST_Call) { var ctor = global[body.expression.name]; assert.ok(ctor === Error || ctor.prototype instanceof Error, tmpl("Unsupported expect_stdout format [{line},{col}]", { line: label.start.line, col: label.start.col })); test[label.name] = ctor.apply(null, body.args.map(function(node) { assert.ok(node instanceof U.AST_Constant, tmpl("Unsupported expect_stdout format [{line},{col}]", { line: label.start.line, col: label.start.col })); return node.value; })); } else { test[label.name] = read_string(stat) + "\n"; } } else { test[label.name] = stat; } return true; } }); block.walk(tw); return test; } } // Try to reminify original input with standard options // to see if it matches expect_stdout. function reminify(orig_options, input_code, input_formatted, stdout) { for (var i = 0; i < minify_options.length; i++) { var options = JSON.parse(minify_options[i]); if (options.compress) [ "keep_fargs", "keep_fnames", ].forEach(function(name) { if (name in orig_options) { options.compress[name] = orig_options[name]; } }); var options_formatted = JSON.stringify(options, null, 4); var result = U.minify(input_code, options); if (result.error) { log([ "!!! failed input reminify", "---INPUT---", "{input}", "---OPTIONS---", "{options}", "--ERROR---", "{error}", "", "", ].join("\n"), { input: input_formatted, options: options_formatted, error: result.error, }); return false; } else { var expected = stdout[options.toplevel ? 1 : 0]; var actual = run_code(result.code, options.toplevel); if (typeof expected != "string" && typeof actual != "string" && expected.name == actual.name) { actual = expected; } if (!sandbox.same_stdout(expected, actual)) { log([ "!!! failed running reminified input", "---INPUT---", "{input}", "---OPTIONS---", "{options}", "---OUTPUT---", "{output}", "---EXPECTED {expected_type}---", "{expected}", "---ACTUAL {actual_type}---", "{actual}", "", "", ].join("\n"), { input: input_formatted, options: options_formatted, output: result.code, expected_type: typeof expected == "string" ? "STDOUT" : "ERROR", expected: expected, actual_type: typeof actual == "string" ? "STDOUT" : "ERROR", actual: actual, }); return false; } } } return true; } function run_code(code, toplevel) { var result = sandbox.run_code(code, toplevel); return typeof result == "string" ? result.replace(/\u001b\[\d+m/g, "") : result; } function test_case(test) { log(" Running test [{name}]", { name: test.name }); var output_options = test.beautify || {}; var expect; if (test.expect) { expect = make_code(to_toplevel(test.expect, test.mangle), output_options); } else { expect = test.expect_exact; } var input = to_toplevel(test.input, test.mangle); var input_code = make_code(input); var input_formatted = make_code(test.input, { beautify: true, comments: "all", keep_quoted_props: true, quote_style: 3, }); try { U.parse(input_code); } catch (ex) { log([ "!!! Cannot parse input", "---INPUT---", "{input}", "--PARSE ERROR--", "{error}", "", "", ].join("\n"), { input: input_formatted, error: ex, }); return false; } var warnings_emitted = []; if (test.expect_warnings) { var expected_warnings = make_code(test.expect_warnings, { beautify: false, quote_style: 2, // force double quote to match JSON }); U.AST_Node.log_function(function(text) { warnings_emitted.push(text); }, /"INFO: /.test(expected_warnings)); } if (test.mangle && test.mangle.properties && test.mangle.properties.keep_quoted) { var quoted_props = test.mangle.properties.reserved; if (!Array.isArray(quoted_props)) quoted_props = []; test.mangle.properties.reserved = quoted_props; U.reserve_quoted_keys(input, quoted_props); } if (test.rename) { input.figure_out_scope(test.mangle); input.expand_names(test.mangle); } var cmp = new U.Compressor(test.options, true); var output = cmp.compress(input); output.figure_out_scope(test.mangle); if (test.mangle) { output.compute_char_frequency(test.mangle); output.mangle_names(test.mangle); if (test.mangle.properties) { output = U.mangle_properties(output, test.mangle.properties); } } output = make_code(output, output_options); if (expect != output) { log([ "!!! failed", "---INPUT---", "{input}", "---OUTPUT---", "{output}", "---EXPECTED---", "{expected}", "", "", ].join("\n"), { input: input_formatted, output: output, expected: expect }); return false; } // expect == output try { U.parse(output); } catch (ex) { log([ "!!! Test matched expected result but cannot parse output", "---INPUT---", "{input}", "---OUTPUT---", "{output}", "--REPARSE ERROR--", "{error}", "", "", ].join("\n"), { input: input_formatted, output: output, error: ex, }); return false; } if (test.expect_warnings) { warnings_emitted = warnings_emitted.map(function(input) { return input.split(process.cwd() + path.sep).join("").split(path.sep).join("/"); }); var actual_warnings = JSON.stringify(warnings_emitted); if (expected_warnings != actual_warnings) { log([ "!!! failed", "---INPUT---", "{input}", "---EXPECTED WARNINGS---", "{expected_warnings}", "---ACTUAL WARNINGS---", "{actual_warnings}", "", "", ].join("\n"), { input: input_formatted, expected_warnings: expected_warnings, actual_warnings: actual_warnings, }); return false; } } if (test.expect_stdout && (!test.node_version || semver.satisfies(process.version, test.node_version))) { var stdout = [ run_code(input_code), run_code(input_code, true) ]; var toplevel = test.options.toplevel; var actual = stdout[toplevel ? 1 : 0]; if (test.expect_stdout === true) { test.expect_stdout = actual; } if (!sandbox.same_stdout(test.expect_stdout, actual)) { log([ "!!! Invalid input or expected stdout", "---INPUT---", "{input}", "---EXPECTED {expected_type}---", "{expected}", "---ACTUAL {actual_type}---", "{actual}", "", "", ].join("\n"), { input: input_formatted, expected_type: typeof test.expect_stdout == "string" ? "STDOUT" : "ERROR", expected: test.expect_stdout, actual_type: typeof actual == "string" ? "STDOUT" : "ERROR", actual: actual, }); return false; } actual = run_code(output, toplevel); if (!sandbox.same_stdout(test.expect_stdout, actual)) { log([ "!!! failed", "---INPUT---", "{input}", "---EXPECTED {expected_type}---", "{expected}", "---ACTUAL {actual_type}---", "{actual}", "", "", ].join("\n"), { input: input_formatted, expected_type: typeof test.expect_stdout == "string" ? "STDOUT" : "ERROR", expected: test.expect_stdout, actual_type: typeof actual == "string" ? "STDOUT" : "ERROR", actual: actual, }); return false; } if (!reminify(test.options, input_code, input_formatted, stdout)) { return false; } } return true; } function tmpl() { return U.string_template.apply(null, arguments); } function to_toplevel(input, mangle_options) { if (!(input instanceof U.AST_BlockStatement)) throw new Error("Unsupported input syntax"); var directive = true; var offset = input.start.line; var tokens = []; var toplevel = new U.AST_Toplevel(input.transform(new U.TreeTransformer(function(node) { if (U.push_uniq(tokens, node.start)) node.start.line -= offset; if (!directive || node === input) return; if (node instanceof U.AST_SimpleStatement && node.body instanceof U.AST_String) { return new U.AST_Directive(node.body); } else { directive = false; } }))); toplevel.figure_out_scope(mangle_options); return toplevel; } UglifyJS2-3.6.3/test/compress/000077500000000000000000000000001355252637300161265ustar00rootroot00000000000000UglifyJS2-3.6.3/test/compress/arguments.js000066400000000000000000000414151355252637300204760ustar00rootroot00000000000000replace_index: { options = { arguments: true, evaluate: true, properties: true, } input: { var arguments = []; console.log(arguments[0]); (function() { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); (function(a, b) { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); (function(arguments) { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); (function() { var arguments; console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); } expect: { var arguments = []; console.log(arguments[0]); (function() { console.log(arguments[1], arguments[1], arguments.foo); })("bar", 42); (function(a, b) { console.log(b, b, arguments.foo); })("bar", 42); (function(arguments) { console.log(arguments[1], arguments[1], arguments.foo); })("bar", 42); (function() { var arguments; console.log(arguments[1], arguments[1], arguments.foo); })("bar", 42); } expect_stdout: [ "undefined", "42 42 undefined", "42 42 undefined", "a a undefined", "42 42 undefined", ] } replace_index_strict: { options = { arguments: true, evaluate: true, properties: true, reduce_vars: true, } input: { "use strict"; (function() { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); (function(a, b) { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); } expect: { "use strict"; (function() { console.log(arguments[1], arguments[1], arguments.foo); })("bar", 42); (function(a, b) { console.log(b, b, arguments.foo); })("bar", 42); } expect_stdout: [ "42 42 undefined", "42 42 undefined", ] } replace_index_keep_fargs: { options = { arguments: true, evaluate: true, keep_fargs: false, properties: true, } input: { var arguments = []; console.log(arguments[0]); (function() { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); (function(a, b) { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); (function(arguments) { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); (function() { var arguments; console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); } expect: { var arguments = []; console.log(arguments[0]); (function(argument_0, argument_1) { console.log(argument_1, argument_1, arguments.foo); })("bar", 42); (function(a, b) { console.log(b, b, arguments.foo); })("bar", 42); (function(arguments) { console.log(arguments[1], arguments[1], arguments.foo); })("bar", 42); (function() { var arguments; console.log(arguments[1], arguments[1], arguments.foo); })("bar", 42); } expect_stdout: [ "undefined", "42 42 undefined", "42 42 undefined", "a a undefined", "42 42 undefined", ] } replace_index_keep_fargs_strict: { options = { arguments: true, evaluate: true, keep_fargs: false, properties: true, reduce_vars: true, } input: { "use strict"; (function() { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); (function(a, b) { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); } expect: { "use strict"; (function(argument_0, argument_1) { console.log(argument_1, argument_1, arguments.foo); })("bar", 42); (function(a, b) { console.log(b, b, arguments.foo); })("bar", 42); } expect_stdout: [ "42 42 undefined", "42 42 undefined", ] } modified: { options = { arguments: true, } input: { (function(a, b) { var c = arguments[0]; var d = arguments[1]; var a = "foo"; b++; arguments[0] = "moo"; arguments[1] *= 2; console.log(a, b, c, d, arguments[0], arguments[1]); })("bar", 42); } expect: { (function(a, b) { var c = a; var d = b; var a = "foo"; b++; a = "moo"; b *= 2; console.log(a, b, c, d, a, b); })("bar", 42); } expect_stdout: "moo 86 bar 42 moo 86" } modified_strict: { options = { arguments: true, reduce_vars: true, } input: { "use strict"; (function(a, b) { var c = arguments[0]; var d = arguments[1]; var a = "foo"; b++; arguments[0] = "moo"; arguments[1] *= 2; console.log(a, b, c, d, arguments[0], arguments[1]); })("bar", 42); } expect: { "use strict"; (function(a, b) { var c = arguments[0]; var d = arguments[1]; var a = "foo"; b++; arguments[0] = "moo"; arguments[1] *= 2; console.log(a, b, c, d, arguments[0], arguments[1]); })("bar", 42); } expect_stdout: "foo 43 bar 42 moo 84" } duplicate_argname: { options = { arguments: true, } input: { (function(a, b, a) { console.log(a, b, arguments[0], arguments[1], arguments[2]); })("foo", 42, "bar"); } expect: { (function(a, b, a) { console.log(a, b, arguments[0], b, a); })("foo", 42, "bar"); } expect_stdout: "bar 42 foo 42 bar" } issue_3273: { options = { arguments: true, } input: { function f(a) { console.log(arguments[0], a); arguments[0]++; console.log(arguments[0], a); } f(0); } expect: { function f(a) { console.log(a, a); a++; console.log(a, a); } f(0); } expect_stdout: [ "0 0", "1 1", ] } issue_3273_reduce_vars: { options = { arguments: true, reduce_vars: true, } input: { function f(a) { console.log(arguments[0], a); arguments[0]++; console.log(arguments[0], a); } f(0); } expect: { function f(a) { console.log(a, a); a++; console.log(a, a); } f(0); } expect_stdout: [ "0 0", "1 1", ] } issue_3273_local_strict: { options = { arguments: true, } input: { function f(a) { "use strict"; console.log(arguments[0], a); arguments[0]++; console.log(arguments[0], a); } f(0); } expect: { function f(a) { "use strict"; console.log(arguments[0], a); arguments[0]++; console.log(arguments[0], a); } f(0); } expect_stdout: [ "0 0", "1 0", ] } issue_3273_local_strict_reduce_vars: { options = { arguments: true, reduce_vars: true, } input: { function f(a) { "use strict"; console.log(arguments[0], a); arguments[0]++; console.log(arguments[0], a); } f(0); } expect: { function f(a) { "use strict"; console.log(arguments[0], a); arguments[0]++; console.log(arguments[0], a); } f(0); } expect_stdout: [ "0 0", "1 0", ] } issue_3273_global_strict: { options = { arguments: true, } input: { "use strict"; function f(a) { console.log(arguments[0], a); arguments[0]++; console.log(arguments[0], a); } f(0); } expect: { "use strict"; function f(a) { console.log(arguments[0], a); arguments[0]++; console.log(arguments[0], a); } f(0); } expect_stdout: [ "0 0", "1 0", ] } issue_3273_global_strict_reduce_vars: { options = { arguments: true, reduce_vars: true, } input: { "use strict"; function f(a) { console.log(arguments[0], a); arguments[0]++; console.log(arguments[0], a); } f(0); } expect: { "use strict"; function f(a) { console.log(arguments[0], a); arguments[0]++; console.log(arguments[0], a); } f(0); } expect_stdout: [ "0 0", "1 0", ] } issue_3273_keep_fargs_false: { options = { arguments: true, keep_fargs: false, reduce_vars: true, } input: { (function() { "use strict"; arguments[0]++; console.log(arguments[0]); })(0); } expect: { (function(argument_0) { "use strict"; argument_0++; console.log(argument_0); })(0); } expect_stdout: "1" } issue_3273_keep_fargs_strict: { options = { arguments: true, keep_fargs: "strict", reduce_vars: true, } input: { (function() { "use strict"; arguments[0]++; console.log(arguments[0]); })(0); } expect: { (function(argument_0) { "use strict"; argument_0++; console.log(argument_0); })(0); } expect_stdout: "1" } issue_3282_1: { options = { arguments: true, reduce_funcs: true, reduce_vars: true, keep_fargs: false, unused: true, } input: { (function(t) { return function() { t(); }; })(function() { 'use strict'; function e() { return arguments[0]; } e(); e(); })(); } expect: { (function() { return function() { (function() { "use strict"; function e() { return arguments[0]; } e(); e(); })(); }; })()(); } expect_stdout: true } issue_3282_1_passes: { options = { arguments: true, passes: 2, reduce_funcs: true, reduce_vars: true, keep_fargs: false, unused: true, } input: { (function(t) { return function() { t(); }; })(function() { 'use strict'; function e() { return arguments[0]; } e(); e(); })(); } expect: { (function() { return function() { (function() { "use strict"; function e(argument_0) { return argument_0; } e(); e(); })(); }; })()(); } expect_stdout: true } issue_3282_2: { options = { arguments: true, reduce_vars: true, keep_fargs: false, unused: true, } input: { (function(f) { f(); })(function() { return (function(t) { return function() { t(); }; })(function() { 'use strict'; function e() { return arguments[0]; } e(); e(); })(); }); } expect: { (function() { (function() { return function(t) { return function() { t(); }; }(function() { "use strict"; function e() { return arguments[0]; } e(); e(); })(); })(); })(); } expect_stdout: true } issue_3282_2_passes: { options = { arguments: true, passes: 2, reduce_vars: true, keep_fargs: false, unused: true, } input: { (function(f) { f(); })(function() { return (function(t) { return function() { t(); }; })(function() { 'use strict'; function e() { return arguments[0]; } e(); e(); })(); }); } expect: { (function() { (function() { return function(t) { return function() { t(); }; }(function() { "use strict"; function e(argument_0) { return argument_0; } e(); e(); })(); })(); })(); } expect_stdout: true } issue_3420_1: { options = { arguments: true, keep_fargs: "strict", } input: { console.log(function() { return function() { return arguments[0]; }; }().length); } expect: { console.log(function() { return function() { return arguments[0]; }; }().length); } expect_stdout: "0" } issue_3420_2: { options = { arguments: true, keep_fargs: "strict", } input: { var foo = function() { delete arguments[0]; }; foo(); } expect: { var foo = function() { delete arguments[0]; }; foo(); } expect_stdout: true } issue_3420_3: { options = { arguments: true, keep_fargs: "strict", } input: { "use strict"; var foo = function() { delete arguments[0]; }; foo(); } expect: { "use strict"; var foo = function() { delete arguments[0]; }; foo(); } expect_stdout: true } issue_3420_4: { options = { arguments: true, keep_fargs: "strict", } input: { !function() { console.log(arguments[0]); delete arguments[0]; console.log(arguments[0]); }(42); } expect: { !function(argument_0) { console.log(argument_0); delete arguments[0]; console.log(arguments[0]); }(42); } expect_stdout: [ "42", "undefined", ] } issue_3420_5: { options = { arguments: true, keep_fargs: "strict", } input: { "use strict"; !function() { console.log(arguments[0]); delete arguments[0]; console.log(arguments[0]); }(42); } expect: { "use strict"; !function(argument_0) { console.log(argument_0); delete arguments[0]; console.log(arguments[0]); }(42); } expect_stdout: [ "42", "undefined", ] } issue_3420_6: { options = { arguments: true, keep_fargs: "strict", } input: { console.log(function() { return delete arguments[0]; }()); } expect: { console.log(function() { return delete arguments[0]; }()); } expect_stdout: "true" } issue_3420_7: { options = { arguments: true, keep_fargs: "strict", } input: { "use strict"; console.log(function() { return delete arguments[0]; }()); } expect: { "use strict"; console.log(function() { return delete arguments[0]; }()); } expect_stdout: "true" } UglifyJS2-3.6.3/test/compress/arrays.js000066400000000000000000000225201355252637300177660ustar00rootroot00000000000000holes_and_undefined: { input: { w = [1,,]; x = [1, 2, undefined]; y = [1, , 2, ]; z = [1, undefined, 3]; } expect: { w=[1,,]; x=[1,2,void 0]; y=[1,,2]; z=[1,void 0,3]; } } constant_join: { options = { evaluate: true, unsafe: true, } input: { var a = [ "foo", "bar", "baz" ].join(""); var a1 = [ "foo", "bar", "baz" ].join(); var a2 = [ "foo", "bar", "baz" ].join(null); var a3 = [ "foo", "bar", "baz" ].join(void 0); var a4 = [ "foo", , "baz" ].join(); var a5 = [ "foo", null, "baz" ].join(); var a6 = [ "foo", void 0, "baz" ].join(); var b = [ "foo", 1, 2, 3, "bar" ].join(""); var c = [ boo(), "foo", 1, 2, 3, "bar", bar() ].join(""); var c1 = [ boo(), bar(), "foo", 1, 2, 3, "bar", bar() ].join(""); var c2 = [ 1, 2, "foo", "bar", baz() ].join(""); var c3 = [ boo() + bar() + "foo", 1, 2, 3, "bar", bar() + "foo" ].join(""); var c4 = [ 1, 2, null, undefined, "foo", "bar", baz() ].join(""); var c5 = [ boo() + bar() + "foo", 1, 2, 3, "bar", bar() + "foo" ].join(); var c6 = [ 1, 2, null, undefined, "foo", "bar", baz() ].join(); var d = [ "foo", 1 + 2 + "bar", "baz" ].join("-"); var e = [].join(foo + bar); var f = [].join(""); var g = [].join("foo"); } expect: { var a = "foobarbaz"; var a1 = "foo,bar,baz"; var a2 = "foonullbarnullbaz"; var a3 = "foo,bar,baz"; var a4 = "foo,,baz"; var a5 = "foo,,baz"; var a6 = "foo,,baz"; var b = "foo123bar"; var c = boo() + "foo123bar" + bar(); var c1 = "" + boo() + bar() + "foo123bar" + bar(); var c2 = "12foobar" + baz(); var c3 = boo() + bar() + "foo123bar" + bar() + "foo"; var c4 = "12foobar" + baz(); var c5 = [ boo() + bar() + "foo", 1, 2, 3, "bar", bar() + "foo" ].join(); var c6 = [ "1,2,,,foo,bar", baz() ].join(); var d = "foo-3bar-baz"; var e = [].join(foo + bar); var f = ""; var g = ""; } } constant_join_2: { options = { evaluate: true, unsafe: true, } input: { var a = [ "foo", "bar", boo(), "baz", "x", "y" ].join(""); var b = [ "foo", "bar", boo(), "baz", "x", "y" ].join("-"); var c = [ "foo", "bar", boo(), "baz", "x", "y" ].join("really-long-separator"); var d = [ "foo", "bar", boo(), [ "foo", 1, 2, 3, "bar" ].join("+"), "baz", "x", "y" ].join("-"); var e = [ "foo", "bar", boo(), [ "foo", 1, 2, 3, "bar" ].join("+"), "baz", "x", "y" ].join("really-long-separator"); var f = [ "str", "str" + variable, "foo", "bar", "moo" + foo ].join(""); } expect: { var a = "foobar" + boo() + "bazxy"; var b = [ "foo-bar", boo(), "baz-x-y" ].join("-"); var c = [ "foo", "bar", boo(), "baz", "x", "y" ].join("really-long-separator"); var d = [ "foo-bar", boo(), "foo+1+2+3+bar-baz-x-y" ].join("-"); var e = [ "foo", "bar", boo(), "foo+1+2+3+bar", "baz", "x", "y" ].join("really-long-separator"); var f = "strstr" + variable + "foobarmoo" + foo; } } constant_join_3: { options = { evaluate: true, unsafe: true, } input: { var a = [ null ].join(); var b = [ , ].join(); var c = [ , 1, , 3 ].join(); var d = [ foo ].join(); var e = [ foo, null, undefined, bar ].join("-"); var f = [ foo, bar ].join(""); var g = [ null, "foo", null, bar + "baz" ].join(""); var h = [ null, "foo", null, bar + "baz" ].join("-"); var i = [ "foo" + bar, null, baz + "moo" ].join(""); var j = [ foo + "bar", baz ].join(""); var k = [ foo, "bar" + baz ].join(""); var l = [ foo, bar + "baz" ].join(""); } expect: { var a = ""; var b = ""; var c = ",1,,3"; var d = "" + foo; var e = [ foo, "-", bar ].join("-"); var f = "" + foo + bar; var g = "foo" + bar + "baz"; var h = [ "-foo-", bar + "baz" ].join("-"); var i = "foo" + bar + baz + "moo"; var j = foo + "bar" + baz; var k = foo + "bar" + baz; var l = foo + (bar + "baz"); } } for_loop: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unsafe: true, unused: true, } input: { function f0() { var a = [1, 2, 3]; var b = 0; for (var i = 0; i < a.length; i++) b += a[i]; return b; } function f1() { var a = [1, 2, 3]; 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]++; 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_funcs: 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_funcs: 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_funcs: 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" } constructor_bad: { options = { unsafe: true } input: { try { Array(NaN); console.log("FAIL1"); } catch (ex) { try { new Array(NaN); console.log("FAIL2"); } catch (ex) { console.log("PASS"); } } try { Array(3.14); console.log("FAIL1"); } catch (ex) { try { new Array(3.14); console.log("FAIL2"); } catch (ex) { console.log("PASS"); } } } expect: { try { Array(NaN); console.log("FAIL1"); } catch (ex) { try { Array(NaN); console.log("FAIL2"); } catch (ex) { console.log("PASS"); } } try { Array(3.14); console.log("FAIL1"); } catch (ex) { try { Array(3.14); console.log("FAIL2"); } catch (ex) { console.log("PASS"); } } } expect_stdout: [ "PASS", "PASS", ] expect_warnings: [ "WARN: Invalid array length: 3.14 [test/compress/arrays.js:13,12]", "WARN: Invalid array length: 3.14 [test/compress/arrays.js:17,16]", ] } constructor_good: { options = { unsafe: true } input: { console.log(Array()); console.log(Array(0)); console.log(Array(1)); console.log(Array(6)); console.log(Array(7)); console.log(Array(1, 2)); console.log(Array(false)); console.log(Array("foo")); console.log(Array(Array)); console.log(new Array()); console.log(new Array(0)); console.log(new Array(1)); console.log(new Array(6)); console.log(new Array(7)); console.log(new Array(1, 2)); console.log(new Array(false)); console.log(new Array("foo")); console.log(new Array(Array)); } expect: { console.log([]); console.log([]); console.log([,]); console.log([,,,,,,]); console.log(Array(7)); console.log([ 1, 2 ]); console.log([ false ]); console.log([ "foo" ]); console.log(Array(Array)); console.log([]); console.log([]); console.log([,]); console.log([,,,,,,]); console.log(Array(7)); console.log([ 1, 2 ]); console.log([ false ]); console.log([ "foo" ]); console.log(Array(Array)); } expect_stdout: true expect_warnings: [] } UglifyJS2-3.6.3/test/compress/ascii.js000066400000000000000000000030211355252637300175500ustar00rootroot00000000000000ascii_only_true: { options = {} beautify = { ascii_only : true, ie8 : false, beautify : false, } input: { function f() { return "\x000\x001\x007\x008\x00" + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" + "\x20\x21\x22\x23 ... \x7d\x7e\x7f\x80\x81 ... \xfe\xff\u0fff\uffff"; } } expect_exact: 'function f(){return"\\x000\\x001\\x007\\x008\\0"+"\\0\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\b\\t\\n\\v\\f\\r\\x0e\\x0f"+"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f"+\' !"# ... }~\\x7f\\x80\\x81 ... \\xfe\\xff\\u0fff\\uffff\'}' } ascii_only_false: { options = {} beautify = { ascii_only : false, ie8 : false, beautify : false, } input: { function f() { return "\x000\x001\x007\x008\x00" + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" + "\x20\x21\x22\x23 ... \x7d\x7e\x7f\x80\x81 ... \xfe\xff\u0fff\uffff"; } } expect_exact: 'function f(){return"\\x000\\x001\\x007\\x008\\0"+"\\0\x01\x02\x03\x04\x05\x06\x07\\b\\t\\n\\v\\f\\r\x0e\x0f"+"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"+\' !"# ... }~\x7f\x80\x81 ... \xfe\xff\u0fff\uffff\'}' } UglifyJS2-3.6.3/test/compress/asm.js000066400000000000000000000116751355252637300172560ustar00rootroot00000000000000asm_mixed: { options = { assignments: true, booleans: true, comparisons: true, conditionals: true, dead_code: true, drop_debugger: true, evaluate: true, hoist_funs: true, hoist_vars: true, if_return: true, join_vars: true, keep_fargs: true, keep_fnames: false, loops: true, negate_iife: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { // adapted from http://asmjs.org/spec/latest/ function asm_GeometricMean(stdlib, foreign, buffer) { "use asm"; var exp = stdlib.Math.exp; var log = stdlib.Math.log; var values = new stdlib.Float64Array(buffer); function logSum(start, end) { start = start|0; end = end|0; var sum = 0.0, p = 0, q = 0; // asm.js forces byte addressing of the heap by requiring shifting by 3 for (p = start << 3, q = end << 3; (p|0) < (q|0); p = (p + 8)|0) { sum = sum + +log(values[p>>3]); } return +sum; } function geometricMean(start, end) { start = start|0; end = end|0; return +exp(+logSum(start, end) / +((end - start)|0)); } return { geometricMean: geometricMean }; } function no_asm_GeometricMean(stdlib, foreign, buffer) { var exp = stdlib.Math.exp; var log = stdlib.Math.log; var values = new stdlib.Float64Array(buffer); function logSum(start, end) { start = start|0; end = end|0; var sum = 0.0, p = 0, q = 0; // asm.js forces byte addressing of the heap by requiring shifting by 3 for (p = start << 3, q = end << 3; (p|0) < (q|0); p = (p + 8)|0) { sum = sum + +log(values[p>>3]); } return +sum; } function geometricMean(start, end) { start = start|0; end = end|0; return +exp(+logSum(start, end) / +((end - start)|0)); } return { geometricMean: geometricMean }; } } expect: { function asm_GeometricMean(stdlib, foreign, buffer) { "use asm"; var exp = stdlib.Math.exp; var log = stdlib.Math.log; var values = new stdlib.Float64Array(buffer); function logSum(start, end) { start = start | 0; end = end | 0; var sum = 0.0, p = 0, q = 0; for (p = start << 3, q = end << 3; (p | 0) < (q | 0); p = p + 8 | 0) { sum = sum + +log(values[p >> 3]); } return +sum; } function geometricMean(start, end) { start = start | 0; end = end | 0; return +exp(+logSum(start, end) / +(end - start | 0)); } return { geometricMean: geometricMean }; } function no_asm_GeometricMean(stdlib, foreign, buffer) { function logSum(start, end) { start |= 0, end |= 0; var sum = 0, p = 0, q = 0; for (p = start << 3, q = end << 3; (0 | p) < (0 | q); p = p + 8 | 0) sum += +log(values[p >> 3]); return +sum; } function geometricMean(start, end) { return start |= 0, end |= 0, +exp(+logSum(start, end) / +(end - start | 0)); } var exp = stdlib.Math.exp, log = stdlib.Math.log, values = new stdlib.Float64Array(buffer); return { geometricMean: geometricMean }; } } } 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;' } UglifyJS2-3.6.3/test/compress/assignment.js000066400000000000000000000152511355252637300206400ustar00rootroot00000000000000op_equals_left_local_var: { options = { assignments: true, evaluate: true, } input: { var x; x = x + 3; x = x - 3; x = x / 3; x = x * 3; x = x >> 3; x = x << 3; x = x >>> 3; x = x | 3; x = x ^ 3; x = x % 3; x = x & 3; x = x + g(); x = x - g(); x = x / g(); x = x * g(); x = x >> g(); x = x << g(); x = x >>> g(); x = x | g(); x = x ^ g(); x = x % g(); x = x & g(); } expect: { var x; x += 3; x -= 3; x /= 3; x *= 3; x >>= 3; x <<= 3; x >>>= 3; x |= 3; x ^= 3; x %= 3; x &= 3; x += g(); x -= g(); x /= g(); x *= g(); x >>= g(); x <<= g(); x >>>= g(); x |= g(); x ^= g(); x %= g(); x &= g(); } } op_equals_right_local_var: { options = { assignments: true, evaluate: true, } input: { var x; x = (x -= 2) ^ x; x = 3 + x; x = 3 - x; x = 3 / x; x = 3 * x; x = 3 >> x; x = 3 << x; x = 3 >>> x; x = 3 | x; x = 3 ^ x; x = 3 % x; x = 3 & x; x = g() + x; x = g() - x; x = g() / x; x = g() * x; x = g() >> x; x = g() << x; x = g() >>> x; x = g() | x; x = g() ^ x; x = g() % x; x = g() & x; } expect: { var x; x = (x -= 2) ^ x; x = 3 + x; x = 3 - x; x = 3 / x; x *= 3; x = 3 >> x; x = 3 << x; x = 3 >>> x; x |= 3; x ^= 3; x = 3 % x; x &= 3; x = g() + x; x = g() - x; x = g() / x; x = g() * x; x = g() >> x; x = g() << x; x = g() >>> x; x = g() | x; x = g() ^ x; x = g() % x; x = g() & x; } } op_equals_left_global_var: { options = { assignments: true, evaluate: true, } input: { x = x + 3; x = x - 3; x = x / 3; x = x * 3; x = x >> 3; x = x << 3; x = x >>> 3; x = x | 3; x = x ^ 3; x = x % 3; x = x & 3; x = x + g(); x = x - g(); x = x / g(); x = x * g(); x = x >> g(); x = x << g(); x = x >>> g(); x = x | g(); x = x ^ g(); x = x % g(); x = x & g(); } expect: { x += 3; x -= 3; x /= 3; x *= 3; x >>= 3; x <<= 3; x >>>= 3; x |= 3; x ^= 3; x %= 3; x &= 3; x += g(); x -= g(); x /= g(); x *= g(); x >>= g(); x <<= g(); x >>>= g(); x |= g(); x ^= g(); x %= g(); x &= g(); } } op_equals_right_global_var: { options = { assignments: true, evaluate: true, } input: { x = (x -= 2) ^ x; x = 3 + x; x = 3 - x; x = 3 / x; x = 3 * x; x = 3 >> x; x = 3 << x; x = 3 >>> x; x = 3 | x; x = 3 ^ x; x = 3 % x; x = 3 & x; x = g() + x; x = g() - x; x = g() / x; x = g() * x; x = g() >> x; x = g() << x; x = g() >>> x; x = g() | x; x = g() ^ x; x = g() % x; x = g() & x; } expect: { x = (x -= 2) ^ x; x = 3 + x; x = 3 - x; x = 3 / x; x *= 3; x = 3 >> x; x = 3 << x; x = 3 >>> x; x |= 3; x ^= 3; x = 3 % x; x &= 3; x = g() + x; x = g() - x; x = g() / x; x = g() * x; x = g() >> x; x = g() << x; x = g() >>> x; x = g() | x; x = g() ^ x; x = g() % x; x = g() & x; } } increment_decrement_1: { options = { assignments: true, reduce_vars: true, } input: { console.log(function(a) { a += 1; a -= 1; return a; }(42)); } expect: { console.log(function(a){ ++a; --a; return a; }(42)); } expect_stdout: "42" } increment_decrement_2: { options = { assignments: true, passes: 2, reduce_vars: true, } input: { console.log(function(a) { a = a + 1; a = a - 1; a += 1; a -= 1; return a; }(42)); } expect: { console.log(function(a){ ++a; --a; ++a; --a; return a; }(42)); } expect_stdout: "42" } issue_3375: { options = { assignments: true, reduce_vars: true, } input: { console.log(typeof function(b) { var a = b += 1; --b; return a; }("object")); } expect: { console.log(typeof function(b) { var a = b += 1; --b; return a; }("object")); } expect_stdout: "string" } issue_3427: { options = { assignments: true, sequences: true, side_effects: true, unused: true, } input: { (function() { var a; a || (a = {}); })(); } expect: {} } issue_3429_1: { options = { assignments: true, side_effects: true, unused: true, } input: { var a = "PASS"; (function(b) { b && (b = a = "FAIL"); })(); console.log(a); } expect: { var a = "PASS"; (function(b) { b = b && (a = "FAIL"); })(); console.log(a); } expect_stdout: "PASS" } issue_3429_2: { options = { assignments: true, side_effects: true, unused: true, } input: { var a; (function(b) { b || (b = a = "FAIL"); })(42); console.log(a); } expect: { var a; (function(b) { b = b || (a = "FAIL"); })(42); console.log(a); } expect_stdout: "undefined" } UglifyJS2-3.6.3/test/compress/blocks.js000066400000000000000000000014021355252637300177360ustar00rootroot00000000000000remove_blocks: { input: { {;} foo(); {}; { {}; }; bar(); {} } expect: { foo(); bar(); } } keep_some_blocks: { input: { // 1. if (foo) { {{{}}} if (bar) { baz(); } {{}} } else { stuff(); } // 2. if (foo) { for (var i = 0; i < 5; ++i) if (bar) baz(); } else { stuff(); } } expect: { // 1. if (foo) { if (bar) baz(); } else stuff(); // 2. if (foo) { for (var i = 0; i < 5; ++i) if (bar) baz(); } else stuff(); } } UglifyJS2-3.6.3/test/compress/booleans.js000066400000000000000000000035331355252637300202720ustar00rootroot00000000000000iife_boolean_context: { options = { booleans: true, evaluate: true, } input: { console.log(function() { return Object(1) || false; }() ? "PASS" : "FAIL"); console.log(function() { return [].length || true; }() ? "PASS" : "FAIL"); } expect: { console.log(function() { return Object(1); }() ? "PASS" : "FAIL"); console.log(function() { return [].length, 1; }() ? "PASS" : "FAIL"); } expect_stdout: [ "PASS", "PASS", ] expect_warnings: [ "WARN: Dropping side-effect-free || [test/compress/booleans.js:2,19]", "WARN: Boolean || always true [test/compress/booleans.js:5,19]", ] } issue_3465_1: { options = { booleans: true, } input: { console.log(function(a) { return typeof a; }() ? "PASS" : "FAIL"); } expect: { console.log(function(a) { return 1; }() ? "PASS" : "FAIL"); } expect_stdout: "PASS" } issue_3465_2: { options = { booleans: true, } input: { console.log(function f(a) { if (!a) console.log(f(42)); return typeof a; }() ? "PASS" : "FAIL"); } expect: { console.log(function f(a) { if (!a) console.log(f(42)); return typeof a; }() ? "PASS" : "FAIL"); } expect_stdout: [ "number", "PASS", ] } issue_3465_3: { options = { booleans: true, passes: 2, unused: true, } input: { console.log(function f(a) { return typeof a; }() ? "PASS" : "FAIL"); } expect: { console.log(function(a) { return 1; }() ? "PASS" : "FAIL"); } expect_stdout: "PASS" } UglifyJS2-3.6.3/test/compress/collapse_vars.js000066400000000000000000004175111355252637300213320ustar00rootroot00000000000000collapse_vars_side_effects_1: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { function f1() { var e = 7; var s = "abcdef"; var i = 2; var log = console.log.bind(console); var x = s.charAt(i++); var y = s.charAt(i++); var z = s.charAt(i++); log(x, y, z, e); } function f2() { var e = 7; var log = console.log.bind(console); var s = "abcdef"; var i = 2; var x = s.charAt(i++); var y = s.charAt(i++); var z = s.charAt(i++); log(x, i, y, z, e); } function f3() { var e = 7; var s = "abcdef"; var i = 2; var log = console.log.bind(console); var x = s.charAt(i++); var y = s.charAt(i++); var z = s.charAt(i++); log(x, z, y, e); } function f4() { var log = console.log.bind(console), i = 10, x = i += 2, y = i += 3, z = i += 4; log(x, z, y, i); } f1(), f2(), f3(), f4(); } expect: { function f1() { var s = "abcdef", i = 2; console.log.bind(console)(s.charAt(i++), s.charAt(i++), s.charAt(i++), 7); } function f2() { var s = "abcdef", i = 2; console.log.bind(console)(s.charAt(i++), 5, s.charAt(i++), s.charAt(i++), 7); } function f3() { var s = "abcdef", i = 2, log = console.log.bind(console), x = s.charAt(i++), y = s.charAt(i++); log(x, s.charAt(i++), y, 7); } function f4() { var i = 10, x = i += 2, y = i += 3; console.log.bind(console)(x, i += 4, y, 19); } f1(), f2(), f3(), f4(); } expect_stdout: true } collapse_vars_side_effects_2: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { function fn(x) { return console.log(x), x; } function p1() { var a = foo(), b = bar(), c = baz(); return a + b + c; } function p2() { var a = foo(), c = bar(), b = baz(); return a + b + c; } function p3() { var b = foo(), a = bar(), c = baz(); return a + b + c; } function p4() { var b = foo(), c = bar(), a = baz(); return a + b + c; } function p5() { var c = foo(), a = bar(), b = baz(); return a + b + c; } function p6() { var c = foo(), b = bar(), a = baz(); return a + b + c; } function q1() { var a = foo(), b = bar(), c = baz(); return fn(a + b + c); } function q2() { var a = foo(), c = bar(), b = baz(); return fn(a + b + c); } function q3() { var b = foo(), a = bar(), c = baz(); return fn(a + b + c); } function q4() { var b = foo(), c = bar(), a = baz(); return fn(a + b + c); } function q5() { var c = foo(), a = bar(), b = baz(); return fn(a + b + c); } function q6() { var c = foo(), b = bar(), a = baz(); return fn(a + b + c); } function r1() { var a = foo(), b = bar(), c = baz(); return fn(a) + fn(b) + fn(c); } function r2() { var a = foo(), c = bar(), b = baz(); return fn(a) + fn(b) + fn(c); } function r3() { var b = foo(), a = bar(), c = baz(); return fn(a) + fn(b) + fn(c); } function r4() { var b = foo(), c = bar(), a = baz(); return fn(a) + fn(b) + fn(c); } function r5() { var c = foo(), a = bar(), b = baz(); return fn(a) + fn(b) + fn(c); } function r6() { var c = foo(), b = bar(), a = baz(); return fn(a) + fn(b) + fn(c); } function s1() { var a = foo(), b = bar(), c = baz(); return g(a + b + c); } function s6() { var c = foo(), b = bar(), a = baz(); return g(a + b + c); } function t1() { var a = foo(), b = bar(), c = baz(); return g(a) + g(b) + g(c); } function t6() { var c = foo(), b = bar(), a = baz(); return g(a) + g(b) + g(c); } } expect: { function fn(x) { return console.log(x), x; } function p1() { return foo() + bar() + baz(); } function p2() { var a = foo(), c = bar(); return a + baz() + c; } function p3() { var b = foo(); return bar() + b + baz(); } function p4() { var b = foo(), c = bar(); return baz() + b + c; } function p5() { var c = foo(); return bar() + baz() + c; } function p6() { var c = foo(), b = bar(); return baz() + b + c; } function q1() { return fn(foo() + bar() + baz()); } function q2() { var a = foo(), c = bar(); return fn(a + baz() + c); } function q3() { var b = foo(); return fn(bar() + b + baz()); } function q4() { var b = foo(), c = bar(); return fn(baz() + b + c); } function q5() { var c = foo(); return fn(bar() + baz() + c); } function q6() { var c = foo(), b = bar(); return fn(baz() + b + c); } function r1() { var a = foo(), b = bar(), c = baz(); return fn(a) + fn(b) + fn(c); } function r2() { var a = foo(), c = bar(), b = baz(); return fn(a) + fn(b) + fn(c); } function r3() { var b = foo(), a = bar(), c = baz(); return fn(a) + fn(b) + fn(c); } function r4() { var b = foo(), c = bar(); return fn(baz()) + fn(b) + fn(c); } function r5() { var c = foo(), a = bar(), b = baz(); return fn(a) + fn(b) + fn(c); } function r6() { var c = foo(), b = bar(); return fn(baz()) + fn(b) + fn(c); } function s1() { var a = foo(), b = bar(), c = baz(); return g(a + b + c); } function s6() { var c = foo(), b = bar(), a = baz(); return g(a + b + c); } function t1() { var a = foo(), b = bar(), c = baz(); return g(a) + g(b) + g(c); } function t6() { var c = foo(), b = bar(), a = baz(); return g(a) + g(b) + g(c); } } } collapse_vars_issue_721: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, passes: 2, properties: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { define(["require", "exports", 'handlebars'], function(require, exports, hb) { var win = window; var _hb = win.Handlebars = hb; return _hb; }); def(function(hb) { var win = window; var prop = 'Handlebars'; var _hb = win[prop] = hb; return _hb; }); def(function(hb) { var prop = 'Handlebars'; var win = window; var _hb = win[prop] = hb; return _hb; }); def(function(hb) { var prop = 'Handlebars'; var win = g(); var _hb = win[prop] = hb; return _hb; }); def(function(hb) { var prop = g1(); var win = g2(); var _hb = win[prop] = hb; return _hb; }); def(function(hb) { var win = g2(); var prop = g1(); var _hb = win[prop] = hb; return _hb; }); } expect: { define([ "require", "exports", "handlebars" ], function(require, exports, hb) { return window.Handlebars = hb; }), def(function(hb) { return window.Handlebars = hb; }), def(function(hb) { return window.Handlebars = hb; }), def(function(hb) { return g().Handlebars = hb; }), def(function(hb) { var prop = g1(); return g2()[prop] = hb; }), def(function(hb) { return g2()[g1()] = hb; }); } } collapse_vars_properties: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { function f1(obj) { var prop = 'LiteralProperty'; return !!-+obj[prop]; } function f2(obj) { var prop1 = 'One'; var prop2 = 'Two'; return ~!!-+obj[prop1 + prop2]; } } expect: { function f1(obj) { return !!-+obj.LiteralProperty; } function f2(obj) { return ~!!-+obj.OneTwo; } } } collapse_vars_if: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { function f1() { var not_used = sideeffect(), x = g1 + g2; var y = x / 4, z = 'Bar' + y; if ('x' != z) { return g9; } else return g5; } function f2() { var x = g1 + g2, not_used = sideeffect(); var y = x / 4 var z = 'Bar' + y; if ('x' != z) { return g9; } else return g5; } function f3(x) { if (x) { var a = 1; return a; } else { var b = 2; return b; } } } expect: { function f1() { sideeffect(); return "x" != "Bar" + (g1 + g2) / 4 ? g9 : g5; } function f2() { var x = g1 + g2; sideeffect(); return "x" != "Bar" + x / 4 ? g9 : g5; } function f3(x) { if (x) { return 1; } return 2; } } } collapse_vars_while: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: false, properties: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { function f1(y) { // Neither the non-constant while condition `c` will be // replaced, nor the non-constant `x` in the body. var x = y, c = 3 - y; while (c) { return x; } var z = y * y; return z; } function f2(y) { // The constant `x` will be replaced in the while body. var x = 7; while (y) { return x; } var z = y * y; return z; } function f3(y) { // The non-constant `n` will not be replaced in the while body. var n = 5 - y; while (y) { return n; } var z = y * y; return z; } } expect: { function f1(y) { var x = y, c = 3 - y; while (c) return x; return y * y; } function f2(y) { while (y) return 7; return y * y } function f3(y) { var n = 5 - y; while (y) return n; return y * y; } } } collapse_vars_do_while: { options = { booleans: false, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: false, properties: true, sequences: true, side_effects: true, unused: "keep_assign", } input: { function f1(y) { // The constant do-while condition `c` will not be replaced. var c = 9; do {} while (c === 77); } function f2(y) { // The non-constant do-while condition `c` will not be replaced. var c = 5 - y; do { } while (c); } function f3(y) { // The constant `x` will be replaced in the do loop body. function fn(n) { console.log(n); } var a = 2, x = 7; do { fn(a = x); break; } while (y); } function f4(y) { // The non-constant `a` will not be replaced in the do loop body. var a = y / 4; do { return a; } while (y); } function f5(y) { function p(x) { console.log(x); } do { // The non-constant `a` will be replaced in p(a) // because it is declared in same block. var a = y - 3; p(a); } while (--y); } } expect: { function f1(y) { var c = 9; do ; while (77 === c); } function f2(y) { var c = 5 - y; do ; while (c); } function f3(y) { function fn(n) { console.log(n); } var a = 2, x = 7; do { fn(a = x); break; } while (y); } function f4(y) { var a = y / 4; do return a; while (y); } function f5(y) { function p(x) { console.log(x); } do { p(y - 3); } while (--y); } } } collapse_vars_do_while_drop_assign: { options = { booleans: false, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: false, properties: true, sequences: true, side_effects: true, unused: true, } input: { function f1(y) { // The constant do-while condition `c` will be not replaced. var c = 9; do {} while (c === 77); } function f2(y) { // The non-constant do-while condition `c` will not be replaced. var c = 5 - y; do { } while (c); } function f3(y) { // The constant `x` will be replaced in the do loop body. function fn(n) { console.log(n); } var a = 2, x = 7; do { fn(a = x); break; } while (y); } function f4(y) { // The non-constant `a` will not be replaced in the do loop body. var a = y / 4; do { return a; } while (y); } function f5(y) { function p(x) { console.log(x); } do { // The non-constant `a` will be replaced in p(a) // because it is declared in same block. var a = y - 3; p(a); } while (--y); } } expect: { function f1(y) { var c = 9; do ; while (77 === c); } function f2(y) { var c = 5 - y; do ; while (c); } function f3(y) { function fn(n) { console.log(n); } var x = 7; do { fn(x); break; } while (y); } function f4(y) { var a = y / 4; do return a; while (y); } function f5(y) { function p(x) { console.log(x); } do { p(y - 3); } while (--y); } } } collapse_vars_seq: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { var f1 = function(x, y) { var a, b, r = x + y, q = r * r, z = q - r; a = z, b = 7; return a + b; }; console.log(f1(1, 2)); } expect: { var f1 = function(x, y) { var r = x + y; return r * r - r + 7; }; console.log(f1(1, 2)); } expect_stdout: "13" } collapse_vars_throw: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { var f1 = function(x, y) { var a, b, r = x + y, q = r * r, z = q - r; a = z, b = 7; throw a + b; }; try { f1(1, 2); } catch (e) { console.log(e); } } expect: { var f1 = function(x, y) { var r = x + y; throw r * r - r + 7; }; try { f1(1, 2); } catch (e) { console.log(e); } } expect_stdout: "13" } collapse_vars_switch: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { function f1() { var not_used = sideeffect(), x = g1 + g2; var y = x / 4, z = 'Bar' + y; switch (z) { case 0: return g9; } } function f2() { var x = g1 + g2, not_used = sideeffect(); var y = x / 4 var z = 'Bar' + y; switch (z) { case 0: return g9; } } function f3(x) { switch(x) { case 1: var a = 3 - x; return a; } } } expect: { function f1() { sideeffect(); switch ("Bar" + (g1 + g2) / 4) { case 0: return g9 } } function f2() { var x = g1 + g2; sideeffect(); switch ("Bar" + x / 4) { case 0: return g9 } } function f3(x) { // verify no extraneous semicolon in case block before return // when the var definition was eliminated switch(x) { case 1: return 3 - x; } } } } collapse_vars_assignment: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { function log(x) { return console.log(x), x; } function f0(c) { var a = 3 / c; return a = a; } function f1(c) { var a = 3 / c; var b = 1 - a; return b; } function f2(c) { var a = 3 / c; var b = a - 7; return log(c = b); } function f3(c) { var a = 3 / c; var b = a - 7; return log(c |= b); } function f4(c) { var a = 3 / c; var b = 2; return log(b += a); } function f5(c) { var b = 2; var a = 3 / c; return log(b += a); } function f6(c) { var b = g(); var a = 3 / c; return log(b += a); } } expect: { function log(x) { return console.log(x), x; } function f0(c) { var a = 3 / c; return a = a; } function f1(c) { return 1 - 3 / c; } function f2(c) { return log(c = 3 / c - 7); } function f3(c) { return log(c |= 3 / c - 7); } function f4(c) { var b = 2; return log(b += 3 / c); } function f5(c) { var b = 2; return log(b += 3 / c); } function f6(c) { var b = g(); return log(b += 3 / c); } } } collapse_vars_lvalues: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: "keep_assign", } input: { function f0(x) { var i = ++x; return x += i; } function f1(x) { var a = (x -= 3); return x += a; } function f2(x) { var z = x, a = ++z; return z += a; } function f3(x) { var a = (x -= 3), b = x + a; return b; } function f4(x) { var a = (x -= 3); return x + a; } function f5(x) { var w = e1(), v = e2(), c = v = --x, b = w = x; return b - c; } function f6(x) { var w = e1(), v = e2(), c = v = --x, b = w = x; return c - b; } function f7(x) { var w = e1(), v = e2(), c = v - x, b = w = x; return b - c; } function f8(x) { var w = e1(), v = e2(), b = w = x, c = v - x; return b - c; } function f9(x) { var w = e1(), v = e2(), b = w = x, c = v - x; return c - b; } } expect: { function f0(x) { var i = ++x; return x += i; } function f1(x) { var a = (x -= 3); return x += a; } function f2(x) { var z = x, a = ++z; return z += a; } function f3(x) { var a = (x -= 3); return x + a; } function f4(x) { var a = (x -= 3); return x + a; } function f5(x) { var w = e1(), v = e2(), c = v = --x; return (w = x) - c; } function f6(x) { var w = e1(), v = e2(); return (v = --x) - (w = x); } function f7(x) { var w = e1(); return (w = x) - (e2() - x); } function f8(x) { var w = e1(); return (w = x) - (e2() - x); } function f9(x) { var w = e1(); return e2() - x - (w = x); } } } collapse_vars_lvalues_drop_assign: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, passes: 3, properties: true, sequences: true, side_effects: true, unused: true, } input: { function f0(x) { var i = ++x; return x += i; } function f1(x) { var a = (x -= 3); return x += a; } function f2(x) { var z = x, a = ++z; return z += a; } function f3(x) { var a = (x -= 3), b = x + a; return b; } function f4(x) { var a = (x -= 3); return x + a; } function f5(x) { var w = e1(), v = e2(), c = v = --x, b = w = x; return b - c; } function f6(x) { var w = e1(), v = e2(), c = v = --x, b = w = x; return c - b; } function f7(x) { var w = e1(), v = e2(), c = v - x, b = w = x; return b - c; } function f8(x) { var w = e1(), v = e2(), b = w = x, c = v - x; return b - c; } function f9(x) { var w = e1(), v = e2(), b = w = x, c = v - x; return c - b; } } expect: { function f0(x) { var i = ++x; return x += i; } function f1(x) { var a = (x -= 3); return x += a; } function f2(x) { var z = x, a = ++z; return z += a; } function f3(x) { var a = (x -= 3); return x + a; } function f4(x) { var a = (x -= 3); return x + a; } function f5(x) { e1(), e2(); var c = --x; return x - c; } function f6(x) { return e1(), e2(), --x - x; } function f7(x) { return e1(), x - (e2() - x); } function f8(x) { return e1(), x - (e2() - x); } function f9(x) { return e1(), e2() - x - x; } } } collapse_vars_misc1: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: 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; } function f1(x) { var y = 5 - x; return y; } function f2(x) { var z = foo(), y = z / (5 - x); return y; } function f3(x) { var z = foo(), y = (5 - x) / z; return y; } function f4(x) { var z = foo(), y = (5 - u) / z; return y; } function f5(x) { var z = foo(), y = (5 - window.x) / z; return y; } function f6() { var b = window.a * window.z; return b && zap(); } function f7() { var b = window.a * window.z; return b + b; } function f8() { var b = window.a * window.z; var c = b + 5; return b + c; } function f9() { var b = window.a * window.z; return bar() || b; } function f10(x) { var a = 5, b = 3; return a += b; } function f11(x) { var a = 5, b = 3; return a += --b; } } expect: { function f0(o, a, h) { var b = 3 - a; return o.run(b)[7] = h; } function f1(x) { return 5 - x } function f2(x) { return foo() / (5 - x) } function f3(x) { return (5 - x) / foo() } function f4(x) { var z = foo(); return (5 - u) / z } function f5(x) { var z = foo(); return (5 - window.x) / z } function f6() { return window.a * window.z && zap() } function f7() { var b = window.a * window.z; return b + b } function f8() { var b = window.a * window.z; return b + (5 + b) } function f9() { var b = window.a * window.z; return bar() || b } function f10(x) { var a = 5; return a += 3; } function f11(x) { var a = 5; return a += 2; } } } collapse_vars_self_reference: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: false, } input: { // avoid bug in self-referential declaration. function f1() { var self = { inner: function() { return self; } }; } function f2() { var self = { inner: self }; } } expect: { // note: `unused` option is false function f1() { var self = { inner: function() { return self } }; } function f2() { var self = { inner: self }; } } } collapse_vars_repeated: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { function f1() { var dummy = 3, a = 5, unused = 2, a = 1, a = 3; return -a; } function f2(x) { var a = 3, a = x; return a; } (function(x){ var a = "GOOD" + x, e = "BAD", k = "!", e = a; console.log(e + k); })("!"), (function(x){ var a = "GOOD" + x, e = "BAD" + x, k = "!", e = a; console.log(e + k); })("!"); } expect: { function f1() { return -3; } function f2(x) { return x; } (function(x){ console.log("GOOD!!"); })(), (function(x){ console.log("GOOD!!"); })(); } expect_stdout: true } collapse_vars_closures: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { function constant_vars_can_be_replaced_in_any_scope() { var outer = 3; return function() { return outer; } } function non_constant_vars_can_only_be_replace_in_same_scope(x) { var outer = x; return function() { return outer; } } } expect: { function constant_vars_can_be_replaced_in_any_scope() { return function() { return 3 } } function non_constant_vars_can_only_be_replace_in_same_scope(x) { var outer = x return function() { return outer } } } } collapse_vars_unary: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { function f0(o, p) { var x = o[p]; return delete x; } function f1(n) { var k = !!n; return n > +k; } function f2(n) { // test unary with constant var k = 7; return k--; } function f3(n) { // test unary with constant var k = 7; return ++k; } function f4(n) { // test unary with non-constant var k = 8 - n; return k--; } function f5(n) { // test unary with non-constant var k = 9 - n; return ++k; } } expect: { function f0(o, p) { var x = o[p]; return delete x; } function f1(n) { return +!!n < n; } function f2(n) { var k = 7; return k--; } function f3(n) { var k = 7; return ++k; } function f4(n) { var k = 8 - n; return k--; } function f5(n) { var k = 9 - n; return ++k; } } } collapse_vars_try: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { function f1() { try { var a = 1; return a; } catch (ex) { var b = 2; return b; } finally { var c = 3; return c; } } function f2() { var t = could_throw(); // shouldn't be replaced in try block try { return t + might_throw(); } catch (ex) { return 3; } } } expect: { function f1() { try { return 1; } catch (ex) { return 2; } finally { return 3; } } function f2() { var t = could_throw(); try { return t + might_throw(); } catch (ex) { return 3; } } } } collapse_vars_array: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { function f1(x, y) { var z = x + y; return [z]; } function f2(x, y) { var z = x + y; return [x, side_effect(), z]; } function f3(x, y) { var z = f(x + y); return [ [3], [z, x, y], [g()] ]; } } expect: { function f1(x, y) { return [x + y] } function f2(x, y) { var z = x + y return [x, side_effect(), z] } function f3(x, y) { return [ [3], [f(x + y), x, y], [g()] ] } } } collapse_vars_object: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { function f0(x, y) { var z = x + y; return { get b() { return 7; }, r: z }; } function f1(x, y) { var z = x + y; return { r: z, get b() { return 7; } }; } function f2(x, y) { var z = x + y; var k = x - y; return { q: k, r: g(x), s: z }; } function f3(x, y) { var z = f(x + y); return [{ a: {q: x, r: y, s: z}, b: g() }]; } } expect: { function f0(x, y) { return { get b() { return 7; }, r: x + y }; } function f1(x, y) { return { r: x + y, get b() { return 7; } }; } function f2(x, y) { var z = x + y; return { q: x - y, r: g(x), s: z }; } function f3(x, y) { return [{ a: {q: x, r: y, s: f(x + y)}, b: g() }]; } } } collapse_vars_eval_and_with: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: false, side_effects: true, unused: true, } input: { // Don't attempt to collapse vars in presence of eval() or with statement. (function f0() { var a = 2; console.log(a - 5); eval("console.log(a);"); })(); (function f1() { var o = {a: 1}, a = 2; with (o) console.log(a); })(); (function f2() { var o = {a: 1}, a = 2; return function() { with (o) console.log(a) }; })()(); } expect: { (function f0() { var a = 2; console.log(a - 5); eval("console.log(a);"); })(); (function f1() { var o = {a: 1}, a = 2; with(o) console.log(a); })(); (function f2() { var o = {a: 1}, a = 2; return function() { with (o) console.log(a) }; })()(); } expect_stdout: true } collapse_vars_constants: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { function f1(x) { var a = 4, b = x.prop, c = 5, d = sideeffect1(), e = sideeffect2(); return b + (function() { return d - a * e - c; })(); } function f2(x) { var a = 4, b = x.prop, c = 5, not_used = sideeffect1(), e = sideeffect2(); return b + (function() { return -a * e - c; })(); } function f3(x) { var a = 4, b = x.prop, c = 5, not_used = sideeffect1(); return b + (function() { return -a - c; })(); } } expect: { function f1(x) { var b = x.prop, d = sideeffect1(), e = sideeffect2(); return b + (function() { return d - 4 * e - 5; })(); } function f2(x) { var b = x.prop, e = (sideeffect1(), sideeffect2()); return b + (function() { return -4 * e - 5; })(); } function f3(x) { var b = x.prop; sideeffect1(); return b + (function() { return -9; })(); } } } collapse_vars_arguments: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, unused: true, } input: { var outer = function() { // Do not replace `arguments` but do replace the constant `k` before it. var k = 7, arguments = 5, inner = function() { console.log(arguments); } inner(k, 1); } outer(); } expect: { (function() { (function(){console.log(arguments);})(7, 1); })(); } expect_stdout: true } collapse_vars_short_circuit: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { function f0(x) { var a = foo(), b = bar(); return b || x; } function f1(x) { var a = foo(), b = bar(); return b && x; } function f2(x) { var a = foo(), b = bar(); return x && a && b; } function f3(x) { var a = foo(), b = bar(); return a && x; } function f4(x) { var a = foo(), b = bar(); return a && x && b; } function f5(x) { var a = foo(), b = bar(); return x || a || b; } function f6(x) { var a = foo(), b = bar(); return a || x || b; } function f7(x) { var a = foo(), b = bar(); return a && b && x; } function f8(x,y) { var a = foo(), b = bar(); return (x || a) && (y || b); } function f9(x,y) { var a = foo(), b = bar(); return (x && a) || (y && b); } function f10(x,y) { var a = foo(), b = bar(); return (x - a) || (y - b); } function f11(x,y) { var a = foo(), b = bar(); return (x - b) || (y - a); } function f12(x,y) { var a = foo(), b = bar(); return (x - y) || (b - a); } function f13(x,y) { var a = foo(), b = bar(); return (a - b) || (x - y); } function f14(x,y) { var a = foo(), b = bar(); return (b - a) || (x - y); } } expect: { function f0(x) { foo(); return bar() || x; } function f1(x) { foo(); return bar() && x; } function f2(x) { var a = foo(), b = bar(); return x && a && b; } function f3(x) { var a = foo(); bar(); return a && x; } function f4(x) { var a = foo(), b = bar(); return a && x && b; } function f5(x) { var a = foo(), b = bar(); return x || a || b; } function f6(x) { var a = foo(), b = bar(); return a || x || b; } function f7(x) { var a = foo(), b = bar(); return a && b && x; } function f8(x,y) { var a = foo(), b = bar(); return (x || a) && (y || b); } function f9(x,y) { var a = foo(), b = bar(); return (x && a) || (y && b); } function f10(x,y) { var a = foo(), b = bar(); return (x - a) || (y - b); } function f11(x,y) { var a = foo(); return (x - bar()) || (y - a); } function f12(x,y) { var a = foo(), b = bar(); return (x - y) || (b - a); } function f13(x,y) { return (foo() - bar()) || (x - y); } function f14(x,y) { var a = foo(); return (bar() - a) || (x - y); } } } collapse_vars_short_circuited_conditions: { options = { booleans: true, collapse_vars: true, comparisons: false, conditionals: false, dead_code: true, evaluate: true, hoist_funs: true, if_return: false, join_vars: true, keep_fargs: true, loops: true, sequences: false, side_effects: true, unused: true, } input: { function c1(x) { var a = foo(), b = bar(), c = baz(); return a ? b : c; } function c2(x) { var a = foo(), b = bar(), c = baz(); return a ? c : b; } function c3(x) { var a = foo(), b = bar(), c = baz(); return b ? a : c; } function c4(x) { var a = foo(), b = bar(), c = baz(); return b ? c : a; } function c5(x) { var a = foo(), b = bar(), c = baz(); return c ? a : b; } function c6(x) { var a = foo(), b = bar(), c = baz(); return c ? b : a; } function i1(x) { var a = foo(), b = bar(), c = baz(); if (a) return b; else return c; } function i2(x) { var a = foo(), b = bar(), c = baz(); if (a) return c; else return b; } function i3(x) { var a = foo(), b = bar(), c = baz(); if (b) return a; else return c; } function i4(x) { var a = foo(), b = bar(), c = baz(); if (b) return c; else return a; } function i5(x) { var a = foo(), b = bar(), c = baz(); if (c) return a; else return b; } function i6(x) { var a = foo(), b = bar(), c = baz(); if (c) return b; else return a; } } expect: { function c1(x) { var a = foo(), b = bar(), c = baz(); return a ? b : c; } function c2(x) { var a = foo(), b = bar(), c = baz(); return a ? c : b; } function c3(x) { var a = foo(), b = bar(), c = baz(); return b ? a : c; } function c4(x) { var a = foo(), b = bar(), c = baz(); return b ? c : a; } function c5(x) { var a = foo(), b = bar(); return baz() ? a : b; } function c6(x) { var a = foo(), b = bar(); return baz() ? b : a; } function i1(x) { var a = foo(), b = bar(), c = baz(); if (a) return b; else return c; } function i2(x) { var a = foo(), b = bar(), c = baz(); if (a) return c; else return b; } function i3(x) { var a = foo(), b = bar(), c = baz(); if (b) return a; else return c; } function i4(x) { var a = foo(), b = bar(), c = baz(); if (b) return c; else return a; } function i5(x) { var a = foo(), b = bar(); if (baz()) return a; else return b; } function i6(x) { var a = foo(), b = bar(); if (baz()) return b; else return a; } } } collapse_vars_regexp: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: false, reduce_funcs: true, reduce_vars: true, side_effects: true, unused: true, } input: { function f1() { var k = 9; var rx = /[A-Z]+/; return [rx, k]; } function f2() { var rx = /ab*/g; return function(s) { return rx.exec(s); }; } (function() { var result; var s = 'acdabcdeabbb'; var rx = /ab*/g; while (result = rx.exec(s)) { console.log(result[0]); } })(); (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 = /ab*/g; return function(s) { return rx.exec(s); }; } (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 } issue_1537: { options = { collapse_vars: true, } input: { var k = ''; for (k in {prop: 'val'}){} } expect: { var k = ''; for (k in {prop: 'val'}); } } issue_1562: { options = { collapse_vars: true, evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var v = 1, B = 2; for (v in objs) f(B); var x = 3, C = 10; while(x + 2) bar(C); var y = 4, D = 20; do bar(D); while(y + 2); var z = 5, E = 30; for (; f(z + 2) ;) bar(E); } expect: { var v = 1; for (v in objs) f(2); while(5) bar(10); do bar(20); while(6); for (; f(7) ;) bar(30); } } issue_1605_1: { options = { collapse_vars: true, toplevel: false, unused: true, } input: { function foo(x) { var y = x; return y; } var o = new Object; o.p = 1; } expect: { function foo(x) { return x; } var o = new Object; o.p = 1; } } issue_1605_2: { options = { collapse_vars: true, toplevel: "vars", unused: true, } input: { function foo(x) { var y = x; return y; } var o = new Object; o.p = 1; } expect: { function foo(x) { return x; } (new Object).p = 1; } } issue_1631_1: { options = { collapse_vars: true, hoist_funs: true, join_vars: true, sequences: true, side_effects: true, } input: { var pc = 0; function f(x) { pc = 200; return 100; } function x() { var t = f(); pc += t; return pc; } console.log(x()); } expect: { function f(x) { return pc = 200, 100; } function x() { var t = f(); return pc += t; } var pc = 0; console.log(x()); } expect_stdout: "300" } issue_1631_2: { options = { collapse_vars: true, hoist_funs: true, join_vars: true, sequences: true, side_effects: true, } input: { var a = 0, b = 1; function f() { a = 2; return 4; } function g() { var t = f(); b = a + t; return b; } console.log(g()); } expect: { function f() { return a = 2, 4; } function g() { var t = f(); return b = a + t; } var a = 0, b = 1; console.log(g()); } expect_stdout: "6" } issue_1631_3: { options = { collapse_vars: true, hoist_funs: true, join_vars: true, sequences: true, side_effects: true, } input: { function g() { var a = 0, b = 1; function f() { a = 2; return 4; } var t = f(); b = a + t; return b; } console.log(g()); } expect: { function g() { function f() { return a = 2, 4; } var a = 0, b = 1, t = f(); return b = a + t; } console.log(g()); } expect_stdout: "6" } var_side_effects_1: { options = { collapse_vars: true, unused: true, } input: { var print = console.log.bind(console); function foo(x) { var twice = x * 2; print('Foo:', twice); } foo(10); } expect: { var print = console.log.bind(console); function foo(x) { print('Foo:', 2 * x); } foo(10); } expect_stdout: true } var_side_effects_2: { options = { collapse_vars: true, unused: true, } input: { var print = console.log.bind(console); function foo(x) { var twice = x.y * 2; print('Foo:', twice); } foo({ y: 10 }); } expect: { var print = console.log.bind(console); function foo(x) { var twice = 2 * x.y; print('Foo:', twice); } foo({ y: 10 }); } expect_stdout: true } var_side_effects_3: { options = { collapse_vars: true, pure_getters: true, unsafe: true, unused: true, } input: { var print = console.log.bind(console); function foo(x) { var twice = x.y * 2; print('Foo:', twice); } foo({ y: 10 }); } expect: { var print = console.log.bind(console); function foo(x) { print('Foo:', 2 * x.y); } foo({ y: 10 }); } expect_stdout: true } reduce_vars_assign: { options = { collapse_vars: true, reduce_funcs: true, reduce_vars: true, } input: { !function() { var a = 1; a = [].length, console.log(a); }(); } expect: { !function() { var a = 1; a = [].length, console.log(a); }(); } expect_stdout: "0" } iife_1: { options = { collapse_vars: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var log = function(x) { console.log(x); }, foo = bar(); log(foo); } expect: { (function(x) { console.log(x); })(bar()); } } iife_2: { options = { collapse_vars: true, reduce_funcs: false, reduce_vars: false, toplevel: true, unused: false, } input: { var foo = bar(); !function(x) { console.log(x); }(foo); } expect: { var foo; !function(x) { console.log(x); }(bar()); } } var_defs: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { var f1 = function(x, y) { var a, b, r = x + y, q = r * r, z = q - r, a = z, b = 7; console.log(a + b); }; f1("1", 0); } expect: { var f1 = function(x, y) { var r = x + y, a = r * r - r, b = 7; console.log(a + b); }; f1("1", 0); } expect_stdout: "97" } assignment: { options = { collapse_vars: true, unused: true, } input: { function f() { var a; a = x; return a; } } expect: { function f() { return x; } } } for_init: { options = { collapse_vars: true, unused: true, } input: { function f(x, y) { var a = x; var b = y; for (a; b;); } } expect: { function f(x, y) { var b = y; for (x; b;); } } } switch_case_1: { options = { collapse_vars: true, unused: true, } input: { function f(x, y, z) { var a = x(); var b = y(); var c = z; switch (a) { default: d(); case b: e(); case c: f(); } } } expect: { function f(x, y, z) { switch (x()) { default: d(); case y(): e(); case z: f(); } } } } switch_case_2: { options = { collapse_vars: true, } input: { var a = 1, b = 2; switch (b++) { case b: var c = a; var a; break; } console.log(a); } expect: { var a = 1, b = 2; switch (b++) { case b: var c = a; var a; break; } console.log(a); } expect_stdout: "1" } switch_case_3: { options = { collapse_vars: true, } input: { var a = 1, b = 2; switch (a) { case a: var b; break; case b: break; } console.log(b); } expect: { var a = 1, b = 2; switch (a) { case a: var b; break; case b: break; } console.log(b); } expect_stdout: "2" } issue_27: { options = { collapse_vars: true, unused: true, } input: { (function(jQuery) { var $; $ = jQuery; $("body").addClass("foo"); })(jQuery); } expect: { (function(jQuery) { jQuery("body").addClass("foo"); })(jQuery); } } modified: { options = { collapse_vars: true, unused: true, } input: { function f1(b) { var a = b; return b + a; } function f2(b) { var a = b; return b++ + a; } function f3(b) { var a = b++; return b + a; } function f4(b) { var a = b++; return b++ + a; } function f5(b) { var a = function() { return b; }(); return b++ + a; } console.log(f1(1), f2(1), f3(1), f4(1), f5(1)); } expect: { function f1(b) { return b + b; } function f2(b) { var a = b; return b++ + a; } function f3(b) { var a = b++; return b + a; } function f4(b) { var a = b++; return b++ + a; } function f5(b) { var a = function() { return b; }(); return b++ + a; } console.log(f1(1), f2(1), f3(1), f4(1), f5(1)); } expect_stdout: "2 2 3 3 2" } issue_1858: { options = { collapse_vars: true, pure_getters: true, unused: true, } input: { console.log(function(x) { var a = {}, b = a.b = x; return a.b + b; }(1)); } expect: { console.log(function(x) { var a = {}, b = a.b = 1; return a.b + b; }()); } expect_stdout: "2" } anonymous_function: { options = { collapse_vars: true, } input: { console.log(function f(a) { f ^= 0; return f * a; }(1)); } expect: { console.log(function f(a) { f ^= 0; return f * a; }(1)); } expect_stdout: true } side_effects_property: { options = { collapse_vars: true, } input: { var a = []; var b = 0; a[b++] = function() { return 42;}; var c = a[b++](); console.log(c); } expect: { var a = []; var b = 0; a[b++] = function() { return 42;}; var c = a[b++](); console.log(c); } expect_stdout: true } undeclared: { options = { collapse_vars: true, unused: true, } input: { function f(x, y) { var a; a = x; b = y; return b + a; } } expect: { function f(x, y) { b = y; return b + x; } } } ref_scope: { options = { collapse_vars: true, unused: true, } input: { console.log(function() { var a = 1, b = 2, c = 3; var a = c++, b = b /= a; return function() { return a; }() + b; }()); } expect: { console.log(function() { var a = 1, b = 2, c = 3; b = b /= a = c++; return function() { return a; }() + b; }()); } expect_stdout: true } chained_1: { options = { collapse_vars: true, unused: true, } input: { var a = 2; var a = 3 / a; console.log(a); } expect: { var a = 3 / (a = 2); console.log(a); } expect_stdout: true } chained_2: { options = { collapse_vars: true, unused: true, } input: { var a; var a = 2; a = 3 / a; console.log(a); } expect: { var a; a = 3 / (a = 2); console.log(a); } expect_stdout: true } chained_3: { options = { collapse_vars: true, unused: true, } input: { console.log(function(a, b) { var c = a, c = b; b++; return c; }(1, 2)); } expect: { console.log(function(a, b) { var c = 1; c = b; b++; return c; }(0, 2)); } expect_stdout: "2" } boolean_binary_1: { options = { collapse_vars: true, } input: { var a = 1; a++; (function() {} || a || 3).toString(); console.log(a); } expect: { var a = 1; a++; (function() {} || a || 3).toString(); console.log(a); } expect_stdout: true } boolean_binary_2: { options = { collapse_vars: true, } input: { var c = 0; c += 1; (function() { c = 1 + c; } || 9).toString(); console.log(c); } expect: { var c = 0; c += 1; (function() { c = 1 + c; } || 9).toString(); console.log(c); } expect_stdout: true } inner_lvalues: { options = { collapse_vars: true, unused: true, } input: { var a, b = 10; var a = (--b || a || 3).toString(), c = --b + -a; console.log(null, a, b); } expect: { var b = 10; var a = (--b || a || 3).toString(), c = --b + -a; console.log(null, a, b); } expect_stdout: true } double_def_1: { options = { collapse_vars: true, unused: true, } input: { var a = x, a = a && y; a(); } expect: { var a; (a = (a = x) && y)(); } } double_def_2: { options = { collapse_vars: true, toplevel: true, unused: true, } input: { var a = x, a = a && y; a(); } expect: { (x && y)(); } } toplevel_single_reference: { options = { collapse_vars: true, unused: true, } input: { var a; for (var b in x) { var a = b; b(a); } } expect: { for (var b in x) { var a; b(a = b); } } } unused_orig: { options = { collapse_vars: true, dead_code: true, passes: 2, reduce_funcs: true, reduce_vars: true, unused: true, } input: { var a = 1; console.log(function(b) { var a; var c = b; for (var d in c) { var a = c[0]; return --b + a; } try { } catch (e) { --b + a; } a && a.NaN; }([2]), a); } expect: { var a = 1; console.log(function(b) { var c = b; for (var d in c) { var a; return --b + c[0]; } a && a.NaN; }([2]), a); } expect_stdout: "3 1" } issue_315: { options = { collapse_vars: true, evaluate: true, keep_fargs: false, reduce_funcs: true, reduce_vars: true, sequences: true, unused: true, } input: { console.log(function(s) { var w, _i, _len, _ref, _results; _ref = s.trim().split(" "); _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { w = _ref[_i]; _results.push(w.toLowerCase()); } return _results; }("test")); } expect: { console.log(function() { var w, _i, _len, _ref, _results; for (_results = [], _i = 0, _len = (_ref = "test".trim().split(" ")).length; _i < _len ; _i++) w = _ref[_i], _results.push(w.toLowerCase()); return _results; }()); } expect_stdout: true } lvalues_def: { options = { collapse_vars: true, side_effects: true, unused: true, } input: { var a = 0, b = 1; var a = b++, b = +function() {}(); a && a[a++]; console.log(a, b); } expect: { var a = 0, b = 1; a = b++, b = +void 0; a && a[a++]; console.log(a, b); } expect_stdout: true } compound_assignment: { options = { collapse_vars: true, } input: { var a; a = 1; a += a + 2; console.log(a); } expect: { var a; a = 1; a += a + 2; console.log(a); } expect_stdout: "4" } issue_2187_1: { options = { collapse_vars: true, unused: true, } input: { var a = 1; !function(foo) { foo(); var a = 2; console.log(a); }(function() { console.log(a); }); } expect: { var a = 1; !function(foo) { foo(); var a = 2; console.log(a); }(function() { console.log(a); }); } expect_stdout: [ "1", "2", ] } issue_2187_2: { options = { collapse_vars: true, unused: true, } input: { var b = 1; console.log(function(a) { return a && ++b; }(b--)); } expect: { var b = 1; console.log(function(a) { return b-- && ++b; }()); } expect_stdout: "1" } issue_2187_3: { options = { collapse_vars: true, inline: true, unused: true, } input: { var b = 1; console.log(function(a) { return a && ++b; }(b--)); } expect: { var b = 1; console.log(b-- && ++b); } expect_stdout: "1" } issue_2203_1: { options = { collapse_vars: true, unused: true, } input: { a = "FAIL"; console.log({ a: "PASS", b: function() { return function(c) { return c.a; }((String, (Object, this))); } }.b()); } expect: { a = "FAIL"; console.log({ a: "PASS", b: function() { return function(c) { return c.a; }((String, (Object, this))); } }.b()); } expect_stdout: "PASS" } issue_2203_2: { options = { collapse_vars: true, unused: true, } input: { a = "PASS"; console.log({ a: "FAIL", b: function() { return function(c) { return c.a; }((String, (Object, function() { return this; }()))); } }.b()); } expect: { a = "PASS"; console.log({ a: "FAIL", b: function() { return function(c) { return (String, (Object, function() { return this; }())).a; }(); } }.b()); } expect_stdout: "PASS" } duplicate_argname: { options = { collapse_vars: true, unused: true, } input: { function f() { return "PASS"; } console.log(function(a, a) { f++; return a; }("FAIL", f())); } expect: { function f() { return "PASS"; } console.log(function(a, a) { f++; return a; }("FAIL", f())); } expect_stdout: "PASS" } issue_2298: { options = { collapse_vars: true, passes: 2, reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { function f() { var a = undefined; var undefined = a++; try { !function g(b) { b[1] = "foo"; }(); console.log("FAIL"); } catch (e) { console.log("PASS"); } } f(); }(); } expect: { !function() { (function() { var a = undefined; var undefined = a++; try { !function(b) { (void 0)[1] = "foo"; }(); console.log("FAIL"); } catch (e) { console.log("PASS"); } })(); }(); } expect_stdout: "PASS" } issue_2313_1: { options = { collapse_vars: true, conditionals: true, } input: { var a = 0, b = 0; var foo = { get c() { a++; return 42; }, set c(c) { b++; }, d: function() { this.c++; if (this.c) console.log(a, b); } } foo.d(); } expect: { var a = 0, b = 0; var foo = { get c() { a++; return 42; }, set c(c) { b++; }, d: function() { this.c++; this.c && console.log(a, b); } } foo.d(); } expect_stdout: "2 1" } issue_2313_2: { options = { collapse_vars: true, } input: { var c = 0; !function a() { a && c++; var a = 0; a && c++; }(); console.log(c); } expect: { var c = 0; !function a() { a && c++; var a = 0; a && c++; }(); console.log(c); } 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, properties: true, pure_getters: true, reduce_funcs: 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, unused: 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, unused: 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, unused: 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" } issue_2437_1: { options = { collapse_vars: true, conditionals: true, inline: true, join_vars: true, passes: 2, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, unused: true, } input: { function foo() { return bar(); } function bar() { if (xhrDesc) { var req = new XMLHttpRequest(); var result = !!req.onreadystatechange; Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', xhrDesc || {}); return result; } else { var req = new XMLHttpRequest(); var detectFunc = function(){}; req.onreadystatechange = detectFunc; var result = req[SYMBOL_FAKE_ONREADYSTATECHANGE_1] === detectFunc; req.onreadystatechange = null; return result; } } console.log(foo()); } expect: { console.log(function() { if (xhrDesc) { var result = !!(req = new XMLHttpRequest()).onreadystatechange; return Object.defineProperty(XMLHttpRequest.prototype, "onreadystatechange", xhrDesc || {}), result; } var req = new XMLHttpRequest(), detectFunc = function(){}; return req.onreadystatechange = detectFunc, result = req[SYMBOL_FAKE_ONREADYSTATECHANGE_1] === detectFunc, req.onreadystatechange = null, result; }()); } } issue_2437_2: { options = { collapse_vars: true, conditionals: true, inline: true, join_vars: true, passes: 2, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, unused: true, } input: { function foo() { bar(); } function bar() { if (xhrDesc) { var req = new XMLHttpRequest(); var result = !!req.onreadystatechange; Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', xhrDesc || {}); return result; } else { var req = new XMLHttpRequest(); var detectFunc = function(){}; req.onreadystatechange = detectFunc; var result = req[SYMBOL_FAKE_ONREADYSTATECHANGE_1] === detectFunc; req.onreadystatechange = null; return result; } } foo(); } expect: { !function() { if (xhrDesc) return (req = new XMLHttpRequest()).onreadystatechange, Object.defineProperty(XMLHttpRequest.prototype, "onreadystatechange", xhrDesc || {}); var req = new XMLHttpRequest(); req.onreadystatechange = function(){}, req[SYMBOL_FAKE_ONREADYSTATECHANGE_1], req.onreadystatechange = null; }(); } } issue_2436_1: { options = { collapse_vars: true, inline: true, pure_getters: "strict", reduce_vars: true, toplevel: true, unused: true, } input: { var o = { a: 1, b: 2, }; console.log(function(c) { return { x: c.a, y: c.b, }; }(o)); } expect: { var o = { a: 1, b: 2, }; console.log({ x: o.a, y: o.b, }); } expect_stdout: true } issue_2436_2: { options = { collapse_vars: true, inline: true, pure_getters: "strict", reduce_vars: true, toplevel: true, unused: true, } input: { var o = { a: 1, b: 2, }; console.log(function(c) { o.a = 3; return { x: c.a, y: c.b, }; }(o)); } expect: { var o = { a: 1, b: 2, }; console.log(function(c) { o.a = 3; return { x: c.a, y: c.b, }; }(o)); } expect_stdout: true } issue_2436_3: { options = { collapse_vars: true, inline: true, pure_getters: "strict", reduce_vars: true, toplevel: true, unused: true, } input: { var o = { a: 1, b: 2, }; console.log(function(c) { o = { a: 3, b: 4, }; return { x: c.a, y: c.b, }; }(o)); } expect: { var o = { a: 1, b: 2, }; console.log(function(c) { o = { a: 3, b: 4, }; return { x: c.a, y: c.b, }; }(o)); } expect_stdout: true } issue_2436_4: { options = { collapse_vars: true, inline: true, pure_getters: "strict", reduce_vars: true, toplevel: true, unused: true, } input: { var o = { a: 1, b: 2, }; console.log(function(c) { return { x: c.a, y: c.b, }; var o; }(o)); } expect: { console.log({ x: (c = { a: 1, b: 2, }).a, y: c.b, }); var c; } expect_stdout: true } issue_2436_5: { options = { collapse_vars: true, inline: true, pure_getters: "strict", reduce_vars: true, toplevel: true, unused: true, } input: { var o = { a: 1, b: 2, }; console.log(function(o) { return { x: o.a, y: o.b, }; }(o)); } expect: { console.log(function(o) { return { x: o.a, y: o.b, }; }({ a: 1, b: 2, })); } expect_stdout: true } issue_2436_6: { options = { collapse_vars: true, evaluate: true, inline: true, passes: 2, pure_getters: "strict", reduce_vars: true, toplevel: true, unsafe: true, unused: true, } input: { var o = { a: 1, b: 2, }; console.log(function(c) { return { x: c.a, y: c.b, }; }(o)); } expect: { console.log({ x: 1, y: 2, }); } expect_stdout: true } issue_2436_7: { options = { collapse_vars: true, hoist_props: true, inline: true, passes: 3, pure_getters: "strict", reduce_vars: true, toplevel: true, unused: true, } input: { var o = { a: 1, b: 2, }; console.log(function(c) { return { x: c.a, y: c.b, }; }(o)); } expect: { console.log({ x: 1, y: 2, }); } expect_stdout: true } issue_2436_8: { options = { collapse_vars: true, inline: true, pure_getters: "strict", reduce_vars: true, toplevel: true, unused: true, } input: { console.log(function(c) { return { x: c.a, y: c.b, }; }(o)); } expect: { console.log({ x: (c = o).a, y: c.b, }); var c; } expect_stdout: true } issue_2436_9: { options = { collapse_vars: true, inline: true, pure_getters: "strict", reduce_vars: true, toplevel: true, unused: true, } input: { var o = console; console.log(function(c) { return { x: c.a, y: c.b, }; }(o)); } expect: { var o = console; console.log({ x: (c = o).a, y: c.b, }); var c; } expect_stdout: true } issue_2436_10: { options = { collapse_vars: true, inline: true, pure_getters: true, reduce_vars: true, toplevel: true, unused: true, } input: { var o = { a: 1, b: 2, }; function f(n) { o = { b: 3 }; return n; } console.log(function(c) { return [ c.a, f(c.b), c.b, ]; }(o).join(" ")); } expect: { var o = { a: 1, b: 2, }; function f(n) { o = { b: 3 }; return n; } console.log([ (c = o).a, f(c.b), c.b, ].join(" ")); var c; } expect_stdout: "1 2 2" } issue_2436_11: { options = { collapse_vars: true, join_vars: true, reduce_vars: true, unused: true, } input: { function matrix() {} function isCollection() {} function _randomDataForMatrix() {} function _randomInt() {} function f(arg1, arg2) { if (isCollection(arg1)) { var size = arg1; var max = arg2; var min = 0; var res = _randomDataForMatrix(size.valueOf(), min, max, _randomInt); return size && true === size.isMatrix ? matrix(res) : res; } else { var min = arg1; var max = arg2; return _randomInt(min, max); } } } expect: { function matrix() {} function isCollection() {} function _randomDataForMatrix() {} function _randomInt() {} function f(arg1, arg2) { if (isCollection(arg1)) { var size = arg1, max = arg2, min = 0, res = _randomDataForMatrix(size.valueOf(), min, max, _randomInt); return size && true === size.isMatrix ? matrix(res) : res; } else { return _randomInt(min = arg1, max = arg2); } } } } issue_2436_12: { options = { collapse_vars: true, unused: true, } input: { function isUndefined() {} function f() { var viewValue = this.$$lastCommittedViewValue; var modelValue = viewValue; return isUndefined(modelValue) ? modelValue : null; } } expect: { function isUndefined() {} function f() { var modelValue = this.$$lastCommittedViewValue; return isUndefined(modelValue) ? modelValue : null; } } } issue_2436_13: { options = { collapse_vars: true, passes: 2, reduce_vars: true, unused: true, } input: { var a = "PASS"; (function() { function f(b) { (function g(b) { var b = b && (b.null = "FAIL"); })(a); } f(); })(); console.log(a); } expect: { var a = "PASS"; (function() { (function(b) { (function(b) { a && (a.null = "FAIL"); })(); })(); })(); console.log(a); } expect_stdout: "PASS" } issue_2436_14: { options = { collapse_vars: true, reduce_vars: true, unused: true, } input: { var a = "PASS"; var b = {}; (function() { var c = a; c && function(c, d) { console.log(c, d); }(b, c); })(); } expect: { var a = "PASS"; var b = {}; (function() { a && function(c, d) { console.log(c, d); }(b, a); })(); } expect_stdout: true } issue_2497: { options = { collapse_vars: true, unused: true, } input: { function sample() { if (true) { for (var i = 0; i < 1; ++i) { for (var k = 0; k < 1; ++k) { var value = 1; var x = value; value = x ? x + 1 : 0; } } } else { for (var i = 0; i < 1; ++i) { for (var k = 0; k < 1; ++k) { var value = 1; } } } } } expect: { function sample() { if (true) for (var i = 0; i < 1; ++i) for (var k = 0; k < 1; ++k) { value = 1; value = value ? value + 1 : 0; } else for (i = 0; i < 1; ++i) for (k = 0; k < 1; ++k) var value = 1; } } } issue_2506: { options = { collapse_vars: true, passes: 2, reduce_vars: true, unused: true, } input: { var c = 0; function f0(bar) { function f1(Infinity_2) { function f13(NaN) { if (false <= NaN & this >> 1 >= 0) { c++; } } var b_2 = f13(NaN, c++); } var bar = f1(-3, -1); } f0(false); console.log(c); } expect: { var c = 0; function f0(bar) { (function(Infinity_2) { (function(NaN) { if (false <= 0/0 & this >> 1 >= 0) c++; })(0, c++); })(); } f0(false); console.log(c); } expect_stdout: "1" } issue_2571_1: { options = { collapse_vars: true, toplevel: true, } input: { var b = 1; try { var a = function f0(c) { throw c; }(2); var d = --b + a; } catch (e) { } console.log(b); } expect: { var b = 1; try { var a = function f0(c) { throw c; }(2); var d = --b + a; } catch (e) { } console.log(b); } expect_stdout: "1" } issue_2571_2: { options = { collapse_vars: true, toplevel: true, } input: { try { var a = A, b = 1; throw a; } catch (e) { console.log(b); } } expect: { try { var a = A, b = 1; throw a; } catch (e) { console.log(b); } } expect_stdout: "undefined" } may_throw_1: { options = { collapse_vars: true, } input: { function f() { var a_2 = function() { var a; }(); } } expect: { function f() { var a_2 = function() { var a; }(); } } } may_throw_2: { options = { collapse_vars: true, unused: true, } input: { function f(b) { try { var a = x(); ++b; return b(a); } catch (e) {} console.log(b); } f(0); } expect: { function f(b) { try { var a = x(); return (++b)(a); } catch (e) {} console.log(b); } f(0); } expect_stdout: "0" } side_effect_free_replacement: { options = { collapse_vars: true, inline: true, side_effects: true, unused: true, } input: { var b; (function(a) { x(a); })(b); } expect: { var b; x(b); } } recursive_function_replacement: { rename = true options = { collapse_vars: true, inline: true, passes: 2, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } mangle = {} input: { function f(a) { return x(g(a)); } function g(a) { return y(f(a)); } console.log(f(c)); } expect: { console.log(function n(o) { return x(y(n(o))); }(c)); } } cascade_conditional: { options = { collapse_vars: true, } input: { function f(a, b) { (a = x(), a) ? a++ : (b = y(a), b(a)); } } expect: { function f(a, b) { (a = x()) ? a++ : (b = y(a))(a); } } } cascade_if_1: { options = { collapse_vars: true, } input: { var a; if (a = x(), a) if (a == y()) z(); } expect: { var a; if (a = x()) if (a == y()) z(); } } cascade_if_2: { options = { collapse_vars: true, } input: { function f(a, b) { if (a(), b = x()) return b; } } expect: { function f(a, b) { if (a(), b = x()) return b; } } } cascade_return: { options = { collapse_vars: true, } input: { function f(a) { return a = x(); return a; } } expect: { function f(a) { return a = x(); return a; } } } cascade_switch: { options = { collapse_vars: true, } input: { function f(a, b) { switch(a = x(), a) { case a = x(), b(a): break; } } } expect: { function f(a, b) { switch(a = x()) { case b(a = x()): break; } } } } cascade_call: { options = { collapse_vars: true, unused: true, } input: { function f(a) { var b; return x((b = a, y(b))); } } expect: { function f(a) { return x(y(a)); } } } replace_all_var: { options = { collapse_vars: true, unused: true, } input: { var a = "PASS"; (function() { var b = b || c && c[a = "FAIL"], c = a; })(); console.log(a); } expect: { var a = "PASS"; (function() { var b = b || c && c[a = "FAIL"], c = a; })(); console.log(a); } expect_stdout: "PASS" } replace_all_var_scope: { rename = true options = { collapse_vars: true, unused: true, } mangle = {} input: { var a = 100, b = 10; (function(r, a) { switch (~a) { case (b += a): case a++: } })(--b, a); console.log(a, b); } expect: { var a = 100, b = 10; (function(c, o) { switch (~a) { case (b += a): case o++: } })(--b, a); console.log(a, b); } expect_stdout: "100 109" } cascade_statement: { options = { collapse_vars: true, } input: { function f1(a, b) { var c; if (a) return c = b, c || a; else c = a, c(b); } function f2(a, b) { var c; while (a) c = b, a = c + b; do throw c = a + b, c; while (c); } function f3(a, b) { for (; a < b; a++) if (c = a, c && b) var c = (c = b(a), c); } } expect: { function f1(a, b) { var c; if (a) return (c = b) || a; else (c = a)(b); } function f2(a, b) { var c; while (a) a = (c = b) + b; do throw c = a + b; while (c); } function f3(a, b) { for (; a < b; a++) if ((c = a) && b) var c = c = b(a); } } } cascade_forin: { options = { collapse_vars: true, } input: { var a; function f(b) { return [ b, b, b ]; } for (var c in a = console, f(a)) console.log(c); } expect: { var a; function f(b) { return [ b, b, b ]; } for (var c in f(a = console)) console.log(c); } expect_stdout: [ "0", "1", "2", ] } unsafe_builtin: { options = { collapse_vars: true, pure_getters: "strict", unsafe: true, unused: true, } input: { function f(a) { var b = Math.abs(a); return Math.pow(b, 2); } console.log(f(-1), f(2)); } expect: { function f(a) { return Math.pow(Math.abs(a), 2); } console.log(f(-1), f(2)); } expect_stdout: "1 4" } return_1: { options = { collapse_vars: true, unused: true, } input: { var log = console.log; function f(b, c) { var a = c; if (b) return b; log(a); } f(false, 1); f(true, 2); } expect: { var log = console.log; function f(b, c) { if (b) return b; log(c); } f(false, 1); f(true, 2); } expect_stdout: "1" } return_2: { options = { collapse_vars: true, unused: true, } input: { var log = console.log; function f(b, c) { var a = c(); if (b) return b; log(a); } f(false, function() { return 1 }); f(true, function() { return 2 }); } expect: { var log = console.log; function f(b, c) { var a = c(); if (b) return b; log(a); } f(false, function() { return 1 }); f(true, function() { return 2 }); } expect_stdout: "1" } return_3: { options = { collapse_vars: true, unused: true, } input: { var log = console.log; function f(b, c) { var a = b <<= c; if (b) return b; log(a); } f(false, 1); f(true, 2); } expect: { var log = console.log; function f(b, c) { var a = b <<= c; if (b) return b; log(a); } f(false, 1); f(true, 2); } expect_stdout: "0" } return_4: { options = { collapse_vars: true, } input: { var a = "FAIL"; (function(b) { a = "PASS"; return; b(a); })(); console.log(a); } expect: { var a = "FAIL"; (function(b) { a = "PASS"; return; b(a); })(); console.log(a); } expect_stdout: "PASS" } issue_2858: { options = { collapse_vars: true, unused: true, } input: { var b; (function() { function f() { a++; } f(); var c = f(); var a = void 0; c || (b = a); })(); console.log(b); } expect: { var b; (function() { function f() { a++; } f(); var c = f(); var a = void 0; c || (b = a); })(); console.log(b); } expect_stdout: "undefined" } cond_branch_1: { options = { collapse_vars: true, sequences: true, unused: true, } input: { function f1(b, c) { var log = console.log; var a = ++c; if (b) b++; log(a, b); } function f2(b, c) { var log = console.log; var a = ++c; b && b++; log(a, b); } function f3(b, c) { var log = console.log; var a = ++c; b ? b++ : b--; log(a, b); } f1(1, 2); f2(3, 4); f3(5, 6); } expect: { function f1(b, c) { if (b) b++; (0, console.log)(++c, b); } function f2(b, c) { b && b++, (0, console.log)(++c, b); } function f3(b, c) { b ? b++ : b--, (0, console.log)(++c, b); } f1(1, 2), f2(3, 4), f3(5, 6); } expect_stdout: [ "3 2", "5 4", "7 6", ] } cond_branch_2: { options = { collapse_vars: true, sequences: true, unused: true, } input: { function f1(b, c) { var log = console.log; var a = ++c; if (b) b += a; log(a, b); } function f2(b, c) { var log = console.log; var a = ++c; b && (b += a); log(a, b); } function f3(b, c) { var log = console.log; var a = ++c; b ? b += a : b--; log(a, b); } f1(1, 2); f2(3, 4); f3(5, 6); } expect: { function f1(b, c) { var a = ++c; if (b) b += a; (0, console.log)(a, b); } function f2(b, c) { var a = ++c; b && (b += a), (0, console.log)(a, b); } function f3(b, c) { var a = ++c; b ? b += a : b--, (0, console.log)(a, b); } f1(1, 2), f2(3, 4), f3(5, 6); } expect_stdout: [ "3 4", "5 8", "7 12", ] } cond_branch_switch: { options = { collapse_vars: true, } input: { var c = 0; if (c = 1 + c, 0) switch (c = 1 + c) { } console.log(c); } expect: { var c = 0; if (c = 1 + c, 0) switch (c = 1 + c) { } console.log(c); } expect_stdout: "1" } issue_2873_1: { options = { collapse_vars: true, } input: { var b = 1, c = 0; do { c++; if (!--b) break; c = 1 + c; } while (0); console.log(b, c); } expect: { var b = 1, c = 0; do { c++; if (!--b) break; c = 1 + c; } while (0); console.log(b, c); } expect_stdout: "0 1" } issue_2873_2: { options = { collapse_vars: true, } input: { var b = 1, c = 0; do { c++; if (!--b) continue; c = 1 + c; } while (0); console.log(b, c); } expect: { var b = 1, c = 0; do { c++; if (!--b) continue; c = 1 + c; } while (0); console.log(b, c); } expect_stdout: "0 1" } issue_2878: { options = { collapse_vars: true, sequences: true, } input: { var c = 0; (function(a, b) { function f2() { if (a) c++; } b = f2(); a = 1; b && b.b; f2(); })(); console.log(c); } expect: { var c = 0; (function(a, b) { function f2() { if (a) c++; } b = f2(), a = 1, b && b.b, f2(); })(), console.log(c); } expect_stdout: "1" } issue_2891_1: { options = { collapse_vars: true, } input: { var a = "PASS", b; try { b = c.p = 0; a = "FAIL"; b(); } catch (e) { } console.log(a); } expect: { var a = "PASS", b; try { b = c.p = 0; a = "FAIL"; b(); } catch (e) { } console.log(a); } expect_stdout: "PASS" } issue_2891_2: { options = { collapse_vars: true, } input: { "use strict"; var a = "PASS", b; try { b = c = 0; a = "FAIL"; b(); } catch (e) { } console.log(a); } expect: { "use strict"; var a = "PASS", b; try { b = c = 0; a = "FAIL"; b(); } catch (e) { } console.log(a); } expect_stdout: true } issue_2908: { options = { collapse_vars: true, } input: { var a = 0, b = 0; function f(c) { if (1 == c) return; a++; if (2 == c) b = a; } f(0); f(2); console.log(b); } expect: { var a = 0, b = 0; function f(c) { if (1 == c) return; a++; if (2 == c) b = a; } f(0); f(2); console.log(b); } expect_stdout: "2" } issue_2914_1: { options = { collapse_vars: true, } input: { function read(input) { var i = 0; var e = 0; var t = 0; while (e < 32) { var n = input[i++]; t |= (127 & n) << e; if (0 === (128 & n)) return t; e += 7; } } console.log(read([129])); } expect: { function read(input) { var i = 0; var e = 0; var t = 0; while (e < 32) { var n = input[i++]; t |= (127 & n) << e; if (0 === (128 & n)) return t; e += 7; } } console.log(read([129])); } expect_stdout: "1" } issue_2914_2: { options = { collapse_vars: true, } input: { function read(input) { var i = 0; var e = 0; var t = 0; while (e < 32) { var n = input[i++]; t = (127 & n) << e; if (0 === (128 & n)) return t; e += 7; } } console.log(read([129])); } expect: { function read(input) { var i = 0; var e = 0; var t = 0; while (e < 32) { var n = input[i++]; if (0 === (128 & n)) return t = (127 & n) << e; e += 7; } } console.log(read([129])); } expect_stdout: "0" } issue_805: { options = { collapse_vars: true, pure_getters: "strict", reduce_vars: true, } input: { function f() { function Foo(){} Foo.prototype = {}; Foo.prototype.bar = 42; return Foo; } } expect: { function f() { function Foo(){} (Foo.prototype = {}).bar = 42; return Foo; } } } issue_2931: { options = { collapse_vars: true, unused: true, } input: { console.log(function() { var a = function() { return; }(); return a; }()); } expect: { console.log(function() { return function() { return; }(); }()); } expect_stdout: "undefined" } issue_2954_1: { options = { collapse_vars: true, } input: { var a = "PASS", b; try { do { b = function() { throw 0; }(); a = "FAIL"; b && b.c; } while (0); } catch (e) { } console.log(a); } expect: { var a = "PASS", b; try { do { b = function() { throw 0; }(); a = "FAIL"; b && b.c; } while (0); } catch (e) { } console.log(a); } expect_stdout: "PASS" } issue_2954_2: { options = { collapse_vars: true, } input: { var a = "FAIL_1", b; try { throw 0; } catch (e) { do { b = function() { throw new Error("PASS"); }(); a = "FAIL_2"; b && b.c; } while (0); } console.log(a); } expect: { var a = "FAIL_1", b; try { throw 0; } catch (e) { do { a = "FAIL_2"; (b = function() { throw new Error("PASS"); }()) && b.c; } while (0); } console.log(a); } expect_stdout: Error("PASS") } issue_2954_3: { options = { collapse_vars: true, } input: { var a = "FAIL_1", b; try { } finally { do { b = function() { throw new Error("PASS"); }(); a = "FAIL_2"; b && b.c; } while (0); } console.log(a); } expect: { var a = "FAIL_1", b; try { } finally { do { a = "FAIL_2"; (b = function() { throw new Error("PASS"); }()) && b.c; } while (0); } console.log(a); } expect_stdout: Error("PASS") } collapse_rhs_conditional_1: { options = { collapse_vars: true, } input: { var a = "PASS", b = "FAIL"; b = a; "function" == typeof f && f(a); console.log(a, b); } expect: { var a = "PASS", b = "FAIL"; b = a; "function" == typeof f && f(a); console.log(a, b); } expect_stdout: "PASS PASS" } collapse_rhs_conditional_2: { options = { collapse_vars: true, } input: { var a = "FAIL", b; while ((a = "PASS", --b) && "PASS" == b); console.log(a, b); } expect: { var a = "FAIL", b; while ((a = "PASS", --b) && "PASS" == b); console.log(a, b); } expect_stdout: "PASS NaN" } collapse_rhs_lhs_1: { options = { collapse_vars: true, } input: { var c = 0; new function() { this[c++] = 1; c += 1; }(); console.log(c); } expect: { var c = 0; new function() { this[c++] = 1; c += 1; }(); console.log(c); } expect_stdout: "2" } collapse_rhs_lhs_2: { options = { collapse_vars: true, } input: { var b = 1; (function f(f) { f = b; f[b] = 0; })(); console.log("PASS"); } expect: { var b = 1; (function f(f) { f = b; f[b] = 0; })(); console.log("PASS"); } expect_stdout: "PASS" } collapse_rhs_loop: { options = { collapse_vars: true, } input: { var s; s = "PASS"; for (var m, r = /(.*)<\/tpl>/; m = s.match(r);) s = s.replace(m[0], m[1]); console.log(s); } expect: { var s; s = "PASS"; for (var m, r = /(.*)<\/tpl>/; m = s.match(r);) s = s.replace(m[0], m[1]); console.log(s); } expect_stdout: "PASS" } collapse_rhs_side_effects: { options = { collapse_vars: true, } input: { var a = 1, c = 0; new function f() { this[a-- && f()] = 1; c += 1; }(); console.log(c); } expect: { var a = 1, c = 0; new function f() { this[a-- && f()] = 1; c += 1; }(); console.log(c); } expect_stdout: "2" } collapse_rhs_vardef: { options = { collapse_vars: true, } input: { var a, b = 1; a = --b + function c() { var b; c[--b] = 1; }(); b |= a; console.log(a, b); } expect: { var a, b = 1; a = --b + function c() { var b; c[--b] = 1; }(); b |= a; console.log(a, b); } expect_stdout: "NaN 0" } collapse_rhs_array: { options = { collapse_vars: true, } input: { var a, b; function f() { a = []; b = []; return []; } var c = f(); console.log(a === b, b === c, c === a); } expect: { var a, b; function f() { a = []; b = []; return []; } var c = f(); console.log(a === b, b === c, c === a); } expect_stdout: "false false false" } collapse_rhs_boolean_1: { options = { collapse_vars: true, } input: { var a, b; function f() { a = !0; b = !0; return !0; } var c = f(); console.log(a === b, b === c, c === a); } expect: { var a, b; function f() { return b = a = !0; } var c = f(); console.log(a === b, b === c, c === a); } expect_stdout: "true true true" } collapse_rhs_boolean_2: { options = { collapse_vars: true, } input: { var a; (function f1() { a = function() {}; if (/foo/) console.log(typeof a); })(); console.log(function f2() { a = []; return !1; }()); } expect: { var a; (function f1() { if (a = function() {}) console.log(typeof a); })(); console.log(function f2() { return !(a = []); }()); } expect_stdout: [ "function", "false", ] } collapse_rhs_boolean_3: { options = { booleans: true, collapse_vars: true, conditionals: true, } input: { var a, f, g, h, i, n, s, t, x, y; if (x()) { n = a; } else if (y()) { n = f(); } else if (s) { i = false; n = g(true); } else if (t) { i = false; n = h(true); } else { n = []; } } expect: { var a, f, g, h, i, n, s, t, x, y; n = x() ? a : y() ? f() : s ? g(!(i = !1)) : t ? h(!(i = !1)) : []; } } collapse_rhs_function: { options = { collapse_vars: true, } input: { var a, b; function f() { a = function() {}; b = function() {}; return function() {}; } var c = f(); console.log(a === b, b === c, c === a); } expect: { var a, b; function f() { a = function() {}; b = function() {}; return function() {}; } var c = f(); console.log(a === b, b === c, c === a); } expect_stdout: "false false false" } collapse_rhs_number: { options = { collapse_vars: true, } input: { var a, b; function f() { a = 42; b = 42; return 42; } var c = f(); console.log(a === b, b === c, c === a); } expect: { var a, b; function f() { return b = a = 42; } var c = f(); console.log(a === b, b === c, c === a); } expect_stdout: "true true true" } collapse_rhs_object: { options = { collapse_vars: true, } input: { var a, b; function f() { a = {}; b = {}; return {}; } var c = f(); console.log(a === b, b === c, c === a); } expect: { var a, b; function f() { a = {}; b = {}; return {}; } var c = f(); console.log(a === b, b === c, c === a); } expect_stdout: "false false false" } collapse_rhs_regexp: { options = { collapse_vars: true, } input: { var a, b; function f() { a = /bar/; b = /bar/; return /bar/; } var c = f(); console.log(a === b, b === c, c === a); } expect: { var a, b; function f() { a = /bar/; b = /bar/; return /bar/; } var c = f(); console.log(a === b, b === c, c === a); } expect_stdout: "false false false" } collapse_rhs_string: { options = { collapse_vars: true, } input: { var a, b; function f() { a = "foo"; b = "foo"; return "foo"; } var c = f(); console.log(a === b, b === c, c === a); } expect: { var a, b; function f() { return b = a = "foo"; } var c = f(); console.log(a === b, b === c, c === a); } expect_stdout: "true true true" } collapse_rhs_var: { options = { collapse_vars: true, } input: { var a, b; function f() { a = f; b = f; return f; } var c = f(); console.log(a === b, b === c, c === a); } expect: { var a, b; function f() { return b = a = f; } var c = f(); console.log(a === b, b === c, c === a); } expect_stdout: "true true true" } collapse_rhs_this: { options = { collapse_vars: true, } input: { var a, b; function f() { a = this; b = this; return this; } var c = f(); console.log(a === b, b === c, c === a); } expect: { var a, b; function f() { return b = a = this; } var c = f(); console.log(a === b, b === c, c === a); } expect_stdout: "true true true" } collapse_rhs_undefined: { options = { collapse_vars: true, } input: { var a, b; function f() { a = void 0; b = void 0; return void 0; } var c = f(); console.log(a === b, b === c, c === a); } expect: { var a, b; function f() { b = a = void 0; return; } var c = f(); console.log(a === b, b === c, c === a); } expect_stdout: "true true true" } issue_2974: { options = { booleans: true, collapse_vars: true, evaluate: true, loops: true, passes: 2, pure_getters: "strict", reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { var c = 0; (function f(b) { var a = 2; do { b && b[b]; b && (b.null = -4); c++; } while (b.null && --a > 0); })(true); console.log(c); } expect: { var c = 0; (function(b) { var a = 2; for (; b.null = -4, c++, b.null && --a > 0;); })(!0), console.log(c); } expect_stdout: "1" } issue_3032: { options = { collapse_vars: true, pure_getters: true, } input: { console.log({ f: function() { this.a = 42; return [ this.a, !1 ]; } }.f()[0]); } expect: { console.log({ f: function() { this.a = 42; return [ this.a, !1 ]; } }.f()[0]); } expect_stdout: "42" } issue_3096: { options = { collapse_vars: true, } input: { console.log(function() { var ar = ["a", "b"]; var first = ar.pop(); return ar + "" + first; }()); } expect: { console.log(function() { var ar = ["a", "b"]; var first = ar.pop(); return ar + "" + first; }()); } expect_stdout: "ab" } issue_3215_1: { options = { collapse_vars: true, evaluate: true, ie8: false, inline: true, passes: 2, side_effects: true, unused: true, } input: { console.log(function a() { var a = 42; return typeof a; }()); } expect: { console.log("number"); } expect_stdout: "number" } issue_3215_2: { options = { collapse_vars: true, evaluate: true, ie8: true, inline: true, passes: 2, side_effects: true, unused: true, } input: { console.log(function a() { var a = 42; return typeof a; }()); } expect: { console.log(function a() { var a = 42; return typeof a; }()); } expect_stdout: "number" } issue_3215_3: { options = { collapse_vars: true, evaluate: true, ie8: false, inline: true, passes: 2, side_effects: true, unused: true, } input: { console.log(function() { var a = 42; (function a() {}); return typeof a; }()); } expect: { console.log("number"); } expect_stdout: "number" } issue_3215_4: { options = { collapse_vars: true, evaluate: true, ie8: true, inline: true, passes: 2, side_effects: true, unused: true, } input: { console.log(function() { var a = 42; (function a() {}); return typeof a; }()); } expect: { console.log(function() { var a = 42; (function a() {}); return typeof a; }()); } expect_stdout: "number" } issue_3238_1: { options = { collapse_vars: true, unsafe: true, } input: { function f(a) { var b, c; if (a) { b = Object.create(null); c = Object.create(null); } return b === c; } console.log(f(0), f(1)); } expect: { function f(a) { var b, c; if (a) { b = Object.create(null); c = Object.create(null); } return b === c; } console.log(f(0), f(1)); } expect_stdout: "true false" } issue_3238_2: { options = { collapse_vars: true, unsafe: true, } input: { function f(a) { var b, c; if (a) { b = Error(); c = Error(); } return b === c; } console.log(f(0), f(1)); } expect: { function f(a) { var b, c; if (a) { b = Error(); c = Error(); } return b === c; } console.log(f(0), f(1)); } expect_stdout: "true false" } issue_3238_3: { options = { collapse_vars: true, unsafe: true, } input: { function f(a) { var b, c; if (a) { b = new Date(); c = new Date(); } return b === c; } console.log(f(0), f(1)); } expect: { function f(a) { var b, c; if (a) { b = new Date(); c = new Date(); } return b === c; } console.log(f(0), f(1)); } expect_stdout: "true false" } issue_3238_4: { options = { collapse_vars: true, unsafe: true, } input: { function f(a) { var b, c; if (a) { b = a && {}; c = a && {}; } return b === c; } console.log(f(0), f(1)); } expect: { function f(a) { var b, c; if (a) { b = a && {}; c = a && {}; } return b === c; } console.log(f(0), f(1)); } expect_stdout: "true false" } issue_3238_5: { options = { collapse_vars: true, unsafe: true, } input: { function f(a) { var b, c; if (a) { b = a ? [] : 42; c = a ? [] : 42; } return b === c; } console.log(f(0), f(1)); } expect: { function f(a) { var b, c; if (a) { b = a ? [] : 42; c = a ? [] : 42; } return b === c; } console.log(f(0), f(1)); } expect_stdout: "true false" } issue_3238_6: { options = { collapse_vars: true, unsafe: true, } input: { function f(a) { var b, c; if (a) { b = a && 0 || []; c = a && 0 || []; } return b === c; } console.log(f(0), f(1)); } expect: { function f(a) { var b, c; if (a) { b = a && 0 || []; c = a && 0 || []; } return b === c; } console.log(f(0), f(1)); } expect_stdout: "true false" } issue_3247: { options = { collapse_vars: true, } input: { function f(o) { console.log(o.p); } var a; a = Object({ p: "PASS" }); a.q = true; f(a, true); } expect: { function f(o) { console.log(o.p); } var a; (a = Object({ p: "PASS" })).q = true; f(a, true); } expect_stdout: "PASS" } issue_3305: { options = { collapse_vars: true, conditionals: true, sequences: true, } input: { function calc(a) { var x, w; if (a) { x = a; w = 1; } else { x = 1; w = 0; } return add(x, w); } function add(x, w) { return x + w; } console.log(calc(41)); } expect: { function calc(a) { var x, w; return w = a ? (x = a, 1) : (x = 1, 0), add(x, w); } function add(x, w) { return x + w; } console.log(calc(41)); } expect_stdout: "42" } issue_3314: { options = { collapse_vars: true, } input: { function test(a, b) { console.log(a, b); } var a = "FAIL", b; b = a = "PASS"; test(a, b); } expect: { function test(a, b) { console.log(a, b); } var a = "FAIL", b; b = a = "PASS"; test(a, b); } expect_stdout: "PASS PASS" } issue_3327: { options = { collapse_vars: true, conditionals: true, sequences: true, } input: { var a, b, l = ["PASS", 42]; if (l.length === 1) { a = l[0].a; b = l[0].b; } else { a = l[0]; b = l[1]; } function echo(a, b) { console.log(a, b); } echo(a, b); } expect: { var a, b, l = ["PASS", 42]; function echo(a, b) { console.log(a, b); } b = 1 === l.length ? (a = l[0].a, l[0].b) : (a = l[0], l[1]), echo(a,b); } expect_stdout: "PASS 42" } assign_left: { options = { collapse_vars: true, } input: { console.log(function(a, b) { (b = a, b.p).q = "PASS"; return a.p.q; }({p: {}})); } expect: { console.log(function(a, b) { (b = a).p.q = "PASS"; return a.p.q; }({p: {}})); } expect_stdout: "PASS" } sub_property: { options = { collapse_vars: true, } input: { console.log(function(a, b) { return a[(b = a, b.length - 1)]; }([ "FAIL", "PASS" ])); } expect: { console.log(function(a, b) { return a[(b = a).length - 1]; }([ "FAIL", "PASS" ])); } expect_stdout: "PASS" } assign_undeclared: { options = { collapse_vars: true, toplevel: true, unused: true, } input: { var A = (console.log(42), function() {}); B = new A(); console.log(typeof B); } expect: { B = new (console.log(42), function() {})(); console.log(typeof B); } expect_stdout: [ "42", "object", ] } Infinity_assignment: { options = { collapse_vars: true, pure_getters: "strict", unsafe: true, } input: { var Infinity; Infinity = 42; console.log(Infinity); } expect: { var Infinity; Infinity = 42; console.log(Infinity); } expect_stdout: true } issue_3439_1: { options = { collapse_vars: true, unused: true, } input: { console.log(typeof function(a) { function a() {} return a; }(42)); } expect: { console.log(typeof function(a) { function a() {} return a; }(42)); } expect_stdout: "function" } issue_3439_2: { options = { collapse_vars: true, unused: true, } input: { console.log(typeof function() { var a = 42; function a() {} return a; }()); } expect: { console.log(typeof function() { return 42; }()); } expect_stdout: "number" } UglifyJS2-3.6.3/test/compress/comparisons.js000066400000000000000000000264341355252637300210320ustar00rootroot00000000000000comparisons: { options = { comparisons: true, } input: { var obj1, obj2; var result1 = obj1 <= obj2; var result2 = obj1 < obj2; var result3 = obj1 >= obj2; var result4 = obj1 > obj2; } expect: { var obj1, obj2; var result1 = obj1 <= obj2; var result2 = obj1 < obj2; var result3 = obj2 <= obj1; var result4 = obj2 < obj1; } } unsafe_comps: { options = { comparisons: true, conditionals: true, unsafe_comps: true, } input: { var obj1, obj2; obj1 <= obj2 ? f1() : g1(); obj1 < obj2 ? f2() : g2(); obj1 >= obj2 ? f3() : g3(); obj1 > obj2 ? f4() : g4(); } expect: { var obj1, obj2; obj2 < obj1 ? g1() : f1(); obj1 < obj2 ? f2() : g2(); obj1 < obj2 ? g3() : f3(); obj2 < obj1 ? f4() : g4(); } } dont_change_in_or_instanceof_expressions: { input: { 1 in 1; null in null; 1 instanceof 1; null instanceof null; } expect: { 1 in 1; null in null; 1 instanceof 1; null instanceof null; } } 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_funcs: 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" } issue_2857_1: { options = { comparisons: true, } input: { function f1(a) { a === undefined || a === null; a === undefined || a !== null; a !== undefined || a === null; a !== undefined || a !== null; a === undefined && a === null; a === undefined && a !== null; a !== undefined && a === null; a !== undefined && a !== null; } function f2(a) { a === null || a === undefined; a === null || a !== undefined; a !== null || a === undefined; a !== null || a !== undefined; a === null && a === undefined; a === null && a !== undefined; a !== null && a === undefined; a !== null && a !== undefined; } } expect: { function f1(a) { null == a; void 0 === a || null !== a; void 0 !== a || null === a; void 0 !== a || null !== a; void 0 === a && null === a; void 0 === a && null !== a; void 0 !== a && null === a; null != a; } function f2(a) { null == a; null === a || void 0 !== a; null !== a || void 0 === a; null !== a || void 0 !== a; null === a && void 0 === a; null === a && void 0 !== a; null !== a && void 0 === a; null != a; } } } issue_2857_2: { options = { comparisons: true, } input: { function f(a, p) { a === undefined || a === null || p; a === undefined || a !== null || p; a !== undefined || a === null || p; a !== undefined || a !== null || p; a === undefined && a === null || p; a === undefined && a !== null || p; a !== undefined && a === null || p; a !== undefined && a !== null || p; } } expect: { function f(a, p) { null == a || p; void 0 === a || null !== a || p; void 0 !== a || null === a || p; void 0 !== a || null !== a || p; void 0 === a && null === a || p; void 0 === a && null !== a || p; void 0 !== a && null === a || p; null != a || p; } } } issue_2857_3: { options = { comparisons: true, } input: { function f(a, p) { a === undefined || a === null && p; a === undefined || a !== null && p; a !== undefined || a === null && p; a !== undefined || a !== null && p; a === undefined && a === null && p; a === undefined && a !== null && p; a !== undefined && a === null && p; a !== undefined && a !== null && p; } } expect: { function f(a, p) { void 0 === a || null === a && p; void 0 === a || null !== a && p; void 0 !== a || null === a && p; void 0 !== a || null !== a && p; void 0 === a && null === a && p; void 0 === a && null !== a && p; void 0 !== a && null === a && p; null != a && p; } } } issue_2857_4: { options = { comparisons: true, } input: { function f(a, p) { p || a === undefined || a === null; p || a === undefined || a !== null; p || a !== undefined || a === null; p || a !== undefined || a !== null; p || a === undefined && a === null; p || a === undefined && a !== null; p || a !== undefined && a === null; p || a !== undefined && a !== null; } } expect: { function f(a, p) { p || null == a; p || void 0 === a || null !== a; p || void 0 !== a || null === a; p || void 0 !== a || null !== a; p || void 0 === a && null === a; p || void 0 === a && null !== a; p || void 0 !== a && null === a; p || null != a; } } } issue_2857_5: { options = { comparisons: true, } input: { function f(a, p) { p && a === undefined || a === null; p && a === undefined || a !== null; p && a !== undefined || a === null; p && a !== undefined || a !== null; p && a === undefined && a === null; p && a === undefined && a !== null; p && a !== undefined && a === null; p && a !== undefined && a !== null; } } expect: { function f(a, p) { p && void 0 === a || null === a; p && void 0 === a || null !== a; p && void 0 !== a || null === a; p && void 0 !== a || null !== a; p && void 0 === a && null === a; p && void 0 === a && null !== a; p && void 0 !== a && null === a; p && null != a; } } } issue_2857_6: { options = { comparisons: true, pure_getters: "strict", reduce_vars: true, } input: { function f(a) { if (({}).b === undefined || {}.b === null) return a.b !== undefined && a.b !== null; } console.log(f({ a: [ null ], get b() { return this.a.shift(); } })); } expect: { function f(a) { if (null == {}.b) return void 0 !== a.b && null !== a.b; } console.log(f({ a: [ null ], get b() { return this.a.shift(); } })); } expect_stdout: "true" } is_boolean_unsafe: { options = { comparisons: true, unsafe: true, } input: { console.log(/foo/.test("bar") === [].isPrototypeOf({})); } expect: { console.log(/foo/.test("bar") == [].isPrototypeOf({})); } expect_stdout: "true" } is_number_unsafe: { options = { comparisons: true, unsafe: true, } input: { console.log(Math.acos(42) !== "foo".charCodeAt(4)); } expect: { console.log(Math.acos(42) != "foo".charCodeAt(4)); } expect_stdout: "true" } is_boolean_var: { options = { comparisons: true, reduce_vars: true, } input: { console.log(function(a, b) { for (var i = 0, c = !b; i < a.length; i++) if (!a[i] === c) return i; }([ false, true ], 42)); } expect: { console.log(function(a, b) { for (var i = 0, c = !b; i < a.length; i++) if (!a[i] == c) return i; }([ false, true ], 42)); } expect_stdout: "1" } is_defined: { options = { comparisons: true, } input: { console.log(function a() { return void 0 === a; }()); } expect: { console.log(function a() { return a, false; }()); } expect_stdout: "false" expect_warnings: [ "WARN: Expression always defined [test/compress/comparisons.js:2,19]", ] } unsafe_indexOf: { options = { booleans: true, comparisons: true, unsafe: true, } input: { var a = Object.keys({ foo: 42 }); if (a.indexOf("bar") < 0) console.log("PASS"); if (0 > a.indexOf("bar")) console.log("PASS"); if (a.indexOf("foo") >= 0) console.log("PASS"); if (0 <= a.indexOf("foo")) console.log("PASS"); if (a.indexOf("foo") > -1) console.log("PASS"); if (-1 < a.indexOf("foo")) console.log("PASS"); if (a.indexOf("bar") == -1) console.log("PASS"); if (-1 == a.indexOf("bar")) console.log("PASS"); if (a.indexOf("bar") === -1) console.log("PASS"); if (-1 === a.indexOf("bar")) console.log("PASS"); if (a.indexOf("foo") != -1) console.log("PASS"); if (-1 != a.indexOf("foo")) console.log("PASS"); if (a.indexOf("foo") !== -1) console.log("PASS"); if (-1 !== a.indexOf("foo")) console.log("PASS"); } expect: { var a = Object.keys({ foo: 42 }); if (!~a.indexOf("bar")) console.log("PASS"); if (!~a.indexOf("bar")) console.log("PASS"); if (~a.indexOf("foo")) console.log("PASS"); if (~a.indexOf("foo")) console.log("PASS"); if (~a.indexOf("foo")) console.log("PASS"); if (~a.indexOf("foo")) console.log("PASS"); if (!~a.indexOf("bar")) console.log("PASS"); if (!~a.indexOf("bar")) console.log("PASS"); if (!~a.indexOf("bar")) console.log("PASS"); if (!~a.indexOf("bar")) console.log("PASS"); if (~a.indexOf("foo")) console.log("PASS"); if (~a.indexOf("foo")) console.log("PASS"); if (~a.indexOf("foo")) console.log("PASS"); if (~a.indexOf("foo")) console.log("PASS"); } expect_stdout: [ "PASS", "PASS", "PASS", "PASS", "PASS", "PASS", "PASS", "PASS", "PASS", "PASS", "PASS", "PASS", "PASS", "PASS", ] } issue_3413: { options = { comparisons: true, evaluate: true, side_effects: true, } input: { var b; void 0 !== ("" < b || void 0) || console.log("PASS"); } expect: { var b; void 0 !== ("" < b || void 0) || console.log("PASS"); } expect_stdout: "PASS" } UglifyJS2-3.6.3/test/compress/concat-strings.js000066400000000000000000000123351355252637300214260ustar00rootroot00000000000000concat_1: { options = { evaluate: true, } input: { var a = "foo" + "bar" + x() + "moo" + "foo" + y() + "x" + "y" + "z" + q(); var b = "foo" + 1 + x() + 2 + "boo"; var c = 1 + x() + 2 + "boo"; // this CAN'T safely be shortened to 1 + x() + "5boo" var d = 1 + x() + 2 + 3 + "boo"; var e = 1 + x() + 2 + "X" + 3 + "boo"; // be careful with concatentation with "\0" with octal-looking strings. var f = "\0" + 360 + "\0" + 8 + "\0"; } expect: { var a = "foobar" + x() + "moofoo" + y() + "xyz" + q(); var b = "foo1" + x() + "2boo"; var c = 1 + x() + 2 + "boo"; var d = 1 + x() + 2 + 3 + "boo"; var e = 1 + x() + 2 + "X3boo"; var f = "\x00360\x008\0"; } } concat_2: { options = {} input: { console.log( 1 + (2 + 3), 1 + (2 + "3"), 1 + ("2" + 3), 1 + ("2" + "3"), "1" + (2 + 3), "1" + (2 + "3"), "1" + ("2" + 3), "1" + ("2" + "3") ); } expect: { console.log( 1 + (2 + 3), 1 + (2 + "3"), 1 + "2" + 3, 1 + "2" + "3", "1" + (2 + 3), "1" + 2 + "3", "1" + "2" + 3, "1" + "2" + "3" ); } expect_stdout: true } concat_3: { options = {} input: { console.log( 1 + 2 + (3 + 4 + 5), 1 + 2 + (3 + 4 + "5"), 1 + 2 + (3 + "4" + 5), 1 + 2 + (3 + "4" + "5"), 1 + 2 + ("3" + 4 + 5), 1 + 2 + ("3" + 4 + "5"), 1 + 2 + ("3" + "4" + 5), 1 + 2 + ("3" + "4" + "5") ); } expect: { console.log( 1 + 2 + (3 + 4 + 5), 1 + 2 + (3 + 4 + "5"), 1 + 2 + (3 + "4") + 5, 1 + 2 + (3 + "4") + "5", 1 + 2 + "3" + 4 + 5, 1 + 2 + "3" + 4 + "5", 1 + 2 + "3" + "4" + 5, 1 + 2 + "3" + "4" + "5" ); } expect_stdout: true } concat_4: { options = {} input: { console.log( 1 + "2" + (3 + 4 + 5), 1 + "2" + (3 + 4 + "5"), 1 + "2" + (3 + "4" + 5), 1 + "2" + (3 + "4" + "5"), 1 + "2" + ("3" + 4 + 5), 1 + "2" + ("3" + 4 + "5"), 1 + "2" + ("3" + "4" + 5), 1 + "2" + ("3" + "4" + "5") ); } expect: { console.log( 1 + "2" + (3 + 4 + 5), 1 + "2" + (3 + 4) + "5", 1 + "2" + 3 + "4" + 5, 1 + "2" + 3 + "4" + "5", 1 + "2" + "3" + 4 + 5, 1 + "2" + "3" + 4 + "5", 1 + "2" + "3" + "4" + 5, 1 + "2" + "3" + "4" + "5" ); } expect_stdout: true } concat_5: { options = {} input: { console.log( "1" + 2 + (3 + 4 + 5), "1" + 2 + (3 + 4 + "5"), "1" + 2 + (3 + "4" + 5), "1" + 2 + (3 + "4" + "5"), "1" + 2 + ("3" + 4 + 5), "1" + 2 + ("3" + 4 + "5"), "1" + 2 + ("3" + "4" + 5), "1" + 2 + ("3" + "4" + "5") ); } expect: { console.log( "1" + 2 + (3 + 4 + 5), "1" + 2 + (3 + 4) + "5", "1" + 2 + 3 + "4" + 5, "1" + 2 + 3 + "4" + "5", "1" + 2 + "3" + 4 + 5, "1" + 2 + "3" + 4 + "5", "1" + 2 + "3" + "4" + 5, "1" + 2 + "3" + "4" + "5" ); } expect_stdout: true } concat_6: { options = {} input: { console.log( "1" + "2" + (3 + 4 + 5), "1" + "2" + (3 + 4 + "5"), "1" + "2" + (3 + "4" + 5), "1" + "2" + (3 + "4" + "5"), "1" + "2" + ("3" + 4 + 5), "1" + "2" + ("3" + 4 + "5"), "1" + "2" + ("3" + "4" + 5), "1" + "2" + ("3" + "4" + "5") ); } expect: { console.log( "1" + "2" + (3 + 4 + 5), "1" + "2" + (3 + 4) + "5", "1" + "2" + 3 + "4" + 5, "1" + "2" + 3 + "4" + "5", "1" + "2" + "3" + 4 + 5, "1" + "2" + "3" + 4 + "5", "1" + "2" + "3" + "4" + 5, "1" + "2" + "3" + "4" + "5" ); } expect_stdout: true } concat_7: { input: { console.log( "" + 1, "" + "1", "" + 1 + 2, "" + 1 + "2", "" + "1" + 2, "" + "1" + "2", "" + (x += "foo") ); } expect: { console.log( "" + 1, "1", "" + 1 + 2, 1 + "2", "1" + 2, "1" + "2", x += "foo" ); } expect_stdout: true } concat_8: { input: { console.log( 1 + "", "1" + "", 1 + 2 + "", 1 + "2" + "", "1" + 2 + "", "1" + "2" + "", (x += "foo") + "" ); } expect: { console.log( 1 + "", "1", 1 + 2 + "", 1 + "2", "1" + 2, "1" + "2", x += "foo" ); } expect_stdout: true } UglifyJS2-3.6.3/test/compress/conditionals.js000066400000000000000000000763051355252637300211650ustar00rootroot00000000000000ifs_1: { options = { conditionals: true, } input: { if (foo) bar(); if (!foo); else bar(); if (foo); else bar(); if (foo); else; } expect: { foo&&bar(); foo&&bar(); foo||bar(); foo; } } ifs_2: { options = { conditionals: true, } input: { if (foo) { x(); } else if (bar) { y(); } else if (baz) { z(); } if (foo) { x(); } else if (bar) { y(); } else if (baz) { z(); } else { t(); } } expect: { foo ? x() : bar ? y() : baz && z(); foo ? x() : bar ? y() : baz ? z() : t(); } } ifs_3_should_warn: { options = { booleans: true, conditionals: true, dead_code: true, evaluate: true, side_effects: true, } input: { var x, y; if (x && !(x + "1") && y) { // 1 var qq; foo(); } else { bar(); } if (x || !!(x + "1") || y) { // 2 foo(); } else { var jj; bar(); } } expect: { var x, y; var qq; bar(); // 1 var jj; foo(); // 2 } } ifs_4: { options = { conditionals: true, } input: { if (foo && bar) { x(foo)[10].bar.baz = something(); } else x(foo)[10].bar.baz = something_else(); } expect: { foo && bar ? x(foo)[10].bar.baz = something() : x(foo)[10].bar.baz = something_else(); } } ifs_5: { options = { comparisons: true, conditionals: true, if_return: true, } input: { function f() { if (foo) return; bar(); baz(); } function g() { if (foo) return; if (bar) return; if (baz) return; if (baa) return; a(); b(); } } expect: { function f() { if (!foo) { bar(); baz(); } } function g() { if (!(foo || bar || baz || baa)) { a(); b(); } } } } ifs_6: { options = { comparisons: true, conditionals: true, } input: { var x, y; if (!foo && !bar && !baz && !boo) { x = 10; } else { x = 20; } if (y) { x[foo] = 10; } else { x[foo] = 20; } if (foo) { x[bar] = 10; } else { x[bar] = 20; } } expect: { var x, y; x = foo || bar || baz || boo ? 20 : 10; x[foo] = y ? 10 : 20; foo ? x[bar] = 10 : x[bar] = 20; } } cond_1: { options = { conditionals: true, } input: { function foo(do_something, some_condition) { if (some_condition) { do_something(x); } else { do_something(y); } if (some_condition) { side_effects(x); } else { side_effects(y); } } } expect: { function foo(do_something, some_condition) { do_something(some_condition ? x : y); some_condition ? side_effects(x) : side_effects(y); } } } cond_2: { options = { conditionals: true, } input: { function foo(x, FooBar, some_condition) { if (some_condition) { x = new FooBar(1); } else { x = new FooBar(2); } } } expect: { function foo(x, FooBar, some_condition) { x = new FooBar(some_condition ? 1 : 2); } } } cond_3: { options = { conditionals: true, } input: { var FooBar; if (some_condition()) { new FooBar(1); } else { FooBar(2); } } expect: { var FooBar; some_condition() ? new FooBar(1) : FooBar(2); } } cond_4: { options = { conditionals: true, } input: { var do_something; if (some_condition()) { do_something(); } else { do_something(); } if (some_condition()) { side_effects(); } else { side_effects(); } } expect: { var do_something; some_condition(), do_something(); some_condition(), side_effects(); } } cond_5: { options = { conditionals: true, } input: { if (some_condition()) { if (some_other_condition()) { do_something(); } else { alternate(); } } else { alternate(); } if (some_condition()) { if (some_other_condition()) { do_something(); } } } expect: { some_condition() && some_other_condition() ? do_something() : alternate(); some_condition() && some_other_condition() && do_something(); } } cond_7: { options = { conditionals: true, evaluate: true, side_effects: true, } input: { var x, y, z, a, b; // compress these if (y) { x = 1+1; } else { x = 2; } if (y) { x = 1+1; } else if (z) { x = 2; } else { x = 3-1; } x = y ? 'foo' : 'fo'+'o'; x = y ? 'foo' : y ? 'foo' : 'fo'+'o'; // Compress conditions that have side effects if (condition()) { x = 10+10; } else { x = 20; } if (z) { x = 'fuji'; } else if (condition()) { x = 'fu'+'ji'; } else { x = 'fuji'; } x = condition() ? 'foobar' : 'foo'+'bar'; // don't compress these x = y ? a : b; x = y ? 'foo' : 'fo'; } expect: { var x, y, z, a, b; x = 2; x = 2; x = 'foo'; x = 'foo'; x = (condition(), 20); x = (z || condition(), 'fuji'); x = (condition(), 'foobar'); x = y ? a : b; x = y ? 'foo' : 'fo'; } } cond_7_1: { options = { conditionals: true, evaluate: true, } input: { var x; // access to global should be assumed to have side effects if (y) { x = 1+1; } else { x = 2; } } expect: { var x; x = (y, 2); } } cond_8: { options = { booleans: false, conditionals: true, evaluate: true, } input: { var a; // compress these a = condition ? true : false; a = !condition ? true : false; a = condition() ? true : false; a = condition ? !0 : !1; a = !condition ? !null : !2; a = condition() ? !0 : !-3.5; if (condition) { a = true; } else { a = false; } if (condition) { a = !0; } else { a = !1; } a = condition ? false : true; a = !condition ? false : true; a = condition() ? false : true; a = condition ? !3 : !0; a = !condition ? !2 : !0; a = condition() ? !1 : !0; if (condition) { a = false; } else { a = true; } if (condition) { a = !1; } else { a = !0; } // don't compress these a = condition ? 1 : false; a = !condition ? true : 0; a = condition ? 1 : 0; } expect: { var a; a = !!condition; a = !condition; a = !!condition(); a = !!condition; a = !condition; a = !!condition(); a = !!condition; a = !!condition; a = !condition; a = !!condition; a = !condition(); a = !condition; a = !!condition; a = !condition(); a = !condition; a = !condition; a = !!condition && 1; a = !condition || 0; a = condition ? 1 : 0; } } cond_8b: { options = { booleans: true, conditionals: true, evaluate: true, } input: { var a; // compress these a = condition ? true : false; a = !condition ? true : false; a = condition() ? true : false; a = condition ? !0 : !1; a = !condition ? !null : !2; a = condition() ? !0 : !-3.5; if (condition) { a = true; } else { a = false; } if (condition) { a = !0; } else { a = !1; } a = condition ? false : true; a = !condition ? false : true; a = condition() ? false : true; a = condition ? !3 : !0; a = !condition ? !2 : !0; a = condition() ? !1 : !0; if (condition) { a = false; } else { a = true; } if (condition) { a = !1; } else { a = !0; } a = condition ? 1 : false; a = !condition ? true : 0; a = condition ? 1 : 0; } expect: { var a; a = !!condition; a = !condition; a = !!condition(); a = !!condition; a = !condition; a = !!condition(); a = !!condition; a = !!condition; a = !condition; a = !!condition; a = !condition(); a = !condition; a = !!condition; a = !condition(); a = !condition; a = !condition; a = !!condition && 1; a = !condition || 0; a = condition ? 1 : 0; } } cond_8c: { options = { booleans: false, conditionals: true, evaluate: false, } input: { var a; // compress these a = condition ? true : false; a = !condition ? true : false; a = condition() ? true : false; a = condition ? !0 : !1; a = !condition ? !null : !2; a = condition() ? !0 : !-3.5; if (condition) { a = true; } else { a = false; } if (condition) { a = !0; } else { a = !1; } a = condition ? false : true; a = !condition ? false : true; a = condition() ? false : true; a = condition ? !3 : !0; a = !condition ? !2 : !0; a = condition() ? !1 : !0; if (condition) { a = false; } else { a = true; } if (condition) { a = !1; } else { a = !0; } a = condition ? 1 : false; a = !condition ? true : 0; a = condition ? 1 : 0; } expect: { var a; a = !!condition; a = !condition; a = !!condition(); a = !!condition; a = !condition; a = !!condition() || !-3.5; a = !!condition; a = !!condition; a = !condition; a = !!condition; a = !condition(); a = !condition; a = !!condition; a = !condition(); a = !condition; a = !condition; a = !!condition && 1; a = !condition || 0; a = condition ? 1 : 0; } } cond_9: { options = { conditionals: true, } input: { function f(x, y) { g() ? x(1) : x(2); x ? (y || x)() : (y || x)(); x ? y(a, b) : y(d, b, c); x ? y(a, b, c) : y(a, b, c); x ? y(a, b, c) : y(a, b, f); x ? y(a, b, c) : y(a, e, c); x ? y(a, b, c) : y(a, e, f); x ? y(a, b, c) : y(d, b, c); x ? y(a, b, c) : y(d, b, f); x ? y(a, b, c) : y(d, e, c); x ? y(a, b, c) : y(d, e, f); } } expect: { function f(x, y) { g() ? x(1) : x(2); x, (y || x)(); x ? y(a, b) : y(d, b, c); x, y(a, b, c); y(a, b, x ? c : f); y(a, x ? b : e, c); x ? y(a, b, c) : y(a, e, f); y(x ? a : d, b, c); x ? y(a, b, c) : y(d, b, f); x ? y(a, b, c) : y(d, e, c); x ? y(a, b, c) : y(d, e, f); } } } ternary_boolean_consequent: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { function f1() { return a == b ? true : x; } function f2() { return a == b ? false : x; } function f3() { return a < b ? !0 : x; } function f4() { return a < b ? !1 : x; } function f5() { return c ? !0 : x; } function f6() { return c ? false : x; } function f7() { return !c ? true : x; } function f8() { return !c ? !1 : x; } } expect: { function f1() { return a == b || x; } function f2() { return a != b && x; } function f3() { return a < b || x; } function f4() { return !(a < b) && x; } function f5() { return !!c || x; } function f6() { return !c && x; } function f7() { return !c || x; } function f8() { return !!c && x; } } } ternary_boolean_alternative: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { function f1() { return a == b ? x : true; } function f2() { return a == b ? x : false; } function f3() { return a < b ? x : !0; } function f4() { return a < b ? x : !1; } function f5() { return c ? x : true; } function f6() { return c ? x : !1; } function f7() { return !c ? x : !0; } function f8() { return !c ? x : false; } } expect: { function f1() { return a != b || x; } function f2() { return a == b && x; } function f3() { return !(a < b) || x; } function f4() { return a < b && x; } function f5() { return !c || x; } function f6() { return !!c && x; } function f7() { return !!c || x; } function f8() { return !c && x; } } } trivial_boolean_ternary_expressions : { options = { booleans: true, conditionals: true, evaluate: true, side_effects: true, } input: { f('foo' in m ? true : false); f('foo' in m ? false : true); f(g ? true : false); f(foo() ? true : false); f("bar" ? true : false); f(5 ? true : false); f(5.7 ? true : false); f(x - y ? true : false); f(x == y ? true : false); f(x === y ? !0 : !1); f(x < y ? !0 : false); f(x <= y ? true : false); f(x > y ? true : !1); f(x >= y ? !0 : !1); f(g ? false : true); f(foo() ? false : true); f("bar" ? false : true); f(5 ? false : true); f(5.7 ? false : true); f(x - y ? false : true); f(x == y ? !1 : !0); f(x === y ? false : true); f(x < y ? false : true); f(x <= y ? false : !0); f(x > y ? !1 : true); f(x >= y ? !1 : !0); } expect: { f('foo' in m); f(!('foo' in m)); f(!!g); f(!!foo()); f(!0); f(!0); f(!0); f(!!(x - y)); f(x == y); f(x === y); f(x < y); f(x <= y); f(x > y); f(x >= y); f(!g); f(!foo()); f(!1); f(!1); f(!1); f(!(x - y)); f(x != y); f(x !== y); f(!(x < y)); f(!(x <= y)); f(!(x > y)); f(!(x >= y)); } } issue_1154: { options = { booleans: true, conditionals: true, evaluate: true, side_effects: true, } input: { function f1(x) { return x ? -1 : -1; } function f2(x) { return x ? +2 : +2; } function f3(x) { return x ? ~3 : ~3; } function f4(x) { return x ? !4 : !4; } function f5(x) { return x ? void 5 : void 5; } function f6(x) { return x ? typeof 6 : typeof 6; } function g1() { return g() ? -1 : -1; } function g2() { return g() ? +2 : +2; } function g3() { return g() ? ~3 : ~3; } function g4() { return g() ? !4 : !4; } function g5() { return g() ? void 5 : void 5; } function g6() { return g() ? typeof 6 : typeof 6; } } expect: { function f1(x) { return -1; } function f2(x) { return 2; } function f3(x) { return -4; } function f4(x) { return !1; } function f5(x) { return; } function f6(x) { return "number"; } function g1() { return g(), -1; } function g2() { return g(), 2; } function g3() { return g(), -4; } function g4() { return g(), !1; } function g5() { return void g(); } function g6() { return g(), "number"; } } } no_evaluate: { options = { conditionals: true, evaluate: false, side_effects: true, } input: { function f(b) { a = b ? !0 : !0; a = b ? ~1 : ~1; a = b ? -2 : -2; a = b ? +3 : +3; } } expect: { function f(b) { a = !0; a = ~1; a = -2; a = +3; } } } equality_conditionals_false: { options = { conditionals: false, sequences: true, } input: { function f(a, b, c) { console.log( a == (b ? a : a), a == (b ? a : c), a != (b ? a : a), a != (b ? a : c), a === (b ? a : a), a === (b ? a : c), a !== (b ? a : a), a !== (b ? a : c) ); } f(0, 0, 0); f(0, true, 0); f(1, 2, 3); f(1, null, 3); f(NaN); f(NaN, "foo"); } expect: { function f(a, b, c) { console.log( a == (b ? a : a), a == (b ? a : c), a != (b ? a : a), a != (b ? a : c), a === (b ? a : a), a === (b ? a : c), a !== (b ? a : a), a !== (b ? a : c) ); } f(0, 0, 0), f(0, true, 0), f(1, 2, 3), f(1, null, 3), f(NaN), f(NaN, "foo"); } expect_stdout: true } equality_conditionals_true: { options = { conditionals: true, sequences: true, } input: { function f(a, b, c) { console.log( a == (b ? a : a), a == (b ? a : c), a != (b ? a : a), a != (b ? a : c), a === (b ? a : a), a === (b ? a : c), a !== (b ? a : a), a !== (b ? a : c) ); } f(0, 0, 0); f(0, true, 0); f(1, 2, 3); f(1, null, 3); f(NaN); f(NaN, "foo"); } expect: { function f(a, b, c) { console.log( (b, a == a), a == (b ? a : c), (b, a != a), a != (b ? a : c), (b, a === a), a === (b ? a : c), (b, a !== a), a !== (b ? a : c) ); } f(0, 0, 0), f(0, true, 0), f(1, 2, 3), f(1, null, 3), f(NaN), f(NaN, "foo"); } expect_stdout: true } issue_1645_1: { options = { conditionals: true, } input: { var a = 100, b = 10; (b = a) ? a++ + (b += a) ? b += a : b += a : b ^= a; console.log(a, b); } expect: { var a = 100, b = 10; (b = a) ? (a++ + (b += a), b += a) : b ^= a; console.log(a,b); } expect_stdout: true } issue_1645_2: { options = { conditionals: true, } input: { var a = 0; function f() { return a++; } f() ? a += 2 : a += 4; console.log(a); } expect: { var a = 0; function f(){ return a++; } f() ? a += 2 : a += 4; console.log(a); } expect_stdout: true } condition_symbol_matches_consequent: { options = { conditionals: true, } input: { function foo(x, y) { return x ? x : y; } function bar() { return g ? g : h; } var g = 4; var h = 5; console.log(foo(3, null), foo(0, 7), foo(true, false), bar()); } expect: { function foo(x, y) { return x || y; } function bar() { return g || h; } var g = 4; var h = 5; console.log(foo(3, null), foo(0, 7), foo(true, false), bar()); } expect_stdout: "3 7 true 4" } delete_conditional_1: { options = { booleans: true, conditionals: true, evaluate: true, side_effects: true, } input: { console.log(delete (1 ? undefined : x)); console.log(delete (1 ? void 0 : x)); console.log(delete (1 ? Infinity : x)); console.log(delete (1 ? 1 / 0 : x)); console.log(delete (1 ? NaN : x)); console.log(delete (1 ? 0 / 0 : x)); } expect: { console.log(!0); console.log(!0); console.log(!0); console.log(!0); console.log(!0); console.log(!0); } expect_stdout: true } delete_conditional_2: { options = { booleans: true, conditionals: true, evaluate: true, keep_infinity: true, side_effects: true, } input: { console.log(delete (0 ? x : undefined)); console.log(delete (0 ? x : void 0)); console.log(delete (0 ? x : Infinity)); console.log(delete (0 ? x : 1 / 0)); console.log(delete (0 ? x : NaN)); console.log(delete (0 ? x : 0 / 0)); } expect: { console.log(!0); console.log(!0); console.log(!0); console.log(!0); console.log(!0); console.log(!0); } expect_stdout: true } issue_2535_1: { options = { booleans: true, conditionals: true, evaluate: true, passes: 2, side_effects: true, } input: { if (true || x()) y(); if (true && x()) y(); if (x() || true) y(); if (x() && true) y(); if (false || x()) y(); if (false && x()) y(); if (x() || false) y(); if (x() && false) y(); } expect: { y(); x() && y(); (x(), 1) && y(); x() && y(); x() && y(); x() && y(); (x(), 0) && y(); } } issue_2535_2: { options = { booleans: true, conditionals: true, evaluate: true, side_effects: true, } input: { function x() {} function y() { return "foo"; } console.log((x() || true) || y()); console.log((y() || true) || x()); console.log((x() || true) && y()); console.log((y() || true) && x()); console.log((x() && true) || y()); console.log((y() && true) || x()); console.log((x() && true) && y()); console.log((y() && true) && x()); console.log((x() || false) || y()); console.log((y() || false) || x()); console.log((x() || false) && y()); console.log((y() || false) && x()); console.log((x() && false) || y()); console.log((y() && false) || x()); console.log((x() && false) && y()); console.log((y() && false) && x()); } expect: { function x() {} function y() { return "foo"; } console.log(x() || !0); console.log(y() || !0); console.log((x(), y())); console.log((y(), x())); console.log(!!x() || y()); console.log(!!y() || x()); console.log(x() && y()); console.log(y() && x()); console.log(x() || y()); console.log(y() || x()); console.log(!!x() && y()); console.log(!!y() && x()); console.log((x(), y())); console.log((y(), x())); console.log(x() && !1); console.log(y() && !1); } expect_stdout: [ "true", "foo", "foo", "undefined", "foo", "true", "undefined", "undefined", "foo", "foo", "false", "undefined", "foo", "undefined", "undefined", "false", ] } issue_2560: { options = { conditionals: true, inline: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function log(x) { console.log(x); } function foo() { return log; } function bar() { if (x !== (x = foo())) { x(1); } else { x(2); } } var x = function() { console.log("init"); }; bar(); bar(); } expect: { function log(x) { console.log(x); } function bar() { x !== (x = log) ? x(1) : x(2); } var x = function() { console.log("init"); }; bar(); bar(); } expect_stdout: [ "1", "2", ] } hoist_decl: { options = { conditionals: true, join_vars: true, sequences: true, } input: { if (x()) { var a; y(); } else { z(); var b; } } expect: { var a, b; x() ? y() : z(); } } to_and_or: { options = { conditionals: true, } input: { var values = [ 0, null, true, "foo", false, -1 / 0, void 0, ]; values.forEach(function(x) { values.forEach(function(y) { values.forEach(function(z) { console.log(x ? y || z : z); }); }); }); } expect: { var values = [ 0, null, true, "foo", false, -1 / 0, void 0, ]; values.forEach(function(x) { values.forEach(function(y) { values.forEach(function(z) { console.log(x && y || z); }); }); }); } expect_stdout: true } cond_seq_assign_1: { options = { conditionals: true, sequences: true, } input: { function f(a) { var t; if (a) { t = "foo"; t = "bar"; } else { console.log(t); t = 42; } console.log(t); } f(f); f(); } expect: { function f(a) { var t; t = a ? (t = "foo", "bar") : (console.log(t), 42), console.log(t); } f(f), f(); } expect_stdout: [ "bar", "undefined", "42", ] } cond_seq_assign_2: { options = { conditionals: true, sequences: true, } input: { function f(a) { var t; if (a) { t = "foo"; a = "bar"; } else { console.log(t); t = 42; } console.log(t); } f(f); f(); } expect: { function f(a) { var t; a ? (t = "foo", a = "bar") : (console.log(t), t = 42), console.log(t); } f(f), f(); } expect_stdout: [ "foo", "undefined", "42", ] } cond_seq_assign_3: { options = { assignments: true, conditionals: true, } input: { var c = 0; if (this) c = 1 + c, c = c + 1; else c = 1 + c, c = c + 1; console.log(c); } expect: { var c = 0; this, c = 1 + c, c += 1; console.log(c); } expect_stdout: "2" } issue_3271: { options = { conditionals: true, } input: { function f(a) { var i = 0, b = []; if (a) { b[i++] = 4, b[i++] = 1; } else { b[i++] = 3, b[i++] = 2, b[i++] = 1; } return b; } console.log(f(0).pop(), f(1).pop()); } expect: { function f(a) { var i = 0, b = []; a ? b[i++] = 4 : (b[i++] = 3, b[i++] = 2), b[i++] = 1; return b; } console.log(f(0).pop(), f(1).pop()); } expect_stdout: "1 1" } iife_condition: { options = { conditionals: true, side_effects: true, } input: { if (function() { return console; }()) console.log("PASS"); } expect: { !function() { return console; }() || console.log("PASS"); } expect_stdout: "PASS" } angularjs_chain: { options = { conditionals: true, passes: 2, side_effects: true, } input: { function nonComputedMember(left, right, context, create) { var lhs = left(); if (create && create !== 1) { if (lhs && lhs[right] == null) { lhs[right] = {}; } } var value = lhs != null ? lhs[right] : undefined; if (context) { return { context: lhs, name: right, value: value }; } else { return value; } } } expect: { function nonComputedMember(left, right, context, create) { var lhs = left(); create && 1 !== create && lhs && null == lhs[right] && (lhs[right] = {}); var value = null != lhs ? lhs[right] : void 0; return context ? { context: lhs, name: right, value: value } : value; } } } UglifyJS2-3.6.3/test/compress/dead-code.js000066400000000000000000000510601355252637300202730ustar00rootroot00000000000000dead_code_1: { options = { dead_code: true, } input: { function f() { a(); b(); x = 10; return; if (x) { y(); } } } expect: { function f() { a(); b(); x = 10; return; } } } dead_code_2_should_warn: { options = { dead_code: true, } input: { function f() { g(); x = 10; throw new Error("foo"); // completely discarding the `if` would introduce some // bugs. UglifyJS v1 doesn't deal with this issue; in v2 // we copy any declarations to the upper scope. if (x) { y(); var x; function g(){}; // but nested declarations should not be kept. (function(){ var q; function y(){}; })(); } } f(); } expect: { function f() { g(); x = 10; throw new Error("foo"); var x; function g(){}; } f(); } expect_stdout: true node_version: "<=4" } dead_code_constant_boolean_should_warn_more: { options = { booleans: true, conditionals: true, dead_code: true, evaluate: true, loops: true, side_effects: true, } input: { while (!((foo && bar) || (x + "0"))) { console.log("unreachable"); var foo; function bar() {} } for (var x = 10, y; x && (y || x) && (!typeof x); ++x) { asdf(); foo(); var moo; } bar(); } expect: { var foo; function bar() {} // nothing for the while // as for the for, it should keep: var moo; var x = 10, y; bar(); } expect_stdout: true node_version: "<=4" } try_catch_finally: { options = { conditionals: true, dead_code: true, evaluate: true, passes: 2, side_effects: true, } input: { var a = 1; !function() { try { if (false) throw x; } catch (a) { var a = 2; console.log("FAIL"); } finally { a = 3; console.log("PASS"); } }(); try { console.log(a); } finally { } } expect: { var a = 1; !function() { var a; a = 3; console.log("PASS"); }(); try { console.log(a); } finally { } } expect_stdout: [ "PASS", "1", ] } accessor: { options = { side_effects: true, } input: { ({ get a() {}, set a(v){ this.b = 2; }, b: 1 }); } expect: {} } issue_2233_1: { options = { pure_getters: "strict", side_effects: true, unsafe: true, } input: { Array.isArray; Boolean; console.log; Date; decodeURI; decodeURIComponent; encodeURI; encodeURIComponent; Error.name; escape; eval; EvalError; Function.length; isFinite; isNaN; JSON; Math.random; Number.isNaN; parseFloat; parseInt; RegExp; Object.defineProperty; String.fromCharCode; RangeError; ReferenceError; SyntaxError; TypeError; unescape; URIError; } expect: {} expect_stdout: true } global_timeout_and_interval_symbols: { options = { pure_getters: "strict", side_effects: true, unsafe: true, } input: { // These global symbols do not exist in the test sandbox // and must be tested separately. clearInterval; clearTimeout; setInterval; setTimeout; } expect: {} } issue_2233_2: { options = { pure_getters: "strict", reduce_funcs: true, reduce_vars: true, side_effects: true, unsafe: true, unused: true, } input: { var RegExp; Array.isArray; RegExp; UndeclaredGlobal; function foo() { var Number; AnotherUndeclaredGlobal; Math.sin; Number.isNaN; } } expect: { var RegExp; UndeclaredGlobal; function foo() { var Number; AnotherUndeclaredGlobal; Number.isNaN; } } } issue_2233_3: { options = { pure_getters: "strict", reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unsafe: true, unused: true, } input: { var RegExp; Array.isArray; RegExp; UndeclaredGlobal; function foo() { var Number; AnotherUndeclaredGlobal; Math.sin; Number.isNaN; } } expect: { UndeclaredGlobal; } } global_fns: { options = { side_effects: true, unsafe: true, } input: { Boolean(1, 2); decodeURI(1, 2); decodeURIComponent(1, 2); Date(1, 2); encodeURI(1, 2); encodeURIComponent(1, 2); Error(1, 2); escape(1, 2); EvalError(1, 2); isFinite(1, 2); isNaN(1, 2); Number(1, 2); Object(1, 2); parseFloat(1, 2); parseInt(1, 2); RangeError(1, 2); ReferenceError(1, 2); String(1, 2); SyntaxError(1, 2); TypeError(1, 2); unescape(1, 2); URIError(1, 2); try { Function(1, 2); } catch (e) { console.log(e.name); } try { RegExp(1, 2); } catch (e) { console.log(e.name); } try { Array(NaN); } catch (e) { console.log(e.name); } } expect: { try { Function(1, 2); } catch (e) { console.log(e.name); } try { RegExp(1, 2); } catch (e) { console.log(e.name); } try { Array(NaN); } catch (e) { console.log(e.name); } } expect_stdout: [ "SyntaxError", "SyntaxError", "RangeError", ] } collapse_vars_assignment: { options = { collapse_vars: true, dead_code: true, passes: 2, unused: true, } input: { function f0(c) { var a = 3 / c; return a = a; } } expect: { function f0(c) { return 3 / c; } } } collapse_vars_lvalues_drop_assign: { options = { collapse_vars: true, dead_code: true, unused: true, } input: { function f0(x) { var i = ++x; return x += i; } function f1(x) { var a = (x -= 3); return x += a; } function f2(x) { var z = x, a = ++z; return z += a; } } expect: { function f0(x) { var i = ++x; return x + i; } function f1(x) { var a = (x -= 3); return x + a; } function f2(x) { var z = x, a = ++z; return z + a; } } } collapse_vars_misc1: { options = { collapse_vars: true, dead_code: true, unused: true, } input: { function f10(x) { var a = 5, b = 3; return a += b; } function f11(x) { var a = 5, b = 3; return a += --b; } } expect: { function f10(x) { return 5 + 3; } function f11(x) { var b = 3; return 5 + --b; } } } return_assignment: { options = { dead_code: true, unused: true, } input: { function f1(a, b, c) { return a = x(), b = y(), b = a && (c >>= 5); } function f2() { return e = x(); } function f3(e) { return e = x(); } function f4() { var e; return e = x(); } function f5(a) { try { return a = x(); } catch (b) { console.log(a); } } function f6(a) { try { return a = x(); } finally { console.log(a); } } function y() { console.log("y"); } function test(inc) { var counter = 0; x = function() { counter += inc; if (inc < 0) throw counter; return counter; }; [ f1, f2, f3, f4, f5, f6 ].forEach(function(f, i) { e = null; try { i += 1; console.log("result " + f(10 * i, 100 * i, 1000 * i)); } catch (x) { console.log("caught " + x); } if (null !== e) console.log("e: " + e); }); } var x, e; test(1); test(-1); } expect: { function f1(a, b, c) { return a = x(), y(), a && (c >> 5); } function f2() { return e = x(); } function f3(e) { return x(); } function f4() { return x(); } function f5(a) { try { return x(); } catch (b) { console.log(a); } } function f6(a) { try { return a = x(); } finally { console.log(a); } } function y() { console.log("y"); } function test(inc) { var counter = 0; x = function() { counter += inc; if (inc < 0) throw counter; return counter; }; [ f1, f2, f3, f4, f5, f6 ].forEach(function(f, i) { e = null; try { i += 1; console.log("result " + f(10 * i, 100 * i, 1000 * i)); } catch (x) { console.log("caught " + x); } if (null !== e) console.log("e: " + e); }); } var x, e; test(1); test(-1); } expect_stdout: [ "y", "result 31", "result 2", "e: 2", "result 3", "result 4", "result 5", "6", "result 6", "caught -1", "caught -2", "caught -3", "caught -4", "50", "result undefined", "60", "caught -6", ] } throw_assignment: { options = { dead_code: true, unused: true, } input: { function f1() { throw a = x(); } function f2(a) { throw a = x(); } function f3() { var a; throw a = x(); } function f4() { try { throw a = x(); } catch (b) { console.log(a); } } function f5(a) { try { throw a = x(); } catch (b) { console.log(a); } } function f6() { var a; try { throw a = x(); } catch (b) { console.log(a); } } function f7() { try { throw a = x(); } finally { console.log(a); } } function f8(a) { try { throw a = x(); } finally { console.log(a); } } function f9() { var a; try { throw a = x(); } finally { console.log(a); } } function test(inc) { var counter = 0; x = function() { counter += inc; if (inc < 0) throw counter; return counter; }; [ f1, f2, f3, f4, f5, f6, f7, f8, f9 ].forEach(function(f, i) { a = null; try { f(10 * (1 + i)); } catch (x) { console.log("caught " + x); } if (null !== a) console.log("a: " + a); }); } var x, a; test(1); test(-1); } expect: { function f1() { throw a = x(); } function f2(a) { throw x(); } function f3() { throw x(); } function f4() { try { throw a = x(); } catch (b) { console.log(a); } } function f5(a) { try { throw a = x(); } catch (b) { console.log(a); } } function f6() { var a; try { throw a = x(); } catch (b) { console.log(a); } } function f7() { try { throw a = x(); } finally { console.log(a); } } function f8(a) { try { throw a = x(); } finally { console.log(a); } } function f9() { var a; try { throw a = x(); } finally { console.log(a); } } function test(inc) { var counter = 0; x = function() { counter += inc; if (inc < 0) throw counter; return counter; }; [ f1, f2, f3, f4, f5, f6, f7, f8, f9 ].forEach(function(f, i) { a = null; try { f(10 * (1 + i)); } catch (x) { console.log("caught " + x); } if (null !== a) console.log("a: " + a); }); } var x, a; test(1); test(-1); } expect_stdout: [ "caught 1", "a: 1", "caught 2", "caught 3", "4", "a: 4", "5", "6", "7", "caught 7", "a: 7", "8", "caught 8", "9", "caught 9", "caught -1", "caught -2", "caught -3", "null", "50", "undefined", "null", "caught -7", "80", "caught -8", "undefined", "caught -9", ] } issue_2597: { options = { dead_code: true, } input: { function f(b) { try { try { throw "foo"; } catch (e) { return b = true; } } finally { b && (a = "PASS"); } } var a = "FAIL"; f(); console.log(a); } expect: { function f(b) { try { try { throw "foo"; } catch (e) { return b = true; } } finally { b && (a = "PASS"); } } var a = "FAIL"; f(); console.log(a); } expect_stdout: "PASS" } issue_2666: { options = { dead_code: true, } input: { function f(a) { return a = { p: function() { return a; } }; } console.log(typeof f().p()); } expect: { function f(a) { return a = { p: function() { return a; } }; } console.log(typeof f().p()); } expect_stdout: "object" } issue_2692: { options = { dead_code: true, reduce_vars: false, } input: { function f(a) { return a = g; function g() { return a; } } console.log(typeof f()()); } expect: { function f(a) { return a = g; function g() { return a; } } console.log(typeof f()()); } expect_stdout: "function" } issue_2701: { options = { dead_code: true, inline: false, } input: { function f(a) { return a = function() { return function() { return a; }; }(); } console.log(typeof f()()); } expect: { function f(a) { return a = function() { return function() { return a; }; }(); } console.log(typeof f()()); } expect_stdout: "function" } issue_2749: { options = { dead_code: true, inline: true, toplevel: true, unused: true, } input: { var a = 2, c = "PASS"; while (a--) (function() { return b ? c = "FAIL" : b = 1; try { } catch (b) { var b; } })(); console.log(c); } expect: { var a = 2, c = "PASS"; while (a--) b = void 0, b ? c = "FAIL" : b = 1; var b; console.log(c); } expect_stdout: "PASS" } unsafe_builtin: { options = { side_effects: true, unsafe: true, } input: { (!w).constructor(x); Math.abs(y); [ 1, 2, z ].valueOf(); } expect: { w, x; y; z; } } issue_2860_1: { options = { dead_code: true, evaluate: true, reduce_vars: true, } input: { console.log(function(a) { return a ^= 1; }()); } expect: { console.log(function(a) { return 1 ^ a; }()); } expect_stdout: "1" } issue_2860_2: { options = { dead_code: true, evaluate: true, inline: true, passes: 2, reduce_vars: true, } input: { console.log(function(a) { return a ^= 1; }()); } expect: { console.log(1); } expect_stdout: "1" } issue_2929: { options = { dead_code: true, } input: { console.log(function(a) { try { return null.p = a = 1; } catch (e) { return a ? "PASS" : "FAIL"; } }()); } expect: { console.log(function(a) { try { return null.p = a = 1; } catch (e) { return a ? "PASS" : "FAIL"; } }()); } expect_stdout: "PASS" } unsafe_string_replace: { options = { side_effects: true, unsafe: true, } input: { "foo".replace("f", function() { console.log("PASS"); }); } expect: { "foo".replace("f", function() { console.log("PASS"); }); } expect_stdout: "PASS" } issue_3402: { options = { dead_code: true, evaluate: true, functions: true, passes: 2, reduce_vars: true, side_effects: true, toplevel: true, typeofs: true, unused: true, } input: { var f = function f() { f = 42; console.log(typeof f); }; "function" == typeof f && f(); "function" == typeof f && f(); console.log(typeof f); } expect: { function f() { console.log(typeof f); } f(); f(); console.log(typeof f); } expect_stdout: [ "function", "function", "function", ] } issue_3406: { options = { dead_code: true, } input: { console.log(function f(a) { return delete (f = a); }()); } expect: { console.log(function f(a) { return delete (0, a); }()); } expect_stdout: "true" } UglifyJS2-3.6.3/test/compress/debugger.js000066400000000000000000000004771355252637300202600ustar00rootroot00000000000000keep_debugger: { options = { drop_debugger: false, } input: { debugger; } expect: { debugger; } } drop_debugger: { options = { drop_debugger: true, } input: { debugger; if (foo) debugger; } expect: { if (foo); } } UglifyJS2-3.6.3/test/compress/drop-console.js000066400000000000000000000010021355252637300210610ustar00rootroot00000000000000drop_console_1: { options = {} input: { console.log('foo'); console.log.apply(console, arguments); } expect: { console.log('foo'); console.log.apply(console, arguments); } } drop_console_2: { options = { drop_console: true, } input: { console.log('foo'); console.log.apply(console, arguments); } expect: { // with regular compression these will be stripped out as well void 0; void 0; } } UglifyJS2-3.6.3/test/compress/drop-unused.js000066400000000000000000001145641355252637300207440ustar00rootroot00000000000000unused_funarg_1: { options = { keep_fargs: false, unused: true, } input: { function f(a, b, c, d, e) { return a + b; } } expect: { function f(a, b) { return a + b; } } } unused_funarg_2: { options = { keep_fargs: false, unused: true, } input: { function f(a, b, c, d, e) { return a + c; } } expect: { function f(a, b, c) { return a + c; } } } unused_nested_function: { options = { unused: true, } input: { function f(x, y) { function g() { something(); } return x + y; } }; expect: { function f(x, y) { return x + y; } } } unused_circular_references_1: { options = { unused: true, } input: { function f(x, y) { // circular reference function g() { return h(); } function h() { return g(); } return x + y; } }; expect: { function f(x, y) { return x + y; } } } unused_circular_references_2: { options = { unused: true, } input: { function f(x, y) { var foo = 1, bar = baz, baz = foo + bar, qwe = moo(); return x + y; } }; expect: { function f(x, y) { moo(); // keeps side effect return x + y; } } } unused_circular_references_3: { options = { unused: true, } input: { function f(x, y) { var g = function() { return h() }; var h = function() { return g() }; return x + y; } }; expect: { function f(x, y) { return x + y; } } } unused_keep_setter_arg: { options = { unused: true, } input: { var x = { _foo: null, set foo(val) { }, get foo() { return this._foo; } } } expect: { var x = { _foo: null, set foo(val) { }, get foo() { return this._foo; } } } } unused_var_in_catch: { options = { unused: true, } input: { function foo() { try { foo(); } catch (ex) { var x = 10; } } } expect: { function foo() { try { foo(); } catch (ex) {} } } } used_var_in_catch: { options = { unused: true, } input: { function foo() { try { foo(); } catch (ex) { var x = 10; } return x; } } expect: { function foo() { try { foo(); } catch (ex) { var x = 10; } return x; } } } keep_fnames: { options = { keep_fnames: true, unsafe: true, unused: true, } input: { function foo() { return function bar(baz) {}; } } expect: { function foo() { return function bar(baz) {}; } } } drop_assign: { options = { unused: true, } input: { function f1() { var a; a = 1; } function f2() { var a = 1; a = 2; } function f3(a) { a = 1; } function f4() { var a; return a = 1; } function f5() { var a; return function() { a = 1; }; } } expect: { function f1() { 1; } function f2() { 2; } function f3(a) { 1; } function f4() { return 1; } function f5() { return function() { 1; }; } } } keep_assign: { options = { unused: "keep_assign", } input: { function f1() { var a; a = 1; } function f2() { var a = 1; a = 2; } function f3(a) { a = 1; } function f4() { var a; return a = 1; } function f5() { var a; return function() { a = 1; }; } } expect: { function f1() { var a; a = 1; } function f2() { var a = 1; a = 2; } function f3(a) { a = 1; } function f4() { var a; return a = 1; } function f5() { var a; return function() { a = 1; }; } } } drop_toplevel_funcs: { options = { toplevel: "funcs", unused: true, } input: { var a, b = 1, c = g; function f(d) { return function() { c = 2; }; } a = 2; function g() {} function h() {} console.log(b = 3); } expect: { var a, b = 1, c = g; a = 2; function g() {} console.log(b = 3); } } drop_toplevel_vars: { options = { toplevel: "vars", unused: true, } input: { var a, b = 1, c = g; function f(d) { return function() { c = 2; }; } a = 2; function g() {} function h() {} console.log(b = 3); } expect: { function f(d) { return function() { 2; }; } 2; function g() {} function h() {} console.log(3); } } drop_toplevel_vars_fargs: { options = { keep_fargs: false, toplevel: "vars", unused: true, } input: { var a, b = 1, c = g; function f(d) { return function() { c = 2; }; } a = 2; function g() {} function h() {} console.log(b = 3); } expect: { function f() { return function() { 2; }; } 2; function g() {} function h() {} console.log(3); } } drop_toplevel_all: { options = { toplevel: true, unused: true, } input: { var a, b = 1, c = g; function f(d) { return function() { c = 2; }; } a = 2; function g() {} function h() {} console.log(b = 3); } expect: { 2; console.log(3); } } drop_toplevel_retain: { options = { top_retain: "f,a,o", unused: true, } input: { var a, b = 1, c = g; function f(d) { return function() { c = 2; }; } a = 2; function g() {} function h() {} console.log(b = 3); } expect: { var a; function f(d) { return function() { 2; }; } a = 2; console.log(3); } } drop_toplevel_retain_array: { options = { top_retain: [ "f", "a", "o" ], unused: true, } input: { var a, b = 1, c = g; function f(d) { return function() { c = 2; }; } a = 2; function g() {} function h() {} console.log(b = 3); } expect: { var a; function f(d) { return function() { 2; }; } a = 2; console.log(3); } } drop_toplevel_retain_regex: { options = { top_retain: /^[fao]$/, unused: true, } input: { var a, b = 1, c = g; function f(d) { return function() { c = 2; }; } a = 2; function g() {} function h() {} console.log(b = 3); } expect: { var a; function f(d) { return function() { 2; }; } a = 2; console.log(3); } } drop_toplevel_all_retain: { options = { top_retain: "f,a,o", toplevel: true, unused: true, } input: { var a, b = 1, c = g; function f(d) { return function() { c = 2; }; } a = 2; function g() {} function h() {} console.log(b = 3); } expect: { var a; function f(d) { return function() { 2; }; } a = 2; console.log(3); } } drop_toplevel_funcs_retain: { options = { top_retain: "f,a,o", toplevel: "funcs", unused: true, } input: { var a, b = 1, c = g; function f(d) { return function() { c = 2; }; } a = 2; function g() {} function h() {} console.log(b = 3); } expect: { var a, b = 1, c = g; function f(d) { return function() { c = 2; }; } a = 2; function g() {} console.log(b = 3); } } drop_toplevel_vars_retain: { options = { top_retain: "f,a,o", toplevel: "vars", unused: true, } input: { var a, b = 1, c = g; function f(d) { return function() { c = 2; }; } a = 2; function g() {} function h() {} console.log(b = 3); } expect: { var a; function f(d) { return function() { 2; }; } a = 2; function g() {} function h() {} console.log(3); } } drop_toplevel_keep_assign: { options = { toplevel: true, unused: "keep_assign", } input: { var a, b = 1, c = g; function f(d) { return function() { c = 2; }; } a = 2; function g() {} function h() {} console.log(b = 3); } expect: { var a, b = 1; a = 2; console.log(b = 3); } } drop_fargs: { options = { keep_fargs: false, unused: true, } input: { function f(a) { var b = a; } } expect: { function f() {} } } drop_fnames: { options = { keep_fnames: false, unused: true, } input: { function f() { return function g() { var a = g; }; } } expect: { function f() { return function() {}; } } } global_var: { options = { side_effects: true, unused: true, } input: { var a; function foo(b) { a; b; c; typeof c === "undefined"; c + b + a; b && b.ar(); return b; } } expect: { var a; function foo(b) { c; c; b && b.ar(); return b; } } } iife: { options = { side_effects: true, unused: true, } input: { function f() { var a; ~function() {}(b); } } expect: { function f() { b; } } } drop_value: { options = { side_effects: true, } input: { (1, [2, foo()], 3, {a:1, b:bar()}); } expect: { foo(), bar(); } } issue_1539: { options = { collapse_vars: true, sequences: true, side_effects: true, unused: true, } input: { function f() { var a, b; a = b = 42; return a; } } expect: { function f() { return 42; } } } vardef_value: { options = { keep_fnames: false, reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f() { function g(){ return x(); } var a = g(); return a(42); } } expect: { function f() { var a = function(){ return x(); }(); return a(42); } } } assign_binding: { options = { collapse_vars: true, side_effects: true, unused: true, } input: { function f() { var a; a = f.g, a(); } } expect: { function f() { (0, f.g)(); } } } assign_chain: { options = { unused: true, } input: { function f() { var a, b; x = a = y = b = 42; } } expect: { function f() { x = y = 42; } } } issue_1583: { options = { keep_fargs: true, passes: 2, reduce_funcs: true, reduce_vars: true, unused: true, } input: { function m(t) { (function(e) { t = e(); })(function() { return (function(a) { return a; })(function(a) {}); }); } } expect: { function m(t) { (function(e) { (function() { return (function(a) { return function(a) {}; })(); })(); })(); } } } issue_1656: { options = { toplevel: true, unused: true, } beautify = { beautify: true, } input: { for(var a=0;;); } expect_exact: "for (;;) ;" } issue_1709: { options = { unused: true, } input: { console.log( function x() { var x = 1; return x; }(), function z() { function z() {} return z; }() ); } expect: { console.log( function() { var x = 1; return x; }(), function() { function z() {} return z; }() ); } expect_stdout: true } issue_1715_1: { options = { unused: true, } input: { var a = 1; function f() { a++; try { x(); } catch (a) { var a; } } f(); console.log(a); } expect: { var a = 1; function f() { a++; try { x(); } catch (a) { var a; } } f(); console.log(a); } expect_stdout: "1" } issue_1715_2: { options = { unused: true, } input: { var a = 1; function f() { a++; try { x(); } catch (a) { var a = 2; } } f(); console.log(a); } expect: { var a = 1; function f() { a++; try { x(); } catch (a) { var a; } } f(); console.log(a); } expect_stdout: "1" } issue_1715_3: { options = { unused: true, } input: { var a = 1; function f() { a++; try { console; } catch (a) { var a = 2 + x(); } } f(); console.log(a); } expect: { var a = 1; function f() { a++; try { console; } catch (a) { var a; x(); } } f(); console.log(a); } expect_stdout: "1" } issue_1715_4: { options = { unused: true, } input: { var a = 1; !function a() { a++; try { x(); } catch (a) { var a; } }(); console.log(a); } expect: { var a = 1; !function() { a++; try { x(); } catch (a) { var a; } }(); console.log(a); } expect_stdout: "1" } delete_assign_1: { options = { booleans: true, evaluate: true, toplevel: true, unused: true, } input: { var a; console.log(delete (a = undefined)); console.log(delete (a = void 0)); console.log(delete (a = Infinity)); console.log(delete (a = 1 / 0)); console.log(delete (a = NaN)); console.log(delete (a = 0 / 0)); } expect: { console.log((void 0, !0)); console.log((void 0, !0)); console.log((1 / 0, !0)); console.log((1 / 0, !0)); console.log((NaN, !0)); console.log((NaN, !0)); } expect_stdout: true } delete_assign_2: { options = { booleans: true, evaluate: true, keep_infinity: true, toplevel: true, unused: true, } input: { var a; console.log(delete (a = undefined)); console.log(delete (a = void 0)); console.log(delete (a = Infinity)); console.log(delete (a = 1 / 0)); console.log(delete (a = NaN)); console.log(delete (a = 0 / 0)); } expect: { console.log((void 0, !0)); console.log((void 0, !0)); console.log((Infinity, !0)); console.log((1 / 0, !0)); console.log((NaN, !0)); console.log((NaN, !0)); } expect_stdout: true } drop_var: { options = { toplevel: true, unused: true, } input: { var a; console.log(a, b); var a = 1, b = 2; console.log(a, b); var a = 3; console.log(a, b); } expect: { console.log(a, b); var a = 1, b = 2; console.log(a, b); a = 3; console.log(a, b); } expect_stdout: [ "undefined undefined", "1 2", "3 2", ] } issue_1830_1: { options = { unused: true, } input: { !function() { L: for (var b = console.log(1); !1;) continue L; }(); } expect: { !function() { L: for (console.log(1); !1;) continue L; }(); } expect_stdout: "1" } issue_1830_2: { options = { unused: true, } input: { !function() { L: for (var a = 1, b = console.log(a); --a;) continue L; }(); } expect: { !function() { var a = 1; L: for (console.log(a); --a;) continue L; }(); } expect_stdout: "1" } issue_1838: { options = { join_vars: true, loops: true, unused: true, } beautify = { beautify: true, } input: { function f() { var b = a; while (c); } } expect_exact: [ "function f() {", " for (a; c; ) ;", "}", ] } var_catch_toplevel: { options = { conditionals: true, negate_iife: true, passes: 2, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { function f() { a--; try { a++; x(); } catch (a) { if (a) var a; var a = 10; } } f(); } expect: { !function() { try { x(); } catch (a) { var a; } }(); } } issue_2105_1: { options = { collapse_vars: true, inline: true, passes: 3, reduce_funcs: true, reduce_vars: true, side_effects: 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 }; } return bar; } ); }); } expect: { ({ prop: function() { console.log; console.log("PASS"); } }).prop(); } expect_stdout: "PASS" } issue_2105_2: { options = { collapse_vars: true, inline: true, passes: 3, properties: true, pure_getters: "strict", reduce_funcs: true, 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 }; } return bar; } ); }); } expect: { console.log("PASS"); } expect_stdout: "PASS" } issue_2226_1: { options = { side_effects: true, unused: true, } input: { function f1() { var a = b; a += c; } function f2(a) { a <<= b; } function f3(a) { --a; } function f4() { var a = b; return a *= c; } function f5(a) { x(a /= b); } } expect: { function f1() { b; c; } function f2(a) { b; } function f3(a) { 0; } function f4() { var a = b; return a *= c; } function f5(a) { x(a /= b); } } } issue_2226_2: { options = { collapse_vars: true, sequences: true, side_effects: true, unused: true, } input: { console.log(function(a, b) { a += b; return a; }(1, 2)); } expect: { console.log(function(a, b) { return a += 2; }(1)); } expect_stdout: "3" } issue_2226_3: { options = { collapse_vars: true, side_effects: true, unused: true, } input: { console.log(function(a, b) { a += b; return a; }(1, 2)); } expect: { console.log(function(a, b) { return a += 2; }(1)); } expect_stdout: "3" } issue_2288: { options = { unused: true, } beautify = { beautify: true, } input: { function foo(o) { for (var j = o.a, i = 0; i < 0; i++); for (var i = 0; i < 0; i++); } } expect: { function foo(o) { o.a; for (var i = 0; i < 0; i++); for (i = 0; i < 0; i++); } } } issue_2516_1: { options = { collapse_vars: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { function foo() { function qux(x) { bar.call(null, x); } function bar(x) { var FOUR = 4; var trouble = x || never_called(); var value = (FOUR - 1) * trouble; console.log(value == 6 ? "PASS" : value); } Baz = qux; } var Baz; foo(); Baz(2); } expect: { function foo() { Baz = function(x) { (function(x) { var trouble = x || never_called(); var value = (4 - 1) * trouble; console.log(6 == value ? "PASS" : value); }).call(null, x); }; } var Baz; foo(); Baz(2); } } issue_2516_2: { options = { collapse_vars: true, passes: 2, reduce_funcs: true, reduce_vars: true, unused: true, } input: { function foo() { function qux(x) { bar.call(null, x); } function bar(x) { var FOUR = 4; var trouble = x || never_called(); var value = (FOUR - 1) * trouble; console.log(value == 6 ? "PASS" : value); } Baz = qux; } var Baz; foo(); Baz(2); } expect: { function foo() { Baz = function(x) { (function(x) { var value = (4 - 1) * (x || never_called()); console.log(6 == value ? "PASS" : value); }).call(null, x); }; } var Baz; foo(); Baz(2); } } defun_lambda_same_name: { options = { toplevel: true, unused: true, } input: { function f(n) { return n ? n * f(n - 1) : 1; } console.log(function f(n) { return n ? n * f(n - 1) : 1; }(5)); } expect: { console.log(function f(n) { return n ? n * f(n - 1) : 1; }(5)); } expect_stdout: "120" } issue_2660_1: { options = { reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { var a = 2; function f(b) { return b && f() || a--; } f(1); console.log(a); } expect: { var a = 2; (function f(b) { return b && f() || a--; })(1); console.log(a); } expect_stdout: "1" } issue_2660_2: { options = { collapse_vars: true, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, unused: true, } input: { var a = 1; function f(b) { b && f(); --a, a.toString(); } f(); console.log(a); } expect: { var a = 1; (function f(b) { b && f(), (--a).toString(); })(), console.log(a); } expect_stdout: "0" } issue_2665: { options = { evaluate: true, inline: true, keep_fargs: false, passes: 2, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, typeofs: true, unused: true, } input: { var a = 1; function g() { a-- && g(); } typeof h == "function" && h(); function h() { typeof g == "function" && g(); } console.log(a); } expect: { var a = 1; !function g() { a-- && g(); }(); console.log(a); } expect_stdout: "-1" } double_assign_1: { options = { passes: 2, reduce_vars: true, side_effects: true, unused: true, } input: { function f1() { var a = {}; var a = []; return a; } function f2() { var a = {}; a = []; return a; } function f3() { a = {}; var a = []; return a; } function f4(a) { a = {}; a = []; return a; } function f5(a) { var a = {}; a = []; return a; } function f6(a) { a = {}; var a = []; return a; } console.log(f1(), f2(), f3(), f4(), f5(), f6()); } expect: { function f1() { return []; } function f2() { var a; a = []; return a; } function f3() { return []; } function f4(a) { a = []; return a; } function f5(a) { a = []; return a; } function f6(a) { a = []; return a; } console.log(f1(), f2(), f3(), f4(), f5(), f6()); } expect_stdout: true } double_assign_2: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { for (var i = 0; i < 2; i++) a = void 0, a = {}, console.log(a); var a; } expect: { for (var i = 0; i < 2; i++) void 0, a = {}, console.log(a); var a; } } double_assign_3: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { for (var i = 0; i < 2; i++) a = void 0, a = { a: a }, console.log(a); var a; } expect: { for (var i = 0; i < 2; i++) a = void 0, a = { a: a }, console.log(a); var a; } } cascade_drop_assign: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { var a, b = a = "PASS"; console.log(b); } expect: { var b = "PASS"; console.log(b); } expect_stdout: "PASS" } chained_3: { options = { reduce_vars: true, unused: true, } input: { console.log(function(a, b) { var c = a, c = b; b++; return c; }(1, 2)); } expect: { console.log(function(a, b) { var c = b; b++; return c; }(0, 2)); } expect_stdout: "2" } issue_2768: { options = { inline: true, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, unused: true, } input: { var a = "FAIL", c = 1; var c = function(b) { var d = b = a; var e = --b + (d && (a = "PASS")); }(); console.log(a, typeof c); } expect: { var a = "FAIL"; var c = (d = a, 0, void (d && (a = "PASS"))); var d; console.log(a, typeof c); } expect_stdout: "PASS undefined" } issue_2846: { options = { collapse_vars: true, reduce_vars: true, toplevel: true, unused: true, } input: { function f(a, b) { var a = 0; b && b(a); return a++; } var c = f(); console.log(c); } expect: { var c = function(a, b) { a = 0; b && b(a); return a++; }(); console.log(c); } expect_stdout: "0" } issue_805_1: { options = { inline: true, passes: 2, pure_getters: "strict", reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { (function(a) { var unused = function() {}; unused.prototype[a()] = 42; (unused.prototype.bar = function() { console.log("bar"); })(); return unused; })(function() { console.log("foo"); return "foo"; }); } expect: { console.log("foo"), console.log("bar"); } expect_stdout: [ "foo", "bar", ] } issue_805_2: { options = { inline: true, passes: 2, pure_getters: "strict", reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { (function(a) { function unused() {} unused.prototype[a()] = 42; (unused.prototype.bar = function() { console.log("bar"); })(); return unused; })(function() { console.log("foo"); return "foo"; }); } expect: { console.log("foo"), console.log("bar"); } expect_stdout: [ "foo", "bar", ] } issue_2995: { options = { pure_getters: "strict", reduce_vars: true, unused: true, } input: { function f(a) { var b; a.b = b = function() {}; b.c = "PASS"; } var o = {}; f(o); console.log(o.b.c); } expect: { function f(a) { var b; a.b = b = function() {}; b.c = "PASS"; } var o = {}; f(o); console.log(o.b.c); } expect_stdout: "PASS" } issue_3146_1: { options = { collapse_vars: true, unused: true, } input: { (function(f) { f("g()"); })(function(a) { eval(a); function g(b) { if (!b) b = "PASS"; console.log(b); } }); } expect: { (function(f) { f("g()"); })(function(a) { eval(a); function g(b) { if (!b) b = "PASS"; console.log(b); } }); } expect_stdout: "PASS" } issue_3146_2: { options = { reduce_vars: true, unused: true, } input: { (function(f) { f("g()"); })(function(a) { eval(a); function g(b) { if (!b) b = "PASS"; console.log(b); } }); } expect: { (function(f) { f("g()"); })(function(a) { eval(a); function g(b) { if (!b) b = "PASS"; console.log(b); } }); } expect_stdout: "PASS" } issue_3146_3: { options = { collapse_vars: true, unused: true, } input: { var g = "PASS"; (function(f) { var g = "FAIL"; f("console.log(g)", g[g]); })(function(a) { eval(a); }); } expect: { var g = "PASS"; (function(f) { var g = "FAIL"; f("console.log(g)", g[g]); })(function(a) { eval(a); }); } expect_stdout: "PASS" } issue_3146_4: { options = { reduce_vars: true, unused: true, } input: { var g = "PASS"; (function(f) { var g = "FAIL"; f("console.log(g)", g[g]); })(function(a) { eval(a); }); } expect: { var g = "PASS"; (function(f) { var g = "FAIL"; f("console.log(g)", g[g]); })(function(a) { eval(a); }); } expect_stdout: "PASS" } issue_3192: { options = { unused: true, } input: { (function(a) { console.log(a = "foo", arguments[0]); })("bar"); (function(a) { "use strict"; console.log(a = "foo", arguments[0]); })("bar"); } expect: { (function(a) { console.log(a = "foo", arguments[0]); })("bar"); (function(a) { "use strict"; console.log("foo", arguments[0]); })("bar"); } expect_stdout: [ "foo foo", "foo bar", ] } issue_3233: { options = { pure_getters: "strict", side_effects: true, unused: true, } input: { var a = function b() { b.c = "PASS"; }; a(); console.log(a.c); } expect: { var a = function b() { b.c = "PASS"; }; a(); console.log(a.c); } expect_stdout: "PASS" } issue_3375: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { var b = 1; var a = c = [], c = --b + ("function" == typeof f && f()); var a = c && c[a]; console.log(a, b); } expect: { var b = 1; var a = [], c = --b + ("function" == typeof f && f()); a = c && c[a]; console.log(a, b); } expect_stdout: "0 0" } issue_3427_1: { options = { sequences: true, side_effects: true, unused: true, } input: { (function() { var a; a = a || {}; })(); } expect: {} } issue_3427_2: { options = { unused: true, } input: { (function() { var s = "PASS"; console.log(s = s || "FAIL"); })(); } expect: { (function() { var s = "PASS"; console.log(s = s || "FAIL"); })(); } expect_stdout: "PASS" } issue_3495: { options = { dead_code: true, pure_getters: "strict", side_effects: true, unused: true, } input: { console.log(function f() { f = 0; var a = f.p; }()); } expect: { console.log(void 0); } expect_stdout: "undefined" } issue_3497: { options = { pure_getters: "strict", side_effects: true, unused: true, } input: { var a; console.log(function(b) { (b += a).p = 0; }()); } expect: { var a; console.log(function(b) { (b += a).p = 0; }()); } expect_stdout: "undefined" } UglifyJS2-3.6.3/test/compress/evaluate.js000066400000000000000000001101041355252637300202670ustar00rootroot00000000000000and: { options = { evaluate: true, side_effects: true, } input: { var a; // compress these a = true && condition; a = 1 && console.log("a"); a = 2 * 3 && 2 * condition; a = 5 == 5 && condition + 3; a = "string" && 4 - condition; a = 5 + "" && condition / 5; a = -4.5 && 6 << condition; a = 6 && 7; a = false && condition; a = NaN && console.log("b"); a = 0 && console.log("c"); a = undefined && 2 * condition; a = null && condition + 3; a = 2 * 3 - 6 && 4 - condition; a = 10 == 7 && condition / 5; a = !"string" && 6 % condition; a = 0 && 7; // don't compress these a = condition && true; a = console.log("a") && 2; a = 4 - condition && "string"; a = 6 << condition && -4.5; a = condition && false; a = console.log("b") && NaN; a = console.log("c") && 0; a = 2 * condition && undefined; a = condition + 3 && null; } expect: { var a; a = condition; a = console.log("a"); a = 2 * condition; a = condition + 3; a = 4 - condition; a = condition / 5; a = 6 << condition; a = 7; a = false; a = NaN; a = 0; a = void 0; a = null; a = 0; a = false; a = false; a = 0; a = condition && true; a = console.log("a") && 2; a = 4 - condition && "string"; a = 6 << condition && -4.5; a = condition && false; a = console.log("b") && NaN; a = console.log("c") && 0; a = 2 * condition && void 0; a = condition + 3 && null; } } or: { options = { evaluate: true, side_effects: true, } input: { var a; // compress these a = true || condition; a = 1 || console.log("a"); a = 2 * 3 || 2 * condition; a = 5 == 5 || condition + 3; a = "string" || 4 - condition; a = 5 + "" || condition / 5; a = -4.5 || 6 << condition; a = 6 || 7; a = false || condition; a = 0 || console.log("b"); a = NaN || console.log("c"); a = undefined || 2 * condition; a = null || condition + 3; a = 2 * 3 - 6 || 4 - condition; a = 10 == 7 || condition / 5; a = !"string" || 6 % condition; a = null || 7; a = console.log(undefined && condition || null); a = console.log(undefined || condition && null); // don't compress these a = condition || true; a = console.log("a") || 2; a = 4 - condition || "string"; a = 6 << condition || -4.5; a = condition || false; a = console.log("b") || NaN; a = console.log("c") || 0; a = 2 * condition || undefined; a = condition + 3 || null; } expect: { var a; a = true; a = 1; a = 6; a = true; a = "string"; a = "5"; a = -4.5; a = 6; a = condition; a = console.log("b"); a = console.log("c"); a = 2 * condition; a = condition + 3; a = 4 - condition; a = condition / 5; a = 6 % condition; a = 7; a = console.log(null); a = console.log(condition && null); a = condition || true; a = console.log("a") || 2; a = 4 - condition || "string"; a = 6 << condition || -4.5; a = condition || false; a = console.log("b") || NaN; a = console.log("c") || 0; a = 2 * condition || void 0; a = condition + 3 || null; } } unary_prefix: { options = { evaluate: true, side_effects: true, } input: { a = !0 && b; a = !0 || b; a = ~1 && b; a = ~1 || b; a = -2 && b; a = -2 || b; a = +3 && b; a = +3 || b; } expect: { a = b; a = !0; a = b; a = -2; a = b; a = -2; a = b; a = 3; } } negative_zero: { options = { evaluate: true, } input: { console.log( -"", - -"", 1 / (-0), 1 / (-"") ); } expect: { console.log( -0, 0, -1/0, -1/0 ); } expect_stdout: true } positive_zero: { options = { evaluate: true, } input: { console.log( +"", + -"", 1 / (+0), 1 / (+"") ); } expect: { console.log( 0, -0, 1/0, 1/0 ); } expect_stdout: true } unsafe_constant: { options = { evaluate: true, unsafe: true, } input: { console.log( true.a, false.a, null.a, undefined.a ); } expect: { console.log( void 0, false.a, null.a, (void 0).a ); } expect_stdout: true } unsafe_object: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unsafe: true, } input: { var o = { a: 1 }; console.log( o + 1, o.a + 1, o.b + 1, o.a.b + 1 ); } expect: { var o = { a: 1 }; console.log( o + 1, 2, o.b + 1, NaN ); } expect_stdout: true } unsafe_object_nested: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unsafe: true, } input: { var o = { a: { b: 1 } }; console.log( o + 1, o.a + 1, o.b + 1, o.a.b + 1 ); } expect: { var o = { a: { b: 1 } }; console.log( o + 1, o.a + 1, o.b + 1, 2 ); } expect_stdout: true } unsafe_object_complex: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unsafe: true, } input: { var o = { a: { b: 1 }, b: 1 }; console.log( o + 1, o.a + 1, o.b + 1, o.a.b + 1 ); } expect: { var o = { a: { b: 1 }, b: 1 }; console.log( o + 1, o.a + 1, 2, 2 ); } expect_stdout: true } unsafe_object_repeated: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unsafe: true, } input: { var o = { a: { b: 1 }, a: 1 }; console.log( o + 1, o.a + 1, o.b + 1, o.a.b + 1 ); } expect: { var o = { a: { b: 1 }, a: 1 }; console.log( o + 1, 2, o.b + 1, NaN ); } expect_stdout: true } unsafe_object_accessor: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unsafe: true, } input: { function f() { var a = { get b() {}, set b() {} }; return {a:a}; } } expect: { function f() { var a = { get b() {}, set b() {} }; return {a:a}; } } } prop_function: { options = { evaluate: true, properties: true, side_effects: true, } input: { console.log( ({a:{b:1},b:function(){}}) + 1, ({a:{b:1},b:function(){}}).a + 1, ({a:{b:1},b:function(){}}).b + 1, ({a:{b:1},b:function(){}}).a.b + 1 ); } expect: { console.log( ({a:{b:1},b:function(){}}) + 1, ({b:1}) + 1, function(){} + 1, 2 ); } expect_stdout: true } unsafe_integer_key: { options = { evaluate: true, unsafe: true, } input: { console.log( ({0:1}) + 1, ({0:1})[0] + 1, ({0:1})["0"] + 1, ({0:1})[1] + 1, ({0:1})[0][1] + 1, ({0:1})[0]["1"] + 1 ); } expect: { console.log( ({0:1}) + 1, 2, 2, ({0:1})[1] + 1, NaN, NaN ); } expect_stdout: true } unsafe_integer_key_complex: { options = { evaluate: true, unsafe: true, } input: { console.log( ({0:{1:1},1:1}) + 1, ({0:{1:1},1:1})[0] + 1, ({0:{1:1},1:1})["0"] + 1, ({0:{1:1},1:1})[1] + 1, ({0:{1:1},1:1})[0][1] + 1, ({0:{1:1},1:1})[0]["1"] + 1 ); } expect: { console.log( ({0:{1:1},1:1}) + 1, "[object Object]1", "[object Object]1", 2, 2, 2 ); } expect_stdout: true } unsafe_float_key: { options = { evaluate: true, unsafe: true, } input: { console.log( ({2.72:1}) + 1, ({2.72:1})[2.72] + 1, ({2.72:1})["2.72"] + 1, ({2.72:1})[3.14] + 1, ({2.72:1})[2.72][3.14] + 1, ({2.72:1})[2.72]["3.14"] + 1 ); } expect: { console.log( ({2.72:1}) + 1, 2, 2, ({2.72:1})[3.14] + 1, NaN, NaN ); } expect_stdout: true } unsafe_float_key_complex: { options = { evaluate: true, unsafe: true, } input: { console.log( ({2.72:{3.14:1},3.14:1}) + 1, ({2.72:{3.14:1},3.14:1})[2.72] + 1, ({2.72:{3.14:1},3.14:1})["2.72"] + 1, ({2.72:{3.14:1},3.14:1})[3.14] + 1, ({2.72:{3.14:1},3.14:1})[2.72][3.14] + 1, ({2.72:{3.14:1},3.14:1})[2.72]["3.14"] + 1 ); } expect: { console.log( "[object Object]1", "[object Object]1", "[object Object]1", 2, 2, 2 ); } expect_stdout: true } unsafe_array: { options = { evaluate: true, unsafe: true, } input: { console.log( [1, , 3][1], [1, 2, 3, a] + 1, [1, 2, 3, 4] + 1, [1, 2, 3, a][0] + 1, [1, 2, 3, 4][0] + 1, [1, 2, 3, 4][6 - 5] + 1, [1, , 3, 4][6 - 5] + 1, [[1, 2], [3, 4]][0] + 1, [[1, 2], [3, 4]][6 - 5][1] + 1, [[1, 2], , [3, 4]][6 - 5][1] + 1 ); } expect: { console.log( void 0, [1, 2, 3, a] + 1, "1,2,3,41", [1, 2, 3, a][0] + 1, 2, 3, NaN, "1,21", 5, (void 0)[1] + 1 ); } expect_stdout: true } unsafe_string: { options = { evaluate: true, unsafe: true, } input: { console.log( "1234" + 1, "1234"[0] + 1, "1234"[6 - 5] + 1, ("12" + "34")[0] + 1, ("12" + "34")[6 - 5] + 1, [1, 2, 3, 4].join("")[0] + 1 ); } expect: { console.log( "12341", "11", "21", "11", "21", "11" ); } expect_stdout: true } unsafe_array_bad_index: { options = { evaluate: true, unsafe: true, } input: { console.log( [1, 2, 3, 4].a + 1, [1, 2, 3, 4]["a"] + 1, [1, 2, 3, 4][3.14] + 1 ); } expect: { console.log( [1, 2, 3, 4].a + 1, [1, 2, 3, 4]["a"] + 1, [1, 2, 3, 4][3.14] + 1 ); } expect_stdout: true } unsafe_string_bad_index: { options = { evaluate: true, unsafe: true, } input: { console.log( "1234".a + 1, "1234"["a"] + 1, "1234"[3.14] + 1 ); } expect: { console.log( NaN, NaN, NaN ); } expect_stdout: "NaN NaN NaN" } prototype_function: { options = { evaluate: true, properties: true, side_effects: true, } input: { var a = ({valueOf: 0}) < 1; var b = ({toString: 0}) < 1; var c = ({valueOf: 0}) + ""; var d = ({toString: 0}) + ""; var e = (({valueOf: 0}) + "")[2]; var f = (({toString: 0}) + "")[2]; var g = ({valueOf: 0}).valueOf(); var h = ({toString: 0}).toString(); } expect: { var a = ({valueOf: 0}) < 1; var b = ({toString: 0}) < 1; var c = ({valueOf: 0}) + ""; var d = ({toString: 0}) + ""; var e = (({valueOf: 0}) + "")[2]; var f = (({toString: 0}) + "")[2]; var g = 0(); var h = 0(); } } call_args: { options = { evaluate: true, inline: true, reduce_funcs: true, reduce_vars: true, toplevel: true, } input: { var a = 1; console.log(a); +function(a) { return a; }(a); } expect: { var a = 1; console.log(1); +(1, 1); } expect_stdout: true } call_args_drop_param: { options = { evaluate: true, inline: true, keep_fargs: false, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var a = 1; console.log(a); +function(a) { return a; }(a, b); } expect: { console.log(1); +(b, 1); } expect_stdout: true } in_boolean_context: { options = { booleans: true, evaluate: true, sequences: true, side_effects: true, } input: { console.log( !42, !"foo", ![1, 2], !/foo/, !b(42), !b("foo"), !b([1, 2]), !b(/foo/), ![1, foo()], ![1, foo(), 2] ); } expect: { console.log( !1, !1, !1, !1, !b(42), !b("foo"), !b([1, 2]), !b(/foo/), (foo(), !1), (foo(), !1) ); } expect_stdout: true } unsafe_charAt: { options = { evaluate: true, unsafe: true, } input: { console.log( "1234" + 1, "1234".charAt(0) + 1, "1234".charAt(6 - 5) + 1, ("12" + "34").charAt(0) + 1, ("12" + "34").charAt(6 - 5) + 1, [1, 2, 3, 4].join("").charAt(0) + 1 ); } expect: { console.log( "12341", "11", "21", "11", "21", "11" ); } expect_stdout: true } unsafe_charAt_bad_index: { options = { evaluate: true, unsafe: true, } input: { console.log( "1234".charAt() + 1, "1234".charAt("a") + 1, "1234".charAt(3.14) + 1 ); } expect: { console.log( "11", "11", "41" ); } expect_stdout: true } unsafe_charAt_noop: { options = { evaluate: true, unsafe: true, } input: { console.log( s.charAt(0), "string".charAt(x), (typeof x).charAt() ); } expect: { console.log( s[0], "string"[0 | x], (typeof x)[0] ); } } issue_1649: { options = { evaluate: true, } input: { console.log(-1 + -1); } expect: { console.log(-2); } expect_stdout: "-2"; } issue_1760_1: { options = { evaluate: true, } input: { !function(a) { try { throw 0; } catch (NaN) { a = +"foo"; } console.log(a); }(); } expect: { !function(a) { try { throw 0; } catch (NaN) { a = 0 / 0; } console.log(a); }(); } expect_stdout: "NaN" } issue_1760_2: { options = { evaluate: true, keep_infinity: true, } input: { !function(a) { try { throw 0; } catch (Infinity) { a = 123456789 / 0; } console.log(a); }(); } expect: { !function(a) { try { throw 0; } catch (Infinity) { a = 1 / 0; } console.log(a); }(); } expect_stdout: "Infinity" } delete_expr_1: { options = { booleans: true, evaluate: true, } input: { console.log(delete undefined); console.log(delete void 0); console.log(delete Infinity); console.log(delete (1 / 0)); console.log(delete NaN); console.log(delete (0 / 0)); } expect: { console.log(delete undefined); console.log((void 0, !0)); console.log(delete Infinity); console.log((1 / 0, !0)); console.log(delete NaN); console.log((0 / 0, !0)); } expect_stdout: true } delete_expr_2: { options = { booleans: true, evaluate: true, keep_infinity: true, } input: { console.log(delete undefined); console.log(delete void 0); console.log(delete Infinity); console.log(delete (1 / 0)); console.log(delete NaN); console.log(delete (0 / 0)); } expect: { console.log(delete undefined); console.log((void 0, !0)); console.log(delete Infinity); console.log((1 / 0, !0)); console.log(delete NaN); console.log((0 / 0, !0)); } expect_stdout: true } delete_binary_1: { options = { booleans: true, evaluate: true, side_effects: true, } input: { console.log(delete (true && undefined)); console.log(delete (true && void 0)); console.log(delete (true && Infinity)); console.log(delete (true && (1 / 0))); console.log(delete (true && NaN)); console.log(delete (true && (0 / 0))); } expect: { console.log(!0); console.log(!0); console.log(!0); console.log(!0); console.log(!0); console.log(!0); } expect_stdout: true } delete_binary_2: { options = { booleans: true, evaluate: true, keep_infinity: true, side_effects: true, } input: { console.log(delete (false || undefined)); console.log(delete (false || void 0)); console.log(delete (false || Infinity)); console.log(delete (false || (1 / 0))); console.log(delete (false || NaN)); console.log(delete (false || (0 / 0))); } expect: { console.log(!0); console.log(!0); console.log(!0); console.log(!0); console.log(!0); console.log(!0); } expect_stdout: true } Infinity_NaN_undefined_LHS: { beautify = { beautify: true, } input: { function f() { Infinity = Infinity; ++Infinity; Infinity--; NaN *= NaN; ++NaN; NaN--; undefined |= undefined; ++undefined; undefined--; } } expect_exact: [ "function f() {", " Infinity = 1 / 0;", " ++Infinity;", " Infinity--;", " NaN *= NaN;", " ++NaN;", " NaN--;", " undefined |= void 0;", " ++undefined;", " undefined--;", "}", ] } issue_1964_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unsafe_regexp: false, unused: true, } 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() { 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: [ "\\s", "b", ] } issue_1964_2: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unsafe_regexp: true, unused: true, } 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: [ "\\s", "b", ] } array_slice_index: { options = { evaluate: true, unsafe: true, } input: { console.log([1,2,3].slice(1)[1]); } expect: { console.log(3); } expect_stdout: "3" } string_charCodeAt: { options = { evaluate: true, unsafe: true, } input: { console.log("foo".charCodeAt("bar".length)); } expect: { console.log(NaN); } expect_stdout: "NaN" } issue_2207_1: { options = { evaluate: true, unsafe: true, } input: { console.log(String.fromCharCode(65)); console.log(Math.max(3, 6, 2, 7, 3, 4)); console.log(Math.cos(1.2345)); console.log(Math.cos(1.2345) - Math.sin(4.321)); console.log(Math.pow(Math.PI, Math.E - Math.LN10).toFixed(15)); } expect: { console.log("A"); console.log(7); console.log(Math.cos(1.2345)); console.log(1.2543732512566947); console.log("1.609398451447204"); } expect_stdout: true } issue_2207_2: { options = { evaluate: true, unsafe: true, } input: { console.log(Math.E); console.log(Math.LN10); console.log(Math.LN2); console.log(Math.LOG2E); console.log(Math.LOG10E); console.log(Math.PI); console.log(Math.SQRT1_2); console.log(Math.SQRT2); } expect: { console.log(Math.E); console.log(Math.LN10); console.log(Math.LN2); console.log(Math.LOG2E); console.log(Math.LOG10E); console.log(Math.PI); console.log(Math.SQRT1_2); console.log(Math.SQRT2); } expect_stdout: true } issue_2207_3: { options = { evaluate: true, unsafe: true, } input: { console.log(Number.MAX_VALUE); console.log(Number.MIN_VALUE); console.log(Number.NaN); console.log(Number.NEGATIVE_INFINITY); console.log(Number.POSITIVE_INFINITY); } expect: { console.log(Number.MAX_VALUE); console.log(5e-324); console.log(NaN); console.log(-1/0); console.log(1/0); } expect_stdout: true } issue_2231_1: { options = { evaluate: true, unsafe: true, } input: { console.log(Object.keys(void 0)); } expect: { console.log(Object.keys(void 0)); } expect_stdout: true expect_warnings: [ "WARN: Error evaluating Object.keys(void 0) [test/compress/evaluate.js:1,20]", ] } issue_2231_2: { options = { evaluate: true, unsafe: true, } input: { console.log(Object.getOwnPropertyNames(null)); } expect: { console.log(Object.getOwnPropertyNames(null)); } expect_stdout: true expect_warnings: [ "WARN: Error evaluating Object.getOwnPropertyNames(null) [test/compress/evaluate.js:1,20]", ] } issue_2231_3: { options = { evaluate: true, unsafe: true, } input: { console.log(Object.keys({ foo: "bar" })[0]); } expect: { console.log("foo"); } expect_stdout: "foo" } self_comparison_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unsafe: true, unused: true, } input: { var o = { n: NaN }; console.log(typeof o.n, o.n == o.n, o.n === o.n, o.n != o.n, o.n !== o.n); } expect: { console.log("number", false, false, true, true); } expect_stdout: "number false false true true" } self_comparison_2: { options = { evaluate: true, hoist_props: true, passes: 2, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var o = { n: NaN }; console.log(typeof o.n, o.n == o.n, o.n === o.n, o.n != o.n, o.n !== o.n); } expect: { console.log("number", false, false, true, true); } expect_stdout: "number false false true true" } issue_2535_1: { options = { booleans: true, evaluate: true, sequences: true, side_effects: true, } input: { if ((x() || true) || y()) z(); if ((x() || true) && y()) z(); if ((x() && true) || y()) z(); if ((x() && true) && y()) z(); if ((x() || false) || y()) z(); if ((x() || false) && y()) z(); if ((x() && false) || y()) z(); if ((x() && false) && y()) z(); } expect: { if (x(), 1) z(); if (x(), y()) z(); if (x() || y()) z(); if (x() && y()) z(); if (x() || y()) z(); if (x() && y()) z(); if (x(), y()) z(); if (x(), 0) z(); } } issue_2535_2: { options = { booleans: true, evaluate: true, sequences: true, side_effects: true, } input: { (x() || true) || y(); (x() || true) && y(); (x() && true) || y(); (x() && true) && y(); (x() || false) || y(); (x() || false) && y(); (x() && false) || y(); (x() && false) && y(); } expect: { x(), x(), y(), x() || y(), x() && y(), x() || y(), x() && y(), x(), y(), x(); } } issue_2535_3: { options = { booleans: true, evaluate: true, } input: { console.log(Object(1) && 1 && 2); console.log(Object(1) && true && 1 && 2 && Object(2)); console.log(Object(1) && true && 1 && null && 2 && Object(2)); console.log(2 == Object(1) || 0 || void 0 || null); console.log(2 == Object(1) || 0 || void 0 || null || Object(2)); console.log(2 == Object(1) || 0 || void 0 || "ok" || null || Object(2)); } expect: { console.log(Object(1) && 2); console.log(Object(1) && Object(2)); console.log(Object(1) && null); console.log(2 == Object(1) || null); console.log(2 == Object(1) || Object(2)); console.log(2 == Object(1) || "ok"); } expect_stdout: true expect_warnings: [ "WARN: Dropping side-effect-free && [test/compress/evaluate.js:1,20]", "WARN: Dropping side-effect-free && [test/compress/evaluate.js:2,20]", "WARN: Dropping side-effect-free && [test/compress/evaluate.js:3,20]", "WARN: Condition left of && always false [test/compress/evaluate.js:3,20]", "WARN: Dropping side-effect-free || [test/compress/evaluate.js:4,20]", "WARN: Dropping side-effect-free || [test/compress/evaluate.js:5,20]", "WARN: Dropping side-effect-free || [test/compress/evaluate.js:6,20]", "WARN: Condition left of || always true [test/compress/evaluate.js:6,20]", ] } issue_2822: { options = { evaluate: true, unsafe: true, } input: { console.log([ function() {}, "PASS", "FAIL" ][1]); } expect: { console.log("PASS"); } expect_stdout: "PASS" } string_case: { options = { evaluate: true, unsafe: true, } input: { console.log("İ".toLowerCase().charCodeAt(0)); console.log("I".toLowerCase().charCodeAt(0)); console.log("Ş".toLowerCase().charCodeAt(0)); console.log("Ğ".toLowerCase().charCodeAt(0)); console.log("Ü".toLowerCase().charCodeAt(0)); console.log("Ö".toLowerCase().charCodeAt(0)); console.log("Ç".toLowerCase().charCodeAt(0)); console.log("i".toUpperCase().charCodeAt(0)); console.log("ı".toUpperCase().charCodeAt(0)); console.log("ş".toUpperCase().charCodeAt(0)); console.log("ğ".toUpperCase().charCodeAt(0)); console.log("ü".toUpperCase().charCodeAt(0)); console.log("ö".toUpperCase().charCodeAt(0)); console.log("ç".toUpperCase().charCodeAt(0)); } expect: { console.log(105); console.log(105); console.log(351); console.log(287); console.log(252); console.log(246); console.log(231); console.log(73); console.log(73); console.log(350); console.log(286); console.log(220); console.log(214); console.log(199); } expect_stdout: [ "105", "105", "351", "287", "252", "246", "231", "73", "73", "350", "286", "220", "214", "199", ] } issue_2916_1: { options = { evaluate: true, reduce_vars: true, unsafe: true, } input: { var c = "PASS"; (function(a, b) { (function(d) { d[0] = 1; })(b); a == b && (c = "FAIL"); })("", []); console.log(c); } expect: { var c = "PASS"; (function(a, b) { (function(d) { d[0] = 1; })(b); a == b && (c = "FAIL"); })("", []); console.log(c); } expect_stdout: "PASS" } issue_2916_2: { options = { collapse_vars: true, evaluate: true, inline: true, reduce_vars: true, side_effects: true, unsafe: true, unused: true, } input: { var c = "FAIL"; (function(b) { (function(d) { d[0] = 1; })(b); +b && (c = "PASS"); })([]); console.log(c); } expect: { var c = "FAIL"; (function(b) { b[0] = 1; +b && (c = "PASS"); })([]); console.log(c); } expect_stdout: "PASS" } issue_2919: { options = { evaluate: true, unsafe: true, } input: { console.log([ function() {} ].toString()); } expect: { console.log("function(){}"); } } issue_2926_1: { options = { evaluate: true, reduce_vars: true, unsafe: true, } input: { (function f(a) { console.log(f.name.length, f.length); })(); } expect: { (function f(a) { console.log(1, 1); })(); } expect_stdout: "1 1" } issue_2926_2: { options = { evaluate: true, unsafe: true, } input: { console.log(typeof function() {}.valueOf()); } expect: { console.log("function"); } expect_stdout: "function" } issue_2968_1: { options = { collapse_vars: true, evaluate: true, inline: true, passes: 2, reduce_vars: true, unused: true, } input: { var c = "FAIL"; (function() { (function(a, b) { a <<= 0; a && (a[(c = "PASS", 0 >>> (b += 1))] = 0); })(42, -42); })(); console.log(c); } expect: { var c = "FAIL"; (function() { b = -(a = 42), void ((a <<= 0) && (a[(c = "PASS", 0 >>> (b += 1))] = 0)); var a, b; })(); console.log(c); } expect_stdout: "PASS" } issue_2968_2: { options = { assignments: true, collapse_vars: true, evaluate: true, inline: true, passes: 2, reduce_vars: true, side_effects: true, unused: true, } input: { var c = "FAIL"; (function() { (function(a, b) { a <<= 0; a && (a[(c = "PASS", 0 >>> (b += 1))] = 0); })(42, -42); })(); console.log(c); } expect: { var c = "FAIL"; (function() { a = 42, ((a <<= 0) && (a[(c = "PASS", 0)] = 0)); var a; })(); console.log(c); } expect_stdout: "PASS" } truthy_conditionals: { options = { conditionals: true, evaluate: true, } input: { if (a = {}) x(); (b = /foo/) && y(); (c = function() {}) || z(); } expect: { a = {}, x(); b = /foo/, y(); c = function() {}; } } truthy_loops: { options = { evaluate: true, loops: true, } input: { while ([]) x(); do { y(); } while(a = {}); } expect: { for (;;) { []; x(); } for (;;) { y(); a = {}; } } } if_increment: { options = { evaluate: true, reduce_vars: true, unused: true, } input: { console.log(function(a) { if (console) return ++a; }(0)); } expect: { console.log(function(a) { if (console) return 1; }()); } expect_stdout: "1" } try_increment: { options = { evaluate: true, reduce_vars: true, unused: true, } input: { console.log(function(a) { try { return ++a; } catch (e) {} }(0)); } expect: { console.log(function(a) { try { return 1; } catch (e) {} }()); } expect_stdout: "1" } unsafe_escaped: { options = { evaluate: true, inline: true, passes: 3, reduce_vars: true, side_effects: true, unsafe: true, unused: true, } input: { (function(a) { console.log(function(index) { return a[index]; }(function(term) { return a.indexOf(term); }("PASS"))); })([ "PASS" ]); } expect: { console.log("PASS"); } expect_stdout: "PASS" } unsafe_string_replace: { options = { evaluate: true, unsafe: true, } input: { "foo".replace("f", function() { console.log("PASS"); }); } expect: { "foo".replace("f", function() { console.log("PASS"); }); } expect_stdout: "PASS" } issue_3387_1: { options = { evaluate: true, } input: { console.log(1 + (2 + "3"[4])); } expect: { console.log(1 + (2 + "3"[4])); } expect_stdout: "NaN" } issue_3387_2: { options = { evaluate: true, unsafe: true, } input: { console.log(1 + (2 + "3"[4])); } expect: { console.log(NaN); } expect_stdout: "NaN" } UglifyJS2-3.6.3/test/compress/functions.js000066400000000000000000002107231355252637300205010ustar00rootroot00000000000000non_ascii_function_identifier_name: { input: { function fooλ(δλ) {} function λ(δλ) {} (function λ(δλ) {})() } expect_exact: "function fooλ(δλ){}function λ(δλ){}(function λ(δλ){})();" } iifes_returning_constants_keep_fargs_true: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, if_return: true, inline: true, join_vars: true, keep_fargs: true, reduce_funcs: true, reduce_vars: true, side_effects: true, unused: true, } input: { (function(){ return -1.23; }()); console.log( function foo(){ return "okay"; }() ); console.log( function foo(x, y, z){ return 123; }() ); console.log( function(x, y, z){ return z; }() ); console.log( function(x, y, z){ if (x) return y; return z; }(1, 2, 3) ); console.log( function(x, y){ return x * y; }(2, 3) ); console.log( function(x, y){ return x * y; }(2, 3, a(), b()) ); } expect: { console.log("okay"); console.log(123); console.log(void 0); console.log(2); console.log(6); console.log((a(), b(), 6)); } expect_stdout: true } iifes_returning_constants_keep_fargs_false: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, if_return: true, inline: true, join_vars: true, keep_fargs: false, reduce_funcs: true, reduce_vars: true, side_effects: true, unused: true, } input: { (function(){ return -1.23; }()); console.log( function foo(){ return "okay"; }() ); console.log( function foo(x, y, z){ return 123; }() ); console.log( function(x, y, z){ return z; }() ); console.log( function(x, y, z){ if (x) return y; return z; }(1, 2, 3) ); console.log( function(x, y){ return x * y; }(2, 3) ); console.log( function(x, y){ return x * y; }(2, 3, a(), b()) ); } expect: { console.log("okay"); console.log(123); console.log(void 0); console.log(2); console.log(6); console.log((a(), b(), 6)); } expect_stdout: true } issue_485_crashing_1530: { options = { conditionals: true, dead_code: true, evaluate: true, inline: true, side_effects: true, } input: { (function(a) { if (true) return; var b = 42; })(this); } expect: {} } issue_1841_1: { options = { keep_fargs: false, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, unused: true, } input: { var b = 10; !function(arg) { for (var key in "hi") var n = arg.baz, n = [ b = 42 ]; }(--b); console.log(b); } expect: { var b = 10; !function() { for (var key in "hi") b = 42; }(--b); console.log(b); } expect_exact: "42" } issue_1841_2: { options = { keep_fargs: false, pure_getters: false, reduce_funcs: true, reduce_vars: true, unused: true, } input: { var b = 10; !function(arg) { for (var key in "hi") var n = arg.baz, n = [ b = 42 ]; }(--b); console.log(b); } expect: { var b = 10; !function(arg) { for (var key in "hi") arg.baz, b = 42; }(--b); console.log(b); } expect_exact: "42" } function_returning_constant_literal: { options = { inline: true, passes: 2, properties: true, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { function greeter() { return { message: 'Hello there' }; } var greeting = greeter(); console.log(greeting.message); } expect: { console.log("Hello there"); } expect_stdout: "Hello there" } hoist_funs: { options = { hoist_funs: true, } input: { console.log(1, typeof f, typeof g); if (console.log(2, typeof f, typeof g)) console.log(3, typeof f, typeof g); else { console.log(4, typeof f, typeof g); function f() {} console.log(5, typeof f, typeof g); } function g() {} console.log(6, typeof f, typeof g); } expect: { function f() {} function g() {} console.log(1, typeof f, typeof g); if (console.log(2, typeof f, typeof g)) console.log(3, typeof f, typeof g); else { console.log(4, typeof f, typeof g); console.log(5, typeof f, typeof g); } console.log(6, typeof f, typeof g); } expect_stdout: [ "1 'function' 'function'", "2 'function' 'function'", "4 'function' 'function'", "5 'function' 'function'", "6 'function' 'function'", ] node_version: "<=4" } issue_203: { options = { keep_fargs: false, side_effects: true, unsafe_Function: true, unused: true, } input: { var m = {}; var fn = Function("require", "module", "exports", "module.exports = 42;"); fn(null, m, m.exports); console.log(m.exports); } expect: { var m = {}; var fn = Function("n,o", "o.exports=42"); fn(null, m, m.exports); console.log(m.exports); } expect_stdout: "42" } issue_2084: { options = { collapse_vars: true, conditionals: true, evaluate: true, inline: true, passes: 2, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { var c = 0; !function() { !function(c) { c = 1 + c; var c = 0; function f14(a_1) { if (c = 1 + c, 0 !== 23..toString()) c = 1 + c, a_1 && (a_1[0] = 0); } f14(); }(-1); }(); console.log(c); } expect: { var c = 0; !function() { var c; c = 1 + (c = -1), c = 1 + (c = 0), 0 !== 23..toString() && (c = 1 + c); }(), console.log(c); } expect_stdout: "0" } issue_2097: { options = { negate_iife: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function f() { try { throw 0; } catch (e) { console.log(arguments[0]); } } f(1); } expect: { !function() { try { throw 0; } catch (e) { console.log(arguments[0]); } }(1); } expect_stdout: "1" } issue_2101: { options = { inline: true, } input: { a = {}; console.log(function() { return function() { return this.a; }(); }() === function() { return a; }()); } expect: { a = {}; console.log(function() { return this.a; }() === a); } expect_stdout: "true" } inner_ref: { options = { inline: true, unused: true, } input: { console.log(function(a) { return function() { return a; }(); }(1), function(a) { return function(a) { return a; }(); }(2)); } expect: { console.log(function(a) { return a; }(1), function(a) { return a; }()); } expect_stdout: "1 undefined" } issue_2107: { options = { assignments: true, collapse_vars: true, inline: true, passes: 3, sequences: true, side_effects: true, unused: true, } input: { var c = 0; !function() { c++; }(c++ + new function() { this.a = 0; var a = (c = c + 1) + (c = 1 + c); return c++ + a; }()); console.log(c); } expect: { var c = 0; c++, new function() { this.a = 0, c = 1 + (c += 1), c++; }(), c++, console.log(c); } expect_stdout: "5" } issue_2114_1: { options = { assignments: true, collapse_vars: true, if_return: true, inline: true, keep_fargs: false, side_effects: true, unused: true, } input: { var c = 0; !function(a) { a = 0; }([ { 0: c = c + 1, length: c = 1 + c }, typeof void function a() { var b = function f1(a) { }(b && (b.b += (c = c + 1, 0))); }() ]); console.log(c); } expect: { var c = 0; c = 1 + (c += 1), function() { var b = void (b && (b.b += (c += 1, 0))); }(); console.log(c); } expect_stdout: "2" } issue_2114_2: { options = { assignments: true, collapse_vars: true, if_return: true, inline: true, keep_fargs: false, passes: 2, side_effects: true, unused: true, } input: { var c = 0; !function(a) { a = 0; }([ { 0: c = c + 1, length: c = 1 + c }, typeof void function a() { var b = function f1(a) { }(b && (b.b += (c = c + 1, 0))); }() ]); console.log(c); } expect: { var c = 0; c = 1 + (c += 1), function() { var b = void (b && (b.b += (c += 1, 0))); }(); console.log(c); } expect_stdout: "2" } issue_2428: { options = { collapse_vars: true, inline: true, passes: 3, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unsafe: true, unused: true, } input: { function bar(k) { console.log(k); } function foo(x) { return bar(x); } function baz(a) { foo(a); } baz(42); baz("PASS"); } expect: { function baz(a) { console.log(a); } baz(42); baz("PASS"); } expect_stdout: [ "42", "PASS", ] } issue_2531_1: { options = { evaluate: true, inline: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { function outer() { function inner(value) { function closure() { return value; } return function() { return closure(); }; } return inner("Hello"); } console.log("Greeting:", outer()()); } expect: { function outer() { return value = "Hello", function() { return value; }; var value; } console.log("Greeting:", outer()()); } expect_stdout: "Greeting: Hello" } issue_2531_2: { options = { evaluate: true, inline: true, passes: 3, reduce_funcs: true, reduce_vars: true, side_effects: true, unused: true, } input: { function outer() { function inner(value) { function closure() { return value; } return function() { return closure(); }; } return inner("Hello"); } console.log("Greeting:", outer()()); } expect: { function outer() { return function() { return "Hello"; }; } console.log("Greeting:", outer()()); } expect_stdout: "Greeting: Hello" } issue_2531_3: { options = { evaluate: true, inline: true, passes: 3, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { function outer() { function inner(value) { function closure() { return value; } return function() { return closure(); }; } return inner("Hello"); } console.log("Greeting:", outer()()); } expect: { console.log("Greeting:", "Hello"); } expect_stdout: "Greeting: Hello" } empty_body: { options = { reduce_vars: true, side_effects: true, } input: { function f() { function noop() {} noop(); return noop; } } expect: { function f() { function noop() {} return noop; } } } inline_loop_1: { options = { inline: true, reduce_vars: true, toplevel: true, unused: true, } input: { function f() { return x(); } for (;;) f(); } expect: { for (;;) x(); } } inline_loop_2: { options = { inline: true, reduce_vars: true, toplevel: true, unused: true, } input: { for (;;) f(); function f() { return x(); } } expect: { for (;;) x(); } } inline_loop_3: { options = { inline: true, reduce_vars: true, toplevel: true, unused: true, } input: { var f = function() { return x(); }; for (;;) f(); } expect: { for (;;) x(); } } inline_loop_4: { options = { inline: true, reduce_vars: true, toplevel: true, unused: true, } input: { for (;;) f(); var f = function() { return x(); }; } expect: { for (;;) f(); var f = function() { return x(); }; } } issue_2476: { options = { inline: true, reduce_vars: true, toplevel: true, unused: true, } input: { function foo(x, y, z) { return x < y ? x * y + z : x * z - y; } for (var sum = 0, i = 0; i < 10; i++) sum += foo(i, i + 1, 3 * i); console.log(sum); } expect: { for (var sum = 0, i = 0; i < 10; i++) sum += (x = i, y = i + 1, z = 3 * i, x < y ? x * y + z : x * z - y); var x, y, z; console.log(sum); } expect_stdout: "465" } issue_2601_1: { options = { inline: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { var a = "FAIL"; (function() { function f(b) { function g(b) { b && b(); } g(); (function() { b && (a = "PASS"); })(); } f("foo"); })(); console.log(a); } expect: { var a = "FAIL"; (function() { var b; b = "foo", function(b) { b && b(); }(), b && (a = "PASS"); })(), console.log(a); } expect_stdout: "PASS" } issue_2601_2: { rename = true options = { evaluate: true, inline: true, passes: 3, reduce_vars: true, sequences: true, side_effects: true, unused: true, } mangle = {} input: { var a = "FAIL"; (function() { function f(b) { function g(b) { b && b(); } g(); (function() { b && (a = "PASS"); })(); } f("foo"); })(); console.log(a); } expect: { var a = "FAIL"; a = "PASS", console.log(a); } expect_stdout: "PASS" } issue_2604_1: { options = { inline: true, side_effects: true, unused: true, } input: { var a = "FAIL"; (function() { try { throw 1; } catch (b) { (function f(b) { b && b(); })(); b && (a = "PASS"); } })(); console.log(a); } expect: { var a = "FAIL"; (function() { try { throw 1; } catch (b) { (function(b) { b && b(); })(); b && (a = "PASS"); } })(); console.log(a); } expect_stdout: "PASS" } issue_2604_2: { rename = true options = { evaluate: true, inline: true, passes: 3, reduce_vars: true, side_effects: true, unused: true, } mangle = {} input: { var a = "FAIL"; (function() { try { throw 1; } catch (b) { (function f(b) { b && b(); })(); b && (a = "PASS"); } })(); console.log(a); } expect: { var a = "FAIL"; (function() { try { throw 1; } catch (o) { o && (a = "PASS"); } })(); console.log(a); } expect_stdout: "PASS" } unsafe_apply_1: { options = { inline: true, passes: 2, reduce_vars: true, side_effects: true, unsafe: true, unused: true, } input: { (function(a, b) { console.log(a, b); }).apply("foo", [ "bar" ]); (function(a, b) { console.log(this, a, b); }).apply("foo", [ "bar" ]); (function(a, b) { console.log(a, b); }).apply("foo", [ "bar" ], "baz"); } expect: { console.log("bar", void 0); (function(a, b) { console.log(this, a, b); }).call("foo", "bar"); (function(a, b) { console.log(a, b); }).apply("foo", [ "bar" ], "baz"); } expect_stdout: true } unsafe_apply_2: { options = { reduce_vars: true, side_effects: true, toplevel: true, unsafe: true, } input: { function foo() { console.log(a, b); } var bar = function(a, b) { console.log(this, a, b); } (function() { foo.apply("foo", [ "bar" ]); bar.apply("foo", [ "bar" ]); })(); } expect: { function foo() { console.log(a, b); } var bar = function(a, b) { console.log(this, a, b); } (function() { foo("bar"); bar.call("foo", "bar"); })(); } expect_stdout: true } unsafe_call_1: { options = { inline: true, passes: 2, reduce_vars: true, side_effects: true, unsafe: true, unused: true, } input: { (function(a, b) { console.log(a, b); }).call("foo", "bar"); (function(a, b) { console.log(this, a, b); }).call("foo", "bar"); } expect: { console.log("bar", void 0); (function(a, b) { console.log(this, a, b); }).call("foo", "bar"); } expect_stdout: true } unsafe_call_2: { options = { reduce_vars: true, side_effects: true, toplevel: true, unsafe: true, } input: { function foo() { console.log(a, b); } var bar = function(a, b) { console.log(this, a, b); } (function() { foo.call("foo", "bar"); bar.call("foo", "bar"); })(); } expect: { function foo() { console.log(a, b); } var bar = function(a, b) { console.log(this, a, b); } (function() { foo("bar"); bar.call("foo", "bar"); })(); } expect_stdout: true } unsafe_call_3: { options = { side_effects: true, unsafe: true, } input: { console.log(function() { return arguments[0] + eval("arguments")[1]; }.call(0, 1, 2)); } expect: { console.log(function() { return arguments[0] + eval("arguments")[1]; }(1, 2)); } expect_stdout: "3" } issue_2616: { options = { evaluate: true, inline: true, reduce_vars: true, side_effects: true, unused: true, } input: { var c = "FAIL"; (function() { function f() { function g(NaN) { (true << NaN) - 0/0 || (c = "PASS"); } g([]); } f(); })(); console.log(c); } expect: { var c = "FAIL"; !function(NaN) { (true << NaN) - 0/0 || (c = "PASS"); }([]); console.log(c); } expect_stdout: "PASS" } issue_2620_1: { options = { inline: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { var c = "FAIL"; (function() { function f(a) { var b = function g(a) { a && a(); }(); if (a) { var d = c = "PASS"; } } f(1); })(); console.log(c); } expect: { var c = "FAIL"; !function(a) { if (function(a) { a && a(); }(), a) c = "PASS"; }(1), console.log(c); } expect_stdout: "PASS" } issue_2620_2: { options = { conditionals: true, evaluate: true, inline: true, passes: 2, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { var c = "FAIL"; (function() { function f(a) { var b = function g(a) { a && a(); }(); if (a) { var d = c = "PASS"; } } f(1); })(); console.log(c); } expect: { var c = "FAIL"; c = "PASS", console.log(c); } expect_stdout: "PASS" } issue_2620_3: { options = { evaluate: true, inline: true, reduce_vars: true, side_effects: true, unused: true, } input: { var c = "FAIL"; (function() { function f(a, NaN) { function g() { switch (a) { case a: break; case c = "PASS", NaN: break; } } g(); } f(0/0); })(); console.log(c); } expect: { var c = "FAIL"; !function(a, NaN) { (function() { switch (a) { case a: break; case c = "PASS", NaN: break; } })(); }(NaN); console.log(c); } expect_stdout: "PASS" } issue_2620_4: { rename = true options = { dead_code: true, evaluate: true, inline: true, passes: 2, reduce_vars: true, side_effects: true, switches: true, unused: true, } input: { var c = "FAIL"; (function() { function f(a, NaN) { function g() { switch (a) { case a: break; case c = "PASS", NaN: break; } } g(); } f(0/0); })(); console.log(c); } expect: { var c = "FAIL"; !function() { switch (NaN) { case void (c = "PASS"): } }(); console.log(c); } expect_stdout: "PASS" } issue_2630_1: { options = { collapse_vars: true, inline: true, passes: 2, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { var c = 0; (function() { while (f()); function f() { var a = function() { var b = c++, d = c = 1 + c; }(); } })(); console.log(c); } expect: { var c = 0; (function() { while (void (c = 1 + ++c)); })(), console.log(c); } expect_stdout: "2" } issue_2630_2: { options = { assignments: true, collapse_vars: true, inline: true, passes: 2, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { var c = 0; !function() { while (f()) {} function f() { var not_used = function() { c = 1 + c; }(c = c + 1); } }(); console.log(c); } expect: { var c = 0; !function() { while (void (c = 1 + (c += 1))); }(), console.log(c); } expect_stdout: "2" } issue_2630_3: { options = { inline: true, reduce_vars: true, unused: true, } input: { var x = 2, a = 1; (function() { function f1(a) { f2(); --x >= 0 && f1({}); } f1(a++); function f2() { a++; } })(); console.log(a); } expect: { var x = 2, a = 1; (function() { (function f1(a) { f2(); --x >= 0 && f1({}); })(a++); function f2() { a++; } })(); console.log(a); } expect_stdout: "5" } issue_2630_4: { options = { collapse_vars: true, inline: true, reduce_vars: true, side_effects: true, unused: true, } input: { var x = 3, a = 1, b = 2; (function() { (function f1() { while (--x >= 0 && f2()); }()); function f2() { a++ + (b += a); } })(); console.log(a); } expect: { var x = 3, a = 1, b = 2; !function() { while (--x >= 0 && void (b += ++a)); }(); console.log(a); } expect_stdout: "2" } issue_2630_5: { options = { assignments: true, collapse_vars: true, inline: true, reduce_vars: true, unused: true, } input: { var c = 1; !function() { do { c *= 10; } while (f()); function f() { return function() { return (c = 2 + c) < 100; }(c = c + 3); } }(); console.log(c); } expect: { var c = 1; !function() { do { c *= 10; } while ((c = 2 + (c += 3)) < 100); }(); console.log(c); } expect_stdout: "155" } recursive_inline_1: { options = { inline: true, reduce_funcs: true, reduce_vars: true, sequences: true, toplevel: true, unused: true, } input: { function f() { h(); } function g(a) { a(); } function h(b) { g(); if (b) x(); } } expect: {} } recursive_inline_2: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { function f(n) { return n ? n * f(n - 1) : 1; } console.log(f(5)); } expect: { console.log(function f(n) { return n ? n * f(n - 1) : 1; }(5)); } expect_stdout: "120" } issue_2657: { options = { inline: true, passes: 2, reduce_funcs: true, reduce_vars: true, sequences: true, unused: true, } input: { "use strict"; console.log(function f() { return h; function g(b) { return b || b(); } function h(a) { g(a); return a; } }()(42)); } expect: { "use strict"; console.log(function(a) { return b = a, b || b(), a; var b; }(42)); } expect_stdout: "42" } issue_2663_1: { options = { inline: true, reduce_vars: true, unused: true, } input: { (function() { var i, o = {}; function createFn(j) { return function() { console.log(j); }; } for (i in { a: 1, b: 2, c: 3 }) o[i] = createFn(i); for (i in o) o[i](); })(); } expect: { (function() { var i, o = {}; function createFn(j) { return function() { console.log(j); }; } for (i in { a: 1, b: 2, c: 3 }) o[i] = createFn(i); for (i in o) o[i](); })(); } expect_stdout: [ "a", "b", "c", ] } issue_2663_2: { options = { inline: true, reduce_vars: true, side_effects: true, unused: true, } input: { (function() { var i; function fn(j) { return function() { console.log(j); }(); } for (i in { a: 1, b: 2, c: 3 }) fn(i); })(); } expect: { (function() { var i; for (i in { a: 1, b: 2, c: 3 }) j = i, console.log(j); var j; })(); } expect_stdout: [ "a", "b", "c", ] } issue_2663_3: { options = { inline: true, reduce_vars: true, unused: true, } input: { (function() { var outputs = [ { type: 0, target: null, eventName: "ngSubmit", propName: null }, { type: 0, target: null, eventName: "submit", propName: null }, { type: 0, target: null, eventName: "reset", propName: null }, ]; function listenToElementOutputs(outputs) { var handlers = []; for (var i = 0; i < outputs.length; i++) { var output = outputs[i]; var handleEventClosure = renderEventHandlerClosure(output.eventName); handlers.push(handleEventClosure) } var target, name; return handlers; } function renderEventHandlerClosure(eventName) { return function() { return console.log(eventName); }; } listenToElementOutputs(outputs).forEach(function(handler) { return handler() }); })(); } expect: { (function() { function renderEventHandlerClosure(eventName) { return function() { return console.log(eventName); }; } (function(outputs) { var handlers = []; for (var i = 0; i < outputs.length; i++) { var output = outputs[i]; var handleEventClosure = renderEventHandlerClosure(output.eventName); handlers.push(handleEventClosure); } return handlers; })([ { type: 0, target: null, eventName: "ngSubmit", propName: null }, { type: 0, target: null, eventName: "submit", propName: null }, { type: 0, target: null, eventName: "reset", propName: null } ]).forEach(function(handler) { return handler(); }); })(); } expect_stdout: [ "ngSubmit", "submit", "reset", ] } duplicate_argnames: { options = { inline: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { var a = "PASS"; function f(b, b, b) { b && (a = "FAIL"); } f(0, console); console.log(a); } expect: { var a = "PASS"; console, b && (a = "FAIL"); var b; console.log(a); } expect_stdout: "PASS" } loop_init_arg: { options = { inline: true, side_effects: true, toplevel: true, } input: { var a = "PASS"; for (var k in "12") (function(b) { (b >>= 1) && (a = "FAIL"), b = 2; })(); console.log(a); } expect: { var a = "PASS"; for (var k in "12") b = void 0, (b >>= 1) && (a = "FAIL"), b = 2; var b; console.log(a); } expect_stdout: "PASS" } inline_false: { options = { inline: false, side_effects: true, toplevel: true, } input: { (function() { console.log(1); })(); (function(a) { console.log(a); })(2); (function(b) { var c = b; console.log(c); })(3); } expect: { (function() { console.log(1); })(); (function(a) { console.log(a); })(2); (function(b) { var c = b; console.log(c); })(3); } expect_stdout: [ "1", "2", "3", ] } inline_0: { options = { inline: 0, side_effects: true, toplevel: true, } input: { (function() { console.log(1); })(); (function(a) { console.log(a); })(2); (function(b) { var c = b; console.log(c); })(3); } expect: { (function() { console.log(1); })(); (function(a) { console.log(a); })(2); (function(b) { var c = b; console.log(c); })(3); } expect_stdout: [ "1", "2", "3", ] } inline_1: { options = { inline: 1, side_effects: true, toplevel: true, } input: { (function() { console.log(1); })(); (function(a) { console.log(a); })(2); (function(b) { var c = b; console.log(c); })(3); } expect: { console.log(1); (function(a) { console.log(a); })(2); (function(b) { var c = b; console.log(c); })(3); } expect_stdout: [ "1", "2", "3", ] } inline_2: { options = { inline: 2, side_effects: true, toplevel: true, } input: { (function() { console.log(1); })(); (function(a) { console.log(a); })(2); (function(b) { var c = b; console.log(c); })(3); } expect: { console.log(1); a = 2, console.log(a); var a; (function(b) { var c = b; console.log(c); })(3); } expect_stdout: [ "1", "2", "3", ] } inline_3: { options = { inline: 3, side_effects: true, toplevel: true, } input: { (function() { console.log(1); })(); (function(a) { console.log(a); })(2); (function(b) { var c = b; console.log(c); })(3); } expect: { console.log(1); a = 2, console.log(a); var a; b = 3, c = b, console.log(c); var b, c; } expect_stdout: [ "1", "2", "3", ] } inline_true: { options = { inline: true, side_effects: true, toplevel: true, } input: { (function() { console.log(1); })(); (function(a) { console.log(a); })(2); (function(b) { var c = b; console.log(c); })(3); } expect: { console.log(1); a = 2, console.log(a); var a; b = 3, c = b, console.log(c); var b, c; } expect_stdout: [ "1", "2", "3", ] } use_before_init_in_loop: { options = { inline: true, side_effects: true, toplevel: true, } input: { var a = "PASS"; for (var b = 2; --b >= 0;) (function() { var c = function() { return 1; }(c && (a = "FAIL")); })(); console.log(a); } expect: { var a = "PASS"; for (var b = 2; --b >= 0;) c = void 0, c = (c && (a = "FAIL"), 1); var c; console.log(a); } expect_stdout: "PASS" } duplicate_arg_var: { options = { inline: true, toplevel: true, } input: { console.log(function(b) { return b; var b; }("PASS")); } expect: { console.log((b = "PASS", b)); var b; } expect_stdout: "PASS" } issue_2737_1: { options = { inline: true, reduce_vars: true, unused: true, } input: { (function(a) { while (a()); })(function f() { console.log(typeof f); }); } expect: { (function(a) { while (a()); })(function f() { console.log(typeof f); }); } expect_stdout: "function" } issue_2737_2: { options = { inline: true, reduce_vars: true, unused: true, } input: { (function(bar) { for (;bar(); ) break; })(function qux() { return console.log("PASS"), qux; }); } expect: { (function(bar) { for (;bar(); ) break; })(function qux() { return console.log("PASS"), qux; }); } expect_stdout: "PASS" } issue_2783: { options = { collapse_vars: true, conditionals: true, if_return: true, inline: true, reduce_vars: true, unused: true, } input: { (function() { return g; function f(a) { var b = a.b; if (b) return b; return a; } function g(o, i) { while (i--) { console.log(f(o)); } } })()({ b: "PASS" }, 1); } expect: { (function() { return function(o,i) { while (i--) console.log(f(o)); }; function f(a) { var b = a.b; return b || a; } })()({ b: "PASS" },1); } expect_stdout: "PASS" } issue_2898: { options = { collapse_vars: true, inline: true, reduce_vars: true, sequences: true, unused: true, } input: { var c = 0; (function() { while (f()); function f() { var b = (c = 1 + c, void (c = 1 + c)); b && b[0]; } })(); console.log(c); } expect: { var c = 0; (function() { while (b = void 0, void ((b = void (c = 1 + (c = 1 + c))) && b[0])); var b; })(), console.log(c); } expect_stdout: "2" } deduplicate_parenthesis: { input: { ({}).a = b; (({}).a = b)(); (function() {}).a = b; ((function() {}).a = b)(); } expect_exact: "({}).a=b;({}.a=b)();(function(){}).a=b;(function(){}.a=b)();" } drop_lone_use_strict: { options = { directives: true, side_effects: true, } input: { function f1() { "use strict"; } function f2() { "use strict"; function f3() { "use strict"; } } (function f4() { "use strict"; })(); } expect: { function f1() { } function f2() { "use strict"; function f3() { } } } } issue_3166: { options = { directives: true, } input: { "foo"; "use strict"; function f() { "use strict"; "bar"; "use asm"; } } expect: { "use strict"; function f() { "use asm"; } } } issue_3016_1: { options = { inline: true, toplevel: true, } input: { var b = 1; do { (function(a) { return a[b]; var a; })(3); } while (0); console.log(b); } expect: { var b = 1; do { a = 3, a[b]; } while(0); var a; console.log(b); } expect_stdout: "1" } issue_3016_2: { options = { dead_code: true, inline: true, toplevel: true, } input: { var b = 1; do { (function(a) { return a[b]; try { a = 2; } catch (a) { var a; } })(3); } while (0); console.log(b); } expect: { var b = 1; do { a = 3, a[b]; } while(0); var a; console.log(b); } expect_stdout: "1" } issue_3016_2_ie8: { options = { dead_code: true, ie8: true, inline: true, toplevel: true, } input: { var b = 1; do { (function(a) { return a[b]; try { a = 2; } catch (a) { var a; } })(3); } while (0); console.log(b); } expect: { var b = 1; do { a = 3, a[b]; } while(0); var a; console.log(b); } expect_stdout: "1" } issue_3016_3: { options = { dead_code: true, inline: true, toplevel: true, } input: { var b = 1; do { console.log(function() { return a ? "FAIL" : a = "PASS"; try { a = 2; } catch (a) { var a; } }()); } while (b--); } expect: { var b = 1; do { console.log((a = void 0, a ? "FAIL" : a = "PASS")); } while(b--); var a; } expect_stdout: [ "PASS", "PASS", ] } issue_3016_3_ie8: { options = { dead_code: true, ie8: true, inline: true, toplevel: true, } input: { var b = 1; do { console.log(function() { return a ? "FAIL" : a = "PASS"; try { a = 2; } catch (a) { var a; } }()); } while (b--); } expect: { var b = 1; do { console.log((a = void 0, a ? "FAIL" : a = "PASS")); } while(b--); var a; } expect_stdout: [ "PASS", "PASS", ] } issue_3018: { options = { inline: true, side_effects: true, toplevel: true, } input: { var b = 1, c = "PASS"; do { (function() { (function(a) { a = 0 != (a && (c = "FAIL")); })(); })(); } while (b--); console.log(c); } expect: { var b = 1, c = "PASS"; do { a = void 0, a = 0 != (a && (c = "FAIL")); } while (b--); var a; console.log(c); } expect_stdout: "PASS" } issue_3054: { options = { booleans: true, collapse_vars: true, inline: 1, reduce_vars: true, toplevel: true, } input: { "use strict"; function f() { return { a: true }; } console.log(function(b) { b = false; return f(); }().a, f.call().a); } expect: { "use strict"; function f() { return { a: !0 }; } console.log(function(b) { return { a: !(b = !1) }; }().a, f.call().a); } expect_stdout: "true true" } issue_3076: { options = { dead_code: true, inline: true, sequences: true, unused: true, } input: { var c = "PASS"; (function(b) { var n = 2; while (--b + function() { e && (c = "FAIL"); e = 5; return 1; try { var a = 5; } catch (e) { var e; } }().toString() && --n > 0); })(2); console.log(c); } expect: { var c = "PASS"; (function(b) { var n = 2; while (--b + (e = void 0, e && (c = "FAIL"), e = 5, 1).toString() && --n > 0); var e; })(2), console.log(c); } expect_stdout: "PASS" } issue_3125: { options = { inline: true, unsafe: true, } input: { console.log(function() { return "PASS"; }.call()); } expect: { console.log("PASS"); } expect_stdout: "PASS" } issue_3274: { options = { collapse_vars: true, inline: true, join_vars: true, loops: true, reduce_vars: true, unused: true, } input: { (function() { var g = function(a) { var c = a.p, b = c; return b != c; }; while (g(1)) console.log("FAIL"); console.log("PASS"); })(); } expect: { (function() { for (var c; void 0, (c = 1..p) != c;) console.log("FAIL"); console.log("PASS"); })(); } expect_stdout: "PASS" } issue_3297_1: { options = { collapse_vars: true, inline: true, reduce_vars: true, unused: true, } mangle = {} input: { function function1() { var r = { function2: function2 }; function function2() { alert(1234); function function3() { function2(); }; function3(); } return r; } } expect: { function function1() { return { function2: function n() { alert(1234); function t() { n(); } t(); } }; } } } issue_3297_2: { options = { collapse_vars: true, conditionals: true, inline: true, reduce_vars: true, unused: true, } mangle = {} input: { function function1(session) { var public = { processBulk: processBulk }; return public; function processBulk(bulk) { var subparam1 = session(); function processOne(param1) { var param2 = { subparam1: subparam1 }; doProcessOne({ param1: param1, param2: param2, }, function () { processBulk(bulk); }); }; if (bulk && bulk.length > 0) processOne(bulk.shift()); } function doProcessOne(config, callback) { console.log(JSON.stringify(config)); callback(); } } function1(function session() { return 42; }).processBulk([1, 2, 3]); } expect: { function function1(o) { return { processBulk: function t(u) { var r = o(); function n(n) { var o = { subparam1: r }; c({ param1: n, param2: o }, function() { t(u); }); } u && u.length > 0 && n(u.shift()); } }; function c(n, o) { console.log(JSON.stringify(n)); o(); } } function1(function() { return 42; }).processBulk([ 1, 2, 3 ]); } expect_stdout: [ '{"param1":1,"param2":{"subparam1":42}}', '{"param1":2,"param2":{"subparam1":42}}', '{"param1":3,"param2":{"subparam1":42}}', ] } issue_3297_3: { options = { collapse_vars: true, comparisons: true, conditionals: true, inline: true, join_vars: true, passes: 3, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } mangle = {} input: { function function1(session) { var public = { processBulk: processBulk }; return public; function processBulk(bulk) { var subparam1 = session(); function processOne(param1) { var param2 = { subparam1: subparam1 }; doProcessOne({ param1: param1, param2: param2, }, function () { processBulk(bulk); }); }; if (bulk && bulk.length > 0) processOne(bulk.shift()); } function doProcessOne(config, callback) { console.log(JSON.stringify(config)); callback(); } } function1(function session() { return 42; }).processBulk([1, 2, 3]); } expect: { function function1(u) { return { processBulk: function n(r) { var o, t = u(); r && 0 < r.length && (o = { param1: r.shift(), param2: { subparam1: t } }, console.log(JSON.stringify(o)), n(r)); } }; } function1(function() { return 42; }).processBulk([ 1, 2, 3 ]); } expect_stdout: [ '{"param1":1,"param2":{"subparam1":42}}', '{"param1":2,"param2":{"subparam1":42}}', '{"param1":3,"param2":{"subparam1":42}}', ] } cross_references_1: { options = { inline: true, reduce_vars: true, unused: true, } input: { var Math = { square: function(n) { return n * n; } }; console.log((function(factory) { return factory(); })(function() { return function(Math) { return function(n) { return Math.square(n); }; }(Math); })(3)); } expect: { var Math = { square: function(n) { return n * n; } }; console.log(function(Math) { return function(n) { return Math.square(n); }; }(Math)(3)); } expect_stdout: "9" } cross_references_2: { options = { collapse_vars: true, evaluate: true, hoist_props: true, inline: true, passes: 4, pure_getters: true, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, unused: true, } input: { var Math = { square: function(n) { return n * n; } }; console.log((function(factory) { return factory(); })(function() { return function(Math) { return function(n) { return Math.square(n); }; }(Math); })(3)); } expect: { console.log(9); } expect_stdout: "9" } cross_references_3: { options = { inline: true, reduce_vars: true, unused: true, } input: { var Math = { square: function(n) { return n * n; }, cube: function(n) { return n * n * n; } }; console.log(function(factory) { return factory(); }(function() { return function(Math) { return function(n) { Math = { square: function(x) { return "(SQUARE" + x + ")"; }, cube: function(x) { return "(CUBE" + x + ")"; } }; return Math.square(n) + Math.cube(n); }; }(Math); })(2)); console.log(Math.square(3), Math.cube(3)); } expect: { var Math = { square: function(n) { return n * n; }, cube: function(n) { return n * n * n; } }; console.log(function(Math) { return function(n) { Math = { square: function(x) { return "(SQUARE" + x + ")"; }, cube: function(x) { return "(CUBE" + x + ")"; } }; return Math.square(n) + Math.cube(n); }; }(Math)(2)); console.log(Math.square(3), Math.cube(3)); } expect_stdout: [ "(SQUARE2)(CUBE2)", "9 27", ] } loop_inline: { options = { inline: true, reduce_vars: true, unused: true, } input: { console.log(function(o) { function g(p) { return o[p]; } function h(q) { while (g(q)); } return h; }([ 1, "foo", 0 ])(2)); } expect: { console.log(function(o) { return function(q) { while (p = q, o[p]); var p; }; }([ 1, "foo", 0 ])(2)); } expect_stdout: "undefined" } functions: { options = { functions: true, reduce_vars: true, unused: true, } input: { !function() { var a = function a() { return a && "a"; }; var b = function x() { return !!x; }; var c = function(c) { return c; }; if (c(b(a()))) { var d = function() {}; var e = function y() { return typeof y; }; var f = function(f) { return f; }; console.log(a(d()), b(e()), c(f(42)), typeof d, e(), typeof f); } }(); } expect: { !function() { function a() { return a && "a"; } function b() { return !!b; } var c = function(c) { return c; }; if (c(b(a()))) { function d() {} function e() { return typeof e; } var f = function(f) { return f; }; console.log(a(d()), b(e()), c(f(42)), typeof d, e(), typeof f); } }(); } expect_stdout: "a true 42 function function function" } functions_use_strict: { options = { functions: true, reduce_vars: true, unused: true, } input: { "use strict"; !function() { var a = function a() { return a && "a"; }; var b = function x() { return !!x; }; var c = function(c) { return c; }; if (c(b(a()))) { var d = function() {}; var e = function y() { return typeof y; }; var f = function(f) { return f; }; console.log(a(d()), b(e()), c(f(42)), typeof d, e(), typeof f); } }(); } expect: { "use strict"; !function() { function a() { return a && "a"; } function b() { return !!b; } var c = function(c) { return c; }; if (c(b(a()))) { var d = function() {}; var e = function y() { return typeof y; }; var f = function(f) { return f; }; console.log(a(d()), b(e()), c(f(42)), typeof d, e(), typeof f); } }(); } expect_stdout: "a true 42 function function function" } issue_2437: { options = { collapse_vars: true, conditionals: true, functions: true, inline: true, join_vars: true, passes: 2, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, unused: true, } input: { function foo() { return bar(); } function bar() { if (xhrDesc) { var req = new XMLHttpRequest(); var result = !!req.onreadystatechange; Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', xhrDesc || {}); return result; } else { var req = new XMLHttpRequest(); var detectFunc = function(){}; req.onreadystatechange = detectFunc; var result = req[SYMBOL_FAKE_ONREADYSTATECHANGE_1] === detectFunc; req.onreadystatechange = null; return result; } } console.log(foo()); } expect: { console.log(function() { if (xhrDesc) { var result = !!(req = new XMLHttpRequest()).onreadystatechange; return Object.defineProperty(XMLHttpRequest.prototype, "onreadystatechange", xhrDesc || {}), result; } function detectFunc() {} var req = new XMLHttpRequest(); return req.onreadystatechange = detectFunc, result = req[SYMBOL_FAKE_ONREADYSTATECHANGE_1] === detectFunc, req.onreadystatechange = null, result; }()); } } issue_2485: { options = { functions: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { var foo = function(bar) { var n = function(a, b) { return a + b; }; var sumAll = function(arg) { return arg.reduce(n, 0); }; var runSumAll = function(arg) { return sumAll(arg); }; bar.baz = function(arg) { var n = runSumAll(arg); return (n.get = 1), n; }; return bar; }; var bar = foo({}); console.log(bar.baz([1, 2, 3])); } expect: { var foo = function(bar) { function n(a, b) { return a + b; } function runSumAll(arg) { return function(arg) { return arg.reduce(n, 0); }(arg); } bar.baz = function(arg) { var n = runSumAll(arg); return (n.get = 1), n; }; return bar; }; var bar = foo({}); console.log(bar.baz([1, 2, 3])); } expect_stdout: "6" } issue_3364: { options = { functions: true, reduce_vars: true, toplevel: true, unused: true, } mangle = {} input: { var s = 2, a = 100, b = 10, c = 0; function f(p, e, r) { try { for (var i = 1; i-- > 0;) var a = function(x) { function g(y) { y && y[a++]; } var x = g(--s >= 0 && f(c++)); for (var j = 1; --j > 0;); }(); } catch (e) { try { return; } catch (z) { for (var k = 1; --k > 0;) { for (var l = 1; l > 0; --l) { var n = function() {}; for (var k in n) var o = (n, k); } } } } } var r = f(); console.log(c); } expect: { var s = 2, c = 0; (function n(r, o, a) { try { for (var f = 1; f-- >0;) var t = function(r) { (function(r) { r && r[t++]; })(--s >= 0 && n(c++)); for (var o = 1; --o > 0;); }(); } catch (o) { try { return; } catch (r) { for (var v = 1; --v > 0;) for (var i = 1; i > 0;--i) { function u() {} for (var v in u); } } } })(); console.log(c); } expect_stdout: "2" } issue_3366: { options = { functions: true, inline: true, reduce_vars: true, toplevel: true, unused: true, } input: { function f() { function g() { return function() {}; } var a = g(); (function() { this && a && console.log("PASS"); })(); } f(); } expect: { void function() { this && a && console.log("PASS"); }(); function a() {} } expect_stdout: "PASS" } issue_3371: { options = { functions: true, inline: true, reduce_vars: true, side_effects: true, unused: true, } input: { (function() { var a = function f() { (function() { console.log(typeof f); })(); }; while (a()); })(); } expect: { (function() { function a() { console.log(typeof a); } while (a()); })(); } expect_stdout: "function" } class_iife: { options = { inline: true, sequences: true, toplevel: true, } input: { var A = function() { function B() {} B.prototype.m = function() { console.log("PASS"); }; return B; }(); new A().m(); } expect: { var A = (B.prototype.m = function() { console.log("PASS"); }, B); function B() {} new A().m(); } expect_stdout: "PASS" } issue_3400: { options = { collapse_vars: true, inline: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function(f) { console.log(f()()[0].p); })(function() { function g() { function h(u) { var o = { p: u }; return console.log(o[g]), o; } function e() { return [ 42 ].map(function(v) { return h(v); }); } return e(); } return g; }); } expect: { void console.log(function g() { function e() { return [42].map(function(v) { return o = { p: v }, console.log(o[g]) , o; var o; }); } return e(); }()[0].p); } expect_stdout: [ "undefined", "42", ] } issue_3402: { options = { evaluate: true, functions: true, reduce_vars: true, side_effects: true, toplevel: true, typeofs: true, unused: true, } input: { var f = function f() { f = 42; console.log(typeof f); }; "function" == typeof f && f(); "function" == typeof f && f(); console.log(typeof f); } expect: { var f = function f() { f = 42; console.log(typeof f); }; f(); f(); console.log(typeof f); } expect_stdout: [ "function", "function", "function", ] } issue_3439: { options = { inline: true, } input: { console.log(typeof function() { return function(a) { function a() {} return a; }(42); }()); } expect: { console.log(typeof function(a) { function a() {} return a; }(42)); } expect_stdout: "function" } issue_3444: { options = { inline: true, reduce_vars: true, unused: true, } input: { (function(h) { return f; function f() { g(); } function g() { h("PASS"); } })(console.log)(); } expect: { (function(h) { return function() { void h("PASS"); }; })(console.log)(); } expect_stdout: "PASS" } UglifyJS2-3.6.3/test/compress/global_defs.js000066400000000000000000000105251355252637300207300ustar00rootroot00000000000000must_replace: { options = { global_defs: { D: "foo bar", }, } input: { console.log(D); } expect: { console.log("foo bar"); } } keyword: { options = { global_defs: { undefined: 0, NaN: 1, Infinity: 2, }, } input: { console.log(undefined, NaN, Infinity); } expect: { console.log(0, 1, 2); } } object: { options = { evaluate: true, global_defs: { CONFIG: { DEBUG: [ 0 ], VALUE: 42, }, }, side_effects: true, unsafe: true, } input: { function f(CONFIG) { // CONFIG not global - do not replace return CONFIG.VALUE; } function g() { var CONFIG = { VALUE: 1 }; // CONFIG not global - do not replace return CONFIG.VALUE; } function h() { return CONFIG.VALUE; } if (CONFIG.DEBUG[0]) console.debug("foo"); } expect: { function f(CONFIG) { return CONFIG.VALUE; } function g() { var CONFIG = { VALUE: 1 }; return CONFIG.VALUE; } function h() { return 42; } if (0) console.debug("foo"); } } expanded: { options = { global_defs: { "CONFIG.DEBUG": [ 0 ], "CONFIG.VALUE": 42, }, } input: { function f(CONFIG) { // CONFIG not global - do not replace return CONFIG.VALUE; } function g() { var CONFIG = { VALUE: 1 }; // CONFIG not global - do not replace return CONFIG.VALUE; } function h() { return CONFIG.VALUE; } if (CONFIG.DEBUG[0]) console.debug("foo"); } expect: { function f(CONFIG) { return CONFIG.VALUE; } function g() { var CONFIG = { VALUE: 1 }; return CONFIG.VALUE; } function h() { return 42; } if ([0][0]) console.debug("foo"); } } mixed: { options = { evaluate: true, global_defs: { "CONFIG.VALUE": 42, "FOO.BAR": "moo", }, properties: true, } input: { var FOO = { BAR: 0 }; console.log(FOO.BAR); console.log(++CONFIG.DEBUG); console.log(++CONFIG.VALUE); console.log(++CONFIG["VAL" + "UE"]); console.log(++DEBUG[CONFIG.VALUE]); CONFIG.VALUE.FOO = "bar"; console.log(CONFIG); } expect: { var FOO = { BAR: 0 }; console.log("moo"); console.log(++CONFIG.DEBUG); console.log(++CONFIG.VALUE); console.log(++CONFIG.VALUE); console.log(++DEBUG[42]); CONFIG.VALUE.FOO = "bar"; console.log(CONFIG); } expect_warnings: [ "WARN: global_defs CONFIG.VALUE redefined [test/compress/global_defs.js:4,22]", "WARN: global_defs CONFIG.VALUE redefined [test/compress/global_defs.js:7,8]", ] } issue_1801: { options = { booleans: true, global_defs: { "CONFIG.FOO.BAR": true, }, } input: { console.log(CONFIG.FOO.BAR); } expect: { console.log(!0); } } issue_1986: { options = { global_defs: { "@alert": "console.log", }, } input: { alert(42); } expect: { console.log(42); } } issue_2167: { options = { conditionals: true, dead_code: true, evaluate: true, global_defs: { "@isDevMode": "function(){}", }, passes: 2, side_effects: true, } input: { if (isDevMode()) { greetOverlord(); } doWork(); } expect: { doWork(); } } issue_3217: { options = { collapse_vars: true, global_defs: { "@o": "{fn:function(){var a=42;console.log(a)}}", }, inline: true, properties: true, reduce_vars: true, side_effects: true, unused: true, } input: { o.fn(); } expect: { console.log(42); } } UglifyJS2-3.6.3/test/compress/hoist_props.js000066400000000000000000000425771355252637300210540ustar00rootroot00000000000000issue_2377_1: { options = { evaluate: true, hoist_props: true, inline: true, reduce_funcs: 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, hoist_props: true, inline: true, passes: 2, reduce_funcs: 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: { console.log(1, (x = 3, x * x * x)); var x; } expect_stdout: "1 27" } issue_2377_3: { options = { evaluate: true, hoist_props: true, inline: true, passes: 4, reduce_funcs: true, reduce_vars: true, side_effects: 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_funcs: 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_funcs: 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_funcs: 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_funcs: 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_funcs: 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_funcs: true, reduce_vars: true, toplevel: true, } input: { var o = { p: 1, "+": function(x) { return x; }, "-": function(x) { return x + 1; } }, o__$0 = 2, o__$1 = 3; console.log(o.p === o.p, o["+"](4), o["-"](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_funcs: true, reduce_vars: true, toplevel: true, } input: { var o = { p: 1, "+": function(x) { return x; }, "-": function(x) { return x + 1; } }, o__$0 = 2, o__$1 = 3; console.log(o.p === o.p, o["+"](4), o["-"](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_funcs: true, 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_funcs: true, 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_funcs: true, 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_funcs: true, 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" } issue_2473_1: { options = { hoist_props: false, reduce_vars: true, top_retain: [ "x", "y" ], toplevel: true, unused: true, } input: { var x = {}; var y = []; var z = {}; } expect: { var x = {}; var y = []; } } issue_2473_2: { options = { hoist_props: true, reduce_vars: true, top_retain: [ "x", "y" ], toplevel: true, unused: true, } input: { var x = {}; var y = []; var z = {}; } expect: { var x = {}; var y = []; } } issue_2473_3: { options = { hoist_props: true, reduce_vars: true, top_retain: "o", toplevel: true, unused: true, } input: { var o = { a: 1, b: 2, }; console.log(o.a, o.b); } expect: { var o = { a: 1, b: 2, }; console.log(o.a, o.b); } expect_stdout: "1 2" } issue_2473_4: { options = { hoist_props: true, reduce_vars: true, top_retain: "o", toplevel: true, unused: true, } input: { (function() { var o = { a: 1, b: 2, }; console.log(o.a, o.b); })(); } expect: { (function() { var o_a = 1, o_b = 2; console.log(o_a, o_b); })(); } expect_stdout: "1 2" } issue_2508_1: { options = { collapse_vars: true, hoist_props: true, reduce_vars: true, toplevel: true, unused: true, } input: { var o = { a: [ 1 ], f: function(x) { console.log(x); } }; o.f(o.a); } expect: { (function(x) { console.log(x); })([ 1 ]); } expect_stdout: true } issue_2508_2: { options = { collapse_vars: true, hoist_props: true, reduce_vars: true, toplevel: true, unused: true, } input: { var o = { a: { b: 2 }, f: function(x) { console.log(x); } }; o.f(o.a); } expect: { (function(x) { console.log(x); })({ b: 2 }); } expect_stdout: true } issue_2508_3: { options = { collapse_vars: true, hoist_props: true, reduce_vars: true, toplevel: true, unused: true, } input: { var o = { a: [ o ], f: function(x) { console.log(x); } }; o.f(o.a); } expect: { var o = { a: [ o ], f: function(x) { console.log(x); } }; o.f(o.a); } expect_stdout: true } issue_2508_4: { options = { collapse_vars: true, hoist_props: true, reduce_vars: true, toplevel: true, unused: true, } input: { var o = { a: { b: o }, f: function(x) { console.log(x); } }; o.f(o.a); } expect: { var o = { a: { b: o }, f: function(x) { console.log(x); } }; o.f(o.a); } expect_stdout: true } issue_2508_5: { options = { collapse_vars: true, hoist_props: true, reduce_vars: true, toplevel: true, unused: true, } input: { var o = { f: function(x) { console.log(x); } }; o.f(o.f); } expect: { var o_f = function(x) { console.log(x); }; o_f(o_f); } expect_stdout: true } issue_2519: { options = { collapse_vars: true, evaluate: true, hoist_props: true, reduce_vars: true, unused: true, } input: { function testFunc() { var dimensions = { minX: 5, maxX: 6, }; var scale = 1; var d = { x: (dimensions.maxX + dimensions.minX) / 2, }; return d.x * scale; } console.log(testFunc()); } expect: { function testFunc() { return 1 * ((6 + 5) / 2); } console.log(testFunc()); } expect_stdout: "5.5" } undefined_key: { options = { evaluate: true, hoist_props: true, join_vars: true, passes: 4, reduce_vars: true, toplevel: true, unused: true, } input: { var a, o = {}; o[a] = 1; o.b = 2; console.log(o[a] + o.b); } expect: { console.log(3); } expect_stdout: "3" } issue_3021: { options = { hoist_props: true, reduce_vars: true, } input: { var a = 1, b = 2; (function() { b = a; if (a++ + b--) return 1; return; var b = {}; })(); console.log(a, b); } expect: { var a = 1, b = 2; (function() { b = a; if (a++ + b--) return 1; return; var b = {}; })(); console.log(a, b); } expect_stdout: "2 2" } issue_3046: { options = { hoist_props: true, reduce_vars: true, } input: { console.log(function(a) { do { var b = { c: a++ }; } while (b.c && a); return a; }(0)); } expect: { console.log(function(a) { do { var b_c = a++; } while (b_c && a); return a; }(0)); } expect_stdout: "1" } issue_3071_1: { options = { evaluate: true, hoist_props: true, inline: true, join_vars: true, passes: 3, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, unused: true, } input: { (function() { var obj = {}; obj.one = 1; obj.two = 2; console.log(obj.one); })(); } expect: { console.log(1); } expect_stdout: "1" } issue_3071_2: { options = { evaluate: true, hoist_props: true, inline: true, join_vars: true, passes: 3, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { (function() { obj = {}; obj.one = 1; obj.two = 2; console.log(obj.one); var obj; })(); } expect: { console.log(1); } expect_stdout: "1" } issue_3071_2_toplevel: { options = { evaluate: true, hoist_props: true, inline: true, join_vars: true, passes: 3, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, unused: true, } input: { (function() { obj = {}; obj.one = 1; obj.two = 2; console.log(obj.one); var obj; })(); } expect: { console.log(1); } expect_stdout: "1" } issue_3071_3: { options = { hoist_props: true, reduce_vars: true, } input: { var c = 0; (function(a, b) { (function f(o) { var n = 2; while (--b + (o = { p: c++, }) && --n > 0); })(); })(); console.log(c); } expect: { var c = 0; (function(a, b) { (function f(o) { var n = 2; while (--b + (o = { p: c++, }) && --n > 0); })(); })(); console.log(c); } expect_stdout: "2" } issue_3411: { options = { hoist_props: true, reduce_vars: true, } input: { var c = 1; !function f() { var o = { p: --c && f() }; +o || console.log("PASS"); }(); } expect: { var c = 1; !function f() { var o_p = --c && f(); +{} || console.log("PASS"); }(); } expect_stdout: "PASS" } issue_3440: { options = { hoist_props: true, reduce_vars: true, unused: true, } input: { (function() { function f() { console.log(o.p); } var o = { p: "PASS", }; return f; })()(); } expect: { (function() { var o_p = "PASS"; return function() { console.log(o_p); }; })()(); } expect_stdout: "PASS" } UglifyJS2-3.6.3/test/compress/hoist_vars.js000066400000000000000000000037531355252637300206550ustar00rootroot00000000000000statements: { options = { hoist_funs: false, hoist_vars: true, } input: { function f() { var a = 1; var b = 2; var c = 3; function g() {} return g(a, b, c); } } expect: { function f() { var a = 1, b = 2, c = 3; function g() {} return g(a, b, c); } } } statements_funs: { options = { hoist_funs: true, hoist_vars: true, } input: { function f() { var a = 1; var b = 2; var c = 3; function g() {} return g(a, b, c); } } expect: { function f() { function g() {} var a = 1, b = 2, c = 3; return g(a, b, c); } } } sequences: { options = { hoist_funs: false, hoist_vars: true, } input: { function f() { var a = 1, b = 2; function g() {} var c = 3; return g(a, b, c); } } expect: { function f() { var c, a = 1, b = 2; function g() {} c = 3; return g(a, b, c); } } } sequences_funs: { options = { hoist_funs: true, hoist_vars: true, } input: { function f() { var a = 1, b = 2; function g() {} var c = 3; return g(a, b, c); } } expect: { function f() { function g() {} var a = 1, b = 2, c = 3; return g(a, b, c); } } } issue_2295: { options = { collapse_vars: true, hoist_vars: true, } input: { function foo(o) { var a = o.a; if (a) return a; var a = 1; } } expect: { function foo(o) { var a = o.a; if (a) return a; a = 1; } } } UglifyJS2-3.6.3/test/compress/html_comments.js000066400000000000000000000025031355252637300213350ustar00rootroot00000000000000html_comment_in_expression: { input: { function f(a, b, x, y) { return a < !--b && x-- > y; } } expect_exact: "function f(a,b,x,y){return a< !--b&&x-- >y}"; } html_comment_in_less_than: { input: { function f(a, b) { return a < !--b; } } expect_exact: "function f(a,b){return a< !--b}"; } html_comment_in_left_shift: { input: { function f(a, b) { return a << !--b; } } expect_exact: "function f(a,b){return a<< !--b}"; } html_comment_in_right_shift: { input: { function f(a, b) { return a-- >> b; } } expect_exact: "function f(a,b){return a-- >>b}"; } html_comment_in_zero_fill_right_shift: { input: { function f(a, b) { return a-- >>> b; } } expect_exact: "function f(a,b){return a-- >>>b}"; } html_comment_in_greater_than: { input: { function f(a, b) { return a-- > b; } } expect_exact: "function f(a,b){return a-- >b}"; } html_comment_in_greater_than_or_equal: { input: { function f(a, b) { return a-- >= b; } } expect_exact: "function f(a,b){return a-- >=b}"; } html_comment_in_string_literal: { input: { function f() { return "comment in"; } } expect_exact: 'function f(){return"\\x3c!--HTML--\\x3ecomment in\\x3c!--string literal--\\x3e"}'; } UglifyJS2-3.6.3/test/compress/ie8.js000066400000000000000000001122301355252637300171500ustar00rootroot00000000000000do_screw: { options = { ie8: false, } beautify = { ie8: false, ascii_only: true, } input: { f("\v"); } expect_exact: 'f("\\v");' } dont_screw: { options = { ie8: true, } beautify = { ie8: true, ascii_only: true, } input: { f("\v"); } expect_exact: 'f("\\x0B");' } do_screw_constants: { options = { ie8: false, } input: { f(undefined, Infinity); } expect_exact: "f(void 0,1/0);" } dont_screw_constants: { options = { ie8: true, } input: { f(undefined, Infinity); } expect_exact: "f(undefined,Infinity);" } do_screw_try_catch: { options = { ie8: false, } mangle = { ie8: false, } beautify = { ie8: false, } input: { good = function(e){ return function(error){ try { e() } catch (e) { error(e) } } }; } expect: { good = function(n){ return function(t){ try { n() } catch (n) { t(n) } } }; } } dont_screw_try_catch: { options = { ie8: true, } mangle = { ie8: true, } beautify = { ie8: true, } input: { bad = function(e){ return function(error){ try { e() } catch (e) { error(e) } } }; } expect: { bad = function(t){ return function(n){ try { t() } catch (t) { n(t) } } }; } } do_screw_try_catch_undefined: { options = { ie8: false, } mangle = { ie8: false, } beautify = { ie8: false, } input: { function a(b) { try { throw "Stuff"; } catch (undefined) { console.log("caught: " + undefined); } console.log("undefined is " + undefined); return b === undefined; } console.log(a(42), a(void 0)); } expect: { function a(o) { try { throw "Stuff"; } catch (o) { console.log("caught: " + o); } console.log("undefined is " + void 0); return void 0 === o; } console.log(a(42), a(void 0)); } expect_stdout: [ "caught: Stuff", "undefined is undefined", "caught: Stuff", "undefined is undefined", "false true", ] } dont_screw_try_catch_undefined: { options = { ie8: true, } mangle = { ie8: true, } beautify = { ie8: true, } input: { function a(b) { try { throw "Stuff"; } catch (undefined) { console.log("caught: " + undefined); } // IE8: undefined is Stuff console.log("undefined is " + undefined); return b === undefined; } console.log(a(42), a(void 0)); } expect: { function a(n) { try { throw "Stuff"; } catch (undefined) { console.log("caught: " + undefined); } console.log("undefined is " + undefined); return n === undefined; } console.log(a(42), a(void 0)); } expect_stdout: [ "caught: Stuff", "undefined is undefined", "caught: Stuff", "undefined is undefined", "false true", ] } reduce_vars: { options = { evaluate: true, ie8: true, reduce_funcs: true, reduce_vars: true, unused: true, } mangle = { ie8: true, } input: { function f() { var a; try { x(); } catch (a) { y(); } alert(a); } } expect: { function f() { var t; try { x(); } catch (t) { y(); } alert(t); } } } issue_1586_1: { options = { ie8: true, } mangle = { ie8: true, } input: { function f() { try { x(); } catch (err) { console.log(err.message); } } } expect_exact: "function f(){try{x()}catch(c){console.log(c.message)}}" } issue_1586_2: { options = { ie8: false, } mangle = { ie8: false, } input: { function f() { try { x(); } catch (err) { console.log(err.message); } } } expect_exact: "function f(){try{x()}catch(c){console.log(c.message)}}" } issue_2120_1: { mangle = { ie8: false, } input: { "aaaaaaaa"; var a = 1, b = "FAIL"; try { throw 1; } catch (c) { try { throw 0; } catch (a) { if (c) b = "PASS"; } } console.log(b); } expect: { "aaaaaaaa"; var a = 1, b = "FAIL"; try { throw 1; } catch (t) { try { throw 0; } catch (a) { if (t) b = "PASS"; } } console.log(b); } expect_stdout: "PASS" } issue_2120_2: { mangle = { ie8: true, } input: { "aaaaaaaa"; var a = 1, b = "FAIL"; try { throw 1; } catch (c) { try { throw 0; } catch (a) { if (c) b = "PASS"; } } console.log(b); } expect: { "aaaaaaaa"; var a = 1, b = "FAIL"; try { throw 1; } catch (c) { try { throw 0; } catch (a) { if (c) b = "PASS"; } } console.log(b); } expect_stdout: "PASS" } issue_2254_1: { mangle = { ie8: false, } input: { "eeeeee"; try { console.log(f("PASS")); } catch (e) {} function f(s) { try { throw "FAIL"; } catch (e) { return s; } } } expect: { "eeeeee"; try { console.log(f("PASS")); } catch (e) {} function f(t) { try { throw "FAIL"; } catch (e) { return t; } } } expect_stdout: "PASS" } issue_2254_2: { mangle = { ie8: true, } input: { "eeeeee"; try { console.log(f("PASS")); } catch (e) {} function f(s) { try { throw "FAIL"; } catch (e) { return s; } } } expect: { "eeeeee"; try { console.log(f("PASS")); } catch (e) {} function f(t) { try { throw "FAIL"; } catch (e) { return t; } } } expect_stdout: "PASS" } issue_24_1: { mangle = { ie8: false, } input: { (function(a) { console.log(typeof function f(){} === typeof a ? "FAIL" : "PASS"); })(); } expect: { (function(o) { console.log(typeof function o(){} === typeof o ? "FAIL" : "PASS"); })(); } expect_stdout: "PASS" } issue_24_2: { mangle = { ie8: true, } input: { (function(a) { console.log(typeof function f(){} === typeof a ? "FAIL" : "PASS"); })(); } expect: { (function(o) { console.log(typeof function n(){} === typeof o ? "FAIL" : "PASS"); })(); } expect_stdout: "PASS" } issue_2976_1: { mangle = { ie8: false, } input: { console.log(function f() { var a; return a === f ? "FAIL" : "PASS"; }()); } expect: { console.log(function n() { var o; return o === n ? "FAIL" : "PASS"; }()); } expect_stdout: "PASS" } issue_2976_2: { mangle = { ie8: true, } input: { console.log(function f() { var a; return a === f ? "FAIL" : "PASS"; }()); } expect: { console.log(function f() { var n; return n === f ? "FAIL" : "PASS"; }()); } expect_stdout: "PASS" } issue_2976_3: { mangle = { ie8: true, toplevel: true, } input: { console.log(function f() { var a; return a === f ? "FAIL" : "PASS"; }()); } expect: { console.log(function o() { var n; return n === o ? "FAIL" : "PASS"; }()); } expect_stdout: "PASS" } issue_3035: { mangle = { ie8: false, } input: { var c = "FAIL"; (function(a) { try { throw 1; } catch (b) { try { throw 0; } catch (a) { b && (c = "PASS"); } } })(); console.log(c); } expect: { var c = "FAIL"; (function(o) { try { throw 1; } catch (t) { try { throw 0; } catch (o) { t && (c = "PASS"); } } })(); console.log(c); } expect_stdout: "PASS" } issue_3035_ie8: { mangle = { ie8: true, } input: { var c = "FAIL"; (function(a) { try { throw 1; } catch (b) { try { throw 0; } catch (a) { b && (c = "PASS"); } } })(); console.log(c); } expect: { var c = "FAIL"; (function(t) { try { throw 1; } catch (o) { try { throw 0; } catch (t) { o && (c = "PASS"); } } })(); console.log(c); } expect_stdout: "PASS" } issue_3197_1: { options = { ie8: false, inline: true, reduce_vars: true, side_effects: true, unused: true, } mangle = { ie8: false, } input: { var window = {}; !function() { function Foo() { console.log(this instanceof Foo); } window.Foo = Foo; }(); new window.Foo(); } expect: { var window = {}; window.Foo = function o() { console.log(this instanceof o); }; new window.Foo(); } expect_stdout: "true" } issue_3197_1_ie8: { options = { ie8: true, inline: true, reduce_vars: true, side_effects: true, unused: true, } mangle = { ie8: true, } input: { var window = {}; !function() { function Foo() { console.log(this instanceof Foo); } window.Foo = Foo; }(); new window.Foo(); } expect: { var window = {}; window.Foo = function Foo() { console.log(this instanceof Foo); }; new window.Foo(); } expect_stdout: "true" } issue_3197_2: { mangle = { ie8: false, } input: { (function(a) { var f = function f() { console.log(this instanceof f); }; new f(a); })(); } expect: { (function(n) { var o = function n() { console.log(this instanceof n); }; new o(n); })(); } expect_stdout: "true" } issue_3197_2_ie8: { mangle = { ie8: true, } input: { (function(a) { var f = function f() { console.log(this instanceof f); }; new f(a); })(); } expect: { (function(n) { var o = function o() { console.log(this instanceof o); }; new o(n); })(); } expect_stdout: "true" } issue_3206_1: { options = { evaluate: true, ie8: false, reduce_vars: true, typeofs: true, unused: true, } input: { console.log(function() { var foo = function bar() {}; var baz = function moo() {}; return "function" == typeof bar; }()); } expect: { console.log(function() { return "function" == typeof bar; }()); } expect_stdout: "false" } issue_3206_2: { options = { evaluate: true, ie8: true, reduce_vars: true, typeofs: true, unused: true, } input: { console.log(function() { var foo = function bar() {}; var baz = function moo() {}; return "function" == typeof bar; }()); } expect: { console.log(function() { (function bar() {}); return "function" == typeof bar; }()); } expect_stdout: "false" } issue_3215_1: { mangle = { ie8: false, } input: { console.log(function foo() { var bar = function bar(name) { return "PASS"; }; try { "moo"; } catch (e) { bar = function bar(name) { return "FAIL"; }; } return bar; }()()); } expect: { console.log(function n() { var o = function n(o) { return "PASS"; }; try { "moo"; } catch (n) { o = function n(o) { return "FAIL"; }; } return o; }()()); } expect_stdout: "PASS" } issue_3215_2: { mangle = { ie8: true, } input: { console.log(function foo() { var bar = function bar(name) { return "PASS"; }; try { "moo"; } catch (e) { bar = function bar(name) { return "FAIL"; }; } return bar; }()()); } expect: { console.log(function foo() { var o = function o(n) { return "PASS"; }; try { "moo"; } catch (n) { o = function o(n) { return "FAIL"; }; } return o; }()()); } expect_stdout: "PASS" } issue_3215_3: { mangle = { ie8: false, } input: { console.log(function foo() { var bar = function bar(name) { return "FAIL"; }; try { moo; } catch (e) { bar = function bar(name) { return "PASS"; }; } return bar; }()()); } expect: { console.log(function n() { var o = function n(o) { return "FAIL"; }; try { moo; } catch (n) { o = function n(o) { return "PASS"; }; } return o; }()()); } expect_stdout: "PASS" } issue_3215_4: { mangle = { ie8: true, } input: { console.log(function foo() { var bar = function bar(name) { return "FAIL"; }; try { moo; } catch (e) { bar = function bar(name) { return "PASS"; }; } return bar; }()()); } expect: { console.log(function foo() { var o = function o(n) { return "FAIL"; }; try { moo; } catch (n) { o = function o(n) { return "PASS"; }; } return o; }()()); } expect_stdout: "PASS" } issue_3355_1: { mangle = { ie8: false, } input: { (function f() { var f; })(); (function g() { })(); console.log(typeof f === typeof g); } expect: { (function o() { var o; })(); (function o() { })(); console.log(typeof f === typeof g); } expect_stdout: "true" } issue_3355_2: { mangle = { ie8: true, } input: { (function f() { var f; })(); (function g() { })(); console.log(typeof f === typeof g); } expect: { (function f() { var f; })(); (function g() { })(); console.log(typeof f === typeof g); } expect_stdout: "true" } issue_3355_3: { mangle = { ie8: false, } input: { !function(a) { "aaaaaaaaaa"; a(); var b = function c() { var c = 42; console.log("FAIL"); }; }(function() { console.log("PASS"); }); } expect: { !function(a) { "aaaaaaaaaa"; a(); var o = function a() { var a = 42; console.log("FAIL"); }; }(function() { console.log("PASS"); }); } expect_stdout: "PASS" } issue_3355_4: { mangle = { ie8: true, } input: { !function(a) { "aaaaaaaaaa"; a(); var b = function c() { var c = 42; console.log("FAIL"); }; }(function() { console.log("PASS"); }); } expect: { !function(a) { "aaaaaaaaaa"; a(); var o = function n() { var n = 42; console.log("FAIL"); }; }(function() { console.log("PASS"); }); } expect_stdout: "PASS" } issue_3468: { options = { collapse_vars: true, ie8: false, } input: { var a = 42; console.log(function a() { a++; return typeof a; }()); } expect: { var a = 42; console.log(function a() { a++; return typeof a; }()); } expect_stdout: "function" } issue_3468_ie8: { options = { collapse_vars: true, ie8: true, } input: { var a = 42; console.log(function a() { a++; return typeof a; }()); } expect: { var a = 42; console.log(function a() { a++; return typeof a; }()); } expect_stdout: "function" } issue_3471: { options = { ie8: false, functions: true, reduce_vars: true, toplevel: true, unused: true, } input: { var c = 1; function f() { var a = function g() { --c && f(); g.p = 0; }; for (var p in a) a[p]; } f(); } expect: { var c = 1; (function f() { function a() { --c && f(); a.p = 0; } for (var p in a) a[p]; })(); } expect_stdout: true } issue_3471_ie8: { options = { ie8: true, functions: true, reduce_vars: true, toplevel: true, unused: true, } input: { var c = 1; function f() { var a = function g() { --c && f(); g.p = 0; }; for (var p in a) a[p]; } f(); } expect: { var c = 1; (function f() { var a = function g() { --c && f(); g.p = 0; }; for (var p in a) a[p]; })(); } expect_stdout: true } issue_3473: { rename = true mangle = { ie8: false, toplevel: false, } input: { var d = 42, a = 100, b = 10, c = 0; (function b() { try { c++; } catch (b) {} })(); console.log(a, b, c); } expect: { var d = 42, a = 100, b = 10, c = 0; (function a() { try { c++; } catch (a) {} })(); console.log(a, b, c); } expect_stdout: "100 10 1" } issue_3473_ie8: { rename = true mangle = { ie8: true, toplevel: false, } input: { var d = 42, a = 100, b = 10, c = 0; (function b() { try { c++; } catch (b) {} })(); console.log(a, b, c); } expect: { var d = 42, a = 100, b = 10, c = 0; (function b() { try { c++; } catch (b) {} })(); console.log(a, b, c); } expect_stdout: "100 10 1" } issue_3473_toplevel: { rename = true mangle = { ie8: false, toplevel: true, } input: { var d = 42, a = 100, b = 10, c = 0; (function b() { try { c++; } catch (b) {} })(); console.log(a, b, c); } expect: { var c = 42, o = 100, n = 10, t = 0; (function c() { try { t++; } catch (c) {} })(); console.log(o, n, t); } expect_stdout: "100 10 1" } issue_3473_ie8_toplevel: { rename = true mangle = { ie8: true, toplevel: true, } input: { var d = 42, a = 100, b = 10, c = 0; (function b() { try { c++; } catch (b) {} })(); console.log(a, b, c); } expect: { var c = 42, o = 100, n = 10, t = 0; (function n() { try { t++; } catch (n) {} })(); console.log(o, n, t); } expect_stdout: "100 10 1" } issue_3475: { rename = true mangle = { ie8: false, toplevel: false, } input: { "ooooo ddddd"; var a = "FAIL"; try { throw 42; } catch (b) { (function f() { a = "PASS"; })(); } console.log(a); } expect: { "ooooo ddddd"; var a = "FAIL"; try { throw 42; } catch (o) { (function o() { a = "PASS"; })(); } console.log(a); } expect_stdout: "PASS" } issue_3475_ie8: { rename = true mangle = { ie8: true, toplevel: false, } input: { "ooooo ddddd"; var a = "FAIL"; try { throw 42; } catch (b) { (function f() { a = "PASS"; })(); } console.log(a); } expect: { "ooooo ddddd"; var a = "FAIL"; try { throw 42; } catch (b) { (function f() { a = "PASS"; })(); } console.log(a); } expect_stdout: "PASS" } issue_3475_toplevel: { rename = true mangle = { ie8: false, toplevel: true, } input: { "ooooo ddddd"; var a = "FAIL"; try { throw 42; } catch (b) { (function f() { a = "PASS"; })(); } console.log(a); } expect: { "ooooo ddddd"; var d = "FAIL"; try { throw 42; } catch (o) { (function o() { d = "PASS"; })(); } console.log(d); } expect_stdout: "PASS" } issue_3475_ie8_toplevel: { rename = true mangle = { ie8: true, toplevel: true, } input: { "ooooo ddddd"; var a = "FAIL"; try { throw 42; } catch (b) { (function f() { a = "PASS"; })(); } console.log(a); } expect: { "ooooo ddddd"; var o = "FAIL"; try { throw 42; } catch (d) { (function c() { o = "PASS"; })(); } console.log(o); } expect_stdout: "PASS" } issue_3478_1: { rename = true mangle = { ie8: false, toplevel: false, } input: { "aaaaaaaaaa"; (function f() { (function f() { var a; console.log(typeof f); })(); })(); } expect: { "aaaaaaaaaa"; (function a() { (function a() { var o; console.log(typeof a); })(); })(); } expect_stdout: "function" } issue_3478_1_ie8: { rename = true mangle = { ie8: true, toplevel: false, } input: { "aaaaaaaaaa"; (function f() { (function f() { var a; console.log(typeof f); })(); })(); } expect: { "aaaaaaaaaa"; (function f() { (function f() { var a; console.log(typeof f); })(); })(); } expect_stdout: "function" } issue_3478_1_toplevel: { rename = true mangle = { ie8: false, toplevel: true, } input: { "aaaaaaaaaa"; (function f() { (function f() { var a; console.log(typeof f); })(); })(); } expect: { "aaaaaaaaaa"; (function a() { (function a() { var o; console.log(typeof a); })(); })(); } expect_stdout: "function" } issue_3478_1_ie8_toplevel: { rename = true mangle = { ie8: true, toplevel: true, } input: { "aaaaaaaaaa"; (function f() { (function f() { var a; console.log(typeof f); })(); })(); } expect: { "aaaaaaaaaa"; (function o() { (function o() { var a; console.log(typeof o); })(); })(); } expect_stdout: "function" } issue_3478_2: { rename = true mangle = { ie8: false, toplevel: false, } input: { "bbbbbbb"; var c = "FAIL"; (function f() { (function f() { var b = function g() { f && (c = "PASS"); }(); })(); })(); console.log(c); } expect: { "bbbbbbb"; var c = "FAIL"; (function b() { (function n() { var b = function b() { n && (c = "PASS"); }(); })(); })(); console.log(c); } expect_stdout: "PASS" } issue_3478_2_ie8: { rename = true mangle = { ie8: true, toplevel: false, } input: { "bbbbbbb"; var c = "FAIL"; (function f() { (function f() { var b = function g() { f && (c = "PASS"); }(); })(); })(); console.log(c); } expect: { "bbbbbbb"; var c = "FAIL"; (function f() { (function f() { var b = function n() { f && (c = "PASS"); }(); })(); })(); console.log(c); } expect_stdout: "PASS" } issue_3478_2_toplevel: { rename = true mangle = { ie8: false, toplevel: true, } input: { "bbbbbbb"; var c = "FAIL"; (function f() { (function f() { var b = function g() { f && (c = "PASS"); }(); })(); })(); console.log(c); } expect: { "bbbbbbb"; var o = "FAIL"; (function b() { (function n() { var b = function b() { n && (o = "PASS"); }(); })(); })(); console.log(o); } expect_stdout: "PASS" } issue_3478_2_ie8_toplevel: { rename = true mangle = { ie8: true, toplevel: true, } input: { "bbbbbbb"; var c = "FAIL"; (function f() { (function f() { var b = function g() { f && (c = "PASS"); }(); })(); })(); console.log(c); } expect: { "bbbbbbb"; var o = "FAIL"; (function c() { (function c() { var b = function n() { c && (o = "PASS"); }(); })(); })(); console.log(o); } expect_stdout: "PASS" } issue_3482_1: { options = { evaluate: true, ie8: false, } input: { try { throw 42; } catch (NaN) { var a = +"a"; } console.log(a, NaN, 0 / 0); } expect: { try { throw 42; } catch (NaN) { var a = 0 / 0; } console.log(a, NaN, NaN); } expect_stdout: "NaN NaN NaN" } issue_3482_1_ie8: { options = { evaluate: true, ie8: true, } input: { try { throw 42; } catch (NaN) { var a = +"a"; } // IE8: NaN 42 NaN console.log(a, NaN, 0 / 0); } expect: { try { throw 42; } catch (NaN) { var a = 0 / 0; } console.log(a, NaN, 0 / 0); } expect_stdout: "NaN NaN NaN" } issue_3482_2: { options = { evaluate: true, ie8: false, } input: { (function() { try { throw 42; } catch (NaN) { a = +"a"; } })(); console.log(a, NaN, 0 / 0); } expect: { (function() { try { throw 42; } catch (NaN) { a = 0 / 0; } })(); console.log(a, NaN, NaN); } expect_stdout: "NaN NaN NaN" } issue_3482_2_ie8: { options = { evaluate: true, ie8: true, } input: { (function() { try { throw 42; } catch (NaN) { a = +"a"; } })(); console.log(a, NaN, 0 / 0); } expect: { (function() { try { throw 42; } catch (NaN) { a = 0 / 0; } })(); console.log(a, NaN, 0 / 0); } expect_stdout: "NaN NaN NaN" } issue_3484_1: { options = { ie8: false, side_effects: true, toplevel: false, } input: { (function f() {})(); console.log(typeof f); } expect: { console.log(typeof f); } expect_stdout: "undefined" } issue_3484_1_ie8: { options = { ie8: true, side_effects: true, toplevel: false, } input: { (function f() {})(); // IE8: function console.log(typeof f); } expect: { (function f() {})(); console.log(typeof f); } expect_stdout: "undefined" } issue_3484_1_toplevel: { options = { ie8: false, side_effects: true, toplevel: true, } input: { (function f() {})(); console.log(typeof f); } expect: { console.log(typeof f); } expect_stdout: "undefined" } issue_3484_1_ie8_toplevel: { options = { ie8: true, side_effects: true, toplevel: true, } input: { (function f() {})(); // IE8: function console.log(typeof f); } expect: { (function f() {})(); console.log(typeof f); } expect_stdout: "undefined" } issue_3484_2: { options = { evaluate: true, ie8: false, reduce_vars: true, toplevel: false, } input: { (function Infinity() { var Infinity; })(); console.log(typeof (1 / 0), typeof Infinity); } expect: { (function Infinity() { var Infinity; })(); console.log("number", "number"); } expect_stdout: "number number" } issue_3484_2_ie8: { options = { evaluate: true, ie8: true, reduce_vars: true, toplevel: false, } input: { (function Infinity() { var Infinity; })(); // IE8: number function console.log(typeof (1 / 0), typeof Infinity); } expect: { (function Infinity() { var Infinity; })(); console.log("number", typeof Infinity); } expect_stdout: "number number" } issue_3484_2_toplevel: { options = { evaluate: true, ie8: false, reduce_vars: true, toplevel: true, } input: { (function Infinity() { var Infinity; })(); console.log(typeof (1 / 0), typeof Infinity); } expect: { (function Infinity() { var Infinity; })(); console.log("number", "number"); } expect_stdout: "number number" } issue_3484_2_ie8_toplevel: { options = { evaluate: true, ie8: true, reduce_vars: true, toplevel: true, } input: { (function Infinity() { var Infinity; })(); // IE8: number function console.log(typeof (1 / 0), typeof Infinity); } expect: { (function Infinity() { var Infinity; })(); console.log("number", typeof Infinity); } expect_stdout: "number number" } issue_3486: { options = { conditionals: true, ie8: false, reduce_vars: true, } input: { (function a() { (function a(a) { console.log(a ? "FAIL" : "PASS"); })(); })(); } expect: { (function a() { (function a(a) { console.log(a ? "FAIL" : "PASS"); })(); })(); } expect_stdout: "PASS" } issue_3486_ie8: { options = { conditionals: true, ie8: true, reduce_vars: true, } input: { (function a() { (function a(a) { console.log(a ? "FAIL" : "PASS"); })(); })(); } expect: { (function a() { (function a(a) { console.log(a ? "FAIL" : "PASS"); })(); })(); } expect_stdout: "PASS" } issue_3493: { options = { dead_code: true, ie8: false, } input: { var c = "PASS"; (function() { try { (function a() { throw {}; })(); } catch (a) { a >>= 0; a && (c = "FAIL"); } })(); console.log(c); } expect: { var c = "PASS"; (function() { try { (function a() { throw {}; })(); } catch (a) { a >>= 0; a && (c = "FAIL"); } })(); console.log(c); } expect_stdout: "PASS" } issue_3493_ie8: { options = { dead_code: true, ie8: true, } input: { var c = "PASS"; (function() { try { (function a() { throw {}; })(); } catch (a) { a >>= 0; a && (c = "FAIL"); } })(); console.log(c); } expect: { var c = "PASS"; (function() { try { (function a() { throw {}; })(); } catch (a) { a >>= 0; a && (c = "FAIL"); } })(); console.log(c); } expect_stdout: "PASS" } UglifyJS2-3.6.3/test/compress/if_return.js000066400000000000000000000254241355252637300204700ustar00rootroot00000000000000if_return_1: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, if_return: true, sequences: true, side_effects: true, unused: true, } input: { function f(x) { if (x) { return true; } } } expect: { function f(x){if(x)return!0} } } if_return_2: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, if_return: true, sequences: true, side_effects: true, unused: true, } input: { function f(x, y) { if (x) return 3; if (y) return c(); } } expect: { function f(x,y){return x?3:y?c():void 0} } } if_return_3: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, if_return: true, sequences: true, side_effects: true, unused: true, } input: { function f(x) { a(); if (x) { b(); return false; } } } expect: { function f(x){if(a(),x)return b(),!1} } } if_return_4: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, if_return: true, sequences: true, side_effects: true, unused: true, } input: { function f(x, y) { a(); if (x) return 3; b(); if (y) return c(); } } expect: { function f(x,y){return a(),x?3:(b(),y?c():void 0)} } } if_return_5: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, if_return: true, sequences: true, side_effects: true, unused: true, } input: { function f() { if (x) return; return 7; if (y) return j; } } expect: { function f(){if(!x)return 7} } } if_return_6: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, if_return: true, sequences: true, side_effects: true, unused: true, } input: { function f(x) { return x ? true : void 0; return y; } } expect: { // suboptimal function f(x){return!!x||void 0} } } if_return_7: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, if_return: true, sequences: true, side_effects: true, unused: true, } input: { function f(x) { if (x) { return true; } foo(); bar(); } } expect: { function f(x){if(x)return!0;foo(),bar()} } } if_return_8: { options = { conditionals: true, if_return: true, sequences: true, side_effects: true, } input: { function f(e) { if (2 == e) return foo(); if (3 == e) return bar(); if (4 == e) return baz(); fail(e); } function g(e) { if (a(e)) return foo(); if (b(e)) return bar(); if (c(e)) return baz(); fail(e); } function h(e) { if (a(e)) return foo(); else if (b(e)) return bar(); else if (c(e)) return baz(); else fail(e); } function i(e) { if (a(e)) return foo(); else if (b(e)) return bar(); else if (c(e)) return baz(); fail(e); } } expect: { function f(e){return 2==e?foo():3==e?bar():4==e?baz():void fail(e)} function g(e){return a(e)?foo():b(e)?bar():c(e)?baz():void fail(e)} function h(e){return a(e)?foo():b(e)?bar():c(e)?baz():void fail(e)} function i(e){return a(e)?foo():b(e)?bar():c(e)?baz():void fail(e)} } } issue_1089: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, if_return: true, sequences: true, side_effects: true, unused: true, } input: { function x() { var f = document.getElementById("fname"); if (f.files[0].size > 12345) { alert("alert"); f.focus(); return false; } } } expect: { function x() { var f = document.getElementById("fname"); if (12345 < f.files[0].size) return alert("alert"), f.focus(), !1; } } } issue_1437: { options = { conditionals: false, if_return: true, sequences: true, } input: { function x() { if (a()) return b(); if (c()) return d(); else e(); f(); } } expect: { function x() { if (a()) return b(); if (c()) return d(); else e() f(); } } } issue_1437_conditionals: { options = { conditionals: true, if_return: true, sequences: true, } input: { function x() { if (a()) return b(); if (c()) return d(); else e(); f(); } } expect: { function x() { return a() ? b() : c() ? d() : (e(), f(), void 0); } } } issue_512: { options = { conditionals: true, if_return: true, sequences: true, side_effects: true, } input: { function a() { if (b()) { c(); return; } throw e; } } expect: { function a() { if (!b()) throw e; c(); } } } if_var_return: { options = { conditionals: true, if_return: true, join_vars: true, sequences: true, } input: { function f() { var a; return; var b; } function g() { var a; if (u()) { var b; return v(); var c; } var d; if (w()) { var e; return x(); var f; } else { var g; y(); var h; } var i; z(); var j; } } expect: { function f() { var a, b; } function g() { var a, b, c, d, e, f, g, h, i, j; return u() ? v() : w() ? x() : (y(), z(), void 0); } } } if_if_return_return: { options = { conditionals: true, if_return: true, } input: { function f(a, b) { if (a) { if (b) return b; return; } g(); } } expect: { function f(a, b) { if (a) return b || void 0; g(); } } } if_body_return_1: { options = { if_return: true, } input: { var c = "PASS"; function f(a, b) { if (a) { if (b) throw new Error(c); return 42; } return true; } console.log(f(0, 0)); console.log(f(0, 1)); console.log(f(1, 0)); try { f(1, 1); console.log("FAIL"); } catch (e) { console.log(e.message); } } expect: { var c = "PASS"; function f(a, b) { if (a) { if (b) throw new Error(c); return 42; } return true; } console.log(f(0, 0)); console.log(f(0, 1)); console.log(f(1, 0)); try { f(1, 1); console.log("FAIL"); } catch (e) { console.log(e.message); } } expect_stdout: [ "true", "true", "42", "PASS", ] } if_body_return_2: { options = { if_return: true, } input: { var c = "PASS"; function f(a, b) { if (0 + a) { if (b) throw new Error(c); return 42; } return true; } console.log(f(0, 0)); console.log(f(0, 1)); console.log(f(1, 0)); try { f(1, 1); console.log("FAIL"); } catch (e) { console.log(e.message); } } expect: { var c = "PASS"; function f(a, b) { if (0 + a) { if (b) throw new Error(c); return 42; } return true; } console.log(f(0, 0)); console.log(f(0, 1)); console.log(f(1, 0)); try { f(1, 1); console.log("FAIL"); } catch (e) { console.log(e.message); } } expect_stdout: [ "true", "true", "42", "PASS", ] } if_body_return_3: { options = { if_return: true, } input: { var c = "PASS"; function f(a, b) { if (1 == a) { if (b) throw new Error(c); return 42; } return true; } console.log(f(0, 0)); console.log(f(0, 1)); console.log(f(1, 0)); try { f(1, 1); console.log("FAIL"); } catch (e) { console.log(e.message); } } expect: { var c = "PASS"; function f(a, b) { if (1 != a) return true; if (b) throw new Error(c); return 42; } console.log(f(0, 0)); console.log(f(0, 1)); console.log(f(1, 0)); try { f(1, 1); console.log("FAIL"); } catch (e) { console.log(e.message); } } expect_stdout: [ "true", "true", "42", "PASS", ] } UglifyJS2-3.6.3/test/compress/issue-1034.js000066400000000000000000000225671355252637300202150ustar00rootroot00000000000000non_hoisted_function_after_return: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: false, if_return: true, join_vars: true, keep_fargs: true, loops: true, side_effects: true, unused: true, } input: { function foo(x) { if (x) { return bar(); not_called1(); } else { return baz(); not_called2(); } function bar() { return 7; } return not_reached; function UnusedFunction() {} function baz() { return 8; } } } expect: { function foo(x) { return x ? bar() : baz(); function bar() { return 7 } function baz() { return 8 } } } expect_warnings: [ "WARN: Dropping unreachable code [test/compress/issue-1034.js:4,16]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:7,16]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:10,12]", "WARN: Dropping unused function UnusedFunction [test/compress/issue-1034.js:11,21]" ] } non_hoisted_function_after_return_2a: { options = { booleans: true, collapse_vars: false, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: false, if_return: true, join_vars: true, keep_fargs: true, loops: true, passes: 2, side_effects: true, unused: true, } input: { function foo(x) { if (x) { return bar(1); var a = not_called(1); } else { return bar(2); var b = not_called(2); } var c = bar(3); function bar(x) { return 7 - x; } function nope() {} return b || c; } } expect: { function foo(x) { return bar(x ? 1 : 2); function bar(x) { return 7 - x; } } } expect_warnings: [ "WARN: Dropping unreachable code [test/compress/issue-1034.js:4,16]", "WARN: Declarations in unreachable code! [test/compress/issue-1034.js:4,16]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:7,16]", "WARN: Declarations in unreachable code! [test/compress/issue-1034.js:7,16]", "WARN: Dropping unused variable a [test/compress/issue-1034.js:4,20]", "WARN: Dropping unused function nope [test/compress/issue-1034.js:11,21]", "INFO: pass 0: last_count: Infinity, count: 37", "WARN: Dropping unreachable code [test/compress/issue-1034.js:9,12]", "WARN: Declarations in unreachable code! [test/compress/issue-1034.js:9,12]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:12,12]", "INFO: Dropping unused variable b [test/compress/issue-1034.js:7,20]", "INFO: Dropping unused variable c [test/compress/issue-1034.js:9,16]", "INFO: pass 1: last_count: 37, count: 18", ] } non_hoisted_function_after_return_2b: { options = { booleans: true, collapse_vars: false, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: false, if_return: true, join_vars: true, keep_fargs: true, loops: true, side_effects: true, unused: true, } input: { function foo(x) { if (x) { return bar(1); } else { return bar(2); var b; } var c = bar(3); function bar(x) { return 7 - x; } return b || c; } } expect: { function foo(x) { return bar(x ? 1 : 2); function bar(x) { return 7 - x; } } } expect_warnings: [ "WARN: Dropping unreachable code [test/compress/issue-1034.js:6,16]", "WARN: Declarations in unreachable code! [test/compress/issue-1034.js:6,16]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:8,12]", "WARN: Declarations in unreachable code! [test/compress/issue-1034.js:8,12]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:12,12]", ] } non_hoisted_function_after_return_strict: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: false, if_return: true, join_vars: true, keep_fargs: true, loops: true, side_effects: true, unused: true, } input: { "use strict"; function foo(x) { if (x) { return bar(); not_called1(); } else { return baz(); not_called2(); } function bar() { return 7; } return not_reached; function UnusedFunction() {} function baz() { return 8; } } console.log(foo(0), foo(1)); } expect: { "use strict"; function foo(x) { return x ? bar() : baz(); function bar() { return 7 } function baz() { return 8 } } console.log(foo(0), foo(1)); } expect_stdout: "8 7" expect_warnings: [ "WARN: Dropping unreachable code [test/compress/issue-1034.js:5,16]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:8,16]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:11,12]", "WARN: Dropping unused function UnusedFunction [test/compress/issue-1034.js:12,21]", ] } non_hoisted_function_after_return_2a_strict: { options = { booleans: true, collapse_vars: false, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: false, if_return: true, join_vars: true, keep_fargs: true, loops: true, passes: 2, side_effects: true, unused: true, } input: { "use strict"; function foo(x) { if (x) { return bar(1); var a = not_called(1); } else { return bar(2); var b = not_called(2); } var c = bar(3); function bar(x) { return 7 - x; } function nope() {} return b || c; } console.log(foo(0), foo(1)); } expect: { "use strict"; function foo(x) { return bar(x ? 1 : 2); function bar(x) { return 7 - x; } } console.log(foo(0), foo(1)); } expect_stdout: "5 6" expect_warnings: [ "WARN: Dropping unreachable code [test/compress/issue-1034.js:5,16]", "WARN: Declarations in unreachable code! [test/compress/issue-1034.js:5,16]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:8,16]", "WARN: Declarations in unreachable code! [test/compress/issue-1034.js:8,16]", "WARN: Dropping unused variable a [test/compress/issue-1034.js:5,20]", "WARN: Dropping unused function nope [test/compress/issue-1034.js:12,21]", "INFO: pass 0: last_count: Infinity, count: 48", "WARN: Dropping unreachable code [test/compress/issue-1034.js:10,12]", "WARN: Declarations in unreachable code! [test/compress/issue-1034.js:10,12]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:13,12]", "INFO: Dropping unused variable b [test/compress/issue-1034.js:8,20]", "INFO: Dropping unused variable c [test/compress/issue-1034.js:10,16]", "INFO: pass 1: last_count: 48, count: 29", ] } non_hoisted_function_after_return_2b_strict: { options = { booleans: true, collapse_vars: false, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: false, if_return: true, join_vars: true, keep_fargs: true, loops: true, side_effects: true, unused: true, } input: { "use strict"; function foo(x) { if (x) { return bar(1); } else { return bar(2); var b; } var c = bar(3); function bar(x) { return 7 - x; } return b || c; } console.log(foo(0), foo(1)); } expect: { "use strict"; function foo(x) { return bar(x ? 1 : 2); function bar(x) { return 7 - x; } } console.log(foo(0), foo(1)); } expect_stdout: "5 6" expect_warnings: [ "WARN: Dropping unreachable code [test/compress/issue-1034.js:7,16]", "WARN: Declarations in unreachable code! [test/compress/issue-1034.js:7,16]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:9,12]", "WARN: Declarations in unreachable code! [test/compress/issue-1034.js:9,12]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:13,12]", ] } UglifyJS2-3.6.3/test/compress/issue-1041.js000066400000000000000000000007401355252637300202000ustar00rootroot00000000000000const_pragma: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { /** @const */ var goog = goog || {}; } expect: { var goog = goog || {}; } } // for completeness' sake not_const: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { var goog = goog || {}; } expect: { var goog = goog || {}; } } UglifyJS2-3.6.3/test/compress/issue-1052.js000066400000000000000000000062321355252637300202040ustar00rootroot00000000000000multiple_functions: { options = { hoist_funs: false, if_return: true, } input: { ( function() { if ( !window ) { return; } function f() {} function g() {} } )(); } expect: { ( function() { // NOTE: other compression steps will reduce this // down to just `window`. if ( window ); function f() {} function g() {} } )(); } } single_function: { options = { hoist_funs: false, if_return: true, } input: { ( function() { if ( !window ) { return; } function f() {} } )(); } expect: { ( function() { if ( window ); function f() {} } )(); } } deeply_nested: { options = { hoist_funs: false, if_return: true, } input: { ( function() { if ( !window ) { return; } function f() {} function g() {} if ( !document ) { return; } function h() {} } )(); } expect: { ( function() { // NOTE: other compression steps will reduce this // down to just `window`. if ( window ) if (document); function f() {} function g() {} function h() {} } )(); } } not_hoisted_when_already_nested: { options = { hoist_funs: false, if_return: true, } input: { ( function() { if ( !window ) { return; } if ( foo ) function f() {} } )(); } expect: { ( function() { if ( window ) if ( foo ) function f() {} } )(); } } defun_if_return: { options = { hoist_funs: false, if_return: true, } input: { function e() { function f() {} if (!window) return; else function g() {} function h() {} } } expect: { function e() { function f() {} if (window) function g() {} function h() {} } } } defun_hoist_funs: { options = { hoist_funs: true, if_return: true, } input: { function e() { function f() {} if (!window) return; else function g() {} function h() {} } } expect: { function e() { function f() {} function g() {} function h() {} if (window); } } } defun_else_if_return: { options = { hoist_funs: false, if_return: true, } input: { function e() { function f() {} if (window) function g() {} else return; function h() {} } } expect: { function e() { function f() {} if (window) function g() {} function h() {} } } } UglifyJS2-3.6.3/test/compress/issue-1105.js000066400000000000000000000155541355252637300202120ustar00rootroot00000000000000with_in_global_scope: { options = { unused: true, } input: { var o = 42; with(o) { var foo = 'something' } doSomething(o); } expect: { var o=42; with(o) var foo = "something"; doSomething(o); } } with_in_function_scope: { options = { unused: true, } input: { function foo() { var o = 42; with(o) { var foo = "something" } doSomething(o); } } expect: { function foo() { var o=42; with(o) var foo = "something"; doSomething(o) } } } compress_with_with_in_other_scope: { options = { unused: true, } input: { function foo() { var o = 42; with(o) { var foo = "something" } doSomething(o); } function bar() { var unused = 42; return something(); } } expect: { function foo() { var o = 42; with(o) var foo = "something"; doSomething(o) } function bar() { return something() } } } with_using_existing_variable_outside_scope: { options = { unused: true, } input: { function f() { var o = {}; var unused = {}; // Doesn't get removed because upper scope uses with function foo() { with(o) { var foo = "something" } doSomething(o); } foo() } } expect: { function f() { var o = {}; var unused = {}; function foo() { with(o) var foo = "something"; doSomething(o) } foo() } } } check_drop_unused_in_peer_function: { options = { unused: true, } input: { function outer() { var o = {}; var unused = {}; // should be kept function foo() { // should be kept function not_in_use() { var nested_unused = "foo"; // should be dropped return 24; } var unused = {}; // should be kept with (o) { var foo = "something"; } doSomething(o); } function bar() { var unused = {}; // should be dropped doSomethingElse(); } foo(); bar(); } } expect: { function outer() { var o = {}; var unused = {}; // should be kept function foo() { // should be kept function not_in_use() { return 24; } var unused = {}; // should be kept with (o) var foo = "something"; doSomething(o); } function bar() { doSomethingElse(); } foo(); bar(); } } } Infinity_not_in_with_scope: { options = { unused: true, } input: { var o = { Infinity: 'oInfinity' }; var vInfinity = "Infinity"; vInfinity = Infinity; } expect: { var o = { Infinity: 'oInfinity' } var vInfinity = "Infinity" vInfinity = 1/0 } } Infinity_in_with_scope: { options = { unused: true, } input: { var o = { Infinity: 'oInfinity' }; var vInfinity = "Infinity"; with (o) { vInfinity = Infinity; } } expect: { var o = { Infinity: 'oInfinity' } var vInfinity = "Infinity" with (o) vInfinity = Infinity } } assorted_Infinity_NaN_undefined_in_with_scope: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, keep_infinity: false, sequences: false, side_effects: true, unused: true, } input: { var f = console.log; var o = { undefined : 3, NaN : 4, Infinity : 5, }; if (o) { f(undefined, void 0); f(NaN, 0/0); f(Infinity, 1/0); f(-Infinity, -(1/0)); f(2 + 7 + undefined, 2 + 7 + void 0); } with (o) { f(undefined, void 0); f(NaN, 0/0); f(Infinity, 1/0); f(-Infinity, -(1/0)); f(2 + 7 + undefined, 2 + 7 + void 0); } } expect: { var f = console.log, o = { undefined : 3, NaN : 4, Infinity : 5 }; if (o) { f(void 0, void 0); f(NaN, NaN); f(1/0, 1/0); f(-1/0, -1/0); f(NaN, NaN); } with (o) { f(undefined, void 0); f(NaN, 0/0); f(Infinity, 1/0); f(-Infinity, -1/0); f(9 + undefined, 9 + void 0); } } expect_stdout: true } assorted_Infinity_NaN_undefined_in_with_scope_keep_infinity: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, keep_infinity: true, sequences: false, side_effects: true, unused: true, } input: { var f = console.log; var o = { undefined : 3, NaN : 4, Infinity : 5, }; if (o) { f(undefined, void 0); f(NaN, 0/0); f(Infinity, 1/0); f(-Infinity, -(1/0)); f(2 + 7 + undefined, 2 + 7 + void 0); } with (o) { f(undefined, void 0); f(NaN, 0/0); f(Infinity, 1/0); f(-Infinity, -(1/0)); f(2 + 7 + undefined, 2 + 7 + void 0); } } expect: { var f = console.log, o = { undefined : 3, NaN : 4, Infinity : 5 }; if (o) { f(void 0, void 0); f(NaN, NaN); f(Infinity, 1/0); f(-Infinity, -1/0); f(NaN, NaN); } with (o) { f(undefined, void 0); f(NaN, 0/0); f(Infinity, 1/0); f(-Infinity, -1/0); f(9 + undefined, 9 + void 0); } } expect_stdout: true } UglifyJS2-3.6.3/test/compress/issue-12.js000066400000000000000000000024631355252637300200410ustar00rootroot00000000000000keep_name_of_getter: { options = { unused: true, } input: { a = { get foo () {} } } expect: { a = { get foo () {} } } } keep_name_of_setter: { options = { unused: true, } input: { a = { set foo () {} } } expect: { a = { set foo () {} } } } setter_with_operator_keys: { input: { var tokenCodes = { get instanceof(){ return test0; }, set instanceof(value){ test0 = value; }, set typeof(value){ test1 = value; }, get typeof(){ return test1; }, set else(value){ test2 = value; }, get else(){ return test2; } }; } expect: { var tokenCodes = { get instanceof(){ return test0; }, set instanceof(value){ test0 = value; }, set typeof(value){ test1 = value; }, get typeof(){ return test1; }, set else(value){ test2 = value; }, get else(){ return test2; } }; } }UglifyJS2-3.6.3/test/compress/issue-1202.js000066400000000000000000000017271355252637300202050ustar00rootroot00000000000000mangle_keep_fnames_false: { options = { keep_fargs: true, keep_fnames: true, } mangle = { keep_fnames : false, } input: { "use strict"; function total() { return function n(a, b, c) { return a + b + c; }; } } expect: { "use strict"; function total() { return function t(n, r, u) { return n + r + u; }; } } } mangle_keep_fnames_true: { options = { keep_fargs: true, keep_fnames: true, } mangle = { keep_fnames : true, } input: { "use strict"; function total() { return function n(a, b, c) { return a + b + c; }; } } expect: { "use strict"; function total() { return function n(t, r, u) { return t + r + u; }; } } } UglifyJS2-3.6.3/test/compress/issue-126.js000066400000000000000000000015071355252637300201250ustar00rootroot00000000000000concatenate_rhs_strings: { options = { evaluate: true, unsafe: true, } input: { foo(bar() + 123 + "Hello" + "World"); foo(bar() + (123 + "Hello") + "World"); foo((bar() + 123) + "Hello" + "World"); foo(bar() + 123 + "Hello" + "World" + ("Foo" + "Bar")); foo("Foo" + "Bar" + bar() + 123 + "Hello" + "World" + ("Foo" + "Bar")); foo("Hello" + bar() + 123 + "World"); foo(bar() + 'Foo' + (10 + parseInt('10'))); } expect: { foo(bar() + 123 + "HelloWorld"); foo(bar() + "123HelloWorld"); foo((bar() + 123) + "HelloWorld"); foo(bar() + 123 + "HelloWorldFooBar"); foo("FooBar" + bar() + "123HelloWorldFooBar"); foo("Hello" + bar() + "123World"); foo(bar() + 'Foo' + (10 + parseInt('10'))); } } UglifyJS2-3.6.3/test/compress/issue-1261.js000066400000000000000000000157111355252637300202100ustar00rootroot00000000000000pure_function_calls: { options = { booleans: true, comparisons: true, conditionals: true, evaluate: true, if_return: true, join_vars: true, negate_iife: true, side_effects: true, unused: true, } input: { // pure top-level IIFE will be dropped // @__PURE__ - comment (function() { console.log("iife0"); })(); // pure top-level IIFE assigned to unreferenced var will not be dropped var iife1 = /*@__PURE__*/(function() { console.log("iife1"); function iife1() {} return iife1; })(); (function(){ // pure IIFE in function scope assigned to unreferenced var will be dropped var iife2 = /*#__PURE__*/(function() { console.log("iife2"); function iife2() {} return iife2; })(); })(); // comment #__PURE__ comment bar(), baz(), quux(); a.b(), /* @__PURE__ */ c.d.e(), f.g(); } expect: { var iife1 = function() { console.log("iife1"); function iife1() {} return iife1; }(); baz(), quux(); a.b(), f.g(); } expect_warnings: [ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:3,8]", "WARN: Dropping side-effect-free statement [test/compress/issue-1261.js:3,8]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:16,37]", "WARN: Dropping unused variable iife2 [test/compress/issue-1261.js:16,16]", "WARN: Dropping side-effect-free statement [test/compress/issue-1261.js:14,8]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:24,8]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:25,31]", ] } pure_function_calls_toplevel: { options = { booleans: true, comparisons: true, conditionals: true, evaluate: true, if_return: true, join_vars: true, negate_iife: true, side_effects: true, toplevel: true, unused: true, } input: { // pure top-level IIFE will be dropped // @__PURE__ - comment (function() { console.log("iife0"); })(); // pure top-level IIFE assigned to unreferenced var will be dropped var iife1 = /*@__PURE__*/(function() { console.log("iife1"); function iife1() {} return iife1; })(); (function(){ // pure IIFE in function scope assigned to unreferenced var will be dropped var iife2 = /*#__PURE__*/(function() { console.log("iife2"); function iife2() {} return iife2; })(); })(); // pure top-level calls will be dropped regardless of the leading comments position var MyClass = /*#__PURE__*//*@class*/(function(){ function MyClass() {} MyClass.prototype.method = function() {}; return MyClass; })(); // comment #__PURE__ comment bar(), baz(), quux(); a.b(), /* @__PURE__ */ c.d.e(), f.g(); } expect: { baz(), quux(); a.b(), f.g(); } expect_warnings: [ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:3,8]", "WARN: Dropping side-effect-free statement [test/compress/issue-1261.js:3,8]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:16,37]", "WARN: Dropping unused variable iife2 [test/compress/issue-1261.js:16,16]", "WARN: Dropping side-effect-free statement [test/compress/issue-1261.js:14,8]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:31,8]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:32,31]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:8,33]", "WARN: Dropping unused variable iife1 [test/compress/issue-1261.js:8,12]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:24,45]", "WARN: Dropping unused variable MyClass [test/compress/issue-1261.js:24,12]", ] } should_warn: { options = { booleans: true, conditionals: true, evaluate: true, side_effects: true, } input: { /* @__PURE__ */(function(){x})(), void/* @__PURE__ */(function(){y})(); /* @__PURE__ */(function(){x})() || true ? foo() : bar(); true || /* @__PURE__ */(function(){y})() ? foo() : bar(); /* @__PURE__ */(function(){x})() && false ? foo() : bar(); false && /* @__PURE__ */(function(){y})() ? foo() : bar(); /* @__PURE__ */(function(){x})() + "foo" ? bar() : baz(); "foo" + /* @__PURE__ */(function(){y})() ? bar() : baz(); /* @__PURE__ */(function(){x})() ? foo() : foo(); [/* @__PURE__ */(function(){x})()] ? foo() : bar(); !{ foo: /* @__PURE__ */(function(){x})() } ? bar() : baz(); } expect: { foo(); foo(); bar(); bar(); bar(); bar(); foo(); foo(); baz(); } expect_warnings: [ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:1,61]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:1,23]", "WARN: Dropping side-effect-free statement [test/compress/issue-1261.js:1,23]", "WARN: Boolean || always true [test/compress/issue-1261.js:2,23]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:2,23]", "WARN: Condition always true [test/compress/issue-1261.js:2,23]", "WARN: Condition left of || always true [test/compress/issue-1261.js:3,8]", "WARN: Condition always true [test/compress/issue-1261.js:3,8]", "WARN: Boolean && always false [test/compress/issue-1261.js:4,23]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:4,23]", "WARN: Condition always false [test/compress/issue-1261.js:4,23]", "WARN: Condition left of && always false [test/compress/issue-1261.js:5,8]", "WARN: Condition always false [test/compress/issue-1261.js:5,8]", "WARN: + in boolean context always true [test/compress/issue-1261.js:6,23]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:6,23]", "WARN: Condition always true [test/compress/issue-1261.js:6,23]", "WARN: + in boolean context always true [test/compress/issue-1261.js:7,8]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:7,31]", "WARN: Condition always true [test/compress/issue-1261.js:7,8]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:8,23]", "WARN: Condition always true [test/compress/issue-1261.js:9,8]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:9,24]", "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:10,31]", "WARN: Condition always false [test/compress/issue-1261.js:10,8]", ] } UglifyJS2-3.6.3/test/compress/issue-1275.js000066400000000000000000000024351355252637300202140ustar00rootroot00000000000000string_plus_optimization: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, side_effects: true, unused: true, } input: { function foo(anything) { function throwing_function() { throw "nope"; } try { console.log('0' + throwing_function() ? "yes" : "no"); } catch (ex) { console.log(ex); } console.log('0' + anything ? "yes" : "no"); console.log(anything + '0' ? "Yes" : "No"); console.log('' + anything); console.log(anything + ''); } foo(); } expect: { function foo(anything) { function throwing_function() { throw "nope"; } try { console.log((throwing_function(), "yes")); } catch (ex) { console.log(ex); } console.log("yes"); console.log("Yes"); console.log('' + anything); console.log(anything + ''); } foo(); } expect_stdout: true } UglifyJS2-3.6.3/test/compress/issue-1321.js000066400000000000000000000022211355252637300201750ustar00rootroot00000000000000issue_1321_no_debug: { mangle = { properties: { keep_quoted: true, }, } input: { var x = {}; x.foo = 1; x["a"] = 2 * x.foo; console.log(x.foo, x["a"]); } expect: { var x = {}; x.x = 1; x["a"] = 2 * x.x; console.log(x.x, x["a"]); } expect_stdout: true } issue_1321_debug: { mangle = { properties: { debug: "", keep_quoted: true, }, } input: { var x = {}; x.foo = 1; x["_$foo$_"] = 2 * x.foo; console.log(x.foo, x["_$foo$_"]); } expect: { var x = {}; x.x = 1; x["_$foo$_"] = 2 * x.x; console.log(x.x, x["_$foo$_"]); } expect_stdout: true } issue_1321_with_quoted: { mangle = { properties: { keep_quoted: false, }, } input: { var x = {}; x.foo = 1; x["a"] = 2 * x.foo; console.log(x.foo, x["a"]); } expect: { var x = {}; x.x = 1; x["o"] = 2 * x.x; console.log(x.x, x["o"]); } expect_stdout: true } UglifyJS2-3.6.3/test/compress/issue-143.js000066400000000000000000000021041355252637300201160ustar00rootroot00000000000000/** * There was an incorrect sort behaviour documented in issue #143: * (x = f(…)) <= x → x >= (x = f(…)) * * For example, let the equation be: * (a = parseInt('100')) <= a * * If a was an integer and has the value of 99, * (a = parseInt('100')) <= a → 100 <= 100 → true * * When transformed incorrectly: * a >= (a = parseInt('100')) → 99 >= 100 → false */ tranformation_sort_order_equal: { options = { comparisons: true, } input: { (a = parseInt('100')) == a } expect: { (a = parseInt('100')) == a } } tranformation_sort_order_unequal: { options = { comparisons: true, } input: { (a = parseInt('100')) != a } expect: { (a = parseInt('100')) != a } } tranformation_sort_order_lesser_or_equal: { options = { comparisons: true, } input: { (a = parseInt('100')) <= a } expect: { (a = parseInt('100')) <= a } } tranformation_sort_order_greater_or_equal: { options = { comparisons: true, } input: { (a = parseInt('100')) >= a } expect: { (a = parseInt('100')) >= a } }UglifyJS2-3.6.3/test/compress/issue-1431.js000066400000000000000000000063001355252637300202010ustar00rootroot00000000000000level_zero: { options = { keep_fnames: true, } mangle = { keep_fnames: true } input: { function f(x) { function n(a) { return a * a; } return function() { return x; }; } } expect: { function f(r) { function n(n) { return n * n; } return function() { return r; }; } } } level_one: { options = { keep_fnames: true, } mangle = { keep_fnames: true } input: { function f(x) { return function() { function n(a) { return a * a; } return x(n); }; } } expect: { function f(r) { return function() { function n(n) { return n * n; } return r(n); }; } } } level_two: { options = { keep_fnames: true, } mangle = { keep_fnames: true } input: { function f(x) { return function() { function r(a) { return a * a; } return function() { function n(a) { return a * a; } return x(n); }; }; } } expect: { function f(t) { return function() { function r(n) { return n * n; } return function() { function n(n) { return n * n; } return t(n); }; }; } } } level_three: { options = { keep_fnames: true, } mangle = { keep_fnames: true } input: { function f(x) { return function() { function r(a) { return a * a; } return [ function() { function t(a) { return a * a; } return t; }, function() { function n(a) { return a * a; } return x(n); } ]; }; } } expect: { function f(t) { return function() { function r(n) { return n * n; } return [ function() { function t(n) { return n * n; } return t; }, function() { function n(n) { return n * n; } return t(n); } ]; }; } } } UglifyJS2-3.6.3/test/compress/issue-1443.js000066400000000000000000000024021355252637300202030ustar00rootroot00000000000000// tests assume that variable `undefined` not redefined and has `void 0` as value unsafe_undefined: { options = { conditionals: true, if_return: true, unsafe_undefined: true, } mangle = {} input: { function f(undefined) { return function() { if (a) return b; if (c) return d; }; } } expect: { function f(n) { return function() { return a ? b : c ? d : n; }; } } } keep_fnames: { options = { conditionals: true, if_return: true, unsafe_undefined: true, } mangle = { keep_fnames: true } input: { function f(undefined) { return function() { function n(a) { return a * a; } if (a) return b; if (c) return d; }; } } expect: { function f(r) { return function() { function n(n) { return n * n; } return a ? b : c ? d : r; }; } } } UglifyJS2-3.6.3/test/compress/issue-1446.js000066400000000000000000000034211355252637300202100ustar00rootroot00000000000000typeof_eq_undefined: { options = { comparisons: true, typeofs: true, } input: { var a = typeof b != "undefined"; b = typeof a != "undefined"; var c = typeof d.e !== "undefined"; var f = "undefined" === typeof g; g = "undefined" === typeof f; var h = "undefined" == typeof i.j; } expect: { var a = "undefined" != typeof b; b = void 0 !== a; var c = void 0 !== d.e; var f = "undefined" == typeof g; g = void 0 === f; var h = void 0 === i.j; } } typeof_eq_undefined_ie8: { options = { comparisons: true, ie8: true, typeofs: true, } input: { var a = typeof b != "undefined"; b = typeof a != "undefined"; var c = typeof d.e !== "undefined"; var f = "undefined" === typeof g; g = "undefined" === typeof f; var h = "undefined" == typeof i.j; } expect: { var a = "undefined" != typeof b; b = void 0 !== a; var c = "undefined" != typeof d.e; var f = "undefined" == typeof g; g = void 0 === f; var h = "undefined" == typeof i.j; } } undefined_redefined: { options = { comparisons: true, typeofs: true, } input: { function f(undefined) { var n = 1; return typeof n == "undefined"; } } expect_exact: "function f(undefined){var n=1;return void 0===n}" } undefined_redefined_mangle: { options = { comparisons: true, typeofs: true, } mangle = {} input: { function f(undefined) { var n = 1; return typeof n == "undefined"; } } expect_exact: "function f(n){var r=1;return void 0===r}" } UglifyJS2-3.6.3/test/compress/issue-1447.js000066400000000000000000000015561355252637300202200ustar00rootroot00000000000000else_with_empty_block: { options = {} input: { if (x) yes(); else { } } expect_exact: "if(x)yes();" } else_with_empty_statement: { options = {} input: { if (x) yes(); else ; } expect_exact: "if(x)yes();" } conditional_false_stray_else_in_loop: { options = { booleans: true, comparisons: true, conditionals: false, dead_code: true, evaluate: true, hoist_vars: true, if_return: true, join_vars: true, loops: true, side_effects: true, unused: true, } input: { for (var i = 1; i <= 4; ++i) { if (i <= 2) continue; console.log(i); } } expect_exact: "for(var i=1;i<=4;++i)if(!(i<=2))console.log(i);" expect_stdout: true } UglifyJS2-3.6.3/test/compress/issue-1569.js000066400000000000000000000005311355252637300202150ustar00rootroot00000000000000inner_reference: { options = { side_effects: true, } input: { !function f(a) { return a && f(a - 1) + a; }(42); !function g(a) { return a; }(42); } expect: { !function f(a) { return a && f(a - 1) + a; }(42); !void 0; } } UglifyJS2-3.6.3/test/compress/issue-1588.js000066400000000000000000000032751355252637300202260ustar00rootroot00000000000000screw_ie8: { options = { ie8: false, } mangle = { ie8: false, } input: { try { throw "foo"; } catch (x) { console.log(x); } } expect_exact: 'try{throw"foo"}catch(o){console.log(o)}' expect_stdout: [ "foo" ] } support_ie8: { options = { ie8: true, } mangle = { ie8: true, } input: { try { throw "foo"; } catch (x) { console.log(x); } } expect_exact: 'try{throw"foo"}catch(x){console.log(x)}' expect_stdout: "foo" } safe_undefined: { options = { conditionals: true, if_return: true, unsafe: false, } mangle = {} input: { var a, c; console.log(function(undefined) { return function() { if (a) return b; if (c) return d; }; }(1)()); } expect: { var a, c; console.log(function(n) { return function() { return a ? b : c ? d : void 0; }; }(1)()); } expect_stdout: true } unsafe_undefined: { options = { conditionals: true, if_return: true, unsafe_undefined: true, } mangle = {} input: { var a, c; console.log(function(undefined) { return function() { if (a) return b; if (c) return d; }; }()()); } expect: { var a, c; console.log(function(n) { return function() { return a ? b : c ? d : n; }; }()()); } expect_stdout: true } UglifyJS2-3.6.3/test/compress/issue-1609.js000066400000000000000000000021101355252637300202030ustar00rootroot00000000000000chained_evaluation_1: { options = { collapse_vars: true, evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function() { var a = 1; (function() { var b = a, c; c = f(b); c.bar = b; })(); })(); } expect: { (function() { (function() { f(1).bar = 1; })(); })(); } } chained_evaluation_2: { options = { collapse_vars: true, evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function() { var a = "long piece of string"; (function() { var b = a, c; c = f(b); c.bar = b; })(); })(); } expect: { (function() { (function() { var b = "long piece of string"; f(b).bar = b; })(); })(); } } UglifyJS2-3.6.3/test/compress/issue-1639.js000066400000000000000000000032421355252637300202150ustar00rootroot00000000000000issue_1639_1: { options = { booleans: true, collapse_vars: true, conditionals: true, evaluate: true, join_vars: true, loops: true, sequences: true, side_effects: true, } input: { var a = 100, b = 10; var L1 = 5; while (--L1 > 0) { if ((--b), false) { if (b) { var ignore = 0; } } } console.log(a, b); } expect: { for (var a = 100, b = 10, L1 = 5; --L1 > 0;) if (--b, 0) var ignore = 0; console.log(a, b); } expect_stdout: "100 6" } issue_1639_2: { options = { booleans: true, collapse_vars: true, conditionals: true, evaluate: true, join_vars: true, sequences: true, side_effects: true, } input: { var a = 100, b = 10; function f19() { if (++a, false) if (a) if (++a); } f19(); console.log(a, b); } expect: { var a = 100, b = 10; function f19() { ++a, 1; } f19(), console.log(a, b); } expect_stdout: "101 10" } issue_1639_3: { options = { booleans: true, collapse_vars: true, conditionals: true, evaluate: true, sequences: true, side_effects: true, } input: { var a = 100, b = 10; a++ && false && a ? 0 : 0; console.log(a, b); } expect: { var a = 100, b = 10; a++, console.log(a, b); } expect_stdout: "101 10" } UglifyJS2-3.6.3/test/compress/issue-1656.js000066400000000000000000000017661355252637300202250ustar00rootroot00000000000000f7: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, drop_debugger: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, loops: true, negate_iife: true, passes: 3, properties: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, unused: true, } beautify = { beautify: true, } input: { var a = 100, b = 10; function f22464() { var brake146670 = 5; while (((b = a) ? !a : ~a ? null : b += a) && --brake146670 > 0) { } } f22464(); console.log(a, b); } expect_exact: [ "var b = 10;", "", "!function() {", " b = 100;", "}(), console.log(100, b);", ] expect_stdout: true } UglifyJS2-3.6.3/test/compress/issue-1673.js000066400000000000000000000062721355252637300202210ustar00rootroot00000000000000side_effects_catch: { options = { reduce_funcs: true, reduce_vars: true, side_effects: true, unused: true, } input: { function f() { function g() { try { throw 0; } catch (e) { console.log("PASS"); } } g(); } f(); } expect: { function f() { (function() { try { throw 0; } catch (e) { console.log("PASS"); } })(); } f(); } expect_stdout: "PASS" } side_effects_else: { options = { reduce_funcs: true, reduce_vars: true, side_effects: true, unused: true, } input: { function f(x) { function g() { if (x); else console.log("PASS"); } g(); } f(0); } expect: { function f(x) { (function() { if (x); else console.log("PASS"); })(); } f(0); } expect_stdout: "PASS" } side_effects_finally: { options = { reduce_funcs: true, reduce_vars: true, side_effects: true, unused: true, } input: { function f() { function g() { try { x(); } catch (e) { } finally { console.log("PASS"); } } g(); } f(); } expect: { function f() { (function() { try { x(); } catch (e) { } finally { console.log("PASS"); } })(); } f(); } expect_stdout: "PASS" } side_effects_label: { options = { reduce_funcs: true, reduce_vars: true, side_effects: true, unused: true, } input: { function f(x) { function g() { L: { console.log("PASS"); break L; } } g(); } f(0); } expect: { function f(x) { (function() { L: { console.log("PASS"); break L; } })(); } f(0); } expect_stdout: "PASS" } side_effects_switch: { options = { reduce_funcs: true, reduce_vars: true, side_effects: true, unused: true, } input: { function f() { function g() { switch (0) { default: case console.log("PASS"): } } g(); } f(); } expect: { function f() { (function() { switch (0) { default: case console.log("PASS"): } })(); } f(); } expect_stdout: "PASS" } UglifyJS2-3.6.3/test/compress/issue-1704.js000066400000000000000000000214331355252637300202100ustar00rootroot00000000000000mangle_catch: { options = { ie8: false, toplevel: false, } mangle = { ie8: false, toplevel: false, } input: { var a = "FAIL"; try { throw 1; } catch (args) { a = "PASS"; } console.log(a); } expect_exact: 'var a="FAIL";try{throw 1}catch(o){a="PASS"}console.log(a);' expect_stdout: "PASS" } mangle_catch_ie8: { options = { ie8: true, toplevel: false, } mangle = { ie8: true, toplevel: false, } input: { var a = "FAIL"; try { throw 1; } catch (args) { a = "PASS"; } console.log(a); } expect_exact: 'var a="FAIL";try{throw 1}catch(args){a="PASS"}console.log(a);' expect_stdout: "PASS" } mangle_catch_var: { options = { ie8: false, toplevel: false, } mangle = { ie8: false, toplevel: false, } input: { var a = "FAIL"; try { throw 1; } catch (args) { var a = "PASS"; } console.log(a); } expect_exact: 'var a="FAIL";try{throw 1}catch(o){var a="PASS"}console.log(a);' expect_stdout: "PASS" } mangle_catch_var_ie8: { options = { ie8: true, toplevel: false, } mangle = { ie8: true, toplevel: false, } input: { var a = "FAIL"; try { throw 1; } catch (args) { var a = "PASS"; } console.log(a); } expect_exact: 'var a="FAIL";try{throw 1}catch(args){var a="PASS"}console.log(a);' expect_stdout: "PASS" } mangle_catch_toplevel: { options = { ie8: false, toplevel: true, } mangle = { ie8: false, toplevel: true, } input: { var a = "FAIL"; try { throw 1; } catch (args) { a = "PASS"; } console.log(a); } expect_exact: 'var c="FAIL";try{throw 1}catch(o){c="PASS"}console.log(c);' expect_stdout: "PASS" } mangle_catch_ie8_toplevel: { options = { ie8: true, toplevel: true, } mangle = { ie8: true, toplevel: true, } input: { var a = "FAIL"; try { throw 1; } catch (args) { a = "PASS"; } console.log(a); } expect_exact: 'var o="FAIL";try{throw 1}catch(c){o="PASS"}console.log(o);' expect_stdout: "PASS" } mangle_catch_var_toplevel: { options = { ie8: false, toplevel: true, } mangle = { ie8: false, toplevel: true, } input: { var a = "FAIL"; try { throw 1; } catch (args) { var a = "PASS"; } console.log(a); } expect_exact: 'var r="FAIL";try{throw 1}catch(o){var r="PASS"}console.log(r);' expect_stdout: "PASS" } mangle_catch_var_ie8_toplevel: { options = { ie8: true, toplevel: true, } mangle = { ie8: true, toplevel: true, } input: { var a = "FAIL"; try { throw 1; } catch (args) { var a = "PASS"; } console.log(a); } expect_exact: 'var o="FAIL";try{throw 1}catch(r){var o="PASS"}console.log(o);' expect_stdout: "PASS" } mangle_catch_redef_1: { options = { ie8: false, toplevel: false, } mangle = { ie8: false, toplevel: false, } input: { var a = "PASS"; try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'var a="PASS";try{throw"FAIL1"}catch(a){var a="FAIL2"}console.log(a);' expect_stdout: "PASS" } mangle_catch_redef_1_ie8: { options = { ie8: true, toplevel: false, } mangle = { ie8: true, toplevel: false, } input: { var a = "PASS"; try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'var a="PASS";try{throw"FAIL1"}catch(a){var a="FAIL2"}console.log(a);' expect_stdout: "PASS" } mangle_catch_redef_1_toplevel: { options = { ie8: false, toplevel: true, } mangle = { ie8: false, toplevel: true, } input: { var a = "PASS"; try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'var o="PASS";try{throw"FAIL1"}catch(o){var o="FAIL2"}console.log(o);' expect_stdout: "PASS" } mangle_catch_redef_1_ie8_toplevel: { options = { ie8: true, toplevel: true, } mangle = { ie8: true, toplevel: true, } input: { var a = "PASS"; try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'var o="PASS";try{throw"FAIL1"}catch(o){var o="FAIL2"}console.log(o);' expect_stdout: "PASS" } mangle_catch_redef_2: { options = { ie8: false, toplevel: false, } mangle = { ie8: false, toplevel: false, } input: { try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'try{throw"FAIL1"}catch(a){var a="FAIL2"}console.log(a);' expect_stdout: "undefined" } mangle_catch_redef_2_ie8: { options = { ie8: true, toplevel: false, } mangle = { ie8: true, toplevel: false, } input: { try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'try{throw"FAIL1"}catch(a){var a="FAIL2"}console.log(a);' expect_stdout: "undefined" } mangle_catch_redef_2_toplevel: { options = { ie8: false, toplevel: true, } mangle = { ie8: false, toplevel: true, } input: { try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'try{throw"FAIL1"}catch(o){var o="FAIL2"}console.log(o);' expect_stdout: "undefined" } mangle_catch_redef_2_ie8_toplevel: { options = { ie8: true, toplevel: true, } mangle = { ie8: true, toplevel: true, } input: { try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'try{throw"FAIL1"}catch(o){var o="FAIL2"}console.log(o);' expect_stdout: "undefined" } mangle_catch_redef_3: { mangle = { ie8: false, toplevel: false, } input: { var o = "PASS"; try { throw 0; } catch (o) { (function() { function f() { o = "FAIL"; } f(), f(); })(); } console.log(o); } expect_exact: 'var o="PASS";try{throw 0}catch(o){(function(){function c(){o="FAIL"}c(),c()})()}console.log(o);' expect_stdout: true } mangle_catch_redef_3_toplevel: { mangle = { ie8: false, toplevel: true, } input: { var o = "PASS"; try { throw 0; } catch (o) { (function() { function f() { o = "FAIL"; } f(), f(); })(); } console.log(o); } expect_exact: 'var c="PASS";try{throw 0}catch(c){(function(){function o(){c="FAIL"}o(),o()})()}console.log(c);' expect_stdout: true } mangle_catch_redef_3_ie8: { mangle = { ie8: true, toplevel: false, } input: { var o = "PASS"; try { throw 0; } catch (o) { (function() { function f() { o = "FAIL"; } f(), f(); })(); } console.log(o); } expect_exact: 'var o="PASS";try{throw 0}catch(o){(function(){function c(){o="FAIL"}c(),c()})()}console.log(o);' expect_stdout: true } mangle_catch_redef_3_ie8_toplevel: { mangle = { ie8: true, toplevel: true, } input: { var o = "PASS"; try { throw 0; } catch (o) { (function() { function f() { o = "FAIL"; } f(), f(); })(); } console.log(o); } expect_exact: 'var c="PASS";try{throw 0}catch(c){(function(){function o(){c="FAIL"}o(),o()})()}console.log(c);' expect_stdout: true } UglifyJS2-3.6.3/test/compress/issue-1733.js000066400000000000000000000042051355252637300202100ustar00rootroot00000000000000function_iife_catch: { mangle = { ie8: false, } input: { function f(n) { !function() { try { throw 0; } catch (n) { var a = 1; console.log(n, a); } }(); } f(); } expect_exact: "function f(o){!function(){try{throw 0}catch(o){var c=1;console.log(o,c)}}()}f();" expect_stdout: "0 1" } function_iife_catch_ie8: { mangle = { ie8: true, } input: { function f(n) { !function() { try { throw 0; } catch (n) { var a = 1; console.log(n, a); } }(); } f(); } expect_exact: "function f(c){!function(){try{throw 0}catch(c){var o=1;console.log(c,o)}}()}f();" expect_stdout: "0 1" } function_catch_catch: { mangle = { ie8: false, } input: { var o = 0; function f() { try { throw 1; } catch (c) { try { throw 2; } catch (o) { var o = 3; console.log(o); } } console.log(o); } f(); } expect_exact: "var o=0;function f(){try{throw 1}catch(o){try{throw 2}catch(c){var c=3;console.log(c)}}console.log(c)}f();" expect_stdout: [ "3", "undefined", ] } function_catch_catch_ie8: { mangle = { ie8: true, } input: { var o = 0; function f() { try { throw 1; } catch (c) { try { throw 2; } catch (o) { var o = 3; console.log(o); } } console.log(o); } f(); } expect_exact: "var o=0;function f(){try{throw 1}catch(c){try{throw 2}catch(o){var o=3;console.log(o)}}console.log(o)}f();" expect_stdout: [ "3", "undefined", ] } UglifyJS2-3.6.3/test/compress/issue-1750.js000066400000000000000000000016611355252637300202120ustar00rootroot00000000000000case_1: { options = { dead_code: true, evaluate: true, switches: true, } input: { var a = 0, b = 1; switch (true) { case a || true: default: b = 2; case true: } console.log(a, b); } expect: { var a = 0, b = 1; switch (true) { case a || true: b = 2; } console.log(a, b); } expect_stdout: "0 2" } case_2: { options = { dead_code: true, evaluate: true, switches: true, } input: { var a = 0, b = 1; switch (0) { default: b = 2; case a: a = 3; case 0: } console.log(a, b); } expect: { var a = 0, b = 1; switch (0) { case a: a = 3; } console.log(a, b); } expect_stdout: "3 1" } UglifyJS2-3.6.3/test/compress/issue-1770.js000066400000000000000000000120711355252637300202110ustar00rootroot00000000000000mangle_props: { mangle = { properties: true, } input: { var obj = { undefined: 1, NaN: 2, Infinity: 3, "-Infinity": 4, null: 5, }; console.log( obj[void 0], obj[undefined], obj["undefined"], obj[0/0], obj[NaN], obj["NaN"], obj[1/0], obj[Infinity], obj["Infinity"], obj[-1/0], obj[-Infinity], obj["-Infinity"], obj[null], obj["null"] ); } expect: { var obj = { undefined: 1, NaN: 2, Infinity: 3, "-Infinity": 4, null: 5, }; console.log( obj[void 0], obj[void 0], obj["undefined"], obj[0/0], obj[NaN], obj["NaN"], obj[1/0], obj[1/0], obj["Infinity"], obj[-1/0], obj[-1/0], obj["-Infinity"], obj[null], obj["null"] ); } expect_stdout: "1 1 1 2 2 2 3 3 3 4 4 4 5 5" } numeric_literal: { mangle = { properties: true, } beautify = { beautify: true, } input: { var obj = { 0: 0, "-0": 1, 42: 2, "42": 3, 0x25: 4, "0x25": 5, 1E42: 6, "1E42": 7, "1e+42": 8, }; console.log(obj[-0], obj[-""], obj["-0"]); console.log(obj[42], obj["42"]); console.log(obj[0x25], obj["0x25"], obj[37], obj["37"]); console.log(obj[1E42], obj["1E42"], obj["1e+42"]); } expect_exact: [ 'var obj = {', ' 0: 0,', ' "-0": 1,', ' 42: 2,', ' 42: 3,', ' 37: 4,', ' o: 5,', ' 1e42: 6,', ' b: 7,', ' 1e42: 8', '};', '', 'console.log(obj[-0], obj[-""], obj["-0"]);', '', 'console.log(obj[42], obj["42"]);', '', 'console.log(obj[37], obj["o"], obj[37], obj["37"]);', '', 'console.log(obj[1e42], obj["b"], obj["1e+42"]);', ] expect_stdout: [ "0 0 1", "3 3", "4 5 4 4", "8 7 8", ] } identifier: { mangle = { properties: true, } input: { var obj = { abstract: 1, boolean: 2, byte: 3, char: 4, class: 5, double: 6, enum: 7, export: 8, extends: 9, final: 10, float: 11, goto: 12, implements: 13, import: 14, int: 15, interface: 16, let: 17, long: 18, native: 19, package: 20, private: 21, protected: 22, public: 23, short: 24, static: 25, super: 26, synchronized: 27, this: 28, throws: 29, transient: 30, volatile: 31, yield: 32, false: 33, null: 34, true: 35, break: 36, case: 37, catch: 38, const: 39, continue: 40, debugger: 41, default: 42, delete: 43, do: 44, else: 45, finally: 46, for: 47, function: 48, if: 49, in: 50, instanceof: 51, new: 52, return: 53, switch: 54, throw: 55, try: 56, typeof: 57, var: 58, void: 59, while: 60, with: 61, }; } expect: { var obj = { e: 1, t: 2, n: 3, a: 4, i: 5, o: 6, r: 7, l: 8, s: 9, c: 10, f: 11, u: 12, d: 13, h: 14, p: 15, b: 16, v: 17, w: 18, y: 19, g: 20, m: 21, k: 22, x: 23, j: 24, z: 25, q: 26, A: 27, B: 28, C: 29, D: 30, F: 31, G: 32, false: 33, null: 34, true: 35, H: 36, I: 37, J: 38, K: 39, L: 40, M: 41, N: 42, O: 43, P: 44, Q: 45, R: 46, S: 47, T: 48, U: 49, V: 50, W: 51, X: 52, Y: 53, Z: 54, $: 55, _: 56, ee: 57, te: 58, ne: 59, ae: 60, ie: 61, }; } } UglifyJS2-3.6.3/test/compress/issue-1787.js000066400000000000000000000005301355252637300202160ustar00rootroot00000000000000unary_prefix: { options = { evaluate: true, inline: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { console.log(function() { var x = -(2 / 3); return x; }()); } expect_exact: "console.log(-2/3);" expect_stdout: true } UglifyJS2-3.6.3/test/compress/issue-1833.js000066400000000000000000000045771355252637300202250ustar00rootroot00000000000000iife_for: { options = { negate_iife: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function f() { function g() { L: for (;;) break L; } g(); } f(); } expect: { !function() { !function() { L: for (;;) break L; }(); }(); } } iife_for_in: { options = { negate_iife: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function f() { function g() { L: for (var a in x) break L; } g(); } f(); } expect: { !function() { !function() { L: for (var a in x) break L; }(); }(); } } iife_do: { options = { negate_iife: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function f() { function g() { L: do { break L; } while (1); } g(); } f(); } expect: { !function() { !function() { L: do { break L; } while (1); }(); }(); } } iife_while: { options = { negate_iife: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function f() { function g() { L: while (1) break L; } g(); } f(); } expect: { !function() { !function() { L: while (1) break L; }(); }(); } } label_do: { options = { evaluate: true, loops: true, } input: { L: do { continue L; } while (0); } expect: { L: do { continue L; } while (0); } } label_while: { options = { dead_code: true, evaluate: true, loops: true, } input: { function f() { L: while (0) continue L; } } expect_exact: "function f(){L:0}" } UglifyJS2-3.6.3/test/compress/issue-1943.js000066400000000000000000000005721355252637300202160ustar00rootroot00000000000000operator: { input: { a. //comment typeof } expect_exact: "a.typeof;" } name: { input: { a. //comment b } expect_exact: "a.b;" } keyword: { input: { a. //comment default } expect_exact: "a.default;" } atom: { input: { a. //comment true } expect_exact: "a.true;" } UglifyJS2-3.6.3/test/compress/issue-208.js000066400000000000000000000027361355252637300201330ustar00rootroot00000000000000do_not_update_lhs: { options = { global_defs: { DEBUG: 0, }, } input: { DEBUG++; DEBUG += 1; DEBUG = 1; } expect: { DEBUG++; DEBUG += 1; DEBUG = 1; } } do_update_rhs: { options = { global_defs: { DEBUG: 0, }, } input: { MY_DEBUG = DEBUG; MY_DEBUG += DEBUG; } expect: { MY_DEBUG = 0; MY_DEBUG += 0; } } mixed: { options = { evaluate: true, global_defs: { DEBUG: 0, ENV: 1, FOO: 2, }, } input: { var ENV = 3; var FOO = 4; f(ENV * 10); --FOO; DEBUG = 1; DEBUG++; DEBUG += 1; f(DEBUG); x = DEBUG; } expect: { var ENV = 3; var FOO = 4; f(10); --FOO; DEBUG = 1; DEBUG++; DEBUG += 1; f(0); x = 0; } expect_warnings: [ "WARN: global_defs ENV redefined [test/compress/issue-208.js:1,12]", "WARN: global_defs FOO redefined [test/compress/issue-208.js:2,12]", "WARN: global_defs FOO redefined [test/compress/issue-208.js:4,10]", "WARN: global_defs DEBUG redefined [test/compress/issue-208.js:5,8]", "WARN: global_defs DEBUG redefined [test/compress/issue-208.js:6,8]", "WARN: global_defs DEBUG redefined [test/compress/issue-208.js:7,8]", ] } UglifyJS2-3.6.3/test/compress/issue-22.js000066400000000000000000000005341355252637300200370ustar00rootroot00000000000000return_with_no_value_in_if_body: { options = { conditionals: true, } input: { function foo(bar) { if (bar) { return; } else { return 1; } } } expect: { function foo (bar) { return bar ? void 0 : 1; } } } UglifyJS2-3.6.3/test/compress/issue-2652.js000066400000000000000000000007351355252637300202150ustar00rootroot00000000000000insert_semicolon: { beautify = { beautify: true, comments: "all", } input: { var a /* foo */ var b } expect_exact: [ "var a", "/* foo */;", "", "var b;", ] } unary_postfix: { beautify = { beautify: true, comments: "all", } input: { a /* foo */++b } expect_exact: [ "a", "/* foo */;", "", "++b;", ] } UglifyJS2-3.6.3/test/compress/issue-267.js000066400000000000000000000003221355252637300201250ustar00rootroot00000000000000issue_267: { options = { comparisons: true, } input: { x = a % b / b * c * 2; x = a % b * 2 } expect: { x = a % b / b * c * 2; x = a % b * 2; } } UglifyJS2-3.6.3/test/compress/issue-269.js000066400000000000000000000023511355252637300201330ustar00rootroot00000000000000issue_269_1: { options = { unsafe: true, } input: { f( String(x), Number(x), Boolean(x), String(), Number(), Boolean() ); } expect: { f( x + '', +x, !!x, '', 0, false ); } } issue_269_dangers: { options = { unsafe: true, } input: { f( String(x, x), Number(x, x), Boolean(x, x) ); } expect: { f(String(x, x), Number(x, x), Boolean(x, x)); } } issue_269_in_scope: { options = { unsafe: true, } input: { var String, Number, Boolean; f( String(x), Number(x, x), Boolean(x) ); } expect: { var String, Number, Boolean; f(String(x), Number(x, x), Boolean(x)); } } strings_concat: { options = { unsafe: true, } input: { f( String(x + 'str'), String('str' + x) ); } expect: { f( x + 'str', 'str' + x ); } } regexp: { options = { evaluate: true, unsafe: true, } input: { RegExp("foo"); RegExp("bar", "ig"); RegExp(foo); RegExp("bar", ig); RegExp("should", "fail"); } expect: { /foo/; /bar/ig; RegExp(foo); RegExp("bar", ig); RegExp("should", "fail"); } expect_warnings: [ 'WARN: Error converting RegExp("should","fail") [test/compress/issue-269.js:5,2]', ] } UglifyJS2-3.6.3/test/compress/issue-2719.js000066400000000000000000000014731355252637300202210ustar00rootroot00000000000000warn: { options = { evaluate: true, inline: true, passes: 2, properties: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function f() { return g(); } function g() { return g["call" + "er"].arguments; } // 3 console.log(f(1, 2, 3).length); } expect: { // TypeError: Cannot read property 'arguments' of null console.log(function g() { return g.caller.arguments; }().length); } expect_warnings: [ "WARN: Function.prototype.caller not supported [test/compress/issue-2719.js:5,19]", "WARN: Function.prototype.arguments not supported [test/compress/issue-2719.js:5,19]", ] } UglifyJS2-3.6.3/test/compress/issue-281.js000066400000000000000000000234141355252637300201300ustar00rootroot00000000000000collapse_vars_constants: { options = { collapse_vars: true, evaluate: true, inline: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f1(x) { var a = 4, b = x.prop, c = 5, d = sideeffect1(), e = sideeffect2(); return b + (function() { return d - a * e - c; })(); } function f2(x) { var a = 4, b = x.prop, c = 5, not_used = sideeffect1(), e = sideeffect2(); return b + (function() { return -a * e - c; })(); } } expect: { function f1(x) { var b = x.prop, d = sideeffect1(), e = sideeffect2(); return b + (d - 4 * e - 5); } function f2(x) { var b = x.prop; sideeffect1(); return b + (-4 * sideeffect2() - 5); } } } modified: { options = { collapse_vars: true, inline: true, unused: true, } input: { function f5(b) { var a = function() { return b; }(); return b++ + a; } console.log(f5(1)); } expect: { function f5(b) { var a = b; return b++ + a; } console.log(f5(1)); } expect_stdout: "2" } ref_scope: { options = { collapse_vars: true, inline: true, unused: true, } input: { console.log(function() { var a = 1, b = 2, c = 3; var a = c++, b = b /= a; return function() { return a; }() + b; }()); } expect: { console.log(function() { var a = 1, b = 2, c = 3; b = b /= a = c++; return a + b; }()); } expect_stdout: true } safe_undefined: { options = { conditionals: true, if_return: true, inline: true, unsafe: false, unused: true, } mangle = {} input: { var a, c; console.log(function(undefined) { return function() { if (a) return b; if (c) return d; }; }(1)()); } expect: { var a, c; console.log(a ? b : c ? d : void 0); } expect_stdout: true } negate_iife_3: { options = { conditionals: true, expression: true, inline: true, negate_iife: true, } input: { (function(){ return t })() ? console.log(true) : console.log(false); } expect: { t ? console.log(true) : console.log(false); } } negate_iife_3_off: { options = { conditionals: true, expression: true, inline: true, negate_iife: false, } input: { (function(){ return t })() ? console.log(true) : console.log(false); } expect: { t ? console.log(true) : console.log(false); } } negate_iife_4: { options = { conditionals: true, expression: true, inline: true, negate_iife: true, sequences: true, } input: { (function(){ return t })() ? console.log(true) : console.log(false); (function(){ console.log("something"); })(); } expect: { t ? console.log(true) : console.log(false), void console.log("something"); } } negate_iife_5: { options = { conditionals: true, expression: true, inline: true, negate_iife: true, sequences: true, } input: { if ((function(){ return t })()) { foo(true); } else { bar(false); } (function(){ console.log("something"); })(); } expect: { t ? foo(true) : bar(false), void console.log("something"); } } negate_iife_5_off: { options = { conditionals: true, expression: true, inline: true, negate_iife: false, sequences: true, } input: { if ((function(){ return t })()) { foo(true); } else { bar(false); } (function(){ console.log("something"); })(); } expect: { t ? foo(true) : bar(false), void console.log("something"); } } issue_1254_negate_iife_true: { options = { expression: true, inline: true, negate_iife: true, } input: { (function() { return function() { console.log('test') }; })()(); } expect_exact: 'void console.log("test");' expect_stdout: true } issue_1254_negate_iife_nested: { options = { expression: true, inline: true, negate_iife: true, } input: { (function() { return function() { console.log('test') }; })()()()()(); } expect_exact: '(void console.log("test"))()()();' } negate_iife_issue_1073: { options = { conditionals: true, evaluate: true, inline: true, negate_iife: true, reduce_funcs: true, reduce_vars: true, sequences: true, unused: true, } input: { new (function(a) { return function Foo() { this.x = a; console.log(this); }; }(7))(); } expect: { new function() { this.x = 7, console.log(this); }(); } expect_stdout: true } issue_1288_side_effects: { options = { conditionals: true, evaluate: true, inline: true, negate_iife: true, reduce_funcs: true, reduce_vars: true, side_effects: true, unused: true, } input: { if (w) ; else { (function f() {})(); } if (!x) { (function() { x = {}; })(); } if (y) (function() {})(); else (function(z) { return z; })(0); } expect: { w; x || (x = {}); y; } } inner_var_for_in_1: { options = { evaluate: true, inline: true, reduce_funcs: true, reduce_vars: true, } input: { function f() { var a = 1, b = 2; for (b in (function() { return x(a, b, c); })()) { var c = 3, d = 4; x(a, b, c, d); } x(a, b, c, d); } } expect: { function f() { var a = 1, b = 2; for (b in x(1, b, c)) { var c = 3, d = 4; x(1, b, c, d); } x(1, b, c, d); } } } issue_1595_3: { options = { evaluate: true, inline: true, passes: 2, reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function f(a) { return g(a + 1); })(2); } expect: { g(3); } } issue_1758: { options = { inline: true, sequences: true, side_effects: true, } input: { console.log(function(c) { var undefined = 42; return function() { c--; c--, c.toString(); return; }(); }()); } expect: { console.log(function(c) { var undefined = 42; return c--, c--, void c.toString(); }()); } expect_stdout: "undefined" } wrap_iife: { options = { inline: true, negate_iife: false, } beautify = { wrap_iife: true, } input: { (function() { return function() { console.log('test') }; })()(); } expect_exact: 'void console.log("test");' } wrap_iife_in_expression: { options = { inline: true, negate_iife: false, } beautify = { wrap_iife: true, } input: { foo = (function() { return bar(); })(); } expect_exact: 'foo=bar();' } wrap_iife_in_return_call: { options = { inline: true, negate_iife: false, } beautify = { wrap_iife: true, } input: { (function() { return (function() { console.log('test') })(); })()(); } expect_exact: '(void console.log("test"))();' } pure_annotation_1: { options = { inline: true, side_effects: true, } input: { /*@__PURE__*/(function() { console.log("hello"); }()); } expect_exact: "" } pure_annotation_2: { options = { collapse_vars: true, inline: true, side_effects: true, } input: { /*@__PURE__*/(function(n) { console.log("hello", n); }(42)); } expect_exact: "" } drop_fargs: { options = { collapse_vars: true, inline: true, keep_fargs: false, side_effects: true, unused: true, } input: { var a = 1; !function(a_1) { a++; }(a++ + (a && a.var)); console.log(a); } expect: { var a = 1; ++a && a.var, a++; console.log(a); } expect_stdout: "3" } keep_fargs: { options = { collapse_vars: true, inline: true, keep_fargs: true, side_effects: true, unused: true, } input: { var a = 1; !function(a_1) { a++; }(a++ + (a && a.var)); console.log(a); } expect: { var a = 1; ++a && a.var, a++; console.log(a); } expect_stdout: "3" } UglifyJS2-3.6.3/test/compress/issue-2871.js000066400000000000000000000012131355252637300202100ustar00rootroot00000000000000comparison_with_undefined: { options = { comparisons: true, } input: { a == undefined; a != undefined; a === undefined; a !== undefined; undefined == a; undefined != a; undefined === a; undefined !== a; void 0 == a; void 0 != a; void 0 === a; void 0 !== a; } expect: { null == a; null != a; void 0 === a; void 0 !== a; null == a; null != a; void 0 === a; void 0 !== a; null == a; null != a; void 0 === a; void 0 !== a; } } UglifyJS2-3.6.3/test/compress/issue-2989.js000066400000000000000000000006351355252637300202310ustar00rootroot00000000000000inline_script_off: { beautify = { inline_script: false, } input: { console.log(""); } expect_exact: 'console.log("");' expect_stdout: "" } inline_script_on: { beautify = { inline_script: true, } input: { console.log(""); } expect_exact: 'console.log("<\\/sCrIpT>");' expect_stdout: "" } UglifyJS2-3.6.3/test/compress/issue-368.js000066400000000000000000000024711355252637300201360ustar00rootroot00000000000000collapse: { options = { collapse_vars: true, sequences: true, side_effects: true, unused: true, } input: { function f1() { var a; a = typeof b === 'function' ? b() : b; return a !== undefined && c(); } function f2(b) { var a; b = c(); a = typeof b === 'function' ? b() : b; return 'stirng' == typeof a && d(); } function f3(c) { var a; a = b(a / 2); if (a < 0) { a++; ++c; return c / 2; } } function f4(c) { var a; a = b(a / 2); if (a < 0) { a++; c++; return c / 2; } } } expect: { function f1() { return void 0 !== ('function' === typeof b ? b() : b) && c(); } function f2(b) { return 'stirng' == typeof ('function' === typeof (b = c()) ? b() : b) && d(); } function f3(c) { var a; if ((a = b(a / 2)) < 0) return a++, ++c / 2; } function f4(c) { var a; if ((a = b(a / 2)) < 0) return a++, ++c / 2; } } } UglifyJS2-3.6.3/test/compress/issue-44.js000066400000000000000000000011611355252637300200400ustar00rootroot00000000000000issue_44_valid_ast_1: { options = { unused: true, } input: { function a(b) { for (var i = 0, e = b.qoo(); ; i++) {} } } expect: { function a(b) { var i = 0; for (b.qoo(); ; i++); } } } issue_44_valid_ast_2: { options = { unused: true, } input: { function a(b) { if (foo) for (var i = 0, e = b.qoo(); ; i++) {} } } expect: { function a(b) { if (foo) { var i = 0; for (b.qoo(); ; i++); } } } } UglifyJS2-3.6.3/test/compress/issue-59.js000066400000000000000000000010471355252637300200510ustar00rootroot00000000000000keep_continue: { options = { dead_code: true, evaluate: true, } input: { while (a) { if (b) { switch (true) { case c(): d(); } continue; } f(); } } expect: { while (a) { if (b) { switch (true) { case c(): d(); } continue; } f(); } } } UglifyJS2-3.6.3/test/compress/issue-597.js000066400000000000000000000062511355252637300201420ustar00rootroot00000000000000NaN_and_Infinity_must_have_parens: { options = {} input: { Infinity.toString(); NaN.toString(); } expect: { (1/0).toString(); NaN.toString(); } } NaN_and_Infinity_should_not_be_replaced_when_they_are_redefined: { options = {} input: { var Infinity, NaN; Infinity.toString(); NaN.toString(); } expect: { var Infinity, NaN; Infinity.toString(); NaN.toString(); } } NaN_and_Infinity_must_have_parens_evaluate: { options = { evaluate: true, } input: { (123456789 / 0).toString(); (+"foo").toString(); } expect: { (1/0).toString(); NaN.toString(); } } NaN_and_Infinity_should_not_be_replaced_when_they_are_redefined_evaluate: { options = { evaluate: true, } input: { var Infinity, NaN; (123456789 / 0).toString(); (+"foo").toString(); } expect: { var Infinity, NaN; (1/0).toString(); (0/0).toString(); } } beautify_off_1: { options = { evaluate: true, } beautify = { beautify: false, } input: { var NaN; console.log( null, undefined, Infinity, NaN, Infinity * undefined, Infinity.toString(), NaN.toString(), (Infinity * undefined).toString() ); } expect_exact: "var NaN;console.log(null,void 0,1/0,NaN,0/0,(1/0).toString(),NaN.toString(),(0/0).toString());" expect_stdout: true } beautify_off_2: { options = { evaluate: true, } beautify = { beautify: false, } input: { console.log( null.toString(), undefined.toString() ); } expect_exact: "console.log(null.toString(),(void 0).toString());" } beautify_on_1: { options = { evaluate: true, } beautify = { beautify: true, } input: { var NaN; console.log( null, undefined, Infinity, NaN, Infinity * undefined, Infinity.toString(), NaN.toString(), (Infinity * undefined).toString() ); } expect_exact: [ "var NaN;", "", "console.log(null, void 0, 1 / 0, NaN, 0 / 0, (1 / 0).toString(), NaN.toString(), (0 / 0).toString());", ] expect_stdout: true } beautify_on_2: { options = { evaluate: true, } beautify = { beautify: true, } input: { console.log( null.toString(), undefined.toString() ); } expect_exact: "console.log(null.toString(), (void 0).toString());" } issue_1724: { input: { var a = 0; ++a % Infinity | Infinity ? a++ : 0; console.log(a); } expect_exact: "var a=0;++a%(1/0)|1/0?a++:0;console.log(a);" expect_stdout: "2" } issue_1725: { input: { ([].length === 0) % Infinity ? console.log("PASS") : console.log("FAIL"); } expect_exact: '(0===[].length)%(1/0)?console.log("PASS"):console.log("FAIL");' expect_stdout: "PASS" } UglifyJS2-3.6.3/test/compress/issue-611.js000066400000000000000000000005051355252637300201210ustar00rootroot00000000000000issue_611: { options = { sequences: true, side_effects: true, } input: { define(function() { function fn() {} if (fn()) { fn(); return void 0; } }); } expect: { define(function() { function fn(){} if (fn()) return void fn(); }); } } UglifyJS2-3.6.3/test/compress/issue-637.js000066400000000000000000000006241355252637300201330ustar00rootroot00000000000000wrongly_optimized: { options = { booleans: true, conditionals: true, evaluate: true, } input: { function func() { foo(); } if (func() || true) { bar(); } } expect: { function func() { foo(); } // TODO: optimize to `func(), bar()` (func(), 1) && bar(); } } UglifyJS2-3.6.3/test/compress/issue-640.js000066400000000000000000000150541355252637300201300ustar00rootroot00000000000000cond_5: { options = { conditionals: true, expression: true, } input: { if (some_condition()) { if (some_other_condition()) { do_something(); } else { alternate(); } } else { alternate(); } if (some_condition()) { if (some_other_condition()) { do_something(); } } } expect: { some_condition() && some_other_condition() ? do_something() : alternate(); if (some_condition() && some_other_condition()) do_something(); } } dead_code_const_annotation_regex: { options = { booleans: true, conditionals: true, dead_code: true, evaluate: true, expression: true, loops: true, } input: { var unused; // @constraint this shouldn't be a constant var CONST_FOO_ANN = false; if (CONST_FOO_ANN) { console.log("reachable"); } } expect: { var unused; var CONST_FOO_ANN = !1; if (CONST_FOO_ANN) console.log('reachable'); } expect_stdout: true } drop_console_2: { options = { drop_console: true, expression: true, } input: { console.log('foo'); console.log.apply(console, arguments); } expect: { // with regular compression these will be stripped out as well void 0; void 0; } } drop_value: { options = { expression: true, side_effects: true, } input: { (1, [2, foo()], 3, {a:1, b:bar()}); } expect: { foo(), {a:1, b:bar()}; } } wrongly_optimized: { options = { booleans: true, conditionals: true, evaluate: true, expression: true, } input: { function func() { foo(); } if (func() || true) { bar(); } } expect: { function func() { foo(); } // TODO: optimize to `func(), bar()` if (func(), 1) bar(); } } negate_iife_1: { options = { expression: true, negate_iife: true, } input: { (function(){ stuff() })(); } expect: { (function(){ stuff() })(); } } negate_iife_3: { options = { conditionals: true, expression: true, negate_iife: true, } input: { (function(){ return t })() ? console.log(true) : console.log(false); } expect: { (function(){ return t })() ? console.log(true) : console.log(false); } } negate_iife_3_off: { options = { conditionals: true, expression: true, negate_iife: false, } input: { (function(){ return t })() ? console.log(true) : console.log(false); } expect: { (function(){ return t })() ? console.log(true) : console.log(false); } } negate_iife_4: { options = { conditionals: true, expression: true, negate_iife: true, sequences: true, } input: { (function(){ return t })() ? console.log(true) : console.log(false); (function(){ console.log("something"); })(); } expect: { !function(){ return t }() ? console.log(false) : console.log(true), function(){ console.log("something"); }(); } } negate_iife_5: { options = { conditionals: true, expression: true, negate_iife: true, sequences: true, } input: { if ((function(){ return t })()) { foo(true); } else { bar(false); } (function(){ console.log("something"); })(); } expect: { !function(){ return t }() ? bar(false) : foo(true), function(){ console.log("something"); }(); } } negate_iife_5_off: { options = { conditionals: true, expression: true, negate_iife: false, sequences: true, } input: { if ((function(){ return t })()) { foo(true); } else { bar(false); } (function(){ console.log("something"); })(); } expect: { !function(){ return t }() ? bar(false) : foo(true), function(){ console.log("something"); }(); } } issue_1254_negate_iife_true: { options = { expression: true, negate_iife: true, } input: { (function() { return function() { console.log('test') }; })()(); } expect_exact: '(function(){return function(){console.log("test")}})()();' expect_stdout: true } issue_1254_negate_iife_nested: { options = { expression: true, negate_iife: true, } input: { (function() { return function() { console.log('test') }; })()()()()(); } expect_exact: '(function(){return function(){console.log("test")}})()()()()();' expect_stdout: true } conditional: { options = { expression: true, pure_funcs: [ "pure" ], side_effects: true, } input: { pure(1 | a() ? 2 & b() : 7 ^ c()); pure(1 | a() ? 2 & b() : 5); pure(1 | a() ? 4 : 7 ^ c()); pure(1 | a() ? 4 : 5); pure(3 ? 2 & b() : 7 ^ c()); pure(3 ? 2 & b() : 5); pure(3 ? 4 : 7 ^ c()); pure(3 ? 4 : 5); } expect: { 1 | a() ? b() : c(); 1 | a() && b(); 1 | a() || c(); a(); 3 ? b() : c(); 3 && b(); 3 || c(); pure(3 ? 4 : 5); } } limit_1: { options = { expression: true, sequences: 3, } input: { a; b; c; d; e; f; g; h; i; j; k; } expect: { // Turned into a single return statement // so it can no longer be split into lines a,b,c,d,e,f,g,h,i,j,k; } } iife: { options = { expression: true, sequences: true, } input: { x = 42; (function a() {})(); !function b() {}(); ~function c() {}(); +function d() {}(); -function e() {}(); void function f() {}(); typeof function g() {}(); } expect: { x = 42, function a() {}(), function b() {}(), function c() {}(), function d() {}(), function e() {}(), function f() {}(), typeof function g() {}(); } } UglifyJS2-3.6.3/test/compress/issue-747.js000066400000000000000000000015101355252637300201300ustar00rootroot00000000000000dont_reuse_prop: { mangle = { properties: { regex: /asd/, }, } input: { "aaaaaaaaaabbbbb"; var obj = {}; obj.a = 123; obj.asd = 256; console.log(obj.a); } expect: { "aaaaaaaaaabbbbb"; var obj = {}; obj.a = 123; obj.b = 256; console.log(obj.a); } expect_stdout: "123" } unmangleable_props_should_always_be_reserved: { mangle = { properties: { regex: /asd/, }, } input: { "aaaaaaaaaabbbbb"; var obj = {}; obj.asd = 256; obj.a = 123; console.log(obj.a); } expect: { "aaaaaaaaaabbbbb"; var obj = {}; obj.b = 256; obj.a = 123; console.log(obj.a); } expect_stdout: "123" } UglifyJS2-3.6.3/test/compress/issue-751.js000066400000000000000000000010551355252637300201270ustar00rootroot00000000000000negate_booleans_1: { options = { comparisons: true, } input: { var a = !a || !b || !c || !d || !e || !f; } expect: { var a = !(a && b && c && d && e && f); } } negate_booleans_2: { options = { comparisons: true, } input: { var match = !x && // should not touch this one (!z || c) && (!k || d) && the_stuff(); } expect: { var match = !x && (!z || c) && (!k || d) && the_stuff(); } } UglifyJS2-3.6.3/test/compress/issue-782.js000066400000000000000000000016521355252637300201360ustar00rootroot00000000000000remove_sequence: { options = { side_effects: true, } input: { (0, 1, eval)(); (0, 1, logThis)(); (0, 1, _decorators.logThis)(); } expect: { eval(); logThis(); (0, _decorators.logThis)(); } } remove_redundant_sequence_items: { options = { side_effects: true, } input: { "use strict"; (0, 1, eval)(); (0, 1, logThis)(); (0, 1, _decorators.logThis)(); } expect: { "use strict"; (0, eval)(); logThis(); (0, _decorators.logThis)(); } } dont_remove_this_binding_sequence: { options = { side_effects: true, } input: { "use strict"; (0, eval)(); (0, logThis)(); (0, _decorators.logThis)(); } expect: { "use strict"; (0, eval)(); logThis(); (0, _decorators.logThis)(); } } UglifyJS2-3.6.3/test/compress/issue-892.js000066400000000000000000000014651355252637300201420ustar00rootroot00000000000000dont_mangle_arguments: { mangle = { }; options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, drop_debugger: true, evaluate: true, hoist_funs: true, hoist_vars: true, if_return: true, join_vars: true, keep_fargs: true, keep_fnames: false, loops: true, negate_iife: false, properties: true, sequences: true, side_effects: true, unused: true, } input: { (function(){ var arguments = arguments, not_arguments = 9; console.log(not_arguments, arguments); })(5,6,7); } expect_exact: "(function(){var arguments=arguments,o=9;console.log(o,arguments)})(5,6,7);" expect_stdout: true } UglifyJS2-3.6.3/test/compress/issue-913.js000066400000000000000000000005651355252637300201340ustar00rootroot00000000000000keep_var_for_in: { options = { hoist_vars: true, unused: true, } input: { (function(obj){ var foo = 5; for (var i in obj) return foo; })(); } expect: { (function(obj){ var i, foo = 5; for (i in obj) return foo; })(); } } UglifyJS2-3.6.3/test/compress/issue-973.js000066400000000000000000000064041355252637300201400ustar00rootroot00000000000000this_binding_conditionals: { options = { conditionals: true, evaluate: true, side_effects: true, } input: { "use strict"; (1 && a)(); (0 || a)(); (0 || 1 && a)(); (1 ? a : 0)(); (1 && a.b)(); (0 || a.b)(); (0 || 1 && a.b)(); (1 ? a.b : 0)(); (1 && a[b])(); (0 || a[b])(); (0 || 1 && a[b])(); (1 ? a[b] : 0)(); (1 && eval)(); (0 || eval)(); (0 || 1 && eval)(); (1 ? eval : 0)(); } expect: { "use strict"; a(); a(); a(); a(); (0, a.b)(); (0, a.b)(); (0, a.b)(); (0, a.b)(); (0, a[b])(); (0, a[b])(); (0, a[b])(); (0, a[b])(); (0, eval)(); (0, eval)(); (0, eval)(); (0, eval)(); } } this_binding_collapse_vars: { options = { collapse_vars: true, toplevel: true, unused: true, } input: { "use strict"; var c = a; c(); var d = a.b; d(); var e = eval; e(); } expect: { "use strict"; a(); (0, a.b)(); (0, eval)(); } } this_binding_side_effects: { options = { side_effects: true, } input: { (function(foo) { (0, foo)(); (0, foo.bar)(); (0, eval)("console.log(foo);"); }()); (function(foo) { "use strict"; (0, foo)(); (0, foo.bar)(); (0, eval)("console.log(foo);"); }()); (function(foo) { var eval = console; (0, foo)(); (0, foo.bar)(); (0, eval)("console.log(foo);"); }()); } expect: { (function(foo) { foo(); (0, foo.bar)(); eval("console.log(foo);"); }()); (function(foo) { "use strict"; foo(); (0, foo.bar)(); (0, eval)("console.log(foo);"); }()); (function(foo) { var eval = console; foo(); (0, foo.bar)(); eval("console.log(foo);"); }()); } } this_binding_sequences: { options = { sequences: true, side_effects: true, } input: { console.log(typeof function() { return eval("this"); }()); console.log(typeof function() { "use strict"; return eval("this"); }()); console.log(typeof function() { return (0, eval)("this"); }()); console.log(typeof function() { "use strict"; return (0, eval)("this"); }()); } expect: { console.log(typeof function() { return eval("this"); }()), console.log(typeof function() { "use strict"; return eval("this"); }()), console.log(typeof function() { return eval("this"); }()), console.log(typeof function() { "use strict"; return (0, eval)("this"); }()); } expect_stdout: [ "object", "undefined", "object", "object", ] } UglifyJS2-3.6.3/test/compress/issue-976.js000066400000000000000000000052741355252637300201470ustar00rootroot00000000000000eval_collapse_vars: { options = { booleans: true, collapse_vars: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: false, side_effects: true, unused: true, } input: { function f1() { var e = 7; var s = "abcdef"; var i = 2; var eval = console.log.bind(console); var x = s.charAt(i++); var y = s.charAt(i++); var z = s.charAt(i++); eval(x, y, z, e); } function p1() { var a = foo(), b = bar(), eval = baz(); return a + b + eval; } function p2() { var a = foo(), b = bar(), eval = baz; return a + b + eval(); } (function f2(eval) { var a = 2; console.log(a - 5); eval("console.log(a);"); })(eval); } expect: { function f1() { var e = 7, s = "abcdef", i = 2, eval = console.log.bind(console), x = s.charAt(i++), y = s.charAt(i++), z = s.charAt(i++); eval(x, y, z, e); } function p1() { return foo() + bar() + baz(); } function p2() { var a = foo(), b = bar(), eval = baz; return a + b + eval(); } (function f2(eval) { var a = 2; console.log(a - 5); eval("console.log(a);"); })(eval); } expect_stdout: true } eval_unused: { options = { keep_fargs: false, unused: true, } input: { function f1(a, eval, c, d, e) { return a('c') + eval; } function f2(a, b, c, d, e) { return a + eval('c'); } function f3(a, eval, c, d, e) { return a + eval('c'); } } expect: { function f1(a, eval) { return a('c') + eval; } function f2(a, b, c, d, e) { return a + eval('c'); } function f3(a, eval, c, d, e) { return a + eval('c'); } } } eval_mangle: { mangle = { }; input: { function f1(a, eval, c, d, e) { return a('c') + eval; } function f2(a, b, c, d, e) { return a + eval('c'); } function f3(a, eval, c, d, e) { return a + eval('c'); } } expect_exact: 'function f1(n,c,e,a,f){return n("c")+c}function f2(a,b,c,d,e){return a+eval("c")}function f3(a,eval,c,d,e){return a+eval("c")}' } UglifyJS2-3.6.3/test/compress/issue-979.js000066400000000000000000000043371355252637300201510ustar00rootroot00000000000000issue979_reported: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { function f1() { if (a == 1 || b == 2) { foo(); } } function f2() { if (!(a == 1 || b == 2)) { } else { foo(); } } } expect: { function f1() { 1!=a&&2!=b||foo(); } function f2() { 1!=a&&2!=b||foo(); } } } issue979_test_negated_is_best: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, evaluate: true, hoist_funs: true, if_return: true, join_vars: true, keep_fargs: true, loops: true, properties: true, sequences: true, side_effects: true, unused: true, } input: { function f3() { if (a == 1 | b == 2) { foo(); } } function f4() { if (!(a == 1 | b == 2)) { } else { foo(); } } function f5() { if (a == 1 && b == 2) { foo(); } } function f6() { if (!(a == 1 && b == 2)) { } else { foo(); } } function f7() { if (a == 1 || b == 2) { foo(); } else { return bar(); } } } expect: { function f3() { 1==a|2==b&&foo(); } function f4() { 1==a|2==b&&foo(); } function f5() { 1==a&&2==b&&foo(); } function f6() { 1!=a||2!=b||foo(); } function f7() { if(1!=a&&2!=b)return bar();foo() } } } UglifyJS2-3.6.3/test/compress/keep_fargs.js000066400000000000000000000572461355252637300206100ustar00rootroot00000000000000keep_fargs_false: { options = { keep_fargs: false, unused: true, } input: { console.log(function f(a) { return f.length; }(), function g(b) { return g; }().length); function h(c) { return h.length; } function i(d) { return i; } function j(e) {} console.log(h(), i().length, j.length); } expect: { console.log(function f() { return f.length; }(), function g() { return g; }().length); function h() { return h.length; } function i() { return i; } function j() {} console.log(h(), i().length, j.length); } } keep_fargs_strict: { options = { keep_fargs: "strict", unused: true, } input: { console.log(function f(a) { return f.length; }(), function g(b) { return g; }().length); function h(c) { return h.length; } function i(d) { return i; } function j(e) {} console.log(h(), i().length, j.length); } expect: { console.log(function f(a) { return f.length; }(), function g(b) { return g; }().length); function h(c) { return h.length; } function i(d) { return i; } function j(e) {} console.log(h(), i().length, j.length); } expect_stdout: [ "1 1", "1 1 1", ] } keep_fargs_true: { options = { keep_fargs: true, unused: true, } input: { console.log(function f(a) { return f.length; }(), function g(b) { return g; }().length); function h(c) { return h.length; } function i(d) { return i; } function j(e) {} console.log(h(), i().length, j.length); } expect: { console.log(function f(a) { return f.length; }(), function g(b) { return g; }().length); function h(c) { return h.length; } function i(d) { return i; } function j(e) {} console.log(h(), i().length, j.length); } expect_stdout: [ "1 1", "1 1 1", ] } replace_index: { options = { arguments: true, evaluate: true, keep_fargs: "strict", properties: true, } input: { var arguments = []; console.log(arguments[0]); (function() { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); (function(a, b) { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); (function(arguments) { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); (function() { var arguments; console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); } expect: { var arguments = []; console.log(arguments[0]); (function(argument_0, argument_1) { console.log(argument_1, argument_1, arguments.foo); })("bar", 42); (function(a, b) { console.log(b, b, arguments.foo); })("bar", 42); (function(arguments) { console.log(arguments[1], arguments[1], arguments.foo); })("bar", 42); (function() { var arguments; console.log(arguments[1], arguments[1], arguments.foo); })("bar", 42); } expect_stdout: [ "undefined", "42 42 undefined", "42 42 undefined", "a a undefined", "42 42 undefined", ] } replace_index_strict: { options = { arguments: true, evaluate: true, keep_fargs: "strict", properties: true, reduce_vars: true, } input: { "use strict"; (function() { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); (function(a, b) { console.log(arguments[1], arguments["1"], arguments["foo"]); })("bar", 42); } expect: { "use strict"; (function(argument_0, argument_1) { console.log(argument_1, argument_1, arguments.foo); })("bar", 42); (function(a, b) { console.log(b, b, arguments.foo); })("bar", 42); } expect_stdout: [ "42 42 undefined", "42 42 undefined", ] } issue_1858: { options = { collapse_vars: true, keep_fargs: "strict", pure_getters: true, unused: true, } input: { console.log(function(x) { var a = {}, b = a.b = x; return a.b + b; }(1)); } expect: { console.log(function() { var a = {}, b = a.b = 1; return a.b + b; }()); } expect_stdout: "2" } issue_2187_2: { options = { collapse_vars: true, keep_fargs: "strict", unused: true, } input: { var b = 1; console.log(function(a) { return a && ++b; }(b--)); } expect: { var b = 1; console.log(function() { return b-- && ++b; }()); } expect_stdout: "1" } issue_2203_2: { options = { collapse_vars: true, keep_fargs: "strict", unused: true, } input: { a = "PASS"; console.log({ a: "FAIL", b: function() { return function(c) { return c.a; }((String, (Object, function() { return this; }()))); } }.b()); } expect: { a = "PASS"; console.log({ a: "FAIL", b: function() { return function() { return (String, (Object, function() { return this; }())).a; }(); } }.b()); } expect_stdout: "PASS" } issue_2298: { options = { collapse_vars: true, keep_fargs: "strict", passes: 2, reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { function f() { var a = undefined; var undefined = a++; try { !function g(b) { b[1] = "foo"; }(); console.log("FAIL"); } catch (e) { console.log("PASS"); } } f(); }(); } expect: { !function() { (function() { var a = undefined; var undefined = a++; try { !function() { (void 0)[1] = "foo"; }(); console.log("FAIL"); } catch (e) { console.log("PASS"); } })(); }(); } expect_stdout: "PASS" } issue_2319_1: { options = { collapse_vars: true, keep_fargs: "strict", unused: true, } input: { console.log(function(a) { return a; }(!function() { return this; }())); } expect: { console.log(function() { return !function() { return this; }(); }()); } expect_stdout: "false" } issue_2319_2: { options = { collapse_vars: true, keep_fargs: "strict", 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, keep_fargs: "strict", unused: true, } input: { "use strict"; console.log(function(a) { return a; }(!function() { return this; }())); } expect: { "use strict"; console.log(function() { return !function() { return this; }(); }()); } expect_stdout: "true" } issue_2425_1: { options = { collapse_vars: true, keep_fargs: "strict", 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, keep_fargs: "strict", unused: true, } input: { var a = 8; (function(b, c) { 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_3: { options = { collapse_vars: true, keep_fargs: "strict", unused: true, } input: { var a = 8; (function(b, b) { b.toString(); })(--a, a |= 10); console.log(a); } expect: { var a = 8; (function() { (a |= 10).toString(); })(--a); console.log(a); } expect_stdout: "15" } issue_2436_13: { options = { collapse_vars: true, keep_fargs: "strict", passes: 2, reduce_vars: true, unused: true, } input: { var a = "PASS"; (function() { function f(b) { (function g(b) { var b = b && (b.null = "FAIL"); })(a); } f(); })(); console.log(a); } expect: { var a = "PASS"; (function() { (function() { (function() { a && (a.null = "FAIL"); })(); })(); })(); console.log(a); } expect_stdout: "PASS" } issue_2506: { options = { collapse_vars: true, keep_fargs: "strict", passes: 2, reduce_vars: true, unused: true, } input: { var c = 0; function f0(bar) { function f1(Infinity_2) { function f13(NaN) { if (false <= NaN & this >> 1 >= 0) { c++; } } var b_2 = f13(NaN, c++); } var bar = f1(-3, -1); } f0(false); console.log(c); } expect: { var c = 0; function f0(bar) { (function() { (function() { if (false <= 0/0 & this >> 1 >= 0) c++; })(c++); })(); } f0(false); console.log(c); } expect_stdout: "1" } issue_2226_1: { options = { keep_fargs: "strict", side_effects: true, unused: true, } input: { function f1() { var a = b; a += c; } function f2(a) { a <<= b; } function f3(a) { --a; } function f4() { var a = b; return a *= c; } function f5(a) { x(a /= b); } } expect: { function f1() { b; c; } function f2(a) { b; } function f3(a) { 0; } function f4() { var a = b; return a *= c; } function f5(a) { x(a /= b); } } } issue_2226_2: { options = { collapse_vars: true, keep_fargs: "strict", sequences: true, side_effects: true, unused: true, } input: { console.log(function(a, b) { a += b; return a; }(1, 2)); } expect: { console.log(function(a) { return a += 2; }(1)); } expect_stdout: "3" } issue_2226_3: { options = { collapse_vars: true, keep_fargs: "strict", side_effects: true, unused: true, } input: { console.log(function(a, b) { a += b; return a; }(1, 2)); } expect: { console.log(function(a) { return a += 2; }(1)); } expect_stdout: "3" } issue_3192: { options = { keep_fargs: "strict", unused: true, } input: { (function(a) { console.log(a = "foo", arguments[0]); })("bar"); (function(a) { "use strict"; console.log(a = "foo", arguments[0]); })("bar"); } expect: { (function(a) { console.log(a = "foo", arguments[0]); })("bar"); (function() { "use strict"; console.log("foo", arguments[0]); })("bar"); } expect_stdout: [ "foo foo", "foo bar", ] } if_increment: { options = { evaluate: true, keep_fargs: "strict", reduce_vars: true, unused: true, } input: { console.log(function(a) { if (console) return ++a; }(0)); } expect: { console.log(function() { if (console) return 1; }()); } expect_stdout: "1" } try_increment: { options = { evaluate: true, keep_fargs: "strict", reduce_vars: true, unused: true, } input: { console.log(function(a) { try { return ++a; } catch (e) {} }(0)); } expect: { console.log(function() { try { return 1; } catch (e) {} }()); } expect_stdout: "1" } issue_2630_3: { options = { inline: true, keep_fargs: "strict", reduce_vars: true, unused: true, } input: { var x = 2, a = 1; (function() { function f1(a) { f2(); --x >= 0 && f1({}); } f1(a++); function f2() { a++; } })(); console.log(a); } expect: { var x = 2, a = 1; (function() { (function f1() { f2(); --x >= 0 && f1({}); })(a++); function f2() { a++; } })(); console.log(a); } expect_stdout: "5" } issue_3364: { options = { functions: true, keep_fargs: "strict", reduce_vars: true, toplevel: true, unused: true, } mangle = {} input: { var s = 2, a = 100, b = 10, c = 0; function f(p, e, r) { try { for (var i = 1; i-- > 0;) var a = function(x) { function g(y) { y && y[a++]; } var x = g(--s >= 0 && f(c++)); for (var j = 1; --j > 0;); }(); } catch (e) { try { return; } catch (z) { for (var k = 1; --k > 0;) { for (var l = 1; l > 0; --l) { var n = function() {}; for (var k in n) var o = (n, k); } } } } } var r = f(); console.log(c); } expect: { var s = 2, c = 0; (function o() { try { for (var r = 1; r-- > 0;) var n = function() { (function(r) { r && r[n++]; })(--s >= 0 && o(c++)); for (var r = 1; --r > 0;); }(); } catch (r) { try { return; } catch (r) { for (var a = 1; --a > 0;) for (var f = 1; f > 0; --f) { function t() {} for (var a in t); } } } })(); console.log(c); } expect_stdout: "2" } defun_label: { options = { keep_fargs: "strict", passes: 2, reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { function f(a) { L: { if (a) break L; return 1; } } console.log(f(2)); }(); } expect: { !function() { console.log(function() { L: { if (2) break L; return 1; } }()); }(); } expect_stdout: true } iife_func_side_effects: { options = { keep_fargs: "strict", reduce_funcs: true, reduce_vars: true, unused: true, } input: { function x() { console.log("x"); } function y() { console.log("y"); } function z() { console.log("z"); } (function(a, b, c) { function y() { console.log("FAIL"); } return y + b(); })(x(), function() { return y(); }, z()); } expect: { function x() { console.log("x"); } function y() { console.log("y"); } function z() { console.log("z"); } (function(a, b) { return function() { console.log("FAIL"); } + b(); })(x(), function() { return y(); }, z()); } expect_stdout: [ "x", "z", "y", ] } issue_1595_1: { options = { evaluate: true, keep_fargs: "strict", reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function f(a) { return f(a + 1); })(2); } expect: { (function f(a) { return f(a + 1); })(2); } } issue_1595_2: { options = { evaluate: true, keep_fargs: "strict", reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function f(a) { return g(a + 1); })(2); } expect: { (function(a) { return g(a + 1); })(2); } } issue_1595_3: { options = { evaluate: true, keep_fargs: "strict", passes: 2, reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function f(a) { return g(a + 1); })(2); } expect: { (function() { return g(3); })(); } } issue_1595_4: { options = { evaluate: true, keep_fargs: "strict", reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function iife(a, b, c) { console.log(a, b, c); if (a) iife(a - 1, b, c); })(3, 4, 5); } expect: { (function iife(a, b, c) { console.log(a, b, c); if (a) iife(a - 1, b, c); })(3, 4, 5); } expect_stdout: true } duplicate_lambda_defun_name_1: { options = { keep_fargs: "strict", reduce_vars: true, } input: { console.log(function f(a) { function f() {} return f.length; }()); } expect: { console.log(function f(a) { function f() {} return f.length; }()); } expect_stdout: "0" } duplicate_lambda_defun_name_2: { options = { keep_fargs: "strict", passes: 2, reduce_vars: true, unused: true, } input: { console.log(function f(a) { function f() {} return f.length; }()); } expect: { console.log(function() { return function() {}.length; }()); } expect_stdout: "0" } function_name_mangle: { options = { keep_fargs: "strict", keep_fnames: true, reduce_vars: true, unused: true, } mangle = {} input: { (function() { function foo(bar) {} console.log(typeof foo); })(); } expect_exact: "(function(){console.log(typeof function o(){})})();" expect_stdout: "function" } function_name_mangle_ie8: { options = { keep_fargs: "strict", keep_fnames: true, reduce_vars: true, unused: true, } mangle = { ie8: true, toplevel: true, } input: { (function() { function foo(bar) {} console.log(typeof foo); })(); } expect_exact: "(function(){console.log(typeof function o(){})})();" expect_stdout: "function" } issue_3420_1: { options = { keep_fargs: "strict", unused: true, } input: { console.log(function() { return function(a, b, c, d) { return a + b; }; }().length); } expect: { console.log(function() { return function(a, b, c, d) { return a + b; }; }().length); } expect_stdout: "4" } issue_3420_2: { options = { inline: true, keep_fargs: "strict", unused: true, } input: { console.log(function() { return function(a, b, c, d) { return a + b; }; }().length); } expect: { console.log(function(a, b, c, d) { return a + b; }.length); } expect_stdout: "4" } issue_3420_3: { options = { inline: true, keep_fargs: "strict", reduce_vars: true, unused: true, } input: { console.log(function() { function f(a, b, c, d) { return a + b; } return f; }().length); } expect: { console.log(function(a, b, c, d) { return a + b; }.length); } expect_stdout: "4" } issue_3423_1: { options = { keep_fargs: "strict", unused: true, } input: { function f(g) { console.log(g.length); } f(function(a) {}); } expect: { function f(g) { console.log(g.length); } f(function(a) {}); } expect_stdout: "1" } issue_3423_2: { options = { keep_fargs: "strict", unused: true, } input: { new function(a) { console.log(this.constructor.length); }(); } expect: { new function(a) { console.log(this.constructor.length); }(); } expect_stdout: "1" } UglifyJS2-3.6.3/test/compress/labels.js000066400000000000000000000065441355252637300177370ustar00rootroot00000000000000labels_1: { options = { conditionals: true, dead_code: true, if_return: true, } input: { out: { if (foo) break out; console.log("bar"); } }; expect: { foo || console.log("bar"); } expect_stdout: true } labels_2: { options = { conditionals: true, dead_code: true, if_return: true, } input: { out: { if (foo) print("stuff"); else break out; console.log("here"); } }; expect: { if (foo) { print("stuff"); console.log("here"); } } } labels_3: { options = { conditionals: true, dead_code: true, if_return: true, } input: { for (var i = 0; i < 5; ++i) { if (i < 3) continue; console.log(i); } }; expect: { for (var i = 0; i < 5; ++i) i < 3 || console.log(i); } expect_stdout: true } labels_4: { options = { conditionals: true, dead_code: true, if_return: true, } input: { out: for (var i = 0; i < 5; ++i) { if (i < 3) continue out; console.log(i); } }; expect: { for (var i = 0; i < 5; ++i) i < 3 || console.log(i); } expect_stdout: true } labels_5: { options = { conditionals: true, dead_code: true, if_return: true, } // should keep the break-s in the following input: { while (foo) { if (bar) break; console.log("foo"); } out: while (foo) { if (bar) break out; console.log("foo"); } }; expect: { while (foo) { if (bar) break; console.log("foo"); } out: while (foo) { if (bar) break out; console.log("foo"); } } } labels_6: { input: { out: break out; }; expect: {} } labels_7: { options = { conditionals: true, dead_code: true, if_return: true, } input: { while (foo) { x(); y(); continue; } }; expect: { while (foo) { x(); y(); } } } labels_8: { options = { conditionals: true, dead_code: true, if_return: true, } input: { while (foo) { x(); y(); break; } }; expect: { while (foo) { x(); y(); break; } } } labels_9: { options = { conditionals: true, dead_code: true, if_return: true, } input: { out: while (foo) { x(); y(); continue out; z(); k(); } }; expect: { while (foo) { x(); y(); } } } labels_10: { options = { conditionals: true, dead_code: true, if_return: true, } input: { out: while (foo) { x(); y(); break out; z(); k(); } }; expect: { out: while (foo) { x(); y(); break out; } } } UglifyJS2-3.6.3/test/compress/loops.js000066400000000000000000000277531355252637300176360ustar00rootroot00000000000000while_becomes_for: { options = { loops: true, } input: { while (foo()) bar(); } expect: { for (; foo(); ) bar(); } } drop_if_break_1: { options = { loops: true, } input: { for (;;) if (foo()) break; } expect: { for (; !foo();); } } drop_if_break_2: { options = { loops: true, } input: { for (;bar();) if (foo()) break; } expect: { for (; bar() && !foo();); } } drop_if_break_3: { options = { loops: true, } input: { for (;bar();) { if (foo()) break; stuff1(); stuff2(); } } expect: { for (; bar() && !foo();) { stuff1(); stuff2(); } } } drop_if_break_4: { options = { loops: true, sequences: true, } input: { for (;bar();) { x(); y(); if (foo()) break; z(); k(); } } expect: { for (; bar() && (x(), y(), !foo());) z(), k(); } } drop_if_else_break_1: { options = { loops: true, } input: { for (;;) if (foo()) bar(); else break; } expect: { for (; foo(); ) bar(); } } drop_if_else_break_2: { options = { loops: true, } input: { for (;bar();) { if (foo()) baz(); else break; } } expect: { for (; bar() && foo();) baz(); } } drop_if_else_break_3: { options = { loops: true, } input: { for (;bar();) { if (foo()) baz(); else break; stuff1(); stuff2(); } } expect: { for (; bar() && foo();) { baz(); stuff1(); stuff2(); } } } drop_if_else_break_4: { options = { loops: true, sequences: true, } input: { for (;bar();) { x(); y(); if (foo()) baz(); else break; z(); k(); } } expect: { for (; bar() && (x(), y(), foo());) baz(), z(), k(); } } parse_do_while_with_semicolon: { options = { loops: false, } input: { do { x(); } while (false);y() } expect: { do x(); while (false);y(); } } parse_do_while_without_semicolon: { options = { loops: false, } input: { do { x(); } while (false)y() } expect: { do x(); while (false);y(); } } evaluate: { options = { dead_code: true, evaluate: true, loops: true, passes: 2, side_effects: true, } input: { while (true) { a(); } while (false) { b(); } do { c(); } while (true); do { d(); } while (false); } expect: { for(;;) a(); for(;;) c(); d(); } } issue_1532: { options = { evaluate: true, loops: true, } input: { function f(x, y) { do { if (x) break; foo(); } while (false); } } expect: { function f(x, y) { do { if (x) break; foo(); } while (false); } } } issue_186: { beautify = { beautify: false, ie8: false, } input: { var x = 3; if (foo()) do do alert(x); while (--x); while (x); else bar(); } expect_exact: 'var x=3;if(foo())do{do{alert(x)}while(--x)}while(x);else bar();' } issue_186_ie8: { beautify = { beautify: false, ie8: true, } input: { var x = 3; if (foo()) do do alert(x); while (--x); while (x); else bar(); } expect_exact: 'var x=3;if(foo()){do{do{alert(x)}while(--x)}while(x)}else bar();' } issue_186_beautify: { beautify = { beautify: true, ie8: false, } input: { var x = 3; if (foo()) do do alert(x); while (--x); while (x); else bar(); } expect_exact: [ 'var x = 3;', '', 'if (foo()) do {', ' do {', ' alert(x);', ' } while (--x);', '} while (x); else bar();', ] } issue_186_beautify_ie8: { beautify = { beautify: true, ie8: true, } input: { var x = 3; if (foo()) do do alert(x); while (--x); while (x); else bar(); } expect_exact: [ 'var x = 3;', '', 'if (foo()) {', ' do {', ' do {', ' alert(x);', ' } while (--x);', ' } while (x);', '} else bar();', ] } issue_186_braces: { beautify = { beautify: false, braces: true, ie8: false, } input: { var x = 3; if (foo()) do do alert(x); while (--x); while (x); else bar(); } expect_exact: 'var x=3;if(foo()){do{do{alert(x)}while(--x)}while(x)}else{bar()}' } issue_186_braces_ie8: { beautify = { beautify: false, braces: true, ie8: true, } input: { var x = 3; if (foo()) do do alert(x); while (--x); while (x); else bar(); } expect_exact: 'var x=3;if(foo()){do{do{alert(x)}while(--x)}while(x)}else{bar()}' } issue_186_beautify_braces: { beautify = { beautify: true, braces: true, ie8: false, } input: { var x = 3; if (foo()) do do alert(x); while (--x); while (x); else bar(); } expect_exact: [ 'var x = 3;', '', 'if (foo()) {', ' do {', ' do {', ' alert(x);', ' } while (--x);', ' } while (x);', '} else {', ' bar();', '}', ] } issue_186_beautify_braces_ie8: { beautify = { beautify: true, braces: true, ie8: true, } input: { var x = 3; if (foo()) do do alert(x); while (--x); while (x); else bar(); } expect_exact: [ 'var x = 3;', '', 'if (foo()) {', ' do {', ' do {', ' alert(x);', ' } while (--x);', ' } while (x);', '} else {', ' bar();', '}', ] } issue_1648: { options = { join_vars: true, loops: true, passes: 2, sequences: true, unused: true, } input: { function f() { x(); var b = 1; while (1); } } expect_exact: "function f(){for(x();1;);}" } do_switch: { options = { evaluate: true, loops: true, } input: { do { switch (a) { case b: continue; } } while (false); } expect: { do { switch (a) { case b: continue; } } while (false); } } in_parenthesis_1: { input: { for (("foo" in {});0;); } expect_exact: 'for(("foo"in{});0;);' } in_parenthesis_2: { input: { for ((function(){ "foo" in {}; });0;); } expect_exact: 'for(function(){"foo"in{}};0;);' } init_side_effects: { options = { loops: true, side_effects: true, } input: { for (function() {}(), i = 0; i < 5; i++) console.log(i); for (function() {}(); i < 10; i++) console.log(i); } expect: { for (i = 0; i < 5; i++) console.log(i); for (; i < 10; i++) console.log(i); } expect_stdout: true } dead_code_condition: { options = { dead_code: true, evaluate: true, loops: true, sequences: true, } input: { for (var a = 0, b = 5; (a += 1, 3) - 3 && b > 0; b--) { var c = function() { b--; }(a++); } console.log(a); } expect: { var c; var a = 0, b = 5; a += 1, 0, console.log(a); } expect_stdout: "1" } issue_2740_1: { options = { dead_code: true, loops: true, } input: { for (; ; ) break; for (a(); ; ) break; for (; b(); ) break; for (c(); d(); ) break; for (; ; e()) break; for (f(); ; g()) break; for (; h(); i()) break; for (j(); k(); l()) break; } expect: { a(); b(); c(); d(); f(); h(); j(); k(); } } issue_2740_2: { options = { dead_code: true, loops: true, passes: 2, } input: { L1: while (x()) { break L1; } } expect: { x(); } } issue_2740_3: { options = { dead_code: true, loops: true, } input: { L1: for (var x = 0; x < 3; x++) { L2: for (var y = 0; y < 2; y++) { break L1; } } console.log(x, y); } expect: { L1: for (var x = 0; x < 3; x++) for (var y = 0; y < 2; y++) break L1; console.log(x, y); } expect_stdout: "0 0" } issue_2740_4: { options = { dead_code: true, loops: true, passes: 2, } input: { L1: for (var x = 0; x < 3; x++) { L2: for (var y = 0; y < 2; y++) { break L2; } } console.log(x, y); } expect: { for (var x = 0; x < 3; x++) { var y = 0; y < 2; } console.log(x, y); } expect_stdout: "3 0" } issue_2740_5: { options = { dead_code: true, loops: true, passes: 2, } input: { L1: for (var x = 0; x < 3; x++) { break L1; L2: for (var y = 0; y < 2; y++) { break L2; } } console.log(x, y); } expect: { var x = 0; x < 3; var y; console.log(x,y); } expect_stdout: "0 undefined" } issue_2904: { options = { join_vars: true, loops: true, } input: { var a = 1; do { console.log(a); } while (--a); } expect: { for (var a = 1; console.log(a), --a;); } expect_stdout: "1" } issue_3371: { options = { functions: true, join_vars: true, loops: true, reduce_vars: true, unused: true, } input: { (function() { var a = function() { console.log("PASS"); }; while (a()); })(); } expect: { (function() { function a() { console.log("PASS"); } for (; a(); ); })(); } expect_stdout: "PASS" } step: { options = { loops: true, side_effects: true, } input: { for (var i = 0; i < 42; "foo", i++, "bar"); console.log(i); } expect: { for (var i = 0; i < 42; i++); console.log(i); } expect_stdout: "42" } UglifyJS2-3.6.3/test/compress/max_line_len.js000066400000000000000000000016211355252637300211160ustar00rootroot00000000000000too_short: { beautify = { max_line_len: 10, } input: { function f(a) { return { c: 42, d: a(), e: "foo"}; } } expect_exact: [ "function f(", "a){return{", "c:42,d:a(", '),e:"foo"}', "}", ] expect_warnings: [ "WARN: Output exceeds 10 characters", ] } just_enough: { beautify = { max_line_len: 14, } input: { function f(a) { return { c: 42, d: a(), e: "foo"}; } } expect_exact: [ "function f(a){", "return{c:42,", 'd:a(),e:"foo"}', "}", ] expect_warnings: [] } issue_304: { beautify = { max_line_len: 10, } input: { var a = 0, b = 0, c = 0, d = 0, e = 0; } expect_exact: [ "var a=0,", "b=0,c=0,", "d=0,e=0;", ] expect_warnings: [] } UglifyJS2-3.6.3/test/compress/negate-iife.js000066400000000000000000000210241355252637300206400ustar00rootroot00000000000000negate_iife_1: { options = { negate_iife: true, } input: { (function(){ stuff() })(); } expect: { !function(){ stuff() }(); } } negate_iife_1_off: { options = { negate_iife: false, } input: { (function(){ stuff() })(); } expect_exact: '(function(){stuff()})();' } negate_iife_2: { options = { inline: true, negate_iife: true, } input: { (function(){ return {} })().x = 10; } expect_exact: "({}).x=10;" } negate_iife_2_side_effects: { options = { inline: true, negate_iife: true, side_effects: true, } input: { (function(){ return {} })().x = 10; } expect_exact: "({}).x=10;" } negate_iife_3: { options = { conditionals: true, negate_iife: true, } input: { (function(){ return t })() ? console.log(true) : console.log(false); } expect: { !function(){ return t }() ? console.log(false) : console.log(true); } } negate_iife_3_evaluate: { options = { conditionals: true, evaluate: true, inline: true, negate_iife: true, } input: { (function(){ return true })() ? console.log(true) : console.log(false); } expect: { true, console.log(true); } expect_stdout: true } negate_iife_3_side_effects: { options = { conditionals: true, negate_iife: true, side_effects: true, } input: { (function(){ return t })() ? console.log(true) : console.log(false); } expect: { !function(){ return t }() ? console.log(false) : console.log(true); } } negate_iife_3_off: { options = { conditionals: true, negate_iife: false, } input: { (function(){ return t })() ? console.log(true) : console.log(false); } expect: { !function(){ return t }() ? console.log(false) : console.log(true); } } negate_iife_3_off_evaluate: { options = { conditionals: true, evaluate: true, inline: true, negate_iife: false, } input: { (function(){ return true })() ? console.log(true) : console.log(false); } expect: { true, console.log(true); } expect_stdout: true } negate_iife_4: { options = { conditionals: true, negate_iife: true, sequences: true, } input: { (function(){ return t })() ? console.log(true) : console.log(false); (function(){ console.log("something"); })(); } expect: { !function(){ return t }() ? console.log(false) : console.log(true), function(){ console.log("something"); }(); } } sequence_off: { options = { conditionals: true, negate_iife: false, passes: 2, sequences: true, } input: { function f() { (function(){ return t })() ? console.log(true) : console.log(false); (function(){ console.log("something"); })(); } function g() { (function(){ console.log("something"); })(); (function(){ return t })() ? console.log(true) : console.log(false); } } expect: { function f() { !function(){ return t }() ? console.log(false) : console.log(true), function(){ console.log("something"); }(); } function g() { (function(){ console.log("something"); })(), function(){ return t }() ? console.log(true) : console.log(false); } } } negate_iife_5: { options = { conditionals: true, negate_iife: true, sequences: true, } input: { if ((function(){ return t })()) { foo(true); } else { bar(false); } (function(){ console.log("something"); })(); } expect: { !function(){ return t }() ? bar(false) : foo(true), function(){ console.log("something"); }(); } } negate_iife_5_off: { options = { conditionals: true, negate_iife: false, sequences: true, } input: { if ((function(){ return t })()) { foo(true); } else { bar(false); } (function(){ console.log("something"); })(); } expect: { !function(){ return t }() ? bar(false) : foo(true), function(){ console.log("something"); }(); } } negate_iife_nested: { options = { conditionals: true, negate_iife: true, sequences: true, } input: { function Foo(f) { this.f = f; } new Foo(function() { (function(x) { (function(y) { console.log(y); })(x); })(7); }).f(); } expect: { function Foo(f) { this.f = f; } new Foo(function() { !function(x) { !function(y) { console.log(y); }(x); }(7); }).f(); } expect_stdout: true } negate_iife_nested_off: { options = { conditionals: true, negate_iife: false, sequences: true, } input: { function Foo(f) { this.f = f; } new Foo(function() { (function(x) { (function(y) { console.log(y); })(x); })(7); }).f(); } expect: { function Foo(f) { this.f = f; } new Foo(function() { (function(x) { (function(y) { console.log(y); })(x); })(7); }).f(); } expect_stdout: true } negate_iife_issue_1073: { options = { conditionals: true, negate_iife: true, sequences: true, } input: { new (function(a) { return function Foo() { this.x = a; console.log(this); }; }(7))(); } expect: { new (function(a) { return function Foo() { this.x = a, console.log(this); }; }(7))(); } expect_stdout: true } issue_1254_negate_iife_false: { options = { negate_iife: false, } input: { (function() { return function() { console.log('test') }; })()(); } expect_exact: '(function(){return function(){console.log("test")}})()();' expect_stdout: true } issue_1254_negate_iife_true: { options = { negate_iife: true, } input: { (function() { return function() { console.log('test') }; })()(); } expect_exact: '!function(){return function(){console.log("test")}}()();' expect_stdout: true } issue_1254_negate_iife_nested: { options = { negate_iife: true, } input: { (function() { return function() { console.log('test') }; })()()()()(); } expect_exact: '!function(){return function(){console.log("test")}}()()()()();' expect_stdout: true } issue_1288: { options = { conditionals: true, negate_iife: true, side_effects: false, } input: { if (w) ; else { (function f() {})(); } if (!x) { (function() { x = {}; })(); } if (y) (function() {})(); else (function(z) { return z; })(0); } expect: { w || !function f() {}(); x || !function() { x = {}; }(); y ? !function() {}() : !function(z) { return z; }(0); } } issue_1288_side_effects: { options = { conditionals: true, negate_iife: true, side_effects: true, } input: { if (w) ; else { (function f() {})(); } if (!x) { (function() { x = {}; })(); } if (y) (function() {})(); else (function(z) { return z; })(0); } expect: { w; x || function() { x = {}; }(); y; } } UglifyJS2-3.6.3/test/compress/new.js000066400000000000000000000057211355252637300172620ustar00rootroot00000000000000new_statement: { input: { new x(1); new x(1)(2); new x(1)(2)(3); new new x(1); new new x(1)(2); new (new x(1))(2); (new new x(1))(2); } expect_exact: "new x(1);new x(1)(2);new x(1)(2)(3);new new x(1);new new x(1)(2);new new x(1)(2);(new new x(1))(2);" } new_statements_2: { input: { new x; new new x; new new new x; new true; new (0); new (!0); new (bar = function(foo) {this.foo=foo;})(123); new (bar = function(foo) {this.foo=foo;})(); } expect_exact: "new x;new(new x);new(new(new x));new true;new 0;new(!0);new(bar=function(foo){this.foo=foo})(123);new(bar=function(foo){this.foo=foo});" } new_statements_3: { input: { new (function(foo){this.foo=foo;})(1); new (function(foo){this.foo=foo;})(); new (function test(foo){this.foo=foo;})(1); new (function test(foo){this.foo=foo;})(); } expect_exact: "new function(foo){this.foo=foo}(1);new function(foo){this.foo=foo};new function test(foo){this.foo=foo}(1);new function test(foo){this.foo=foo};" } new_with_rewritten_true_value: { options = { booleans: true, } input: { new true; } expect_exact: "new(!0);" } new_with_many_parameters: { input: { new foo.bar("baz"); new x(/123/, 456); } expect_exact: 'new foo.bar("baz");new x(/123/,456);' } new_constructor_with_unary_arguments: { input: { new x(); new x(-1); new x(-1, -2); new x(void 1, +2, -3, ~4, !5, --a, ++b, c--, d++, typeof e, delete f); new (-1); // should parse despite being invalid at runtime. new (-1)(); // should parse despite being invalid at runtime. new (-1)(-2); // should parse despite being invalid at runtime. } expect_exact: "new x;new x(-1);new x(-1,-2);new x(void 1,+2,-3,~4,!5,--a,++b,c--,d++,typeof e,delete f);new(-1);new(-1);new(-1)(-2);" } call_with_unary_arguments: { input: { x(); x(-1); x(-1, -2); x(void 1, +2, -3, ~4, !5, --a, ++b, c--, d++, typeof e, delete f); (-1)(); // should parse despite being invalid at runtime. (-1)(-2); // should parse despite being invalid at runtime. } expect_exact: "x();x(-1);x(-1,-2);x(void 1,+2,-3,~4,!5,--a,++b,c--,d++,typeof e,delete f);(-1)();(-1)(-2);" } new_with_unary_prefix: { input: { var bar = (+new Date()).toString(32); } expect_exact: 'var bar=(+new Date).toString(32);'; } dot_parenthesis_1: { input: { console.log(new (Math.random().constructor) instanceof Number); } expect_exact: "console.log(new(Math.random().constructor)instanceof Number);" expect_stdout: "true" } dot_parenthesis_2: { input: { console.log(typeof new function(){Math.random()}.constructor); } expect_exact: "console.log(typeof new function(){Math.random()}.constructor);" expect_stdout: "function" } UglifyJS2-3.6.3/test/compress/node_version.js000066400000000000000000000013301355252637300211530ustar00rootroot00000000000000eval_let_6: { input: { eval("let a;"); console.log(); } expect: { eval("let a;"); console.log(); } expect_stdout: "" node_version: ">=6" } eval_let_4: { input: { eval("let a;"); console.log(); } expect: { eval("let a;"); console.log(); } expect_stdout: SyntaxError("Block-scoped declarations (let, const, function, class) not yet supported outside strict mode") node_version: "4" } eval_let_0: { input: { eval("let a;"); console.log(); } expect: { eval("let a;"); console.log(); } expect_stdout: SyntaxError("Unexpected identifier") node_version: "<=0.12" } UglifyJS2-3.6.3/test/compress/numbers.js000066400000000000000000000110751355252637300201430ustar00rootroot00000000000000hex_numbers_in_parentheses_for_prototype_functions: { beautify = { beautify: true, } input: { function f() { (-2); (-2).toFixed(0); (2); (2).toFixed(0); (0.2); (0.2).toFixed(0); (2.34e20); (2.34e20).toFixed(0); (0.00000002); (0.00000002).toFixed(0); (1000000000000000128); (1000000000000000128).toFixed(0); (-1000000000000000128); (-1000000000000000128).toFixed(0); } } expect_exact: [ "function f() {", " -2;", " (-2).toFixed(0);", " 2;", " 2..toFixed(0);", " .2;", " .2.toFixed(0);", " 234e18;", " 234e18.toFixed(0);", " 2e-8;", " 2e-8.toFixed(0);", " 0xde0b6b3a7640080;", " (0xde0b6b3a7640080).toFixed(0);", " -0xde0b6b3a7640080;", " (-0xde0b6b3a7640080).toFixed(0);", "}", ] } comparisons: { options = { comparisons: true, } input: { console.log( ~x === 42, x % n === 42 ); } expect: { console.log( 42 == ~x, x % n == 42 ); } } evaluate_1: { options = { evaluate: true, unsafe_math: false, } input: { console.log( x + 1 + 2, x * 1 * 2, +x + 1 + 2, 1 + x + 2 + 3, 1 | x | 2 | 3, 1 + x-- + 2 + 3, 1 + (x*y + 2) + 3, 1 + (2 + x + 3), 1 + (2 + ~x + 3), -y + (2 + ~x + 3), 1 & (2 & x & 3), 1 + (2 + (x |= 0) + 3) ); } expect: { console.log( x + 1 + 2, 1 * x * 2, +x + 1 + 2, 1 + x + 2 + 3, 3 | x, 1 + x-- + 2 + 3, x*y + 2 + 1 + 3, 1 + (2 + x + 3), 2 + ~x + 3 + 1, -y + (2 + ~x + 3), 0 & x, 2 + (x |= 0) + 3 + 1 ); } } evaluate_2: { options = { evaluate: true, unsafe_math: true, } input: { console.log( x + 1 + 2, x * 1 * 2, +x + 1 + 2, 1 + x + 2 + 3, 1 | x | 2 | 3, 1 + x-- + 2 + 3, 1 + (x*y + 2) + 3, 1 + (2 + x + 3), 1 & (2 & x & 3), 1 + (2 + (x |= 0) + 3) ); } expect: { console.log( x + 1 + 2, 2 * x, 3 + +x, 1 + x + 2 + 3, 3 | x, 6 + x--, 6 + x*y, 1 + (2 + x + 3), 0 & x, 6 + (x |= 0) ); } } evaluate_3: { options = { evaluate: true, unsafe: true, unsafe_math: true, } input: { console.log(1 + Number(x) + 2); } expect: { console.log(3 + +x); } } evaluate_4: { options = { evaluate: true, } input: { console.log( 1+ +a, +a+1, 1+-a, -a+1, +a+ +b, +a+-b, -a+ +b, -a+-b ); } expect: { console.log( +a+1, +a+1, 1-a, 1-a, +a+ +b, +a-b, -a+ +b, -a-b ); } } issue_1710: { options = { evaluate: true, } input: { var x = {}; console.log((x += 1) + -x); } expect: { var x = {}; console.log((x += 1) + -x); } expect_stdout: true } unary_binary_parenthesis: { input: { var v = [ 0, 1, NaN, Infinity, null, undefined, true, false, "", "foo", /foo/ ]; v.forEach(function(x) { v.forEach(function(y) { console.log( +(x*y), +(x/y), +(x%y), -(x*y), -(x/y), -(x%y) ); }); }); } expect: { var v = [ 0, 1, NaN, 1/0, null, void 0, true, false, "", "foo", /foo/ ]; v.forEach(function(x) { v.forEach(function(y) { console.log( +x*y, +x/y, +x%y, -x*y, -x/y, -x%y ); }); }); } expect_stdout: true } UglifyJS2-3.6.3/test/compress/preserve_line.js000066400000000000000000000072111355252637300213270ustar00rootroot00000000000000return_1: { beautify = { beautify: false, preserve_line: true, } input: { console.log(function f() { return ( f.toString() != 42 ); }()); } expect_exact: [ "console.log(function f(){", "", "return 42!=f.toString()}());", ] expect_stdout: "true" } return_2: { beautify = { beautify: true, preserve_line: true, } input: { console.log(function f() { return ( f.toString() != 42 ); }()); } expect_exact: [ "console.log(function f() {", "", " return 42 != f.toString();", "}());", ] expect_stdout: "true" } return_3: { options = {} beautify = { beautify: false, preserve_line: true, } input: { console.log(function f() { return ( f.toString() != 42 ); }()); } expect_exact: [ "console.log(function f(){", "", "return 42!=f.toString()}());", ] expect_stdout: "true" } return_4: { options = {} beautify = { beautify: true, preserve_line: true, } input: { console.log(function f() { return ( f.toString() != 42 ); }()); } expect_exact: [ "console.log(function f() {", "", " return 42 != f.toString();", "}());", ] expect_stdout: "true" } return_5: { beautify = { beautify: false, preserve_line: true, } input: { _is_selected = function(tags, slug) { var ref; return (ref = _.find(tags, { slug: slug })) != null ? ref.selected : void 0; }; } expect_exact: [ "_is_selected=function(tags,slug){", "var ref", "", "", ";return null!=(ref=_.find(tags,{slug:slug}))?ref.selected:void 0};", ] } return_6: { beautify = { beautify: true, preserve_line: true, } input: { _is_selected = function(tags, slug) { var ref; return (ref = _.find(tags, { slug: slug })) != null ? ref.selected : void 0; }; } expect_exact: [ "_is_selected = function(tags, slug) {", " var ref;", "", "", " return null != (ref = _.find(tags, {", " slug: slug", " })) ? ref.selected : void 0;", "};", ] } return_7: { options = {} mangle = {} beautify = { beautify: false, preserve_line: true, } input: { _is_selected = function(tags, slug) { var ref; return (ref = _.find(tags, { slug: slug })) != null ? ref.selected : void 0; }; } expect_exact: [ "_is_selected=function(e,l){", "var n", "", "", ";return null!=(n=_.find(e,{slug:l}))?n.selected:void 0};", ] } return_8: { options = {} mangle = {} beautify = { beautify: true, preserve_line: true, } input: { _is_selected = function(tags, slug) { var ref; return (ref = _.find(tags, { slug: slug })) != null ? ref.selected : void 0; }; } expect_exact: [ "_is_selected = function(e, l) {", " var n;", "", "", " return null != (n = _.find(e, {", " slug: l", " })) ? n.selected : void 0;", "};", ] } UglifyJS2-3.6.3/test/compress/properties.js000066400000000000000000001433021355252637300206630ustar00rootroot00000000000000keep_properties: { options = { evaluate: true, properties: false, } input: { a["foo"] = "bar"; } expect: { a["foo"] = "bar"; } } dot_properties: { options = { evaluate: true, properties: true, } beautify = { ie8: true, } input: { a["foo"] = "bar"; a["if"] = "if"; a["*"] = "asterisk"; a["\u0EB3"] = "unicode"; a[""] = "whitespace"; a["1_1"] = "foo"; } expect: { a.foo = "bar"; a["if"] = "if"; a["*"] = "asterisk"; a["\u0EB3"] = "unicode"; a[""] = "whitespace"; a["1_1"] = "foo"; } } dot_properties_es5: { options = { evaluate: true, properties: true, } beautify = { ie8: false, } input: { a["foo"] = "bar"; a["if"] = "if"; a["*"] = "asterisk"; a["\u0EB3"] = "unicode"; a[""] = "whitespace"; } expect: { a.foo = "bar"; a.if = "if"; a["*"] = "asterisk"; a["\u0EB3"] = "unicode"; a[""] = "whitespace"; } } sub_properties: { options = { evaluate: true, properties: true, } input: { a[0] = 0; a["0"] = 1; a[3.14] = 2; a["3" + ".14"] = 3; a["i" + "f"] = 4; a["foo" + " bar"] = 5; a[0 / 0] = 6; a[null] = 7; a[undefined] = 8; } expect: { a[0] = 0; a[0] = 1; a[3.14] = 2; a[3.14] = 3; a.if = 4; a["foo bar"] = 5; a.NaN = 6; a.null = 7; a[void 0] = 8; } } evaluate_array_length: { options = { evaluate: true, properties: true, unsafe: true, } input: { a = [1, 2, 3].length; a = [1, 2, 3].join()["len" + "gth"]; a = [1, 2, b].length; a = [1, 2, 3].join(b).length; } expect: { a = 3; a = 5; a = [1, 2, b].length; a = [1, 2, 3].join(b).length; } } evaluate_string_length: { options = { evaluate: true, properties: true, unsafe: true, } input: { a = "foo".length; a = ("foo" + "bar")["len" + "gth"]; a = b.length; a = ("foo" + b).length; } expect: { a = 3; a = 6; a = b.length; a = ("foo" + b).length; } } mangle_properties: { mangle = { properties: { keep_quoted: false, }, } input: { a["foo"] = "bar"; a.color = "red"; x = {"bar": 10}; a.run(x.bar, a.foo); a['run']({color: "blue", foo: "baz"}); } expect: { a["a"] = "bar"; a.b = "red"; x = {o: 10}; a.r(x.o, a.a); a['r']({b: "blue", a: "baz"}); } } mangle_unquoted_properties: { options = { evaluate: true, properties: false, } mangle = { properties: { keep_quoted: true, }, } beautify = { beautify: false, quote_style: 3, keep_quoted_props: true, } input: { a.top = 1; function f1() { a["foo"] = "bar"; a.color = "red"; a.stuff = 2; x = {"bar": 10, size: 7}; a.size = 9; } function f2() { a.foo = "bar"; a['color'] = "red"; x = {bar: 10, size: 7}; a.size = 9; a.stuff = 3; } } expect: { a.a = 1; function f1() { a["foo"] = "bar"; a.color = "red"; a.r = 2; x = {"bar": 10, b: 7}; a.b = 9; } function f2() { a.foo = "bar"; a['color'] = "red"; x = {bar: 10, b: 7}; a.b = 9; a.r = 3; } } } mangle_debug: { mangle = { properties: { debug: "", }, } input: { a.foo = "bar"; x = { baz: "ban" }; } expect: { a._$foo$_ = "bar"; x = { _$baz$_: "ban" }; } } mangle_debug_true: { mangle = { properties: { debug: true, }, } input: { a.foo = "bar"; x = { baz: "ban" }; } expect: { a._$foo$_ = "bar"; x = { _$baz$_: "ban" }; } } mangle_debug_suffix: { mangle = { properties: { debug: "XYZ", }, } input: { a.foo = "bar"; x = { baz: "ban" }; } expect: { a._$foo$XYZ_ = "bar"; x = { _$baz$XYZ_: "ban" }; } } mangle_debug_suffix_keep_quoted: { options = { evaluate: true, properties: false, } mangle = { properties: { debug: "XYZ", keep_quoted: true, reserved: [], }, } beautify = { beautify: false, quote_style: 3, keep_quoted_props: true, } input: { a.top = 1; function f1() { a["foo"] = "bar"; a.color = "red"; a.stuff = 2; x = {"bar": 10, size: 7}; a.size = 9; } function f2() { a.foo = "bar"; a['color'] = "red"; x = {bar: 10, size: 7}; a.size = 9; a.stuff = 3; } } expect: { a._$top$XYZ_ = 1; function f1() { a["foo"] = "bar"; a.color = "red"; a._$stuff$XYZ_ = 2; x = {"bar": 10, _$size$XYZ_: 7}; a._$size$XYZ_ = 9; } function f2() { a.foo = "bar"; a['color'] = "red"; x = {bar: 10, _$size$XYZ_: 7}; a._$size$XYZ_ = 9; a._$stuff$XYZ_ = 3; } } } first_256_chars_as_properties: { beautify = { ascii_only: true, } input: { // Note: some of these unicode character keys are not visible on github.com var o = { "\0":0,"":1,"":2,"":3,"":4,"":5,"":6,"":7,"\b":8, "\t":9,"\n":10,"\v":11,"\f":12,"\r":13,"":14,"":15,"":16,"":17, "":18,"":19,"":20,"":21,"":22,"":23,"":24,"":25,"":26, "":27,"":28,"":29,"":30,"":31," ":32,"!":33,'"':34,"#":35, $:36,"%":37,"&":38,"'":39,"(":40,")":41,"*":42,"+":43,",":44, "-":45,".":46,"/":47,"0":48,"1":49,"2":50,"3":51,"4":52,"5":53,"6":54,"7":55, "8":56,"9":57,":":58,";":59,"<":60,"=":61,">":62,"?":63,"@":64,A:65, B:66,C:67,D:68,E:69,F:70,G:71,H:72,I:73,J:74,K:75,L:76,M:77,N:78, O:79,P:80,Q:81,R:82,S:83,T:84,U:85,V:86,W:87,X:88,Y:89,Z:90,"[":91, "\\":92,"]":93,"^":94,_:95,"`":96,a:97,b:98,c:99,d:100,e:101, f:102,g:103,h:104,i:105,j:106,k:107,l:108,m:109,n:110,o:111,p:112, q:113,r:114,s:115,t:116,u:117,v:118,w:119,x:120,y:121,z:122,"{":123, "|":124,"}":125,"~":126,"":127,"€":128,"":129,"‚":130,"ƒ":131, "„":132,"…":133,"†":134,"‡":135,"ˆ":136,"‰":137,"Š":138,"‹":139, "Œ":140,"":141,"Ž":142,"":143,"":144,"‘":145,"’":146,"“":147, "”":148,"•":149,"–":150,"—":151,"˜":152,"™":153,"š":154,"›":155, "œ":156,"":157,"ž":158,"Ÿ":159," ":160,"¡":161,"¢":162,"£":163, "¤":164,"¥":165,"¦":166,"§":167,"¨":168,"©":169,"ª":170,"«":171, "¬":172,"­":173,"®":174,"¯":175,"°":176,"±":177,"²":178,"³":179, "´":180,"µ":181,"¶":182,"·":183,"¸":184,"¹":185,"º":186,"»":187, "¼":188,"½":189,"¾":190,"¿":191,"À":192,"Á":193,"Â":194,"Ã":195, "Ä":196,"Å":197,"Æ":198,"Ç":199,"È":200,"É":201,"Ê":202,"Ë":203, "Ì":204,"Í":205,"Î":206,"Ï":207,"Ð":208,"Ñ":209,"Ò":210,"Ó":211, "Ô":212,"Õ":213,"Ö":214,"×":215,"Ø":216,"Ù":217,"Ú":218,"Û":219, "Ü":220,"Ý":221,"Þ":222,"ß":223,"à":224,"á":225,"â":226,"ã":227, "ä":228,"å":229,"æ":230,"ç":231,"è":232,"é":233,"ê":234,"ë":235, "ì":236,"í":237,"î":238,"ï":239,"ð":240,"ñ":241,"ò":242,"ó":243, "ô":244,"õ":245,"ö":246,"÷":247,"ø":248,"ù":249,"ú":250,"û":251, "ü":252,"ý":253,"þ":254,"ÿ":255 }; } expect: { var o = { "\0":0,"\x01":1,"\x02":2,"\x03":3,"\x04":4,"\x05":5,"\x06":6, "\x07":7,"\b":8,"\t":9,"\n":10,"\v":11,"\f":12,"\r":13,"\x0e":14, "\x0f":15,"\x10":16,"\x11":17,"\x12":18,"\x13":19,"\x14":20,"\x15":21, "\x16":22,"\x17":23,"\x18":24,"\x19":25,"\x1a":26,"\x1b":27,"\x1c":28, "\x1d":29,"\x1e":30,"\x1f":31," ":32,"!":33,'"':34,"#":35,$:36, "%":37,"&":38,"'":39,"(":40,")":41,"*":42,"+":43,",":44,"-":45, ".":46,"/":47,0:48,1:49,2:50,3:51,4:52,5:53,6:54,7:55,8:56,9:57, ":":58,";":59,"<":60,"=":61,">":62,"?":63,"@":64,A:65,B:66,C:67, D:68,E:69,F:70,G:71,H:72,I:73,J:74,K:75,L:76,M:77,N:78,O:79,P:80, Q:81,R:82,S:83,T:84,U:85,V:86,W:87,X:88,Y:89,Z:90,"[":91,"\\":92, "]":93,"^":94,_:95,"`":96,a:97,b:98,c:99,d:100,e:101,f:102,g:103, h:104,i:105,j:106,k:107,l:108,m:109,n:110,o:111,p:112,q:113,r:114, s:115,t:116,u:117,v:118,w:119,x:120,y:121,z:122,"{":123,"|":124, "}":125,"~":126,"\x7f":127,"\x80":128,"\x81":129,"\x82":130,"\x83":131, "\x84":132,"\x85":133,"\x86":134,"\x87":135,"\x88":136,"\x89":137, "\x8a":138,"\x8b":139,"\x8c":140,"\x8d":141,"\x8e":142,"\x8f":143, "\x90":144,"\x91":145,"\x92":146,"\x93":147,"\x94":148,"\x95":149, "\x96":150,"\x97":151,"\x98":152,"\x99":153,"\x9a":154,"\x9b":155, "\x9c":156,"\x9d":157,"\x9e":158,"\x9f":159,"\xa0":160,"\xa1":161, "\xa2":162,"\xa3":163,"\xa4":164,"\xa5":165,"\xa6":166,"\xa7":167, "\xa8":168,"\xa9":169,"\xaa":170,"\xab":171,"\xac":172,"\xad":173, "\xae":174,"\xaf":175,"\xb0":176,"\xb1":177,"\xb2":178,"\xb3":179, "\xb4":180,"\xb5":181,"\xb6":182,"\xb7":183,"\xb8":184,"\xb9":185, "\xba":186,"\xbb":187,"\xbc":188,"\xbd":189,"\xbe":190,"\xbf":191, "\xc0":192,"\xc1":193,"\xc2":194,"\xc3":195,"\xc4":196,"\xc5":197, "\xc6":198,"\xc7":199,"\xc8":200,"\xc9":201,"\xca":202,"\xcb":203, "\xcc":204,"\xcd":205,"\xce":206,"\xcf":207,"\xd0":208,"\xd1":209, "\xd2":210,"\xd3":211,"\xd4":212,"\xd5":213,"\xd6":214,"\xd7":215, "\xd8":216,"\xd9":217,"\xda":218,"\xdb":219,"\xdc":220,"\xdd":221, "\xde":222,"\xdf":223,"\xe0":224,"\xe1":225,"\xe2":226,"\xe3":227, "\xe4":228,"\xe5":229,"\xe6":230,"\xe7":231,"\xe8":232,"\xe9":233, "\xea":234,"\xeb":235,"\xec":236,"\xed":237,"\xee":238,"\xef":239, "\xf0":240,"\xf1":241,"\xf2":242,"\xf3":243,"\xf4":244,"\xf5":245, "\xf6":246,"\xf7":247,"\xf8":248,"\xf9":249,"\xfa":250,"\xfb":251, "\xfc":252,"\xfd":253,"\xfe":254,"\xff":255 }; } } first_256_unicode_chars_as_properties: { input: { var o = { "\u0000": 0, "\u0001": 1, "\u0002": 2, "\u0003": 3, "\u0004": 4, "\u0005": 5, "\u0006": 6, "\u0007": 7, "\u0008": 8, "\u0009": 9, "\u000A": 10, "\u000B": 11, "\u000C": 12, "\u000D": 13, "\u000E": 14, "\u000F": 15, "\u0010": 16, "\u0011": 17, "\u0012": 18, "\u0013": 19, "\u0014": 20, "\u0015": 21, "\u0016": 22, "\u0017": 23, "\u0018": 24, "\u0019": 25, "\u001A": 26, "\u001B": 27, "\u001C": 28, "\u001D": 29, "\u001E": 30, "\u001F": 31, "\u0020": 32, "\u0021": 33, "\u0022": 34, "\u0023": 35, "\u0024": 36, "\u0025": 37, "\u0026": 38, "\u0027": 39, "\u0028": 40, "\u0029": 41, "\u002A": 42, "\u002B": 43, "\u002C": 44, "\u002D": 45, "\u002E": 46, "\u002F": 47, "\u0030": 48, "\u0031": 49, "\u0032": 50, "\u0033": 51, "\u0034": 52, "\u0035": 53, "\u0036": 54, "\u0037": 55, "\u0038": 56, "\u0039": 57, "\u003A": 58, "\u003B": 59, "\u003C": 60, "\u003D": 61, "\u003E": 62, "\u003F": 63, "\u0040": 64, "\u0041": 65, "\u0042": 66, "\u0043": 67, "\u0044": 68, "\u0045": 69, "\u0046": 70, "\u0047": 71, "\u0048": 72, "\u0049": 73, "\u004A": 74, "\u004B": 75, "\u004C": 76, "\u004D": 77, "\u004E": 78, "\u004F": 79, "\u0050": 80, "\u0051": 81, "\u0052": 82, "\u0053": 83, "\u0054": 84, "\u0055": 85, "\u0056": 86, "\u0057": 87, "\u0058": 88, "\u0059": 89, "\u005A": 90, "\u005B": 91, "\u005C": 92, "\u005D": 93, "\u005E": 94, "\u005F": 95, "\u0060": 96, "\u0061": 97, "\u0062": 98, "\u0063": 99, "\u0064": 100, "\u0065": 101, "\u0066": 102, "\u0067": 103, "\u0068": 104, "\u0069": 105, "\u006A": 106, "\u006B": 107, "\u006C": 108, "\u006D": 109, "\u006E": 110, "\u006F": 111, "\u0070": 112, "\u0071": 113, "\u0072": 114, "\u0073": 115, "\u0074": 116, "\u0075": 117, "\u0076": 118, "\u0077": 119, "\u0078": 120, "\u0079": 121, "\u007A": 122, "\u007B": 123, "\u007C": 124, "\u007D": 125, "\u007E": 126, "\u007F": 127, "\u0080": 128, "\u0081": 129, "\u0082": 130, "\u0083": 131, "\u0084": 132, "\u0085": 133, "\u0086": 134, "\u0087": 135, "\u0088": 136, "\u0089": 137, "\u008A": 138, "\u008B": 139, "\u008C": 140, "\u008D": 141, "\u008E": 142, "\u008F": 143, "\u0090": 144, "\u0091": 145, "\u0092": 146, "\u0093": 147, "\u0094": 148, "\u0095": 149, "\u0096": 150, "\u0097": 151, "\u0098": 152, "\u0099": 153, "\u009A": 154, "\u009B": 155, "\u009C": 156, "\u009D": 157, "\u009E": 158, "\u009F": 159, "\u00A0": 160, "\u00A1": 161, "\u00A2": 162, "\u00A3": 163, "\u00A4": 164, "\u00A5": 165, "\u00A6": 166, "\u00A7": 167, "\u00A8": 168, "\u00A9": 169, "\u00AA": 170, "\u00AB": 171, "\u00AC": 172, "\u00AD": 173, "\u00AE": 174, "\u00AF": 175, "\u00B0": 176, "\u00B1": 177, "\u00B2": 178, "\u00B3": 179, "\u00B4": 180, "\u00B5": 181, "\u00B6": 182, "\u00B7": 183, "\u00B8": 184, "\u00B9": 185, "\u00BA": 186, "\u00BB": 187, "\u00BC": 188, "\u00BD": 189, "\u00BE": 190, "\u00BF": 191, "\u00C0": 192, "\u00C1": 193, "\u00C2": 194, "\u00C3": 195, "\u00C4": 196, "\u00C5": 197, "\u00C6": 198, "\u00C7": 199, "\u00C8": 200, "\u00C9": 201, "\u00CA": 202, "\u00CB": 203, "\u00CC": 204, "\u00CD": 205, "\u00CE": 206, "\u00CF": 207, "\u00D0": 208, "\u00D1": 209, "\u00D2": 210, "\u00D3": 211, "\u00D4": 212, "\u00D5": 213, "\u00D6": 214, "\u00D7": 215, "\u00D8": 216, "\u00D9": 217, "\u00DA": 218, "\u00DB": 219, "\u00DC": 220, "\u00DD": 221, "\u00DE": 222, "\u00DF": 223, "\u00E0": 224, "\u00E1": 225, "\u00E2": 226, "\u00E3": 227, "\u00E4": 228, "\u00E5": 229, "\u00E6": 230, "\u00E7": 231, "\u00E8": 232, "\u00E9": 233, "\u00EA": 234, "\u00EB": 235, "\u00EC": 236, "\u00ED": 237, "\u00EE": 238, "\u00EF": 239, "\u00F0": 240, "\u00F1": 241, "\u00F2": 242, "\u00F3": 243, "\u00F4": 244, "\u00F5": 245, "\u00F6": 246, "\u00F7": 247, "\u00F8": 248, "\u00F9": 249, "\u00FA": 250, "\u00FB": 251, "\u00FC": 252, "\u00FD": 253, "\u00FE": 254, "\u00FF": 255 }; } expect: { var o = { "\0":0,"\x01":1,"\x02":2,"\x03":3,"\x04":4,"\x05":5,"\x06":6, "\x07":7,"\b":8,"\t":9,"\n":10,"\v":11,"\f":12,"\r":13,"\x0e":14, "\x0f":15,"\x10":16,"\x11":17,"\x12":18,"\x13":19,"\x14":20,"\x15":21, "\x16":22,"\x17":23,"\x18":24,"\x19":25,"\x1a":26,"\x1b":27,"\x1c":28, "\x1d":29,"\x1e":30,"\x1f":31," ":32,"!":33,'"':34,"#":35,$:36, "%":37,"&":38,"'":39,"(":40,")":41,"*":42,"+":43,",":44,"-":45, ".":46,"/":47,0:48,1:49,2:50,3:51,4:52,5:53,6:54,7:55,8:56,9:57, ":":58,";":59,"<":60,"=":61,">":62,"?":63,"@":64,A:65,B:66,C:67, D:68,E:69,F:70,G:71,H:72,I:73,J:74,K:75,L:76,M:77,N:78,O:79,P:80, Q:81,R:82,S:83,T:84,U:85,V:86,W:87,X:88,Y:89,Z:90,"[":91,"\\":92, "]":93,"^":94,_:95,"`":96,a:97,b:98,c:99,d:100,e:101,f:102,g:103, h:104,i:105,j:106,k:107,l:108,m:109,n:110,o:111,p:112,q:113,r:114, s:115,t:116,u:117,v:118,w:119,x:120,y:121,z:122,"{":123,"|":124, "}":125,"~":126,"\x7f":127,"\x80":128,"\x81":129,"\x82":130,"\x83":131, "\x84":132,"\x85":133,"\x86":134,"\x87":135,"\x88":136,"\x89":137, "\x8a":138,"\x8b":139,"\x8c":140,"\x8d":141,"\x8e":142,"\x8f":143, "\x90":144,"\x91":145,"\x92":146,"\x93":147,"\x94":148,"\x95":149, "\x96":150,"\x97":151,"\x98":152,"\x99":153,"\x9a":154,"\x9b":155, "\x9c":156,"\x9d":157,"\x9e":158,"\x9f":159,"\xa0":160,"\xa1":161, "\xa2":162,"\xa3":163,"\xa4":164,"\xa5":165,"\xa6":166,"\xa7":167, "\xa8":168,"\xa9":169,"\xaa":170,"\xab":171,"\xac":172,"\xad":173, "\xae":174,"\xaf":175,"\xb0":176,"\xb1":177,"\xb2":178,"\xb3":179, "\xb4":180,"\xb5":181,"\xb6":182,"\xb7":183,"\xb8":184,"\xb9":185, "\xba":186,"\xbb":187,"\xbc":188,"\xbd":189,"\xbe":190,"\xbf":191, "\xc0":192,"\xc1":193,"\xc2":194,"\xc3":195,"\xc4":196,"\xc5":197, "\xc6":198,"\xc7":199,"\xc8":200,"\xc9":201,"\xca":202,"\xcb":203, "\xcc":204,"\xcd":205,"\xce":206,"\xcf":207,"\xd0":208,"\xd1":209, "\xd2":210,"\xd3":211,"\xd4":212,"\xd5":213,"\xd6":214,"\xd7":215, "\xd8":216,"\xd9":217,"\xda":218,"\xdb":219,"\xdc":220,"\xdd":221, "\xde":222,"\xdf":223,"\xe0":224,"\xe1":225,"\xe2":226,"\xe3":227, "\xe4":228,"\xe5":229,"\xe6":230,"\xe7":231,"\xe8":232,"\xe9":233, "\xea":234,"\xeb":235,"\xec":236,"\xed":237,"\xee":238,"\xef":239, "\xf0":240,"\xf1":241,"\xf2":242,"\xf3":243,"\xf4":244,"\xf5":245, "\xf6":246,"\xf7":247,"\xf8":248,"\xf9":249,"\xfa":250,"\xfb":251, "\xfc":252,"\xfd":253,"\xfe":254,"\xff":255 }; } } first_256_hex_chars_as_properties: { input: { var o = { "\x00": 0, "\x01": 1, "\x02": 2, "\x03": 3, "\x04": 4, "\x05": 5, "\x06": 6, "\x07": 7, "\x08": 8, "\x09": 9, "\x0A": 10, "\x0B": 11, "\x0C": 12, "\x0D": 13, "\x0E": 14, "\x0F": 15, "\x10": 16, "\x11": 17, "\x12": 18, "\x13": 19, "\x14": 20, "\x15": 21, "\x16": 22, "\x17": 23, "\x18": 24, "\x19": 25, "\x1A": 26, "\x1B": 27, "\x1C": 28, "\x1D": 29, "\x1E": 30, "\x1F": 31, "\x20": 32, "\x21": 33, "\x22": 34, "\x23": 35, "\x24": 36, "\x25": 37, "\x26": 38, "\x27": 39, "\x28": 40, "\x29": 41, "\x2A": 42, "\x2B": 43, "\x2C": 44, "\x2D": 45, "\x2E": 46, "\x2F": 47, "\x30": 48, "\x31": 49, "\x32": 50, "\x33": 51, "\x34": 52, "\x35": 53, "\x36": 54, "\x37": 55, "\x38": 56, "\x39": 57, "\x3A": 58, "\x3B": 59, "\x3C": 60, "\x3D": 61, "\x3E": 62, "\x3F": 63, "\x40": 64, "\x41": 65, "\x42": 66, "\x43": 67, "\x44": 68, "\x45": 69, "\x46": 70, "\x47": 71, "\x48": 72, "\x49": 73, "\x4A": 74, "\x4B": 75, "\x4C": 76, "\x4D": 77, "\x4E": 78, "\x4F": 79, "\x50": 80, "\x51": 81, "\x52": 82, "\x53": 83, "\x54": 84, "\x55": 85, "\x56": 86, "\x57": 87, "\x58": 88, "\x59": 89, "\x5A": 90, "\x5B": 91, "\x5C": 92, "\x5D": 93, "\x5E": 94, "\x5F": 95, "\x60": 96, "\x61": 97, "\x62": 98, "\x63": 99, "\x64": 100, "\x65": 101, "\x66": 102, "\x67": 103, "\x68": 104, "\x69": 105, "\x6A": 106, "\x6B": 107, "\x6C": 108, "\x6D": 109, "\x6E": 110, "\x6F": 111, "\x70": 112, "\x71": 113, "\x72": 114, "\x73": 115, "\x74": 116, "\x75": 117, "\x76": 118, "\x77": 119, "\x78": 120, "\x79": 121, "\x7A": 122, "\x7B": 123, "\x7C": 124, "\x7D": 125, "\x7E": 126, "\x7F": 127, "\x80": 128, "\x81": 129, "\x82": 130, "\x83": 131, "\x84": 132, "\x85": 133, "\x86": 134, "\x87": 135, "\x88": 136, "\x89": 137, "\x8A": 138, "\x8B": 139, "\x8C": 140, "\x8D": 141, "\x8E": 142, "\x8F": 143, "\x90": 144, "\x91": 145, "\x92": 146, "\x93": 147, "\x94": 148, "\x95": 149, "\x96": 150, "\x97": 151, "\x98": 152, "\x99": 153, "\x9A": 154, "\x9B": 155, "\x9C": 156, "\x9D": 157, "\x9E": 158, "\x9F": 159, "\xA0": 160, "\xA1": 161, "\xA2": 162, "\xA3": 163, "\xA4": 164, "\xA5": 165, "\xA6": 166, "\xA7": 167, "\xA8": 168, "\xA9": 169, "\xAA": 170, "\xAB": 171, "\xAC": 172, "\xAD": 173, "\xAE": 174, "\xAF": 175, "\xB0": 176, "\xB1": 177, "\xB2": 178, "\xB3": 179, "\xB4": 180, "\xB5": 181, "\xB6": 182, "\xB7": 183, "\xB8": 184, "\xB9": 185, "\xBA": 186, "\xBB": 187, "\xBC": 188, "\xBD": 189, "\xBE": 190, "\xBF": 191, "\xC0": 192, "\xC1": 193, "\xC2": 194, "\xC3": 195, "\xC4": 196, "\xC5": 197, "\xC6": 198, "\xC7": 199, "\xC8": 200, "\xC9": 201, "\xCA": 202, "\xCB": 203, "\xCC": 204, "\xCD": 205, "\xCE": 206, "\xCF": 207, "\xD0": 208, "\xD1": 209, "\xD2": 210, "\xD3": 211, "\xD4": 212, "\xD5": 213, "\xD6": 214, "\xD7": 215, "\xD8": 216, "\xD9": 217, "\xDA": 218, "\xDB": 219, "\xDC": 220, "\xDD": 221, "\xDE": 222, "\xDF": 223, "\xE0": 224, "\xE1": 225, "\xE2": 226, "\xE3": 227, "\xE4": 228, "\xE5": 229, "\xE6": 230, "\xE7": 231, "\xE8": 232, "\xE9": 233, "\xEA": 234, "\xEB": 235, "\xEC": 236, "\xED": 237, "\xEE": 238, "\xEF": 239, "\xF0": 240, "\xF1": 241, "\xF2": 242, "\xF3": 243, "\xF4": 244, "\xF5": 245, "\xF6": 246, "\xF7": 247, "\xF8": 248, "\xF9": 249, "\xFA": 250, "\xFB": 251, "\xFC": 252, "\xFD": 253, "\xFE": 254, "\xFF": 255 }; } expect: { var o = { "\0":0,"\x01":1,"\x02":2,"\x03":3,"\x04":4,"\x05":5,"\x06":6, "\x07":7,"\b":8,"\t":9,"\n":10,"\v":11,"\f":12,"\r":13,"\x0e":14, "\x0f":15,"\x10":16,"\x11":17,"\x12":18,"\x13":19,"\x14":20,"\x15":21, "\x16":22,"\x17":23,"\x18":24,"\x19":25,"\x1a":26,"\x1b":27,"\x1c":28, "\x1d":29,"\x1e":30,"\x1f":31," ":32,"!":33,'"':34,"#":35,$:36, "%":37,"&":38,"'":39,"(":40,")":41,"*":42,"+":43,",":44,"-":45, ".":46,"/":47,0:48,1:49,2:50,3:51,4:52,5:53,6:54,7:55,8:56,9:57, ":":58,";":59,"<":60,"=":61,">":62,"?":63,"@":64,A:65,B:66,C:67, D:68,E:69,F:70,G:71,H:72,I:73,J:74,K:75,L:76,M:77,N:78,O:79,P:80, Q:81,R:82,S:83,T:84,U:85,V:86,W:87,X:88,Y:89,Z:90,"[":91,"\\":92, "]":93,"^":94,_:95,"`":96,a:97,b:98,c:99,d:100,e:101,f:102,g:103, h:104,i:105,j:106,k:107,l:108,m:109,n:110,o:111,p:112,q:113,r:114, s:115,t:116,u:117,v:118,w:119,x:120,y:121,z:122,"{":123,"|":124, "}":125,"~":126,"\x7f":127,"\x80":128,"\x81":129,"\x82":130,"\x83":131, "\x84":132,"\x85":133,"\x86":134,"\x87":135,"\x88":136,"\x89":137, "\x8a":138,"\x8b":139,"\x8c":140,"\x8d":141,"\x8e":142,"\x8f":143, "\x90":144,"\x91":145,"\x92":146,"\x93":147,"\x94":148,"\x95":149, "\x96":150,"\x97":151,"\x98":152,"\x99":153,"\x9a":154,"\x9b":155, "\x9c":156,"\x9d":157,"\x9e":158,"\x9f":159,"\xa0":160,"\xa1":161, "\xa2":162,"\xa3":163,"\xa4":164,"\xa5":165,"\xa6":166,"\xa7":167, "\xa8":168,"\xa9":169,"\xaa":170,"\xab":171,"\xac":172,"\xad":173, "\xae":174,"\xaf":175,"\xb0":176,"\xb1":177,"\xb2":178,"\xb3":179, "\xb4":180,"\xb5":181,"\xb6":182,"\xb7":183,"\xb8":184,"\xb9":185, "\xba":186,"\xbb":187,"\xbc":188,"\xbd":189,"\xbe":190,"\xbf":191, "\xc0":192,"\xc1":193,"\xc2":194,"\xc3":195,"\xc4":196,"\xc5":197, "\xc6":198,"\xc7":199,"\xc8":200,"\xc9":201,"\xca":202,"\xcb":203, "\xcc":204,"\xcd":205,"\xce":206,"\xcf":207,"\xd0":208,"\xd1":209, "\xd2":210,"\xd3":211,"\xd4":212,"\xd5":213,"\xd6":214,"\xd7":215, "\xd8":216,"\xd9":217,"\xda":218,"\xdb":219,"\xdc":220,"\xdd":221, "\xde":222,"\xdf":223,"\xe0":224,"\xe1":225,"\xe2":226,"\xe3":227, "\xe4":228,"\xe5":229,"\xe6":230,"\xe7":231,"\xe8":232,"\xe9":233, "\xea":234,"\xeb":235,"\xec":236,"\xed":237,"\xee":238,"\xef":239, "\xf0":240,"\xf1":241,"\xf2":242,"\xf3":243,"\xf4":244,"\xf5":245, "\xf6":246,"\xf7":247,"\xf8":248,"\xf9":249,"\xfa":250,"\xfb":251, "\xfc":252,"\xfd":253,"\xfe":254,"\xff":255 }; } } native_prototype: { options = { unsafe_proto: true, } input: { Array.prototype.splice.apply(a, [1, 2, b, c]); Function.prototype.call.apply(console.log, console, [ "foo" ]); Number.prototype.toFixed.call(Math.PI, 2); Object.prototype.hasOwnProperty.call(d, "foo"); RegExp.prototype.test.call(/foo/, "bar"); String.prototype.indexOf.call(e, "bar"); } expect: { [].splice.apply(a, [1, 2, b, c]); (function() {}).call.apply(console.log, console, [ "foo" ]); 0..toFixed.call(Math.PI, 2); ({}).hasOwnProperty.call(d, "foo"); /t/.test.call(/foo/, "bar"); "".indexOf.call(e, "bar"); } } native_prototype_lhs: { options = { unsafe_proto: true, } input: { console.log(function() { Function.prototype.bar = "PASS"; return function() {}; }().bar); } expect: { console.log(function() { Function.prototype.bar = "PASS"; return function() {}; }().bar); } expect_stdout: "PASS" } accessor_boolean: { input: { var a = 1; var b = { get true() { return a; }, set false(c) { a = c; } }; console.log(b.true, b.false = 2, b.true); } expect_exact: 'var a=1;var b={get true(){return a},set false(c){a=c}};console.log(b.true,b.false=2,b.true);' expect_stdout: "1 2 2" } accessor_get_set: { input: { var a = 1; var b = { get set() { return a; }, set get(c) { a = c; } }; console.log(b.set, b.get = 2, b.set); } expect_exact: 'var a=1;var b={get set(){return a},set get(c){a=c}};console.log(b.set,b.get=2,b.set);' expect_stdout: "1 2 2" } accessor_null_undefined: { input: { var a = 1; var b = { get null() { return a; }, set undefined(c) { a = c; } }; console.log(b.null, b.undefined = 2, b.null); } expect_exact: 'var a=1;var b={get null(){return a},set undefined(c){a=c}};console.log(b.null,b.undefined=2,b.null);' expect_stdout: "1 2 2" } accessor_number: { input: { var a = 1; var b = { get 42() { return a; }, set 42(c) { a = c; } }; console.log(b[42], b[42] = 2, b[42]); } expect_exact: 'var a=1;var b={get 42(){return a},set 42(c){a=c}};console.log(b[42],b[42]=2,b[42]);' expect_stdout: "1 2 2" } accessor_string: { input: { var a = 1; var b = { get "a-b"() { return a; }, set "a-b"(c) { a = c; } }; console.log(b["a-b"], b["a-b"] = 2, b["a-b"]); } expect_exact: 'var a=1;var b={get"a-b"(){return a},set"a-b"(c){a=c}};console.log(b["a-b"],b["a-b"]=2,b["a-b"]);' expect_stdout: "1 2 2" } accessor_this: { input: { var a = 1; var b = { get this() { return a; }, set this(c) { a = c; } }; console.log(b.this, b.this = 2, b.this); } expect_exact: 'var a=1;var b={get this(){return a},set this(c){a=c}};console.log(b.this,b.this=2,b.this);' expect_stdout: "1 2 2" } issue_2208_1: { options = { inline: true, properties: true, side_effects: true, } input: { console.log({ p: function() { return 42; } }.p()); } expect: { console.log(42); } expect_stdout: "42" } issue_2208_2: { options = { inline: true, properties: true, side_effects: true, } input: { console.log({ a: 42, p: function() { return this.a; } }.p()); } expect: { console.log({ a: 42, p: function() { return this.a; } }.p()); } expect_stdout: "42" } issue_2208_3: { options = { inline: true, properties: true, side_effects: true, } input: { a = 42; console.log({ p: function() { return function() { return this.a; }(); } }.p()); } expect: { a = 42; console.log(function() { return this.a; }()); } expect_stdout: "42" } issue_2208_4: { options = { inline: true, properties: true, side_effects: true, } input: { function foo() {} console.log({ a: foo(), p: function() { return 42; } }.p()); } expect: { function foo() {} console.log((foo(), function() { return 42; })()); } expect_stdout: "42" } issue_2208_5: { options = { inline: true, properties: true, side_effects: true, } input: { console.log({ p: "FAIL", p: function() { return 42; } }.p()); } expect: { console.log(42); } expect_stdout: "42" } issue_2256: { options = { side_effects: true, } mangle = { properties: { keep_quoted: true, }, } input: { ({ "keep": 1 }); g.keep = g.change; } expect: { 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_funcs: 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_funcs: 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_funcs: 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); } } issue_2513: { options = { evaluate: true, properties: true, } input: { !function(Infinity, NaN, undefined) { console.log("a"[1/0], "b"["Infinity"]); console.log("c"[0/0], "d"["NaN"]); console.log("e"[void 0], "f"["undefined"]); }(0, 0, 0); } expect: { !function(Infinity, NaN, undefined) { console.log("a"[1/0], "b"[1/0]); console.log("c".NaN, "d".NaN); console.log("e"[void 0], "f"[void 0]); }(0, 0, 0); } expect_stdout: [ "undefined undefined", "undefined undefined", "undefined undefined", ] } const_prop_assign_strict: { options = { pure_getters: "strict", side_effects: true, } input: { function Simulator() { /abc/.index = 1; this._aircraft = []; } (function() {}).prototype.destroy = x(); } expect: { function Simulator() { this._aircraft = []; } x(); } } const_prop_assign_pure: { options = { pure_getters: true, side_effects: true, } input: { function Simulator() { /abc/.index = 1; this._aircraft = []; } (function() {}).prototype.destroy = x(); } expect: { function Simulator() { this._aircraft = []; } x(); } } join_object_assignments_1: { options = { evaluate: true, join_vars: true, } input: { console.log(function() { var x = { a: 1, c: (console.log("c"), "C"), }; x.b = 2; x[3] = function() { console.log(x); }, x["a"] = /foo/, x.bar = x; return x; }()); } expect: { console.log(function() { var x = { a: 1, c: (console.log("c"), "C"), b: 2, 3: function() { console.log(x); }, a: /foo/, }; x.bar = x; return x; }()); } expect_stdout: true } join_object_assignments_2: { options = { evaluate: true, hoist_props: true, join_vars: true, passes: 3, reduce_vars: true, toplevel: true, unused: true, } input: { var o = { foo: 1, }; o.bar = 2; o.baz = 3; console.log(o.foo, o.bar + o.bar, o.foo * o.bar * o.baz); } expect: { console.log(1, 4, 6); } expect_stdout: "1 4 6" } join_object_assignments_3: { options = { evaluate: true, join_vars: true, } input: { console.log(function() { var o = { a: "PASS", }, a = o.a; o.a = "FAIL"; return a; }()); } expect: { console.log(function() { var o = { a: "PASS", }, a = o.a; o.a = "FAIL"; return a; }()); } expect_stdout: "PASS" } join_object_assignments_4: { options = { join_vars: true, sequences: true, } input: { var o; console.log(o); o = {}; o.a = "foo"; console.log(o.b); o.b = "bar"; console.log(o.a); } expect: { var o; console.log(o), o = { a: "foo", }, console.log(o.b), o.b = "bar", console.log(o.a); } expect_stdout: [ "undefined", "undefined", "foo", ] } join_object_assignments_return_1: { options = { join_vars: true, } input: { console.log(function() { var o = { p: 3 }; return o.q = "foo"; }()); } expect: { console.log(function() { var o = { p: 3, q: "foo" }; return o.q; }()); } expect_stdout: "foo" } join_object_assignments_return_2: { options = { join_vars: true, } input: { console.log(function() { var o = { p: 3 }; return o.q = /foo/, o.r = "bar"; }()); } expect: { console.log(function() { var o = { p: 3, q: /foo/, r: "bar" }; return o.r; }()); } expect_stdout: "bar" } join_object_assignments_return_3: { options = { join_vars: true, } input: { console.log(function() { var o = { p: 3 }; return o.q = "foo", o.p += "", console.log(o.q), o.p; }()); } expect: { console.log(function() { var o = { p: 3, q: "foo" }; return o.p += "", console.log(o.q), o.p; }()); } expect_stdout: [ "foo", "3", ] } join_object_assignments_for: { options = { join_vars: true, } input: { console.log(function() { var o = { p: 3 }; for (o.q = "foo"; console.log(o.q);); return o.p; }()); } expect: { console.log(function() { for (var o = { p: 3, q: "foo" }; console.log(o.q);); return o.p; }()); } expect_stdout: [ "foo", "3", ] } join_object_assignments_if: { options = { join_vars: true, } input: { console.log(function() { var o = {}; if (o.a = "PASS") return o.a; }()) } expect: { console.log(function() { var o = { a: "PASS" }; if (o.a) return o.a; }()); } expect_stdout: "PASS" } join_object_assignments_forin: { options = { join_vars: true, } input: { console.log(function() { var o = {}; for (var a in o.a = "PASS", o) return o[a]; }()) } expect: { console.log(function() { var o = { a: "PASS" }; for (var a in o) return o[a]; }()); } expect_stdout: "PASS" } join_object_assignments_negative: { options = { evaluate: true, join_vars: true, properties: true, } input: { var o = {}; o[0] = 0; o[-0] = 1; o[-1] = 2; console.log(o[0], o[-0], o[-1]); } expect: { var o = { 0: 0, 0: 1, "-1": 2 }; console.log(o[0], o[-0], o[-1]); } expect_stdout: "1 1 2" } join_object_assignments_NaN_1: { options = { join_vars: true, } input: { var o = {}; o[NaN] = 1; o[0/0] = 2; console.log(o[NaN], o[NaN]); } expect: { var o = {}; o[NaN] = 1; o[0/0] = 2; console.log(o[NaN], o[NaN]); } expect_stdout: "2 2" } join_object_assignments_NaN_2: { options = { evaluate: true, join_vars: true, properties: true, } input: { var o = {}; o[NaN] = 1; o[0/0] = 2; console.log(o[NaN], o[NaN]); } expect: { var o = { NaN: 1, NaN: 2 }; console.log(o.NaN, o.NaN); } expect_stdout: "2 2" } join_object_assignments_null_0: { options = { join_vars: true, } input: { var o = {}; o[null] = 1; console.log(o[null]); } expect: { var o = {}; o[null] = 1; console.log(o[null]); } expect_stdout: "1" } join_object_assignments_null_1: { options = { evaluate: true, join_vars: true, properties: true, } input: { var o = {}; o[null] = 1; console.log(o[null]); } expect: { var o = { null: 1 }; console.log(o.null); } expect_stdout: "1" } join_object_assignments_void_0: { options = { evaluate: true, join_vars: true, } input: { var o = {}; o[void 0] = 1; console.log(o[void 0]); } expect: { var o = { undefined: 1 }; console.log(o[void 0]); } expect_stdout: "1" } join_object_assignments_undefined_1: { options = { join_vars: true, } input: { var o = {}; o[undefined] = 1; console.log(o[undefined]); } expect: { var o = {}; o[void 0] = 1; console.log(o[void 0]); } expect_stdout: "1" } join_object_assignments_undefined_2: { options = { evaluate: true, join_vars: true, properties: true, } input: { var o = {}; o[undefined] = 1; console.log(o[undefined]); } expect: { var o = { undefined : 1 }; console.log(o[void 0]); } expect_stdout: "1" } join_object_assignments_Infinity: { options = { evaluate: true, join_vars: true, properties: true, } input: { var o = {}; o[Infinity] = 1; o[1/0] = 2; o[-Infinity] = 3; o[-1/0] = 4; console.log(o[Infinity], o[1/0], o[-Infinity], o[-1/0]); } expect: { var o = { Infinity: 1, Infinity: 2, "-Infinity": 3, "-Infinity": 4 }; console.log(o[1/0], o[1/0], o[-1/0], o[-1/0]); } expect_stdout: "2 2 4 4" } join_object_assignments_regex: { options = { evaluate: true, join_vars: true, properties: true, } input: { var o = {}; o[/rx/] = 1; console.log(o[/rx/]); } expect: { var o = { "/rx/": 1 }; console.log(o[/rx/]); } expect_stdout: "1" } issue_2816: { options = { join_vars: true, } input: { "use strict"; var o = { a: 1 }; o.b = 2; o.a = 3; o.c = 4; console.log(o.a, o.b, o.c); } expect: { "use strict"; var o = { a: 1, b: 2 }; o.a = 3; o.c = 4; console.log(o.a, o.b, o.c); } expect_stdout: "3 2 4" } issue_2893_1: { options = { join_vars: true, } input: { var o = { get a() { return "PASS"; }, }; o.a = "FAIL"; console.log(o.a); } expect: { var o = { get a() { return "PASS"; }, }; o.a = "FAIL"; console.log(o.a); } expect_stdout: "PASS" } issue_2893_2: { options = { join_vars: true, } input: { var o = { set a(v) { this.b = v; }, b: "FAIL", }; o.a = "PASS"; console.log(o.b); } expect: { var o = { set a(v) { this.b = v; }, b: "FAIL", }; o.a = "PASS"; console.log(o.b); } expect_stdout: "PASS" } issue_869_1: { mangle = { properties: { reserved: [ "get" ] }, } input: { var o = { p: "FAIL" }; Object.defineProperty(o, "p", { get: function() { return "PASS"; } }); console.log(o.p); } expect: { var o = { o: "FAIL" }; Object.defineProperty(o, "o", { get: function() { return "PASS"; } }); console.log(o.o); } expect_stdout: "PASS" } issue_869_2: { mangle = { properties: { reserved: [ "get" ] }, } input: { var o = { p: "FAIL" }; Object.defineProperties(o, { p: { get: function() { return "PASS"; } } }); console.log(o.p); } expect: { var o = { o: "FAIL" }; Object.defineProperties(o, { o: { get: function() { return "PASS"; } } }); console.log(o.o); } expect_stdout: "PASS" } issue_3188_1: { options = { collapse_vars: true, inline: true, properties: true, reduce_vars: true, side_effects: true, } input: { (function() { function f() { console.log(this.p); } (function() { var o = { p: "PASS", f: f }; o.f(); })(); })(); } expect: { (function() { function f() { console.log(this.p); } ({ p: "PASS", f: f }).f(); var o; })(); } expect_stdout: "PASS" } issue_3188_2: { options = { collapse_vars: true, inline: true, properties: true, reduce_vars: true, side_effects: true, unused: true, } input: { (function() { var f = function() { console.log(this.p); }; function g() { var o = { p: "PASS", f: f }; o.f(); } g(); })(); } expect: { ({ p: "PASS", f: function() { console.log(this.p); } }).f(); } expect_stdout: "PASS" } issue_3188_3: { options = { collapse_vars: true, inline: true, properties: true, reduce_vars: true, side_effects: true, } input: { (function() { function f() { console.log(this[0]); } (function() { var o = ["PASS", f]; o[1](); })(); })(); } expect: { (function() { function f() { console.log(this[0]); } ["PASS", f][1](); var o; })(); } expect_stdout: "PASS" } join_expr: { options = { evaluate: true, join_vars: true, } input: { var c = "FAIL"; (function() { var a = 0; switch ((a = {}) && (a.b = 0)) { case 0: c = "PASS"; } })(); console.log(c); } expect: { var c = "FAIL"; (function() { var a = 0; switch (a = { b: 0 }, a.b) { case 0: c = "PASS"; } })(); console.log(c); } expect_stdout: "PASS" } issue_3389: { options = { evaluate: true, properties: true, reduce_vars: true, unsafe: true, } input: { (function() { var a = "PASS"; if (delete b) b = a[null] = 42; console.log(a); })(); } expect: { (function() { var a = "PASS"; if (delete b) b = a.null = 42; console.log(a); })(); } expect_stdout: "PASS" } UglifyJS2-3.6.3/test/compress/pure_funcs.js000066400000000000000000000326201355252637300206400ustar00rootroot00000000000000array: { options = { pure_funcs: [ "Math.floor" ], side_effects: true, } input: { var a; function f(b) { Math.floor(a / b); Math.floor(c / b); } } expect: { var a; function f(b) { c; } } } func: { options = { pure_funcs: function(node) { return !~node.args[0].print_to_string().indexOf("a"); }, side_effects: true, } input: { function f(a, b) { Math.floor(a / b); Math.floor(c / b); } } expect: { function f(a, b) { Math.floor(c / b); } } } side_effects: { options = { pure_funcs: [ "console.log" ], side_effects: true, } input: { function f(a, b) { console.log(a()); console.log(b); } } expect: { function f(a, b) { a(); } } } unused: { options = { pure_funcs: [ "pure" ], side_effects: true, unused: true, } input: { function foo() { var u = pure(1); var x = pure(2); var y = pure(x); var z = pure(pure(side_effects())); return pure(3); } } expect: { function foo() { side_effects(); return pure(3); } } } babel: { options = { pure_funcs: [ "_classCallCheck" ], side_effects: true, unused: true, } input: { function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } var Foo = function Foo() { _classCallCheck(this, Foo); }; } expect: { function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } var Foo = function() { }; } } conditional: { options = { pure_funcs: [ "pure" ], side_effects: true, } input: { pure(1 | a() ? 2 & b() : 7 ^ c()); pure(1 | a() ? 2 & b() : 5); pure(1 | a() ? 4 : 7 ^ c()); pure(1 | a() ? 4 : 5); pure(3 ? 2 & b() : 7 ^ c()); pure(3 ? 2 & b() : 5); pure(3 ? 4 : 7 ^ c()); pure(3 ? 4 : 5); } expect: { 1 | a() ? b() : c(); 1 | a() && b(); 1 | a() || c(); a(); 3 ? b() : c(); 3 && b(); 3 || c(); } } relational: { options = { pure_funcs: [ "foo" ], side_effects :true, } input: { foo() in foo(); foo() instanceof bar(); foo() < "bar"; bar() > foo(); bar() != bar(); bar() !== "bar"; "bar" == foo(); "bar" === bar(); "bar" >= "bar"; } expect: { bar(); bar(); bar(), bar(); bar(); bar(); } } arithmetic: { options = { pure_funcs: [ "foo" ], side_effects :true, } input: { foo() + foo(); foo() - bar(); foo() * "bar"; bar() / foo(); bar() & bar(); bar() | "bar"; "bar" >> foo(); "bar" << bar(); "bar" >>> "bar"; } expect: { bar(); bar(); bar(), bar(); bar(); bar(); } } boolean_and: { options = { pure_funcs: [ "foo" ], side_effects :true, } input: { foo() && foo(); foo() && bar(); foo() && "bar"; bar() && foo(); bar() && bar(); bar() && "bar"; "bar" && foo(); "bar" && bar(); "bar" && "bar"; } expect: { foo() && bar(); bar(); bar() && bar(); bar(); "bar" && bar(); } } boolean_or: { options = { pure_funcs: [ "foo" ], side_effects :true, } input: { foo() || foo(); foo() || bar(); foo() || "bar"; bar() || foo(); bar() || bar(); bar() || "bar"; "bar" || foo(); "bar" || bar(); "bar" || "bar"; } expect: { foo() || bar(); bar(); bar() || bar(); bar(); "bar" || bar(); } } assign: { options = { pure_funcs: [ "foo" ], side_effects :true, } input: { var a; function f(b) { a = foo(); b *= 4 + foo(); c >>= 0 | foo(); } } expect: { var a; function f(b) { a = foo(); b *= 4 + foo(); c >>= 0 | foo(); } } } unary: { options = { pure_funcs: [ "foo" ], side_effects :true, } input: { typeof foo(); typeof bar(); typeof "bar"; void foo(); void bar(); void "bar"; delete a[foo()]; delete a[bar()]; delete a["bar"]; a[foo()]++; a[bar()]++; a["bar"]++; --a[foo()]; --a[bar()]; --a["bar"]; ~foo(); ~bar(); ~"bar"; } expect: { bar(); bar(); delete a[foo()]; delete a[bar()]; delete a["bar"]; a[foo()]++; a[bar()]++; a["bar"]++; --a[foo()]; --a[bar()]; --a["bar"]; bar(); } } issue_2629_1: { options = { side_effects: true, } beautify = { comments: "all", } input: { /*@__PURE__*/ a(); /*@__PURE__*/ (b()); (/*@__PURE__*/ c)(); (/*@__PURE__*/ d()); } expect_exact: [ "/* */c();", ] } issue_2629_2: { options = { side_effects: true, } beautify = { comments: "all", } input: { /*@__PURE__*/ a(1)(2)(3); /*@__PURE__*/ (b(1))(2)(3); /*@__PURE__*/ (c(1)(2))(3); /*@__PURE__*/ (d(1)(2)(3)); (/*@__PURE__*/ e)(1)(2)(3); (/*@__PURE__*/ f(1))(2)(3); (/*@__PURE__*/ g(1)(2))(3); (/*@__PURE__*/ h(1)(2)(3)); } expect_exact: [ "/* */e(1)(2)(3);", "/* */f(1)(2)(3);", "/* */g(1)(2)(3);", ] } issue_2629_3: { options = { side_effects: true, } beautify = { comments: "all", } input: { /*@__PURE__*/ a.x(1).y(2).z(3); /*@__PURE__*/ (b.x)(1).y(2).z(3); /*@__PURE__*/ (c.x(1)).y(2).z(3); /*@__PURE__*/ (d.x(1).y)(2).z(3); /*@__PURE__*/ (e.x(1).y(2)).z(3); /*@__PURE__*/ (f.x(1).y(2).z)(3); /*@__PURE__*/ (g.x(1).y(2).z(3)); (/*@__PURE__*/ h).x(1).y(2).z(3); (/*@__PURE__*/ i.x)(1).y(2).z(3); (/*@__PURE__*/ j.x(1)).y(2).z(3); (/*@__PURE__*/ k.x(1).y)(2).z(3); (/*@__PURE__*/ l.x(1).y(2)).z(3); (/*@__PURE__*/ m.x(1).y(2).z)(3); (/*@__PURE__*/ n.x(1).y(2).z(3)); } expect_exact: [ "/* */h.x(1).y(2).z(3);", "/* */i.x(1).y(2).z(3);", "/* */j.x(1).y(2).z(3);", "/* */k.x(1).y(2).z(3);", "/* */l.x(1).y(2).z(3);", "/* */m.x(1).y(2).z(3);", ] } issue_2629_4: { options = { side_effects: true, } input: { (/*@__PURE__*/ x(), y()); (w(), /*@__PURE__*/ x(), y()); } expect: { y(); w(), y(); } } issue_2629_5: { options = { side_effects: true, } input: { [ /*@__PURE__*/ x() ]; [ /*@__PURE__*/ x(), y() ]; [ w(), /*@__PURE__*/ x(), y() ]; } expect: { y(); w(), y(); } } issue_2638: { options = { side_effects: true, } beautify = { comments: "all", } input: { /*@__PURE__*/(g() || h())(x(), y()); (/*@__PURE__*/ (a() || b()))(c(), d()); } expect_exact: [ "/* */x(),y();", "/* */(a()||b())(c(),d());", ] } issue_2705_1: { options = { side_effects: true, } beautify = { comments: "all", } input: { /*@__PURE__*/ new a(); /*@__PURE__*/ (new b()); new (/*@__PURE__*/ c)(); (/*@__PURE__*/ new d()); } expect_exact: [ "new/* */c;", ] } issue_2705_2: { options = { side_effects: true, } beautify = { comments: "all", } input: { /*@__PURE__*/ new a(1)(2)(3); /*@__PURE__*/ new (b(1))(2)(3); /*@__PURE__*/ new (c(1)(2))(3); /*@__PURE__*/ new (d(1)(2)(3)); new (/*@__PURE__*/ e)(1)(2)(3); (/*@__PURE__*/ new f(1))(2)(3); (/*@__PURE__*/ new g(1)(2))(3); (/*@__PURE__*/ new h(1)(2)(3)); } expect_exact: [ "new/* */e(1)(2)(3);", "/* */new f(1)(2)(3);", "/* */new g(1)(2)(3);", ] } issue_2705_3: { options = { side_effects: true, } beautify = { comments: "all", } input: { /*@__PURE__*/ new a.x(1).y(2).z(3); /*@__PURE__*/ new (b.x)(1).y(2).z(3); /*@__PURE__*/ new (c.x(1)).y(2).z(3); /*@__PURE__*/ new (d.x(1).y)(2).z(3); /*@__PURE__*/ new (e.x(1).y(2)).z(3); /*@__PURE__*/ new (f.x(1).y(2).z)(3); /*@__PURE__*/ new (g.x(1).y(2).z(3)); new (/*@__PURE__*/ h).x(1).y(2).z(3); /* */ new (/*@__PURE__*/ i.x)(1).y(2).z(3); (/*@__PURE__*/ new j.x(1)).y(2).z(3); (/*@__PURE__*/ new k.x(1).y)(2).z(3); (/*@__PURE__*/ new l.x(1).y(2)).z(3); (/*@__PURE__*/ new m.x(1).y(2).z)(3); (/*@__PURE__*/ new n.x(1).y(2).z(3)); } expect_exact: [ "new/* */h.x(1).y(2).z(3);", "/* */new/* */i.x(1).y(2).z(3);", "/* */new j.x(1).y(2).z(3);", "/* */new k.x(1).y(2).z(3);", "/* */new l.x(1).y(2).z(3);", "/* */new m.x(1).y(2).z(3);", ] } issue_2705_4: { options = { side_effects: true, } input: { (/*@__PURE__*/ new x(), y()); (w(), /*@__PURE__*/ new x(), y()); } expect: { y(); w(), y(); } } issue_2705_5: { options = { side_effects: true, } input: { [ /*@__PURE__*/ new x() ]; [ /*@__PURE__*/ new x(), y() ]; [ w(), /*@__PURE__*/ new x(), y() ]; } expect: { y(); w(), y(); } } issue_2705_6: { options = { side_effects: true, } beautify = { comments: "all", } input: { /*@__PURE__*/new (g() || h())(x(), y()); /* */ new (/*@__PURE__*/ (a() || b()))(c(), d()); } expect_exact: [ "/* */x(),y();", "/* */new(/* */a()||b())(c(),d());", ] } issue_3065_1: { options = { inline: true, pure_funcs: [ "pureFunc" ], reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { function modifyWrapper(a, f, wrapper) { wrapper.a = a; wrapper.f = f; return wrapper; } function pureFunc(fun) { return modifyWrapper(1, fun, function(a) { return fun(a); }); } var unused = pureFunc(function(x) { return x; }); } expect: {} } issue_3065_2: { rename = true options = { inline: true, pure_funcs: [ "pureFunc" ], reduce_vars: true, side_effects: true, toplevel: true, unused: true, } mangle = { reserved: [ "pureFunc" ], toplevel: true, } input: { function modifyWrapper(a, f, wrapper) { wrapper.a = a; wrapper.f = f; return wrapper; } function pureFunc(fun) { return modifyWrapper(1, fun, function(a) { return fun(a); }); } var unused = pureFunc(function(x) { return x; }); } expect: {} } issue_3065_3: { options = { pure_funcs: [ "debug" ], reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { function debug(msg) { console.log(msg); } debug(function() { console.log("PASS"); return "FAIL"; }()); } expect: { (function() { console.log("PASS"); })(); } } issue_3065_4: { options = { pure_funcs: [ "debug" ], reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { var debug = function(msg) { console.log(msg); }; debug(function() { console.log("PASS"); return "FAIL"; }()); } expect: { (function() { console.log("PASS"); })(); } } issue_3325_1: { options = { pure_funcs: "cb", side_effects: true, } input: { function cb() { console.log("PASS"); } cb(); } expect: { function cb() { console.log("PASS"); } } } issue_3325_2: { options = { pure_funcs: "xxxcbxxx", side_effects: true, } input: { function cb() { console.log("PASS"); } cb(); } expect: { function cb() { console.log("PASS"); } cb(); } expect_stdout: "PASS" } UglifyJS2-3.6.3/test/compress/pure_getters.js000066400000000000000000000575621355252637300212130ustar00rootroot00000000000000strict: { options = { pure_getters: "strict", reduce_funcs: false, reduce_vars: false, side_effects: true, toplevel: true, } input: { var a, b = null, c = {}; a.prop; b.prop; c.prop; d.prop; null.prop; (void 0).prop; undefined.prop; } expect: { var a, b = null, c = {}; a.prop; b.prop; c.prop; d.prop; null.prop; (void 0).prop; (void 0).prop; } } strict_reduce_vars: { options = { pure_getters: "strict", reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, } input: { var a, b = null, c = {}; a.prop; b.prop; c.prop; d.prop; null.prop; (void 0).prop; undefined.prop; } expect: { var a, b = null, c = {}; a.prop; b.prop; d.prop; null.prop; (void 0).prop; (void 0).prop; } } unsafe: { options = { pure_getters: true, reduce_funcs: false, reduce_vars: false, side_effects: true, toplevel: true, } input: { var a, b = null, c = {}; a.prop; b.prop; c.prop; d.prop; null.prop; (void 0).prop; undefined.prop; } expect: { var a, b = null, c = {}; d; null.prop; (void 0).prop; (void 0).prop; } } unsafe_reduce_vars: { options = { pure_getters: true, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, } input: { var a, b = null, c = {}; a.prop; b.prop; c.prop; d.prop; null.prop; (void 0).prop; undefined.prop; } expect: { var a, b = null, c = {}; d; null.prop; (void 0).prop; (void 0).prop; } } chained: { options = { pure_getters: "strict", side_effects: true, } input: { a.b.c; } expect: { a.b.c; } } impure_getter_1: { options = { pure_getters: "strict", side_effects: true, } input: { ({ get a() { console.log(1); }, b: 1 }).a; ({ get a() { console.log(1); }, b: 1 }).b; } expect: { ({ get a() { console.log(1); }, b: 1 }).a; ({ get a() { console.log(1); }, b: 1 }).b; } expect_stdout: "1" } impure_getter_2: { options = { pure_getters: true, side_effects: true, } input: { // will produce incorrect output because getter is not pure ({ get a() { console.log(1); }, b: 1 }).a; ({ get a() { console.log(1); }, b: 1 }).b; } expect: {} } issue_2110_1: { options = { collapse_vars: true, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { function f() { function f() {} function g() { return this; } f.g = g; return f.g(); } console.log(typeof f()); } expect: { function f() { function f() {} return f.g = function() { return this; }, f.g(); } console.log(typeof f()); } expect_stdout: "function" } issue_2110_2: { options = { collapse_vars: true, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f() { function f() {} function g() { return this; } f.g = g; return f.g(); } console.log(typeof f()); } expect: { function f() { function f() {} f.g = function() { return this; }; return f.g(); } console.log(typeof f()); } expect_stdout: "function" } set_immutable_1: { options = { collapse_vars: true, evaluate: true, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var a = 1; a.foo += ""; if (a.foo) console.log("FAIL"); else console.log("PASS"); } expect: { 1..foo += ""; if (1..foo) console.log("FAIL"); else console.log("PASS"); } expect_stdout: "PASS" } set_immutable_2: { options = { collapse_vars: true, conditionals: true, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, } input: { var a = 1; a.foo += ""; if (a.foo) console.log("FAIL"); else console.log("PASS"); } expect: { var a = 1; a.foo += "", a.foo ? console.log("FAIL") : console.log("PASS"); } expect_stdout: "PASS" } set_immutable_3: { options = { collapse_vars: true, evaluate: true, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { "use strict"; var a = 1; a.foo += ""; if (a.foo) console.log("FAIL"); else console.log("PASS"); } expect: { "use strict"; 1..foo += ""; if (1..foo) console.log("FAIL"); else console.log("PASS"); } expect_stdout: true } set_immutable_4: { options = { collapse_vars: true, conditionals: true, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, } input: { "use strict"; var a = 1; a.foo += ""; if (a.foo) console.log("FAIL"); else console.log("PASS"); } expect: { "use strict"; var a = 1; a.foo += "", a.foo ? console.log("FAIL") : console.log("PASS"); } expect_stdout: true } set_immutable_5: { options = { collapse_vars: true, conditionals: true, evaluate: true, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { "use strict"; var a = 1; a.foo += ""; if (a.foo) console.log("FAIL"); else console.log("PASS"); } expect: { "use strict"; 1..foo += ""; 1..foo ? console.log("FAIL") : console.log("PASS"); } expect_stdout: true } set_immutable_6: { options = { collapse_vars: true, conditionals: true, evaluate: true, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { var a = 1; a.foo += ""; if (a.foo) console.log("FAIL"); else console.log("PASS"); } expect: { 1..foo ? console.log("FAIL") : console.log("PASS"); } expect_stdout: true } set_mutable_1: { options = { collapse_vars: true, evaluate: true, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function a() { a.foo += ""; if (a.foo) console.log("PASS"); else console.log("FAIL"); }(); } expect: { !function a() { if (a.foo += "") console.log("PASS"); else console.log("FAIL"); }(); } expect_stdout: "PASS" } set_mutable_2: { options = { collapse_vars: true, conditionals: true, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, } input: { !function a() { a.foo += ""; if (a.foo) console.log("PASS"); else console.log("FAIL"); }(); } expect: { !function a() { (a.foo += "") ? console.log("PASS") : console.log("FAIL"); }(); } expect_stdout: "PASS" } issue_2313_1: { options = { collapse_vars: true, conditionals: true, pure_getters: "strict", sequences: true, side_effects: true, } input: { function x() { console.log(1); return { y: function() { console.log(2); return { z: 0 }; } }; } x().y().z++; if (x().y().z) { console.log(3); } } expect: { function x() { return console.log(1), { y: function() { return console.log(2), { z: 0 }; } }; } x().y().z++, x().y().z && console.log(3); } expect_stdout: [ "1", "2", "1", "2", ] } issue_2313_2: { options = { collapse_vars: true, conditionals: true, pure_getters: true, sequences: true, side_effects: true, } input: { function x() { console.log(1); return { y: function() { console.log(2); return { z: 0 }; } }; } x().y().z++; if (x().y().z) { console.log(3); } } expect: { function x() { return console.log(1), { y: function() { return console.log(2), { z: 0 }; } }; } x().y().z++, x().y().z && console.log(3); } expect_stdout: [ "1", "2", "1", "2", ] } issue_2313_3: { options = { collapse_vars: true, conditionals: true, pure_getters: "strict", } input: { function x() { console.log(1); return { y: function() { console.log(2); return { z: 0 }; } }; } x().y().z++; if (x().y().z) { console.log(3); } } expect: { function x() { console.log(1); return { y: function() { console.log(2); return { z: 0 }; } }; } x().y().z++; x().y().z && console.log(3); } expect_stdout: [ "1", "2", "1", "2", ] } issue_2313_4: { options = { collapse_vars: true, conditionals: true, pure_getters: true, } input: { function x() { console.log(1); return { y: function() { console.log(2); return { z: 0 }; } }; } x().y().z++; if (x().y().z) { console.log(3); } } expect: { function x() { console.log(1); return { y: function() { console.log(2); return { z: 0 }; } }; } x().y().z++; x().y().z && console.log(3); } expect_stdout: [ "1", "2", "1", "2", ] } issue_2313_5: { options = { pure_getters: "strict", side_effects: true, } input: { x().y++; x().y; } expect: { x().y++; x().y; } } issue_2313_6: { options = { pure_getters: true, side_effects: true, } input: { x().y++; x().y; } expect: { x().y++; x(); } } issue_2678: { options = { pure_getters: "strict", side_effects: true, } input: { var a = 1, c = "FAIL"; (function f() { (a-- && f()).p; return { get p() { c = "PASS"; } }; })(); console.log(c); } expect: { var a = 1, c = "FAIL"; (function f() { (a-- && f()).p; return { get p() { c = "PASS"; } }; })(); console.log(c); } expect_stdout: "PASS" } issue_2838: { options = { pure_getters: true, side_effects: true, } input: { function f(a, b) { (a || b).c = "PASS"; (function() { return f(a, b); }).prototype.foo = "bar"; } var o = {}; f(null, o); console.log(o.c); } expect: { function f(a, b) { (a || b).c = "PASS"; } var o = {}; f(null, o); console.log(o.c); } expect_stdout: "PASS" } issue_2938_1: { options = { pure_getters: true, unused: true, } input: { function f(a) { a.b = "PASS"; } var o = {}; f(o); console.log(o.b); } expect: { function f(a) { a.b = "PASS"; } var o = {}; f(o); console.log(o.b); } expect_stdout: "PASS" } issue_2938_2: { options = { pure_getters: true, toplevel: true, unused: true, } input: { var Parser = function Parser() {}; var p = Parser.prototype; p.initialContext = function initialContext() { console.log("PASS"); }; p.braceIsBlock = function() {}; (new Parser).initialContext(); } expect: { var Parser = function() {}; var p = Parser.prototype; p.initialContext = function() { console.log("PASS"); }; p.braceIsBlock = function() {}; (new Parser).initialContext(); } expect_stdout: "PASS" } issue_2938_3: { options = { pure_getters: true, side_effects: true, unused: true, } input: { function f(a) { var unused = a.a; a.b = "PASS"; a.c; } var o = {}; o.d; f(o); console.log(o.b); } expect: { function f(a) { a.b = "PASS"; } var o = {}; f(o); console.log(o.b); } expect_stdout: "PASS" } issue_2938_4: { options = { pure_getters: true, side_effects: true, toplevel: true, unused: true, } input: { var Parser = function Parser() {}; var p = Parser.prototype; var unused = p.x; p.initialContext = function initialContext() { p.y; console.log("PASS"); }; p.braceIsBlock = function() {}; (new Parser).initialContext(); } expect: { var Parser = function() {}; var p = Parser.prototype; p.initialContext = function() { console.log("PASS"); }; p.braceIsBlock = function() {}; (new Parser).initialContext(); } expect_stdout: "PASS" } collapse_vars_1_true: { options = { collapse_vars: true, pure_getters: true, unused: true, } input: { function f(a, b) { for (;;) { var c = a.g(); var d = b.p; if (c || d) break; } } } expect: { function f(a, b) { for (;;) { if (a.g() || b.p) break; } } } } collapse_vars_1_false: { options = { collapse_vars: true, pure_getters: false, unused: true, } input: { function f(a, b) { for (;;) { var c = a.g(); var d = b.p; if (c || d) break; } } } expect: { function f(a, b) { for (;;) { var c = a.g(); var d = b.p; if (c || d) break; } } } } collapse_vars_1_strict: { options = { collapse_vars: true, pure_getters: "strict", unused: true, } input: { function f(a, b) { for (;;) { var c = a.g(); var d = b.p; if (c || d) break; } } } expect: { function f(a, b) { for (;;) { var c = a.g(); var d = b.p; if (c || d) break; } } } } collapse_vars_2_true: { options = { collapse_vars: true, pure_getters: true, reduce_vars: true, } input: { function f() { function g() {} g.a = function() {}; g.b = g.a; return g; } } expect: { function f() { function g() {} g.b = g.a = function() {}; return g; } } } collapse_vars_2_false: { options = { collapse_vars: true, pure_getters: false, reduce_vars: true, } input: { function f() { function g() {} g.a = function() {}; g.b = g.a; return g; } } expect: { function f() { function g() {} g.a = function() {}; g.b = g.a; return g; } } } collapse_vars_2_strict: { options = { collapse_vars: true, pure_getters: "strict", reduce_vars: true, } input: { function f() { function g() {} g.a = function() {}; g.b = g.a; return g; } } expect: { function f() { function g() {} g.b = g.a = function() {}; return g; } } } collapse_rhs_true: { options = { collapse_vars: true, pure_getters: true, } input: { console.log((42..length = "PASS", "PASS")); console.log(("foo".length = "PASS", "PASS")); console.log((false.length = "PASS", "PASS")); console.log((function() {}.length = "PASS", "PASS")); console.log(({ get length() { return "FAIL"; } }.length = "PASS", "PASS")); } expect: { console.log(42..length = "PASS"); console.log("foo".length = "PASS"); console.log(false.length = "PASS"); console.log(function() {}.length = "PASS"); console.log({ get length() { return "FAIL"; } }.length = "PASS"); } expect_stdout: [ "PASS", "PASS", "PASS", "PASS", "PASS", ] } collapse_rhs_false: { options = { collapse_vars: true, pure_getters: false, } input: { console.log((42..length = "PASS", "PASS")); console.log(("foo".length = "PASS", "PASS")); console.log((false.length = "PASS", "PASS")); console.log((function() {}.length = "PASS", "PASS")); console.log(({ get length() { return "FAIL"; } }.length = "PASS", "PASS")); } expect: { console.log(42..length = "PASS"); console.log("foo".length = "PASS"); console.log(false.length = "PASS"); console.log(function() {}.length = "PASS"); console.log({ get length() { return "FAIL"; } }.length = "PASS"); } expect_stdout: [ "PASS", "PASS", "PASS", "PASS", "PASS", ] } collapse_rhs_strict: { options = { collapse_vars: true, pure_getters: "strict", } input: { console.log((42..length = "PASS", "PASS")); console.log(("foo".length = "PASS", "PASS")); console.log((false.length = "PASS", "PASS")); console.log((function() {}.length = "PASS", "PASS")); console.log(({ get length() { return "FAIL"; } }.length = "PASS", "PASS")); } expect: { console.log(42..length = "PASS"); console.log("foo".length = "PASS"); console.log(false.length = "PASS"); console.log(function() {}.length = "PASS"); console.log({ get length() { return "FAIL"; } }.length = "PASS"); } expect_stdout: [ "PASS", "PASS", "PASS", "PASS", "PASS", ] } collapse_rhs_setter: { options = { collapse_vars: true, pure_getters: "strict", } input: { try { console.log(({ set length(v) { throw "PASS"; } }.length = "FAIL", "FAIL")); } catch (e) { console.log(e); } } expect: { try { console.log({ set length(v) { throw "PASS"; } }.length = "FAIL"); } catch (e) { console.log(e); } } expect_stdout: "PASS" } collapse_rhs_call: { options = { collapse_vars: true, passes: 2, pure_getters: "strict", reduce_vars: true, toplevel: true, unused: true, } input: { var o = {}; function f() { console.log("PASS"); } o.f = f; f(); } expect: { ({}.f = function() { console.log("PASS"); })(); } expect_stdout: "PASS" } collapse_rhs_lhs: { options = { collapse_vars: true, pure_getters: true, } input: { function f(a, b) { a.b = b, b += 2; console.log(a.b, b); } f({}, 1); } expect: { function f(a, b) { a.b = b, b += 2; console.log(a.b, b); } f({}, 1); } expect_stdout: "1 3" } drop_arguments: { options = { pure_getters: "strict", side_effects: true, } input: { (function() { arguments.slice = function() { console.log("PASS"); }; arguments[42]; arguments.length; arguments.slice(); })(); } expect: { (function() { arguments.slice = function() { console.log("PASS"); }; arguments.slice(); })(); } expect_stdout: "PASS" } issue_3427: { options = { assignments: true, collapse_vars: true, inline: true, pure_getters: "strict", sequences: true, side_effects: true, toplevel: true, unused: true, } input: { var a; (function(b) { b.p = 42; })(a || (a = {})); } expect: {} } UglifyJS2-3.6.3/test/compress/reduce_vars.js000066400000000000000000004141151355252637300207740ustar00rootroot00000000000000reduce_vars: { options = { conditionals: true, evaluate: true, global_defs: { C: 0, }, inline: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var A = 1; (function f0() { var a = 2; console.log(a - 5); console.log(A - 5); })(); (function f1() { var a = 2; console.log(a - 5); eval("console.log(a);"); })(); (function f2(eval) { var a = 2; console.log(a - 5); eval("console.log(a);"); })(eval); (function f3() { var b = typeof C !== "undefined"; var c = 4; if (b) { return 'yes'; } else { return 'no'; } })(); console.log(A + 1); } expect: { var A = 1; (function() { console.log(-3); console.log(A - 5); })(); (function f1() { var a = 2; console.log(a - 5); eval("console.log(a);"); })(); (function f2(eval) { var a = 2; console.log(a - 5); eval("console.log(a);"); })(eval); true, "yes"; console.log(A + 1); } expect_stdout: true } modified: { options = { conditionals: true, evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f0() { var a = 1, b = 2; b++; console.log(a + 1); console.log(b + 1); } function f1() { var a = 1, b = 2; --b; console.log(a + 1); console.log(b + 1); } function f2() { var a = 1, b = 2, c = 3; b = c; console.log(a + b); console.log(b + c); console.log(a + c); console.log(a + b + c); } function f3() { var a = 1, b = 2, c = 3; b *= c; console.log(a + b); console.log(b + c); console.log(a + c); console.log(a + b + c); } function f4() { var a = 1, b = 2, c = 3; if (a) { b = c; } else { c = b; } console.log(a + b); console.log(b + c); console.log(a + c); console.log(a + b + c); } function f5(a) { B = a; console.log(typeof A ? "yes" : "no"); console.log(typeof B ? "yes" : "no"); } f0(), f1(), f2(), f3(), f4(), f5(); } expect: { function f0() { var b = 2; b++; console.log(2); console.log(4); } function f1() { var b = 2; --b; console.log(2); console.log(2); } function f2() { 3; console.log(4); console.log(6); console.log(4); console.log(7); } function f3() { var b = 2; b *= 3; console.log(7); console.log(9); console.log(4); console.log(10); } function f4() { var b = 2, c = 3; 1, b = c; console.log(1 + b); console.log(b + c); console.log(1 + c); console.log(1 + b + c); } function f5(a) { B = a; console.log(typeof A ? "yes" : "no"); console.log(typeof B ? "yes" : "no"); } f0(), f1(), f2(), f3(), f4(), f5(); } expect_stdout: [ "2", "4", "2", "2", "4", "6", "4", "7", "7", "9", "4", "10", "4", "6", "4", "7", "yes", "yes", ] } unsafe_evaluate: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, side_effects: true, unsafe: true, unused: true, } input: { function f0() { var a = { b: 1 }; console.log(a.b + 3); } function f1() { var a = { b: { c: 1 }, d: 2 }; console.log(a.b + 3, a.d + 4, a.b.c + 5, a.d.c + 6); } f0(); f1(); } expect: { function f0() { console.log(4); } function f1() { var a = { b: { c: 1 }, d: 2 }; console.log(a.b + 3, 6, 6, NaN); } f0(); f1(); } expect_stdout: true } unsafe_evaluate_side_effect_free_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unsafe: true, unused: true, } input: { console.log(function(){ var o={p:1}; console.log(o.p); return o.p; }()); console.log(function(){ var o={p:2}; console.log(o.p); return o; }()); console.log(function(){ var o={p:3}; console.log([o][0].p); return o.p; }()); } expect: { console.log(function(){ console.log(1); return 1; }()); console.log(function(){ var o={p:2}; console.log(2); return o; }()); console.log(function(){ console.log(3); return 3; }()); } expect_stdout: true } unsafe_evaluate_side_effect_free_2: { options = { collapse_vars: true, evaluate: true, passes: 2, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, unsafe: true, unused: true, } input: { console.log(function(){ var o={p:1},a=[o]; console.log(a[0].p); return o.p; }()); } expect: { console.log(function(){ console.log(1); return 1; }()); } expect_stdout: true } unsafe_evaluate_escaped: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unsafe: true, unused: true, } input: { console.log(function(){ var o={p:1}; console.log(o, o.p); return o.p; }()); console.log(function(){ var o={p:2}; console.log(o.p, o); return o.p; }()); console.log(function(){ var o={p:3},a=[o]; console.log(a[0].p++); return o.p; }()); } expect: { console.log(function(){ var o={p:1}; console.log(o, 1); return o.p; }()); console.log(function(){ var o={p:2}; console.log(2, o); return o.p; }()); console.log(function(){ var o={p:3},a=[o]; console.log(a[0].p++); return o.p; }()); } expect_stdout: true } unsafe_evaluate_modified: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unsafe: true, unused: true, } input: { console.log(function(){ var o={p:1}; o.p++; console.log(o.p); return o.p; }()); console.log(function(){ var o={p:2}; --o.p; console.log(o.p); return o.p; }()); console.log(function(){ var o={p:3}; o.p += ""; console.log(o.p); return o.p; }()); console.log(function(){ var o={p:4}; o = {}; console.log(o.p); return o.p; }()); console.log(function(){ var o={p:5}; o.p = -9; console.log(o.p); return o.p; }()); function inc() { this.p++; } console.log(function(){ var o={p:6}; inc.call(o); console.log(o.p); return o.p; }()); console.log(function(){ var o={p:7}; console.log([o][0].p++); return o.p; }()); console.log(function(){ var o={p:8}; console.log({q:o}.q.p++); return o.p; }()); } expect: { console.log(function(){ var o={p:1}; o.p++; console.log(o.p); return o.p; }()); console.log(function(){ var o={p:2}; --o.p; console.log(o.p); return o.p; }()); console.log(function(){ var o={p:3}; o.p += ""; console.log(o.p); return o.p; }()); console.log(function(){ var o; o = {}; console.log(o.p); return o.p; }()); console.log(function(){ var o={p:5}; o.p = -9; console.log(o.p); return o.p; }()); function inc() { this.p++; } console.log(function(){ var o={p:6}; inc.call(o); console.log(o.p); return o.p; }()); console.log(function(){ var o={p:7}; console.log([o][0].p++); return o.p; }()); console.log(function(){ var o={p:8}; console.log({q:o}.q.p++); return o.p; }()); } expect_stdout: true } unsafe_evaluate_unknown: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unsafe: true, unused: true, } input: { console.log(function(){ var o={p:1}; console.log(o.not_present); return o.p; }()); console.log(function(){ var o={p:2}; console.log(o.prototype); return o.p; }()); console.log(function(){ var o={p:3}; console.log(o.hasOwnProperty); return o.p; }()); } expect: { console.log(function(){ var o={p:1}; console.log(o.not_present); return o.p; }()); console.log(function(){ var o={p:2}; console.log(o.prototype); return o.p; }()); console.log(function(){ var o={p:3}; console.log(o.hasOwnProperty); return o.p; }()); } expect_stdout: true } unsafe_evaluate_object_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unsafe: true, } input: { function f0(){ var a = 1; var b = {}; b[a] = 2; console.log(a + 3); } function f1(){ var a = { b:1 }; a.b = 2; console.log(a.b + 3); } } expect: { function f0(){ var a = 1; var b = {}; b[1] = 2; console.log(4); } function f1(){ var a = { b:1 }; a.b = 2; console.log(a.b + 3); } } } unsafe_evaluate_object_2: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unsafe: 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.bar, obj.square(2), obj.cube); } expect: { var obj = { foo: 1, bar: 2, square: function(x) { return x * x; }, cube: function(x) { return x * x * x; }, }; console.log(1, 2, obj.square(2), obj.cube); } expect_stdout: true } unsafe_evaluate_object_3: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unsafe: true, } input: { var obj = { get foo() { return 1; }, bar: 2, square: function(x) { return x * x; }, cube: function(x) { return x * x * x; }, }; console.log(obj.foo, obj.bar, obj.square(2), obj.cube); } expect: { var obj = { get foo() { return 1; }, bar: 2, square: function(x) { return x * x; }, cube: function(x) { return x * x * x; }, }; console.log(obj.foo, obj.bar, obj.square(2), obj.cube); } expect_stdout: true } unsafe_evaluate_array_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unsafe: true, } input: { function f0(){ var a = 1; var b = []; b[a] = 2; console.log(a + 3); } function f1(){ var a = [1]; a[2] = 3; console.log(a.length); } function f2(){ var a = [1]; a.push(2); console.log(a.length); } } expect: { function f0(){ var a = 1; var b = []; b[1] = 2; console.log(4); } function f1(){ var a = [1]; a[2] = 3; console.log(a.length); } function f2(){ var a = [1]; a.push(2); console.log(a.length); } } } unsafe_evaluate_array_2: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unsafe: true, } input: { var arr = [ 1, 2, function(x) { return x * x; }, function(x) { return x * x * x; }, ]; console.log(arr[0], arr[1], arr[2](2), arr[3]); } expect: { var arr = [ 1, 2, function(x) { return x * x; }, function(x) { return x * x * x; }, ]; console.log(1, 2, arr[2](2), arr[3]); } expect_stdout: true } unsafe_evaluate_array_3: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unsafe: true, } input: { var arr = [ 1, 2, function() { return ++arr[0]; }, ]; console.log(arr[0], arr[1], arr[2](), arr[0]); } expect: { var arr = [ 1, 2, function() { return ++arr[0]; }, ]; console.log(arr[0], arr[1], arr[2](), arr[0]); } expect_stdout: "1 2 2 2" } unsafe_evaluate_array_4: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unsafe: true, } input: { var arr = [ 1, 2, function() { return ++this[0]; }, ]; console.log(arr[0], arr[1], arr[2], arr[0]); } expect: { var arr = [ 1, 2, function() { return ++this[0]; }, ]; console.log(1, 2, arr[2], 1); } expect_stdout: true } unsafe_evaluate_array_5: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unsafe: true, } input: { var arr = [ 1, 2, function() { return ++this[0]; }, ]; console.log(arr[0], arr[1], arr[2](), arr[0]); } expect: { var arr = [ 1, 2, function() { return ++this[0]; }, ]; console.log(arr[0], arr[1], arr[2](), arr[0]); } expect_stdout: "1 2 2 2" } unsafe_evaluate_equality_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unsafe: true, unused: true, } input: { function f0() { var a = {}; return a === a; } function f1() { var a = []; return a === a; } console.log(f0(), f1()); } expect: { function f0() { return true; } function f1() { return true; } console.log(f0(), f1()); } expect_stdout: true } unsafe_evaluate_equality_2: { options = { collapse_vars: true, evaluate: true, passes: 2, reduce_funcs: true, reduce_vars: true, unsafe: true, unused: true, } input: { function f2() { var a = {a:1, b:2}; var b = a; var c = a; return b === c; } function f3() { var a = [1, 2, 3]; var b = a; var c = a; return b === c; } console.log(f2(), f3()); } expect: { function f2() { return true; } function f3() { return true; } console.log(f2(), f3()); } expect_stdout: true } passes: { options = { conditionals: true, evaluate: true, passes: 2, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { (function() { var a = 1, b = 2, c = 3; if (a) { b = c; } else { c = b; } console.log(a + b); console.log(b + c); console.log(a + c); console.log(a + b + c); })(); } expect: { (function() { console.log(4), console.log(6), console.log(4), console.log(7); })(); } expect_stdout: [ "4", "6", "4", "7", ] } iife: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { !function(a, b, c) { b++; console.log(a - 1, b * 1, c + 2); }(1, 2, 3); } expect: { !function(a, b, c) { b++; console.log(0, 3, 5); }(1, 2, 3); } expect_stdout: true } iife_new: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { var A = new function(a, b, c) { b++; console.log(a - 1, b * 1, c + 2); }(1, 2, 3); } expect: { var A = new function(a, b, c) { b++; console.log(0, 3, 5); }(1, 2, 3); } expect_stdout: true } multi_def_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { function f(a) { if (a) var b = 1; else var b = 2; console.log(b + 1); } } expect: { function f(a) { if (a) var b = 1; else var b = 2; console.log(b + 1); } } } multi_def_2: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { function f(){ if (code == 16) var bitsLength = 2, bitsOffset = 3, what = len; else if (code == 17) var bitsLength = 3, bitsOffset = 3, what = (len = 0); else if (code == 18) var bitsLength = 7, bitsOffset = 11, what = (len = 0); var repeatLength = this.getBits(bitsLength) + bitsOffset; } } expect: { function f(){ if (16 == code) var bitsLength = 2, bitsOffset = 3, what = len; else if (17 == code) var bitsLength = 3, bitsOffset = 3, what = (len = 0); else if (18 == code) var bitsLength = 7, bitsOffset = 11, what = (len = 0); var repeatLength = this.getBits(bitsLength) + bitsOffset; } } } multi_def_3: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { function f(a) { var b = 2; if (a) var b; else var b; console.log(b + 1); } } expect: { function f(a) { var b = 2; if (a) var b; else var b; console.log(3); } } } use_before_var: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { function f(){ console.log(t); var t = 1; } } expect: { function f(){ console.log(t); var t = 1; } } } inner_var_if: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { function f(a){ if (a) var t = 1; if (!t) console.log(t); } } expect: { function f(a){ if (a) var t = 1; if (!t) console.log(t); } } } inner_var_label: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { function f(a){ l: { if (a) break l; var t = 1; } console.log(t); } } expect: { function f(a){ l: { if (a) break l; var t = 1; } console.log(t); } } } inner_var_for_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { function f() { var a = 1; x(a, b, d); for (var b = 2, c = 3; x(a, b, c, d); x(a, b, c, d)) { var d = 4, e = 5; x(a, b, c, d, e); } x(a, b, c, d, e); } } expect: { function f() { var a = 1; x(1, b, d); for (var b = 2, c = 3; x(1, b, 3, d); x(1, b, 3, d)) { var d = 4, e = 5; x(1, b, 3, d, e); } x(1, b, 3, d, e); } } } inner_var_for_2: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { var a = 1; for (var b = 1; --b;) var a = 2; console.log(a); }(); } expect: { !function() { var a = 1; for (var b = 1; --b;) a = 2; console.log(a); }(); } expect_stdout: "1" } inner_var_for_in_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { function f() { var a = 1, b = 2; for (b in (function() { return x(a, b, c); })()) { var c = 3, d = 4; x(a, b, c, d); } x(a, b, c, d); } } expect: { function f() { var a = 1, b = 2; for (b in (function() { return x(1, b, c); })()) { var c = 3, d = 4; x(1, b, c, d); } x(1, b, c, d); } } } inner_var_for_in_2: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { function f() { for (var long_name in {}) console.log(long_name); } } expect: { function f() { for (var long_name in {}) console.log(long_name); } } } inner_var_catch: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { function f() { try { a(); } catch (e) { var b = 1; } console.log(b); } } expect: { function f() { try { a(); } catch (e) { var b = 1; } console.log(b); } } } issue_1533_1: { options = { collapse_vars: true, reduce_funcs: true, reduce_vars: true, } input: { function f() { var id = ""; for (id in {break: "me"}) console.log(id); } } expect: { function f() { var id = ""; for (id in {break: "me"}) console.log(id); } } } issue_1533_2: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { function f() { var id = ""; for (var id in {break: "me"}) console.log(id); console.log(id); } } expect: { function f() { var id = ""; for (var id in {break: "me"}) console.log(id); console.log(id); } } } toplevel_on: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var x = 3; console.log(x); } expect: { console.log(3); } expect_stdout: true } toplevel_off: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: false, unused: true, } input: { var x = 3; console.log(x); } expect: { var x = 3; console.log(x); } expect_stdout: true } toplevel_on_loops_1: { options = { evaluate: true, loops: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function bar() { console.log("bar:", --x); } var x = 3; do bar(); while (x); } expect: { function bar() { console.log("bar:", --x); } var x = 3; for (;bar(), x;); } expect_stdout: true } toplevel_off_loops_1: { options = { evaluate: true, loops: true, reduce_funcs: true, reduce_vars: true, toplevel: false, unused: true, } input: { function bar() { console.log("bar:", --x); } var x = 3; do bar(); while (x); } expect: { function bar() { console.log("bar:", --x); } var x = 3; for (;bar(), x;); } expect_stdout: true } toplevel_on_loops_2: { options = { evaluate: true, loops: true, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { function bar() { console.log("bar:"); } var x = 3; do bar(); while (x); } expect: { function bar() { console.log("bar:"); } for (;;) bar(); } } toplevel_off_loops_2: { options = { evaluate: true, loops: true, reduce_funcs: true, reduce_vars: true, toplevel: false, unused: true, } input: { function bar() { console.log("bar:"); } var x = 3; do bar(); while (x); } expect: { function bar() { console.log("bar:"); } var x = 3; for (;bar(), x;); } } toplevel_on_loops_3: { options = { evaluate: true, loops: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var x = 3; while (x) bar(); } expect: { for (;;) bar(); } } toplevel_off_loops_3: { options = { evaluate: true, loops: true, reduce_funcs: true, reduce_vars: true, toplevel: false, unused: true, } input: { var x = 3; while (x) bar(); } expect: { var x = 3; for (;x;) bar(); } } defun_reference: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { function f() { function g() { x(); return a; } var a = h(); var b = 2; return a + b; function h() { y(); return b; } } } expect: { function f() { function g() { x(); return a; } var a = h(); var b = 2; return a + b; function h() { y(); return b; } } } } defun_inline_1: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f() { return g(2) + h(); function g(b) { return b; } function h() { return h(); } } } expect: { function f() { return function(b) { return b; }(2) + function h() { return h(); }(); } } } defun_inline_2: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f() { function g(b) { return b; } function h() { return h(); } return g(2) + h(); } } expect: { function f() { return function(b) { return b; }(2) + function h() { return h(); }(); } } } defun_inline_3: { options = { evaluate: true, inline: true, passes: 3, reduce_funcs: true, reduce_vars: true, side_effects: true, unused: true, } input: { function f() { return g(2); function g(b) { return b; } } } expect: { function f() { return 2; } } } defun_call: { options = { inline: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f() { return g() + h(1) - h(g(), 2, 3); function g() { return 4; } function h(a) { return a; } } } expect: { function f() { return 4 + h(1) - h(4); function h(a) { return a; } } } } defun_redefine: { options = { inline: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f() { function g() { return 1; } function h() { return 2; } g = function() { return 3; }; return g() + h(); } console.log(f()); } expect: { function f() { (function() { return 3; }); return 3 + 2; } console.log(f()); } expect_stdout: "5" } func_inline: { options = { inline: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f() { var g = function() { return 1; }; console.log(g() + h()); var h = function() { return 2; }; } } expect: { function f() { console.log(1 + h()); var h = function() { return 2; }; } } } func_modified: { options = { inline: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f(a) { function a() { return 1; } function b() { return 2; } function c() { return 3; } b.inject = []; c = function() { return 4; }; return a() + b() + c(); } console.log(f()); } expect: { function f(a) { function b() { return 2; } b.inject = []; (function() { return 4; }); return 1 + 2 + 4; } console.log(f()); } expect_stdout: "7" } unused_modified: { options = { reduce_vars: true, unused: true, } input: { console.log(function() { var b = 1, c = "FAIL"; if (0 || b--) c = "PASS"; b = 1; return c; }()); } expect: { console.log(function() { var b = 1, c = "FAIL"; if (0 || b--) c = "PASS"; b = 1; return c; }()); } expect_stdout: "PASS" } defun_label: { options = { passes: 2, reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { function f(a) { L: { if (a) break L; return 1; } } console.log(f(2)); }(); } expect: { !function() { console.log(function(a) { L: { if (2) break L; return 1; } }()); }(); } expect_stdout: true } double_reference: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f() { var g = function g() { g(); }; g(); } } expect: { function f() { (function g() { g(); })(); } } } iife_arguments_1: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function(x) { console.log(x() === arguments[0]); })(function f() { return f; }); } expect: { (function(x) { console.log(x() === arguments[0]); })(function f() { return f; }); } expect_stdout: true } iife_arguments_2: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function() { var x = function f() { return f; }; console.log(x() === arguments[0]); })(); } expect: { (function() { console.log(function f() { return f; }() === arguments[0]); })(); } expect_stdout: true } iife_eval_1: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function(x) { console.log(x() === eval("x")); })(function f() { return f; }); } expect: { (function(x) { console.log(x() === eval("x")); })(function f() { return f; }); } expect_stdout: true } iife_eval_2: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function() { var x = function f() { return f; }; console.log(x() === eval("x")); })(); } expect: { (function() { var x = function f() { return f; }; console.log(x() === eval("x")); })(); } expect_stdout: true } iife_func_side_effects: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { function x() { console.log("x"); } function y() { console.log("y"); } function z() { console.log("z"); } (function(a, b, c) { function y() { console.log("FAIL"); } return y + b(); })(x(), function() { return y(); }, z()); } expect: { function x() { console.log("x"); } function y() { console.log("y"); } function z() { console.log("z"); } (function(a, b, c) { return function() { console.log("FAIL"); } + b(); })(x(), function() { return y(); }, z()); } expect_stdout: [ "x", "z", "y", ] } issue_1595_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function f(a) { return f(a + 1); })(2); } expect: { (function f(a) { return f(a + 1); })(2); } } issue_1595_2: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function f(a) { return g(a + 1); })(2); } expect: { (function(a) { return g(a + 1); })(2); } } issue_1595_3: { options = { evaluate: true, passes: 2, reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function f(a) { return g(a + 1); })(2); } expect: { (function(a) { return g(3); })(); } } issue_1595_4: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function iife(a, b, c) { console.log(a, b, c); if (a) iife(a - 1, b, c); })(3, 4, 5); } expect: { (function iife(a, b, c) { console.log(a, b, c); if (a) iife(a - 1, b, c); })(3, 4, 5); } expect_stdout: true } issue_1606: { options = { evaluate: true, hoist_vars: true, reduce_funcs: true, reduce_vars: true, } input: { function f() { var a; function g(){}; var b = 2; x(b); } } expect: { function f() { var a, b; function g(){}; b = 2; x(b); } } } issue_1670_1: { options = { comparisons: true, conditionals: true, dead_code: true, evaluate: true, reduce_funcs: true, reduce_vars: true, side_effects: true, switches: true, typeofs: true, unused: true, } input: { (function f() { switch (1) { case 0: var a = true; break; default: if (typeof a === "undefined") console.log("PASS"); else console.log("FAIL"); } })(); } expect: { (function() { var a; void 0 === a ? console.log("PASS") : console.log("FAIL"); })(); } expect_stdout: "PASS" } issue_1670_2: { options = { conditionals: true, dead_code: true, evaluate: true, passes: 2, reduce_funcs: true, reduce_vars: true, side_effects: true, switches: true, unused: true, } input: { (function f() { switch (1) { case 0: var a = true; break; default: if (typeof a === "undefined") console.log("PASS"); else console.log("FAIL"); } })(); } expect: { (function() { console.log("PASS"); })(); } expect_stdout: "PASS" } issue_1670_3: { options = { comparisons: true, conditionals: true, dead_code: true, evaluate: true, reduce_funcs: true, reduce_vars: true, side_effects: true, switches: true, typeofs: true, unused: true, } input: { (function f() { switch (1) { case 0: var a = true; break; case 1: if (typeof a === "undefined") console.log("PASS"); else console.log("FAIL"); } })(); } expect: { (function() { var a; void 0 === a ? console.log("PASS") : console.log("FAIL"); })(); } expect_stdout: "PASS" } issue_1670_4: { options = { conditionals: true, dead_code: true, evaluate: true, passes: 2, reduce_funcs: true, reduce_vars: true, side_effects: true, switches: true, unused: true, } input: { (function f() { switch (1) { case 0: var a = true; break; case 1: if (typeof a === "undefined") console.log("PASS"); else console.log("FAIL"); } })(); } expect: { (function() { console.log("PASS"); })(); } expect_stdout: "PASS" } issue_1670_5: { options = { dead_code: true, evaluate: true, keep_fargs: false, reduce_funcs: true, reduce_vars: true, side_effects: true, switches: true, unused: true, } input: { (function(a) { switch (1) { case a: console.log(a); break; default: console.log(2); break; } })(1); } expect: { (function() { console.log(1); })(); } expect_stdout: "1" } issue_1670_6: { options = { dead_code: true, evaluate: true, keep_fargs: false, reduce_funcs: true, reduce_vars: true, side_effects: true, switches: true, unused: true, } input: { (function(a) { switch (1) { case a = 1: console.log(a); break; default: console.log(2); break; } })(1); } expect: { (function(a) { switch (1) { case a = 1: console.log(a); break; default: console.log(2); } })(1); } expect_stdout: "1" } unary_delete: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { var b = 10; function f() { var a; if (delete a) b--; } f(); console.log(b); } expect: { var b = 10; function f() { var a; if (delete a) b--; } f(); console.log(b); } expect_stdout: true } redefine_arguments_1: { options = { evaluate: true, keep_fargs: false, reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f() { var arguments; return typeof arguments; } function g() { var arguments = 42; return typeof arguments; } function h(x) { var arguments = x; return typeof arguments; } console.log(f(), g(), h()); } expect: { function f() { var arguments; return typeof arguments; } function g() { return "number"; } function h(x) { var arguments = x; return typeof arguments; } console.log(f(), g(), h()); } expect_stdout: "object number undefined" } redefine_arguments_2: { options = { evaluate: true, inline: true, keep_fargs: false, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { function f() { var arguments; return typeof arguments; } function g() { var arguments = 42; return typeof arguments; } function h(x) { var arguments = x; return typeof arguments; } console.log(f(), g(), h()); } expect: { console.log(function() { var arguments; return typeof arguments; }(), "number", function(x) { var arguments = x; return typeof arguments; }()); } expect_stdout: "object number undefined" } redefine_arguments_3: { options = { evaluate: true, inline: true, keep_fargs: false, passes: 3, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { function f() { var arguments; return typeof arguments; } function g() { var arguments = 42; return typeof arguments; } function h(x) { var arguments = x; return typeof arguments; } console.log(f(), g(), h()); } expect: { console.log(function() { var arguments; return typeof arguments; }(), "number", function(x) { var arguments = x; return typeof arguments; }()); } expect_stdout: "object number undefined" } redefine_farg_1: { options = { evaluate: true, keep_fargs: false, reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f(a) { var a; return typeof a; } function g(a) { var a = 42; return typeof a; } function h(a, b) { var a = b; return typeof a; } console.log(f([]), g([]), h([])); } expect: { function f(a) { return typeof a; } function g() { return "number"; } function h(a, b) { a = b; return typeof a; } console.log(f([]), g([]), h([])); } expect_stdout: "object number undefined" } redefine_farg_2: { options = { evaluate: true, inline: true, keep_fargs: false, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { function f(a) { var a; return typeof a; } function g(a) { var a = 42; return typeof a; } function h(a, b) { var a = b; return typeof a; } console.log(f([]), g([]), h([])); } expect: { console.log((a = [], typeof a), "number",function(a, b) { a = b; return typeof a; }([])); var a; } expect_stdout: "object number undefined" } redefine_farg_3: { options = { collapse_vars: true, evaluate: true, inline: true, keep_fargs: false, passes: 3, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, unused: true, } input: { function f(a) { var a; return typeof a; } function g(a) { var a = 42; return typeof a; } function h(a, b) { var a = b; return typeof a; } console.log(f([]), g([]), h([])); } expect: { console.log(typeof [], "number", "undefined"); } expect_stdout: "object number undefined" } delay_def: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f() { return a; var a; } function g() { return a; var a = 1; } console.log(f(), g()); } expect: { function f() { return; } function g() { return a; var a = 1; } console.log(f(), g()); } expect_stdout: true } delay_def_lhs: { options = { evaluate: true, reduce_vars: true, } input: { console.log(function() { long_name++; return long_name; var long_name; }()); } expect: { console.log(function() { long_name++; return long_name; var long_name; }()); } expect_stdout: "NaN" } booleans: { options = { booleans: true, evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { console.log(function(a) { if (a != 0); switch (a) { case 0: return "FAIL"; case false: return "PASS"; } }(false)); } expect: { console.log(function(a) { if (0); switch (!1) { case 0: return "FAIL"; case !1: return "PASS"; } }(!1)); } expect_stdout: "PASS" } side_effects_assign: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, } input: { var a = typeof void (a && a.in == 1, 0); console.log(a); } expect: { var a = typeof void (a && a.in); console.log(a); } expect_stdout: "undefined" } pure_getters_1: { options = { pure_getters: "strict", reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, } input: { try { var a = (a.b, 2); } catch (e) {} console.log(a); } expect: { try { var a = (a.b, 2); } catch (e) {} console.log(a); } expect_stdout: "undefined" } pure_getters_2: { options = { pure_getters: "strict", reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var a; var a = a && a.b; } expect: { var a = a && a.b; } } pure_getters_3: { options = { pure_getters: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var a; var a = a && a.b; } expect: { } } catch_var: { options = { booleans: true, evaluate: true, reduce_funcs: true, reduce_vars: true, } input: { try { throw {}; } catch (e) { var e; console.log(!!e); } } expect: { try { throw {}; } catch (e) { var e; console.log(!!e); } } expect_stdout: "true" } var_assign_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { !function() { var a; a = 2; console.log(a); }(); } expect: { !function() { console.log(2); }(); } expect_stdout: "2" } var_assign_2: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { !function() { var a; if (a = 2) console.log(a); }(); } expect: { !function() { if (2) console.log(2); }(); } expect_stdout: "2" } var_assign_3: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { !function() { var a; while (a = 2); console.log(a); }(); } expect: { !function() { var a; while (a = 2); console.log(a); }(); } } var_assign_4: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { !function a() { a = 2; console.log(a); }(); } expect: { !function a() { a = 2, console.log(a); }(); } } var_assign_5: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { !function() { var a; !function(b) { a = 2; console.log(a, b); }(a); }(); } expect: { !function() { var a; !function(b) { a = 2, console.log(a, b); }(a); }(); } expect_stdout: "2 undefined" } var_assign_6: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { var a = function(){}(a = 1); console.log(a); }(); } expect: { !function() { var a = function(){}(a = 1); console.log(a); }(); } expect_stdout: "undefined" } immutable: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { var a = "test"; console.log(a.indexOf("e")); }(); } expect: { !function() { console.log("test".indexOf("e")); }(); } expect_stdout: "1" } issue_1814_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var a = 42; !function() { var b = a; !function(a) { console.log(a++, b); }(0); }(); } expect: { !function() { !function(a) { console.log(a++, 42); }(0); }(); } expect_stdout: "0 42" } issue_1814_2: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var a = "32"; !function() { var b = a + 1; !function(a) { console.log(b, a++); }(0); }(); } expect: { !function() { !function(a) { console.log("321", a++); }(0); }(); } expect_stdout: "321 0" } try_abort: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { try { var a = 1; throw ""; var b = 2; } catch (e) { } console.log(a, b); }(); } expect: { !function() { try { var a = 1; throw ""; var b = 2; } catch (e) { } console.log(a, b); }(); } expect_stdout: "1 undefined" } boolean_binary_assign: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { var a; void 0 && (a = 1); console.log(a); }(); } expect: { !function() { var a; void 0; console.log(a); }(); } expect_stdout: "undefined" } cond_assign: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { var a; void 0 ? (a = 1) : 0; console.log(a); }(); } expect: { !function() { var a; void 0 ? (a = 1) : 0; console.log(a); }(); } expect_stdout: "undefined" } iife_assign: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { var a = 1, b = 0; !function() { b++; return; a = 2; }(); console.log(a); }(); } expect: { !function() { var a = 1, b = 0; !function() { b++; return; a = 2; }(); console.log(a); }(); } expect_stdout: "1" } issue_1850_1: { options = { reduce_funcs: true, reduce_vars: true, toplevel: false, unused: true, } input: { function f() { console.log(a, a, a); } var a = 1; f(); } expect: { function f() { console.log(a, a, a); } var a = 1; f(); } expect_stdout: true } issue_1850_2: { options = { reduce_funcs: true, reduce_vars: true, toplevel: "funcs", unused: true, } input: { function f() { console.log(a, a, a); } var a = 1; f(); } expect: { var a = 1; (function() { console.log(a, a, a); })(); } expect_stdout: true } issue_1850_3: { options = { reduce_funcs: true, reduce_vars: true, toplevel: "vars", unused: true, } input: { function f() { console.log(a, a, a); } var a = 1; f(); } expect: { function f() { console.log(a, a, a); } var a = 1; f(); } expect_stdout: true } issue_1850_4: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function f() { console.log(a, a, a); } var a = 1; f(); } expect: { var a = 1; (function() { console.log(a, a, a); })(); } expect_stdout: true } issue_1865: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unsafe: true, } input: { function f(some) { some.thing = false; } console.log(function() { var some = { thing: true }; f(some); return some.thing; }()); } expect: { function f(some) { some.thing = false; } console.log(function() { var some = { thing: true }; f(some); return some.thing; }()); } expect_stdout: true } issue_1922_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { console.log(function(a) { arguments[0] = 2; return a; }(1)); } expect: { console.log(function(a) { arguments[0] = 2; return a; }(1)); } expect_stdout: "2" } issue_1922_2: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, unused: true, } input: { console.log(function() { var a; eval("a = 1"); return a; }(1)); } expect: { console.log(function() { var a; eval("a = 1"); return a; }(1)); } expect_stdout: "1" } accessor_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, } input: { var a = 1; console.log({ get a() { a = 2; return a; }, b: 1 }.b, a); } expect: { var a = 1; console.log({ get a() { a = 2; return a; }, b: 1 }.b, a); } expect_stdout: "1 1" } accessor_2: { options = { collapse_vars: true, evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var A = 1; var B = { get c() { console.log(A); } }; B.c; } expect: { ({ get c() { console.log(1); } }).c; } expect_stdout: "1" } for_in_prop: { options = { reduce_funcs: true, reduce_vars: true, } input: { var a = { foo: function() { for (this.b in [1, 2]); } }; a.foo(); console.log(a.b); } expect: { var a = { foo: function() { for (this.b in [1, 2]); } }; a.foo(); console.log(a.b); } expect_stdout: "1" } obj_var_1: { options = { evaluate: true, passes: 2, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var C = 1; var obj = { bar: function() { return C + C; } }; console.log(obj.bar()); } expect: { console.log({ bar: function() { return 2; } }.bar()); } expect_stdout: "2" } obj_var_2: { options = { evaluate: true, inline: true, passes: 2, properties: true, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unsafe: true, unused: true, } input: { var C = 1; var obj = { bar: function() { return C + C; } }; console.log(obj.bar()); } expect: { console.log(2); } expect_stdout: "2" } obj_arg_1: { options = { collapse_vars: true, evaluate: true, inline: true, passes: 2, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var C = 1; function f(obj) { return obj.bar(); } console.log(f({ bar: function() { return C + C; } })); } expect: { console.log({ bar: function() { return 2; } }.bar()); } expect_stdout: "2" } obj_arg_2: { options = { collapse_vars: true, evaluate: true, inline: true, passes: 3, properties: true, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { var C = 1; function f(obj) { return obj.bar(); } console.log(f({ bar: function() { return C + C; } })); } expect: { console.log(2); } expect_stdout: "2" } func_arg_1: { options = { evaluate: true, inline: true, passes: 2, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { var a = 42; !function(a) { console.log(a()); }(function() { return a; }); } expect: { console.log(42); } expect_stdout: "42" } func_arg_2: { options = { evaluate: true, inline: true, passes: 2, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { var a = 42; !function(a) { console.log(a()); }(function(a) { return a; }); } expect: { console.log(void 0); } expect_stdout: "undefined" } regex_loop: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function f(x) { for (var r, s = "acdabcdeabbb"; r = x().exec(s);) console.log(r[0]); } var a = /ab*/g; f(function() { return a; }); } expect: { var a = /ab*/g; (function(x) { for (var r, s = "acdabcdeabbb"; r = x().exec(s);) console.log(r[0]); })(function() { return a; }); } expect_stdout: true } obj_for_1: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var o = { a: 1 }; for (var i = o.a--; i; i--) console.log(i); } expect: { for (var i = { a: 1 }.a--; i; i--) console.log(i); } expect_stdout: "1" } obj_for_2: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var o = { a: 1 }; for (var i; i = o.a--;) console.log(i); } expect: { var o = { a: 1 }; for (var i; i = o.a--;) console.log(i); } expect_stdout: "1" } array_forin_1: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var a = [ 1, 2, 3 ]; for (var b in a) console.log(b); } expect: { for (var b in [ 1, 2, 3 ]) console.log(b); } expect_stdout: [ "0", "1", "2", ] } array_forin_2: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var a = []; for (var b in [ 1, 2, 3 ]) a.push(b); console.log(a.length); } expect: { var a = []; for (var b in [ 1, 2, 3 ]) a.push(b); console.log(a.length); } expect_stdout: "3" } const_expr_1: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unsafe: true, unused: true, } input: { var o = { a: 1, b: 2 }; o.a++; console.log(o.a, o.b); } expect: { var o = { a: 1, b: 2 }; o.a++; console.log(o.a, o.b); } expect_stdout: "2 2" } const_expr_2: { options = { evaluate: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unsafe: true, unused: true, } input: { Object.prototype.c = function() { this.a++; }; var o = { a: 1, b: 2 }; o.c(); console.log(o.a, o.b); } expect: { Object.prototype.c = function() { this.a++; }; var o = { a: 1, b: 2 }; o.c(); console.log(o.a, o.b); } expect_stdout: "2 2" } escaped_prop_1: { options = { collapse_vars: true, evaluate: true, inline: true, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unsafe: true, unused: true, } input: { var obj = { o: { a: 1 } }; (function(o) { o.a++; })(obj.o); (function(o) { console.log(o.a); })(obj.o); } expect: { var obj = { o: { a: 1 } }; obj.o.a++; console.log(obj.o.a); } expect_stdout: "2" } escaped_prop_2: { options = { collapse_vars: true, evaluate: true, inline: true, passes: 2, pure_getters: "strict", reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unsafe: true, unused: true, } input: { var obj = { o: { a: 1 } }; (function(o) { o.a++; })(obj.o); (function(o) { console.log(o.a); })(obj.o); } expect: { var obj = { o: { a: 1 } }; obj.o.a++; console.log(obj.o.a); } expect_stdout: "2" } escaped_prop_3: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var a; function f(b) { if (a) console.log(a === b.c); a = b.c; } function g() {} function h() { f({ c: g }); } h(); h(); } expect: { var a; function g() {} function h() { (function(b) { if (a) console.log(a === b.c); a = b.c; })({ c: g }); } h(); h(); } expect_stdout: "true" } issue_2420_1: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { function run() { var self = this; if (self.count++) self.foo(); else self.bar(); } var o = { count: 0, foo: function() { console.log("foo"); }, bar: function() { console.log("bar"); }, }; run.call(o); run.call(o); } expect: { function run() { if (this.count++) this.foo(); else this.bar(); } var o = { count: 0, foo: function() { console.log("foo"); }, bar: function() { console.log("bar"); }, }; run.call(o); run.call(o); } expect_stdout: [ "bar", "foo", ] } issue_2420_2: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { function f() { var that = this; if (that.bar) that.foo(); else !function(that, self) { console.log(this === that, self === this, that === self); }(that, this); } f.call({ bar: 1, foo: function() { console.log("foo", this.bar); }, }); f.call({}); } expect: { function f() { if (this.bar) this.foo(); else !function(that, self) { console.log(this === that, self === this, that === self); }(this, this); } f.call({ bar: 1, foo: function() { console.log("foo", this.bar); }, }); f.call({}); } expect_stdout: [ "foo 1", "false false true", ] } issue_2423_1: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function c() { return 1; } function p() { console.log(c()); } p(); p(); } expect: { function p() { console.log(function() { return 1; }()); } p(); p(); } expect_stdout: [ "1", "1", ] } issue_2423_2: { options = { inline: true, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function c() { return 1; } function p() { console.log(c()); } p(); p(); } expect: { function p() { console.log(1); } p(); p(); } expect_stdout: [ "1", "1", ] } issue_2423_3: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function c() { return 1; } function p() { console.log(c()); } p(); } expect: { (function() { console.log(function() { return 1; }()); })(); } expect_stdout: "1" } issue_2423_4: { options = { inline: true, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { function c() { return 1; } function p() { console.log(c()); } p(); } expect: { console.log(1); } expect_stdout: "1" } issue_2423_5: { options = { inline: true, passes: 2, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { function x() { y(); } function y() { console.log(1); } function z() { function y() { console.log(2); } x(); } z(); z(); } expect: { function z() { console.log(1); } z(); z(); } expect_stdout: [ "1", "1", ] } issue_2423_6: { options = { inline: true, passes: 2, reduce_funcs: true, reduce_vars: true, side_effects: true, toplevel: true, unused: true, } input: { function x() { y(); } function y() { console.log(1); } function z() { function y() { console.log(2); } x(); y(); } z(); z(); } expect: { function z(){ console.log(1); console.log(2); } z(); z(); } expect_stdout: [ "1", "2", "1", "2", ] } issue_2440_eval_1: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function foo() { return bar(); } baz = { quux: foo }; exec = function() { return eval("foo()"); }; } expect: { function foo() { return bar(); } baz = { quux: foo }; exec = function() { return eval("foo()"); }; } } issue_2440_eval_2: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { baz = { quux: foo }; exec = function() { return eval("foo()"); }; function foo() { return bar(); } } expect: { baz = { quux: foo }; exec = function() { return eval("foo()"); }; function foo() { return bar(); } } } issue_2440_with_1: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function foo() { return bar(); } baz = { quux: foo }; with (o) whatever(); } expect: { function foo() { return bar(); } baz = { quux: foo }; with (o) whatever(); } } issue_2440_with_2: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { baz = { quux: foo }; with (o) whatever(); function foo() { return bar(); } } expect: { baz = { quux: foo }; with (o) whatever(); function foo() { return bar(); } } } issue_2442: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function foo() { foo(); } } expect: {} } recursive_inlining_1: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { function foo() { bar(); } function bar() { foo(); } console.log("PASS"); }(); } expect: { !function() { console.log("PASS"); }(); } expect_stdout: "PASS" } recursive_inlining_2: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { function foo() { qux(); } function bar() { foo(); } function qux() { bar(); } console.log("PASS"); }(); } expect: { !function() { console.log("PASS"); }(); } expect_stdout: "PASS" } recursive_inlining_3: { options = { passes: 2, reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { function foo(x) { console.log("foo", x); if (x) bar(x-1); } function bar(x) { console.log("bar", x); if (x) qux(x-1); } function qux(x) { console.log("qux", x); if (x) foo(x-1); } qux(4); }(); } expect: { !function() { function qux(x) { console.log("qux", x); if (x) (function(x) { console.log("foo", x); if (x) (function(x) { console.log("bar", x); if (x) qux(x - 1); })(x - 1); })(x - 1); } qux(4); }(); } expect_stdout: [ "qux 4", "foo 3", "bar 2", "qux 1", "foo 0", ] } recursive_inlining_4: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { function foo(x) { console.log("foo", x); if (x) bar(x-1); } function bar(x) { console.log("bar", x); if (x) qux(x-1); } function qux(x) { console.log("qux", x); if (x) foo(x-1); } qux(4); bar(5); }(); } expect: { !function() { function bar(x) { console.log("bar", x); if (x) qux(x - 1); } function qux(x) { console.log("qux", x); if (x) (function(x) { console.log("foo", x); if (x) bar(x - 1); })(x - 1); } qux(4); bar(5); }(); } expect_stdout: [ "qux 4", "foo 3", "bar 2", "qux 1", "foo 0", "bar 5", "qux 4", "foo 3", "bar 2", "qux 1", "foo 0", ] } recursive_inlining_5: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { !function() { function foo(x) { console.log("foo", x); if (x) bar(x-1); } function bar(x) { console.log("bar", x); if (x) qux(x-1); } function qux(x) { console.log("qux", x); if (x) foo(x-1); } qux(4); bar(5); foo(3); }(); } expect: { !function() { function foo(x) { console.log("foo", x); if (x) bar(x - 1); } function bar(x) { console.log("bar", x); if (x) qux(x - 1); } function qux(x) { console.log("qux", x); if (x) foo(x - 1); } qux(4); bar(5); foo(3); }(); } expect_stdout: [ "qux 4", "foo 3", "bar 2", "qux 1", "foo 0", "bar 5", "qux 4", "foo 3", "bar 2", "qux 1", "foo 0", "foo 3", "bar 2", "qux 1", "foo 0", ] } issue_2450_1: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function f() {} function g() { return f; } console.log(g() === g()); } expect: { function f() {} function g() { return f; } console.log(g() === g()); } expect_stdout: "true" } issue_2450_2: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function g() { function f() {} return f; } console.log(g() === g()); } expect: { function g() { return function() {}; } console.log(g() === g()); } expect_stdout: "false" } issue_2450_3: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { var x = (function() { function test() { return "foo"; } return function b() { return [1, test]; } })(); console.log(x()[1] === x()[1]); } expect: { var x = (function() { function test() { return "foo"; } return function() { return [1, test]; } })(); console.log(x()[1] === x()[1]); } expect_stdout: "true" } issue_2450_4: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var a; function f(b) { console.log(a === b); a = b; } function g() {} for (var i = 3; --i >= 0;) f(g); } expect: { var a; function f(b) { console.log(a === b); a = b; } function g() {} for (var i = 3; --i >= 0;) f(g); } expect_stdout: [ "false", "true", "true", ] } issue_2450_5: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var a; function f(b) { console.log(a === b); a = b; } function g() {} [1, 2, 3].forEach(function() { f(g); }); } expect: { var a; function g() {} [1, 2, 3].forEach(function() { (function(b) { console.log(a === b); a = b; })(g); }); } expect_stdout: [ "false", "true", "true", ] } issue_2449: { options = { passes: 10, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var a = "PASS"; function f() { return a; } function g() { return f(); } (function() { var a = "FAIL"; if (a == a) console.log(g()); })(); } expect: { var a = "PASS"; function g() { return function() { return a; }(); } (function() { var a = "FAIL"; if (a == a) console.log(g()); })(); } expect_stdout: "PASS" } perf_1: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function foo(x, y, z) { return x < y ? x * y + z : x * z - y; } function indirect_foo(x, y, z) { return foo(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) { sum += indirect_foo(i, i + 1, 3 * i); } console.log(sum); } expect: { function indirect_foo(x, y, z) { return function(x, y, z) { return x < y ? x * y + z : x * z - y; }(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) sum += indirect_foo(i, i + 1, 3 * i); console.log(sum); } expect_stdout: "348150" } perf_2: { options = { reduce_funcs: false, reduce_vars: true, toplevel: true, unused: true, } input: { function foo(x, y, z) { return x < y ? x * y + z : x * z - y; } function indirect_foo(x, y, z) { return foo(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) { sum += indirect_foo(i, i + 1, 3 * i); } console.log(sum); } expect: { function foo(x, y, z) { return x < y ? x * y + z : x * z - y; } function indirect_foo(x, y, z) { return foo(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) sum += indirect_foo(i, i + 1, 3 * i); console.log(sum); } expect_stdout: "348150" } perf_3: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var foo = function(x, y, z) { return x < y ? x * y + z : x * z - y; } var indirect_foo = function(x, y, z) { return foo(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) sum += indirect_foo(i, i + 1, 3 * i); console.log(sum); } expect: { var indirect_foo = function(x, y, z) { return function(x, y, z) { return x < y ? x * y + z : x * z - y; }(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) sum += indirect_foo(i, i + 1, 3 * i); console.log(sum); } expect_stdout: "348150" } perf_4: { options = { reduce_funcs: false, reduce_vars: true, toplevel: true, unused: true, } input: { var foo = function(x, y, z) { return x < y ? x * y + z : x * z - y; } var indirect_foo = function(x, y, z) { return foo(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) sum += indirect_foo(i, i + 1, 3 * i); console.log(sum); } expect: { var foo = function(x, y, z) { return x < y ? x * y + z : x * z - y; } var indirect_foo = function(x, y, z) { return foo(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) sum += indirect_foo(i, i + 1, 3 * i); console.log(sum); } expect_stdout: "348150" } perf_5: { options = { passes: 10, reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function indirect_foo(x, y, z) { function foo(x, y, z) { return x < y ? x * y + z : x * z - y; } return foo(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) { sum += indirect_foo(i, i + 1, 3 * i); } console.log(sum); } expect: { function indirect_foo(x, y, z) { return function(x, y, z) { return x < y ? x * y + z : x * z - y; }(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) sum += indirect_foo(i, i + 1, 3 * i); console.log(sum); } expect_stdout: "348150" } perf_6: { options = { passes: 10, reduce_funcs: false, reduce_vars: true, toplevel: true, unused: true, } input: { function indirect_foo(x, y, z) { function foo(x, y, z) { return x < y ? x * y + z : x * z - y; } return foo(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) { sum += indirect_foo(i, i + 1, 3 * i); } console.log(sum); } expect: { function indirect_foo(x, y, z) { return function(x, y, z) { return x < y ? x * y + z : x * z - y; }(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) sum += indirect_foo(i, i + 1, 3 * i); console.log(sum); } expect_stdout: "348150" } perf_7: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { var indirect_foo = function(x, y, z) { var foo = function(x, y, z) { return x < y ? x * y + z : x * z - y; } return foo(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) sum += indirect_foo(i, i + 1, 3 * i); console.log(sum); } expect: { var indirect_foo = function(x, y, z) { return function(x, y, z) { return x < y ? x * y + z : x * z - y; }(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) sum += indirect_foo(i, i + 1, 3 * i); console.log(sum); } expect_stdout: "348150" } perf_8: { options = { reduce_funcs: false, reduce_vars: true, toplevel: true, unused: true, } input: { var indirect_foo = function(x, y, z) { var foo = function(x, y, z) { return x < y ? x * y + z : x * z - y; } return foo(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) sum += indirect_foo(i, i + 1, 3 * i); console.log(sum); } expect: { var indirect_foo = function(x, y, z) { return function(x, y, z) { return x < y ? x * y + z : x * z - y; }(x, y, z); } var sum = 0; for (var i = 0; i < 100; ++i) sum += indirect_foo(i, i + 1, 3 * i); console.log(sum); } expect_stdout: "348150" } issue_2485: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { var foo = function(bar) { var n = function(a, b) { return a + b; }; var sumAll = function(arg) { return arg.reduce(n, 0); }; var runSumAll = function(arg) { return sumAll(arg); }; bar.baz = function(arg) { var n = runSumAll(arg); return (n.get = 1), n; }; return bar; }; var bar = foo({}); console.log(bar.baz([1, 2, 3])); } expect: { var foo = function(bar) { var n = function(a, b) { return a + b; }; var runSumAll = function(arg) { return function(arg) { return arg.reduce(n, 0); }(arg); }; bar.baz = function(arg) { var n = runSumAll(arg); return (n.get = 1), n; }; return bar; }; var bar = foo({}); console.log(bar.baz([1, 2, 3])); } expect_stdout: "6" } issue_2455: { options = { reduce_vars: true, unused: true, } input: { function foo() { var that = this; for (;;) that.bar(); } } expect: { function foo() { for (;;) this.bar(); } } } escape_conditional: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function main() { var thing = baz(); if (thing !== (thing = baz())) console.log("FAIL"); else console.log("PASS"); } function baz(s) { return s ? foo : bar; } function foo() {} function bar() {} main(); } expect: { function baz(s) { return s ? foo : bar; } function foo() {} function bar() {} (function() { var thing = baz(); if (thing !== (thing = baz())) console.log("FAIL"); else console.log("PASS"); })(); } expect_stdout: "PASS" } escape_sequence: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function main() { var thing = baz(); if (thing !== (thing = baz())) console.log("FAIL"); else console.log("PASS"); } function baz() { return foo, bar; } function foo() {} function bar() {} main(); } expect: { function baz() { return function() {}, bar; } function bar() {} (function() { var thing = baz(); if (thing !== (thing = baz())) console.log("FAIL"); else console.log("PASS"); })(); } expect_stdout: "PASS" } escape_throw: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function main() { var thing = baz(); if (thing !== (thing = baz())) console.log("FAIL"); else console.log("PASS"); } function baz() { try { throw foo; } catch (bar) { return bar; } } function foo() {} main(); } expect: { function baz() { try { throw foo; } catch (bar) { return bar; } } function foo() {} (function() { var thing = baz(); if (thing !== (thing = baz())) console.log("FAIL"); else console.log("PASS"); })(); } expect_stdout: "PASS" } escape_local_conditional: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function main() { var thing = baz(); if (thing !== (thing = baz())) console.log("PASS"); else console.log("FAIL"); } function baz(s) { function foo() {} function bar() {} return s ? foo : bar; } main(); } expect: { function baz(s) { return s ? function() {} : function() {}; } (function() { var thing = baz(); if (thing !== (thing = baz())) console.log("PASS"); else console.log("FAIL"); })(); } expect_stdout: "PASS" } escape_local_sequence: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function main() { var thing = baz(); if (thing !== (thing = baz())) console.log("PASS"); else console.log("FAIL"); } function baz() { function foo() {} function bar() {} return foo, bar; } main(); } expect: { function baz() { return function() {}, function() {}; } (function() { var thing = baz(); if (thing !== (thing = baz())) console.log("PASS"); else console.log("FAIL"); })(); } expect_stdout: "PASS" } escape_local_throw: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function main() { var thing = baz(); if (thing !== (thing = baz())) console.log("PASS"); else console.log("FAIL"); } function baz() { function foo() {} try { throw foo; } catch (bar) { return bar; } } main(); } expect: { function baz() { try { throw function() {}; } catch (bar) { return bar; } } (function() { var thing = baz(); if (thing !== (thing = baz())) console.log("PASS"); else console.log("FAIL"); })(); } expect_stdout: "PASS" } inverted_var: { options = { evaluate: true, inline: true, passes: 3, reduce_vars: true, side_effects: true, unused: true, } input: { console.log(function() { var a = 1; return a; }(), function() { var b; b = 2; return b; }(), function() { c = 3; return c; var c; }(), function(c) { c = 4; return c; }(), function(c) { c = 5; return c; var c; }(), function c() { c = 6; return c; }(), function c() { c = 7; return c; var c; }(), function() { c = 8; return c; var c = "foo"; }()); } expect: { console.log(1, 2, 3, 4, 5, function c() { c = 6; return c; }(), 7, function() { c = 8; return c; var c = "foo"; }()); } expect_stdout: true } defun_single_use_loop: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { for (var x, i = 2; --i >= 0; ) { var y = x; x = f; console.log(x === y); } function f() {}; } expect: { for (var x, i = 2; --i >= 0; ) { var y = x; x = f; console.log(x === y); } function f() {}; } expect_stdout: [ "false", "true", ] } do_while: { options = { evaluate: true, reduce_vars: true, } input: { function f(a) { do { (function() { a && (c = "PASS"); })(); } while (a = 0); } var c = "FAIL"; f(1); console.log(c); } expect: { function f(a) { do { (function() { a && (c = "PASS"); })(); } while (a = 0); } var c = "FAIL"; f(1); console.log(c); } expect_stdout: "PASS" } issue_2598: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function f() {} function g(a) { return a || f; } console.log(g(false) === g(null)); } expect: { function f() {} function g(a) { return a || f; } console.log(g(false) === g(null)); } expect_stdout: "true" } var_if: { options = { evaluate: true, reduce_vars: true, unused: true, } input: { function f() { if (x()) { var a; if (!g) a = true; if (a) g(); } } } expect: { function f() { if (x()) { var a; if (!g) a = true; if (a) g(); } } } } defun_assign: { options = { reduce_vars: true, toplevel: true, } input: { console.log(typeof a); a = 42; console.log(typeof a); function a() {} console.log(typeof a); } expect: { console.log(typeof a); a = 42; console.log(typeof a); function a() {} console.log(typeof a); } expect_stdout: [ "function", "number", "number", ] } defun_var_1: { options = { evaluate: true, reduce_vars: true, toplevel: true, typeofs: true, unused: true, } input: { var a = 42, b; function a() {} function b() {} console.log(typeof a, typeof b); } expect: { console.log("number", "function"); } expect_stdout: "number function" } defun_var_2: { options = { evaluate: true, reduce_vars: true, toplevel: true, typeofs: true, unused: true, } input: { function a() {} function b() {} var a = 42, b; console.log(typeof a, typeof b); } expect: { console.log("number", "function"); } expect_stdout: "number function" } defun_var_3: { options = { evaluate: true, reduce_vars: true, toplevel: true, typeofs: true, unused: true, } input: { function a() {} function b() {} console.log(typeof a, typeof b); var a = 42, b; } expect: { function a() {} console.log(typeof a, "function"); var a = 42; } expect_stdout: "function function" } defun_catch_1: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { function a() {} try { throw 42; } catch (a) { console.log(a); } } expect: { try { throw 42; } catch (a) { console.log(a); } } expect_stdout: "42" } defun_catch_2: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { try { function a() {} throw 42; } catch (a) { console.log(a); } } expect: { try { throw 42; } catch (a) { console.log(a); } } expect_stdout: "42" } defun_catch_3: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { try { throw 42; function a() {} } catch (a) { console.log(a); } } expect: { try { throw 42; } catch (a) { console.log(a); } } expect_stdout: "42" } defun_catch_4: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { try { throw 42; } catch (a) { function a() {} console.log(a); } } expect: { try { throw 42; } catch (a) { console.log(a); } } expect_stdout: "42" node_version: "<=4" } defun_catch_5: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { try { throw 42; } catch (a) { console.log(a); function a() {} } } expect: { try { throw 42; } catch (a) { console.log(a); } } expect_stdout: "42" node_version: "<=4" } defun_catch_6: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { try { throw 42; } catch (a) { console.log(a); } function a() {} } expect: { try { throw 42; } catch (a) { console.log(a); } } expect_stdout: "42" } duplicate_lambda_defun_name_1: { options = { reduce_vars: true, } input: { console.log(function f(a) { function f() {} return f.length; }()); } expect: { console.log(function f(a) { function f() {} return f.length; }()); } expect_stdout: "0" } duplicate_lambda_defun_name_2: { options = { passes: 2, reduce_vars: true, unused: true, } input: { console.log(function f(a) { function f() {} return f.length; }()); } expect: { console.log(function(a) { return function() {}.length; }()); } expect_stdout: "0" } issue_2774: { options = { reduce_vars: true, unused: true, } input: { console.log({ get a() { var b; (b = true) && b.c; b = void 0; } }.a); } expect: { console.log({ get a() { var b; (b = true) && b.c; b = void 0; } }.a); } expect_stdout: "undefined" } issue_2799_1: { options = { passes: 2, reduce_funcs: true, reduce_vars: true, unused: true, } input: { console.log(function() { return f; function f(n) { function g(i) { return i && i + g(i - 1); } function h(j) { return g(j); } return h(n); } }()(5)); } expect: { console.log(function() { return function(n) { return function(j) { return function g(i) { return i && i + g(i - 1); }(j); }(n); } }()(5)); } expect_stdout: "15" } issue_2799_2: { options = { reduce_vars: true, unsafe_proto: true, unused: true, } input: { (function() { function foo() { Function.prototype.call.apply(console.log, [ null, "PASS" ]); } foo(); })(); } expect: { (function() { (function() { (function() {}).call.apply(console.log, [ null, "PASS" ]); })(); })(); } expect_stdout: "PASS" } issue_2836: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { function f() { return "FAIL"; } console.log(f()); function f() { return "PASS"; } } expect: { console.log(function() { return "PASS"; }()); } expect_stdout: "PASS" } lvalues_def_1: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { var b = 1; var a = b++, b = NaN; console.log(a, b); } expect: { var b = 1; var a = b++; b = NaN; console.log(a, b); } expect_stdout: "1 NaN" } lvalues_def_2: { options = { reduce_vars: true, toplevel: true, unused: true, } input: { var b = 1; var a = b += 1, b = NaN; console.log(a, b); } expect: { var b = 1; var a = b += 1; b = NaN; console.log(a, b); } expect_stdout: "2 NaN" } chained_assignments: { options = { evaluate: true, inline: true, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, unsafe: true, unused: true, } input: { function f() { var a = [0x5e, 0xad, 0xbe, 0xef]; var b = 0; b |= a[0]; b <<= 8; b |= a[1]; b <<= 8; b |= a[2]; b <<= 8; b |= a[3]; return b; } console.log(f().toString(16)); } expect: { console.log("5eadbeef"); } expect_stdout: "5eadbeef" } issue_2860_1: { options = { dead_code: true, evaluate: true, reduce_vars: true, } input: { console.log(function(a) { return a ^= 1; a ^= 2; }()); } expect: { console.log(function(a) { return 1 ^ a; }()); } expect_stdout: "1" } issue_2860_2: { options = { dead_code: true, evaluate: true, inline: true, passes: 2, reduce_vars: true, } input: { console.log(function(a) { return a ^= 1; a ^= 2; }()); } expect: { console.log(1); } expect_stdout: "1" } issue_2869: { options = { evaluate: true, reduce_vars: true, } input: { var c = "FAIL"; (function f(a) { var a; if (!f) a = 0; if (a) c = "PASS"; })(1); console.log(c); } expect: { var c = "FAIL"; (function f(a) { var a; if (!f) a = 0; if (a) c = "PASS"; })(1); console.log(c); } expect_stdout: "PASS" } issue_2919: { options = { evaluate: true, reduce_vars: true, toplevel: true, unsafe: true, unused: true, } input: { var arr = [ function() {} ]; console.log(typeof arr[0]); } expect: { console.log("function"); } expect_stdout: "function" } issue_2992: { options = { evaluate: true, reduce_vars: true, } input: { var c = "PASS"; (function f(b) { switch (0) { case 0: case b = 1: b && (c = "FAIL"); } })(); console.log(c); } expect: { var c = "PASS"; (function f(b) { switch (0) { case 0: case b = 1: b && (c = "FAIL"); } })(); console.log(c); } expect_stdout: "PASS" } issue_3042_1: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function f() {} var a = [ 1, 2 ].map(function() { return new f(); }); console.log(a[0].constructor === a[1].constructor); } expect: { function f() {} var a = [ 1, 2 ].map(function() { return new f(); }); console.log(a[0].constructor === a[1].constructor); } expect_stdout: "true" } issue_3042_2: { options = { reduce_funcs: true, reduce_vars: true, toplevel: true, unused: true, } input: { function Foo() { this.isFoo = function(o) { return o instanceof Foo; }; } function FooCollection() { this.foos = [1, 1].map(function() { return new Foo(); }); } var fooCollection = new FooCollection(); console.log(fooCollection.foos[0].isFoo(fooCollection.foos[0])); console.log(fooCollection.foos[0].isFoo(fooCollection.foos[1])); console.log(fooCollection.foos[1].isFoo(fooCollection.foos[0])); console.log(fooCollection.foos[1].isFoo(fooCollection.foos[1])); } expect: { function Foo() { this.isFoo = function(o) { return o instanceof Foo; }; } var fooCollection = new function() { this.foos = [1, 1].map(function() { return new Foo(); }); }(); console.log(fooCollection.foos[0].isFoo(fooCollection.foos[0])); console.log(fooCollection.foos[0].isFoo(fooCollection.foos[1])); console.log(fooCollection.foos[1].isFoo(fooCollection.foos[0])); console.log(fooCollection.foos[1].isFoo(fooCollection.foos[1])); } expect_stdout: [ "true", "true", "true", "true", ] } issue_3068_1: { options = { evaluate: true, reduce_vars: true, } input: { (function() { do { continue; var b = "defined"; } while (b && b.c); })(); } expect: { (function() { do { continue; var b = "defined"; } while (b && b.c); })(); } expect_stdout: true } issue_3068_2: { options = { evaluate: true, reduce_vars: true, } input: { (function() { do { try { while ("" == typeof a); } finally { continue; } var b = "defined"; } while (b && b.c); })(); } expect: { (function() { do { try { while ("" == typeof a); } finally { continue; } var b = "defined"; } while (b && b.c); })(); } expect_stdout: true } issue_3110_1: { options = { conditionals: true, evaluate: true, inline: true, passes: 3, properties: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { (function() { function foo() { return isDev ? "foo" : "bar"; } var isDev = true; var obj = { foo: foo }; console.log(foo()); console.log(obj.foo()); })(); } expect: { console.log("foo"), console.log("foo"); } expect_stdout: [ "foo", "foo", ] } issue_3110_2: { options = { conditionals: true, evaluate: true, inline: true, passes: 4, properties: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { (function() { function foo() { return isDev ? "foo" : "bar"; } var isDev = true; console.log(foo()); var obj = { foo: foo }; console.log(obj.foo()); })(); } expect: { console.log("foo"), console.log("foo"); } expect_stdout: [ "foo", "foo", ] } issue_3110_3: { options = { conditionals: true, evaluate: true, inline: true, properties: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { (function() { function foo() { return isDev ? "foo" : "bar"; } console.log(foo()); var isDev = true; var obj = { foo: foo }; console.log(obj.foo()); })(); } expect: { (function() { function foo() { return isDev ? "foo" : "bar"; } console.log(foo()); var isDev = true; var obj = { foo: foo }; console.log(obj.foo()); })(); } expect_stdout: [ "bar", "foo", ] } issue_3113_1: { options = { evaluate: true, reduce_vars: true, } input: { var c = 0; (function() { function f() { while (g()); } var a = f(); function g() { a && a[c++]; } g(a = 1); })(); console.log(c); } expect: { var c = 0; (function() { function f() { while (g()); } var a = f(); function g() { a && a[c++]; } g(a = 1); })(); console.log(c); } expect_stdout: "1" } issue_3113_2: { options = { evaluate: true, reduce_vars: true, } input: { var c = 0; (function() { function f() { while (g()); } var a = f(); function g() { a && a[c++]; } a = 1; g(); })(); console.log(c); } expect: { var c = 0; (function() { function f() { while (g()); } var a = f(); function g() { a && a[c++]; } a = 1; g(); })(); console.log(c); } expect_stdout: "1" } issue_3113_3: { options = { evaluate: true, inline: true, passes: 2, pure_getters: "strict", reduce_vars: true, side_effects: true, unused: true, } input: { var c = 0; (function() { function f() { while (g()); } var a; function g() { a && a[c++]; } g(a = 1); })(); console.log(c); } expect: { var c = 0; c++; console.log(c); } expect_stdout: "1" } issue_3113_4: { options = { evaluate: true, reduce_vars: true, toplevel: true, } input: { var a = 0, b = 0; function f() { b += a; } f(f(), ++a); console.log(a, b); } expect: { var a = 0, b = 0; function f() { b += a; } f(f(), ++a); console.log(a, b); } expect_stdout: "1 1" } issue_3113_5: { options = { evaluate: true, reduce_vars: true, toplevel: true, } input: { function f() { console.log(a); } function g() { f(); } while (g()); var a = 1; f(); } expect: { function f() { console.log(a); } function g() { f(); } while (g()); var a = 1; f(); } expect_stdout: [ "undefined", "1", ] } conditional_nested_1: { options = { evaluate: true, reduce_vars: true, } input: { var a = 1, b = 0; (function f(c) { function g() { c && (c.a = 0); c && (c.a = 0); c && (c[b++] *= 0); } g(a-- && f(g(c = 42))); })(); console.log(b); } expect: { var a = 1, b = 0; (function f(c) { function g() { c && (c.a = 0); c && (c.a = 0); c && (c[b++] *= 0); } g(a-- && f(g(c = 42))); })(); console.log(b); } expect_stdout: "2" } conditional_nested_2: { options = { evaluate: true, reduce_vars: true, } input: { var c = 0; (function(a) { function f() { a && c++; } f(!c && f(), a = 1); })(); console.log(c); } expect: { var c = 0; (function(a) { function f() { a && c++; } f(!c && f(), a = 1); })(); console.log(c); } expect_stdout: "1" } conditional_nested_3: { options = { evaluate: true, reduce_vars: true, } input: { var n = 2, c = 0; (function f(a) { 0 < n-- && g(a = 1); function g() { a && c++; } g(); 0 < n-- && f(); })(); console.log(c); } expect: { var n = 2, c = 0; (function f(a) { 0 < n-- && g(a = 1); function g() { a && c++; } g(); 0 < n-- && f(); })(); console.log(c); } expect_stdout: "2" } issue_2436: { options = { evaluate: true, reduce_vars: true, toplevel: true, unsafe: true, } input: { var c; console.log(((c = { a: 1, b: 2 }).a = 3, { x: c.a, y: c.b })); } expect: { var c; console.log(((c = { a: 1, b: 2 }).a = 3, { x: c.a, y: c.b })); } expect_stdout: true } issue_2916: { options = { collapse_vars: true, evaluate: true, inline: true, passes: 2, reduce_vars: true, side_effects: true, unsafe: true, unused: true, } input: { var c = "FAIL"; (function(b) { (function(d) { d[0] = 1; })(b); +b && (c = "PASS"); })([]); console.log(c); } expect: { var c = "FAIL"; (function(b) { b[0] = 1; +b && (c = "PASS"); })([]); console.log(c); } expect_stdout: "PASS" } issue_3125: { options = { evaluate: true, reduce_vars: true, toplevel: true, unsafe: true, } input: { var o; console.log((function() { this.p++; }.call(o = { p: 6 }), o.p)); } expect: { var o; console.log((function() { this.p++; }.call(o = { p: 6 }), o.p)); } expect_stdout: "7" } issue_3140_1: { options = { reduce_vars: true, unused: true, } input: { (function() { var a; function f() { } f.g = function g() { function h() { console.log(a ? "PASS" : "FAIL"); } a = true; this(); a = false; h.g = g; return h; }; return f; })().g().g(); } expect: { (function() { var a; function f() { } f.g = function g() { function h() { console.log(a ? "PASS" : "FAIL"); } a = true; this(); a = false; h.g = g; return h; }; return f; })().g().g(); } expect_stdout: "PASS" } issue_3140_2: { options = { reduce_vars: true, unused: true, } input: { (function() { var a; function f() { } f.g = function g() { var self = this; function h() { console.log(a ? "PASS" : "FAIL"); } a = true; self(); a = false; h.g = g; return h; }; return f; })().g().g(); } expect: { (function() { var a; function f() { } f.g = function g() { function h() { console.log(a ? "PASS" : "FAIL"); } a = true; this(); a = false; h.g = g; return h; }; return f; })().g().g(); } expect_stdout: "PASS" } issue_3140_3: { options = { reduce_vars: true, unused: true, } input: { (function() { var a; function f() { } f.g = function g() { var self = this; function h() { console.log(a ? "PASS" : "FAIL"); } a = true; (function() { return self; })()(); a = false; h.g = g; return h; }; return f; })().g().g(); } expect: { (function() { var a; function f() { } f.g = function g() { var self = this; function h() { console.log(a ? "PASS" : "FAIL"); } a = true; (function() { return self; })()(); a = false; h.g = g; return h; }; return f; })().g().g(); } expect_stdout: "PASS" } issue_3140_4: { options = { reduce_vars: true, unused: true, } input: { (function() { var a; function f() { } f.g = function g() { var o = { p: this }; function h() { console.log(a ? "PASS" : "FAIL"); } a = true; o.p(); a = false; h.g = g; return h; }; return f; })().g().g(); } expect: { (function() { var a; function f() { } f.g = function g() { var o = { p: this }; function h() { console.log(a ? "PASS" : "FAIL"); } a = true; o.p(); a = false; h.g = g; return h; }; return f; })().g().g(); } expect_stdout: "PASS" } issue_3140_5: { options = { evaluate: true, reduce_vars: true, } input: { var n = 1, c = 0; (function(a) { var b = function() { this; n-- && h(); }(); function h() { b && c++; } h(b = 1); })(); console.log(c); } expect: { var n = 1, c = 0; (function(a) { var b = function() { this; n-- && h(); }(); function h() { b && c++; } h(b = 1); })(); console.log(c); } expect_stdout: "1" } issue_3240_1: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function() { f(1); function f(a) { console.log(a); var g = function() { f(a - 1); }; if (a) g(); } })(); } expect: { (function() { (function f(a) { console.log(a); var g = function() { f(a - 1); }; if (a) g(); })(1); })(); } expect_stdout: [ "1", "0", ] } issue_3240_2: { options = { passes: 2, reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function() { f(1); function f(a) { console.log(a); var g = function() { f(a - 1); }; if (a) g(); } })(); } expect: { (function() { (function f(a) { console.log(a); if (a) (function() { f(a - 1); })(); })(1); })(); } expect_stdout: [ "1", "0", ] } issue_3240_3: { options = { reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function() { f(); function f(b) { if (!f.a) f.a = 0; console.log(f.a.toString()); var g = function() { (b ? function() {} : function() { f.a++; f(1); })(); }; g(); } })(); } expect: { (function() { (function f(b) { if (!f.a) f.a = 0; console.log(f.a.toString()); var g = function() { (b ? function() {} : function() { f.a++; f(1); })(); }; g(); })(); })(); } expect_stdout: [ "0", "1", ] } issue_3240_4: { options = { passes: 2, reduce_funcs: true, reduce_vars: true, unused: true, } input: { (function() { f(); function f(b) { if (!f.a) f.a = 0; console.log(f.a.toString()); var g = function() { (b ? function() {} : function() { f.a++; f(1); })(); }; g(); } })(); } expect: { (function() { (function f(b) { if (!f.a) f.a = 0; console.log(f.a.toString()); (function() { (b ? function() {} : function() { f.a++; f(1); })(); })(); })(); })(); } expect_stdout: [ "0", "1", ] } issues_3267_1: { options = { collapse_vars: true, conditionals: true, dead_code: true, evaluate: true, inline: true, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { (function(x) { x(); })(function() { (function(i) { if (i) return console.log("PASS"); throw "FAIL"; })(Object()); }); } expect: { !function(i) { if (i) return console.log("PASS"); throw "FAIL"; }(Object()); } expect_stdout: "PASS" } issues_3267_2: { options = { collapse_vars: true, conditionals: true, dead_code: true, evaluate: true, inline: true, keep_fargs: false, passes: 2, reduce_vars: true, sequences: true, side_effects: true, unused: true, } input: { (function(x) { x(); })(function() { (function(i) { if (i) return console.log("PASS"); throw "FAIL"; })(Object()); }); } expect: { !function() { if (Object()) return console.log("PASS"); throw "FAIL"; }(); } expect_stdout: "PASS" } issues_3267_3: { options = { collapse_vars: true, conditionals: true, dead_code: true, evaluate: true, inline: true, keep_fargs: false, passes: 2, reduce_vars: true, sequences: true, side_effects: true, unsafe: true, unused: true, } input: { (function(x) { x(); })(function() { (function(i) { if (i) return console.log("PASS"); throw "FAIL"; })(Object()); }); } expect: { console.log("PASS"); } expect_stdout: "PASS" } issue_3297: { options = { reduce_vars: true, unused: true, } input: { (function() { function f() { var a; var b = function a() { console.log(a === b) && f(); }; b(); } f(); })(); } expect: { (function() { (function f() { var b = function a() { console.log(a === b) && f(); }; b(); })(); })(); } expect_stdout: "true" } drop_side_effect_free: { options = { collapse_vars: true, evaluate: true, reduce_vars: true, side_effects: true, toplevel: true, } input: { var a = 123; "" + (a && (a.b = 0) || a); console.log(a); } expect: { var a = 123; a.b = 0; console.log(a); } expect_stdout: "123" } issue_3377: { options = { reduce_vars: true, unused: true, } input: { console.log(function f() { return f[0], (f = 42); }()); } expect: { console.log(function f() { return f[0], (f = 42); }()); } expect_stdout: "42" } UglifyJS2-3.6.3/test/compress/regexp.js000066400000000000000000000074411355252637300177640ustar00rootroot00000000000000regexp_simple: { input: { /rx/ig } expect_exact: "/rx/gi;" } regexp_slashes: { input: { /\\\/rx\/\\/ig } expect_exact: "/\\\\\\/rx\\/\\\\/gi;" } regexp_1: { input: { console.log(JSON.stringify("COMPASS? Overpass.".match(/([Sap]+)/ig))); } expect: { console.log(JSON.stringify("COMPASS? Overpass.".match(/([Sap]+)/gi))); } expect_stdout: '["PASS","pass"]' } regexp_2: { options = { evaluate: true, unsafe: true, } input: { console.log(JSON.stringify("COMPASS? Overpass.".match(new RegExp("([Sap]+)", "ig")))); } expect: { console.log(JSON.stringify("COMPASS? Overpass.".match(/([Sap]+)/gi))); } expect_stdout: '["PASS","pass"]' } issue_3434_1: { options = { evaluate: true, unsafe: true, } beautify = { beautify: true, } input: { var o = { "\n": RegExp("\n"), "\r": RegExp("\r"), "\t": RegExp("\t"), "\b": RegExp("\b"), "\f": RegExp("\f"), "\0": RegExp("\0"), "\x0B": RegExp("\x0B"), "\u2028": RegExp("\u2028"), "\u2029": RegExp("\u2029"), }; for (var c in o) console.log(o[c].test("\\"), o[c].test(c)); } expect_exact: [ "var o = {", ' "\\n": /\\n/,', ' "\\r": /\\r/,', ' "\\t": /\t/,', ' "\\b": /\b/,', ' "\\f": /\f/,', ' "\\0": /\0/,', ' "\\v": /\v/,', ' "\\u2028": /\\u2028/,', ' "\\u2029": /\\u2029/', "};", "", 'for (var c in o) console.log(o[c].test("\\\\"), o[c].test(c));', ] expect_stdout: [ "false true", "false true", "false true", "false true", "false true", "false true", "false true", "false true", "false true", ] } issue_3434_2: { options = { evaluate: true, unsafe: true, } beautify = { beautify: true, } input: { var o = { "\n": RegExp("\\\n"), "\r": RegExp("\\\r"), "\t": RegExp("\\\t"), "\b": RegExp("\\\b"), "\f": RegExp("\\\f"), "\0": RegExp("\\\0"), "\x0B": RegExp("\\\x0B"), "\u2028": RegExp("\\\u2028"), "\u2029": RegExp("\\\u2029"), }; for (var c in o) console.log(o[c].test("\\"), o[c].test(c)); } expect_exact: [ "var o = {", ' "\\n": /\\n/,', ' "\\r": /\\r/,', ' "\\t": /\t/,', ' "\\b": /\b/,', ' "\\f": /\f/,', ' "\\0": /\0/,', ' "\\v": /\v/,', ' "\\u2028": /\\u2028/,', ' "\\u2029": /\\u2029/', "};", "", 'for (var c in o) console.log(o[c].test("\\\\"), o[c].test(c));', ] expect_stdout: [ "false true", "false true", "false true", "false true", "false true", "false true", "false true", "false true", "false true", ] } issue_3434_3: { options = { evaluate: true, unsafe: true, } input: { RegExp("\n"); RegExp("\r"); RegExp("\\n"); RegExp("\\\n"); RegExp("\\\\n"); RegExp("\\\\\n"); RegExp("\\\\\\n"); RegExp("\\\\\\\n"); RegExp("\u2028"); RegExp("\u2029"); RegExp("\n\r\u2028\u2029"); RegExp("\\\nfo\n[\n]o\\bbb"); } expect: { /\n/; /\r/; /\n/; /\n/; /\\n/; /\\\n/; /\\\n/; /\\\n/; /\u2028/; /\u2029/; /\n\r\u2028\u2029/; /\nfo\n[\n]o\bbb/; } } UglifyJS2-3.6.3/test/compress/rename.js000066400000000000000000000357741355252637300177530ustar00rootroot00000000000000mangle_catch: { rename = true options = { ie8: false, toplevel: false, } mangle = { ie8: false, toplevel: false, } input: { var a = "FAIL"; try { throw 1; } catch (args) { a = "PASS"; } console.log(a); } expect_exact: 'var a="FAIL";try{throw 1}catch(o){a="PASS"}console.log(a);' expect_stdout: "PASS" } mangle_catch_ie8: { rename = true options = { ie8: true, toplevel: false, } mangle = { ie8: true, toplevel: false, } input: { var a = "FAIL"; try { throw 1; } catch (args) { a = "PASS"; } console.log(a); } expect_exact: 'var a="FAIL";try{throw 1}catch(args){a="PASS"}console.log(a);' expect_stdout: "PASS" } mangle_catch_var: { rename = true options = { ie8: false, toplevel: false, } mangle = { ie8: false, toplevel: false, } input: { var a = "FAIL"; try { throw 1; } catch (args) { var a = "PASS"; } console.log(a); } expect_exact: 'var a="FAIL";try{throw 1}catch(o){var a="PASS"}console.log(a);' expect_stdout: "PASS" } mangle_catch_var_ie8: { rename = true options = { ie8: true, toplevel: false, } mangle = { ie8: true, toplevel: false, } input: { var a = "FAIL"; try { throw 1; } catch (args) { var a = "PASS"; } console.log(a); } expect_exact: 'var a="FAIL";try{throw 1}catch(args){var a="PASS"}console.log(a);' expect_stdout: "PASS" } mangle_catch_toplevel: { rename = true options = { ie8: false, toplevel: true, } mangle = { ie8: false, toplevel: true, } input: { var a = "FAIL"; try { throw 1; } catch (args) { a = "PASS"; } console.log(a); } expect_exact: 'var c="FAIL";try{throw 1}catch(o){c="PASS"}console.log(c);' expect_stdout: "PASS" } mangle_catch_ie8_toplevel: { rename = true options = { ie8: true, toplevel: true, } mangle = { ie8: true, toplevel: true, } input: { var a = "FAIL"; try { throw 1; } catch (args) { a = "PASS"; } console.log(a); } expect_exact: 'var o="FAIL";try{throw 1}catch(c){o="PASS"}console.log(o);' expect_stdout: "PASS" } mangle_catch_var_toplevel: { rename = true options = { ie8: false, toplevel: true, } mangle = { ie8: false, toplevel: true, } input: { var a = "FAIL"; try { throw 1; } catch (args) { var a = "PASS"; } console.log(a); } expect_exact: 'var r="FAIL";try{throw 1}catch(o){var r="PASS"}console.log(r);' expect_stdout: "PASS" } mangle_catch_var_ie8_toplevel: { rename = true options = { ie8: true, toplevel: true, } mangle = { ie8: true, toplevel: true, } input: { var a = "FAIL"; try { throw 1; } catch (args) { var a = "PASS"; } console.log(a); } expect_exact: 'var o="FAIL";try{throw 1}catch(r){var o="PASS"}console.log(o);' expect_stdout: "PASS" } mangle_catch_redef_1: { rename = true options = { ie8: false, toplevel: false, } mangle = { ie8: false, toplevel: false, } input: { var a = "PASS"; try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'var a="PASS";try{throw"FAIL1"}catch(a){var a="FAIL2"}console.log(a);' expect_stdout: "PASS" } mangle_catch_redef_1_ie8: { rename = true options = { ie8: true, toplevel: false, } mangle = { ie8: true, toplevel: false, } input: { var a = "PASS"; try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'var a="PASS";try{throw"FAIL1"}catch(a){var a="FAIL2"}console.log(a);' expect_stdout: "PASS" } mangle_catch_redef_1_toplevel: { rename = true options = { ie8: false, toplevel: true, } mangle = { ie8: false, toplevel: true, } input: { var a = "PASS"; try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'var o="PASS";try{throw"FAIL1"}catch(o){var o="FAIL2"}console.log(o);' expect_stdout: "PASS" } mangle_catch_redef_1_ie8_toplevel: { rename = true options = { ie8: true, toplevel: true, } mangle = { ie8: true, toplevel: true, } input: { var a = "PASS"; try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'var o="PASS";try{throw"FAIL1"}catch(o){var o="FAIL2"}console.log(o);' expect_stdout: "PASS" } mangle_catch_redef_2: { rename = true options = { ie8: false, toplevel: false, } mangle = { ie8: false, toplevel: false, } input: { try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'try{throw"FAIL1"}catch(a){var a="FAIL2"}console.log(a);' expect_stdout: "undefined" } mangle_catch_redef_2_ie8: { rename = true options = { ie8: true, toplevel: false, } mangle = { ie8: true, toplevel: false, } input: { try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'try{throw"FAIL1"}catch(a){var a="FAIL2"}console.log(a);' expect_stdout: "undefined" } mangle_catch_redef_2_toplevel: { rename = true options = { ie8: false, toplevel: true, } mangle = { ie8: false, toplevel: true, } input: { try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'try{throw"FAIL1"}catch(o){var o="FAIL2"}console.log(o);' expect_stdout: "undefined" } mangle_catch_redef_2_ie8_toplevel: { rename = true options = { ie8: true, toplevel: true, } mangle = { ie8: true, toplevel: true, } input: { try { throw "FAIL1"; } catch (a) { var a = "FAIL2"; } console.log(a); } expect_exact: 'try{throw"FAIL1"}catch(o){var o="FAIL2"}console.log(o);' expect_stdout: "undefined" } issue_2120_1: { rename = true mangle = { ie8: false, } input: { "aaaaaaaa"; var a = 1, b = "FAIL"; try { throw 1; } catch (c) { try { throw 0; } catch (a) { if (c) b = "PASS"; } } console.log(b); } expect: { "aaaaaaaa"; var a = 1, b = "FAIL"; try { throw 1; } catch (t) { try { throw 0; } catch (a) { if (t) b = "PASS"; } } console.log(b); } expect_stdout: "PASS" } issue_2120_2: { rename = true mangle = { ie8: true, } input: { "aaaaaaaa"; var a = 1, b = "FAIL"; try { throw 1; } catch (c) { try { throw 0; } catch (a) { if (c) b = "PASS"; } } console.log(b); } expect: { "aaaaaaaa"; var a = 1, b = "FAIL"; try { throw 1; } catch (c) { try { throw 0; } catch (a) { if (c) b = "PASS"; } } console.log(b); } expect_stdout: "PASS" } function_iife_catch: { rename = true mangle = { ie8: false, } input: { function f(n) { !function() { try { throw 0; } catch (n) { var a = 1; console.log(n, a); } }(); } f(); } expect_exact: "function f(o){!function(){try{throw 0}catch(o){var c=1;console.log(o,c)}}()}f();" expect_stdout: "0 1" } function_iife_catch_ie8: { rename = true mangle = { ie8: true, } input: { function f(n) { !function() { try { throw 0; } catch (n) { var a = 1; console.log(n, a); } }(); } f(); } expect_exact: "function f(c){!function(){try{throw 0}catch(c){var o=1;console.log(c,o)}}()}f();" expect_stdout: "0 1" } function_catch_catch: { rename = true mangle = { ie8: false, } input: { var o = 0; function f() { try { throw 1; } catch (c) { try { throw 2; } catch (o) { var o = 3; console.log(o); } } console.log(o); } f(); } expect_exact: "var o=0;function f(){try{throw 1}catch(o){try{throw 2}catch(c){var c=3;console.log(c)}}console.log(c)}f();" expect_stdout: [ "3", "undefined", ] } function_catch_catch_ie8: { rename = true mangle = { ie8: true, } input: { var o = 0; function f() { try { throw 1; } catch (c) { try { throw 2; } catch (o) { var o = 3; console.log(o); } } console.log(o); } f(); } expect_exact: "var o=0;function f(){try{throw 1}catch(c){try{throw 2}catch(o){var o=3;console.log(o)}}console.log(o)}f();" expect_stdout: [ "3", "undefined", ] } function_do_catch_ie8: { rename = true options = { ie8: true, side_effects: true, unused: true, } mangle = { ie8: true, toplevel: true, } input: { var a = 1, b = 1, c = 0; function d(e) { var f, g, h, i; do { try { try { var j = function q(){}(); } catch (r) { --a && w("ddddddddeeeeeeegggggggggiiiiilllllllnnnnntuuuuuuuuyyyyyyy"); var k, l, m, n, o; --m; --n; --o; } try { i[1]; } catch (s) { var p; switch (function t() { c++; }()) { case j + --p: } } } catch (u) {} } while (--i); b--; } d(); console.log(b, c); } expect: { var u = 1, y = 1, a = 0; function c(c) { var d; do { try { try { var e = void 0; } catch (i) { --u && w("ddddddddeeeeeeegggggggggiiiiilllllllnnnnntuuuuuuuuyyyyyyy"); 0; 0; 0; } try { d[1]; } catch (l) { var g; switch (function n() { a++; }()) { case e + --g: } } } catch (t) {} } while (--d); y--; } c(); console.log(y, a); } expect_stdout: "0 1" } issue_3480: { rename = true, mangle = { ie8: false, toplevel: false, } input: { var d, a, b, c = "FAIL"; (function b() { (function() { try { c = "PASS"; } catch (b) { } })(); })(); console.log(c); } expect: { var d, a, b, c = "FAIL"; (function n() { (function() { try { c = "PASS"; } catch (c) {} })(); })(); console.log(c); } expect_stdout: "PASS" } issue_3480_ie8: { rename = true, mangle = { ie8: true, toplevel: false, } input: { var d, a, b, c = "FAIL"; (function b() { (function() { try { c = "PASS"; } catch (b) { } })(); })(); console.log(c); } expect: { var d, a, b, c = "FAIL"; (function b() { (function() { try { c = "PASS"; } catch (b) {} })(); })(); console.log(c); } expect_stdout: "PASS" } issue_3480_toplevel: { rename = true, mangle = { ie8: false, toplevel: true, } input: { var d, a, b, c = "FAIL"; (function b() { (function() { try { c = "PASS"; } catch (b) { } })(); })(); console.log(c); } expect: { var c, n, o, t = "FAIL"; (function c() { (function() { try { t = "PASS"; } catch (c) {} })(); })(); console.log(t); } expect_stdout: "PASS" } issue_3480_ie8_toplevel: { rename = true, mangle = { ie8: true, toplevel: true, } input: { var d, a, b, c = "FAIL"; (function b() { (function() { try { c = "PASS"; } catch (b) { } })(); })(); console.log(c); } expect: { var c, n, o, t = "FAIL"; (function o() { (function() { try { t = "PASS"; } catch (o) {} })(); })(); console.log(t); } expect_stdout: "PASS" } UglifyJS2-3.6.3/test/compress/return_undefined.js000066400000000000000000000056351355252637300220350ustar00rootroot00000000000000return_undefined: { options = { booleans: true, comparisons: true, conditionals: true, dead_code: true, drop_debugger: true, evaluate: true, hoist_funs: true, hoist_vars: true, if_return: true, join_vars: true, keep_fargs: true, keep_fnames: false, loops: true, negate_iife: true, properties: true, sequences: false, side_effects: true, unused: true, } input: { function f0() { } function f1() { return undefined; } function f2() { return void 0; } function f3() { return void 123; } function f4() { return; } function f5(a, b) { console.log(a, b); baz(a); return; } function f6(a, b) { console.log(a, b); if (a) { foo(b); baz(a); return a + b; } return undefined; } function f7(a, b) { console.log(a, b); if (a) { foo(b); baz(a); return void 0; } return a + b; } function f8(a, b) { foo(a); bar(b); return void 0; } function f9(a, b) { foo(a); bar(b); return undefined; } function f10() { return false; } function f11() { return null; } function f12() { return 0; } } expect: { function f0() {} function f1() {} function f2() {} function f3() {} function f4() {} function f5(a, b) { console.log(a, b); baz(a); } function f6(a, b) { console.log(a, b); if (a) { foo(b); baz(a); return a + b; } } function f7(a, b) { console.log(a, b); if (!a) return a + b; foo(b); baz(a); } function f8(a, b) { foo(a); bar(b); } function f9(a, b) { foo(a); bar(b); } function f10() { return !1; } function f11() { return null; } function f12() { return 0; } } } return_void: { options = { if_return: true, inline: true, reduce_vars: true, unused: true, } input: { function f() { function g() { h(); } return g(); } } expect: { function f() { h(); } } } UglifyJS2-3.6.3/test/compress/sandbox.js000066400000000000000000000030311355252637300201170ustar00rootroot00000000000000console_log: { input: { console.log("%% %s"); console.log("%% %s", "%s"); } expect: { console.log("%% %s"); console.log("%% %s", "%s"); } expect_stdout: [ "%% %s", "% %s", ] } typeof_arguments: { options = { evaluate: true, reduce_vars: true, toplevel: true, unused: true, } input: { var arguments; console.log((typeof arguments).length); } expect: { var arguments; console.log((typeof arguments).length); } expect_stdout: "6" } typeof_arguments_assigned: { options = { evaluate: true, reduce_vars: true, toplevel: true, unused: true, } input: { var arguments = void 0; console.log((typeof arguments).length); } expect: { console.log("undefined".length); } expect_stdout: "9" } toplevel_Infinity_NaN_undefined: { options = { evaluate: true, reduce_vars: true, toplevel: true, unused: true, } input: { var Infinity = "foo"; var NaN = 42; var undefined = null; console.log(Infinity, NaN, undefined); } expect: { console.log("foo", 42, null); } expect_stdout: "foo 42 null" } log_global: { input: { console.log(function() { return this; }()); } expect: { console.log(function() { return this; }()); } expect_stdout: "[object global]" } UglifyJS2-3.6.3/test/compress/sequences.js000066400000000000000000000506111355252637300204620ustar00rootroot00000000000000make_sequences_1: { options = { sequences: true, } input: { foo(); bar(); baz(); } expect: { foo(),bar(),baz(); } } make_sequences_2: { options = { sequences: true, } input: { if (boo) { foo(); bar(); baz(); } else { x(); y(); z(); } } expect: { if (boo) foo(),bar(),baz(); else x(),y(),z(); } } make_sequences_3: { options = { sequences: true, } input: { function f() { foo(); bar(); return baz(); } function g() { foo(); bar(); throw new Error(); } } expect: { function f() { return foo(), bar(), baz(); } function g() { throw foo(), bar(), new Error(); } } } make_sequences_4: { options = { sequences: true, } input: { x = 5; if (y) z(); x = 5; for (i = 0; i < 5; i++) console.log(i); x = 5; for (; i < 5; i++) console.log(i); x = 5; switch (y) {} x = 5; with (obj) {} } expect: { if (x = 5, y) z(); for (x = 5, i = 0; i < 5; i++) console.log(i); for (x = 5; i < 5; i++) console.log(i); switch (x = 5, y) {} with (x = 5, obj); } expect_stdout: true } lift_sequences_1: { options = { sequences: true, } input: { var foo, x, y, bar; foo = !(x(), y(), bar()); } expect: { var foo, x, y, bar; x(), y(), foo = !bar(); } } lift_sequences_2: { options = { evaluate: true, sequences: true, } input: { var foo = 1, bar; foo.x = (foo = {}, 10); bar = (bar = {}, 10); console.log(foo, bar); } expect: { var foo = 1, bar; foo.x = (foo = {}, 10), bar = {}, bar = 10, console.log(foo, bar); } expect_stdout: true } lift_sequences_3: { options = { conditionals: true, sequences: true, } input: { var x, foo, bar, baz; x = (foo(), bar(), baz()) ? 10 : 20; } expect: { var x, foo, bar, baz; foo(), bar(), x = baz() ? 10 : 20; } } lift_sequences_4: { options = { side_effects: true, } input: { var x, foo, bar, baz; x = (foo, bar, baz); } expect: { var x, foo, bar, baz; x = baz; } } lift_sequences_5: { options = { sequences: true, } input: { var a = 2, b; a *= (b, a = 4, 3); console.log(a); } expect: { var a = 2, b; b, a *= (a = 4, 3), console.log(a); } expect_stdout: "6" } for_sequences: { options = { sequences: true, } input: { // 1 foo(); bar(); for (; false;); // 2 foo(); bar(); for (x = 5; false;); // 3 x = (foo in bar); for (; false;); // 4 x = (foo in bar); for (y = 5; false;); // 5 x = function() { foo in bar; }; for (y = 5; false;); } expect: { // 1 for (foo(), bar(); false;); // 2 for (foo(), bar(), x = 5; false;); // 3 x = (foo in bar); for (; false;); // 4 x = (foo in bar); for (y = 5; false;); // 5 for (x = function() { foo in bar; }, y = 5; false;); } } limit_1: { options = { sequences: 3, } input: { a; b; c; d; e; f; g; h; i; j; k; } expect: { a, b, c; d, e, f; g, h, i; j, k; } } limit_2: { options = { sequences: 3, } input: { a, b; c, d; e, f; g, h; i, j; k; } expect: { a, b, c, d; e, f, g, h; i, j, k; } } negate_iife_for: { options = { negate_iife: true, sequences: true, } input: { (function() {})(); for (i = 0; i < 5; i++) console.log(i); (function() {})(); for (; i < 10; i++) console.log(i); } expect: { for (!function() {}(), i = 0; i < 5; i++) console.log(i); for (!function() {}(); i < 10; i++) console.log(i); } expect_stdout: true } iife: { options = { sequences: true, } input: { x = 42; (function a() {})(); !function b() {}(); ~function c() {}(); +function d() {}(); -function e() {}(); void function f() {}(); typeof function g() {}(); } expect: { x = 42, function a() {}(), function b() {}(), function c() {}(), function d() {}(), function e() {}(), function f() {}(), function g() {}(); } } unsafe_undefined: { options = { conditionals: true, if_return: true, sequences: true, side_effects: true, unsafe_undefined: true, } input: { function f(undefined) { if (a) return b; if (c) return d; } function g(undefined) { if (a) return b; if (c) return d; e(); } } expect: { function f(undefined) { return a ? b : c ? d : undefined; } function g(undefined) { return a ? b : c ? d : void e(); } } } issue_1685: { options = { collapse_vars: true, side_effects: true, } input: { var a = 100, b = 10; function f() { var a = (a--, delete a && --b); } f(); console.log(a, b); } expect: { var a = 100, b = 10; function f() { var a = (a--, delete a && --b); } f(); console.log(a, b); } expect_stdout: true } func_def_1: { options = { collapse_vars: true, side_effects: true, } input: { function f() { return f = 0, !!f; } console.log(f()); } expect: { function f() { return !!(f = 0); } console.log(f()); } expect_stdout: "false" } func_def_2: { options = { collapse_vars: true, side_effects: true, } input: { console.log(function f() { return f = 0, !!f; }()); } expect: { console.log(function f() { return f = 0, !!f; }()); } expect_stdout: "true" } func_def_3: { options = { collapse_vars: true, side_effects: true, } input: { function f() { function g() {} return g = 0, !!g; } console.log(f()); } expect: { function f() { function g() {} return !!(g = 0); } console.log(f()); } expect_stdout: "false" } func_def_4: { options = { collapse_vars: true, side_effects: true, } input: { function f() { function g() { return g = 0, !!g; } return g(); } console.log(f()); } expect: { function f() { function g() { return !!(g = 0); } return g(); } console.log(f()); } expect_stdout: "false" } func_def_5: { options = { collapse_vars: true, side_effects: true, } input: { function f() { return function g(){ return g = 0, !!g; }(); } console.log(f()); } expect: { function f() { return function g(){ return g = 0, !!g; }(); } console.log(f()); } expect_stdout: "true" } issue_1758: { options = { sequences: true, side_effects: true, } input: { console.log(function(c) { var undefined = 42; return function() { c--; c--, c.toString(); return; }(); }()); } expect: { console.log(function(c) { var undefined = 42; return function() { return c--, c--, void c.toString(); }(); }()); } expect_stdout: "undefined" } delete_seq_1: { options = { booleans: true, evaluate: true, side_effects: true, } input: { console.log(delete (1, undefined)); console.log(delete (1, void 0)); console.log(delete (1, Infinity)); console.log(delete (1, 1 / 0)); console.log(delete (1, NaN)); console.log(delete (1, 0 / 0)); } expect: { console.log(!0); console.log(!0); console.log(!0); console.log(!0); console.log(!0); console.log(!0); } expect_stdout: true } delete_seq_2: { options = { booleans: true, evaluate: true, side_effects: true, } input: { console.log(delete (1, 2, undefined)); console.log(delete (1, 2, void 0)); console.log(delete (1, 2, Infinity)); console.log(delete (1, 2, 1 / 0)); console.log(delete (1, 2, NaN)); console.log(delete (1, 2, 0 / 0)); } expect: { console.log(!0); console.log(!0); console.log(!0); console.log(!0); console.log(!0); console.log(!0); } expect_stdout: true } delete_seq_3: { options = { booleans: true, evaluate: true, keep_infinity: true, side_effects: true, } input: { console.log(delete (1, 2, undefined)); console.log(delete (1, 2, void 0)); console.log(delete (1, 2, Infinity)); console.log(delete (1, 2, 1 / 0)); console.log(delete (1, 2, NaN)); console.log(delete (1, 2, 0 / 0)); } expect: { console.log(!0); console.log(!0); console.log(!0); console.log(!0); console.log(!0); console.log(!0); } expect_stdout: true } delete_seq_4: { options = { booleans: true, evaluate: true, sequences: true, side_effects: true, } input: { function f() {} console.log(delete (f(), undefined)); console.log(delete (f(), void 0)); console.log(delete (f(), Infinity)); console.log(delete (f(), 1 / 0)); console.log(delete (f(), NaN)); console.log(delete (f(), 0 / 0)); } expect: { function f() {} console.log((f(), !0)), console.log((f(), !0)), console.log((f(), !0)), console.log((f(), !0)), console.log((f(), !0)), console.log((f(), !0)); } expect_stdout: true } delete_seq_5: { options = { booleans: true, evaluate: true, keep_infinity: true, sequences: true, side_effects: true, } input: { function f() {} console.log(delete (f(), undefined)); console.log(delete (f(), void 0)); console.log(delete (f(), Infinity)); console.log(delete (f(), 1 / 0)); console.log(delete (f(), NaN)); console.log(delete (f(), 0 / 0)); } expect: { function f() {} console.log((f(), !0)), console.log((f(), !0)), console.log((f(), !0)), console.log((f(), !0)), console.log((f(), !0)), console.log((f(), !0)); } expect_stdout: true } delete_seq_6: { options = { booleans: true, evaluate: true, side_effects: true, } input: { var a; console.log(delete (1, a)); } expect: { var a; console.log(!0); } expect_stdout: true } side_effects: { options = { sequences: true, side_effects: true, } input: { 0, a(), 1, b(), 2, c(), 3; } expect: { a(), b(), c(); } } side_effects_cascade_1: { options = { collapse_vars: true, conditionals: true, sequences: true, side_effects: true, } input: { function f(a, b) { a -= 42; if (a < 0) a = 0; b.a = a; } } expect: { function f(a, b) { (a -= 42) < 0 && (a = 0), b.a = a; } } } side_effects_cascade_2: { options = { collapse_vars: true, side_effects: true, } input: { function f(a, b) { b = a, !a + (b += a) || (b += a), b = a, b; } } expect: { function f(a, b) { !(b = a) + (b += a) || (b += a), b = a; } } } side_effects_cascade_3: { options = { collapse_vars: true, conditionals: true, side_effects: true, } input: { function f(a, b) { "foo" ^ (b += a), b ? false : (b = a) ? -1 : (b -= a) - (b ^= a), a-- || !a, a; } } expect: { function f(a, b) { (b += a) || (b = a) || (b -= a, b ^= a), a--; } } } issue_27: { options = { collapse_vars: true, passes: 2, sequences: true, side_effects: true, unused: true, } input: { (function(jQuery) { var $; $ = jQuery; $("body").addClass("foo"); })(jQuery); } expect: { (function(jQuery) { jQuery("body").addClass("foo"); })(jQuery); } } issue_2062: { options = { booleans: true, collapse_vars: true, conditionals: true, side_effects: true, } input: { var a = 1; if ([ a || a++ + a--, a++ + a--, a && a.var ]); console.log(a); } expect: { var a = 1; a || (a++, a--), a++, --a && a.var; console.log(a); } expect_stdout: "1" } issue_2313: { options = { collapse_vars: true, sequences: true, side_effects: true, } input: { var a = 0, b = 0; var foo = { get c() { a++; return 42; }, set c(c) { b++; }, d: function() { this.c++; if (this.c) console.log(a, b); } } foo.d(); } expect: { var a = 0, b = 0; var foo = { get c() { return a++, 42; }, set c(c) { b++; }, d: function() { if (this.c++, this.c) console.log(a, b); } } foo.d(); } expect_stdout: "2 1" } cascade_assignment_in_return: { options = { collapse_vars: true, unused: true, } input: { function f(a, b) { return a = x(), b(a); } } expect: { function f(a, b) { return b(x()); } } } hoist_defun: { options = { join_vars: true, sequences: true, } input: { x(); function f() {} y(); } expect: { function f() {} x(), y(); } } hoist_decl: { options = { join_vars: true, sequences: true, } input: { var a; w(); var b = x(); y(); for (var c; 0;) z(); var d; } expect: { var a; w(); var b = x(), c, d; for (y(); 0;) z(); } } for_init_var: { options = { join_vars: true, unused: false, } input: { var a = "PASS"; (function() { var b = 42; for (var c = 5; c > 0;) c--; a = "FAIL"; var a; })(); console.log(a); } expect: { var a = "PASS"; (function() { for (var b = 42, c = 5, a; c > 0;) c--; a = "FAIL"; })(); console.log(a); } expect_stdout: "PASS" } forin: { options = { sequences: true, } input: { var o = []; o.push("PASS"); for (var a in o) console.log(o[a]); } expect: { var o = []; for (var a in o.push("PASS"), o) console.log(o[a]); } expect_stdout: "PASS" } call: { options = { sequences: true, } input: { var a = function() { return this; }(); function b() { console.log("foo"); } b.c = function() { console.log(this === b ? "bar" : "baz"); }; (a, b)(); (a, b.c)(); (a, function() { console.log(this === a); })(); new (a, b)(); new (a, b.c)(); new (a, function() { console.log(this === a); })(); } expect: { var a = function() { return this; }(); function b() { console.log("foo"); } b.c = function() { console.log(this === b ? "bar" : "baz"); }, b(), (a, b.c)(), function() { console.log(this === a); }(), new b(), new b.c(), new function() { console.log(this === a); }(); } expect_stdout: [ "foo", "baz", "true", "foo", "baz", "false", ] } missing_link: { options = { conditionals: true, evaluate: true, sequences: true, } input: { var a = 100; a; a++ + (0 ? 2 : 1); console.log(a); } expect: { var a = 100; a, a++, console.log(a); } } angularjs_chain: { options = { conditionals: true, sequences: true, side_effects: true, } input: { function nonComputedMember(left, right, context, create) { var lhs = left(); if (create && create !== 1) { if (lhs && lhs[right] == null) { lhs[right] = {}; } } var value = lhs != null ? lhs[right] : undefined; if (context) { return { context: lhs, name: right, value: value }; } else { return value; } } } expect: { function nonComputedMember(left, right, context, create) { var lhs = left(); create && 1 !== create && lhs && null == lhs[right] && (lhs[right] = {}); var value = null != lhs ? lhs[right] : void 0; return context ? { context: lhs, name: right, value: value } : value; } } } issue_3490_1: { options = { conditionals: true, dead_code: true, inline: true, sequences: true, side_effects: true, toplevel: true, } input: { var b = 42, c = "FAIL"; if ({ 3: function() { var a; return (a && a.p) < this; }(), }) c = "PASS"; if (b) while ("" == typeof d); console.log(c, b); } expect: { var b = 42, c = "FAIL"; if (function() { var a; a && a.p; }(), c = "PASS", b) while ("" == typeof d); console.log(c, b); } expect_stdout: "PASS 42" } issue_3490_2: { options = { conditionals: true, dead_code: true, evaluate: true, inline: true, reduce_vars: true, sequences: true, side_effects: true, toplevel: true, } input: { var b = 42, c = "FAIL"; if ({ 3: function() { var a; return (a && a.p) < this; }(), }) c = "PASS"; if (b) for (; "" == typeof d;); console.log(c, b); } expect: { var b = 42, c = "FAIL"; for (function() { var a; }(), c = "PASS", b; "" == typeof d;); console.log(c, b); } expect_stdout: "PASS 42" } UglifyJS2-3.6.3/test/compress/string-literal.js000066400000000000000000000012351355252637300214250ustar00rootroot00000000000000octal_escape_sequence: { input: { var boundaries = "\0\7\00\07\70\77\000\077\300\377"; var border_check = "\400\700\0000\3000"; } expect: { var boundaries = "\x00\x07\x00\x07\x38\x3f\x00\x3f\xc0\xff"; var border_check = "\x20\x30\x38\x30\x00\x30\xc0\x30"; } } issue_1929: { input: { function f(s) { return s.split(/[\\/]/); } console.log(JSON.stringify(f("A/B\\C\\D/E\\F"))); } expect: { function f(s) { return s.split(/[\\/]/); } console.log(JSON.stringify(f("A/B\\C\\D/E\\F"))); } expect_stdout: '["A","B","C","D","E","F"]' } UglifyJS2-3.6.3/test/compress/switch.js000066400000000000000000000355371355252637300200020ustar00rootroot00000000000000constant_switch_1: { options = { dead_code: true, evaluate: true, side_effects: true, switches: true, } input: { switch (1+1) { case 1: foo(); break; case 1+1: bar(); break; case 1+1+1: baz(); break; } } expect: { bar(); } } constant_switch_2: { options = { dead_code: true, evaluate: true, side_effects: true, switches: true, } input: { switch (1) { case 1: foo(); case 1+1: bar(); break; case 1+1+1: baz(); } } expect: { foo(); bar(); } } constant_switch_3: { options = { dead_code: true, evaluate: true, side_effects: true, switches: true, } input: { switch (10) { case 1: foo(); case 1+1: bar(); break; case 1+1+1: baz(); default: def(); } } expect: { def(); } } constant_switch_4: { options = { dead_code: true, evaluate: true, side_effects: true, switches: true, } input: { switch (2) { case 1: x(); if (foo) break; y(); break; case 1+1: bar(); default: def(); } } expect: { bar(); def(); } } constant_switch_5: { options = { dead_code: true, evaluate: true, side_effects: true, switches: true, } input: { switch (1) { case 1: x(); if (foo) break; y(); break; case 1+1: bar(); default: def(); } } expect: { // the break inside the if ruins our job // we can still get rid of irrelevant cases. switch (1) { case 1: x(); if (foo) break; y(); } // XXX: we could optimize this better by inventing an outer // labeled block, but that's kinda tricky. } } constant_switch_6: { options = { dead_code: true, evaluate: true, side_effects: true, switches: true, } input: { OUT: { foo(); switch (1) { case 1: x(); if (foo) break OUT; y(); case 1+1: bar(); break; default: def(); } } } expect: { OUT: { foo(); x(); if (foo) break OUT; y(); bar(); } } } constant_switch_7: { options = { dead_code: true, evaluate: true, side_effects: true, switches: true, } input: { OUT: { foo(); switch (1) { case 1: x(); if (foo) break OUT; for (var x = 0; x < 10; x++) { if (x > 5) break; // this break refers to the for, not to the switch; thus it // shouldn't ruin our optimization console.log(x); } y(); case 1+1: bar(); break; default: def(); } } } expect: { OUT: { foo(); x(); if (foo) break OUT; for (var x = 0; x < 10; x++) { if (x > 5) break; console.log(x); } y(); bar(); } } } constant_switch_8: { options = { dead_code: true, evaluate: true, side_effects: true, switches: true, } input: { OUT: switch (1) { case 1: x(); for (;;) break OUT; y(); break; case 1+1: bar(); default: def(); } } expect: { OUT: { x(); for (;;) break OUT; y(); } } } constant_switch_9: { options = { dead_code: true, evaluate: true, side_effects: true, switches: true, } input: { OUT: switch (1) { case 1: x(); for (;;) if (foo) break OUT; y(); case 1+1: bar(); default: def(); } } expect: { OUT: { x(); for (;;) if (foo) break OUT; y(); bar(); def(); } } } drop_default_1: { options = { dead_code: true, switches: true, } input: { switch (foo) { case 'bar': baz(); default: } } expect: { switch (foo) { case 'bar': baz(); } } } drop_default_2: { options = { dead_code: true, switches: true, } input: { switch (foo) { case 'bar': baz(); break; default: break; } } expect: { switch (foo) { case 'bar': baz(); } } } keep_default: { options = { dead_code: true, switches: true, } input: { switch (foo) { case 'bar': baz(); default: something(); break; } } expect: { switch (foo) { case 'bar': baz(); default: something(); } } } issue_1663: { options = { dead_code: true, evaluate: true, side_effects: true, switches: true, } input: { var a = 100, b = 10; function f() { switch (1) { case 1: b = a++; return ++b; default: var b; } } f(); console.log(a, b); } expect: { var a = 100, b = 10; function f() { var b; b = a++; return ++b; } f(); console.log(a, b); } expect_stdout: true } drop_case: { options = { dead_code: true, switches: true, } input: { switch (foo) { case 'bar': baz(); break; case 'moo': break; } } expect: { switch (foo) { case 'bar': baz(); } } } keep_case: { options = { dead_code: true, switches: true, } input: { switch (foo) { case 'bar': baz(); break; case moo: break; } } expect: { switch (foo) { case 'bar': baz(); break; case moo: } } } issue_376: { options = { dead_code: true, evaluate: true, switches: true, } input: { switch (true) { case boolCondition: console.log(1); break; case false: console.log(2); break; } } expect: { switch (true) { case boolCondition: console.log(1); } } } issue_441_1: { options = { dead_code: true, switches: true, } input: { switch (foo) { case bar: qux(); break; case baz: qux(); break; default: qux(); break; } } expect: { switch (foo) { case bar: case baz: default: qux(); } } } issue_441_2: { options = { dead_code: true, switches: true, } input: { switch (foo) { case bar: // TODO: Fold into the case below qux(); break; case fall: case baz: qux(); break; default: qux(); break; } } expect: { switch (foo) { case bar: qux(); break; case fall: case baz: default: qux(); } } } issue_1674: { options = { dead_code: true, evaluate: true, side_effects: true, switches: true, } input: { switch (0) { default: console.log("FAIL"); break; case 0: console.log("PASS"); break; } } expect: { console.log("PASS"); } expect_stdout: "PASS" } issue_1679: { options = { dead_code: true, evaluate: true, switches: true, } input: { var a = 100, b = 10; function f() { switch (--b) { default: case !function x() {}: break; case b--: switch (0) { default: case a--: } break; case (a++): break; } } f(); console.log(a, b); } expect: { var a = 100, b = 10; function f() { switch (--b) { default: case !function x() {}: break; case b--: switch (0) { default: case a--: } break; case (a++): } } f(); console.log(a, b); } expect_stdout: true } issue_1680_1: { options = { dead_code: true, evaluate: true, switches: true, } input: { function f(x) { console.log(x); return x + 1; } switch (2) { case f(0): case f(1): f(2); case 2: case f(3): case f(4): f(5); } } expect: { function f(x) { console.log(x); return x + 1; } switch (2) { case f(0): case f(1): f(2); case 2: f(5); } } expect_stdout: [ "0", "1", "2", "5", ] } issue_1680_2: { options = { dead_code: true, switches: true, } input: { var a = 100, b = 10; switch (b) { case a--: break; case b: var c; break; case a: break; case a--: break; } console.log(a, b); } expect: { var a = 100, b = 10; switch (b) { case a--: break; case b: var c; break; case a: case a--: } console.log(a, b); } expect_stdout: true } issue_1690_1: { options = { dead_code: true, switches: true, } input: { switch (console.log("PASS")) {} } expect: { console.log("PASS"); } expect_stdout: "PASS" } issue_1690_2: { options = { dead_code: false, switches: true, } input: { switch (console.log("PASS")) {} } expect: { switch (console.log("PASS")) {} } expect_stdout: "PASS" } if_switch_typeof: { options = { conditionals: true, dead_code: true, side_effects: true, switches: true, } input: { if (a) switch(typeof b) {} } expect: { a; } } issue_1698: { options = { side_effects: true, switches: true, } input: { var a = 1; !function() { switch (a++) {} }(); console.log(a); } expect: { var a = 1; !function() { switch (a++) {} }(); console.log(a); } expect_stdout: "2" } issue_1705_1: { options = { dead_code: true, switches: true, } input: { var a = 0; switch (a) { default: console.log("FAIL"); case 0: break; } } expect: { var a = 0; switch (a) { default: console.log("FAIL"); case 0: } } expect_stdout: true } issue_1705_2: { options = { dead_code: true, evaluate: true, reduce_funcs: true, reduce_vars: true, sequences: true, side_effects: true, switches: true, toplevel: true, unused: true, } input: { var a = 0; switch (a) { default: console.log("FAIL"); case 0: break; } } expect: { } expect_stdout: true } issue_1705_3: { options = { dead_code: true, switches: true, } input: { switch (a) { case 0: break; default: break; } } expect: { a; } expect_stdout: true } beautify: { beautify = { beautify: true, } input: { switch (a) { case 0: case 1: break; case 2: default: } switch (b) { case 3: foo(); bar(); default: break; } } expect_exact: [ "switch (a) {", " case 0:", " case 1:", " break;", "", " case 2:", " default:", "}", "", "switch (b) {", " case 3:", " foo();", " bar();", "", " default:", " break;", "}", ] } issue_1758: { options = { dead_code: true, switches: true, } input: { var a = 1, b = 2; switch (a--) { default: b++; } console.log(a, b); } expect: { var a = 1, b = 2; a--; b++; console.log(a, b); } expect_stdout: "0 3" } issue_2535: { options = { dead_code: true, evaluate: true, switches: true, } input: { switch(w(), 42) { case 13: x(); case 42: y(); default: z(); } } expect: { w(), 42; 42; y(); z(); } } issue_1750: { options = { dead_code: true, evaluate: true, switches: true, } input: { var a = 0, b = 1; switch (true) { case a, true: default: b = 2; case true: } console.log(a, b); } expect: { var a = 0, b = 1; true; a, true; b = 2; console.log(a, b); } expect_stdout: "0 2" } UglifyJS2-3.6.3/test/compress/transform.js000066400000000000000000000044501355252637300205020ustar00rootroot00000000000000booleans_evaluate: { options = { booleans: true, evaluate: true, } input: { console.log(typeof void 0 != "undefined"); console.log(1 == 1, 1 === 1) console.log(1 != 1, 1 !== 1) } expect: { console.log(!1); console.log(!0, !0); console.log(!1, !1); } expect_stdout: true } booleans_global_defs: { options = { booleans: true, evaluate: true, global_defs: { A: true, }, } input: { console.log(A == 1); } expect: { console.log(!0); } } condition_evaluate: { options = { booleans: true, dead_code: false, evaluate: true, loops: false, } input: { while (1 === 2); for (; 1 == true;); if (void 0 == null); } expect: { while (0); for (; 1;); if (1); } } if_else_empty: { options = { conditionals: true, } input: { if ({} ? a : b); else {} } expect: { ({}), a; } } label_if_break: { options = { conditionals: true, dead_code: true, evaluate: true, side_effects: true, } input: { L: if (true) { a; break L; } } expect: { a; } } while_if_break: { options = { conditionals: true, loops: true, sequences: true, } input: { while (a) { if (b) if(c) d; if (e) break; } } expect: { for(; a && (b && c && d, !e);); } } if_return: { options = { booleans: true, conditionals: true, if_return: true, sequences: true, side_effects: true, } input: { function f(w, x, y, z) { if (x) return; if (w) { if (y) return; } else if (z) return; if (x == y) return true; if (x) w(); if (y) z(); return true; } } expect: { function f(w, x, y, z) { if (!x) { if (w) { if (y) return; } else if (z) return; return x == y || (x && w(), y && z()), !0; } } } } UglifyJS2-3.6.3/test/compress/typeof.js000066400000000000000000000136311355252637300177760ustar00rootroot00000000000000typeof_evaluation: { options = { evaluate: true, typeofs: true, } input: { a = typeof 1; b = typeof 'test'; c = typeof []; d = typeof {}; e = typeof /./; f = typeof false; g = typeof function(){}; h = typeof undefined; } expect: { a='number'; b='string'; c=typeof[]; d=typeof{}; e=typeof/./; f='boolean'; g='function'; h='undefined'; } } typeof_in_boolean_context: { options = { booleans: true, conditionals: true, evaluate: true, side_effects: true, } input: { function f1(x) { return typeof x ? "yes" : "no"; } function f2() { return typeof g()? "Yes" : "No"; } typeof 0 ? foo() : bar(); !typeof console.log(1); var a = !typeof console.log(2); if (typeof (1 + foo())); } expect: { function f1(x) { return "yes"; } function f2() { return g(), "Yes"; } foo(); console.log(1); var a = !(console.log(2), 1); foo(); } } issue_1668: { options = { booleans: true, } input: { if (typeof bar); } expect: { if (1); } } typeof_defun_1: { options = { evaluate: true, inline: true, passes: 2, reduce_vars: true, side_effects: true, toplevel: true, typeofs: true, unused: true, } input: { function f() { console.log("YES"); } function g() { h = 42; console.log("NOPE"); } function h() { console.log("YUP"); } g = 42; "function" == typeof f && f(); "function" == typeof g && g(); "function" == typeof h && h(); } expect: { function h() { console.log("YUP"); } console.log("YES"); h(); } expect_stdout: [ "YES", "YUP", ] } typeof_defun_2: { options = { evaluate: true, reduce_vars: true, toplevel: true, typeofs: true, } input: { var f = function() { console.log(x); }; var x = 0; x++ < 2 && typeof f == "function" && f(); x++ < 2 && typeof f == "function" && f(); x++ < 2 && typeof f == "function" && f(); } expect: { var f = function() { console.log(x); }; var x = 0; x++ < 2 && f(); x++ < 2 && f(); x++ < 2 && f(); } expect_stdout: [ "1", "2", ] } duplicate_defun_arg_name: { options = { evaluate: true, reduce_vars: true, typeofs: true, } input: { function long_name(long_name) { return typeof long_name; } console.log(typeof long_name, long_name()); } expect: { function long_name(long_name) { return typeof long_name; } console.log(typeof long_name, long_name()); } expect_stdout: "function undefined" } duplicate_lambda_arg_name: { options = { evaluate: true, reduce_vars: true, typeofs: true, } input: { console.log(function long_name(long_name) { return typeof long_name; }()); } expect: { console.log(function long_name(long_name) { return typeof long_name; }()); } expect_stdout: "undefined" } issue_2728_1: { options = { evaluate: true, reduce_vars: true, typeofs: true, } input: { (function arguments() { console.log(typeof arguments); })(); } expect: { (function arguments() { console.log(typeof arguments); })(); } expect_stdout: "object" } issue_2728_2: { options = { evaluate: true, reduce_vars: true, typeofs: true, } input: { function arguments() { return typeof arguments; } console.log(typeof arguments, arguments()); } expect: { function arguments() { return typeof arguments; } console.log(typeof arguments, arguments()); } expect_stdout: "function object" } issue_2728_3: { options = { evaluate: true, reduce_vars: true, typeofs: true, } input: { (function() { function arguments() { } console.log(typeof arguments); })(); } expect: { (function() { function arguments() { } console.log("function"); })(); } expect_stdout: "function" } issue_2728_4: { options = { evaluate: true, reduce_vars: true, toplevel: true, typeofs: true, } input: { function arguments() { } console.log(typeof arguments); } expect: { function arguments() { } console.log("function"); } expect_stdout: "function" } issue_2728_5: { options = { evaluate: true, reduce_vars: true, typeofs: true, } input: { (function arguments(arguments) { console.log(typeof arguments); })(); } expect: { (function arguments(arguments) { console.log(typeof arguments); })(); } expect_stdout: "undefined" } issue_2728_6: { options = { evaluate: true, reduce_vars: true, typeofs: true, } input: { function arguments(arguments) { return typeof arguments; } console.log(typeof arguments, arguments()); } expect: { function arguments(arguments) { return typeof arguments; } console.log(typeof arguments, arguments()); } expect_stdout: "function undefined" } UglifyJS2-3.6.3/test/compress/unicode.js000066400000000000000000000026751355252637300201240ustar00rootroot00000000000000unicode_parse_variables: { options = {} input: { var a = {}; a.你好 = 456; var ↂωↂ = 123; var l০ = 3; // 2nd char is a unicode digit } expect: { var a = {}; a.你好 = 456; var ↂωↂ = 123; var l০ = 3; } } issue_2242_1: { beautify = { ascii_only: false, } input: { console.log("\ud83d", "\ude00", "\ud83d\ude00", "\ud83d@\ude00"); } expect_exact: 'console.log("\\ud83d","\\ude00","\ud83d\ude00","\\ud83d@\\ude00");' } issue_2242_2: { beautify = { ascii_only: true, } input: { console.log("\ud83d", "\ude00", "\ud83d\ude00", "\ud83d@\ude00"); } expect_exact: 'console.log("\\ud83d","\\ude00","\\ud83d\\ude00","\\ud83d@\\ude00");' } issue_2242_3: { options = { evaluate: false, } input: { console.log("\ud83d" + "\ude00", "\ud83d" + "@" + "\ude00"); } expect_exact: 'console.log("\\ud83d"+"\\ude00","\\ud83d"+"@"+"\\ude00");' } issue_2242_4: { options = { evaluate: true, } input: { console.log("\ud83d" + "\ude00", "\ud83d" + "@" + "\ude00"); } expect_exact: 'console.log("\ud83d\ude00","\\ud83d@\\ude00");' } issue_2569: { input: { new RegExp("[\udc42-\udcaa\udd74-\udd96\ude45-\ude4f\udea3-\udecc]"); } expect_exact: 'new RegExp("[\\udc42-\\udcaa\\udd74-\\udd96\\ude45-\\ude4f\\udea3-\\udecc]");' } UglifyJS2-3.6.3/test/compress/webkit.js000066400000000000000000000042671355252637300177620ustar00rootroot00000000000000lambda_call_dot_assign: { beautify = { webkit: false, } input: { console.log(function() { return {}; }().a = 1); } expect_exact: "console.log(function(){return{}}().a=1);" expect_stdout: "1" } lambda_call_dot_assign_webkit: { beautify = { webkit: true, } input: { console.log(function() { return {}; }().a = 1); } expect_exact: "console.log((function(){return{}}()).a=1);" expect_stdout: "1" } lambda_dot_assign: { beautify = { webkit: false, } input: { console.log(function() { 1 + 1; }.a = 1); } expect_exact: "console.log(function(){1+1}.a=1);" expect_stdout: "1" } lambda_dot_assign_webkit: { beautify = { webkit: true, } input: { console.log(function() { 1 + 1; }.a = 1); } expect_exact: "console.log((function(){1+1}).a=1);" expect_stdout: "1" } lambda_name_mangle: { mangle = {} input: { console.log(typeof function foo(bar) {}); } expect_exact: "console.log(typeof function o(n){});" expect_stdout: "function" } lambda_name_mangle_ie8: { mangle = { ie8: true, toplevel: true, } input: { console.log(typeof function foo(bar) {}); } expect_exact: "console.log(typeof function n(o){});" expect_stdout: "function" } function_name_mangle: { options = { keep_fnames: true, reduce_vars: true, unused: true, } mangle = {} input: { (function() { function foo(bar) {} console.log(typeof foo); })(); } expect_exact: "(function(){console.log(typeof function o(n){})})();" expect_stdout: "function" } function_name_mangle_ie8: { options = { keep_fnames: true, reduce_vars: true, unused: true, } mangle = { ie8: true, toplevel: true, } input: { (function() { function foo(bar) {} console.log(typeof foo); })(); } expect_exact: "(function(){console.log(typeof function n(o){})})();" expect_stdout: "function" } UglifyJS2-3.6.3/test/compress/wrap_iife.js000066400000000000000000000016721355252637300204370ustar00rootroot00000000000000wrap_iife: { options = { negate_iife: false, } beautify = { wrap_iife: true, } input: { (function() { return function() { console.log('test') }; })()(); } expect_exact: '(function(){return function(){console.log("test")}})()();' } wrap_iife_in_expression: { options = { negate_iife: false, } beautify = { wrap_iife: true, } input: { foo = (function() { return bar(); })(); } expect_exact: 'foo=(function(){return bar()})();' } wrap_iife_in_return_call: { options = { negate_iife: false, } beautify = { wrap_iife: true, } input: { (function() { return (function() { console.log('test') })(); })()(); } expect_exact: '(function(){return(function(){console.log("test")})()})()();' } UglifyJS2-3.6.3/test/exports.js000066400000000000000000000010321355252637300163310ustar00rootroot00000000000000exports["Compressor"] = Compressor; exports["defaults"] = defaults; exports["JS_Parse_Error"] = JS_Parse_Error; exports["mangle_properties"] = mangle_properties; exports["minify"] = minify; exports["OutputStream"] = OutputStream; exports["parse"] = parse; exports["push_uniq"] = push_uniq; exports["reserve_quoted_keys"] = reserve_quoted_keys; exports["string_template"] = string_template; exports["to_ascii"] = to_ascii; exports["tokenizer"] = tokenizer; exports["TreeTransformer"] = TreeTransformer; exports["TreeWalker"] = TreeWalker; UglifyJS2-3.6.3/test/fetch.js000066400000000000000000000015651355252637300157310ustar00rootroot00000000000000var fs = require("fs"); var parse = require("url").parse; var path = require("path"); try { fs.mkdirSync("./tmp"); } catch (e) { if (e.code != "EEXIST") throw e; } function local(url) { return path.join("./tmp", encodeURIComponent(url)); } function read(url) { return fs.createReadStream(local(url)); } module.exports = function(url, callback) { var result = read(url); result.on("error", function(e) { if (e.code != "ENOENT") return callback(e); var options = parse(url); options.rejectUnauthorized = false; require(options.protocol.slice(0, -1)).get(options, function(res) { if (res.statusCode !== 200) return callback(res.statusCode); res.pipe(fs.createWriteStream(local(url))); callback(null, res); }); }).on("open", function() { callback(null, result); }); }; UglifyJS2-3.6.3/test/input/000077500000000000000000000000001355252637300154325ustar00rootroot00000000000000UglifyJS2-3.6.3/test/input/comments/000077500000000000000000000000001355252637300172575ustar00rootroot00000000000000UglifyJS2-3.6.3/test/input/comments/filter.js000066400000000000000000000000341355252637300210770ustar00rootroot00000000000000// foo /*@preserve*/ // bar UglifyJS2-3.6.3/test/input/enclose/000077500000000000000000000000001355252637300170625ustar00rootroot00000000000000UglifyJS2-3.6.3/test/input/enclose/input.js000066400000000000000000000001031355252637300205510ustar00rootroot00000000000000function enclose() { console.log("test enclose"); } enclose(); UglifyJS2-3.6.3/test/input/global_defs/000077500000000000000000000000001355252637300176735ustar00rootroot00000000000000UglifyJS2-3.6.3/test/input/global_defs/nested.js000066400000000000000000000000271355252637300215120ustar00rootroot00000000000000console.log(C.V, C.D); UglifyJS2-3.6.3/test/input/global_defs/simple.js000066400000000000000000000000201355252637300215120ustar00rootroot00000000000000console.log(D); UglifyJS2-3.6.3/test/input/invalid/000077500000000000000000000000001355252637300170605ustar00rootroot00000000000000UglifyJS2-3.6.3/test/input/invalid/assign_1.js000066400000000000000000000000271355252637300211210ustar00rootroot00000000000000console.log(1 || 5--); UglifyJS2-3.6.3/test/input/invalid/assign_2.js000066400000000000000000000000501355252637300211160ustar00rootroot00000000000000console.log(2 || (Math.random() /= 2)); UglifyJS2-3.6.3/test/input/invalid/assign_3.js000066400000000000000000000000321355252637300211170ustar00rootroot00000000000000console.log(3 || ++this); UglifyJS2-3.6.3/test/input/invalid/assign_4.js000066400000000000000000000000071355252637300211220ustar00rootroot00000000000000++null UglifyJS2-3.6.3/test/input/invalid/delete.js000066400000000000000000000002711355252637300206600ustar00rootroot00000000000000function f(x) { delete 42; delete (0, x); delete null; delete x; } function g(x) { "use strict"; delete 42; delete (0, x); delete null; delete x; } UglifyJS2-3.6.3/test/input/invalid/dot_1.js000066400000000000000000000000041355252637300204160ustar00rootroot00000000000000a.= UglifyJS2-3.6.3/test/input/invalid/dot_2.js000066400000000000000000000000051355252637300204200ustar00rootroot00000000000000%.a; UglifyJS2-3.6.3/test/input/invalid/dot_3.js000066400000000000000000000000071355252637300204230ustar00rootroot00000000000000a./(); UglifyJS2-3.6.3/test/input/invalid/else.js000066400000000000000000000000171355252637300203440ustar00rootroot00000000000000if (0) else 1; UglifyJS2-3.6.3/test/input/invalid/eof.js000066400000000000000000000000121355252637300201600ustar00rootroot00000000000000foo, bar( UglifyJS2-3.6.3/test/input/invalid/for-in_1.js000066400000000000000000000001041355252637300210230ustar00rootroot00000000000000var a, b = [1, 2]; for (1, 2, a in b) { console.log(a, b[a]); } UglifyJS2-3.6.3/test/input/invalid/for-in_2.js000066400000000000000000000001021355252637300210220ustar00rootroot00000000000000var c = [1, 2]; for (var a, b in c) { console.log(a, c[a]); } UglifyJS2-3.6.3/test/input/invalid/function_1.js000066400000000000000000000001071355252637300214610ustar00rootroot00000000000000function f(arguments) { } function g(arguments) { "use strict"; } UglifyJS2-3.6.3/test/input/invalid/function_2.js000066400000000000000000000001001355252637300214530ustar00rootroot00000000000000function arguments() { } function eval() { "use strict"; } UglifyJS2-3.6.3/test/input/invalid/function_3.js000066400000000000000000000001101355252637300214550ustar00rootroot00000000000000!function eval() { }(); !function arguments() { "use strict"; }(); UglifyJS2-3.6.3/test/input/invalid/loop-no-body.js000066400000000000000000000000351355252637300217320ustar00rootroot00000000000000for (var i = 0; i < 1; i++) UglifyJS2-3.6.3/test/input/invalid/object.js000066400000000000000000000000251355252637300206610ustar00rootroot00000000000000console.log({%: 1}); UglifyJS2-3.6.3/test/input/invalid/return.js000066400000000000000000000000131355252637300207270ustar00rootroot00000000000000return 42; UglifyJS2-3.6.3/test/input/invalid/simple.js000066400000000000000000000000171355252637300207050ustar00rootroot00000000000000function f(a{} UglifyJS2-3.6.3/test/input/invalid/tab.js000066400000000000000000000000231355252637300201570ustar00rootroot00000000000000 foo( xyz, 0abc); UglifyJS2-3.6.3/test/input/invalid/try.js000066400000000000000000000001531355252637300202330ustar00rootroot00000000000000function f() { try {} catch (eval) {} } function g() { "use strict"; try {} catch (eval) {} } UglifyJS2-3.6.3/test/input/invalid/var.js000066400000000000000000000001211355252637300202000ustar00rootroot00000000000000function f() { var eval; } function g() { "use strict"; var eval; } UglifyJS2-3.6.3/test/input/issue-1236/000077500000000000000000000000001355252637300171535ustar00rootroot00000000000000UglifyJS2-3.6.3/test/input/issue-1236/simple.js000066400000000000000000000002001355252637300207720ustar00rootroot00000000000000"use strict"; var foo = function foo(x) { return "foo " + x; }; console.log(foo("bar")); //# sourceMappingURL=simple.js.map UglifyJS2-3.6.3/test/input/issue-1236/simple.js.map000066400000000000000000000004371355252637300215620ustar00rootroot00000000000000{ "version": 3, "sources": ["index.js"], "names": [], "mappings": ";;AAAA,IAAI,MAAM,SAAN,GAAM;AAAA,SAAK,SAAS,CAAd;AAAA,CAAV;AACA,QAAQ,GAAR,CAAY,IAAI,KAAJ,CAAZ", "file": "simple.js", "sourcesContent": ["let foo = x => \"foo \" + x;\nconsole.log(foo(\"bar\"));"] } UglifyJS2-3.6.3/test/input/issue-1242/000077500000000000000000000000001355252637300171505ustar00rootroot00000000000000UglifyJS2-3.6.3/test/input/issue-1242/bar.es5000066400000000000000000000001051355252637300203260ustar00rootroot00000000000000function bar(x) { var triple = x * (2 + 1); return triple; } UglifyJS2-3.6.3/test/input/issue-1242/baz.es5000066400000000000000000000000731355252637300203420ustar00rootroot00000000000000function baz(x) { var half = x / 2; return half; } UglifyJS2-3.6.3/test/input/issue-1242/foo.es5000066400000000000000000000001541355252637300203510ustar00rootroot00000000000000var print = console.log.bind(console); function foo(x) { var twice = x * 2; print('Foo:', twice); } UglifyJS2-3.6.3/test/input/issue-1242/qux.js000066400000000000000000000001141355252637300203170ustar00rootroot00000000000000var x = bar(1+2); var y = baz(3+9); print('q' + 'u' + 'x', x, y); foo(5+6); UglifyJS2-3.6.3/test/input/issue-1323/000077500000000000000000000000001355252637300171505ustar00rootroot00000000000000UglifyJS2-3.6.3/test/input/issue-1323/sample.js000066400000000000000000000001411355252637300207630ustar00rootroot00000000000000var bar = (function() { function foo (bar) { return bar; } return foo; })();UglifyJS2-3.6.3/test/input/issue-1431/000077500000000000000000000000001355252637300171505ustar00rootroot00000000000000UglifyJS2-3.6.3/test/input/issue-1431/sample.js000066400000000000000000000003121355252637300207630ustar00rootroot00000000000000function f(x) { return function() { function n(a) { return a * a; } return x(n); }; } function g(op) { return op(1) + op(2); } console.log(f(g)() == 5);UglifyJS2-3.6.3/test/input/issue-1482/000077500000000000000000000000001355252637300171565ustar00rootroot00000000000000UglifyJS2-3.6.3/test/input/issue-1482/braces.js000066400000000000000000000014361355252637300207570ustar00rootroot00000000000000if (x) { foo(); } if (x) { foo(); } else { baz(); } if (x) { foo(); } else if (y) { bar(); } else { baz(); } if (x) { if (y) { foo(); } else { bar(); } } else { baz(); } if (x) { foo(); } else if (y) { bar(); } else if (z) { baz(); } else { moo(); } function f() { if (x) { foo(); } if (x) { foo(); } else { baz(); } if (x) { foo(); } else if (y) { bar(); } else { baz(); } if (x) { if (y) { foo(); } else { bar(); } } else { baz(); } if (x) { foo(); } else if (y) { bar(); } else if (z) { baz(); } else { moo(); } } UglifyJS2-3.6.3/test/input/issue-1482/default.js000066400000000000000000000006561355252637300211470ustar00rootroot00000000000000if (x) foo(); if (x) foo(); else baz(); if (x) foo(); else if (y) bar(); else baz(); if (x) if (y) foo(); else bar(); else baz(); if (x) foo(); else if (y) bar(); else if (z) baz(); else moo(); function f() { if (x) foo(); if (x) foo(); else baz(); if (x) foo(); else if (y) bar(); else baz(); if (x) if (y) foo(); else bar(); else baz(); if (x) foo(); else if (y) bar(); else if (z) baz(); else moo(); } UglifyJS2-3.6.3/test/input/issue-1482/input.js000066400000000000000000000006251355252637300206560ustar00rootroot00000000000000if (x) foo(); if (x) foo(); else baz(); if (x) foo(); else if (y) bar(); else baz(); if (x) if (y) foo(); else bar(); else baz(); if (x) foo(); else if (y) bar(); else if (z) baz(); else moo(); function f() { if (x) foo(); if (x) foo(); else baz(); if (x) foo(); else if (y) bar(); else baz(); if (x) if (y) foo(); else bar(); else baz(); if (x) foo(); else if (y) bar(); else if (z) baz(); else moo(); } UglifyJS2-3.6.3/test/input/issue-1632/000077500000000000000000000000001355252637300171535ustar00rootroot00000000000000UglifyJS2-3.6.3/test/input/issue-1632/^{foo}[bar](baz)+$.js000066400000000000000000000000171355252637300227320ustar00rootroot00000000000000console.log(x);UglifyJS2-3.6.3/test/input/issue-2082/000077500000000000000000000000001355252637300171535ustar00rootroot00000000000000UglifyJS2-3.6.3/test/input/issue-2082/sample.js000066400000000000000000000000171355252637300207700ustar00rootroot00000000000000console.log(x);UglifyJS2-3.6.3/test/input/issue-2082/sample.js.map000066400000000000000000000000671355252637300215510ustar00rootroot00000000000000{"version": 3,"sources": ["index.js"],"mappings": ";"} UglifyJS2-3.6.3/test/input/issue-2310/000077500000000000000000000000001355252637300171455ustar00rootroot00000000000000UglifyJS2-3.6.3/test/input/issue-2310/input.js000066400000000000000000000002001355252637300206320ustar00rootroot00000000000000function foo() { return function() { console.log("PASS"); }; } (function() { var f = foo(); f(); })(); UglifyJS2-3.6.3/test/input/issue-3040/000077500000000000000000000000001355252637300171465ustar00rootroot00000000000000UglifyJS2-3.6.3/test/input/issue-3040/expect.js000066400000000000000000000012251355252637300207740ustar00rootroot00000000000000function _toConsumableArray(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i= 0) { args.splice(debug, 1); debug = true; } else { debug = false; } if (!args.length) { args.push("-mcb", "beautify=false,webkit"); } args.push("--timings"); var child_process = require("child_process"); var fetch = require("./fetch"); var http = require("http"); var server = http.createServer(function(request, response) { request.resume(); var url = site + request.url; fetch(url, function(err, res) { if (err) { if (typeof err != "number") throw err; response.writeHead(err); response.end(); } else { response.writeHead(200, { "Content-Type": { css: "text/css", js: "application/javascript", png: "image/png" }[url.slice(url.lastIndexOf(".") + 1)] || "text/html; charset=utf-8" }); if (/\.js$/.test(url)) { var stderr = ""; var uglifyjs = child_process.fork("bin/uglifyjs", args, { silent: true }).on("exit", function(code) { console.log("uglifyjs", url.slice(site.length + 1), args.join(" ")); console.log(stderr); if (code) throw new Error("uglifyjs failed with code " + code); }); uglifyjs.stderr.on("data", function(data) { stderr += data; }).setEncoding("utf8"); uglifyjs.stdout.pipe(response); res.pipe(uglifyjs.stdin); } else { res.pipe(response); } } }); }).listen(); server.on("listening", function() { var port = server.address().port; if (debug) { console.log("http://localhost:" + port + "/"); } else { child_process.spawn(process.platform == "win32" ? "npm.cmd" : "npm", [ "install", "phantomjs-prebuilt@2.1.14", "--no-audit", "--no-optional", "--no-save", "--no-update-notifier", ], { stdio: [ "ignore", 1, 2 ] }).on("exit", function(code) { if (code) throw new Error("npm install failed!"); var program = require("phantomjs-prebuilt").exec(process.argv[1], port); program.stdout.pipe(process.stdout); program.stderr.pipe(process.stderr); program.on("exit", function(code) { server.close(); if (code) throw new Error("JetStream failed!"); console.log("JetStream completed successfully."); process.exit(0); }); }); } }); server.timeout = 0; } else { var page = require("webpage").create(); page.onError = function(msg, trace) { var body = [ msg ]; if (trace) trace.forEach(function(t) { body.push(" " + (t.function || "Anonymous function") + " (" + t.file + ":" + t.line + ")"); }); console.error(body.join("\n")); phantom.exit(1); }; var url = "http://localhost:" + require("system").args[1] + "/"; page.onConsoleMessage = function(msg) { if (/Error:/i.test(msg)) { console.error(msg); phantom.exit(1); } console.log(msg); if (~msg.indexOf("Raw results:")) { phantom.exit(); } }; page.open(url, function(status) { if (status != "success") phantom.exit(1); page.evaluate(function() { JetStream.switchToQuick(); JetStream.start(); }); }); } UglifyJS2-3.6.3/test/mocha.js000066400000000000000000000061451355252637300157260ustar00rootroot00000000000000var fs = require("fs"); var config = { limit: 5000, timeout: function(limit) { this.limit = limit; } }; var tasks = []; var titles = []; describe = function(title, fn) { config = Object.create(config); titles.push(title); fn.call(config); titles.pop(); config = Object.getPrototypeOf(config); }; it = function(title, fn) { fn.limit = config.limit; fn.titles = titles.slice(); fn.titles.push(title); tasks.push(fn); }; fs.readdirSync("test/mocha").filter(function(file) { return /\.js$/.test(file); }).forEach(function(file) { require("./mocha/" + file); }); function log_titles(log, current, marker) { var indent = ""; var writing = false; for (var i = 0; i < current.length; i++, indent += " ") { if (titles[i] != current[i]) writing = true; if (writing) log(indent + (i == current.length - 1 && marker || "") + current[i]); } titles = current; } function red(text) { return "\u001B[31m" + text + "\u001B[39m"; } function green(text) { return "\u001B[32m" + text + "\u001B[39m"; } var errors = []; var total = tasks.length; titles = []; process.nextTick(function run() { var task = tasks.shift(); if (task) try { var elapsed = Date.now(); var timer; var done = function() { reset(); elapsed = Date.now() - elapsed; if (elapsed > task.limit) { throw new Error("Timed out: " + elapsed + "ms > " + task.limit + "ms"); } log_titles(console.log, task.titles, green('\u221A ')); process.nextTick(run); }; if (task.length) { task.timeout = function(limit) { clearTimeout(timer); task.limit = limit; timer = setTimeout(function() { raise(new Error("Timed out: exceeds " + limit + "ms")); }, limit); }; task.timeout(task.limit); process.on("uncaughtException", raise); task.call(task, done); } else { task.timeout = config.timeout; task.call(task); done(); } } catch (err) { raise(err); } else if (errors.length) { console.error(); console.log(red(errors.length + " test(s) failed!")); titles = []; errors.forEach(function(titles, index) { console.error(); log_titles(console.error, titles, (index + 1) + ") "); var lines = titles.error.stack.split('\n'); console.error(red(lines[0])); console.error(lines.slice(1).join("\n")); }); process.exit(1); } else { console.log(); console.log(green(total + " test(s) passed.")); } function raise(err) { reset(); task.titles.error = err; errors.push(task.titles); log_titles(console.log, task.titles, red('\u00D7 ')); process.nextTick(run); } function reset() { clearTimeout(timer); done = function() {}; process.removeListener("uncaughtException", raise); } }); UglifyJS2-3.6.3/test/mocha/000077500000000000000000000000001355252637300153625ustar00rootroot00000000000000UglifyJS2-3.6.3/test/mocha/arguments.js000066400000000000000000000024111355252637300177230ustar00rootroot00000000000000var assert = require("assert"); var UglifyJS = require("../.."); describe("arguments", function() { it("Should known that arguments in functions are local scoped", function() { var ast = UglifyJS.parse("var arguments; var f = function() {arguments.length}"); ast.figure_out_scope(); // Test scope of `var arguments` assert.strictEqual(ast.find_variable("arguments").global, true); // Select arguments symbol in function var symbol = ast.body[1].definitions[0].value.find_variable("arguments"); assert.strictEqual(symbol.global, false); assert.strictEqual(symbol.scope, ast. // From ast body[1]. // Select 2nd statement (equals to `var f ...`) definitions[0]. // First definition of selected statement value // Select function as scope ); }); it("Should recognize when a function uses arguments", function() { var ast = UglifyJS.parse("function a(){function b(){function c(){}; return arguments[0];}}"); ast.figure_out_scope(); assert.strictEqual(ast.body[0].uses_arguments, false); assert.strictEqual(ast.body[0].body[0].uses_arguments, true); assert.strictEqual(ast.body[0].body[0].body[0].uses_arguments, false); }); }); UglifyJS2-3.6.3/test/mocha/cli.js000066400000000000000000001031051355252637300164670ustar00rootroot00000000000000var assert = require("assert"); var exec = require("child_process").exec; var fs = require("fs"); var run_code = require("../sandbox").run_code; function read(path) { return fs.readFileSync(path, "utf8"); } describe("bin/uglifyjs", function() { var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs'; it("Should produce a functional build when using --self", function(done) { this.timeout(30000); var command = uglifyjscmd + ' --self -cm --wrap WrappedUglifyJS'; exec(command, function(err, stdout) { if (err) throw err; eval(stdout); assert.strictEqual(typeof WrappedUglifyJS, "object"); var result = WrappedUglifyJS.minify("foo([true,,2+3]);"); assert.strictEqual(result.error, undefined); assert.strictEqual(result.code, "foo([!0,,5]);"); done(); }); }); it("Should be able to filter comments correctly with `--comments all`", function(done) { var command = uglifyjscmd + ' test/input/comments/filter.js --comments all'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, "// foo\n/*@preserve*/\n// bar\n\n"); done(); }); }); it("Should be able to filter comments correctly with `--comment `", function(done) { var command = uglifyjscmd + ' test/input/comments/filter.js --comments /r/'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, "/*@preserve*/\n// bar\n\n"); done(); }); }); it("Should be able to filter comments correctly with just `--comment`", function(done) { var command = uglifyjscmd + ' test/input/comments/filter.js --comments'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, "/*@preserve*/\n\n"); done(); }); }); it("Should give sensible error against invalid input source map", function(done) { var command = uglifyjscmd + " test/mocha.js --source-map content=blah,url=inline --verbose"; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.deepEqual(stderr.split(/\n/).slice(0, 2), [ "INFO: Using input source map: blah", "ERROR: invalid input source map: blah", ]); done(); }); }); it("Should append source map to output when using --source-map url=inline", function(done) { var command = uglifyjscmd + " test/input/issue-1323/sample.js --source-map url=inline"; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, [ "var bar=function(){function foo(bar){return bar}return foo}();", "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMTMyMy9zYW1wbGUuanMiXSwibmFtZXMiOlsiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxJQUFJQSxJQUFNLFdBQ04sU0FBU0MsSUFBS0QsS0FDVixPQUFPQSxJQUdYLE9BQU9DLElBTEQifQ==", "", ].join("\n")); done(); }); }); it("Should not append source map to output when not using --source-map url=inline", function(done) { var command = uglifyjscmd + ' test/input/issue-1323/sample.js'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, "var bar=function(){function foo(bar){return bar}return foo}();\n"); done(); }); }); it("Should not consider source map file content as source map file name (issue #2082)", function(done) { var command = [ uglifyjscmd, "test/input/issue-2082/sample.js", "--source-map", "content=test/input/issue-2082/sample.js.map", "--source-map", "url=inline", "--verbose", ].join(" "); exec(command, function(err, stdout, stderr) { if (err) throw err; var stderrLines = stderr.split("\n"); assert.strictEqual(stderrLines[0], "INFO: Using input source map: test/input/issue-2082/sample.js.map"); assert.notStrictEqual(stderrLines[1], 'INFO: Using input source map: {"version": 3,"sources": ["index.js"],"mappings": ";"}'); done(); }); }); it("Should not load source map before finish reading from STDIN", function(done) { var mapFile = "tmp/input.js.map"; try { fs.mkdirSync("./tmp"); } catch (e) { if (e.code != "EEXIST") throw e; } try { fs.unlinkSync(mapFile); } catch (e) { if (e.code != "ENOENT") throw e; } var command = [ uglifyjscmd, "--source-map", "content=" + mapFile, "--source-map", "includeSources=true", "--source-map", "url=inline", ].join(" "); var child = exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, read("test/input/issue-3040/expect.js")); done(); }); setTimeout(function() { fs.writeFileSync(mapFile, read("test/input/issue-3040/input.js.map")); child.stdin.end(read("test/input/issue-3040/input.js")); }, 1000); }); it("Should work with --keep-fnames (mangle only)", function(done) { var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n"); done(); }); }); it("Should work with --keep-fnames (mangle & compress)", function(done) { var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m -c unused=false'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(5==f(g)());\n"); done(); }); }); it("Should work with keep_fnames under mangler options", function(done) { var command = uglifyjscmd + ' test/input/issue-1431/sample.js -m keep_fnames=true'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n"); done(); }); }); it("Should work with --define (simple)", function(done) { var command = uglifyjscmd + ' test/input/global_defs/simple.js --define D=5 -c'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, "console.log(5);\n"); done(); }); }); it("Should work with --define (nested)", function(done) { var command = uglifyjscmd + ' test/input/global_defs/nested.js --define C.D=5,C.V=3 -c'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, "console.log(3,5);\n"); done(); }); }); it("Should work with --define (AST_Node)", function(done) { var command = uglifyjscmd + ' test/input/global_defs/simple.js --define console.log=stdout.println -c'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, "stdout.println(D);\n"); done(); }); }); it("Should work with `--beautify`", function(done) { var command = uglifyjscmd + ' test/input/issue-1482/input.js -b'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, read("test/input/issue-1482/default.js")); done(); }); }); it("Should work with `--beautify braces`", function(done) { var command = uglifyjscmd + ' test/input/issue-1482/input.js -b braces'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, read("test/input/issue-1482/braces.js")); done(); }); }); it("Should process inline source map", function(done) { var command = [ uglifyjscmd, "test/input/issue-520/input.js", "-mc", "toplevel", "--source-map", "content=inline", "--source-map", "includeSources=true", "--source-map", "url=inline", ].join(" "); exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, read("test/input/issue-520/output.js")); done(); }); }); it("Should warn for missing inline source map", function(done) { var command = uglifyjscmd + " test/input/issue-1323/sample.js --source-map content=inline,url=inline --warn"; exec(command, function(err, stdout, stderr) { if (err) throw err; assert.strictEqual(stdout, [ "var bar=function(){function foo(bar){return bar}return foo}();", "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMTMyMy9zYW1wbGUuanMiXSwibmFtZXMiOlsiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxJQUFJQSxJQUFNLFdBQ04sU0FBU0MsSUFBS0QsS0FDVixPQUFPQSxJQUdYLE9BQU9DLElBTEQifQ==", "", ].join("\n")); var stderrLines = stderr.split("\n"); assert.strictEqual(stderrLines[0], "WARN: inline source map not found: test/input/issue-1323/sample.js"); done(); }); }); it("Should handle multiple input and inline source map", function(done) { var command = [ uglifyjscmd, "test/input/issue-520/input.js", "test/input/issue-1323/sample.js", "--source-map", "content=inline,url=inline", "--warn", ].join(" "); exec(command, function(err, stdout, stderr) { if (err) throw err; assert.strictEqual(stdout, [ "var Foo=function Foo(){console.log(1+2)};new Foo;var bar=function(){function foo(bar){return bar}return foo}();", "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0ZGluIiwidGVzdC9pbnB1dC9pc3N1ZS0xMzIzL3NhbXBsZS5qcyJdLCJuYW1lcyI6WyJGb28iLCJjb25zb2xlIiwibG9nIiwiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxJQUFNQSxJQUFJLFNBQUFBLE1BQWdCQyxRQUFRQyxJQUFJLEVBQUUsSUFBTyxJQUFJRixJQ0FuRCxJQUFJRyxJQUFNLFdBQ04sU0FBU0MsSUFBS0QsS0FDVixPQUFPQSxJQUdYLE9BQU9DLElBTEQifQ==", "", ].join("\n")); var stderrLines = stderr.split("\n"); assert.strictEqual(stderrLines[0], "WARN: inline source map not found: test/input/issue-1323/sample.js"); done(); }); }); it("Should fail with acorn and inline source map", function(done) { var command = uglifyjscmd + " test/input/issue-520/input.js --source-map content=inline,url=inline -p acorn"; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stderr, "ERROR: inline source map only works with built-in parser\n"); done(); }); }); it("Should fail with SpiderMonkey and inline source map", function(done) { var command = uglifyjscmd + " test/input/issue-520/input.js --source-map content=inline,url=inline -p spidermonkey"; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stderr, "ERROR: inline source map only works with built-in parser\n"); done(); }); }); it("Should fail with invalid syntax", function(done) { var command = uglifyjscmd + ' test/input/invalid/simple.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); var lines = stderr.split(/\n/); assert.strictEqual(lines[0], "Parse error at test/input/invalid/simple.js:1,12"); assert.strictEqual(lines[1], "function f(a{}"); assert.strictEqual(lines[2], " ^"); assert.strictEqual(lines[3], "ERROR: Unexpected token: punc «{», expected: punc «,»"); done(); }); }); it("Should fail with correct marking of tabs", function(done) { var command = uglifyjscmd + ' test/input/invalid/tab.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); var lines = stderr.split(/\n/); assert.strictEqual(lines[0], "Parse error at test/input/invalid/tab.js:1,12"); assert.strictEqual(lines[1], "\t\tfoo(\txyz, 0abc);"); assert.strictEqual(lines[2], "\t\t \t ^"); assert.strictEqual(lines[3], "ERROR: Invalid syntax: 0abc"); done(); }); }); it("Should fail with correct marking at start of line", function(done) { var command = uglifyjscmd + ' test/input/invalid/eof.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); var lines = stderr.split(/\n/); assert.strictEqual(lines[0], "Parse error at test/input/invalid/eof.js:2,0"); assert.strictEqual(lines[1], "foo, bar("); assert.strictEqual(lines[2], " ^"); assert.strictEqual(lines[3], "ERROR: Unexpected token: eof"); done(); }); }); it("Should fail with a missing loop body", function(done) { var command = uglifyjscmd + ' test/input/invalid/loop-no-body.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); var lines = stderr.split(/\n/); assert.strictEqual(lines[0], "Parse error at test/input/invalid/loop-no-body.js:2,0"); assert.strictEqual(lines[1], "for (var i = 0; i < 1; i++) "); assert.strictEqual(lines[2], " ^"); assert.strictEqual(lines[3], "ERROR: Unexpected token: eof"); done(); }); }); it("Should throw syntax error (5--)", function(done) { var command = uglifyjscmd + ' test/input/invalid/assign_1.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/assign_1.js:1,18", "console.log(1 || 5--);", " ^", "ERROR: Invalid use of -- operator" ].join("\n")); done(); }); }); it("Should throw syntax error (Math.random() /= 2)", function(done) { var command = uglifyjscmd + ' test/input/invalid/assign_2.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/assign_2.js:1,32", "console.log(2 || (Math.random() /= 2));", " ^", "ERROR: Invalid assignment" ].join("\n")); done(); }); }); it("Should throw syntax error (++this)", function(done) { var command = uglifyjscmd + ' test/input/invalid/assign_3.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/assign_3.js:1,17", "console.log(3 || ++this);", " ^", "ERROR: Invalid use of ++ operator" ].join("\n")); done(); }); }); it("Should throw syntax error (++null)", function(done) { var command = uglifyjscmd + ' test/input/invalid/assign_4.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/assign_4.js:1,0", "++null", "^", "ERROR: Invalid use of ++ operator" ].join("\n")); done(); }); }); it("Should throw syntax error (a.=)", function(done) { var command = uglifyjscmd + ' test/input/invalid/dot_1.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/dot_1.js:1,2", "a.=", " ^", "ERROR: Unexpected token: operator «=», expected: name" ].join("\n")); done(); }); }); it("Should throw syntax error (%.a)", function(done) { var command = uglifyjscmd + ' test/input/invalid/dot_2.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/dot_2.js:1,0", "%.a;", "^", "ERROR: Unexpected token: operator «%»" ].join("\n")); done(); }); }); it("Should throw syntax error (a./();)", function(done) { var command = uglifyjscmd + ' test/input/invalid/dot_3.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/dot_3.js:1,2", "a./();", " ^", "ERROR: Unexpected token: operator «/», expected: name" ].join("\n")); done(); }); }); it("Should throw syntax error ({%: 1})", function(done) { var command = uglifyjscmd + ' test/input/invalid/object.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/object.js:1,13", "console.log({%: 1});", " ^", "ERROR: Unexpected token: operator «%»" ].join("\n")); done(); }); }); it("Should throw syntax error (delete x)", function(done) { var command = uglifyjscmd + ' test/input/invalid/delete.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/delete.js:13,11", " delete x;", " ^", "ERROR: Calling delete on expression not allowed in strict mode" ].join("\n")); done(); }); }); it("Should throw syntax error (function g(arguments))", function(done) { var command = uglifyjscmd + ' test/input/invalid/function_1.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/function_1.js:4,11", "function g(arguments) {", " ^", "ERROR: Unexpected arguments in strict mode" ].join("\n")); done(); }); }); it("Should throw syntax error (function eval())", function(done) { var command = uglifyjscmd + ' test/input/invalid/function_2.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/function_2.js:4,9", "function eval() {", " ^", "ERROR: Unexpected eval in strict mode" ].join("\n")); done(); }); }); it("Should throw syntax error (iife arguments())", function(done) { var command = uglifyjscmd + ' test/input/invalid/function_3.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/function_3.js:4,10", "!function arguments() {", " ^", "ERROR: Unexpected arguments in strict mode" ].join("\n")); done(); }); }); it("Should throw syntax error (catch (eval))", function(done) { var command = uglifyjscmd + ' test/input/invalid/try.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/try.js:7,18", " try {} catch (eval) {}", " ^", "ERROR: Unexpected eval in strict mode" ].join("\n")); done(); }); }); it("Should throw syntax error (var eval)", function(done) { var command = uglifyjscmd + ' test/input/invalid/var.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/var.js:7,8", " var eval;", " ^", "ERROR: Unexpected eval in strict mode" ].join("\n")); done(); }); }); it("Should throw syntax error (else)", function(done) { var command = uglifyjscmd + ' test/input/invalid/else.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/else.js:1,7", "if (0) else 1;", " ^", "ERROR: Unexpected token: keyword «else»" ].join("\n")); done(); }); }); it("Should throw syntax error (return)", function(done) { var command = uglifyjscmd + ' test/input/invalid/return.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/return.js:1,0", "return 42;", "^", "ERROR: 'return' outside of function" ].join("\n")); done(); }); }); it("Should throw syntax error (for-in init)", function(done) { var command = uglifyjscmd + ' test/input/invalid/for-in_1.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/for-in_1.js:2,5", "for (1, 2, a in b) {", " ^", "ERROR: Invalid left-hand side in for..in loop" ].join("\n")); done(); }); }); it("Should throw syntax error (for-in var)", function(done) { var command = uglifyjscmd + ' test/input/invalid/for-in_2.js'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ "Parse error at test/input/invalid/for-in_2.js:2,5", "for (var a, b in c) {", " ^", "ERROR: Only one variable declaration allowed in for..in loop" ].join("\n")); done(); }); }); it("Should handle literal string as source map input", function(done) { var command = [ uglifyjscmd, "test/input/issue-1236/simple.js", "--source-map", 'content="' + read_map() + '",url=inline' ].join(" "); exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, [ '"use strict";var foo=function foo(x){return"foo "+x};console.log(foo("bar"));', "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LmpzIl0sIm5hbWVzIjpbImZvbyIsIngiLCJjb25zb2xlIiwibG9nIl0sIm1hcHBpbmdzIjoiYUFBQSxJQUFJQSxJQUFNLFNBQU5BLElBQU1DLEdBQUEsTUFBSyxPQUFTQSxHQUN4QkMsUUFBUUMsSUFBSUgsSUFBSSJ9", "" ].join("\n")); done(); }); function read_map() { var map = JSON.parse(read("./test/input/issue-1236/simple.js.map")); delete map.sourcesContent; return JSON.stringify(map).replace(/"/g, '\\"'); } }); it("Should include function calls in source map", function(done) { var command = [ uglifyjscmd, "test/input/issue-2310/input.js", "-c", "--source-map", "url=inline", ].join(" "); exec(command, function(err, stdout, stderr) { if (err) throw err; assert.strictEqual(stdout, [ 'function foo(){return function(){console.log("PASS")}}foo()();', "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMjMxMC9pbnB1dC5qcyJdLCJuYW1lcyI6WyJmb28iLCJjb25zb2xlIiwibG9nIiwiZiJdLCJtYXBwaW5ncyI6IkFBQUEsU0FBU0EsTUFDTCxPQUFPLFdBQ0hDLFFBQVFDLElBQUksU0FLUkYsS0FDUkcifQ==", "" ].join("\n")); done(); }); }); it("Should dump AST as JSON", function(done) { var command = uglifyjscmd + " test/input/global_defs/simple.js -mco ast"; exec(command, function(err, stdout) { if (err) throw err; var ast = JSON.parse(stdout); assert.strictEqual(ast._class, "AST_Toplevel"); assert.ok(Array.isArray(ast.body)); done(); }); }); it("Should print supported options on invalid option syntax", function(done) { var command = uglifyjscmd + " test/input/comments/filter.js -b ascii-only"; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.ok(/^Supported options:\n[\s\S]*?\nERROR: `ascii-only` is not a supported option/.test(stderr), stderr); done(); }); }); it("Should work with --mangle reserved=[]", function(done) { var command = uglifyjscmd + " test/input/issue-505/input.js -m reserved=[callback]"; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, 'function test(callback){"aaaaaaaaaaaaaaaa";callback(err,data);callback(err,data)}\n'); done(); }); }); it("Should work with --mangle reserved=false", function(done) { var command = uglifyjscmd + " test/input/issue-505/input.js -m reserved=false"; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, 'function test(a){"aaaaaaaaaaaaaaaa";a(err,data);a(err,data)}\n'); done(); }); }); it("Should fail with --mangle-props reserved=[in]", function(done) { var command = uglifyjscmd + " test/input/issue-505/input.js --mangle-props reserved=[in]"; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.ok(/^Supported options:\n[\s\S]*?\nERROR: `reserved=\[in]` is not a supported option/.test(stderr), stderr); done(); }); }); it("Should work with mangle.properties.regex from --config-file", function(done) { var command = uglifyjscmd + " test/input/issue-3315/input.js --config-file test/input/issue-3315/config.json"; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, 'function f(){"aaaaaaaaaa";var a={prop:1,a:2};return a.prop+a.a}\n'); done(); }); }); it("Should fail with --define a-b", function(done) { var command = uglifyjscmd + " test/input/issue-505/input.js --define a-b"; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.strictEqual(stdout, ""); assert.strictEqual(stderr, "ERROR: cannot parse arguments for 'define': a-b\n"); done(); }); }); it("Should work with explicit --rename", function(done) { var command = uglifyjscmd + " test/input/rename/input.js --rename"; exec(command, function(err, stdout, stderr) { if (err) throw err; assert.strictEqual(stdout, "function f(a){return b(a);function b(c){return c}}\n"); done(); }); }); it("Should work with explicit --no-rename", function(done) { var command = uglifyjscmd + " test/input/rename/input.js -mc passes=2 --no-rename"; exec(command, function(err, stdout, stderr) { if (err) throw err; assert.strictEqual(stdout, "function f(n){return function(n){return n}(n)}\n"); done(); }); }); it("Should work with implicit --rename", function(done) { var command = uglifyjscmd + " test/input/rename/input.js -mc passes=2"; exec(command, function(err, stdout, stderr) { if (err) throw err; assert.strictEqual(stdout, "function f(n){return n}\n"); done(); }); }); it("Should work with implicit --no-rename", function(done) { var command = uglifyjscmd + " test/input/rename/input.js -c passes=2"; exec(command, function(err, stdout, stderr) { if (err) throw err; assert.strictEqual(stdout, "function f(x){return function(x){return x}(x)}\n"); done(); }); }); it("Should work with --enclose", function(done) { var command = uglifyjscmd + " test/input/enclose/input.js --enclose"; exec(command, function(err, stdout, stderr) { if (err) throw err; assert.strictEqual(stdout, '(function(){function enclose(){console.log("test enclose")}enclose()})();\n'); done(); }); }); it("Should work with --enclose arg", function(done) { var command = uglifyjscmd + " test/input/enclose/input.js --enclose undefined"; exec(command, function(err, stdout, stderr) { if (err) throw err; assert.strictEqual(stdout, '(function(undefined){function enclose(){console.log("test enclose")}enclose()})();\n'); done(); }); }); it("Should work with --enclose arg:value", function(done) { var command = uglifyjscmd + " test/input/enclose/input.js --enclose window,undefined:window"; exec(command, function(err, stdout, stderr) { if (err) throw err; assert.strictEqual(stdout, '(function(window,undefined){function enclose(){console.log("test enclose")}enclose()})(window);\n'); done(); }); }); it("Should work with --enclose & --wrap", function(done) { var command = uglifyjscmd + " test/input/enclose/input.js --enclose window,undefined:window --wrap exports"; exec(command, function(err, stdout, stderr) { if (err) throw err; assert.strictEqual(stdout, '(function(exports){(function(window,undefined){function enclose(){console.log("test enclose")}enclose()})(window)})(typeof exports=="undefined"?exports={}:exports);\n'); done(); }); }); it("Should compress swarm of unused variables with reasonable performance", function(done) { var code = [ "console.log(function() {", ]; for (var i = 0; i < 10000; i++) { code.push("var obj" + i + " = {p: " + i + "};"); } code.push("var map = {"); for (var i = 0; i < 10000; i++) { code.push("obj" + i + ": obj" + i + ","); } code = code.concat([ "};", "return obj25.p + obj121.p + obj1024.p;", "}());", ]).join("\n"); exec(uglifyjscmd + " -mc", function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, [ "console.log(function(){", "var p={p:25},n={p:121},o={p:1024};", "return p.p+n.p+o.p", "}());\n", ].join("")); assert.strictEqual(run_code(stdout), run_code(code)); done(); }).stdin.end(code); }); }); UglifyJS2-3.6.3/test/mocha/comments.js000066400000000000000000000367331355252637300175610ustar00rootroot00000000000000var assert = require("assert"); var UglifyJS = require("../node"); describe("comments", function() { it("Should recognize eol of single line comments", function() { var tests = [ "//Some comment 1\n>", "//Some comment 2\r>", "//Some comment 3\r\n>", "//Some comment 4\u2028>", "//Some comment 5\u2029>" ]; var fail = function(e) { return e instanceof UglifyJS.JS_Parse_Error && e.message === "Unexpected token: operator «>»" && e.line === 2 && e.col === 0; } for (var i = 0; i < tests.length; i++) { assert.throws(function() { UglifyJS.parse(tests[i]); }, fail, tests[i]); } }); it("Should update the position of a multiline comment correctly", function() { var tests = [ "/*Some comment 1\n\n\n*/\n>\n\n\n\n\n\n", "/*Some comment 2\r\n\r\n\r\n*/\r\n>\n\n\n\n\n\n", "/*Some comment 3\r\r\r*/\r>\n\n\n\n\n\n", "/*Some comment 4\u2028\u2028\u2028*/\u2028>\n\n\n\n\n\n", "/*Some comment 5\u2029\u2029\u2029*/\u2029>\n\n\n\n\n\n" ]; var fail = function(e) { return e instanceof UglifyJS.JS_Parse_Error && e.message === "Unexpected token: operator «>»" && e.line === 5 && e.col === 0; } for (var i = 0; i < tests.length; i++) { assert.throws(function() { UglifyJS.parse(tests[i]); }, fail, tests[i]); } }); it("Should handle comment within return correctly", function() { var result = UglifyJS.minify([ "function unequal(x, y) {", " return (", " // Either one", " x < y", " ||", " y < x", " );", "}", ].join("\n"), { compress: false, mangle: false, output: { beautify: true, comments: "all", }, }); if (result.error) throw result.error; assert.strictEqual(result.code, [ "function unequal(x, y) {", " // Either one", " return x < y || y < x;", "}", ].join("\n")); }); it("Should handle comment folded into return correctly", function() { var result = UglifyJS.minify([ "function f() {", " /* boo */ x();", " return y();", "}", ].join("\n"), { mangle: false, output: { beautify: true, comments: "all", }, }); if (result.error) throw result.error; assert.strictEqual(result.code, [ "function f() {", " /* boo */", " return x(), y();", "}", ].join("\n")); }); it("Should not drop comments after first OutputStream", function() { var code = "/* boo */\nx();"; var ast = UglifyJS.parse(code); var out1 = UglifyJS.OutputStream({ beautify: true, comments: "all", }); ast.print(out1); var out2 = UglifyJS.OutputStream({ beautify: true, comments: "all", }); ast.print(out2); assert.strictEqual(out1.get(), code); assert.strictEqual(out2.get(), out1.get()); }); it("Should retain trailing comments", function() { var code = [ "if (foo /* lost comment */ && bar /* lost comment */) {", " // this one is kept", " {/* lost comment */}", " !function() {", " // lost comment", " }();", " function baz() {/* lost comment */}", " // lost comment", "}", "// comments right before EOF are lost as well", ].join("\n"); var result = UglifyJS.minify(code, { compress: false, mangle: false, output: { beautify: true, comments: "all", }, }); if (result.error) throw result.error; assert.strictEqual(result.code, code); }); it("Should retain comments within braces", function() { var code = [ "{/* foo */}", "a({/* foo */});", "while (a) {/* foo */}", "switch (a) {/* foo */}", "if (a) {/* foo */} else {/* bar */}", ].join("\n\n"); var result = UglifyJS.minify(code, { compress: false, mangle: false, output: { beautify: true, comments: "all", }, }); if (result.error) throw result.error; assert.strictEqual(result.code, code); }); it("Should correctly preserve new lines around comments", function() { var tests = [ [ "// foo", "// bar", "x();", ].join("\n"), [ "// foo", "/* bar */", "x();", ].join("\n"), [ "// foo", "/* bar */ x();", ].join("\n"), [ "/* foo */", "// bar", "x();", ].join("\n"), [ "/* foo */ // bar", "x();", ].join("\n"), [ "/* foo */", "/* bar */", "x();", ].join("\n"), [ "/* foo */", "/* bar */ x();", ].join("\n"), [ "/* foo */ /* bar */", "x();", ].join("\n"), "/* foo */ /* bar */ x();", ].forEach(function(code) { var result = UglifyJS.minify(code, { compress: false, mangle: false, output: { beautify: true, comments: "all", }, }); if (result.error) throw result.error; assert.strictEqual(result.code, code); }); }); it("Should preserve new line before comment without beautify", function() { var code = [ "function f(){", "/* foo */bar()}", ].join("\n"); var result = UglifyJS.minify(code, { compress: false, mangle: false, output: { comments: "all", }, }); if (result.error) throw result.error; assert.strictEqual(result.code, code); }); it("Should preserve comments around IIFE", function() { var result = UglifyJS.minify("/*a*/(/*b*/function(){/*c*/}/*d*/)/*e*/();", { compress: false, mangle: false, output: { comments: "all", }, }); if (result.error) throw result.error; assert.strictEqual(result.code, "/*a*/ /*b*/(function(){/*c*/}/*d*/ /*e*/)();"); }); it("Should output line comments after statements", function() { var result = UglifyJS.minify([ "x()//foo", "{y()//bar", "}", ].join("\n"), { compress: false, mangle: false, output: { comments: "all", }, }); if (result.error) throw result.error; assert.strictEqual(result.code, [ "x();//foo", "{y();//bar", "}", ].join("\n")); }); it("Should handle programmatic AST insertions gracefully", function() { var ast = UglifyJS.parse([ "function f() {", " //foo", " bar;", " return;", "}", ].join("\n")); ast.body[0].body[0] = new UglifyJS.AST_Throw({value: ast.body[0].body[0].body}); ast.body[0].body[1].value = new UglifyJS.AST_Number({value: 42}); assert.strictEqual(ast.print_to_string({comments: "all"}), [ "function f(){", "//foo", "throw bar;return 42}", ].join("\n")); }); it("Should not duplicate sourceMappingURL", function() { var code = [ "//# sourceMappingURL=input.map", "print(42);", "//# sourceMappingURL=input.map", "", "//# sourceMappingURL=input.map", "//# sourceMappingURL=input.map", "", ].join("\n"); var result = UglifyJS.minify(code, { output: { comments: true }, }); if (result.error) throw result.error; assert.strictEqual(result.code, [ "//# sourceMappingURL=input.map", "print(42);", "//# sourceMappingURL=input.map", "//# sourceMappingURL=input.map", "//# sourceMappingURL=input.map", ].join("\n")); result = UglifyJS.minify(code, { output: { comments: true }, sourceMap: { url: "output.map" }, }); if (result.error) throw result.error; assert.strictEqual(result.code, [ "//# sourceMappingURL=input.map", "print(42);", "//# sourceMappingURL=input.map", "//# sourceMappingURL=input.map", "//# sourceMappingURL=output.map", ].join("\n")); }); describe("comment before constant", function() { var js = 'function f() { /*c1*/ var /*c2*/ foo = /*c3*/ false; return foo; }'; it("Should test comment before constant is retained and output after mangle.", function() { var result = UglifyJS.minify(js, { compress: { collapse_vars: false, reduce_vars: false }, output: { comments: true }, }); assert.strictEqual(result.code, 'function f(){/*c1*/var/*c2*/n=/*c3*/!1;return n}'); }); it("Should test code works when comments disabled.", function() { var result = UglifyJS.minify(js, { compress: { collapse_vars: false, reduce_vars: false }, output: { comments: false }, }); assert.strictEqual(result.code, 'function f(){var n=!1;return n}'); }); }); describe("comment filters", function() { it("Should be able to filter comments by passing regexp", function() { var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\ntest7\n-->!test8"); assert.strictEqual(ast.print_to_string({comments: /^!/}), "/*!test1*/\n//!test3\n//!test6\n//!test8\n"); }); it("Should be able to filter comments with the 'all' option", function() { var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\ntest7\n-->!test8"); assert.strictEqual(ast.print_to_string({comments: "all"}), "/*!test1*/\n/*test2*/\n//!test3\n//test4\n//test5\n//!test6\n//test7\n//!test8\n"); }); it("Should be able to filter commments with the 'some' option", function() { var ast = UglifyJS.parse("// foo\n/*@preserve*/\n// bar\n/*@license*/\n//@license with the wrong comment type\n/*@cc_on something*/"); assert.strictEqual(ast.print_to_string({comments: "some"}), "/*@preserve*/\n/*@license*/\n/*@cc_on something*/"); }); it("Should be able to filter comments by passing a function", function() { var ast = UglifyJS.parse("/*TEST 123*/\n//An other comment\n//8 chars."); var f = function(node, comment) { return comment.value.length === 8; }; assert.strictEqual(ast.print_to_string({comments: f}), "/*TEST 123*/\n//8 chars.\n"); }); it("Should be able to filter comments by passing regex in string format", function() { var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\ntest7\n-->!test8"); assert.strictEqual(ast.print_to_string({comments: "/^!/"}), "/*!test1*/\n//!test3\n//!test6\n//!test8\n"); }); it("Should be able to get the comment and comment type when using a function", function() { var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\ntest7\n-->!test8"); var f = function(node, comment) { return comment.type == "comment1" || comment.type == "comment3"; }; assert.strictEqual(ast.print_to_string({comments: f}), "//!test3\n//test4\n//test5\n//!test6\n"); }); it("Should be able to filter comments by passing a boolean", function() { var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\ntest7\n-->!test8"); assert.strictEqual(ast.print_to_string({comments: true}), "/*!test1*/\n/*test2*/\n//!test3\n//test4\n//test5\n//!test6\n//test7\n//!test8\n"); assert.strictEqual(ast.print_to_string({comments: false}), ""); }); it("Should never be able to filter comment5 (shebangs)", function() { var ast = UglifyJS.parse("#!Random comment\n//test1\n/*test2*/"); var f = function(node, comment) { assert.strictEqual(comment.type === "comment5", false); return true; }; assert.strictEqual(ast.print_to_string({comments: f}), "#!Random comment\n//test1\n/*test2*/"); }); it("Should never be able to filter comment5 when using 'some' as filter", function() { var ast = UglifyJS.parse("#!foo\n//foo\n/*@preserve*/\n/* please hide me */"); assert.strictEqual(ast.print_to_string({comments: "some"}), "#!foo\n/*@preserve*/"); }); it("Should have no problem on multiple calls", function() { const options = { comments: /ok/ }; assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}"); assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}"); assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}"); }); it("Should handle shebang and preamble correctly", function() { var code = UglifyJS.minify("#!/usr/bin/node\nvar x = 10;", { output: { preamble: "/* Build */" } }).code; assert.strictEqual(code, "#!/usr/bin/node\n/* Build */\nvar x=10;"); }); it("Should handle preamble without shebang correctly", function() { var code = UglifyJS.minify("var x = 10;", { output: { preamble: "/* Build */" } }).code; assert.strictEqual(code, "/* Build */\nvar x=10;"); }); }); describe("Huge number of comments.", function() { it("Should parse and compress code with thousands of consecutive comments", function() { var js = "function lots_of_comments(x) { return 7 -"; for (var i = 1; i <= 5000; ++i) js += "// " + i + "\n"; for (; i <= 10000; ++i) js += "/* " + i + " */ /**/"; js += "x; }"; var result = UglifyJS.minify(js, { mangle: false }); assert.strictEqual(result.code, "function lots_of_comments(x){return 7-x}"); }); }); }); UglifyJS2-3.6.3/test/mocha/directives.js000066400000000000000000000333001355252637300200600ustar00rootroot00000000000000var assert = require("assert"); var UglifyJS = require("../node"); describe("Directives", function() { it("Should allow tokenizer to store directives state", function() { var tokenizer = UglifyJS.tokenizer("", "foo.js"); // Stack level 0 assert.strictEqual(tokenizer.has_directive("use strict"), false); assert.strictEqual(tokenizer.has_directive("use asm"), false); assert.strictEqual(tokenizer.has_directive("use thing"), false); // Stack level 2 tokenizer.push_directives_stack(); tokenizer.push_directives_stack(); tokenizer.add_directive("use strict"); assert.strictEqual(tokenizer.has_directive("use strict"), true); assert.strictEqual(tokenizer.has_directive("use asm"), false); assert.strictEqual(tokenizer.has_directive("use thing"), false); // Stack level 3 tokenizer.push_directives_stack(); tokenizer.add_directive("use strict"); tokenizer.add_directive("use asm"); assert.strictEqual(tokenizer.has_directive("use strict"), true); assert.strictEqual(tokenizer.has_directive("use asm"), true); assert.strictEqual(tokenizer.has_directive("use thing"), false); // Stack level 2 tokenizer.pop_directives_stack(); assert.strictEqual(tokenizer.has_directive("use strict"), true); assert.strictEqual(tokenizer.has_directive("use asm"), false); assert.strictEqual(tokenizer.has_directive("use thing"), false); // Stack level 3 tokenizer.push_directives_stack(); tokenizer.add_directive("use thing"); tokenizer.add_directive("use\\\nasm"); assert.strictEqual(tokenizer.has_directive("use strict"), true); assert.strictEqual(tokenizer.has_directive("use asm"), false); // Directives are strict! assert.strictEqual(tokenizer.has_directive("use thing"), true); // Stack level 2 tokenizer.pop_directives_stack(); assert.strictEqual(tokenizer.has_directive("use strict"), true); assert.strictEqual(tokenizer.has_directive("use asm"), false); assert.strictEqual(tokenizer.has_directive("use thing"), false); // Stack level 1 tokenizer.pop_directives_stack(); assert.strictEqual(tokenizer.has_directive("use strict"), false); assert.strictEqual(tokenizer.has_directive("use asm"), false); assert.strictEqual(tokenizer.has_directive("use thing"), false); // Stack level 0 tokenizer.pop_directives_stack(); assert.strictEqual(tokenizer.has_directive("use strict"), false); assert.strictEqual(tokenizer.has_directive("use asm"), false); assert.strictEqual(tokenizer.has_directive("use thing"), false); }); it("Should know which strings are directive and which ones are not", function() { [ [ '"use strict"\n', [ "use strict"], [ "use asm"] ], [ '"use\\\nstrict";', [], [ "use strict", "use\nstrict", "use \nstrict", "use asm" ] ], [ '"use strict"\n"use asm"\n"use bar"\n', [ "use strict", "use asm", "use bar" ], [ "use foo", "use\\x20strict" ] ], [ '"use \\\nstrict";"use strict";', [], [ "use strict", "use\nstrict", "use \nstrict", "use asm" ] ], [ '"\\76";', [], [ ">", "\\76" ] ], [ // no ; or newline '"use strict"', [], [ "use strict", "use\nstrict", "use \nstrict", "use asm" ] ], [ ';"use strict"', [], [ "use strict", "use\nstrict", "use \nstrict", "use asm" ] ], // Duplicate above code but put it in a function [ 'function foo() {"use strict"\n', [ "use strict" ], [ "use asm" ] ], [ 'function foo() {"use\\\nstrict";', [], [ "use strict", "use\nstrict", "use \nstrict", "use asm" ] ], [ 'function foo() {"use strict"\n"use asm"\n"use bar"\n', [ "use strict", "use asm", "use bar" ], [ "use foo", "use\\x20strict" ] ], [ 'function foo() {"use \\\nstrict";"use strict";', [], [ "use strict", "use\nstrict", "use \nstrict", "use asm" ] ], [ 'var foo = function() {"\\76";', [], [ ">", "\\76" ] ], [ 'var foo = function() {"use strict"', // no ; or newline [], [ "use strict", "use\nstrict", "use \nstrict", "use asm" ] ], [ 'var foo = function() {;"use strict"', [], [ "use strict", "use\nstrict", "use \nstrict", "use asm" ] ], // Special cases [ '"1";"2";"3";"4";;"5"', [ "1", "2", "3", "4" ], [ "5", "6", "use strict", "use asm" ] ], [ 'if(1){"use strict";', [], [ "use strict", "use\nstrict", "use \nstrict", "use asm" ] ], [ '"use strict";try{"use asm";', [ "use strict" ], [ "use\nstrict", "use \nstrict", "use asm" ] ], ].forEach(function(test) { var tokenizer = UglifyJS.tokenizer(test[0] + "]", "foo.js"); assert.throws(function() { UglifyJS.parse(tokenizer); }, function(e) { return e instanceof UglifyJS.JS_Parse_Error && /^Unexpected token: punc «]»/.test(e.message) }, test[0]); test[1].forEach(function(directive) { assert.strictEqual(tokenizer.has_directive(directive), true, directive + " in " + test[0]); }); test[2].forEach(function(fake_directive) { assert.strictEqual(tokenizer.has_directive(fake_directive), false, fake_directive + " in " + test[0]); }); }); }); it("Should test EXPECT_DIRECTIVE RegExp", function() { [ [ "", true ], [ "'test';", true ], [ "'test';;", true ], [ "'tests';\n", true ], [ "'tests'", false ], [ "'tests'; \n\t", true ], [ "'tests';\n\n", true ], [ "\n\n\"use strict\";\n\n", true ], ].forEach(function(test) { var out = UglifyJS.OutputStream(); out.print(test[0]); out.print_string("", null, true); assert.strictEqual(out.get() === test[0] + ';""', test[1], test[0]); }); }); it("Should only print 2 semicolons spread over 2 lines in beautify mode", function() { var result = UglifyJS.minify([ '"use strict";', "'use strict';", '"use strict";', '"use strict";;', "'use strict';", "console.log('use strict');" ].join(""), { compress: false, output: { beautify: true, quote_style: 3 } }); if (result.error) throw result.error; assert.strictEqual(result.code, [ '"use strict";', "'use strict';", '"use strict";', '"use strict";', ";'use strict';", "console.log('use strict');" ].join("\n\n")); }); it("Should not add double semicolons in non-scoped block statements to avoid strings becoming directives", function() { [ [ '{"use\x20strict"}', '{"use strict"}' ], [ 'function foo(){"use\x20strict";}', // Valid place for directives 'function foo(){"use strict"}' ], [ 'try{"use\x20strict"}catch(e){}finally{"use\x20strict"}', 'try{"use strict"}catch(e){}finally{"use strict"}' ], [ 'if(1){"use\x20strict"} else {"use strict"}', 'if(1){"use strict"}else{"use strict"}' ] ].forEach(function(test) { var result = UglifyJS.minify(test[0], { compress: false, mangle: false }); if (result.error) throw result.error; assert.strictEqual(result.code, test[1], test[0]); }); }); it("Should add double semicolon when relying on automatic semicolon insertion", function() { var result = UglifyJS.minify('"use strict";"use\\x20strict";', { compress: false, output: { semicolons: false } }); if (result.error) throw result.error; assert.strictEqual(result.code, '"use strict";;"use strict"\n'); }); it("Should check quote style of directives", function() { [ // 0. Prefer double quotes, unless string contains more double quotes than single quotes [ '"testing something";', 0, '"testing something";' ], [ "'use strict';", 0, '"use strict";' ], [ '"\\\'use strict\\\'";', // Not a directive as it contains quotes 0, ';"\'use strict\'";', ], [ "'\"use strict\"';", 0, "'\"use strict\"';", ], // 1. Always use single quote [ '"testing something";', 1, "'testing something';" ], [ "'use strict';", 1, "'use strict';" ], [ '"\'use strict\'";', 1, // Intentionally causes directive breakage at cost of less logic, usage should be rare anyway "'\\'use strict\\'';", ], [ "'\\'use strict\\'';", // Not a valid directive 1, "'\\'use strict\\'';" // But no ; necessary as directive stays invalid ], [ "'\"use strict\"';", 1, "'\"use strict\"';", ], // 2. Always use double quote [ '"testing something";', 2, '"testing something";' ], [ "'use strict';", 2, '"use strict";' ], [ '"\'use strict\'";', 2, "\"'use strict'\";", ], [ "'\"use strict\"';", 2, // Intentionally causes directive breakage at cost of less logic, usage should be rare anyway '"\\\"use strict\\\"";', ], [ '"\\"use strict\\"";', // Not a valid directive 2, '"\\"use strict\\"";' // But no ; necessary as directive stays invalid ], // 3. Always use original [ '"testing something";', 3, '"testing something";' ], [ "'use strict';", 3, "'use strict';", ], [ '"\'use strict\'";', 3, '"\'use strict\'";', ], [ "'\"use strict\"';", 3, "'\"use strict\"';", ], ].forEach(function(test) { var result = UglifyJS.minify(test[0], { compress: false, output: { quote_style: test[1] } }); if (result.error) throw result.error; assert.strictEqual(result.code, test[2], test[0] + " using mode " + test[1]); }); }); it("Should be able to compress without side effects", function() { [ [ '"use strict";"use strict";"use strict";"use foo";"use strict";;"use sloppy";doSomething("foo");', '"use strict";doSomething("foo");' ], [ // Nothing gets optimised in the compressor because "use asm" is the first statement '"use asm";"use\\x20strict";1+1;', // Yet, the parser noticed that "use strict" wasn't a directive '"use asm";;"use strict";1+1;', ], [ 'function f(){ "use strict" }', 'function f(){}' ], [ 'function f(){ "use asm" }', 'function f(){"use asm"}' ], [ 'function f(){ "use nondirective" }', 'function f(){}' ], [ 'function f(){ ;"use strict" }', 'function f(){}' ], [ 'function f(){ "use \\n"; }', 'function f(){}' ], ].forEach(function(test) { var result = UglifyJS.minify(test[0]); if (result.error) throw result.error; assert.strictEqual(result.code, test[1], test[0]); }); }); }); UglifyJS2-3.6.3/test/mocha/getter-setter.js000066400000000000000000000044021355252637300205160ustar00rootroot00000000000000var assert = require("assert"); var UglifyJS = require("../node"); describe("Getters and setters", function() { it("Should not accept operator symbols as getter/setter name", function() { var illegalOperators = [ "++", "--", "+", "-", "!", "~", "&", "|", "^", "*", "/", "%", ">>", "<<", ">>>", "<", ">", "<=", ">=", "==", "===", "!=", "!==", "?", "=", "+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&=", "&&", "||" ]; var generator = function() { var results = []; for (var i in illegalOperators) { results.push({ code: "var obj = { get " + illegalOperators[i] + "() { return test; }};", operator: illegalOperators[i], method: "get" }); results.push({ code: "var obj = { set " + illegalOperators[i] + "(value) { test = value}};", operator: illegalOperators[i], method: "set" }); } return results; }; var testCase = function(data) { return function() { UglifyJS.parse(data.code); }; }; var fail = function(data) { return function(e) { return e instanceof UglifyJS.JS_Parse_Error && e.message === "Unexpected token: operator «" + data.operator + "»"; }; }; var errorMessage = function(data) { return "Expected but didn't get a syntax error while parsing following line:\n" + data.code; }; var tests = generator(); for (var i = 0; i < tests.length; i++) { var test = tests[i]; assert.throws(testCase(test), fail(test), errorMessage(test)); } }); }); UglifyJS2-3.6.3/test/mocha/glob.js000066400000000000000000000055731355252637300166550ustar00rootroot00000000000000var assert = require("assert"); var exec = require("child_process").exec; var path = require("path"); var readFileSync = require("fs").readFileSync; describe("bin/uglifyjs with input file globs", function() { var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs'; it("bin/uglifyjs with one input file extension glob.", function(done) { var command = uglifyjscmd + ' "test/input/issue-1242/foo.*" -cm'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, 'var print=console.log.bind(console);function foo(o){print("Foo:",2*o)}\n'); done(); }); }); it("bin/uglifyjs with one input file name glob.", function(done) { var command = uglifyjscmd + ' "test/input/issue-1242/b*.es5" -cm'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, 'function bar(n){return 3*n}function baz(n){return n/2}\n'); done(); }); }); it("bin/uglifyjs with multiple input file globs.", function(done) { var command = uglifyjscmd + ' "test/input/issue-1242/???.es5" "test/input/issue-1242/*.js" -mc toplevel,passes=3'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, 'var print=console.log.bind(console);print("qux",9,6),print("Foo:",22);\n'); done(); }); }); it("Should throw with non-matching glob string", function(done) { var command = uglifyjscmd + ' "test/input/issue-1242/blah.*"'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.ok(/^ERROR: ENOENT/.test(stderr)); done(); }); }); it('"?" in glob string should not match "/"', function(done) { var command = uglifyjscmd + ' "test/input?issue-1242/foo.*"'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.ok(/^ERROR: ENOENT/.test(stderr)); done(); }); }); it("Should handle special characters in glob string", function(done) { var command = uglifyjscmd + ' "test/input/issue-1632/^{*}[???](*)+$.??" -cm'; exec(command, function(err, stdout) { if (err) throw err; assert.strictEqual(stdout, "console.log(x);\n"); done(); }); }); it("Should handle array of glob strings - matching and otherwise", function(done) { var dir = "test/input/issue-1242"; var command = uglifyjscmd + ' "' + [ path.join(dir, "b*.es5"), path.join(dir, "z*.es5"), path.join(dir, "*.js") ].join('" "') + '"'; exec(command, function(err, stdout, stderr) { assert.ok(err); assert.ok(/^ERROR: ENOENT.*?z\*\.es5/.test(stderr)); done(); }); }); }); UglifyJS2-3.6.3/test/mocha/ie8.js000066400000000000000000000014731355252637300164120ustar00rootroot00000000000000var assert = require("assert"); var UglifyJS = require("../.."); describe("ie8", function() { it("Should be able to minify() with undefined as catch parameter in a try...catch statement", function() { assert.strictEqual( UglifyJS.minify([ "function a(b){", " try {", " throw 'Stuff';", " } catch (undefined) {", " console.log('caught: ' + undefined);", " }", " console.log('undefined is ' + undefined);", " return b === undefined;", "};", ].join("\n")).code, 'function a(o){try{throw"Stuff"}catch(o){console.log("caught: "+o)}return console.log("undefined is "+void 0),void 0===o}' ); }); }); UglifyJS2-3.6.3/test/mocha/let.js000066400000000000000000000035061355252637300165100ustar00rootroot00000000000000var assert = require("assert"); var UglifyJS = require("../.."); describe("let", function() { this.timeout(30000); it("Should not produce reserved keywords as variable name in mangle", function() { // Produce a lot of variables in a function and run it through mangle. var s = '"dddddeeeeelllllooooottttt"; function foo() {'; for (var i = 0; i < 18000; i++) { s += "var v" + i + "=0;"; } s += '}'; var result = UglifyJS.minify(s, { compress: false }).code; // Verify that select keywords and reserved keywords not produced [ "do", "let", "var", ].forEach(function(name) { assert.strictEqual(result.indexOf("var " + name + "="), -1); }); // Verify that the variable names that appeared immediately before // and after the erroneously generated variable name still exist // to show the test generated enough symbols. [ "to", "eo", "eet", "fet", "rar", "oar", ].forEach(function(name) { assert.notStrictEqual(result.indexOf("var " + name + "="), -1); }); }); it("Should quote mangled properties that are reserved keywords", function() { var s = '"rrrrrnnnnniiiiiaaaaa";'; for (var i = 0; i < 18000; i++) { s += "v.b" + i + ";"; } var result = UglifyJS.minify(s, { compress: false, ie8: true, mangle: { properties: true, } }).code; [ "in", "var", ].forEach(function(name) { assert.notStrictEqual(result.indexOf(name), -1); assert.notStrictEqual(result.indexOf('v["' + name + '"]'), -1); }); }); }); UglifyJS2-3.6.3/test/mocha/line-endings.js000066400000000000000000000035641355252637300203040ustar00rootroot00000000000000var assert = require("assert"); var UglifyJS = require("../node"); describe("line-endings", function() { var options = { compress: false, mangle: false, output: { beautify: false, comments: /^!/, } }; var expected_code = '/*!one\n2\n3*/\nfunction f(x){if(x)return 3}'; it("Should parse LF line endings", function() { var js = '/*!one\n2\n3*///comment\nfunction f(x) {\n if (x)\n//comment\n return 3;\n}\n'; var result = UglifyJS.minify(js, options); assert.strictEqual(result.code, expected_code); }); it("Should parse CR/LF line endings", function() { var js = '/*!one\r\n2\r\n3*///comment\r\nfunction f(x) {\r\n if (x)\r\n//comment\r\n return 3;\r\n}\r\n'; var result = UglifyJS.minify(js, options); assert.strictEqual(result.code, expected_code); }); it("Should parse CR line endings", function() { var js = '/*!one\r2\r3*///comment\rfunction f(x) {\r if (x)\r//comment\r return 3;\r}\r'; var result = UglifyJS.minify(js, options); assert.strictEqual(result.code, expected_code); }); it("Should not allow line terminators in regexp", function() { var inputs = [ "/\n/", "/\r/", "/\u2028/", "/\u2029/", "/\\\n/", "/\\\r/", "/\\\u2028/", "/\\\u2029/", "/someRandomTextLike[]()*AndThen\n/" ] var test = function(input) { return function() { UglifyJS.parse(input); } } var fail = function(e) { return e instanceof UglifyJS.JS_Parse_Error && e.message === "Unexpected line terminator"; } for (var i = 0; i < inputs.length; i++) { assert.throws(test(inputs[i]), fail); } }); }); UglifyJS2-3.6.3/test/mocha/minify-file-map.js000066400000000000000000000031721355252637300207060ustar00rootroot00000000000000var assert = require("assert"); var UglifyJS = require("../.."); describe("Input file as map", function() { it("Should accept object", function() { var jsMap = { '/scripts/foo.js': 'var foo = {"x": 1, y: 2, \'z\': 3};' }; var result = UglifyJS.minify(jsMap, {sourceMap: true}); var map = JSON.parse(result.map); assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};'); assert.deepEqual(map.sources, ['/scripts/foo.js']); assert.strictEqual(map.file, undefined); result = UglifyJS.minify(jsMap); assert.strictEqual(result.map, undefined); result = UglifyJS.minify(jsMap, {sourceMap: {filename: 'out.js'}}); map = JSON.parse(result.map); assert.strictEqual(map.file, 'out.js'); }); it("Should accept array of strings", function() { var jsSeq = [ 'var foo = {"x": 1, y: 2, \'z\': 3};', 'var bar = 15;' ]; var result = UglifyJS.minify(jsSeq, {sourceMap: true}); var map = JSON.parse(result.map); assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3},bar=15;'); assert.deepEqual(map.sources, ['0', '1']); }); it("Should correctly include source", function() { var jsMap = { '/scripts/foo.js': 'var foo = {"x": 1, y: 2, \'z\': 3};' }; var result = UglifyJS.minify(jsMap, {sourceMap: {includeSources: true}}); var map = JSON.parse(result.map); assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};'); assert.deepEqual(map.sourcesContent, ['var foo = {"x": 1, y: 2, \'z\': 3};']); }); }); UglifyJS2-3.6.3/test/mocha/minify.js000066400000000000000000000334321355252637300172200ustar00rootroot00000000000000var assert = require("assert"); var readFileSync = require("fs").readFileSync; var run_code = require("../sandbox").run_code; var UglifyJS = require("../../"); function read(path) { return readFileSync(path, "utf8"); } describe("minify", function() { it("Should test basic sanity of minify with default options", function() { var js = 'function foo(bar) { if (bar) return 3; else return 7; var u = not_called(); }'; var result = UglifyJS.minify(js); assert.strictEqual(result.code, 'function foo(n){return n?3:7}'); }); it("Should skip inherited keys from `files`", function() { var files = Object.create({ skip: this }); files[0] = "alert(1 + 1)"; var result = UglifyJS.minify(files); assert.strictEqual(result.code, "alert(2);"); }); it("Should work with mangle.cache", function() { var cache = {}; var original = ""; var compressed = ""; [ "bar.es5", "baz.es5", "foo.es5", "qux.js", ].forEach(function(file) { var code = read("test/input/issue-1242/" + file); var result = UglifyJS.minify(code, { mangle: { cache: cache, toplevel: true } }); if (result.error) throw result.error; original += code; compressed += result.code; }); assert.strictEqual(JSON.stringify(cache).slice(0, 10), '{"props":{'); assert.strictEqual(compressed, [ "function n(n){return 3*n}", "function r(n){return n/2}", "var c=console.log.bind(console);", 'function o(o){c("Foo:",2*o)}', "var a=n(3),b=r(12);", 'c("qux",a,b),o(11);', ].join("")); assert.strictEqual(run_code(compressed), run_code(original)); }); it("Should work with nameCache", function() { var cache = {}; var original = ""; var compressed = ""; [ "bar.es5", "baz.es5", "foo.es5", "qux.js", ].forEach(function(file) { var code = read("test/input/issue-1242/" + file); var result = UglifyJS.minify(code, { mangle: { toplevel: true }, nameCache: cache }); if (result.error) throw result.error; original += code; compressed += result.code; }); assert.strictEqual(JSON.stringify(cache).slice(0, 18), '{"vars":{"props":{'); assert.strictEqual(compressed, [ "function n(n){return 3*n}", "function r(n){return n/2}", "var c=console.log.bind(console);", 'function o(o){c("Foo:",2*o)}', "var a=n(3),b=r(12);", 'c("qux",a,b),o(11);', ].join("")); assert.strictEqual(run_code(compressed), run_code(original)); }); it("Should avoid cached names when mangling top-level variables", function() { var cache = {}; var original = ""; var compressed = ""; [ '"xxxyy";var i={s:1};', '"xxyyy";var j={t:2,u:3},k=4;', 'console.log(i.s,j.t,j.u,k);', ].forEach(function(code) { var result = UglifyJS.minify(code, { compress: false, mangle: { properties: true, toplevel: true }, nameCache: cache }); if (result.error) throw result.error; original += code; compressed += result.code; }); assert.strictEqual(compressed, [ '"xxxyy";var x={x:1};', '"xxyyy";var y={y:2,a:3},a=4;', 'console.log(x.x,y.y,y.a,a);', ].join("")); assert.strictEqual(run_code(compressed), run_code(original)); }); it("Should avoid cached names when mangling inner-scoped variables", function() { var cache = {}; var original = ""; var compressed = ""; [ 'var extend = function(a, b) { console.log("extend"); a(); b(); }; function A() { console.log("A"); };', 'var B = function(A) { function B() { console.log("B") }; extend(B, A); return B; }(A);', ].forEach(function(code) { var result = UglifyJS.minify(code, { compress: false, nameCache: cache, toplevel: true, }); if (result.error) throw result.error; original += code; compressed += result.code; }); assert.strictEqual(compressed, [ 'var o=function(o,n){console.log("extend");o();n()};function n(){console.log("A")}', 'var e=function(n){function e(){console.log("B")}o(e,n);return e}(n);', ].join("")); assert.strictEqual(run_code(compressed), run_code(original)); }); it("Should not parse invalid use of reserved words", function() { assert.strictEqual(UglifyJS.minify("function enum(){}").error, undefined); assert.strictEqual(UglifyJS.minify("function static(){}").error, undefined); assert.strictEqual(UglifyJS.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};'; var result = UglifyJS.minify(js, { output: { keep_quoted_props: true }}); assert.strictEqual(result.code, 'var foo={"x":1,y:2,"z":3};'); }); it("Should preserve quote styles when quote_style is 3", function() { var js = 'var foo = {"x": 1, y: 2, \'z\': 3};'; var result = UglifyJS.minify(js, { output: { keep_quoted_props: true, quote_style: 3 }}); assert.strictEqual(result.code, 'var foo={"x":1,y:2,\'z\':3};'); }); it("Should not preserve quotes in object literals when disabled", function() { var js = 'var foo = {"x": 1, y: 2, \'z\': 3};'; var result = UglifyJS.minify(js, { output: { keep_quoted_props: false, quote_style: 3 }}); assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};'); }); }); describe("mangleProperties", function() { it("Shouldn't mangle quoted properties", function() { var js = 'a["foo"] = "bar"; a.color = "red"; x = {"bar": 10};'; var result = UglifyJS.minify(js, { compress: { properties: false }, mangle: { properties: { keep_quoted: true } }, output: { keep_quoted_props: true, quote_style: 3 } }); assert.strictEqual(result.code, 'a["foo"]="bar",a.a="red",x={"bar":10};'); }); it("Should not mangle quoted property within dead code", function() { var result = UglifyJS.minify('({ "keep": 1 }); g.keep = g.change;', { mangle: { properties: { keep_quoted: true } } }); if (result.error) throw result.error; assert.strictEqual(result.code, "g.keep=g.g;"); }); }); describe("#__PURE__", function() { it("Should drop #__PURE__ hint after use", function() { var result = UglifyJS.minify('//@__PURE__ comment1 #__PURE__ comment2\n foo(), bar();', { output: { comments: "all", beautify: false, } }); var code = result.code; assert.strictEqual(code, "// comment1 comment2\nbar();"); }); it("Should drop #__PURE__ hint if function is retained", function() { var result = UglifyJS.minify("var a = /*#__PURE__*/(function(){ foo(); })();", { output: { comments: "all", beautify: false, } }); var code = result.code; assert.strictEqual(code, "var a=/* */function(){foo()}();"); }) }); describe("JS_Parse_Error", function() { it("Should return syntax error", function() { var result = UglifyJS.minify("function f(a{}"); var err = result.error; assert.ok(err instanceof Error); assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Unexpected token: punc «{», expected: punc «,»"); assert.strictEqual(err.filename, "0"); assert.strictEqual(err.line, 1); assert.strictEqual(err.col, 12); }); it("Should reject duplicated label name", function() { var result = UglifyJS.minify("L:{L:{}}"); var err = result.error; assert.ok(err instanceof Error); assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Label L defined twice"); assert.strictEqual(err.filename, "0"); assert.strictEqual(err.line, 1); assert.strictEqual(err.col, 4); }); }); describe("global_defs", function() { it("Should throw for non-trivial expressions", function() { var result = UglifyJS.minify("alert(42);", { compress: { global_defs: { "@alert": "debugger" } } }); var err = result.error; assert.ok(err instanceof Error); assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Unexpected token: keyword «debugger»"); }); it("Should skip inherited properties", function() { var foo = Object.create({ skip: this }); foo.bar = 42; var result = UglifyJS.minify("alert(FOO);", { compress: { global_defs: { FOO: foo } } }); assert.strictEqual(result.code, "alert({bar:42});"); }); }); describe("collapse_vars", function() { it("Should not produce invalid AST", function() { var code = [ "function f(a) {", " a = x();", " return a;", "}", "f();", ].join("\n"); var ast = UglifyJS.minify(code, { compress: false, mangle: false, output: { ast: true }, }).ast; assert.strictEqual(ast.TYPE, "Toplevel"); assert.strictEqual(ast.body.length, 2); assert.strictEqual(ast.body[0].TYPE, "Defun"); assert.strictEqual(ast.body[0].body.length, 2); assert.strictEqual(ast.body[0].body[0].TYPE, "SimpleStatement"); var stat = ast.body[0].body[0]; UglifyJS.minify(ast, { compress: { sequences: false }, mangle: false }); assert.ok(stat.body); assert.strictEqual(stat.print_to_string(), "a=x()"); }); }); describe("rename", function() { it("Should be repeatable", function() { var code = "!function(x){return x(x)}(y);"; for (var i = 0; i < 2; i++) { assert.strictEqual(UglifyJS.minify(code, { compress: { toplevel: true, }, rename: true, }).code, "var a;(a=y)(a);"); } }); }); describe("enclose", function() { var code = read("test/input/enclose/input.js"); it("Should work with true", function() { var result = UglifyJS.minify(code, { compress: false, enclose: true, mangle: false, }); if (result.error) throw result.error; assert.strictEqual(result.code, '(function(){function enclose(){console.log("test enclose")}enclose()})();'); }); it("Should work with arg", function() { var result = UglifyJS.minify(code, { compress: false, enclose: 'undefined', mangle: false, }); if (result.error) throw result.error; assert.strictEqual(result.code, '(function(undefined){function enclose(){console.log("test enclose")}enclose()})();'); }); it("Should work with arg:value", function() { var result = UglifyJS.minify(code, { compress: false, enclose: 'window,undefined:window', mangle: false, }); if (result.error) throw result.error; assert.strictEqual(result.code, '(function(window,undefined){function enclose(){console.log("test enclose")}enclose()})(window);'); }); it("Should work alongside wrap", function() { var result = UglifyJS.minify(code, { compress: false, enclose: 'window,undefined:window', mangle: false, wrap: 'exports', }); if (result.error) throw result.error; assert.strictEqual(result.code, '(function(exports){(function(window,undefined){function enclose(){console.log("test enclose")}enclose()})(window)})(typeof exports=="undefined"?exports={}:exports);'); }); }); }); UglifyJS2-3.6.3/test/mocha/number-literal.js000066400000000000000000000013571355252637300206500ustar00rootroot00000000000000var assert = require("assert"); var UglifyJS = require("../node"); describe("Number literals", function() { it("Should not allow legacy octal literals in strict mode", function() { var inputs = [ '"use strict";00;', '"use strict"; var foo = 00;' ]; var test = function(input) { return function() { UglifyJS.parse(input); } }; var error = function(e) { return e instanceof UglifyJS.JS_Parse_Error && e.message === "Legacy octal literals are not allowed in strict mode"; }; for (var i = 0; i < inputs.length; i++) { assert.throws(test(inputs[i]), error, inputs[i]); } }); }); UglifyJS2-3.6.3/test/mocha/operator.js000066400000000000000000000511341355252637300175570ustar00rootroot00000000000000var assert = require("assert"); var UglifyJS = require("../.."); describe("operator", function() { it("Should handle mixing of ++/+/--/- correctly", function() { function evaluate(exp) { return new Function("var a=1,b=2,c=" + exp + ";return{a:a,b:b,c:c}")(); } [ "", "+", "-" ].forEach(function(p) { [ "++a", "--a", "a", "a--", "a++" ].forEach(function(a) { [ "+", "-" ].forEach(function(o) { [ "", "+", "-" ].forEach(function(q) { [ "++b", "--b", "b", "b--", "b++" ].forEach(function(b) { var exp = [p, a, o, q, b].join(" "); var orig = evaluate(exp); var uglify = evaluate(UglifyJS.parse(exp).print_to_string()); assert.strictEqual(orig.a, uglify.a); assert.strictEqual(orig.b, uglify.b); assert.strictEqual(orig.c, uglify.c); var beautify = evaluate(UglifyJS.parse(exp).print_to_string({ beautify: true })); assert.strictEqual(orig.a, beautify.a); assert.strictEqual(orig.b, beautify.b); assert.strictEqual(orig.c, beautify.c); }); }); }); }); }); }); it("Should remove extraneous spaces", function() { [ [ "++a + ++b", "++a+ ++b" ], [ "++a + --b", "++a+--b" ], [ "++a + b", "++a+b" ], [ "++a + b--", "++a+b--" ], [ "++a + b++", "++a+b++" ], [ "++a + + ++b", "++a+ + ++b" ], [ "++a + + --b", "++a+ +--b" ], [ "++a + + b", "++a+ +b" ], [ "++a + + b--", "++a+ +b--" ], [ "++a + + b++", "++a+ +b++" ], [ "++a + - ++b", "++a+-++b" ], [ "++a + - --b", "++a+- --b" ], [ "++a + - b", "++a+-b" ], [ "++a + - b--", "++a+-b--" ], [ "++a + - b++", "++a+-b++" ], [ "++a - ++b", "++a-++b" ], [ "++a - --b", "++a- --b" ], [ "++a - b", "++a-b" ], [ "++a - b--", "++a-b--" ], [ "++a - b++", "++a-b++" ], [ "++a - + ++b", "++a-+ ++b" ], [ "++a - + --b", "++a-+--b" ], [ "++a - + b", "++a-+b" ], [ "++a - + b--", "++a-+b--" ], [ "++a - + b++", "++a-+b++" ], [ "++a - - ++b", "++a- -++b" ], [ "++a - - --b", "++a- - --b" ], [ "++a - - b", "++a- -b" ], [ "++a - - b--", "++a- -b--" ], [ "++a - - b++", "++a- -b++" ], [ "--a + ++b", "--a+ ++b" ], [ "--a + --b", "--a+--b" ], [ "--a + b", "--a+b" ], [ "--a + b--", "--a+b--" ], [ "--a + b++", "--a+b++" ], [ "--a + + ++b", "--a+ + ++b" ], [ "--a + + --b", "--a+ +--b" ], [ "--a + + b", "--a+ +b" ], [ "--a + + b--", "--a+ +b--" ], [ "--a + + b++", "--a+ +b++" ], [ "--a + - ++b", "--a+-++b" ], [ "--a + - --b", "--a+- --b" ], [ "--a + - b", "--a+-b" ], [ "--a + - b--", "--a+-b--" ], [ "--a + - b++", "--a+-b++" ], [ "--a - ++b", "--a-++b" ], [ "--a - --b", "--a- --b" ], [ "--a - b", "--a-b" ], [ "--a - b--", "--a-b--" ], [ "--a - b++", "--a-b++" ], [ "--a - + ++b", "--a-+ ++b" ], [ "--a - + --b", "--a-+--b" ], [ "--a - + b", "--a-+b" ], [ "--a - + b--", "--a-+b--" ], [ "--a - + b++", "--a-+b++" ], [ "--a - - ++b", "--a- -++b" ], [ "--a - - --b", "--a- - --b" ], [ "--a - - b", "--a- -b" ], [ "--a - - b--", "--a- -b--" ], [ "--a - - b++", "--a- -b++" ], [ "a + ++b", "a+ ++b" ], [ "a + --b", "a+--b" ], [ "a + b", "a+b" ], [ "a + b--", "a+b--" ], [ "a + b++", "a+b++" ], [ "a + + ++b", "a+ + ++b" ], [ "a + + --b", "a+ +--b" ], [ "a + + b", "a+ +b" ], [ "a + + b--", "a+ +b--" ], [ "a + + b++", "a+ +b++" ], [ "a + - ++b", "a+-++b" ], [ "a + - --b", "a+- --b" ], [ "a + - b", "a+-b" ], [ "a + - b--", "a+-b--" ], [ "a + - b++", "a+-b++" ], [ "a - ++b", "a-++b" ], [ "a - --b", "a- --b" ], [ "a - b", "a-b" ], [ "a - b--", "a-b--" ], [ "a - b++", "a-b++" ], [ "a - + ++b", "a-+ ++b" ], [ "a - + --b", "a-+--b" ], [ "a - + b", "a-+b" ], [ "a - + b--", "a-+b--" ], [ "a - + b++", "a-+b++" ], [ "a - - ++b", "a- -++b" ], [ "a - - --b", "a- - --b" ], [ "a - - b", "a- -b" ], [ "a - - b--", "a- -b--" ], [ "a - - b++", "a- -b++" ], [ "a-- + ++b", "a--+ ++b" ], [ "a-- + --b", "a--+--b" ], [ "a-- + b", "a--+b" ], [ "a-- + b--", "a--+b--" ], [ "a-- + b++", "a--+b++" ], [ "a-- + + ++b", "a--+ + ++b" ], [ "a-- + + --b", "a--+ +--b" ], [ "a-- + + b", "a--+ +b" ], [ "a-- + + b--", "a--+ +b--" ], [ "a-- + + b++", "a--+ +b++" ], [ "a-- + - ++b", "a--+-++b" ], [ "a-- + - --b", "a--+- --b" ], [ "a-- + - b", "a--+-b" ], [ "a-- + - b--", "a--+-b--" ], [ "a-- + - b++", "a--+-b++" ], [ "a-- - ++b", "a---++b" ], [ "a-- - --b", "a--- --b" ], [ "a-- - b", "a---b" ], [ "a-- - b--", "a---b--" ], [ "a-- - b++", "a---b++" ], [ "a-- - + ++b", "a---+ ++b" ], [ "a-- - + --b", "a---+--b" ], [ "a-- - + b", "a---+b" ], [ "a-- - + b--", "a---+b--" ], [ "a-- - + b++", "a---+b++" ], [ "a-- - - ++b", "a--- -++b" ], [ "a-- - - --b", "a--- - --b" ], [ "a-- - - b", "a--- -b" ], [ "a-- - - b--", "a--- -b--" ], [ "a-- - - b++", "a--- -b++" ], [ "a++ + ++b", "a+++ ++b" ], [ "a++ + --b", "a+++--b" ], [ "a++ + b", "a+++b" ], [ "a++ + b--", "a+++b--" ], [ "a++ + b++", "a+++b++" ], [ "a++ + + ++b", "a+++ + ++b" ], [ "a++ + + --b", "a+++ +--b" ], [ "a++ + + b", "a+++ +b" ], [ "a++ + + b--", "a+++ +b--" ], [ "a++ + + b++", "a+++ +b++" ], [ "a++ + - ++b", "a+++-++b" ], [ "a++ + - --b", "a+++- --b" ], [ "a++ + - b", "a+++-b" ], [ "a++ + - b--", "a+++-b--" ], [ "a++ + - b++", "a+++-b++" ], [ "a++ - ++b", "a++-++b" ], [ "a++ - --b", "a++- --b" ], [ "a++ - b", "a++-b" ], [ "a++ - b--", "a++-b--" ], [ "a++ - b++", "a++-b++" ], [ "a++ - + ++b", "a++-+ ++b" ], [ "a++ - + --b", "a++-+--b" ], [ "a++ - + b", "a++-+b" ], [ "a++ - + b--", "a++-+b--" ], [ "a++ - + b++", "a++-+b++" ], [ "a++ - - ++b", "a++- -++b" ], [ "a++ - - --b", "a++- - --b" ], [ "a++ - - b", "a++- -b" ], [ "a++ - - b--", "a++- -b--" ], [ "a++ - - b++", "a++- -b++" ], [ "+ ++a + ++b", "+ ++a+ ++b" ], [ "+ ++a + --b", "+ ++a+--b" ], [ "+ ++a + b", "+ ++a+b" ], [ "+ ++a + b--", "+ ++a+b--" ], [ "+ ++a + b++", "+ ++a+b++" ], [ "+ ++a + + ++b", "+ ++a+ + ++b" ], [ "+ ++a + + --b", "+ ++a+ +--b" ], [ "+ ++a + + b", "+ ++a+ +b" ], [ "+ ++a + + b--", "+ ++a+ +b--" ], [ "+ ++a + + b++", "+ ++a+ +b++" ], [ "+ ++a + - ++b", "+ ++a+-++b" ], [ "+ ++a + - --b", "+ ++a+- --b" ], [ "+ ++a + - b", "+ ++a+-b" ], [ "+ ++a + - b--", "+ ++a+-b--" ], [ "+ ++a + - b++", "+ ++a+-b++" ], [ "+ ++a - ++b", "+ ++a-++b" ], [ "+ ++a - --b", "+ ++a- --b" ], [ "+ ++a - b", "+ ++a-b" ], [ "+ ++a - b--", "+ ++a-b--" ], [ "+ ++a - b++", "+ ++a-b++" ], [ "+ ++a - + ++b", "+ ++a-+ ++b" ], [ "+ ++a - + --b", "+ ++a-+--b" ], [ "+ ++a - + b", "+ ++a-+b" ], [ "+ ++a - + b--", "+ ++a-+b--" ], [ "+ ++a - + b++", "+ ++a-+b++" ], [ "+ ++a - - ++b", "+ ++a- -++b" ], [ "+ ++a - - --b", "+ ++a- - --b" ], [ "+ ++a - - b", "+ ++a- -b" ], [ "+ ++a - - b--", "+ ++a- -b--" ], [ "+ ++a - - b++", "+ ++a- -b++" ], [ "+ --a + ++b", "+--a+ ++b" ], [ "+ --a + --b", "+--a+--b" ], [ "+ --a + b", "+--a+b" ], [ "+ --a + b--", "+--a+b--" ], [ "+ --a + b++", "+--a+b++" ], [ "+ --a + + ++b", "+--a+ + ++b" ], [ "+ --a + + --b", "+--a+ +--b" ], [ "+ --a + + b", "+--a+ +b" ], [ "+ --a + + b--", "+--a+ +b--" ], [ "+ --a + + b++", "+--a+ +b++" ], [ "+ --a + - ++b", "+--a+-++b" ], [ "+ --a + - --b", "+--a+- --b" ], [ "+ --a + - b", "+--a+-b" ], [ "+ --a + - b--", "+--a+-b--" ], [ "+ --a + - b++", "+--a+-b++" ], [ "+ --a - ++b", "+--a-++b" ], [ "+ --a - --b", "+--a- --b" ], [ "+ --a - b", "+--a-b" ], [ "+ --a - b--", "+--a-b--" ], [ "+ --a - b++", "+--a-b++" ], [ "+ --a - + ++b", "+--a-+ ++b" ], [ "+ --a - + --b", "+--a-+--b" ], [ "+ --a - + b", "+--a-+b" ], [ "+ --a - + b--", "+--a-+b--" ], [ "+ --a - + b++", "+--a-+b++" ], [ "+ --a - - ++b", "+--a- -++b" ], [ "+ --a - - --b", "+--a- - --b" ], [ "+ --a - - b", "+--a- -b" ], [ "+ --a - - b--", "+--a- -b--" ], [ "+ --a - - b++", "+--a- -b++" ], [ "+ a + ++b", "+a+ ++b" ], [ "+ a + --b", "+a+--b" ], [ "+ a + b", "+a+b" ], [ "+ a + b--", "+a+b--" ], [ "+ a + b++", "+a+b++" ], [ "+ a + + ++b", "+a+ + ++b" ], [ "+ a + + --b", "+a+ +--b" ], [ "+ a + + b", "+a+ +b" ], [ "+ a + + b--", "+a+ +b--" ], [ "+ a + + b++", "+a+ +b++" ], [ "+ a + - ++b", "+a+-++b" ], [ "+ a + - --b", "+a+- --b" ], [ "+ a + - b", "+a+-b" ], [ "+ a + - b--", "+a+-b--" ], [ "+ a + - b++", "+a+-b++" ], [ "+ a - ++b", "+a-++b" ], [ "+ a - --b", "+a- --b" ], [ "+ a - b", "+a-b" ], [ "+ a - b--", "+a-b--" ], [ "+ a - b++", "+a-b++" ], [ "+ a - + ++b", "+a-+ ++b" ], [ "+ a - + --b", "+a-+--b" ], [ "+ a - + b", "+a-+b" ], [ "+ a - + b--", "+a-+b--" ], [ "+ a - + b++", "+a-+b++" ], [ "+ a - - ++b", "+a- -++b" ], [ "+ a - - --b", "+a- - --b" ], [ "+ a - - b", "+a- -b" ], [ "+ a - - b--", "+a- -b--" ], [ "+ a - - b++", "+a- -b++" ], [ "+ a-- + ++b", "+a--+ ++b" ], [ "+ a-- + --b", "+a--+--b" ], [ "+ a-- + b", "+a--+b" ], [ "+ a-- + b--", "+a--+b--" ], [ "+ a-- + b++", "+a--+b++" ], [ "+ a-- + + ++b", "+a--+ + ++b" ], [ "+ a-- + + --b", "+a--+ +--b" ], [ "+ a-- + + b", "+a--+ +b" ], [ "+ a-- + + b--", "+a--+ +b--" ], [ "+ a-- + + b++", "+a--+ +b++" ], [ "+ a-- + - ++b", "+a--+-++b" ], [ "+ a-- + - --b", "+a--+- --b" ], [ "+ a-- + - b", "+a--+-b" ], [ "+ a-- + - b--", "+a--+-b--" ], [ "+ a-- + - b++", "+a--+-b++" ], [ "+ a-- - ++b", "+a---++b" ], [ "+ a-- - --b", "+a--- --b" ], [ "+ a-- - b", "+a---b" ], [ "+ a-- - b--", "+a---b--" ], [ "+ a-- - b++", "+a---b++" ], [ "+ a-- - + ++b", "+a---+ ++b" ], [ "+ a-- - + --b", "+a---+--b" ], [ "+ a-- - + b", "+a---+b" ], [ "+ a-- - + b--", "+a---+b--" ], [ "+ a-- - + b++", "+a---+b++" ], [ "+ a-- - - ++b", "+a--- -++b" ], [ "+ a-- - - --b", "+a--- - --b" ], [ "+ a-- - - b", "+a--- -b" ], [ "+ a-- - - b--", "+a--- -b--" ], [ "+ a-- - - b++", "+a--- -b++" ], [ "+ a++ + ++b", "+a+++ ++b" ], [ "+ a++ + --b", "+a+++--b" ], [ "+ a++ + b", "+a+++b" ], [ "+ a++ + b--", "+a+++b--" ], [ "+ a++ + b++", "+a+++b++" ], [ "+ a++ + + ++b", "+a+++ + ++b" ], [ "+ a++ + + --b", "+a+++ +--b" ], [ "+ a++ + + b", "+a+++ +b" ], [ "+ a++ + + b--", "+a+++ +b--" ], [ "+ a++ + + b++", "+a+++ +b++" ], [ "+ a++ + - ++b", "+a+++-++b" ], [ "+ a++ + - --b", "+a+++- --b" ], [ "+ a++ + - b", "+a+++-b" ], [ "+ a++ + - b--", "+a+++-b--" ], [ "+ a++ + - b++", "+a+++-b++" ], [ "+ a++ - ++b", "+a++-++b" ], [ "+ a++ - --b", "+a++- --b" ], [ "+ a++ - b", "+a++-b" ], [ "+ a++ - b--", "+a++-b--" ], [ "+ a++ - b++", "+a++-b++" ], [ "+ a++ - + ++b", "+a++-+ ++b" ], [ "+ a++ - + --b", "+a++-+--b" ], [ "+ a++ - + b", "+a++-+b" ], [ "+ a++ - + b--", "+a++-+b--" ], [ "+ a++ - + b++", "+a++-+b++" ], [ "+ a++ - - ++b", "+a++- -++b" ], [ "+ a++ - - --b", "+a++- - --b" ], [ "+ a++ - - b", "+a++- -b" ], [ "+ a++ - - b--", "+a++- -b--" ], [ "+ a++ - - b++", "+a++- -b++" ], [ "- ++a + ++b", "-++a+ ++b" ], [ "- ++a + --b", "-++a+--b" ], [ "- ++a + b", "-++a+b" ], [ "- ++a + b--", "-++a+b--" ], [ "- ++a + b++", "-++a+b++" ], [ "- ++a + + ++b", "-++a+ + ++b" ], [ "- ++a + + --b", "-++a+ +--b" ], [ "- ++a + + b", "-++a+ +b" ], [ "- ++a + + b--", "-++a+ +b--" ], [ "- ++a + + b++", "-++a+ +b++" ], [ "- ++a + - ++b", "-++a+-++b" ], [ "- ++a + - --b", "-++a+- --b" ], [ "- ++a + - b", "-++a+-b" ], [ "- ++a + - b--", "-++a+-b--" ], [ "- ++a + - b++", "-++a+-b++" ], [ "- ++a - ++b", "-++a-++b" ], [ "- ++a - --b", "-++a- --b" ], [ "- ++a - b", "-++a-b" ], [ "- ++a - b--", "-++a-b--" ], [ "- ++a - b++", "-++a-b++" ], [ "- ++a - + ++b", "-++a-+ ++b" ], [ "- ++a - + --b", "-++a-+--b" ], [ "- ++a - + b", "-++a-+b" ], [ "- ++a - + b--", "-++a-+b--" ], [ "- ++a - + b++", "-++a-+b++" ], [ "- ++a - - ++b", "-++a- -++b" ], [ "- ++a - - --b", "-++a- - --b" ], [ "- ++a - - b", "-++a- -b" ], [ "- ++a - - b--", "-++a- -b--" ], [ "- ++a - - b++", "-++a- -b++" ], [ "- --a + ++b", "- --a+ ++b" ], [ "- --a + --b", "- --a+--b" ], [ "- --a + b", "- --a+b" ], [ "- --a + b--", "- --a+b--" ], [ "- --a + b++", "- --a+b++" ], [ "- --a + + ++b", "- --a+ + ++b" ], [ "- --a + + --b", "- --a+ +--b" ], [ "- --a + + b", "- --a+ +b" ], [ "- --a + + b--", "- --a+ +b--" ], [ "- --a + + b++", "- --a+ +b++" ], [ "- --a + - ++b", "- --a+-++b" ], [ "- --a + - --b", "- --a+- --b" ], [ "- --a + - b", "- --a+-b" ], [ "- --a + - b--", "- --a+-b--" ], [ "- --a + - b++", "- --a+-b++" ], [ "- --a - ++b", "- --a-++b" ], [ "- --a - --b", "- --a- --b" ], [ "- --a - b", "- --a-b" ], [ "- --a - b--", "- --a-b--" ], [ "- --a - b++", "- --a-b++" ], [ "- --a - + ++b", "- --a-+ ++b" ], [ "- --a - + --b", "- --a-+--b" ], [ "- --a - + b", "- --a-+b" ], [ "- --a - + b--", "- --a-+b--" ], [ "- --a - + b++", "- --a-+b++" ], [ "- --a - - ++b", "- --a- -++b" ], [ "- --a - - --b", "- --a- - --b" ], [ "- --a - - b", "- --a- -b" ], [ "- --a - - b--", "- --a- -b--" ], [ "- --a - - b++", "- --a- -b++" ], [ "- a + ++b", "-a+ ++b" ], [ "- a + --b", "-a+--b" ], [ "- a + b", "-a+b" ], [ "- a + b--", "-a+b--" ], [ "- a + b++", "-a+b++" ], [ "- a + + ++b", "-a+ + ++b" ], [ "- a + + --b", "-a+ +--b" ], [ "- a + + b", "-a+ +b" ], [ "- a + + b--", "-a+ +b--" ], [ "- a + + b++", "-a+ +b++" ], [ "- a + - ++b", "-a+-++b" ], [ "- a + - --b", "-a+- --b" ], [ "- a + - b", "-a+-b" ], [ "- a + - b--", "-a+-b--" ], [ "- a + - b++", "-a+-b++" ], [ "- a - ++b", "-a-++b" ], [ "- a - --b", "-a- --b" ], [ "- a - b", "-a-b" ], [ "- a - b--", "-a-b--" ], [ "- a - b++", "-a-b++" ], [ "- a - + ++b", "-a-+ ++b" ], [ "- a - + --b", "-a-+--b" ], [ "- a - + b", "-a-+b" ], [ "- a - + b--", "-a-+b--" ], [ "- a - + b++", "-a-+b++" ], [ "- a - - ++b", "-a- -++b" ], [ "- a - - --b", "-a- - --b" ], [ "- a - - b", "-a- -b" ], [ "- a - - b--", "-a- -b--" ], [ "- a - - b++", "-a- -b++" ], [ "- a-- + ++b", "-a--+ ++b" ], [ "- a-- + --b", "-a--+--b" ], [ "- a-- + b", "-a--+b" ], [ "- a-- + b--", "-a--+b--" ], [ "- a-- + b++", "-a--+b++" ], [ "- a-- + + ++b", "-a--+ + ++b" ], [ "- a-- + + --b", "-a--+ +--b" ], [ "- a-- + + b", "-a--+ +b" ], [ "- a-- + + b--", "-a--+ +b--" ], [ "- a-- + + b++", "-a--+ +b++" ], [ "- a-- + - ++b", "-a--+-++b" ], [ "- a-- + - --b", "-a--+- --b" ], [ "- a-- + - b", "-a--+-b" ], [ "- a-- + - b--", "-a--+-b--" ], [ "- a-- + - b++", "-a--+-b++" ], [ "- a-- - ++b", "-a---++b" ], [ "- a-- - --b", "-a--- --b" ], [ "- a-- - b", "-a---b" ], [ "- a-- - b--", "-a---b--" ], [ "- a-- - b++", "-a---b++" ], [ "- a-- - + ++b", "-a---+ ++b" ], [ "- a-- - + --b", "-a---+--b" ], [ "- a-- - + b", "-a---+b" ], [ "- a-- - + b--", "-a---+b--" ], [ "- a-- - + b++", "-a---+b++" ], [ "- a-- - - ++b", "-a--- -++b" ], [ "- a-- - - --b", "-a--- - --b" ], [ "- a-- - - b", "-a--- -b" ], [ "- a-- - - b--", "-a--- -b--" ], [ "- a-- - - b++", "-a--- -b++" ], [ "- a++ + ++b", "-a+++ ++b" ], [ "- a++ + --b", "-a+++--b" ], [ "- a++ + b", "-a+++b" ], [ "- a++ + b--", "-a+++b--" ], [ "- a++ + b++", "-a+++b++" ], [ "- a++ + + ++b", "-a+++ + ++b" ], [ "- a++ + + --b", "-a+++ +--b" ], [ "- a++ + + b", "-a+++ +b" ], [ "- a++ + + b--", "-a+++ +b--" ], [ "- a++ + + b++", "-a+++ +b++" ], [ "- a++ + - ++b", "-a+++-++b" ], [ "- a++ + - --b", "-a+++- --b" ], [ "- a++ + - b", "-a+++-b" ], [ "- a++ + - b--", "-a+++-b--" ], [ "- a++ + - b++", "-a+++-b++" ], [ "- a++ - ++b", "-a++-++b" ], [ "- a++ - --b", "-a++- --b" ], [ "- a++ - b", "-a++-b" ], [ "- a++ - b--", "-a++-b--" ], [ "- a++ - b++", "-a++-b++" ], [ "- a++ - + ++b", "-a++-+ ++b" ], [ "- a++ - + --b", "-a++-+--b" ], [ "- a++ - + b", "-a++-+b" ], [ "- a++ - + b--", "-a++-+b--" ], [ "- a++ - + b++", "-a++-+b++" ], [ "- a++ - - ++b", "-a++- -++b" ], [ "- a++ - - --b", "-a++- - --b" ], [ "- a++ - - b", "-a++- -b" ], [ "- a++ - - b--", "-a++- -b--" ], [ "- a++ - - b++", "-a++- -b++" ], ].forEach(function(exp) { assert.strictEqual(UglifyJS.parse(exp[0]).print_to_string(), exp[1] + ";"); }); }); }); UglifyJS2-3.6.3/test/mocha/parentheses.js000066400000000000000000000072501355252637300202450ustar00rootroot00000000000000var assert = require("assert"); var UglifyJS = require("../.."); describe("parentheses", function() { it("Should add trailing parentheses for new expressions with zero arguments in beautify mode", function() { var tests = [ "new x(1);", "new x;", "new new x;", "new (function(foo){this.foo=foo;})(1);", "new (function(foo){this.foo=foo;})();", "new (function test(foo){this.foo=foo;})(1);", "new (function test(foo){this.foo=foo;})();", "new true;", "new (0);", "new (!0);", "new (bar = function(foo) {this.foo=foo;})(123);", "new (bar = function(foo) {this.foo=foo;})();" ]; var expected = [ "new x(1);", "new x();", "new new x()();", "new function(foo) {\n this.foo = foo;\n}(1);", "new function(foo) {\n this.foo = foo;\n}();", "new function test(foo) {\n this.foo = foo;\n}(1);", "new function test(foo) {\n this.foo = foo;\n}();", "new true();", "new 0();", "new (!0)();", "new (bar = function(foo) {\n this.foo = foo;\n})(123);", "new (bar = function(foo) {\n this.foo = foo;\n})();" ]; for (var i = 0; i < tests.length; i++) { assert.strictEqual( UglifyJS.minify(tests[i], { output: {beautify: true}, compress: false, mangle: false }).code, expected[i] ); } }); it("Should not add trailing parentheses for new expressions with zero arguments in non-beautify mode", function() { var tests = [ "new x(1);", "new x;", "new new x;", "new (function(foo){this.foo=foo;})(1);", "new (function(foo){this.foo=foo;})();", "new (function test(foo){this.foo=foo;})(1);", "new (function test(foo){this.foo=foo;})();", "new true;", "new (0);", "new (!0);", "new (bar = function(foo) {this.foo=foo;})(123);", "new (bar = function(foo) {this.foo=foo;})();" ]; var expected = [ "new x(1);", "new x;", "new(new x);", "new function(foo){this.foo=foo}(1);", "new function(foo){this.foo=foo};", "new function test(foo){this.foo=foo}(1);", "new function test(foo){this.foo=foo};", "new true;", "new 0;", "new(!0);", "new(bar=function(foo){this.foo=foo})(123);", "new(bar=function(foo){this.foo=foo});" ]; for (var i = 0; i < tests.length; i++) { assert.strictEqual( UglifyJS.minify(tests[i], { output: {beautify: false}, compress: false, mangle: false }).code, expected[i] ); } }); it("Should compress leading parenthesis with reasonable performance", function() { this.timeout(30000); var code = [ "({}?0:1)&&x();", "(function(){}).name;", ]; for (var i = 16; --i >= 0;) { code = code.concat(code); } code = code.join(""); var result = UglifyJS.minify(code, { compress: false, mangle: false, }); if (result.error) throw result.error; // Dismal performance for `assert.strictEqual()` in Node.js 6 assert.ok(result.code === code); }); }); UglifyJS2-3.6.3/test/mocha/sourcemaps.js000066400000000000000000000276521355252637300201150ustar00rootroot00000000000000var assert = require("assert"); var readFileSync = require("fs").readFileSync; var SourceMapConsumer = require("source-map").SourceMapConsumer; var UglifyJS = require("../node"); function read(path) { return readFileSync(path, "utf8"); } function source_map(code) { return JSON.parse(UglifyJS.minify(code, { compress: false, mangle: false, sourceMap: true, }).map); } function get_map() { return { "version": 3, "sources": ["index.js"], "names": [], "mappings": ";;AAAA,IAAI,MAAM,SAAN,GAAM;AAAA,SAAK,SAAS,CAAd;AAAA,CAAV;AACA,QAAQ,GAAR,CAAY,IAAI,KAAJ,CAAZ", "file": "bundle.js", "sourcesContent": ["let foo = x => \"foo \" + x;\nconsole.log(foo(\"bar\"));"] }; } function prepare_map(sourceMap) { var code = [ '"use strict";', "", "var foo = function foo(x) {", ' return "foo " + x;', "};", 'console.log(foo("bar"));', "", "//# sourceMappingURL=bundle.js.map", ].join("\n"); var result = UglifyJS.minify(code, { sourceMap: { content: sourceMap, includeSources: true, } }); if (result.error) throw result.error; return new SourceMapConsumer(result.map); } describe("sourcemaps", function() { it("Should give correct version", function() { var map = source_map("var x = 1 + 1;"); assert.strictEqual(map.version, 3); assert.deepEqual(map.names, [ "x" ]); }); it("Should give correct names", function() { var map = source_map([ "({", " get enabled() {", " return 3;", " },", " set enabled(x) {", " ;", " }", "});", ].join("\n")); assert.deepEqual(map.names, [ "enabled", "x" ]); }); it("Should mark array/object literals", function() { var result = UglifyJS.minify([ "var obj = {};", "obj.wat([]);", ].join("\n"), { sourceMap: true, toplevel: true, }); if (result.error) throw result.error; assert.strictEqual(result.code, "({}).wat([]);"); assert.strictEqual(result.map, '{"version":3,"sources":["0"],"names":["wat"],"mappings":"CAAU,IACNA,IAAI"}'); }); it("Should give correct sourceRoot", function() { var code = "console.log(42);"; var result = UglifyJS.minify(code, { sourceMap: { root: "//foo.bar/", }, }); if (result.error) throw result.error; assert.strictEqual(result.code, code); assert.strictEqual(result.map, '{"version":3,"sources":["0"],"names":["console","log"],"mappings":"AAAAA,QAAQC,IAAI","sourceRoot":"//foo.bar/"}'); }); describe("inSourceMap", function() { it("Should read the given string filename correctly when sourceMapIncludeSources is enabled", function() { var result = UglifyJS.minify(read("./test/input/issue-1236/simple.js"), { sourceMap: { content: read("./test/input/issue-1236/simple.js.map"), filename: "simple.min.js", includeSources: true } }); if (result.error) throw result.error; var map = JSON.parse(result.map); assert.equal(map.file, "simple.min.js"); assert.equal(map.sourcesContent.length, 1); assert.equal(map.sourcesContent[0], 'let foo = x => "foo " + x;\nconsole.log(foo("bar"));'); }); it("Should process inline source map", function() { var result = UglifyJS.minify(read("./test/input/issue-520/input.js"), { compress: { toplevel: true }, sourceMap: { content: "inline", includeSources: true, url: "inline" } }); if (result.error) throw result.error; assert.strictEqual(result.code + "\n", readFileSync("test/input/issue-520/output.js", "utf8")); }); it("Should warn for missing inline source map", function() { var result = UglifyJS.minify(read("./test/input/issue-1323/sample.js"), { mangle: false, sourceMap: { content: "inline" }, warnings: true, }); assert.strictEqual(result.code, "var bar=function(bar){return bar};"); assert.deepEqual(result.warnings, [ "WARN: inline source map not found: 0" ]); }); it("Should handle multiple input and inline source map", function() { var result = UglifyJS.minify([ read("./test/input/issue-520/input.js"), read("./test/input/issue-1323/sample.js"), ], { sourceMap: { content: "inline", url: "inline", }, warnings: true, }); if (result.error) throw result.error; assert.strictEqual(result.code, [ "var Foo=function(){console.log(3)};new Foo;var bar=function(o){return o};", "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0ZGluIiwiMSJdLCJuYW1lcyI6WyJGb28iLCJjb25zb2xlIiwibG9nIiwiYmFyIl0sIm1hcHBpbmdzIjoiQUFBQSxJQUFNQSxJQUFJLFdBQWdCQyxRQUFRQyxJQUFJLElBQVMsSUFBSUYsSUNBbkQsSUFBSUcsSUFDQSxTQUFjQSxHQUNWLE9BQU9BIn0=", ].join("\n")); assert.deepEqual(result.warnings, [ "WARN: inline source map not found: 1" ]); }); it("Should drop source contents for includeSources=false", function() { var result = UglifyJS.minify(read("./test/input/issue-520/input.js"), { compress: false, mangle: false, sourceMap: { content: "inline", includeSources: true, }, }); if (result.error) throw result.error; var map = JSON.parse(result.map); assert.strictEqual(map.sourcesContent.length, 1); result = UglifyJS.minify(result.code, { compress: false, mangle: false, sourceMap: { content: result.map, }, }); if (result.error) throw result.error; map = JSON.parse(result.map); assert.ok(!("sourcesContent" in map)); }); it("Should parse the correct sourceMappingURL", function() { var result = UglifyJS.minify(read("./test/input/issue-3294/input.js"), { compress: { toplevel: true }, sourceMap: { content: "inline", includeSources: true, url: "inline" } }); if (result.error) throw result.error; assert.strictEqual(result.code + "\n", readFileSync("test/input/issue-3294/output.js", "utf8")); }); it("Should work in presence of unrecognised annotations", function() { var result = UglifyJS.minify(read("./test/input/issue-3441/input.js"), { compress: false, mangle: false, sourceMap: { content: "inline", }, }); if (result.error) throw result.error; assert.strictEqual(result.code, '(function(){console.log("hello")}).call(this);'); assert.strictEqual(result.map, '{"version":3,"sources":["main.coffee"],"names":["console","log"],"mappings":"CAAA,WAAAA,QAAQC,IAAI"}'); }); }); describe("sourceMapInline", function() { it("Should append source map to output js when sourceMapInline is enabled", function() { var result = UglifyJS.minify('var a = function(foo) { return foo; };', { sourceMap: { url: "inline" } }); if (result.error) throw result.error; var code = result.code; assert.strictEqual(code, "var a=function(n){return n};\n" + "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjAiXSwibmFtZXMiOlsiYSIsImZvbyJdLCJtYXBwaW5ncyI6IkFBQUEsSUFBSUEsRUFBSSxTQUFTQyxHQUFPLE9BQU9BIn0="); }); it("Should not append source map to output js when sourceMapInline is not enabled", function() { var result = UglifyJS.minify('var a = function(foo) { return foo; };'); if (result.error) throw result.error; var code = result.code; assert.strictEqual(code, "var a=function(n){return n};"); }); it("Should work with max_line_len", function() { var result = UglifyJS.minify(read("./test/input/issue-505/input.js"), { compress: { directives: false, }, output: { max_line_len: 20 }, sourceMap: { url: "inline" } }); if (result.error) throw result.error; assert.strictEqual(result.code, read("./test/input/issue-505/output.js")); }); it("Should work with unicode characters", function() { var code = [ "var tëst = '→unicøde←';", "alert(tëst);", ].join("\n"); var result = UglifyJS.minify(code, { sourceMap: { includeSources: true, url: "inline", } }); if (result.error) throw result.error; var map = JSON.parse(result.map); assert.strictEqual(map.sourcesContent.length, 1); assert.strictEqual(map.sourcesContent[0], code); var encoded = result.code.slice(result.code.lastIndexOf(",") + 1); map = JSON.parse(UglifyJS.to_ascii(encoded)); assert.strictEqual(map.sourcesContent.length, 1); assert.strictEqual(map.sourcesContent[0], code); result = UglifyJS.minify(result.code, { sourceMap: { content: "inline", includeSources: true, } }); if (result.error) throw result.error; map = JSON.parse(result.map); assert.strictEqual(map.names.length, 2); assert.strictEqual(map.names[0], "tëst"); assert.strictEqual(map.names[1], "alert"); }); }); describe("input sourcemaps", function() { it("Should copy over original sourcesContent", function() { var orig = get_map(); var map = prepare_map(orig); assert.equal(map.sourceContentFor("index.js"), orig.sourcesContent[0]); }); it("Should copy sourcesContent if sources are relative", function() { var relativeMap = get_map(); relativeMap.sources = ['./index.js']; var map = prepare_map(relativeMap); assert.notEqual(map.sourcesContent, null); assert.equal(map.sourcesContent.length, 1); assert.equal(map.sourceContentFor("index.js"), relativeMap.sourcesContent[0]); }); it("Should not have invalid mappings from inputSourceMap", function() { var map = prepare_map(get_map()); // The original source has only 2 lines, check that mappings don't have more lines var msg = "Mapping should not have higher line number than the original file had"; map.eachMapping(function(mapping) { assert.ok(mapping.originalLine <= 2, msg); }); map.allGeneratedPositionsFor({ source: "index.js", line: 1, column: 1 }).forEach(function(pos) { assert.ok(pos.line <= 2, msg); }); }); }); }); UglifyJS2-3.6.3/test/mocha/spidermonkey.js000066400000000000000000000077651355252637300204500ustar00rootroot00000000000000var assert = require("assert"); var exec = require("child_process").exec; var UglifyJS = require("../.."); describe("spidermonkey export/import sanity test", function() { it("Should produce a functional build when using --self with spidermonkey", function(done) { this.timeout(60000); var uglifyjs = '"' + process.argv[0] + '" bin/uglifyjs'; var command = uglifyjs + " --self -cm --wrap SpiderUglify -o spidermonkey | " + uglifyjs + " -p spidermonkey -cm"; exec(command, function(err, stdout) { if (err) throw err; eval(stdout); assert.strictEqual(typeof SpiderUglify, "object"); var result = SpiderUglify.minify("foo([true,,2+3]);"); assert.strictEqual(result.error, undefined); assert.strictEqual(result.code, "foo([!0,,5]);"); done(); }); }); it("Should not add unnecessary escape slashes to regexps", function() { var input = "/[\\\\/]/;"; var ast = UglifyJS.parse(input).to_mozilla_ast(); assert.equal( UglifyJS.AST_Node.from_mozilla_ast(ast).print_to_string(), input ); }); it("Should judge between directives and strings correctly on import", function() { var tests = [ { input: '"use strict";;"use sloppy"', directives: 1, strings: 1 }, { input: ';"use strict"', directives: 0, strings: 1 }, { input: '"use strict"; "use something else";', directives: 2, strings: 0 }, { input: 'function foo() {"use strict";;"use sloppy" }', directives: 1, strings: 1 }, { input: 'function foo() {;"use strict" }', directives: 0, strings: 1 }, { input: 'function foo() {"use strict"; "use something else"; }', directives: 2, strings: 0 }, { input: 'var foo = function() {"use strict";;"use sloppy" }', directives: 1, strings: 1 }, { input: 'var foo = function() {;"use strict" }', directives: 0, strings: 1 }, { input: 'var foo = function() {"use strict"; "use something else"; }', directives: 2, strings: 0 }, { input: '{"use strict";;"use sloppy" }', directives: 0, strings: 2 }, { input: '{;"use strict" }', directives: 0, strings: 1 }, { input: '{"use strict"; "use something else"; }', directives: 0, strings: 2 } ]; var counter_directives; var counter_strings; var checkWalker = new UglifyJS.TreeWalker(function(node, descend) { if (node instanceof UglifyJS.AST_String) { counter_strings++; } else if (node instanceof UglifyJS.AST_Directive) { counter_directives++; } }); for (var i = 0; i < tests.length; i++) { counter_directives = 0; counter_strings = 0; var ast = UglifyJS.parse(tests[i].input); var moz_ast = ast.to_mozilla_ast(); var from_moz_ast = UglifyJS.AST_Node.from_mozilla_ast(moz_ast); from_moz_ast.walk(checkWalker); assert.strictEqual(counter_directives, tests[i].directives, "Directives count mismatch for test " + tests[i].input); assert.strictEqual(counter_strings, tests[i].strings, "String count mismatch for test " + tests[i].input); } }); }); UglifyJS2-3.6.3/test/mocha/string-literal.js000066400000000000000000000075141355252637300206670ustar00rootroot00000000000000var assert = require("assert"); var UglifyJS = require("../node"); describe("String literals", function() { it("Should throw syntax error if a string literal contains a newline", function() { var inputs = [ "'\n'", "'\r'", '"\r\n"', "'\u2028'", '"\u2029"' ]; var test = function(input) { return function() { var ast = UglifyJS.parse(input); }; }; var error = function(e) { return e instanceof UglifyJS.JS_Parse_Error && e.message === "Unterminated string constant"; }; for (var input in inputs) { assert.throws(test(inputs[input]), error); } }); it("Should not throw syntax error if a string has a line continuation", function() { var output = UglifyJS.parse('var a = "a\\\nb";').print_to_string(); assert.equal(output, 'var a="ab";'); }); it("Should throw error in strict mode if string contains escaped octalIntegerLiteral", function() { var inputs = [ '"use strict";\n"\\76";', '"use strict";\nvar foo = "\\76";', '"use strict";\n"\\1";', '"use strict";\n"\\07";', '"use strict";\n"\\011"' ]; var test = function(input) { return function() { var output = UglifyJS.parse(input); } }; var error = function(e) { return e instanceof UglifyJS.JS_Parse_Error && e.message === "Legacy octal escape sequences are not allowed in strict mode"; } for (var input in inputs) { assert.throws(test(inputs[input]), error); } }); it("Should not throw error outside strict mode if string contains escaped octalIntegerLiteral", function() { var tests = [ ['"\\76";', ';">";'], ['"\\0"', '"\\0";'], ['"\\08"', '"\\x008";'], ['"\\008"', '"\\x008";'], ['"\\0008"', '"\\x008";'], ['"use strict" === "use strict";\n"\\76";', '"use strict"==="use strict";">";'], ['"use\\\n strict";\n"\\07";', ';"use strict";"\07";'] ]; for (var test in tests) { var output = UglifyJS.parse(tests[test][0]).print_to_string(); assert.equal(output, tests[test][1]); } }); it("Should not throw error when digit is 8 or 9", function() { assert.equal(UglifyJS.parse('"use strict";"\\08"').print_to_string(), '"use strict";"\\x008";'); assert.equal(UglifyJS.parse('"use strict";"\\09"').print_to_string(), '"use strict";"\\x009";'); }); it("Should not unescape unpaired surrogates", function() { var code = []; for (var i = 0; i <= 0xF; i++) { code.push("\\u000" + i.toString(16)); } for (;i <= 0xFF; i++) { code.push("\\u00" + i.toString(16)); } for (;i <= 0xFFF; i++) { code.push("\\u0" + i.toString(16)); } for (; i <= 0xFFFF; i++) { code.push("\\u" + i.toString(16)); } code = '"' + code.join() + '"'; var normal = UglifyJS.minify(code, { compress: false, mangle: false, output: { ascii_only: false } }); if (normal.error) throw normal.error; assert.ok(code.length > normal.code.length); assert.strictEqual(eval(code), eval(normal.code)); var ascii = UglifyJS.minify(code, { compress: false, mangle: false, output: { ascii_only: false } }); if (ascii.error) throw ascii.error; assert.ok(code.length > ascii.code.length); assert.strictEqual(eval(code), eval(ascii.code)); }); }); UglifyJS2-3.6.3/test/mocha/tokens.js000066400000000000000000000022771355252637300172330ustar00rootroot00000000000000var assert = require("assert"); var UglifyJS = require("../.."); describe("tokens", function() { it("Should give correct positions for accessors", function() { // location 0 1 2 3 4 // 01234567890123456789012345678901234567890123456789 var ast = UglifyJS.parse("var obj = { get latest() { return undefined; } }"); // test all AST_ObjectProperty tokens are set as expected var found = false; ast.walk(new UglifyJS.TreeWalker(function(node) { if (node instanceof UglifyJS.AST_ObjectProperty) { found = true; assert.equal(node.start.pos, 12); assert.equal(node.end.endpos, 46); assert(node.key instanceof UglifyJS.AST_SymbolAccessor); assert.equal(node.key.start.pos, 16); assert.equal(node.key.end.endpos, 22); assert(node.value instanceof UglifyJS.AST_Accessor); assert.equal(node.value.start.pos, 22); assert.equal(node.value.end.endpos, 46); } })); assert(found, "AST_ObjectProperty not found"); }); }); UglifyJS2-3.6.3/test/mocha/with.js000066400000000000000000000016521355252637300166770ustar00rootroot00000000000000var assert = require("assert"); var UglifyJS = require("../node"); describe("With", function() { it("Should throw syntaxError when using with statement in strict mode", function() { var code = '"use strict";\nthrow NotEarlyError;\nwith ({}) { }'; var test = function() { UglifyJS.parse(code); } var error = function(e) { return e instanceof UglifyJS.JS_Parse_Error && e.message === "Strict mode may not include a with statement"; } assert.throws(test, error); }); it("Should set uses_with for scopes involving With statements", function() { var ast = UglifyJS.parse("with(e) {f(1, 2)}"); ast.figure_out_scope(); assert.equal(ast.uses_with, true); assert.equal(ast.body[0].expression.scope.uses_with, true); assert.equal(ast.body[0].body.body[0].body.expression.scope.uses_with, true); }); }); UglifyJS2-3.6.3/test/mozilla-ast.js000066400000000000000000000044021355252637300170650ustar00rootroot00000000000000// Testing UglifyJS <-> SpiderMonkey AST conversion "use strict"; var acorn = require("acorn"); var ufuzz = require("./ufuzz"); var UglifyJS = require(".."); function try_beautify(code) { var beautified = UglifyJS.minify(code, { compress: false, mangle: false, output: { beautify: true, braces: true } }); if (beautified.error) { console.log("// !!! beautify failed !!!"); console.log(beautified.error.stack); console.log(code); } else { console.log("// (beautified)"); console.log(beautified.code); } } function test(original, estree, description) { var transformed = UglifyJS.minify(UglifyJS.AST_Node.from_mozilla_ast(estree), { compress: false, mangle: false }); if (transformed.error || original !== transformed.code) { console.log("//============================================================="); console.log("// !!!!!! Failed... round", round); console.log("// original code"); try_beautify(original); console.log(); console.log(); console.log("//-------------------------------------------------------------"); console.log("//", description); if (transformed.error) { console.log(transformed.error.stack); } else { try_beautify(transformed.code); } console.log("!!!!!! Failed... round", round); process.exit(1); } } var num_iterations = ufuzz.num_iterations; for (var round = 1; round <= num_iterations; round++) { process.stdout.write(round + " of " + num_iterations + "\r"); var code = ufuzz.createTopLevelCode(); var uglified = UglifyJS.minify(code, { compress: false, mangle: false, output: { ast: true } }); test(uglified.code, uglified.ast.to_mozilla_ast(), "AST_Node.to_mozilla_ast()"); try { test(uglified.code, acorn.parse(code), "acorn.parse()"); } catch (e) { console.log("//============================================================="); console.log("// acorn parser failed... round", round); console.log(e); console.log("// original code"); console.log(code); } } console.log(); UglifyJS2-3.6.3/test/node.js000066400000000000000000000004321355252637300155550ustar00rootroot00000000000000var fs = require("fs"); new Function("MOZ_SourceMap", "exports", require("../tools/node").FILES.map(function(file) { if (/exports\.js$/.test(file)) file = require.resolve("./exports"); return fs.readFileSync(file, "utf8"); }).join("\n\n"))(require("source-map"), exports); UglifyJS2-3.6.3/test/release/000077500000000000000000000000001355252637300157135ustar00rootroot00000000000000UglifyJS2-3.6.3/test/release/benchmark.js000066400000000000000000000006021355252637300202010ustar00rootroot00000000000000require("./run")([ "-b", "-b braces", "-m", "-mc passes=3", "-mc passes=3,toplevel", "-mc passes=3,unsafe", "-mc keep_fargs=false,passes=3", "-mc keep_fargs=false,passes=3,pure_getters,unsafe,unsafe_comps,unsafe_math,unsafe_proto", ].map(function(options) { var args = options.split(/ /); args.unshift("test/benchmark.js"); return args; })); UglifyJS2-3.6.3/test/release/jetstream.js000066400000000000000000000004451355252637300202520ustar00rootroot00000000000000require("./run")([ "-mc", "-mc keep_fargs=false,passes=3,pure_getters,unsafe,unsafe_comps,unsafe_math,unsafe_proto", ].map(function(options) { var args = options.split(/ /); args.unshift("test/jetstream.js"); args.push("-b", "beautify=false,webkit"); return args; })); UglifyJS2-3.6.3/test/release/run.js000066400000000000000000000007441355252637300170620ustar00rootroot00000000000000var child_process = require("child_process"); module.exports = function(tasks) { (function next() { if (!tasks.length) return; var args = tasks.shift(); console.log(); console.log("\u001B[36m$> " + args.join(" ") + "\u001B[39m"); child_process.spawn(process.argv[0], args, { stdio: [ "ignore", 1, 2 ] }).on("exit", function(code) { if (code) process.exit(code); next(); }); })(); }; UglifyJS2-3.6.3/test/sandbox.js000066400000000000000000000057301355252637300162740ustar00rootroot00000000000000var semver = require("semver"); var vm = require("vm"); var setupContext = new vm.Script([ "[ Array, Boolean, Error, Function, Number, Object, RegExp, String ].forEach(function(f) {", " f.toString = Function.prototype.toString;", "});", "Function.prototype.toString = function() {", " var id = 100000;", " return function() {", " var n = this.name;", " if (!/^F[0-9]{6}N$/.test(n)) {", ' n = "F" + ++id + "N";', ].concat(Object.getOwnPropertyDescriptor(Function.prototype, "name").configurable ? [ ' Object.defineProperty(this, "name", {', " get: function() {", " return n;", " }", " });", ] : [], [ " }", ' return "function(){}";', " };", "}();", "this;", ]).join("\n")); function createContext() { var ctx = vm.createContext(Object.defineProperty({}, "console", { value: { log: log } })); var global = setupContext.runInContext(ctx); return ctx; function safe_log(arg, level) { if (arg) switch (typeof arg) { case "function": return arg.toString(); case "object": if (arg === global) return "[object global]"; if (/Error$/.test(arg.name)) return arg.toString(); arg.constructor.toString(); if (level--) for (var key in arg) { var desc = Object.getOwnPropertyDescriptor(arg, key); if (!desc || !desc.get) arg[key] = safe_log(arg[key], level); } } return arg; } function log(msg) { if (arguments.length == 1 && typeof msg == "string") return console.log("%s", msg); return console.log.apply(console, [].map.call(arguments, function(arg) { return safe_log(arg, 3); })); } } exports.run_code = function(code, toplevel) { var stdout = ""; var original_write = process.stdout.write; process.stdout.write = function(chunk) { stdout += chunk; }; try { vm.runInContext(toplevel ? "(function(){" + code + "})()" : code, createContext(), { timeout: 5000 }); return stdout; } catch (ex) { return ex; } finally { process.stdout.write = original_write; } }; function strip_func_ids(text) { return ("" + text).replace(/F[0-9]{6}N/g, "N>"); } exports.same_stdout = semver.satisfies(process.version, "0.12") ? function(expected, actual) { if (typeof expected != typeof actual) return false; if (typeof expected != "string") { if (expected.name != actual.name) return false; expected = expected.message.slice(expected.message.lastIndexOf("\n") + 1); actual = actual.message.slice(actual.message.lastIndexOf("\n") + 1); } return strip_func_ids(expected) == strip_func_ids(actual); } : function(expected, actual) { return typeof expected == typeof actual && strip_func_ids(expected) == strip_func_ids(actual); }; UglifyJS2-3.6.3/test/travis-ufuzz.js000066400000000000000000000052311355252637300173230ustar00rootroot00000000000000"use strict"; var child_process = require("child_process"); var https = require("https"); var url = require("url"); var period = 45 * 60 * 1000; var wait = 2 * 60 * 1000; var ping = 5 * 60 * 1000; if (process.argv[2] == "run") { var endTime = Date.now() + period; for (var i = 0; i < 2; i++) spawn(endTime); } else if (process.argv.length > 2) { var token = process.argv[2]; var branch = process.argv[3] || "v" + require("../package.json").version; var repository = encodeURIComponent(process.argv[4] || "mishoo/UglifyJS2"); var concurrency = process.argv[5] || 1; var platform = process.argv[6] || "latest"; (function request() { setTimeout(request, (period + wait) / concurrency); var options = url.parse("https://api.travis-ci.org/repo/" + repository + "/requests"); options.method = "POST"; options.headers = { "Content-Type": "application/json", "Travis-API-Version": 3, "Authorization": "token " + token }; https.request(options, function(res) { console.log("HTTP", res.statusCode); console.log(JSON.stringify(res.headers, null, 2)); console.log(); res.setEncoding("utf8"); res.on("data", console.log); }).on("error", console.error).end(JSON.stringify({ request: { message: "ufuzz testing", branch: branch, config: { cache: false, env: "NODE=" + platform, script: "node test/travis-ufuzz run" } } })); })(); } else { console.log("Usage: test/travis-ufuzz.js [branch] [repository] [concurrency] [platform]"); } function spawn(endTime) { var child = child_process.spawn("node", [ "--max-old-space-size=2048", "test/ufuzz" ], { stdio: [ "ignore", "pipe", "pipe" ] }).on("exit", respawn); var line = ""; child.stdout.on("data", function(data) { line += data; }); child.stderr.on("data", function() { process.exitCode = 1; }).pipe(process.stdout); var keepAlive = setInterval(function() { var end = line.lastIndexOf("\r"); console.log(line.slice(line.lastIndexOf("\r", end - 1) + 1, end)); line = line.slice(end + 1); }, ping); var timer = setTimeout(function() { clearInterval(keepAlive); child.removeListener("exit", respawn); child.kill(); }, endTime - Date.now()); function respawn() { console.log(line); clearInterval(keepAlive); clearTimeout(timer); spawn(endTime); } } UglifyJS2-3.6.3/test/ufuzz.js000066400000000000000000001202241355252637300160150ustar00rootroot00000000000000// ufuzz.js // derived from https://github.com/qfox/uglyfuzzer by Peter van der Zee "use strict"; // check both CLI and file modes of nodejs (!). See #1695 for details. and the various settings of uglify. // bin/uglifyjs s.js -c && bin/uglifyjs s.js -c passes=3 && bin/uglifyjs s.js -c passes=3 -m // cat s.js | node && node s.js && bin/uglifyjs s.js -c | node && bin/uglifyjs s.js -c passes=3 | node && bin/uglifyjs s.js -c passes=3 -m | node require("../tools/exit"); var UglifyJS = require(".."); var randomBytes = require("crypto").randomBytes; var sandbox = require("./sandbox"); var MAX_GENERATED_TOPLEVELS_PER_RUN = 1; var MAX_GENERATION_RECURSION_DEPTH = 12; var INTERVAL_COUNT = 100; var STMT_ARG_TO_ID = Object.create(null); var STMTS_TO_USE = []; function STMT_(name) { return STMT_ARG_TO_ID[name] = STMTS_TO_USE.push(STMTS_TO_USE.length) - 1; } var STMT_BLOCK = STMT_("block"); var STMT_IF_ELSE = STMT_("ifelse"); var STMT_DO_WHILE = STMT_("dowhile"); var STMT_WHILE = STMT_("while"); var STMT_FOR_LOOP = STMT_("forloop"); var STMT_FOR_IN = STMT_("forin"); var STMT_SEMI = STMT_("semi"); var STMT_EXPR = STMT_("expr"); var STMT_SWITCH = STMT_("switch"); var STMT_VAR = STMT_("var"); var STMT_RETURN_ETC = STMT_("stop"); var STMT_FUNC_EXPR = STMT_("funcexpr"); var STMT_TRY = STMT_("try"); var STMT_C = STMT_("c"); var STMT_FIRST_LEVEL_OVERRIDE = -1; var STMT_SECOND_LEVEL_OVERRIDE = -1; var STMT_COUNT_FROM_GLOBAL = true; // count statement depth from nearest function scope or just global scope? var num_iterations = +process.argv[2] || 1/0; var verbose = false; // log every generated test var verbose_interval = false; // log every 100 generated tests var use_strict = false; var catch_redef = require.main === module; var generate_directive = require.main === module; for (var i = 2; i < process.argv.length; ++i) { switch (process.argv[i]) { case '-v': verbose = true; break; case '-V': verbose_interval = true; break; case '-t': MAX_GENERATED_TOPLEVELS_PER_RUN = +process.argv[++i]; if (!MAX_GENERATED_TOPLEVELS_PER_RUN) throw new Error('Must generate at least one toplevel per run'); break; case '-r': MAX_GENERATION_RECURSION_DEPTH = +process.argv[++i]; if (!MAX_GENERATION_RECURSION_DEPTH) throw new Error('Recursion depth must be at least 1'); break; case '-s1': var name = process.argv[++i]; STMT_FIRST_LEVEL_OVERRIDE = STMT_ARG_TO_ID[name]; if (!(STMT_FIRST_LEVEL_OVERRIDE >= 0)) throw new Error('Unknown statement name; use -? to get a list'); break; case '-s2': var name = process.argv[++i]; STMT_SECOND_LEVEL_OVERRIDE = STMT_ARG_TO_ID[name]; if (!(STMT_SECOND_LEVEL_OVERRIDE >= 0)) throw new Error('Unknown statement name; use -? to get a list'); break; case '--no-catch-redef': catch_redef = false; break; case '--no-directive': generate_directive = false; break; case '--use-strict': use_strict = true; break; case '--stmt-depth-from-func': STMT_COUNT_FROM_GLOBAL = false; break; case '--only-stmt': STMTS_TO_USE = process.argv[++i].split(',').map(function(name){ return STMT_ARG_TO_ID[name]; }); break; case '--without-stmt': // meh. it runs once it's fine. process.argv[++i].split(',').forEach(function(name){ var omit = STMT_ARG_TO_ID[name]; STMTS_TO_USE = STMTS_TO_USE.filter(function(id){ return id !== omit; }) }); break; case '--help': case '-h': case '-?': println('** UglifyJS fuzzer help **'); println('Valid options (optional):'); println(': generate this many cases (if used must be first arg)'); println('-v: print every generated test case'); println('-V: print every 100th generated test case'); println('-t : generate this many toplevels per run (more take longer)'); println('-r : maximum recursion depth for generator (higher takes longer)'); println('-s1 : force the first level statement to be this one (see list below)'); println('-s2 : force the second level statement to be this one (see list below)'); println('--no-catch-redef: do not redefine catch variables'); println('--no-directive: do not generate directives'); println('--use-strict: generate "use strict"'); println('--stmt-depth-from-func: reset statement depth counter at each function, counts from global otherwise'); println('--only-stmt : a comma delimited white list of statements that may be generated'); println('--without-stmt : a comma delimited black list of statements never to generate'); println('List of accepted statement names: ' + Object.keys(STMT_ARG_TO_ID)); println('** UglifyJS fuzzer exiting **'); return 0; default: // first arg may be a number. if (i > 2 || !parseInt(process.argv[i], 10)) throw new Error('Unknown argument[' + process.argv[i] + ']; see -h for help'); } } var VALUES = [ '"a"', '"b"', '"c"', '""', 'true', 'false', ' /[a2][^e]+$/ ', '(-1)', '(-2)', '(-3)', '(-4)', '(-5)', '0', '1', '2', '3', '4', '5', '22', '-0', // 0/-0 !== 0 '23..toString()', '24 .toString()', '25. ', '0x26.toString()', 'NaN', 'undefined', 'Infinity', 'null', '[]', '[,0][1]', // an array with elisions... but this is always false '([,0].length === 2)', // an array with elisions... this is always true '({})', // wrapped the object causes too many syntax errors in statements '"foo"', '"bar"', '"undefined"', '"object"', '"number"', '"function"', 'this', ]; var BINARY_OPS_NO_COMMA = [ ' + ', // spaces needed to disambiguate with ++ cases (could otherwise cause syntax errors) ' - ', '/', '*', '&', '|', '^', '<', '<=', '>', '>=', '==', '===', '!=', '!==', '<<', '>>', '>>>', '%', '&&', '||', '^' ]; var BINARY_OPS = [','].concat(BINARY_OPS_NO_COMMA); var ASSIGNMENTS = [ '=', '=', '=', '=', '=', '=', '=', '=', '=', '=', '=', '=', '=', '=', '=', '=', '=', '=', '=', '=', '+=', '+=', '+=', '+=', '+=', '+=', '+=', '+=', '+=', '+=', '-=', '*=', '/=', '&=', '|=', '^=', '<<=', '>>=', '>>>=', '%=', ]; var UNARY_SAFE = [ '+', '-', '~', '!', 'void ', 'delete ', ]; var UNARY_POSTFIX = [ '++', '--', ]; var UNARY_PREFIX = UNARY_POSTFIX.concat(UNARY_SAFE); var NO_COMMA = true; var COMMA_OK = false; var MAYBE = true; var MANDATORY = false; var CAN_THROW = true; var CANNOT_THROW = false; var CAN_BREAK = true; var CANNOT_BREAK = false; var CAN_CONTINUE = true; var CANNOT_CONTINUE = false; var CAN_RETURN = false; var CANNOT_RETURN = true; var NO_DEFUN = false; var DEFUN_OK = true; var DONT_STORE = true; var VAR_NAMES = [ 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', // prevent redeclaring this, avoid assigning to this 'foo', 'foo', 'bar', 'bar', 'undefined', 'NaN', 'Infinity', 'arguments', 'Math', 'parseInt', ]; var INITIAL_NAMES_LEN = VAR_NAMES.length; var TYPEOF_OUTCOMES = [ 'function', 'undefined', 'string', 'number', 'object', 'boolean', 'special', 'unknown', 'symbol', 'crap' ]; var unique_vars = []; var loops = 0; var funcs = 0; var called = Object.create(null); var labels = 10000; function rng(max) { var r = randomBytes(2).readUInt16LE(0) / 65536; return Math.floor(max * r); } function strictMode() { return use_strict && rng(4) == 0 ? '"use strict";' : ''; } function createTopLevelCode() { VAR_NAMES.length = INITIAL_NAMES_LEN; // prune any previous names still in the list unique_vars.length = 0; loops = 0; funcs = 0; called = Object.create(null); return [ strictMode(), 'var _calls_ = 10, a = 100, b = 10, c = 0;', rng(2) == 0 ? createStatements(3, MAX_GENERATION_RECURSION_DEPTH, CANNOT_THROW, CANNOT_BREAK, CANNOT_CONTINUE, CANNOT_RETURN, 0) : createFunctions(rng(MAX_GENERATED_TOPLEVELS_PER_RUN) + 1, MAX_GENERATION_RECURSION_DEPTH, DEFUN_OK, CANNOT_THROW, 0), // preceding `null` makes for a cleaner output (empty string still shows up etc) 'console.log(null, a, b, c, Infinity, NaN, undefined);' ].join('\n'); } function createFunctions(n, recurmax, allowDefun, canThrow, stmtDepth) { if (--recurmax < 0) { return ';'; } var s = ''; while (n-- > 0) { s += createFunction(recurmax, allowDefun, canThrow, stmtDepth) + '\n'; } return s; } function createParams() { var params = []; for (var n = rng(4); --n >= 0;) { params.push(createVarName(MANDATORY)); } return params.join(', '); } function createArgs(recurmax, stmtDepth, canThrow) { var args = []; for (var n = rng(4); --n >= 0;) { args.push(rng(2) ? createValue() : createExpression(recurmax - 1, COMMA_OK, stmtDepth, canThrow)); } return args.join(', '); } function filterDirective(s) { if (!generate_directive && !s[1] && /\("/.test(s[2])) s[2] = ';' + s[2]; return s; } function createFunction(recurmax, allowDefun, canThrow, stmtDepth) { if (--recurmax < 0) { return ';'; } if (!STMT_COUNT_FROM_GLOBAL) stmtDepth = 0; var namesLenBefore = VAR_NAMES.length; var name; if (allowDefun || rng(5) > 0) { name = 'f' + funcs++; } else { unique_vars.push('a', 'b', 'c'); name = createVarName(MANDATORY, !allowDefun); unique_vars.length -= 3; } var s = [ 'function ' + name + '(' + createParams() + '){', strictMode() ]; if (rng(5) === 0) { // functions with functions. lower the recursion to prevent a mess. s.push(createFunctions(rng(5) + 1, Math.ceil(recurmax * 0.7), DEFUN_OK, canThrow, stmtDepth)); } else { // functions with statements s.push(createStatements(3, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth)); } s.push('}', ''); s = filterDirective(s).join('\n'); VAR_NAMES.length = namesLenBefore; if (!allowDefun) { // avoid "function statements" (decl inside statements) s = 'var ' + createVarName(MANDATORY) + ' = ' + s; s += '(' + createArgs(recurmax, stmtDepth, canThrow) + ')'; } else if (!(name in called) || rng(3) > 0) { s += 'var ' + createVarName(MANDATORY) + ' = ' + name; s += '(' + createArgs(recurmax, stmtDepth, canThrow) + ')'; } return s + ';'; } function createStatements(n, recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) { if (--recurmax < 0) { return ';'; } var s = ''; while (--n > 0) { s += createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + '\n'; } return s; } function enableLoopControl(flag, defaultValue) { return Array.isArray(flag) && flag.indexOf("") < 0 ? flag.concat("") : flag || defaultValue; } function createLabel(canBreak, canContinue) { var label; if (rng(10) < 3) { label = ++labels; if (Array.isArray(canBreak)) { canBreak = canBreak.slice(); } else { canBreak = canBreak ? [ "" ] : []; } canBreak.push(label); if (Array.isArray(canContinue)) { canContinue = canContinue.slice(); } else { canContinue = canContinue ? [ "" ] : []; } canContinue.push(label); } return { break: canBreak, continue: canContinue, target: label ? "L" + label + ": " : "" }; } function getLabel(label) { if (!Array.isArray(label)) return ""; label = label[rng(label.length)]; return label && " L" + label; } function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth, target) { ++stmtDepth; var loop = ++loops; if (--recurmax < 0) { return createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ';'; } // allow to forcefully generate certain structures at first or second recursion level if (target === undefined) { if (stmtDepth === 1 && STMT_FIRST_LEVEL_OVERRIDE >= 0) target = STMT_FIRST_LEVEL_OVERRIDE; else if (stmtDepth === 2 && STMT_SECOND_LEVEL_OVERRIDE >= 0) target = STMT_SECOND_LEVEL_OVERRIDE; else target = STMTS_TO_USE[rng(STMTS_TO_USE.length)]; } switch (target) { case STMT_BLOCK: var label = createLabel(canBreak); return label.target + '{' + createStatements(rng(5) + 1, recurmax, canThrow, label.break, canContinue, cannotReturn, stmtDepth) + '}'; case STMT_IF_ELSE: return 'if (' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ')' + createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + (rng(2) === 1 ? ' else ' + createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) : ''); case STMT_DO_WHILE: var label = createLabel(canBreak, canContinue); canBreak = label.break || enableLoopControl(canBreak, CAN_BREAK); canContinue = label.continue || enableLoopControl(canContinue, CAN_CONTINUE); return '{var brake' + loop + ' = 5; ' + label.target + 'do {' + createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + '} while ((' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ') && --brake' + loop + ' > 0);}'; case STMT_WHILE: var label = createLabel(canBreak, canContinue); canBreak = label.break || enableLoopControl(canBreak, CAN_BREAK); canContinue = label.continue || enableLoopControl(canContinue, CAN_CONTINUE); return '{var brake' + loop + ' = 5; ' + label.target + 'while ((' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ') && --brake' + loop + ' > 0)' + createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + '}'; case STMT_FOR_LOOP: var label = createLabel(canBreak, canContinue); canBreak = label.break || enableLoopControl(canBreak, CAN_BREAK); canContinue = label.continue || enableLoopControl(canContinue, CAN_CONTINUE); return label.target + 'for (var brake' + loop + ' = 5; (' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ') && brake' + loop + ' > 0; --brake' + loop + ')' + createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth); case STMT_FOR_IN: var label = createLabel(canBreak, canContinue); canBreak = label.break || enableLoopControl(canBreak, CAN_BREAK); canContinue = label.continue || enableLoopControl(canContinue, CAN_CONTINUE); var optElementVar = ''; if (rng(5) > 1) { optElementVar = 'c = 1 + c; var ' + createVarName(MANDATORY) + ' = expr' + loop + '[key' + loop + ']; '; } return '{var expr' + loop + ' = ' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + '; ' + label.target + ' for (var key' + loop + ' in expr' + loop + ') {' + optElementVar + createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + '}}'; case STMT_SEMI: return use_strict && rng(20) === 0 ? '"use strict";' : ';'; case STMT_EXPR: return createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ';'; case STMT_SWITCH: // note: case args are actual expressions // note: default does not _need_ to be last return 'switch (' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ') { ' + createSwitchParts(recurmax, 4, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + '}'; case STMT_VAR: switch (rng(3)) { case 0: unique_vars.push('c'); var name = createVarName(MANDATORY); unique_vars.pop(); return 'var ' + name + ';'; case 1: // initializer can only have one expression unique_vars.push('c'); var name = createVarName(MANDATORY); unique_vars.pop(); return 'var ' + name + ' = ' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ';'; default: // initializer can only have one expression unique_vars.push('c'); var n1 = createVarName(MANDATORY); var n2 = createVarName(MANDATORY); unique_vars.pop(); return 'var ' + n1 + ' = ' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ', ' + n2 + ' = ' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ';'; } case STMT_RETURN_ETC: switch (rng(8)) { case 0: case 1: case 2: case 3: if (canBreak && rng(5) === 0) return 'break' + getLabel(canBreak) + ';'; if (canContinue && rng(5) === 0) return 'continue' + getLabel(canContinue) + ';'; if (cannotReturn) return createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ';'; if (rng(3) == 0) return '/*3*/return;'; return 'return ' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ';'; case 4: // this is actually more like a parser test, but perhaps it hits some dead code elimination traps // must wrap in curlies to prevent orphaned `else` statement // note: you can't `throw` without an expression so don't put a `throw` option in this case if (cannotReturn) return createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ';'; return '{ /*2*/ return\n' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + '}'; default: // must wrap in curlies to prevent orphaned `else` statement if (canThrow && rng(5) === 0) return '{ throw ' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + '}'; if (cannotReturn) return createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ';'; return '{ return ' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + '}'; } case STMT_FUNC_EXPR: // "In non-strict mode code, functions can only be declared at top level, inside a block, or ..." // (dont both with func decls in `if`; it's only a parser thing because you cant call them without a block) return '{' + createFunction(recurmax, NO_DEFUN, canThrow, stmtDepth) + '}'; case STMT_TRY: // catch var could cause some problems // note: the "blocks" are syntactically mandatory for try/catch/finally var n = rng(3); // 0=only catch, 1=only finally, 2=catch+finally var s = 'try {' + createStatement(recurmax, n === 1 ? CANNOT_THROW : CAN_THROW, canBreak, canContinue, cannotReturn, stmtDepth) + ' }'; if (n !== 1) { // the catch var should only be accessible in the catch clause... // we have to do go through some trouble here to prevent leaking it var nameLenBefore = VAR_NAMES.length; var catchName = createVarName(MANDATORY); var freshCatchName = VAR_NAMES.length !== nameLenBefore; if (!catch_redef) unique_vars.push(catchName); s += ' catch (' + catchName + ') { ' + createStatements(3, recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + ' }'; // remove catch name if (!catch_redef) unique_vars.pop(); if (freshCatchName) VAR_NAMES.splice(nameLenBefore, 1); } if (n !== 0) s += ' finally { ' + createStatements(3, recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + ' }'; return s; case STMT_C: return 'c = c + 1;'; default: throw 'no'; } } function createSwitchParts(recurmax, n, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) { var hadDefault = false; var s = ['']; canBreak = enableLoopControl(canBreak, CAN_BREAK); while (n-- > 0) { //hadDefault = n > 0; // disables weird `default` clause positioning (use when handling destabilizes) if (hadDefault || rng(5) > 0) { s.push( 'case ' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ':', createStatements(rng(3) + 1, recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth), rng(10) > 0 ? ' break;' : '/* fall-through */', '' ); } else { hadDefault = true; s.push( 'default:', createStatements(rng(3) + 1, recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth), '' ); } } return s.join('\n'); } function createExpression(recurmax, noComma, stmtDepth, canThrow) { if (--recurmax < 0) { return '(c = 1 + c, ' + createNestedBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ')'; // note: should return a simple non-recursing expression value! } // since `a` and `b` are our canaries we want them more frequently than other expressions (1/3rd chance of a canary) switch (rng(6)) { case 0: return '(a++ + (' + _createExpression(recurmax, noComma, stmtDepth, canThrow) + '))'; case 1: return '((--b) + (' + _createExpression(recurmax, noComma, stmtDepth, canThrow) + '))'; case 2: return '((c = c + 1) + (' + _createExpression(recurmax, noComma, stmtDepth, canThrow) + '))'; // c only gets incremented default: return '(' + _createExpression(recurmax, noComma, stmtDepth, canThrow) + ')'; } } function _createExpression(recurmax, noComma, stmtDepth, canThrow) { var p = 0; switch (rng(_createExpression.N)) { case p++: case p++: return createUnaryPrefix() + (rng(2) === 1 ? 'a' : 'b'); case p++: case p++: return (rng(2) === 1 ? 'a' : 'b') + createUnaryPostfix(); case p++: case p++: // parens needed because assignments aren't valid unless they're the left-most op(s) in an expression return 'b ' + createAssignment() + ' a'; case p++: case p++: return rng(2) + ' === 1 ? a : b'; case p++: case p++: return createValue(); case p++: case p++: return getVarName(); case p++: return getVarName() + createAssignment() + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow); case p++: return createExpression(recurmax, COMMA_OK, stmtDepth, canThrow); case p++: return createExpression(recurmax, noComma, stmtDepth, canThrow) + '?' + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ':' + createExpression(recurmax, noComma, stmtDepth, canThrow); case p++: case p++: var nameLenBefore = VAR_NAMES.length; unique_vars.push('c'); var name = createVarName(MAYBE); // note: this name is only accessible from _within_ the function. and immutable at that. unique_vars.pop(); var s = []; switch (rng(5)) { case 0: s.push( '(function ' + name + '(){', strictMode(), createStatements(rng(5) + 1, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth), rng(2) == 0 ? '})' : '})()' ); break; case 1: s.push( '+function ' + name + '(){', strictMode(), createStatements(rng(5) + 1, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth), '}()' ); break; case 2: s.push( '!function ' + name + '(){', strictMode(), createStatements(rng(5) + 1, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth), '}()' ); break; case 3: s.push( 'void function ' + name + '(){', strictMode(), createStatements(rng(5) + 1, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth), '}()' ); break; default: var instantiate = rng(4) ? 'new ' : ''; s.push( instantiate + 'function ' + name + '(){', strictMode() ); if (instantiate) for (var i = rng(4); --i >= 0;) { if (rng(2)) s.push('this.' + getDotKey(true) + createAssignment() + _createBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ';'); else s.push('this[' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ']' + createAssignment() + _createBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ';'); } s.push( createStatements(rng(5) + 1, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth), rng(2) == 0 ? '}' : '}()' ); break; } VAR_NAMES.length = nameLenBefore; return filterDirective(s).join('\n'); case p++: case p++: return createTypeofExpr(recurmax, stmtDepth, canThrow); case p++: case p++: // more like a parser test but perhaps comment nodes mess up the analysis? // note: parens not needed for post-fix (since that's the default when ambiguous) // for prefix ops we need parens to prevent accidental syntax errors. switch (rng(6)) { case 0: return 'a/* ignore */++'; case 1: return 'b/* ignore */--'; case 2: return '++/* ignore */a'; case 3: return '--/* ignore */b'; case 4: // only groups that wrap a single variable return a "Reference", so this is still valid. // may just be a parser edge case that is invisible to uglify... return '--(b)'; case 5: // classic 0.3-0.1 case; 1-0.1-0.1-0.1 is not 0.7 :) return 'b + 1-0.1-0.1-0.1'; default: return '--/* ignore */b'; } case p++: case p++: return createNestedBinaryExpr(recurmax, noComma, stmtDepth, canThrow); case p++: case p++: return createUnarySafePrefix() + '(' + createNestedBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ')'; case p++: return " ((" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ") || a || 3).toString() "; case p++: return " /[abc4]/.test(((" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ") || b || 5).toString()) "; case p++: return " ((" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ") || " + rng(10) + ").toString()[" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + "] "; case p++: return createArrayLiteral(recurmax, stmtDepth, canThrow); case p++: return createObjectLiteral(recurmax, stmtDepth, canThrow); case p++: return createArrayLiteral(recurmax, stmtDepth, canThrow) + '[' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ']'; case p++: return createObjectLiteral(recurmax, stmtDepth, canThrow) + '[' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ']'; case p++: return createArrayLiteral(recurmax, stmtDepth, canThrow) + '.' + getDotKey(); case p++: return createObjectLiteral(recurmax, stmtDepth, canThrow) + '.' + getDotKey(); case p++: var name = getVarName(); return name + ' && ' + name + '[' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ']'; case p++: var name = getVarName(); return name + ' && ' + name + '.' + getDotKey(); case p++: case p++: case p++: case p++: var name = rng(3) == 0 ? getVarName() : 'f' + rng(funcs + 2); called[name] = true; return 'typeof ' + name + ' == "function" && --_calls_ >= 0 && ' + name + '(' + createArgs(recurmax, stmtDepth, canThrow) + ')'; } _createExpression.N = p; return _createExpression(recurmax, noComma, stmtDepth, canThrow); } function createArrayLiteral(recurmax, stmtDepth, canThrow) { recurmax--; var arr = "["; for (var i = rng(6); --i >= 0;) { // in rare cases produce an array hole element var element = rng(20) ? createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) : ""; arr += element + ", "; } return arr + "]"; } var SAFE_KEYS = [ "length", "foo", "a", "b", "c", "undefined", "null", "NaN", "Infinity", "in", "var", ]; var KEYS = [ "''", '"\t"', '"-2"', "0", "1.5", "3", ].concat(SAFE_KEYS); function getDotKey(assign) { var key; do { key = SAFE_KEYS[rng(SAFE_KEYS.length)]; } while (assign && key == "length"); return key; } function createAccessor(recurmax, stmtDepth, canThrow) { var namesLenBefore = VAR_NAMES.length; var s; var prop1 = getDotKey(); if (rng(2) == 0) { s = [ 'get ' + prop1 + '(){', strictMode(), createStatements(2, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth), createStatement(recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth, STMT_RETURN_ETC), '},' ]; } else { var prop2; do { prop2 = getDotKey(); } while (prop1 == prop2); s = [ 'set ' + prop1 + '(' + createVarName(MANDATORY) + '){', strictMode(), createStatements(2, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth), 'this.' + prop2 + createAssignment() + _createBinaryExpr(recurmax, COMMA_OK, stmtDepth, canThrow) + ';', '},' ]; } VAR_NAMES.length = namesLenBefore; return filterDirective(s).join('\n'); } function createObjectLiteral(recurmax, stmtDepth, canThrow) { recurmax--; var obj = ['({']; for (var i = rng(6); --i >= 0;) { if (rng(20) == 0) { obj.push(createAccessor(recurmax, stmtDepth, canThrow)); } else { var key = KEYS[rng(KEYS.length)]; obj.push(key + ':(' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + '),'); } } obj.push('})'); return obj.join('\n'); } function createNestedBinaryExpr(recurmax, noComma, stmtDepth, canThrow) { recurmax = 3; // note that this generates 2^recurmax expression parts... make sure to cap it return _createSimpleBinaryExpr(recurmax, noComma, stmtDepth, canThrow); } function _createBinaryExpr(recurmax, noComma, stmtDepth, canThrow) { return '(' + _createSimpleBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + createBinaryOp(noComma) + _createSimpleBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ')'; } function _createSimpleBinaryExpr(recurmax, noComma, stmtDepth, canThrow) { // intentionally generate more hardcore ops if (--recurmax < 0) return createValue(); var assignee, expr; switch (rng(30)) { case 0: return '(c = c + 1, ' + _createSimpleBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ')'; case 1: return '(' + createUnarySafePrefix() + '(' + _createSimpleBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + '))'; case 2: assignee = getVarName(); return '(' + assignee + createAssignment() + _createBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ')'; case 3: assignee = getVarName(); expr = '(' + assignee + '[' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ']' + createAssignment() + _createBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ')'; return canThrow && rng(10) == 0 ? expr : '(' + assignee + ' && ' + expr + ')'; case 4: assignee = getVarName(); expr = '(' + assignee + '.' + getDotKey(true) + createAssignment() + _createBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ')'; return canThrow && rng(10) == 0 ? expr : '(' + assignee + ' && ' + expr + ')'; default: return _createBinaryExpr(recurmax, noComma, stmtDepth, canThrow); } } function createTypeofExpr(recurmax, stmtDepth, canThrow) { switch (rng(8)) { case 0: return '(typeof ' + createVarName(MANDATORY, DONT_STORE) + ' === "' + TYPEOF_OUTCOMES[rng(TYPEOF_OUTCOMES.length)] + '")'; case 1: return '(typeof ' + createVarName(MANDATORY, DONT_STORE) + ' !== "' + TYPEOF_OUTCOMES[rng(TYPEOF_OUTCOMES.length)] + '")'; case 2: return '(typeof ' + createVarName(MANDATORY, DONT_STORE) + ' == "' + TYPEOF_OUTCOMES[rng(TYPEOF_OUTCOMES.length)] + '")'; case 3: return '(typeof ' + createVarName(MANDATORY, DONT_STORE) + ' != "' + TYPEOF_OUTCOMES[rng(TYPEOF_OUTCOMES.length)] + '")'; case 4: return '(typeof ' + createVarName(MANDATORY, DONT_STORE) + ')'; default: return '(typeof ' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ')'; } } function createValue() { return VALUES[rng(VALUES.length)]; } function createBinaryOp(noComma) { if (noComma) return BINARY_OPS_NO_COMMA[rng(BINARY_OPS_NO_COMMA.length)]; return BINARY_OPS[rng(BINARY_OPS.length)]; } function createAssignment() { return ASSIGNMENTS[rng(ASSIGNMENTS.length)]; } function createUnarySafePrefix() { return UNARY_SAFE[rng(UNARY_SAFE.length)]; } function createUnaryPrefix() { return UNARY_PREFIX[rng(UNARY_PREFIX.length)]; } function createUnaryPostfix() { return UNARY_POSTFIX[rng(UNARY_POSTFIX.length)]; } function getVarName() { // try to get a generated name reachable from current scope. default to just `a` return VAR_NAMES[INITIAL_NAMES_LEN + rng(VAR_NAMES.length - INITIAL_NAMES_LEN)] || 'a'; } function createVarName(maybe, dontStore) { if (!maybe || rng(2)) { var suffix = rng(3); var name; do { name = VAR_NAMES[rng(VAR_NAMES.length)]; if (suffix) name += '_' + suffix; } while (unique_vars.indexOf(name) >= 0); if (suffix && !dontStore) VAR_NAMES.push(name); return name; } return ''; } if (require.main !== module) { exports.createTopLevelCode = createTopLevelCode; exports.num_iterations = num_iterations; return; } function writeln(stream, msg) { if (typeof msg != "undefined") { stream.write(typeof msg == "string" ? msg : msg.stack || "" + msg); } stream.write("\n"); } function println(msg) { writeln(process.stdout, msg); } function errorln(msg) { writeln(process.stderr, msg); } function try_beautify(code, toplevel, result, printfn) { var beautified = UglifyJS.minify(code, { compress: false, mangle: false, output: { beautify: true, braces: true, }, }); if (beautified.error) { printfn("// !!! beautify failed !!!"); printfn(beautified.error); } else if (sandbox.same_stdout(sandbox.run_code(beautified.code, toplevel), result)) { printfn("// (beautified)"); printfn(beautified.code); return; } printfn("//"); printfn(code); } var default_options = UglifyJS.default_options(); function log_suspects(minify_options, component) { var options = component in minify_options ? minify_options[component] : true; if (!options) return; if (typeof options != "object") options = {}; var defs = default_options[component]; var suspects = Object.keys(defs).filter(function(name) { var flip = name == "keep_fargs"; if (flip ? name in options : (name in options ? options : defs)[name]) { var m = JSON.parse(JSON.stringify(minify_options)); var o = JSON.parse(JSON.stringify(options)); o[name] = flip; m[component] = o; var result = UglifyJS.minify(original_code, m); if (result.error) { errorln("Error testing options." + component + "." + name); errorln(result.error); } else { var r = sandbox.run_code(result.code, m.toplevel); return sandbox.same_stdout(original_result, r); } } }); if (suspects.length > 0) { errorln("Suspicious " + component + " options:"); suspects.forEach(function(name) { errorln(" " + name); }); errorln(); } } function log_rename(options) { var m = JSON.parse(JSON.stringify(options)); m.rename = false; var result = UglifyJS.minify(original_code, m); if (result.error) { errorln("Error testing options.rename"); errorln(result.error); } else { var r = sandbox.run_code(result.code, m.toplevel); if (sandbox.same_stdout(original_result, r)) { errorln("Suspicious options:"); errorln(" rename"); errorln(); } } } function log(options) { if (!ok) errorln('\n\n\n\n\n\n!!!!!!!!!!\n\n\n'); errorln("//============================================================="); if (!ok) errorln("// !!!!!! Failed... round " + round); errorln("// original code"); try_beautify(original_code, false, original_result, errorln); errorln(); errorln(); errorln("//-------------------------------------------------------------"); options = JSON.parse(options); if (typeof uglify_code == "string") { errorln("// uglified code"); try_beautify(uglify_code, options.toplevel, uglify_result, errorln); errorln(); errorln(); errorln("original result:"); errorln(original_result); errorln("uglified result:"); errorln(uglify_result); } else { errorln("// !!! uglify failed !!!"); errorln(uglify_code); if (errored) { errorln(); errorln(); errorln("original stacktrace:"); errorln(original_result); } } errorln("minify(options):"); errorln(JSON.stringify(options, null, 2)); errorln(); if (!ok && typeof uglify_code == "string") { Object.keys(default_options).forEach(log_suspects.bind(null, options)); log_rename(options); errorln("!!!!!! Failed... round " + round); } } var fallback_options = [ JSON.stringify({ compress: false, mangle: false }) ]; var minify_options = require("./ufuzz.json").map(JSON.stringify); var original_code, original_result, errored; var uglify_code, uglify_result, ok; for (var round = 1; round <= num_iterations; round++) { process.stdout.write(round + " of " + num_iterations + "\r"); original_code = createTopLevelCode(); var orig_result = [ sandbox.run_code(original_code) ]; errored = typeof orig_result[0] != "string"; if (!errored) orig_result.push(sandbox.run_code(original_code, true)); (errored ? fallback_options : minify_options).forEach(function(options) { var o = JSON.parse(options); uglify_code = UglifyJS.minify(original_code, o); original_result = orig_result[o.toplevel ? 1 : 0]; if (!uglify_code.error) { uglify_code = uglify_code.code; uglify_result = sandbox.run_code(uglify_code, o.toplevel); ok = sandbox.same_stdout(original_result, uglify_result); } else { uglify_code = uglify_code.error; if (errored) { ok = uglify_code.name == original_result.name; } } if (verbose || (verbose_interval && !(round % INTERVAL_COUNT)) || !ok) log(options); else if (errored) { println("//============================================================="); println("// original code"); try_beautify(original_code, o.toplevel, original_result, println); println(); println(); println("original result:"); println(original_result); println(); } if (!ok && isFinite(num_iterations)) { println(); process.exit(1); } }); } println(); UglifyJS2-3.6.3/test/ufuzz.json000066400000000000000000000013541355252637300163540ustar00rootroot00000000000000[ { "compress": false, "mangle": false, "output": { "beautify": true, "braces": true }, "rename": true }, { "compress": false }, { "mangle": false }, {}, { "ie8": true, "toplevel": true }, { "compress": { "passes": 1e6, "unsafe": true }, "toplevel": true }, { "compress": { "keep_fargs": false, "passes": 1e6, "sequences": 1e6, "unsafe": true, "unsafe_Function": true, "unsafe_math": true, "unsafe_proto": true, "unsafe_regexp": true } } ] UglifyJS2-3.6.3/tools/000077500000000000000000000000001355252637300144545ustar00rootroot00000000000000UglifyJS2-3.6.3/tools/domprops.json000066400000000000000000004455651355252637300172350ustar00rootroot00000000000000[ "$&", "$'", "$*", "$+", "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9", "$_", "$`", "$input", "-moz-animation", "-moz-animation-delay", "-moz-animation-direction", "-moz-animation-duration", "-moz-animation-fill-mode", "-moz-animation-iteration-count", "-moz-animation-name", "-moz-animation-play-state", "-moz-animation-timing-function", "-moz-appearance", "-moz-backface-visibility", "-moz-binding", "-moz-border-end", "-moz-border-end-color", "-moz-border-end-style", "-moz-border-end-width", "-moz-border-image", "-moz-border-start", "-moz-border-start-color", "-moz-border-start-style", "-moz-border-start-width", "-moz-box-align", "-moz-box-direction", "-moz-box-flex", "-moz-box-ordinal-group", "-moz-box-orient", "-moz-box-pack", "-moz-box-sizing", "-moz-column-count", "-moz-column-fill", "-moz-column-gap", "-moz-column-rule", "-moz-column-rule-color", "-moz-column-rule-style", "-moz-column-rule-width", "-moz-column-width", "-moz-columns", "-moz-float-edge", "-moz-font-feature-settings", "-moz-font-language-override", "-moz-force-broken-image-icon", "-moz-hyphens", "-moz-image-region", "-moz-margin-end", "-moz-margin-start", "-moz-orient", "-moz-outline-radius", "-moz-outline-radius-bottomleft", "-moz-outline-radius-bottomright", "-moz-outline-radius-topleft", "-moz-outline-radius-topright", "-moz-padding-end", "-moz-padding-start", "-moz-perspective", "-moz-perspective-origin", "-moz-stack-sizing", "-moz-tab-size", "-moz-text-size-adjust", "-moz-transform", "-moz-transform-origin", "-moz-transform-style", "-moz-transition", "-moz-transition-delay", "-moz-transition-duration", "-moz-transition-property", "-moz-transition-timing-function", "-moz-user-focus", "-moz-user-input", "-moz-user-modify", "-moz-user-select", "-moz-window-dragging", "-webkit-align-content", "-webkit-align-items", "-webkit-align-self", "-webkit-animation", "-webkit-animation-delay", "-webkit-animation-direction", "-webkit-animation-duration", "-webkit-animation-fill-mode", "-webkit-animation-iteration-count", "-webkit-animation-name", "-webkit-animation-play-state", "-webkit-animation-timing-function", "-webkit-appearance", "-webkit-backface-visibility", "-webkit-background-clip", "-webkit-background-origin", "-webkit-background-size", "-webkit-border-bottom-left-radius", "-webkit-border-bottom-right-radius", "-webkit-border-image", "-webkit-border-radius", "-webkit-border-top-left-radius", "-webkit-border-top-right-radius", "-webkit-box-align", "-webkit-box-direction", "-webkit-box-flex", "-webkit-box-ordinal-group", "-webkit-box-orient", "-webkit-box-pack", "-webkit-box-shadow", "-webkit-box-sizing", "-webkit-filter", "-webkit-flex", "-webkit-flex-basis", "-webkit-flex-direction", "-webkit-flex-flow", "-webkit-flex-grow", "-webkit-flex-shrink", "-webkit-flex-wrap", "-webkit-justify-content", "-webkit-mask", "-webkit-mask-clip", "-webkit-mask-composite", "-webkit-mask-image", "-webkit-mask-origin", "-webkit-mask-position", "-webkit-mask-position-x", "-webkit-mask-position-y", "-webkit-mask-repeat", "-webkit-mask-size", "-webkit-order", "-webkit-perspective", "-webkit-perspective-origin", "-webkit-text-fill-color", "-webkit-text-size-adjust", "-webkit-text-stroke", "-webkit-text-stroke-color", "-webkit-text-stroke-width", "-webkit-transform", "-webkit-transform-origin", "-webkit-transform-style", "-webkit-transition", "-webkit-transition-delay", "-webkit-transition-duration", "-webkit-transition-property", "-webkit-transition-timing-function", "-webkit-user-select", "@@iterator", "ABORT_ERR", "ACTIVE", "ACTIVE_ATTRIBUTES", "ACTIVE_TEXTURE", "ACTIVE_UNIFORMS", "ADDITION", "ALIASED_LINE_WIDTH_RANGE", "ALIASED_POINT_SIZE_RANGE", "ALLOW_KEYBOARD_INPUT", "ALLPASS", "ALPHA", "ALPHA_BITS", "ALT_MASK", "ALWAYS", "ANGLE_instanced_arrays", "ANY_TYPE", "ANY_UNORDERED_NODE_TYPE", "ARRAY_BUFFER", "ARRAY_BUFFER_BINDING", "ATTACHED_SHADERS", "ATTRIBUTE_NODE", "AT_TARGET", "AbortController", "AbortSignal", "AbsoluteOrientationSensor", "Accelerometer", "ActiveXObject", "AddSearchProvider", "AesGcmEncryptResult", "AnalyserNode", "Animation", "AnimationEffect", "AnimationEvent", "AnimationPlaybackEvent", "AnonXMLHttpRequest", "ApplicationCache", "ApplicationCacheErrorEvent", "Array", "ArrayBuffer", "Atomics", "Attr", "Audio", "AudioBuffer", "AudioBufferSourceNode", "AudioContext", "AudioDestinationNode", "AudioListener", "AudioNode", "AudioParam", "AudioParamMap", "AudioProcessingEvent", "AudioScheduledSourceNode", "AudioStreamTrack", "AudioTrack", "AudioTrackList", "AudioWorklet", "AudioWorkletNode", "AuthenticatorAssertionResponse", "AuthenticatorAttestationResponse", "AuthenticatorResponse", "AutocompleteErrorEvent", "BACK", "BAD_BOUNDARYPOINTS_ERR", "BANDPASS", "BLEND", "BLEND_COLOR", "BLEND_DST_ALPHA", "BLEND_DST_RGB", "BLEND_EQUATION", "BLEND_EQUATION_ALPHA", "BLEND_EQUATION_RGB", "BLEND_SRC_ALPHA", "BLEND_SRC_RGB", "BLUE_BITS", "BLUR", "BOOL", "BOOLEAN_TYPE", "BOOL_VEC2", "BOOL_VEC3", "BOOL_VEC4", "BOTH", "BROWSER_DEFAULT_WEBGL", "BUBBLING_PHASE", "BUFFER_SIZE", "BUFFER_USAGE", "BYTE", "BYTES_PER_ELEMENT", "BarProp", "BaseAudioContext", "BaseHref", "BatteryManager", "BeforeInstallPromptEvent", "BeforeLoadEvent", "BeforeUnloadEvent", "BigInt", "BigInt64Array", "BigUint64Array", "BiquadFilterNode", "Blob", "BlobEvent", "Bluetooth", "BluetoothCharacteristicProperties", "BluetoothDevice", "BluetoothRemoteGATTCharacteristic", "BluetoothRemoteGATTDescriptor", "BluetoothRemoteGATTServer", "BluetoothRemoteGATTService", "BluetoothUUID", "BookmarkCollection", "Boolean", "BroadcastChannel", "ByteLengthQueuingStrategy", "CANNOT_RUN", "CAPTURING_PHASE", "CCW", "CDATASection", "CDATA_SECTION_NODE", "CHANGE", "CHARSET_RULE", "CHECKING", "CLAMP_TO_EDGE", "CLICK", "CLOSED", "CLOSING", "COLOR_ATTACHMENT0", "COLOR_BUFFER_BIT", "COLOR_CLEAR_VALUE", "COLOR_WRITEMASK", "COMMENT_NODE", "COMPILE_STATUS", "COMPRESSED_RGBA_S3TC_DXT1_EXT", "COMPRESSED_RGBA_S3TC_DXT3_EXT", "COMPRESSED_RGBA_S3TC_DXT5_EXT", "COMPRESSED_RGB_S3TC_DXT1_EXT", "COMPRESSED_TEXTURE_FORMATS", "CONNECTING", "CONSTANT_ALPHA", "CONSTANT_COLOR", "CONSTRAINT_ERR", "CONTENT", "CONTEXT_LOST_WEBGL", "CONTROL_MASK", "COUNTER_STYLE_RULE", "CSS", "CSS2Properties", "CSSCharsetRule", "CSSConditionRule", "CSSCounterStyleRule", "CSSFontFaceRule", "CSSFontFeatureValuesRule", "CSSGroupingRule", "CSSImageValue", "CSSImportRule", "CSSKeyframeRule", "CSSKeyframesRule", "CSSKeywordValue", "CSSMathInvert", "CSSMathMax", "CSSMathMin", "CSSMathNegate", "CSSMathProduct", "CSSMathSum", "CSSMathValue", "CSSMatrixComponent", "CSSMediaRule", "CSSMozDocumentRule", "CSSNameSpaceRule", "CSSNamespaceRule", "CSSNumericArray", "CSSNumericValue", "CSSPageRule", "CSSPerspective", "CSSPositionValue", "CSSPrimitiveValue", "CSSRotate", "CSSRule", "CSSRuleList", "CSSScale", "CSSSkew", "CSSSkewX", "CSSSkewY", "CSSStyleDeclaration", "CSSStyleRule", "CSSStyleSheet", "CSSStyleValue", "CSSSupportsRule", "CSSTransformComponent", "CSSTransformValue", "CSSTranslate", "CSSUnitValue", "CSSUnknownRule", "CSSUnparsedValue", "CSSValue", "CSSValueList", "CSSVariableReferenceValue", "CSSVariablesDeclaration", "CSSVariablesRule", "CSSViewportRule", "CSS_ATTR", "CSS_CM", "CSS_COUNTER", "CSS_CUSTOM", "CSS_DEG", "CSS_DIMENSION", "CSS_EMS", "CSS_EXS", "CSS_FILTER_BLUR", "CSS_FILTER_BRIGHTNESS", "CSS_FILTER_CONTRAST", "CSS_FILTER_CUSTOM", "CSS_FILTER_DROP_SHADOW", "CSS_FILTER_GRAYSCALE", "CSS_FILTER_HUE_ROTATE", "CSS_FILTER_INVERT", "CSS_FILTER_OPACITY", "CSS_FILTER_REFERENCE", "CSS_FILTER_SATURATE", "CSS_FILTER_SEPIA", "CSS_GRAD", "CSS_HZ", "CSS_IDENT", "CSS_IN", "CSS_INHERIT", "CSS_KHZ", "CSS_MATRIX", "CSS_MATRIX3D", "CSS_MM", "CSS_MS", "CSS_NUMBER", "CSS_PC", "CSS_PERCENTAGE", "CSS_PERSPECTIVE", "CSS_PRIMITIVE_VALUE", "CSS_PT", "CSS_PX", "CSS_RAD", "CSS_RECT", "CSS_RGBCOLOR", "CSS_ROTATE", "CSS_ROTATE3D", "CSS_ROTATEX", "CSS_ROTATEY", "CSS_ROTATEZ", "CSS_S", "CSS_SCALE", "CSS_SCALE3D", "CSS_SCALEX", "CSS_SCALEY", "CSS_SCALEZ", "CSS_SKEW", "CSS_SKEWX", "CSS_SKEWY", "CSS_STRING", "CSS_TRANSLATE", "CSS_TRANSLATE3D", "CSS_TRANSLATEX", "CSS_TRANSLATEY", "CSS_TRANSLATEZ", "CSS_UNKNOWN", "CSS_URI", "CSS_VALUE_LIST", "CSS_VH", "CSS_VMAX", "CSS_VMIN", "CSS_VW", "CULL_FACE", "CULL_FACE_MODE", "CURRENT_PROGRAM", "CURRENT_VERTEX_ATTRIB", "CUSTOM", "CW", "Cache", "CacheStorage", "CanvasCaptureMediaStream", "CanvasCaptureMediaStreamTrack", "CanvasGradient", "CanvasPattern", "CanvasPixelArray", "CanvasRenderingContext2D", "CaretPosition", "ChannelMergerNode", "ChannelSplitterNode", "CharacterData", "Chrome PDF Plugin", "Chrome PDF Viewer", "ClientRect", "ClientRectList", "Clipboard", "ClipboardEvent", "CloseEvent", "Collator", "CollectGarbage", "CommandEvent", "Comment", "CompileError", "CompositionEvent", "Console", "ConstantSourceNode", "ControlRangeCollection", "Controllers", "ConvolverNode", "Coordinates", "CountQueuingStrategy", "Counter", "Credential", "CredentialsContainer", "Crypto", "CryptoKey", "CryptoOperation", "CustomElementRegistry", "CustomEvent", "DATABASE_ERR", "DATA_CLONE_ERR", "DATA_ERR", "DBLCLICK", "DECR", "DECR_WRAP", "DELETE_STATUS", "DEPTH_ATTACHMENT", "DEPTH_BITS", "DEPTH_BUFFER_BIT", "DEPTH_CLEAR_VALUE", "DEPTH_COMPONENT", "DEPTH_COMPONENT16", "DEPTH_FUNC", "DEPTH_RANGE", "DEPTH_STENCIL", "DEPTH_STENCIL_ATTACHMENT", "DEPTH_TEST", "DEPTH_WRITEMASK", "DIRECTION_DOWN", "DIRECTION_LEFT", "DIRECTION_RIGHT", "DIRECTION_UP", "DISABLED", "DISPATCH_REQUEST_ERR", "DITHER", "DOCUMENT_FRAGMENT_NODE", "DOCUMENT_NODE", "DOCUMENT_POSITION_CONTAINED_BY", "DOCUMENT_POSITION_CONTAINS", "DOCUMENT_POSITION_DISCONNECTED", "DOCUMENT_POSITION_FOLLOWING", "DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC", "DOCUMENT_POSITION_PRECEDING", "DOCUMENT_TYPE_NODE", "DOMCursor", "DOMError", "DOMException", "DOMImplementation", "DOMImplementationLS", "DOMMatrix", "DOMMatrixReadOnly", "DOMParser", "DOMPoint", "DOMPointReadOnly", "DOMQuad", "DOMRect", "DOMRectList", "DOMRectReadOnly", "DOMRequest", "DOMSTRING_SIZE_ERR", "DOMSettableTokenList", "DOMStringList", "DOMStringMap", "DOMTokenList", "DOMTransactionEvent", "DOM_DELTA_LINE", "DOM_DELTA_PAGE", "DOM_DELTA_PIXEL", "DOM_INPUT_METHOD_DROP", "DOM_INPUT_METHOD_HANDWRITING", "DOM_INPUT_METHOD_IME", "DOM_INPUT_METHOD_KEYBOARD", "DOM_INPUT_METHOD_MULTIMODAL", "DOM_INPUT_METHOD_OPTION", "DOM_INPUT_METHOD_PASTE", "DOM_INPUT_METHOD_SCRIPT", "DOM_INPUT_METHOD_UNKNOWN", "DOM_INPUT_METHOD_VOICE", "DOM_KEY_LOCATION_JOYSTICK", "DOM_KEY_LOCATION_LEFT", "DOM_KEY_LOCATION_MOBILE", "DOM_KEY_LOCATION_NUMPAD", "DOM_KEY_LOCATION_RIGHT", "DOM_KEY_LOCATION_STANDARD", "DOM_VK_0", "DOM_VK_1", "DOM_VK_2", "DOM_VK_3", "DOM_VK_4", "DOM_VK_5", "DOM_VK_6", "DOM_VK_7", "DOM_VK_8", "DOM_VK_9", "DOM_VK_A", "DOM_VK_ACCEPT", "DOM_VK_ADD", "DOM_VK_ALT", "DOM_VK_ALTGR", "DOM_VK_AMPERSAND", "DOM_VK_ASTERISK", "DOM_VK_AT", "DOM_VK_ATTN", "DOM_VK_B", "DOM_VK_BACKSPACE", "DOM_VK_BACK_QUOTE", "DOM_VK_BACK_SLASH", "DOM_VK_BACK_SPACE", "DOM_VK_C", "DOM_VK_CANCEL", "DOM_VK_CAPS_LOCK", "DOM_VK_CIRCUMFLEX", "DOM_VK_CLEAR", "DOM_VK_CLOSE_BRACKET", "DOM_VK_CLOSE_CURLY_BRACKET", "DOM_VK_CLOSE_PAREN", "DOM_VK_COLON", "DOM_VK_COMMA", "DOM_VK_CONTEXT_MENU", "DOM_VK_CONTROL", "DOM_VK_CONVERT", "DOM_VK_CRSEL", "DOM_VK_CTRL", "DOM_VK_D", "DOM_VK_DECIMAL", "DOM_VK_DELETE", "DOM_VK_DIVIDE", "DOM_VK_DOLLAR", "DOM_VK_DOUBLE_QUOTE", "DOM_VK_DOWN", "DOM_VK_E", "DOM_VK_EISU", "DOM_VK_END", "DOM_VK_ENTER", "DOM_VK_EQUALS", "DOM_VK_EREOF", "DOM_VK_ESCAPE", "DOM_VK_EXCLAMATION", "DOM_VK_EXECUTE", "DOM_VK_EXSEL", "DOM_VK_F", "DOM_VK_F1", "DOM_VK_F10", "DOM_VK_F11", "DOM_VK_F12", "DOM_VK_F13", "DOM_VK_F14", "DOM_VK_F15", "DOM_VK_F16", "DOM_VK_F17", "DOM_VK_F18", "DOM_VK_F19", "DOM_VK_F2", "DOM_VK_F20", "DOM_VK_F21", "DOM_VK_F22", "DOM_VK_F23", "DOM_VK_F24", "DOM_VK_F25", "DOM_VK_F26", "DOM_VK_F27", "DOM_VK_F28", "DOM_VK_F29", "DOM_VK_F3", "DOM_VK_F30", "DOM_VK_F31", "DOM_VK_F32", "DOM_VK_F33", "DOM_VK_F34", "DOM_VK_F35", "DOM_VK_F36", "DOM_VK_F4", "DOM_VK_F5", "DOM_VK_F6", "DOM_VK_F7", "DOM_VK_F8", "DOM_VK_F9", "DOM_VK_FINAL", "DOM_VK_FRONT", "DOM_VK_G", "DOM_VK_GREATER_THAN", "DOM_VK_H", "DOM_VK_HANGUL", "DOM_VK_HANJA", "DOM_VK_HASH", "DOM_VK_HELP", "DOM_VK_HK_TOGGLE", "DOM_VK_HOME", "DOM_VK_HYPHEN_MINUS", "DOM_VK_I", "DOM_VK_INSERT", "DOM_VK_J", "DOM_VK_JUNJA", "DOM_VK_K", "DOM_VK_KANA", "DOM_VK_KANJI", "DOM_VK_L", "DOM_VK_LEFT", "DOM_VK_LEFT_TAB", "DOM_VK_LESS_THAN", "DOM_VK_M", "DOM_VK_META", "DOM_VK_MODECHANGE", "DOM_VK_MULTIPLY", "DOM_VK_N", "DOM_VK_NONCONVERT", "DOM_VK_NUMPAD0", "DOM_VK_NUMPAD1", "DOM_VK_NUMPAD2", "DOM_VK_NUMPAD3", "DOM_VK_NUMPAD4", "DOM_VK_NUMPAD5", "DOM_VK_NUMPAD6", "DOM_VK_NUMPAD7", "DOM_VK_NUMPAD8", "DOM_VK_NUMPAD9", "DOM_VK_NUM_LOCK", "DOM_VK_O", "DOM_VK_OEM_1", "DOM_VK_OEM_102", "DOM_VK_OEM_2", "DOM_VK_OEM_3", "DOM_VK_OEM_4", "DOM_VK_OEM_5", "DOM_VK_OEM_6", "DOM_VK_OEM_7", "DOM_VK_OEM_8", "DOM_VK_OEM_COMMA", "DOM_VK_OEM_MINUS", "DOM_VK_OEM_PERIOD", "DOM_VK_OEM_PLUS", "DOM_VK_OPEN_BRACKET", "DOM_VK_OPEN_CURLY_BRACKET", "DOM_VK_OPEN_PAREN", "DOM_VK_P", "DOM_VK_PA1", "DOM_VK_PAGEDOWN", "DOM_VK_PAGEUP", "DOM_VK_PAGE_DOWN", "DOM_VK_PAGE_UP", "DOM_VK_PAUSE", "DOM_VK_PERCENT", "DOM_VK_PERIOD", "DOM_VK_PIPE", "DOM_VK_PLAY", "DOM_VK_PLUS", "DOM_VK_PRINT", "DOM_VK_PRINTSCREEN", "DOM_VK_PROCESSKEY", "DOM_VK_PROPERITES", "DOM_VK_Q", "DOM_VK_QUESTION_MARK", "DOM_VK_QUOTE", "DOM_VK_R", "DOM_VK_REDO", "DOM_VK_RETURN", "DOM_VK_RIGHT", "DOM_VK_S", "DOM_VK_SCROLL_LOCK", "DOM_VK_SELECT", "DOM_VK_SEMICOLON", "DOM_VK_SEPARATOR", "DOM_VK_SHIFT", "DOM_VK_SLASH", "DOM_VK_SLEEP", "DOM_VK_SPACE", "DOM_VK_SUBTRACT", "DOM_VK_T", "DOM_VK_TAB", "DOM_VK_TILDE", "DOM_VK_U", "DOM_VK_UNDERSCORE", "DOM_VK_UNDO", "DOM_VK_UNICODE", "DOM_VK_UP", "DOM_VK_V", "DOM_VK_VOLUME_DOWN", "DOM_VK_VOLUME_MUTE", "DOM_VK_VOLUME_UP", "DOM_VK_W", "DOM_VK_WIN", "DOM_VK_WINDOW", "DOM_VK_WIN_ICO_00", "DOM_VK_WIN_ICO_CLEAR", "DOM_VK_WIN_ICO_HELP", "DOM_VK_WIN_OEM_ATTN", "DOM_VK_WIN_OEM_AUTO", "DOM_VK_WIN_OEM_BACKTAB", "DOM_VK_WIN_OEM_CLEAR", "DOM_VK_WIN_OEM_COPY", "DOM_VK_WIN_OEM_CUSEL", "DOM_VK_WIN_OEM_ENLW", "DOM_VK_WIN_OEM_FINISH", "DOM_VK_WIN_OEM_FJ_JISHO", "DOM_VK_WIN_OEM_FJ_LOYA", "DOM_VK_WIN_OEM_FJ_MASSHOU", "DOM_VK_WIN_OEM_FJ_ROYA", "DOM_VK_WIN_OEM_FJ_TOUROKU", "DOM_VK_WIN_OEM_JUMP", "DOM_VK_WIN_OEM_PA1", "DOM_VK_WIN_OEM_PA2", "DOM_VK_WIN_OEM_PA3", "DOM_VK_WIN_OEM_RESET", "DOM_VK_WIN_OEM_WSCTRL", "DOM_VK_X", "DOM_VK_XF86XK_ADD_FAVORITE", "DOM_VK_XF86XK_APPLICATION_LEFT", "DOM_VK_XF86XK_APPLICATION_RIGHT", "DOM_VK_XF86XK_AUDIO_CYCLE_TRACK", "DOM_VK_XF86XK_AUDIO_FORWARD", "DOM_VK_XF86XK_AUDIO_LOWER_VOLUME", "DOM_VK_XF86XK_AUDIO_MEDIA", "DOM_VK_XF86XK_AUDIO_MUTE", "DOM_VK_XF86XK_AUDIO_NEXT", "DOM_VK_XF86XK_AUDIO_PAUSE", "DOM_VK_XF86XK_AUDIO_PLAY", "DOM_VK_XF86XK_AUDIO_PREV", "DOM_VK_XF86XK_AUDIO_RAISE_VOLUME", "DOM_VK_XF86XK_AUDIO_RANDOM_PLAY", "DOM_VK_XF86XK_AUDIO_RECORD", "DOM_VK_XF86XK_AUDIO_REPEAT", "DOM_VK_XF86XK_AUDIO_REWIND", "DOM_VK_XF86XK_AUDIO_STOP", "DOM_VK_XF86XK_AWAY", "DOM_VK_XF86XK_BACK", "DOM_VK_XF86XK_BACK_FORWARD", "DOM_VK_XF86XK_BATTERY", "DOM_VK_XF86XK_BLUE", "DOM_VK_XF86XK_BLUETOOTH", "DOM_VK_XF86XK_BOOK", "DOM_VK_XF86XK_BRIGHTNESS_ADJUST", "DOM_VK_XF86XK_CALCULATOR", "DOM_VK_XF86XK_CALENDAR", "DOM_VK_XF86XK_CD", "DOM_VK_XF86XK_CLOSE", "DOM_VK_XF86XK_COMMUNITY", "DOM_VK_XF86XK_CONTRAST_ADJUST", "DOM_VK_XF86XK_COPY", "DOM_VK_XF86XK_CUT", "DOM_VK_XF86XK_CYCLE_ANGLE", "DOM_VK_XF86XK_DISPLAY", "DOM_VK_XF86XK_DOCUMENTS", "DOM_VK_XF86XK_DOS", "DOM_VK_XF86XK_EJECT", "DOM_VK_XF86XK_EXCEL", "DOM_VK_XF86XK_EXPLORER", "DOM_VK_XF86XK_FAVORITES", "DOM_VK_XF86XK_FINANCE", "DOM_VK_XF86XK_FORWARD", "DOM_VK_XF86XK_FRAME_BACK", "DOM_VK_XF86XK_FRAME_FORWARD", "DOM_VK_XF86XK_GAME", "DOM_VK_XF86XK_GO", "DOM_VK_XF86XK_GREEN", "DOM_VK_XF86XK_HIBERNATE", "DOM_VK_XF86XK_HISTORY", "DOM_VK_XF86XK_HOME_PAGE", "DOM_VK_XF86XK_HOT_LINKS", "DOM_VK_XF86XK_I_TOUCH", "DOM_VK_XF86XK_KBD_BRIGHTNESS_DOWN", "DOM_VK_XF86XK_KBD_BRIGHTNESS_UP", "DOM_VK_XF86XK_KBD_LIGHT_ON_OFF", "DOM_VK_XF86XK_LAUNCH0", "DOM_VK_XF86XK_LAUNCH1", "DOM_VK_XF86XK_LAUNCH2", "DOM_VK_XF86XK_LAUNCH3", "DOM_VK_XF86XK_LAUNCH4", "DOM_VK_XF86XK_LAUNCH5", "DOM_VK_XF86XK_LAUNCH6", "DOM_VK_XF86XK_LAUNCH7", "DOM_VK_XF86XK_LAUNCH8", "DOM_VK_XF86XK_LAUNCH9", "DOM_VK_XF86XK_LAUNCH_A", "DOM_VK_XF86XK_LAUNCH_B", "DOM_VK_XF86XK_LAUNCH_C", "DOM_VK_XF86XK_LAUNCH_D", "DOM_VK_XF86XK_LAUNCH_E", "DOM_VK_XF86XK_LAUNCH_F", "DOM_VK_XF86XK_LIGHT_BULB", "DOM_VK_XF86XK_LOG_OFF", "DOM_VK_XF86XK_MAIL", "DOM_VK_XF86XK_MAIL_FORWARD", "DOM_VK_XF86XK_MARKET", "DOM_VK_XF86XK_MEETING", "DOM_VK_XF86XK_MEMO", "DOM_VK_XF86XK_MENU_KB", "DOM_VK_XF86XK_MENU_PB", "DOM_VK_XF86XK_MESSENGER", "DOM_VK_XF86XK_MON_BRIGHTNESS_DOWN", "DOM_VK_XF86XK_MON_BRIGHTNESS_UP", "DOM_VK_XF86XK_MUSIC", "DOM_VK_XF86XK_MY_COMPUTER", "DOM_VK_XF86XK_MY_SITES", "DOM_VK_XF86XK_NEW", "DOM_VK_XF86XK_NEWS", "DOM_VK_XF86XK_OFFICE_HOME", "DOM_VK_XF86XK_OPEN", "DOM_VK_XF86XK_OPEN_URL", "DOM_VK_XF86XK_OPTION", "DOM_VK_XF86XK_PASTE", "DOM_VK_XF86XK_PHONE", "DOM_VK_XF86XK_PICTURES", "DOM_VK_XF86XK_POWER_DOWN", "DOM_VK_XF86XK_POWER_OFF", "DOM_VK_XF86XK_RED", "DOM_VK_XF86XK_REFRESH", "DOM_VK_XF86XK_RELOAD", "DOM_VK_XF86XK_REPLY", "DOM_VK_XF86XK_ROCKER_DOWN", "DOM_VK_XF86XK_ROCKER_ENTER", "DOM_VK_XF86XK_ROCKER_UP", "DOM_VK_XF86XK_ROTATE_WINDOWS", "DOM_VK_XF86XK_ROTATION_KB", "DOM_VK_XF86XK_ROTATION_PB", "DOM_VK_XF86XK_SAVE", "DOM_VK_XF86XK_SCREEN_SAVER", "DOM_VK_XF86XK_SCROLL_CLICK", "DOM_VK_XF86XK_SCROLL_DOWN", "DOM_VK_XF86XK_SCROLL_UP", "DOM_VK_XF86XK_SEARCH", "DOM_VK_XF86XK_SEND", "DOM_VK_XF86XK_SHOP", "DOM_VK_XF86XK_SPELL", "DOM_VK_XF86XK_SPLIT_SCREEN", "DOM_VK_XF86XK_STANDBY", "DOM_VK_XF86XK_START", "DOM_VK_XF86XK_STOP", "DOM_VK_XF86XK_SUBTITLE", "DOM_VK_XF86XK_SUPPORT", "DOM_VK_XF86XK_SUSPEND", "DOM_VK_XF86XK_TASK_PANE", "DOM_VK_XF86XK_TERMINAL", "DOM_VK_XF86XK_TIME", "DOM_VK_XF86XK_TOOLS", "DOM_VK_XF86XK_TOP_MENU", "DOM_VK_XF86XK_TO_DO_LIST", "DOM_VK_XF86XK_TRAVEL", "DOM_VK_XF86XK_USER1KB", "DOM_VK_XF86XK_USER2KB", "DOM_VK_XF86XK_USER_PB", "DOM_VK_XF86XK_UWB", "DOM_VK_XF86XK_VENDOR_HOME", "DOM_VK_XF86XK_VIDEO", "DOM_VK_XF86XK_VIEW", "DOM_VK_XF86XK_WAKE_UP", "DOM_VK_XF86XK_WEB_CAM", "DOM_VK_XF86XK_WHEEL_BUTTON", "DOM_VK_XF86XK_WLAN", "DOM_VK_XF86XK_WORD", "DOM_VK_XF86XK_WWW", "DOM_VK_XF86XK_XFER", "DOM_VK_XF86XK_YELLOW", "DOM_VK_XF86XK_ZOOM_IN", "DOM_VK_XF86XK_ZOOM_OUT", "DOM_VK_Y", "DOM_VK_Z", "DOM_VK_ZOOM", "DONE", "DONT_CARE", "DOWNLOADING", "DRAGDROP", "DST_ALPHA", "DST_COLOR", "DYNAMIC_DRAW", "DataChannel", "DataTransfer", "DataTransferItem", "DataTransferItemList", "DataView", "Date", "DateTimeFormat", "Debug", "DelayNode", "DesktopNotification", "DesktopNotificationCenter", "DeviceAcceleration", "DeviceLightEvent", "DeviceMotionEvent", "DeviceOrientationEvent", "DeviceProximityEvent", "DeviceRotationRate", "DeviceStorage", "DeviceStorageChangeEvent", "Directory", "Document", "DocumentFragment", "DocumentType", "DragEvent", "DynamicsCompressorNode", "E", "ELEMENT_ARRAY_BUFFER", "ELEMENT_ARRAY_BUFFER_BINDING", "ELEMENT_NODE", "EMPTY", "ENCODING_ERR", "ENDED", "END_TO_END", "END_TO_START", "ENTITY_NODE", "ENTITY_REFERENCE_NODE", "EPSILON", "EQUAL", "EQUALPOWER", "ERROR", "EXPONENTIAL_DISTANCE", "EXT_texture_filter_anisotropic", "Element", "ElementQuery", "EnterPictureInPictureEvent", "Entity", "EntityReference", "Enumerator", "Error", "ErrorEvent", "EvalError", "Event", "EventException", "EventSource", "EventTarget", "External", "FASTEST", "FIDOSDK", "FILTER_ACCEPT", "FILTER_INTERRUPT", "FILTER_REJECT", "FILTER_SKIP", "FINISHED_STATE", "FIRST_ORDERED_NODE_TYPE", "FLOAT", "FLOAT_MAT2", "FLOAT_MAT3", "FLOAT_MAT4", "FLOAT_VEC2", "FLOAT_VEC3", "FLOAT_VEC4", "FOCUS", "FONT_FACE_RULE", "FONT_FEATURE_VALUES_RULE", "FRAGMENT_SHADER", "FRAGMENT_SHADER_DERIVATIVE_HINT_OES", "FRAMEBUFFER", "FRAMEBUFFER_ATTACHMENT_OBJECT_NAME", "FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE", "FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE", "FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL", "FRAMEBUFFER_BINDING", "FRAMEBUFFER_COMPLETE", "FRAMEBUFFER_INCOMPLETE_ATTACHMENT", "FRAMEBUFFER_INCOMPLETE_DIMENSIONS", "FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT", "FRAMEBUFFER_UNSUPPORTED", "FRONT", "FRONT_AND_BACK", "FRONT_FACE", "FUNC_ADD", "FUNC_REVERSE_SUBTRACT", "FUNC_SUBTRACT", "FederatedCredential", "Feed", "FeedEntry", "File", "FileError", "FileList", "FileReader", "FileSystem", "FileSystemDirectoryEntry", "FileSystemDirectoryReader", "FileSystemEntry", "FileSystemFileEntry", "FindInPage", "Float32Array", "Float64Array", "FocusEvent", "FontFace", "FontFaceSet", "FontFaceSetLoadEvent", "FormData", "Function", "GENERATE_MIPMAP_HINT", "GEQUAL", "GREATER", "GREEN_BITS", "GainNode", "Gamepad", "GamepadButton", "GamepadEvent", "GamepadHapticActuator", "GamepadPose", "Geolocation", "GestureEvent", "Global", "Gyroscope", "HAVE_CURRENT_DATA", "HAVE_ENOUGH_DATA", "HAVE_FUTURE_DATA", "HAVE_METADATA", "HAVE_NOTHING", "HEADERS_RECEIVED", "HIDDEN", "HIERARCHY_REQUEST_ERR", "HIGHPASS", "HIGHSHELF", "HIGH_FLOAT", "HIGH_INT", "HORIZONTAL", "HORIZONTAL_AXIS", "HRTF", "HTMLAllCollection", "HTMLAnchorElement", "HTMLAppletElement", "HTMLAreaElement", "HTMLAreasCollection", "HTMLAudioElement", "HTMLBGSoundElement", "HTMLBRElement", "HTMLBaseElement", "HTMLBaseFontElement", "HTMLBlockElement", "HTMLBlockquoteElement", "HTMLBodyElement", "HTMLButtonElement", "HTMLCanvasElement", "HTMLCollection", "HTMLCommandElement", "HTMLContentElement", "HTMLDDElement", "HTMLDListElement", "HTMLDTElement", "HTMLDataElement", "HTMLDataListElement", "HTMLDetailsElement", "HTMLDialogElement", "HTMLDirectoryElement", "HTMLDivElement", "HTMLDocument", "HTMLElement", "HTMLEmbedElement", "HTMLFieldSetElement", "HTMLFontElement", "HTMLFormControlsCollection", "HTMLFormElement", "HTMLFrameElement", "HTMLFrameSetElement", "HTMLHRElement", "HTMLHeadElement", "HTMLHeadingElement", "HTMLHtmlElement", "HTMLIFrameElement", "HTMLImageElement", "HTMLInputElement", "HTMLIsIndexElement", "HTMLKeygenElement", "HTMLLIElement", "HTMLLabelElement", "HTMLLegendElement", "HTMLLinkElement", "HTMLMapElement", "HTMLMarqueeElement", "HTMLMediaElement", "HTMLMenuElement", "HTMLMenuItemElement", "HTMLMetaElement", "HTMLMeterElement", "HTMLModElement", "HTMLNextIdElement", "HTMLOListElement", "HTMLObjectElement", "HTMLOptGroupElement", "HTMLOptionElement", "HTMLOptionsCollection", "HTMLOutputElement", "HTMLParagraphElement", "HTMLParamElement", "HTMLPhraseElement", "HTMLPictureElement", "HTMLPreElement", "HTMLProgressElement", "HTMLPropertiesCollection", "HTMLQuoteElement", "HTMLScriptElement", "HTMLSelectElement", "HTMLShadowElement", "HTMLSlotElement", "HTMLSourceElement", "HTMLSpanElement", "HTMLStyleElement", "HTMLTableCaptionElement", "HTMLTableCellElement", "HTMLTableColElement", "HTMLTableDataCellElement", "HTMLTableElement", "HTMLTableHeaderCellElement", "HTMLTableRowElement", "HTMLTableSectionElement", "HTMLTemplateElement", "HTMLTextAreaElement", "HTMLTimeElement", "HTMLTitleElement", "HTMLTrackElement", "HTMLUListElement", "HTMLUnknownElement", "HTMLVideoElement", "HashChangeEvent", "Headers", "History", "ICE_CHECKING", "ICE_CLOSED", "ICE_COMPLETED", "ICE_CONNECTED", "ICE_FAILED", "ICE_GATHERING", "ICE_WAITING", "IDBCursor", "IDBCursorWithValue", "IDBDatabase", "IDBDatabaseException", "IDBFactory", "IDBFileHandle", "IDBFileRequest", "IDBIndex", "IDBKeyRange", "IDBMutableFile", "IDBObjectStore", "IDBOpenDBRequest", "IDBRequest", "IDBTransaction", "IDBVersionChangeEvent", "IDLE", "IIRFilterNode", "IMPLEMENTATION_COLOR_READ_FORMAT", "IMPLEMENTATION_COLOR_READ_TYPE", "IMPORT_RULE", "INCR", "INCR_WRAP", "INDEX_SIZE_ERR", "INSTALLED", "INT", "INT_VEC2", "INT_VEC3", "INT_VEC4", "INUSE_ATTRIBUTE_ERR", "INVALID_ACCESS_ERR", "INVALID_CHARACTER_ERR", "INVALID_ENUM", "INVALID_EXPRESSION_ERR", "INVALID_FRAMEBUFFER_OPERATION", "INVALID_MODIFICATION_ERR", "INVALID_NODE_TYPE_ERR", "INVALID_OPERATION", "INVALID_STATE_ERR", "INVALID_VALUE", "INVERSE_DISTANCE", "INVERT", "IceCandidate", "IdleDeadline", "Image", "ImageBitmap", "ImageBitmapRenderingContext", "ImageCapture", "ImageData", "Infinity", "InputDeviceCapabilities", "InputDeviceInfo", "InputEvent", "InputMethodContext", "InstallState", "InstallTrigger", "Instance", "Int16Array", "Int32Array", "Int8Array", "Intent", "InternalError", "IntersectionObserver", "IntersectionObserverEntry", "Intl", "IsSearchProviderInstalled", "Iterator", "JSON", "KEEP", "KEYDOWN", "KEYFRAMES_RULE", "KEYFRAME_RULE", "KEYPRESS", "KEYUP", "Key", "KeyEvent", "KeyOperation", "KeyPair", "Keyboard", "KeyboardEvent", "KeyboardLayoutMap", "KeyframeEffect", "LENGTHADJUST_SPACING", "LENGTHADJUST_SPACINGANDGLYPHS", "LENGTHADJUST_UNKNOWN", "LEQUAL", "LESS", "LINEAR", "LINEAR_DISTANCE", "LINEAR_MIPMAP_LINEAR", "LINEAR_MIPMAP_NEAREST", "LINES", "LINE_LOOP", "LINE_STRIP", "LINE_WIDTH", "LINK_STATUS", "LIVE", "LN10", "LN2", "LOADED", "LOADING", "LOCALE", "LOG10E", "LOG2E", "LOWPASS", "LOWSHELF", "LOW_FLOAT", "LOW_INT", "LSException", "LSParserFilter", "LUMINANCE", "LUMINANCE_ALPHA", "LinearAccelerationSensor", "LinkError", "ListFormat", "LocalMediaStream", "Location", "Lock", "LockManager", "MAX_COMBINED_TEXTURE_IMAGE_UNITS", "MAX_CUBE_MAP_TEXTURE_SIZE", "MAX_FRAGMENT_UNIFORM_VECTORS", "MAX_RENDERBUFFER_SIZE", "MAX_SAFE_INTEGER", "MAX_TEXTURE_IMAGE_UNITS", "MAX_TEXTURE_MAX_ANISOTROPY_EXT", "MAX_TEXTURE_SIZE", "MAX_VALUE", "MAX_VARYING_VECTORS", "MAX_VERTEX_ATTRIBS", "MAX_VERTEX_TEXTURE_IMAGE_UNITS", "MAX_VERTEX_UNIFORM_VECTORS", "MAX_VIEWPORT_DIMS", "MEDIA_ERR_ABORTED", "MEDIA_ERR_DECODE", "MEDIA_ERR_ENCRYPTED", "MEDIA_ERR_NETWORK", "MEDIA_ERR_SRC_NOT_SUPPORTED", "MEDIA_KEYERR_CLIENT", "MEDIA_KEYERR_DOMAIN", "MEDIA_KEYERR_HARDWARECHANGE", "MEDIA_KEYERR_OUTPUT", "MEDIA_KEYERR_SERVICE", "MEDIA_KEYERR_UNKNOWN", "MEDIA_RULE", "MEDIUM_FLOAT", "MEDIUM_INT", "META_MASK", "MIDIAccess", "MIDIConnectionEvent", "MIDIInput", "MIDIInputMap", "MIDIMessageEvent", "MIDIOutput", "MIDIOutputMap", "MIDIPort", "MIN_SAFE_INTEGER", "MIN_VALUE", "MIRRORED_REPEAT", "MODE_ASYNCHRONOUS", "MODE_SYNCHRONOUS", "MODIFICATION", "MOUSEDOWN", "MOUSEDRAG", "MOUSEMOVE", "MOUSEOUT", "MOUSEOVER", "MOUSEUP", "MOZ_KEYFRAMES_RULE", "MOZ_KEYFRAME_RULE", "MOZ_SOURCE_CURSOR", "MOZ_SOURCE_ERASER", "MOZ_SOURCE_KEYBOARD", "MOZ_SOURCE_MOUSE", "MOZ_SOURCE_PEN", "MOZ_SOURCE_TOUCH", "MOZ_SOURCE_UNKNOWN", "MSBehaviorUrnsCollection", "MSBlobBuilder", "MSCSSMatrix", "MSCSSProperties", "MSCSSRuleList", "MSCompatibleInfo", "MSCompatibleInfoCollection", "MSCurrentStyleCSSProperties", "MSEventObj", "MSGESTURE_FLAG_BEGIN", "MSGESTURE_FLAG_CANCEL", "MSGESTURE_FLAG_END", "MSGESTURE_FLAG_INERTIA", "MSGESTURE_FLAG_NONE", "MSGesture", "MSGestureEvent", "MSGraphicsTrust", "MSInputMethodContext", "MSManipulationEvent", "MSMediaKeyError", "MSMediaKeyMessageEvent", "MSMediaKeyNeededEvent", "MSMediaKeySession", "MSMediaKeys", "MSMimeTypesCollection", "MSPOINTER_TYPE_MOUSE", "MSPOINTER_TYPE_PEN", "MSPOINTER_TYPE_TOUCH", "MSPluginsCollection", "MSPointerEvent", "MSRangeCollection", "MSSiteModeEvent", "MSStream", "MSStreamReader", "MSStyleCSSProperties", "MS_ASYNC_CALLBACK_STATUS_ASSIGN_DELEGATE", "MS_ASYNC_CALLBACK_STATUS_CANCEL", "MS_ASYNC_CALLBACK_STATUS_CHOOSEANY", "MS_ASYNC_CALLBACK_STATUS_ERROR", "MS_ASYNC_CALLBACK_STATUS_JOIN", "MS_ASYNC_OP_STATUS_CANCELED", "MS_ASYNC_OP_STATUS_ERROR", "MS_ASYNC_OP_STATUS_SUCCESS", "MS_MANIPULATION_STATE_ACTIVE", "MS_MANIPULATION_STATE_CANCELLED", "MS_MANIPULATION_STATE_COMMITTED", "MS_MANIPULATION_STATE_DRAGGING", "MS_MANIPULATION_STATE_INERTIA", "MS_MANIPULATION_STATE_PRESELECT", "MS_MANIPULATION_STATE_SELECTING", "MS_MANIPULATION_STATE_STOPPED", "MS_MEDIA_ERR_ENCRYPTED", "MS_MEDIA_KEYERR_CLIENT", "MS_MEDIA_KEYERR_DOMAIN", "MS_MEDIA_KEYERR_HARDWARECHANGE", "MS_MEDIA_KEYERR_OUTPUT", "MS_MEDIA_KEYERR_SERVICE", "MS_MEDIA_KEYERR_UNKNOWN", "Map", "Math", "MediaCapabilities", "MediaCapabilitiesInfo", "MediaController", "MediaDeviceInfo", "MediaDevices", "MediaElementAudioSourceNode", "MediaEncryptedEvent", "MediaError", "MediaKeyError", "MediaKeyEvent", "MediaKeyMessageEvent", "MediaKeyNeededEvent", "MediaKeySession", "MediaKeyStatusMap", "MediaKeySystemAccess", "MediaKeys", "MediaList", "MediaMetadata", "MediaQueryList", "MediaQueryListEvent", "MediaRecorder", "MediaRecorderErrorEvent", "MediaSession", "MediaSettingsRange", "MediaSource", "MediaStream", "MediaStreamAudioDestinationNode", "MediaStreamAudioSourceNode", "MediaStreamEvent", "MediaStreamTrack", "MediaStreamTrackEvent", "Memory", "MessageChannel", "MessageEvent", "MessagePort", "Methods", "MimeType", "MimeTypeArray", "Module", "MouseEvent", "MouseScrollEvent", "MouseWheelEvent", "MozAnimation", "MozAnimationDelay", "MozAnimationDirection", "MozAnimationDuration", "MozAnimationFillMode", "MozAnimationIterationCount", "MozAnimationName", "MozAnimationPlayState", "MozAnimationTimingFunction", "MozAppearance", "MozBackfaceVisibility", "MozBinding", "MozBorderBottomColors", "MozBorderEnd", "MozBorderEndColor", "MozBorderEndStyle", "MozBorderEndWidth", "MozBorderImage", "MozBorderLeftColors", "MozBorderRightColors", "MozBorderStart", "MozBorderStartColor", "MozBorderStartStyle", "MozBorderStartWidth", "MozBorderTopColors", "MozBoxAlign", "MozBoxDirection", "MozBoxFlex", "MozBoxOrdinalGroup", "MozBoxOrient", "MozBoxPack", "MozBoxSizing", "MozCSSKeyframeRule", "MozCSSKeyframesRule", "MozColumnCount", "MozColumnFill", "MozColumnGap", "MozColumnRule", "MozColumnRuleColor", "MozColumnRuleStyle", "MozColumnRuleWidth", "MozColumnWidth", "MozColumns", "MozContactChangeEvent", "MozFloatEdge", "MozFontFeatureSettings", "MozFontLanguageOverride", "MozForceBrokenImageIcon", "MozHyphens", "MozImageRegion", "MozMarginEnd", "MozMarginStart", "MozMmsEvent", "MozMmsMessage", "MozMobileMessageThread", "MozOSXFontSmoothing", "MozOrient", "MozOutlineRadius", "MozOutlineRadiusBottomleft", "MozOutlineRadiusBottomright", "MozOutlineRadiusTopleft", "MozOutlineRadiusTopright", "MozPaddingEnd", "MozPaddingStart", "MozPerspective", "MozPerspectiveOrigin", "MozPowerManager", "MozSettingsEvent", "MozSmsEvent", "MozSmsMessage", "MozStackSizing", "MozTabSize", "MozTextAlignLast", "MozTextDecorationColor", "MozTextDecorationLine", "MozTextDecorationStyle", "MozTextSizeAdjust", "MozTransform", "MozTransformOrigin", "MozTransformStyle", "MozTransition", "MozTransitionDelay", "MozTransitionDuration", "MozTransitionProperty", "MozTransitionTimingFunction", "MozUserFocus", "MozUserInput", "MozUserModify", "MozUserSelect", "MozWindowDragging", "MozWindowShadow", "MutationEvent", "MutationObserver", "MutationRecord", "NAMESPACE_ERR", "NAMESPACE_RULE", "NEAREST", "NEAREST_MIPMAP_LINEAR", "NEAREST_MIPMAP_NEAREST", "NEGATIVE_INFINITY", "NETWORK_EMPTY", "NETWORK_ERR", "NETWORK_IDLE", "NETWORK_LOADED", "NETWORK_LOADING", "NETWORK_NO_SOURCE", "NEVER", "NEW", "NEXT", "NEXT_NO_DUPLICATE", "NICEST", "NODE_AFTER", "NODE_BEFORE", "NODE_BEFORE_AND_AFTER", "NODE_INSIDE", "NONE", "NON_TRANSIENT_ERR", "NOTATION_NODE", "NOTCH", "NOTEQUAL", "NOT_ALLOWED_ERR", "NOT_FOUND_ERR", "NOT_INSTALLED", "NOT_READABLE_ERR", "NOT_SUPPORTED_ERR", "NO_DATA_ALLOWED_ERR", "NO_ERR", "NO_ERROR", "NO_MODIFICATION_ALLOWED_ERR", "NUMBER_TYPE", "NUM_COMPRESSED_TEXTURE_FORMATS", "NaN", "NamedNodeMap", "Native Client", "NavigationPreloadManager", "Navigator", "NearbyLinks", "NetworkInformation", "Node", "NodeFilter", "NodeIterator", "NodeList", "Notation", "Notification", "NotifyPaintEvent", "Number", "NumberFormat", "OBSOLETE", "OES_element_index_uint", "OES_standard_derivatives", "OES_texture_float", "OES_texture_float_linear", "ONE", "ONE_MINUS_CONSTANT_ALPHA", "ONE_MINUS_CONSTANT_COLOR", "ONE_MINUS_DST_ALPHA", "ONE_MINUS_DST_COLOR", "ONE_MINUS_SRC_ALPHA", "ONE_MINUS_SRC_COLOR", "OPEN", "OPENED", "OPENING", "ORDERED_NODE_ITERATOR_TYPE", "ORDERED_NODE_SNAPSHOT_TYPE", "OUT_OF_MEMORY", "Object", "OfflineAudioCompletionEvent", "OfflineAudioContext", "OfflineResourceList", "OffscreenCanvas", "OffscreenCanvasRenderingContext2D", "Option", "OrientationSensor", "OscillatorNode", "OverconstrainedError", "OverflowEvent", "PACKAGE", "PACK_ALIGNMENT", "PAGE_RULE", "PARSE_ERR", "PATHSEG_ARC_ABS", "PATHSEG_ARC_REL", "PATHSEG_CLOSEPATH", "PATHSEG_CURVETO_CUBIC_ABS", "PATHSEG_CURVETO_CUBIC_REL", "PATHSEG_CURVETO_CUBIC_SMOOTH_ABS", "PATHSEG_CURVETO_CUBIC_SMOOTH_REL", "PATHSEG_CURVETO_QUADRATIC_ABS", "PATHSEG_CURVETO_QUADRATIC_REL", "PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS", "PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL", "PATHSEG_LINETO_ABS", "PATHSEG_LINETO_HORIZONTAL_ABS", "PATHSEG_LINETO_HORIZONTAL_REL", "PATHSEG_LINETO_REL", "PATHSEG_LINETO_VERTICAL_ABS", "PATHSEG_LINETO_VERTICAL_REL", "PATHSEG_MOVETO_ABS", "PATHSEG_MOVETO_REL", "PATHSEG_UNKNOWN", "PATH_EXISTS_ERR", "PEAKING", "PERMISSION_DENIED", "PERSISTENT", "PI", "PLAYING_STATE", "POINTS", "POLYGON_OFFSET_FACTOR", "POLYGON_OFFSET_FILL", "POLYGON_OFFSET_UNITS", "POSITION_UNAVAILABLE", "POSITIVE_INFINITY", "PREV", "PREV_NO_DUPLICATE", "PROCESSING_INSTRUCTION_NODE", "PageChangeEvent", "PageTransitionEvent", "PaintRequest", "PaintRequestList", "PannerNode", "PasswordCredential", "Path2D", "PaymentAddress", "PaymentInstruments", "PaymentManager", "PaymentRequest", "PaymentRequestUpdateEvent", "PaymentResponse", "Performance", "PerformanceEntry", "PerformanceLongTaskTiming", "PerformanceMark", "PerformanceMeasure", "PerformanceNavigation", "PerformanceNavigationTiming", "PerformanceObserver", "PerformanceObserverEntryList", "PerformancePaintTiming", "PerformanceResourceTiming", "PerformanceServerTiming", "PerformanceTiming", "PeriodicWave", "PermissionStatus", "Permissions", "PhotoCapabilities", "PictureInPictureWindow", "Plugin", "PluginArray", "PluralRules", "PointerEvent", "PopStateEvent", "PopupBlockedEvent", "Position", "PositionError", "Presentation", "PresentationAvailability", "PresentationConnection", "PresentationConnectionAvailableEvent", "PresentationConnectionCloseEvent", "PresentationConnectionList", "PresentationReceiver", "PresentationRequest", "ProcessingInstruction", "ProgressEvent", "Promise", "PromiseRejectionEvent", "PropertyNodeList", "Proxy", "PublicKeyCredential", "PushManager", "PushSubscription", "PushSubscriptionOptions", "Q", "QUOTA_ERR", "QUOTA_EXCEEDED_ERR", "QueryInterface", "READY_TO_RUN", "READ_ONLY", "READ_ONLY_ERR", "READ_WRITE", "RED_BITS", "REMOVAL", "RENDERBUFFER", "RENDERBUFFER_ALPHA_SIZE", "RENDERBUFFER_BINDING", "RENDERBUFFER_BLUE_SIZE", "RENDERBUFFER_DEPTH_SIZE", "RENDERBUFFER_GREEN_SIZE", "RENDERBUFFER_HEIGHT", "RENDERBUFFER_INTERNAL_FORMAT", "RENDERBUFFER_RED_SIZE", "RENDERBUFFER_STENCIL_SIZE", "RENDERBUFFER_WIDTH", "RENDERER", "RENDERING_INTENT_ABSOLUTE_COLORIMETRIC", "RENDERING_INTENT_AUTO", "RENDERING_INTENT_PERCEPTUAL", "RENDERING_INTENT_RELATIVE_COLORIMETRIC", "RENDERING_INTENT_SATURATION", "RENDERING_INTENT_UNKNOWN", "REPEAT", "REPLACE", "RGB", "RGB565", "RGB5_A1", "RGBA", "RGBA4", "RGBColor", "ROTATION_CLOCKWISE", "ROTATION_COUNTERCLOCKWISE", "RTCCertificate", "RTCDTMFSender", "RTCDTMFToneChangeEvent", "RTCDataChannel", "RTCDataChannelEvent", "RTCIceCandidate", "RTCPeerConnection", "RTCPeerConnectionIceEvent", "RTCRtpReceiver", "RTCRtpSender", "RTCRtpTransceiver", "RTCSessionDescription", "RTCStatsReport", "RTCTrackEvent", "RUNNING", "RadioNodeList", "Range", "RangeError", "RangeException", "ReadableStream", "RecordErrorEvent", "Rect", "ReferenceError", "Reflect", "RegExp", "RelativeOrientationSensor", "RelativeTimeFormat", "RemotePlayback", "ReportingObserver", "Request", "ResizeObserver", "ResizeObserverEntry", "Response", "RunningState", "RuntimeError", "SAMPLER_2D", "SAMPLER_CUBE", "SAMPLES", "SAMPLE_ALPHA_TO_COVERAGE", "SAMPLE_BUFFERS", "SAMPLE_COVERAGE", "SAMPLE_COVERAGE_INVERT", "SAMPLE_COVERAGE_VALUE", "SAWTOOTH", "SCHEDULED_STATE", "SCISSOR_BOX", "SCISSOR_TEST", "SCROLL_PAGE_DOWN", "SCROLL_PAGE_UP", "SDP_ANSWER", "SDP_OFFER", "SDP_PRANSWER", "SECURITY_ERR", "SELECT", "SERIALIZE_ERR", "SEVERITY_ERROR", "SEVERITY_FATAL_ERROR", "SEVERITY_WARNING", "SHADER_COMPILER", "SHADER_TYPE", "SHADING_LANGUAGE_VERSION", "SHIFT_MASK", "SHORT", "SHOWING", "SHOW_ALL", "SHOW_ATTRIBUTE", "SHOW_CDATA_SECTION", "SHOW_COMMENT", "SHOW_DOCUMENT", "SHOW_DOCUMENT_FRAGMENT", "SHOW_DOCUMENT_TYPE", "SHOW_ELEMENT", "SHOW_ENTITY", "SHOW_ENTITY_REFERENCE", "SHOW_NOTATION", "SHOW_PROCESSING_INSTRUCTION", "SHOW_TEXT", "SINE", "SKIN", "SOUNDFIELD", "SQLException", "SQRT1_2", "SQRT2", "SQUARE", "SRC_ALPHA", "SRC_ALPHA_SATURATE", "SRC_COLOR", "START_TO_END", "START_TO_START", "STATIC_DRAW", "STENCIL_ATTACHMENT", "STENCIL_BACK_FAIL", "STENCIL_BACK_FUNC", "STENCIL_BACK_PASS_DEPTH_FAIL", "STENCIL_BACK_PASS_DEPTH_PASS", "STENCIL_BACK_REF", "STENCIL_BACK_VALUE_MASK", "STENCIL_BACK_WRITEMASK", "STENCIL_BITS", "STENCIL_BUFFER_BIT", "STENCIL_CLEAR_VALUE", "STENCIL_FAIL", "STENCIL_FUNC", "STENCIL_INDEX", "STENCIL_INDEX8", "STENCIL_PASS_DEPTH_FAIL", "STENCIL_PASS_DEPTH_PASS", "STENCIL_REF", "STENCIL_TEST", "STENCIL_VALUE_MASK", "STENCIL_WRITEMASK", "STREAM_DRAW", "STRING_TYPE", "STYLE_RULE", "SUBPIXEL_BITS", "SUPPORTS_RULE", "SVGAElement", "SVGAltGlyphDefElement", "SVGAltGlyphElement", "SVGAltGlyphItemElement", "SVGAngle", "SVGAnimateColorElement", "SVGAnimateElement", "SVGAnimateMotionElement", "SVGAnimateTransformElement", "SVGAnimatedAngle", "SVGAnimatedBoolean", "SVGAnimatedEnumeration", "SVGAnimatedInteger", "SVGAnimatedLength", "SVGAnimatedLengthList", "SVGAnimatedNumber", "SVGAnimatedNumberList", "SVGAnimatedPreserveAspectRatio", "SVGAnimatedRect", "SVGAnimatedString", "SVGAnimatedTransformList", "SVGAnimationElement", "SVGCircleElement", "SVGClipPathElement", "SVGColor", "SVGComponentTransferFunctionElement", "SVGCursorElement", "SVGDefsElement", "SVGDescElement", "SVGDiscardElement", "SVGDocument", "SVGElement", "SVGElementInstance", "SVGElementInstanceList", "SVGEllipseElement", "SVGException", "SVGFEBlendElement", "SVGFEColorMatrixElement", "SVGFEComponentTransferElement", "SVGFECompositeElement", "SVGFEConvolveMatrixElement", "SVGFEDiffuseLightingElement", "SVGFEDisplacementMapElement", "SVGFEDistantLightElement", "SVGFEDropShadowElement", "SVGFEFloodElement", "SVGFEFuncAElement", "SVGFEFuncBElement", "SVGFEFuncGElement", "SVGFEFuncRElement", "SVGFEGaussianBlurElement", "SVGFEImageElement", "SVGFEMergeElement", "SVGFEMergeNodeElement", "SVGFEMorphologyElement", "SVGFEOffsetElement", "SVGFEPointLightElement", "SVGFESpecularLightingElement", "SVGFESpotLightElement", "SVGFETileElement", "SVGFETurbulenceElement", "SVGFilterElement", "SVGFontElement", "SVGFontFaceElement", "SVGFontFaceFormatElement", "SVGFontFaceNameElement", "SVGFontFaceSrcElement", "SVGFontFaceUriElement", "SVGForeignObjectElement", "SVGGElement", "SVGGeometryElement", "SVGGlyphElement", "SVGGlyphRefElement", "SVGGradientElement", "SVGGraphicsElement", "SVGHKernElement", "SVGImageElement", "SVGLength", "SVGLengthList", "SVGLineElement", "SVGLinearGradientElement", "SVGMPathElement", "SVGMarkerElement", "SVGMaskElement", "SVGMatrix", "SVGMetadataElement", "SVGMissingGlyphElement", "SVGNumber", "SVGNumberList", "SVGPaint", "SVGPathElement", "SVGPathSeg", "SVGPathSegArcAbs", "SVGPathSegArcRel", "SVGPathSegClosePath", "SVGPathSegCurvetoCubicAbs", "SVGPathSegCurvetoCubicRel", "SVGPathSegCurvetoCubicSmoothAbs", "SVGPathSegCurvetoCubicSmoothRel", "SVGPathSegCurvetoQuadraticAbs", "SVGPathSegCurvetoQuadraticRel", "SVGPathSegCurvetoQuadraticSmoothAbs", "SVGPathSegCurvetoQuadraticSmoothRel", "SVGPathSegLinetoAbs", "SVGPathSegLinetoHorizontalAbs", "SVGPathSegLinetoHorizontalRel", "SVGPathSegLinetoRel", "SVGPathSegLinetoVerticalAbs", "SVGPathSegLinetoVerticalRel", "SVGPathSegList", "SVGPathSegMovetoAbs", "SVGPathSegMovetoRel", "SVGPatternElement", "SVGPoint", "SVGPointList", "SVGPolygonElement", "SVGPolylineElement", "SVGPreserveAspectRatio", "SVGRadialGradientElement", "SVGRect", "SVGRectElement", "SVGRenderingIntent", "SVGSVGElement", "SVGScriptElement", "SVGSetElement", "SVGStopElement", "SVGStringList", "SVGStyleElement", "SVGSwitchElement", "SVGSymbolElement", "SVGTRefElement", "SVGTSpanElement", "SVGTextContentElement", "SVGTextElement", "SVGTextPathElement", "SVGTextPositioningElement", "SVGTitleElement", "SVGTransform", "SVGTransformList", "SVGUnitTypes", "SVGUseElement", "SVGVKernElement", "SVGViewElement", "SVGViewSpec", "SVGZoomAndPan", "SVGZoomEvent", "SVG_ANGLETYPE_DEG", "SVG_ANGLETYPE_GRAD", "SVG_ANGLETYPE_RAD", "SVG_ANGLETYPE_UNKNOWN", "SVG_ANGLETYPE_UNSPECIFIED", "SVG_CHANNEL_A", "SVG_CHANNEL_B", "SVG_CHANNEL_G", "SVG_CHANNEL_R", "SVG_CHANNEL_UNKNOWN", "SVG_COLORTYPE_CURRENTCOLOR", "SVG_COLORTYPE_RGBCOLOR", "SVG_COLORTYPE_RGBCOLOR_ICCCOLOR", "SVG_COLORTYPE_UNKNOWN", "SVG_EDGEMODE_DUPLICATE", "SVG_EDGEMODE_NONE", "SVG_EDGEMODE_UNKNOWN", "SVG_EDGEMODE_WRAP", "SVG_FEBLEND_MODE_COLOR", "SVG_FEBLEND_MODE_COLOR_BURN", "SVG_FEBLEND_MODE_COLOR_DODGE", "SVG_FEBLEND_MODE_DARKEN", "SVG_FEBLEND_MODE_DIFFERENCE", "SVG_FEBLEND_MODE_EXCLUSION", "SVG_FEBLEND_MODE_HARD_LIGHT", "SVG_FEBLEND_MODE_HUE", "SVG_FEBLEND_MODE_LIGHTEN", "SVG_FEBLEND_MODE_LUMINOSITY", "SVG_FEBLEND_MODE_MULTIPLY", "SVG_FEBLEND_MODE_NORMAL", "SVG_FEBLEND_MODE_OVERLAY", "SVG_FEBLEND_MODE_SATURATION", "SVG_FEBLEND_MODE_SCREEN", "SVG_FEBLEND_MODE_SOFT_LIGHT", "SVG_FEBLEND_MODE_UNKNOWN", "SVG_FECOLORMATRIX_TYPE_HUEROTATE", "SVG_FECOLORMATRIX_TYPE_LUMINANCETOALPHA", "SVG_FECOLORMATRIX_TYPE_MATRIX", "SVG_FECOLORMATRIX_TYPE_SATURATE", "SVG_FECOLORMATRIX_TYPE_UNKNOWN", "SVG_FECOMPONENTTRANSFER_TYPE_DISCRETE", "SVG_FECOMPONENTTRANSFER_TYPE_GAMMA", "SVG_FECOMPONENTTRANSFER_TYPE_IDENTITY", "SVG_FECOMPONENTTRANSFER_TYPE_LINEAR", "SVG_FECOMPONENTTRANSFER_TYPE_TABLE", "SVG_FECOMPONENTTRANSFER_TYPE_UNKNOWN", "SVG_FECOMPOSITE_OPERATOR_ARITHMETIC", "SVG_FECOMPOSITE_OPERATOR_ATOP", "SVG_FECOMPOSITE_OPERATOR_IN", "SVG_FECOMPOSITE_OPERATOR_OUT", "SVG_FECOMPOSITE_OPERATOR_OVER", "SVG_FECOMPOSITE_OPERATOR_UNKNOWN", "SVG_FECOMPOSITE_OPERATOR_XOR", "SVG_INVALID_VALUE_ERR", "SVG_LENGTHTYPE_CM", "SVG_LENGTHTYPE_EMS", "SVG_LENGTHTYPE_EXS", "SVG_LENGTHTYPE_IN", "SVG_LENGTHTYPE_MM", "SVG_LENGTHTYPE_NUMBER", "SVG_LENGTHTYPE_PC", "SVG_LENGTHTYPE_PERCENTAGE", "SVG_LENGTHTYPE_PT", "SVG_LENGTHTYPE_PX", "SVG_LENGTHTYPE_UNKNOWN", "SVG_MARKERUNITS_STROKEWIDTH", "SVG_MARKERUNITS_UNKNOWN", "SVG_MARKERUNITS_USERSPACEONUSE", "SVG_MARKER_ORIENT_ANGLE", "SVG_MARKER_ORIENT_AUTO", "SVG_MARKER_ORIENT_UNKNOWN", "SVG_MASKTYPE_ALPHA", "SVG_MASKTYPE_LUMINANCE", "SVG_MATRIX_NOT_INVERTABLE", "SVG_MEETORSLICE_MEET", "SVG_MEETORSLICE_SLICE", "SVG_MEETORSLICE_UNKNOWN", "SVG_MORPHOLOGY_OPERATOR_DILATE", "SVG_MORPHOLOGY_OPERATOR_ERODE", "SVG_MORPHOLOGY_OPERATOR_UNKNOWN", "SVG_PAINTTYPE_CURRENTCOLOR", "SVG_PAINTTYPE_NONE", "SVG_PAINTTYPE_RGBCOLOR", "SVG_PAINTTYPE_RGBCOLOR_ICCCOLOR", "SVG_PAINTTYPE_UNKNOWN", "SVG_PAINTTYPE_URI", "SVG_PAINTTYPE_URI_CURRENTCOLOR", "SVG_PAINTTYPE_URI_NONE", "SVG_PAINTTYPE_URI_RGBCOLOR", "SVG_PAINTTYPE_URI_RGBCOLOR_ICCCOLOR", "SVG_PRESERVEASPECTRATIO_NONE", "SVG_PRESERVEASPECTRATIO_UNKNOWN", "SVG_PRESERVEASPECTRATIO_XMAXYMAX", "SVG_PRESERVEASPECTRATIO_XMAXYMID", "SVG_PRESERVEASPECTRATIO_XMAXYMIN", "SVG_PRESERVEASPECTRATIO_XMIDYMAX", "SVG_PRESERVEASPECTRATIO_XMIDYMID", "SVG_PRESERVEASPECTRATIO_XMIDYMIN", "SVG_PRESERVEASPECTRATIO_XMINYMAX", "SVG_PRESERVEASPECTRATIO_XMINYMID", "SVG_PRESERVEASPECTRATIO_XMINYMIN", "SVG_SPREADMETHOD_PAD", "SVG_SPREADMETHOD_REFLECT", "SVG_SPREADMETHOD_REPEAT", "SVG_SPREADMETHOD_UNKNOWN", "SVG_STITCHTYPE_NOSTITCH", "SVG_STITCHTYPE_STITCH", "SVG_STITCHTYPE_UNKNOWN", "SVG_TRANSFORM_MATRIX", "SVG_TRANSFORM_ROTATE", "SVG_TRANSFORM_SCALE", "SVG_TRANSFORM_SKEWX", "SVG_TRANSFORM_SKEWY", "SVG_TRANSFORM_TRANSLATE", "SVG_TRANSFORM_UNKNOWN", "SVG_TURBULENCE_TYPE_FRACTALNOISE", "SVG_TURBULENCE_TYPE_TURBULENCE", "SVG_TURBULENCE_TYPE_UNKNOWN", "SVG_UNIT_TYPE_OBJECTBOUNDINGBOX", "SVG_UNIT_TYPE_UNKNOWN", "SVG_UNIT_TYPE_USERSPACEONUSE", "SVG_WRONG_TYPE_ERR", "SVG_ZOOMANDPAN_DISABLE", "SVG_ZOOMANDPAN_MAGNIFY", "SVG_ZOOMANDPAN_UNKNOWN", "SYNTAX_ERR", "SavedPages", "Screen", "ScreenOrientation", "Script", "ScriptEngine", "ScriptEngineBuildVersion", "ScriptEngineMajorVersion", "ScriptEngineMinorVersion", "ScriptProcessorNode", "ScrollAreaEvent", "SecurityPolicyViolationEvent", "Selection", "Sensor", "SensorErrorEvent", "ServiceWorker", "ServiceWorkerContainer", "ServiceWorkerRegistration", "SessionDescription", "Set", "ShadowRoot", "SharedArrayBuffer", "SharedWorker", "SimpleGestureEvent", "SourceBuffer", "SourceBufferList", "SpeechSynthesis", "SpeechSynthesisErrorEvent", "SpeechSynthesisEvent", "SpeechSynthesisUtterance", "SpeechSynthesisVoice", "StaticRange", "StereoPannerNode", "StopIteration", "Storage", "StorageEvent", "StorageManager", "String", "StyleMedia", "StylePropertyMap", "StylePropertyMapReadOnly", "StyleSheet", "StyleSheetList", "StyleSheetPageList", "SubtleCrypto", "Symbol", "SyncManager", "SyntaxError", "TEMPORARY", "TEXTPATH_METHODTYPE_ALIGN", "TEXTPATH_METHODTYPE_STRETCH", "TEXTPATH_METHODTYPE_UNKNOWN", "TEXTPATH_SPACINGTYPE_AUTO", "TEXTPATH_SPACINGTYPE_EXACT", "TEXTPATH_SPACINGTYPE_UNKNOWN", "TEXTURE", "TEXTURE0", "TEXTURE1", "TEXTURE10", "TEXTURE11", "TEXTURE12", "TEXTURE13", "TEXTURE14", "TEXTURE15", "TEXTURE16", "TEXTURE17", "TEXTURE18", "TEXTURE19", "TEXTURE2", "TEXTURE20", "TEXTURE21", "TEXTURE22", "TEXTURE23", "TEXTURE24", "TEXTURE25", "TEXTURE26", "TEXTURE27", "TEXTURE28", "TEXTURE29", "TEXTURE3", "TEXTURE30", "TEXTURE31", "TEXTURE4", "TEXTURE5", "TEXTURE6", "TEXTURE7", "TEXTURE8", "TEXTURE9", "TEXTURE_2D", "TEXTURE_BINDING_2D", "TEXTURE_BINDING_CUBE_MAP", "TEXTURE_CUBE_MAP", "TEXTURE_CUBE_MAP_NEGATIVE_X", "TEXTURE_CUBE_MAP_NEGATIVE_Y", "TEXTURE_CUBE_MAP_NEGATIVE_Z", "TEXTURE_CUBE_MAP_POSITIVE_X", "TEXTURE_CUBE_MAP_POSITIVE_Y", "TEXTURE_CUBE_MAP_POSITIVE_Z", "TEXTURE_MAG_FILTER", "TEXTURE_MAX_ANISOTROPY_EXT", "TEXTURE_MIN_FILTER", "TEXTURE_WRAP_S", "TEXTURE_WRAP_T", "TEXT_NODE", "TIMEOUT", "TIMEOUT_ERR", "TOO_LARGE_ERR", "TRANSACTION_INACTIVE_ERR", "TRIANGLE", "TRIANGLES", "TRIANGLE_FAN", "TRIANGLE_STRIP", "TYPE_BACK_FORWARD", "TYPE_ERR", "TYPE_MISMATCH_ERR", "TYPE_NAVIGATE", "TYPE_RELOAD", "TYPE_RESERVED", "Table", "TaskAttributionTiming", "Text", "TextDecoder", "TextDecoderStream", "TextEncoder", "TextEncoderStream", "TextEvent", "TextMetrics", "TextRange", "TextRangeCollection", "TextTrack", "TextTrackCue", "TextTrackCueList", "TextTrackList", "TimeEvent", "TimeRanges", "Touch", "TouchEvent", "TouchList", "TrackEvent", "TransformStream", "TransitionEvent", "TreeWalker", "TypeError", "UIEvent", "UNCACHED", "UNKNOWN_ERR", "UNKNOWN_RULE", "UNMASKED_RENDERER_WEBGL", "UNMASKED_VENDOR_WEBGL", "UNORDERED_NODE_ITERATOR_TYPE", "UNORDERED_NODE_SNAPSHOT_TYPE", "UNPACK_ALIGNMENT", "UNPACK_COLORSPACE_CONVERSION_WEBGL", "UNPACK_FLIP_Y_WEBGL", "UNPACK_PREMULTIPLY_ALPHA_WEBGL", "UNSCHEDULED_STATE", "UNSENT", "UNSIGNED_BYTE", "UNSIGNED_INT", "UNSIGNED_SHORT", "UNSIGNED_SHORT_4_4_4_4", "UNSIGNED_SHORT_5_5_5_1", "UNSIGNED_SHORT_5_6_5", "UNSPECIFIED_EVENT_TYPE_ERR", "UPDATEREADY", "URIError", "URL", "URLSearchParams", "URLUnencoded", "URL_MISMATCH_ERR", "USB", "USBAlternateInterface", "USBConfiguration", "USBConnectionEvent", "USBDevice", "USBEndpoint", "USBInTransferResult", "USBInterface", "USBIsochronousInTransferPacket", "USBIsochronousInTransferResult", "USBIsochronousOutTransferPacket", "USBIsochronousOutTransferResult", "USBOutTransferResult", "UTC", "Uint16Array", "Uint32Array", "Uint8Array", "Uint8ClampedArray", "UserActivation", "UserMessageHandler", "UserMessageHandlersNamespace", "UserProximityEvent", "VALIDATE_STATUS", "VALIDATION_ERR", "VARIABLES_RULE", "VBArray", "VENDOR", "VERSION", "VERSION_CHANGE", "VERSION_ERR", "VERTEX_ATTRIB_ARRAY_BUFFER_BINDING", "VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE", "VERTEX_ATTRIB_ARRAY_ENABLED", "VERTEX_ATTRIB_ARRAY_NORMALIZED", "VERTEX_ATTRIB_ARRAY_POINTER", "VERTEX_ATTRIB_ARRAY_SIZE", "VERTEX_ATTRIB_ARRAY_STRIDE", "VERTEX_ATTRIB_ARRAY_TYPE", "VERTEX_SHADER", "VERTICAL", "VERTICAL_AXIS", "VER_ERR", "VIEWPORT", "VIEWPORT_RULE", "VRDisplay", "VRDisplayCapabilities", "VRDisplayEvent", "VREyeParameters", "VRFieldOfView", "VRFrameData", "VRPose", "VRStageParameters", "VTTCue", "VTTRegion", "ValidityState", "VideoPlaybackQuality", "VideoStreamTrack", "VisualViewport", "WEBGL_compressed_texture_s3tc", "WEBGL_debug_renderer_info", "WEBKIT_FILTER_RULE", "WEBKIT_KEYFRAMES_RULE", "WEBKIT_KEYFRAME_RULE", "WEBKIT_REGION_RULE", "WRONG_DOCUMENT_ERR", "WaveShaperNode", "WeakMap", "WeakSet", "WebAssembly", "WebGL2RenderingContext", "WebGLActiveInfo", "WebGLBuffer", "WebGLContextEvent", "WebGLFramebuffer", "WebGLObject", "WebGLProgram", "WebGLQuery", "WebGLRenderbuffer", "WebGLRenderingContext", "WebGLSampler", "WebGLShader", "WebGLShaderPrecisionFormat", "WebGLSync", "WebGLTexture", "WebGLTransformFeedback", "WebGLUniformLocation", "WebGLVertexArray", "WebGLVertexArrayObject", "WebKitAnimationEvent", "WebKitBlobBuilder", "WebKitCSSFilterRule", "WebKitCSSFilterValue", "WebKitCSSKeyframeRule", "WebKitCSSKeyframesRule", "WebKitCSSMatrix", "WebKitCSSRegionRule", "WebKitCSSTransformValue", "WebKitDataCue", "WebKitGamepad", "WebKitMediaKeyError", "WebKitMediaKeyMessageEvent", "WebKitMediaKeySession", "WebKitMediaKeys", "WebKitMediaSource", "WebKitMutationObserver", "WebKitNamespace", "WebKitPlaybackTargetAvailabilityEvent", "WebKitPoint", "WebKitShadowRoot", "WebKitSourceBuffer", "WebKitSourceBufferList", "WebKitTransitionEvent", "WebSocket", "WebkitAlignContent", "WebkitAlignItems", "WebkitAlignSelf", "WebkitAnimation", "WebkitAnimationDelay", "WebkitAnimationDirection", "WebkitAnimationDuration", "WebkitAnimationFillMode", "WebkitAnimationIterationCount", "WebkitAnimationName", "WebkitAnimationPlayState", "WebkitAnimationTimingFunction", "WebkitAppearance", "WebkitBackfaceVisibility", "WebkitBackgroundClip", "WebkitBackgroundOrigin", "WebkitBackgroundSize", "WebkitBorderBottomLeftRadius", "WebkitBorderBottomRightRadius", "WebkitBorderImage", "WebkitBorderRadius", "WebkitBorderTopLeftRadius", "WebkitBorderTopRightRadius", "WebkitBoxAlign", "WebkitBoxDirection", "WebkitBoxFlex", "WebkitBoxOrdinalGroup", "WebkitBoxOrient", "WebkitBoxPack", "WebkitBoxShadow", "WebkitBoxSizing", "WebkitFilter", "WebkitFlex", "WebkitFlexBasis", "WebkitFlexDirection", "WebkitFlexFlow", "WebkitFlexGrow", "WebkitFlexShrink", "WebkitFlexWrap", "WebkitJustifyContent", "WebkitMask", "WebkitMaskClip", "WebkitMaskComposite", "WebkitMaskImage", "WebkitMaskOrigin", "WebkitMaskPosition", "WebkitMaskPositionX", "WebkitMaskPositionY", "WebkitMaskRepeat", "WebkitMaskSize", "WebkitOrder", "WebkitPerspective", "WebkitPerspectiveOrigin", "WebkitTextFillColor", "WebkitTextSizeAdjust", "WebkitTextStroke", "WebkitTextStrokeColor", "WebkitTextStrokeWidth", "WebkitTransform", "WebkitTransformOrigin", "WebkitTransformStyle", "WebkitTransition", "WebkitTransitionDelay", "WebkitTransitionDuration", "WebkitTransitionProperty", "WebkitTransitionTimingFunction", "WebkitUserSelect", "WheelEvent", "Window", "Worker", "Worklet", "WritableStream", "XMLDocument", "XMLHttpRequest", "XMLHttpRequestEventTarget", "XMLHttpRequestException", "XMLHttpRequestProgressEvent", "XMLHttpRequestUpload", "XMLSerializer", "XMLStylesheetProcessingInstruction", "XPathEvaluator", "XPathException", "XPathExpression", "XPathNSResolver", "XPathResult", "XSLTProcessor", "ZERO", "_XD0M_", "_YD0M_", "__defineGetter__", "__defineSetter__", "__lookupGetter__", "__lookupSetter__", "__opera", "__proto__", "_browserjsran", "a", "aLink", "abbr", "abort", "aborted", "abs", "absolute", "acceleration", "accelerationIncludingGravity", "accelerator", "accept", "acceptCharset", "acceptNode", "accessKey", "accessKeyLabel", "accuracy", "acos", "acosh", "action", "actionURL", "activated", "active", "activeCues", "activeElement", "activeSourceBuffers", "activeSourceCount", "activeTexture", "activeVRDisplays", "add", "addBehavior", "addCandidate", "addColorStop", "addCue", "addElement", "addEventListener", "addFilter", "addFromString", "addFromUri", "addIceCandidate", "addImport", "addListener", "addModule", "addNamed", "addPageRule", "addPath", "addPointer", "addRange", "addRegion", "addRule", "addSearchEngine", "addSourceBuffer", "addStream", "addTextTrack", "addTrack", "addTransceiver", "addWakeLockListener", "addedNodes", "additionalName", "additiveSymbols", "addons", "adoptNode", "adoptedCallback", "adoptedStyleSheets", "adr", "advance", "after", "album", "alert", "algorithm", "align", "align-content", "align-items", "align-self", "alignContent", "alignItems", "alignSelf", "alignmentBaseline", "alinkColor", "all", "allow", "allowFullscreen", "allowPaymentRequest", "allowedDirections", "alpha", "alt", "altGraphKey", "altHtml", "altKey", "altLeft", "altitude", "altitudeAccuracy", "amplitude", "ancestorOrigins", "anchor", "anchorNode", "anchorOffset", "anchors", "and", "angle", "angularAcceleration", "angularVelocity", "animVal", "animate", "animatedInstanceRoot", "animatedNormalizedPathSegList", "animatedPathSegList", "animatedPoints", "animation", "animation-delay", "animation-direction", "animation-duration", "animation-fill-mode", "animation-iteration-count", "animation-name", "animation-play-state", "animation-timing-function", "animationDelay", "animationDirection", "animationDuration", "animationFillMode", "animationIterationCount", "animationName", "animationPlayState", "animationStartTime", "animationTimingFunction", "animationsPaused", "anniversary", "app", "appCodeName", "appMinorVersion", "appName", "appNotifications", "appVersion", "append", "appendBuffer", "appendChild", "appendData", "appendItem", "appendMedium", "appendNamed", "appendRule", "appendStream", "appendWindowEnd", "appendWindowStart", "applets", "application/pdf", "application/x-google-chrome-pdf", "application/x-nacl", "application/x-pnacl", "applicationCache", "apply", "applyElement", "arc", "arcTo", "archive", "areas", "arguments", "arrayBuffer", "artist", "artwork", "as", "asin", "asinh", "assert", "assign", "assignedSlot", "async", "atEnd", "atan", "atan2", "atanh", "atob", "attachEvent", "attachShader", "attachShadow", "attachments", "attack", "attrChange", "attrName", "attributeChangedCallback", "attributeFilter", "attributeName", "attributeNamespace", "attributeOldValue", "attributeStyleMap", "attributes", "audioTracks", "audioWorklet", "autoIncrement", "autobuffer", "autocapitalize", "autocomplete", "autocorrect", "autofocus", "automationRate", "autoplay", "availHeight", "availLeft", "availTop", "availWidth", "availability", "available", "aversion", "axes", "axis", "azimuth", "b", "back", "backface-visibility", "backfaceVisibility", "background", "background-attachment", "background-blend-mode", "background-clip", "background-color", "background-image", "background-origin", "background-position", "background-position-x", "background-position-y", "background-repeat", "background-size", "backgroundAttachment", "backgroundBlendMode", "backgroundClip", "backgroundColor", "backgroundImage", "backgroundOrigin", "backgroundPosition", "backgroundPositionX", "backgroundPositionY", "backgroundRepeat", "backgroundRepeatX", "backgroundRepeatY", "backgroundSize", "badInput", "balance", "baseFrequencyX", "baseFrequencyY", "baseLatency", "baseNode", "baseOffset", "baseURI", "baseVal", "baselineShift", "battery", "bday", "before", "beginElement", "beginElementAt", "beginPath", "behavior", "behaviorCookie", "behaviorPart", "behaviorUrns", "beta", "bezierCurveTo", "bgColor", "bgProperties", "bias", "big", "binaryType", "bind", "bindAttribLocation", "bindBuffer", "bindFramebuffer", "bindRenderbuffer", "bindTexture", "blendColor", "blendEquation", "blendEquationSeparate", "blendFunc", "blendFuncSeparate", "blink", "blob", "block-size", "blockDirection", "blockSize", "blue", "bluetooth", "blur", "body", "bodyUsed", "bold", "bookmarks", "booleanValue", "border", "border-block", "border-block-color", "border-block-end", "border-block-end-color", "border-block-end-style", "border-block-end-width", "border-block-start", "border-block-start-color", "border-block-start-style", "border-block-start-width", "border-block-style", "border-block-width", "border-bottom", "border-bottom-color", "border-bottom-left-radius", "border-bottom-right-radius", "border-bottom-style", "border-bottom-width", "border-collapse", "border-color", "border-end-end-radius", "border-end-start-radius", "border-image", "border-image-outset", "border-image-repeat", "border-image-slice", "border-image-source", "border-image-width", "border-inline", "border-inline-color", "border-inline-end", "border-inline-end-color", "border-inline-end-style", "border-inline-end-width", "border-inline-start", "border-inline-start-color", "border-inline-start-style", "border-inline-start-width", "border-inline-style", "border-inline-width", "border-left", "border-left-color", "border-left-style", "border-left-width", "border-radius", "border-right", "border-right-color", "border-right-style", "border-right-width", "border-spacing", "border-start-end-radius", "border-start-start-radius", "border-style", "border-top", "border-top-color", "border-top-left-radius", "border-top-right-radius", "border-top-style", "border-top-width", "border-width", "borderBlock", "borderBlockColor", "borderBlockEnd", "borderBlockEndColor", "borderBlockEndStyle", "borderBlockEndWidth", "borderBlockStart", "borderBlockStartColor", "borderBlockStartStyle", "borderBlockStartWidth", "borderBlockStyle", "borderBlockWidth", "borderBottom", "borderBottomColor", "borderBottomLeftRadius", "borderBottomRightRadius", "borderBottomStyle", "borderBottomWidth", "borderCollapse", "borderColor", "borderColorDark", "borderColorLight", "borderEndEndRadius", "borderEndStartRadius", "borderImage", "borderImageOutset", "borderImageRepeat", "borderImageSlice", "borderImageSource", "borderImageWidth", "borderInline", "borderInlineColor", "borderInlineEnd", "borderInlineEndColor", "borderInlineEndStyle", "borderInlineEndWidth", "borderInlineStart", "borderInlineStartColor", "borderInlineStartStyle", "borderInlineStartWidth", "borderInlineStyle", "borderInlineWidth", "borderLeft", "borderLeftColor", "borderLeftStyle", "borderLeftWidth", "borderRadius", "borderRight", "borderRightColor", "borderRightStyle", "borderRightWidth", "borderSpacing", "borderStartEndRadius", "borderStartStartRadius", "borderStyle", "borderTop", "borderTopColor", "borderTopLeftRadius", "borderTopRightRadius", "borderTopStyle", "borderTopWidth", "borderWidth", "bottom", "bottomMargin", "bound", "boundElements", "boundingClientRect", "boundingHeight", "boundingLeft", "boundingTop", "boundingWidth", "bounds", "box-decoration-break", "box-shadow", "box-sizing", "boxDecorationBreak", "boxShadow", "boxSizing", "break-after", "break-before", "break-inside", "breakAfter", "breakBefore", "breakInside", "browserLanguage", "btoa", "bubbles", "buffer", "bufferData", "bufferDepth", "bufferSize", "bufferSubData", "buffered", "bufferedAmount", "bufferedRendering", "buildID", "buildNumber", "button", "buttonID", "buttons", "byteLength", "byteOffset", "c", "caches", "call", "caller", "canBeFormatted", "canBeMounted", "canBeShared", "canHaveChildren", "canHaveHTML", "canPlayType", "canTrickleIceCandidates", "cancel", "cancelAndHoldAtTime", "cancelAnimationFrame", "cancelBubble", "cancelIdleCallback", "cancelScheduledValues", "cancelWatchAvailability", "cancelable", "candidate", "canvas", "caption", "caption-side", "captionSide", "capture", "captureEvents", "captureStackTrace", "captureStream", "caret-color", "caretColor", "caretPositionFromPoint", "caretRangeFromPoint", "cast", "catch", "category", "cbrt", "cd", "ceil", "cellIndex", "cellPadding", "cellSpacing", "cells", "ch", "chOff", "chain", "challenge", "changedTouches", "channel", "channelCount", "channelCountMode", "channelInterpretation", "char", "charAt", "charCode", "charCodeAt", "charIndex", "characterData", "characterDataOldValue", "characterSet", "charging", "chargingTime", "charset", "check", "checkEnclosure", "checkFramebufferStatus", "checkIntersection", "checkValidity", "checked", "childElementCount", "childList", "childNodes", "children", "chrome", "ciphertext", "cite", "classList", "className", "classid", "clear", "clearAttributes", "clearColor", "clearData", "clearDepth", "clearImmediate", "clearInterval", "clearLiveSeekableRange", "clearMarks", "clearMeasures", "clearParameters", "clearRect", "clearResourceTimings", "clearShadow", "clearStencil", "clearTimeout", "clearWatch", "click", "clickCount", "clientHeight", "clientInformation", "clientLeft", "clientRect", "clientRects", "clientTop", "clientWidth", "clientX", "clientY", "clip", "clip-path", "clip-rule", "clipBottom", "clipLeft", "clipPath", "clipPathUnits", "clipRight", "clipRule", "clipTop", "clipboard", "clipboardData", "clone", "cloneContents", "cloneNode", "cloneRange", "close", "closePath", "closed", "closest", "clz", "clz32", "cmp", "code", "codeBase", "codePointAt", "codeType", "colSpan", "collapse", "collapseToEnd", "collapseToStart", "collapsed", "collect", "colno", "color", "color-adjust", "color-interpolation", "color-interpolation-filters", "colorAdjust", "colorDepth", "colorInterpolation", "colorInterpolationFilters", "colorMask", "colorRendering", "colorType", "cols", "column-count", "column-fill", "column-gap", "column-rule", "column-rule-color", "column-rule-style", "column-rule-width", "column-width", "columnCount", "columnFill", "columnGap", "columnNumber", "columnRule", "columnRuleColor", "columnRuleStyle", "columnRuleWidth", "columnSpan", "columnWidth", "columns", "command", "commitPreferences", "commonAncestorContainer", "compact", "compare", "compareBoundaryPoints", "compareDocumentPosition", "compareEndPoints", "compareExchange", "compareNode", "comparePoint", "compatMode", "compatible", "compile", "compileShader", "compileStreaming", "complete", "componentFromPoint", "composed", "composedPath", "compositionEndOffset", "compositionStartOffset", "compressedTexImage2D", "compressedTexSubImage2D", "computedStyleMap", "concat", "conditionText", "coneInnerAngle", "coneOuterAngle", "coneOuterGain", "confirm", "confirmComposition", "confirmSiteSpecificTrackingException", "confirmWebWideTrackingException", "connect", "connectEnd", "connectStart", "connected", "connectedCallback", "connection", "connectionSpeed", "connectionState", "console", "consolidate", "constrictionActive", "construct", "constructor", "contactID", "contain", "contains", "containsNode", "content", "contentDocument", "contentEditable", "contentOverflow", "contentScriptType", "contentStyleType", "contentType", "contentWindow", "context", "contextMenu", "contextmenu", "continue", "continuous", "control", "controller", "controls", "controlsList", "convertToSpecifiedUnits", "cookie", "cookieEnabled", "coords", "copyFromChannel", "copyTexImage2D", "copyTexSubImage2D", "copyToChannel", "copyWithin", "correspondingElement", "correspondingUseElement", "cos", "cosh", "count", "countReset", "counter-increment", "counter-reset", "counterIncrement", "counterReset", "cpuClass", "cpuSleepAllowed", "create", "createAnalyser", "createAnswer", "createAttribute", "createAttributeNS", "createBiquadFilter", "createBuffer", "createBufferSource", "createCDATASection", "createCSSStyleSheet", "createCaption", "createChannelMerger", "createChannelSplitter", "createComment", "createConstantSource", "createContextualFragment", "createControlRange", "createConvolver", "createDTMFSender", "createDataChannel", "createDelay", "createDelayNode", "createDocument", "createDocumentFragment", "createDocumentType", "createDynamicsCompressor", "createElement", "createElementNS", "createEntityReference", "createEvent", "createEventObject", "createExpression", "createFramebuffer", "createFunction", "createGain", "createGainNode", "createHTMLDocument", "createIIRFilter", "createImageBitmap", "createImageData", "createIndex", "createJavaScriptNode", "createLinearGradient", "createMediaElementSource", "createMediaKeys", "createMediaStreamDestination", "createMediaStreamSource", "createMutableFile", "createNSResolver", "createNodeIterator", "createNotification", "createObjectStore", "createObjectURL", "createOffer", "createOscillator", "createPanner", "createPattern", "createPeriodicWave", "createPopup", "createProcessingInstruction", "createProgram", "createRadialGradient", "createRange", "createRangeCollection", "createRenderbuffer", "createSVGAngle", "createSVGLength", "createSVGMatrix", "createSVGNumber", "createSVGPathSegArcAbs", "createSVGPathSegArcRel", "createSVGPathSegClosePath", "createSVGPathSegCurvetoCubicAbs", "createSVGPathSegCurvetoCubicRel", "createSVGPathSegCurvetoCubicSmoothAbs", "createSVGPathSegCurvetoCubicSmoothRel", "createSVGPathSegCurvetoQuadraticAbs", "createSVGPathSegCurvetoQuadraticRel", "createSVGPathSegCurvetoQuadraticSmoothAbs", "createSVGPathSegCurvetoQuadraticSmoothRel", "createSVGPathSegLinetoAbs", "createSVGPathSegLinetoHorizontalAbs", "createSVGPathSegLinetoHorizontalRel", "createSVGPathSegLinetoRel", "createSVGPathSegLinetoVerticalAbs", "createSVGPathSegLinetoVerticalRel", "createSVGPathSegMovetoAbs", "createSVGPathSegMovetoRel", "createSVGPoint", "createSVGRect", "createSVGTransform", "createSVGTransformFromMatrix", "createScriptProcessor", "createSession", "createShader", "createShadowRoot", "createStereoPanner", "createStyleSheet", "createTBody", "createTFoot", "createTHead", "createTextNode", "createTextRange", "createTexture", "createTouch", "createTouchList", "createTreeWalker", "createWaveShaper", "creationTime", "credentials", "crossOrigin", "crypto", "csi", "csp", "cssFloat", "cssRules", "cssText", "cssValueType", "ctrlKey", "ctrlLeft", "cues", "cullFace", "currentLocalDescription", "currentNode", "currentPage", "currentRemoteDescription", "currentScale", "currentScript", "currentSrc", "currentState", "currentStyle", "currentTarget", "currentTime", "currentTranslate", "currentView", "cursor", "curve", "customElements", "customError", "cx", "cy", "d", "data", "dataFld", "dataFormatAs", "dataPageSize", "dataSrc", "dataTransfer", "database", "databases", "dataset", "dateTime", "db", "debug", "debuggerEnabled", "declare", "decode", "decodeAudioData", "decodeURI", "decodeURIComponent", "decoding", "decodingInfo", "decrypt", "default", "defaultCharset", "defaultChecked", "defaultMuted", "defaultPlaybackRate", "defaultPrevented", "defaultRequest", "defaultSelected", "defaultStatus", "defaultURL", "defaultValue", "defaultView", "defaultstatus", "defer", "define", "defineMagicFunction", "defineMagicVariable", "defineProperties", "defineProperty", "delayTime", "delegatesFocus", "delete", "deleteBuffer", "deleteCaption", "deleteCell", "deleteContents", "deleteData", "deleteDatabase", "deleteFramebuffer", "deleteFromDocument", "deleteIndex", "deleteMedium", "deleteObjectStore", "deleteProgram", "deleteProperty", "deleteRenderbuffer", "deleteRow", "deleteRule", "deleteShader", "deleteTFoot", "deleteTHead", "deleteTexture", "deliverChangeRecords", "delivery", "deliveryInfo", "deliveryStatus", "deliveryTimestamp", "delta", "deltaMode", "deltaX", "deltaY", "deltaZ", "depthFunc", "depthMask", "depthRange", "deriveBits", "deriveKey", "description", "deselectAll", "designMode", "destination", "destinationURL", "detach", "detachEvent", "detachShader", "detail", "detune", "deviceMemory", "devicePixelRatio", "deviceSessionId", "deviceXDPI", "deviceYDPI", "diffuseConstant", "digest", "dimensions", "dir", "dirName", "direction", "dirxml", "disable", "disablePictureInPicture", "disableRemotePlayback", "disableVertexAttribArray", "disabled", "dischargingTime", "disconnect", "disconnectedCallback", "dispatchEvent", "display", "distanceModel", "divisor", "djsapi", "djsproxy", "doImport", "doNotTrack", "doScroll", "doctype", "document", "documentElement", "documentMode", "documentURI", "dolphin", "dolphinGameCenter", "dolphininfo", "dolphinmeta", "domComplete", "domContentLoadedEventEnd", "domContentLoadedEventStart", "domInteractive", "domLoading", "domain", "domainLookupEnd", "domainLookupStart", "dominant-baseline", "dominantBaseline", "done", "dopplerFactor", "dotAll", "downlink", "download", "dragDrop", "draggable", "drawArrays", "drawArraysInstancedANGLE", "drawCustomFocusRing", "drawElements", "drawElementsInstancedANGLE", "drawFocusIfNeeded", "drawImage", "drawImageFromRect", "drawSystemFocusRing", "drawingBufferHeight", "drawingBufferWidth", "dropEffect", "droppedVideoFrames", "dropzone", "dump", "duplicate", "duration", "dvname", "dvnum", "dx", "dy", "dynsrc", "e", "edgeMode", "effect", "effectAllowed", "effectiveType", "elapsedTime", "elementFromPoint", "elements", "elementsFromPoint", "elevation", "ellipse", "email", "embeds", "empty", "empty-cells", "emptyCells", "enable", "enableBackground", "enableStyleSheetsForSet", "enableVertexAttribArray", "enabled", "enabledPlugin", "encode", "encodeInto", "encodeURI", "encodeURIComponent", "encoding", "encodingInfo", "encrypt", "enctype", "end", "endContainer", "endElement", "endElementAt", "endOfStream", "endOffset", "endTime", "ended", "endsWith", "entities", "entries", "entryType", "enumerate", "enumerateDevices", "enumerateEditable", "epubCaptionSide", "epubTextCombine", "epubTextEmphasis", "epubTextEmphasisColor", "epubTextEmphasisStyle", "epubTextOrientation", "epubTextTransform", "epubWordBreak", "epubWritingMode", "error", "errorCode", "escape", "estimate", "eval", "evaluate", "event", "eventPhase", "every", "exception", "exchange", "exec", "execCommand", "execCommandShowHelp", "execScript", "exitFullscreen", "exitPictureInPicture", "exitPointerLock", "exp", "expand", "expandEntityReferences", "expando", "expansion", "expiryDate", "explicitOriginalTarget", "expm1", "exponent", "exponentialRampToValueAtTime", "exportKey", "extend", "extensions", "extentNode", "extentOffset", "external", "externalResourcesRequired", "extractContents", "extractable", "f", "face", "factoryReset", "fallback", "familyName", "farthestViewportElement", "fastSeek", "fatal", "fetch", "fetchStart", "fftSize", "fgColor", "fileCreatedDate", "fileHandle", "fileModifiedDate", "fileName", "fileSize", "fileUpdatedDate", "filename", "files", "fill", "fill-opacity", "fill-rule", "fillOpacity", "fillRect", "fillRule", "fillStyle", "fillText", "filter", "filterResX", "filterResY", "filterUnits", "filters", "finally", "find", "findIndex", "findRule", "findText", "finish", "finished", "fireEvent", "firesTouchEvents", "firstChild", "firstElementChild", "firstPage", "fixed", "flags", "flat", "flatMap", "flex", "flex-basis", "flex-direction", "flex-flow", "flex-grow", "flex-shrink", "flex-wrap", "flexBasis", "flexDirection", "flexFlow", "flexGrow", "flexShrink", "flexWrap", "flipX", "flipY", "float", "flood-color", "flood-opacity", "floodColor", "floodOpacity", "floor", "flush", "focus", "focusNode", "focusOffset", "font", "font-family", "font-feature-settings", "font-kerning", "font-language-override", "font-size", "font-size-adjust", "font-stretch", "font-style", "font-synthesis", "font-variant", "font-variant-alternates", "font-variant-caps", "font-variant-east-asian", "font-variant-ligatures", "font-variant-numeric", "font-variant-position", "font-weight", "fontDisplay", "fontFamily", "fontFeatureSettings", "fontKerning", "fontLanguageOverride", "fontSize", "fontSizeAdjust", "fontSmoothingEnabled", "fontStretch", "fontStyle", "fontSynthesis", "fontVariant", "fontVariantAlternates", "fontVariantCaps", "fontVariantEastAsian", "fontVariantLigatures", "fontVariantNumeric", "fontVariantPosition", "fontVariationSettings", "fontWeight", "fontcolor", "fonts", "fontsize", "for", "forEach", "forceRedraw", "form", "formAction", "formData", "formEnctype", "formMethod", "formNoValidate", "formTarget", "format", "formatToParts", "forms", "forward", "forwardX", "forwardY", "forwardZ", "fr", "frame", "frameBorder", "frameElement", "frameSpacing", "framebufferRenderbuffer", "framebufferTexture2D", "frames", "freeSpace", "freeze", "frequency", "frequencyBinCount", "from", "fromCharCode", "fromCodePoint", "fromElement", "frontFace", "fround", "fullScreen", "fullscreen", "fullscreenElement", "fullscreenEnabled", "fx", "fy", "gain", "gamepad", "gamma", "gap", "genderIdentity", "generateKey", "generateMipmap", "generateRequest", "geolocation", "gestureObject", "get", "getActiveAttrib", "getActiveUniform", "getAdjacentText", "getAll", "getAllResponseHeaders", "getAsFile", "getAsString", "getAttachedShaders", "getAttribLocation", "getAttribute", "getAttributeNS", "getAttributeNames", "getAttributeNode", "getAttributeNodeNS", "getAudioTracks", "getBBox", "getBattery", "getBlob", "getBookmark", "getBoundingClientRect", "getBounds", "getBufferParameter", "getByteFrequencyData", "getByteTimeDomainData", "getCSSCanvasContext", "getCTM", "getCandidateWindowClientRect", "getCanonicalLocales", "getChannelData", "getCharNumAtPosition", "getClientRect", "getClientRects", "getCompositionAlternatives", "getComputedStyle", "getComputedTextLength", "getConfiguration", "getContext", "getContextAttributes", "getCounterValue", "getCueAsHTML", "getCueById", "getCurrentPosition", "getCurrentTime", "getData", "getDatabaseNames", "getDate", "getDay", "getDefaultComputedStyle", "getDestinationInsertionPoints", "getDetails", "getDevices", "getDisplayMedia", "getDistributedNodes", "getEditable", "getElementById", "getElementsByClassName", "getElementsByName", "getElementsByTagName", "getElementsByTagNameNS", "getEnclosureList", "getEndPositionOfChar", "getEntries", "getEntriesByName", "getEntriesByType", "getError", "getExtension", "getExtentOfChar", "getFeature", "getFile", "getFloat32", "getFloat64", "getFloatFrequencyData", "getFloatTimeDomainData", "getFloatValue", "getFramebufferAttachmentParameter", "getFrequencyResponse", "getFullYear", "getGamepads", "getHours", "getIdentityAssertion", "getImageData", "getInt16", "getInt32", "getInt8", "getIntersectionList", "getIsInstalled", "getItem", "getItems", "getKey", "getLayoutMap", "getLineDash", "getLocalStreams", "getMarks", "getMatchedCSSRules", "getMeasures", "getMetadata", "getMilliseconds", "getMinutes", "getModifierState", "getMonth", "getNamedItem", "getNamedItemNS", "getNotifier", "getNumberOfChars", "getOutputTimestamp", "getOverrideHistoryNavigationMode", "getOverrideStyle", "getOwnPropertyDescriptor", "getOwnPropertyNames", "getOwnPropertySymbols", "getParameter", "getPathSegAtLength", "getPointAtLength", "getPreference", "getPreferenceDefault", "getPresentationAttribute", "getPreventDefault", "getProgramInfoLog", "getProgramParameter", "getPropertyCSSValue", "getPropertyPriority", "getPropertyShorthand", "getPropertyValue", "getPrototypeOf", "getRGBColorValue", "getRandomValues", "getRangeAt", "getReader", "getReceivers", "getRectValue", "getRegistration", "getRegistrations", "getRemoteStreams", "getRenderbufferParameter", "getResponseHeader", "getRoot", "getRootNode", "getRotationOfChar", "getSVGDocument", "getScreenCTM", "getSeconds", "getSelection", "getSenders", "getShaderInfoLog", "getShaderParameter", "getShaderPrecisionFormat", "getShaderSource", "getSimpleDuration", "getSiteIcons", "getSources", "getSpeculativeParserUrls", "getStartPositionOfChar", "getStartTime", "getStats", "getStorageUpdates", "getStreamById", "getStringValue", "getSubStringLength", "getSubscription", "getSupportedConstraints", "getSupportedExtensions", "getTexParameter", "getTime", "getTimezoneOffset", "getTotalLength", "getTrackById", "getTracks", "getTransceivers", "getTransformToElement", "getUTCDate", "getUTCDay", "getUTCFullYear", "getUTCHours", "getUTCMilliseconds", "getUTCMinutes", "getUTCMonth", "getUTCSeconds", "getUint16", "getUint32", "getUint8", "getUniform", "getUniformLocation", "getUserMedia", "getVRDisplays", "getValues", "getVarDate", "getVariableValue", "getVertexAttrib", "getVertexAttribOffset", "getVideoPlaybackQuality", "getVideoTracks", "getVoices", "getWakeLockState", "getWriter", "getYear", "givenName", "global", "globalAlpha", "globalCompositeOperation", "globalThis", "glyphOrientationHorizontal", "glyphOrientationVertical", "glyphRef", "go", "gradientTransform", "gradientUnits", "grammars", "green", "grid", "grid-area", "grid-auto-columns", "grid-auto-flow", "grid-auto-rows", "grid-column", "grid-column-end", "grid-column-gap", "grid-column-start", "grid-gap", "grid-row", "grid-row-end", "grid-row-gap", "grid-row-start", "grid-template", "grid-template-areas", "grid-template-columns", "grid-template-rows", "gridArea", "gridAutoColumns", "gridAutoFlow", "gridAutoRows", "gridColumn", "gridColumnEnd", "gridColumnGap", "gridColumnStart", "gridGap", "gridRow", "gridRowEnd", "gridRowGap", "gridRowStart", "gridTemplate", "gridTemplateAreas", "gridTemplateColumns", "gridTemplateRows", "group", "groupCollapsed", "groupEnd", "hardwareConcurrency", "has", "hasAttribute", "hasAttributeNS", "hasAttributes", "hasBeenActive", "hasChildNodes", "hasComposition", "hasExtension", "hasFeature", "hasFocus", "hasLayout", "hasOwnProperty", "hasPointerCapture", "hasReading", "hasStorageAccess", "hash", "head", "headers", "heading", "height", "hidden", "hide", "hideFocus", "high", "hint", "history", "honorificPrefix", "honorificSuffix", "horizontalOverflow", "host", "hostname", "href", "hreflang", "hspace", "html5TagCheckInerface", "htmlFor", "htmlText", "httpEquiv", "hwTimestamp", "hyphens", "hypot", "iccId", "iceConnectionState", "iceGatheringState", "icon", "id", "identifier", "identity", "idpLoginUrl", "ignoreBOM", "ignoreCase", "image-orientation", "image-rendering", "imageOrientation", "imageRendering", "imageSizes", "imageSrcset", "images", "ime-mode", "imeMode", "implementation", "import", "importKey", "importNode", "importStylesheet", "imports", "impp", "imul", "in1", "in2", "inBandMetadataTrackDispatchType", "inRange", "includes", "incremental", "indeterminate", "index", "indexNames", "indexOf", "indexedDB", "inertiaDestinationX", "inertiaDestinationY", "info", "init", "initAnimationEvent", "initBeforeLoadEvent", "initClipboardEvent", "initCloseEvent", "initCommandEvent", "initCompositionEvent", "initCustomEvent", "initData", "initDeviceMotionEvent", "initDeviceOrientationEvent", "initDragEvent", "initErrorEvent", "initEvent", "initFocusEvent", "initGestureEvent", "initHashChangeEvent", "initKeyEvent", "initKeyboardEvent", "initMSManipulationEvent", "initMessageEvent", "initMouseEvent", "initMouseScrollEvent", "initMouseWheelEvent", "initMutationEvent", "initNSMouseEvent", "initOverflowEvent", "initPageEvent", "initPageTransitionEvent", "initPointerEvent", "initPopStateEvent", "initProgressEvent", "initScrollAreaEvent", "initSimpleGestureEvent", "initStorageEvent", "initTextEvent", "initTimeEvent", "initTouchEvent", "initTransitionEvent", "initUIEvent", "initWebKitAnimationEvent", "initWebKitTransitionEvent", "initWebKitWheelEvent", "initWheelEvent", "initialTime", "initialize", "initiatorType", "inline-size", "inlineSize", "inner", "innerHTML", "innerHeight", "innerText", "innerWidth", "input", "inputBuffer", "inputEncoding", "inputMethod", "inputMode", "insertAdjacentElement", "insertAdjacentHTML", "insertAdjacentText", "insertBefore", "insertCell", "insertData", "insertItemBefore", "insertNode", "insertRow", "insertRule", "inset", "inset-block", "inset-block-end", "inset-block-start", "inset-inline", "inset-inline-end", "inset-inline-start", "insetBlock", "insetBlockEnd", "insetBlockStart", "insetInline", "insetInlineEnd", "insetInlineStart", "install", "installChrome", "installState", "instanceRoot", "instantiate", "instantiateStreaming", "integrity", "intercept", "interimResults", "internalSubset", "intersectsNode", "interval", "invalidIteratorState", "inverse", "invertSelf", "is", "is2D", "isActive", "isAlternate", "isArray", "isBingCurrentSearchDefault", "isBuffer", "isCandidateWindowVisible", "isChar", "isCollapsed", "isComposing", "isConnected", "isContentEditable", "isContentHandlerRegistered", "isContextLost", "isDefaultNamespace", "isDisabled", "isEnabled", "isEqual", "isEqualNode", "isExtensible", "isFinite", "isFramebuffer", "isFrozen", "isGenerator", "isId", "isIdentity", "isInjected", "isInstalled", "isInteger", "isLockFree", "isMap", "isMultiLine", "isNaN", "isOpen", "isPointInFill", "isPointInPath", "isPointInRange", "isPointInStroke", "isPrefAlternate", "isPrimary", "isProgram", "isPropertyImplicit", "isProtocolHandlerRegistered", "isPrototypeOf", "isRenderbuffer", "isSafeInteger", "isSameNode", "isSealed", "isSecureContext", "isShader", "isSupported", "isTextEdit", "isTexture", "isTrusted", "isTypeSupported", "isView", "isolation", "italics", "item", "itemId", "itemProp", "itemRef", "itemScope", "itemType", "itemValue", "items", "iterateNext", "iterator", "javaEnabled", "jobTitle", "join", "jsHeapSizeLimit", "json", "justify-content", "justify-items", "justify-self", "justifyContent", "justifyItems", "justifySelf", "k1", "k2", "k3", "k4", "kernelMatrix", "kernelUnitLengthX", "kernelUnitLengthY", "kerning", "key", "keyCode", "keyFor", "keyIdentifier", "keyLightEnabled", "keyLocation", "keyPath", "keySystem", "keyText", "keyUsage", "keyboard", "keys", "keytype", "kind", "knee", "label", "labels", "lang", "language", "languages", "largeArcFlag", "lastChild", "lastElementChild", "lastEventId", "lastIndex", "lastIndexOf", "lastMatch", "lastMessageSubject", "lastMessageType", "lastModified", "lastModifiedDate", "lastPage", "lastParen", "lastState", "lastStyleSheetSet", "latitude", "layerX", "layerY", "layoutFlow", "layoutGrid", "layoutGridChar", "layoutGridLine", "layoutGridMode", "layoutGridType", "lbound", "left", "leftContext", "leftMargin", "leftProjectionMatrix", "leftViewMatrix", "length", "lengthAdjust", "lengthComputable", "letter-spacing", "letterSpacing", "level", "lighting-color", "lightingColor", "limitingConeAngle", "line", "line-height", "lineAlign", "lineBreak", "lineCap", "lineDashOffset", "lineHeight", "lineJoin", "lineNumber", "lineTo", "lineWidth", "linearAcceleration", "linearRampToValueAtTime", "linearVelocity", "lineno", "lines", "link", "linkColor", "linkProgram", "links", "list", "list-style", "list-style-image", "list-style-position", "list-style-type", "listStyle", "listStyleImage", "listStylePosition", "listStyleType", "listener", "load", "loadEventEnd", "loadEventStart", "loadTimes", "loaded", "localDescription", "localName", "localStorage", "locale", "localeCompare", "location", "locationbar", "lock", "locked", "lockedFile", "locks", "log", "log10", "log1p", "log2", "logicalXDPI", "logicalYDPI", "longDesc", "longitude", "lookupNamespaceURI", "lookupPrefix", "loop", "loopEnd", "loopStart", "looping", "low", "lower", "lowerBound", "lowerOpen", "lowsrc", "m11", "m12", "m13", "m14", "m21", "m22", "m23", "m24", "m31", "m32", "m33", "m34", "m41", "m42", "m43", "m44", "manifest", "map", "mapping", "margin", "margin-block", "margin-block-end", "margin-block-start", "margin-bottom", "margin-inline", "margin-inline-end", "margin-inline-start", "margin-left", "margin-right", "margin-top", "marginBlock", "marginBlockEnd", "marginBlockStart", "marginBottom", "marginHeight", "marginInline", "marginInlineEnd", "marginInlineStart", "marginLeft", "marginRight", "marginTop", "marginWidth", "mark", "marker", "marker-end", "marker-mid", "marker-offset", "marker-start", "markerEnd", "markerHeight", "markerMid", "markerOffset", "markerStart", "markerUnits", "markerWidth", "marks", "mask", "mask-clip", "mask-composite", "mask-image", "mask-mode", "mask-origin", "mask-position", "mask-position-x", "mask-position-y", "mask-repeat", "mask-size", "mask-type", "maskClip", "maskComposite", "maskContentUnits", "maskImage", "maskMode", "maskOrigin", "maskPosition", "maskPositionX", "maskPositionY", "maskRepeat", "maskSize", "maskType", "maskUnits", "match", "matchAll", "matchMedia", "matchMedium", "matches", "matrix", "matrixTransform", "max", "max-block-size", "max-height", "max-inline-size", "max-width", "maxAlternatives", "maxBlockSize", "maxChannelCount", "maxConnectionsPerServer", "maxDecibels", "maxDistance", "maxHeight", "maxInlineSize", "maxLength", "maxTouchPoints", "maxValue", "maxWidth", "maxZoom", "measure", "measureText", "media", "mediaCapabilities", "mediaDevices", "mediaElement", "mediaGroup", "mediaKeys", "mediaSession", "mediaText", "meetOrSlice", "memory", "menubar", "mergeAttributes", "message", "messageClass", "messageHandlers", "metaKey", "metadata", "method", "mimeType", "mimeTypes", "min", "min-block-size", "min-height", "min-inline-size", "min-width", "minBlockSize", "minDecibels", "minHeight", "minInlineSize", "minLength", "minValue", "minWidth", "minZoom", "miterLimit", "mix-blend-mode", "mixBlendMode", "mode", "modify", "mount", "move", "moveBy", "moveEnd", "moveFirst", "moveFocusDown", "moveFocusLeft", "moveFocusRight", "moveFocusUp", "moveNext", "moveRow", "moveStart", "moveTo", "moveToBookmark", "moveToElementText", "moveToPoint", "movementX", "movementY", "mozAdd", "mozAnimationStartTime", "mozAnon", "mozApps", "mozAudioCaptured", "mozAudioChannelType", "mozAutoplayEnabled", "mozCancelAnimationFrame", "mozCancelFullScreen", "mozCancelRequestAnimationFrame", "mozCaptureStream", "mozCaptureStreamUntilEnded", "mozClearDataAt", "mozContact", "mozContacts", "mozCreateFileHandle", "mozCurrentTransform", "mozCurrentTransformInverse", "mozCursor", "mozDash", "mozDashOffset", "mozDecodedFrames", "mozExitPointerLock", "mozFillRule", "mozFragmentEnd", "mozFrameDelay", "mozFullScreen", "mozFullScreenElement", "mozFullScreenEnabled", "mozGetAll", "mozGetAllKeys", "mozGetAsFile", "mozGetDataAt", "mozGetMetadata", "mozGetUserMedia", "mozHasAudio", "mozHasItem", "mozHidden", "mozImageSmoothingEnabled", "mozIndexedDB", "mozInnerScreenX", "mozInnerScreenY", "mozInputSource", "mozIsTextField", "mozItem", "mozItemCount", "mozItems", "mozLength", "mozLockOrientation", "mozMatchesSelector", "mozMovementX", "mozMovementY", "mozOpaque", "mozOrientation", "mozPaintCount", "mozPaintedFrames", "mozParsedFrames", "mozPay", "mozPointerLockElement", "mozPresentedFrames", "mozPreservesPitch", "mozPressure", "mozPrintCallback", "mozRTCIceCandidate", "mozRTCPeerConnection", "mozRTCSessionDescription", "mozRemove", "mozRequestAnimationFrame", "mozRequestFullScreen", "mozRequestPointerLock", "mozSetDataAt", "mozSetImageElement", "mozSourceNode", "mozSrcObject", "mozSystem", "mozTCPSocket", "mozTextStyle", "mozTypesAt", "mozUnlockOrientation", "mozUserCancelled", "mozVisibilityState", "msAnimation", "msAnimationDelay", "msAnimationDirection", "msAnimationDuration", "msAnimationFillMode", "msAnimationIterationCount", "msAnimationName", "msAnimationPlayState", "msAnimationStartTime", "msAnimationTimingFunction", "msBackfaceVisibility", "msBlockProgression", "msCSSOMElementFloatMetrics", "msCaching", "msCachingEnabled", "msCancelRequestAnimationFrame", "msCapsLockWarningOff", "msClearImmediate", "msClose", "msContentZoomChaining", "msContentZoomFactor", "msContentZoomLimit", "msContentZoomLimitMax", "msContentZoomLimitMin", "msContentZoomSnap", "msContentZoomSnapPoints", "msContentZoomSnapType", "msContentZooming", "msConvertURL", "msCrypto", "msDoNotTrack", "msElementsFromPoint", "msElementsFromRect", "msExitFullscreen", "msExtendedCode", "msFillRule", "msFirstPaint", "msFlex", "msFlexAlign", "msFlexDirection", "msFlexFlow", "msFlexItemAlign", "msFlexLinePack", "msFlexNegative", "msFlexOrder", "msFlexPack", "msFlexPositive", "msFlexPreferredSize", "msFlexWrap", "msFlowFrom", "msFlowInto", "msFontFeatureSettings", "msFullscreenElement", "msFullscreenEnabled", "msGetInputContext", "msGetRegionContent", "msGetUntransformedBounds", "msGraphicsTrustStatus", "msGridColumn", "msGridColumnAlign", "msGridColumnSpan", "msGridColumns", "msGridRow", "msGridRowAlign", "msGridRowSpan", "msGridRows", "msHidden", "msHighContrastAdjust", "msHyphenateLimitChars", "msHyphenateLimitLines", "msHyphenateLimitZone", "msHyphens", "msImageSmoothingEnabled", "msImeAlign", "msIndexedDB", "msInterpolationMode", "msIsStaticHTML", "msKeySystem", "msKeys", "msLaunchUri", "msLockOrientation", "msManipulationViewsEnabled", "msMatchMedia", "msMatchesSelector", "msMaxTouchPoints", "msOrientation", "msOverflowStyle", "msPerspective", "msPerspectiveOrigin", "msPlayToDisabled", "msPlayToPreferredSourceUri", "msPlayToPrimary", "msPointerEnabled", "msRegionOverflow", "msReleasePointerCapture", "msRequestAnimationFrame", "msRequestFullscreen", "msSaveBlob", "msSaveOrOpenBlob", "msScrollChaining", "msScrollLimit", "msScrollLimitXMax", "msScrollLimitXMin", "msScrollLimitYMax", "msScrollLimitYMin", "msScrollRails", "msScrollSnapPointsX", "msScrollSnapPointsY", "msScrollSnapType", "msScrollSnapX", "msScrollSnapY", "msScrollTranslation", "msSetImmediate", "msSetMediaKeys", "msSetPointerCapture", "msTextCombineHorizontal", "msTextSizeAdjust", "msToBlob", "msTouchAction", "msTouchSelect", "msTraceAsyncCallbackCompleted", "msTraceAsyncCallbackStarting", "msTraceAsyncOperationCompleted", "msTraceAsyncOperationStarting", "msTransform", "msTransformOrigin", "msTransformStyle", "msTransition", "msTransitionDelay", "msTransitionDuration", "msTransitionProperty", "msTransitionTimingFunction", "msUnlockOrientation", "msUpdateAsyncCallbackRelation", "msUserSelect", "msVisibilityState", "msWrapFlow", "msWrapMargin", "msWrapThrough", "msWriteProfilerMark", "msZoom", "msZoomTo", "mt", "multiEntry", "multiSelectionObj", "multiline", "multiple", "multiply", "multiplySelf", "mutableFile", "muted", "n", "name", "nameProp", "namedItem", "namedRecordset", "names", "namespaceURI", "namespaces", "naturalHeight", "naturalWidth", "navigate", "navigation", "navigationMode", "navigationStart", "navigator", "near", "nearestViewportElement", "negative", "netscape", "networkState", "newScale", "newTranslate", "newURL", "newValue", "newValueSpecifiedUnits", "newVersion", "newhome", "next", "nextElementSibling", "nextNode", "nextPage", "nextSibling", "nickname", "noHref", "noModule", "noResize", "noShade", "noValidate", "noWrap", "nodeName", "nodeType", "nodeValue", "nonce", "normalize", "normalizedPathSegList", "notationName", "notations", "note", "noteGrainOn", "noteOff", "noteOn", "notify", "now", "numOctaves", "number", "numberOfChannels", "numberOfInputs", "numberOfItems", "numberOfOutputs", "numberValue", "oMatchesSelector", "object", "object-fit", "object-position", "objectFit", "objectPosition", "objectStore", "objectStoreNames", "observe", "observedAttributes", "of", "offscreenBuffering", "offset", "offsetDistance", "offsetHeight", "offsetLeft", "offsetNode", "offsetParent", "offsetPath", "offsetRotate", "offsetTop", "offsetWidth", "offsetX", "offsetY", "ok", "oldURL", "oldValue", "oldVersion", "olderShadowRoot", "onLine", "onabort", "onabsolutedeviceorientation", "onactivate", "onactive", "onaddsourcebuffer", "onaddstream", "onaddtrack", "onafterprint", "onafterscriptexecute", "onafterupdate", "onanimationcancel", "onanimationend", "onanimationiteration", "onanimationstart", "onappinstalled", "onaudioend", "onaudioprocess", "onaudiostart", "onautocomplete", "onautocompleteerror", "onauxclick", "onbeforeactivate", "onbeforecopy", "onbeforecut", "onbeforedeactivate", "onbeforeeditfocus", "onbeforeinstallprompt", "onbeforepaste", "onbeforeprint", "onbeforescriptexecute", "onbeforeunload", "onbeforeupdate", "onblocked", "onblur", "onbounce", "onboundary", "oncached", "oncancel", "oncandidatewindowhide", "oncandidatewindowshow", "oncandidatewindowupdate", "oncanplay", "oncanplaythrough", "once", "oncellchange", "onchange", "onchargingchange", "onchargingtimechange", "onchecking", "onclick", "onclose", "oncompassneedscalibration", "oncomplete", "onconnect", "onconnecting", "onconnectionstatechange", "oncontextmenu", "oncontrollerchange", "oncontrolselect", "oncopy", "oncuechange", "oncut", "ondataavailable", "ondatachannel", "ondatasetchanged", "ondatasetcomplete", "ondblclick", "ondeactivate", "ondevicechange", "ondevicelight", "ondevicemotion", "ondeviceorientation", "ondeviceorientationabsolute", "ondeviceproximity", "ondischargingtimechange", "ondisconnect", "ondisplay", "ondownloading", "ondrag", "ondragend", "ondragenter", "ondragexit", "ondragleave", "ondragover", "ondragstart", "ondrop", "ondurationchange", "onemptied", "onencrypted", "onend", "onended", "onenter", "onenterpictureinpicture", "onerror", "onerrorupdate", "onexit", "onfilterchange", "onfinish", "onfocus", "onfocusin", "onfocusout", "onfreeze", "onfullscreenchange", "onfullscreenerror", "ongesturechange", "ongestureend", "ongesturestart", "ongotpointercapture", "onhashchange", "onhelp", "onicecandidate", "oniceconnectionstatechange", "onicegatheringstatechange", "oninactive", "oninput", "oninvalid", "onkeydown", "onkeypress", "onkeyup", "onlanguagechange", "onlayoutcomplete", "onleavepictureinpicture", "onlevelchange", "onload", "onloadeddata", "onloadedmetadata", "onloadend", "onloading", "onloadingdone", "onloadingerror", "onloadstart", "onlosecapture", "onlostpointercapture", "only", "onmark", "onmessage", "onmessageerror", "onmousedown", "onmouseenter", "onmouseleave", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onmousewheel", "onmove", "onmoveend", "onmovestart", "onmozfullscreenchange", "onmozfullscreenerror", "onmozorientationchange", "onmozpointerlockchange", "onmozpointerlockerror", "onmscontentzoom", "onmsfullscreenchange", "onmsfullscreenerror", "onmsgesturechange", "onmsgesturedoubletap", "onmsgestureend", "onmsgesturehold", "onmsgesturestart", "onmsgesturetap", "onmsgotpointercapture", "onmsinertiastart", "onmslostpointercapture", "onmsmanipulationstatechanged", "onmsneedkey", "onmsorientationchange", "onmspointercancel", "onmspointerdown", "onmspointerenter", "onmspointerhover", "onmspointerleave", "onmspointermove", "onmspointerout", "onmspointerover", "onmspointerup", "onmssitemodejumplistitemremoved", "onmsthumbnailclick", "onnegotiationneeded", "onnomatch", "onnoupdate", "onobsolete", "onoffline", "ononline", "onopen", "onorientationchange", "onpagechange", "onpagehide", "onpageshow", "onpaste", "onpause", "onplay", "onplaying", "onpluginstreamstart", "onpointercancel", "onpointerdown", "onpointerenter", "onpointerleave", "onpointerlockchange", "onpointerlockerror", "onpointermove", "onpointerout", "onpointerover", "onpointerup", "onpopstate", "onprogress", "onpropertychange", "onratechange", "onreading", "onreadystatechange", "onrejectionhandled", "onremovesourcebuffer", "onremovestream", "onremovetrack", "onreset", "onresize", "onresizeend", "onresizestart", "onresourcetimingbufferfull", "onresult", "onresume", "onrowenter", "onrowexit", "onrowsdelete", "onrowsinserted", "onscroll", "onsearch", "onseeked", "onseeking", "onselect", "onselectionchange", "onselectstart", "onshow", "onsignalingstatechange", "onsoundend", "onsoundstart", "onsourceclose", "onsourceclosed", "onsourceended", "onsourceopen", "onspeechend", "onspeechstart", "onstalled", "onstart", "onstatechange", "onstop", "onstorage", "onstoragecommit", "onsubmit", "onsuccess", "onsuspend", "ontextinput", "ontimeout", "ontimeupdate", "ontoggle", "ontouchcancel", "ontouchend", "ontouchmove", "ontouchstart", "ontrack", "ontransitioncancel", "ontransitionend", "ontransitionrun", "ontransitionstart", "onunhandledrejection", "onunload", "onupdateready", "onupgradeneeded", "onuserproximity", "onversionchange", "onvisibilitychange", "onvoiceschanged", "onvolumechange", "onvrdisplayactivate", "onvrdisplayconnect", "onvrdisplaydeactivate", "onvrdisplaydisconnect", "onvrdisplaypresentchange", "onwaiting", "onwaitingforkey", "onwarning", "onwebkitanimationend", "onwebkitanimationiteration", "onwebkitanimationstart", "onwebkitcurrentplaybacktargetiswirelesschanged", "onwebkitfullscreenchange", "onwebkitfullscreenerror", "onwebkitkeyadded", "onwebkitkeyerror", "onwebkitkeymessage", "onwebkitneedkey", "onwebkitorientationchange", "onwebkitplaybacktargetavailabilitychanged", "onwebkitpointerlockchange", "onwebkitpointerlockerror", "onwebkitresourcetimingbufferfull", "onwebkittransitionend", "onwheel", "onzoom", "opacity", "open", "openCursor", "openDatabase", "openKeyCursor", "opener", "opera", "operationType", "operator", "opr", "optimum", "options", "or", "order", "orderX", "orderY", "ordered", "org", "orient", "orientAngle", "orientType", "orientation", "orientationX", "orientationY", "orientationZ", "origin", "originalTarget", "orphans", "oscpu", "outerHTML", "outerHeight", "outerText", "outerWidth", "outline", "outline-color", "outline-offset", "outline-style", "outline-width", "outlineColor", "outlineOffset", "outlineStyle", "outlineWidth", "outputBuffer", "overflow", "overflow-anchor", "overflow-wrap", "overflow-x", "overflow-y", "overflowAnchor", "overflowWrap", "overflowX", "overflowY", "overrideMimeType", "oversample", "overscroll-behavior", "overscroll-behavior-x", "overscroll-behavior-y", "overscrollBehavior", "overscrollBehaviorX", "overscrollBehaviorY", "ownKeys", "ownerDocument", "ownerElement", "ownerNode", "ownerRule", "ownerSVGElement", "owningElement", "p1", "p2", "p3", "p4", "pad", "padEnd", "padStart", "padding", "padding-block", "padding-block-end", "padding-block-start", "padding-bottom", "padding-inline", "padding-inline-end", "padding-inline-start", "padding-left", "padding-right", "padding-top", "paddingBlock", "paddingBlockEnd", "paddingBlockStart", "paddingBottom", "paddingInline", "paddingInlineEnd", "paddingInlineStart", "paddingLeft", "paddingRight", "paddingTop", "page", "page-break-after", "page-break-before", "page-break-inside", "pageBreakAfter", "pageBreakBefore", "pageBreakInside", "pageCount", "pageLeft", "pageTop", "pageX", "pageXOffset", "pageY", "pageYOffset", "pages", "paint-order", "paintOrder", "paintRequests", "paintType", "palette", "pan", "panningModel", "parent", "parentElement", "parentNode", "parentRule", "parentStyleSheet", "parentTextEdit", "parentWindow", "parse", "parseFloat", "parseFromString", "parseInt", "part", "participants", "passive", "password", "pasteHTML", "path", "pathLength", "pathSegList", "pathSegType", "pathSegTypeAsLetter", "pathname", "pattern", "patternContentUnits", "patternMismatch", "patternTransform", "patternUnits", "pause", "pauseAnimations", "pauseOnExit", "paused", "peerIdentity", "pending", "pendingLocalDescription", "pendingRemoteDescription", "performance", "permission", "permissions", "persist", "persisted", "personalbar", "perspective", "perspective-origin", "perspectiveOrigin", "phoneticFamilyName", "phoneticGivenName", "photo", "pictureInPictureElement", "pictureInPictureEnabled", "ping", "pipeThrough", "pipeTo", "pitch", "pixelBottom", "pixelDepth", "pixelHeight", "pixelLeft", "pixelRight", "pixelStorei", "pixelTop", "pixelUnitToMillimeterX", "pixelUnitToMillimeterY", "pixelWidth", "place-content", "place-items", "place-self", "placeContent", "placeItems", "placeSelf", "placeholder", "platform", "play", "playState", "playbackRate", "playbackState", "playbackTime", "played", "plugins", "pluginspage", "pname", "pointer-events", "pointerBeforeReferenceNode", "pointerEnabled", "pointerEvents", "pointerId", "pointerLockElement", "pointerType", "points", "pointsAtX", "pointsAtY", "pointsAtZ", "polygonOffset", "pop", "populateMatrix", "popupWindowFeatures", "popupWindowName", "popupWindowURI", "port", "port1", "port2", "ports", "posBottom", "posHeight", "posLeft", "posRight", "posTop", "posWidth", "pose", "position", "positionAlign", "positionX", "positionY", "positionZ", "postError", "postMessage", "poster", "pow", "powerOff", "preMultiplySelf", "precision", "preferredStyleSheetSet", "preferredStylesheetSet", "prefix", "preload", "prepend", "presentation", "preserveAlpha", "preserveAspectRatio", "preserveAspectRatioString", "pressed", "pressure", "prevValue", "preventDefault", "preventExtensions", "preventSilentAccess", "previousElementSibling", "previousNode", "previousPage", "previousScale", "previousSibling", "previousTranslate", "primaryKey", "primitiveType", "primitiveUnits", "principals", "print", "privateKey", "probablySupportsContext", "process", "processIceMessage", "product", "productSub", "profile", "profileEnd", "profiles", "prompt", "properties", "propertyIsEnumerable", "propertyName", "protocol", "protocolLong", "prototype", "pseudoClass", "pseudoElement", "publicId", "publicKey", "published", "push", "pushNotification", "pushState", "put", "putImageData", "quadraticCurveTo", "qualifier", "quaternion", "query", "queryCommandEnabled", "queryCommandIndeterm", "queryCommandState", "queryCommandSupported", "queryCommandText", "queryCommandValue", "querySelector", "querySelectorAll", "queryUsageAndQuota", "queueMicrotask", "quote", "quotes", "r", "r1", "r2", "race", "radiogroup", "radiusX", "radiusY", "random", "range", "rangeCount", "rangeMax", "rangeMin", "rangeOffset", "rangeOverflow", "rangeParent", "rangeUnderflow", "rate", "ratio", "raw", "read", "readAsArrayBuffer", "readAsBinaryString", "readAsBlob", "readAsDataURL", "readAsText", "readOnly", "readPixels", "readReportRequested", "readText", "readable", "ready", "readyState", "reason", "reboot", "receiver", "receivers", "recordNumber", "recordset", "rect", "red", "redirectCount", "redirectEnd", "redirectStart", "redirected", "reduce", "reduceRight", "reduction", "refDistance", "refX", "refY", "referenceNode", "referrer", "referrerPolicy", "refresh", "region", "regionAnchorX", "regionAnchorY", "regionId", "regions", "register", "registerContentHandler", "registerElement", "registerProtocolHandler", "reject", "rel", "relList", "relatedNode", "relatedTarget", "release", "releaseCapture", "releaseEvents", "releasePointerCapture", "releaseShaderCompiler", "reliable", "reload", "remainingSpace", "remote", "remoteDescription", "remove", "removeAllRanges", "removeAttribute", "removeAttributeNS", "removeAttributeNode", "removeBehavior", "removeChild", "removeCue", "removeEventListener", "removeFilter", "removeImport", "removeItem", "removeListener", "removeNamedItem", "removeNamedItemNS", "removeNode", "removeParameter", "removeProperty", "removeRange", "removeRegion", "removeRule", "removeSiteSpecificTrackingException", "removeSourceBuffer", "removeStream", "removeTrack", "removeVariable", "removeWakeLockListener", "removeWebWideTrackingException", "removedNodes", "renderbufferStorage", "renderedBuffer", "renderingMode", "repeat", "replace", "replaceAdjacentText", "replaceChild", "replaceData", "replaceId", "replaceItem", "replaceNode", "replaceState", "replaceSync", "replaceTrack", "replaceWholeText", "replaceWith", "reportValidity", "request", "requestAnimationFrame", "requestAutocomplete", "requestData", "requestDevice", "requestFullscreen", "requestIdleCallback", "requestMIDIAccess", "requestMediaKeySystemAccess", "requestPermission", "requestPictureInPicture", "requestPointerLock", "requestQuota", "requestStart", "requestStorageAccess", "requestingWindow", "required", "requiredExtensions", "requiredFeatures", "reset", "resetTransform", "resize", "resizeBy", "resizeTo", "resolve", "resolvedOptions", "response", "responseBody", "responseEnd", "responseStart", "responseText", "responseType", "responseURL", "responseXML", "restore", "result", "resultType", "resume", "returnValue", "rev", "reverse", "reversed", "revocable", "revokeObjectURL", "rgbColor", "right", "rightContext", "rightMargin", "rightProjectionMatrix", "rightViewMatrix", "rolloffFactor", "root", "rootElement", "rotate", "rotateAxisAngle", "rotateAxisAngleSelf", "rotateFromVector", "rotateFromVectorSelf", "rotateSelf", "rotation", "rotationRate", "round", "row-gap", "rowGap", "rowIndex", "rowSpan", "rows", "rtt", "ruby-align", "ruby-position", "rubyAlign", "rubyOverhang", "rubyPosition", "rules", "runningState", "runtime", "runtimeStyle", "rx", "ry", "safari", "sampleCoverage", "sampleRate", "sandbox", "save", "saveData", "scale", "scale3d", "scale3dSelf", "scaleNonUniform", "scaleNonUniformSelf", "scaleSelf", "scheme", "scissor", "scope", "scopeName", "scoped", "screen", "screenBrightness", "screenEnabled", "screenLeft", "screenPixelToMillimeterX", "screenPixelToMillimeterY", "screenTop", "screenX", "screenY", "scripts", "scroll", "scroll-behavior", "scroll-snap-coordinate", "scroll-snap-destination", "scroll-snap-points-x", "scroll-snap-points-y", "scroll-snap-type", "scroll-snap-type-x", "scroll-snap-type-y", "scrollAmount", "scrollBehavior", "scrollBy", "scrollByLines", "scrollByPages", "scrollDelay", "scrollHeight", "scrollIntoView", "scrollIntoViewIfNeeded", "scrollLeft", "scrollLeftMax", "scrollMargin", "scrollMarginBlock", "scrollMarginBlockEnd", "scrollMarginBlockStart", "scrollMarginBottom", "scrollMarginInline", "scrollMarginInlineEnd", "scrollMarginInlineStart", "scrollMarginLeft", "scrollMarginRight", "scrollMarginTop", "scrollMaxX", "scrollMaxY", "scrollPadding", "scrollPaddingBlock", "scrollPaddingBlockEnd", "scrollPaddingBlockStart", "scrollPaddingBottom", "scrollPaddingInline", "scrollPaddingInlineEnd", "scrollPaddingInlineStart", "scrollPaddingLeft", "scrollPaddingRight", "scrollPaddingTop", "scrollRestoration", "scrollSnapAlign", "scrollSnapCoordinate", "scrollSnapDestination", "scrollSnapPointsX", "scrollSnapPointsY", "scrollSnapStop", "scrollSnapType", "scrollSnapTypeX", "scrollSnapTypeY", "scrollTo", "scrollTop", "scrollTopMax", "scrollWidth", "scrollX", "scrollY", "scrollbar-color", "scrollbar-width", "scrollbar3dLightColor", "scrollbarArrowColor", "scrollbarBaseColor", "scrollbarColor", "scrollbarDarkShadowColor", "scrollbarFaceColor", "scrollbarHighlightColor", "scrollbarShadowColor", "scrollbarTrackColor", "scrollbarWidth", "scrollbars", "scrolling", "scrollingElement", "sdp", "sdpMLineIndex", "sdpMid", "seal", "search", "searchBox", "searchBoxJavaBridge_", "searchParams", "sectionRowIndex", "secureConnectionStart", "security", "seed", "seekToNextFrame", "seekable", "seeking", "select", "selectAllChildren", "selectNode", "selectNodeContents", "selectNodes", "selectSingleNode", "selectSubString", "selected", "selectedIndex", "selectedOptions", "selectedStyleSheetSet", "selectedStylesheetSet", "selection", "selectionDirection", "selectionEnd", "selectionStart", "selector", "selectorText", "self", "send", "sendAsBinary", "sendBeacon", "sender", "sentTimestamp", "separator", "serializeToString", "serviceWorker", "sessionId", "sessionStorage", "set", "setActionHandler", "setActive", "setAlpha", "setAttribute", "setAttributeNS", "setAttributeNode", "setAttributeNodeNS", "setBaseAndExtent", "setBingCurrentSearchDefault", "setCapture", "setColor", "setCompositeOperation", "setConfiguration", "setCurrentTime", "setCustomValidity", "setData", "setDate", "setDragImage", "setEnd", "setEndAfter", "setEndBefore", "setEndPoint", "setFillColor", "setFilterRes", "setFloat32", "setFloat64", "setFloatValue", "setFullYear", "setHours", "setIdentityProvider", "setImmediate", "setInt16", "setInt32", "setInt8", "setInterval", "setItem", "setLineCap", "setLineDash", "setLineJoin", "setLineWidth", "setLiveSeekableRange", "setLocalDescription", "setMatrix", "setMatrixValue", "setMediaKeys", "setMilliseconds", "setMinutes", "setMiterLimit", "setMonth", "setNamedItem", "setNamedItemNS", "setNonUserCodeExceptions", "setOrientToAngle", "setOrientToAuto", "setOrientation", "setOverrideHistoryNavigationMode", "setPaint", "setParameter", "setPeriodicWave", "setPointerCapture", "setPosition", "setPreference", "setProperty", "setPrototypeOf", "setRGBColor", "setRGBColorICCColor", "setRadius", "setRangeText", "setRemoteDescription", "setRequestHeader", "setResizable", "setResourceTimingBufferSize", "setRotate", "setScale", "setSeconds", "setSelectionRange", "setServerCertificate", "setShadow", "setSinkId", "setSkewX", "setSkewY", "setStart", "setStartAfter", "setStartBefore", "setStdDeviation", "setStringValue", "setStrokeColor", "setSuggestResult", "setTargetAtTime", "setTargetValueAtTime", "setTime", "setTimeout", "setTransform", "setTranslate", "setUTCDate", "setUTCFullYear", "setUTCHours", "setUTCMilliseconds", "setUTCMinutes", "setUTCMonth", "setUTCSeconds", "setUint16", "setUint32", "setUint8", "setUri", "setValueAtTime", "setValueCurveAtTime", "setVariable", "setVelocity", "setVersion", "setYear", "settingName", "settingValue", "sex", "shaderSource", "shadowBlur", "shadowColor", "shadowOffsetX", "shadowOffsetY", "shadowRoot", "shape", "shape-image-threshold", "shape-margin", "shape-outside", "shape-rendering", "shapeImageThreshold", "shapeMargin", "shapeOutside", "shapeRendering", "sheet", "shift", "shiftKey", "shiftLeft", "show", "showHelp", "showModal", "showModalDialog", "showModelessDialog", "showNotification", "sidebar", "sign", "signal", "signalingState", "sin", "singleNodeValue", "sinh", "sinkId", "size", "sizeToContent", "sizes", "skewX", "skewXSelf", "skewY", "skewYSelf", "slice", "slope", "slot", "small", "smil", "smoothingTimeConstant", "snapToLines", "snapshotItem", "snapshotLength", "some", "sort", "source", "sourceBuffer", "sourceBuffers", "sourceCapabilities", "sourceIndex", "spacing", "span", "speak", "speakAs", "speaking", "specified", "specularConstant", "specularExponent", "speechSynthesis", "speed", "speedOfSound", "spellcheck", "splice", "split", "splitText", "spreadMethod", "sqrt", "src", "srcElement", "srcFilter", "srcObject", "srcUrn", "srcdoc", "srclang", "srcset", "stack", "stackTraceLimit", "stacktrace", "standalone", "standby", "start", "startContainer", "startIce", "startMessages", "startOffset", "startRendering", "startSoftwareUpdate", "startTime", "startsWith", "state", "status", "statusMessage", "statusText", "statusbar", "stdDeviationX", "stdDeviationY", "stencilFunc", "stencilFuncSeparate", "stencilMask", "stencilMaskSeparate", "stencilOp", "stencilOpSeparate", "step", "stepDown", "stepMismatch", "stepUp", "sticky", "stitchTiles", "stop", "stop-color", "stop-opacity", "stopColor", "stopImmediatePropagation", "stopOpacity", "stopPropagation", "storage", "storageArea", "storageName", "storageStatus", "store", "storeSiteSpecificTrackingException", "storeWebWideTrackingException", "stpVersion", "stream", "strike", "stringValue", "stringify", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "strokeDasharray", "strokeDashoffset", "strokeLinecap", "strokeLinejoin", "strokeMiterlimit", "strokeOpacity", "strokeRect", "strokeStyle", "strokeText", "strokeWidth", "style", "styleFloat", "styleMedia", "styleSheet", "styleSheetSets", "styleSheets", "sub", "subarray", "subject", "submit", "subscribe", "substr", "substring", "substringData", "subtle", "subtree", "suffix", "suffixes", "summary", "sup", "supports", "surfaceScale", "surroundContents", "suspend", "suspendRedraw", "swapCache", "swapNode", "sweepFlag", "symbols", "system", "systemCode", "systemId", "systemLanguage", "systemXDPI", "systemYDPI", "tBodies", "tFoot", "tHead", "tabIndex", "tabSize", "table", "table-layout", "tableLayout", "tableValues", "tag", "tagName", "tagUrn", "tags", "taintEnabled", "takeRecords", "tan", "tanh", "target", "targetElement", "targetTouches", "targetX", "targetY", "tee", "tel", "terminate", "test", "texImage2D", "texParameterf", "texParameteri", "texSubImage2D", "text", "text-align", "text-align-last", "text-anchor", "text-combine-upright", "text-decoration", "text-decoration-color", "text-decoration-line", "text-decoration-style", "text-emphasis", "text-emphasis-color", "text-emphasis-position", "text-emphasis-style", "text-indent", "text-justify", "text-orientation", "text-overflow", "text-rendering", "text-shadow", "text-transform", "textAlign", "textAlignLast", "textAnchor", "textAutospace", "textBaseline", "textCombineUpright", "textContent", "textDecoration", "textDecorationBlink", "textDecorationColor", "textDecorationLine", "textDecorationLineThrough", "textDecorationNone", "textDecorationOverline", "textDecorationSkipInk", "textDecorationStyle", "textDecorationUnderline", "textEmphasis", "textEmphasisColor", "textEmphasisPosition", "textEmphasisStyle", "textIndent", "textJustify", "textJustifyTrim", "textKashida", "textKashidaSpace", "textLength", "textOrientation", "textOverflow", "textRendering", "textShadow", "textSizeAdjust", "textTracks", "textTransform", "textUnderlinePosition", "then", "threadId", "threshold", "tiltX", "tiltY", "time", "timeEnd", "timeLog", "timeOrigin", "timeStamp", "timeout", "timestamp", "timestampOffset", "timing", "title", "toArray", "toBlob", "toDataURL", "toDateString", "toElement", "toExponential", "toFixed", "toFloat32Array", "toFloat64Array", "toGMTString", "toISOString", "toJSON", "toLocaleDateString", "toLocaleFormat", "toLocaleLowerCase", "toLocaleString", "toLocaleTimeString", "toLocaleUpperCase", "toLowerCase", "toMethod", "toPrecision", "toSdp", "toSource", "toStaticHTML", "toString", "toStringTag", "toTimeString", "toUTCString", "toUpperCase", "toggle", "toggleAttribute", "toggleLongPressEnabled", "tooLong", "tooShort", "toolbar", "top", "topMargin", "total", "totalFrameDelay", "totalJSHeapSize", "totalVideoFrames", "touch-action", "touchAction", "touches", "trace", "track", "transaction", "transactions", "transferControlToOffscreen", "transform", "transform-box", "transform-origin", "transform-style", "transformBox", "transformOrigin", "transformPoint", "transformString", "transformStyle", "transformToDocument", "transformToFragment", "transition", "transition-delay", "transition-duration", "transition-property", "transition-timing-function", "transitionDelay", "transitionDuration", "transitionProperty", "transitionTimingFunction", "translate", "translateSelf", "translationX", "translationY", "trim", "trimEnd", "trimLeft", "trimRight", "trimStart", "trueSpeed", "trunc", "truncate", "type", "typeDetail", "typeMismatch", "typeMustMatch", "types", "ubound", "undefined", "unescape", "uneval", "unicode", "unicode-bidi", "unicodeBidi", "unicodeRange", "uniform1f", "uniform1fv", "uniform1i", "uniform1iv", "uniform2f", "uniform2fv", "uniform2i", "uniform2iv", "uniform3f", "uniform3fv", "uniform3i", "uniform3iv", "uniform4f", "uniform4fv", "uniform4i", "uniform4iv", "uniformMatrix2fv", "uniformMatrix3fv", "uniformMatrix4fv", "unique", "uniqueID", "uniqueNumber", "unitType", "units", "unloadEventEnd", "unloadEventStart", "unlock", "unmount", "unobserve", "unpause", "unpauseAnimations", "unreadCount", "unregister", "unregisterContentHandler", "unregisterProtocolHandler", "unscopables", "unselectable", "unshift", "unsubscribe", "unsuspendRedraw", "unsuspendRedrawAll", "unwatch", "unwrapKey", "upX", "upY", "upZ", "update", "updateCommands", "updateEnabled", "updateIce", "updateInterval", "updatePlaybackRate", "updateSettings", "updated", "updating", "upgrade", "upload", "upper", "upperBound", "upperOpen", "uri", "url", "urn", "urns", "usages", "usb", "useCurrentView", "useMap", "useProgram", "usedJSHeapSize", "usedSpace", "userActivation", "userAgent", "userLanguage", "userSelect", "userZoom", "username", "v8BreakIterator", "vAlign", "vLink", "valid", "validate", "validateProgram", "validationMessage", "validity", "value", "valueAsDate", "valueAsNumber", "valueAsString", "valueInSpecifiedUnits", "valueMissing", "valueOf", "valueText", "valueType", "values", "vector-effect", "vectorEffect", "velocityAngular", "velocityExpansion", "velocityX", "velocityY", "vendor", "vendorSub", "verify", "version", "vertexAttrib1f", "vertexAttrib1fv", "vertexAttrib2f", "vertexAttrib2fv", "vertexAttrib3f", "vertexAttrib3fv", "vertexAttrib4f", "vertexAttrib4fv", "vertexAttribDivisorANGLE", "vertexAttribPointer", "vertical", "vertical-align", "verticalAlign", "verticalOverflow", "vibrate", "videoHeight", "videoTracks", "videoWidth", "view", "viewBox", "viewBoxString", "viewTarget", "viewTargetString", "viewport", "viewportAnchorX", "viewportAnchorY", "viewportElement", "visibility", "visibilityState", "visible", "visualViewport", "vlinkColor", "voice", "volume", "vrml", "vspace", "w", "wait", "wake", "wand", "warn", "wasClean", "wasDiscarded", "watch", "watchAvailability", "watchPosition", "webdriver", "webkitAddKey", "webkitAlignContent", "webkitAlignItems", "webkitAlignSelf", "webkitAnimation", "webkitAnimationDelay", "webkitAnimationDirection", "webkitAnimationDuration", "webkitAnimationFillMode", "webkitAnimationIterationCount", "webkitAnimationName", "webkitAnimationPlayState", "webkitAnimationTimingFunction", "webkitAppRegion", "webkitAppearance", "webkitAudioContext", "webkitAudioDecodedByteCount", "webkitAudioPannerNode", "webkitBackfaceVisibility", "webkitBackground", "webkitBackgroundAttachment", "webkitBackgroundClip", "webkitBackgroundColor", "webkitBackgroundImage", "webkitBackgroundOrigin", "webkitBackgroundPosition", "webkitBackgroundPositionX", "webkitBackgroundPositionY", "webkitBackgroundRepeat", "webkitBackgroundSize", "webkitBackingStorePixelRatio", "webkitBorderAfter", "webkitBorderAfterColor", "webkitBorderAfterStyle", "webkitBorderAfterWidth", "webkitBorderBefore", "webkitBorderBeforeColor", "webkitBorderBeforeStyle", "webkitBorderBeforeWidth", "webkitBorderBottomLeftRadius", "webkitBorderBottomRightRadius", "webkitBorderEnd", "webkitBorderEndColor", "webkitBorderEndStyle", "webkitBorderEndWidth", "webkitBorderHorizontalSpacing", "webkitBorderImage", "webkitBorderImageOutset", "webkitBorderImageRepeat", "webkitBorderImageSlice", "webkitBorderImageSource", "webkitBorderImageWidth", "webkitBorderRadius", "webkitBorderStart", "webkitBorderStartColor", "webkitBorderStartStyle", "webkitBorderStartWidth", "webkitBorderTopLeftRadius", "webkitBorderTopRightRadius", "webkitBorderVerticalSpacing", "webkitBoxAlign", "webkitBoxDecorationBreak", "webkitBoxDirection", "webkitBoxFlex", "webkitBoxOrdinalGroup", "webkitBoxOrient", "webkitBoxPack", "webkitBoxReflect", "webkitBoxShadow", "webkitBoxSizing", "webkitCancelAnimationFrame", "webkitCancelFullScreen", "webkitCancelKeyRequest", "webkitCancelRequestAnimationFrame", "webkitClearResourceTimings", "webkitClipPath", "webkitClosedCaptionsVisible", "webkitColumnBreakAfter", "webkitColumnBreakBefore", "webkitColumnBreakInside", "webkitColumnCount", "webkitColumnGap", "webkitColumnRule", "webkitColumnRuleColor", "webkitColumnRuleStyle", "webkitColumnRuleWidth", "webkitColumnSpan", "webkitColumnWidth", "webkitColumns", "webkitConvertPointFromNodeToPage", "webkitConvertPointFromPageToNode", "webkitCreateShadowRoot", "webkitCurrentFullScreenElement", "webkitCurrentPlaybackTargetIsWireless", "webkitDecodedFrameCount", "webkitDirectionInvertedFromDevice", "webkitDisplayingFullscreen", "webkitDroppedFrameCount", "webkitEnterFullScreen", "webkitEnterFullscreen", "webkitEntries", "webkitExitFullScreen", "webkitExitFullscreen", "webkitExitPointerLock", "webkitFilter", "webkitFlex", "webkitFlexBasis", "webkitFlexDirection", "webkitFlexFlow", "webkitFlexGrow", "webkitFlexShrink", "webkitFlexWrap", "webkitFontFeatureSettings", "webkitFontSizeDelta", "webkitFontSmoothing", "webkitFullScreenKeyboardInputAllowed", "webkitFullscreenElement", "webkitFullscreenEnabled", "webkitGenerateKeyRequest", "webkitGetAsEntry", "webkitGetDatabaseNames", "webkitGetEntries", "webkitGetEntriesByName", "webkitGetEntriesByType", "webkitGetFlowByName", "webkitGetGamepads", "webkitGetImageDataHD", "webkitGetNamedFlows", "webkitGetRegionFlowRanges", "webkitGetUserMedia", "webkitHasClosedCaptions", "webkitHidden", "webkitHighlight", "webkitHyphenateCharacter", "webkitIDBCursor", "webkitIDBDatabase", "webkitIDBDatabaseError", "webkitIDBDatabaseException", "webkitIDBFactory", "webkitIDBIndex", "webkitIDBKeyRange", "webkitIDBObjectStore", "webkitIDBRequest", "webkitIDBTransaction", "webkitImageSmoothingEnabled", "webkitIndexedDB", "webkitInitMessageEvent", "webkitIsFullScreen", "webkitJustifyContent", "webkitKeys", "webkitLineBreak", "webkitLineClamp", "webkitLineDashOffset", "webkitLocale", "webkitLockOrientation", "webkitLogicalHeight", "webkitLogicalWidth", "webkitMarginAfter", "webkitMarginAfterCollapse", "webkitMarginBefore", "webkitMarginBeforeCollapse", "webkitMarginBottomCollapse", "webkitMarginCollapse", "webkitMarginEnd", "webkitMarginStart", "webkitMarginTopCollapse", "webkitMask", "webkitMaskBoxImage", "webkitMaskBoxImageOutset", "webkitMaskBoxImageRepeat", "webkitMaskBoxImageSlice", "webkitMaskBoxImageSource", "webkitMaskBoxImageWidth", "webkitMaskClip", "webkitMaskComposite", "webkitMaskImage", "webkitMaskOrigin", "webkitMaskPosition", "webkitMaskPositionX", "webkitMaskPositionY", "webkitMaskRepeat", "webkitMaskRepeatX", "webkitMaskRepeatY", "webkitMaskSize", "webkitMatchesSelector", "webkitMaxLogicalHeight", "webkitMaxLogicalWidth", "webkitMediaStream", "webkitMinLogicalHeight", "webkitMinLogicalWidth", "webkitNotifications", "webkitOfflineAudioContext", "webkitOpacity", "webkitOrder", "webkitOrientation", "webkitPaddingAfter", "webkitPaddingBefore", "webkitPaddingEnd", "webkitPaddingStart", "webkitPeerConnection00", "webkitPersistentStorage", "webkitPerspective", "webkitPerspectiveOrigin", "webkitPerspectiveOriginX", "webkitPerspectiveOriginY", "webkitPointerLockElement", "webkitPostMessage", "webkitPreservesPitch", "webkitPrintColorAdjust", "webkitPutImageDataHD", "webkitRTCPeerConnection", "webkitRegionOverset", "webkitRequestAnimationFrame", "webkitRequestFileSystem", "webkitRequestFullScreen", "webkitRequestFullscreen", "webkitRequestPointerLock", "webkitResolveLocalFileSystemURL", "webkitRtlOrdering", "webkitRubyPosition", "webkitSetMediaKeys", "webkitSetResourceTimingBufferSize", "webkitShadowRoot", "webkitShapeImageThreshold", "webkitShapeMargin", "webkitShapeOutside", "webkitShowPlaybackTargetPicker", "webkitSlice", "webkitSpeechGrammar", "webkitSpeechGrammarList", "webkitSpeechRecognition", "webkitSpeechRecognitionError", "webkitSpeechRecognitionEvent", "webkitStorageInfo", "webkitSupportsFullscreen", "webkitTapHighlightColor", "webkitTemporaryStorage", "webkitTextCombine", "webkitTextDecorationsInEffect", "webkitTextEmphasis", "webkitTextEmphasisColor", "webkitTextEmphasisPosition", "webkitTextEmphasisStyle", "webkitTextFillColor", "webkitTextOrientation", "webkitTextSecurity", "webkitTextSizeAdjust", "webkitTextStroke", "webkitTextStrokeColor", "webkitTextStrokeWidth", "webkitTransform", "webkitTransformOrigin", "webkitTransformOriginX", "webkitTransformOriginY", "webkitTransformOriginZ", "webkitTransformStyle", "webkitTransition", "webkitTransitionDelay", "webkitTransitionDuration", "webkitTransitionProperty", "webkitTransitionTimingFunction", "webkitURL", "webkitUnlockOrientation", "webkitUserDrag", "webkitUserModify", "webkitUserSelect", "webkitVideoDecodedByteCount", "webkitVisibilityState", "webkitWirelessVideoPlaybackDisabled", "webkitWritingMode", "webkitdirectory", "webkitdropzone", "webstore", "weight", "whatToShow", "wheelDelta", "wheelDeltaX", "wheelDeltaY", "whenDefined", "which", "white-space", "whiteSpace", "wholeText", "widows", "width", "will-change", "willChange", "willValidate", "window", "withCredentials", "word-break", "word-spacing", "word-wrap", "wordBreak", "wordSpacing", "wordWrap", "wrap", "wrapKey", "writable", "write", "writeText", "writeln", "writing-mode", "writingMode", "x", "x1", "x2", "xChannelSelector", "xmlEncoding", "xmlStandalone", "xmlVersion", "xmlbase", "xmllang", "xmlspace", "xor", "y", "y1", "y2", "yChannelSelector", "yandex", "z", "z-index", "zIndex", "zoom", "zoomAndPan", "zoomRectScreen" ] UglifyJS2-3.6.3/tools/exit.js000066400000000000000000000007251355252637300157670ustar00rootroot00000000000000// workaround for tty output truncation upon process.exit() var exit = process.exit; process.exit = function() { var args = [].slice.call(arguments); process.once("uncaughtException", function() { (function callback() { if (process.stdout.bufferSize || process.stderr.bufferSize) { setImmediate(callback); } else { exit.apply(process, args); } })(); }); throw exit; }; UglifyJS2-3.6.3/tools/exports.js000066400000000000000000000003161355252637300165160ustar00rootroot00000000000000exports["Dictionary"] = Dictionary; exports["minify"] = minify; exports["parse"] = parse; exports["push_uniq"] = push_uniq; exports["TreeTransformer"] = TreeTransformer; exports["TreeWalker"] = TreeWalker; UglifyJS2-3.6.3/tools/node.js000066400000000000000000000045331355252637300157440ustar00rootroot00000000000000var fs = require("fs"); exports.FILES = [ require.resolve("../lib/utils.js"), require.resolve("../lib/ast.js"), require.resolve("../lib/parse.js"), require.resolve("../lib/transform.js"), require.resolve("../lib/scope.js"), require.resolve("../lib/output.js"), require.resolve("../lib/compress.js"), require.resolve("../lib/sourcemap.js"), require.resolve("../lib/mozilla-ast.js"), require.resolve("../lib/propmangle.js"), require.resolve("../lib/minify.js"), require.resolve("./exports.js"), ]; new Function("MOZ_SourceMap", "exports", function() { var code = exports.FILES.map(function(file) { return fs.readFileSync(file, "utf8"); }); code.push("exports.describe_ast = " + describe_ast.toString()); return code.join("\n\n"); }())(require("source-map"), exports); function describe_ast() { var out = OutputStream({ beautify: true }); function doitem(ctor) { out.print("AST_" + ctor.TYPE); var props = ctor.SELF_PROPS.filter(function(prop) { return !/^\$/.test(prop); }); if (props.length > 0) { out.space(); out.with_parens(function() { props.forEach(function(prop, i) { if (i) out.space(); out.print(prop); }); }); } if (ctor.documentation) { out.space(); out.print_string(ctor.documentation); } if (ctor.SUBCLASSES.length > 0) { out.space(); out.with_block(function() { ctor.SUBCLASSES.sort(function(a, b) { return a.TYPE < b.TYPE ? -1 : 1; }).forEach(function(ctor, i) { out.indent(); doitem(ctor); out.newline(); }); }); } }; doitem(AST_Node); return out + "\n"; } function infer_options(options) { var result = exports.minify("", options); return result.error && result.error.defs; } exports.default_options = function() { var defs = {}; Object.keys(infer_options({ 0: 0 })).forEach(function(component) { var options = {}; options[component] = { 0: 0 }; if (options = infer_options(options)) { defs[component] = options; } }); return defs; }; UglifyJS2-3.6.3/tools/props.html000066400000000000000000000374601355252637300165170ustar00rootroot00000000000000