UNPKG

@alu0101265704/addlogging

Version:

[![npm version](https://badge.fury.io/js/%40alu0101265704%2Faddlogging.svg)](https://badge.fury.io/js/%40alu0101265704%2Faddlogging)

99 lines (76 loc) 3.08 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: index.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: index.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>#!/usr/bin/env node const fs = require('fs'); const program = require('commander'); const { version, description } = require('../package.json'); let escodegen = require('escodegen'); let esprima = require('espree'); const espree = require('espree/lib/espree'); let estraverse = require('estraverse'); /** * Changes the initial code into other code with extra lines * @param {String} code Code from which more code will be generated * @param {String} patternName Specified pattern name to be compared with the function in * case it has an id * @returns {String} generated code with modifications */ let addLogging = function(code, patternName = '') { let ast = esprima.parse(code, { ecmaVersion: 6, loc: true}); estraverse.traverse(ast, { enter: function(node, parent) { if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') { if (!patternName || node.id &amp;&amp; node.id.name.match(patternName) || (node.id === null &amp;&amp; parent.id.name.match(patternName))) { addBeforeCode(node); } } } }); return escodegen.generate(ast); } /** * Receives a node with associated code, for it to be verified and add an * additional line straightaway once entered into the function * @param {} node */ let addBeforeCode = function(node) { const functionName = node.id ? node.id.name : '&lt;anonymous function>'; const parameters = node.params.map(param => `\$\{${param.name}\}`); let beforeCode = `console.log(\`Entering ${functionName} (${parameters})) 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); } module.exports.addLogging = addLogging;</code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#addBeforeCode">addBeforeCode</a></li><li><a href="global.html#addLogging">addLogging</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a> on Wed Mar 10 2021 13:49:30 GMT+0000 (Coordinated Universal Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>