1.0.1 • Published 1 year ago

@joseantpul/espree-logging-solution v1.0.1

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

Open in Codespaces

Práctica Espree logging

Description of the project, how to use

To install use: npm i @joseantpul/espree-logging-solution

Transpiler for JavaScript, adds console.log at the beggining of functions with information such as name of the function we are entering, line of the function and parameters passed.

To use it we just call bin/log.js passing as argument the file with the code.

Documentation

Table of Contents

Parameters

  • inputFile File File with JS code
  • outputFile File

Returns any outputFile with the code including console.log in functions, if no outputFile was specificated it will print the output

addLogging

Takes code as a string and returns an AST wiht the console.log's added at the start of functions

Parameters

Returns any AST of the code with the console.log's added

addBeforeCode

Adds console.log to the node of the function (passed by reference because its an object)

Parameters

  • node object AST node of a function

Resumen de lo aprendido

En esta práctica lo principal ha sido aprender a usar estraverse para recorrer los nodos de nuestro arbol de análisis sintáctico mediante estraverse.traverse y como gracias a esto podemos modificarlo y por consiguiente modificar el código que se genera.

Además se ha aprendido a publicar un paquete en npm.

Finalmente se ha dado un repaso a las herramientas de desarrollo que nos ayudan con nuestro trabajo como los test, cubrimiento, integración continua...

CLI con Commander.js

program
  .version(version)
  .argument("<filename>", 'file with the original code')
  .option("-o, --output <filename>", "file in which to write the output")
  .action((filename, options) => {
    transpile(filename, options.output);
  });
program.parse(process.argv);

Indicar los valores de los argumentos, soportar funciones flecha y añadir el número de línea

Se ha modificado el código de logging-espree.js para que el log también indique los valores de los argumentos que se pasaron a la función, soporte funciones flecha y añada el número de línea. Ejemplo:

function foo(a, b, c) {
  let x = 'tutu';
  let y = (function (x) { return x*x })(2);
  let z = (e => { return e +1 })(4);
  console.log(x,y,z);
}
function foo(a, b) {
    console.log(`Entering foo(${ a }, ${ b })`);
    var x = 'blah';
    var y = function (z) {
        console.log(`Entering <anonymous function>(${ z })`);
        return z + 3;
    }(2);
}
foo(1, 'wut', 3);

Código:

Para detectar Funciones flecha

if (node.type === 'FunctionDeclaration' ||
    node.type === 'ArrowFunctionExpression' ||
    node.type === 'FunctionExpression') {
    addBeforeCode(node);
}

Para añadir número de línea y parámetros

let parmNames = "";
if (node.params.length) {
    parmNames = "${" + node.params.map(param => param.name).join("}, ${") + "}";
}
const lineN = node.loc.start.line;
const beforeCode = "console.log(" + "`" + "Entering " + name + "(" + parmNames + ")" + " at line " + lineN + "`" + ");";

Tests and Covering

Se realizan los test pertinentes en test.mjs para comprobar que las funcionalidades cumplen su función correctamente, ademaś se hace un crubimiento de los test para comprobar que se testea todo el código.

-------------------|---------|----------|---------|---------|------------------- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s -------------------|---------|----------|---------|---------|------------------- All files | 96 | 92.3 | 100 | 96 |
logging-espree.js | 96 | 92.3 | 100 | 96 | 17-19
-------------------|---------|----------|---------|---------|-------------------

Publicación como paquete npm

1) Darse de alta en npm 2) Autenticarse mediante npm adduser 3) Comprobamos que estamos loggeados npm whoami 4) Cambiar el name del package.json y ponerle el scope (nuestro nombre de usuario de npm) delante 5) Publicar con npm publish --access public

Scripts en package.json

"test": "mocha test/test.mjs", "cov": "c8 npm run test", "docgen": "npx documentation build src/logging-espree.js -f md"

Github Actions

Se realiza integración continua corriendo los test jobs: steps: - run: npm run test

1.0.1

1 year ago

1.0.0

1 year ago

0.8.1

1 year ago

0.8.0

1 year ago