update rename functionality

This commit is contained in:
Pavol Bielik 2015-01-26 15:39:51 +01:00
parent db76bb3c1f
commit 072e0c16e1
3 changed files with 41 additions and 11 deletions

View File

@ -119,6 +119,11 @@ function processFile(file) {
return; return;
} }
if (ARGS.print_ast){
console.log(output);
return;
}
if (!ARGS.nice_formatting) { if (!ARGS.nice_formatting) {
output = removeWhitespace(output); output = removeWhitespace(output);
} }
@ -155,10 +160,16 @@ function processFile(file) {
inferred_names[result[i].v] = result[i].inf.green; inferred_names[result[i].v] = result[i].inf.green;
} }
} }
console.log(UglifyJS.replaceMangled(code, file, inferred_names)); try {
var renamed_js = UglifyJS.replaceMangled(code, file, inferred_names);
console.log(renamed_js);
} catch (ex){
sys.error("ERROR: ".red + "failed rename '" + file + "': " + ex);
}
}, },
function(err) { function(err) {
console.log("ERROR: ".red + "connecting to server '" + HOST + ":" + PORT + "' " + err); sys.error("ERROR: ".red + "connecting to server '" + HOST + ":" + PORT + "' " + err);
}); });
} }
} }

View File

@ -57,14 +57,25 @@ function replaceMangled(code, file, infered_names) {
//replace variables with inferred names //replace variables with inferred names
stream = OutputStream({ stream = OutputStream({
beautify: true, replace_mangled: function (node) { beautify: true, replace_mangled: function (node) {
return node.definition() ? infered_names[feature_outputter.string_map.getId("$" + node.definition().id + "-" + node.name)] : node.name; var label = nodeToString(node);
if (node.definition() && feature_outputter.string_map.hasId(label) && feature_outputter.string_map.getId(label) in infered_names){
return infered_names[feature_outputter.string_map.getId(label)];
} else {
return node.name;
}
//return node.definition() ? infered_names[feature_outputter.string_map.getId("$" + node.definition().id + "-" + node.name)] : node.name;
} }
}); });
} else { } else {
//replace variables with placeholders. Using in the online demo for interactive renaming. //replace variables with placeholders. Using in the online demo for interactive renaming.
stream = OutputStream({ stream = OutputStream({
beautify: true, replace_mangled: function (node) { beautify: true, replace_mangled: function (node) {
return node.definition() ? "local$$" + feature_outputter.string_map.getId("$" + node.definition().id + "-" + node.name) : node.name; if (node.definition() && feature_outputter.string_map.hasId(nodeToString(node))){
return "local$$" + feature_outputter.string_map.getId(nodeToString(node));
} else {
return node.name;
}
//return node.definition() ? "local$$" + feature_outputter.string_map.getId("$" + node.definition().id + "-" + node.name) : node.name;
} }
}); });
} }
@ -213,13 +224,13 @@ function printAst(toplevel){
var output = ""; var output = "";
var walker = new TreeWalker(function(node){ var walker = new TreeWalker(function(node){
output += string_template(" node{id} [label=\"{label}\"];\n", { output += string_template(' node{id} [label="{label}"];\n', {
id: node.id, id: node.id,
label: nodeType(node) label: nodeType(node)
}); });
if (walker.parent() != null) { if (walker.parent() != null) {
output += string_template(" node{id1} -> node{id2} [weight=1];\n", { output += string_template(' node{id1} -> node{id2} [weight=1];\n', {
id1: walker.parent().id, id1: walker.parent().id,
id2: node.id id2: node.id
}); });
@ -383,7 +394,7 @@ NodePathFinder.prototype.find = function(node) {
/* ---[ JsonOutputter ]--- */ /* ---[ JsonOutputter ]--- */
function FeatureJsonOutputter() { function FeatureJsonOutputter() {
this.string_map = new StringMap(false); this.string_map = new StringMap();
this.first_element = true; this.first_element = true;
this.output = ""; this.output = "";
this.depth = 0; this.depth = 0;
@ -540,18 +551,26 @@ FeatureJsonOutputter.prototype.endScope = function(){
/* -----[ StringMap ]----- */ /* -----[ StringMap ]----- */
function StringMap(nice_names) { function StringMap() {
this.map = {}; this.map = {};
this.current_id = 0; this.current_id = 0;
this.nice_names = nice_names;
this.keys = []; this.keys = [];
} }
StringMap.prototype.hasId = function(input){
if (input == null){
throw new Error("error null");
}
//we add a special character in from to allow for keys such as "toString"
var escaped_input = "#" + input;
return escaped_input in this.map;
};
StringMap.prototype.getId = function(input){ StringMap.prototype.getId = function(input){
if (input == null){ if (input == null){
throw new Error("error null"); throw new Error("error null");
} }
if (this.nice_names) return input;
//we add a special character in from to allow for keys such as "toString" //we add a special character in from to allow for keys such as "toString"
var escaped_input = "#" + input; var escaped_input = "#" + input;

View File

@ -1101,7 +1101,7 @@ function OutputStream(options) {
var def = self.definition(); var def = self.definition();
// output.print_name(def ? def.mangled_name || def.name : self.name); // output.print_name(def ? def.mangled_name || def.name : self.name);
if (output.replace_mangled == null || self instanceof AST_This || self.unmangleable()) if (output.replace_mangled == null || !def || self instanceof AST_This || self.unmangleable())
//retain original name for variables that cannot be mangled //retain original name for variables that cannot be mangled
output.print_name(def ? def.mangled_name || def.name : self.name); output.print_name(def ? def.mangled_name || def.name : self.name);
else { else {