{"id":15395004,"url":"https://github.com/goto-bus-stop/transform-ast","last_synced_at":"2025-07-02T13:39:57.938Z","repository":{"id":22102304,"uuid":"95302225","full_name":"goto-bus-stop/transform-ast","owner":"goto-bus-stop","description":"transform an AST with source maps","archived":false,"fork":false,"pushed_at":"2023-06-27T12:38:25.000Z","size":72,"stargazers_count":13,"open_issues_count":8,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-18T06:08:09.160Z","etag":null,"topics":["ast"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/goto-bus-stop.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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,"publiccode":null,"codemeta":null}},"created_at":"2017-06-24T14:21:27.000Z","updated_at":"2023-09-01T07:09:51.000Z","dependencies_parsed_at":"2024-06-18T17:01:24.103Z","dependency_job_id":null,"html_url":"https://github.com/goto-bus-stop/transform-ast","commit_stats":{"total_commits":61,"total_committers":2,"mean_commits":30.5,"dds":0.06557377049180324,"last_synced_commit":"920bf44720fa52a1ee3c7c547acc400dd9c7bbfc"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/goto-bus-stop/transform-ast","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goto-bus-stop%2Ftransform-ast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goto-bus-stop%2Ftransform-ast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goto-bus-stop%2Ftransform-ast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goto-bus-stop%2Ftransform-ast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/goto-bus-stop","download_url":"https://codeload.github.com/goto-bus-stop/transform-ast/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goto-bus-stop%2Ftransform-ast/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260994442,"owners_count":23094313,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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"],"created_at":"2024-10-01T15:25:17.826Z","updated_at":"2025-07-02T13:39:57.873Z","avatar_url":"https://github.com/goto-bus-stop.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# transform-ast\n\nTransform an AST with source maps.\nBasically @substack's [falafel](https://github.com/substack/node-falafel), but based on [magic-string][].\n\n## Example\n\n```js\nvar result = require('transform-ast')(`\n  var multiply = (a, b) =\u003e {\n    return a * b\n  }\n  var add = (a, b) =\u003e a + b\n`, function (node) {\n  if (node.type === 'ArrowFunctionExpression') {\n    var params = node.params.map(function (param) { return param.getSource() })\n    if (node.body.type !== 'BlockStatement') {\n      node.body.edit.update(`{ return ${node.body.getSource()} }`)\n    }\n    node.edit.update(`function (${params.join(', ')}) ${node.body.getSource()}`)\n  }\n})\nresult.toString() === `\n  var multiply = function (a, b) {\n    return a * b\n  }\n  var add = function (a, b) { return a + b }\n`\nfs.writeFile('output.js.map', JSON.stringify(result.map))\n```\n\n## Install\n\n```bash\nnpm install --save transform-ast\n```\n\n## API\n\n### `magicString = transformAst(source, opts = {}, fn = function () {})`\n\nParse and transform a `source` string.\n`fn` will be called on each node.\nThe returned `magicString` is a [magic-string][] instance, with a `toString()` method to get the transformed string and a `.map` property to access the source map.\n\n`opts.parser` sets the parser module to use. This should be an object with a `.parse(src, opts)` function. The default is [`require('acorn-node')`](https://github.com/browserify/acorn-node).\n\nIf you already have an AST, pass it in `opts.ast`. This will skip the parse step inside `transformAst()`.\n\n```js\ntransformAst(source, { ast: parsedSource }, cb)\n```\n\n### `magicString.walk(fn)`\n\nWalk the AST again.\n`fn` will be called on each node.\n\n### `magicString.map`\n\nGenerate and return a source map.\nIf the input `source` had an inline source map comment, this will be taken into account, and the final source map will point back to the original string.\nThe source map for _only_ the changes made by transform-ast can be accessed by using [magic-string][]'s `generateMap()` method.\n\n### nodes\n\nIn addition to the usual AST node properties, each node object also has some additional methods.\nUnlike falafel, these methods live on the `.edit` property, to prevent name conflicts (such as the `update()` method and the `.update` property of a ForStatement).\nThey're still also defined on the `node`s themselves, but only if there is no naming conflict.\nIt's better to use the `.edit` property.\n\n### `node.getSource()`, `node.edit.source()`\n\nGet the source string for a node, including transformations.\n\n### `node.edit.update(string)`\n\nReplace `node` with the given string.\n\n### `node.edit.append(string)`\n\nAppend the source `string` after this node.\n\n### `node.edit.prepend(string)`\n\nPrepend the source `string` before this node.\n\n## Custom Parser\n\nYou can pass in a custom parser using the `parser` option.\nThe parser should be an object with a `parse` function that takes a string and returns an AST.\nEach AST node should have `.start` and `.end` properties indicating their position in the source string.\n\nFor example, parsing JSX using [babylon](https://github.com/babel/babylon):\n\n```js\nvar babylon = require('babylon')\nvar transform = require('transform-ast')\nvar assert = require('assert')\n\nassert.equal(transform(`\n  var el = \u003cdiv /\u003e;\n`, { parser: babylon, plugins: [ 'jsx' ] }, function (node) {\n  if (node.type === 'JSXElement') {\n    node.edit.update(JSON.stringify(node.source()))\n  }\n}).toString(), `\n  var el = \"\u003cdiv /\u003e\";\n`)\n```\n\nBut parsers for other languages too, like [tacoscript](https://tacoscript.github.io)'s parser module [horchata](https://github.com/forivall/tacoscript/tree/master/packages/horchata):\n\n```js\nvar horchata = require('horchata')\nvar transform = require('transform-ast')\nvar assert = require('assert')\n\nassert.equal(transform(`\nX = () -\u003e {\n  @prop or= 'value'\n}\nnew X\n`, { parser: horchata }, function (node) {\n  switch (node.type) {\n  case 'FunctionExpression':\n    node.edit.update('function () ' + node.body.getSource())\n  }\n}).toString(), `\nX = function () {\n  @prop or= 'value'\n}\nnew X\n`)\n```\n\n## License\n\n[MIT](./LICENSE)\n\n[magic-string]: https://github.com/rich-harris/magic-string\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoto-bus-stop%2Ftransform-ast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoto-bus-stop%2Ftransform-ast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoto-bus-stop%2Ftransform-ast/lists"}