🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

uglify-js

Package Overview
Dependencies
Maintainers
3
Versions
288
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

uglify-js - npm Package Compare versions

Comparing version

to
2.4.16

test/mozilla-ast.js

385

lib/mozilla-ast.js

@@ -49,3 +49,22 @@ /***********************************************************************

var MOZ_TO_ME = {
TryStatement : function(M) {
ExpressionStatement: function(M) {
var expr = M.expression;
if (expr.type === "Literal" && typeof expr.value === "string") {
return new AST_Directive({
start: my_start_token(M),
end: my_end_token(M),
value: expr.value
});
}
return new AST_SimpleStatement({
start: my_start_token(M),
end: my_end_token(M),
body: from_moz(expr)
});
},
TryStatement: function(M) {
var handlers = M.handlers || [M.handler];
if (handlers.length > 1 || M.guardedHandlers && M.guardedHandlers.length) {
throw new Error("Multiple catch clauses are not supported.");
}
return new AST_Try({

@@ -55,15 +74,27 @@ start : my_start_token(M),

body : from_moz(M.block).body,
bcatch : from_moz(M.handlers ? M.handlers[0] : M.handler),
bcatch : from_moz(handlers[0]),
bfinally : M.finalizer ? new AST_Finally(from_moz(M.finalizer)) : null
});
},
CatchClause : function(M) {
return new AST_Catch({
start : my_start_token(M),
end : my_end_token(M),
argname : from_moz(M.param),
body : from_moz(M.body).body
});
Property: function(M) {
var key = M.key;
var name = key.type == "Identifier" ? key.name : key.value;
var args = {
start : my_start_token(key),
end : my_end_token(M.value),
key : name,
value : from_moz(M.value)
};
switch (M.kind) {
case "init":
return new AST_ObjectKeyVal(args);
case "set":
args.value.name = from_moz(key);
return new AST_ObjectSetter(args);
case "get":
args.value.name = from_moz(key);
return new AST_ObjectGetter(args);
}
},
ObjectExpression : function(M) {
ObjectExpression: function(M) {
return new AST_Object({

@@ -73,27 +104,11 @@ start : my_start_token(M),

properties : M.properties.map(function(prop){
var key = prop.key;
var name = key.type == "Identifier" ? key.name : key.value;
var args = {
start : my_start_token(key),
end : my_end_token(prop.value),
key : name,
value : from_moz(prop.value)
};
switch (prop.kind) {
case "init":
return new AST_ObjectKeyVal(args);
case "set":
args.value.name = from_moz(key);
return new AST_ObjectSetter(args);
case "get":
args.value.name = from_moz(key);
return new AST_ObjectGetter(args);
}
prop.type = "Property";
return from_moz(prop)
})
});
},
SequenceExpression : function(M) {
SequenceExpression: function(M) {
return AST_Seq.from_array(M.expressions.map(from_moz));
},
MemberExpression : function(M) {
MemberExpression: function(M) {
return new (M.computed ? AST_Sub : AST_Dot)({

@@ -106,3 +121,3 @@ start : my_start_token(M),

},
SwitchCase : function(M) {
SwitchCase: function(M) {
return new (M.test ? AST_Case : AST_Default)({

@@ -115,3 +130,10 @@ start : my_start_token(M),

},
Literal : function(M) {
VariableDeclaration: function(M) {
return new (M.kind === "const" ? AST_Const : AST_Var)({
start : my_start_token(M),
end : my_end_token(M),
definitions : M.declarations.map(from_moz)
});
},
Literal: function(M) {
var val = M.value, args = {

@@ -136,8 +158,5 @@ start : my_start_token(M),

},
UnaryExpression: From_Moz_Unary,
UpdateExpression: From_Moz_Unary,
Identifier: function(M) {
var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2];
return new (M.name == "this" ? AST_This
: p.type == "LabeledStatement" ? AST_Label
return new ( p.type == "LabeledStatement" ? AST_Label
: p.type == "VariableDeclarator" && p.id === M ? (p.kind == "const" ? AST_SymbolConst : AST_SymbolVar)

@@ -156,3 +175,4 @@ : p.type == "FunctionExpression" ? (p.id === M ? AST_SymbolLambda : AST_SymbolFunarg)

function From_Moz_Unary(M) {
MOZ_TO_ME.UpdateExpression =
MOZ_TO_ME.UnaryExpression = function To_Moz_Unary(M) {
var prefix = "prefix" in M ? M.prefix

@@ -168,10 +188,5 @@ : M.type == "UnaryExpression" ? true : false;

var ME_TO_MOZ = {};
map("Node", AST_Node);
map("Program", AST_Toplevel, "body@body");
map("Function", AST_Function, "id>name, params@argnames, body%body");
map("EmptyStatement", AST_EmptyStatement);
map("BlockStatement", AST_BlockStatement, "body@body");
map("ExpressionStatement", AST_SimpleStatement, "expression>body");
map("IfStatement", AST_If, "test>condition, consequent>body, alternate>alternative");

@@ -191,4 +206,4 @@ map("LabeledStatement", AST_LabeledStatement, "label>label, body>body");

map("FunctionDeclaration", AST_Defun, "id>name, params@argnames, body%body");
map("VariableDeclaration", AST_Var, "declarations@definitions");
map("VariableDeclarator", AST_VarDef, "id>name, init>value");
map("CatchClause", AST_Catch, "param>argname, body%body");

@@ -199,4 +214,4 @@ map("ThisExpression", AST_This);

map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right");
map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right");
map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right");
map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right");
map("ConditionalExpression", AST_Conditional, "test>condition, consequent>consequent, alternate>alternative");

@@ -206,11 +221,172 @@ map("NewExpression", AST_New, "callee>expression, arguments@args");

def_to_moz(AST_Directive, function To_Moz_Directive(M) {
return {
type: "ExpressionStatement",
expression: {
type: "Literal",
value: M.value
}
};
});
def_to_moz(AST_SimpleStatement, function To_Moz_ExpressionStatement(M) {
return {
type: "ExpressionStatement",
expression: to_moz(M.body)
};
});
def_to_moz(AST_SwitchBranch, function To_Moz_SwitchCase(M) {
return {
type: "SwitchCase",
test: to_moz(M.expression),
consequent: M.body.map(to_moz)
};
});
def_to_moz(AST_Try, function To_Moz_TryStatement(M) {
return {
type: "TryStatement",
block: to_moz_block(M),
handler: to_moz(M.bcatch),
guardedHandlers: [],
finalizer: to_moz(M.bfinally)
};
});
def_to_moz(AST_Catch, function To_Moz_CatchClause(M) {
return {
type: "CatchClause",
param: to_moz(M.argname),
guard: null,
body: to_moz_block(M)
};
});
def_to_moz(AST_Definitions, function To_Moz_VariableDeclaration(M) {
return {
type: "VariableDeclaration",
kind: M instanceof AST_Const ? "const" : "var",
declarations: M.definitions.map(to_moz)
};
});
def_to_moz(AST_Seq, function To_Moz_SequenceExpression(M) {
return {
type: "SequenceExpression",
expressions: M.to_array().map(to_moz)
};
});
def_to_moz(AST_PropAccess, function To_Moz_MemberExpression(M) {
var isComputed = M instanceof AST_Sub;
return {
type: "MemberExpression",
object: to_moz(M.expression),
computed: isComputed,
property: isComputed ? to_moz(M.property) : {type: "Identifier", name: M.property}
};
});
def_to_moz(AST_Unary, function To_Moz_Unary(M) {
return {
type: M.operator == "++" || M.operator == "--" ? "UpdateExpression" : "UnaryExpression",
operator: M.operator,
prefix: M instanceof AST_UnaryPrefix,
argument: to_moz(M.expression)
};
});
def_to_moz(AST_Binary, function To_Moz_BinaryExpression(M) {
return {
type: M.operator == "&&" || M.operator == "||" ? "LogicalExpression" : "BinaryExpression",
left: to_moz(M.left),
operator: M.operator,
right: to_moz(M.right)
};
});
def_to_moz(AST_Object, function To_Moz_ObjectExpression(M) {
return {
type: "ObjectExpression",
properties: M.properties.map(to_moz)
};
});
def_to_moz(AST_ObjectProperty, function To_Moz_Property(M) {
var key = (
is_identifier(M.key)
? {type: "Identifier", name: M.key}
: {type: "Literal", value: M.key}
);
var kind;
if (M instanceof AST_ObjectKeyVal) {
kind = "init";
} else
if (M instanceof AST_ObjectGetter) {
kind = "get";
} else
if (M instanceof AST_ObjectSetter) {
kind = "set";
}
return {
type: "Property",
kind: kind,
key: key,
value: to_moz(M.value)
};
});
def_to_moz(AST_Symbol, function To_Moz_Identifier(M) {
var def = M.definition();
return {
type: "Identifier",
name: def ? def.mangled_name || def.name : M.name
};
});
def_to_moz(AST_Constant, function To_Moz_Literal(M) {
var value = M.value;
if (typeof value === 'number' && (value < 0 || (value === 0 && 1 / value < 0))) {
return {
type: "UnaryExpression",
operator: "-",
prefix: true,
argument: {
type: "Literal",
value: -value
}
};
}
return {
type: "Literal",
value: value
};
});
def_to_moz(AST_Atom, function To_Moz_Atom(M) {
return {
type: "Identifier",
name: String(M.value)
};
});
AST_Boolean.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
AST_Null.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
AST_Hole.DEFMETHOD("to_mozilla_ast", function To_Moz_ArrayHole() { return null });
AST_Block.DEFMETHOD("to_mozilla_ast", AST_BlockStatement.prototype.to_mozilla_ast);
AST_Lambda.DEFMETHOD("to_mozilla_ast", AST_Function.prototype.to_mozilla_ast);
/* -----[ tools ]----- */
function my_start_token(moznode) {
var loc = moznode.loc;
var range = moznode.range;
return new AST_Token({
file : moznode.loc && moznode.loc.source,
line : moznode.loc && moznode.loc.start.line,
col : moznode.loc && moznode.loc.start.column,
pos : moznode.start,
endpos : moznode.start
file : loc && loc.source,
line : loc && loc.start.line,
col : loc && loc.start.column,
pos : range ? range[0] : moznode.start,
endpos : range ? range[0] : moznode.start
});

@@ -220,8 +396,10 @@ };

function my_end_token(moznode) {
var loc = moznode.loc;
var range = moznode.range;
return new AST_Token({
file : moznode.loc && moznode.loc.source,
line : moznode.loc && moznode.loc.end.line,
col : moznode.loc && moznode.loc.end.column,
pos : moznode.end,
endpos : moznode.end
file : loc && loc.source,
line : loc && loc.end.line,
col : loc && loc.end.column,
pos : range ? range[1] : moznode.end,
endpos : range ? range[1] : moznode.end
});

@@ -232,30 +410,53 @@ };

var moz_to_me = "function From_Moz_" + moztype + "(M){\n";
moz_to_me += "return new mytype({\n" +
moz_to_me += "return new " + mytype.name + "({\n" +
"start: my_start_token(M),\n" +
"end: my_end_token(M)";
var me_to_moz = "function To_Moz_" + moztype + "(M){\n";
me_to_moz += "return {\n" +
"type: " + JSON.stringify(moztype);
if (propmap) propmap.split(/\s*,\s*/).forEach(function(prop){
var m = /([a-z0-9$_]+)(=|@|>|%)([a-z0-9$_]+)/i.exec(prop);
if (!m) throw new Error("Can't understand property map: " + prop);
var moz = "M." + m[1], how = m[2], my = m[3];
var moz = m[1], how = m[2], my = m[3];
moz_to_me += ",\n" + my + ": ";
if (how == "@") {
moz_to_me += moz + ".map(from_moz)";
} else if (how == ">") {
moz_to_me += "from_moz(" + moz + ")";
} else if (how == "=") {
moz_to_me += moz;
} else if (how == "%") {
moz_to_me += "from_moz(" + moz + ").body";
} else throw new Error("Can't understand operator in propmap: " + prop);
me_to_moz += ",\n" + moz + ": ";
switch (how) {
case "@":
moz_to_me += "M." + moz + ".map(from_moz)";
me_to_moz += "M." + my + ".map(to_moz)";
break;
case ">":
moz_to_me += "from_moz(M." + moz + ")";
me_to_moz += "to_moz(M." + my + ")";
break;
case "=":
moz_to_me += "M." + moz;
me_to_moz += "M." + my;
break;
case "%":
moz_to_me += "from_moz(M." + moz + ").body";
me_to_moz += "to_moz_block(M)";
break;
default:
throw new Error("Can't understand operator in propmap: " + prop);
}
});
moz_to_me += "\n})}";
// moz_to_me = parse(moz_to_me).print_to_string({ beautify: true });
// console.log(moz_to_me);
moz_to_me += "\n})\n}";
me_to_moz += "\n}\n}";
moz_to_me = new Function("mytype", "my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")(
mytype, my_start_token, my_end_token, from_moz
//moz_to_me = parse(moz_to_me).print_to_string({ beautify: true });
//me_to_moz = parse(me_to_moz).print_to_string({ beautify: true });
//console.log(moz_to_me);
moz_to_me = new Function("my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")(
my_start_token, my_end_token, from_moz
);
return MOZ_TO_ME[moztype] = moz_to_me;
me_to_moz = new Function("to_moz", "to_moz_block", "return(" + me_to_moz + ")")(
to_moz, to_moz_block
);
MOZ_TO_ME[moztype] = moz_to_me;
def_to_moz(mytype, me_to_moz);
};

@@ -280,2 +481,44 @@

function moz_sub_loc(token) {
return token.line ? {
line: token.line,
column: token.col
} : null;
};
function set_moz_loc(mynode, moznode) {
var start = mynode.start;
var end = mynode.end;
if (start.pos != null && end.pos != null) {
moznode.range = [start.pos, end.pos];
}
if (start.line) {
moznode.loc = {
start: moz_sub_loc(start),
end: moz_sub_loc(end)
};
if (start.file) {
moznode.loc.source = start.file;
}
}
return moznode;
};
function def_to_moz(mytype, handler) {
mytype.DEFMETHOD("to_mozilla_ast", function() {
return set_moz_loc(this, handler(this));
});
};
function to_moz(node) {
return node != null ? node.to_mozilla_ast() : null;
};
function to_moz_block(node) {
return {
type: "BlockStatement",
body: node.body.map(to_moz)
};
};
})();

17

lib/output.js

@@ -437,3 +437,9 @@ /***********************************************************************

function PARENS(nodetype, func) {
nodetype.DEFMETHOD("needs_parens", func);
if (Array.isArray(nodetype)) {
nodetype.forEach(function(nodetype){
PARENS(nodetype, func);
});
} else {
nodetype.DEFMETHOD("needs_parens", func);
}
};

@@ -457,3 +463,3 @@

PARENS(AST_Unary, function(output){
PARENS([ AST_Unary, AST_Undefined ], function(output){
var p = output.parent();

@@ -554,3 +560,3 @@ return p instanceof AST_PropAccess && p.expression === this;

function assign_and_conditional_paren_rules(output) {
PARENS([ AST_Assign, AST_Conditional ], function (output){
var p = output.parent();

@@ -572,7 +578,4 @@ // !(a = false) → true

return true;
};
});
PARENS(AST_Assign, assign_and_conditional_paren_rules);
PARENS(AST_Conditional, assign_and_conditional_paren_rules);
/* -----[ PRINTERS ]----- */

@@ -579,0 +582,0 @@

@@ -612,2 +612,3 @@ /***********************************************************************

html5_comments : true,
bare_returns : false,
});

@@ -792,3 +793,3 @@

case "return":
if (S.in_function == 0)
if (S.in_function == 0 && !options.bare_returns)
croak("'return' outside of function");

@@ -795,0 +796,0 @@ return new AST_Return({

@@ -535,2 +535,3 @@ /***********************************************************************

&& (node instanceof AST_SymbolDeclaration || node instanceof AST_Label)
&& !(node instanceof AST_SymbolCatch)
&& node.unreferenced()) {

@@ -537,0 +538,0 @@ AST_Node.warn("{type} {name} is declared but not referenced [{file}:{line},{col}]", {

@@ -73,3 +73,3 @@ /***********************************************************************

orig_col = info.column;
name = info.name;
name = info.name || name;
}

@@ -76,0 +76,0 @@ generator.addMapping({

{
"name": "uglify-js",
"description": "JavaScript parser, mangler/compressor and beautifier toolkit",
"name": "uglify-js",
"description": "JavaScript parser, mangler/compressor and beautifier toolkit",
"homepage": "http://lisperator.net/uglifyjs",
"main": "tools/node.js",
"version": "2.4.15",
"version": "2.4.16",
"engines": { "node" : ">=0.4.0" },

@@ -20,5 +20,11 @@ "maintainers": [{

"source-map" : "0.1.34",
"optimist" : "~0.3.5",
"optimist": "~0.3.5",
"uglify-to-browserify": "~1.0.0"
},
"devDependencies": {
"acorn": "~0.6.0",
"escodegen": "~1.3.3",
"esfuzz": "~0.3.1",
"estraverse": "~1.5.1"
},
"browserify": {

@@ -30,3 +36,4 @@ "transform": [ "uglify-to-browserify" ]

},
"license": "BSD",
"scripts": {"test": "node test/run-tests.js"}
}

@@ -259,2 +259,6 @@ UglifyJS 2

- `keep_fargs` -- default `false`. Pass `true` to prevent the
compressor from discarding unused function arguments. You need this
for code which relies on `Function.length`.
### The `unsafe` option

@@ -404,2 +408,34 @@

### Using UglifyJS to transform SpiderMonkey AST
Now you can use UglifyJS as any other intermediate tool for transforming
JavaScript ASTs in SpiderMonkey format.
Example:
```javascript
function uglify(ast, options, mangle) {
// Conversion from SpiderMonkey AST to internal format
var uAST = UglifyJS.AST_Node.from_mozilla_ast(ast);
// Compression
uAST.figure_out_scope();
uAST = uAST.transform(UglifyJS.Compressor(options));
// Mangling (optional)
if (mangle) {
uAST.figure_out_scope();
uAST.compute_char_frequency();
uAST.mangle_names();
}
// Back-conversion to SpiderMonkey AST
return uAST.to_mozilla_ast();
}
```
Check out
[original blog post](http://rreverser.com/using-mozilla-ast-with-uglifyjs/)
for details.
API Reference

@@ -406,0 +442,0 @@ -------------

@@ -235,1 +235,81 @@ ifs_1: {

}
cond_7: {
options = {
conditionals: true,
evaluate : true
};
input: {
var x, y, z, a, b;
// compress these
if (y) {
x = 1+1;
} else {
x = 2;
}
if (y) {
x = 1+1;
} else if (z) {
x = 2;
} else {
x = 3-1;
}
x = y ? 'foo' : 'fo'+'o';
x = y ? 'foo' : y ? 'foo' : 'fo'+'o';
// Compress conditions that have side effects
if (condition()) {
x = 10+10;
} else {
x = 20;
}
if (z) {
x = 'fuji';
} else if (condition()) {
x = 'fu'+'ji';
} else {
x = 'fuji';
}
x = condition() ? 'foobar' : 'foo'+'bar';
// don't compress these
x = y ? a : b;
x = y ? 'foo' : 'fo';
}
expect: {
var x, y, z, a, b;
x = 2;
x = 2;
x = 'foo';
x = 'foo';
x = (condition(), 20);
x = z ? 'fuji' : (condition(), 'fuji');
x = (condition(), 'foobar');
x = y ? a : b;
x = y ? 'foo' : 'fo';
}
}
cond_7_1: {
options = {
conditionals: true,
evaluate : true
};
input: {
// access to global should be assumed to have side effects
if (y) {
x = 1+1;
} else {
x = 2;
}
}
expect: {
x = (y, 2);
}
}

@@ -94,5 +94,7 @@ make_sequences_1: {

input: {
var foo, x, y, bar;
foo = !(x(), y(), bar());
}
expect: {
var foo, x, y, bar;
x(), y(), foo = !bar();

@@ -105,2 +107,3 @@ }

input: {
var foo, bar;
foo.x = (foo = {}, 10);

@@ -110,2 +113,3 @@ bar = (bar = {}, 10);

expect: {
var foo, bar;
foo.x = (foo = {}, 10),

@@ -119,5 +123,7 @@ bar = {}, bar = 10;

input: {
var x, foo, bar, baz;
x = (foo(), bar(), baz()) ? 10 : 20;
}
expect: {
var x, foo, bar, baz;
foo(), bar(), x = baz() ? 10 : 20;

@@ -130,5 +136,7 @@ }

input: {
var x, foo, bar, baz;
x = (foo, bar, baz);
}
expect: {
var x, foo, bar, baz;
x = baz;

@@ -135,0 +143,0 @@ }

@@ -20,2 +20,8 @@ #! /usr/bin/env node

var run_ast_conversion_tests = require("./mozilla-ast");
run_ast_conversion_tests({
iterations: 1000
});
/* -----[ utils ]----- */

@@ -22,0 +28,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display