Compare commits

...

114 Commits
v2.0 ... v2.2.0

Author SHA1 Message Date
Mihai Bazon
642ba2e92c rename the npm package to "uglify-js" and cli tool to "uglifyjs" 2012-11-21 13:27:03 +02:00
Mihai Bazon
089ac908b7 fix #51 2012-11-18 17:37:45 +02:00
Mihai Bazon
0d3fd2ef30 retain (1,eval) as is when it's the expression of an AST_Call
otherwise we change the meaning of eval from global to lexical.
2012-11-17 12:05:31 +02:00
Richard van Velzen
e98119496a Add support for somewhat preserving line numbers.
Usage: uglifyjs2 -b "beautify=0,preserve_line=1" /path/to/js

ref #46
2012-11-14 15:30:18 +02:00
Mihai Bazon
bdfcbf496b better solution for the last test in constant switch folding 2012-11-14 12:21:43 +02:00
Mihai Bazon
dba8da4800 optimize constant switch blocks
ref. mishoo/UglifyJS#441
2012-11-14 12:06:07 +02:00
Mihai Bazon
60c0f40250 Merge branch 'optimize_concat' of https://github.com/rvanvelzen/UglifyJS2 into rvanvelzen-optimize_concat 2012-11-13 14:38:55 +02:00
Mihai Bazon
e02771a5f2 don't change order in binary expressions if both operands have side effects 2012-11-13 14:32:07 +02:00
Richard van Velzen
f96f796f71 Add simple optimization for empty-string concats.
ref. #43
2012-11-12 15:41:03 +01:00
Mihai Bazon
a9fa178f86 v2.1.11 2012-11-12 13:24:52 +02:00
Mihai Bazon
53355bdb24 fix invalid AST produced by dropping unused variable
close #44
2012-11-12 13:23:57 +02:00
Mihai Bazon
f05c99d89f Merge pull request #41 from Skalman/toString-patch
Convert x.toString() to ""+x instead of x+""
2012-11-12 00:47:56 -08:00
Dan Wolff
b49230ab8d convert x.toString() to ""+x instead of x+""
In some places this can save one byte in whitespace, e.g. after return.
Example:

function f(arg) {
        // return""+arg - no space between return and ""
        return arg.toString();
}
2012-11-11 15:53:34 +02:00
Mihai Bazon
78856a3dab declare dependency versions
close #40
2012-11-09 16:43:49 +02:00
Mihai Bazon
1e5e13ed81 AST_LabelRef no longer inherits from AST_SymbolRef 2012-11-08 15:39:14 +02:00
Mihai Bazon
64270b9778 v2.1.10 2012-11-08 12:33:27 +02:00
Mihai Bazon
e312c5c2a7 fix API breakage
close #36, #38
2012-11-08 12:31:28 +02:00
Mihai Bazon
1a5fd3e052 optimization for if/break as first statement in a loop body
for(...; x; ...) if (y) break; → for(...; x&&!y; ...);

similarly for `while` and some combinations (i.e. the `break` appears in the
`else` clause, etc.)
2012-11-08 11:43:14 +02:00
Mihai Bazon
5a7e54cf72 ignore node_modules/ 2012-11-07 15:27:12 +02:00
Mihai Bazon
39f8a62703 v2.1.9 2012-11-07 13:31:58 +02:00
Mihai Bazon
46be3f2bf1 fix another small regression
we do need parens here: `new (foo.bar().baz)`, but not here: `new foo.bar.baz`
2012-11-07 13:31:43 +02:00
Mihai Bazon
258b46f4dc v2.1.8 2012-11-07 13:03:11 +02:00
Mihai Bazon
80da21dab4 fix regression from 5346fb94 (shouldn't parenthesize i++ in x[i++]) 2012-11-07 13:02:51 +02:00
Mihai Bazon
bb0e4d7126 v2.1.7 2012-11-07 12:45:23 +02:00
Mihai Bazon
5276a4a873 add AST_Accessor and AST_SymbolAccessor node types
AST_Accessor will represent the function for a setter or getter.  Since they
are not mangleable, and they should not introduce a name in scope, we have a
new node for their name (AST_SymbolAccessor) which doesn't inherit from
AST_SymbolDeclaration.

fix #37
2012-11-07 12:43:27 +02:00
Mihai Bazon
a1ae0c8609 parenthesize property access when it's the expression in New
refs #35
2012-11-07 12:26:33 +02:00
Mihai Bazon
a90c1aeafe further fix for parens around New (refs #35) 2012-11-07 11:49:06 +02:00
Mihai Bazon
ff388a8d2d parenthesize a Call expression when its parent is New
fix #35
2012-11-07 11:36:15 +02:00
Mihai Bazon
5346fb94bb add proper parens around unary expressions
fix #34
2012-11-07 11:23:50 +02:00
Mihai Bazon
a4f6d46118 add option to mangle names even if eval/with is in use
(for more fair comparison to Closure compiler)
2012-11-06 18:19:51 +02:00
Mihai Bazon
7f5f4d60b7 discard the hack that worked around the deprecation warning
(since the source-map module no longer uses require.js)

refs #9
2012-11-05 22:23:51 +02:00
Mihai Bazon
ffccb233e5 convert while into for 2012-11-05 16:01:20 +02:00
Mihai Bazon
fba0c1aafe minor 2012-11-05 16:01:09 +02:00
Mihai Bazon
774f2ded94 minor optimization
for `==` or `!=` against a constant, prefer to display the constant first.
should help a bit after gzip, i.e.:

    typeof foo=="undefined"
    ^^^^^^    ^^^^^^^^^^^^^

vs:

    "undefined"==typeof foo
    ^^^^^^^^^^^^^^^^^^^     (longer sequence that could repeat)

idea stolen from closure.
2012-11-05 13:13:06 +02:00
Mihai Bazon
85af942d64 print final semicolon
close #28
2012-11-05 13:09:39 +02:00
Mihai Bazon
8413787efc use a Dictionary object instead of plain object for hashes
to mitigate the `__proto__` issue

related to #30
2012-11-02 10:58:45 +02:00
Mihai Bazon
dde57452aa v2.1.6 2012-11-01 16:55:10 +02:00
Mihai Bazon
cf409800be it's safe to negate expression in !EXP only in boolean context
#kendo
2012-11-01 15:49:05 +02:00
Mihai Bazon
18270dd9f3 added unsafe_comps for negating <= with >
since it has the potential to break code, let's keep it disabled by default
2012-11-01 15:14:56 +02:00
Mihai Bazon
d4c25c571b fix compressing UnaryPrefix
only try negating the expression if the operator is `!`

#kendo
2012-11-01 13:35:08 +02:00
Mihai Bazon
5248b79506 v2.1.5 2012-10-30 14:51:05 +02:00
Mihai Bazon
abe0ebbf02 don't move expressions containing the binary in operator into the for initializer
(opera can't parse it)

close #25
2012-10-30 14:50:47 +02:00
Mihai Bazon
0852f5595e v2.1.4 2012-10-25 18:52:49 +03:00
Mihai Bazon
cb3cafa14d cripple scope to make IE happy :-(
close #24
2012-10-25 18:52:35 +03:00
Mihai Bazon
202fb93799 test for fs.existsSync 2012-10-25 10:58:48 +03:00
Mihai Bazon
7b87d2ef83 v2.1.3 2012-10-24 09:41:40 +03:00
Mihai Bazon
70fd2b1f33 fix for if (...) return; else return ...;
(it was assumed that the first `return` always contains a value)

close #22
2012-10-24 09:33:32 +03:00
Mihai Bazon
30faaf13ed more sequence optimizations (lift some sequences above binary/unary expressions so that we can avoid parens) 2012-10-22 11:58:06 +03:00
Mihai Bazon
41be8632d3 v2.1.2 2012-10-22 07:57:28 +03:00
Mihai Bazon
bee01dc1be Merge branch 'master' of github.com:mishoo/UglifyJS2 2012-10-20 11:14:25 +03:00
Mihai Bazon
12f71e01d0 alternate hack to disable deprecation warning
ref #9, close #20
2012-10-20 11:12:21 +03:00
Mihai Bazon
3a72deacab Merge pull request #19 from SevInf/master
Allow to specify sourceRoot in minify
2012-10-19 04:29:40 -07:00
Mihai Bazon
fc8314e810 minor fix for dropping unused definitions.
function f(x, y) {
        var g = function() { return h() };
        var h = function() { return g() };
        return x + y;
    }

now compresses to `function f(x, y) { return x + y }`
2012-10-19 12:57:29 +03:00
Sergej Tatarincev
11dffe950e Add sourceRoot option to minify 2012-10-19 12:35:19 +03:00
Mihai Bazon
6f45928a73 add fromString argument to UglifyJS.minify (allows to pass the source
code, instead of file names, as first argument).

close #17
2012-10-18 15:49:15 +03:00
Mihai Bazon
afb7faa6fa more optimizations for some break/continue cases 2012-10-18 15:14:57 +03:00
Mihai Bazon
6aa56f92fe v2.1.1 2012-10-18 10:54:30 +03:00
Mihai Bazon
4fe4257c69 fix --comments (close #16) 2012-10-18 10:54:10 +03:00
Mihai Bazon
a5e75c5a21 v2.1.0 2012-10-17 22:00:11 +03:00
Mihai Bazon
4482fdd63f added note about API docs and online demo 2012-10-17 21:59:36 +03:00
Mihai Bazon
253bd8559b more small optimizations
(unlikely to help for hand-written code)
2012-10-17 21:57:08 +03:00
Mihai Bazon
6a099fba66 define aborts on AST_If: true if both branches abort 2012-10-17 16:17:14 +03:00
Mihai Bazon
a21f3c6cdd employ a better parser for command-line arguments
to support passing commas in strings in for example:

    uglifyjs2 -cd TEST="'a,b'" <<EOF
    console.log(TEST);
    EOF

    → console.log("a,b")

close #14
2012-10-17 15:56:45 +03:00
Mihai Bazon
8f66458598 the sort option is broken anyway, removed it
we need to mangle names from outermost to innermost scope; mangling names
from inner scopes before we got to the outer scope won't work correctly,
therefore sorting doesn't make sense.
2012-10-17 15:24:47 +03:00
Mihai Bazon
6472f9410e add semicolons option in the code generator (default: true)
pass `false` to separate statements with newlines instead of semicolons
2012-10-17 14:52:08 +03:00
Mihai Bazon
8957b3a694 fix small glitches in source map generation 2012-10-16 15:54:12 +03:00
Mihai Bazon
1ffd526554 disable warnings in the test suite 2012-10-13 15:18:11 +03:00
Mihai Bazon
fcc0229087 drop unused function arguments
also add test for "drop_unused" (the last one fails for now)
2012-10-13 15:04:44 +03:00
Mihai Bazon
b071c9d079 add parens to AST_Seq whose parent is AST_Unary 2012-10-13 14:32:08 +03:00
Mihai Bazon
851b48e4a3 fix compressing benchmark.js (it tried to evaluate a statement)
the following code in benchmark.js triggered the issue:

    support.decompilation = Function(
      'return (' + (function(x) { return { 'x': '' + (1 + x) + '', 'y': 0 }; }) + ')'
    )()(0).x === '1';

technically that could be resolved into a constant expression, but seems
it's being used here for browser bugs detection :-\
2012-10-13 12:57:10 +03:00
Mihai Bazon
708abb1ab1 minor 2012-10-13 12:42:01 +03:00
Mihai Bazon
370d3e0917 fix regression from fb5c01c073
is_digit takes a char code now, not a string
2012-10-13 12:24:27 +03:00
Mihai Bazon
b51fe0dcc3 fix end tokens in spidermonkey ast import 2012-10-13 11:37:58 +03:00
Mihai Bazon
70d205c447 update for acorn 2012-10-13 00:35:24 +03:00
Mihai Bazon
8149be551e minor 2012-10-12 14:55:54 +03:00
Mihai Bazon
ba3df646c0 actually enable the option that drops unused names in the test of issue #12 2012-10-12 11:41:48 +03:00
Mihai Bazon
1b6f8d463f remove the $self hack
operations are destructive anyway, so there's no point to clone the nodes in
the transformer.  speed++
2012-10-12 11:07:35 +03:00
Mihai Bazon
731fa9c236 add test for issue #12 2012-10-12 10:49:48 +03:00
Mihai Bazon
72cb5328ee fix in_boolean_context() (two tests were broken) 2012-10-12 10:49:41 +03:00
Mihai Bazon
fc39553714 use AST_Lambda for object setters/getters
so that the optimization that drops the name if unused doesn't apply.
close #12
2012-10-12 10:11:01 +03:00
Mihai Bazon
d9d67317b1 fix pos in syntax error exception 2012-10-11 15:25:38 +03:00
Mihai Bazon
fb5c01c073 stealing more hacks from acorn in the name of speed 2012-10-11 15:17:42 +03:00
Mihai Bazon
f4584af42c using makeComparator from acorn to generate functions that tests whether a
string is keyword, reserved etc.

speeds up the parser a bit, though not spectacular.. still far from acorn.
2012-10-11 11:52:05 +03:00
Mihai Bazon
172aa7a93c cleanup
- use prototype-less objects where feasible (minor speed improvement)
- get rid of HOP
2012-10-11 11:07:42 +03:00
Mihai Bazon
5053a29bc0 fix propagation of symbol references 2012-10-11 10:28:48 +03:00
Mihai Bazon
f322b32e0e disable warnings by default in minify (pass warnings: true to enable)
close #11
2012-10-11 09:31:17 +03:00
Mihai Bazon
9cdaed9860 fix node name 2012-10-10 23:16:40 +03:00
Mihai Bazon
dacce1b1fa seems cleaner if AST_Label doesn't inherit from AST_SymbolDeclaration 2012-10-10 11:37:51 +03:00
Mihai Bazon
f26f3b44bc small improvements in wrap_commonjs:
- use MAP.splice instead of a BlockStatement to inject code (avoids some
  warnings in the linter)
- use the original symbol in exports, so that we get the proper source mapping
2012-10-10 11:28:05 +03:00
Mihai Bazon
c5ecbfc756 drop unused variable 2012-10-10 11:27:06 +03:00
Mihai Bazon
3799ac8973 add --lint and display {file} in scope_warnings 2012-10-10 11:26:59 +03:00
Mihai Bazon
86182afa7f minor 2012-10-09 22:56:59 +03:00
Mihai Bazon
4807c6e756 update on @cc_on 2012-10-09 19:09:11 +03:00
Mihai Bazon
a84d07e312 add AST_Infinity node 2012-10-09 18:35:53 +03:00
Mihai Bazon
88beddfa91 make --comments keep @cc_on too 2012-10-09 18:25:00 +03:00
Mihai Bazon
1b0aab2ce9 added $propdoc to AST nodes and some cleanups
hopefully we can make the AST documentation self-generating
2012-10-09 18:20:39 +03:00
Mihai Bazon
9ead49641d minor AST cleanup (AST_BlockStatement may inherit from AST_Block) 2012-10-09 13:59:17 +03:00
Mihai Bazon
e1862cd36f add --ast-help
displays a rather cruel description of the AST classes, derived
directly from the node definitions.
2012-10-09 13:21:21 +03:00
Mihai Bazon
2c025f23db fix detecting symbols in use 2012-10-09 13:13:55 +03:00
Mihai Bazon
9dfcd47ec8 Merge pull request #8 from SevInf/master
Fix crash in minify function
2012-10-09 03:04:33 -07:00
Sergej Tatarincev
203ecaf85b Fix nodejs minify without inSourceMap exception
When inSourceMap is omitted fs.readFile throws exception. Fixed version
calls fs.readFile only when inSourceMap argument is present
2012-10-09 12:52:28 +03:00
Mihai Bazon
c967f0b0fe fix inSourceMap in minify (should read the file) 2012-10-08 21:22:20 +03:00
Mihai Bazon
dfc04e6677 add simple API wrapper: UglifyJS.minify
(refs #7)
2012-10-08 21:15:59 +03:00
Mihai Bazon
42ea3c95e0 added some basic API doc 2012-10-08 14:30:34 +03:00
Mihai Bazon
d4970b35ac should not expose base54.sort() in the API docs, I think 2012-10-08 13:37:27 +03:00
Mihai Bazon
dd8286bce1 added --self to easily get a browser-runnable version of UglifyJS 2012-10-08 12:55:18 +03:00
Mihai Bazon
093a9031dc eliminate redundant directives in the same scope 2012-10-08 12:53:17 +03:00
Mihai Bazon
80a18fe2fa for certain nodes that we invent we might not have a original source file to
map from, so just use "?".  and in any case, don't fail hard when addMapping throws.
2012-10-08 12:52:25 +03:00
Mihai Bazon
fe1411bba1 fix typo 2012-10-05 23:10:52 +03:00
Mihai Bazon
455ac5435d remove unused code 2012-10-05 22:59:05 +03:00
Mihai Bazon
4a2b91220a minor 2012-10-05 20:24:56 +03:00
Mihai Bazon
a1e0885930 replace (function(){ ...no side effects ... })() with undefined. 2012-10-05 16:51:16 +03:00
Mihai Bazon
7ae09120ed prevent mangling only when eval is *called*, not when it's just referenced 2012-10-05 16:17:31 +03:00
Mihai Bazon
42c25d901c add note about installation 2012-10-05 15:31:55 +03:00
22 changed files with 2327 additions and 652 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
tmp/
node_modules/

291
README.md
View File

@@ -3,13 +3,29 @@ UglifyJS 2
UglifyJS is a JavaScript parser, minifier, compressor or beautifier toolkit.
For now this page documents the command line utility. More advanced
API documentation will be made available later.
This page documents the command line utility. For
[API and internals documentation see my website](http://lisperator.net/uglifyjs/).
There's also an
[in-browser online demo](http://lisperator.net/uglifyjs/#demo) (for Firefox,
Chrome and probably Safari).
Install
-------
From NPM:
npm install uglify-js
From Git:
git clone git://github.com/mishoo/UglifyJS2.git
cd UglifyJS2
npm link .
Usage
-----
uglifyjs2 [input files] [options]
uglifyjs [input files] [options]
UglifyJS2 can take multiple input files. It's recommended that you pass the
input files first, then pass the options. UglifyJS will parse input files
@@ -53,6 +69,15 @@ The available options are:
--acorn Use Acorn for parsing. [boolean]
--spidermonkey Assume input fles are SpiderMonkey AST format (as JSON).
[boolean]
--self Build itself (UglifyJS2) as a library (implies
--wrap=UglifyJS --export-all) [boolean]
--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.
[string]
--export-all Only used when --wrap, this tells UglifyJS to add code to
automatically export all globals. [boolean]
-v, --verbose Verbose [boolean]
Specify `--output` (`-o`) to declare the output file. Otherwise the output
@@ -73,12 +98,12 @@ map.
For example:
uglifyjs2 /home/doe/work/foo/src/js/file1.js \
/home/doe/work/foo/src/js/file2.js \
-o foo.min.js \
--source-map foo.min.js.map \
--source-map-root http://foo.com/src \
-p 5 -c -m
uglifyjs /home/doe/work/foo/src/js/file1.js \
/home/doe/work/foo/src/js/file2.js \
-o foo.min.js \
--source-map foo.min.js.map \
--source-map-root http://foo.com/src \
-p 5 -c -m
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
@@ -115,7 +140,7 @@ When mangling is enabled but you want to prevent certain names from being
mangled, you can declare those names with `--reserved` (`-r`) — pass a
comma-separated list of names. For example:
uglifyjs2 ... -m -r '$,require,exports'
uglifyjs ... -m -r '$,require,exports'
to prevent the `require`, `exports` and `$` names from being changed.
@@ -181,7 +206,7 @@ separate file and include it into the build. For example you can have a
and build your code like this:
uglifyjs2 build/defines.js js/foo.js js/bar.js... -c
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
@@ -219,15 +244,19 @@ can pass additional arguments that control the code output:
- `bracketize` (default `false`) -- always insert brackets in `if`, `for`,
`do`, `while` or `with` statements, even if their body is a single
statement.
- `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).
### 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" or
"@license". 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 '/foo|bar/'` will keep only comments that contain "foo"
or "bar".
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
'/foo|bar/'` will keep only comments that contain "foo" or "bar".
Note, however, that there might be situations where comments are lost. For
example:
@@ -245,7 +274,7 @@ 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 me kept in the output) are comments attached to toplevel nodes.
needs to be kept in the output) are comments attached to toplevel nodes.
## Support for the SpiderMonkey AST
@@ -254,12 +283,12 @@ UglifyJS2 has its own abstract syntax tree format; for
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](https://github.com/marijnh/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:
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 | uglifyjs2 --spidermonkey -m -c
acorn file.js | uglifyjs --spidermonkey -m -c
The `--spidermonkey` option tells UglifyJS that all input files are not
JavaScript, but JS code described in SpiderMonkey AST in JSON. Therefore we
@@ -269,11 +298,221 @@ internal AST.
### Use Acorn for parsing
More for fun, I added the `--acorn` option which will use Acorn to do all
the parsing. If you pass this option, UglifyJS will `require("acorn")`. At
the time I'm writing this it needs
[this commit](https://github.com/mishoo/acorn/commit/17c0d189c7f9ce5447293569036949b5d0a05fef)
in Acorn to support multiple input files and properly generate source maps.
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.
API Reference
-------------
Assuming installation via NPM, you can load UglifyJS in your application
like this:
var UglifyJS = require("uglify-js");
It exports a lot of names, but I'll discuss here the basics that are needed
for parsing, mangling and compressing a piece of code. The sequence is (1)
parse, (2) compress, (3) mangle, (4) generate output code.
### The simple way
There's a single toplevel function which combines all the steps. If you
don't need additional customization, you might want to go with `minify`.
Example:
// see "fromString" below if you need to pass code instead of file name
var result = UglifyJS.minify("/path/to/file.js");
console.log(result.code); // minified output
You can also compress multiple files:
var result = UglifyJS.minify([ "file1.js", "file2.js", "file3.js" ]);
console.log(result.code);
To generate a source map:
var result = UglifyJS.minify([ "file1.js", "file2.js", "file3.js" ], {
outSourceMap: "out.js.map"
});
console.log(result.code); // minified output
console.log(result.map);
Note that the source map is not saved in a file, it's just returned in
`result.map`. The value passed for `outSourceMap` is only used to set the
`file` attribute in the source map (see [the spec][sm-spec]).
You can also specify sourceRoot property to be included in source map:
var result = UglifyJS.minify([ "file1.js", "file2.js", "file3.js" ], {
outSourceMap: "out.js.map",
sourceRoot: "http://example.com/src"
});
If you're compressing compiled JavaScript and have a source map for it, you
can use the `inSourceMap` argument:
var result = UglifyJS.minify("compiled.js", {
inSourceMap: "compiled.js.map",
outSourceMap: "minified.js.map"
});
// same as before, it returns `code` and `map`
The `inSourceMap` is only used if you also request `outSourceMap` (it makes
no sense otherwise).
Other options:
- `warnings` (default `false`) — pass `true` to display compressor warnings.
- `fromString` (default `false`) — if you pass `true` then you can pass
JavaScript source code, rather than file names.
We could add more options to `UglifyJS.minify` — if you need additional
functionality please suggest!
### The hard way
Following there's more detailed API info, in case the `minify` function is
too simple for your needs.
#### The parser
var toplevel_ast = UglifyJS.parse(code, options);
`options` is optional and if present it must be an object. The following
properties are available:
- `strict` — disable automatic semicolon insertion and support for trailing
comma in arrays and objects
- `filename` — the name of the file where this code is coming from
- `toplevel` — a `toplevel` node (as returned by a previous invocation of
`parse`)
The last two options are useful when you'd like to minify multiple files and
get a single file as the output and a proper source map. Our CLI tool does
something like this:
var toplevel = null;
files.forEach(function(file){
var code = fs.readFileSync(file);
toplevel = UglifyJS.parse(code, {
filename: file,
toplevel: toplevel
});
});
After this, we have in `toplevel` a big AST containing all our files, with
each token having proper information about where it came from.
#### Scope information
UglifyJS contains a scope analyzer that you need to call manually before
compressing or mangling. Basically it augments various nodes in the AST
with information about where is a name defined, how many times is a name
referenced, if it is a global or not, if a function is using `eval` or the
`with` statement etc. I will discuss this some place else, for now what's
important to know is that you need to call the following before doing
anything with the tree:
toplevel.figure_out_scope()
#### Compression
Like this:
var compressor = UglifyJS.Compressor(options);
var compressed_ast = toplevel.transform(compressor);
The `options` can be missing. Available options are discussed above in
“Compressor options”. Defaults should lead to best compression in most
scripts.
The compressor is destructive, so don't rely that `toplevel` remains the
original tree.
#### Mangling
After compression it is a good idea to call again `figure_out_scope` (since
the compressor might drop unused variables / unreachable code and this might
change the number of identifiers or their position). Optionally, you can
call a trick that helps after Gzip (counting character frequency in
non-mangleable words). Example:
compressed_ast.figure_out_scope();
compressed_ast.compute_char_frequency();
compressed_ast.mangle_names();
#### Generating output
AST nodes have a `print` method that takes an output stream. Essentially,
to generate code you do this:
var stream = UglifyJS.OutputStream(options);
compressed_ast.print(stream);
var code = stream.toString(); // this is your minified code
or, for a shortcut you can do:
var code = compressed_ast.print_to_string(options);
As usual, `options` is optional. The output stream accepts a lot of otions,
most of them documented above in section “Beautifier options”. The two
which we care about here are `source_map` and `comments`.
#### Keeping comments in the output
In order to keep certain comments in the output you need to pass the
`comments` option. Pass a RegExp or a function. If you pass a RegExp, only
those comments whose body matches the regexp will be kept. Note that body
means without the initial `//` or `/*`. If you pass a function, it will be
called for every comment in the tree and will receive two arguments: the
node that the comment is attached to, and the comment token itself.
The comment token has these properties:
- `type`: "comment1" for single-line comments or "comment2" for multi-line
comments
- `value`: the comment body
- `pos` and `endpos`: the start/end positions (zero-based indexes) in the
original code where this comment appears
- `line` and `col`: the line and column where this comment appears in the
original code
- `file` — the file name of the original file
- `nlb` — true if there was a newline before this comment in the original
code, or if this comment contains a newline.
Your function should return `true` to keep the comment, or a falsy value
otherwise.
#### Generating a source mapping
You need to pass the `source_map` argument when calling `print`. It needs
to be a `SourceMap` object (which is a thin wrapper on top of the
[source-map][source-map] library).
Example:
var source_map = UglifyJS.SourceMap(source_map_options);
var stream = UglifyJS.OutputStream({
...
source_map: source_map
});
compressed_ast.print(stream);
var code = stream.toString();
var map = source_map.toString(); // json output for your source map
The `source_map_options` (optional) can contain the following properties:
- `file`: the name of the JavaScript output file that this mapping refers to
- `root`: the `sourceRoot` property (see the [spec][sm-spec])
- `orig`: the "original source map", handy when you compress generated JS
and want to map the minified output back to the original code where it
came from. It can be simply a string in JSON, or a JSON object containing
the original source map.
[acorn]: https://github.com/marijnh/acorn
[source-map]: https://github.com/mozilla/source-map
[sm-spec]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit

View File

@@ -43,6 +43,11 @@ because of dead code removal or cascading statements into sequences.")
.describe("stats", "Display operations run time on STDERR.")
.describe("acorn", "Use Acorn for parsing.")
.describe("spidermonkey", "Assume input fles are SpiderMonkey AST format (as JSON).")
.describe("self", "Build itself (UglifyJS2) as a library (implies --wrap=UglifyJS --export-all)")
.describe("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.")
.describe("export-all", "Only used when --wrap, this tells UglifyJS to add code to automatically export all globals.")
.describe("lint", "Display some scope warnings")
.describe("v", "Verbose")
.alias("p", "prefix")
@@ -61,16 +66,28 @@ because of dead code removal or cascading statements into sequences.")
.string("c")
.string("d")
.string("comments")
.string("wrap")
.boolean("export-all")
.boolean("self")
.boolean("v")
.boolean("stats")
.boolean("acorn")
.boolean("spidermonkey")
.boolean("lint")
.wrap(80)
.argv
;
normalize(ARGS);
if (ARGS.ast_help) {
var desc = UglifyJS.describe_ast();
sys.puts(typeof desc == "string" ? desc : JSON.stringify(desc, null, 2));
process.exit(0);
}
if (ARGS.h || ARGS.help) {
sys.puts(optimist.help());
process.exit(0);
@@ -80,11 +97,9 @@ if (ARGS.acorn) {
acorn = require("acorn");
}
normalize(ARGS);
var COMPRESS = getOptions("c");
var MANGLE = getOptions("m");
var BEAUTIFY = getOptions("b");
var COMPRESS = getOptions("c", true);
var MANGLE = getOptions("m", true);
var BEAUTIFY = getOptions("b", true);
if (COMPRESS && ARGS.d) {
COMPRESS.global_defs = getOptions("d");
@@ -112,8 +127,7 @@ if (ARGS.comments) {
var type = comment.type;
if (type == "comment2") {
// multiline comment
return text.indexOf("@preserve") >= 0
|| text.indexOf("@license") >= 0;
return /@preserve|@license|@cc_on/i.test(text);
}
}
}
@@ -121,6 +135,15 @@ if (ARGS.comments) {
var files = ARGS._.slice();
if (ARGS.self) {
if (files.length > 0) {
sys.error("WARN: Ignoring input files since --self was passed");
}
files = UglifyJS.FILES;
if (!ARGS.wrap) ARGS.wrap = "UglifyJS";
ARGS.export_all = true;
}
var ORIG_MAP = ARGS.in_source_map;
if (ORIG_MAP) {
@@ -204,11 +227,18 @@ if (ARGS.acorn || ARGS.spidermonkey) time_it("convert_ast", function(){
TOPLEVEL = UglifyJS.AST_Node.from_mozilla_ast(TOPLEVEL);
});
var SCOPE_IS_NEEDED = COMPRESS || MANGLE;
if (ARGS.wrap) {
TOPLEVEL = TOPLEVEL.wrap_commonjs(ARGS.wrap, ARGS.export_all);
}
var SCOPE_IS_NEEDED = COMPRESS || MANGLE || ARGS.lint;
if (SCOPE_IS_NEEDED) {
time_it("scope", function(){
TOPLEVEL.figure_out_scope();
if (ARGS.lint) {
TOPLEVEL.scope_warnings();
}
});
}
@@ -222,8 +252,7 @@ if (SCOPE_IS_NEEDED) {
time_it("scope", function(){
TOPLEVEL.figure_out_scope();
if (MANGLE) {
TOPLEVEL.compute_char_frequency();
UglifyJS.base54.sort();
TOPLEVEL.compute_char_frequency(MANGLE);
}
});
}
@@ -270,16 +299,36 @@ function normalize(o) {
}
}
function getOptions(x) {
function getOptions(x, constants) {
x = ARGS[x];
if (!x) return null;
var ret = {};
if (x !== true) {
x.replace(/^\s+|\s+$/g).split(/\s*,+\s*/).forEach(function(opt){
var a = opt.split(/\s*[=:]\s*/);
ret[a[0]] = a.length > 1 ? new Function("return(" + a[1] + ")")() : true;
});
normalize(ret);
var ast;
try {
ast = UglifyJS.parse(x);
} catch(ex) {
if (ex instanceof UglifyJS.JS_Parse_Error) {
sys.error("Error parsing arguments in: " + x);
process.exit(1);
}
}
ast.walk(new UglifyJS.TreeWalker(function(node){
if (node instanceof UglifyJS.AST_Toplevel) return; // descend
if (node instanceof UglifyJS.AST_SimpleStatement) return; // descend
if (node instanceof UglifyJS.AST_Seq) return; // descend
if (node instanceof UglifyJS.AST_Assign) {
var name = node.left.print_to_string({ beautify: false }).replace(/-/g, "_");
var value = node.right;
if (constants)
value = new Function("return (" + value.print_to_string() + ")")();
ret[name] = value;
return true; // no descend
}
sys.error(node.TYPE)
sys.error("Error parsing arguments in: " + x);
process.exit(1);
}));
}
return ret;
}

View File

@@ -43,8 +43,6 @@
"use strict";
var NODE_HIERARCHY = {};
function DEFNODE(type, props, methods, base) {
if (arguments.length < 4) base = AST_Node;
if (!props) props = [];
@@ -59,21 +57,21 @@ function DEFNODE(type, props, methods, base) {
var proto = base && new base;
if (proto && proto.initialize || (methods && methods.initialize))
code += "this.initialize();";
code += " } ";
code += "if (!this.$self) this.$self = this;";
code += " } ";
code += "}}";
var ctor = new Function(code)();
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 (i in methods) if (HOP(methods, i)) {
if (methods) for (i in methods) if (methods.hasOwnProperty(i)) {
if (/^\$/.test(i)) {
ctor[i.substr(1)] = methods[i];
} else {
@@ -83,21 +81,21 @@ function DEFNODE(type, props, methods, base) {
ctor.DEFMETHOD = function(name, method) {
this.prototype[name] = method;
};
NODE_HIERARCHY[type] = {
def: ctor,
base: base
};
return ctor;
};
var AST_Token = DEFNODE("Token", "type value line col pos endpos nlb comments_before file", {
}, null);
var AST_Node = DEFNODE("Node", "$self start end", {
var AST_Node = DEFNODE("Node", "start end", {
clone: function() {
return new this.CTOR(this);
},
$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);
},
@@ -124,10 +122,17 @@ var AST_Debugger = DEFNODE("Debugger", null, {
var AST_Directive = DEFNODE("Directive", "value scope", {
$documentation: "Represents a directive, like \"use strict\";",
$propdoc: {
value: "[string] The value of this directive as a plain string (it's not an AST_String!)",
scope: "[AST_Scope/S] The scope that this directive affects"
},
}, AST_Statement);
var AST_SimpleStatement = DEFNODE("SimpleStatement", "body", {
$documentation: "A statement consisting of an expression, i.e. a = 1 + 2.",
$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);
@@ -135,17 +140,6 @@ var AST_SimpleStatement = DEFNODE("SimpleStatement", "body", {
}
}, AST_Statement);
var AST_BlockStatement = DEFNODE("BlockStatement", "body", {
$documentation: "A block statement.",
_walk: function(visitor) {
return visitor._visit(this, function(){
this.body.forEach(function(stat){
stat._walk(visitor);
});
});
}
}, AST_Statement);
function walk_body(node, visitor) {
if (node.body instanceof AST_Statement) {
node.body._walk(visitor);
@@ -156,7 +150,10 @@ function walk_body(node, visitor) {
};
var AST_Block = DEFNODE("Block", "body", {
$documentation: "A block of statements (usually always bracketed)",
$documentation: "A body of statements (usually bracketed)",
$propdoc: {
body: "[AST_Statement*] an array of statements"
},
_walk: function(visitor) {
return visitor._visit(this, function(){
walk_body(this, visitor);
@@ -164,15 +161,22 @@ var AST_Block = DEFNODE("Block", "body", {
}
}, 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).",
$documentation: "The empty statement (empty block or simply a semicolon)",
_walk: function(visitor) {
return visitor._visit(this);
}
}, AST_Statement);
var AST_StatementWithBody = DEFNODE("StatementWithBody", "body", {
$documentation: "Base class for all statements that contain one nested body: `For`, `ForIn`, `Do`, `While`, `With`.",
$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"
},
_walk: function(visitor) {
return visitor._visit(this, function(){
this.body._walk(visitor);
@@ -182,6 +186,9 @@ var AST_StatementWithBody = DEFNODE("StatementWithBody", "body", {
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);
@@ -191,7 +198,10 @@ var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
}, AST_StatementWithBody);
var AST_DWLoop = DEFNODE("DWLoop", "condition", {
$documentation: "Base class for do/while statements.",
$documentation: "Base class for do/while statements",
$propdoc: {
condition: "[AST_Node] the loop condition. Should not be instanceof AST_Statement"
},
_walk: function(visitor) {
return visitor._visit(this, function(){
this.condition._walk(visitor);
@@ -201,15 +211,20 @@ var AST_DWLoop = DEFNODE("DWLoop", "condition", {
}, AST_StatementWithBody);
var AST_Do = DEFNODE("Do", null, {
$documentation: "A `do` statement"
$documentation: "A `do` statement",
}, AST_DWLoop);
var AST_While = DEFNODE("While", null, {
$documentation: "A `while` statement"
$documentation: "A `while` statement",
}, 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);
@@ -222,6 +237,11 @@ var AST_For = DEFNODE("For", "init condition step", {
var AST_ForIn = DEFNODE("ForIn", "init name object", {
$documentation: "A `for ... in` statement",
$propdoc: {
init: "[AST_Node] the `for/in` initialization code",
name: "[AST_SymbolRef?] the loop variable, only if `init` is AST_Var",
object: "[AST_Node] the object that we're looping through"
},
_walk: function(visitor) {
return visitor._visit(this, function(){
this.init._walk(visitor);
@@ -233,6 +253,9 @@ var AST_ForIn = DEFNODE("ForIn", "init name object", {
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);
@@ -245,14 +268,72 @@ var AST_With = DEFNODE("With", "expression", {
var AST_Scope = DEFNODE("Scope", "directives variables functions uses_with uses_eval parent_scope enclosed cname", {
$documentation: "Base class for all statements introducing a lexical scope",
$propdoc: {
directives: "[string*/S] an array of directives declared in this scope",
variables: "[Object/S] a map of name -> SymbolDef for all variables/functions defined in this scope",
functions: "[Object/S] like `variables`, but only lists function declarations",
uses_with: "[boolean/S] tells whether this scope uses the `with` statement",
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)",
},
}, AST_Block);
var AST_Toplevel = DEFNODE("Toplevel", "globals", {
$documentation: "The toplevel scope"
$documentation: "The toplevel scope",
$propdoc: {
globals: "[Object/S] a map of name -> SymbolDef for all undeclared names",
},
wrap_commonjs: function(name, export_all) {
var self = this;
if (export_all) {
self.figure_out_scope();
var to_export = [];
self.walk(new TreeWalker(function(node){
if (node instanceof AST_SymbolDeclaration && node.definition().global) {
if (!find_if(function(n){ return n.name == node.name }, to_export))
to_export.push(node);
}
}));
}
var wrapped_tl = "(function(exports, global){ global['" + name + "'] = exports; '$ORIG'; '$EXPORTS'; }({}, (function(){return this}())))";
wrapped_tl = parse(wrapped_tl);
wrapped_tl = wrapped_tl.transform(new TreeTransformer(function before(node){
if (node instanceof AST_SimpleStatement) {
node = node.body;
if (node instanceof AST_String) switch (node.getValue()) {
case "$ORIG":
return MAP.splice(self.body);
case "$EXPORTS":
var body = [];
to_export.forEach(function(sym){
body.push(new AST_SimpleStatement({
body: new AST_Assign({
left: new AST_Sub({
expression: new AST_SymbolRef({ name: "exports" }),
property: new AST_String({ value: sym.name }),
}),
operator: "=",
right: new AST_SymbolRef(sym),
}),
}));
});
return MAP.splice(body);
}
}
}));
return wrapped_tl;
}
}, AST_Scope);
var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments", {
$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);
@@ -264,6 +345,10 @@ var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments", {
}
}, AST_Scope);
var AST_Accessor = DEFNODE("Accessor", null, {
$documentation: "A setter/getter function"
}, AST_Lambda);
var AST_Function = DEFNODE("Function", null, {
$documentation: "A function expression"
}, AST_Lambda);
@@ -280,6 +365,9 @@ var AST_Jump = DEFNODE("Jump", null, {
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);
@@ -295,8 +383,11 @@ var AST_Throw = DEFNODE("Throw", null, {
$documentation: "A `throw` statement"
}, AST_Exit);
var AST_LoopControl = DEFNODE("LoopControl", "label loopcontrol_target", {
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);
@@ -316,6 +407,10 @@ var AST_Continue = DEFNODE("Continue", null, {
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);
@@ -327,15 +422,18 @@ var AST_If = DEFNODE("If", "condition alternative", {
/* -----[ SWITCH ]----- */
var AST_Switch = DEFNODE("Switch", "body expression", {
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_Statement);
}, AST_Block);
var AST_SwitchBranch = DEFNODE("SwitchBranch", null, {
$documentation: "Base class for `switch` branches",
@@ -347,6 +445,9 @@ var AST_Default = DEFNODE("Default", null, {
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);
@@ -359,6 +460,10 @@ var AST_Case = DEFNODE("Case", "expression", {
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);
@@ -376,6 +481,9 @@ var AST_Try = DEFNODE("Try", "bcatch bfinally", {
// AST_Scope.
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);
@@ -392,6 +500,9 @@ var AST_Finally = DEFNODE("Finally", null, {
var AST_Definitions = DEFNODE("Definitions", "definitions", {
$documentation: "Base class for `var` or `const` nodes (variable declarations/initializations)",
$propdoc: {
definitions: "[AST_VarDef*] array of variable definitions"
},
_walk: function(visitor) {
return visitor._visit(this, function(){
this.definitions.forEach(function(def){
@@ -411,6 +522,10 @@ var AST_Const = DEFNODE("Const", null, {
var AST_VarDef = DEFNODE("VarDef", "name value", {
$documentation: "A variable declaration; only appears in a AST_Definitions node",
$propdoc: {
name: "[AST_SymbolVar|AST_SymbolConst] 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);
@@ -423,6 +538,10 @@ var AST_VarDef = DEFNODE("VarDef", "name value", {
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);
@@ -434,11 +553,15 @@ var AST_Call = DEFNODE("Call", "expression args", {
});
var AST_New = DEFNODE("New", null, {
$documentation: "An object instantiation. Derives from a function call since it has exactly the same properties."
$documentation: "An object instantiation. Derives from a function call since it has exactly the same properties"
}, AST_Call);
var AST_Seq = DEFNODE("Seq", "car cdr", {
$documentation: "A sequence expression (two comma-separated expressions)",
$propdoc: {
car: "[AST_Node] first element in sequence",
cdr: "[AST_Node] second element in sequence"
},
$cons: function(x, y) {
var seq = new AST_Seq(x);
seq.car = x;
@@ -462,6 +585,18 @@ var AST_Seq = DEFNODE("Seq", "car cdr", {
}
return list;
},
to_array: function() {
var p = this, a = [];
while (p) {
a.push(p.car);
if (p.cdr && !(p.cdr instanceof AST_Seq)) {
a.push(p.cdr);
break;
}
p = p.cdr;
}
return a;
},
add: function(node) {
var p = this;
while (p) {
@@ -481,7 +616,11 @@ var AST_Seq = DEFNODE("Seq", "car cdr", {
});
var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
$documentation: "Base class for property access expressions, i.e. `a.foo` or `a[\"foo\"]`"
$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"
}
});
var AST_Dot = DEFNODE("Dot", null, {
@@ -505,6 +644,10 @@ var AST_Sub = DEFNODE("Sub", null, {
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);
@@ -522,6 +665,11 @@ var AST_UnaryPostfix = DEFNODE("UnaryPostfix", null, {
var AST_Binary = DEFNODE("Binary", "left operator 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);
@@ -532,6 +680,11 @@ var AST_Binary = DEFNODE("Binary", "left operator right", {
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);
@@ -541,7 +694,7 @@ var AST_Conditional = DEFNODE("Conditional", "condition consequent alternative",
}
});
var AST_Assign = DEFNODE("Assign", "left operator right", {
var AST_Assign = DEFNODE("Assign", null, {
$documentation: "An assignment expression — `a = b + 5`",
}, AST_Binary);
@@ -549,6 +702,9 @@ var AST_Assign = DEFNODE("Assign", "left operator right", {
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(el){
@@ -560,6 +716,9 @@ var AST_Array = DEFNODE("Array", "elements", {
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){
@@ -571,6 +730,10 @@ var AST_Object = DEFNODE("Object", "properties", {
var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", {
$documentation: "Base class for literal object properties",
$propdoc: {
key: "[string] the property name; it's always a plain string in our AST, no matter if it was a string, number or identifier in original code",
value: "[AST_Node] property value. For setters and getters this is an AST_Function."
},
_walk: function(visitor) {
return visitor._visit(this, function(){
this.value._walk(visitor);
@@ -578,7 +741,7 @@ var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", {
}
});
var AST_ObjectKeyVal = DEFNODE("ObjectKeyval", null, {
var AST_ObjectKeyVal = DEFNODE("ObjectKeyVal", null, {
$documentation: "A key: value object property",
}, AST_ObjectProperty);
@@ -591,11 +754,23 @@ var AST_ObjectGetter = DEFNODE("ObjectGetter", null, {
}, 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/const, function name or argument, symbol in catch)",
$propdoc: {
init: "[AST_Node*/S] array of initializers for this declaration."
}
}, AST_Symbol);
var AST_SymbolVar = DEFNODE("SymbolVar", null, {
@@ -622,9 +797,12 @@ var AST_SymbolCatch = DEFNODE("SymbolCatch", null, {
$documentation: "Symbol naming the exception in catch",
}, AST_SymbolDeclaration);
var AST_Label = DEFNODE("Label", "references label_target", {
var AST_Label = DEFNODE("Label", "references", {
$documentation: "Symbol naming a label (declaration)",
}, AST_SymbolDeclaration);
$propdoc: {
references: "[AST_LabelRef*] a list of nodes referring to this label"
}
}, AST_Symbol);
var AST_SymbolRef = DEFNODE("SymbolRef", null, {
$documentation: "Reference to some symbol (not definition/declaration)",
@@ -632,7 +810,7 @@ var AST_SymbolRef = DEFNODE("SymbolRef", null, {
var AST_LabelRef = DEFNODE("LabelRef", null, {
$documentation: "Reference to a label symbol",
}, AST_SymbolRef);
}, AST_Symbol);
var AST_This = DEFNODE("This", null, {
$documentation: "The `this` symbol",
@@ -647,16 +825,22 @@ var AST_Constant = DEFNODE("Constant", null, {
var AST_String = DEFNODE("String", "value", {
$documentation: "A string literal",
$propdoc: {
value: "[string] the contents of this string"
}
}, 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", "pattern mods", {
var AST_RegExp = DEFNODE("RegExp", "value", {
$documentation: "A regexp literal",
initialize: function() {
this.value = new RegExp(this.pattern, this.mods);
$propdoc: {
value: "[RegExp] the actual regexp"
}
}, AST_Constant);
@@ -679,6 +863,11 @@ var AST_Undefined = DEFNODE("Undefined", null, {
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);
@@ -748,4 +937,23 @@ TreeWalker.prototype = {
self = p;
}
},
loopcontrol_target: function(label) {
var stack = this.stack;
if (label) {
for (var i = stack.length; --i >= 0;) {
var x = stack[i];
if (x instanceof AST_LabeledStatement && x.label.name == label.name) {
return x.body;
}
}
} else {
for (var i = stack.length; --i >= 0;) {
var x = stack[i];
if (x instanceof AST_Switch
|| x instanceof AST_For
|| x instanceof AST_ForIn
|| x instanceof AST_DWLoop) return x;
}
}
}
};

View File

@@ -53,6 +53,7 @@ function Compressor(options, false_by_default) {
dead_code : !false_by_default,
drop_debugger : !false_by_default,
unsafe : !false_by_default,
unsafe_comps : false,
conditionals : !false_by_default,
comparisons : !false_by_default,
evaluate : !false_by_default,
@@ -80,7 +81,6 @@ merge(Compressor.prototype, {
},
before: function(node, descend, in_list) {
if (node._squeezed) return node;
node = node.clone();
if (node instanceof AST_Scope) {
node.drop_unused(this);
node = node.hoist_declarations(this);
@@ -103,7 +103,7 @@ merge(Compressor.prototype, {
}
});
(function(undefined){
(function(){
function OPT(node, optimizer) {
node.DEFMETHOD("optimize", function(compressor){
@@ -135,6 +135,17 @@ merge(Compressor.prototype, {
};
function make_node_from_constant(compressor, val, orig) {
// XXX: WIP.
// if (val instanceof AST_Node) return val.transform(new TreeTransformer(null, function(node){
// if (node instanceof AST_SymbolRef) {
// var scope = compressor.find_parent(AST_Scope);
// var def = scope.find_variable(node);
// node.thedef = def;
// return node;
// }
// })).transform(compressor);
if (val instanceof AST_Node) return val.transform(compressor);
switch (typeof val) {
case "string":
return make_node(AST_String, orig, {
@@ -152,6 +163,9 @@ merge(Compressor.prototype, {
if (val === null) {
return make_node(AST_Null, orig).optimize(compressor);
}
if (val instanceof RegExp) {
return make_node(AST_RegExp, orig).optimize(compressor);
}
throw new Error(string_template("Can't handle constant of type: {type}", {
type: typeof val
}));
@@ -173,6 +187,14 @@ merge(Compressor.prototype, {
return false;
};
function loop_body(x) {
if (x instanceof AST_Switch) return x;
if (x instanceof AST_For || x instanceof AST_ForIn || x instanceof AST_DWLoop) {
return (x.body instanceof AST_BlockStatement ? x.body : x);
}
return x;
};
function tighten_body(statements, compressor) {
var CHANGED;
do {
@@ -194,12 +216,20 @@ merge(Compressor.prototype, {
return statements;
function eliminate_spurious_blocks(statements) {
var seen_dirs = [];
return statements.reduce(function(a, stat){
if (stat instanceof AST_BlockStatement) {
CHANGED = true;
a.push.apply(a, eliminate_spurious_blocks(stat.body));
} else if (stat instanceof AST_EmptyStatement) {
CHANGED = true;
} else if (stat instanceof AST_Directive) {
if (seen_dirs.indexOf(stat.value) < 0) {
a.push(stat);
seen_dirs.push(stat.value);
} else {
CHANGED = true;
}
} else {
a.push(stat);
}
@@ -282,8 +312,13 @@ merge(Compressor.prototype, {
}
var ab = aborts(stat.body);
var lct = ab instanceof AST_LoopControl ? compressor.loopcontrol_target(ab.label) : null;
if (ab && ((ab instanceof AST_Return && !ab.value && in_lambda)
|| (ab instanceof AST_Continue && self === ab.target()))) {
|| (ab instanceof AST_Continue && self === loop_body(lct))
|| (ab instanceof AST_Break && lct instanceof AST_BlockStatement && self === lct))) {
if (ab.label) {
remove(ab.label.thedef.references, ab.label);
}
CHANGED = true;
var body = as_statement_array(stat.body).slice(0, -1);
stat = stat.clone();
@@ -299,8 +334,13 @@ merge(Compressor.prototype, {
}
var ab = aborts(stat.alternative);
var lct = ab instanceof AST_LoopControl ? compressor.loopcontrol_target(ab.label) : null;
if (ab && ((ab instanceof AST_Return && !ab.value && in_lambda)
|| (ab instanceof AST_Continue && self === ab.target()))) {
|| (ab instanceof AST_Continue && self === loop_body(lct))
|| (ab instanceof AST_Break && lct instanceof AST_BlockStatement && self === lct))) {
if (ab.label) {
remove(ab.label.thedef.references, ab.label);
}
CHANGED = true;
stat = stat.clone();
stat.body = make_node(AST_BlockStatement, stat.body, {
@@ -326,14 +366,27 @@ merge(Compressor.prototype, {
function eliminate_dead_code(statements, compressor) {
var has_quit = false;
var orig = statements.length;
var self = compressor.self();
statements = statements.reduce(function(a, stat){
if (has_quit) {
extract_declarations_from_unreachable_code(compressor, stat, a);
} else {
a.push(stat);
if (stat instanceof AST_Jump) {
has_quit = true;
if (stat instanceof AST_LoopControl) {
var lct = compressor.loopcontrol_target(stat.label);
if ((stat instanceof AST_Break
&& lct instanceof AST_BlockStatement
&& loop_body(lct) === self) || (stat instanceof AST_Continue
&& loop_body(lct) === self)) {
if (stat.label) {
remove(stat.label.thedef.references, stat.label);
}
} else {
a.push(stat);
}
} else {
a.push(stat);
}
if (aborts(stat)) has_quit = true;
}
return a;
}, []);
@@ -375,12 +428,23 @@ merge(Compressor.prototype, {
var ret = [], prev = null;
statements.forEach(function(stat){
if (prev) {
if (stat instanceof AST_For && stat.init && !(stat.init instanceof AST_Definitions)) {
stat.init = cons_seq(stat.init);
}
else if (stat instanceof AST_For && !stat.init) {
stat.init = prev.body;
ret.pop();
if (stat instanceof AST_For) {
var opera = {};
try {
prev.body.walk(new TreeWalker(function(node){
if (node instanceof AST_Binary && node.operator == "in")
throw opera;
}));
if (stat.init && !(stat.init instanceof AST_Definitions)) {
stat.init = cons_seq(stat.init);
}
else if (!stat.init) {
stat.init = prev.body;
ret.pop();
}
} catch(ex) {
if (ex !== opera) throw ex;
}
}
else if (stat instanceof AST_If) {
stat.condition = cons_seq(stat.condition);
@@ -439,7 +503,6 @@ merge(Compressor.prototype, {
stat.walk(new TreeWalker(function(node){
if (node instanceof AST_Definitions) {
compressor.warn("Declarations in unreachable code! [{file}:{line},{col}]", node.start);
node = node.clone();
node.remove_initializers();
target.push(node);
return true;
@@ -491,12 +554,24 @@ merge(Compressor.prototype, {
def(AST_UnaryPrefix, function(){
return this.operator == "typeof";
});
def(AST_Binary, function(){
def(AST_Binary, function(compressor){
return this.operator == "+" &&
(this.left.is_string() || this.right.is_string());
(this.left.is_string(compressor) || this.right.is_string(compressor));
});
def(AST_Assign, function(){
return this.operator == "=" && this.right.is_string();
def(AST_Assign, function(compressor){
return (this.operator == "=" || this.operator == "+=") && this.right.is_string(compressor);
});
def(AST_Seq, function(compressor){
return this.cdr.is_string(compressor);
});
def(AST_Conditional, function(compressor){
return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);
});
def(AST_Call, function(compressor){
return compressor.option("unsafe")
&& this.expression instanceof AST_SymbolRef
&& this.expression.name == "String"
&& this.expression.undeclared();
});
})(function(node, func){
node.DEFMETHOD("is_string", func);
@@ -528,7 +603,14 @@ merge(Compressor.prototype, {
}
});
def(AST_Statement, function(){
throw new Error("Cannot evaluate a statement");
throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));
});
def(AST_Function, function(){
// XXX: AST_Function inherits from AST_Scope, which itself
// inherits from AST_Statement; however, an AST_Function
// isn't really a statement. This could byte in other
// places too. :-( Wish JS had multiple inheritance.
return [ this ];
});
function ev(node) {
return node._eval();
@@ -634,7 +716,7 @@ merge(Compressor.prototype, {
});
def(AST_Binary, function(compressor){
var self = this.clone(), op = this.operator;
if (compressor.option("comparisons") && compressor.option("unsafe")) {
if (compressor.option("unsafe_comps")) {
switch (op) {
case "<=" : self.operator = ">" ; return self;
case "<" : self.operator = ">=" ; return self;
@@ -673,8 +755,8 @@ merge(Compressor.prototype, {
def(AST_EmptyStatement, function(){ return false });
def(AST_Constant, function(){ return false });
def(AST_This, function(){ return false });
def(AST_Function, function(){ return false });
def(AST_BlockStatement, function(){
def(AST_Block, function(){
for (var i = this.body.length; --i >= 0;) {
if (this.body[i].has_side_effects())
return true;
@@ -685,6 +767,8 @@ merge(Compressor.prototype, {
def(AST_SimpleStatement, function(){
return this.body.has_side_effects();
});
def(AST_Defun, function(){ return true });
def(AST_Function, function(){ return false });
def(AST_Binary, function(){
return this.left.has_side_effects()
|| this.right.has_side_effects();
@@ -742,9 +826,14 @@ merge(Compressor.prototype, {
(function(def){
def(AST_Statement, function(){ return null });
def(AST_Jump, function(){ return this });
def(AST_BlockStatement, function(){
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);
});
})(function(node, func){
node.DEFMETHOD("aborts", func);
@@ -766,9 +855,18 @@ merge(Compressor.prototype, {
});
OPT(AST_LabeledStatement, function(self, compressor){
if (self.body instanceof AST_Break
&& compressor.loopcontrol_target(self.body.label) === self.body) {
return make_node(AST_EmptyStatement, self);
}
return self.label.references.length == 0 ? self.body : self;
});
OPT(AST_Block, function(self, compressor){
self.body = tighten_body(self.body, compressor);
return self;
});
OPT(AST_BlockStatement, function(self, compressor){
self.body = tighten_body(self.body, compressor);
switch (self.body.length) {
@@ -778,16 +876,6 @@ merge(Compressor.prototype, {
return self;
});
OPT(AST_Block, function(self, compressor){
self.body = tighten_body(self.body, compressor);
return self;
});
OPT(AST_Scope, function(self, compressor){
self.body = tighten_body(self.body, compressor);
return self;
});
AST_Scope.DEFMETHOD("drop_unused", function(compressor){
var self = this;
if (compressor.option("unused")
@@ -811,7 +899,7 @@ merge(Compressor.prototype, {
});
return true;
}
if (node instanceof AST_SymbolRef && !(node instanceof AST_LabelRef)) {
if (node instanceof AST_SymbolRef) {
push_uniq(in_use, node.definition());
return true;
}
@@ -834,8 +922,7 @@ merge(Compressor.prototype, {
if (decl instanceof AST_SymbolDeclaration) {
decl.init.forEach(function(init){
var tw = new TreeWalker(function(node){
if (node instanceof AST_SymbolRef
&& node.definition().scope.$self === self.$self) {
if (node instanceof AST_SymbolRef) {
push_uniq(in_use, node.definition());
}
});
@@ -846,7 +933,22 @@ merge(Compressor.prototype, {
}
// pass 3: we should drop declarations not in_use
var tt = new TreeTransformer(
function before(node, descend) {
function before(node, descend, in_list) {
if (node instanceof AST_Lambda) {
for (var a = node.argnames, i = a.length; --i >= 0;) {
var sym = a[i];
if (sym.unreferenced()) {
a.pop();
compressor.warn("Dropping unused function argument {name} [{file}:{line},{col}]", {
name : sym.name,
file : sym.start.file,
line : sym.start.line,
col : sym.start.col
});
}
else break;
}
}
if (node instanceof AST_Defun && node !== self) {
if (!member(node.name.definition(), in_use)) {
compressor.warn("Dropping unused function {name} [{file}:{line},{col}]", {
@@ -922,6 +1024,19 @@ merge(Compressor.prototype, {
}
return node;
}
if (node instanceof AST_For && node.init instanceof AST_BlockStatement) {
descend(node, this);
// certain combination of unused name + side effect leads to:
// https://github.com/mishoo/UglifyJS2/issues/44
// that's an invalid AST.
// We fix it at this stage by moving the `var` outside the `for`.
var body = node.init.body.slice(0, -1);
node.init = node.init.body.slice(-1)[0].body;
body.push(node);
return in_list ? MAP.splice(body) : make_node(AST_BlockStatement, node, {
body: body
});
}
if (node instanceof AST_Scope && node !== self)
return node;
}
@@ -937,7 +1052,7 @@ merge(Compressor.prototype, {
if (hoist_funs || hoist_vars) {
var dirs = [];
var hoisted = [];
var vars = {}, vars_found = 0, var_decl = 0;
var vars = new Dictionary(), vars_found = 0, var_decl = 0;
// let's count var_decl first, we seem to waste a lot of
// space if we hoist `var` when there's only one.
self.walk(new TreeWalker(function(node){
@@ -962,7 +1077,7 @@ merge(Compressor.prototype, {
}
if (node instanceof AST_Var && hoist_vars) {
node.definitions.forEach(function(def){
vars[def.name.name] = def;
vars.set(def.name.name, def);
++vars_found;
});
var seq = node.to_assignments();
@@ -986,8 +1101,8 @@ merge(Compressor.prototype, {
);
self = self.transform(tt);
if (vars_found > 0) hoisted.unshift(make_node(AST_Var, self, {
definitions: Object.keys(vars).map(function(name){
var def = vars[name].clone();
definitions: vars.map(function(def){
def = def.clone();
def.value = null;
return def;
})
@@ -1029,6 +1144,61 @@ merge(Compressor.prototype, {
return self;
});
function if_break_in_loop(self, compressor) {
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);
}
if_break_in_loop(self, compressor);
}
var first = self.body instanceof AST_BlockStatement ? self.body.body[0] : self.body;
if (first instanceof AST_If) {
if (first.body instanceof AST_Break
&& compressor.loopcontrol_target(first.body.label) === self) {
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 (first.alternative instanceof AST_Break
&& compressor.loopcontrol_target(first.alternative.label) === self) {
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);
}
}
};
OPT(AST_While, function(self, compressor) {
if (!compressor.option("loops")) return self;
self = AST_DWLoop.prototype.optimize.call(self, compressor);
if (self instanceof AST_While) {
if_break_in_loop(self, compressor);
self = make_node(AST_For, self, self).transform(compressor);
}
return self;
});
OPT(AST_For, function(self, compressor){
var cond = self.condition;
if (cond) {
@@ -1053,6 +1223,7 @@ merge(Compressor.prototype, {
}
}
}
if_break_in_loop(self, compressor);
return self;
});
@@ -1073,7 +1244,7 @@ merge(Compressor.prototype, {
extract_declarations_from_unreachable_code(compressor, self.alternative, a);
}
a.push(self.body);
return make_node(AST_BlockStatement, self, { body: a });
return make_node(AST_BlockStatement, self, { body: a }).transform(compressor);
}
} else {
compressor.warn("Condition always false [{file}:{line},{col}]", self.condition.start);
@@ -1081,7 +1252,7 @@ merge(Compressor.prototype, {
var a = [];
extract_declarations_from_unreachable_code(compressor, self.body, a);
if (self.alternative) a.push(self.alternative);
return make_node(AST_BlockStatement, self, { body: a });
return make_node(AST_BlockStatement, self, { body: a }).transform(compressor);
}
}
}
@@ -1098,7 +1269,7 @@ merge(Compressor.prototype, {
if (is_empty(self.body) && is_empty(self.alternative)) {
return make_node(AST_SimpleStatement, self.condition, {
body: self.condition
});
}).transform(compressor);
}
if (self.body instanceof AST_SimpleStatement
&& self.alternative instanceof AST_SimpleStatement) {
@@ -1108,7 +1279,7 @@ merge(Compressor.prototype, {
consequent : self.body.body,
alternative : self.alternative.body
})
});
}).transform(compressor);
}
if (is_empty(self.alternative) && self.body instanceof AST_SimpleStatement) {
if (negated_is_best) return make_node(AST_SimpleStatement, self, {
@@ -1117,14 +1288,14 @@ merge(Compressor.prototype, {
left : negated,
right : self.body.body
})
});
}).transform(compressor);
return make_node(AST_SimpleStatement, self, {
body: make_node(AST_Binary, self, {
operator : "&&",
left : self.condition,
right : self.body.body
})
});
}).transform(compressor);
}
if (self.body instanceof AST_EmptyStatement
&& self.alternative
@@ -1135,7 +1306,7 @@ merge(Compressor.prototype, {
left : self.condition,
right : self.alternative.body
})
});
}).transform(compressor);
}
if (self.body instanceof AST_Exit
&& self.alternative instanceof AST_Exit
@@ -1143,10 +1314,10 @@ merge(Compressor.prototype, {
return make_node(self.body.CTOR, self, {
value: make_node(AST_Conditional, self, {
condition : self.condition,
consequent : self.body.value,
alternative : self.alternative.value || make_node(AST_Undefined, self).optimize(compressor)
consequent : self.body.value || make_node(AST_Undefined, self.body).optimize(compressor),
alternative : self.alternative.value || make_node(AST_Undefined, self.alternative).optimize(compressor)
})
});
}).transform(compressor);
}
if (self.body instanceof AST_If
&& !self.body.alternative
@@ -1164,7 +1335,7 @@ merge(Compressor.prototype, {
self.alternative = null;
return make_node(AST_BlockStatement, self, {
body: [ self, alt ]
});
}).transform(compressor);
}
}
if (aborts(self.alternative)) {
@@ -1174,18 +1345,96 @@ merge(Compressor.prototype, {
self.alternative = null;
return make_node(AST_BlockStatement, self, {
body: [ self, body ]
});
}).transform(compressor);
}
return self;
});
OPT(AST_Switch, function(self, compressor){
if (self.body.length == 0 && compressor.option("conditionals")) {
return make_node(AST_SimpleStatement, self, {
body: self.expression
}).transform(compressor);
}
var last_branch = self.body[self.body.length - 1];
if (last_branch) {
var stat = last_branch.body[last_branch.body.length - 1]; // last statement
if (stat instanceof AST_Break && !stat.label)
if (stat instanceof AST_Break && loop_body(compressor.loopcontrol_target(stat.label)) === self)
last_branch.body.pop();
}
var exp = self.expression.evaluate(compressor);
out: if (exp.length == 2) try {
// constant expression
self.expression = exp[0];
if (!compressor.option("dead_code")) break out;
var value = exp[1];
var in_if = false;
var in_block = false;
var started = false;
var stopped = false;
var ruined = false;
var tt = new TreeTransformer(function(node, descend, in_list){
if (node instanceof AST_Lambda || node instanceof AST_SimpleStatement) {
// no need to descend these node types
return node;
}
else if (node instanceof AST_Switch && node === self) {
node = node.clone();
descend(node, this);
return ruined ? node : make_node(AST_BlockStatement, node, {
body: node.body.reduce(function(a, branch){
return a.concat(branch.body);
}, [])
}).transform(compressor);
}
else if (node instanceof AST_If || node instanceof AST_Try) {
var save = in_if;
in_if = !in_block;
descend(node, this);
in_if = save;
return node;
}
else if (node instanceof AST_StatementWithBody || node instanceof AST_Switch) {
var save = in_block;
in_block = true;
descend(node, this);
in_block = save;
return node;
}
else if (node instanceof AST_Break && this.loopcontrol_target(node.label) === self) {
if (in_if) {
ruined = true;
return node;
}
if (in_block) return node;
stopped = true;
return in_list ? MAP.skip : make_node(AST_EmptyStatement, node);
}
else if (node instanceof AST_SwitchBranch && this.parent() === self) {
if (stopped) return MAP.skip;
if (node instanceof AST_Case) {
var exp = node.expression.evaluate(compressor);
if (exp.length < 2) {
// got a case with non-constant expression, baling out
throw self;
}
if (exp[1] === value || started) {
started = true;
if (aborts(node)) stopped = true;
descend(node, this);
return node;
}
return MAP.skip;
}
descend(node, this);
return node;
}
});
tt.stack = compressor.stack; // so that's able to see parent nodes
self = self.transform(tt);
} catch(ex) {
if (ex !== self) throw ex;
}
return self;
});
@@ -1200,11 +1449,7 @@ merge(Compressor.prototype, {
});
AST_Definitions.DEFMETHOD("remove_initializers", function(){
this.definitions = this.definitions.map(function(def){
def = def.clone();
def.value = null;
return def;
});
this.definitions.forEach(function(def){ def.value = null });
});
AST_Definitions.DEFMETHOD("to_assignments", function(){
@@ -1220,14 +1465,7 @@ merge(Compressor.prototype, {
return a;
}, []);
if (assignments.length == 0) return null;
return (function seq(list){
var first = list[0];
if (list.length == 1) return first;
return make_node(AST_Seq, first, {
car: first,
cdr: seq(list.slice(1))
});
})(assignments);
return AST_Seq.from_array(assignments);
});
OPT(AST_Definitions, function(self, compressor){
@@ -1275,10 +1513,17 @@ merge(Compressor.prototype, {
}
else if (exp instanceof AST_Dot && exp.property == "toString" && self.args.length == 0) {
return make_node(AST_Binary, self, {
left: exp.expression,
left: make_node(AST_String, self, { value: "" }),
operator: "+",
right: make_node(AST_String, self, { value: "" })
});
right: exp.expression
}).transform(compressor);
}
}
if (compressor.option("side_effects")) {
if (self.expression instanceof AST_Function
&& self.args.length == 0
&& !AST_Block.prototype.has_side_effects.call(self.expression)) {
return make_node(AST_Undefined, self).transform(compressor);
}
}
return self;
@@ -1302,6 +1547,21 @@ merge(Compressor.prototype, {
});
OPT(AST_Seq, function(self, compressor){
if (!compressor.option("side_effects"))
return self;
if (!self.car.has_side_effects()) {
// we shouldn't compress (1,eval)(something) to
// eval(something) because that changes the meaning of
// eval (becomes lexical instead of global).
var p;
if (!(self.cdr instanceof AST_SymbolRef
&& self.cdr.name == "eval"
&& self.cdr.undeclared()
&& (p = compressor.parent()) instanceof AST_Call
&& p.expression === self)) {
return self.cdr;
}
}
if (compressor.option("cascade")) {
if (self.car instanceof AST_Assign
&& !self.car.left.has_side_effects()
@@ -1317,7 +1577,26 @@ merge(Compressor.prototype, {
return self;
});
AST_Unary.DEFMETHOD("lift_sequences", function(compressor){
if (compressor.option("sequences")) {
if (this.expression instanceof AST_Seq) {
var seq = this.expression;
var x = seq.to_array();
this.expression = x.pop();
x.push(this);
seq = AST_Seq.from_array(x).transform(compressor);
return seq;
}
}
return this;
});
OPT(AST_UnaryPostfix, function(self, compressor){
return self.lift_sequences(compressor);
});
OPT(AST_UnaryPrefix, function(self, compressor){
self = self.lift_sequences(compressor);
var e = self.expression;
if (compressor.option("booleans") && compressor.in_boolean_context()) {
switch (self.operator) {
@@ -1333,39 +1612,69 @@ merge(Compressor.prototype, {
compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
return make_node(AST_True, self);
}
}
if (e instanceof AST_Binary) {
self = best_of(self, e.negate(compressor));
if (e instanceof AST_Binary && self.operator == "!") {
self = best_of(self, e.negate(compressor));
}
}
return self.evaluate(compressor)[0];
});
AST_Binary.DEFMETHOD("lift_sequences", function(compressor){
if (compressor.option("sequences")) {
if (this.left instanceof AST_Seq) {
var seq = this.left;
var x = seq.to_array();
this.left = x.pop();
x.push(this);
seq = AST_Seq.from_array(x).transform(compressor);
return seq;
}
if (this.right instanceof AST_Seq
&& !(this.operator == "||" || this.operator == "&&")
&& !this.left.has_side_effects()) {
var seq = this.right;
var x = seq.to_array();
this.right = x.pop();
x.push(this);
seq = AST_Seq.from_array(x).transform(compressor);
return seq;
}
}
return this;
});
var commutativeOperators = makePredicate("== === != !== * & | ^");
OPT(AST_Binary, function(self, compressor){
function reverse(op) {
if (!(self.left.has_side_effects() && self.right.has_side_effects())) {
if (op) self.operator = op;
var tmp = self.left;
self.left = self.right;
self.right = tmp;
}
};
if (commutativeOperators(self.operator)) {
if (self.right instanceof AST_Constant
&& !(self.left instanceof AST_Constant)) {
reverse();
}
}
self = self.lift_sequences(compressor);
if (compressor.option("comparisons")) switch (self.operator) {
case "===":
case "!==":
if ((self.left.is_string() && self.right.is_string()) ||
if ((self.left.is_string(compressor) && self.right.is_string(compressor)) ||
(self.left.is_boolean() && self.right.is_boolean())) {
self.operator = self.operator.substr(0, 2);
}
// XXX: intentionally falling down to the next case
case "==":
case "!=":
if (self.left instanceof AST_UnaryPrefix
&& self.left.operator == "typeof"
&& self.right instanceof AST_String
&& self.right.value == "undefined") {
if (!(self.left.expression instanceof AST_SymbolRef)
|| !self.left.expression.undeclared()) {
self.left = self.left.expression;
self.right = make_node(AST_Undefined, self.right).optimize(compressor);
if (self.operator.length == 2) self.operator += "=";
}
}
else if (self.left instanceof AST_String
&& self.left.value == "undefined"
&& self.right instanceof AST_UnaryPrefix
&& self.right.operator == "typeof") {
if (self.left instanceof AST_String
&& self.left.value == "undefined"
&& self.right instanceof AST_UnaryPrefix
&& self.right.operator == "typeof") {
if (!(self.right.expression instanceof AST_SymbolRef)
|| !self.right.expression.undeclared()) {
self.left = self.right.expression;
@@ -1428,24 +1737,23 @@ merge(Compressor.prototype, {
});
self = best_of(self, negated);
}
var reverse = function(op) {
self.operator = op;
var tmp = self.left;
self.left = self.right;
self.right = tmp;
};
switch (self.operator) {
case "<": reverse(">"); break;
case "<=": reverse(">="); break;
}
}
if (self.operator == "+" && self.right instanceof AST_String
&& self.right.getValue() === "" && self.left instanceof AST_Binary
&& self.left.operator == "+" && self.left.is_string()) {
return self.left;
}
return self;
});
OPT(AST_SymbolRef, function(self, compressor){
if (self.undeclared()) {
var defines = compressor.option("global_defs");
if (defines && HOP(defines, self.name)) {
if (defines && defines.hasOwnProperty(self.name)) {
return make_node_from_constant(compressor, defines[self.name], self);
}
switch (self.name) {
@@ -1453,6 +1761,8 @@ merge(Compressor.prototype, {
return make_node(AST_Undefined, self);
case "NaN":
return make_node(AST_NaN, self);
case "Infinity":
return make_node(AST_Infinity, self);
}
}
return self;
@@ -1477,6 +1787,7 @@ merge(Compressor.prototype, {
var ASSIGN_OPS = [ '+', '-', '/', '*', '%', '>>', '<<', '>>>', '|', '^', '&' ];
OPT(AST_Assign, function(self, compressor){
self = self.lift_sequences(compressor);
if (self.operator == "="
&& self.left instanceof AST_SymbolRef
&& self.right instanceof AST_Binary
@@ -1580,4 +1891,14 @@ merge(Compressor.prototype, {
return self;
});
function literals_in_boolean_context(self, compressor) {
if (compressor.option("booleans") && compressor.in_boolean_context()) {
return make_node(AST_True, self);
}
return self;
};
OPT(AST_Array, literals_in_boolean_context);
OPT(AST_Object, literals_in_boolean_context);
OPT(AST_RegExp, literals_in_boolean_context);
})();

View File

@@ -58,7 +58,7 @@
CatchClause : function(M) {
return new AST_Catch({
start : my_start_token(M),
end : my_start_token(M),
end : my_end_token(M),
argname : from_moz(M.param),
body : from_moz(M.body).body
});
@@ -95,7 +95,7 @@
MemberExpression : function(M) {
return new (M.computed ? AST_Sub : AST_Dot)({
start : my_start_token(M),
end : my_start_token(M),
end : my_end_token(M),
property : M.computed ? from_moz(M.property) : M.property.name,
expression : from_moz(M.object)
});
@@ -103,7 +103,7 @@
SwitchCase : function(M) {
return new (M.test ? AST_Case : AST_Default)({
start : my_start_token(M),
end : my_start_token(M),
end : my_end_token(M),
expression : from_moz(M.test),
body : M.consequent.map(from_moz)
});
@@ -125,9 +125,6 @@
return new (val ? AST_True : AST_False)(args);
default:
args.value = val;
var m = /\/(.*)\/(.*)/.exec(val+"");
args.pattern = m[1];
args.mods = m[2];
return new AST_RegExp(args);
}
},
@@ -235,8 +232,6 @@
moz_to_me += moz;
} else if (how == "%") {
moz_to_me += "from_moz(" + moz + ").body";
} else if (how == "@>") {
moz_to_me += "from_moz(" + moz + "[0])";
} else throw new Error("Can't understand operator in propmap: " + prop);
});
moz_to_me += "\n})}";

View File

@@ -58,7 +58,9 @@ function OutputStream(options) {
beautify : false,
source_map : null,
bracketize : false,
comments : false
semicolons : true,
comments : false,
preserve_line : false
}, true);
var indentation = 0;
@@ -130,20 +132,41 @@ function OutputStream(options) {
print("\n");
};
var requireSemicolonChars = makePredicate("( [ + * / - , .");
function print(str) {
str = String(str);
var ch = str.charAt(0);
if (might_need_semicolon) {
if (";}".indexOf(ch) < 0 && !/[;]$/.test(last)) {
OUTPUT += ";";
current_col++;
current_pos++;
if ((!ch || ";}".indexOf(ch) < 0) && !/[;]$/.test(last)) {
if (options.semicolons || requireSemicolonChars(ch)) {
OUTPUT += ";";
current_col++;
current_pos++;
} else {
OUTPUT += "\n";
current_pos++;
current_line++;
current_col = 0;
}
if (!options.beautify)
might_need_space = false;
}
might_need_semicolon = false;
maybe_newline();
}
if (!options.beautify && options.preserve_line && stack[stack.length - 1]) {
var target_line = stack[stack.length - 1].start.line;
while (current_line < target_line) {
OUTPUT += "\n";
current_pos++;
current_line++;
current_col = 0;
might_need_space = false;
}
}
if (might_need_space) {
var prev = last_char();
if ((is_identifier_char(prev)
@@ -248,12 +271,23 @@ function OutputStream(options) {
};
var add_mapping = options.source_map ? function(token, name) {
options.source_map.add(
token.file,
current_line, current_col,
token.line, token.col,
(!name && token.type == "name") ? token.value : name
);
try {
if (token) options.source_map.add(
token.file || "?",
current_line, current_col,
token.line, token.col,
(!name && token.type == "name") ? token.value : name
);
} catch(ex) {
AST_Node.warn("Couldn't figure out mapping for {file}:{line},{col} → {cline},{ccol} [{name}]", {
file: token.file,
line: token.line,
col: token.col,
cline: current_line,
ccol: current_col,
name: name || ""
})
}
} : noop;
function get() {
@@ -276,6 +310,7 @@ function OutputStream(options) {
last : function() { return last },
semicolon : semicolon,
force_semicolon : force_semicolon,
to_ascii : to_ascii,
print_name : function(name) { print(make_name(name)) },
print_string : function(str) { print(encode_string(str)) },
next_indent : next_indent,
@@ -388,10 +423,16 @@ function OutputStream(options) {
return first_in_statement(output);
});
PARENS(AST_Unary, function(output){
var p = output.parent();
return p instanceof AST_PropAccess && p.expression === this;
});
PARENS(AST_Seq, function(output){
var p = output.parent();
return p instanceof AST_Call // (foo, bar)() or foo(1, (2, 3), 4)
|| p instanceof AST_Binary // 1 + (2, 3) + 4 ==> 7
|| 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_Dot // (1, {foo:2}).foo ==> 2
|| p instanceof AST_Array // [ 1, (2, 3), 4 ] ==> [ 1, 3, 4 ]
@@ -440,10 +481,36 @@ function OutputStream(options) {
}
});
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.
try {
this.walk(new TreeWalker(function(node){
if (node instanceof AST_Call) throw p;
}));
} catch(ex) {
if (ex !== p) throw ex;
return true;
}
}
});
PARENS(AST_Call, function(output){
var p = output.parent();
return p instanceof AST_New && p.expression === this;
});
PARENS(AST_New, function(output){
var p = output.parent();
// (new Date).getTime();
if (p instanceof AST_Dot && no_constructor_parens(this, output))
if (no_constructor_parens(this, output)
&& (p instanceof AST_Dot // (new Date).getTime()
|| p instanceof AST_Call && p.expression === this)) // (new foo)(bar)
return true;
});
@@ -506,6 +573,7 @@ function OutputStream(options) {
});
DEFPRINT(AST_Toplevel, function(self, output){
display_body(self.body, true, output);
output.print("");
});
DEFPRINT(AST_LabeledStatement, function(self, output){
self.label.print(output);
@@ -857,6 +925,8 @@ function OutputStream(options) {
}
}
output.print(".");
// the name after dot would be mapped about here.
output.add_mapping(self.end);
output.print_name(self.property);
});
DEFPRINT(AST_Sub, function(self, output){
@@ -951,9 +1021,10 @@ function OutputStream(options) {
output.print_name(def ? def.mangled_name || def.name : self.name);
});
DEFPRINT(AST_Undefined, function(self, output){
// XXX: should add more options for this
output.print("void 0");
//output.print("[][0]");
});
DEFPRINT(AST_Infinity, function(self, output){
output.print("1/0");
});
DEFPRINT(AST_NaN, function(self, output){
output.print("0/0");
@@ -971,10 +1042,10 @@ function OutputStream(options) {
output.print(make_num(self.getValue()));
});
DEFPRINT(AST_RegExp, function(self, output){
output.print("/");
output.print(self.pattern);
output.print("/");
if (self.mods) output.print(self.mods);
var str = self.getValue().toString();
if (output.option("ascii_only"))
str = output.to_ascii(str);
output.print(str);
});
function force_statement(stat, output) {
@@ -1092,11 +1163,13 @@ function OutputStream(options) {
DEFMAP(AST_Symbol, basic_sourcemap_gen);
DEFMAP(AST_Jump, basic_sourcemap_gen);
DEFMAP(AST_StatementWithBody, basic_sourcemap_gen);
DEFMAP(AST_LabeledStatement, noop); // since the label symbol will mark it
DEFMAP(AST_Lambda, basic_sourcemap_gen);
DEFMAP(AST_PropAccess, basic_sourcemap_gen);
DEFMAP(AST_Switch, basic_sourcemap_gen);
DEFMAP(AST_SwitchBranch, basic_sourcemap_gen);
DEFMAP(AST_BlockStatement, basic_sourcemap_gen);
DEFMAP(AST_Toplevel, noop);
DEFMAP(AST_New, basic_sourcemap_gen);
DEFMAP(AST_Try, basic_sourcemap_gen);
DEFMAP(AST_Catch, basic_sourcemap_gen);
DEFMAP(AST_Finally, basic_sourcemap_gen);

View File

@@ -44,89 +44,24 @@
"use strict";
var KEYWORDS = array_to_hash([
"break",
"case",
"catch",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"else",
"finally",
"for",
"function",
"if",
"in",
"instanceof",
"new",
"return",
"switch",
"throw",
"try",
"typeof",
"var",
"void",
"while",
"with"
]);
var KEYWORDS = 'break case catch const continue debugger default delete do else finally for function if in instanceof new return switch throw try typeof var void while with';
var KEYWORDS_ATOM = 'false null true';
var RESERVED_WORDS = 'abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized this throws transient volatile'
+ " " + KEYWORDS_ATOM + " " + KEYWORDS;
var KEYWORDS_BEFORE_EXPRESSION = 'return new delete throw else case';
var RESERVED_WORDS = array_to_hash([
"abstract",
"boolean",
"byte",
"char",
"class",
"double",
"enum",
"export",
"extends",
"final",
"float",
"goto",
"implements",
"import",
"int",
"interface",
"long",
"native",
"package",
"private",
"protected",
"public",
"short",
"static",
"super",
"synchronized",
"throws",
"transient",
"volatile"
]);
KEYWORDS = makePredicate(KEYWORDS);
RESERVED_WORDS = makePredicate(RESERVED_WORDS);
KEYWORDS_BEFORE_EXPRESSION = makePredicate(KEYWORDS_BEFORE_EXPRESSION);
KEYWORDS_ATOM = makePredicate(KEYWORDS_ATOM);
var KEYWORDS_BEFORE_EXPRESSION = array_to_hash([
"return",
"new",
"delete",
"throw",
"else",
"case"
]);
var KEYWORDS_ATOM = array_to_hash([
"false",
"null",
"true"
]);
var OPERATOR_CHARS = array_to_hash(characters("+-*&%=<>!?|~^"));
var OPERATOR_CHARS = makePredicate(characters("+-*&%=<>!?|~^"));
var RE_HEX_NUMBER = /^0x[0-9a-f]+$/i;
var RE_OCT_NUMBER = /^0[0-7]+$/;
var RE_DEC_NUMBER = /^\d*\.?\d*(?:e[+-]?\d*(?:\d\.?|\.?\d)\d*)?$/i;
var OPERATORS = array_to_hash([
var OPERATORS = makePredicate([
"in",
"instanceof",
"typeof",
@@ -173,13 +108,13 @@ var OPERATORS = array_to_hash([
"||"
]);
var WHITESPACE_CHARS = array_to_hash(characters(" \u00a0\n\r\t\f\u000b\u200b\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000"));
var WHITESPACE_CHARS = makePredicate(characters(" \u00a0\n\r\t\f\u000b\u200b\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000"));
var PUNC_BEFORE_EXPRESSION = array_to_hash(characters("[{(,.;:"));
var PUNC_BEFORE_EXPRESSION = makePredicate(characters("[{(,.;:"));
var PUNC_CHARS = array_to_hash(characters("[]{}(),;:"));
var PUNC_CHARS = makePredicate(characters("[]{}(),;:"));
var REGEXP_MODIFIERS = array_to_hash(characters("gmsiy"));
var REGEXP_MODIFIERS = makePredicate(characters("gmsiy"));
/* -----[ Tokenizer ]----- */
@@ -191,17 +126,18 @@ var UNICODE = {
connector_punctuation: new RegExp("[\\u005F\\u203F\\u2040\\u2054\\uFE33\\uFE34\\uFE4D-\\uFE4F\\uFF3F]")
};
function is_letter(ch) {
return UNICODE.letter.test(ch);
function is_letter(code) {
return (code >= 97 && code <= 122)
|| (code >= 65 && code <= 90)
|| (code >= 0xaa && UNICODE.letter.test(String.fromCharCode(code)));
};
function is_digit(ch) {
ch = ch.charCodeAt(0);
return ch >= 48 && ch <= 57; //XXX: find out if "UnicodeDigit" means something else than 0..9
function is_digit(code) {
return code >= 48 && code <= 57; //XXX: find out if "UnicodeDigit" means something else than 0..9
};
function is_alphanumeric_char(ch) {
return is_digit(ch) || is_letter(ch);
function is_alphanumeric_char(code) {
return is_digit(code) || is_letter(code);
};
function is_unicode_combining_mark(ch) {
@@ -213,24 +149,21 @@ function is_unicode_connector_punctuation(ch) {
};
function is_identifier(name) {
return /^[a-z_$][a-z0-9_$]*$/i.test(name)
&& name != "this"
&& !HOP(KEYWORDS_ATOM, name)
&& !HOP(RESERVED_WORDS, name)
&& !HOP(KEYWORDS, name);
return /^[a-z_$][a-z0-9_$]*$/i.test(name) && !RESERVED_WORDS(name);
};
function is_identifier_start(ch) {
return ch == "$" || ch == "_" || is_letter(ch);
function is_identifier_start(code) {
return code == 36 || code == 95 || is_letter(code);
};
function is_identifier_char(ch) {
return is_identifier_start(ch)
var code = ch.charCodeAt(0);
return is_identifier_start(code)
|| is_digit(code)
|| code == 8204 // \u200c: zero-width non-joiner <ZWNJ>
|| code == 8205 // \u200d: zero-width joiner <ZWJ> (in my ECMA-262 PDF, this is also 200c)
|| is_unicode_combining_mark(ch)
|| is_digit(ch)
|| is_unicode_connector_punctuation(ch)
|| ch == "\u200c" // zero-width non-joiner <ZWNJ>
|| ch == "\u200d" // zero-width joiner <ZWJ> (in my ECMA-262 PDF, this is also 200c)
;
};
@@ -248,7 +181,7 @@ function JS_Parse_Error(message, line, col, pos) {
this.message = message;
this.line = line;
this.col = col;
this.pos = pos + 1;
this.pos = pos;
this.stack = new Error().stack;
};
@@ -317,9 +250,9 @@ function tokenizer($TEXT, filename) {
};
function token(type, value, is_comment) {
S.regex_allowed = ((type == "operator" && !HOP(UNARY_POSTFIX, value)) ||
(type == "keyword" && HOP(KEYWORDS_BEFORE_EXPRESSION, value)) ||
(type == "punc" && HOP(PUNC_BEFORE_EXPRESSION, value)));
S.regex_allowed = ((type == "operator" && !UNARY_POSTFIX[value]) ||
(type == "keyword" && KEYWORDS_BEFORE_EXPRESSION(value)) ||
(type == "punc" && PUNC_BEFORE_EXPRESSION(value)));
var ret = {
type : type,
value : value,
@@ -343,16 +276,14 @@ function tokenizer($TEXT, filename) {
};
function skip_whitespace() {
while (HOP(WHITESPACE_CHARS, peek()))
while (WHITESPACE_CHARS(peek()))
next();
};
function read_while(pred) {
var ret = "", ch = peek(), i = 0;
while (ch && pred(ch, i++)) {
var ret = "", ch, i = 0;
while ((ch = peek()) && pred(ch, i++))
ret += next();
ch = peek();
}
return ret;
};
@@ -363,29 +294,22 @@ function tokenizer($TEXT, filename) {
function read_num(prefix) {
var has_e = false, after_e = false, has_x = false, has_dot = prefix == ".";
var num = read_while(function(ch, i){
if (ch == "x" || ch == "X") {
if (has_x) return false;
return has_x = true;
var code = ch.charCodeAt(0);
switch (code) {
case 120: case 88: // xX
return has_x ? false : (has_x = true);
case 101: case 69: // eE
return has_x ? true : has_e ? false : (has_e = after_e = true);
case 45: // -
return after_e || (i == 0 && !prefix);
case 43: // +
return after_e;
case (after_e = false, 46): // .
return (!has_dot && !has_x && !has_e) ? (has_dot = true) : false;
}
if (!has_x && (ch == "E" || ch == "e")) {
if (has_e) return false;
return has_e = after_e = true;
}
if (ch == "-") {
if (after_e || (i == 0 && !prefix)) return true;
return false;
}
if (ch == "+") return after_e;
after_e = false;
if (ch == ".") {
if (!has_dot && !has_x && !has_e)
return has_dot = true;
return false;
}
return is_alphanumeric_char(ch);
return is_alphanumeric_char(code);
});
if (prefix)
num = prefix + num;
if (prefix) num = prefix + num;
var valid = parse_js_number(num);
if (!isNaN(valid)) {
return token("num", valid);
@@ -396,17 +320,17 @@ function tokenizer($TEXT, filename) {
function read_escaped_char(in_string) {
var ch = next(true, in_string);
switch (ch) {
case "n" : return "\n";
case "r" : return "\r";
case "t" : return "\t";
case "b" : return "\b";
case "v" : return "\u000b";
case "f" : return "\f";
case "0" : return "\0";
case "x" : return String.fromCharCode(hex_bytes(2));
case "u" : return String.fromCharCode(hex_bytes(4));
case "\n": return "";
switch (ch.charCodeAt(0)) {
case 110 : return "\n";
case 114 : return "\r";
case 116 : return "\t";
case 98 : return "\b";
case 118 : return "\u000b"; // \v
case 102 : return "\f";
case 48 : return "\0";
case 120 : return String.fromCharCode(hex_bytes(2)); // \x
case 117 : return String.fromCharCode(hex_bytes(4)); // \u
case 10 : return ""; // newline
default : return ch;
}
};
@@ -422,35 +346,33 @@ function tokenizer($TEXT, filename) {
return num;
};
function read_string() {
return with_eof_error("Unterminated string constant", function(){
var quote = next(), ret = "";
for (;;) {
var ch = next(true);
if (ch == "\\") {
// read OctalEscapeSequence (XXX: deprecated if "strict mode")
// https://github.com/mishoo/UglifyJS/issues/178
var octal_len = 0, first = null;
ch = read_while(function(ch){
if (ch >= "0" && ch <= "7") {
if (!first) {
first = ch;
return ++octal_len;
}
else if (first <= "3" && octal_len <= 2) return ++octal_len;
else if (first >= "4" && octal_len <= 1) return ++octal_len;
var read_string = with_eof_error("Unterminated string constant", function(){
var quote = next(), ret = "";
for (;;) {
var ch = next(true);
if (ch == "\\") {
// read OctalEscapeSequence (XXX: deprecated if "strict mode")
// https://github.com/mishoo/UglifyJS/issues/178
var octal_len = 0, first = null;
ch = read_while(function(ch){
if (ch >= "0" && ch <= "7") {
if (!first) {
first = ch;
return ++octal_len;
}
return false;
});
if (octal_len > 0) ch = String.fromCharCode(parseInt(ch, 8));
else ch = read_escaped_char(true);
}
else if (ch == quote) break;
ret += ch;
else if (first <= "3" && octal_len <= 2) return ++octal_len;
else if (first >= "4" && octal_len <= 1) return ++octal_len;
}
return false;
});
if (octal_len > 0) ch = String.fromCharCode(parseInt(ch, 8));
else ch = read_escaped_char(true);
}
return token("string", ret);
});
};
else if (ch == quote) break;
ret += ch;
}
return token("string", ret);
});
function read_line_comment() {
next();
@@ -465,25 +387,20 @@ function tokenizer($TEXT, filename) {
return token("comment1", ret, true);
};
function read_multiline_comment() {
var read_multiline_comment = with_eof_error("Unterminated multiline comment", function(){
next();
return with_eof_error("Unterminated multiline comment", function(){
var i = find("*/", true),
text = S.text.substring(S.pos, i);
S.pos = i + 2;
S.line += text.split("\n").length - 1;
S.newline_before = S.newline_before || text.indexOf("\n") >= 0;
// https://github.com/mishoo/UglifyJS/issues/#issue/100
if (/^@cc_on/i.test(text)) {
warn("WARNING: at line " + S.line);
warn("*** Found \"conditional comment\": " + text);
warn("*** UglifyJS DISCARDS ALL COMMENTS. This means your code might no longer work properly in Internet Explorer.");
}
return token("comment2", text, true);
});
};
var i = find("*/", true);
var text = S.text.substring(S.pos, i);
var a = text.split("\n"), n = a.length;
// update stream position
S.pos = i + 2;
S.line += n - 1;
if (n > 1) S.col = a[n - 1].length;
else S.col += a[n - 1].length;
S.col += 2;
S.newline_before = S.newline_before || text.indexOf("\n") >= 0;
return token("comment2", text, true);
});
function read_name() {
var backslash = false, name = "", ch, escaped = false, hex;
@@ -501,42 +418,40 @@ function tokenizer($TEXT, filename) {
backslash = false;
}
}
if (HOP(KEYWORDS, name) && escaped) {
if (KEYWORDS(name) && escaped) {
hex = name.charCodeAt(0).toString(16).toUpperCase();
name = "\\u" + "0000".substr(hex.length) + hex + name.slice(1);
}
return name;
};
function read_regexp(regexp) {
return with_eof_error("Unterminated regular expression", function(){
var prev_backslash = false, ch, in_class = false;
while ((ch = next(true))) if (prev_backslash) {
regexp += "\\" + ch;
prev_backslash = false;
} else if (ch == "[") {
in_class = true;
regexp += ch;
} else if (ch == "]" && in_class) {
in_class = false;
regexp += ch;
} else if (ch == "/" && !in_class) {
break;
} else if (ch == "\\") {
prev_backslash = true;
} else {
regexp += ch;
}
var mods = read_name();
return token("regexp", [ regexp, mods ]);
});
};
var read_regexp = with_eof_error("Unterminated regular expression", function(regexp){
var prev_backslash = false, ch, in_class = false;
while ((ch = next(true))) if (prev_backslash) {
regexp += "\\" + ch;
prev_backslash = false;
} else if (ch == "[") {
in_class = true;
regexp += ch;
} else if (ch == "]" && in_class) {
in_class = false;
regexp += ch;
} else if (ch == "/" && !in_class) {
break;
} else if (ch == "\\") {
prev_backslash = true;
} else {
regexp += ch;
}
var mods = read_name();
return token("regexp", new RegExp(regexp, mods));
});
function read_operator(prefix) {
function grow(op) {
if (!peek()) return op;
var bigger = op + peek();
if (HOP(OPERATORS, bigger)) {
if (OPERATORS(bigger)) {
next();
return grow(bigger);
} else {
@@ -564,29 +479,28 @@ function tokenizer($TEXT, filename) {
function handle_dot() {
next();
return is_digit(peek())
return is_digit(peek().charCodeAt(0))
? read_num(".")
: token("punc", ".");
};
function read_word() {
var word = read_name();
return HOP(KEYWORDS_ATOM, word)
? token("atom", word)
: !HOP(KEYWORDS, word)
? token("name", word)
: HOP(OPERATORS, word)
? token("operator", word)
return KEYWORDS_ATOM(word) ? token("atom", word)
: !KEYWORDS(word) ? token("name", word)
: OPERATORS(word) ? token("operator", word)
: token("keyword", word);
};
function with_eof_error(eof_error, cont) {
try {
return cont();
} catch(ex) {
if (ex === EX_EOF) parse_error(eof_error);
else throw ex;
}
return function(x) {
try {
return cont(x);
} catch(ex) {
if (ex === EX_EOF) parse_error(eof_error);
else throw ex;
}
};
};
function next_token(force_regexp) {
@@ -596,13 +510,16 @@ function tokenizer($TEXT, filename) {
start_token();
var ch = peek();
if (!ch) return token("eof");
if (is_digit(ch)) return read_num();
if (ch == '"' || ch == "'") return read_string();
if (HOP(PUNC_CHARS, ch)) return token("punc", next());
if (ch == ".") return handle_dot();
if (ch == "/") return handle_slash();
if (HOP(OPERATOR_CHARS, ch)) return read_operator();
if (ch == "\\" || is_identifier_start(ch)) return read_word();
var code = ch.charCodeAt(0);
switch (code) {
case 34: case 39: return read_string();
case 46: return handle_dot();
case 47: return handle_slash();
}
if (is_digit(code)) return read_num();
if (PUNC_CHARS(ch)) return token("punc", next());
if (OPERATOR_CHARS(ch)) return read_operator();
if (code == 92 || is_identifier_start(code)) return read_word();
parse_error("Unexpected character '" + ch + "'");
};
@@ -617,7 +534,7 @@ function tokenizer($TEXT, filename) {
/* -----[ Parser (constants) ]----- */
var UNARY_PREFIX = array_to_hash([
var UNARY_PREFIX = makePredicate([
"typeof",
"void",
"delete",
@@ -629,19 +546,9 @@ var UNARY_PREFIX = array_to_hash([
"+"
]);
var UNARY_POSTFIX = array_to_hash([ "--", "++" ]);
var UNARY_POSTFIX = makePredicate([ "--", "++" ]);
var ASSIGNMENT = (function(a, ret, i){
while (i < a.length) {
ret[a[i]] = a[i];
i++;
}
return ret;
})(
[ "=", "+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&=" ],
{},
0
);
var ASSIGNMENT = makePredicate([ "=", "+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&=" ]);
var PRECEDENCE = (function(a, ret){
for (var i = 0, n = 1; i < a.length; ++i, ++n) {
@@ -759,7 +666,7 @@ function parse($TEXT, options) {
function parenthesised() {
expect("(");
var exp = expression();
var exp = expression(true);
expect(")");
return exp;
};
@@ -767,7 +674,7 @@ function parse($TEXT, options) {
function embed_tokens(parser) {
return function() {
var start = S.token;
var expr = parser.apply(this, arguments);
var expr = parser();
var end = prev();
expr.start = start;
expr.end = end;
@@ -776,6 +683,7 @@ function parse($TEXT, options) {
};
var statement = embed_tokens(function() {
var tmp;
if (is("operator", "/") || is("operator", "/=")) {
S.peeked = null;
S.token = S.input(S.token.value.substr(1)); // force regexp
@@ -817,7 +725,7 @@ function parse($TEXT, options) {
}
case "keyword":
switch (prog1(S.token.value, next)) {
switch (tmp = S.token.value, next(), tmp) {
case "break":
return break_cont(AST_Break);
@@ -831,7 +739,7 @@ function parse($TEXT, options) {
case "do":
return new AST_Do({
body : in_loop(statement),
condition : (expect_token("keyword", "while"), prog1(parenthesised, semicolon))
condition : (expect_token("keyword", "while"), tmp = parenthesised(), semicolon(), tmp)
});
case "while":
@@ -857,30 +765,30 @@ function parse($TEXT, options) {
? (next(), null)
: can_insert_semicolon()
? null
: prog1(expression, semicolon) )
: (tmp = expression(true), semicolon(), tmp) )
});
case "switch":
return new AST_Switch({
expression : parenthesised(),
body : switch_body_()
body : in_loop(switch_body_)
});
case "throw":
if (S.token.nlb)
croak("Illegal newline after 'throw'");
return new AST_Throw({
value: prog1(expression, semicolon)
value: (tmp = expression(true), semicolon(), tmp)
});
case "try":
return try_();
case "var":
return prog1(var_, semicolon);
return tmp = var_(), semicolon(), tmp;
case "const":
return prog1(const_, semicolon);
return tmp = const_(), semicolon(), tmp;
case "with":
return new AST_With({
@@ -910,8 +818,8 @@ function parse($TEXT, options) {
return new AST_LabeledStatement({ body: stat, label: label });
};
function simple_statement() {
return new AST_SimpleStatement({ body: prog1(expression, semicolon) });
function simple_statement(tmp) {
return new AST_SimpleStatement({ body: (tmp = expression(true), semicolon(), tmp) });
};
function break_cont(type) {
@@ -948,9 +856,9 @@ function parse($TEXT, options) {
function regular_for(init) {
expect(";");
var test = is("punc", ";") ? null : expression();
var test = is("punc", ";") ? null : expression(true);
expect(";");
var step = is("punc", ")") ? null : expression();
var step = is("punc", ")") ? null : expression(true);
expect(")");
return new AST_For({
init : init,
@@ -962,7 +870,7 @@ function parse($TEXT, options) {
function for_in(init) {
var lhs = init instanceof AST_Var ? init.definitions[0].name : null;
var obj = expression();
var obj = expression(true);
expect(")");
return new AST_ForIn({
init : init,
@@ -972,14 +880,16 @@ function parse($TEXT, options) {
});
};
var function_ = function(in_statement) {
var function_ = function(in_statement, ctor) {
var name = is("name") ? as_symbol(in_statement
? AST_SymbolDefun
: ctor === AST_Accessor
? AST_SymbolAccessor
: AST_SymbolLambda) : null;
if (in_statement && !name)
unexpected();
expect("(");
var ctor = in_statement ? AST_Defun : AST_Function;
if (!ctor) ctor = in_statement ? AST_Defun : AST_Function;
return new ctor({
name: name,
argnames: (function(first, a){
@@ -990,10 +900,8 @@ function parse($TEXT, options) {
next();
return a;
})(true, []),
body: embed_tokens(function(){
body: (function(loop, labels){
++S.in_function;
var loop = S.in_loop;
var labels = S.labels;
S.in_directives = true;
S.in_loop = 0;
S.labels = [];
@@ -1002,7 +910,7 @@ function parse($TEXT, options) {
S.in_loop = loop;
S.labels = labels;
return a;
})()
})(S.in_loop, S.labels)
});
};
@@ -1030,17 +938,17 @@ function parse($TEXT, options) {
return a;
};
var switch_body_ = curry(in_loop, function(){
function switch_body_() {
expect("{");
var a = [], cur = null, branch = null;
var a = [], cur = null, branch = null, tmp;
while (!is("punc", "}")) {
if (is("eof")) unexpected();
if (is("keyword", "case")) {
if (branch) branch.end = prev();
cur = [];
branch = new AST_Case({
start : prog1(S.token, next),
expression : expression(),
start : (tmp = S.token, next(), tmp),
expression : expression(true),
body : cur
});
a.push(branch);
@@ -1050,9 +958,9 @@ function parse($TEXT, options) {
if (branch) branch.end = prev();
cur = [];
branch = new AST_Default({
start : prog1(S.token, next, curry(expect, ":")),
start : (tmp = S.token, next(), expect(":"), tmp),
body : cur
})
});
a.push(branch);
}
else {
@@ -1063,7 +971,7 @@ function parse($TEXT, options) {
if (branch) branch.end = prev();
next();
return a;
});
};
function try_() {
var body = block_(), bcatch = null, bfinally = null;
@@ -1160,7 +1068,7 @@ function parse($TEXT, options) {
ret = new AST_String({ start: tok, end: tok, value: tok.value });
break;
case "regexp":
ret = new AST_RegExp({ start: tok, end: tok, pattern: tok.value[0], mods: tok.value[1] });
ret = new AST_RegExp({ start: tok, end: tok, value: tok.value });
break;
case "atom":
switch (tok.value) {
@@ -1189,7 +1097,7 @@ function parse($TEXT, options) {
switch (start.value) {
case "(":
next();
var ex = expression();
var ex = expression(true);
ex.start = start;
ex.end = S.token;
expect(")");
@@ -1208,7 +1116,7 @@ function parse($TEXT, options) {
func.end = prev();
return subscripts(func, allow_calls);
}
if (HOP(ATOMIC_START_TOKEN, S.token.type)) {
if (ATOMIC_START_TOKEN[S.token.type]) {
return subscripts(as_atom_node(), allow_calls);
}
unexpected();
@@ -1252,7 +1160,7 @@ function parse($TEXT, options) {
a.push(new AST_ObjectGetter({
start : start,
key : name,
value : function_(false),
value : function_(false, AST_Accessor),
end : prev()
}));
continue;
@@ -1261,7 +1169,7 @@ function parse($TEXT, options) {
a.push(new AST_ObjectSetter({
start : start,
key : name,
value : function_(false),
value : function_(false, AST_Accessor),
end : prev()
}));
continue;
@@ -1280,26 +1188,30 @@ function parse($TEXT, options) {
});
function as_property_name() {
switch (S.token.type) {
var tmp = S.token;
next();
switch (tmp.type) {
case "num":
case "string":
case "name":
case "operator":
case "keyword":
case "atom":
return prog1(S.token.value, next);
return tmp.value;
default:
unexpected();
}
};
function as_name() {
switch (S.token.type) {
var tmp = S.token;
next();
switch (tmp.type) {
case "name":
case "operator":
case "keyword":
case "atom":
return prog1(S.token.value, next);
return tmp.value;
default:
unexpected();
}
@@ -1333,7 +1245,7 @@ function parse($TEXT, options) {
}
if (is("punc", "[")) {
next();
var prop = expression();
var prop = expression(true);
expect("]");
return subscripts(new AST_Sub({
start : start,
@@ -1356,16 +1268,15 @@ function parse($TEXT, options) {
var maybe_unary = function(allow_calls) {
var start = S.token;
if (is("operator") && HOP(UNARY_PREFIX, S.token.value)) {
var ex = make_unary(AST_UnaryPrefix,
prog1(S.token.value, next),
maybe_unary(allow_calls));
if (is("operator") && UNARY_PREFIX(start.value)) {
next();
var ex = make_unary(AST_UnaryPrefix, start.value, maybe_unary(allow_calls));
ex.start = start;
ex.end = prev();
return ex;
}
var val = expr_atom(allow_calls);
while (is("operator") && HOP(UNARY_POSTFIX, S.token.value) && !S.token.nlb) {
while (is("operator") && UNARY_POSTFIX(S.token.value) && !S.token.nlb) {
val = make_unary(AST_UnaryPostfix, S.token.value, val);
val.start = start;
val.end = S.token;
@@ -1436,13 +1347,13 @@ function parse($TEXT, options) {
var maybe_assign = function(no_in) {
var start = S.token;
var left = maybe_conditional(no_in), val = S.token.value;
if (is("operator") && HOP(ASSIGNMENT, val)) {
if (is("operator") && ASSIGNMENT(val)) {
if (is_assignable(left)) {
next();
return new AST_Assign({
start : start,
left : left,
operator : ASSIGNMENT[val],
operator : val,
right : maybe_assign(no_in),
end : peek()
});
@@ -1453,8 +1364,6 @@ function parse($TEXT, options) {
};
var expression = function(commas, no_in) {
if (arguments.length == 0)
commas = true;
var start = S.token;
var expr = maybe_assign(no_in);
if (commas && is("punc", ",")) {
@@ -1493,5 +1402,3 @@ function parse($TEXT, options) {
})();
};
var warn = function() {};

View File

@@ -43,7 +43,7 @@
"use strict";
function SymbolDef(scope, orig) {
function SymbolDef(scope, index, orig) {
this.name = orig.name;
this.orig = [ orig ];
this.scope = scope;
@@ -52,15 +52,18 @@ function SymbolDef(scope, orig) {
this.mangled_name = null;
this.undeclared = false;
this.constant = false;
this.index = index;
};
SymbolDef.prototype = {
unmangleable: function() {
return this.global || this.undeclared || this.scope.uses_eval || this.scope.uses_with;
unmangleable: function(options) {
return this.global
|| this.undeclared
|| (!(options && options.eval) && (this.scope.uses_eval || this.scope.uses_with));
},
mangle: function() {
if (!this.mangled_name && !this.unmangleable())
this.mangled_name = this.scope.next_mangled();
mangle: function(options) {
if (!this.mangled_name && !this.unmangleable(options))
this.mangled_name = this.scope.next_mangled(options);
}
};
@@ -75,14 +78,17 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
// pass 1: setup scope chaining and handle definitions
var self = this;
var scope = self.parent_scope = null;
var labels = {};
var labels = new Dictionary();
var nesting = 0;
var tw = new TreeWalker(function(node, descend){
if (node instanceof AST_Scope) {
node.init_scope_vars();
node.init_scope_vars(nesting);
var save_scope = node.parent_scope = scope;
++nesting;
scope = node;
descend();
scope = save_scope;
--nesting;
return true; // don't descend again in TreeWalker
}
if (node instanceof AST_Directive) {
@@ -97,11 +103,11 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
}
if (node instanceof AST_LabeledStatement) {
var l = node.label;
if (labels[l.name])
if (labels.has(l.name))
throw new Error(string_template("Label {name} defined twice", l));
labels[l.name] = l;
labels.set(l.name, l);
descend();
delete labels[l.name];
labels.del(l.name);
return true; // no descend again
}
if (node instanceof AST_SymbolDeclaration) {
@@ -113,29 +119,16 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
if (node instanceof AST_Label) {
node.thedef = node;
node.init_scope_vars();
var p = tw.parent(); // AST_LabeledStatement
var block = p.body;
if (block instanceof AST_StatementWithBody)
block = block.body;
node.label_target = block;
}
if (node instanceof AST_LoopControl) {
if (!node.label) {
var a = tw.stack, i = a.length - 1;
while (--i >= 0) {
var p = a[i];
if (p instanceof AST_For
|| p instanceof AST_ForIn
|| p instanceof AST_DWLoop
|| p instanceof AST_SwitchBranch) {
node.loopcontrol_target = p.body;
break;
}
}
}
}
else if (node instanceof AST_SymbolLambda) {
scope.def_function(node);
if (node instanceof AST_SymbolLambda) {
//scope.def_function(node);
//
// https://github.com/mishoo/UglifyJS2/issues/24 — MSIE
// leaks function expression names into the containing
// scope. Don't like this fix but seems we can't do any
// better. IE: please die. Please!
(node.scope = scope.parent_scope).def_function(node);
node.init.push(tw.parent());
}
else if (node instanceof AST_SymbolDefun) {
@@ -164,7 +157,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
scope.def_variable(node);
}
if (node instanceof AST_LabelRef) {
var sym = labels[node.name];
var sym = labels.get(node.name);
if (!sym) throw new Error(string_template("Undefined label {name} [{line},{col}]", {
name: node.name,
line: node.start.line,
@@ -177,7 +170,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
// pass 2: find back references and eval
var func = null;
var globals = self.globals = {};
var globals = self.globals = new Dictionary();
var tw = new TreeWalker(function(node, descend){
if (node instanceof AST_Lambda) {
var prev_func = func;
@@ -195,15 +188,15 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
var sym = node.scope.find_variable(name);
if (!sym) {
var g;
if (HOP(globals, name)) {
g = globals[name];
if (globals.has(name)) {
g = globals.get(name);
} else {
g = new SymbolDef(self, node);
g = new SymbolDef(self, globals.size(), node);
g.undeclared = true;
globals[name] = g;
globals.set(name, g);
}
node.thedef = g;
if (name == "eval") {
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;
}
@@ -220,15 +213,16 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
self.walk(tw);
});
AST_Scope.DEFMETHOD("init_scope_vars", function(){
AST_Scope.DEFMETHOD("init_scope_vars", function(nesting){
this.directives = []; // contains the directives defined in this scope, i.e. "use strict"
this.variables = {}; // map name to AST_SymbolVar (variables defined in this scope; includes functions)
this.functions = {}; // map name to AST_SymbolDefun (functions defined in this 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 = null; // 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
this.nesting = nesting; // the nesting level of this scope (0 means toplevel)
});
AST_Scope.DEFMETHOD("strict", function(){
@@ -236,7 +230,7 @@ AST_Scope.DEFMETHOD("strict", function(){
});
AST_Lambda.DEFMETHOD("init_scope_vars", function(){
AST_Scope.prototype.init_scope_vars.call(this);
AST_Scope.prototype.init_scope_vars.apply(this, arguments);
this.uses_arguments = false;
});
@@ -246,8 +240,10 @@ AST_SymbolRef.DEFMETHOD("reference", function() {
var s = this.scope;
while (s) {
push_uniq(s.enclosed, def);
if (s === def.scope) break;
s = s.parent_scope;
}
this.frame = this.scope.nesting - def.scope.nesting;
});
AST_SymbolDeclaration.DEFMETHOD("init_scope_vars", function(){
@@ -264,9 +260,8 @@ AST_LabelRef.DEFMETHOD("reference", function(){
AST_Scope.DEFMETHOD("find_variable", function(name){
if (name instanceof AST_Symbol) name = name.name;
return HOP(this.variables, name)
? this.variables[name]
: (this.parent_scope && this.parent_scope.find_variable(name));
return this.variables.get(name)
|| (this.parent_scope && this.parent_scope.find_variable(name));
});
AST_Scope.DEFMETHOD("has_directive", function(value){
@@ -275,23 +270,23 @@ AST_Scope.DEFMETHOD("has_directive", function(value){
});
AST_Scope.DEFMETHOD("def_function", function(symbol){
this.functions[symbol.name] = this.def_variable(symbol);
this.functions.set(symbol.name, this.def_variable(symbol));
});
AST_Scope.DEFMETHOD("def_variable", function(symbol){
var def;
if (!HOP(this.variables, symbol.name)) {
def = new SymbolDef(this, symbol);
this.variables[symbol.name] = def;
if (!this.variables.has(symbol.name)) {
def = new SymbolDef(this, this.variables.size(), symbol);
this.variables.set(symbol.name, def);
def.global = !this.parent_scope;
} else {
def = this.variables[symbol.name];
def = this.variables.get(symbol.name);
def.orig.push(symbol);
}
return symbol.thedef = def;
});
AST_Scope.DEFMETHOD("next_mangled", function(){
AST_Scope.DEFMETHOD("next_mangled", function(options){
var ext = this.enclosed, n = ext.length;
out: while (true) {
var m = base54(++this.cname);
@@ -301,7 +296,7 @@ AST_Scope.DEFMETHOD("next_mangled", function(){
// inner scopes.
for (var i = n; --i >= 0;) {
var sym = ext[i];
var name = sym.mangled_name || (sym.unmangleable() && sym.name);
var name = sym.mangled_name || (sym.unmangleable(options) && sym.name);
if (m == name) continue out;
}
return m;
@@ -313,8 +308,13 @@ AST_Scope.DEFMETHOD("references", function(sym){
return this.enclosed.indexOf(sym) < 0 ? null : sym;
});
AST_Symbol.DEFMETHOD("unmangleable", function(){
return this.definition().unmangleable();
AST_Symbol.DEFMETHOD("unmangleable", function(options){
return this.definition().unmangleable(options);
});
// property accessors are not mangleable
AST_SymbolAccessor.DEFMETHOD("unmangleable", function(){
return true;
});
// labels are always mangleable
@@ -347,16 +347,15 @@ AST_Symbol.DEFMETHOD("global", function(){
return this.definition().global;
});
AST_LoopControl.DEFMETHOD("target", function(){
if (this.label) return this.label.definition().label_target;
return this.loopcontrol_target;
AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){
return defaults(options, {
except : [],
eval : false,
});
});
AST_Toplevel.DEFMETHOD("mangle_names", function(options){
options = defaults(options, {
sort : false,
except : []
});
options = this._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
@@ -373,16 +372,11 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
}
if (node instanceof AST_Scope) {
var p = tw.parent();
var is_setget = p instanceof AST_ObjectSetter || p instanceof AST_ObjectGetter;
var a = node.variables;
for (var i in a) if (HOP(a, i)) {
var symbol = a[i];
if (!(is_setget && symbol instanceof AST_SymbolLambda)) {
if (options.except.indexOf(symbol.name) < 0) {
to_mangle.push(symbol);
}
node.variables.each(function(symbol){
if (options.except.indexOf(symbol.name) < 0) {
to_mangle.push(symbol);
}
}
});
return;
}
if (node instanceof AST_Label) {
@@ -393,15 +387,11 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
}
});
this.walk(tw);
if (options.sort) to_mangle = mergeSort(to_mangle, function(a, b){
return b.references.length - a.references.length;
});
to_mangle.forEach(function(def){ def.mangle(options) });
});
AST_Toplevel.DEFMETHOD("compute_char_frequency", function(){
AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options){
options = this._default_mangler_options(options);
var tw = new TreeWalker(function(node){
if (node instanceof AST_Constant)
base54.consider(node.print_to_string());
@@ -459,7 +449,7 @@ AST_Toplevel.DEFMETHOD("compute_char_frequency", function(){
base54.consider("catch");
else if (node instanceof AST_Finally)
base54.consider("finally");
else if (node instanceof AST_Symbol && node.unmangleable())
else if (node instanceof AST_Symbol && node.unmangleable(options))
base54.consider(node.name);
else if (node instanceof AST_Unary || node instanceof AST_Binary)
base54.consider(node.operator);
@@ -467,21 +457,21 @@ AST_Toplevel.DEFMETHOD("compute_char_frequency", function(){
base54.consider(node.property);
});
this.walk(tw);
base54.sort();
});
var base54 = (function() {
var string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789";
var chars, frequency;
function reset() {
frequency = {};
chars = string.split("");
chars.map(function(ch){ frequency[ch] = 0 });
frequency = Object.create(null);
chars = string.split("").map(function(ch){ return ch.charCodeAt(0) });
chars.forEach(function(ch){ frequency[ch] = 0 });
}
base54.consider = function(str){
for (var i = str.length; --i >= 0;) {
var ch = str.charAt(i);
if (string.indexOf(ch) >= 0)
++frequency[ch];
var code = str.charCodeAt(i);
if (code in frequency) ++frequency[code];
}
};
base54.sort = function() {
@@ -498,7 +488,7 @@ var base54 = (function() {
function base54(num) {
var ret = "", base = 54;
do {
ret += chars[num % base];
ret += String.fromCharCode(chars[num % base]);
num = Math.floor(num / base);
base = 64;
} while (num > 0);
@@ -524,8 +514,9 @@ AST_Toplevel.DEFMETHOD("scope_warnings", function(options){
// XXX: this also warns about JS standard names,
// i.e. Object, Array, parseInt etc. Should add a list of
// exceptions.
AST_Node.warn("Undeclared symbol: {name} [{line},{col}]", {
AST_Node.warn("Undeclared symbol: {name} [{file}:{line},{col}]", {
name: node.name,
file: node.start.file,
line: node.start.line,
col: node.start.col
});
@@ -540,9 +531,10 @@ AST_Toplevel.DEFMETHOD("scope_warnings", function(options){
if (sym
&& (sym.undeclared()
|| (sym.global() && sym.scope !== sym.definition().scope))) {
AST_Node.warn("{msg}: {name} [{line},{col}]", {
AST_Node.warn("{msg}: {name} [{file}:{line},{col}]", {
msg: sym.undeclared() ? "Accidental global?" : "Assignment to global",
name: sym.name,
file: sym.start.file,
line: sym.start.line,
col: sym.start.col
});
@@ -552,14 +544,15 @@ AST_Toplevel.DEFMETHOD("scope_warnings", function(options){
&& node instanceof AST_SymbolRef
&& node.undeclared()
&& node.name == "eval") {
AST_Node.warn("Eval is used [{line},{col}]", node.start);
AST_Node.warn("Eval is used [{file}:{line},{col}]", node.start);
}
if (options.unreferenced
&& node instanceof AST_SymbolDeclaration
&& (node instanceof AST_SymbolDeclaration || node instanceof AST_Label)
&& node.unreferenced()) {
AST_Node.warn("{type} {name} is declared but not referenced [{line},{col}]", {
AST_Node.warn("{type} {name} is declared but not referenced [{file}:{line},{col}]", {
type: node instanceof AST_Label ? "Label" : "Symbol",
name: node.name,
file: node.start.file,
line: node.start.line,
col: node.start.col
});
@@ -567,8 +560,9 @@ AST_Toplevel.DEFMETHOD("scope_warnings", function(options){
if (options.func_arguments
&& node instanceof AST_Lambda
&& node.uses_arguments) {
AST_Node.warn("arguments used in function {name} [{line},{col}]", {
AST_Node.warn("arguments used in function {name} [{file}:{line},{col}]", {
name: node.name ? node.name.name : "anonymous",
file: node.start.file,
line: node.start.line,
col: node.start.col
});
@@ -576,8 +570,10 @@ AST_Toplevel.DEFMETHOD("scope_warnings", function(options){
if (options.nested_defuns
&& node instanceof AST_Defun
&& !(tw.parent() instanceof AST_Scope)) {
AST_Node.warn("Function {name} declared in nested statement [{line},{col}]", {
AST_Node.warn("Function {name} declared in nested statement \"{type}\" [{file}:{line},{col}]", {
name: node.name.name,
type: tw.parent().TYPE,
file: node.start.file,
line: node.start.line,
col: node.start.col
});

View File

@@ -59,13 +59,13 @@ TreeTransformer.prototype = new TreeWalker;
node.DEFMETHOD("transform", function(tw, in_list){
var x, y;
tw.push(this);
x = tw.before(this, descend, in_list);
if (tw.before) x = tw.before(this, descend, in_list);
if (x === undefined) {
if (!tw.after) {
x = this;
descend(x, tw);
} else {
x = this.clone();
tw.stack[tw.stack.length - 1] = x = this.clone();
descend(x, tw);
y = tw.after(x, in_list);
if (y !== undefined) x = y;
@@ -93,10 +93,6 @@ TreeTransformer.prototype = new TreeWalker;
self.body = self.body.transform(tw);
});
_(AST_BlockStatement, function(self, tw){
self.body = do_list(self.body, tw);
});
_(AST_Block, function(self, tw){
self.body = do_list(self.body, tw);
});

View File

@@ -43,21 +43,8 @@
"use strict";
function curry(f) {
var args = slice(arguments, 1);
return function() { return f.apply(this, args.concat(slice(arguments))); };
};
function prog1(ret) {
if (ret instanceof Function)
ret = ret();
for (var i = 1, n = arguments.length; --n > 0; ++i)
arguments[i]();
return ret;
};
function array_to_hash(a) {
var ret = {};
var ret = Object.create(null);
for (var i = 0; i < a.length; ++i)
ret[a[i]] = true;
return ret;
@@ -85,10 +72,6 @@ function find_if(func, array) {
}
};
function HOP(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
};
function repeat_string(str, i) {
if (i <= 0) return "";
if (i == 1) return str;
@@ -107,16 +90,16 @@ 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))
if (croak) for (var i in ret) if (ret.hasOwnProperty(i) && !defs.hasOwnProperty(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];
for (var i in defs) if (defs.hasOwnProperty(i)) {
ret[i] = (args && args.hasOwnProperty(i)) ? args[i] : defs[i];
}
return ret;
};
function merge(obj, ext) {
for (var i in ext) if (HOP(ext, i)) {
for (var i in ext) if (ext.hasOwnProperty(i)) {
obj[i] = ext[i];
}
return obj;
@@ -158,7 +141,7 @@ var MAP = (function(){
}
}
else {
for (i in a) if (HOP(a, i)) if (doit()) break;
for (i in a) if (a.hasOwnProperty(i)) if (doit()) break;
}
return top.concat(ret);
};
@@ -183,6 +166,12 @@ function string_template(text, props) {
});
};
function remove(array, el) {
for (var i = array.length; --i >= 0;) {
if (array[i] === el) array.splice(i, 1);
}
};
function mergeSort(array, cmp) {
if (array.length < 2) return array.slice();
function merge(a, b) {
@@ -218,3 +207,74 @@ function set_intersection(a, b) {
return b.indexOf(el) >= 0;
});
};
// this function is taken from Acorn [1], written by Marijn Haverbeke
// [1] https://github.com/marijnh/acorn
function makePredicate(words) {
if (!(words instanceof Array)) words = words.split(" ");
var f = "", cats = [];
out: for (var i = 0; i < words.length; ++i) {
for (var j = 0; j < cats.length; ++j)
if (cats[j][0].length == words[i].length) {
cats[j].push(words[i]);
continue out;
}
cats.push([words[i]]);
}
function compareTo(arr) {
if (arr.length == 1) return f += "return str === " + JSON.stringify(arr[0]) + ";";
f += "switch(str){";
for (var i = 0; i < arr.length; ++i) f += "case " + JSON.stringify(arr[i]) + ":";
f += "return true}return false;";
}
// When there are more than three length categories, an outer
// switch first dispatches on the lengths, to save on comparisons.
if (cats.length > 3) {
cats.sort(function(a, b) {return b.length - a.length;});
f += "switch(str.length){";
for (var i = 0; i < cats.length; ++i) {
var cat = cats[i];
f += "case " + cat[0].length + ":";
compareTo(cat);
}
f += "}";
// Otherwise, simply generate a flat `switch` statement.
} else {
compareTo(words);
}
return new Function("str", f);
};
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;
},
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;
}
};

View File

@@ -1,9 +1,9 @@
{
"name": "uglify-js2",
"name": "uglify-js",
"description": "JavaScript parser, mangler/compressor and beautifier toolkit",
"homepage": "http://lisperator.net/uglifyjs",
"main": "tools/node.js",
"version": "2.0.0",
"version": "2.2.0",
"engines": { "node" : ">=0.4.0" },
"maintainers": [{
"name": "Mihai Bazon",
@@ -15,11 +15,11 @@
"url": "https://github.com/mishoo/UglifyJS2.git"
}],
"dependencies": {
"source-map" : "*",
"optimist" : "*"
"source-map" : "~0.1.7",
"optimist" : "~0.3.5"
},
"bin": {
"uglifyjs2" : "bin/uglifyjs2"
"uglifyjs" : "bin/uglifyjs"
},
"scripts": {"test": "node test/run-tests.js"}
}

View File

@@ -0,0 +1,97 @@
unused_funarg_1: {
options = { 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 = { 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;
}
}
}

11
test/compress/issue-12.js Normal file
View File

@@ -0,0 +1,11 @@
keep_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 () {} } }
}

17
test/compress/issue-22.js Normal file
View File

@@ -0,0 +1,17 @@
return_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;
}
}
}

31
test/compress/issue-44.js Normal file
View File

@@ -0,0 +1,31 @@
issue_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++);
}
}
}
}

163
test/compress/labels.js Normal file
View File

@@ -0,0 +1,163 @@
labels_1: {
options = { if_return: true, conditionals: true, dead_code: true };
input: {
out: {
if (foo) break out;
console.log("bar");
}
};
expect: {
foo || console.log("bar");
}
}
labels_2: {
options = { if_return: true, conditionals: true, dead_code: true };
input: {
out: {
if (foo) print("stuff");
else break out;
console.log("here");
}
};
expect: {
if (foo) {
print("stuff");
console.log("here");
}
}
}
labels_3: {
options = { if_return: true, conditionals: true, dead_code: 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);
}
}
labels_4: {
options = { if_return: true, conditionals: true, dead_code: 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);
}
}
labels_5: {
options = { if_return: true, conditionals: true, dead_code: 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 = { if_return: true, conditionals: true, dead_code: true };
input: {
while (foo) {
x();
y();
continue;
}
};
expect: {
while (foo) {
x();
y();
}
}
}
labels_8: {
options = { if_return: true, conditionals: true, dead_code: true };
input: {
while (foo) {
x();
y();
break;
}
};
expect: {
while (foo) {
x();
y();
break;
}
}
}
labels_9: {
options = { if_return: true, conditionals: true, dead_code: true };
input: {
out: while (foo) {
x();
y();
continue out;
z();
k();
}
};
expect: {
while (foo) {
x();
y();
}
}
}
labels_10: {
options = { if_return: true, conditionals: true, dead_code: true };
input: {
out: while (foo) {
x();
y();
break out;
z();
k();
}
};
expect: {
out: while (foo) {
x();
y();
break out;
}
}
}

123
test/compress/loops.js Normal file
View File

@@ -0,0 +1,123 @@
while_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();
}
}

View File

@@ -87,3 +87,75 @@ make_sequences_4: {
with (x = 5, obj);
}
}
lift_sequences_1: {
options = { sequences: true };
input: {
foo = !(x(), y(), bar());
}
expect: {
x(), y(), foo = !bar();
}
}
lift_sequences_2: {
options = { sequences: true, evaluate: true };
input: {
q = 1 + (foo(), bar(), 5) + 7 * (5 / (3 - (a(), (QW=ER), c(), 2))) - (x(), y(), 5);
}
expect: {
foo(), bar(), a(), QW = ER, c(), x(), y(), q = 36
}
}
lift_sequences_3: {
options = { sequences: true, conditionals: true };
input: {
x = (foo(), bar(), baz()) ? 10 : 20;
}
expect: {
foo(), bar(), x = baz() ? 10 : 20;
}
}
lift_sequences_4: {
options = { side_effects: true };
input: {
x = (foo, bar, baz);
}
expect: {
x = baz;
}
}
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;);
}
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;);
}
}

210
test/compress/switch.js Normal file
View File

@@ -0,0 +1,210 @@
constant_switch_1: {
options = { dead_code: true, evaluate: 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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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();
}
}
}

View File

@@ -37,6 +37,12 @@ function find_test_files(dir) {
var files = fs.readdirSync(dir).filter(function(name){
return /\.js$/i.test(name);
});
if (process.argv.length > 2) {
var x = process.argv.slice(2);
files = files.filter(function(f){
return x.indexOf(f) >= 0;
});
}
return files;
}
@@ -61,15 +67,19 @@ function run_compress_tests() {
log_start_file(file);
function test_case(test) {
log_test(test.name);
var cmp = new U.Compressor(test.options || {}, true);
var options = U.defaults(test.options, {
warnings: false
});
var cmp = new U.Compressor(options, true);
var expect = make_code(as_toplevel(test.expect), false);
var input = as_toplevel(test.input);
var input_code = make_code(test.input);
var output = input.transform(cmp);
output.figure_out_scope();
output = make_code(output, false);
if (expect != output) {
log("!!! failed\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n---EXPECTED---\n{expected}\n\n", {
input: make_code(test.input),
input: input_code,
output: output,
expected: expect
});

View File

@@ -1,27 +1,14 @@
var save_stderr = process.stderr;
var path = require("path");
var fs = require("fs");
// discard annoying NodeJS warning ("path.existsSync is now called `fs.existsSync`.")
var devnull = fs.createWriteStream("/dev/null");
process.__defineGetter__("stderr", function(){
return devnull;
});
var vm = require("vm");
var sys = require("util");
var path = require("path");
var UglifyJS = vm.createContext({
sys : sys,
console : console,
MOZ_SourceMap : require("source-map")
});
process.__defineGetter__("stderr", function(){
return save_stderr;
});
function load_global(file) {
file = path.resolve(path.dirname(module.filename), file);
try {
@@ -35,15 +22,21 @@ function load_global(file) {
}
};
load_global("../lib/utils.js");
load_global("../lib/ast.js");
load_global("../lib/parse.js");
load_global("../lib/transform.js");
load_global("../lib/scope.js");
load_global("../lib/output.js");
load_global("../lib/compress.js");
load_global("../lib/sourcemap.js");
load_global("../lib/mozilla-ast.js");
var FILES = exports.FILES = [
"../lib/utils.js",
"../lib/ast.js",
"../lib/parse.js",
"../lib/transform.js",
"../lib/scope.js",
"../lib/output.js",
"../lib/compress.js",
"../lib/sourcemap.js",
"../lib/mozilla-ast.js"
].map(function(file){
return path.join(path.dirname(fs.realpathSync(__filename)), file);
});
FILES.forEach(load_global);
UglifyJS.AST_Node.warn_function = function(txt) {
sys.error("WARN: " + txt);
@@ -55,3 +48,106 @@ for (var i in UglifyJS) {
exports[i] = UglifyJS[i];
}
}
exports.minify = function(files, options) {
options = UglifyJS.defaults(options, {
outSourceMap : null,
sourceRoot : null,
inSourceMap : null,
fromString : false,
warnings : false,
});
if (typeof files == "string")
files = [ files ];
// 1. parse
var toplevel = null;
files.forEach(function(file){
var code = options.fromString
? file
: fs.readFileSync(file, "utf8");
toplevel = UglifyJS.parse(code, {
filename: options.fromString ? "?" : file,
toplevel: toplevel
});
});
// 2. compress
toplevel.figure_out_scope();
var sq = UglifyJS.Compressor({
warnings: options.warnings,
});
toplevel = toplevel.transform(sq);
// 3. mangle
toplevel.figure_out_scope();
toplevel.compute_char_frequency();
toplevel.mangle_names();
// 4. output
var map = null;
var inMap = null;
if (options.inSourceMap) {
inMap = fs.readFileSync(options.inSourceMap, "utf8");
}
if (options.outSourceMap) map = UglifyJS.SourceMap({
file: options.outSourceMap,
orig: inMap,
root: options.sourceRoot
});
var stream = UglifyJS.OutputStream({ source_map: map });
toplevel.print(stream);
return {
code : stream + "",
map : map + ""
};
};
// exports.describe_ast = function() {
// function doitem(ctor) {
// var sub = {};
// ctor.SUBCLASSES.forEach(function(ctor){
// sub[ctor.TYPE] = doitem(ctor);
// });
// var ret = {};
// if (ctor.SELF_PROPS.length > 0) ret.props = ctor.SELF_PROPS;
// if (ctor.SUBCLASSES.length > 0) ret.sub = sub;
// return ret;
// }
// return doitem(UglifyJS.AST_Node).sub;
// }
exports.describe_ast = function() {
var out = UglifyJS.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.forEach(function(ctor, i){
out.indent();
doitem(ctor);
out.newline();
});
});
}
};
doitem(UglifyJS.AST_Node);
return out + "";
};