Skip to content

Commit

Permalink
almost finished
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianGrassin committed Mar 3, 2023
1 parent d091a45 commit e3ed534
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 10 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/nodejs.yml
@@ -0,0 +1,58 @@
# Write your workflow for CI here
name: CI

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the $default-branch branch
push:
branches: [ main ]
pull_request:
branches: [ main ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
node_version: [10.x, 12.x, 14.x, 16.x]
architecture:
- x64
name: Node ${{ matrix.node_version }} - ${{ matrix.architecture }} on ${{ matrix.os }}

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node_version }}
architecture: ${{ matrix.architecture }}
- run: npm ci
- run: npm test


# Runs a single command using the runners shell
# - name: Run a one-line script
# run: echo Hello, compilers world!

# Runs a set of commands using the runners shell
# - name: Run a multi-line script
# run: |
# echo Add other actions to build,
# echo test your lab.
# Run tests and coverage
#- name: Run tests and coverage
# run: |
# npm install jison
# npm install mocha
# npm test
# npm run cov
45 changes: 39 additions & 6 deletions README.md
Expand Up @@ -3,12 +3,17 @@

## Resumen de lo aprendido

...
En esta práctica hemos empleado conceptos nuevos como integración continua, que facilitaría el desarrollo del proyecto en caso de que se quisiese escalar mediante GitHub Actions.


-------------------------

## Indicar los valores de los argumentos

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.
Ejemplo:
<br>

*Ejemplo:* <br>

```javascript
function foo(a, b) {
Expand All @@ -31,23 +36,51 @@ function foo(a, b) {
}
foo(1, 'wut', 3);
```
<br>

-------------------------

## CLI con [Commander.js](https://www.npmjs.com/package/commander)

Se han añadido opciones -h y -V, que vienen reconocidas automáticamente con Commander, y una opción que permite especificar el fichero de salida.<br><br>

Por ejemplo con -h:
*Por ejemplo con -h:*<br>
![help](/docs/imgs/help.png)

<br>

*Con -V simplemente nos saldría el número de versión especificado en el package.json*

<br>

-------------------------

Con -V simplemente nos saldría el número de versión especificado en el package.json
## Reto 1: Soportar funciones flecha

...
Para lograr que se puedan soportar este tipo de funciones ha sido necesario modificar la función *addLogging(code)*, incluyendo en el traverse los nodos de tipo *'ArrowFunctionExpression'*. Es importante mencionar que se le tiene que pasar una versión de *ecma* que soporte este tipo de funciones. (En este caso usaremos la versión 12 del estandar).

```javascript
export function addLogging(code) {
var ast = espree.parse(code, {ecmaVersion: 12, loc: true});
estraverse.traverse(ast, { /// Con traverse nos metemos en el árbol y buscamos que tipo de node es
enter: function(node, parent) {
if (node.type === 'FunctionDeclaration' ||
node.type === 'ArrowFunctionExpression' ||
node.type === 'FunctionExpression') {
addBeforeCode(node);
}
}
});
return escodegen.generate(ast); /// Generamos el ast usando escodegen
}
```
-------------------------

## Reto 2: Añadir el número de línea

...
Usando una variable llamada *'lines'*, podemos obtener la propiedad del número de línea ya que cada nodo tiene una propiedad que contiene información sobre su localización en el código al principio del nodo. <br>
![addbeforeCode](./docs/imgs/addbefore.png)
<br>

## Tests and Covering

Expand Down
Binary file added docs/imgs/addbefore.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion package.json
Expand Up @@ -7,7 +7,15 @@
"funlog": "bin/log.js"
},
"scripts": {
"fill": "your scripts here"
"test": "mocha ./test/test.mjs",
"doc": "c8 npm test",
"exec1": "node bin/log.js ./test/data/test1.js",
"exec2": "node bin/log.js ./test/data/test2.js",
"exec3": "node bin/log.js ./test/data/test3.js",
"exec-out": "node bin/log.js ./test/data/test3.js -o ./bin/output.js",
"cov": "c8 npm run test",
"cov_report": "c8 --reporter=html --reporter=text --report-dir=docs mocha",
"jsdoc": "jsdoc2md --files src/*.js > jsdoc/README.md"
},
"dependencies": {
"acorn": "^8.8.2",
Expand Down
24 changes: 21 additions & 3 deletions src/logging-espree.js
Expand Up @@ -4,7 +4,13 @@ import * as estraverse from "estraverse";
import * as fs from "fs/promises";

export async function transpile(inputFile, outputFile) {
// Fill in the code here
let input = await fs.readFile(inputFile, 'utf-8'); // Usamos el formato utf-8 para que no nos de problemas con los caracteres especiales
let output = addLogging(input);
if (outputFile === undefined) {
console.log(output);
return;
}
await fs.writeFile(outputFile, output);
}


Expand All @@ -24,6 +30,18 @@ export function addLogging(code) {
return escodegen.generate(ast);
}

function addBeforeCode(node) {
// Fill in the code here

/**
* @desc Recibe un nodo y le añade un console.log antes de la función, incluyendo el numero de linea y
*
* @param {*} node
*/
function addBeforeCode(node, lines) {
const name = node.id ? node.id.name : '<anonymous function>';
const parameters = node.params.map(param => `\$\{${param.name}\}`);
const beforeCode = "console.log(`Entering " + name + "(" + parameters + ") at line " + lines + "`);";
const beforeNodes = espree.parse(beforeCode, {ecmaVersion:6}).body;
node.body.body = beforeNodes.concat(node.body.body);
}


0 comments on commit e3ed534

Please sign in to comment.