Features

This page describes yasco’s features. Each feature is explained using a before-after-scenario.

Notice: The „Features” page is not nearly complete. I shall update and add as much as I can.

# Identifier substitution

This basic feature is enabled by default. It can be disabled using the `–no-mapping` option (or `-M`). Both variable name and function name substitution are toggled using this option.

## Variable name substitution

**yasco can shorten the names of *declared* variables. The scope in which those are declared, are known to yasco.**

Whenever a variable is implicitly declared using `var`, yasco remembers its name and scope and substitutes it to `vX` (X being a placeholder for a numeric value). In the following example, `d` is not declared and is thus not substituted. It also knows beforehand, if a variable was declared within a scope and used before its declaration (which is valid Javascript). This is the case for `a`.

### Before

do {
    a = 1;
    var a, b = 0, c = 1;
    d = 2;
    alert(a+b+d+c);
} while(false);

### After

do{v1=1;var v1,v2=0,v0=1;d=2;alert(v1+v2+d+v0);}while(false);

You can see that `d` is still present, whereas the rest have been substituted. Also note `a` being `v1` now.

## Function name substitution

**yasco can shorten the names of functions. The scope in which those are declared, are known to yasco.**

yasco knows functions and the scope in which they are available and substitutes their names to `vX` (X being a placeholder for a numeric value).

As with substituted variable names, the function declaration is known even if the usage of the identifier precedes the declaration.

### Before

function foo()
    {
        var a = bar(1,2);
        return a;
    }

function bar(a,b)
    {
        return a + b;
    }

### After
Option `-n1` used for readability:

function v1(){var v2=v0(1,2);
return v2;}
function v0(p2,p3){return p2+p3;}

Note the arguments `a` and `b` to function `bar()` having been substituted to `p2` and `p3`.

## Keyword aliasing

Commonly used objects and methods are aliased. The following keywords are subject to aliasing:

* `clearTimeout`
* `document`
* `extend`
* `fromCharCode`
* `href`
* `indexOf`
* `location`
* `match`
* `navigator`
* `Prototype`
* `prototype`
* `setTimeout`
* `String`
* `substr`
* `substring`
* `userAgent`
* `window`

The following options can be passed to the filter:

* `always_replace`
* `verbose`
* `wrapper_object`

By default, the objects are aliased to global variables sequentially named starting with from the capital letter A. When using the
option `wrapper_object`, a global object is used for aliasing instead:

yaso.pl … –keywords -O Yaso::Filter::Keywords=verbose:1,wrapper_object=1

This will modify the following code

var a = document.location.href,
    b = String.fromCharCode(64);

to look like this:

var Be={D:String,A:document,B:"location",C:"href",E:"fromCharCode"};
var v0=Be.A[Be.B][Be.C],
    v1=Be.D[Be.E](64);

Actually, for this example to work, yasco needs the `always_replace` option set to one. Aliasing will only be done for objects
referenced more than once.

Leave a Reply

Your email address will not be published. Required fields are marked *