{"_id":"@alu0101330105/espree-logging","_rev":"1-c49a995dc776c7f09b132e4beb306f34","name":"@alu0101330105/espree-logging","dist-tags":{"latest":"1.0.0"},"versions":{"0.3.0":{"name":"@alu0101330105/espree-logging","author":{"name":"Jorge González Delgado"},"description":"Adds logs to javascript code","type":"module","bin":{"funlog":"bin/log.js"},"scripts":{"test":"NODE_OPTIONS=--experimental-vm-modules npx jest","cov":"NODE_OPTIONS=--experimental-vm-modules npx jest --coverage; cp -r coverage/lcov-report docs; rm -rf coverage","clear":"rm -rf docs/*","doc":"npx jsdoc"},"dependencies":{"acorn":"^8.8.2","app-root-path":"^3.0.0","commander":"^10.0.0","escodegen":"^2.0.0","espree":"^9.4.1","estraverse":"^5.2.0","jest":"^29.4.3","jsdoc":"^4.0.2","underscore":"^1.12.0"},"version":"0.3.0","devDependencies":{"mocha":"^10.2.0"},"gitHead":"9e8839e52832fa7a31ffde737e1f55b04054cb73","_id":"@alu0101330105/espree-logging@0.3.0","_nodeVersion":"19.7.0","_npmVersion":"9.5.0","dist":{"integrity":"sha512-HrLm0nFnl8M2c/KSSrfoBzJvx8s4xKEqRFBFszl98OAvpmM0Ur7gBJBGd7PAL4Eh6Otb+Gjv1781mY1E+y87Ow==","shasum":"15e554aea4a50e50652b8b8412982d3bebb34a02","tarball":"https://registry.npmjs.org/@alu0101330105/espree-logging/-/espree-logging-0.3.0.tgz","fileCount":38,"unpackedSize":90311,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDJCsjqxNLfgRX2uBslF0sJ0oSYRZBRPMhYrgCr/r11bQIgZ83IdBPeMOsEOOSeCceC9FfnXUOk9DDjMss7xXA2DM0="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkAOvPACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmppBA/+IGRDA0F+DnxD/R3FV6X4aZZ9moZPeXBfZrtl5KJGbq/rI4DD\r\nIfZ/7FkC2rSIgGxM77WFnlc3aiJqNRDzNsJUINn7H3ogFkUE3lNh4LN1U3zF\r\ngUM1t+Zy0p5ayc0iFevTC5ipLRtqaxeGfLF23PphjlveF1f+e1Dlg1Y789CU\r\nn+Y6DI7xyhv/nwDpVxGx63gVUE1lw0Z9M5pGUvJxMJE+myUJgpd4/b4yUp+B\r\ncek6VhXjX98bUus5Jwl4R1xfqJEt/aZWoH2pUoR7LuaFb2526/zJkwTxiKVo\r\n0uo3Pxn+Fcc/d4reiLwM/Y0N47ePlh2mDldIwnx6B7CUymKckVbIhy6yw0NQ\r\nk3KJxiDUKLswjaNXcFRpDpD5mwg4aLg8dxlfkGwhWN9CGu1OK9IcHuphO8XU\r\nVvS/X4JS9qqvUOxQS2QCoMRC0hai+5zBPOV6wkcpGb4Pd3lJNFaSrZHrMx0s\r\nkFpEqgrNwm08tDtHNftOiDibxMnjLZICMIvVetDmVRw0IXvGioPAjMPWh7Ya\r\n9adS5E2yBAC7wcVLdUFZCMp/fNXOZFkVb0Wt3hPRaahiV17ylefs1pDCsIoe\r\npst69mEoN679h310WC9CFwHjiil7YXQCB2fggLm7aPCSMPNA0qcmCR5FaE7t\r\nUz4/KLFLoDT9L46/EbTMgBsxIjd3YATH3nY=\r\n=Ix/P\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"alu0101330105","email":"alu0101330105@ull.edu.es"},"directories":{},"maintainers":[{"name":"alu0101330105","email":"alu0101330105@ull.edu.es"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/espree-logging_0.3.0_1677781967111_0.30452629594087455"},"_hasShrinkwrap":false},"1.0.0":{"name":"@alu0101330105/espree-logging","author":{"name":"Jorge González Delgado"},"description":"Adds logs to javascript code","type":"module","bin":{"funlog":"bin/log.js"},"scripts":{"test":"NODE_OPTIONS=--experimental-vm-modules npx jest","cov":"NODE_OPTIONS=--experimental-vm-modules npx jest --coverage","clear":"rm -rf docs/*; rm -rf coverage/*","doc":"npm run cov; npx jsdoc -d docs src/*.js -R README.md; sed -i 's_<h3>Global</h3>_<h3><a href=\"../coverage/lcov-report/index.html\">Code Coverage</a></h3>_g' docs/index.html; sed -i 's_<h1>All files</h1>_<h1>All files</h1>\\n<h3><a href=\"../../docs/index.html\">Home</a></h3>_g' coverage/lcov-report/index.html;"},"dependencies":{"acorn":"^8.8.2","app-root-path":"^3.0.0","commander":"^10.0.0","escodegen":"^2.0.0","espree":"^9.4.1","estraverse":"^5.2.0","jest":"^29.4.3","jsdoc":"^4.0.2","underscore":"^1.12.0"},"version":"1.0.0","devDependencies":{"mocha":"^10.2.0"},"gitHead":"7bb188febde4c5e81b23eec1aee364ec2f8a27d4","_id":"@alu0101330105/espree-logging@1.0.0","_nodeVersion":"19.7.0","_npmVersion":"9.5.0","dist":{"integrity":"sha512-XNXLuHrr6TYg/JExEUhd1vbqXS7WyENm81PGLbjF9+yvK9IotYMigeaSzoLC8lsWZLKDffCrQKg06Qsud5vKdw==","shasum":"419185cdc70902cc5194294f1ef5606d1ca3d99c","tarball":"https://registry.npmjs.org/@alu0101330105/espree-logging/-/espree-logging-1.0.0.tgz","fileCount":63,"unpackedSize":1131974,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCI3hbrcj/n5GeGYcMLb/rId5qeiQmF62zAnn5SF9aIYgIhAJIy6ODR2OUhRgiHQEFqVjjXoGrUUUL1mh9PqfPEG9OS"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkAQ6AACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmp5Wg//cKH6QbKRaVzPTdoAzK9B03VXPT6NreSp9+3WWFpF43WoTOq1\r\nHSGCYeONytuWKWcMluWcqFX/DzI6ATqRQa93eml3DPrlbGRu3IC64c2bU2hw\r\nafTg1wUJajZtuX5z6l4T+4Z0hH88BBTO6KDtNqAl2IqwWA0FA32V72kdcDT8\r\nsztruypjwApgthhgd5UtxypJ8fEiR2JvMtqfYfZRH9Q4SerMRTSLKnjkgWFL\r\nCfOUgN9cZFO9PYoqHTbk6Fi3bWFrTYO27QhwOd6hkBWHGfihmHYmDi0oJES+\r\nxtxgeXIIgx8Xhv9X3GRvtAvcYo4k34yszzuX67bYB8tqm+5Kxq0WgGhH6T+m\r\n36VdnVfAMaVYBrhJy4wjYD5Yh7SUTsep4AyKxwEwKdvd2iBf8D97vIl25V/g\r\n0M3GiOHNgfDNN6vXL2Ski1phj/DjEEBPla2wwRBVLsvSJo1t8KQE/TB8L62o\r\ndV7LAAeB/8g3p1WE/9FOOZil7A6Z9Ap7wPAr5J8dklMgDG6rKsBAmGNZBW0h\r\n2IaQxd/J5HL4I2BJhLsi5qq0x4JzJFCT/6XdfJ4tOJlPvoB20zh19D4+B3wb\r\nscpK4SoBMmVb2L1CJ6Hosq2s5uX3gS41Mx6uAytzcywYRK4nuAwS+i3LOC9t\r\nggxNpnkTSFgUEShNij0uNK3ghji0WaWc+D4=\r\n=fgpA\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"alu0101330105","email":"alu0101330105@ull.edu.es"},"directories":{},"maintainers":[{"name":"alu0101330105","email":"alu0101330105@ull.edu.es"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/espree-logging_1.0.0_1677790848421_0.26196886796006247"},"_hasShrinkwrap":false}},"time":{"created":"2023-03-02T18:32:47.047Z","0.3.0":"2023-03-02T18:32:47.264Z","modified":"2023-03-02T21:00:48.676Z","1.0.0":"2023-03-02T21:00:48.593Z"},"maintainers":[{"name":"alu0101330105","email":"alu0101330105@ull.edu.es"}],"description":"Adds logs to javascript code","author":{"name":"Jorge González Delgado"},"readme":"[![Open in Codespaces](https://classroom.github.com/assets/launch-codespace-f4981d0f882b2a3f0472912d15f9806d57e124e0fc890972558857b51b24a6f9.svg)](https://classroom.github.com/open-in-codespaces?assignment_repo_id=10341976)\n\n# Espree Loggin\n\n## Introduction\nIn this module is an example of how to edit your code based in some rules you make. In this case what we made was introduce a log message at the start of every function to see with which params and in what order the functions are entered.\n\n## Espree\nTo make this happen we hace to use the reverse operation of the escodegen: espree.parse, a module that takes a code string and transforms it into an ast, just like we did in the previous assingments but better in every single way imaginable.\n\n```javascript\n  const ast = espree.parse(code, { ecmaVersion: 6, loc: true});\n```\nWe must also define the `ecmaVersion` for functionality and code compatibility, and we also turn on the localization, this will make so the ast stores the coordenates in the file of everything:\n```json\n\"expression\": {\n  \"type\": \"Literal\",\n  \"start\": 0,\n  \"end\": 1,\n  \"loc\": {   // ← this thing\n    \"start\": {\n      \"line\": 1,\n      \"column\": 0\n    },\n    \"end\": {\n      \"line\": 1,\n      \"column\": 1\n    }\n  },\n  \"value\": 3,\n  \"raw\": \"3\"\n}\n```\nWe will make use of them later, but first, we need a way to navigate through the ast without needing to resort to directly eddit it like a string, thats where estraverse comes in.\n\n## Estraverse\nEstraverse are the EcmaScript traversal functions from esmangle project, for short functions to traverse and edit ast easily. Using the `estraverse.traverse` function we can traverse all the nodes inside the ast and define certain behaviours on certain actions like on enter or exit.\n\nWe will define the `enter` behaviour so the code can detect when we are inside a function of some sort, so we can call the `addBeforeCode()` function:\n```js\nexport function addLogging(code) {\n  const ast = espree.parse(code, { ecmaVersion: 6, loc: true});\n  estraverse.traverse(ast, {\n    enter: function (node, parent) {\n      if (node.type === \"FunctionDeclaration\" ||\n          node.type === \"FunctionExpression\" ||\n          node.type === \"ArrowFunctionExpression\") {\n        addBeforeCode(node, parent);\n      }\n    }\n  });\n  return escodegen.generate(ast);\n}\n```\nWe do not define an exit behaviour and just return the modified ast, but How did we modified it?\n\n## Editing ast\nThe first thing we must do is obtain the information we will need from the node itself. For our aplication we will be needing the name and the parameters of the function to properly log it, easy so far, but we encounter our first problem rigth here:\n\n```js\nlet func = function(x) {\n  return x + 1;\n}\n```\nThe node that triggers our node type filter is the `FunctionExpression` wich does not have an id where we could find the name, but it definetly have a name since we could invoque it by typing `func()`. The problem is that we are looking in the wrong node, the one that actually has the function id is the parent node which is a `VariableDeclarator` with the actual function id. The way we get that id is telling the `estraverse.traverse` to give us not only the actual node but also the parent on `enter`, we just pass both nodes to the `addBeforeCode()` function and check wheter the actual node or the parent has the id, in case neither has an id (commonly arrow functions), it would be just called `<anonymous function>`.\n\nOnce we've got the name from `id.name` we algo store the `loc.start.line` to tell in which line the function is in the ORIGINAL file, since it would not match once we modify it. The last thing we need are the parameters the function was called with, since its an array we will get all of them but first add a little change to them:\n\nIn order to show a variable inside a string, we must use the backtick string:\n```js\n`this type of string\nwith multiline suport`\n```\nBecause the add support for Tagged Templates, wich is a fancy way to say that you can embed a variable inside the string:\n```js\nconst x = \"this\"\n`like ${x}`\n```\nSo we just set the parameters up so we can directly add them into a string adding them the `${` and `}`.\n\nTo modify the node we need something to modify it with, in our case a console log where we just paste all together inside and call it a day, then we parse it back into an ast node (the console.log) and stuff it rigth at the start of the function body, the `node.body.body`:\n\n```js\nfunction addBeforeCode(node, parent) {\n  let name = node.id ? node.id.name : parent.id ? parent.id.name : \"<anonymous function>\";\n  let params = [];\n  if (node.params.length > 0) {\n    params = \"${\" + node.params.map(param => param.name).join(\"}, ${\") + \"}\";\n  }\n  \n  const lineN = node.loc.start.line;\n  const beforeCode = \"console.log(`Entering \" + name + \"(\" + params + \") at line \" + lineN + \"`);\";\n  const beforeNodes = espree.parse(beforeCode, {ecmaVersion: 12}).body;\n\tnode.body.body = beforeNodes.concat(node.body.body);\n}\n```\n\n## Test Coverage and Documentation\nThe same business as always, testing with Jest like we did in the last repo (check it [here](https://github.com/ULL-ESIT-PL-2223/hello-compilers-jorge-gonzalez-delgado-alu0101330105#testing-with-jest)) and the coverage that is built inside Jest. This time we also brough the Documentation page made with JSDoc.\n\nJSDoc takes comments above functions and makes a very functional html page with all the information given in the comments check it here.","readmeFilename":"README.md"}