I am also new to uglifyjs. I think this is a great library, but it lacks good documentation or examples. One use of uglifyjs, which I think is very nice, is to create a static analysis tool. For this reason, I want to share a small script that I wrote that it finds in the js file all the variables that have a small name, for example var d = 3; These variable names are not very descriptive and make maintaining large programs difficult. Therefore, I require that all variable names be at least 3 characters long. To make things a little interesting, we should not mark as flaws in the counter variables in the For..loops and For..in loops.
However, we can create this smallVariables.js.
var smallVariableNames = []; function addBadVariableName(line,value){ smallVariableNames.push(line + " "+value); } var UglifyJS = require("../tools/node"); //Please install node first var parser = UglifyJS.parser // Parse input.js file and create AST , fs = require('fs') , filename = 'input.js'; var toplevel = UglifyJS.parse(String(fs.readFileSync(filename))); var walker = new UglifyJS.TreeWalker(function(node){ //walk AST var statement = JSON.parse(JSON.stringify(node)); if (node instanceof UglifyJS.AST_VarDef || node instanceof UglifyJS.AST_Constant) { var parentForIn = walker.find_parent(UglifyJS.AST_ForIn); //in order to avoid init vars in for..in loops var parentLoopVariable =""; if (parentForIn) { parentLoopVariable = JSON.parse(JSON.stringify(parentForIn)); parentLoopVariable = parentLoopVariable["init"]; parentLoopVariable = parentLoopVariable["definitions"]; if (parentLoopVariable) parentLoopVariable = parentLoopVariable[0].name["end"].value; } var parentFor = walker.find_parent(UglifyJS.AST_For); //in order to avoid init vars in for loops if (parentFor) { parentLoopVariable = JSON.parse(JSON.stringify(parentFor)); parentLoopVariable = parentLoopVariable["init"]; parentLoopVariable = parentLoopVariable["definitions"]; if (parentLoopVariable) parentLoopVariable = parentLoopVariable[0].name["end"].value; } var expression = statement["name"]; if (expression) { variableName = node.start.value if(variableName && variableName.length < 3 && variableName !== parentLoopVariable) { addBadVariableName(node.start.line,variableName); } } } }); toplevel.walk(walker); for (line in smallVariableNames) console.log(smallVariableNames[line]);
In a few words, our script we read the input.js file, parse it, go to the tree and find all these small variables.
Ok, now we have our script, how can we test it !! I saved the script in the% UGLIFY-JS% / test / folder. The fastest way (if you have node.js and uglify.js installed), you need to run the command: * node node_modules / uglify-js / test / smallVariables.js *
Depending on your input file, you will see some line numbers and the names of small variables. Thus, you can put any rule that suits your needs. The AST tree is a very powerful tool.
I also found a useful article in the following article: http://lisperator.net/uglifyjs/walk
source share