map-ast

0.0.5 • Public • Published

map-ast

Makes it easy to transform a JavaScript AST. There are heaps of other projects trying to solve this same problem. But I think they are all either gross, don't provide variable tracking, or both. This project provides variable and hierarchy tracking while being less gross than any other project I've seen. Here's my thoughts on the ones I am aware of:

Roadmap

Replace the env parameter with a linked list of [env, parent] values

Installation

npm install map-ast

then in your app:

const map = require('map-ast')

API

map(transforms::Object, env::Object, ast::Object)

Transform node by passing it though transforms[node.type] if its defined. Otherwise it recurs into node's children and maps them. The transformer will be passed the AST node and the variable environment it lives in.

map({
  JSXElement({openingElement:{name}}, env) {
    return {
      type: 'CallExpression',
      callee: {type: 'Identifier', name: 'JSX'},
      arguments: [name.name in env
                    ? {type: 'Identifier', name: name.name}
                    : {type: 'Literal', value: name.name}]
    }
  }
}, null, parse('<a/>')) // => parse('JSX("a")')

Package Sidebar

Install

npm i map-ast

Weekly Downloads

3

Version

0.0.5

License

MIT

Last publish

Collaborators

  • jkroso