babel/packages/babel-traverse
2018-05-14 17:15:44 -07:00
..
2018-04-17 17:52:43 -04:00
2018-04-11 09:13:38 -04:00
2017-03-25 21:46:16 -04:00
2018-05-14 17:15:44 -07:00

@babel/traverse

@babel/traverse maintains the overall tree state, and is responsible for replacing, removing, and adding nodes.

Install

$ npm install --save @babel/traverse

Usage

We can use it alongside Babylon to traverse and update nodes:

import * as babylon from "babylon";
import traverse from "@babel/traverse";

const code = `function square(n) {
  return n * n;
}`;

const ast = babylon.parse(code);

traverse(ast, {
  enter(path) {
    if (path.isIdentifier({ name: "n" })) {
      path.node.name = "x";
    }
  }
});

📖 Read the full docs here