addLogging
Module to apply "printf() debugging"
, trought metaprograming every time you call a function
Installation
npm install addLogging --save
Usage
function addLogging (code) {
const ast = esprima.parse(code, { ecmaVersion: esprima.latestEcmaVersion, loc: true });
estraverse.traverse(ast, {
enter: function (node, parent) {
if (node.type === 'FunctionDeclaration' ||
node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression') {
addBeforeCode(node);
}
}
});
return escodegen.generate(ast);
}
function addBeforeCode(node) {
let param_name = [];
node.params.forEach(function (i) { param_name.push("${ " + i.name + " }") });
param_name = param_name.join(', ');
let name = node.id ? node.id.name : '<anonymous function>';
let beforeCode = `console.log(\`Entering ${name}(${param_name}) at line ${node.loc.start.line}\`);`;
let beforeNodes = esprima.parse(beforeCode, { ecmaVersion: 6 }).body; // Is an Array of ASTs
node.body.body = beforeNodes.concat(node.body.body);
}
console.log(addLoggin(<your-code-goes-here>));
Tests
npm test
Constributing
Try to use a consist style with the already existing code, avoid monster commits.
If you consider the contribution to be big, create a pr and add the tag [WIP] so
others can watch over the process
Release History
- 0.1.0 Initial release
Documentation
Table of Contents
addLogging
Write a literal console.log() function with information of the funcion itself
Parameters
-
code
string Code in string form
Returns any The result code in string form
addBeforeCode
Transforms the node of interest
Parameters
-
node
node node of the AST with the type of a function