Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-traverse

Package Overview
Dependencies
Maintainers
1
Versions
78
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-traverse

  • 6.0.19
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.8M
increased by50.2%
Maintainers
1
Weekly downloads
 
Created

What is babel-traverse?

The babel-traverse package is part of the Babel compiler ecosystem and provides the ability to traverse and manipulate the abstract syntax tree (AST) generated from JavaScript code. It allows developers to analyze and rewrite code programmatically, which is useful for building compilers, code transformation tools, linters, and more.

What are babel-traverse's main functionalities?

Visiting Nodes

This feature allows you to visit and manipulate nodes in the AST. In the provided code, we change all identifiers named 'n' to 'x' in a simple function.

const babel = require('@babel/core');
const traverse = require('babel-traverse').default;

const code = `function square(n) { return n * n; }`;
const ast = babel.parse(code);

traverse(ast, {
  enter(path) {
    if (path.isIdentifier({ name: 'n' })) {
      path.node.name = 'x';
    }
  }
});
console.log(babel.generate(ast).code);

Scope Manipulation

This feature involves manipulating the scope of variables. In the example, within a function declaration, the variable 'b' is renamed to 'c'.

const babel = require('@babel/core');
const traverse = require('babel-traverse').default;

const code = `let a = 2; function test() { let b = 4; }`;
const ast = babel.parse(code);

traverse(ast, {
  FunctionDeclaration(path) {
    path.scope.rename('b', 'c');
  }
});
console.log(babel.generate(ast).code);

Other packages similar to babel-traverse

FAQs

Package last updated on 03 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc