UNPKG

strong-trace

Version:

StrongTrace Node.js Tracer

65 lines (54 loc) 1.46 kB
"use strict"; module.exports = shouldInstrument module.exports.complexityStats = complexityStats var est = require("estraverse") // check vs: // "min_calls" // "min_nodes" // "max_scope_depth" // "min_length" function shouldInstrument(node, config, stats) { if (node == null || node.body == null) { return false } if (node.path.length === 0 || stats.depth > config.max_scope_depth) { // Node is a Program body or past the nested scope limit return false } // Calls are calls to other functions. Zero calls is a "leaf" if (stats.calls >= config.min_calls) { return true } // Nodes approximates how much this function "does" if (stats.nodes >= config.min_nodes) { return true } if (stats.nodes >= config.min_length) { return true } return false } var complexityRe = /(?:Expression|Statement)$/ function complexityStats(node) { // nodes starts at -1 because it will count itself var stats = { calls: 0, nodes: -1, length: node.range[1] - node.range[0], depth: node.path.length } est.traverse(node, { enter: function (node, parent) { if (node.type == "FunctionDeclaration" || node.type == "FunctionExpression") { return est.VisitorOption.Skip } if (complexityRe.test(node.type)) { stats.nodes++ } if (node.type == "CallExpression" || node.type == "NewExpression") { stats.calls++ } } }) return stats }