How to use the builders function from recast

Find comprehensive JavaScript recast.builders code examples handpicked from public code repositorys.

recast.builders is a module in Recast that provides a set of functions for building abstract syntax tree (AST) nodes.

-3
fork icon0
star icon2
watch icon0

+ 8 other calls in file

8
9
10
11
12
13
14
15
16
17
18
 */


var assert = require("assert");
var types = require("recast").types;
var isArray = types.builtInTypes.array;
var b = types.builders;
var n = types.namedTypes;
var leap = require("./leap");
var meta = require("./meta");
var util = require("./util");
fork icon0
star icon0
watch icon1

+ 4 other calls in file

How does recast.builders work?

The recast.builders module provides functions for creating AST nodes for the parsed code, and it allows generating a new modified AST. These builder functions provide a convenient way to create and manipulate the AST programatically, without dealing with the complexities of the AST structure.

Ai Example

1
2
3
4
5
6
7
const recast = require("recast");
const { variableDeclaration, variableDeclarator, identifier } =
  recast.types.builders;

const declaration = variableDeclaration("const", [
  variableDeclarator(identifier("message"), identifier("hello world")),
]);

In this example, the variableDeclaration function from recast.types.builders is used to create a new variable declaration node with const as the declaration type. A variableDeclarator node is also created with an identifier for the variable name and an identifier with the value hello world. Finally, these two nodes are passed into the variableDeclaration call to create a new AST node representing the variable declaration.