Babel
  • Docs
  • Setup
  • Try it out
  • Videos
  • Blog
  • Donate
  • Team
  • GitHub

›Tooling Packages

Guides

  • What is Babel?
  • Usage Guide
  • Configure Babel
  • Learn ES2015
  • Upgrade to Babel 7

Config Reference

  • Config Files
  • Config Options
  • Presets
  • Plugins
  • Plugins List
  • Compiler assumptions

Presets

  • @babel/preset-env
  • @babel/preset-react
  • @babel/preset-typescript
  • @babel/preset-flow

Misc

  • Roadmap
  • Caveats
  • Features Timeline
  • FAQ
  • Editors

Integration Packages

  • @babel/cli
  • @babel/polyfill
  • @babel/plugin-transform-runtime
  • @babel/register
  • @babel/standalone

Tooling Packages

  • @babel/parser
  • @babel/core
  • @babel/generator
  • @babel/code-frame
  • @babel/runtime
  • @babel/template
  • @babel/traverse
  • @babel/types

Helper Packages

  • helper-compilation-targets
  • helper-module-imports
Edit

@babel/traverse

Install

npm install --save @babel/traverse

Usage

We can use it alongside the babel parser to traverse and update nodes:

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

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

const ast = parser.parse(code);

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

Also, we can target particular node types in the Syntax Tree

traverse(ast, {
  FunctionDeclaration: function(path) {
    path.node.id.name = "x";
  },
});

📖 Read the full docs here

← @babel/template@babel/types →
  • Install
  • Usage
Babel
Docs
Learn ES2015
Community
VideosUser ShowcaseStack OverflowSlack ChannelTwitter
More
BlogGitHub OrgGitHub RepoWebsite RepoOld 6.x SiteOld 5.x Site