包详细信息

map-ast

jkroso6MIT0.0.5

Makes it easy to transform a JavaScript AST

ast, transform, map, transpile

自述文件

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")')