{"id":49099002,"url":"https://github.com/crguezl/hello-ast-types","last_synced_at":"2026-04-20T22:14:50.290Z","repository":{"id":142444798,"uuid":"463487142","full_name":"crguezl/hello-ast-types","owner":"crguezl","description":null,"archived":false,"fork":false,"pushed_at":"2024-02-15T11:18:06.000Z","size":70,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-02-15T12:29:09.480Z","etag":null,"topics":["ast","ast-types","compiler","ull"],"latest_commit_sha":null,"homepage":"https://ull-esit-gradoii-pl.github.io/temas/introduccion-a-pl/master-the-art-of-the-ast.html#ast-types","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crguezl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-02-25T10:15:59.000Z","updated_at":"2024-02-15T12:29:10.381Z","dependencies_parsed_at":"2023-03-21T20:33:01.392Z","dependency_job_id":null,"html_url":"https://github.com/crguezl/hello-ast-types","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/crguezl/hello-ast-types","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crguezl%2Fhello-ast-types","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crguezl%2Fhello-ast-types/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crguezl%2Fhello-ast-types/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crguezl%2Fhello-ast-types/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crguezl","download_url":"https://codeload.github.com/crguezl/hello-ast-types/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crguezl%2Fhello-ast-types/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32067881,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-20T21:26:33.338Z","status":"ssl_error","status_checked_at":"2026-04-20T21:26:22.081Z","response_time":94,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","ast-types","compiler","ull"],"created_at":"2026-04-20T22:14:49.805Z","updated_at":"2026-04-20T22:14:50.281Z","avatar_url":"https://github.com/crguezl.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Learning ast-types \n\nThe examples here are based in the examples in \u003chttps://github.com/benjamn/ast-types\u003e\nREADME.md\n\n## index.js\n\n```\n➜  hello-ast-types git:(master) node introduction/index.js\n```\n\nThe AST built for \n\n```js\nif (foo) {\n    foo();\n}\n```\n\nis:\n\n```js\n{\n  \"test\": {\n    \"name\": \"foo\",\n    \"loc\": null,\n    \"type\": \"Identifier\",\n    \"comments\": null,\n    \"optional\": false,\n    \"typeAnnotation\": null\n  },\n  \"consequent\": {\n    \"body\": [\n      {\n        \"expression\": {\n          \"callee\": {\n            \"name\": \"foo\",\n            \"loc\": null,\n            \"type\": \"Identifier\",\n            \"comments\": null,\n            \"optional\": false,\n            \"typeAnnotation\": null\n          },\n          \"arguments\": [],\n          \"loc\": null,\n          \"type\": \"CallExpression\",\n          \"comments\": null,\n          \"optional\": false,\n          \"typeArguments\": null\n        },\n        \"loc\": null,\n        \"type\": \"ExpressionStatement\",\n        \"comments\": null\n      }\n    ],\n    \"loc\": null,\n    \"type\": \"BlockStatement\",\n    \"comments\": null,\n    \"directives\": []\n  },\n  \"alternate\": null,\n  \"loc\": null,\n  \"type\": \"IfStatement\",\n  \"comments\": null\n}\n```\n\n## visitmemberexpression.js \n\nThe 5th edition of ECMAScript (ES5) forbids the use of `arguments.callee`.\n\nThe goal of this code example: you want to detect uses of this old trick to update the code.\n\nHere is an execution of the example:\n\n```js\n➜  hello-ast-types git:(flow-parser) ✗ node visitmemberexpression.js \nInput:\n\n/* Early versions of JavaScript did not allow named function expressions, \nand for this reason you could not make a recursive function expression. \nMay be you want to detect uses of this old trick to update the code.\n*/\n\nvar fac = function(n) { return !(n \u003e 1) ? 1 : arguments.callee(n - 1) * n; }\n\n---\nWarning! 'arguments.callee' is used in this code\n```\n\nThe summarized AST for the input code \n\n```js\nvar fac = function(n) { return !(n \u003e 1) ? 1 : arguments.callee(n - 1) * n; }\n```\n\nis:\n\n```js\n✗ compast -p 'var fac = function(n) { return !(n \u003e 1) ? 1 : arguments.callee(n - 1) * n; }'\n['Program',\n  [ 'VariableDeclaration',\n    [ 'VariableDeclarator',\n      [ 'Identifier', 'fac' ],\n      [ 'FunctionExpression',\n        [ 'Identifier', 'n' ],\n        [ 'BlockStatement',\n          [ 'ReturnStatement',\n            [ 'ConditionalExpression',\n              [ 'UnaryExpression', '!',\n                [ 'BinaryExpression', '\u003e',  [ 'Identifier', 'n' ], [ 'Literal', 1 ] ]\n              ],\n              [ 'Literal', 1 ],\n              [ 'BinaryExpression', \n                '*',\n                [ 'CallExpression',\n                  [ 'MemberExpression',\n                    [ 'Identifier', 'arguments' ],  [ 'Identifier', 'callee' ]\n                  ],\n                  [ 'BinaryExpression', '-', [ 'Identifier', 'n' ], [ 'Literal', 1 ] ]\n                ],\n                [ 'Identifier', 'n' ]\n              ]\n            ]\n          ]\n        ]\n      ]\n    ]\n  ]\n]\n```\n\nIn the following code `n` is an abbreviation for the `namedTypes` object provided by `ast-types`:\n\n```js\nimport { visit, namedTypes as n, } from \"ast-types\";\n```\n\nRemmber that:\n\n1. The object `n` has a `check` method to check the type of a node.\n2. The children of a `MemberExpression` node have names `object` and `property`:\n3. \n```js\n\u003e e = require('espree')\n\u003e e.VisitorKeys.MemberExpression\n[ 'object', 'property' ]\n\u003e b = require(\"ast-types\").builder\n\u003e gfn = require(\"ast-types\").getFieldNames, null\n\u003e gfn({type: \"MemberExpression\"})\n[ 'type', 'optional', 'object', 'property', 'computed' ]\n```\n\nNotice how \n\n1. We traverse the AST visiting the `MemberExpression` nodes\n2. We check that the child `object` of the `MemberExpression` node is of type `Identifier`  and its name is `arguments`\n3. We check that the child `property` of the `MemberExpression` node is of type `Identifier`  and its name is `callee`\n\n```js\nvisit(ast, {\n  visitMemberExpression(path) {\n    var node = path.node;\n    if (\n      n.Identifier.check(node.object) \u0026\u0026 \n      node.object.name === \"arguments\" \u0026\u0026\n      n.Identifier.check(node.property)\n    ) {\n      if (node.property.name == \"callee\") console.error(\"Warning! 'arguments.callee' is used in this code\");\n    }\n    this.traverse(path);\n  }\n});\n```\n\nSee the solution in file [visit/visitmemberexpression.js](visit/visitmemberexpression.js)\n\n## spread-operator.js\n\nTranslate ES6 spread operator to older versions of JS.\n\nTransforming `...rest` parameters into browser-runnable ES5 JavaScript:\n\n\nFor the input:\n\n```js \nlet code = `\nfunction tutu(x, ...rest) {\n    return x + rest[0];\n}\nmodule.exports = tutu;\n`;\n```\n\ngives the output:\n\n```js\n➜  visit git:(master) ✗ node spread-operator.js \u003e salida.cjs\n➜  visit git:(master) ✗ cat salida.cjs \nfunction tutu(x) {\n    var rest = Array.prototype.slice.call(arguments, 1);\n    return x + rest[0];\n}\n\nmodule.exports = tutu;\n```\n\nThat we can use this way:\n\n```js\n➜  visit git:(master) ✗ node\n\u003e tutu = require(\"./salida.cjs\")\n[Function: tutu]\n\u003e tutu(2,3,4,5)\n5\n```\n\nSee the full code at [visit/spread-operator.js](visit/spread-operator.js)\n\n## check-this-usage.js\n\nImplement a function that determines if a given function node refers to `this`\n\nThis example has been modified regarding not only the usage of `super`  but also due to the fact\nthat `visitFunction` is triggered by the outer function and the original  produced an erroneous result \n\n\n```\n➜  hello-ast-types git:(master) ✗ npm run this\n\n\u003e hello-ast-types@1.0.0 this\n\u003e node check-this-usage.js\n\n\nfunction tutu() {\n    return this.prop+4;\n}\n\nInside Function visitor tutu\ninside thisexpression\ntrue\n----\n\nfunction tutu() {\n    return prop+4;\n}\n\nInside Function visitor tutu\nfalse\n----\n\nfunction tutu() {\n    function titi() {\n        return this.prop+4;\n    }\n    \n    return prop+4;\n}\n\nInside Function visitor tutu\nInside Function visitor titi\nfalse\n----\n\n  function tutu() {\n    return super();\n  }\n\nInside Function visitor tutu\ntrue\n----\n\n  function tutu() {\n    return super.meth();\n  }\n\nInside Function visitor tutu\ntrue\n----\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrguezl%2Fhello-ast-types","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrguezl%2Fhello-ast-types","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrguezl%2Fhello-ast-types/lists"}