JS compressor of world fame.

Open demo

Latest blog entries tagged "uglifyjs"

/** May the source-map be with you! **/

UglifyJS — scope analyzer

UglifyJS contains a scope analyzer which figures out variable/function definitions, references etc. You need to call it manually before compression or mangling:


The figure_out_scope method is defined only on the AST_Toplevel node.


Several methods do meaningful things after a call to figure_out_scope:


figure_out_scope will supplement nodes with additional information:

In AST_Scope nodes

In AST_Symbol nodes

In AST_LabelRef nodes

Symbol definitions

After parsing you'll want to call toplevel.figure_out_scope() in order to supply additional information in the AST. This function will create an unique definition for each symbol. Think of code like this, for example:

function f(x) {
  if (something) {
    var x = 10;
  var x = 20;

Are the x-es the same variable? Well, they should be, although in the AST they are different AST_SymbolDeclaration nodes. The scope analyzer will create a single definition and point each of them to the same definition. This definition is a SymbolDef object and it has the following properties:

When it sees an undeclared symbol such as the Q below:

function f(x) {
  return Q + Q + x;

UglifyJS will still create a SymbolDef, but mark it "undeclared". This is for consistency, so that every symbol's definition() method returns something usable. It also might be useful for various applications to track access to undeclared globals, which can be found in the SymbolDef's references property.

Fork me on Github