📅 You're Invited: Meet the Socket team at RSAC (April 28 – May 1).RSVP
Socket
Sign inDemoInstall
Socket

recast

Package Overview
Dependencies
Maintainers
0
Versions
268
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

recast

JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator

0.23.11
latest
Source
npm
Version published
Weekly downloads
14M
5.44%
Maintainers
0
Weekly downloads
 
Created

What is recast?

The recast npm package is a JavaScript library for parsing, transforming, and printing JavaScript code. It allows developers to manipulate the syntax tree of JavaScript code, enabling tasks such as code refactoring, code generation, and more. Recast preserves the original formatting of the code as much as possible, which is useful when modifying existing code.

What are recast's main functionalities?

Parsing JavaScript code into an Abstract Syntax Tree (AST)

This feature allows you to parse a string of JavaScript code into an AST, which can then be manipulated or analyzed.

const recast = require('recast');
const code = 'let x = 42;';
const ast = recast.parse(code);

Transforming the AST

This feature enables you to traverse and modify the AST. In this example, all 'let' declarations are changed to 'var'.

const recast = require('recast');
const ast = recast.parse('let x = 42;');
recast.types.visit(ast, {
  visitVariableDeclaration(path) {
    path.node.kind = 'var';
    return false;
  }
});
const transformedCode = recast.print(ast).code;

Printing the modified AST back to JavaScript code

After modifying the AST, this feature allows you to print it back into a formatted JavaScript code string.

const recast = require('recast');
const ast = recast.parse('let x = 42;');
// ... modify the AST ...
const modifiedCode = recast.print(ast).code;

Other packages similar to recast

Keywords

ast

FAQs

Package last updated on 03 Mar 2025

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