How to use the VISITOR_KEYS function from babel-types

Find comprehensive JavaScript babel-types.VISITOR_KEYS code examples handpicked from public code repositorys.

The babel-types.VISITOR_KEYS object is used to specify the child node types that a visitor function should traverse when processing an AST node in Babel.

37
38
39
40
41
42
43
44
45
46
      this.visit(node.decorators[i])
    }
  }
}

// iterate through part of t.VISITOR_KEYS
var flowFlippedAliasKeys = t.FLIPPED_ALIAS_KEYS.Flow.concat([
  'ArrayPattern',
  'ClassDeclaration',
  'ClassExpression',
fork icon0
star icon2
watch icon2

+ 3 other calls in file

238
239
240
241
242
243
244
245
246
247
 */
estraverse.VisitorKeys.ObjectPattern = ['properties']
estraverse.VisitorKeys.ArrayPattern = ['elements']
visitFunction.call(this, node)
// Set them back to normal...
estraverse.VisitorKeys.ObjectPattern = t.VISITOR_KEYS.ObjectPattern
estraverse.VisitorKeys.ArrayPattern = t.VISITOR_KEYS.ArrayPattern
if (typeParamScope) {
  this.close(node)
}
fork icon2
star icon0
watch icon1

+ 5 other calls in file

How does babel-types.VISITOR_KEYS work?

The babel-types.VISITOR_KEYS object is part of the Babel library and is used to specify the child node types that a visitor function should traverse when processing an Abstract Syntax Tree (AST) node. When working with a Babel AST, each node in the tree is represented by an object with various properties, including a type property that specifies the type of the node (e.g. FunctionDeclaration, VariableDeclaration, etc.). The babel-types.VISITOR_KEYS object is used to specify the child node types that should be visited when processing a given node type. The babel-types.VISITOR_KEYS object is an object where each key represents a node type, and the value is an array of strings that specify the child node types that should be visited when processing that node type. For example, the babel-types.VISITOR_KEYS object might look like this: javascript Copy code {{{{{{{ const VISITOR_KEYS = { FunctionDeclaration: ['id', 'params', 'body', 'returnType'], VariableDeclaration: ['declarations'], Identifier: [], ... }; This specifies that when visiting a FunctionDeclaration node, the visitor function should traverse the id, params, body, and returnType child nodes, while ignoring any child nodes of type Identifier. Similarly, when visiting a VariableDeclaration node, the visitor function should traverse only the declarations child node. By specifying the child node types to visit for each node type, the babel-types.VISITOR_KEYS object provides a flexible and efficient way to traverse and modify the nodes of a Babel AST. This can be useful for tasks such as code generation, optimization, or transformation.

265
266
267
268
269
270
271
272
273
274
// escope will traverse into them and include the identifiers within as declarations
estraverse.VisitorKeys.ObjectPattern = ["properties"];
estraverse.VisitorKeys.ArrayPattern = ["elements"];
visitFunction.call(this, node);
// set them back to normal...
estraverse.VisitorKeys.ObjectPattern = t.VISITOR_KEYS.ObjectPattern;
estraverse.VisitorKeys.ArrayPattern = t.VISITOR_KEYS.ArrayPattern;
if (typeParamScope) {
  this.close(node);
}
fork icon1
star icon0
watch icon0

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const t = require("@babel/types");

const ast = t.file(
  t.program([
    t.functionDeclaration(
      t.identifier("add"),
      [t.identifier("a"), t.identifier("b")],
      t.blockStatement([
        t.returnStatement(
          t.binaryExpression("+", t.identifier("a"), t.identifier("b"))
        ),
      ])
    ),
  ])
);

const visitor = {
  FunctionDeclaration: {
    enter(path) {
      // Add a parameter to the function
      path.node.params.push(t.identifier("c"));
    },
  },
};

const modifiedAst = t.cloneDeep(ast);
t.traverse(modifiedAst, visitor);

console.log(t.print(modifiedAst).code);

In this example, we have imported the @babel/types module and defined a simple Babel AST that represents a function declaration. We then define a visitor object with a FunctionDeclaration property that contains an enter function. This function is called when the visitor encounters a FunctionDeclaration node in the AST. Inside the function, we modify the params property of the FunctionDeclaration node by adding a new parameter with the name "c". We then use t.cloneDeep to create a copy of the original AST, and t.traverse to traverse and modify the AST using the visitor object. Finally, we output the modified AST to the console using t.print. When executed, this code will output the following to the console: javascript Copy code

Other functions in babel-types

Sorted by popularity

function icon

babel-types.identifier is the most popular function in babel-types (4076 examples)