These pages are old. They apply to UglifyJS v2. Version 3 has evolved a lot in the mean time to support most of ES6. Please check the documentation in the official repository for up-to-date information. Big thanks to all contributors, especially to Alex Lam S.L., who has maintained this project for years!

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:

toplevel.figure_out_scope();

The figure_out_scope method is defined only on the AST_Toplevel node.

Methods

Several methods do meaningful things after a call to figure_out_scope:

Internals

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.

Welcome! (login)

UglifyJS

JS compressor of world fame.

Open demo

Latest blog entries tagged "uglifyjs"

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

Fork me on Github