Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option for mangling privates in code gen (all members prefixed with a single underscore) #309

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ var options = {
indent_start: 0,
quote_keys: false,
space_colon: false,
inline_script: false
inline_script: false,
mangle_private: false
},
make: false,
output: true // stdout
Expand Down Expand Up @@ -181,6 +182,9 @@ out: while (args.length > 0) {
case "--ascii":
options.codegen_options.ascii_only = true;
break;
case "--mangle-private":
options.codegen_options.mangle_private = true;
break;
case "--make":
options.make = true;
break;
Expand Down
65 changes: 53 additions & 12 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ var jsp = require("./parse-js"),

/* -----[ helper for AST traversal ]----- */


var private_names = null;
function ast_walker() {

function _vardefs(defs) {
return [ this[0], MAP(defs, function(def){
var a = [ def[0] ];
Expand All @@ -81,6 +84,7 @@ function ast_walker() {
out.push(MAP(statements, walk));
return out;
};

var walkers = {
"string": function(str) {
return [ this[0], str ];
Expand Down Expand Up @@ -131,7 +135,9 @@ function ast_walker() {
return [ this[0], op, walk(lvalue), walk(rvalue) ];
},
"dot": function(expr) {
return [ this[0], walk(expr) ].concat(slice(arguments, 1));
var p = slice(arguments, 1)
private_names[p[0]] = true;
return [ this[0], walk(expr) ].concat(p);
},
"call": function(expr, args) {
return [ this[0], walk(expr), MAP(args, walk) ];
Expand Down Expand Up @@ -177,6 +183,7 @@ function ast_walker() {
},
"object": function(props) {
return [ this[0], MAP(props, function(p){
private_names[p[0]] = true;
return p.length == 2
? [ p[0], walk(p[1]) ]
: [ p[0], walk(p[1]), p[2] ]; // get/set-ter
Expand Down Expand Up @@ -402,6 +409,7 @@ function ast_add_scope(ast) {
};

function _lambda(name, args, body) {

var is_defun = this[0] == "defun";
return [ this[0], is_defun ? define(name, "defun") : name, args, with_new_scope(function(){
if (!is_defun) define(name, "lambda");
Expand Down Expand Up @@ -493,6 +501,7 @@ function ast_add_scope(ast) {
/* -----[ mangle names ]----- */

function ast_mangle(ast, options) {
private_names = {};
var w = ast_walker(), walk = w.walk, scope;
options = options || {};

Expand Down Expand Up @@ -993,6 +1002,7 @@ function ast_lift_variables(ast) {
};

function ast_squeeze(ast, options) {

options = defaults(options, {
make_seqs : true,
dead_code : true,
Expand Down Expand Up @@ -1411,14 +1421,16 @@ function to_ascii(str) {
var SPLICE_NEEDS_BRACKETS = jsp.array_to_hash([ "if", "while", "do", "for", "for-in", "with" ]);

function gen_code(ast, options) {

options = defaults(options, {
indent_start : 0,
indent_level : 4,
quote_keys : false,
space_colon : false,
beautify : false,
ascii_only : false,
inline_script: false
indent_start : 0,
indent_level : 4,
quote_keys : false,
space_colon : false,
beautify : false,
ascii_only : false,
inline_script : false,
mangle_private: false
});
var beautify = !!options.beautify;
var indentation = 0,
Expand All @@ -1432,10 +1444,36 @@ function gen_code(ast, options) {
return ret;
};

function make_name(name) {
var pid = 0,
priv = /^_([^_]|$)/,
privates = {};

function make_name(name, matching, is_key) {

if (matching && options.mangle_private && priv.test(name)) {

if (privates[name]) {
return privates[name];

} else {
var m = base54(pid++);
while(Object.prototype.hasOwnProperty.call(private_names, m)) {
m = base54(pid++);
}

return privates[name] = m;
}

if (is_key) {
return name;
}

}

name = name.toString();
if (options.ascii_only)
name = to_ascii(name);

return name;
};

Expand Down Expand Up @@ -1634,7 +1672,7 @@ function gen_code(ast, options) {
} else if (needs_parens(expr))
out = "(" + out + ")";
while (i < arguments.length)
out += "." + make_name(arguments[i++]);
out += "." + make_name(arguments[i++], true);
return out;
},
"call": function(func, args) {
Expand Down Expand Up @@ -1726,12 +1764,15 @@ function gen_code(ast, options) {
return obj_needs_parens ? "({})" : "{}";
var out = "{" + newline + with_indent(function(){
return MAP(props, function(p){

var name = make_name(p[0], true, true);

if (p.length == 3) {
// getter/setter. The name is in p[0], the arg.list in p[1][2], the
// body in p[1][3] and type ("get" / "set") in p[2].
return indent(make_function(p[0], p[1][2], p[1][3], p[2], true));
return indent(make_function(name, p[1][2], p[1][3], p[2], true));
}
var key = p[0], val = parenthesize(p[1], "seq");
var key = name, val = parenthesize(p[1], "seq");
if (options.quote_keys) {
key = encode_string(key);
} else if ((typeof key == "number" || !beautify && +key + "" == key)
Expand Down