1

I am trying to build an analysis program for static analysis on the java script file provided to the program using esprima and estraverse. I want to make out and differentiate between the local functions defined by user and calls to the native library for Objects like 'Object.prototype() , Object.getOwnProperties()'.

    var fs = require('fs');
    var esprima = require('esprima');
    var estraverse = require('estraverse');

    var functionsStats = {}; //1
    var addStatsEntry = function(funcName) { //2
    if (!functionsStats[funcName]) {
      functionsStats[funcName] = {calls: 0, declarations:0};
    } 
    };

    var filename = process.argv[2];
    console.log('Processing', filename);
    var ast = esprima.parse(fs.readFileSync(filename));
    estraverse.traverse(ast, {
     enter: function(node) {
     if (node.type === 'FunctionDeclaration') {
       addStatsEntry(node.id.name); //4
       functionsStats[node.id.name].declarations++;
     } else if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
      addStatsEntry(node.callee.name);
      functionsStats[node.callee.name].calls++; //5
    }
   }
   });

Not sure how to differentiate the native API calls.

Any hint would help. Thanks in anticipation.

0 Answers0